summaryrefslogtreecommitdiffstats
path: root/src/tests/dejagnu/krb-standalone/kadmin.exp
blob: c62e183e75a408baf1d00b51c2e93960912fcb33 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
# Kerberos kadmin test.
# This is a DejaGnu test script.
# This script tests Kerberos kadmin5 using kadmin.local as verification.

#++
# kadmin_add	- Test add new v5 principal function of kadmin.
#
# Adds principal $pname with password $password.  Returns 1 on success.
#--
proc kadmin_add { pname password } {
    global REALMNAME
    global KADMIN
    global KADMIN_LOCAL
    global KEY
    global spawn_id
    global tmppwd

    set good 0
    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "ank $pname"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin add $pname lost KDC"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin add $pname"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin add $pname"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*:" {
	send "adminpass$KEY\r"
    }
    expect "Enter password for principal \"$pname@$REALMNAME\":" { send "$password\r" }
    expect "Re-enter password for principal \"$pname@$REALMNAME\":" { send "$password\r" }
    expect "Principal \"$pname@$REALMNAME\" created." { set good 1 }
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin add)"
    catch "close -i $spawn_id"
    if { $good == 1 } {
	#
	# use kadmin.local to verify that a principal was created and that its
	# salt types are 0 (normal).
	#
	envstack_push
	setup_kerberos_env kdc
	spawn $KADMIN_LOCAL -r $REALMNAME
	envstack_pop
	expect_after {
	    -i $spawn_id
	    timeout {
		fail "kadmin add $pname"
		catch "expect_after"
		return 0
	    }
	    eof {
		fail "kadmin add $pname"
		catch "expect_after"
		return 0
	    }
	}
	set good 0
	expect "kadmin.local: " { send "getprinc $pname\r" }
	expect "Principal: $pname@$REALMNAME" { set good 1 }
	expect "Expiration date:" { verbose "got expiration date" }
	expect "Last password change:" { verbose "got last pwchange" }
	expect "Password expiration date:" { verbose "got pwexpire date" }
	expect "Maximum ticket life:" { verbose "got max life" }
	expect "Maximum renewable life:" { verbose "got max rlife" }
	expect "Last modified:" { verbose "got last modified" }
	expect "Last successful authentication:" { verbose "last succ auth" }
	expect "Last failed authentication:" { verbose "last pw failed" }
	expect "Failed password attempts:" { verbose "num failed attempts" }
	expect "Number of keys:" { verbose "num keys"} 
	expect {
		"Key: " { verbose "Key listed" 
			exp_continue
		}
		"Attributes:" { verbose "attributes" }
	}
	expect "kadmin.local: " { send "q\r" }

	expect_after
	expect eof
	set k_stat [wait -i $spawn_id]
	verbose "wait -i $spawn_id returned $k_stat (kadmin.local show)"
	catch "close -i $spawn_id"
	if { $good == 1 } {
	    pass "kadmin add $pname"
	    return 1
	}
	else {
	    fail "kadmin add $pname"
	    return 0
	}
    }
    else {
	fail "kadmin add $pname"
	return 0
    }
}

#++
# kadmin_add_rnd	- Test add new v5 principal with random key function.
#
# Adds principal $pname with random key.  Returns 1 on success.
#--
proc kadmin_add_rnd { pname { flags "" } } {
    global REALMNAME
    global KADMIN
    global KADMIN_LOCAL
    global KEY
    global spawn_id
    global tmppwd

    set good 0
    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "ank -randkey $flags $pname"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin add rnd $pname lost KDC"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin add_rnd $pname"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin add_rnd $pname"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*: *" {
	send "adminpass$KEY\r"
    }
    expect "Principal \"$pname@$REALMNAME\" created." { set good 1 }
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin add_rnd)"
    catch "close -i $spawn_id"
    if { $good == 1 } {
	#
	# use kadmin.local to verify that a principal was created and that its
	# salt types are 0 (normal).
	#
	envstack_push
	setup_kerberos_env kdc
	spawn $KADMIN_LOCAL -r $REALMNAME
	envstack_pop
	expect_after {
	     -i $spawn_id
	    timeout {
		fail "kadmin add_rnd $pname"
		catch "expect_after"
		return 0
	    }
	    eof {
		fail "kadmin add_rnd $pname"
		catch "expect_after"
		return 0
	    }
	}
	set good 0
	expect "kadmin.local:" { send "getprinc $pname\r" }
	expect "Principal: $pname@$REALMNAME" { set good 1 }
	expect "kadmin.local:" { send "q\r" }
	expect_after
	expect eof
	set k_stat [wait -i $spawn_id]
	verbose "wait -i $spawn_id returned $k_stat (kadmin.local show)"
	catch "close -i $spawn_id"
	if { $good == 1 } {
	    pass "kadmin add_rnd $pname"
	    return 1
	}
	else {
	    fail "kadmin add_rnd $pname"
	    return 0
	}
    }
    else {
	fail "kadmin add_rnd $pname"
	return 0
    }
}

