PostgreSQL Source Code: src/bin/scripts/createuser.c Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
14
15#include <limits.h>
16
23
24
26
27int
28main(int argc, char *argv[])
29{
30 static struct option long_options[] = {
48 {"no-createrole", no_argument, NULL, 'R'},
60 {NULL, 0, NULL, 0}
61 };
62
64 int optindex;
65 int c;
66 const char *newuser = NULL;
67 char *host = NULL;
68 char *port = NULL;
75 bool echo = false;
76 bool interactive = false;
77 int conn_limit = -2;
79 char *newpassword = NULL;
80 char *pwexpiry = NULL;
81
82
90
92
95
99
101
102 while ((c = getopt_long(argc, argv, "a:c:dDeEg:h:iIlLm:p:PrRsSU:v:wW",
103 long_options, &optindex)) != -1)
104 {
105 switch (c)
106 {
107 case 'a':
109 break;
110 case 'c':
112 -1, INT_MAX, &conn_limit))
113 exit(1);
114 break;
115 case 'd':
117 break;
118 case 'D':
120 break;
121 case 'e':
122 echo = true;
123 break;
124 case 'E':
125
126 break;
127 case 'g':
129 break;
130 case 'h':
132 break;
133 case 'i':
135 break;
136 case 'I':
138 break;
139 case 'l':
141 break;
142 case 'L':
144 break;
145 case 'm':
147 break;
148 case 'p':
150 break;
151 case 'P':
153 break;
154 case 'r':
156 break;
157 case 'R':
159 break;
160 case 's':
162 break;
163 case 'S':
165 break;
166 case 'U':
168 break;
169 case 'v':
171 break;
172 case 'w':
173 prompt_password = TRI_NO;
174 break;
175 case 'W':
176 prompt_password = TRI_YES;
177 break;
178 case 1:
180 break;
181 case 2:
182 replication = TRI_NO;
183 break;
184 case 3:
185 interactive = true;
186 break;
187 case 4:
189 break;
190 case 5:
192 break;
193 default:
194
196 exit(1);
197 }
198 }
199
200 switch (argc - optind)
201 {
202 case 0:
203 break;
204 case 1:
205 newuser = argv[optind];
206 break;
207 default:
208 pg_log_error("too many command-line arguments (first is \"%s\")",
211 exit(1);
212 }
213
214 if (newuser == NULL)
215 {
216 if (interactive)
217 {
218 newuser = simple_prompt("Enter name of role to add: ", true);
219 }
220 else
221 {
222 if (getenv("PGUSER"))
223 newuser = getenv("PGUSER");
224 else
226 }
227 }
228
230 {
231 char *pw2;
232
233 newpassword = simple_prompt("Enter password for new role: ", false);
235 if (strcmp(newpassword, pw2) != 0)
236 {
237 fprintf(stderr, _("Passwords didn't match.\n"));
238 exit(1);
239 }
241 }
242
244 {
245 if (interactive && yesno_prompt("Shall the new role be a superuser?"))
247 else
249 }
250
252 {
253
256 }
257
259 {
260 if (interactive && yesno_prompt("Shall the new role be allowed to create databases?"))
262 else
264 }
265
267 {
268 if (interactive && yesno_prompt("Shall the new role be allowed to create more new roles?"))
270 else
272 }
273
276
278 replication = TRI_NO;
279
282
285
286 cparams.dbname = NULL;
287 cparams.pghost = host;
292
294
296
298
300 if (newpassword)
301 {
302 char *encrypted_password;
303
305
307 newpassword,
308 newuser,
309 NULL);
310 if (!encrypted_password)
311 pg_fatal("password encryption failed: %s",
315 }
324 if (createrole == TRI_YES)
326 if (createrole == TRI_NO)
330 if (inherit == TRI_NO)
336 if (replication == TRI_YES)
338 if (replication == TRI_NO)
340 if (bypassrls == TRI_YES)
342 if (bypassrls == TRI_NO)
344 if (conn_limit >= -1)
346 if (pwexpiry != NULL)
347 {
350 }
351 if (roles.head != NULL)
352 {
354
356
357 for (cell = roles.head; cell; cell = cell->next)
358 {
359 if (cell->next)
361 else
363 }
364 }
365 if (members.head != NULL)
366 {
368
370
371 for (cell = members.head; cell; cell = cell->next)
372 {
373 if (cell->next)
375 else
377 }
378 }
379 if (admins.head != NULL)
380 {
382
384
385 for (cell = admins.head; cell; cell = cell->next)
386 {
387 if (cell->next)
389 else
391 }
392 }
393
395
396 if (echo)
399
401 {
404 exit(1);
405 }
406
409 exit(0);
410}
411
412
413static void
415{
416 printf(_("%s creates a new PostgreSQL role.\n\n"), progname);
419 printf(_("\nOptions:\n"));
420 printf(_(" -a, --with-admin=ROLE ROLE will be a member of new role with admin\n"
421 " option\n"));
422 printf(_(" -c, --connection-limit=N connection limit for role (default: no limit)\n"));
423 printf(_(" -d, --createdb role can create new databases\n"));
424 printf(_(" -D, --no-createdb role cannot create databases (default)\n"));
425 printf(_(" -e, --echo show the commands being sent to the server\n"));
426 printf(_(" -g, --member-of=ROLE new role will be a member of ROLE\n"));
427 printf(_(" --role=ROLE (same as --member-of, deprecated)\n"));
428 printf(_(" -i, --inherit role inherits privileges of roles it is a\n"
429 " member of (default)\n"));
430 printf(_(" -I, --no-inherit role does not inherit privileges\n"));
431 printf(_(" -l, --login role can login (default)\n"));
432 printf(_(" -L, --no-login role cannot login\n"));
433 printf(_(" -m, --with-member=ROLE ROLE will be a member of new role\n"));
434 printf(_(" -P, --pwprompt assign a password to new role\n"));
435 printf(_(" -r, --createrole role can create new roles\n"));
436 printf(_(" -R, --no-createrole role cannot create roles (default)\n"));
437 printf(_(" -s, --superuser role will be superuser\n"));
438 printf(_(" -S, --no-superuser role will not be superuser (default)\n"));
439 printf(_(" -v, --valid-until=TIMESTAMP\n"
440 " password expiration date and time for role\n"));
441 printf(_(" -V, --version output version information, then exit\n"));
442 printf(_(" --interactive prompt for missing role name and attributes rather\n"
443 " than using defaults\n"));
444 printf(_(" --bypassrls role can bypass row-level security (RLS) policy\n"));
445 printf(_(" --no-bypassrls role cannot bypass row-level security (RLS) policy\n"
446 " (default)\n"));
447 printf(_(" --replication role can initiate replication\n"));
448 printf(_(" --no-replication role cannot initiate replication (default)\n"));
449 printf(_(" -?, --help show this help, then exit\n"));
450 printf(_("\nConnection options:\n"));
451 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
452 printf(_(" -p, --port=PORT database server port\n"));
453 printf(_(" -U, --username=USERNAME user name to connect as (not the one to create)\n"));
454 printf(_(" -w, --no-password never prompt for password\n"));
455 printf(_(" -W, --password force password prompt\n"));
456 printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
457 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
458}
bool yesno_prompt(const char *question)
#define PG_TEXTDOMAIN(domain)
void set_pglocale_pgservice(const char *argv0, const char *app)
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
int main(int argc, char *argv[])
static void help(const char *progname)
#define fprintf(file, fmt, msg)
Oid createdb(ParseState *pstate, const CreatedbStmt *stmt)
char * PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, const char *algorithm)
int PQclientEncoding(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
void PQfreemem(void *ptr)
ExecStatusType PQresultStatus(const PGresult *res)
void PQclear(PGresult *res)
PGresult * PQexec(PGconn *conn, const char *query)
char * pg_strdup(const char *in)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
void pg_logging_init(const char *argv0)
#define pg_log_error(...)
#define pg_log_error_hint(...)
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
PGDLLIMPORT char * optarg
const char * get_progname(const char *argv0)
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void initPQExpBuffer(PQExpBuffer str)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void simple_string_list_append(SimpleStringList *list, const char *val)
char * simple_prompt(const char *prompt, bool echo)
const char * fmtId(const char *rawid)
void setFmtEncoding(int encoding)
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
char val[FLEXIBLE_ARRAY_MEMBER]
struct SimpleStringListCell * next
SimpleStringListCell * head
enum trivalue prompt_password
const char * get_user_name_or_exit(const char *progname)