8000 Allow arbitrary length passwords via CLI rather than limiting · postgres/postgres@029d8f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 029d8f7

Browse files
committed
Allow arbitrary length passwords via CLI rather than limiting
to 100 characters.
1 parent c9d7004 commit 029d8f7

File tree

10 files changed

+100
-41
lines changed

10 files changed

+100
-41
lines changed

src/bin/initdb/initdb.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,8 +1303,8 @@ get_set_pwd(void)
13031303
/*
13041304
* Read password from terminal
13051305
*/
1306-
pwd1 = simple_prompt("Enter new superuser password: ", 100, false);
1307-
pwd2 = simple_prompt("Enter it again: ", 100, false);
1306+
pwd1 = simple_prompt("Enter new superuser password: ", MAX_PASSWD, false);
1307+
pwd2 = simple_prompt("Enter it again: ", MAX_PASSWD, false);
13081308
if (strcmp(pwd1, pwd2) != 0)
13091309
{
13101310
fprintf(stderr, _("Passwords didn't match.\n"));
@@ -1323,7 +1323,7 @@ get_set_pwd(void)
13231323
* for now.
13241324
*/
13251325
FILE *pwf = fopen(pwfilename, "r");
1326-
char pwdbuf[MAXPGPATH];
1326+
char *pwdbuf = calloc(1,1), buf[1024];
13271327
int i;
13281328

13291329
if (!pwf)
@@ -1332,18 +1332,34 @@ get_set_pwd(void)
13321332
progname, pwfilename, strerror(errno));
13331333
exit_nicely();
13341334
}
1335-
if (!fgets(pwdbuf, sizeof(pwdbuf), pwf))
1335+
1336+
do
1337+
{
1338+
if (fgets(buf, sizeof(buf), pwf) == NULL)
1339+
break;
1340+
pwdbuf = realloc( pwdbuf, strlen(pwdbuf)+1+strlen(buf) );
1341+
if (!pwdbuf)
1342+
{
1343+
// Out of memory ?
1344+
fprintf(stderr, _("%s: could not read password from file \"%s\": %s\n"),
1345+
progname, pwfilename, strerror(errno));
1346+
exit_nicely();
1347+
}
1348+
strcat( pwdbuf, buf);
1349+
i = strlen(pwdbuf);
1350+
} while (strlen(buf) > 0 && pwdbuf[i-1] != '\n');
1351+
1352+
while (i > 0 && (pwdbuf[i - 1] == '\r' || pwdbuf[i - 1] == '\n'))
1353+
pwdbuf[--i] = '\0';
1354+
1355+
if (!i)
13361356
{
13371357
fprintf(stderr, _("%s: could not read password from file \"%s\": %s\n"),
13381358
progname, pwfilename, strerror(errno));
13391359
exit_nicely();
13401360
}
13411361
fclose(pwf);
13421362

1343-
i = strlen(pwdbuf);
1344-
while (i > 0 && (pwdbuf[i - 1] == '\r' || pwdbuf[i - 1] == '\n'))
1345-
pwdbuf[--i] = '\0';
1346-
13471363
pwd1 = xstrdup(pwdbuf);
13481364

13491365
}

src/bin/pg_dump/pg_backup_db.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
143143

144144
if (AH->promptPassword == TRI_YES && password == NULL)
145145
{
146-
password = simple_prompt("Password: ", 100, false);
146+
password = simple_prompt("Password: ", MAX_PASSWD, false);
147147
if (password == NULL)
148148
die_horribly(AH, modulename, "out of memory\n");
149149
}
@@ -195,7 +195,7 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
195195
free(password);
196196

197197
if (AH->promptPassword != TRI_NO)
198-
password = simple_prompt("Password: ", 100, false);
198+
password = simple_prompt("Password: ", MAX_PASSWD, false);
199199
else
200200
die_horribly(AH, modulename, "connection needs password\n");
201201

