]> granicus.if.org Git - postgresql/commitdiff
Allow DOS-style line endings in ~/.pgpass files.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 15 Nov 2016 21:17:19 +0000 (16:17 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 15 Nov 2016 21:17:19 +0000 (16:17 -0500)
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>

src/interfaces/libpq/fe-connect.c

index 553cf7660f78ed97cb4c968430eeead5917d526c..25a33eccd4df28230f75a1e2e8c7d2e004916427 100644 (file)
@@ -5698,18 +5698,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);