PostgreSQL Source Code  git master
createuser.c File Reference
#include "postgres_fe.h"
#include "common.h"
#include "common/logging.h"
#include "common/string.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 347 of file createuser.c.

References _, and printf.

Referenced by main().

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

◆ main()

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

Definition at line 24 of file createuser.c.

References _, appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendStringLiteralConn(), conn, connectMaintenanceDatabase(), createdb(), PQExpBufferData::data, _connParams::dbname, fmtId(), fprintf, free, get_progname(), get_user_name_or_exit(), getopt_long(), handle_help_version_opts(), SimpleStringList::head, help(), initPQExpBuffer(), login, SimpleStringListCell::next, no_argument, optarg, optind, _connParams::override_dbname, pg_log_error, 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().

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