@@ -242,7 +242,7 @@ ConnectDatabase(Archive *AHX,
242242

243243
if (prompt_password == TRI_YES && password == NULL)
244244
{
245-
password = simple_prompt("Password: ", 100, false);
245+
password = simple_prompt("Password: ", MAX_PASSWD, false);
246246
if (password == NULL)
247247
die_horribly(AH, modulename, "out of memory\n");
248248
}
@@ -288,7 +288,7 @@ ConnectDatabase(Archive *AHX,
288288
prompt_password != TRI_NO)
289289
{
290290
PQfinish(AH->connection);
291-
password = simple_prompt("Password: ", 100, false);
291+
password = simple_prompt("Password: ", MAX_PASSWD, false);
292292
if (password == NULL)
293293
die_horribly(AH, modulename, "out of memory\n");
294294
new_pass = true;

src/bin/pg_dump/pg_dumpall.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,7 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
16871687
static char *password = NULL;
16881688

16891689
if (prompt_password == TRI_YES && !password)
1690-
password = simple_prompt("Password: ", 100, false);
1690+
password = simple_prompt("Password: ", MAX_PASSWD, false);
16911691

16921692
/*
16931693
* Start the connection. Loop until we have a password if requested by
@@ -1733,7 +1733,7 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
17331733
prompt_password != TRI_NO)
17341734
{
17351735
PQfinish(conn);
1736-
password = simple_prompt("Password: ", 100, false);
1736+
password = simple_prompt("Password: ", MAX_PASSWD, false);
17371737
new_pass = true;
17381738
}
17391739
} while (new_pass);

src/bin/psql/command.c

Expand all lines: src/bin/psql/command.c
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,8 @@ exec_command(const char *cmd,
895895
char *pw1;
896896
char *pw2;
897897

898-
pw1 = simple_prompt("Enter new password: ", 100, false);
899-
pw2 = simple_prompt("Enter it again: ", 100, false);
898+
pw1 = simple_prompt("Enter new password: ", MAX_PASSWD, false);
899+
pw2 = simple_prompt("Enter it again: ", MAX_PASSWD, false);
900900

901901
if (strcmp(pw1, pw2) != 0)
902902
{
@@ -1462,15 +1462,15 @@ prompt_for_password(const char *username)
14621462
char *result;
14631463

14641464
if (username == NULL)
1465-
result = simple_prompt("Password: ", 100, false);
1465+
result = simple_prompt("Password: ", MAX_PASSWD, false);
14661466
else
14671467
{
14681468
char *prompt_text;
14691469

14701470
prompt_text = malloc(strlen(username) + 100);
14711471
snprintf(prompt_text, strlen(username) + 100,
14721472
_("Password for user %s: "), username);
1473-
result = simple_prompt(prompt_text, 100, false);
1473+
result = simple_prompt(prompt_text, MAX_PASSWD, false);
14741474
free(prompt_text);
14751475
}
14761476

src/bin/psql/startup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ main(int argc, char *argv[])
174174
}
175175

176176
if (pset.getPassword == TRI_YES)
177-
password = simple_prompt(password_prompt, 100, false);
177+
password = simple_prompt(password_prompt, MAX_PASSWD, false);
178178

179179
/* loop until we have a password if requested by backend */
180180
do
@@ -213,7 +213,7 @@ main(int argc, char *argv[])
213213
pset.getPassword != TRI_NO)
214214
{
215215
PQfinish(pset.db);
216-
password = simple_prompt(password_prompt, 100, false);
216+
password = simple_prompt(password_prompt, MAX_PASSWD, false);
217217
new_pass = true;
218218
}
219219
} while (new_pass);

src/bin/scripts/common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
100100
bool new_pass;
101101

102102
if (prompt_password == TRI_YES)
103-
password = simple_prompt("Password: ", 100, false);
103+
password = simple_prompt("Password: ", MAX_PASSWD, false);
104104

105105
/*
106106
* Start the connection. Loop until we have a password if requested by
@@ -152,7 +152,7 @@ connectDatabase(const char *dbname, const char *pghost, const char *pgport,
152152
prompt_password != TRI_NO)
153153
{
154154
PQfinish(conn);
155-
password = simple_prompt("Password: ", 100, false);
155+
password = simple_prompt("Password: ", MAX_PASSWD, false);
156156
new_pass = true;
157157
}
158158
} while (new_pass);

src/bin/scripts/createuser.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ main(int argc, char *argv[])
197197
char *pw1,
198198
*pw2;
199199

200-
pw1 = simple_prompt("Enter password for new role: ", 100, false);
201-
pw2 = simple_prompt("Enter it again: ", 100, false);
200+
pw1 = simple_prompt("Enter password for new role: ", MAX_PASSWD, false);
201+
pw2 = simple_prompt("Enter it again: ", MAX_PASSWD, false);
202202
if (strcmp(pw1, pw2) != 0)
203203
{
204204
fprintf(stderr, _("Passwords didn't match.\n"));

src/include/pg_config_manual.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@
2222
*/
2323
#define NAMEDATALEN 64
2424

