Patch libpq to use ~/.pgpass for conninfo connections - Mailing list pgsql-patches

From ljb
Subject Patch libpq to use ~/.pgpass for conninfo connections
Date
Msg-id avame0$1sm2$1@news.hub.org
Whole thread Raw
List pgsql-patches
Here is a suggested patch to libpq in PostgreSQL-7.3.1 to fix a problem
whereby the new ~/.pgpass feature does not work with conninfo-based
connections.  Without this patch, ~/.pgpass is used for the deprecated
PQsetdbLogin() connection method, but not for the recommended PQconnectdb()
or PQconnectStart() connection methods. Applying this patch should let
~/.pgpass be used to supply a password for all connection methods. It also
fixes libpgtcl's "pg_connect -conninfo {conninfo string}" and PHP's
pg_connect("conninfo string") so they will now look in ~/.pgpass.

Regarding my message of 2002-12-29, I was wrong in thinking the problem was
that DefaultPassword was "" instead of NULL. There are in fact two separate
code paths which process connection parameters, including applying defaults
and environment variables, and the ~/.pgpass code was only in one of them.
The patch below adds it to the other path, in PQconnectStart(). I'm unhappy
with the duplication, but I can't see another way to do it without disturbing
too much existing code.


--- src/interfaces/libpq/fe-connect.c.bak    2002-10-24 19:35:55.000000000 -0400
+++ src/interfaces/libpq/fe-connect.c    2003-01-04 11:18:14.000000000 -0500
@@ -303,7 +303,15 @@
     tmp = conninfo_getval(connOptions, "user");
     conn->pguser = tmp ? strdup(tmp) : NULL;
     tmp = conninfo_getval(connOptions, "password");
-    conn->pgpass = tmp ? strdup(tmp) : NULL;
+    /*
+     * Special handling for password. Note PasswordFromFile mallocs its
+     * result, so strdup is not necessary.
+     */
+    if (tmp && *tmp)
+        conn->pgpass = strdup(tmp);
+    else
+        conn->pgpass = PasswordFromFile(conn->pghost, conn->pgport,
+                                        conn->dbName, conn->pguser);
     tmp = conninfo_getval(connOptions, "connect_timeout");
     conn->connect_timeout = tmp ? strdup(tmp) : NULL;
 #ifdef USE_SSL

pgsql-patches by date:

Previous
From: Tom Lane
Date:
Subject: Re: Fix for HP-UX shared library builds
Next
From: Neil Conway
Date:
Subject: fix broken doc build