automated tests for anonymous pkinit
[krb5.git] / src / tests / dejagnu / krb-standalone / standalone.exp
1 # Standalone Kerberos test.
2 # This is a DejaGnu test script.
3 # This script tests that the Kerberos tools can talk to each other.
4
5 # This mostly just calls procedures in testsuite/config/default.exp.
6
7 # Set up the Kerberos files and environment.
8 if {![get_hostname] || ![setup_kerberos_files] || ![setup_kerberos_env]} {
9     return
10 }
11
12 # Initialize the Kerberos database.  The argument tells
13 # setup_kerberos_db that it is being called from here.
14 if ![setup_kerberos_db 1] {
15     return
16 }
17
18 # We are about to start up a couple of daemon processes.  We do all
19 # the rest of the tests inside a proc, so that we can easily kill the
20 # processes when the procedure ends.
21
22 proc dump_and_reload {} {
23     global KDB5_UTIL
24     global tmppwd
25
26     set dumpfile $tmppwd/dump-file
27     set dumpokfile $dumpfile.dump_ok
28
29     set test1name "kdb5_util dump"
30     set test2name "kdb5_util load"
31
32     if [file exists $dumpfile] { file delete $dumpfile }
33     if [file exists $dumpokfile] { file delete $dumpokfile }
34
35     spawn $KDB5_UTIL dump $dumpfile
36     expect {
37         -re "..*" {
38             fail $test1name
39             untested $test2name
40             return
41         }
42         timeout {
43             fail $test1name
44             untested $test2name
45             return
46         }
47         eof { }
48     }
49     if ![check_exit_status $test1name] {
50         untested $test2name
51         return
52     }
53     if ![file exists $dumpfile]||![file exists $dumpokfile] {
54         fail $test1name
55         untested $test2name
56         return
57     }
58     pass $test1name
59
60     spawn $KDB5_UTIL load $dumpfile
61     expect {
62         -re "..*" {
63             fail $test2name
64             return
65         }
66         timeout {
67             fail $test2name
68             return
69         }
70         eof { }
71     }
72     if [check_exit_status $test2name] {
73         pass $test2name
74     }
75 }
76
77 proc kinit_wrong_pw { name badpass } {
78     global REALMNAME
79     global KINIT
80     global spawn_id
81
82     # Use kinit to get a ticket.
83         #
84         # For now always get forwardable tickets. Later when we need to make
85         # tests that distiguish between forwardable tickets and otherwise
86         # we should but another option to this proc. --proven
87         #
88     spawn $KINIT -5 -f $name@$REALMNAME
89     expect {
90         "Password for $name@$REALMNAME:" {
91             verbose "kinit started"
92         }
93         timeout {
94             fail "kinit bad pw"
95             return 0
96         }
97         eof {
98             fail "kinit bad pw"
99             return 0
100         }
101     }
102     send "$badpass\r"
103     expect {
104         "Password incorrect while getting initial credentials" {
105         }
106         timeout {
107             fail "kinit bad pw"
108             # kill it?
109         }
110         eof {
111             fail "kinit bad pw"
112             return
113         }
114     }
115     expect eof
116
117     set status_list [wait -i $spawn_id]
118     catch "close -i $spawn_id"
119     verbose -log "exit status: $status_list"
120     if { [lindex $status_list 2] != 0 || [lindex $status_list 3] != 0 } {
121         pass "kinit bad pw"
122     } else {
123         fail "kinit bad pw"
124     }
125 }
126
127 proc doit { } {
128     global REALMNAME
129     global KLIST
130     global KDESTROY
131     global KEY
132     global KADMIN_LOCAL
133     global KTUTIL
134     global hostname
135     global tmppwd
136     global spawn_id
137     global supported_enctypes
138     global KRBIV
139     global portbase
140     global mode
141     global tmppwd
142     global KRB5_PA_MODULE_DIR
143
144     setup_kerberos_env kdc
145
146     # Start up the kerberos and kadmind daemons.
147     if ![start_kerberos_daemons 1] {
148         return
149     }
150
151     # Use kadmin to add an host key.
152     if ![add_random_key host/$hostname 1] {
153         return
154     }
155
156     spawn $KADMIN_LOCAL -q "addpol fred"
157     catch expect_after
158     expect {
159         timeout {
160             fail "kadmin.local addpol fred"
161         }
162         eof {
163             pass "kadmin.local addpol fred"
164         }
165     }
166     set k_stat [wait -i $spawn_id]
167     verbose "wait -i $spawn_id returned $k_stat (kadmin addpol)"
168     catch "close -i $spawn_id"
169
170     # Use ksrvutil to create a srvtab entry.
171     if ![setup_srvtab 1] {
172         return
173     }
174
175     # Test dump and load.  Continue on, whatever the result.
176     dump_and_reload
177
178     spawn $KADMIN_LOCAL -q "getpols"
179     expect {
180         fred {
181             pass "kadmin.local getpols"
182             expect eof
183         }
184         timeout {
185             fail "kadmin.local getpols"
186         }
187         eof {
188             fail "kadmin.local getpols"
189         }
190     }
191     set k_stat [wait -i $spawn_id]
192     verbose "wait -i $spawn_id returned $k_stat (kadmin addpol)"
193     catch "close -i $spawn_id"
194
195     # Test use of wrong password.
196     kinit_wrong_pw krbtest/admin wrongpassword
197
198     setup_kerberos_env client
199     # Use kinit to get a ticket.
200     if ![kinit krbtest/admin adminpass$KEY 1] {
201         return
202     }
203
204     # Make sure that klist can see the ticket.
205     if ![do_klist "krbtest/admin@$REALMNAME" "krbtgt/$REALMNAME@$REALMNAME" "klist"] {
206         return
207     }
208
209 # Get a ticket to later use with FAST
210     if ![kinit krbtest/fast adminpass$KEY 1] {
211         return
212     }
213
214     # Use fast to get a ticket
215     if ![kinit_fast krbtest/fast adminpass$KEY 1] {
216         return
217     }
218
219     # Destroy the ticket.
220     spawn $KDESTROY -5
221     if ![check_exit_status "kdestroy"] {
222         return
223     }
224     pass "kdestroy"
225
226     # Double check that the ticket was destroyed.
227     if ![do_klist_err "klist after destroy"] { return }
228
229     if ![add_random_key WELLKNOWN/ANONYMOUS 0] {
230         return
231     }
232
233     # If we have anonymous  then test it
234     if [file exists "$tmppwd/../../../util/fakedest$KRB5_PA_MODULE_DIR/pkinit.so" ] {
235         kinit_anonymous "WELLKNOWN/ANONYMOUS"
236     }
237
238     if ![add_random_key foo/bar 1] {
239         return
240     }
241
242     set keytab $tmppwd/fookeytab
243     catch "exec rm -f $keytab"
244
245     modify_principal foo/bar -kvno 252
246     foreach vno {253 254 255 256 257 258} {
247         xst $tmppwd/fookeytab foo/bar
248         do_klist_kt $tmppwd/fookeytab "klist keytab foo/bar vno $vno"
249         kinit_kt "foo/bar" $tmppwd/fookeytab 1 "kt kvno $vno"
250         do_klist "foo/bar" "krbtgt/$REALMNAME@$REALMNAME" "klist kt foo/bar vno $vno"
251         do_kdestroy "kdestroy foo/bar vno $vno"
252     }
253     catch "exec rm -f $keytab"
254     # Check that kadmin.local can actually read the correct kvno, even
255     # if we don't expect kadmin to be able to.
256     setup_kerberos_env kdc
257     spawn $KADMIN_LOCAL -r $REALMNAME
258     set ok 1
259     expect_after {
260         timeout         { fail "kadmin.local correct high kvno" ; set ok 0 }
261         eof             { fail "kadmin.local correct high kvno" ; set ok 0 }
262     }
263     expect "kadmin.local: "
264     send "getprinc foo/bar\r"
265 #    exec sleep 10
266     expect "Key: vno $vno,"
267     send "quit\r"
268     expect eof
269     if [check_exit_status "kadmin.local examine foo/bar for high kvno"] {
270         if $ok {
271             pass "kadmin.local correct high kvno"
272         }
273     }
274 }
275
276 set status [catch doit msg]
277
278 stop_kerberos_daemons
279
280 if { $status != 0 } {
281     send_error "ERROR: error in standalone.exp\n"
282     send_error "$msg\n"
283     exit 1
284 }