#++
# kadmin_show	- Test show principal function of kadmin.
# 
# Retrieves entry for $pname.  Returns 1 on success.
#--
proc kadmin_show { pname } {
    global REALMNAME
    global KADMIN
    global KEY
    global spawn_id

    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "get_principal $pname"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin show $pname lost KDC"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin show $pname"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin show $pname"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*: *"
    send "adminpass$KEY\r"
    expect -re "\r.*Principal: $pname@$REALMNAME.*Key: .*Attributes:.*Policy: .*\r"
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin show)"
    catch "close -i $spawn_id"
    pass "kadmin show $pname"
    return 1
}

#++
# kadmin_cpw	- Test change password function of kadmin
#
# Change password of $pname to $password.  Returns 1 on success.
#--
proc kadmin_cpw { pname password } {
    global REALMNAME
    global KADMIN
    global KEY
    global spawn_id

    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "cpw $pname"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin cpw $pname lost KDC"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin cpw $pname"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin cpw $pname"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*: *" {
	send "adminpass$KEY\r"
    }

    expect "Enter password for principal \"$pname@$REALMNAME\":" { send "$password\r" }
    expect "Re-enter password for principal \"$pname@$REALMNAME\":" { send "$password\r" }
    # When in doubt, jam one of these in there.
    expect "\r"
    expect "Password for \"$pname@$REALMNAME\" changed."
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin cpw)"
    catch "close -i $spawn_id"
    pass "kadmin cpw $pname"
    return 1
}

#++
# kadmin_cpw_rnd	- Test change random key function of kadmin.
#
# Changes principal $pname's key to a new random key.  Returns 1 on success.
#--
proc kadmin_cpw_rnd { pname } {
    global REALMNAME
    global KADMIN
    global KEY
    global spawn_id

    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "cpw -randkey $pname"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin cpw_rnd $pname lost KDC"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin cpw_rnd $pname"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin cpw_rnd $pname"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*: *" {
	send "adminpass$KEY\r"
    }
    # When in doubt, jam one of these in there.
    expect "\r"
    expect "Key for \"$pname@$REALMNAME\" randomized."
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin cpw_rnd)"
    catch "close -i $spawn_id"
    pass "kadmin cpw_rnd $pname"
    return 1
}

#++
# kadmin_modify	- Test modify principal function of kadmin.
#
# Modifies principal $pname with flags $flags.  Returns 1 on success.
#--
proc kadmin_modify { pname flags } {
    global REALMNAME
    global KADMIN
    global KEY
    global spawn_id

    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "modprinc $flags $pname"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin modify $pname ($flags) lost KDC"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin modify $pname"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin modify $pname"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*: *"
    send "adminpass$KEY\r"
    # When in doubt, jam one of these in there.
    expect "\r"
    expect "Principal \"$pname@$REALMNAME\" modified."
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin modify)"
    catch "close -i $spawn_id"
    pass "kadmin modify $pname"
    return 1
}


