LCOV - code coverage report
Current view: top level - src/bin/scripts - dropuser.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 52 81 64.2 %
Date: 2019-09-19 02:07:14 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * dropuser
       4             :  *
       5             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       6             :  * Portions Copyright (c) 1994, Regents of the University of California
       7             :  *
       8             :  * src/bin/scripts/dropuser.c
       9             :  *
      10             :  *-------------------------------------------------------------------------
      11             :  */
      12             : 
      13             : #include "postgres_fe.h"
      14             : #include "common.h"
      15             : #include "common/logging.h"
      16             : #include "fe_utils/string_utils.h"
      17             : 
      18             : 
      19             : static void help(const char *progname);
      20             : 
      21             : 
      22             : int
      23          10 : main(int argc, char *argv[])
      24             : {
      25             :     static int  if_exists = 0;
      26             : 
      27             :     static struct option long_options[] = {
      28             :         {"host", required_argument, NULL, 'h'},
      29             :         {"port", required_argument, NULL, 'p'},
      30             :         {"username", required_argument, NULL, 'U'},
      31             :         {"no-password", no_argument, NULL, 'w'},
      32             :         {"password", no_argument, NULL, 'W'},
      33             :         {"echo", no_argument, NULL, 'e'},
      34             :         {"interactive", no_argument, NULL, 'i'},
      35             :         {"if-exists", no_argument, &if_exists, 1},
      36             :         {NULL, 0, NULL, 0}
      37             :     };
      38             : 
      39             :     const char *progname;
      40             :     int         optindex;
      41             :     int         c;
      42             : 
      43          10 :     char       *dropuser = NULL;
      44          10 :     char       *host = NULL;
      45          10 :     char       *port = NULL;
      46          10 :     char       *username = NULL;
      47          10 :     enum trivalue prompt_password = TRI_DEFAULT;
      48          10 :     bool        echo = false;
      49          10 :     bool        interactive = false;
      50             :     char        dropuser_buf[128];
      51             : 
      52             :     PQExpBufferData sql;
      53             : 
      54             :     PGconn     *conn;
      55             :     PGresult   *result;
      56             : 
      57          10 :     pg_logging_init(argv[0]);
      58          10 :     progname = get_progname(argv[0]);
      59          10 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
      60             : 
      61          10 :     handle_help_version_opts(argc, argv, "dropuser", help);
      62             : 
      63           6 :     while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
      64             :     {
      65           2 :         switch (c)
      66             :         {
      67             :             case 'h':
      68           0 :                 host = pg_strdup(optarg);
      69           0 :                 break;
      70             :             case 'p':
      71           0 :                 port = pg_strdup(optarg);
      72           0 :                 break;
      73             :             case 'U':
      74           0 :                 username = pg_strdup(optarg);
      75           0 :                 break;
      76             :             case 'w':
      77           0 :                 prompt_password = TRI_NO;
      78           0 :                 break;
      79             :             case 'W':
      80           0 :                 prompt_password = TRI_YES;
      81           0 :                 break;
      82             :             case 'e':
      83           0 :                 echo = true;
      84           0 :                 break;
      85             :             case 'i':
      86           0 :                 interactive = true;
      87           0 :                 break;
      88             :             case 0:
      89             :                 /* this covers the long options */
      90           0 :                 break;
      91             :             default:
      92           2 :                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
      93           2 :                 exit(1);
      94             :         }
      95             :     }
      96             : 
      97           4 :     switch (argc - optind)
      98             :     {
      99             :         case 0:
     100           0 :             break;
     101             :         case 1:
     102           4 :             dropuser = argv[optind];
     103           4 :             break;
     104             :         default:
     105           0 :             pg_log_error("too many command-line arguments (first is \"%s\")",
     106             :                          argv[optind + 1]);
     107           0 :             fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     108           0 :             exit(1);
     109             :     }
     110             : 
     111           4 :     if (dropuser == NULL)
     112             :     {
     113           0 :         if (interactive)
     114             :         {
     115           0 :             simple_prompt("Enter name of role to drop: ",
     116             :                           dropuser_buf, sizeof(dropuser_buf), true);
     117           0 :             dropuser = dropuser_buf;
     118             :         }
     119             :         else
     120             :         {
     121           0 :             pg_log_error("missing required argument role name");
     122           0 :             fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     123           0 :             exit(1);
     124             :         }
     125             :     }
     126             : 
     127           4 :     if (interactive)
     128             :     {
     129           0 :         printf(_("Role \"%s\" will be permanently removed.\n"), dropuser);
     130           0 :         if (!yesno_prompt("Are you sure?"))
     131           0 :             exit(0);
     132             :     }
     133             : 
     134           4 :     initPQExpBuffer(&sql);
     135           8 :     appendPQExpBuffer(&sql, "DROP ROLE %s%s;",
     136           4 :                       (if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
     137             : 
     138           4 :     conn = connectDatabase("postgres", host, port, username, prompt_password,
     139             :                            progname, echo, false, false);
     140             : 
     141           4 :     if (echo)
     142           0 :         printf("%s\n", sql.data);
     143           4 :     result = PQexec(conn, sql.data);
     144             : 
     145           4 :     if (PQresultStatus(result) != PGRES_COMMAND_OK)
     146             :     {
     147           2 :         pg_log_error("removal of role \"%s\" failed: %s",
     148             :                      dropuser, PQerrorMessage(conn));
     149           2 :         PQfinish(conn);
     150           2 :         exit(1);
     151             :     }
     152             : 
     153           2 :     PQclear(result);
     154           2 :     PQfinish(conn);
     155           2 :     exit(0);
     156             : }
     157             : 
     158             : 
     159             : static void
     160           2 : help(const char *progname)
     161             : {
     162           2 :     printf(_("%s removes a PostgreSQL role.\n\n"), progname);
     163           2 :     printf(_("Usage:\n"));
     164           2 :     printf(_("  %s [OPTION]... [ROLENAME]\n"), progname);
     165           2 :     printf(_("\nOptions:\n"));
     166           2 :     printf(_("  -e, --echo                show the commands being sent to the server\n"));
     167           2 :     printf(_("  -i, --interactive         prompt before deleting anything, and prompt for\n"
     168             :              "                            role name if not specified\n"));
     169           2 :     printf(_("  -V, --version             output version information, then exit\n"));
     170           2 :     printf(_("  --if-exists               don't report error if user doesn't exist\n"));
     171           2 :     printf(_("  -?, --help                show this help, then exit\n"));
     172           2 :     printf(_("\nConnection options:\n"));
     173           2 :     printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
     174           2 :     printf(_("  -p, --port=PORT           database server port\n"));
     175           2 :     printf(_("  -U, --username=USERNAME   user name to connect as (not the one to drop)\n"));
     176           2 :     printf(_("  -w, --no-password         never prompt for password\n"));
     177           2 :     printf(_("  -W, --password            force password prompt\n"));
     178           2 :     printf(_("\nReport bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
     179           2 : }

Generated by: LCOV version 1.13