aboutsummaryrefslogtreecommitdiff
path: root/src/kadmin/testing/scripts/make-host-keytab.plin
blob: dfe0b3a0138023af32cf79d221f76c64da2ff516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/local/bin/perl 

$server = undef;
@princs = ();
$top = undef;

($whoami = $0) =~ s,.*/,,;
$usage = "Usage: $whoami [ -server server ] [ -princ principal ]
		[ -top dirname ] [ -verbose ] filename
	Server defaults to the local host.
	Default principals are host/hostname\@SECURE-TEST.OV.COM and
	  test/hostname\@SECURE-TEST.OV.COM.
	If any principals are specified, the default principals are
	  not added to the srvtab.
	The string \"xCANONHOSTx\" in a principal specification will be
	  replaced by the canonical host name of the local host.";

@ORIG_ARGV = @ARGV;

while (($_ = $ARGV[0]) && /^-/) {
    shift;
    if (/^-server$/) {
	($server = shift) || die "Missing argument to $_ option.\n$usage\n";
    }
    elsif (/^-princ$/) {
	($princ = shift) || die "Missing argument to $_ option.\n$usage\n";
	push(@princs, $princ);
    }
    elsif (/^-top$/) {
	($top = shift) || die "Missing argument to $_ option.\n$usage\n";
    }
    elsif (/^-verbose$/) {
	$verbose++;
    }
    elsif (/^--$/) {
	last;
    }
    else {
	die "Unknown option $_.\n$usage\n";
    }
}

@princs = ("host/xCANONHOSTx\@SECURE-TEST.OV.COM",
	   "test/xCANONHOSTx\@SECURE-TEST.OV.COM")
    if (! @princs);

$ktfile = shift(@ARGV) || die "need a keytab file\n";

$verbose++ if ($ENV{'VERBOSE_TEST'});

print "In $0 @ORIG_ARGV...\n" if ($verbose);

chop ($canonhost = `hostname`);

($canonhost,$aliases,$addrtype,$length,@addrs) = gethostbyname($canonhost);
die "couldn't get canonical hostname\n" if !($canonhost && @addrs);
($canonhost2) = gethostbyaddr($addrs[0],$addrtype);
if ($canonhost2) { $canonhost = $canonhost2; }

for (@princs) {
    s/xCANONHOSTx/$canonhost/g;
}

die "Neither \$TOP nor \$TESTDIR is set, and -top not specified.\n"
    if (! ($top || $ENV{'TOP'} || $ENV{'TESTDIR'}));

$top = $ENV{'TOP'} if (! $top);
$TESTDIR = ($ENV{'TESTDIR'} || "$top/testing");
$MAKE_KEYTAB = ($ENV{'MAKE_KEYTAB'} || "$TESTDIR/scripts/$whoami");
$SRVTCL = ($ENV{'SRVTCL'} || "$TESTDIR/util/kadm5_srv_tcl");
$TCLUTIL = ($ENV{'TCLUTIL'} || "$TESTDIR/tcl/util.t");
# This'll be wrong sometimes
$RSH_CMD = ($ENV{'RSH_CMD'} || '/usr/ucb/rsh');
$KADMIN = ($ENV{'KADMIN'} || "$top/cli/kadmin.local");

if ($server) {
# XXX Using /usr/ucb/rsh for now.

# Strip command line options because we're adding our own.

    $MAKE_KEYTAB =~ s/ .*//;

    if ($ENV{'TOP'} && ($top ne $ENV{'TOP'})) {
# Replace the old TOP with the new one where necessary
	for ('TESTDIR', 'SRVTCL', 'TCLUTIL', 'MAKE_KEYTAB') {
	    eval "\$$_ =~ s/^\$ENV{'TOP'}/\$top/;";
	}

# Make the paths as short as possible so our command line isn't too long.
#	for ('SRVTCL', 'TCLUTIL', 'MAKE_KEYTAB') {
#	    eval "\$$_ =~ s/^\$TESTDIR/\\\\\\\$TESTDIR/;";
#	}
#	for ('TESTDIR', 'SRVTCL', 'TCLUTIL', 'MAKE_KEYTAB') {
#	    eval "\$$_ =~ s/^\$top/\\\\\\\$TOP/;";
#	}
    }

    $cmd = "cd $top; \\`testing/scripts/find-make.sh\\` execute TOP=$top ";
    $cmd .= "VERBOSE_TEST=$verbose " if ($verbose);
    $cmd .= "TESTDIR=$TESTDIR ";
    $cmd .= "SRVTCL=$SRVTCL ";
    $cmd .= "TCLUTIL=$TCLUTIL ";

    $cmd .= "CMD='$MAKE_KEYTAB ";
    for (@princs) {
	$cmd .= "-princ $_ ";
    }
    $cmd .= " /tmp/make-keytab.$canonhost.$$'";#';

    $cmd = "$RSH_CMD $server -l root -n \"$cmd\"";

    $cmd2 = "$RSH_CMD $server -l root -n \"cat /tmp/make-keytab.$canonhost.$$\" > $ktfile";

    $cmd3 = "$RSH_CMD $server -l root -n \"rm /tmp/make-keytab.$canonhost.$$\"";

    for ($cmd, $cmd2, $cmd3) {
	print "$_\n" if ($verbose);

	system($_) && die "Couldn't run $_: $!.\n";
    }
}
else {
    $redirect = "> /dev/null" if (! $verbose);

    # We can ignore errors here, because the ktadd below will fail if
    # this fails for any reason other than "principal exists"
    for (@princs) {
	next if (/^kadmin/);
	$cmd = "$KADMIN -q 'ank -randkey $_' $redirect 2>&1";
	system($cmd);
    }
    
    $cmd = "$KADMIN -q 'ktadd -k $ktfile ";
    $cmd .= " -q " if (! $verbose);
    $cmd .= "@princs' $redirect";
    if (system "$cmd") {
	sleep(1);
	die "Error in system($cmd)\n";
    }
}

if (! -f $ktfile) {
    die "$ktfile not created.\n";
}