OSDN Git Service

8705c53340f178308989b889546e36752795ef3c
[pg-rex/syncrep.git] / src / bin / scripts / createuser
1 #!/bin/sh
2 #-------------------------------------------------------------------------
3 #
4 # createuser--
5 #    Utility for creating a user in the PostgreSQL database
6 #
7 # Copyright (c) 1994, Regents of the University of California
8 #
9 #
10 # IDENTIFICATION
11 #    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createuser,v 1.16 2001/01/21 05:16:45 momjian Exp $
12 #
13 # Note - this should NOT be setuid.
14 #
15 #-------------------------------------------------------------------------
16
17 CMDNAME=`basename $0`
18 PATHNAME=`echo $0 | sed "s,$CMDNAME\$,,"`
19
20 NewUser=
21 SysID=
22 CanAddUser=
23 CanCreateDb=
24 PwPrompt=
25 Password=
26 PSQLOPT=
27
28 # Check for echo -n vs echo \c
29
30 if echo '\c' | grep -s c >/dev/null 2>&1
31 then
32     ECHO_N="echo -n"
33     ECHO_C=""
34 else
35     ECHO_N="echo"
36     ECHO_C='\c'
37 fi
38
39
40 while [ $# -gt 0 ]
41 do
42     case "$1" in
43         --help|-\?)
44                 usage=t
45                 break
46                 ;;
47 # options passed on to psql
48         --host|-h)
49                 PSQLOPT="$PSQLOPT -h $2"
50                 shift;;
51         -h*)
52                 PSQLOPT="$PSQLOPT $1"
53                 ;;
54         --host=*)
55                 PSQLOPT="$PSQLOPT -h "`echo $1 | sed 's/^--host=//'`
56                 ;;
57         --port|-p)
58                 PSQLOPT="$PSQLOPT -p $2"
59                 shift;;
60         -p*)
61                 PSQLOPT="$PSQLOPT $1"
62                 ;;
63         --port=*)
64                 PSQLOPT="$PSQLOPT -p "`echo $1 | sed 's/^--port=//'`
65                 ;;
66 # Note: These two specify the user to connect as (like in psql),
67 #       not the user you're creating.
68         --username|-U)
69                 PSQLOPT="$PSQLOPT -U $2"
70                 shift;;
71         -U*)
72                 PSQLOPT="$PSQLOPT $1"
73                 ;;
74         --username=*)
75                 PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--username=//'`
76                 ;;
77         --password|-W)
78                 PSQLOPT="$PSQLOPT -W"
79                 ;;
80         --echo|-e)
81                 PSQLOPT="$PSQLOPT -e"
82                 ;;
83         --quiet|-q)
84                 PSQLOPT="$PSQLOPT -o /dev/null"
85                 ;;
86 # options converted into SQL command    
87         --createdb|-d)
88                 CanCreateDb=t
89                 ;;
90         --no-createdb|-D)
91                 CanCreateDb=f
92                 ;;
93         --adduser|-a)
94                 CanAddUser=t
95                 ;;
96         --no-adduser|-A)
97                 CanAddUser=f
98                 ;;
99         --sysid|-i)
100                 SysID="$2"
101                 shift;;
102         --sysid=*)
103                 SysID=`echo "$1" | sed 's/^--sysid=//'`
104                 ;;
105         -i*)
106                 SysID=`echo "$1" | sed 's/^-i//'`
107                 ;;
108         --pwprompt|--pw|-P)
109                 PwPrompt=t
110                 ;;
111         -*)
112                 echo "$CMDNAME: invalid option: $1" 1>&2
113                 echo "Try '$CMDNAME --help' for more information." 1>&2
114                 exit 1
115                 ;;
116          *)
117                 NewUser="$1"
118                 ;;
119     esac
120     shift;
121 done
122
123 if [ "$usage" ]; then   
124         echo "$CMDNAME creates a new PostgreSQL user."
125         echo
126         echo "Usage:"
127         echo "  $CMDNAME [options] [username]"
128         echo
129         echo "Options:"
130         echo "  -d, --createdb                  User can create new databases"
131         echo "  -D, --no-createdb               User cannot create databases"
132         echo "  -a, --adduser                   User can add new users"
133         echo "  -A, --no-adduser                User cannot add new users"
134         echo "  -i, --sysid=SYSID               Select sysid for new user"     
135         echo "  -P, --pwprompt                  Assign a password to new user"
136         echo "  -h, --host=HOSTNAME             Database server host"
137         echo "  -p, --port=PORT                 Database server port"
138         echo "  -U, --username=USERNAME         Username to connect as (not the one to create)"
139         echo "  -W, --password                  Prompt for password to connect"
140         echo "  -e, --echo                      Show the query being sent to the backend"
141         echo "  -q, --quiet                     Don't write any messages"
142         echo
143         echo "If one of -d, -D, -a, -A, and 'username' is not specified, you will"
144         echo "be prompted interactively."
145         echo
146         echo "Report bugs to <pgsql-bugs@postgresql.org>."
147         exit 0
148 fi
149
150 if [ "$SysID" ]; then
151         if [ "$SysID" != "`echo $SysID | sed 's/[^0-9]//g'`" ]; then
152                 echo "$CMDNAME: user sysid must be a positive number" 1>&2
153                 exit 1
154         fi
155 fi
156
157 # Don't want to leave the user blind if he breaks
158 # during password entry.
159
160 trap 'stty echo >/dev/null 2>&1' 1 2 3 15
161
162 # Get missing user attributes
163
164 if [ -z "$NewUser" ]; then
165         $ECHO_N "Enter name of user to add: "$ECHO_C
166         read NewUser
167         [ $? -ne 0 ] && exit 1
168 fi
169
170 if [ "$PwPrompt" ]; then
171         $ECHO_N "Enter password for user \"$NewUser\": "$ECHO_C
172         stty -echo >/dev/null 2>&1
173         read FirstPw
174         stty echo >/dev/null 2>&1
175         echo
176         $ECHO_N "Enter it again: "$ECHO_C
177         stty -echo >/dev/null 2>&1
178         read SecondPw
179         stty echo >/dev/null 2>&1
180         echo
181         if [ "$FirstPw" != "$SecondPw" ]; then
182             echo "Passwords didn't match." 1>&2
183             exit 1
184         fi
185         Password="$FirstPw"
186 fi
187
188 if [ -z "$CanCreateDb" ]; then
189         $ECHO_N "Shall the new user be allowed to create databases? (y/n) "$ECHO_C
190         read REPLY
191         [ $? -ne 0 ] && exit 1
192         if [ "$REPLY" = "y" -o "$REPLY" = "Y" ]; then
193                 CanCreateDb=t
194         else
195                 CanCreateDb=f
196         fi
197 fi
198
199 if [ -z "$CanAddUser" ]; then
200         $ECHO_N "Shall the new user be allowed to create more new users? (y/n) "$ECHO_C
201         read REPLY
202         [ $? -ne 0 ] && exit 1
203         if [ "$REPLY" = "y" -o "$REPLY" = "Y" ]; then
204                 CanAddUser=t
205         else
206                 CanAddUser=f
207         fi
208 fi
209
210
211 #
212 # build SQL command
213 #
214 NewUser=`echo "$NewUser" | sed 's/\"/\\\"/g'`
215 Password=`echo "$Password" | sed 's/\"/\\\"/g'`
216
217 QUERY="CREATE USER \"$NewUser\""
218
219 SUBQUERY=
220 [ "$SysID" ] &&    SUBQUERY="$SUBQUERY SYSID $SysID"
221 [ "$Password" ] && SUBQUERY="$SUBQUERY PASSWORD '$Password'"
222 [ "$SUBQUERY" ] &&        QUERY="$QUERY WITH $SUBQUERY"
223
224 [ "$CanCreateDb" = t ] && QUERY="$QUERY CREATEDB"
225 [ "$CanCreateDb" = f ] && QUERY="$QUERY NOCREATEDB"
226 [ "$CanAddUser" = t ] &&  QUERY="$QUERY CREATEUSER"
227 [ "$CanAddUser" = f ] &&  QUERY="$QUERY NOCREATEUSER"
228
229 ${PATHNAME}psql -c "$QUERY" -d template1 $PSQLOPT
230 if [ $? -ne 0 ]; then
231         echo "$CMDNAME: creation of user \"$NewUser\" failed" 1>&2
232         exit 1
233 fi
234                 
235 exit 0