| 1 |
|
| 2 |
# Copyright (c) 2022-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings FATAL => 'all'; |
| 6 |
use File::Copy; |
| 7 |
use FindBin; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use PostgreSQL::Test::Cluster; |
| 10 |
use Test::More; |
| 11 |
|
| 12 |
use lib "$FindBin::RealBin/../../../ldap"; |
| 13 |
use LdapServer; |
| 14 |
|
| 15 |
if ($ENV{with_ldap} ne 'yes') |
| 16 |
{ |
| 17 |
plan skip_all => 'LDAP not supported by this build'; |
| 18 |
} |
| 19 |
elsif (!$ENV{PG_TEST_EXTRA} || $ENV{PG_TEST_EXTRA} !~ /\bldap\b/) |
| 20 |
{ |
| 21 |
plan skip_all => |
| 22 |
'Potentially unsafe test LDAP not enabled in PG_TEST_EXTRA'; |
| 23 |
} |
| 24 |
elsif (!$LdapServer::setup) |
| 25 |
{ |
| 26 |
plan skip_all => |
| 27 |
"ldap tests not supported on $^O or dependencies not installed"; |
| 28 |
} |
| 29 |
|
| 30 |
my $clear_ldap_rootpw = "FooBaR1"; |
| 31 |
my $rot13_ldap_rootpw = "SbbOnE1"; |
| 32 |
|
| 33 |
my $ldap = LdapServer->new($clear_ldap_rootpw, 'users'); # no anonymous auth |
| 34 |
$ldap->ldapadd_file("$FindBin::RealBin/../../../ldap/authdata.ldif"); |
| 35 |
$ldap->ldapsetpw('uid=test1,dc=example,dc=net', 'secret1'); |
| 36 |
|
| 37 |
my ($ldap_server, $ldap_port, $ldap_basedn, $ldap_rootdn) = |
| 38 |
$ldap->prop(qw(server port basedn rootdn)); |
| 39 |
|
| 40 |
|
| 41 |
note "setting up PostgreSQL instance"; |
| 42 |
|
| 43 |
my $node = PostgreSQL::Test::Cluster->new('node'); |
| 44 |
$node->init; |
| 45 |
$node->append_conf('postgresql.conf', |
| 46 |
"log_connections = 'receipt,authentication,authorization'\n"); |
| 47 |
$node->append_conf('postgresql.conf', |
| 48 |
"shared_preload_libraries = 'ldap_password_func'"); |
| 49 |
$node->start; |
| 50 |
|
| 51 |
$node->safe_psql('postgres', 'CREATE USER test1;'); |
| 52 |
|
| 53 |
note "running tests"; |
| 54 |
|
| 55 |
sub test_access |
| 56 |
{ |
| 57 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 58 |
|
| 59 |
my ($node, $role, $expected_res, $test_name, %params) = @_; |
| 60 |
my $connstr = "user=$role"; |
| 61 |
|
| 62 |
if ($expected_res eq 0) |
| 63 |
{ |
| 64 |
$node->connect_ok($connstr, $test_name, %params); |
| 65 |
} |
| 66 |
else |
| 67 |
{ |
| 68 |
# No checks of the error message, only the status code. |
| 69 |
$node->connect_fails($connstr, $test_name, %params); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
note "use ldapbindpasswd"; |
| 74 |
|
| 75 |
$ENV{"PGPASSWORD"} = 'secret1'; |
| 76 |
|
| 77 |
unlink($node->data_dir . '/pg_hba.conf'); |
| 78 |
$node->append_conf('pg_hba.conf', |
| 79 |
qq{local all all ldap ldapserver=$ldap_server ldapport=$ldap_port ldapbasedn="$ldap_basedn" ldapbinddn="$ldap_rootdn" ldapbindpasswd=wrong} |
| 80 |
); |
| 81 |
$node->restart; |
| 82 |
|
| 83 |
test_access($node, 'test1', 2, |
| 84 |
'search+bind authentication fails with wrong ldapbindpasswd'); |
| 85 |
|
| 86 |
unlink($node->data_dir . '/pg_hba.conf'); |
| 87 |
$node->append_conf('pg_hba.conf', |
| 88 |
qq{local all all ldap ldapserver=$ldap_server ldapport=$ldap_port ldapbasedn="$ldap_basedn" ldapbinddn="$ldap_rootdn" ldapbindpasswd="$clear_ldap_rootpw"} |
| 89 |
); |
| 90 |
$node->restart; |
| 91 |
|
| 92 |
test_access($node, 'test1', 2, |
| 93 |
'search+bind authentication fails with clear password'); |
| 94 |
|
| 95 |
unlink($node->data_dir . '/pg_hba.conf'); |
| 96 |
$node->append_conf('pg_hba.conf', |
| 97 |
qq{local all all ldap ldapserver=$ldap_server ldapport=$ldap_port ldapbasedn="$ldap_basedn" ldapbinddn="$ldap_rootdn" ldapbindpasswd="$rot13_ldap_rootpw"} |
| 98 |
); |
| 99 |
$node->restart; |
| 100 |
|
| 101 |
test_access($node, 'test1', 0, |
| 102 |
'search+bind authentication succeeds with rot13ed password'); |
| 103 |
|
| 104 |
done_testing(); |