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

References _, and printf.

Referenced by main().

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

◆ 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  char *conn_limit = NULL;
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  switch (c)
92  {
93  case 'h':
94  host = pg_strdup(optarg);
95  break;
96  case 'p':
97  port = pg_strdup(optarg);
98  break;
99  case 'U':
100  username = pg_strdup(optarg);
101  break;
102  case 'g':
104  break;
105  case 'w':
106  prompt_password = TRI_NO;
107  break;
108  case 'W':
109  prompt_password = TRI_YES;
110  break;
111  case 'e':
112  echo = true;
113  break;
114  case 'd':
115  createdb = TRI_YES;
116  break;
117  case 'D':
118  createdb = TRI_NO;
119  break;
120  case 's':
121  superuser = TRI_YES;
122  break;
123  case 'S':
124  superuser = TRI_NO;
125  break;
126  case 'r':
127  createrole = TRI_YES;
128  break;
129  case 'R':
130  createrole = TRI_NO;
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 'c':
145  conn_limit = pg_strdup(optarg);
146  break;
147  case 'P':
148  pwprompt = true;
149  break;
150  case 'E':
151  /* no-op, accepted for backward compatibility */
152  break;
153  case 1:
154  replication = TRI_YES;
155  break;
156  case 2:
157  replication = TRI_NO;
158  break;
159  case 3:
160  interactive = true;
161  break;
162  default:
163  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
164  exit(1);
165  }
166  }
167 
168  switch (argc - optind)
169  {
170  case 0:
171  break;
172  case 1:
173  newuser = argv[optind];
174  break;
175  default:
176  pg_log_error("too many command-line arguments (first is \"%s\")",
177  argv[optind + 1]);
178  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
179  exit(1);
180  }
181 
182  if (newuser == NULL)
183  {
184  if (interactive)
185  {
186  simple_prompt("Enter name of role to add: ",
187  newuser_buf, sizeof(newuser_buf), true);
188  newuser = newuser_buf;
189  }
190  else
191  {
192  if (getenv("PGUSER"))
193  newuser = getenv("PGUSER");
194  else
195  newuser = get_user_name_or_exit(progname);
196  }
197  }
198 
199  if (pwprompt)
200  {
201  char pw2[100];
202 
203  simple_prompt("Enter password for new role: ",
204  newpassword_buf, sizeof(newpassword_buf), false);
205  simple_prompt("Enter it again: ", pw2, sizeof(pw2), false);
206  if (strcmp(newpassword_buf, pw2) != 0)
207  {
208  fprintf(stderr, _("Passwords didn't match.\n"));
209  exit(1);
210  }
211  newpassword = newpassword_buf;
212  }
213 
214  if (superuser == 0)
215  {
216  if (interactive && yesno_prompt("Shall the new role be a superuser?"))
217  superuser = TRI_YES;
218  else
219  superuser = TRI_NO;
220  }
221 
222  if (superuser == TRI_YES)
223  {
224  /* Not much point in trying to restrict a superuser */
225  createdb = TRI_YES;
226  createrole = TRI_YES;
227  }
228 
229  if (createdb == 0)
230  {
231  if (interactive && yesno_prompt("Shall the new role be allowed to create databases?"))
232  createdb = TRI_YES;
233  else
234  createdb = TRI_NO;
235  }
236 
237  if (createrole == 0)
238  {
239  if (interactive && yesno_prompt("Shall the new role be allowed to create more new roles?"))
240  createrole = TRI_YES;
241  else
242  createrole = TRI_NO;
243  }
244 
245  if (inherit == 0)
246  inherit = TRI_YES;
247 
248  if (login == 0)
249  login = TRI_YES;
250 
251  conn = connectDatabase("postgres", host, port, username, prompt_password,
252  progname, echo, false, false);
253 
254  initPQExpBuffer(&sql);
255 
256  printfPQExpBuffer(&sql, "CREATE ROLE %s", fmtId(newuser));
257  if (newpassword)
258  {
259  char *encrypted_password;
260 
261  appendPQExpBufferStr(&sql, " PASSWORD ");
262 
263  encrypted_password = PQencryptPasswordConn(conn,
264  newpassword,
265  newuser,
266  NULL);
267  if (!encrypted_password)
268  {
269  pg_log_error("password encryption failed: %s",
270  PQerrorMessage(conn));
271  exit(1);
272  }
273  appendStringLiteralConn(&sql, encrypted_password, conn);
274  PQfreemem(encrypted_password);
275  }
276  if (superuser == TRI_YES)
277  appendPQExpBufferStr(&sql, " SUPERUSER");
278  if (superuser == TRI_NO)
279  appendPQExpBufferStr(&sql, " NOSUPERUSER");
280  if (createdb == TRI_YES)
281  appendPQExpBufferStr(&sql, " CREATEDB");
282  if (createdb == TRI_NO)
283  appendPQExpBufferStr(&sql, " NOCREATEDB");
284  if (createrole == TRI_YES)
285  appendPQExpBufferStr(&sql, " CREATEROLE");
286  if (createrole == TRI_NO)
287  appendPQExpBufferStr(&sql, " NOCREATEROLE");
288  if (inherit == TRI_YES)
289  appendPQExpBufferStr(&sql, " INHERIT");
290  if (inherit == TRI_NO)
291  appendPQExpBufferStr(&sql, " NOINHERIT");
292  if (login == TRI_YES)
293  appendPQExpBufferStr(&sql, " LOGIN");
294  if (login == TRI_NO)
295  appendPQExpBufferStr(&sql, " NOLOGIN");
296  if (replication == TRI_YES)
297  appendPQExpBufferStr(&sql, " REPLICATION");
298  if (replication == TRI_NO)
299  appendPQExpBufferStr(&sql, " NOREPLICATION");
300  if (conn_limit != NULL)
301  appendPQExpBuffer(&sql, " CONNECTION LIMIT %s", conn_limit);
302  if (roles.head != NULL)
303  {
304  SimpleStringListCell *cell;
305 
306  appendPQExpBufferStr(&sql, " IN ROLE ");
307 
308  for (cell = roles.head; cell; cell = cell->next)
309  {
310  if (cell->next)
311  appendPQExpBuffer(&sql, "%s,", fmtId(cell->val));
312  else
313  appendPQExpBufferStr(&sql, fmtId(cell->val));
314  }
315  }
316  appendPQExpBufferChar(&sql, ';');
317 
318  if (echo)
319  printf("%s\n", sql.data);
320  result = PQexec(conn, sql.data);
321 
322  if (PQresultStatus(result) != PGRES_COMMAND_OK)
323  {
324  pg_log_error("creation of new role failed: %s", PQerrorMessage(conn));
325  PQfinish(conn);
326  exit(1);
327  }
328 
329  PQclear(result);
330  PQfinish(conn);
331  exit(0);
332 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6617
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
bool yesno_prompt(const char *question)
Definition: common.c:443
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:39
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4119
bool superuser(void)
Definition: superuser.c:46
#define printf(...)
Definition: port.h:198
static bool pwprompt
Definition: initdb.c:134
const char * progname
Definition: pg_standby.c:36
#define fprintf
Definition: port.h:196
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:91
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: common.c:40
trivalue
Definition: vacuumlo.c:33
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1160
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:1634
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:565
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:336
#define _(x)
Definition: elog.c:87
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