1996-07-09 08:22:35 +02:00
|
|
|
#!/bin/sh
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# createuser.sh--
|
|
|
|
# utility for creating a user in the POSTGRES database
|
|
|
|
#
|
|
|
|
# Copyright (c) 1994, Regents of the University of California
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# IDENTIFICATION
|
1999-01-31 06:04:25 +01:00
|
|
|
# $Header: /cvsroot/pgsql/src/bin/createuser/Attic/createuser.sh,v 1.11 1999/01/31 05:04:25 scrappy Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
#
|
|
|
|
# Note - this should NOT be setuid.
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
CMDNAME=`basename $0`
|
1998-08-22 07:19:35 +02:00
|
|
|
SYSID=
|
|
|
|
CANADDUSER=
|
|
|
|
CANCREATE=
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
if [ -z "$USER" ]; then
|
|
|
|
if [ -z "$LOGNAME" ]; then
|
|
|
|
if [ -z "`whoami`" ]; then
|
|
|
|
echo "$CMDNAME: cannot determine user name"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
USER=$LOGNAME
|
|
|
|
export USER
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
while [ -n "$1" ]
|
|
|
|
do
|
|
|
|
case $1 in
|
|
|
|
-a) AUTHSYS=$2; shift;;
|
|
|
|
-h) PGHOST=$2; shift;;
|
|
|
|
-p) PGPORT=$2; shift;;
|
1998-08-22 07:19:35 +02:00
|
|
|
-d) CANCREATE=t;;
|
|
|
|
-D) CANCREATE=f;;
|
|
|
|
-u) CANADDUSER=t;;
|
|
|
|
-U) CANADDUSER=f;;
|
|
|
|
-i) SYSID=$2; shift;;
|
1996-07-09 08:22:35 +02:00
|
|
|
*) NEWUSER=$1;;
|
|
|
|
esac
|
|
|
|
shift;
|
|
|
|
done
|
|
|
|
|
1996-11-17 04:55:07 +01:00
|
|
|
if [ -z "$AUTHSYS" ]; then
|
|
|
|
AUTHOPT=""
|
1996-11-14 11:25:54 +01:00
|
|
|
else
|
1996-11-17 04:55:07 +01:00
|
|
|
AUTHOPT="-a $AUTHSYS"
|
1996-11-14 11:25:54 +01:00
|
|
|
fi
|
|
|
|
|
1996-11-17 04:55:07 +01:00
|
|
|
if [ -z "$PGHOST" ]; then
|
|
|
|
PGHOSTOPT=""
|
1996-11-14 11:25:54 +01:00
|
|
|
else
|
1996-11-17 04:55:07 +01:00
|
|
|
PGHOSTOPT="-h $PGHOST"
|
1996-11-14 11:25:54 +01:00
|
|
|
fi
|
|
|
|
|
1996-11-17 04:55:07 +01:00
|
|
|
if [ -z "$PGPORT" ]; then
|
|
|
|
PGPORTOPT=""
|
1996-11-14 11:25:54 +01:00
|
|
|
else
|
1996-11-17 04:55:07 +01:00
|
|
|
PGPORTOPT="-p $PGPORT"
|
1996-11-14 11:25:54 +01:00
|
|
|
fi
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1996-11-17 04:55:07 +01:00
|
|
|
PARGS="-tq $AUTHOPT $PGHOSTOPT $PGPORTOPT"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
# generate the first part of the actual monitor command
|
|
|
|
#
|
|
|
|
|
1996-07-25 08:55:49 +02:00
|
|
|
PSQL="psql $PARGS"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
#
|
1999-01-31 06:04:25 +01:00
|
|
|
# see if user $USER is a superuser
|
1996-07-09 08:22:35 +02:00
|
|
|
#
|
|
|
|
|
1996-10-04 22:29:35 +02:00
|
|
|
QUERY="select usesuper from pg_user where usename = '$USER' "
|
1996-07-09 08:22:35 +02:00
|
|
|
#echo $QUERY
|
|
|
|
|
1996-07-25 08:55:49 +02:00
|
|
|
ADDUSER=`$PSQL -c "$QUERY" template1`
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
if [ $? -ne 0 ]
|
|
|
|
then
|
|
|
|
echo "$CMDNAME: database access failed." 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$ADDUSER" ]
|
|
|
|
then
|
|
|
|
|
|
|
|
if [ $ADDUSER != "t" ]
|
|
|
|
then
|
|
|
|
echo "$CMDNAME: $USER cannot create users." 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
#
|
|
|
|
# get the user name of the new user. Make sure it doesn't already exist.
|
|
|
|
#
|
|
|
|
|
|
|
|
if [ -z "$NEWUSER" ]
|
|
|
|
then
|
1998-08-22 07:19:35 +02:00
|
|
|
echo PG_OPT_DASH_N_PARAM "Enter name of user to add ---> PG_OPT_BACKSLASH_C_PARAM"
|
1996-07-09 08:22:35 +02:00
|
|
|
read NEWUSER
|
|
|
|
fi
|
|
|
|
|
1996-10-04 22:29:35 +02:00
|
|
|
QUERY="select usesysid from pg_user where usename = '$NEWUSER' "
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1996-07-25 08:55:49 +02:00
|
|
|
RES=`$PSQL -c "$QUERY" template1`
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
if [ $? -ne 0 ]
|
|
|
|
then
|
|
|
|
echo "$CMDNAME: database access failed." 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$RES" ]
|
|
|
|
then
|
|
|
|
echo "$CMDNAME: user "\"$NEWUSER\"" already exists" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
done=0
|
|
|
|
|
|
|
|
#
|
|
|
|
# get the system id of the new user. Make sure it is unique.
|
|
|
|
#
|
|
|
|
|
|
|
|
while [ $done -ne 1 ]
|
|
|
|
do
|
|
|
|
DEFSYSID=`pg_id $NEWUSER 2>/dev/null`
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
DEFMSG=" or RETURN to use unix user ID: $DEFSYSID"
|
|
|
|
else
|
|
|
|
DEFMSG=
|
|
|
|
DEFSYSID=
|
|
|
|
fi
|
|
|
|
while [ -z "$SYSID" ]
|
|
|
|
do
|
1998-08-22 07:19:35 +02:00
|
|
|
echo PG_OPT_DASH_N_PARAM "Enter user's postgres ID$DEFMSG -> PG_OPT_BACKSLASH_C_PARAM"
|
1996-07-09 08:22:35 +02:00
|
|
|
read SYSID
|
|
|
|
[ -z "$SYSID" ] && SYSID=$DEFSYSID;
|
|
|
|
SYSIDISNUM=`echo $SYSID | egrep '^[0-9]+$'`
|
|
|
|
if [ -z "$SYSIDISNUM" ]
|
|
|
|
then
|
|
|
|
echo "$CMDNAME: the postgres ID must be a number"
|
1998-08-22 07:19:35 +02:00
|
|
|
SYSID=
|
1996-07-09 08:22:35 +02:00
|
|
|
fi
|
|
|
|
done
|
1998-08-22 07:19:35 +02:00
|
|
|
QUERY="select usename from pg_user where usesysid = '$SYSID'::int4"
|
|
|
|
RES=`$PSQL -c "$QUERY" template1`
|
|
|
|
if [ $? -ne 0 ]
|
|
|
|
then
|
|
|
|
echo "$CMDNAME: database access failed."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ -n "$RES" ]
|
|
|
|
then
|
|
|
|
echo
|
|
|
|
echo "$CMDNAME: $SYSID already belongs to $RES, pick another"
|
|
|
|
DEFMSG= DEFSYSID= SYSID=
|
|
|
|
else
|
|
|
|
done=1
|
|
|
|
fi
|
1996-07-09 08:22:35 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
#
|
|
|
|
# get the rest of the user info...
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# can the user create databases?
|
|
|
|
#
|
1998-08-22 07:19:35 +02:00
|
|
|
if [ -z "$CANCREATE" ]
|
|
|
|
then
|
|
|
|
yn=f
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-08-22 07:19:35 +02:00
|
|
|
while [ "$yn" != y -a "$yn" != n ]
|
|
|
|
do
|
|
|
|
echo PG_OPT_DASH_N_PARAM "Is user \"$NEWUSER\" allowed to create databases (y/n) PG_OPT_BACKSLASH_C_PARAM"
|
|
|
|
read yn
|
|
|
|
done
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-08-22 07:19:35 +02:00
|
|
|
if [ "$yn" = y ]
|
|
|
|
then
|
|
|
|
CANCREATE=t
|
|
|
|
else
|
|
|
|
CANCREATE=f
|
|
|
|
fi
|
1996-07-09 08:22:35 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
#
|
|
|
|
# can the user add users?
|
|
|
|
#
|
|
|
|
|
1998-08-22 07:19:35 +02:00
|
|
|
if [ -z "$CANADDUSER" ]
|
|
|
|
then
|
|
|
|
yn=f
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-08-22 07:19:35 +02:00
|
|
|
while [ "$yn" != y -a "$yn" != n ]
|
|
|
|
do
|
1999-01-31 06:04:25 +01:00
|
|
|
echo PG_OPT_DASH_N_PARAM "Is user \"$NEWUSER\" a superuser? (y/n) PG_OPT_BACKSLASH_C_PARAM"
|
1998-08-22 07:19:35 +02:00
|
|
|
read yn
|
|
|
|
done
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-08-22 07:19:35 +02:00
|
|
|
if (test "$yn" = y)
|
|
|
|
then
|
|
|
|
CANADDUSER=t
|
|
|
|
else
|
|
|
|
CANADDUSER=f
|
|
|
|
fi
|
1996-07-09 08:22:35 +02:00
|
|
|
fi
|
|
|
|
|
1998-02-25 14:09:49 +01:00
|
|
|
QUERY="insert into pg_shadow \
|
1996-07-09 08:22:35 +02:00
|
|
|
(usename, usesysid, usecreatedb, usetrace, usesuper, usecatupd) \
|
|
|
|
values \
|
|
|
|
('$NEWUSER', $SYSID, '$CANCREATE', 't', '$CANADDUSER','t')"
|
|
|
|
|
1996-07-25 08:55:49 +02:00
|
|
|
RES=`$PSQL -c "$QUERY" template1`
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
# Wrap things up. If the user was created successfully, AND the user was
|
|
|
|
# NOT allowed to create databases, remind the DBA to create one for the user.
|
|
|
|
#
|
|
|
|
|
|
|
|
if [ $? -ne 0 ]
|
|
|
|
then
|
|
|
|
echo "$CMDNAME: $NEWUSER was NOT added successfully"
|
|
|
|
else
|
|
|
|
echo "$CMDNAME: $NEWUSER was successfully added"
|
|
|
|
if [ "$CANCREATE" = f ]
|
|
|
|
then
|
1999-01-31 06:04:25 +01:00
|
|
|
echo PG_OPT_DASH_N_PARAM "Shall I create a database for \"$NEWUSER\" (y/n) PG_OPT_BACKSLASH_C_PARAM"
|
|
|
|
read yn
|
|
|
|
|
|
|
|
if [ "$yn" = y ]
|
|
|
|
then
|
|
|
|
createdb $NEWUSER
|
|
|
|
else
|
|
|
|
echo "don't forget to create a database for $NEWUSER"
|
|
|
|
fi
|
|
|
|
fi
|
1996-07-09 08:22:35 +02:00
|
|
|
fi
|