#++
# kadmin_list	- Test list database function of kadmin.
#
# Lists the database and verifies that output matches regular expression
# "(.*@$REALMNAME)*".  Returns 1 on success.
#--
proc kadmin_list {  } {
    global REALMNAME
    global KADMIN
    global KEY
    global spawn_id

    # "*" would match everything
    # "*n" should match a few like kadmin/admin but see ticket 5667
    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "get_principals *n"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin ldb lost KDC"
	    catch "expect_after"
	    return 0
	}
	"Communication failure" {
	    fail "kadmin ldb got RPC error"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin ldb"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin ldb"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*: *" {
	send "adminpass$KEY\r"
    }
    expect -re "\(.*@$REALMNAME\r\n\)+"
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin list)"
    catch "close -i $spawn_id"
    pass "kadmin ldb"
    return 1
}

#++
# kadmin_extract	- Test extract service key function of kadmin.
#
# Extracts service key for service name $name instance $instance.  Returns
# 1 on success.
#--
proc kadmin_extract { instance name } {
    global REALMNAME
    global KADMIN
    global KEY
    global spawn_id
    global tmppwd

    catch "exec rm -f $tmppwd/keytab"

    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "xst -k $tmppwd/keytab $name/$instance"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin xst $instance $name lost KDC"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin xst $instance $name"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin xst $instance $name"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*: *" {
	send "adminpass$KEY\r"
    }
#    expect -re "kadmin: Entry for principal $name/$instance with kvno [0-9], encryption type .* added to keytab WRFILE:$tmppwd/keytab."
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin xst)"
    catch "close -i $spawn_id"
    catch "exec rm -f $instance-new-srvtab"
    pass "kadmin xst $instance $name"
    return 1
}

#++
# kadmin_extractv4	- Test extract service key in v4 format function of
#			  kadmin.
#
# Extracts service key for service name $name instance $instance in version
# 4 format.  Returns 1 on success.
#--
#proc kadmin_extractv4 { instance name } {
#    global REALMNAME
#    global KADMIN
#    global KEY
#    global spawn_id
#
#    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "xst4 $instance $name"
#    expect_after {
#	"Cannot contact any KDC" {
#	    fail "kadmin xst4 $instance $name lost KDC"
#	    catch "expect_after"
#	    return 0
#	}
#	timeout {
#	    fail "kadmin xst4 $instance $name"
#	    catch "expect_after"
#	    return 0
#	}
#	eof {
#	    fail "kadmin xst4 $instance $name"
#	    catch "expect_after"
#	    return 0
#	}
#    }
#    expect -re "assword\[^\r\n\]*: *" {
#	send "adminpass$KEY\r"
#    }
#    expect "extracted entry $name to key table $instance-new-v4-srvtab"
#    expect_after
#    expect eof
#    set k_stat [wait -i $spawn_id]
#    verbose "wait -i $spawn_id returned $k_stat (kadmin xst4)"
#    catch "close -i $spawn_id"
#    catch "exec rm -f $instance-new-v4-srvtab"
#    pass "kadmin xst4 $instance $name"
#    return 1
#}

#++
# kadmin_delete	- Test delete principal function of kadmin.
#
# Deletes principal $pname.  Returns 1 on success.
#--
proc kadmin_delete { pname } {
    global REALMNAME
    global KADMIN
    global KADMIN_LOCAL
    global KEY
    global spawn_id
    global tmppwd

    set good 0
    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "delprinc -force $pname"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin_delete $pname lost KDC"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin delprinc $pname"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin delprinc $pname"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*: *" {
	send "adminpass$KEY\r"
    }
    expect "Principal \"$pname@$REALMNAME\" deleted." { set good 1 }
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin delprinc)"
    catch "close -i $spawn_id"
    if { $good == 1 } {
	#
	# use kadmin.local to verify that the old principal is not present.
	#
	envstack_push
	setup_kerberos_env kdc
	spawn $KADMIN_LOCAL -r $REALMNAME
	envstack_pop
	expect_after {
	    -i $spawn_id
	    timeout {
		fail "kadmin delprinc $pname"
		catch "expect_after"
		return 0
	    }
	    eof {
		fail "kadmin delprinc $pname"
		catch "expect_after"
		return 0
	    }
	}
	set good 0
	expect "kadmin.local: " { send "getprinc $pname\r" }
	expect "Principal does not exist while retrieving \"$pname@$REALMNAME\"." { set good 1 }
	expect "kadmin.local: " { send "quit\r" }
	expect_after
	expect eof
	set k_stat [wait -i $spawn_id]
	verbose "wait -i $spawn_id returned $k_stat (kadmin.local show)"
	catch "close -i $spawn_id"
	if { $good == 1 } {
	    pass "kadmin delprinc $pname"
	    return 1
	}
	else {
	    fail "kadmin delprinc $pname"
	    return 0
	}
    }
    else {
	fail "kadmin delprinc $pname"
	return 0
    }
}

