Debian Codegraph @

src/test/perl/PostgreSQL/Test/Kerberos.pm

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Sets up a stand-alone KDC for testing PostgreSQL GSSAPI / Kerberos
5 # functionality.
6
7 package PostgreSQL::Test::Kerberos;
8
9 use strict;
10 use warnings FATAL => 'all';
11 use PostgreSQL::Test::Utils;
12 use Test::More;
13
14 our (
15 $krb5_bin_dir, $krb5_sbin_dir, $krb5_config, $kinit,
16 $klist, $kdb5_util, $kadmin_local, $krb5kdc,
17 $krb5_conf, $kdc_conf, $krb5_cache, $krb5_log,
18 $kdc_log, $kdc_port, $kdc_datadir, $kdc_pidfile,
19 $keytab);
20
21 INIT
22 {
23 if ($^O eq 'darwin' && -d "/opt/homebrew")
24 {
25 # typical paths for Homebrew on ARM
26 $krb5_bin_dir = '/opt/homebrew/opt/krb5/bin';
27 $krb5_sbin_dir = '/opt/homebrew/opt/krb5/sbin';
28 }
29 elsif ($^O eq 'darwin')
30 {
31 # typical paths for Homebrew on Intel
32 $krb5_bin_dir = '/usr/local/opt/krb5/bin';
33 $krb5_sbin_dir = '/usr/local/opt/krb5/sbin';
34 }
35 elsif ($^O eq 'freebsd')
36 {
37 $krb5_bin_dir = '/usr/local/bin';
38 $krb5_sbin_dir = '/usr/local/sbin';
39 }
40 elsif ($^O eq 'linux')
41 {
42 $krb5_sbin_dir = '/usr/sbin';
43 }
44
45 $krb5_config = 'krb5-config';
46 $kinit = 'kinit';
47 $klist = 'klist';
48 $kdb5_util = 'kdb5_util';
49 $kadmin_local = 'kadmin.local';
50 $krb5kdc = 'krb5kdc';
51
52 if ($krb5_bin_dir && -d $krb5_bin_dir)
53 {
54 $krb5_config = $krb5_bin_dir . '/' . $krb5_config;
55 $kinit = $krb5_bin_dir . '/' . $kinit;
56 $klist = $krb5_bin_dir . '/' . $klist;
57 }
58 if ($krb5_sbin_dir && -d $krb5_sbin_dir)
59 {
60 $kdb5_util = $krb5_sbin_dir . '/' . $kdb5_util;
61 $kadmin_local = $krb5_sbin_dir . '/' . $kadmin_local;
62 $krb5kdc = $krb5_sbin_dir . '/' . $krb5kdc;
63 }
64
65 $krb5_conf = "${PostgreSQL::Test::Utils::tmp_check}/krb5.conf";
66 $kdc_conf = "${PostgreSQL::Test::Utils::tmp_check}/kdc.conf";
67 $krb5_cache = "${PostgreSQL::Test::Utils::tmp_check}/krb5cc";
68 $krb5_log = "${PostgreSQL::Test::Utils::log_path}/krb5libs.log";
69 $kdc_log = "${PostgreSQL::Test::Utils::log_path}/krb5kdc.log";
70 $kdc_port = PostgreSQL::Test::Cluster::get_free_port();
71 $kdc_datadir = "${PostgreSQL::Test::Utils::tmp_check}/krb5kdc";
72 $kdc_pidfile = "${PostgreSQL::Test::Utils::tmp_check}/krb5kdc.pid";
73 $keytab = "${PostgreSQL::Test::Utils::tmp_check}/krb5.keytab";
74 }
75
76 =pod
77
78 =item PostgreSQL::Test::Kerberos->new(host, hostaddr, realm, %params)
79
80 Sets up a new Kerberos realm and KDC. This function assigns a free
81 port for the KDC. The KDC will be shut down automatically when the
82 test script exits.
83
84 =over
85
86 =item host => 'auth-test-localhost.postgresql.example.com'
87
88 Hostname to use in the service principal.
89
90 =item hostaddr => '127.0.0.1'
91
92 Network interface the KDC will listen on.
93
94 =item realm => 'EXAMPLE.COM'
95
96 Name of the Kerberos realm.
97
98 =back
99
100 =cut
101
102 sub new
103 {
104 my $class = shift;
105 my ($host, $hostaddr, $realm) = @_;
106
107 my ($stdout, $krb5_version);
108 run_log [ $krb5_config, '--version' ], '>' => \$stdout
109 or BAIL_OUT("could not execute krb5-config");
110 BAIL_OUT("Heimdal is not supported") if $stdout =~ m/heimdal/;
111 $stdout =~ m/Kerberos 5 release ([0-9]+\.[0-9]+)/
112 or BAIL_OUT("could not get Kerberos version");
113 $krb5_version = $1;
114
115 # Build the krb5.conf to use.
116 #
117 # Explicitly specify the default (test) realm and the KDC for
118 # that realm to avoid the Kerberos library trying to look up
119 # that information in DNS, and also because we're using a
120 # non-standard KDC port.
121 #
122 # Also explicitly disable DNS lookups since this isn't really
123 # our domain and we shouldn't be causing random DNS requests
124 # to be sent out (not to mention that broken DNS environments
125 # can cause the tests to take an extra long time and timeout).
126 #
127 # Reverse DNS is explicitly disabled to avoid any issue with a
128 # captive portal or other cases where the reverse DNS succeeds
129 # and the Kerberos library uses that as the canonical name of
130 # the host and then tries to acquire a cross-realm ticket.
131 append_to_file(
132 $krb5_conf,
133 qq![logging]
134 default = FILE:$krb5_log
135 kdc = FILE:$kdc_log
136
137 [libdefaults]
138 dns_lookup_realm = false
139 dns_lookup_kdc = false
140 default_realm = $realm
141 forwardable = false
142 rdns = false
143
144 [realms]
145 $realm = {
146 kdc = $hostaddr:$kdc_port
147 }
148 !);
149
150 append_to_file(
151 $kdc_conf,
152 qq![kdcdefaults]
153 !);
154
155 # For new-enough versions of krb5, use the _listen settings rather
156 # than the _ports settings so that we can bind to localhost only.
157 if ($krb5_version >= 1.15)
158 {
159 append_to_file(
160 $kdc_conf,
161 qq!kdc_listen = $hostaddr:$kdc_port
162 kdc_tcp_listen = $hostaddr:$kdc_port
163 !);
164 }
165 else
166 {
167 append_to_file(
168 $kdc_conf,
169 qq!kdc_ports = $kdc_port
170 kdc_tcp_ports = $kdc_port
171 !);
172 }
173 append_to_file(
174 $kdc_conf,
175 qq!
176 [realms]
177 $realm = {
178 database_name = $kdc_datadir/principal
179 admin_keytab = FILE:$kdc_datadir/kadm5.keytab
180 acl_file = $kdc_datadir/kadm5.acl
181 key_stash_file = $kdc_datadir/_k5.$realm
182 }!);
183
184 mkdir $kdc_datadir
185 or BAIL_OUT("could not create directory \"$kdc_datadir\"");
186
187 # Ensure that we use test's config and cache files, not global ones.
188 $ENV{'KRB5_CONFIG'} = $krb5_conf;
189 $ENV{'KRB5_KDC_PROFILE'} = $kdc_conf;
190 $ENV{'KRB5CCNAME'} = $krb5_cache;
191
192 my $service_principal = "$ENV{with_krb_srvnam}/$host";
193
194 system_or_bail $kdb5_util, 'create', '-s', '-P', 'secret0';
195
196 system_or_bail $kadmin_local, '-q',
197 "addprinc -randkey $service_principal";
198 system_or_bail $kadmin_local, '-q', "ktadd -k $keytab $service_principal";
199
200 system_or_bail $krb5kdc, '-P', $kdc_pidfile;
201
202 my $self = {};
203 $self->{keytab} = $keytab;
204
205 bless $self, $class;
206
207 return $self;
208 }
209
210 sub create_principal
211 {
212 my ($self, $principal, $password) = @_;
213
214 system_or_bail $kadmin_local, '-q', "addprinc -pw $password $principal";
215 }
216
217 sub create_ticket
218 {
219 my ($self, $principal, $password, %params) = @_;
220
221 my @cmd = ($kinit, $principal);
222
223 push @cmd, '-f' if ($params{forwardable});
224
225 run_log [@cmd], \$password or BAIL_OUT($?);
226 run_log [ $klist, '-f' ] or BAIL_OUT($?);
227 }
228
229 END
230 {
231 # take care not to change the script's exit value
232 my $exit_code = $?;
233
234 kill 'INT', `cat $kdc_pidfile`
235 if defined($kdc_pidfile) && -f $kdc_pidfile;
236
237 $? = $exit_code;
238 }
239
240 1;