head	1.9;
access;
symbols
	release_4_2:1.9
	aix_ok:1.8;
locks; strict;
comment	@# @;


1.9
date	94.03.20.03.17.53;	author aoki;	state Exp;
branches;
next	1.8;

1.8
date	93.08.16.20.34.23;	author aoki;	state Exp;
branches;
next	1.7;

1.7
date	93.08.13.03.10.32;	author aoki;	state Exp;
branches;
next	1.6;

1.6
date	93.07.19.03.36.11;	author aoki;	state Exp;
branches;
next	1.5;

1.5
date	93.02.19.01.26.10;	author aoki;	state Exp;
branches;
next	1.4;

1.4
date	93.02.18.23.58.28;	author marc;	state Exp;
branches;
next	1.3;

1.3
date	91.08.20.10.31.03;	author kemnitz;	state Exp;
branches;
next	1.2;

1.2
date	91.07.29.16.51.44;	author kemnitz;	state Exp;
branches;
next	1.1;

1.1
date	91.05.09.03.17.12;	author kemnitz;	state Exp;
branches;
next	;


desc
@Add user script - moved here from support.
@


1.9
log
@try harder to figure out user name
@
text
@#!/bin/sh
# set -x
#
# $Header: /usr/local/devel/postgres/src/bin/createuser/RCS/createuser.sh,v 1.8 1993/08/16 20:34:23 aoki Exp aoki $
#
# Note - this should NOT be setuid.
#

# ----------------
#       Set paths from environment or default values.
#       The _fUnKy_..._sTuFf_ gets set when the script is installed
#       from the default value for this build.
#       Currently the only thing we look for from the environment is
#       PGDATA, PGHOST, and PGPORT
#
# ----------------
[ -z "$PGPORT" ] && PGPORT=4321
[ -z "$PGHOST" ] && PGHOST=localhost
BINDIR=_fUnKy_BINDIR_sTuFf_
PATH=$BINDIR:$PATH

CMDNAME=`basename $0`

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;;
         *) NEWUSER=$1;;
    esac
    shift;
done

AUTHOPT="-a $AUTHSYS"
[ -z "$AUTHSYS" ] && AUTHOPT=""

MARGS="-TN $AUTHOPT -h $PGHOST -p $PGPORT"

#
# generate the first part of the actual monitor command
#

MONITOR="monitor $MARGS"

#
# see if user $USER is allowed to create new users
#

QUERY='retrieve (pg_user.usesuper) where pg_user.usename = '"\"$USER\""

ADDUSER=`$MONITOR -TN -c "$QUERY" template1`

if [ $? -ne 0 ]
then
    echo "$CMDNAME: database access failed." 1>&2
    exit 1
fi

if [ $ADDUSER != "t" ]
then
    echo "$CMDNAME: $USER cannot create users." 1>&2
    exit 1
fi

#
# get the user name of the new user.  Make sure it doesn't already exist.
#

if [ -z "$NEWUSER" ]
then
    echo _fUnKy_DASH_N_sTuFf_ "Enter name of user to add ---> "_fUnKy_BACKSLASH_C_sTuFf_
    read NEWUSER
fi

QUERY='retrieve (pg_user.usesysid) where pg_user.usename = '"\"$NEWUSER\""

