Add PAM Tests and Option For Custom PAM Config Location

Поиск
Список
Период
Сортировка
Искать

Add PAM Tests and Option For Custom PAM Config Location

От:
Andrew Jackson <andrewjackson947@gmail.com>
Дата:
Вложения:
Hello,

I noticed that there are currently no tests for the PAM authentication
functionality in postgres so I came up with the attached patch to add
some. I suspect that no tests were added previously because it would
have involved using root privileges to set up the environment for the
test or just rely on the individual set up the root environment before
running the test similar to
`src/interfaces/libpq/t/004_load_balance_dns.pl` [0] and then adding
validations to skip if the environment wasn't set up.

This patch allows the PAM tests to be set up and run without root by
use of the `pam_start_confdir`[1] function that was added in linux-pam
1.4.0 [2]. This function is essentially the same as `pam_start` but
has one extra parameter that allows you to either pass NULL for the
same behavior as `pam_start` or pass a char * to the location of an
alternative location for `/etc/pam.d`. This functionality is exposed
via an added `pamconfdir` option that can be used in the pg_hba.conf
file after `pam` method is specified. This option is conditionally
available via macros on the absence or presence of the
`pam_start_confdir` function. I had to make the use of this function
conditional because its use in linux-pam is recent enough to where
some older LTS linux distros will not support it. For example rocky8
uses PAM 1.3.1 which does not container `pam_start_confdir`, rocky9
uses PAM 1.5.1 which will support it.

The tests are skipped if:
- postgres was not built with PAM
- postgres was not build with a PAM that has the `pam_start_confdir` function
- PG_EXTRA_TESTS does not include pam (similar to how LDAP and GSSAPI
tests are set up)

The tests set up a temporary pam configuration folder and only use
pamconfdir in hba. Though this means that we cannot test a non
existent pamconfdir in the HBA file I think this is still better than
no testing at all as the code paths are mostly the same.

Multiple pamservices are tested using `pam_permit` [3] and `pam_exec`
[4] pam modules. These modules are both built into the linux-pam
source tree. `pam_permit` just allows everything, more fine grained
testing can be done with `pam_exec`  which essentially passes the
password, username, etc to a custom made bash script and acceptance or
rejection of the auth request essentially comes down to a zero or non
zero return from that script.

I have tested this functionality using the make build system and using
the meson build system. I tested on nixos with both pam 1.3.1 and
1.7.2 so that I know it builds both with and without this function.

Happy to make any changes and appreciate any feedback.

Thanks,
Andrew Jackson

[0] https://github.com/postgres/postgres/blob/ec13f71049cda06ea55a67783b822d98824f4d15/src/interfaces/libpq/t/004_load_balance_dns.pl
[1] https://man7.org/linux/man-pages/man3/pam_start.3.html
[2] https://github.com/linux-pam/linux-pam/blob/master/NEWS
[3] https://man7.org/linux/man-pages/man8/pam_permit.8.html
[4] https://linux.die.net/man/8/pam_exec

Re: Add PAM Tests and Option For Custom PAM Config Location

От:
Andrew Jackson <andrewjackson947@gmail.com>
Дата:
Вложения:
Updated the patch to fix a doc build issue.
Also increased the specificity of the tests:
- increased coverage over negative test cases
- assertions over postgres logs

On Sat, Aug 29, 2026 at 3:31 PM Andrew Jackson
 wrote:
