Debian Codegraph @

tests/k5start/perms-t

raw
1 #!/usr/bin/perl -w
2 #
3 # Tests for k5start handling of ticket permissions.
4 #
5 # Written by Russ Allbery <[email protected]>
6 # Copyright 2021 Russ Allbery <[email protected]>
7 # Copyright 2011
8 # The Board of Trustees of the Leland Stanford Junior University
9 #
10 # SPDX-License-Identifier: MIT
11
12 use Test::More;
13
14 # The full path to the newly-built k5start client.
15 our $K5START = "$ENV{C_TAP_BUILD}/../commands/k5start";
16
17 # The path to our data directory, which contains the keytab to use to test.
18 our $DATA = "$ENV{C_TAP_BUILD}/data";
19
20 # Load our test utility programs.
21 require "$ENV{C_TAP_SOURCE}/libtest.pl";
22
23 # This test requires running under fakeroot, and therefore can't run unless
24 # fakeroot is available (or it's run as root, although that's a bad idea).
25 if ($> != 0) {
26 if (defined $ENV{TRY_FAKEROOT}) {
27 plan skip_all => 'fakeroot not available';
28 exit 0;
29 }
30 $ENV{TRY_FAKEROOT} = 'trying';
31 unless (exec ('fakeroot', "$ENV{C_TAP_SOURCE}/k5start/perms-t")) {
32 plan skip_all => 'fakeroot not available';
33 exit 0;
34 }
35 }
36
37 # Decide whether we have the configuration to run the tests.
38 if (-f "$DATA/test.keytab" and -f "$DATA/test.principal") {
39 plan tests => 34;
40 } else {
41 plan skip_all => 'no keytab configuration';
42 exit 0;
43 }
44
45 # Get the test principal.
46 my $principal = contents ("$DATA/test.principal");
47
48 # Don't overwrite the user's ticket cache.
49 $ENV{KRB5CCNAME} = 'krb5cc_test';
50
51 # Basic authentication test.
52 unlink 'krb5cc_test';
53 my ($out, $err, $status) = command ($K5START, '-qUf', "$DATA/test.keytab");
54 is ($status, 0, 'Basic k5start command succeeds');
55 is ($err, '', ' with no errors');
56 my ($mode, $uid, $gid) = (stat 'krb5cc_test')[2, 4, 5];
57 is (($mode & 0777), 0600, ' mode is correct');
58 is ($uid, 0, ' owner is correct');
59 is ($gid, 0, ' group is correct');
60
61 # Basic authentication test with -o, -g, and -m.
62 unlink 'krb5cc_test';
63 ($out, $err, $status)
64 = command ($K5START, '-Uf', "$DATA/test.keytab", '-o', 42, '-g', 42,
65 '-m', 440);
66 is ($status, 0, 'k5start -o 42 -g 42 -m 440 succeeds');
67 is ($err, '', ' with no errors');
68 like ($out, qr/^Kerberos initialization for \Q$principal\E(\@\S+)?\n\z/,
69 ' and the right output');
70 ($mode, $uid, $gid) = (stat 'krb5cc_test')[2, 4, 5];
71 is (($mode & 0777), 0440, ' mode is correct');
72 is ($uid, 42, ' owner is correct');
73 is ($gid, 42, ' group is correct');
74 chown (0, 0, 'krb5cc_test');
75 chmod (0600, 'krb5cc_test');
76 my ($default, $service) = klist ();
77 like ($default, qr/^\Q$principal\E(\@\S+)?\z/, ' for the right principal');
78 like ($service, qr%^krbtgt/%, ' and the right service');
79
80 # Just -o.
81 unlink 'krb5cc_test';
82 ($out, $err, $status)
83 = command ($K5START, '-qUf', "$DATA/test.keytab", '-o', 42);
84 is ($status, 0, 'k5start -o 42 succeeds');
85 is ($err, '', ' with no errors');
86 ($mode, $uid, $gid) = (stat 'krb5cc_test')[2, 4, 5];
87 is (($mode & 0777), 0600, ' mode is correct');
88 is ($uid, 42, ' owner is correct');
89 is ($gid, 0, ' group is correct');
90
91 # Just -g.
92 unlink 'krb5cc_test';
93 ($out, $err, $status)
94 = command ($K5START, '-qUf', "$DATA/test.keytab", '-g', 42);
95 is ($status, 0, 'k5start -g 42 succeeds');
96 is ($err, '', ' with no errors');
97 ($mode, $uid, $gid) = (stat 'krb5cc_test')[2, 4, 5];
98 is (($mode & 0777), 0600, ' mode is correct');
99 is ($uid, 0, ' owner is correct');
100 is ($gid, 42, ' group is correct');
101
102 # Just -m.
103 unlink 'krb5cc_test';
104 ($out, $err, $status)
105 = command ($K5START, '-qUf', "$DATA/test.keytab", '-m', 400);
106 is ($status, 0, 'k5start -m 400 succeeds');
107 is ($err, '', ' with no errors');
108 ($mode, $uid, $gid) = (stat 'krb5cc_test')[2, 4, 5];
109 is (($mode & 0777), 0400, ' mode is correct');
110 is ($uid, 0, ' owner is correct');
111 is ($gid, 0, ' group is correct');
112
113 # Test handling of FILE: prefixes for the cache.
114 unlink 'krb5cc_test';
115 ($out, $err, $status)
116 = command ($K5START, '-Uf', "$DATA/test.keytab", '-o', 42, '-g', 42,
117 '-m', 440, '-k', 'FILE:krb5cc_test');
118 is ($status, 0, 'k5start -o 42 -g 42 -m 440 -k succeeds');
119 is ($err, '', ' with no errors');
120 like ($out, qr/^Kerberos initialization for \Q$principal\E(\@\S+)?\n\z/,
121 ' and the right output');
122 ($mode, $uid, $gid) = (stat 'krb5cc_test')[2, 4, 5];
123 is (($mode & 0777), 0440, ' mode is correct');
124 is ($uid, 42, ' owner is correct');
125 is ($gid, 42, ' group is correct');
126
127 # Clean up.
128 unlink 'krb5cc_test';