|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | +use TestLib; |
| 4 | +use PostgresNode; |
| 5 | +use Test::More; |
| 6 | + |
| 7 | +if ($ENV{with_gssapi}eq'yes') |
| 8 | +{ |
| 9 | +plantests=> 4; |
| 10 | +} |
| 11 | +else |
| 12 | +{ |
| 13 | +planskip_all=>'GSSAPI/Kerberos not supported by this build'; |
| 14 | +} |
| 15 | + |
| 16 | +my ($krb5_bin_dir,$krb5_sbin_dir); |
| 17 | + |
| 18 | +if ($^Oeq'darwin') |
| 19 | +{ |
| 20 | +$krb5_bin_dir ='/usr/local/opt/krb5/bin'; |
| 21 | +$krb5_sbin_dir ='/usr/local/opt/krb5/sbin'; |
| 22 | +} |
| 23 | +elsif ($^Oeq'freebsd') |
| 24 | +{ |
| 25 | +$krb5_bin_dir ='/usr/local/bin'; |
| 26 | +$krb5_sbin_dir ='/usr/local/sbin'; |
| 27 | +} |
| 28 | +elsif ($^Oeq'linux') |
| 29 | +{ |
| 30 | +$krb5_sbin_dir ='/usr/sbin'; |
| 31 | +} |
| 32 | + |
| 33 | +my$krb5_config ='krb5-config'; |
| 34 | +my$kinit ='kinit'; |
| 35 | +my$kdb5_util ='kdb5_util'; |
| 36 | +my$kadmin_local ='kadmin.local'; |
| 37 | +my$krb5kdc ='krb5kdc'; |
| 38 | + |
| 39 | +if ($krb5_bin_dir &&-d$krb5_bin_dir) |
| 40 | +{ |
| 41 | +$krb5_config =$krb5_bin_dir .'/' .$krb5_config; |
| 42 | +$kinit =$krb5_bin_dir .'/' .$kinit; |
| 43 | +} |
| 44 | +if ($krb5_sbin_dir &&-d$krb5_sbin_dir) |
| 45 | +{ |
| 46 | +$kdb5_util =$krb5_sbin_dir .'/' .$kdb5_util; |
| 47 | +$kadmin_local =$krb5_sbin_dir .'/' .$kadmin_local; |
| 48 | +$krb5kdc =$krb5_sbin_dir .'/' .$krb5kdc; |
| 49 | +} |
| 50 | + |
| 51 | +my$realm ='EXAMPLE.COM'; |
| 52 | + |
| 53 | +my$krb5_conf ="${TestLib::tmp_check}/krb5.conf"; |
| 54 | +my$kdc_conf ="${TestLib::tmp_check}/kdc.conf"; |
| 55 | +my$krb5_log ="${TestLib::tmp_check}/krb5libs.log"; |
| 56 | +my$kdc_log ="${TestLib::tmp_check}/krb5kdc.log"; |
| 57 | +my$kdc_port =int(rand() * 16384) + 49152; |
| 58 | +my$kdc_datadir ="${TestLib::tmp_check}/krb5kdc"; |
| 59 | +my$kdc_pidfile ="${TestLib::tmp_check}/krb5kdc.pid"; |
| 60 | +my$keytab ="${TestLib::tmp_check}/krb5.keytab"; |
| 61 | + |
| 62 | +note"setting up Kerberos"; |
| 63 | + |
| 64 | +my ($stdout,$krb5_version); |
| 65 | +run_log [$krb5_config,'--version' ],'>', \$stdoutor BAIL_OUT("could not execute krb5-config"); |
| 66 | +BAIL_OUT("Heimdal is not supported")if$stdout =~m/heimdal/; |
| 67 | +$stdout =~m/Kerberos 5 release ([0-9]+\.[0-9]+)/or BAIL_OUT("could not get Kerberos version"); |
| 68 | +$krb5_version =$1; |
| 69 | + |
| 70 | +append_to_file($krb5_conf, |
| 71 | +qq![logging] |
| 72 | +default = FILE:$krb5_log |
| 73 | +kdc = FILE:$kdc_log |
| 74 | +
|
| 75 | +[libdefaults] |
| 76 | +default_realm =$realm |
| 77 | +
|
| 78 | +[realms] |
| 79 | +$realm = { |
| 80 | + kdc = localhost:$kdc_port |
| 81 | +}!); |
| 82 | + |
| 83 | +append_to_file($kdc_conf, |
| 84 | +qq![kdcdefaults] |
| 85 | +!); |
| 86 | +# For new-enough versions of krb5, use the _listen settings rather |
| 87 | +# than the _ports settings so that we can bind to localhost only. |
| 88 | +if ($krb5_version >= 1.15) |
| 89 | +{ |
| 90 | +append_to_file($kdc_conf, |
| 91 | +qq!kdc_listen = localhost:$kdc_port |
| 92 | +kdc_tcp_listen = localhost:$kdc_port |
| 93 | +!); |
| 94 | +} |
| 95 | +else |
| 96 | +{ |
| 97 | +append_to_file($kdc_conf, |
| 98 | +qq!kdc_ports =$kdc_port |
| 99 | +kdc_tcp_ports =$kdc_port |
| 100 | +!); |
| 101 | +} |
| 102 | +append_to_file($kdc_conf, |
| 103 | +qq! |
| 104 | +[realms] |
| 105 | +$realm = { |
| 106 | + database_name =$kdc_datadir/principal |
| 107 | + admin_keytab = FILE:$kdc_datadir/kadm5.keytab |
| 108 | + acl_file =$kdc_datadir/kadm5.acl |
| 109 | + key_stash_file =$kdc_datadir/_k5.$realm |
| 110 | +}!); |
| 111 | + |
| 112 | +mkdir$kdc_datadirordie; |
| 113 | + |
| 114 | +$ENV{'KRB5_CONFIG'} =$krb5_conf; |
| 115 | +$ENV{'KRB5_KDC_PROFILE'} =$kdc_conf; |
| 116 | + |
| 117 | +my$service_principal ="$ENV{with_krb_srvnam}/localhost"; |
| 118 | + |
| 119 | +system_or_bail$kdb5_util,'create','-s','-P','secret0'; |
| 120 | + |
| 121 | +my$test1_password ='secret1'; |
| 122 | +system_or_bail$kadmin_local,'-q',"addprinc -pw$test1_password test1"; |
| 123 | + |
| 124 | +system_or_bail$kadmin_local,'-q',"addprinc -randkey$service_principal"; |
| 125 | +system_or_bail$kadmin_local,'-q',"ktadd -k$keytab$service_principal"; |
| 126 | + |
| 127 | +system_or_bail$krb5kdc,'-P',$kdc_pidfile; |
| 128 | + |
| 129 | +END |
| 130 | +{ |
| 131 | +kill'INT',`cat$kdc_pidfile`if-f$kdc_pidfile; |
| 132 | +} |
| 133 | + |
| 134 | +note"setting up PostgreSQL instance"; |
| 135 | + |
| 136 | +my$node = get_new_node('node'); |
| 137 | +$node->init; |
| 138 | +$node->append_conf('postgresql.conf',"listen_addresses = 'localhost'"); |
| 139 | +$node->append_conf('postgresql.conf',"krb_server_keyfile = '$keytab'"); |
| 140 | +$node->start; |
| 141 | + |
| 142 | +$node->safe_psql('postgres','CREATE USER test1;'); |
| 143 | + |
| 144 | +note"running tests"; |
| 145 | + |
| 146 | +subtest_access |
| 147 | +{ |
| 148 | +my ($node,$role,$expected_res,$test_name) =@_; |
| 149 | + |
| 150 | +# need to connect over TCP/IP for Kerberos |
| 151 | +my$res =$node->psql('postgres','SELECT 1', |
| 152 | +extra_params=> ['-d',$node->connstr('postgres').' host=localhost', |
| 153 | +'-U',$role ]); |
| 154 | +is($res,$expected_res,$test_name); |
| 155 | +} |
| 156 | + |
| 157 | +unlink($node->data_dir .'/pg_hba.conf'); |
| 158 | +$node->append_conf('pg_hba.conf',qq{host all all localhost gss map=mymap}); |
| 159 | +$node->restart; |
| 160 | + |
| 161 | +test_access($node,'test1', 2,'fails without ticket'); |
| 162 | + |
| 163 | +run_log [$kinit,'test1' ], \$test1_passwordor BAIL_OUT($?); |
| 164 | + |
| 165 | +test_access($node,'test1', 2,'fails without mapping'); |
| 166 | + |
| 167 | +$node->append_conf('pg_ident.conf',qq{mymap /^(.*)\@$realm\$\\1}); |
| 168 | +$node->restart; |
| 169 | + |
| 170 | +test_access($node,'test1', 0,'succeeds with mapping'); |
| 171 | + |
| 172 | +truncate($node->data_dir .'/pg_ident.conf', 0); |
| 173 | +unlink($node->data_dir .'/pg_hba.conf'); |
| 174 | +$node->append_conf('pg_hba.conf',qq{host all all localhost gss include_realm=0}); |
| 175 | +$node->restart; |
| 176 | + |
| 177 | +test_access($node,'test1', 0,'succeeds with include_realm=0'); |