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