LCOV - code coverage report
Current view: top level - src/bin/scripts - dropuser.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 96 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 2 0
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.3.2-1