#++
# kpasswd_cpw	- Test password changing using kpasswd.
#
# Change $princ's password from $opw to $npw.  Returns 1 on success.
#--
proc kpasswd_cpw { princ opw npw } {
    global KPASSWD
    global REALMNAME

    spawn $KPASSWD $princ
    expect_after {
	timeout {
	    fail "kpasswd $princ $npw"
#	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kpasswd $princ $npw"
#	    catch "expect_after"
	    return 0
	}
    }

#    expect "Changing password for $princ."
#    expect "Old password:" { send "$opw\r" }
#    expect "New password:" { send "$npw\r" }
#    expect "New password (again):" { send "$npw\r" }
    expect "Password for $princ@$REALMNAME:" { send "$opw\r" }
    expect "Enter new password:"  { send "$npw\r" }
    expect "Enter it again:"      { send "$npw\r" }
#    expect "Kerberos password changed."
    expect "Password changed."
    expect_after
    expect eof

    if ![check_exit_status "kpasswd"] {
	fail "kpasswd $princ $npw"
	return 0
    }
    pass "kpasswd $princ $npw"
    return 1
}

#++
# kadmin_addpol	- Test add new policy function of kadmin.
#
# Adds policy $pname.  Returns 1 on success.
#--
proc kadmin_addpol { pname } {
    global REALMNAME
    global KADMIN
    global KADMIN_LOCAL
    global KEY
    global spawn_id
    global tmppwd

    set good 0
    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "addpol $pname"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin addpol $pname lost KDC"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin addpol $pname"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin addpol $pname"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*: *" {
	send "adminpass$KEY\r"
    }
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin addpol)"
    catch "close -i $spawn_id"
    #
    # use kadmin.local to verify that a policy was created
    #
    envstack_push
    setup_kerberos_env kdc
    spawn $KADMIN_LOCAL -r $REALMNAME
    envstack_pop
    expect_after {
        -i $spawn_id
        timeout {
	    fail "kadmin addpol $pname"
	    catch "expect_after"
	    return 0
        }
        eof {
	    fail "kadmin addpol $pname"
	    catch "expect_after"
	    return 0
        }
    }
    set good 0
    expect "kadmin.local: " { send "getpol $pname\r" }
    expect "Policy: $pname" { set good 1 }
    expect "Maximum password life:" { verbose "got max pw life" }
    expect "Minimum password life:" { verbose "got min pw life" }
    expect "Minimum password length:" { verbose "got min pw length" }
    expect "Minimum number of password character classes:" {
        verbose "got min pw character classes" }
    expect "Number of old keys kept:" { verbose "got num old keys kept" }
    expect "kadmin.local: " { send "q\r" }

    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin.local showpol)"
    catch "close -i $spawn_id"
    if { $good == 1 } {
        pass "kadmin addpol $pname"
        return 1
    }
    else {
        fail "kadmin addpol $pname"
        return 0
    }
}

#++
# kadmin_delpol	- Test delete policy function of kadmin.
#
# Deletes policy $pname.  Returns 1 on success.
#--
proc kadmin_delpol { pname } {
    global REALMNAME
    global KADMIN
    global KADMIN_LOCAL
    global KEY
    global spawn_id
    global tmppwd

    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "delpol -force $pname"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin_delpol $pname lost KDC"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin delpol $pname"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin delpol $pname"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*: *" {
	send "adminpass$KEY\r"
    }
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin delpol)"
    catch "close -i $spawn_id"
    #
    # use kadmin.local to verify that the old policy is not present.
    #
    envstack_push
    setup_kerberos_env kdc
    spawn $KADMIN_LOCAL -r $REALMNAME
    envstack_pop
    expect_after {
        -i $spawn_id
        timeout {
	    fail "kadmin delpol $pname"
	    catch "expect_after"
	    return 0
        }
        eof {
	    fail "kadmin delpol $pname"
	    catch "expect_after"
	    return 0
        }
    }
    set good 0
    expect "kadmin.local: " { send "getpol $pname\r" }
    expect "Policy does not exist while retrieving policy \"$pname\"." {
	set good 1
    }
    expect "kadmin.local: " { send "quit\r" }
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin.local showpol)"
    catch "close -i $spawn_id"
    if { $good == 1 } {
        pass "kadmin delpol $pname"
        return 1
    }
    else {
        fail "kadmin delpol $pname"
        return 0
    }
}

