PostgreSQL Source Code  git master
createuser.c File Reference
#include "postgres_fe.h"
#include <limits.h>
#include "common.h"
#include "common/logging.h"
#include "common/string.h"
#include "fe_utils/option_utils.h"
#include "fe_utils/simple_list.h"
#include "fe_utils/string_utils.h"
Include dependency graph for createuser.c:

Go to the source code of this file.

Functions

static void help (const char *progname)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ help()

static void help ( const char *  progname)
static

Definition at line 411 of file createuser.c.

412 {
413  printf(_("%s creates a new PostgreSQL role.\n\n"), progname);
414  printf(_("Usage:\n"));
415  printf(_(" %s [OPTION]... [ROLENAME]\n"), progname);
416  printf(_("\nOptions:\n"));
417  printf(_(" -a, --admin=ROLE this role will be a member of new role with admin\n"
418  " option\n"));
419  printf(_(" -c, --connection-limit=N connection limit for role (default: no limit)\n"));
420  printf(_(" -d, --createdb role can create new databases\n"));
421  printf(_(" -D, --no-createdb role cannot create databases (default)\n"));
422  printf(_(" -e, --echo show the commands being sent to the server\n"));
423  printf(_(" -g, --role=ROLE new role will be a member of this role\n"));
424  printf(_(" -i, --inherit role inherits privileges of roles it is a\n"
425  " member of (default)\n"));
426  printf(_(" -I, --no-inherit role does not inherit privileges\n"));
427  printf(_(" -l, --login role can login (default)\n"));
428  printf(_(" -L, --no-login role cannot login\n"));
429  printf(_(" -m, --member=ROLE this role will be a member of new role\n"));
430  printf(_(" -P, --pwprompt assign a password to new role\n"));
431  printf(_(" -r, --createrole role can create new roles\n"));
432  printf(_(" -R, --no-createrole role cannot create roles (default)\n"));
433  printf(_(" -s, --superuser role will be superuser\n"));
434  printf(_(" -S, --no-superuser role will not be superuser (default)\n"));
435  printf(_(" -v, --valid-until=TIMESTAMP\n"
436  " password expiration date for role\n"));
437  printf(_(" -V, --version output version information, then exit\n"));
438  printf(_(" --interactive prompt for missing role name and attributes rather\n"
439  " than using defaults\n"));
440  printf(_(" --bypassrls role can bypass row-level security (RLS) policy\n"));
441  printf(_(" --no-bypassrls role cannot bypass row-level security (RLS) policy\n"
442  " (default)\n"));
443  printf(_(" --replication role can initiate replication\n"));
444  printf(_(" --no-replication role cannot initiate replication (default)\n"));
445  printf(_(" -?, --help show this help, then exit\n"));
446  printf(_("\nConnection options:\n"));
447  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
448  printf(_(" -p, --port=PORT database server port\n"));
449  printf(_(" -U, --username=USERNAME user name to connect as (not the one to create)\n"));
450  printf(_(" -w, --no-password never prompt for password\n"));
451  printf(_(" -W, --password force password prompt\n"));
452  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
453  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
454 }
#define _(x)
Definition: elog.c:91
const char * progname
Definition: main.c:45
#define printf(...)
Definition: port.h:244

References _, printf, and progname.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 28 of file createuser.c.

29 {
30  static struct option long_options[] = {
31  {"admin", required_argument, NULL, 'a'},
32  {"connection-limit", required_argument, NULL, 'c'},
33  {"createdb", no_argument, NULL, 'd'},
34  {"no-createdb", no_argument, NULL, 'D'},
35  {"echo", no_argument, NULL, 'e'},
36  {"encrypted", no_argument, NULL, 'E'},
37  {"role", required_argument, NULL, 'g'},
38  {"host", required_argument, NULL, 'h'},
39  {"inherit", no_argument, NULL, 'i'},
40  {"no-inherit", no_argument, NULL, 'I'},
41  {"login", no_argument, NULL, 'l'},
42  {"no-login", no_argument, NULL, 'L'},
43  {"member", required_argument, NULL, 'm'},
44  {"port", required_argument, NULL, 'p'},
45  {"pwprompt", no_argument, NULL, 'P'},
46  {"createrole", no_argument, NULL, 'r'},
47  {"no-createrole", no_argument, NULL, 'R'},
48  {"superuser", no_argument, NULL, 's'},
49  {"no-superuser", no_argument, NULL, 'S'},
50  {"username", required_argument, NULL, 'U'},
51  {"valid-until", required_argument, NULL, 'v'},
52  {"no-password", no_argument, NULL, 'w'},
53  {"password", no_argument, NULL, 'W'},
54  {"replication", no_argument, NULL, 1},
55  {"no-replication", no_argument, NULL, 2},
56  {"interactive", no_argument, NULL, 3},
57  {"bypassrls", no_argument, NULL, 4},
58  {"no-bypassrls", no_argument, NULL, 5},
59  {NULL, 0, NULL, 0}
60  };
61 
62  const char *progname;
63  int optindex;
64  int c;
65  const char *newuser = NULL;
66  char *host = NULL;
67  char *port = NULL;
68  char *username = NULL;
69  SimpleStringList roles = {NULL, NULL};
70  SimpleStringList members = {NULL, NULL};
71  SimpleStringList admins = {NULL, NULL};
72  enum trivalue prompt_password = TRI_DEFAULT;
73  ConnParams cparams;
74  bool echo = false;
75  bool interactive = false;
76  int conn_limit = -2; /* less than minimum valid value */
77  bool pwprompt = false;
78  char *newpassword = NULL;
79  char *pwexpiry = NULL;
80 
81  /* Tri-valued variables. */
84  createrole = TRI_DEFAULT,
85  inherit = TRI_DEFAULT,
86  login = TRI_DEFAULT,
87  replication = TRI_DEFAULT,
88  bypassrls = TRI_DEFAULT;
89 
90  PQExpBufferData sql;
91 
92  PGconn *conn;
93  PGresult *result;
94 
95  pg_logging_init(argv[0]);
96  progname = get_progname(argv[0]);
97  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
98 
99  handle_help_version_opts(argc, argv, "createuser", help);
100 
101  while ((c = getopt_long(argc, argv, "a:c:dDeEg:h:iIlLm:p:PrRsSU:v:wW",
102  long_options, &optindex)) != -1)
103  {
104  switch (c)
105  {
106  case 'a':
108  break;
109  case 'c':
110  if (!option_parse_int(optarg, "-c/--connection-limit",
111  -1, INT_MAX, &conn_limit))
112  exit(1);
113  break;
114  case 'd':
115  createdb = TRI_YES;
116  break;
117  case 'D':
118  createdb = TRI_NO;
119  break;
120  case 'e':
121  echo = true;
122  break;
123  case 'E':
124  /* no-op, accepted for backward compatibility */
125  break;
126  case 'g':
128  break;
129  case 'h':
130  host = pg_strdup(optarg);
131  break;
132  case 'i':
133  inherit = TRI_YES;
134  break;
135  case 'I':
136  inherit = TRI_NO;
137  break;
138  case 'l':
139  login = TRI_YES;
140  break;
141  case 'L':
142  login = TRI_NO;
143  break;
144  case 'm':
146  break;
147  case 'p':
148  port = pg_strdup(optarg);
149  break;
150  case 'P':
151  pwprompt = true;
152  break;
153  case 'r':
154  createrole = TRI_YES;
155  break;
156  case 'R':
157  createrole = TRI_NO;
158  break;
159  case 's':
160  superuser = TRI_YES;
161  break;
162  case 'S':
163  superuser = TRI_NO;
164  break;
165  case 'U':
167  break;
168  case 'v':
169  pwexpiry = pg_strdup(optarg);
170  break;
171  case 'w':
172  prompt_password = TRI_NO;
173  break;
174  case 'W':
175  prompt_password = TRI_YES;
176  break;
177  case 1:
178  replication = TRI_YES;
179  break;
180  case 2:
181  replication = TRI_NO;
182  break;
183  case 3:
184  interactive = true;
185  break;
186  case 4:
187  bypassrls = TRI_YES;
188  break;
189  case 5:
190  bypassrls = TRI_NO;
191  break;
192  default:
193  /* getopt_long already emitted a complaint */
194  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
195  exit(1);
196  }
197  }
198 
199  switch (argc - optind)
200  {
201  case 0:
202  break;
203  case 1:
204  newuser = argv[optind];
205  break;
206  default:
207  pg_log_error("too many command-line arguments (first is \"%s\")",
208  argv[optind + 1]);
209  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
210  exit(1);
211  }
212 
213  if (newuser == NULL)
214  {
215  if (interactive)
216  {
217  newuser = simple_prompt("Enter name of role to add: ", true);
218  }
219  else
220  {
221  if (getenv("PGUSER"))
222  newuser = getenv("PGUSER");
223  else
224  newuser = get_user_name_or_exit(progname);
225  }
226  }
227 
228  if (pwprompt)
229  {
230  char *pw2;
231 
232  newpassword = simple_prompt("Enter password for new role: ", false);
233  pw2 = simple_prompt("Enter it again: ", false);
234  if (strcmp(newpassword, pw2) != 0)
235  {
236  fprintf(stderr, _("Passwords didn't match.\n"));
237  exit(1);
238  }
239  free(pw2);
240  }
241 
242  if (superuser == TRI_DEFAULT)
243  {
244  if (interactive && yesno_prompt("Shall the new role be a superuser?"))
245  superuser = TRI_YES;
246  else
247  superuser = TRI_NO;
248  }
249 
250  if (superuser == TRI_YES)
251  {
252  /* Not much point in trying to restrict a superuser */
253  createdb = TRI_YES;
254  createrole = TRI_YES;
255  }
256 
257  if (createdb == TRI_DEFAULT)
258  {
259  if (interactive && yesno_prompt("Shall the new role be allowed to create databases?"))
260  createdb = TRI_YES;
261  else
262  createdb = TRI_NO;
263  }
264 
265  if (createrole == TRI_DEFAULT)
266  {
267  if (interactive && yesno_prompt("Shall the new role be allowed to create more new roles?"))
268  createrole = TRI_YES;
269  else
270  createrole = TRI_NO;
271  }
272 
273  if (bypassrls == TRI_DEFAULT)
274  bypassrls = TRI_NO;
275 
276  if (replication == TRI_DEFAULT)
277  replication = TRI_NO;
278 
279  if (inherit == TRI_DEFAULT)
280  inherit = TRI_YES;
281 
282  if (login == TRI_DEFAULT)
283  login = TRI_YES;
284 
285  cparams.dbname = NULL; /* this program lacks any dbname option... */
286  cparams.pghost = host;
287  cparams.pgport = port;
288  cparams.pguser = username;
289  cparams.prompt_password = prompt_password;
290  cparams.override_dbname = NULL;
291 
292  conn = connectMaintenanceDatabase(&cparams, progname, echo);
293 
294  initPQExpBuffer(&sql);
295 
296  printfPQExpBuffer(&sql, "CREATE ROLE %s", fmtId(newuser));
297  if (newpassword)
298  {
299  char *encrypted_password;
300 
301  appendPQExpBufferStr(&sql, " PASSWORD ");
302 
303  encrypted_password = PQencryptPasswordConn(conn,
304  newpassword,
305  newuser,
306  NULL);
307  if (!encrypted_password)
308  pg_fatal("password encryption failed: %s",
310  appendStringLiteralConn(&sql, encrypted_password, conn);
311  PQfreemem(encrypted_password);
312  }
313  if (superuser == TRI_YES)
314  appendPQExpBufferStr(&sql, " SUPERUSER");
315  if (superuser == TRI_NO)
316  appendPQExpBufferStr(&sql, " NOSUPERUSER");
317  if (createdb == TRI_YES)
318  appendPQExpBufferStr(&sql, " CREATEDB");
319  if (createdb == TRI_NO)
320  appendPQExpBufferStr(&sql, " NOCREATEDB");
321  if (createrole == TRI_YES)
322  appendPQExpBufferStr(&sql, " CREATEROLE");
323  if (createrole == TRI_NO)
324  appendPQExpBufferStr(&sql, " NOCREATEROLE");
325  if (inherit == TRI_YES)
326  appendPQExpBufferStr(&sql, " INHERIT");
327  if (inherit == TRI_NO)
328  appendPQExpBufferStr(&sql, " NOINHERIT");
329  if (login == TRI_YES)
330  appendPQExpBufferStr(&sql, " LOGIN");
331  if (login == TRI_NO)
332  appendPQExpBufferStr(&sql, " NOLOGIN");
333  if (replication == TRI_YES)
334  appendPQExpBufferStr(&sql, " REPLICATION");
335  if (replication == TRI_NO)
336  appendPQExpBufferStr(&sql, " NOREPLICATION");
337  if (bypassrls == TRI_YES)
338  appendPQExpBufferStr(&sql, " BYPASSRLS");
339  if (bypassrls == TRI_NO)
340  appendPQExpBufferStr(&sql, " NOBYPASSRLS");
341  if (conn_limit >= -1)
342  appendPQExpBuffer(&sql, " CONNECTION LIMIT %d", conn_limit);
343  if (pwexpiry != NULL)
344  {
345  appendPQExpBufferStr(&sql, " VALID UNTIL ");
346  appendStringLiteralConn(&sql, pwexpiry, conn);
347  }
348  if (roles.head != NULL)
349  {
350  SimpleStringListCell *cell;
351 
352  appendPQExpBufferStr(&sql, " IN ROLE ");
353 
354  for (cell = roles.head; cell; cell = cell->next)
355  {
356  if (cell->next)
357  appendPQExpBuffer(&sql, "%s,", fmtId(cell->val));
358  else
359  appendPQExpBufferStr(&sql, fmtId(cell->val));
360  }
361  }
362  if (members.head != NULL)
363  {
364  SimpleStringListCell *cell;
365 
366  appendPQExpBufferStr(&sql, " ROLE ");
367 
368  for (cell = members.head; cell; cell = cell->next)
369  {
370  if (cell->next)
371  appendPQExpBuffer(&sql, "%s,", fmtId(cell->val));
372  else
373  appendPQExpBufferStr(&sql, fmtId(cell->val));
374  }
375  }
376  if (admins.head != NULL)
377  {
378  SimpleStringListCell *cell;
379 
380  appendPQExpBufferStr(&sql, " ADMIN ");
381 
382  for (cell = admins.head; cell; cell = cell->next)
383  {
384  if (cell->next)
385  appendPQExpBuffer(&sql, "%s,", fmtId(cell->val));
386  else
387  appendPQExpBufferStr(&sql, fmtId(cell->val));
388  }
389  }
390 
391  appendPQExpBufferChar(&sql, ';');
392 
393  if (echo)
394  printf("%s\n", sql.data);
395  result = PQexec(conn, sql.data);
396 
397  if (PQresultStatus(result) != PGRES_COMMAND_OK)
398  {
399  pg_log_error("creation of new role failed: %s", PQerrorMessage(conn));
400  PQfinish(conn);
401  exit(1);
402  }
403 
404  PQclear(result);
405  PQfinish(conn);
406  exit(0);
407 }
bool yesno_prompt(const char *question)
Definition: common.c:136
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1204
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:436
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
static void help(const char *progname)
Definition: createuser.c:411
Oid createdb(ParseState *pstate, const CreatedbStmt *stmt)
Definition: dbcommands.c:671
char * PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, const char *algorithm)
Definition: fe-auth.c:1278
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7173
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4560
void PQfreemem(void *ptr)
Definition: fe-exec.c:3869
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3244
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2229
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
#define free(a)
Definition: header.h:65
static bool pwprompt
Definition: initdb.c:155
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:97
exit(1)
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
Definition: option_utils.c:50
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: option_utils.c:24
#define pg_fatal(...)
PGDLLIMPORT int optind
Definition: getopt.c:50
PGDLLIMPORT char * optarg
Definition: getopt.c:52
static int port
Definition: pg_regress.c:109
const char * username
Definition: pgbench.c:306
const char * get_progname(const char *argv0)
Definition: path.c:574
#define fprintf
Definition: port.h:242
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
char * c
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
PGconn * conn
Definition: streamutil.c:54
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:293
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
char val[FLEXIBLE_ARRAY_MEMBER]
Definition: simple_list.h:37
struct SimpleStringListCell * next
Definition: simple_list.h:34
SimpleStringListCell * head
Definition: simple_list.h:42
const char * pguser
Definition: connect_utils.h:31
char * override_dbname
Definition: pg_backup.h:90
char * pgport
Definition: pg_backup.h:84
char * pghost
Definition: pg_backup.h:85
char * dbname
Definition: pg_backup.h:83
enum trivalue prompt_password
Definition: connect_utils.h:32
bool superuser(void)
Definition: superuser.c:46
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74
trivalue
Definition: vacuumlo.c:35
@ TRI_YES
Definition: vacuumlo.c:38
@ TRI_DEFAULT
Definition: vacuumlo.c:36
@ TRI_NO
Definition: vacuumlo.c:37

References _, appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendStringLiteralConn(), conn, connectMaintenanceDatabase(), createdb(), PQExpBufferData::data, _connParams::dbname, exit(), fmtId(), fprintf, free, get_progname(), get_user_name_or_exit(), getopt_long(), handle_help_version_opts(), SimpleStringList::head, help(), initPQExpBuffer(), SimpleStringListCell::next, no_argument, optarg, optind, option_parse_int(), _connParams::override_dbname, pg_fatal, pg_log_error, pg_log_error_hint, pg_logging_init(), pg_strdup(), PG_TEXTDOMAIN, _connParams::pghost, _connParams::pgport, PGRES_COMMAND_OK, _connParams::pguser, port, PQclear(), PQencryptPasswordConn(), PQerrorMessage(), PQexec(), PQfinish(), PQfreemem(), PQresultStatus(), printf, printfPQExpBuffer(), progname, _connParams::prompt_password, pwprompt, required_argument, set_pglocale_pgservice(), simple_prompt(), simple_string_list_append(), superuser(), TRI_DEFAULT, TRI_NO, TRI_YES, username, SimpleStringListCell::val, and yesno_prompt().