OSDN Git Service

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