Imported Upstream version 1.10.2
[platform/upstream/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 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     if ![kinit_renew krbtest/admin adminpass$KEY 1] {
205         return
206     }
207
208     # Make sure that klist can see the ticket.
209     if ![do_klist "krbtest/admin@$REALMNAME" "krbtgt/$REALMNAME@$REALMNAME" "klist"] {
210         return
211     }
212
213 # Get a ticket to later use with FAST
214     if ![kinit krbtest/fast adminpass$KEY 1] {
215         return
216     }
217
218     # Use fast to get a ticket
219     if ![kinit_fast krbtest/fast adminpass$KEY 1] {
220         return
221     }
222
223     # Destroy the ticket.
224     spawn $KDESTROY -5
225     if ![check_exit_status "kdestroy"] {
226         return
227     }
228     pass "kdestroy"
229
230     # Double check that the ticket was destroyed.
231     if ![do_klist_err "klist after destroy"] { return }
232
233     if ![add_random_key WELLKNOWN/ANONYMOUS 0] {
234         return
235     }
236
237     # If we have anonymous  then test it
238     if [file exists "$tmppwd/../../../util/fakedest$MODULE_DIR/preauth/pkinit.so" ] {
239         kinit_anonymous "WELLKNOWN/ANONYMOUS"
240     }
241
242     if ![add_random_key foo/bar 1] {
243         return
244     }
245
246     set keytab $tmppwd/fookeytab
247     catch "exec rm -f $keytab"
248
249     modify_principal foo/bar -kvno 252
250     foreach vno {253 254 255 256 257 258} {
251         xst $tmppwd/fookeytab foo/bar
252         do_klist_kt $tmppwd/fookeytab "klist keytab foo/bar vno $vno"
253         kinit_kt "foo/bar" $tmppwd/fookeytab 1 "kt kvno $vno"
254         do_klist "foo/bar" "krbtgt/$REALMNAME@$REALMNAME" "klist kt foo/bar vno $vno"
255         do_kdestroy "kdestroy foo/bar vno $vno"
256     }
257     catch "exec rm -f $keytab"
258     # Check that kadmin.local can actually read the correct kvno, even
259     # if we don't expect kadmin to be able to.
260     setup_kerberos_env kdc
261     spawn $KADMIN_LOCAL -r $REALMNAME
262     set ok 1
263     expect_after {
264         timeout         { fail "kadmin.local correct high kvno" ; set ok 0 }
265         eof             { fail "kadmin.local correct high kvno" ; set ok 0 }
266     }
267     expect "kadmin.local: "
268     send "getprinc foo/bar\r"
269 #    exec sleep 10
270     expect "Key: vno $vno,"
271     send "quit\r"
272     expect eof
273     if [check_exit_status "kadmin.local examine foo/bar for high kvno"] {
274         if $ok {
275             pass "kadmin.local correct high kvno"
276         }
277     }
278 }
279
280 set status [catch doit msg]
281
282 stop_kerberos_daemons
283
284 if { $status != 0 } {
285     send_error "ERROR: error in standalone.exp\n"
286     send_error "$msg\n"
287     exit 1
288 }