#++
# kadmin_listpols	- Test list policy database function of kadmin.
#
# Lists the policies.  Returns 1 on success.
#--
proc kadmin_listpols {  } {
    global REALMNAME
    global KADMIN
    global KEY
    global spawn_id

    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "get_policies *"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin lpols lost KDC"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin lpols"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin lpols"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*: *" {
	send "adminpass$KEY\r"
    }
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin listpols)"
    catch "close -i $spawn_id"
    pass "kadmin lpols"
    return 1
}

#++
# kadmin_modpol	- Test modify policy function of kadmin.
#
# Modifies policy $pname with flags $flags.  Returns 1 on success.
#--
proc kadmin_modpol { pname flags } {
    global REALMNAME
    global KADMIN
    global KEY
    global spawn_id

    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "modpol $flags $pname"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin modpol $pname ($flags) lost KDC"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin modpol $pname"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin modpol $pname"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*: *"
    send "adminpass$KEY\r"
    # When in doubt, jam one of these in there.
    expect "\r"
    # Sadly, kadmin doesn't print a confirmation message for policy operations.
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin modpol)"
    catch "close -i $spawn_id"
    pass "kadmin modpol $pname"
    return 1
}

#++
# kadmin_showpol	- Test show policy function of kadmin.
# 
# Retrieves entry for $pname.  Returns 1 on success.
#--
proc kadmin_showpol { pname } {
    global REALMNAME
    global KADMIN
    global KEY
    global spawn_id

    spawn $KADMIN -p krbtest/admin@$REALMNAME -q "get_policy $pname"
    expect_after {
	"Cannot contact any KDC" {
	    fail "kadmin showpol $pname lost KDC"
	    catch "expect_after"
	    return 0
	}
	timeout {
	    fail "kadmin showpol $pname"
	    catch "expect_after"
	    return 0
	}
	eof {
	    fail "kadmin showpol $pname"
	    catch "expect_after"
	    return 0
	}
    }
    expect -re "assword\[^\r\n\]*: *"
    send "adminpass$KEY\r"
    expect -re "\r.*Policy: $pname.*Number of old keys kept: .*\r"
    expect_after
    expect eof
    set k_stat [wait -i $spawn_id]
    verbose "wait -i $spawn_id returned $k_stat (kadmin showpol)"
    catch "close -i $spawn_id"
    pass "kadmin showpol $pname"
    return 1
}

#++
# kdestroy
#--
proc kdestroy { } {
    global KDESTROY

    spawn $KDESTROY -5
    if ![check_exit_status "kdestroy"] {
	return 0
    }
    return 1
}

# Wrap the tests in a procedure, so that we can kill the daemons if
# we get some sort of error.

