Skip to content

Commit b203c57

Browse files
committed
Allow "-C variable" and "--describe-config" even to root users.
There's no really compelling reason to refuse to do these read-only, non-server-starting options as root, and there's at least one good reason to allow -C: pg_ctl uses -C to find out the true data directory location when pointed at a config-only directory. On Windows, this is done before dropping administrator privileges, which means that pg_ctl fails for administrators if and only if a config-only layout is used. Since the root-privilege check is done so early in startup, it's a bit awkward to check for these switches. Make the somewhat arbitrary decision that we'll only skip the root check if -C is the first switch. This is not just to make the code a bit simpler: it also guarantees that we can't misinterpret a --boot mode switch. (While AuxiliaryProcessMain doesn't currently recognize any such switch, it might have one in the future.) This is no particular problem for pg_ctl, and since the whole behavior is undocumented anyhow, it's not a documentation issue either. (--describe-config only works as the first switch anyway, so this is no restriction for that case either.) Back-patch to 9.2 where pg_ctl first began to use -C. MauMau, heavily edited by me
1 parent 2209c0f commit b203c57

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/backend/main/main.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ static void check_root(const char *progname);
5858
int
5959
main(int argc, char *argv[])
6060
{
61+
bool do_check_root = true;
62+
6163
progname = get_progname(argv[0]);
6264

6365
/*
@@ -151,7 +153,8 @@ main(int argc, char *argv[])
151153
unsetenv("LC_ALL");
152154

153155
/*
154-
* Catch standard options before doing much else
156+
* Catch standard options before doing much else, in particular before we
157+
* insist on not being root.
155158
*/
156159
if (argc > 1)
157160
{
@@ -165,12 +168,29 @@ main(int argc, char *argv[])
165168
puts("postgres (PostgreSQL) " PG_VERSION);
166169
exit(0);
167170
}
171+
172+
/*
173+
* In addition to the above, we allow "--describe-config" and "-C var"
174+
* to be called by root. This is reasonably safe since these are
175+
* read-only activities. The -C case is important because pg_ctl may
176+
* try to invoke it while still holding administrator privileges on
177+
* Windows. Note that while -C can normally be in any argv position,
178+
* if you wanna bypass the root check you gotta put it first. This
179+
* reduces the risk that we might misinterpret some other mode's -C
180+
* switch as being the postmaster/postgres one.
181+
*/
182+
if (strcmp(argv[1], "--describe-config") == 0)
183+
do_check_root = false;
184+
else if (argc > 2 && strcmp(argv[1], "-C") == 0)
185+
do_check_root = false;
168186
}
169187

170188
/*
171-
* Make sure we are not running as root.
189+
* Make sure we are not running as root, unless it's safe for the selected
190+
* option.
172191
*/
173-
check_root(progname);
192+
if (do_check_root)
193+
check_root(progname);
174194

175195
/*
176196
* Dispatch to one of various subprograms depending on first argument.

src/bin/pg_ctl/pg_ctl.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,9 +2034,11 @@ adjust_data_dir(void)
20342034
else
20352035
my_exec_path = pg_strdup(exec_path);
20362036

2037-
snprintf(cmd, MAXPGPATH, SYSTEMQUOTE "\"%s\" %s%s -C data_directory" SYSTEMQUOTE,
2038-
my_exec_path, pgdata_opt ? pgdata_opt : "", post_opts ?
2039-
post_opts : "");
2037+
/* it's important for -C to be the first option, see main.c */
2038+
snprintf(cmd, MAXPGPATH, SYSTEMQUOTE "\"%s\" -C data_directory %s%s" SYSTEMQUOTE,
2039+
my_exec_path,
2040+
pgdata_opt ? pgdata_opt : "",
2041+
post_opts ? post_opts : "");
20402042

20412043
fd = popen(cmd, "r");
20422044
if (fd == NULL || fgets(filename, sizeof(filename), fd) == NULL)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy