aboutsummaryrefslogtreecommitdiff
path: root/src/bin/scripts/createuser
blob: da8b5a801b67b6cdef2d81d5b19f49b6a5e44b33 (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/sh
#-------------------------------------------------------------------------
#
# createuser--
#    Utility for creating a user in the PostgreSQL database
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createuser,v 1.10 2000/06/09 15:50:53 momjian Exp $
#
# Note - this should NOT be setuid.
#
#-------------------------------------------------------------------------

CMDNAME=`basename $0`
PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`

NewUser=
SysID=
CanAddUser=
CanCreateDb=
CanCreateTab=
CanLockTab=
PwPrompt=
Password=
PSQLOPT=

# Check for echo -n vs echo \c

if echo '\c' | grep -s c >/dev/null 2>&1
then
    ECHO_N="echo -n"
    ECHO_C=""
else
    ECHO_N="echo"
    ECHO_C='\c'
fi


while [ $# -gt 0 ]
do
    case "$1" in
	--help|-\?)
		usage=t
                break
		;;
# options passed on to psql
	--host|-h)
		PSQLOPT="$PSQLOPT -h $2"
		shift;;
        -h*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --host=*)
                PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
                ;;
	--port|-p)
		PSQLOPT="$PSQLOPT -p $2"
		shift;;
        -p*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --port=*)
                PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
                ;;
# Note: These two specify the user to connect as (like in psql),
#       not the user you're creating.
	--username|-U)
		PSQLOPT="$PSQLOPT -U $2"
		shift;;
        -U*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --username=*)
                PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
                ;;
	--password|-W)
		PSQLOPT="$PSQLOPT -W"
		;;
	--echo|-e)
		PSQLOPT="$PSQLOPT -e"
		;;
	--quiet|-q)
		PSQLOPT="$PSQLOPT -o /dev/null"
		;;
# options converted into SQL command	
        --createdb|-d)
		CanCreateDb=t
		;;
        --no-createdb|-D)
		CanCreateDb=f
		;;
        --createtable|-t)
		CanCreateTab=t
		;;
        --no-createtable|-T)
		CanCreateTab=f
		;;	
        --locktable|-l)
		CanLockTab=t
		;;
        --no-locktable|-L)
		CanLockTab=f
		;;			
        --adduser|-a)
		CanAddUser=t
		;;
        --no-adduser|-A)
		CanAddUser=f
		;;
        --sysid|-i)
                SysID=$2
                shift;;
        --sysid=*)
                SysID=`echo $1 | sed 's/^--sysid=//'`
                ;;
        -i*)
                SysID=`echo $1 | sed 's/^-i//'`
                ;;
	--pwprompt|--pw|-P)
		PwPrompt=t
		;;
	-*)
		echo "$CMDNAME: invalid option: $1"
                echo "Try -? for help."
		exit 1
		;;
         *)
		NewUser=$1
		;;
    esac
    shift;
done

if [ "$usage" ]; then	
        echo "$CMDNAME creates a new PostgreSQL user."
        echo
	echo "Usage:"
        echo "  $CMDNAME [options] [username]"
        echo
	echo "Options:"
	echo "  -d, --createdb                  User can create new databases"
	echo "  -D, --no-createdb               User cannot create databases"
	echo "  -t, --createtable               User can create new tables"
	echo "  -T, --no-createtable            User cannot create tables"	
	echo "  -l, --locktable                 User can lock tables"
	echo "  -L, --no-locktable              User cannot lock tables"	
	echo "  -a, --adduser                   User can add new users"
	echo "  -A, --no-adduser                User cannot add new users"
	echo "  -i, --sysid=SYSID               Select sysid for new user"     
	echo "  -P, --pwprompt                  Assign a password to new user"
	echo "  -h, --host=HOSTNAME             Database server host"
	echo "  -p, --port=PORT                 Database server port"
	echo "  -U, --username=USERNAME         Username to connect as (not the one to create)"
	echo "  -W, --password                  Prompt for password to connect"
	echo "  -e, --echo                      Show the query being sent to the backend"
        echo "  -q, --quiet                     Don't write any messages"
	echo
	echo "Report bugs to <pgsql-bugs@postgresql.org>."
	exit 0
fi

if [ "$SysID" ]; then
        if [ "$SysID" != "`echo $SysID | sed 's/[^0-9]//g'`" ]; then
                echo "$CMDNAME: user sysid must be a positive number"
                exit 1
        fi
fi

# Don't want to leave the user blind if he breaks
# during password entry.

trap 'stty echo >/dev/null 2>&1' 1 2 3 15

# Get missing user attributes

if [ -z "$NewUser" ]; then
	$ECHO_N "Enter name of user to add: "$ECHO_C
	read NewUser
	[ $? -ne 0 ] && exit 1
fi

if [ "$PwPrompt" ]; then
	$ECHO_N "Enter password for user \"$NewUser\": "$ECHO_C
        stty -echo >/dev/null 2>&1
        read FirstPw
        stty echo >/dev/null 2>&1
        echo
        $ECHO_N "Enter it again: "$ECHO_C
        stty -echo >/dev/null 2>&1
        read SecondPw
        stty echo >/dev/null 2>&1
        echo
        if [ "$FirstPw" != "$SecondPw" ]; then
            echo "Passwords didn't match."
            exit 1
        fi
	Password=$FirstPw
fi

if [ -z "$CanCreateDb" ]; then
	$ECHO_N "Shall the new user be allowed to create databases? (y/n) "$ECHO_C
	read REPLY
	[ $? -ne 0 ] && exit 1
	if [ $REPLY = "y" -o $REPLY = "Y" ]; then
		CanCreateDb=t
	else
		CanCreateDb=f
	fi
fi

if [ -z "$CanAddUser" ]; then
	$ECHO_N "Shall the new user be allowed to create more new users? (y/n) "$ECHO_C
	read REPLY
	[ $? -ne 0 ] && exit 1
	if [ $REPLY = "y" -o $REPLY = "Y" ]; then
		CanAddUser=t
	else
		CanAddUser=f
	fi
fi

if [ -z "$CanCreateTab" ]; then
	$ECHO_N "Shall the new user be allowed to create tables? (y/n) "$ECHO_C
	read REPLY
	[ $? -ne 0 ] && exit 1
	if [ $REPLY = "y" -o $REPLY = "Y" ]; then
		CanCreateTab=t
	else
		CanCreateTab=f
	fi
fi

if [ -z "$CanLockTab" ]; then
	$ECHO_N "Shall the new user be allowed to lock tables? (y/n) "$ECHO_C
	read REPLY
	[ $? -ne 0 ] && exit 1
	if [ $REPLY = "y" -o $REPLY = "Y" ]; then
		CanLockTab=t
	else
		CanLockTab=f
	fi
fi

#
# build SQL command
#
NewUser=`echo $NewUser | sed 's/\"/\\\"/g'`
Password=`echo $Password | sed 's/\"/\\\"/g'`

QUERY="CREATE USER \"$NewUser\""

SUBQUERY=
[ "$SysID" ] &&    SUBQUERY="$SUBQUERY SYSID $SysID"
[ "$Password" ] && SUBQUERY="$SUBQUERY PASSWORD '$Password'"
[ "$SUBQUERY" ] &&        QUERY="$QUERY WITH $SUBQUERY"

[ "$CanCreateDb" = t ] && QUERY="$QUERY CREATEDB"
[ "$CanCreateDb" = f ] && QUERY="$QUERY NOCREATEDB"
[ "$CanAddUser" = t ] &&  QUERY="$QUERY CREATEUSER"
[ "$CanAddUser" = f ] &&  QUERY="$QUERY NOCREATEUSER"
[ "$CanCreateTab" = t ] && QUERY="$QUERY CREATETABLE"
[ "$CanCreateTab" = f ] && QUERY="$QUERY NOCREATETABLE"
[ "$CanLockTab" = t ] && QUERY="$QUERY LOCKTABLE"
[ "$CanLockTab" = f ] && QUERY="$QUERY NOLOCKTABLE"


${PATHNAME}psql -c "$QUERY" -d template1 $PSQLOPT
if [ $? -ne 0 ]; then
	echo "$CMDNAME: creation of user \"$NewUser\" failed"
	exit 1
fi
		
exit 0