Debian Codegraph @

tests/k5start/daemon-t

raw
1 #!/usr/bin/perl -w
2 #
3 # Tests for k5start daemon functionality.
4 #
5 # Written by Russ Allbery <[email protected]>
6 # Copyright 2015, 2021 Russ Allbery <[email protected]>
7 # Copyright 2008-2009, 2011-2012, 2014
8 # The Board of Trustees of the Leland Stanford Junior University
9 #
10 # SPDX-License-Identifier: MIT
11
12 use Cwd;
13 use File::Copy qw(copy);
14
15 use Test::More;
16
17 # The full path to the newly-built k5start client.
18 our $K5START = "$ENV{C_TAP_BUILD}/../commands/k5start";
19
20 # The path to our data directory, which contains the keytab to use to test.
21 our $DATA = "$ENV{C_TAP_BUILD}/data";
22
23 # The path to our temporary directory used for test ticket caches and the
24 # like.
25 our $TMP = "$ENV{C_TAP_BUILD}/tmp";
26 unless (-d $TMP) {
27 mkdir $TMP or BAIL_OUT ("cannot create $TMP: $!");
28 }
29
30 # Load our test utility programs.
31 require "$ENV{C_TAP_SOURCE}/libtest.pl";
32
33 # Decide whether we have the configuration to run the tests.
34 if (-f "$DATA/test.keytab" and -f "$DATA/test.principal") {
35 plan tests => 86;
36 } else {
37 plan skip_all => "no keytab configuration";
38 exit 0;
39 }
40
41 # Get the test principal.
42 my $principal = contents ("$DATA/test.principal");
43
44 # Don't overwrite the user's ticket cache.
45 $ENV{KRB5CCNAME} = "$TMP/krb5cc_test";
46
47 # Start a k5start daemon and be sure it gets tickets and stays running.
48 unlink "$TMP/krb5cc_test";
49 my $pid = fork;
50 if (!defined $pid) {
51 BAIL_OUT ("can't fork: $!");
52 } elsif ($pid == 0) {
53 exec ($K5START, '-K', 1, '-f', "$DATA/test.keytab", '-p', "$TMP/pid",
54 $principal) or BAIL_OUT ("can't run $K5START: $!");
55 }
56 my $tries = 0;
57 while (not -s "$TMP/pid" and $tries < 100) {
58 select (undef, undef, undef, 0.1);
59 $tries++;
60 }
61 my ($default, $service) = klist ();
62 like ($default, qr/^\Q$principal\E(\@\S+)?\z/,
63 'Authentication succeeded for the right principal');
64 like ($service, qr%^krbtgt/%, ' and the right service');
65 if (-f "$TMP/pid") {
66 my $daemon = contents ("$TMP/pid");
67 is ($pid, $daemon, ' and the right PID is written');
68 $pid = $daemon if $daemon;
69 } else {
70 ok (0, ' and the right PID is written');
71 }
72 ok (kill (0, $pid), ' and k5start is still running');
73 unlink "$TMP/krb5cc_test";
74 ok (! -f "$TMP/krb5cc_test", 'Ticket cache was deleted');
75 kill (14, $pid) or warn "Can't kill $pid: $!\n";
76 $tries = 0;
77 while (not -f "$TMP/krb5cc_test" and $tries < 100) {
78 select (undef, undef, undef, 0.1);
79 $tries++;
80 }
81 ok (kill (0, $pid), ' and k5start is still running after ALRM');
82 ($default, $service) = klist ();
83 like ($default, qr/^\Q$principal\E(\@\S+)?\z/,
84 ' and recreates cache with the right principal');
85 like ($service, qr%^krbtgt/%, ' and the right service');
86 kill (15, $pid) or warn "Can't kill $pid: $!\n";
87 is (waitpid ($pid, 0), $pid, ' and k5start dies after SIGTERM');
88 ok (!-f "$TMP/pid", ' and the PID file was removed');
89 unlink "$TMP/pid";
90
91 # Try again with the -b flag.
92 unlink "$TMP/krb5cc_test";
93 my ($out, $err, $status)
94 = command ($K5START, '-bK', 1, '-f', "$DATA/test.keytab", '-p',
95 "$TMP/pid", $principal);
96 is ($status, 0, 'Backgrounding k5start works');
97 is ($err, '', ' with no error output');
98 is ($out, '', ' and -q was added implicitly');
99 $tries = 0;
100 while (not -s "$TMP/pid" and $tries < 100) {
101 select (undef, undef, undef, 0.1);
102 $tries++;
103 }
104 ($default, $service) = klist ();
105 like ($default, qr/^\Q$principal\E(\@\S+)?\z/,
106 'Authentication succeeded for the right principal');
107 like ($service, qr%^krbtgt/%, ' and the right service');
108 $pid = contents ("$TMP/pid");
109 ok (kill (0, $pid), ' and the PID file is correct');
110 kill (15, $pid) or warn "Can't kill $pid: $!\n";
111 select (undef, undef, undef, 0.2);
112 ok (!-f "$TMP/pid", ' and the PID file was removed');
113 unlink "$TMP/pid";
114
115 # Try again with an initial authentication failure and ensure that k5start
116 # keeps running anyway.
117 unlink "$TMP/krb5cc_test";
118 ($out, $err, $status)
119 = command ($K5START, '-bK', 1, '-f', "$DATA/test.keytab-BAD", '-p',
120 "$TMP/pid", $principal);
121 is ($status, 0, 'Backgrounding k5start with bad keytab works');
122 like ($err, qr/^k5start: error getting credentials: /, ' with error output');
123 is ($out, '', ' and -q was added implicitly');
124 while (not -s "$TMP/pid" and $tries < 100) {
125 select (undef, undef, undef, 0.1);
126 $tries++;
127 }
128 $pid = contents ("$TMP/pid");
129 ok (kill (0, $pid), ' and the PID file is correct');
130 kill (15, $pid) or warn "Can't kill $pid: $!\n";
131 select (undef, undef, undef, 2.0);
132 ok (!-f "$TMP/pid", ' and the PID file was removed');
133 unlink "$TMP/pid";
134
135 # Providing -x should override that behavior and cause the daemon to refuse
136 # to start.
137 unlink "$TMP/krb5cc_test";
138 ($out, $err, $status)
139 = command ($K5START, '-bxK', 1, '-f', "$DATA/test.keytab-BAD", '-p',
140 "$TMP/pid", $principal);
141 is ($status, 1, 'Backgrounding k5start -x with bad keytab fails');
142 like ($err, qr/^k5start: error getting credentials: /, ' with error output');
143 is ($out, '', ' and -q was added implicitly');
144 if ($status == 0) {
145 while (not -s "$TMP/pid" and $tries < 100) {
146 select (undef, undef, undef, 0.1);
147 $tries++;
148 }
149 $pid = contents ("$TMP/pid");
150 kill (15, $pid) or warn "Can't kill $pid: $!\n";
151 }
152
153 # Check that k5start keeps running if the ticket cache directory is not
154 # writeable.
155 $pid = fork;
156 if (!defined $pid) {
157 BAIL_OUT ("can't fork: $!");
158 } elsif ($pid == 0) {
159 open (STDERR, '>', "$TMP/k5start-errors")
160 or BAIL_OUT ("can't create $TMP/k5start-errors: $!");
161 exec ($K5START, '-K', 1, '-Uf', "$DATA/test.keytab", '-p', "$TMP/pid")
162 or BAIL_OUT ("can't run $K5START: $!");
163 }
164 $tries = 0;
165 while (not -s "$TMP/pid" and $tries < 100) {
166 select (undef, undef, undef, 0.1);
167 $tries++;
168 }
169 $pid = contents ("$TMP/pid");
170 ok (kill (0, $pid), 'k5start -K 1 started');
171 chmod 0555, $TMP or BAIL_OUT ("cannot chmod $TMP: $!");
172 kill (14, $pid) or warn "Can't kill $pid: $!\n";
173 $tries = 0;
174 while (not -s "$TMP/k5start-errors" and $tries < 100) {
175 select (undef, undef, undef, 0.1);
176 $tries++;
177 }
178 ok (kill (0, $pid), ' and it keeps running on a non-writeable cache');
179 chmod 0755, $TMP or BAIL_OUT ("cannot chmod $TMP: $!");
180 if (open (ERRORS, '<', "$TMP/k5start-errors")) {
181 like (scalar (<ERRORS>), qr/^k5start: error initializing ticket cache: /,
182 ' and the correct error message');
183 } else {
184 ok (0, ' and the correct error message');
185 }
186 unlink "$TMP/k5start-errors";
187 kill (15, $pid) or warn "Can't kill $pid: $!\n";
188 is (waitpid ($pid, 0), $pid, ' and k5start dies after SIGTERM');
189 ok (!-f "$TMP/pid", ' and the PID file was removed');
190
191 # If we do that again with -x, k5start should exit.
192 ($out, $err, $status)
193 = command ($K5START, '-xbK', 1, '-f', "$DATA/test.keytab", '-p',
194 "$TMP/pid", $principal);
195 is ($status, 0, 'k5start -xb works');
196 is ($err, '', ' with no error output');
197 is ($out, '', ' and -q was added implicitly');
198 $tries = 0;
199 while (not -s "$TMP/pid" and $tries < 100) {
200 select (undef, undef, undef, 0.1);
201 $tries++;
202 }
203 $pid = contents ("$TMP/pid");
204 ok (kill (0, $pid), 'k5start -xb started');
205 chmod 0555, $TMP or BAIL_OUT ("cannot chmod $TMP: $!");
206 kill (14, $pid) or warn "Can't kill $pid: $!\n";
207 $tries = 0;
208 while (kill (0, $pid) and $tries < 100) {
209 select (undef, undef, undef, 0.5);
210 $tries++;
211 }
212 ok (!kill (0, $pid), ' and it exits on a non-writeable cache');
213 chmod 0755, $TMP or BAIL_OUT ("cannot chmod $TMP: $!");
214 unlink "$TMP/pid";
215
216 # Now, run a command in the background.
217 unlink "$TMP/krb5cc_test", "$TMP/krb5cc_child", "$TMP/child-out";
218 ($out, $err, $status)
219 = command ($K5START, '-bK', 1, '-k', "$TMP/krb5cc_child", '-f',
220 "$DATA/test.keytab", '-p', "$TMP/pid", '-c', "$TMP/child-pid",
221 $principal, '--', "$ENV{C_TAP_SOURCE}/data/command",
222 "$TMP/child-out");
223 is ($status, 0, 'Backgrounding k5start works');
224 is ($err, '', ' with no error output');
225 is ($out, '', ' and output was redirected properly');
226 $tries = 0;
227 while (not -s "$TMP/child-pid" and $tries < 100) {
228 select (undef, undef, undef, 0.1);
229 $tries++;
230 }
231 ($default, $service) = klist ();
232 is ($default, undef, 'The normal ticket cache is untouched');
233 $ENV{KRB5CCNAME} = "$TMP/krb5cc_child";
234 ($default, $service) = klist ();
235 like ($default, qr/^\Q$principal\E(\@\S+)?\z/,
236 ' but the other cache has the right principal');
237 like ($service, qr%^krbtgt/%, ' and the right service');
238 $pid = contents ("$TMP/pid");
239 ok (kill (0, $pid), 'k5start is running');
240 $child = contents ("$TMP/child-pid");
241 ok (kill (0, $child), 'The child process is running');
242 $tries = 0;
243 while (not -s "$TMP/child-out" and $tries < 100) {
244 select (undef, undef, undef, 0.1);
245 $tries++;
246 }
247 kill (1, $child) or warn "Cannot send HUP to child $child: $!\n";
248 select (undef, undef, undef, 0.1);
249 kill (2, $child) or warn "Cannot send INT to child $child: $!\n";
250 select (undef, undef, undef, 0.1);
251 kill (15, $child) or warn "Cannot send TERM to child $child: $!\n";
252 select (undef, undef, undef, 0.2);
253 ok (!kill (0, $pid), 'k5start is no longer running');
254 ok (!kill (0, $child), 'The child process is no longer running');
255 open (OUT, '<', "$TMP/child-out") or BAIL_OUT ("cannot open child-out: $!");
256 is (scalar (<OUT>), "$child\n", 'Child PID is correct');
257 is (scalar (<OUT>), "/\n", 'Child working directory is /');
258 is (scalar (<OUT>), "FILE:$TMP/krb5cc_child\n", 'Child cache is correct');
259 is (scalar (<OUT>), "got SIGHUP\n", 'SIGHUP was recorded');
260 is (scalar (<OUT>), "got SIGINT\n", 'SIGINT was recorded');
261 is (scalar (<OUT>), "got SIGTERM\n", 'SIGTERM was recorded');
262 ok (eof OUT, 'No more child output written');
263 close OUT;
264 ok (!-f "$TMP/pid", 'PID file cleaned up');
265 ok (!-f "$TMP/child-pid", 'Child PID file cleaned up');
266
267 # Now, do that again, but test signal propagation from the parent to the
268 # child.
269 unlink "$TMP/krb5cc_child", "$TMP/child-out";
270 ($out, $err, $status)
271 = command ($K5START, '-bK', 1, '-k', "$TMP/krb5cc_child", '-f',
272 "$DATA/test.keytab", '-p', "$TMP/pid", '-c', "$TMP/child-pid",
273 $principal, '--', "$ENV{C_TAP_SOURCE}/data/command",
274 "$TMP/child-out");
275 is ($status, 0, 'Backgrounding k5start works');
276 is ($err, '', ' with no error output');
277 is ($out, '', ' and output was redirected properly');
278 $tries = 0;
279 while (not -s "$TMP/child-pid" and $tries < 100) {
280 select (undef, undef, undef, 0.1);
281 $tries++;
282 }
283 $pid = contents ("$TMP/pid");
284 ok (kill (0, $pid), 'k5start is running');
285 $child = contents ("$TMP/child-pid");
286 ok (kill (0, $child), 'The child process is running');
287 $tries = 0;
288 while (not -s "$TMP/child-out" and $tries < 100) {
289 select (undef, undef, undef, 0.1);
290 $tries++;
291 }
292 kill (1, $pid) or warn "Cannot send HUP to parent $pid: $!\n";
293 select (undef, undef, undef, 0.1);
294 kill (2, $pid) or warn "Cannot send INT to parent $pid: $!\n";
295 select (undef, undef, undef, 0.1);
296 kill (15, $pid) or warn "Cannot send TERM to parent $pid: $!\n";
297 select (undef, undef, undef, 0.2);
298 ok (!kill (0, $pid), 'k5start is no longer running');
299 ok (!kill (0, $child), 'The child process is no longer running');
300 open (OUT, '<', "$TMP/child-out") or BAIL_OUT ("cannot open child-out: $!");
301 is (scalar (<OUT>), "$child\n", 'Child PID is correct');
302 is (scalar (<OUT>), "/\n", 'Child working directory is /');
303 is (scalar (<OUT>), "FILE:$TMP/krb5cc_child\n", 'Child cache is correct');
304 is (scalar (<OUT>), "got SIGHUP\n", 'SIGHUP was propagated');
305 is (scalar (<OUT>), "got SIGINT\n", 'SIGINT was propagated');
306 is (scalar (<OUT>), "got SIGTERM\n", 'SIGTERM was propagated');
307 ok (eof OUT, 'No more child output written');
308 close OUT;
309 ok (!-f "$TMP/pid", 'PID file cleaned up');
310 ok (!-f "$TMP/child-pid", 'Child PID file cleaned up');
311
312 # Finally, start a child program with an invalid keytab path. This should
313 # start and keep trying to authenticate but never start the actual child
314 # process until the ticket cache does exist. We also test SIGQUIT handling.
315 unlink "$TMP/krb5cc_child", "$TMP/child-out";
316 ($out, $err, $status)
317 = command ($K5START, '-bK', 1, '-k', "$TMP/krb5cc_child", '-f',
318 "$TMP/test.keytab", '-p', "$TMP/pid", '-c', "$TMP/child-pid",
319 $principal, '--', "$ENV{C_TAP_SOURCE}/data/command",
320 "$TMP/child-out");
321 is ($status, 0, 'Backgrounding k5start works');
322 like ($err, qr/^k5start: error getting credentials: /, ' with error output');
323 is ($out, '', ' and output was redirected properly');
324 while (not -s "$TMP/pid" and $tries < 100) {
325 select (undef, undef, undef, 0.1);
326 $tries++;
327 }
328 $pid = contents ("$TMP/pid");
329 ok (kill (0, $pid), 'k5start is running');
330 select (undef, undef, undef, 1);
331 ok (!-f "$TMP/child-pid", ' child did not start');
332 ok (!-f "$TMP/child-out", ' and has no output');
333 copy ("$DATA/test.keytab", "$TMP/test.keytab");
334 $tries = 0;
335 while (not -s "$TMP/child-pid" and $tries < 100) {
336 select (undef, undef, undef, 0.1);
337 $tries++;
338 }
339 $child = contents ("$TMP/child-pid");
340 ok (kill (0, $child), 'The child process is running');
341 $tries = 0;
342 while (not -s "$TMP/child-out" and $tries < 100) {
343 select (undef, undef, undef, 0.1);
344 $tries++;
345 }
346 kill (3, $pid) or warn "Cannot send QUIT to parent $pid: $!\n";
347 select (undef, undef, undef, 0.2);
348 ok (!kill (0, $pid), 'k5start is no longer running');
349 ok (!kill (0, $child), 'The child process is no longer running');
350 open (OUT, '<', "$TMP/child-out") or BAIL_OUT ("cannot open child-out: $!");
351 is (scalar (<OUT>), "$child\n", 'Child PID is correct');
352 is (scalar (<OUT>), "/\n", 'Child working directory is /');
353 is (scalar (<OUT>), "FILE:$TMP/krb5cc_child\n", 'Child cache is correct');
354 is (scalar (<OUT>), "got SIGQUIT\n", 'SIGQUIT was propagated');
355 ok (eof OUT, 'No more child output written');
356 close OUT;
357 ok (!-f "$TMP/pid", 'PID file cleaned up');
358 ok (!-f "$TMP/child-pid", 'Child PID file cleaned up');
359
360 # Clean up.
361 unlink "$TMP/krb5cc_child", "$TMP/child-out", "$TMP/test.keytab";
362 unlink "$TMP/pid", "$TMP/child-pid";
363 rmdir $TMP;