summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2016-11-15 16:17:19 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2016-11-15 16:17:19 -0500
commite9802122d42aee661113423d290d41b005a9b1b2 (patch)
tree58b79d1ff60ac64bcb50ab9722be1b9979910fcd /src
parent3e844a34b80355570a9cfb25becac561aee7cf82 (diff)
Allow DOS-style line endings in ~/.pgpass files.
On Windows, libc will mask \r\n line endings for us, since we read the password file in text mode. But that doesn't happen on Unix. People who share password files across both systems might have \r\n line endings in a file they use on Unix, so as a convenience, ignore trailing \r. Per gripe from Josh Berkus. In passing, put the existing check for empty line somewhere where it's actually useful, ie after stripping the newline not before. Vik Fearing, adjusted a bit by me Discussion: <0de37763-5843-b2cc-855e-5d0e5df25807@agliodbs.com>
Diffstat (limited to 'src')
-rw-r--r--src/interfaces/libpq/fe-connect.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 7650d3d712b..13676af24c9 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -5768,18 +5768,26 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
break;
len = strlen(buf);
- if (len == 0)
- continue;
/* Remove trailing newline */
- if (buf[len - 1] == '\n')
- buf[len - 1] = 0;
+ if (len > 0 && buf[len - 1] == '\n')
+ {
+ buf[--len] = '\0';
+ /* Handle DOS-style line endings, too, even when not on Windows */
+ if (len > 0 && buf[len - 1] == '\r')
+ buf[--len] = '\0';
+ }
+
+ if (len == 0)
+ continue;
if ((t = pwdfMatchesString(t, hostname)) == NULL ||
(t = pwdfMatchesString(t, port)) == NULL ||
(t = pwdfMatchesString(t, dbname)) == NULL ||
(t = pwdfMatchesString(t, username)) == NULL)
continue;
+
+ /* Found a match. */
ret = strdup(t);
fclose(fp);