| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# Tests for basic k5start functionality. |
| 4 |
# |
| 5 |
# Written by Russ Allbery <[email protected]> |
| 6 |
# Copyright 2021 Russ Allbery <[email protected]> |
| 7 |
# Copyright 2008-2009 |
| 8 |
# The Board of Trustees of the Leland Stanford Junior University |
| 9 |
# |
| 10 |
# SPDX-License-Identifier: MIT |
| 11 |
|
| 12 |
use POSIX qw(SIGTERM); |
| 13 |
use Test::More; |
| 14 |
|
| 15 |
# The full path to the newly-built k5start client. |
| 16 |
our $K5START = "$ENV{C_TAP_BUILD}/../commands/k5start"; |
| 17 |
|
| 18 |
# The path to our data directory, which contains the keytab to use to test. |
| 19 |
our $DATA = "$ENV{C_TAP_BUILD}/data"; |
| 20 |
|
| 21 |
# Load our test utility programs. |
| 22 |
require "$ENV{C_TAP_SOURCE}/libtest.pl"; |
| 23 |
|
| 24 |
# Decide whether we have the configuration to run the tests. |
| 25 |
if (-f "$DATA/test.keytab" and -f "$DATA/test.principal") { |
| 26 |
plan tests => 92; |
| 27 |
} else { |
| 28 |
plan skip_all => 'no keytab configuration'; |
| 29 |
exit 0; |
| 30 |
} |
| 31 |
|
| 32 |
# Get the test principal. |
| 33 |
my $principal = contents ("$DATA/test.principal"); |
| 34 |
|
| 35 |
# Don't overwrite the user's ticket cache. |
| 36 |
$ENV{KRB5CCNAME} = 'krb5cc_test'; |
| 37 |
|
| 38 |
# Basic authentication test. |
| 39 |
unlink 'krb5cc_test'; |
| 40 |
my ($out, $err, $status) |
| 41 |
= command ($K5START, '-f', "$DATA/test.keytab", $principal); |
| 42 |
is ($status, 0, 'Basic k5start command succeeds'); |
| 43 |
is ($err, '', ' with no errors'); |
| 44 |
like ($out, qr/^Kerberos initialization for \Q$principal\E(\@\S+)?\n\z/, |
| 45 |
' and the right output'); |
| 46 |
my ($default, $service) = klist (); |
| 47 |
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, ' for the right principal'); |
| 48 |
like ($service, qr%^krbtgt/%, ' and the right service'); |
| 49 |
|
| 50 |
# Specify the full principal with -u. |
| 51 |
unlink 'krb5cc_test'; |
| 52 |
($out, $err, $status) |
| 53 |
= command ($K5START, '-u', $principal, '-f', "$DATA/test.keytab"); |
| 54 |
is ($status, 0, 'k5start -u succeeds'); |
| 55 |
is ($err, '', ' with no errors'); |
| 56 |
like ($out, qr/^Kerberos initialization for \Q$principal\E(\@\S+)?\n\z/, |
| 57 |
' and the right output'); |
| 58 |
($default, $service) = klist (); |
| 59 |
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, ' for the right principal'); |
| 60 |
like ($service, qr%^krbtgt/%, ' and the right service'); |
| 61 |
|
| 62 |
# If we have a principal with an instance, try -u and -i. |
| 63 |
my ($name, $inst) = ($principal =~ m%^([^/\@]+)(?:/([^\@]+))%); |
| 64 |
SKIP: { |
| 65 |
skip 'test principal has no instance', 5 unless $inst; |
| 66 |
unlink 'krb5cc_test'; |
| 67 |
($out, $err, $status) = command ($K5START, '-u', $name, '-i', $inst, '-f', |
| 68 |
"$DATA/test.keytab"); |
| 69 |
is ($status, 0, 'k5start -u -i succeeds'); |
| 70 |
is ($err, '', ' with no errors'); |
| 71 |
like ($out, |
| 72 |
qr/^Kerberos initialization for \Q$principal\E(\@\S+)?\n\z/, |
| 73 |
' and the right output'); |
| 74 |
($default, $service) = klist (); |
| 75 |
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, |
| 76 |
' for the right principal'); |
| 77 |
like ($service, qr%^krbtgt/%, ' and the right service'); |
| 78 |
} |
| 79 |
|
| 80 |
# Now with -U it should figure out the principal itself. |
| 81 |
unlink 'krb5cc_test'; |
| 82 |
($out, $err, $status) = command ($K5START, '-Uf', "$DATA/test.keytab"); |
| 83 |
is ($status, 0, 'k5start -U succeeds'); |
| 84 |
is ($err, '', ' with no errors'); |
| 85 |
like ($out, qr/^Kerberos initialization for \Q$principal\E(\@\S+)?\n\z/, |
| 86 |
' and the right output'); |
| 87 |
($default, $service) = klist (); |
| 88 |
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, ' for the right principal'); |
| 89 |
like ($service, qr%^krbtgt/%, ' and the right service'); |
| 90 |
|
| 91 |
# Test quiet and an explicit ticket cache. |
| 92 |
unlink 'krb5cc_test'; |
| 93 |
($out, $err, $status) |
| 94 |
= command ($K5START, '-k', 'krb5cc_test2', '-qUf', "$DATA/test.keytab"); |
| 95 |
is ($status, 0, 'k5start -k -q succeeds'); |
| 96 |
is ($err, '', ' with no errors'); |
| 97 |
is ($out, '', ' and no output'); |
| 98 |
($default, $service) = klist (); |
| 99 |
is ($default, undef, ' and the normal ticket cache is untouched'); |
| 100 |
$ENV{KRB5CCNAME} = 'krb5cc_test2'; |
| 101 |
($default, $service) = klist (); |
| 102 |
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, |
| 103 |
' but the other has the right principal'); |
| 104 |
like ($service, qr%^krbtgt/%, ' and the right service'); |
| 105 |
unlink 'krb5cc_test2'; |
| 106 |
$ENV{KRB5CCNAME} = 'krb5cc_test'; |
| 107 |
|
| 108 |
# Test lifetime. Hopefully even a test principal can get a five minute |
| 109 |
# ticket lifetime. We don't bother to try to parse klist output to figure |
| 110 |
# out the lifetime, but instead check it using the -H option. |
| 111 |
unlink 'krb5cc_test'; |
| 112 |
($out, $err, $status) |
| 113 |
= command ($K5START, '-l', '5m', '-qUf', "$DATA/test.keytab"); |
| 114 |
is ($status, 0, 'k5start -l 5m succeeds'); |
| 115 |
is ($err, '', ' with no errors'); |
| 116 |
is ($out, '', ' and no output'); |
| 117 |
($default, $service) = klist (); |
| 118 |
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, ' and the right principal'); |
| 119 |
like ($service, qr%^krbtgt/%, ' and the right service'); |
| 120 |
($out, $err, $status) |
| 121 |
= command ($K5START, '-H', '4', '-f', '/nonexistent', $principal); |
| 122 |
is ($status, 0, ' and k5start -H succeeds without reauthenticating'); |
| 123 |
is ($err, '', ' with no errors'); |
| 124 |
is ($out, '', ' and no output'); |
| 125 |
($out, $err, $status) |
| 126 |
= command ($K5START, '-H', '10', '-f', '/nonexistent', $principal); |
| 127 |
is ($status, 1, ' but fails if we need a 10 minute ticket'); |
| 128 |
like ($err, qr/^k5start: error getting credentials: /, |
| 129 |
' with the right error'); |
| 130 |
is ($out, '', ' and no output'); |
| 131 |
|
| 132 |
# Test obtaining new tickets with -H. |
| 133 |
($out, $err, $status) |
| 134 |
= command ($K5START, '-qH', '10', '-Uf', "$DATA/test.keytab"); |
| 135 |
is ($status, 0, 'k5start -H succeeds with new tickets'); |
| 136 |
is ($err, '', ' with no errors'); |
| 137 |
is ($out, '', ' and no output'); |
| 138 |
($default, $service) = klist (); |
| 139 |
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, |
| 140 |
' and the right principal'); |
| 141 |
like ($service, qr%^krbtgt/%, ' and the right service'); |
| 142 |
($out, $err, $status) |
| 143 |
= command ($K5START, '-H', '10', '-f', '/nonexistent', $principal); |
| 144 |
is ($status, 0, ' and k5start -H 10 succeeds without reauthenticating'); |
| 145 |
is ($err, '', ' with no errors'); |
| 146 |
is ($out, '', ' and no output'); |
| 147 |
|
| 148 |
# Attempt to authenticate as some other principal. This should fail even |
| 149 |
# though we have a valid ticket, since we don't have a valid ticket for the |
| 150 |
# principal we want. |
| 151 |
($out, $err, $status) = command ($K5START, '-H', 10, '-f', '/nonexistent', |
| 152 |
'[email protected]'); |
| 153 |
is ($status, 1, ' and k5start -H 10 bogus tries to reauthenticate'); |
| 154 |
like ($err, qr/^k5start: error getting credentials: /, ' with correct error'); |
| 155 |
is ($out, '', ' and no output'); |
| 156 |
|
| 157 |
# Get a ticket for ourselves rather than a krbtgt and test verbose. We |
| 158 |
# need an instance here or we get weird results due to the defaults if -I |
| 159 |
# isn't provided. |
| 160 |
SKIP: { |
| 161 |
skip 'test principal has no instance', 8 unless $inst; |
| 162 |
unlink 'krb5cc_test'; |
| 163 |
($out, $err, $status) = command ($K5START, '-S', $name, '-I', $inst, |
| 164 |
'-vUf', "$DATA/test.keytab"); |
| 165 |
is ($status, 0, 'k5start -S -I succeeds'); |
| 166 |
is ($err, '', ' with no errors'); |
| 167 |
my $short = $principal; |
| 168 |
$short =~ s/\@\S+$//; |
| 169 |
like ($out, qr/^Kerberos\ initialization\ for\ \Q$principal\E(\@\S+)? |
| 170 |
\ for\ service\ \Q$short\E(\@\S+)?\n |
| 171 |
k5start:\ authenticating\ as\ \Q$principal\E(\@\S+)?\n |
| 172 |
k5start:\ getting\ tickets\ for |
| 173 |
\ \Q$principal\E(\@\S+)?\n\z/x, |
| 174 |
' and the right output'); |
| 175 |
($default, $service) = klist (); |
| 176 |
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, |
| 177 |
' for the right principal'); |
| 178 |
like ($service, qr/^\Q$principal\E(\@\S+)?\z/, ' and the right service'); |
| 179 |
|
| 180 |
# We now don't have a krbtgt ticket, so a regular k5start -H will attempt |
| 181 |
# to reauthenticate. |
| 182 |
($out, $err, $status) = command ($K5START, '-H', 10, '-f', '/nonexistent', |
| 183 |
$principal); |
| 184 |
is ($status, 1, ' and k5start -H 10 tries to reauthenticate'); |
| 185 |
like ($err, qr/^k5start: error getting credentials: /, |
| 186 |
' with correct error'); |
| 187 |
is ($out, '', ' and no output'); |
| 188 |
} |
| 189 |
|
| 190 |
# Test running a command without the principal. klist may fail if we have |
| 191 |
# no K4 tickets since we're not giving the -5 option; allow for that |
| 192 |
# (we'll catch real failures in the regex match on the output). |
| 193 |
unlink 'krb5cc_test'; |
| 194 |
($out, $err, $status) |
| 195 |
= command ($K5START, '-qUf', "$DATA/test.keytab", 'klist'); |
| 196 |
ok ($status == 0 || $status == 1, 'k5start with command succeeds'); |
| 197 |
ok ($err eq '' || $err eq "klist: You have no tickets cached\n", |
| 198 |
' with no or expected errors'); |
| 199 |
like ($out, qr,^(Credentials|Ticket)\ cache: |
| 200 |
\ (FILE:)?/tmp/krb5cc_\d+_\S{6}\n |
| 201 |
\s*(Default\ )?[Pp]rincipal:\ \Q$principal\E(\@\S+)?\n,xm, |
| 202 |
' and the right output'); |
| 203 |
ok (!-f 'krb5cc_test', ' and the default cache file was not created'); |
| 204 |
my ($cache) = ($out =~ /cache: (?:FILE:)?(\S+)/); |
| 205 |
ok (!$cache || !-f $cache, ' and the new cache file was deleted'); |
| 206 |
|
| 207 |
# Test running a command without the principal prefixed by --. |
| 208 |
unlink 'krb5cc_test'; |
| 209 |
($out, $err, $status) |
| 210 |
= command ($K5START, '-qUf', "$DATA/test.keytab", '--', 'klist', '-5'); |
| 211 |
is ($status, 0, 'k5start with command and -- succeeds'); |
| 212 |
is ($err, '', ' with no errors'); |
| 213 |
like ($out, qr,^(Credentials|Ticket)\ cache: |
| 214 |
\ (FILE:)?/tmp/krb5cc_\d+_\S{6}\n |
| 215 |
\s*(Default\ )?[Pp]rincipal:\ \Q$principal\E(\@\S+)?\n,xm, |
| 216 |
' and the right output'); |
| 217 |
ok (!-f 'krb5cc_test', ' and the default cache file was not created'); |
| 218 |
($cache) = ($out =~ /cache: (?:FILE:)?(\S+)/); |
| 219 |
ok (!$cache || !-f $cache, ' and the new cache file was deleted'); |
| 220 |
|
| 221 |
# Test running a command with the principal. |
| 222 |
unlink 'krb5cc_test'; |
| 223 |
($out, $err, $status) |
| 224 |
= command ($K5START, '-qf', "$DATA/test.keytab", $principal, 'klist'); |
| 225 |
ok ($status == 0 || $status == 1, |
| 226 |
'k5start with command and principal succeeds'); |
| 227 |
ok ($err eq '' || $err eq "klist: You have no tickets cached\n", |
| 228 |
' with no or expected errors'); |
| 229 |
like ($out, qr,^(Credentials|Ticket)\ cache: |
| 230 |
\ (FILE:)?/tmp/krb5cc_\d+_\S{6}\n |
| 231 |
\s*(Default\ )?[Pp]rincipal:\ \Q$principal\E(\@\S+)?\n,xm, |
| 232 |
' and the right output'); |
| 233 |
ok (!-f 'krb5cc_test', ' and the default cache file was not created'); |
| 234 |
($cache) = ($out =~ /cache: (?:FILE:)?(\S+)/); |
| 235 |
ok (!$cache || !-f $cache, ' and the new cache file was deleted'); |
| 236 |
|
| 237 |
# Test running a command with the principal and a command prefixed by --. |
| 238 |
unlink 'krb5cc_test'; |
| 239 |
($out, $err, $status) |
| 240 |
= command ($K5START, '-qf', "$DATA/test.keytab", $principal, '--', |
| 241 |
'klist', '-5'); |
| 242 |
is ($status, 0, 'k5start with command, principal, and -- succeeds'); |
| 243 |
is ($err, '', ' with no errors'); |
| 244 |
like ($out, qr,^(Credentials|Ticket)\ cache: |
| 245 |
\ (FILE:)?/tmp/krb5cc_\d+_\S{6}\n |
| 246 |
\s*(Default\ )?[Pp]rincipal:\ \Q$principal\E(\@\S+)?\n,xm, |
| 247 |
' and the right output'); |
| 248 |
ok (!-f 'krb5cc_test', ' and the default cache file was not created'); |
| 249 |
($cache) = ($out =~ /cache: (?:FILE:)?(\S+)/); |
| 250 |
ok (!$cache || !-f $cache, ' and the new cache file was deleted'); |
| 251 |
|
| 252 |
# Test running a command with the principal specified with -u. |
| 253 |
unlink 'krb5cc_test'; |
| 254 |
($out, $err, $status) |
| 255 |
= command ($K5START, '-u', $principal, '-qf', "$DATA/test.keytab", |
| 256 |
'klist'); |
| 257 |
ok ($status == 0 || $status == 1, 'k5start with command and -u succeeds'); |
| 258 |
ok ($err eq '' || $err eq "klist: You have no tickets cached\n", |
| 259 |
' with no or expected errors'); |
| 260 |
like ($out, qr,^(Credentials|Ticket)\ cache: |
| 261 |
\ (FILE:)?/tmp/krb5cc_\d+_\S{6}\n |
| 262 |
\s*(Default\ )?[Pp]rincipal:\ \Q$principal\E(\@\S+)?\n,xm, |
| 263 |
' and the right output'); |
| 264 |
ok (!-f 'krb5cc_test', ' and the default cache file was not created'); |
| 265 |
($cache) = ($out =~ /cache: (?:FILE:)?(\S+)/); |
| 266 |
ok (!$cache || !-f $cache, ' and the new cache file was deleted'); |
| 267 |
|
| 268 |
# Test running a command with the principal specified with -u and -- |
| 269 |
# before the command. |
| 270 |
unlink 'krb5cc_test'; |
| 271 |
($out, $err, $status) |
| 272 |
= command ($K5START, '-u', $principal, '-qf', "$DATA/test.keytab", |
| 273 |
'--', 'klist', '-5'); |
| 274 |
is ($status, 0, 'k5start with command, -u, and -- succeeds'); |
| 275 |
is ($err, '', ' with no errors'); |
| 276 |
like ($out, qr,^(Credentials|Ticket)\ cache: |
| 277 |
\ (FILE:)?/tmp/krb5cc_\d+_\S{6}\n |
| 278 |
\s*(Default\ )?[Pp]rincipal:\ \Q$principal\E(\@\S+)?\n,xm, |
| 279 |
' and the right output'); |
| 280 |
ok (!-f 'krb5cc_test', ' and the default cache file was not created'); |
| 281 |
($cache) = ($out =~ /cache: (?:FILE:)?(\S+)/); |
| 282 |
ok (!$cache || !-f $cache, ' and the new cache file was deleted'); |
| 283 |
|
| 284 |
# Test propagation of exit status from a command. |
| 285 |
unlink 'krb5cc_test'; |
| 286 |
($out, $err, $status) |
| 287 |
= command ($K5START, '-Uqf', "$DATA/test.keytab", '--', 'sh', '-c', |
| 288 |
'exit 3'); |
| 289 |
is ($status, 3, 'k5start of exit 3 returns correct exit status'); |
| 290 |
is ($err, '', ' with no errors'); |
| 291 |
ok (!-f 'krb5cc_test', ' and the default cache file was not created'); |
| 292 |
|
| 293 |
# Test propagation of exit status from a command which is killed by signal. |
| 294 |
unlink 'krb5cc_test'; |
| 295 |
($out, $err, $status) |
| 296 |
= command ($K5START, '-Uqf', "$DATA/test.keytab", '--', 'sh', '-c', |
| 297 |
'kill $$'); |
| 298 |
is ($status, 128 + SIGTERM, 'k5start of kill $$ returns correct exit status'); |
| 299 |
is ($err, '', ' with no errors'); |
| 300 |
ok (!-f 'krb5cc_test', ' and the default cache file was not created'); |
| 301 |
|
| 302 |
# Clean up. |
| 303 |
unlink 'krb5cc_test'; |