RES=`$MONITOR -TN -c "$QUERY" template1`

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
    SYSID=
    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
	echo _fUnKy_DASH_N_sTuFf_ "Enter user's postgres ID$DEFMSG -> "_fUnKy_BACKSLASH_C_sTuFf_
	read SYSID
	[ -z "$SYSID" ] && SYSID=$DEFSYSID;
	QUERY='retrieve (pg_user.usename) where pg_user.usesysid = '"\"$SYSID\""'::int2'
	RES=`$MONITOR -TN -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
    done
done

#
# get the rest of the user info...
#

#
# can the user create databases?
#

yn=f

while [ "$yn" != y -a "$yn" != n ]
do
    echo _fUnKy_DASH_N_sTuFf_ "Is user \"$NEWUSER\" allowed to create databases (y/n) "_fUnKy_BACKSLASH_C_sTuFf_
    read yn
done

if [ "$yn" = y ]
then
    CANCREATE=t
else
    CANCREATE=f
fi

#
# can the user add users?
#

yn=f

while [ "$yn" != y -a "$yn" != n ]
do
    echo _fUnKy_DASH_N_sTuFf_ "Is user \"$NEWUSER\" allowed to add users? (y/n) "_fUnKy_BACKSLASH_C_sTuFf_
    read yn
done

if (test "$yn" = y)
then
    CANADDUSER=t
else
    CANADDUSER=f
fi

QUERY="append pg_user (usename=\"$NEWUSER\", usesysid=$SYSID, \
      usecreatedb=\"$CANCREATE\", usetrace=\"t\", usesuper=\"$CANADDUSER\",
      usecatupd=\"t\")"

RES=`$MONITOR -TN -c "$QUERY" template1`

#
# 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
        echo "don't forget to create a database for $NEWUSER"
    fi
fi
@


1.8
log
@whoops.  typechecking code broken createuser :-)
@
text
@d4 1
a4 1
# $Header: /home2/aoki/postgres/src/bin/createuser/RCS/createuser.sh,v 1.7 1993/08/13 03:10:32 aoki Exp aoki $
d13 1
a13 1
#       Currently the only thing wee look for from the environment is
d24 12
@


1.7
log
@strip off path from command name in messages
@
text
@d4 1
a4 1
# $Header: /home2/aoki/postgres/src/bin/createuser/RCS/createuser.sh,v 1.6 1993/07/19 03:36:11 aoki Exp aoki $
d113 1
a113 1
	QUERY='retrieve (pg_user.usename) where pg_user.usesysid = '"\"$SYSID\""
@


1.6
log
@echo -n cruft
@
text
@d4 1
a4 1
# $Header: /faerie/hpux/postgres/src/bin/createuser/RCS/createuser.sh,v 1.5 1993/02/19 01:26:10 aoki Exp aoki $
d22 2
d56 1
a56 1
    echo "$0: database access failed." 1>&2
d62 1
a62 1
    echo "$0: $USER cannot create users." 1>&2
d82 1
a82 1
    echo "$0: database access failed." 1>&2
d88 1
a88 1
    echo "$0: user "\"$NEWUSER\"" already exists" 1>&2
d117 1
a117 1
		echo "$0: database access failed."
d123 1
a123 1
		echo "$0: $SYSID already belongs to $RES, pick another"
d186 1
a186 1
    echo "$0: $NEWUSER was NOT added successfully"
d188 1
a188 1
    echo "$0: $NEWUSER was successfully added"
@


1.5
log
@added -a flag for kerbeos
fixed minor sh weirdnesses
@
text
@d4 1
a4 1
# $Header: /home2/aoki/postgres/src/bin/createuser/RCS/createuser.sh,v 1.4 1993/02/18 23:58:28 marc Exp aoki $
d70 1
a70 1
    echo -n "Enter name of user to add ---> "
d108 1
a108 1
	echo -n "Enter user's postgres ID$DEFMSG -> "
d141 1
a141 1
    echo -n "Is user \"$NEWUSER\" allowed to create databases (y/n) "
d160 1
a160 1
    echo -n "Is user \"$NEWUSER\" allowed to add users? (y/n) "
@


1.4
log
@cleanups
@
text
@d4 1
a4 1
# $Header: /usr/local/devel/postgres/src/bin/createuser/RCS/createuser.sh,v 1.3 1991/08/20 10:31:03 kemnitz Exp marc $
a8 2
progname=$0

d25 2
a27 1
        -h) PGHOST=$2; shift;;
d33 4
a36 1
MARGS="-TN -h $PGHOST -p $PGPORT"
d96 1
a96 1
while (test $done -ne 1)
d110 1
a110 3
	case $SYSID in "")
		SYSID=$DEFSYSID
	esac
d123 2
d126 1
@


1.3
log
@fixed problems with wrong variable name
@
text
@d4 1
a4 1
# $Header: RCS/createuser,v 1.2 91/07/29 16:51:44 kemnitz Exp Locker: kemnitz $
a8 22
if (test -n "$POSTGRESHOME")
then
    PG=$POSTGRESHOME
else
    PG=/usr/postgres
fi

#
# find monitor program
#

if (test -f "$PG/bin/monitor")
then
    MONITOR=$PG/bin/monitor
elif (test -f "MONITOR=$PG/obj*/support/monitor")
then
    MONITOR=$PG/obj*/support/monitor
else
    echo "$0: can't find the monitor program!"
	exit 1
fi

d11 12
a22 13
if (test -n "$PGPORT")
then
    port=$PGPORT
else
    port=4321
fi

if (test -n "$PGHOST")
then
    host=$PGHOST
else
    host=localhost
fi
d24 1
a24 1
while (test -n "$1")
d27 2
a28 2
        -h) host=$2; shift;;
        -p) port=$2; shift;;
d34 1
a34 1
MARGS="-TN -h $host -p $port"
d40 1
a40 1
MONITOR="$MONITOR $MARGS"
d50 1
a50 1
if (test $? -ne 0)
d52 1
a52 1
    echo "$0: database access failed."
d56 1
a56 1
if (test $ADDUSER != "t")
d58 1
a58 1
    echo "$0: $USER cannot create users."
d66 1
a66 1
if (test -z "$NEWUSER")
d76 1
a76 1
if (test $? -ne 0)
d78 1
a78 1
    echo "$0: database access failed."
d82 1
a82 1
if (test -n "$RES")
d84 1
a84 1
    echo "$0: user "\"$NEWUSER\"" already exists"
d96 4
a99 17
    echo -n "Enter the user's Postgres system id ---> "
    read SYSID

    QUERY='retrieve (pg_user.usename) where pg_user.usesysid = '"\"$SYSID\""
    RES=`$MONITOR -TN -c "$QUERY" template1`

    if (test $? -ne 0)
    then
        echo "$0: database access failed."
        exit 1
    fi

    if (test -n "$RES")
    then
        echo 
        echo "$0: $SYSID already belongs to $RES"
        echo "The system ID must be unique."
d101 2
a102 1
        done=1
d104 20
d136 1
a136 1
while (test "$yn" != y -a "$yn" != n)
d142 1
a142 1
if (test "$yn" = y)
d155 1
a155 1
while (test "$yn" != y -a "$yn" != n)
d179 1
a179 1
if (test $? -ne 0)
d184 1
a184 1
    if (test $CANCREATE = f)
@


1.2
log
@now uses pguser and pgport
@
text
@d4 1
a4 1
# $Header: RCS/createuser,v 1.1 91/05/09 03:17:12 kemnitz Exp Locker: kemnitz $
d9 1
a9 1
if (test -z "$POSTGRESHOME")
d184 1
a184 1
      usecreatedb=\"$CANCREATEDB\", usetrace=\"t\", usesuper=\"$CANADDUSER\",
@


1.1
log
@Initial revision
@
text
@d4 1
a4 1
# $Header: RCS/adduser,v 1.1 91/05/02 03:15:37 kemnitz Exp $
d33 13
a45 2
port=4321
host=localhost
@
