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

Commit d9720e4

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 7961c31 commit d9720e4

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/bin/initdb/initdb.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ static const char *default_text_search_config = "";
134134
static char *username = "";
135135
static bool pwprompt = false;
136136
static char *pwfilename = NULL;
137+
static char *superuser_password = NULL;
137138
static const char *authmethodhost = "";
138139
static const char *authmethodlocal = "";
139140
static bool debug = false;
@@ -254,7 +255,7 @@ static void test_config_settings(void);
254255
static void setup_config(void);
255256
static void bootstrap_template1(void);
256257
static void setup_auth(FILE *cmdfd);
257-
static void get_set_pwd(FILE *cmdfd);
258+
static void get_su_pwd(void);
258259
static void setup_depend(FILE *cmdfd);
259260
static void setup_sysviews(FILE *cmdfd);
260261
static void setup_description(FILE *cmdfd);
@@ -1551,13 +1552,17 @@ setup_auth(FILE *cmdfd)
15511552

15521553
for (line = pg_authid_setup; *line != NULL; line++)
15531554
PG_CMD_PUTS(*line);
1555+
1556+
if (superuser_password)
1557+
PG_CMD_PRINTF2("ALTER USER \"%s\" WITH PASSWORD E'%s';\n\n",
1558+
username, escape_quotes(superuser_password));
15541559
}
15551560

15561561
/*
1557-
* get the superuser password if required, and call postgres to set it
1562+
* get the superuser password if required
15581563
*/
15591564
static void
1560-
get_set_pwd(FILE *cmdfd)
1565+
get_su_pwd(void)
15611566
{
15621567
char *pwd1,
15631568
*pwd2;
@@ -1567,6 +1572,8 @@ get_set_pwd(FILE *cmdfd)
15671572
/*
15681573
* Read password from terminal
15691574
*/
1575+
printf("\n");
1576+
fflush(stdout);
15701577
pwd1 = simple_prompt("Enter new superuser password: ", 100, false);
15711578
pwd2 = simple_prompt("Enter it again: ", 100, false);
15721579
if (strcmp(pwd1, pwd2) != 0)
@@ -1616,10 +1623,7 @@ get_set_pwd(FILE *cmdfd)
16161623

16171624
}
16181625

1619-
PG_CMD_PRINTF2("ALTER USER \"%s\" WITH PASSWORD E'%s';\n\n",
1620-
username, escape_quotes(pwd1));
1621-
1622-
free(pwd1);
1626+
superuser_password = pwd1;
16231627
}
16241628

16251629
/*
@@ -3286,8 +3290,6 @@ initialize_data_directory(void)
32863290
PG_CMD_OPEN;
32873291

32883292
setup_auth(cmdfd);
3289-
if (pwprompt || pwfilename)
3290-
get_set_pwd(cmdfd);
32913293

32923294
setup_depend(cmdfd);
32933295

@@ -3575,6 +3577,9 @@ main(int argc, char *argv[])
35753577
else
35763578
printf(_("Data page checksums are disabled.\n"));
35773579

3580+
if (pwprompt || pwfilename)
3581+
get_su_pwd();
3582+
35783583
printf("\n");
35793584

35803585
initialize_data_directory();

0 commit comments

Comments
 (0)