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

References _, and printf.

Referenced by main().

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

◆ main()

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

Definition at line 23 of file createuser.c.

References _, appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendStringLiteralConn(), conn, connectDatabase(), createdb(), PQExpBufferData::data, fmtId(), fprintf, get_progname(), get_user_name_or_exit(), getopt_long(), handle_help_version_opts(), SimpleStringList::head, help(), initPQExpBuffer(), login, SimpleStringListCell::next, no_argument, optarg, optind, pg_log_error, pg_logging_init(), pg_strdup(), PG_TEXTDOMAIN, PGRES_COMMAND_OK, port, PQclear(), PQencryptPasswordConn(), PQerrorMessage(), PQexec(), PQfinish(), PQfreemem(), PQresultStatus(), printf, printfPQExpBuffer(), progname, 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().

24 {
25  static struct option long_options[] = {
26  {"host", required_argument, NULL, 'h'},
27  {"port", required_argument, NULL, 'p'},
28  {"username", required_argument, NULL, 'U'},
29  {"role", required_argument, NULL, 'g'},
30  {"no-password", no_argument, NULL, 'w'},
31  {"password", no_argument, NULL, 'W'},
32  {"echo", no_argument, NULL, 'e'},
33  {"createdb", no_argument, NULL, 'd'},
34  {"no-createdb", no_argument, NULL, 'D'},
35  {"superuser", no_argument, NULL, 's'},
36  {"no-superuser", no_argument, NULL, 'S'},
37  {"createrole", no_argument, NULL, 'r'},
38  {"no-createrole", no_argument, NULL, 'R'},
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  {"replication", no_argument, NULL, 1},
44  {"no-replication", no_argument, NULL, 2},
45  {"interactive", no_argument, NULL, 3},
46  {"connection-limit", required_argument, NULL, 'c'},
47  {"pwprompt", no_argument, NULL, 'P'},
48  {"encrypted", no_argument, NULL, 'E'},
49  {NULL, 0, NULL, 0}
50  };
51 
52  const char *progname;
53  int optindex;
54  int c;
55  const char *newuser = NULL;
56  char *host = NULL;
57  char *port = NULL;
58  char *username = NULL;
59  SimpleStringList roles = {NULL, NULL};
60  enum trivalue prompt_password = TRI_DEFAULT;
61  bool echo = false;
62  bool interactive = false;
63  int conn_limit = -2; /* less than minimum valid value */
64  bool pwprompt = false;
65  char *newpassword = NULL;
66  char newuser_buf[128];
67  char newpassword_buf[100];
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  simple_prompt("Enter name of role to add: ",
195  newuser_buf, sizeof(newuser_buf), true);
196  newuser = newuser_buf;
197  }
198  else
199  {
200  if (getenv("PGUSER"))
201  newuser = getenv("PGUSER");
202  else
203  newuser = get_user_name_or_exit(progname);
204  }
205  }
206 
207  if (pwprompt)
208  {
209  char pw2[100];
210 
211  simple_prompt("Enter password for new role: ",
212  newpassword_buf, sizeof(newpassword_buf), false);
213  simple_prompt("Enter it again: ", pw2, sizeof(pw2), false);
214  if (strcmp(newpassword_buf, pw2) != 0)
215  {
216  fprintf(stderr, _("Passwords didn't match.\n"));
217  exit(1);
218  }
219  newpassword = newpassword_buf;
220  }
221 
222  if (superuser == 0)
223  {
224  if (interactive && yesno_prompt("Shall the new role be a superuser?"))
225  superuser = TRI_YES;
226  else
227  superuser = TRI_NO;
228  }
229 
230  if (superuser == TRI_YES)
231  {
232  /* Not much point in trying to restrict a superuser */
233  createdb = TRI_YES;
234  createrole = TRI_YES;
235  }
236 
237  if (createdb == 0)
238  {
239  if (interactive && yesno_prompt("Shall the new role be allowed to create databases?"))
240  createdb = TRI_YES;
241  else
242  createdb = TRI_NO;
243  }
244 
245  if (createrole == 0)
246  {
247  if (interactive && yesno_prompt("Shall the new role be allowed to create more new roles?"))
248  createrole = TRI_YES;
249  else
250  createrole = TRI_NO;
251  }
252 
253  if (inherit == 0)
254  inherit = TRI_YES;
255 
256  if (login == 0)
257  login = TRI_YES;
258 
259  conn = connectDatabase("postgres", host, port, username, prompt_password,
260  progname, echo, false, false);
261 
262  initPQExpBuffer(&sql);
263 
264  printfPQExpBuffer(&sql, "CREATE ROLE %s", fmtId(newuser));
265  if (newpassword)
266  {
267  char *encrypted_password;
268 
269  appendPQExpBufferStr(&sql, " PASSWORD ");
270 
271  encrypted_password = PQencryptPasswordConn(conn,
272  newpassword,
273  newuser,
274  NULL);
275  if (!encrypted_password)
276  {
277  pg_log_error("password encryption failed: %s",
278  PQerrorMessage(conn));
279  exit(1);
280  }
281  appendStringLiteralConn(&sql, encrypted_password, conn);
282  PQfreemem(encrypted_password);
283  }
284  if (superuser == TRI_YES)
285  appendPQExpBufferStr(&sql, " SUPERUSER");
286  if (superuser == TRI_NO)
287  appendPQExpBufferStr(&sql, " NOSUPERUSER");
288  if (createdb == TRI_YES)
289  appendPQExpBufferStr(&sql, " CREATEDB");
290  if (createdb == TRI_NO)
291  appendPQExpBufferStr(&sql, " NOCREATEDB");
292  if (createrole == TRI_YES)
293  appendPQExpBufferStr(&sql, " CREATEROLE");
294  if (createrole == TRI_NO)
295  appendPQExpBufferStr(&sql, " NOCREATEROLE");
296  if (inherit == TRI_YES)
297  appendPQExpBufferStr(&sql, " INHERIT");
298  if (inherit == TRI_NO)
299  appendPQExpBufferStr(&sql, " NOINHERIT");
300  if (login == TRI_YES)
301  appendPQExpBufferStr(&sql, " LOGIN");
302  if (login == TRI_NO)
303  appendPQExpBufferStr(&sql, " NOLOGIN");
304  if (replication == TRI_YES)
305  appendPQExpBufferStr(&sql, " REPLICATION");
306  if (replication == TRI_NO)
307  appendPQExpBufferStr(&sql, " NOREPLICATION");
308  if (conn_limit >= -1)
309  appendPQExpBuffer(&sql, " CONNECTION LIMIT %d", conn_limit);
310  if (roles.head != NULL)
311  {
312  SimpleStringListCell *cell;
313 
314  appendPQExpBufferStr(&sql, " IN ROLE ");
315 
316  for (cell = roles.head; cell; cell = cell->next)
317  {
318  if (cell->next)
319  appendPQExpBuffer(&sql, "%s,", fmtId(cell->val));
320  else
321  appendPQExpBufferStr(&sql, fmtId(cell->val));
322  }
323  }
324  appendPQExpBufferChar(&sql, ';');
325 
326  if (echo)
327  printf("%s\n", sql.data);
328  result = PQexec(conn, sql.data);
329 
330  if (PQresultStatus(result) != PGRES_COMMAND_OK)
331  {
332  pg_log_error("creation of new role failed: %s", PQerrorMessage(conn));
333  PQfinish(conn);
334  exit(1);
335  }
336 
337  PQclear(result);
338  PQfinish(conn);
339  exit(0);
340 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6687
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
bool yesno_prompt(const char *question)
Definition: common.c:435
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:79
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:76
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4185
bool superuser(void)
Definition: superuser.c:46
#define printf(...)
Definition: port.h:199
static bool pwprompt
Definition: initdb.c:134
const char * progname
Definition: pg_standby.c:36
#define fprintf
Definition: port.h:197
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
#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
void simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
Definition: sprompt.c:37
struct SimpleStringListCell * next
Definition: simple_list.h:34
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static int port
Definition: pg_regress.c:90
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: common.c:33
trivalue
Definition: vacuumlo.c:33
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1178
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:133
void PQclear(PGresult *res)
Definition: fe-exec.c:694
static PGconn * connectDatabase(const char *dbname, const char *connstr, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error)
Definition: pg_dumpall.c:1635
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:344
#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:244
const char * get_user_name_or_exit(const char *progname)
Definition: username.c:74