25+
/*
26+
* Maximum password length via command line tools
27+
*
28+
* If 0, no maximum password length is enforced.
29+
* If greater than 0, this defines the maximum number of characters
30+
* which will be read as input for a password prompt. Input in
31+
* excess of this maximum will be silently ignored.
32+
*
33+
* The database itself does not have a password length limit,
34+
* regardless of this setting.
35+
*
36+
*/
37+
#define MAX_PASSWD 0
38+
2539
/*
2640
* Maximum number of arguments to a function.
2741
*

src/interfaces/libpq/fe-connect.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4905,22 +4905,31 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
49054905

49064906
while (!feof(fp) && !ferror(fp))
49074907
{
4908-
char *t = buf,
4908+
char *t = calloc(1,sizeof(char)),
49094909
*ret,
49104910
*p1,
49114911
*p2;
49124912
int len;
49134913

4914-
if (fgets(buf, sizeof(buf), fp) == NULL)
4915-
break;
49164914

4917-
len = strlen(buf);
4915+
do
4916+
{
4917+
if ( fgets(buf, LINELEN, fp) == NULL)
4918+
break;
4919+
t = realloc(t, strlen(t)+1+strlen(buf));
4920+
/* Out of memory? */
4921+
if( !t )
4922+
return NULL;
4923+
strcat(t, buf);
4924+
len = strlen(t);
4925+
} while (strlen(buf) > 0 && t[len-1] != '\n');
4926+
49184927
if (len == 0)
49194928
continue;
49204929

49214930
/* Remove trailing newline */
4922-
if (buf[len - 1] == '\n')
4923-
buf[len - 1] = 0;
4931+
while ( len > 0 && (t[len-1] == '\n' || t[len-1] == '\r'))
4932+
t[--len] = 0;
49244933

49254934
if ((t = pwdfMatchesString(t, hostname)) == NULL ||
49264935
(t = pwdfMatchesString(t, port)) == NULL ||

src/port/sprompt.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ char *
3838
simple_prompt(const char *prompt, int maxlen, bool echo)
3939
{
4040
int length;
41+
int buflen;
42+
int bufsize = 1024;
4143
char *destination;
44+
char buf[bufsize];
4245
FILE *termin,
4346
*termout;
4447

@@ -52,7 +55,11 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
5255
#endif
5356
#endif
5457

55-
destination = (char *) malloc(maxlen + 1);
58+
if (maxlen > 0) {
59+
destination = (char *) calloc(1, sizeof(char));
60+
} else {
61+
destination = (char *) malloc((maxlen + 1) * sizeof(char));
62+
}
5663
if (!destination)
5764
return NULL;
5865

@@ -108,21 +115,34 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
108115
fflush(termout);
109116
}
110117

111-
if (fgets(destination, maxlen + 1, termin) == NULL)
112-
destination[0] = '\0';
113-
114-
length = strlen(destination);
115-
if (length > 0 && destination[length - 1] != '\n')
116-
{
117-
/* eat rest of the line */
118-
char buf[128];
119-
int buflen;
118+
if (maxlen > 0) {
119+
if (fgets(destination, maxlen + 1, termin) == NULL)
120+
destination[0] = '\0';
120121

122+
length = strlen(destination);
123+
if (length > 0 && destination[length - 1] != '\n')
124+
{
125+
/* eat rest of the line */
126+
do
127+
{
128+
if (fgets(buf, bufsize, termin) == NULL)
129+
break;
130+
buflen = strlen(buf);
131+
} while (buflen > 0 && buf[buflen - 1] != '\n');
132+
}
133+
134+
} else {
121135
do
122136
{
123-
if (fgets(buf, sizeof(buf), termin) == NULL)
137+
if (fgets(buf, bufsize, termin) == NULL)
124138
break;
125139
buflen = strlen(buf);
140+
destination = realloc( destination, strlen(destination)+1+buflen );
141+
/* Out of memory ? */
142+
if( !destination )
143+
return NULL;
144+
strcat( destination, buf );
145+
length = strlen(destination);
126146
} while (buflen > 0 && buf[buflen - 1] != '\n');
127147
}
128148

0 commit comments

Comments
 (0)
0