Index: createuser.c =================================================================== RCS file: /home/alvherre/cvs/pgsql/src/bin/scripts/createuser.c,v retrieving revision 1.17 diff -c -r1.17 createuser.c *** createuser.c 21 Jun 2005 04:02:33 -0000 1.17 --- createuser.c 12 Aug 2005 16:20:55 -0000 *************** *** 30,37 **** --- 30,46 ---- {"quiet", no_argument, NULL, 'q'}, {"createdb", no_argument, NULL, 'd'}, {"no-createdb", no_argument, NULL, 'D'}, + {"superuser", no_argument, NULL, 's'}, + {"no-superuser", no_argument, NULL, 'S'}, + {"createrole", no_argument, NULL, 'r'}, + {"no-createrole", no_argument, NULL, 'R'}, + {"inherit", no_argument, NULL, 'y'}, + {"no-inherit", no_argument, NULL, 'Y'}, + {"login", no_argument, NULL, 'l'}, + {"no-login", no_argument, NULL, 'L'}, {"adduser", no_argument, NULL, 'a'}, {"no-adduser", no_argument, NULL, 'A'}, + {"conn-limit", required_argument, NULL, 'c'}, {"sysid", required_argument, NULL, 'i'}, {"pwprompt", no_argument, NULL, 'P'}, {"encrypted", no_argument, NULL, 'E'}, *************** *** 51,58 **** bool echo = false; bool quiet = false; int createdb = 0; ! int adduser = 0; ! char *sysid = NULL; bool pwprompt = false; int encrypted = 0; /* 0 uses server default */ char *newpassword = NULL; --- 60,70 ---- bool echo = false; bool quiet = false; int createdb = 0; ! int superuser = 0; ! int createrole = 0; ! int inherit = 0; ! int login = 0; ! char *conn_limit = NULL; bool pwprompt = false; int encrypted = 0; /* 0 uses server default */ char *newpassword = NULL; *************** *** 67,73 **** handle_help_version_opts(argc, argv, "createuser", help); ! while ((c = getopt_long(argc, argv, "h:p:U:WeqaAdDi:PEN", long_options, &optindex)) != -1) { switch (c) { --- 79,85 ---- handle_help_version_opts(argc, argv, "createuser", help); ! while ((c = getopt_long(argc, argv, "h:p:U:WeqdDsSrRyYlLaAc:i:PEN", long_options, &optindex)) != -1) { switch (c) { *************** *** 89,108 **** case 'q': quiet = true; break; - case 'a': - adduser = +1; - break; - case 'A': - adduser = -1; - break; case 'd': createdb = +1; break; case 'D': createdb = -1; break; case 'i': ! sysid = optarg; break; case 'P': pwprompt = true; --- 101,143 ---- case 'q': quiet = true; break; case 'd': createdb = +1; break; case 'D': createdb = -1; break; + case 's': + case 'a': + superuser = +1; + break; + case 'S': + case 'A': + superuser = -1; + break; + case 'r': + createrole = +1; + break; + case 'R': + createrole = -1; + break; + case 'y': + inherit = +1; + break; + case 'Y': + inherit = -1; + break; + case 'l': + login = +1; + break; + case 'L': + login = -1; + break; + case 'c': + conn_limit = optarg; + break; case 'i': ! /* sysid option silently ignored */ break; case 'P': pwprompt = true; *************** *** 133,158 **** exit(1); } - if (sysid) - { - char *endptr; - - if (strtol(sysid, &endptr, 10) <= 0 || *endptr != '\0') - { - fprintf(stderr, _("%s: user ID must be a positive number\n"), progname); - exit(1); - } - } - if (newuser == NULL) ! newuser = simple_prompt("Enter name of user to add: ", 128, true); if (pwprompt) { char *pw1, *pw2; ! pw1 = simple_prompt("Enter password for new user: ", 100, false); pw2 = simple_prompt("Enter it again: ", 100, false); if (strcmp(pw1, pw2) != 0) { --- 168,182 ---- exit(1); } if (newuser == NULL) ! newuser = simple_prompt("Enter name of role to add: ", 128, true); if (pwprompt) { char *pw1, *pw2; ! pw1 = simple_prompt("Enter password for new role: ", 100, false); pw2 = simple_prompt("Enter it again: ", 100, false); if (strcmp(pw1, pw2) != 0) { *************** *** 163,195 **** free(pw2); } if (createdb == 0) { char *reply; ! reply = simple_prompt("Shall the new user be allowed to create databases? (y/n) ", 1, true); if (check_yesno_response(reply) == 1) createdb = +1; else createdb = -1; } ! if (adduser == 0) { char *reply; ! reply = simple_prompt("Shall the new user be allowed to create more new users? (y/n) ", 1, true); if (check_yesno_response(reply) == 1) ! adduser = +1; else ! adduser = -1; } initPQExpBuffer(&sql); ! printfPQExpBuffer(&sql, "CREATE USER %s", fmtId(newuser)); ! if (sysid) ! appendPQExpBuffer(&sql, " SYSID %s", sysid); if (newpassword) { if (encrypted == +1) --- 187,251 ---- free(pw2); } + if (superuser == 0) + { + char *reply; + + reply = simple_prompt("Shall the new role be superuser? (y/n) ", 1, true); + if (check_yesno_response(reply) == 1) + superuser = +1; + else + superuser = -1; + } + if (createdb == 0) { char *reply; ! reply = simple_prompt("Shall the new role be allowed to create databases? (y/n) ", 1, true); if (check_yesno_response(reply) == 1) createdb = +1; else createdb = -1; } ! if (createrole == 0) ! { ! char *reply; ! ! reply = simple_prompt("Shall the new role be allowed to create more new roles? (y/n) ", 1, true); ! if (check_yesno_response(reply) == 1) ! createrole = +1; ! else ! createrole = -1; ! } ! ! if (inherit == 0) ! { ! char *reply; ! ! reply = simple_prompt("Shall the new role automatically inherit privileges " ! "of roles it's a member of? (y/n) ", 1, true); ! if (check_yesno_response(reply) == 1) ! inherit = +1; ! else ! inherit = -1; ! } ! ! if (login == 0) { char *reply; ! reply = simple_prompt("Shall the new role be allowed to login? (y/n) ", 1, true); if (check_yesno_response(reply) == 1) ! login = +1; else ! login = -1; } initPQExpBuffer(&sql); ! printfPQExpBuffer(&sql, "CREATE ROLE %s", fmtId(newuser)); if (newpassword) { if (encrypted == +1) *************** *** 203,212 **** appendPQExpBuffer(&sql, " CREATEDB"); if (createdb == -1) appendPQExpBuffer(&sql, " NOCREATEDB"); ! if (adduser == +1) ! appendPQExpBuffer(&sql, " CREATEUSER"); ! if (adduser == -1) ! appendPQExpBuffer(&sql, " NOCREATEUSER"); appendPQExpBuffer(&sql, ";\n"); conn = connectDatabase("postgres", host, port, username, password, progname); --- 259,282 ---- appendPQExpBuffer(&sql, " CREATEDB"); if (createdb == -1) appendPQExpBuffer(&sql, " NOCREATEDB"); ! if (createrole == +1) ! appendPQExpBuffer(&sql, " CREATEROLE"); ! if (createrole == -1) ! appendPQExpBuffer(&sql, " NOCREATEROLE"); ! if (superuser == +1) ! appendPQExpBuffer(&sql, " SUPERUSER"); ! if (superuser == -1) ! appendPQExpBuffer(&sql, " NOSUPERUSER"); ! if (inherit == +1) ! appendPQExpBuffer(&sql, " INHERIT"); ! if (inherit == -1) ! appendPQExpBuffer(&sql, " NOINHERIT"); ! if (login == +1) ! appendPQExpBuffer(&sql, " LOGIN"); ! if (login == -1) ! appendPQExpBuffer(&sql, " NOLOGIN"); ! if (conn_limit != NULL) ! appendPQExpBuffer(&sql, " CONNECTION LIMIT %s", conn_limit); appendPQExpBuffer(&sql, ";\n"); conn = connectDatabase("postgres", host, port, username, password, progname); *************** *** 217,223 **** if (PQresultStatus(result) != PGRES_COMMAND_OK) { ! fprintf(stderr, _("%s: creation of new user failed: %s"), progname, PQerrorMessage(conn)); PQfinish(conn); exit(1); --- 287,293 ---- if (PQresultStatus(result) != PGRES_COMMAND_OK) { ! fprintf(stderr, _("%s: creation of new role failed: %s"), progname, PQerrorMessage(conn)); PQfinish(conn); exit(1); *************** *** 226,232 **** PQfinish(conn); if (!quiet) { ! puts("CREATE USER"); fflush(stdout); } exit(0); --- 296,302 ---- PQfinish(conn); if (!quiet) { ! puts("CREATE ROLE"); fflush(stdout); } exit(0); *************** *** 236,253 **** static void help(const char *progname) { ! printf(_("%s creates a new PostgreSQL user.\n\n"), progname); printf(_("Usage:\n")); printf(_(" %s [OPTION]... [USERNAME]\n"), progname); printf(_("\nOptions:\n")); ! printf(_(" -a, --adduser user can add new users\n")); ! printf(_(" -A, --no-adduser user cannot add new users\n")); ! printf(_(" -d, --createdb user can create new databases\n")); ! printf(_(" -D, --no-createdb user cannot create databases\n")); ! printf(_(" -P, --pwprompt assign a password to new user\n")); printf(_(" -E, --encrypted encrypt stored password\n")); printf(_(" -N, --unencrypted do not encrypt stored password\n")); - printf(_(" -i, --sysid=SYSID select sysid for new user\n")); printf(_(" -e, --echo show the commands being sent to the server\n")); printf(_(" -q, --quiet don't write any messages\n")); printf(_(" --help show this help, then exit\n")); --- 306,328 ---- static void help(const char *progname) { ! printf(_("%s creates a new PostgreSQL role.\n\n"), progname); printf(_("Usage:\n")); printf(_(" %s [OPTION]... [USERNAME]\n"), progname); printf(_("\nOptions:\n")); ! printf(_(" -s, --superuser role shall be superuser\n")); ! printf(_(" -S, --no-superuser role shall not be superuser\n")); ! printf(_(" -d, --createdb role can create new databases\n")); ! printf(_(" -D, --no-createdb role cannot create databases\n")); ! printf(_(" -r, --createrole role can create new roles\n")); ! printf(_(" -R, --no-createrole role cannot create roles\n")); ! printf(_(" -l, --login role can login\n")); ! printf(_(" -L, --no-login role cannot login\n")); ! printf(_(" -y, --inherit role inherits permissions of roles it is member of\n")); ! printf(_(" -Y, --no-inherit role does not inherit permissions\n")); ! printf(_(" -P, --pwprompt assign a password to new role\n")); printf(_(" -E, --encrypted encrypt stored password\n")); printf(_(" -N, --unencrypted do not encrypt stored password\n")); printf(_(" -e, --echo show the commands being sent to the server\n")); printf(_(" -q, --quiet don't write any messages\n")); printf(_(" --help show this help, then exit\n")); *************** *** 257,263 **** printf(_(" -p, --port=PORT database server port\n")); printf(_(" -U, --username=USERNAME user name to connect as (not the one to create)\n")); printf(_(" -W, --password prompt for password to connect\n")); ! printf(_("\nIf one of -a, -A, -d, -D, and USERNAME is not specified, you will\n" ! "be prompted interactively.\n")); printf(_("\nReport bugs to .\n")); } --- 332,338 ---- printf(_(" -p, --port=PORT database server port\n")); printf(_(" -U, --username=USERNAME user name to connect as (not the one to create)\n")); printf(_(" -W, --password prompt for password to connect\n")); ! printf(_("\nIf one of -s, -S, -d, -D, -l, -L, -y, -Y and USERNAME is not specified,\n" ! "you will be prompted interactively.\n")); printf(_("\nReport bugs to .\n")); }