LCOV - code coverage report
Current view: top level - src/bin/scripts - dropuser.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 55 93 59.1 %
Date: 2020-09-25 17:06:10 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * dropuser
       4             :  *
       5             :  * Portions Copyright (c) 1996-2020, 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 "common/string.h"
      17             : #include "fe_utils/string_utils.h"
      18             : 
      19             : 
      20             : static void help(const char *progname);
      21             : 
      22             : 
      23             : int
      24          10 : main(int argc, char *argv[])
      25             : {
      26             :     static int  if_exists = 0;
      27             : 
      28             :     static struct option long_options[] = {
      29             :         {"host", required_argument, NULL, 'h'},
      30             :         {"port", required_argument, NULL, 'p'},
      31             :         {"username", required_argument, NULL, 'U'},
      32             :         {"no-password", no_argument, NULL, 'w'},
      33             :         {"password", no_argument, NULL, 'W'},
      34             :         {"echo", no_argument, NULL, 'e'},
      35             :         {"interactive", no_argument, NULL, 'i'},
      36             :         {"if-exists", no_argument, &if_exists, 1},
      37             :         {NULL, 0, NULL, 0}
      38             :     };
      39             : 
      40             :     const char *progname;
      41             :     int         optindex;
      42             :     int         c;
      43             : 
      44          10 :     char       *dropuser = NULL;
      45          10 :     char       *host = NULL;
      46          10 :     char       *port = NULL;
      47          10 :     char       *username = NULL;
      48          10 :     enum trivalue prompt_password = TRI_DEFAULT;
      49          10 :     bool        echo = false;
      50          10 :     bool        interactive = false;
      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           0 :             case 'h':
      68           0 :                 host = pg_strdup(optarg);
      69           0 :                 break;
      70           0 :             case 'p':
      71           0 :                 port = pg_strdup(optarg);
      72           0 :                 break;
      73           0 :             case 'U':
      74           0 :                 username = pg_strdup(optarg);
      75           0 :                 break;
      76           0 :             case 'w':
      77           0 :                 prompt_password = TRI_NO;
      78           0 :                 break;
      79           0 :             case 'W':
      80           0 :                 prompt_password = TRI_YES;
      81           0 :                 break;
      82           0 :             case 'e':
      83           0 :                 echo = true;
      84           0 :                 break;
      85           0 :             case 'i':
      86           0 :                 interactive = true;
      87           0 :                 break;
      88           0 :             case 0:
      89             :                 /* this covers the long options */
      90           0 :                 break;
      91           2 :             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           0 :         case 0:
     100           0 :             break;
     101           4 :         case 1:
     102           4 :             dropuser = argv[optind];
     103           4 :             break;
     104           0 :         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 :             dropuser = simple_prompt("Enter name of role to drop: ", true);
     116             :         }
     117             :         else
     118             :         {
     119           0 :             pg_log_error("missing required argument role name");
     120           0 :             fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     121           0 :             exit(1);
     122             :         }
     123             :     }
     124             : 
     125           4 :     if (interactive)
     126             :     {
     127           0 :         printf(_("Role \"%s\" will be permanently removed.\n"), dropuser);
     128           0 :         if (!yesno_prompt("Are you sure?"))
     129           0 :             exit(0);
     130             :     }
     131             : 
     132           4 :     initPQExpBuffer(&sql);
     133           8 :     appendPQExpBuffer(&sql, "DROP ROLE %s%s;",
     134           4 :                       (if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
     135             : 
     136           4 :     conn = connectDatabase("postgres", host, port, username, prompt_password,
     137             :                            progname, echo, false, false);
     138             : 
     139           4 :     if (echo)
     140           0 :         printf("%s\n", sql.data);
     141           4 :     result = PQexec(conn, sql.data);
     142             : 
     143           4 :     if (PQresultStatus(result) != PGRES_COMMAND_OK)
     144             :     {
     145           2 :         pg_log_error("removal of role \"%s\" failed: %s",
     146             :                      dropuser, PQerrorMessage(conn));
     147           2 :         PQfinish(conn);
     148           2 :         exit(1);
     149             :     }
     150             : 
     151           2 :     PQclear(result);
     152           2 :     PQfinish(conn);
     153           2 :     exit(0);
     154             : }
     155             : 
     156             : 
     157             : static void
     158           2 : help(const char *progname)
     159             : {
     160           2 :     printf(_("%s removes a PostgreSQL role.\n\n"), progname);
     161           2 :     printf(_("Usage:\n"));
     162           2 :     printf(_("  %s [OPTION]... [ROLENAME]\n"), progname);
     163           2 :     printf(_("\nOptions:\n"));
     164           2 :     printf(_("  -e, --echo                show the commands being sent to the server\n"));
     165           2 :     printf(_("  -i, --interactive         prompt before deleting anything, and prompt for\n"
     166             :              "                            role name if not specified\n"));
     167           2 :     printf(_("  -V, --version             output version information, then exit\n"));
     168           2 :     printf(_("  --if-exists               don't report error if user doesn't exist\n"));
     169           2 :     printf(_("  -?, --help                show this help, then exit\n"));
     170           2 :     printf(_("\nConnection options:\n"));
     171           2 :     printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
     172           2 :     printf(_("  -p, --port=PORT           database server port\n"));
     173           2 :     printf(_("  -U, --username=USERNAME   user name to connect as (not the one to drop)\n"));
     174           2 :     printf(_("  -w, --no-password         never prompt for password\n"));
     175           2 :     printf(_("  -W, --password            force password prompt\n"));
     176           2 :     printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     177           2 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
     178           2 : }

Generated by: LCOV version 1.13