4de8be6aa4cfa6c4ac94fbf5fa6618d4d2f04987
[openafs.git] / src / libadmin / test / kas.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * This file implements the kas related funtions for afscp
12  */
13
14 #include <afsconfig.h>
15 #include <afs/param.h>
16
17 RCSID
18     ("$Header$");
19
20 #include "kas.h"
21 #include <time.h>
22
23 /*
24  * Utility functions
25  */
26
27 /*
28  * Generic fuction for converting input string to an integer.  Pass
29  * the error_msg you want displayed if there is an error converting
30  * the string.
31  */
32
33 static int
34 GetIntFromString(const char *int_str, const char *error_msg)
35 {
36     int i;
37     char *bad_char = NULL;
38
39     i = strtoul(int_str, &bad_char, 10);
40     if ((bad_char == NULL) || (*bad_char == 0)) {
41         return i;
42     }
43
44     ERR_EXT(error_msg);
45 }
46
47 int
48 DoKasPrincipalCreate(struct cmd_syndesc *as, char *arock)
49 {
50     typedef enum { PRINCIPAL, INSTANCE,
51         PASSWORD
52     } DoKasPrincipalCreate_parm_t;
53     afs_status_t st = 0;
54     const char *instance = NULL;
55     kas_identity_t user;
56     const char *password;
57
58     strcpy(user.principal, as->parms[PRINCIPAL].items->data);
59
60     if (as->parms[INSTANCE].items) {
61         strcpy(user.instance, as->parms[INSTANCE].items->data);
62     } else {
63         user.instance[0] = 0;
64     }
65
66     password = as->parms[PASSWORD].items->data;
67
68     if (!kas_PrincipalCreate(cellHandle, 0, &user, password, &st)) {
69         ERR_ST_EXT("kas_PrincipalCreate", st);
70     }
71
72     return 0;
73 }
74
75 int
76 DoKasPrincipalDelete(struct cmd_syndesc *as, char *arock)
77 {
78     typedef enum { PRINCIPAL, INSTANCE } DoKasPrincipalGet_parm_t;
79     afs_status_t st = 0;
80     const char *instance = NULL;
81     kas_identity_t user;
82
83     strcpy(user.principal, as->parms[PRINCIPAL].items->data);
84
85     if (as->parms[INSTANCE].items) {
86         strcpy(user.instance, as->parms[PRINCIPAL].items->data);
87     } else {
88         user.instance[0] = 0;
89     }
90
91     if (!kas_PrincipalDelete(cellHandle, 0, &user, &st)) {
92         ERR_ST_EXT("kas_PrincipalDelete", st);
93     }
94
95     return 0;
96 }
97
98 static void
99 Print_kas_principalEntry_p(kas_principalEntry_p principal, const char *prefix)
100 {
101     int i;
102
103     if (principal->adminSetting == KAS_ADMIN) {
104         printf("%sAdmin setting: KAS_ADMIN\n", prefix);
105     } else {
106         printf("%sAdmin setting: NO_KAS_ADMIN\n", prefix);
107     }
108
109     if (principal->tgsSetting == TGS) {
110         printf("%sTGS setting: TGS\n", prefix);
111     } else {
112         printf("%sTGS setting: NO_TGS\n", prefix);
113     }
114
115     if (principal->encSetting == ENCRYPT) {
116         printf("%sEncrypt setting: ENCRYPT\n", prefix);
117     } else {
118         printf("%sEncrypt setting: NO_ENCRYPT\n", prefix);
119     }
120
121     if (principal->cpwSetting == CHANGE_PASSWORD) {
122         printf("%sChange password setting: CHANGE_PASSWORD\n", prefix);
123     } else {
124         printf("%sChange password setting: NO_CHANGE_PASSWORD\n", prefix);
125     }
126
127     if (principal->rpwSetting == REUSE_PASSWORD) {
128         printf("%sReuse password setting: REUSE_PASSWORD\n", prefix);
129     } else {
130         printf("%sReuse password setting: NO_REUSE_PASSWORD\n", prefix);
131     }
132
133     printf("%sExpiration: %u\n", prefix, principal->userExpiration);
134     printf("%sLast modification time %u\n", prefix, principal->lastModTime);
135     printf("%sLast modifying principal %s", prefix,
136            principal->lastModPrincipal.principal);
137     if (principal->lastModPrincipal.instance[0] != 0) {
138         printf(".%s\n", principal->lastModPrincipal.instance);
139     } else {
140         printf("\n");
141     }
142
143     printf("%sLast change password time %u\n", prefix,
144            principal->lastChangePasswordTime);
145     printf("%sMax ticket lifetime %d\n", prefix,
146            principal->maxTicketLifetime);
147     printf("%sKey version number %d\n", prefix, principal->keyVersion);
148
149     printf("%sKey contents :", prefix);
150     for (i = 0; i < KAS_ENCRYPTION_KEY_LEN; i++) {
151         printf("%d ", principal->key.key[i]);
152     }
153     printf("\n");
154
155     printf("%sKey checksum %u\n", prefix, principal->keyCheckSum);
156     printf("%sDays to password expire %d\n", prefix,
157            principal->daysToPasswordExpire);
158     printf("%sFailed login count %d\n", prefix, principal->failLoginCount);
159     printf("%sLock time %d\n", prefix, principal->lockTime);
160 }
161
162 int
163 DoKasPrincipalGet(struct cmd_syndesc *as, char *arock)
164 {
165     typedef enum { PRINCIPAL, INSTANCE } DoKasPrincipalGet_parm_t;
166     afs_status_t st = 0;
167     const char *instance = NULL;
168     kas_identity_t user;
169     kas_principalEntry_t principal;
170
171     strcpy(user.principal, as->parms[PRINCIPAL].items->data);
172
173     if (as->parms[INSTANCE].items) {
174         strcpy(user.instance, as->parms[PRINCIPAL].items->data);
175     } else {
176         user.instance[0] = 0;
177     }
178
179     if (!kas_PrincipalGet(cellHandle, 0, &user, &principal, &st)) {
180         ERR_ST_EXT("kas_PrincipalGet", st);
181     }
182
183     Print_kas_principalEntry_p(&principal, "");
184
185     return 0;
186 }
187
188 int
189 DoKasPrincipalList(struct cmd_syndesc *as, char *arock)
190 {
191     afs_status_t st = 0;
192     void *iter;
193     kas_identity_t prin;
194
195     if (!kas_PrincipalGetBegin(cellHandle, 0, &iter, &st)) {
196         ERR_ST_EXT("kas_PrincipalGetBegin", st);
197     }
198
199     printf("Listing principals:\n");
200     while (kas_PrincipalGetNext(iter, &prin, &st)) {
201         printf("%s", prin.principal);
202         if (prin.instance[0] != 0) {
203             printf(".%s\n", prin.instance);
204         } else {
205             printf("\n");
206         }
207     }
208
209     if (st != ADMITERATORDONE) {
210         ERR_ST_EXT("kas_PrincipalGetNext", st);
211     }
212
213     if (!kas_PrincipalGetDone(iter, &st)) {
214         ERR_ST_EXT("kas_PrincipalGetDone", st);
215     }
216
217     return 0;
218 }
219
220 int
221 DoKasPrincipalKeySet(struct cmd_syndesc *as, char *arock)
222 {
223     typedef enum { PRINCIPAL, INSTANCE, PASSWORD,
224         KEYVERSION
225     } DoKasPrincipalKeySet_parm_t;
226     afs_status_t st = 0;
227     kas_encryptionKey_t key;
228     kas_identity_t user;
229     int key_version;
230     const char *cell;
231     const char *password;
232
233     strcpy(user.principal, as->parms[PRINCIPAL].items->data);
234
235     if (as->parms[INSTANCE].items) {
236         strcpy(user.instance, as->parms[INSTANCE].items->data);
237     } else {
238         user.instance[0] = 0;
239     }
240
241     if (!afsclient_CellNameGet(cellHandle, &cell, &st)) {
242         ERR_ST_EXT("afsclient_CellNameGet", st);
243     }
244
245     password = as->parms[PASSWORD].items->data;
246     key_version =
247         GetIntFromString(as->parms[KEYVERSION].items->data,
248                          "invalid key version number");
249     if (!kas_StringToKey(cell, password, &key, &st)) {
250         ERR_ST_EXT("kas_StringToKey", st);
251     }
252
253     if (!kas_PrincipalKeySet(cellHandle, 0, &user, key_version, &key, &st)) {
254         ERR_ST_EXT("kas_PrincipalKeySet", st);
255     }
256
257     return 0;
258 }
259
260 int
261 DoKasPrincipalLockStatusGet(struct cmd_syndesc *as, char *arock)
262 {
263     typedef enum { PRINCIPAL, INSTANCE } DoKasPrincipalLockStatusGet_parm_t;
264     afs_status_t st = 0;
265     kas_identity_t user;
266     unsigned int lock_end_time = 0;
267
268     strcpy(user.principal, as->parms[PRINCIPAL].items->data);
269
270     if (as->parms[INSTANCE].items) {
271         strcpy(user.instance, as->parms[INSTANCE].items->data);
272     } else {
273         user.instance[0] = 0;
274     }
275
276     if (!kas_PrincipalLockStatusGet
277         (cellHandle, 0, &user, &lock_end_time, &st)) {
278         ERR_ST_EXT("kas_PrincipalLockStatusGet", st);
279     }
280
281     printf("The lock end time is %u\n", lock_end_time);
282
283     return 0;
284 }
285
286 int
287 DoKasPrincipalUnlock(struct cmd_syndesc *as, char *arock)
288 {
289     typedef enum { PRINCIPAL, INSTANCE } DoKasPrincipalUnlock_parm_t;
290     afs_status_t st = 0;
291     kas_identity_t user;
292     unsigned int lock_end_time = 0;
293
294     strcpy(user.principal, as->parms[PRINCIPAL].items->data);
295
296     if (as->parms[INSTANCE].items) {
297         strcpy(user.instance, as->parms[INSTANCE].items->data);
298     } else {
299         user.instance[0] = 0;
300     }
301
302     if (!kas_PrincipalUnlock(cellHandle, 0, &user, &st)) {
303         ERR_ST_EXT("kas_PrincipalUnlock", st);
304     }
305
306     return 0;
307 }
308
309 int
310 DoKasPrincipalFieldsSet(struct cmd_syndesc *as, char *arock)
311 {
312     typedef enum { PRINCIPAL, INSTANCE, ADMIN, NOADMIN, GRANTTICKET,
313         NOGRANTTICKET, ENCRYPT2, NOENCRYPT, CHANGEPASSWORD,
314         NOCHANGEPASSWORD, REUSEPASSWORD, NOREUSEPASSWORD,
315         EXPIRES, MAXTICKETLIFETIME, PASSWORDEXPIRES,
316         FAILEDPASSWORDATTEMPTS, FAILEDPASSWORDLOCKTIME
317     } DoKasPrincipalFieldsSet_parm_t;
318     afs_status_t st = 0;
319     kas_identity_t user;
320     kas_admin_t admin;
321     kas_admin_p admin_ptr = NULL;
322     int have_admin = 0;
323     kas_tgs_t tgs;
324     kas_tgs_p tgs_ptr = NULL;
325     int have_tgs = 0;
326     kas_enc_t enc;
327     kas_enc_p enc_ptr = NULL;
328     int have_enc = 0;
329     kas_cpw_t cpw;
330     kas_cpw_p cpw_ptr = NULL;
331     int have_cpw = 0;
332     kas_rpw_t reuse;
333     kas_rpw_p reuse_ptr = NULL;
334     int have_reuse = 0;
335     unsigned int expire;
336     unsigned int *expire_ptr = NULL;
337     int have_expire = 0;
338     unsigned int max_ticket;
339     unsigned int *max_ticket_ptr = NULL;
340     int have_max_ticket = 0;
341     unsigned int password_expire;
342     unsigned int *password_expire_ptr = NULL;
343     int have_password_expire = 0;
344     unsigned int failed_password_attempts;
345     unsigned int *failed_password_attempts_ptr = NULL;
346     int have_failed_password_attempts = 0;
347     unsigned int failed_password_lock_time;
348     unsigned int *failed_password_lock_time_ptr = NULL;
349     int have_failed_password_lock_time = 0;
350
351     strcpy(user.principal, as->parms[PRINCIPAL].items->data);
352
353     if (as->parms[INSTANCE].items) {
354         strcpy(user.instance, as->parms[INSTANCE].items->data);
355     } else {
356         user.instance[0] = 0;
357     }
358
359     if (as->parms[ADMIN].items) {
360         admin = KAS_ADMIN;
361         admin_ptr = &admin;
362         have_admin = 1;
363     }
364
365     if (as->parms[NOADMIN].items) {
366         admin = NO_KAS_ADMIN;
367         admin_ptr = &admin;
368         if (have_admin) {
369             ERR_EXT("specify either admin or noadmin, not both");
370         }
371         have_admin = 1;
372     }
373
374     if (as->parms[GRANTTICKET].items) {
375         tgs = TGS;
376         tgs_ptr = &tgs;
377         have_tgs = 1;
378     }
379
380     if (as->parms[NOGRANTTICKET].items) {
381         tgs = NO_TGS;
382         tgs_ptr = &tgs;
383         if (have_tgs) {
384             ERR_EXT("specify either grantticket or nograntticket, not both");
385         }
386         have_tgs = 1;
387     }
388
389     if (as->parms[ENCRYPT2].items) {
390         enc = ENCRYPT;
391         enc_ptr = &enc;
392         have_enc = 1;
393     }
394
395     if (as->parms[NOENCRYPT].items) {
396         enc = NO_ENCRYPT;
397         enc_ptr = &enc;
398         if (have_tgs) {
399             ERR_EXT("specify either encrypt or noencrypt, not both");
400         }
401         have_enc = 1;
402     }
403
404     if (as->parms[CHANGEPASSWORD].items) {
405         cpw = CHANGE_PASSWORD;
406         cpw_ptr = &cpw;
407         have_cpw = 1;
408     }
409
410     if (as->parms[NOCHANGEPASSWORD].items) {
411         cpw = NO_CHANGE_PASSWORD;
412         cpw_ptr = &cpw;
413         if (have_tgs) {
414             ERR_EXT("specify either changepassword or "
415                     "nochangepassword, not both");
416         }
417         have_cpw = 1;
418     }
419
420     if (as->parms[REUSEPASSWORD].items) {
421         reuse = REUSE_PASSWORD;
422         reuse_ptr = &reuse;
423         have_reuse = 1;
424     }
425
426     if (as->parms[REUSEPASSWORD].items) {
427         reuse = NO_REUSE_PASSWORD;
428         reuse_ptr = &reuse;
429         if (have_reuse) {
430             ERR_EXT("specify either reusepassword or "
431                     "noreusepassword, not both");
432         }
433         have_reuse = 1;
434     }
435
436     if (as->parms[EXPIRES].items) {
437         expire =
438             GetIntFromString(as->parms[EXPIRES].items->data,
439                              "bad expiration date");
440         expire_ptr = &expire;
441         have_expire = 1;
442     }
443
444     if (as->parms[MAXTICKETLIFETIME].items) {
445         max_ticket =
446             GetIntFromString(as->parms[MAXTICKETLIFETIME].items->data,
447                              "bad max ticket lifetime");
448         max_ticket_ptr = &max_ticket;
449         have_max_ticket = 1;
450     }
451
452     if (as->parms[PASSWORDEXPIRES].items) {
453         password_expire =
454             GetIntFromString(as->parms[PASSWORDEXPIRES].items->data,
455                              "bad expiration date");
456         password_expire_ptr = &password_expire;
457         have_password_expire = 1;
458     }
459
460     if (as->parms[FAILEDPASSWORDATTEMPTS].items) {
461         failed_password_attempts =
462             GetIntFromString(as->parms[FAILEDPASSWORDATTEMPTS].items->data,
463                              "bad expiration date");
464         failed_password_attempts_ptr = &failed_password_attempts;
465         have_failed_password_attempts = 1;
466     }
467
468     if (as->parms[FAILEDPASSWORDLOCKTIME].items) {
469         failed_password_lock_time =
470             GetIntFromString(as->parms[FAILEDPASSWORDLOCKTIME].items->data,
471                              "bad expiration date");
472         failed_password_lock_time_ptr = &failed_password_lock_time;
473         have_failed_password_lock_time = 1;
474     }
475
476     if ((have_admin + have_tgs + have_enc + have_cpw + have_reuse +
477          have_expire + have_max_ticket + have_password_expire +
478          have_failed_password_attempts + have_failed_password_lock_time) ==
479         0) {
480         ERR_EXT("You must specify at least one attribute to change");
481     }
482
483     if (!kas_PrincipalFieldsSet
484         (cellHandle, 0, &user, admin_ptr, tgs_ptr, enc_ptr, cpw_ptr,
485          expire_ptr, max_ticket_ptr, password_expire_ptr, reuse_ptr,
486          failed_password_attempts_ptr, failed_password_lock_time_ptr, &st)) {
487         ERR_ST_EXT("kas_PrincipalFieldsSet", st);
488     }
489
490     return 0;
491 }
492
493 static void
494 Print_kas_serverStats_p(kas_serverStats_p stats, const char *prefix)
495 {
496     time_t stime = stats->serverStartTime;
497
498     printf("%sAllocations %d\n", prefix, stats->allocations);
499     printf("%sFrees %d\n", prefix, stats->frees);
500     printf("%sChange password requests %d\n", prefix,
501            stats->changePasswordRequests);
502     printf("%sAdmin accounts %d\n", prefix, stats->adminAccounts);
503     printf("%sHost %x\n", prefix, stats->host);
504     printf("%sServer start time %s\n", prefix, ctime(&stime));
505     printf("%sUser time %ld secs %ld usec\n", prefix, stats->userTime.tv_sec,
506            stats->userTime.tv_usec);
507     printf("%sSystem time %ld secs %ld usec\n", prefix,
508            stats->systemTime.tv_sec, stats->systemTime.tv_usec);
509     printf("%sData size %d\n", prefix, stats->dataSize);
510     printf("%sStack size %d\n", prefix, stats->stackSize);
511     printf("%sPage faults %d\n", prefix, stats->pageFaults);
512     printf("%sHash table utilization %d\n", prefix,
513            stats->hashTableUtilization);
514     printf("%sAuthentication requests %d aborts %d\n", prefix,
515            stats->authenticate.requests, stats->authenticate.aborts);
516     printf("%sChange password requests %d aborts %d\n", prefix,
517            stats->changePassword.requests, stats->changePassword.aborts);
518     printf("%sGet ticket requests %d aborts %d\n", prefix,
519            stats->getTicket.requests, stats->getTicket.aborts);
520     printf("%sCreate user requests %d aborts %d\n", prefix,
521            stats->createUser.requests, stats->createUser.aborts);
522     printf("%sSet password requests %d aborts %d\n", prefix,
523            stats->setPassword.requests, stats->setPassword.aborts);
524     printf("%sSet fields requests %d aborts %d\n", prefix,
525            stats->setFields.requests, stats->setFields.aborts);
526     printf("%sDelete user requests %d aborts %d\n", prefix,
527            stats->deleteUser.requests, stats->deleteUser.aborts);
528     printf("%sGet entry requests %d aborts %d\n", prefix,
529            stats->getEntry.requests, stats->getEntry.aborts);
530     printf("%sList entry requests %d aborts %d\n", prefix,
531            stats->listEntry.requests, stats->listEntry.aborts);
532     printf("%sGet stats requests %d aborts %d\n", prefix,
533            stats->getStats.requests, stats->getStats.aborts);
534     printf("%sGet password requests %d aborts %d\n", prefix,
535            stats->getPassword.requests, stats->getPassword.aborts);
536     printf("%sGet random key requests %d aborts %d\n", prefix,
537            stats->getRandomKey.requests, stats->getRandomKey.aborts);
538     printf("%sDebug requests %d aborts %d\n", prefix, stats->debug.requests,
539            stats->debug.aborts);
540     printf("%sUDP authenticate requests %d aborts %d\n", prefix,
541            stats->udpAuthenticate.requests, stats->udpAuthenticate.aborts);
542     printf("%sUDP get ticket requests %d aborts %d\n", prefix,
543            stats->udpGetTicket.requests, stats->udpGetTicket.aborts);
544     printf("%sUnlock requests %d aborts %d\n", prefix, stats->unlock.requests,
545            stats->unlock.aborts);
546     printf("%sLock status requests %d aborts %d\n", prefix,
547            stats->lockStatus.requests, stats->lockStatus.aborts);
548     printf("%sString checks %d\n", prefix, stats->stringChecks);
549 }
550
551 int
552 DoKasServerStatsGet(struct cmd_syndesc *as, char *arock)
553 {
554     typedef enum { SERVER } DoKasServerStatsGet_parm_t;
555     afs_status_t st = 0;
556     const char *server_list[2] = { 0, 0 };
557     void *kas_server = NULL;
558     kas_serverStats_t stats;
559
560     if (as->parms[SERVER].items) {
561         server_list[0] = as->parms[SERVER].items->data;
562     }
563
564     if (!kas_ServerOpen(cellHandle, server_list, &kas_server, &st)) {
565         ERR_ST_EXT("kas_ServerOpen", st);
566     }
567
568     if (!kas_ServerStatsGet(0, kas_server, &stats, &st)) {
569         ERR_ST_EXT("kas_ServerStatsGet", st);
570     }
571
572     Print_kas_serverStats_p(&stats, "");
573
574     kas_ServerClose(kas_server, 0);
575
576     return 0;
577 }
578
579 static void
580 Print_kas_serverDebugInfo_p(kas_serverDebugInfo_p debug, const char *prefix)
581 {
582     time_t time;
583     int i;
584
585     printf("%sHost %x\n", prefix, debug->host);
586     time = debug->serverStartTime;
587     printf("%sServer start time %s\n", prefix, ctime(&time));
588     time = debug->currentTime;
589     printf("%sCurrent time %s\n", prefix, ctime(&time));
590     printf("%sNo auth %d\n", prefix, debug->noAuth);
591     time = debug->lastTransaction;
592     printf("%sLast transaction %s\n", prefix, ctime(&time));
593     printf("%sLast operation %s\n", prefix, debug->lastOperation);
594     printf("%sLast principal auth %s\n", prefix, debug->lastPrincipalAuth);
595     printf("%sLast principal UDP auth %s\n", prefix,
596            debug->lastPrincipalUDPAuth);
597     printf("%sLast principal TGS auth %s\n", prefix, debug->lastPrincipalTGS);
598     printf("%sLast principal UDP TGS auth %s\n", prefix,
599            debug->lastPrincipalUDPTGS);
600     printf("%sLast principal admin %s\n", prefix, debug->lastPrincipalAdmin);
601     printf("%sLast server TGS %s\n", prefix, debug->lastServerTGS);
602     printf("%sLast server UDP TGS %s\n", prefix, debug->lastServerUDPTGS);
603     time = debug->nextAutoCheckPointWrite;
604     printf("%sNext auto check point write %s\n", prefix, ctime(&time));
605     printf("%sUpdates remaining before ACPW %d\n", prefix,
606            debug->updatesRemainingBeforeAutoCheckPointWrite);
607     time = debug->dbHeaderRead;
608     printf("%sDatabase header read %s\n", prefix, ctime(&time));
609     printf("%sDatabase version %d\n", prefix, debug->dbVersion);
610     printf("%sDatabase free ptr %d\n", prefix, debug->dbFreePtr);
611     printf("%sDatabase EOF ptr %d\n", prefix, debug->dbEOFPtr);
612     printf("%sDatabase kvno ptr %d\n", prefix, debug->dbKvnoPtr);
613     printf("%sDatabase special keys version%d\n", prefix,
614            debug->dbSpecialKeysVersion);
615     printf("%sDatabase header lock %d\n", prefix, debug->dbHeaderLock);
616     printf("%sKey cache lock %d\n", prefix, debug->keyCacheLock);
617     printf("%sKey cache version %d\n", prefix, debug->keyCacheVersion);
618     printf("%sKey cache size %d\n", prefix, debug->keyCacheSize);
619     printf("%sKey cache used %d\n", prefix, debug->keyCacheUsed);
620
621     printf("%sKey cache\n", prefix);
622
623     for (i = 0; i < debug->keyCacheUsed; i++) {
624         printf("%s\tPrincipal %s\n", prefix, debug->keyCache[i].principal);
625         time = debug->keyCache[i].lastUsed;
626         printf("%s\tLast used %s\n", prefix, ctime(&time));
627         printf("%s\tVersion number %d\n", prefix,
628                debug->keyCache[i].keyVersionNumber);
629         printf("%s\tPrimary %d\n", prefix, debug->keyCache[i].primary);
630         printf("%s\tCheck sum %d\n", prefix, debug->keyCache[i].keyCheckSum);
631         printf("\n");
632     }
633
634 }
635
636 int
637 DoKasServerDebugGet(struct cmd_syndesc *as, char *arock)
638 {
639     typedef enum { SERVER } DoKasServerDebugGet_parm_t;
640     afs_status_t st = 0;
641     const char *server_list[2] = { 0, 0 };
642     void *kas_server = NULL;
643     kas_serverDebugInfo_t debug;
644
645     if (as->parms[SERVER].items) {
646         server_list[0] = as->parms[SERVER].items->data;
647     }
648
649     if (!kas_ServerOpen(cellHandle, server_list, &kas_server, &st)) {
650         ERR_ST_EXT("kas_ServerOpen", st);
651     }
652
653     if (!kas_ServerDebugGet(0, kas_server, &debug, &st)) {
654         ERR_ST_EXT("kas_ServerDebugGet", st);
655     }
656
657     Print_kas_serverDebugInfo_p(&debug, "");
658
659     kas_ServerClose(kas_server, 0);
660
661     return 0;
662 }
663
664 int
665 DoKasServerRandomKeyGet(struct cmd_syndesc *as, char *arock)
666 {
667     afs_status_t st = 0;
668     kas_encryptionKey_t key;
669     int i;
670
671     if (!kas_ServerRandomKeyGet(cellHandle, 0, &key, &st)) {
672         ERR_ST_EXT("kas_ServerRandomKeyGet", st);
673     }
674
675     printf("Key: ");
676     for (i = 0; i < KAS_ENCRYPTION_KEY_LEN; i++) {
677         printf("%d ", key.key[i]);
678     }
679     printf("\n");
680
681     return 0;
682 }
683
684 void
685 SetupKasAdminCmd(void)
686 {
687     struct cmd_syndesc *ts;
688
689     ts = cmd_CreateSyntax("KasPrincipalCreate", DoKasPrincipalCreate, 0,
690                           "create a new principal");
691     cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_REQUIRED,
692                 "principal to create");
693     cmd_AddParm(ts, "-instance", CMD_SINGLE, CMD_OPTIONAL,
694                 "principal instance");
695     cmd_AddParm(ts, "-password", CMD_SINGLE, CMD_REQUIRED,
696                 "initial principal password");
697     SetupCommonCmdArgs(ts);
698
699     ts = cmd_CreateSyntax("KasPrincipalDelete", DoKasPrincipalDelete, 0,
700                           "delete a principal");
701     cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_REQUIRED,
702                 "principal to delete");
703     cmd_AddParm(ts, "-instance", CMD_SINGLE, CMD_OPTIONAL,
704                 "principal instance");
705     SetupCommonCmdArgs(ts);
706
707     ts = cmd_CreateSyntax("KasPrincipalGet", DoKasPrincipalGet, 0,
708                           "get information about a principal");
709     cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_REQUIRED,
710                 "principal to get");
711     cmd_AddParm(ts, "-instance", CMD_SINGLE, CMD_OPTIONAL,
712                 "principal instance");
713     SetupCommonCmdArgs(ts);
714
715     ts = cmd_CreateSyntax("KasPrincipalList", DoKasPrincipalList, 0,
716                           "list all principals");
717     SetupCommonCmdArgs(ts);
718
719     ts = cmd_CreateSyntax("KasPrincipalKeySet", DoKasPrincipalKeySet, 0,
720                           "set the password for a principal");
721     cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_REQUIRED,
722                 "principal to modify");
723     cmd_AddParm(ts, "-instance", CMD_SINGLE, CMD_OPTIONAL,
724                 "principal instance");
725     cmd_AddParm(ts, "-password", CMD_SINGLE, CMD_REQUIRED,
726                 "new principal password");
727     cmd_AddParm(ts, "-version", CMD_SINGLE, CMD_REQUIRED,
728                 "password version number");
729     SetupCommonCmdArgs(ts);
730
731     ts = cmd_CreateSyntax("KasPrincipalLockStatusGet",
732                           DoKasPrincipalLockStatusGet, 0,
733                           "get the lock status of a principal");
734     cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_REQUIRED,
735                 "principal to query");
736     cmd_AddParm(ts, "-instance", CMD_SINGLE, CMD_OPTIONAL,
737                 "principal instance");
738     SetupCommonCmdArgs(ts);
739
740     ts = cmd_CreateSyntax("KasPrincipalUnlock", DoKasPrincipalUnlock, 0,
741                           "unlock a principal");
742     cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_REQUIRED,
743                 "principal to unlock");
744     cmd_AddParm(ts, "-instance", CMD_SINGLE, CMD_OPTIONAL,
745                 "principal instance");
746     SetupCommonCmdArgs(ts);
747
748     ts = cmd_CreateSyntax("KasPrincipalFieldsSet", DoKasPrincipalFieldsSet, 0,
749                           "modify a principal");
750     cmd_AddParm(ts, "-principal", CMD_SINGLE, CMD_REQUIRED,
751                 "principal to modify");
752     cmd_AddParm(ts, "-instance", CMD_SINGLE, CMD_OPTIONAL,
753                 "principal instance");
754     cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL,
755                 "make this principal an admin");
756     cmd_AddParm(ts, "-noadmin", CMD_FLAG, CMD_OPTIONAL,
757                 "remove admin from this principal");
758     cmd_AddParm(ts, "-grantticket", CMD_FLAG, CMD_OPTIONAL,
759                 "this principal can grant server tickets");
760     cmd_AddParm(ts, "-nograntticket", CMD_FLAG, CMD_OPTIONAL,
761                 "this principal cannot grant server tickets");
762     cmd_AddParm(ts, "-encrypt", CMD_FLAG, CMD_OPTIONAL,
763                 "this principal can encrypt data");
764     cmd_AddParm(ts, "-noencrypt", CMD_FLAG, CMD_OPTIONAL,
765                 "this principal cannot encrypt data");
766     cmd_AddParm(ts, "-changepassword", CMD_FLAG, CMD_OPTIONAL,
767                 "this principal can change its password");
768     cmd_AddParm(ts, "-nochangepassword", CMD_FLAG, CMD_OPTIONAL,
769                 "this principal cannot change its password");
770     cmd_AddParm(ts, "-reusepassword", CMD_FLAG, CMD_OPTIONAL,
771                 "this principal can reuse its password");
772     cmd_AddParm(ts, "-noreusepassword", CMD_FLAG, CMD_OPTIONAL,
773                 "this principal cannot reuse its password");
774     cmd_AddParm(ts, "-expires", CMD_SINGLE, CMD_OPTIONAL,
775                 "the time at which this principal expires");
776     cmd_AddParm(ts, "-maxticketlifetime", CMD_SINGLE, CMD_OPTIONAL,
777                 "the maximum ticket lifetime this principal can request");
778     cmd_AddParm(ts, "-passwordexpires", CMD_SINGLE, CMD_OPTIONAL,
779                 "the time at which this principal's password expires");
780     cmd_AddParm(ts, "-failedpasswordattempts", CMD_SINGLE, CMD_OPTIONAL,
781                 "the number of failed password attempts this principal "
782                 "can incur before it is locked");
783     cmd_AddParm(ts, "-failedpasswordlocktime", CMD_SINGLE, CMD_OPTIONAL,
784                 "the amount of time this principal will be locked if the "
785                 "maximum failed password attempts is exceeded");
786     SetupCommonCmdArgs(ts);
787
788     ts = cmd_CreateSyntax("KasServerStatsGet", DoKasServerStatsGet, 0,
789                           "get stats on a kaserver");
790     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED, "server to query");
791     SetupCommonCmdArgs(ts);
792
793     ts = cmd_CreateSyntax("KasServerDebugGet", DoKasServerDebugGet, 0,
794                           "get debug info from a kaserver");
795     cmd_AddParm(ts, "-server", CMD_SINGLE, CMD_REQUIRED, "server to query");
796     SetupCommonCmdArgs(ts);
797
798     ts = cmd_CreateSyntax("KasServerRandomKeyGet", DoKasServerRandomKeyGet, 0,
799                           "create a random key");
800     SetupCommonCmdArgs(ts);
801
802 }