Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 37f6fd1

Browse files
committed
Fix initdb misbehavior when user mis-enters superuser password.
While testing simple_prompt() revisions, I happened to notice that current initdb behaves rather badly when --pwprompt is specified and the user miskeys the second password. It complains about the mismatch, does "rm -rf" on the data directory, and exits. The problem is that since commit c4a8812, there's a standalone backend sitting waiting for commands at that point. It gets unhappy about its datadir having gone away, and spews a PANIC message at the user, which is not nice. (And the shell then adds to the mess with meaningless bleating about a core dump...) We don't really want that sort of thing to happen unless there's an internal failure in initdb, which this surely is not. The best fix seems to be to move the collection of the password earlier, so that it's done essentially as part of argument collection, rather than at the rather ad-hoc time it was done before. Back-patch to 9.6 where the problem was introduced.
1 parent 8e1e3f9 commit 37f6fd1

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/bin/initdb/initdb.c

+14-9
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ static const char *default_text_search_config = "";
135135
static char *username = "";
136136
static bool pwprompt = false;
137137
static char *pwfilename = NULL;
138+
static char *superuser_password = NULL;
138139
static const char *authmethodhost = "";
139140
static const char *authmethodlocal = "";
140141
static bool debug = false;
@@ -255,7 +256,7 @@ static void test_config_settings(void);
255256
static void setup_config(void);
256257
static void bootstrap_template1(void);
257258
static void setup_auth(FILE *cmdfd);
258-
static void get_set_pwd(FILE *cmdfd);
259+
static void get_su_pwd(void);
259260
static void setup_depend(FILE *cmdfd);
260261
static void setup_sysviews(FILE *cmdfd);
261262
static void setup_description(FILE *cmdfd);
@@ -1544,13 +1545,17 @@ setup_auth(FILE *cmdfd)
15441545

15451546
for (line = pg_authid_setup; *line != NULL; line++)
15461547
PG_CMD_PUTS(*line);
1548+
1549+
if (superuser_password)
1550+
PG_CMD_PRINTF2("ALTER USER \"%s\" WITH PASSWORD E'%s';\n\n",
1551+
username, escape_quotes(superuser_password));
15471552
}
15481553

15491554
/*
1550-
* get the superuser password if required, and call postgres to set it
1555+
* get the superuser password if required
15511556
*/
15521557
static void
1553-
get_set_pwd(FILE *cmdfd)
1558+
get_su_pwd(void)
15541559
{
15551560
char *pwd1,
15561561
*pwd2;
@@ -1560,6 +1565,8 @@ get_set_pwd(FILE *cmdfd)
15601565
/*
15611566
* Read password from terminal
15621567
*/
1568+
printf("\n");
1569+
fflush(stdout);
15631570
pwd1 = simple_prompt("Enter new superuser password: ", 100, false);
15641571
pwd2 = simple_prompt("Enter it again: ", 100, false);
15651572
if (strcmp(pwd1, pwd2) != 0)
@@ -1609,10 +1616,7 @@ get_set_pwd(FILE *cmdfd)
16091616

16101617
}
16111618

1612-
PG_CMD_PRINTF2("ALTER USER \"%s\" WITH PASSWORD E'%s';\n\n",
1613-
username, escape_quotes(pwd1));
1614-
1615-
free(pwd1);
1619+
superuser_password = pwd1;
16161620
}
16171621

16181622
/*
@@ -3279,8 +3283,6 @@ initialize_data_directory(void)
32793283
PG_CMD_OPEN;
32803284

32813285
setup_auth(cmdfd);
3282-
if (pwprompt || pwfilename)
3283-
get_set_pwd(cmdfd);
32843286

32853287
setup_depend(cmdfd);
32863288

@@ -3569,6 +3571,9 @@ main(int argc, char *argv[])
35693571
else
35703572
printf(_("Data page checksums are disabled.\n"));
35713573

3574+
if (pwprompt || pwfilename)
3575+
get_su_pwd();
3576+
35723577
printf("\n");
35733578

35743579
initialize_data_directory();

0 commit comments

Comments
 (0)