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 343 of file createuser.c.

344 {
345  printf(_("%s creates a new PostgreSQL role.\n\n"), progname);
346  printf(_("Usage:\n"));
347  printf(_(" %s [OPTION]... [ROLENAME]\n"), progname);
348  printf(_("\nOptions:\n"));
349  printf(_(" -c, --connection-limit=N connection limit for role (default: no limit)\n"));
350  printf(_(" -d, --createdb role can create new databases\n"));
351  printf(_(" -D, --no-createdb role cannot create databases (default)\n"));
352  printf(_(" -e, --echo show the commands being sent to the server\n"));
353  printf(_(" -g, --role=ROLE new role will be a member of this role\n"));
354  printf(_(" -i, --inherit role inherits privileges of roles it is a\n"
355  " member of (default)\n"));
356  printf(_(" -I, --no-inherit role does not inherit privileges\n"));
357  printf(_(" -l, --login role can login (default)\n"));
358  printf(_(" -L, --no-login role cannot login\n"));
359  printf(_(" -P, --pwprompt assign a password to new role\n"));
360  printf(_(" -r, --createrole role can create new roles\n"));
361  printf(_(" -R, --no-createrole role cannot create roles (default)\n"));
362  printf(_(" -s, --superuser role will be superuser\n"));
363  printf(_(" -S, --no-superuser role will not be superuser (default)\n"));
364  printf(_(" -V, --version output version information, then exit\n"));
365  printf(_(" --interactive prompt for missing role name and attributes rather\n"
366  " than using defaults\n"));
367  printf(_(" --replication role can initiate replication\n"));
368  printf(_(" --no-replication role cannot initiate replication\n"));
369  printf(_(" -?, --help show this help, then exit\n"));
370  printf(_("\nConnection options:\n"));
371  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
372  printf(_(" -p, --port=PORT database server port\n"));
373  printf(_(" -U, --username=USERNAME user name to connect as (not the one to create)\n"));
374  printf(_(" -w, --no-password never prompt for password\n"));
375  printf(_(" -W, --password force password prompt\n"));
376  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
377  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
378 }
#define _(x)
Definition: elog.c:89
const char * progname
Definition: main.c:50
#define printf(...)
Definition: port.h:231

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  {"host", required_argument, NULL, 'h'},
32  {"port", required_argument, NULL, 'p'},
33  {"username", required_argument, NULL, 'U'},
34  {"role", required_argument, NULL, 'g'},
35  {"no-password", no_argument, NULL, 'w'},
36  {"password", no_argument, NULL, 'W'},
37  {"echo", no_argument, NULL, 'e'},
38  {"createdb", no_argument, NULL, 'd'},
39  {"no-createdb", no_argument, NULL, 'D'},
40  {"superuser", no_argument, NULL, 's'},
41  {"no-superuser", no_argument, NULL, 'S'},
42  {"createrole", no_argument, NULL, 'r'},
43  {"no-createrole", no_argument, NULL, 'R'},
44  {"inherit", no_argument, NULL, 'i'},
45  {"no-inherit", no_argument, NULL, 'I'},
46  {"login", no_argument, NULL, 'l'},
47  {"no-login", no_argument, NULL, 'L'},
48  {"replication", no_argument, NULL, 1},
49  {"no-replication", no_argument, NULL, 2},
50  {"interactive", no_argument, NULL, 3},
51  {"connection-limit", required_argument, NULL, 'c'},
52  {"pwprompt", no_argument, NULL, 'P'},
53  {"encrypted", no_argument, NULL, 'E'},
54  {NULL, 0, NULL, 0}
55  };
56 
57  const char *progname;
58  int optindex;
59  int c;
60  const char *newuser = NULL;
61  char *host = NULL;
62  char *port = NULL;
63  char *username = NULL;
64  SimpleStringList roles = {NULL, NULL};
65  enum trivalue prompt_password = TRI_DEFAULT;
66  ConnParams cparams;
67  bool echo = false;
68  bool interactive = false;
69  int conn_limit = -2; /* less than minimum valid value */
70  bool pwprompt = false;
71  char *newpassword = NULL;
72 
73  /* Tri-valued variables. */
76  createrole = TRI_DEFAULT,
77  inherit = TRI_DEFAULT,
78  login = TRI_DEFAULT,
79  replication = TRI_DEFAULT;
80 
81  PQExpBufferData sql;
82 
83  PGconn *conn;
84  PGresult *result;
85 
86  pg_logging_init(argv[0]);
87  progname = get_progname(argv[0]);
88  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
89 
90  handle_help_version_opts(argc, argv, "createuser", help);
91 
92  while ((c = getopt_long(argc, argv, "h:p:U:g:wWedDsSrRiIlLc:PE",
93  long_options, &optindex)) != -1)
94  {
95  switch (c)
96  {
97  case 'h':
98  host = pg_strdup(optarg);
99  break;
100  case 'p':
101  port = pg_strdup(optarg);
102  break;
103  case 'U':
105  break;
106  case 'g':
108  break;
109  case 'w':
110  prompt_password = TRI_NO;
111  break;
112  case 'W':
113  prompt_password = TRI_YES;
114  break;
115  case 'e':
116  echo = true;
117  break;
118  case 'd':
119  createdb = TRI_YES;
120  break;
121  case 'D':
122  createdb = TRI_NO;
123  break;
124  case 's':
125  superuser = TRI_YES;
126  break;
127  case 'S':
128  superuser = TRI_NO;
129  break;
130  case 'r':
131  createrole = TRI_YES;
132  break;
133  case 'R':
134  createrole = TRI_NO;
135  break;
136  case 'i':
137  inherit = TRI_YES;
138  break;
139  case 'I':
140  inherit = TRI_NO;
141  break;
142  case 'l':
143  login = TRI_YES;
144  break;
145  case 'L':
146  login = TRI_NO;
147  break;
148  case 'c':
149  if (!option_parse_int(optarg, "-c/--connection-limit",
150  -1, INT_MAX, &conn_limit))
151  exit(1);
152  break;
153  case 'P':
154  pwprompt = true;
155  break;
156  case 'E':
157  /* no-op, accepted for backward compatibility */
158  break;
159  case 1:
160  replication = TRI_YES;
161  break;
162  case 2:
163  replication = TRI_NO;
164  break;
165  case 3:
166  interactive = true;
167  break;
168  default:
169  /* getopt_long already emitted a complaint */
170  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
171  exit(1);
172  }
173  }
174 
175  switch (argc - optind)
176  {
177  case 0:
178  break;
179  case 1:
180  newuser = argv[optind];
181  break;
182  default:
183  pg_log_error("too many command-line arguments (first is \"%s\")",
184  argv[optind + 1]);
185  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
186  exit(1);
187  }
188 
189  if (newuser == NULL)
190  {
191  if (interactive)
192  {
193  newuser = simple_prompt("Enter name of role to add: ", true);
194  }
195  else
196  {
197  if (getenv("PGUSER"))
198  newuser = getenv("PGUSER");
199  else
200  newuser = get_user_name_or_exit(progname);
201  }
202  }
203 
204  if (pwprompt)
205  {
206  char *pw2;
207 
208  newpassword = simple_prompt("Enter password for new role: ", false);
209  pw2 = simple_prompt("Enter it again: ", false);
210  if (strcmp(newpassword, pw2) != 0)
211  {
212  fprintf(stderr, _("Passwords didn't match.\n"));
213  exit(1);
214  }
215  free(pw2);
216  }
217 
218  if (superuser == 0)
219  {
220  if (interactive && yesno_prompt("Shall the new role be a superuser?"))
221  superuser = TRI_YES;
222  else
223  superuser = TRI_NO;
224  }
225 
226  if (superuser == TRI_YES)
227  {
228  /* Not much point in trying to restrict a superuser */
229  createdb = TRI_YES;
230  createrole = TRI_YES;
231  }
232 
233  if (createdb == 0)
234  {
235  if (interactive && yesno_prompt("Shall the new role be allowed to create databases?"))
236  createdb = TRI_YES;
237  else
238  createdb = TRI_NO;
239  }
240 
241  if (createrole == 0)
242  {
243  if (interactive && yesno_prompt("Shall the new role be allowed to create more new roles?"))
244  createrole = TRI_YES;
245  else
246  createrole = TRI_NO;
247  }
248 
249  if (inherit == 0)
250  inherit = TRI_YES;
251 
252  if (login == 0)
253  login = TRI_YES;
254 
255  cparams.dbname = NULL; /* this program lacks any dbname option... */
256  cparams.pghost = host;
257  cparams.pgport = port;
258  cparams.pguser = username;
259  cparams.prompt_password = prompt_password;
260  cparams.override_dbname = NULL;
261 
262  conn = connectMaintenanceDatabase(&cparams, progname, echo);
263 
264  initPQExpBuffer(&sql);
265 
266  printfPQExpBuffer(&sql, "CREATE ROLE %s", fmtId(newuser));
267  if (newpassword)
268  {
269  char *encrypted_password;
270 
271  appendPQExpBufferStr(&sql, " PASSWORD ");
272 
273  encrypted_password = PQencryptPasswordConn(conn,
274  newpassword,
275  newuser,
276  NULL);
277  if (!encrypted_password)
278  pg_fatal("password encryption failed: %s",
280  appendStringLiteralConn(&sql, encrypted_password, conn);
281  PQfreemem(encrypted_password);
282  }
283  if (superuser == TRI_YES)
284  appendPQExpBufferStr(&sql, " SUPERUSER");
285  if (superuser == TRI_NO)
286  appendPQExpBufferStr(&sql, " NOSUPERUSER");
287  if (createdb == TRI_YES)
288  appendPQExpBufferStr(&sql, " CREATEDB");
289  if (createdb == TRI_NO)
290  appendPQExpBufferStr(&sql, " NOCREATEDB");
291  if (createrole == TRI_YES)
292  appendPQExpBufferStr(&sql, " CREATEROLE");
293  if (createrole == TRI_NO)
294  appendPQExpBufferStr(&sql, " NOCREATEROLE");
295  if (inherit == TRI_YES)
296  appendPQExpBufferStr(&sql, " INHERIT");
297  if (inherit == TRI_NO)
298  appendPQExpBufferStr(&sql, " NOINHERIT");
299  if (login == TRI_YES)
300  appendPQExpBufferStr(&sql, " LOGIN");
301  if (login == TRI_NO)
302  appendPQExpBufferStr(&sql, " NOLOGIN");
303  if (replication == TRI_YES)
304  appendPQExpBufferStr(&sql, " REPLICATION");
305  if (replication == TRI_NO)
306  appendPQExpBufferStr(&sql, " NOREPLICATION");
307  if (conn_limit >= -1)
308  appendPQExpBuffer(&sql, " CONNECTION LIMIT %d", conn_limit);
309  if (roles.head != NULL)
310  {
311  SimpleStringListCell *cell;
312 
313  appendPQExpBufferStr(&sql, " IN ROLE ");
314 
315  for (cell = roles.head; cell; cell = cell->next)
316  {
317  if (cell->next)
318  appendPQExpBuffer(&sql, "%s,", fmtId(cell->val));
319  else
320  appendPQExpBufferStr(&sql, fmtId(cell->val));
321  }
322  }
323  appendPQExpBufferChar(&sql, ';');
324 
325  if (echo)
326  printf("%s\n", sql.data);
327  result = PQexec(conn, sql.data);
328 
329  if (PQresultStatus(result) != PGRES_COMMAND_OK)
330  {
331  pg_log_error("creation of new role failed: %s", PQerrorMessage(conn));
332  PQfinish(conn);
333  exit(1);
334  }
335 
336  PQclear(result);
337  PQfinish(conn);
338  exit(0);
339 }
bool yesno_prompt(const char *question)
Definition: common.c:136
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1212
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:446
PGconn * connectMaintenanceDatabase(ConnParams *cparams, const char *progname, bool echo)
static void help(const char *progname)
Definition: createuser.c:343
Oid createdb(ParseState *pstate, const CreatedbStmt *stmt)
Definition: dbcommands.c:674
char * PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, const char *algorithm)
Definition: fe-auth.c:1230
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6908
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4261
void PQfreemem(void *ptr)
Definition: fe-exec.c:3891
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3270
void PQclear(PGresult *res)
Definition: fe-exec.c:718
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2273
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:139
@ 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:92
const char * username
Definition: pgbench.c:309
const char * get_progname(const char *argv0)
Definition: path.c:574
#define fprintf
Definition: port.h:229
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
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:89
char * pgport
Definition: pg_backup.h:83
char * pghost
Definition: pg_backup.h:84
char * dbname
Definition: pg_backup.h:82
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().