| 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-2010 |
| 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 |
# Decide whether we have the configuration to run the tests. |
| 24 |
if (-f "$DATA/test.keytab" and -f "$DATA/test.principal") { |
| 25 |
plan tests => 34; |
| 26 |
} else { |
| 27 |
plan skip_all => "no keytab configuration"; |
| 28 |
exit 0; |
| 29 |
} |
| 30 |
my $principal = contents ("$DATA/test.principal"); |
| 31 |
|
| 32 |
# We have to generate a local krb5.conf that gets forwardable and |
| 33 |
# proxiable tickets by default. Try to locate the local krb5.conf that |
| 34 |
# we're supposed to use and bail if we can't find one. |
| 35 |
my $krb5conf = $ENV{KRB5_CONFIG}; |
| 36 |
unless ($krb5conf) { |
| 37 |
for my $path ('/etc', '/usr/local/etc', "$ENV{C_TAP_BUILD}/data") { |
| 38 |
if (-r "$path/krb5.conf") { |
| 39 |
$krb5conf = "$path/krb5.conf"; |
| 40 |
last; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
if ($krb5conf) { |
| 45 |
open (CONF, '<', $krb5conf) or BAIL_OUT ("cannot open $krb5conf: $!"); |
| 46 |
open (NEWCONF, '>', './krb5.conf') |
| 47 |
or BAIL_OUT ("cannot create krb5.conf: $!"); |
| 48 |
print NEWCONF <CONF>; |
| 49 |
close CONF; |
| 50 |
print NEWCONF "\n"; |
| 51 |
print NEWCONF "[libdefaults]\n"; |
| 52 |
print NEWCONF " forwardable = true\n"; |
| 53 |
print NEWCONF " proxiable = true\n"; |
| 54 |
close NEWCONF; |
| 55 |
$ENV{KRB5_CONFIG} = './krb5.conf'; |
| 56 |
} else { |
| 57 |
BAIL_OUT ("no krb5.conf found, set KRB5_CONFIG"); |
| 58 |
} |
| 59 |
|
| 60 |
# Don't overwrite the user's ticket cache. |
| 61 |
$ENV{KRB5CCNAME} = 'krb5cc_test'; |
| 62 |
|
| 63 |
# Basic authentication test with forwardable and proxiable tickets. |
| 64 |
unlink 'krb5cc_test'; |
| 65 |
my ($out, $err, $status) |
| 66 |
= command ($K5START, '-f', "$DATA/test.keytab", $principal); |
| 67 |
is ($status, 0, 'Basic k5start command succeeds'); |
| 68 |
is ($err, '', ' with no errors'); |
| 69 |
like ($out, qr/^Kerberos initialization for \Q$principal\E(\@\S+)?\n\z/, |
| 70 |
' and the right output'); |
| 71 |
my ($default, $service, $flags) = klist (); |
| 72 |
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, ' for the right principal'); |
| 73 |
like ($service, qr%^krbtgt/%, ' and the right service'); |
| 74 |
like ($flags, qr/F/, ' and has forwardable tickets'); |
| 75 |
like ($flags, qr/P/, ' and has proxiable tickets'); |
| 76 |
|
| 77 |
# Authentication without proxiable tickets. |
| 78 |
unlink 'krb5cc_test'; |
| 79 |
($out, $err, $status) |
| 80 |
= command ($K5START, '-Pf', "$DATA/test.keytab", $principal); |
| 81 |
is ($status, 0, 'k5start -P command succeeds'); |
| 82 |
is ($err, '', ' with no errors'); |
| 83 |
like ($out, qr/^Kerberos initialization for \Q$principal\E(\@\S+)?\n\z/, |
| 84 |
' and the right output'); |
| 85 |
($default, $service, $flags) = klist (); |
| 86 |
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, ' for the right principal'); |
| 87 |
like ($service, qr%^krbtgt/%, ' and the right service'); |
| 88 |
like ($flags, qr/F/, ' and has forwardable tickets'); |
| 89 |
unlike ($flags, qr/P/, ' but not proxiable tickets'); |
| 90 |
|
| 91 |
# Authentication without forwardable tickets. |
| 92 |
unlink 'krb5cc_test'; |
| 93 |
($out, $err, $status) |
| 94 |
= command ($K5START, '-F', '-f', "$DATA/test.keytab", $principal); |
| 95 |
is ($status, 0, 'k5start -F command succeeds'); |
| 96 |
is ($err, '', ' with no errors'); |
| 97 |
like ($out, qr/^Kerberos initialization for \Q$principal\E(\@\S+)?\n\z/, |
| 98 |
' and the right output'); |
| 99 |
($default, $service, $flags) = klist (); |
| 100 |
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, ' for the right principal'); |
| 101 |
like ($service, qr%^krbtgt/%, ' and the right service'); |
| 102 |
like ($flags, qr/P/, ' and has proxiable tickets'); |
| 103 |
unlike ($flags, qr/F/, ' but not forwardable tickets'); |
| 104 |
|
| 105 |
# Authentication with both flags flag. |
| 106 |
unlink 'krb5cc_test'; |
| 107 |
($out, $err, $status) |
| 108 |
= command ($K5START, '-FPf', "$DATA/test.keytab", $principal); |
| 109 |
is ($status, 0, 'k5start -F -P command succeeds'); |
| 110 |
is ($err, '', ' with no errors'); |
| 111 |
like ($out, qr/^Kerberos initialization for \Q$principal\E(\@\S+)?\n\z/, |
| 112 |
' and the right output'); |
| 113 |
($default, $service, $flags) = klist (); |
| 114 |
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, ' for the right principal'); |
| 115 |
like ($service, qr%^krbtgt/%, ' and the right service'); |
| 116 |
unlike ($flags, qr/P/, ' but not proxiable tickets'); |
| 117 |
unlike ($flags, qr/F/, ' and not forwardable tickets'); |
| 118 |
|
| 119 |
# Test -k with a fully-qualified ticket cache name. |
| 120 |
unlink 'krb5cc_test', 'krb5cc_test2'; |
| 121 |
($out, $err, $status) |
| 122 |
= command ($K5START, '-k', 'FILE:krb5cc_test2', '-f', |
| 123 |
"$DATA/test.keytab", $principal); |
| 124 |
is ($status, 0, 'k5start -k command with file succeeds'); |
| 125 |
is ($err, '', ' with no errors'); |
| 126 |
like ($out, qr/^Kerberos initialization for \Q$principal\E(\@\S+)?\n\z/, |
| 127 |
' and the right output'); |
| 128 |
ok (!-f 'krb5cc_test', ' and does not use KRB5CCNAME'); |
| 129 |
$ENV{KRB5CCNAME} = 'krb5cc_test2'; |
| 130 |
($default, $service, $flags) = klist (); |
| 131 |
like ($default, qr/^\Q$principal\E(\@\S+)?\z/, ' for the right principal'); |
| 132 |
like ($service, qr%^krbtgt/%, ' and the right service'); |
| 133 |
$ENV{KRB5CCNAME} = 'krb5cc_test'; |
| 134 |
unlink 'krb5cc_test2'; |
| 135 |
|
| 136 |
# Clean up. |
| 137 |
unlink 'krb5cc_test', 'krb5.conf'; |