proc kadmin_test { } {
    global hostname

    # Start up the kerberos and kadmind daemons
    if {![start_kerberos_daemons 0] } {
	return
    }

    # Test basic kadmin functions.
    if {![kadmin_add v5principal/instance1 v5principal] \
	|| ![kadmin_addpol standardpol] \
	|| ![kadmin_showpol standardpol] \
	|| ![kadmin_listpols] \
	|| ![kadmin_modpol standardpol "-minlength 5"] \
	|| ![kadmin_add v4principal/instance2 v4principal] \
	|| ![kadmin_add_rnd v5random] \
	|| ![kadmin_show v5principal/instance1] \
	|| ![kadmin_show v4principal/instance2] \
	|| ![kadmin_show v5random] \
	|| ![kadmin_cpw v5principal/instance1 faroutman] \
	|| ![kadmin_cpw v4principal/instance2 honkydory] \
	|| ![kadmin_cpw_rnd v5random] \
	|| ![kadmin_modify v5random -allow_tix] \
	|| ![kadmin_modify v5random +allow_tix] \
	|| ![kadmin_modify v5random "-policy standardpol"] \
	|| ![kadmin_list] \
	|| ![kadmin_extract instance1 v5principal] \
	|| ![kadmin_delete v5random] \
	|| ![kadmin_delete v4principal/instance2] \
	|| ![kadmin_delete v5principal/instance1] \
	|| ![kadmin_delpol standardpol]} {
	return
    }

# You cannot extract a v4 key...
#	|| ![kadmin_extractv4 instance2 v4principal] \

    # now test kpasswd
    if {![kadmin_add testprinc/instance thisisatest] \
	    || ![kpasswd_cpw testprinc/instance thisisatest anothertest] \
	    || ![kpasswd_cpw testprinc/instance anothertest goredsox] \
	    || ![kadmin_delete testprinc/instance]} {
	return
    }

    # now test that we can kinit with principals/passwords.
    # We defer kdestroying until after kpasswd at least once to test FAST automatic use in kpasswd
    if {![kadmin_add testprinc1/instance thisisatest] \
	    || ![kinit testprinc1/instance thisisatest 0] \
	    || ![kpasswd_cpw testprinc1/instance thisisatest anothertest] \
	    || ![kdestroy] \
	    || ![kinit testprinc1/instance anothertest 0] \
	    || ![kdestroy] \
	    || ![kpasswd_cpw testprinc1/instance anothertest goredsox] \
	    || ![kinit testprinc1/instance goredsox 0] \
	    || ![kdestroy] \
	    || ![kadmin_cpw testprinc1/instance betterwork] \
	    || ![kinit testprinc1/instance betterwork 0] \
	    || ![kdestroy] \
	    || ![kadmin_delete testprinc1/instance]} {
	return
    }

    # now test modify changes.
    if {![kadmin_add testuser longtestpw] \
	    || ![kinit testuser longtestpw 0] \
	    || ![kdestroy] \
	    || ![kadmin_modify testuser "-maxlife \"2500 seconds\""] \
	    || ![kinit testuser longtestpw 0] \
	    || ![kdestroy] \
	    || ![kadmin_delete testuser]} {
	return
    }

    # now test that reducing the history number doesn't make kadmind vulnerable.
    if {![kadmin_addpol crashpol] \
	    || ![kadmin_modpol crashpol "-history 5"] \
	    || ![kadmin_add crash first] \
	    || ![kadmin_modify crash "-policy crashpol"] \
	    || ![kadmin_cpw crash second] \
	    || ![kadmin_cpw crash third] \
	    || ![kadmin_cpw crash fourth] \
	    || ![kadmin_modpol crashpol "-history 3"] \
	    || ![kadmin_cpw crash fifth] \
	    || ![kadmin_delete crash] \
	    || ![kadmin_delpol crashpol]} {
	return
    }

    # test retrieval of large number of principals
    # bug [2877]
    for { set i 0 } { $i < 200 } { incr i } {
	if { ![kadmin_add "foo$i" foopass] } {
	    return
	}
    }

    if { ![kadmin_list] } {
	return
    }

    # test fallback to kadmin/admin
    if {![kadmin_delete kadmin/$hostname] \
	    || ![kadmin_list] \
	    || ![kadmin_add_rnd kadmin/$hostname -allow_tgs_req] \
	    || ![kadmin_list]} {
	return
    }

    verbose "kadmin_test succeeded"
}

run_once kadmin {
    # Set up the kerberos database.
    if {![get_hostname] \
	    || ![setup_kerberos_files] \
	    || ![setup_kerberos_env] \
	    || ![setup_kerberos_db 0]} {
	return
    }

    # Run the test.
    set status [catch kadmin_test msg]

    # Shut down the kerberos daemons and the rsh daemon.
    stop_kerberos_daemons

    if { $status != 0 } {
	send_error "ERROR: error in kadmin.exp\n"
	send_error "$msg\n"
	exit 1
    }
}