Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
summaryrefslogtreecommitdiff
blob: aead7af672a6419c34d7c976fac5a0287dc8ee0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/bin/sh
#-------------------------------------------------------------------------
#
# createuser.sh--
#    utility for creating a user in the POSTGRES database
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
#    $Header: /cvsroot/pgsql/src/bin/createuser/Attic/createuser.sh,v 1.13 1999/09/27 16:44:56 momjian Exp $
#
# Note - this should NOT be setuid.
#
#-------------------------------------------------------------------------

CMDNAME=`basename $0`
SYSID=
CANADDUSER=
CANCREATE=

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;;
        -d) CANCREATE=t;;
        -D) CANCREATE=f;;
        -u) CANADDUSER=t;;
        -U) CANADDUSER=f;;
        -i) SYSID=$2; shift;;
         *) NEWUSER=$1;;
    esac
    shift;
done

if [ -z "$AUTHSYS" ]; then
  AUTHOPT=""
else
  AUTHOPT="-a $AUTHSYS"
fi

if [ -z "$PGHOST" ]; then
  PGHOSTOPT=""
else
  PGHOSTOPT="-h $PGHOST"
fi

if [ -z "$PGPORT" ]; then
  PGPORTOPT=""
else
  PGPORTOPT="-p $PGPORT"
fi

PARGS="-tq $AUTHOPT $PGHOSTOPT $PGPORTOPT"

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

PSQL="psql $PARGS"

#
# see if user $USER is a superuser
#

QUERY="select usesuper from pg_user where usename = '$USER' "
#echo $QUERY

ADDUSER=`$PSQL -c "$QUERY" template1`

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
    echo PG_OPT_DASH_N_PARAM "Enter name of user to add ---> PG_OPT_BACKSLASH_C_PARAM"
    read NEWUSER
fi

QUERY="select usesysid from pg_user where usename = '$NEWUSER' "

RES=`$PSQL -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
    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 PG_OPT_DASH_N_PARAM "Enter user's postgres ID$DEFMSG -> PG_OPT_BACKSLASH_C_PARAM"
	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"
		SYSID=	
	fi
    done
    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
done

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

#
# can the user create databases?
#
if [ -z "$CANCREATE" ]
then
	yn=f

	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

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

#
# can the user add users?
#

if [ -z "$CANADDUSER" ]
then
	yn=f

	while [ "$yn" != y -a "$yn" != n ]
	do
		echo PG_OPT_DASH_N_PARAM "Is user \"$NEWUSER\" a superuser? (y/n) PG_OPT_BACKSLASH_C_PARAM"
		read yn
	done

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

if [ "$CANCREATE" = "t" -o "$CANADDUSER" = "t" ]
then	CANCATUPD="t"
else	CANCATUPD="f"
fi

QUERY="insert into pg_shadow \
        (usename, usesysid, usecreatedb, usetrace, usesuper, usecatupd) \
       values \
         ('$NEWUSER', $SYSID, '$CANCREATE', 'f', '$CANADDUSER','$CANCATUPD')"

RES=`$PSQL -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 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
fi