>
> Hello,
>
> I noticed that there are currently no tests for the PAM authentication
> functionality in postgres so I came up with the attached patch to add
> some. I suspect that no tests were added previously because it would
> have involved using root privileges to set up the environment for the
> test or just rely on the individual set up the root environment before
> running the test similar to
> `src/interfaces/libpq/t/004_load_balance_dns.pl` [0] and then adding
> validations to skip if the environment wasn't set up.
>
> This patch allows the PAM tests to be set up and run without root by
> use of the `pam_start_confdir`[1] function that was added in linux-pam
> 1.4.0 [2]. This function is essentially the same as `pam_start` but
> has one extra parameter that allows you to either pass NULL for the
> same behavior as `pam_start` or pass a char * to the location of an
> alternative location for `/etc/pam.d`. This functionality is exposed
> via an added `pamconfdir` option that can be used in the pg_hba.conf
> file after `pam` method is specified. This option is conditionally
> available via macros on the absence or presence of the
> `pam_start_confdir` function. I had to make the use of this function
> conditional because its use in linux-pam is recent enough to where
> some older LTS linux distros will not support it. For example rocky8
> uses PAM 1.3.1 which does not container `pam_start_confdir`, rocky9
> uses PAM 1.5.1 which will support it.
>
> The tests are skipped if:
> - postgres was not built with PAM
> - postgres was not build with a PAM that has the `pam_start_confdir` function
> - PG_EXTRA_TESTS does not include pam (similar to how LDAP and GSSAPI
> tests are set up)
>
> The tests set up a temporary pam configuration folder and only use
> pamconfdir in hba. Though this means that we cannot test a non
> existent pamconfdir in the HBA file I think this is still better than
> no testing at all as the code paths are mostly the same.
>
> Multiple pamservices are tested using `pam_permit` [3] and `pam_exec`
> [4] pam modules. These modules are both built into the linux-pam
> source tree. `pam_permit` just allows everything, more fine grained
> testing can be done with `pam_exec`  which essentially passes the
> password, username, etc to a custom made bash script and acceptance or
> rejection of the auth request essentially comes down to a zero or non
> zero return from that script.
>
> I have tested this functionality using the make build system and using
> the meson build system. I tested on nixos with both pam 1.3.1 and
> 1.7.2 so that I know it builds both with and without this function.
>
> Happy to make any changes and appreciate any feedback.
>
> Thanks,
> Andrew Jackson
>
> [0] https://github.com/postgres/postgres/blob/ec13f71049cda06ea55a67783b822d98824f4d15/src/interfaces/libpq/t/004_load_balance_dns.pl
> [1] https://man7.org/linux/man-pages/man3/pam_start.3.html
> [2] https://github.com/linux-pam/linux-pam/blob/master/NEWS
> [3] https://man7.org/linux/man-pages/man8/pam_permit.8.html
> [4] https://linux.die.net/man/8/pam_exec

Re: Add PAM Tests and Option For Custom PAM Config Location

От:
Brian Cosgrove <cosgroveb@gmail.com>
Дата:
Hi Andrew,

I reviewed the PgBouncer counterpart in #1572 [0]. This review
covers the September 4 PostgreSQL patch against d39fda1cc404.
I built with Meson 1.3.2 and PAM 1.5.3 on Linux aarch64. Five
of the 26 PAM assertions failed.

In src/test/pam/t/001_auth.pl:

> my $test_pam_exec_log = "${PostgreSQL::Test::Utils::tmp_check}/log/pam_exec.log";

Meson puts tmp_check under data/ and logs under a sibling log/
directory. The data/log/ directory does not exist. The pam_exec
cases fail with:

    /bin/sh failed: exit code 2

Please use "${PostgreSQL::Test::Utils::log_path}/pam_exec.log".
I tested that change in a temporary copy. All 26 assertions passed.

Commands:

    meson setup /tmp/pg-pam-build . \
      -Dpam=enabled -Dtap_tests=enabled
    PG_TEST_EXTRA=pam meson test -C /tmp/pg-pam-build \
      --suite setup --suite pam --print-errorlogs

In src/backend/libpq/hba.c:

> +        hbaline->pamconfdir = pstrdup(val);

Please add pamconfdir to get_hba_options() in hbafuncs.c.
The pg_hba_file_rules.options column omits this setting.
Please add a test for its output.

In src/test/pam/t/001_auth.pl:

> elsif (!$ENV{PG_TEST_EXTRA} || $ENV{PG_TEST_EXTRA} !~ /\bpam\b/)

The CI workflow omits pam from PG_TEST_EXTRA. Its Linux Autoconf
configuration includes --with-pam, but this gate skips the tests.
Please enable pam for the PAM-capable CI jobs.

In meson.build:

> +  ['pam_start_confdir', {'dependencies': [pam]}],

The func_checks loop adds pam to os_deps after a successful probe.
Frontend tools and libpq then inherit PAM link arguments.
Please put this check in the PAM detection block without adding
pam to os_deps.

Please add pam to the supported PG_TEST_EXTRA values in
doc/src/sgml/regress.sgml. The test README covers it, but the
manual omits it.

Nits:

meson.build:
> +      pam_header_fp = 'security/pam_appl.h'
> +      pam_header_fp = 'pam/pam_appl.h'

Unused.

src/backend/libpq/hba.c:46-54, PAM includes:

Unused.

src/test/pam/t/001_auth.pl:
> use FindBin;
> use lib "$FindBin::RealBin/..";
> use File::Copy;
> use File::Basename;

Unused.

src/test/pam/t/001_auth.pl, descriptions:

    pam_accept.so -> pam_permit.so
    succeds -> succeeds
    test5: postgresql5 -> postgresql4
    test6: Account rejection.

src/backend/libpq/auth.c:2081,2111,2119,2122:

Indentation.

src/test/pam/Makefile:26, src/test/pam/meson.build:16:

Extra EOF blank.

doc/src/sgml/client-auth.sgml:

    pam -> PAM
    undesitable -> undesirable
    note that note that -> note that

src/test/pam/README:

    greatr -> greater
    as well setting -> as well as setting

[0] https://github.com/pgbouncer/pgbouncer/pull/1572

Brian


FAQ