[PATCH] libpq: Add PQpassfileLookup()
Hi hackers,
This grew out of the portaddr thread [1]/messages/by-id/70436540-9d77-4014-89af-462881ed18d9@gmail.com. The problem: an application
that connects through an intermediary, such as a local SSH tunnel on a
dynamic port, connects to a host and port that no longer match the
password file entry written for the real server, so the .pgpass lookup
comes up empty — and the only way out today is to reimplement the
passfile parser on the client side.
After portaddr was withdrawn, Denis Smirnov suggested exposing the
existing lookup as a public API instead [2]/messages/by-id/B2EDB5AE-27F7-4580-871E-1C2433BAEF18@gmail.com:
I think a better solution would be to expose the existing
passwordFromFile() code through a new public libpq API, perhaps
PQpassfileLookup().Then pgcli could look up the password using the original host and
port, and connect to the local tunnel port with that password. This
would avoid both a new connection parameter and a separate .pgpass
parser in each client.
The attached patch does exactly that. The function is a thin wrapper
around the existing passwordFromFile():
char *PQpassfileLookup(const char *hostname, const char *port,
const char *dbname, const char *username,
const char *passfile);
The first four arguments correspond to the fields of a passfile line
and behave as they do during connection establishment: a NULL or empty
hostname is matched as "localhost" (including the default socket
directory rule), and a NULL or empty port becomes the compiled-in
default. dbname and username get no defaults — resolving those belongs
to the conninfo layer, and a caller of this function already knows
them. If passfile is NULL or empty, PGPASSFILE and then the default
location are used, as for a connection.
The return value is malloc'd (to be freed with PQfreemem), or NULL
when no password could be found. The permission check also behaves
exactly as during connection establishment: warn on stderr and ignore
the file.
For tests, I added a --passfile mode to libpq_testclient and a new TAP
test (t/007_passfile.pl) covering matching, wildcards, escaping and
de-escaping, the localhost rule, PGPASSFILE, and the permission check.
The lookup is purely client-side, so none of it needs a server.
To show it end to end, a small PoC is attached too
(poc-passfilelookup.c). The setup: a server on 127.0.0.1:5440, this
passfile line, written before any tunnel exists:
127.0.0.1:5440:postgres:tuser:sekret
and a "tunnel" (a TCP relay) that binds port 0, so the kernel picks
the local port at that moment — 41395 on this run. The connection
through the tunnel cannot use the passfile today, with or without this
patch applied (nothing changes in connection establishment):
$ psql -w "host=127.0.0.1 port=41395 user=tuser dbname=postgres"
psql: error: connection to server at "127.0.0.1", port 41395
failed: fe_sendauth: no password supplied
With the patch, the client makes the two calls — look up under the
real host and port, connect to the local one:
$ ./poc-passfilelookup 127.0.0.1 5440 postgres tuser 41395
PQpassfileLookup("127.0.0.1", "5440", "postgres", "tuser", NULL) =
"sekret"
connected to 127.0.0.1:41395 as tuser -> OK
Against an unpatched libpq the PoC does not even link (undefined
reference to `PQpassfileLookup'), which is a fair summary of the
status quo: the only way a client can do this today is by parsing the
passfile itself.
I'll register this in the next open CommitFest.
[1]: /messages/by-id/70436540-9d77-4014-89af-462881ed18d9@gmail.com
[2]: /messages/by-id/B2EDB5AE-27F7-4580-871E-1C2433BAEF18@gmail.com
Thank you,
BR,
Diego
Hi Diego,
I reviewed v1 (CF 7261). Thanks for writing this, and thanks to Denis
for the original suggestion.
I think exposing the existing passfile lookup is a useful API for the
SSH-tunnel/dynamic-port case.
Just found a couple of issues and some smaller test/documentation points.
1. Error handling is lost by PQpassfileLookup()
The new function calls:
const char *errmsg;
return passwordFromFile(hostname, port, dbname, username,
passfile, &errmsg);
but never examines errmsg. passwordFromFile() uses it to distinguish
an allocation failure from an ordinary lookup miss. The connection
path turns that error into CONNECTION_BAD, whereas PQpassfileLookup()
returns NULL in both cases.
The documentation currently says:
NULL if no password could be found
which does not make the distinction clear.
I think the simplest option is to keep the proposed public signature
and document that NULL is returned if no matching password is found or
if the lookup cannot be completed, for example because of an allocation
failure. Alternatively, the public API would need an error output
parameter.
The local errmsg variable is still required by the current internal
passwordFromFile() signature unless that function is changed to permit
a NULL error pointer.
2. PGPASSFILE can be silently truncated
When passfile is NULL or empty, the wrapper copies PGPASSFILE into a
MAXPGPATH-sized buffer:
strlcpy(pgpassfile, pgpassenv, sizeof(pgpassfile));
A value longer than MAXPGPATH is therefore silently truncated. This
differs from normal connection option processing, where the value of
PGPASSFILE is dynamically allocated.
There is no need to copy the environment value here. passfile can
point directly to pgpassenv; the local buffer is needed only when
constructing the default path.
3. Test coverage for argument defaults
The TAP test covers matching, wildcards, first-match-wins, escaping,
PGPASSFILE, and the permissions check. A few behaviours documented by
the new API are not exercised:
* NULL and empty port should use DEF_PGPORT_STR. The current
localhost test uses a wildcard port, so it does not test that
default. The test should account for a non-standard compiled-in
port rather than assuming 5432.
* NULL dbname and username are tested, but their empty-string forms
are not.
* NULL passfile with PGPASSFILE is tested, but empty passfile and the
fallback to the default password file location are not. The test
client already preserves an empty argument, so the former only
needs another TAP case.
* The documented default-socket-directory-to-localhost rule is not
currently tested.
The last two may require some platform-specific handling, but the
default-port and empty-argument cases should be straightforward.
I am less sure about this test:
*:*:*:fallback:fallpw:anything after an unescaped colon is ignored
The documented format requires a colon in a field to be escaped.
Testing that trailing fields are silently ignored appears to preserve
an undocumented parser behaviour. Since that behaviour is inherited
from passwordFromFile() rather than introduced by this API, I would
prefer to leave it out of this patch.
A couple of smaller points:
* test_passfile_lookup() checks argc < 7 and silently accepts extra
arguments. argc != 7 would match the usage string more closely.
* The C comment describing hostname defaults does not mention the
default socket directory rule that is described in the SGML docs.
* The returned allocation contains a plaintext password.
PQfreemem() does not erase it, whereas libpq explicitly clears
passwords stored in PGconn before freeing them. It would be useful
for the documentation to mention that callers are responsible for
securely clearing the result where appropriate.
The rest looks good to me: reusing passwordFromFile(), the feature
macro, exports.txt entry, Meson integration, and the general test
structure all look appropriate. Connection establishment remains
unchanged, as intended.
I will mark the CF entry as Waiting on Author. Happy to review the
next version!
Best regards,
Yuriy Grigoryev
Hi Jurij,
Thanks for the great review! v2 attached: all your points addressed,
plus a rebase onto current master.
I think the simplest option is to keep the proposed public signature
and document that NULL is returned if no matching password is found or
if the lookup cannot be completed, for example because of an allocation
failure. Alternatively, the public API would need an error output
parameter.
Done the simple way: the docs and the function's comment now say NULL
means no matching password was found or the lookup could not be
completed. I also took your other suggestion and made
passwordFromFile() accept a NULL error pointer, so the wrapper no
longer needs a dummy variable.
There is no need to copy the environment value here. passfile can
point directly to pgpassenv; the local buffer is needed only when
constructing the default path.
Done exactly that way, the truncation is gone.
* NULL and empty port should use DEF_PGPORT_STR. The current
localhost test uses a wildcard port, so it does not test that
default. The test should account for a non-standard compiled-in
port rather than assuming 5432.
Added. The test client grew a --passfile-defaults mode that prints
DEF_PGPORT_STR and DEFAULT_PGSOCKET_DIR, and the TAP test writes its
passfile entry under whatever port that reports, so no 5432 assumption
anywhere. NULL and empty port are both covered. I tried it against a
build configured with -Dpgport=5499 and it passes there too.
* NULL dbname and username are tested, but their empty-string forms
are not.
Added both. Small deviation here: the test client maps an argument of
"=" to an empty string instead of taking a real empty argument. No
TAP test in the tree passes an empty argv element today, and I did not
want to find out on the Windows CI whether one survives process
spawning there.
* NULL passfile with PGPASSFILE is tested, but empty passfile and the
fallback to the default password file location are not.
Added both: an empty passfile falling back to PGPASSFILE, and a NULL
passfile with no PGPASSFILE falling back to ~/.pgpass, by pointing
HOME at a temporary directory (skipped on Windows, where the default
location does not come from the environment).
* The documented default-socket-directory-to-localhost rule is not
currently tested.
Added, using the socket directory reported by --passfile-defaults
(skipped if the platform reports none).
Testing that trailing fields are silently ignored appears to preserve
an undocumented parser behaviour. Since that behaviour is inherited
from passwordFromFile() rather than introduced by this API, I would
prefer to leave it out of this patch.
Agreed, I removed the test case and its passfile line.
* test_passfile_lookup() checks argc < 7 and silently accepts extra
arguments. argc != 7 would match the usage string more closely.
Changed.
* The C comment describing hostname defaults does not mention the
default socket directory rule that is described in the SGML docs.
Added.
* The returned allocation contains a plaintext password.
PQfreemem() does not erase it, whereas libpq explicitly clears
passwords stored in PGconn before freeing them. It would be useful
for the documentation to mention that callers are responsible for
securely clearing the result where appropriate.
Added a paragraph to the docs: the result is a cleartext password,
PQfreemem() does not erase it, and callers that do not want it
lingering in memory should overwrite it before freeing.
v2 also wraps the doc synopsis like the neighboring entries do, and
now spells out that PGPASSFILE is the only environment variable the
function looks at (PGHOST and PGPORT are not applied to the lookup
keys).
I'll move the CF entry back to Needs review.
Thank you,
BR,
Diego
Hi Diego,
Thanks for the updated patch. I went through v2 and confirmed that all
the points from my previous review have been addressed. CI is green,
the implementation, tests, and API boundary look good to me, and I
found no further issues.
Noticed one small wording issue in both the commit message and the
documentation. They say that PGPASSFILE is the only environment
variable consulted by PQpassfileLookup(). Strictly speaking, when the
default password file location is used, pqGetHomeDirectory() consults
HOME on Unix. The new TAP test relies on this behavior as well.
Perhaps this could instead say:
Other libpq connection-parameter environment variables are not
applied to the lookup keys; in particular, PGHOST and PGPORT are
ignored.
This is only a documentation wording issue; the implementation is
correct. With that adjusted, the patch looks ready for committer review
from my side. Since this introduces a new public libpq API, additional
opinions on the API are of course welcome.
Best regards,
Yuriy Grigoryev
________________________________________
От: Diego <mrstephenamell@gmail.com>
Отправлено: 9 сентября 2026 г. 19:33:16
Кому: Григорьев Юрий
Копия: Denis Smirnov; Jacob Champion; pgsql-hackers@lists.postgresql.org
Тема: Re: [PATCH] libpq: Add PQpassfileLookup()
Hi Jurij,
Thanks for the great review! v2 attached: all your points addressed,
plus a rebase onto current master.
I think the simplest option is to keep the proposed public signature
and document that NULL is returned if no matching password is found or
if the lookup cannot be completed, for example because of an allocation
failure. Alternatively, the public API would need an error output
parameter.
Done the simple way: the docs and the function's comment now say NULL
means no matching password was found or the lookup could not be
completed. I also took your other suggestion and made
passwordFromFile() accept a NULL error pointer, so the wrapper no
longer needs a dummy variable.
There is no need to copy the environment value here. passfile can
point directly to pgpassenv; the local buffer is needed only when
constructing the default path.
Done exactly that way, the truncation is gone.
* NULL and empty port should use DEF_PGPORT_STR. The current
localhost test uses a wildcard port, so it does not test that
default. The test should account for a non-standard compiled-in
port rather than assuming 5432.
Added. The test client grew a --passfile-defaults mode that prints
DEF_PGPORT_STR and DEFAULT_PGSOCKET_DIR, and the TAP test writes its
passfile entry under whatever port that reports, so no 5432 assumption
anywhere. NULL and empty port are both covered. I tried it against a
build configured with -Dpgport=5499 and it passes there too.
* NULL dbname and username are tested, but their empty-string forms
are not.
Added both. Small deviation here: the test client maps an argument of
"=" to an empty string instead of taking a real empty argument. No
TAP test in the tree passes an empty argv element today, and I did not
want to find out on the Windows CI whether one survives process
spawning there.
* NULL passfile with PGPASSFILE is tested, but empty passfile and the
fallback to the default password file location are not.
Added both: an empty passfile falling back to PGPASSFILE, and a NULL
passfile with no PGPASSFILE falling back to ~/.pgpass, by pointing
HOME at a temporary directory (skipped on Windows, where the default
location does not come from the environment).
* The documented default-socket-directory-to-localhost rule is not
currently tested.
Added, using the socket directory reported by --passfile-defaults
(skipped if the platform reports none).
Testing that trailing fields are silently ignored appears to preserve
an undocumented parser behaviour. Since that behaviour is inherited
from passwordFromFile() rather than introduced by this API, I would
prefer to leave it out of this patch.
Agreed, I removed the test case and its passfile line.
* test_passfile_lookup() checks argc < 7 and silently accepts extra
arguments. argc != 7 would match the usage string more closely.
Changed.
* The C comment describing hostname defaults does not mention the
default socket directory rule that is described in the SGML docs.
Added.
* The returned allocation contains a plaintext password.
PQfreemem() does not erase it, whereas libpq explicitly clears
passwords stored in PGconn before freeing them. It would be useful
for the documentation to mention that callers are responsible for
securely clearing the result where appropriate.
Added a paragraph to the docs: the result is a cleartext password,
PQfreemem() does not erase it, and callers that do not want it
lingering in memory should overwrite it before freeing.
v2 also wraps the doc synopsis like the neighboring entries do, and
now spells out that PGPASSFILE is the only environment variable the
function looks at (PGHOST and PGPORT are not applied to the lookup
keys).
I'll move the CF entry back to Needs review.
Thank you,
BR,
Diego
Hi Diego,
Thanks for v2!
* The returned allocation contains a plaintext password.
PQfreemem() does not erase it, whereas libpq explicitly clears
passwords stored in PGconn before freeing them. It would be useful
for the documentation to mention that callers are responsible for
securely clearing the result where appropriate.
The new docs now say this, but passwordFromFile() leaves part of the
original password after removing escapes in place:
Password in .pgpass: pa\\ss\:word
Returned buffer: pa\ss:word\0d\0
A caller using explicit_bzero(password, strlen(password)) before
PQfreemem() leaves the final 'd' untouched. Could we zero this tail in
passwordFromFile() before returning? The caller does not know the
original allocation size.
Best regards,
Denis Smirnov
Hi Denis, hi Yuriy,
Thanks to both of you.v3 attached, now as a series of two patches:
- v3-0001 fixes the residue Denis found, in passwordFromFile() itself.
- v3-0002 is the API patch, rebased on 0001, with Yuriy's wording.
The new docs now say this, but passwordFromFile() leaves part of the
original password after removing escapes in place:
Password in .pgpass: pa\\ss\:word
Returned buffer:pa\ss:word\0d\0
A caller using explicit_bzero(password, strlen(password)) before
PQfreemem() leaves the final 'd' untouched. Could we zero this tail in
passwordFromFile() before returning? The caller does not know the
original allocation size.
Confirmed, and it is a bit wider than the last character: strdup(t)
copies the rest of the line, and the in-place loop only overwrites the
de-escaped length, so everything after that point -- the tail of the
escaped password and any further fields on the line -- survives past
the terminator.With a line such as
host:5432:db:user:pw:extra:fields:here
the allocation ends up as "pw\0extra:fields:here\0".
It is also not specific to the new function.Connection establishment
stores the same allocation in conn->connhost[i].password, and
pqReleaseConnHosts() clears it with explicit_bzero(p, strlen(p)), so the
same bytes have been left behind in libpq's own cleanup: they have been
there since the de-escaping was added in 8d15e3ec4fc (2011), and the
explicit_bzero() that fails to reach them dates from 74a308cf522 (2019).
That is why 0001 is a separate patch: it stands on its own against
master, touches only passwordFromFile(), and could be back-patched if a
committer thinks that is worth it -- I have no strong opinion either
way.For what it is worth, it cherry-picks cleanly onto REL_19_STABLE;
on REL_18_STABLE down to REL_14_STABLE the only conflict is the
function's header comment, which is a single line there, and the code
hunks apply.
Rather than zeroing the tail after the fact, 0001 moves the existing
de-escape loop above the strdup(), so it runs in place on the line
buffer -- which is already cleared with explicit_bzero(buf.data,
buf.maxlen) on every exit -- and strdup() then copies only the
de-escaped password.The loop body is unchanged, the returned string
is byte-identical, and the allocation is exactly strlen() + 1 by
construction, so there is nothing a caller needs to know.If you
would rather have a one-line explicit_bzero() of the tail instead,
that is easy to do, but it keeps the oversized copy around and the
documentation could not promise anything about strlen().
0002 depends on 0001: it does not apply on bare master, and the
sentence in its docs and commit message about overwriting strlen()
bytes is only true on top of it.Squashing the two is fine by me if a
committer prefers that; if 0001 is dropped, those sentences go with it.
How I checked it, on master @ bd124434333:
* the libpq TAP suite and authentication/001_password, on 0001 alone
and on the full series: all green (007_passfile now has 20
subtests), no new compiler warnings, pgindent clean.
* an out-of-tree corpus of 21 lookups against a password file that
covers the escaping corners (your example, a lone trailing
backslash, an escaped colon as the last character, an empty
password, fields after the password, a 10 kB password, 3000 escaped
colons, a CRLF line, a four-field line, a wildcard line) run
through v2 and v3: identical output and exit code for all 21.
* a small harness that searches for the expected leftover bytes past
the terminator, within malloc_usable_size() and under
MALLOC_PERTURB_ so untouched slack cannot be mistaken for data: on
v2 it finds them in 5 of the 21 lines ("d" in your example,
"extra:fields:here", 2999 bytes of the escaped-colon case); on v3
it finds them in none.
There is no in-tree test for this, because nothing public can observe
bytes past the terminator without undefined behaviour; the corpus and
the harness are outside the tree.
With that, the sentence in the docs about clearing the result became a
real contract: the string holds nothing but the password and libpq
writes nothing past its terminating zero byte, so overwriting strlen()
bytes before freeing it is sufficient.The comment above
PQpassfileLookup() says the same.
Noticed one small wording issue in both the commit message and the
documentation.They say that PGPASSFILE is the only environment
variable consulted by PQpassfileLookup().Strictly speaking, when the
default password file location is used, pqGetHomeDirectory() consults
HOME on Unix.The new TAP test relies on this behavior as well.
Perhaps this could instead say:
Other libpq connection-parameter environment variables are not
applied to the lookup keys; in particular, PGHOST and PGPORT are
ignored.
Right -- fixed with your sentence, verbatim, in both the docs and the
commit message, and "the default password file location" in the docs
now points at the pgpass section, which already covers HOME (and
%APPDATA% on Windows).I also added a TAP case for an escaped colon
at the end of the password.I did not add one for fields after the
password, per your earlier point about not testing undocumented parser
behaviour; the out-of-tree corpus above includes that line.
One thing I expect to be asked, so let me say it up front: the new
function has no error channel.A lookup that finds nothing, a missing
or badly-permissioned file, no home directory, and an allocation
failure all come back as NULL.That mirrors what connection
establishment does when the password file yields nothing -- the connect
path only turns the out-of-memory case into a hard error -- and it
keeps the function a plain wrapper around the existing lookup.If an
error out-parameter is preferred I can add one; I did not want to
design more API than the use case needs.Relatedly, the default-file
fallback in PQpassfileLookup() repeats a few lines of the connect path;
I can factor a small static helper if that is wanted.
Both patches apply on master in order, most recently checked against
a4f18fd8f28.I'll leave the CF entry at Needs review.
Thanks,
Diego
Attachments:
t253677_6v3-0002-libpq-Add-PQpassfileLookup.patchtext/x-patch; charset=UTF-8; name=v3-0002-libpq-Add-PQpassfileLookup.patchDownload+417-6
v3-0001-libpq-Do-not-leave-password-residue-in-passwordFr.patchtext/x-patch; charset=UTF-8; name=v3-0001-libpq-Do-not-leave-password-residue-in-passwordFr.patchDownload+21-10
Hi Diego,
Thanks for v3. Both patches look good to me. I have no further
comments.
One separate note for future work: even after 0001, copies of the
password can still remain in memory:
- The FILE buffer holds the whole .pgpass file, and fclose() frees
it without clearing it.
- On long lines, realloc() in enlargePQExpBuffer() can leave old
copies behind.
- markPQExpBufferBroken() also frees memory without clearing it
when an allocation fails.
- SCRAM and cleartext authentication make more copies later.
These issues already existed, and I don't see a practical way to
exploit them. They should not delay the new API. Maybe you would
like to look at them separately later.
Best regards,
Denis Smirnov
Show quoted text
On 16 Sep 2026, at 20:39, Diego <mrstephenamell@gmail.com> wrote:
Hi Denis, hi Yuriy,
Thanks to both of you.v3 attached, now as a series of two patches:
- v3-0001 fixes the residue Denis found, in passwordFromFile() itself.
- v3-0002 is the API patch, rebased on 0001, with Yuriy's wording.The new docs now say this, but passwordFromFile() leaves part of the
original password after removing escapes in place:
Password in .pgpass: pa\\ss\:word
Returned buffer:pa\ss:word\0d\0
A caller using explicit_bzero(password, strlen(password)) before
PQfreemem() leaves the final 'd' untouched. Could we zero this tail in
passwordFromFile() before returning? The caller does not know the
original allocation size.Confirmed, and it is a bit wider than the last character: strdup(t)
copies the rest of the line, and the in-place loop only overwrites the
de-escaped length, so everything after that point -- the tail of the
escaped password and any further fields on the line -- survives past
the terminator.With a line such ashost:5432:db:user:pw:extra:fields:here
the allocation ends up as "pw\0extra:fields:here\0".
It is also not specific to the new function.Connection establishment
stores the same allocation in conn->connhost[i].password, and
pqReleaseConnHosts() clears it with explicit_bzero(p, strlen(p)), so the
same bytes have been left behind in libpq's own cleanup: they have been
there since the de-escaping was added in 8d15e3ec4fc (2011), and the
explicit_bzero() that fails to reach them dates from 74a308cf522 (2019).
That is why 0001 is a separate patch: it stands on its own against
master, touches only passwordFromFile(), and could be back-patched if a
committer thinks that is worth it -- I have no strong opinion either
way.For what it is worth, it cherry-picks cleanly onto REL_19_STABLE;
on REL_18_STABLE down to REL_14_STABLE the only conflict is the
function's header comment, which is a single line there, and the code
hunks apply.Rather than zeroing the tail after the fact, 0001 moves the existing
de-escape loop above the strdup(), so it runs in place on the line
buffer -- which is already cleared with explicit_bzero(buf.data,
buf.maxlen) on every exit -- and strdup() then copies only the
de-escaped password.The loop body is unchanged, the returned string
is byte-identical, and the allocation is exactly strlen() + 1 by
construction, so there is nothing a caller needs to know.If you
would rather have a one-line explicit_bzero() of the tail instead,
that is easy to do, but it keeps the oversized copy around and the
documentation could not promise anything about strlen().0002 depends on 0001: it does not apply on bare master, and the
sentence in its docs and commit message about overwriting strlen()
bytes is only true on top of it.Squashing the two is fine by me if a
committer prefers that; if 0001 is dropped, those sentences go with it.How I checked it, on master @ bd124434333:
* the libpq TAP suite and authentication/001_password, on 0001 alone
and on the full series: all green (007_passfile now has 20
subtests), no new compiler warnings, pgindent clean.
* an out-of-tree corpus of 21 lookups against a password file that
covers the escaping corners (your example, a lone trailing
backslash, an escaped colon as the last character, an empty
password, fields after the password, a 10 kB password, 3000 escaped
colons, a CRLF line, a four-field line, a wildcard line) run
through v2 and v3: identical output and exit code for all 21.
* a small harness that searches for the expected leftover bytes past
the terminator, within malloc_usable_size() and under
MALLOC_PERTURB_ so untouched slack cannot be mistaken for data: on
v2 it finds them in 5 of the 21 lines ("d" in your example,
"extra:fields:here", 2999 bytes of the escaped-colon case); on v3
it finds them in none.There is no in-tree test for this, because nothing public can observe
bytes past the terminator without undefined behaviour; the corpus and
the harness are outside the tree.With that, the sentence in the docs about clearing the result became a
real contract: the string holds nothing but the password and libpq
writes nothing past its terminating zero byte, so overwriting strlen()
bytes before freeing it is sufficient.The comment above
PQpassfileLookup() says the same.Noticed one small wording issue in both the commit message and the
documentation.They say that PGPASSFILE is the only environment
variable consulted by PQpassfileLookup().Strictly speaking, when the
default password file location is used, pqGetHomeDirectory() consults
HOME on Unix.The new TAP test relies on this behavior as well.
Perhaps this could instead say:
Other libpq connection-parameter environment variables are not
applied to the lookup keys; in particular, PGHOST and PGPORT are
ignored.Right -- fixed with your sentence, verbatim, in both the docs and the
commit message, and "the default password file location" in the docs
now points at the pgpass section, which already covers HOME (and
%APPDATA% on Windows).I also added a TAP case for an escaped colon
at the end of the password.I did not add one for fields after the
password, per your earlier point about not testing undocumented parser
behaviour; the out-of-tree corpus above includes that line.One thing I expect to be asked, so let me say it up front: the new
function has no error channel.A lookup that finds nothing, a missing
or badly-permissioned file, no home directory, and an allocation
failure all come back as NULL.That mirrors what connection
establishment does when the password file yields nothing -- the connect
path only turns the out-of-memory case into a hard error -- and it
keeps the function a plain wrapper around the existing lookup.If an
error out-parameter is preferred I can add one; I did not want to
design more API than the use case needs.Relatedly, the default-file
fallback in PQpassfileLookup() repeats a few lines of the connect path;
I can factor a small static helper if that is wanted.Both patches apply on master in order, most recently checked against
a4f18fd8f28.I'll leave the CF entry at Needs review.Thanks,
Diego<v3-0002-libpq-Add-PQpassfileLookup.patch><v3-0001-libpq-Do-not-leave-password-residue-in-passwordFr.patch>
Hi Denis,
Thank you, and thanks for the review that found the residue in the first
place.
One separate note for future work: even after 0001, copies of the
password can still remain in memory:
Agreed on all four, and I had noticed two of them while checking 0001: on
an allocation failure, enlargePQExpBuffer() ends up in
markPQExpBufferBroken(), which free()s the block still holding the partially
read line and sets maxlen to 0, so the explicit_bzero(buf.data, buf.maxlen)
further down becomes a no-op on the replacement buffer. I left it alone
because fixing it properly means touching pqexpbuffer.c, which is a
different patch and a different discussion.
The stdio buffer is the largest of the four by far, it can hold the whole
file, not just one line, and it is also the only one that is there on
every successful lookup rather than only on an error path. If nobody
objects I will look at that one separately once this is settled, probably
with setvbuf() on an owned buffer that can be cleared before fclose().
I'll leave those out of this thread so they do not hold up the API. Since
you and Yuriy are both happy with the series, I'll move the CF entry to
Ready for Committer.
Thanks,
Diego