home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / scripts / destroyuser < prev    next >
Encoding:
Text File  |  1992-08-27  |  3.2 KB  |  197 lines

  1. #!/bin/sh
  2. # set -x
  3. #
  4. # $Header: /private/postgres/src/scripts/RCS/destroyuser,v 1.3 1991/08/20 10:31:43 kemnitz Exp $
  5. #
  6. # Note - this should NOT be setuid.
  7. #
  8.  
  9. #
  10. # find postgres tree
  11. #
  12.  
  13. if (test -n "$POSTGRESHOME")
  14. then
  15.     PG=$POSTGRESHOME
  16. else
  17.     PG=/usr/postgres
  18. fi
  19.  
  20. #
  21. # find monitor program
  22. #
  23.  
  24. if (test -f "$PG/bin/monitor")
  25. then
  26.     MONITOR=$PG/bin/monitor
  27. elif (test -f "MONITOR=$PG/obj*/support/monitor")
  28. then
  29.     MONITOR=$PG/obj*/support/monitor
  30. else
  31.     echo "$0: can't find the monitor program!"
  32.     exit 1
  33. fi
  34.  
  35. progname=$0
  36.  
  37. if (test -n "$PGPORT")
  38. then
  39.     port=$PGPORT
  40. else
  41.     port=4321
  42. fi
  43.  
  44. if (test -n "$PGHOST")
  45. then
  46.     host=$PGHOST
  47. else
  48.     host=localhost
  49. fi
  50.  
  51. while (test -n "$1")
  52. do
  53.     case $1 in 
  54.         -h) host=$2; shift;;
  55.         -p) port=$2; shift;;
  56.          *) DELUSER=$1;;
  57.     esac
  58.     shift;
  59. done
  60.  
  61. MARGS="-TN -p $port -h $host"
  62.  
  63. #
  64. # generate the first part of the actual monitor command
  65. #
  66.  
  67. MONITOR="$MONITOR $MARGS"
  68.  
  69. #
  70. # see if user $USER is allowed to create new users.  Only a user who can
  71. # create users can delete them.
  72. #
  73.  
  74. QUERY='retrieve (pg_user.usesuper) where pg_user.usename = '"\"$USER\""
  75. ADDUSER=`$MONITOR -c "$QUERY" template1`
  76.  
  77. if (test $? -ne 0)
  78. then
  79.     echo "$0: database access failed."
  80.     exit 1
  81. fi
  82.  
  83. if (test $ADDUSER != "t")
  84. then
  85.     echo "$0: $USER cannot delete users."
  86. fi
  87.  
  88. #
  89. # get the user name of the user to delete.  Make sure it exists.
  90. #
  91.  
  92. if (test -z "$DELUSER")
  93. then
  94.     echo -n "Enter name of user to delete ---> "
  95.     read DELUSER
  96. fi
  97.  
  98. QUERY='retrieve (pg_user.usesysid) where pg_user.usename = '"\"$DELUSER\""
  99.  
  100. RES=`$MONITOR -c "$QUERY" template1`
  101.  
  102. if (test $? -ne 0)
  103. then
  104.     echo "$0: database access failed."
  105.     exit 1
  106. fi
  107.  
  108. if (test ! -n "$RES")
  109. then
  110.     echo "$0: user "\"$DELUSER\"" does not exist."
  111.     exit 1
  112. fi
  113.  
  114. SYSID=$RES
  115.  
  116. #
  117. # destroy the databases owned by the deleted user.  First, use this query
  118. # to find out what they are.
  119. #
  120.  
  121. QUERY="retrieve (pg_database.datname) where \
  122.        pg_database.datdba = \"$SYSID\"::oid"
  123.  
  124. ALLDBS=`$MONITOR -c "$QUERY" template1`
  125.  
  126. if (test $? -ne 0)
  127. then
  128.     echo "$0: database access failed - exiting..."
  129.     exit 1
  130. fi
  131.  
  132.  
  133. #
  134. # don't try to delete template1!
  135. #
  136.  
  137. for i in $ALLDBS
  138. do
  139.     if (test $i != "template1")
  140.     then
  141.         DBLIST="$DBLIST $i"
  142.     fi
  143. done
  144.  
  145. if (test -n "$DBLIST")
  146. then
  147.     echo "User $DELUSER owned the following databases:"
  148.     echo $DBLIST
  149.     echo
  150.  
  151. #
  152. # Now we warn the DBA that deleting this user will destroy a bunch of databases
  153. #
  154.  
  155.     yn=f
  156.     while (test $yn != y -a $yn != n)
  157.     do
  158.         echo -n "Deleting user $DELUSER will destroy them. Continue (y/n)? "
  159.         read yn
  160.     done
  161.  
  162.     if (test $yn = n)
  163.     then
  164.         echo "$0: exiting"
  165.         exit 1
  166.     fi
  167.  
  168.     #
  169.     # now actually destroy the databases
  170.     #
  171.  
  172.     for i in $DBLIST
  173.     do
  174.         echo "destroying database $i"
  175.  
  176.         QUERY="destroydb $i"
  177.         $MONITOR -c "$QUERY" template1
  178.         if (test $? -ne 0)
  179.         then
  180.             echo "$0: destroydb on $i failed - exiting"
  181.             exit 1
  182.         fi
  183.     done
  184. fi
  185.  
  186. QUERY="delete pg_user where pg_user.usename = \"$DELUSER\""
  187.  
  188. $MONITOR -c "$QUERY" template1
  189. if (test $? -ne 0)
  190. then
  191.     echo "$0: delete of user $DELUSER was UNSUCCESSFUL"
  192. else
  193.     echo "$0: delete of user $DELUSER was successful."
  194. fi
  195.  
  196. exit 0
  197.