2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afs/param.h>
14 #include <sys/types.h>
17 #include <afs/afsutil.h>
19 #include <sys/resource.h>
32 #include <netinet/in.h>
36 #include <afs/cellconfig.h>
42 #include "afs/audit.h"
44 #include "../permit_xprt.h"
47 extern struct ubik_dbase *KA_dbase;
48 struct kaheader cheader;
49 Date cheaderReadTime; /* time cheader last read in */
50 extern struct afsconf_dir *KA_conf; /* for getting cell info */
52 afs_int32 kamCreateUser(), ChangePassWord(), kamSetPassword(), kamSetFields(), kamDeleteUser();
53 afs_int32 kamGetEntry(), kamListEntry(), kamGetStats(), kamGetPassword(), kamGetRandomKey(), kamDebug();
54 char lrealm[MAXKTCREALMLEN];
56 #ifndef EXPIREPW /* password expiration default yes */
60 #ifndef AUTOCPWINTERVAL
61 #define AUTOCPWINTERVAL (24*3600)
63 #ifndef AUTOCPWUPDATES
64 #define AUTOCPWUPDATES 128
69 static afs_int32 autoCPWInterval;
70 static afs_int32 autoCPWUpdates;
72 static afs_int32 set_password (); /* forward */
73 extern afs_int32 InitAuthServ (); /* forward */
74 static afs_int32 impose_reuse_limits(); /* forward */
75 static int create_user(); /* forward */
77 /* This routine is called whenever an RPC interface needs the time. It uses
78 the current time to randomize a 128 bit value that is used to change the
79 AuthServer Admin and TGS keys automatically. */
81 static Date nextAutoCPWTime = 0;
82 static afs_int32 totalUpdates = 0;
84 /* This routine is ostensibly to get the current time, but basically its job is
85 to periodically update a random number. It also periodically updates the
86 keys for the builtin servers. This is why it needs a transaction pointer
87 and returns an error code. If the caller is in a read transaction, the tt
88 ptr should be zero and the return code need not be checked. */
90 static afs_int32 get_time (timeP, tt, admin)
92 struct ubik_trans *tt; /* tt != 0: a write transaction */
93 int admin; /* the caller is an admin user */
95 /* random value used to change Admin & TGS keys, this is at risk during
96 multi-threaded operation, but I think the consequences are fairly
98 static afs_uint32 random_value[4];
101 unsigned int bit, nbit;
105 gettimeofday (&time, 0);
106 bit = (random_value[3] >> 31) & 1; /* get high bit of high word */
107 for (i=0; i<4; i++) {
108 nbit = random_value[i] >> 31;
109 random_value[i] = (random_value[i] << 1) + bit;
112 /* get 60ths from usec. This is all the real randomness there is. */
113 random_value[0] += time.tv_usec / 16667;
115 if (nextAutoCPWTime == 0) { /* initialize things */
116 nextAutoCPWTime = time.tv_sec + autoCPWInterval;
117 bcopy (&time, &random_value[0], 8);
118 bcopy (&time, &random_value[2], 8);
121 if ((++totalUpdates >= autoCPWUpdates) &&
122 tt && /* a write transaction */
123 ((admin && (time.tv_sec >= nextAutoCPWTime)) ||
124 (time.tv_sec >= nextAutoCPWTime + autoCPWInterval))) {
125 struct ktc_encryptionKey key;
126 char buf[4*sizeof(key)+1];
127 struct kaentry tentry;
129 char bob[KA_TIMESTR_LEN];
131 ka_timestr(time.tv_sec,bob,KA_TIMESTR_LEN);
132 es_Report ("Auto CPW at %s\n", bob);
133 if (!admin) es_Report (" ... even though no ADMIN user\n");
135 code = FindBlock (tt, KA_ADMIN_NAME, KA_ADMIN_INST, &to, &tentry);
136 if (code) return code;
137 if (to) { /* check if auto cpw is disabled */
138 if (!(ntohl(tentry.flags) & KAFNOCPW)) {
139 bcopy (&random_value[0], &key, sizeof(key));
140 des_fixup_key_parity (&key);
141 code = set_password (tt, KA_ADMIN_NAME, KA_ADMIN_INST,
144 des_init_random_number_generator (&key);
145 ka_ConvertBytes (buf, sizeof(buf), &key, sizeof(key));
146 es_Report ("New Admin key is %s\n", buf);
148 es_Report ("in get_time: set_password failed because: %d\n", code);
154 code = FindBlock(tt, KA_TGS_NAME, lrealm, &to, &tentry);
155 if (code) return code;
156 if (to) { /* check if auto cpw is disabled */
157 if (!(ntohl(tentry.flags) & KAFNOCPW)) {
158 bcopy (&random_value[2], &key, sizeof(key));
159 des_fixup_key_parity (&key);
160 code = set_password (tt, KA_TGS_NAME, lrealm, &key, 0, 0);
162 ka_ConvertBytes (buf, sizeof(buf), &key, sizeof(key));
163 es_Report ("New TGS key is %s\n", buf);
165 es_Report ("in get_time: set_password failed because: %s\n", error_message (code));
170 code = ka_FillKeyCache (tt); /* ensure in-core copy is uptodate */
171 if (code) return code;
173 nextAutoCPWTime = time.tv_sec + autoCPWInterval;
176 if (timeP) *timeP = time.tv_sec;
180 static int noAuthenticationRequired; /* global state */
181 static int recheckNoAuth; /* global state */
183 /* kaprocsInited is sort of a lock: during a transaction only one process runs
184 while kaprocsInited is false. */
186 static int kaprocsInited = 0;
188 /* This variable is protected by the kaprocsInited flag. */
190 static int (*rebuildDatabase)();
192 /* This is called to initialize the database */
194 static int initialize_database (tt)
195 struct ubik_trans *tt;
196 { struct ktc_encryptionKey key;
199 gettimeofday((struct timeval *)&key, 0); /* this is just a cheap seed key */
200 des_fixup_key_parity (&key);
201 des_init_random_number_generator (&key);
202 if ((code = des_random_key (&key)) ||
203 (code = create_user (tt, KA_ADMIN_NAME, KA_ADMIN_INST,
204 &key, 0, KAFNORMAL|KAFNOSEAL|KAFNOTGS)))
206 if ((code = des_random_key (&key)) ||
207 (code = create_user (tt, KA_TGS_NAME, lrealm,
208 &key, 0, KAFNORMAL|KAFNOSEAL|KAFNOTGS)))
213 /* This routine handles initialization required by this module. The initFlags
214 parameter passes some information about the command line arguments. */
216 afs_int32 init_kaprocs(lclpath, initFlags)
220 struct ubik_trans *tt;
221 struct ktc_encryptionKey key;
225 if (myHost == 0) return KAINTERNALERROR;
226 if (KA_conf == 0) return KAINTERNALERROR;
227 code = afsconf_GetLocalCell (KA_conf, lrealm, sizeof(lrealm));
229 printf("** Can't determine local cell name!\n");
232 ucstring (lrealm, lrealm, sizeof(lrealm));
235 if (initFlags & 1) noAuthenticationRequired = 1;
236 if (initFlags & 2) recheckNoAuth = 0;
238 noAuthenticationRequired = afsconf_GetNoAuthFlag(KA_conf);
239 if (noAuthenticationRequired)
240 printf ("Running server with security disabled\n");
243 autoCPWInterval = 10;
246 autoCPWInterval = AUTOCPWINTERVAL;
247 autoCPWUpdates = AUTOCPWUPDATES;
250 init_kadatabase (initFlags);
251 rebuildDatabase = initialize_database;
253 if (code = InitAuthServ (&tt, LOCKREAD, 0)) {
254 printf ("init_kaprocs: InitAuthServ failed: code = %d\n", code);
257 code = ka_LookupKey (tt, KA_ADMIN_NAME, KA_ADMIN_INST, &kvno, &key);
260 printf ("init_kaprocs: ka_LookupKey (code = %d) DB not initialized properly?\n", code);
263 des_init_random_number_generator (&key);
265 code = ubik_EndTrans(tt);
267 printf ("init_kaprocs: ubik_EndTrans failed: code = %d\n", code);
271 kaux_opendb(lclpath); /* aux database stores failure counters */
272 rebuildDatabase = 0; /* only do this during init */
277 /* These variable are for returning debugging info about the state of the
278 server. If they get trashed during multi-threaded operation it doesn't
281 /* this is global so COUNT_REQ in krb_udp.c can refer to it. */
282 char *lastOperation = 0; /* name of last operation */
283 static Date lastTrans; /* time of last transaction */
285 static char adminPrincipal[256];
286 static char authPrincipal[256];
287 static char tgsPrincipal[256];
288 static char tgsServerPrincipal[256];
290 void save_principal (p, n, i, c)
299 if (i && strlen (i)) {
300 if (s-- <= 0) return;
307 if (c && strlen(c)) {
308 if (s-- <= 0) return;
316 static afs_int32 check_auth (call, at, admin, acaller_id)
317 struct rx_call *call;
318 struct ubik_trans *at;
319 int admin; /* require caller to be ADMIN */
320 afs_int32 *acaller_id;
322 char name[MAXKTCNAMELEN];
323 char instance[MAXKTCNAMELEN];
324 char cell[MAXKTCREALMLEN];
326 Date expiration; /* checked by Security Module */
327 struct kaentry tentry;
334 noAuthenticationRequired = afsconf_GetNoAuthFlag(KA_conf);
336 si = rx_SecurityClassOf(rx_ConnectionOf(call));
337 if (si == RX_SCINDEX_VAB) {
338 printf ("No support for VAB security module yet.\n");
340 } else if (si == RX_SCINDEX_NULL) {
343 } else if (si != RX_SCINDEX_KAD) {
344 es_Report ("Unknown security index %d\n", si);
348 code = rxkad_GetServerInfo (rx_ConnectionOf(call), &level, &expiration,
349 name, instance, cell, &kvno);
353 if (level != rxkad_crypt) {
354 es_Report ("Incorrect security level = %d\n", level);
359 if (!name_instance_legal (name, instance)) return KABADNAME;
361 ka_PrintUserID ("Authorization rejected because we don't understand intercell stuff yet: ",
363 printf ("@%s\n", cell);
367 code = FindBlock(at, name, instance, acaller_id, &tentry);
368 if (code) return code;
369 if (*acaller_id == 0) {
370 ka_PrintUserID ("User ", name, instance, " unknown.\n");
373 save_principal (adminPrincipal, name, instance, 0);
376 if (!(ntohl(tentry.flags) & KAFADMIN)) {
377 if (noAuthenticationRequired) {
378 ka_PrintUserID ("Authorization approved for ", name, instance,
379 " because there is no authentication required\n");
380 osi_auditU (call, UnAuthEvent, code, AUD_STR, name, AUD_STR, instance, AUD_STR, cell, AUD_END);
383 ka_PrintUserID ("User ", name, instance, " is not ADMIN.\n");
386 osi_auditU (call, UseOfPrivilegeEvent, code, AUD_STR,name, AUD_STR,instance, AUD_STR,cell, AUD_END);
391 if (noAuthenticationRequired) {
392 es_Report ("Caller w/o authorization approved no authentication required\n");
393 osi_auditU (call, UnAuthEvent, code, AUD_STR, name, AUD_STR, instance, AUD_STR, cell, AUD_END);
396 return code; /* no auth info */
399 afs_int32 AwaitInitialization ()
400 { afs_int32 start = 0;
401 while (!kaprocsInited) {
402 if (!start) start = time(0);
403 else if (time(0)-start > 5) return UNOQUORUM;
409 /* This is called by every RPC interface to create a Ubik transaction and read
410 the database header into core */
412 afs_int32 InitAuthServ (tt, lock, this_op)
413 struct ubik_trans **tt;
414 int lock; /* indicate read/write transaction */
415 int *this_op; /* opcode of RPC proc, for COUNT_ABO */
417 afs_int32 start = 0; /* time started waiting for quorum */
418 float wait = 0.91; /* start waiting for 1 second */
420 /* Wait for server initialization to finish if not during init_kaprocs */
421 if (this_op) if (code = AwaitInitialization()) return code;
423 for (code = UNOQUORUM; code == UNOQUORUM; ) {
424 if (lock == LOCKREAD)
425 code = ubik_BeginTransReadAny (KA_dbase, UBIK_READTRANS, tt);
426 else code = ubik_BeginTrans (KA_dbase, UBIK_WRITETRANS, tt);
427 if (code == UNOQUORUM) { /* no quorum elected */
428 if (!start) start = time(0);
430 int delay = time(0) - start;
431 if (this_op) { /* punt quickly, if RPC call */
432 if (delay > 5) return code;
433 } else { /* more patient during init. */
434 if (delay > 500) return code;
437 printf ("Waiting for quorum election.\n");
438 if (wait < 15.0) wait *= 1.1;
439 IOMGR_Sleep ((int)wait);
442 if (code) return code;
443 if (code = ubik_SetLock (*tt, 1, 1, lock)) {
444 if (this_op) COUNT_ABO;
445 ubik_AbortTrans (*tt);
448 /* check that dbase is initialized and setup cheader */
449 if (lock == LOCKREAD) {
450 /* init but don't fix because this is read only */
451 code = CheckInit(*tt, 0);
453 ubik_AbortTrans (*tt); /* abort, since probably I/O error */
454 /* we did the check under a ReadAny transaction, but now, after
455 * getting a write transaction (and thus some real guarantees
456 * about what databases are really out there), we will check again
457 * in CheckInit before nuking the database. Since this may now get
458 * a UNOQUORUM we'll just do this from the top.
460 if (code = InitAuthServ (tt, LOCKWRITE, this_op)) return code;
461 if (code = ubik_EndTrans(*tt)) return code;
463 /* now open the read transaction that was originally requested. */
464 return InitAuthServ (tt, lock, this_op);
468 if (code = CheckInit(*tt, rebuildDatabase)) {
469 if (this_op) COUNT_ABO;
470 ubik_AbortTrans(*tt);
475 ka_FillKeyCache (*tt); /* ensure in-core copy is uptodate */
479 /* returns true if name is specially known by AuthServer */
481 static int special_name (name, instance)
485 return ((!strcmp (name, KA_TGS_NAME) && !strcmp(instance, lrealm)) ||
486 (strcmp (name, KA_ADMIN_NAME) == 0));
489 static int create_user (tt, name, instance, key, caller, flags)
490 struct ubik_trans *tt;
498 struct kaentry tentry;
499 afs_int32 maxLifetime;
501 code = FindBlock(tt, name, instance, &to, &tentry);
502 if (code) return code;
503 if (to) return KAEXIST; /* name already exists, we fail */
505 to = AllocBlock(tt, &tentry);
506 if (to == 0) return KACREATEFAIL;
508 /* otherwise we have a block */
509 strncpy(tentry.userID.name, name, sizeof(tentry.userID.name));
510 strncpy(tentry.userID.instance, instance, sizeof(tentry.userID.instance));
511 tentry.flags = htonl(flags);
512 if (special_name (name, instance)) { /* this overrides key & version */
513 tentry.flags = htonl (ntohl(tentry.flags) | KAFSPECIAL);
514 tentry.key_version = htonl(-1); /* don't save this key */
515 if (code = ka_NewKey (tt, to, &tentry, key)) return code;
518 bcopy(key, &tentry.key, sizeof(tentry.key));
519 tentry.key_version = htonl(0);
521 tentry.user_expiration = htonl(NEVERDATE);
522 code = get_time (&tentry.modification_time, tt, 1);
523 if (code) return code;
525 /* time and addr of entry for guy changing this entry */
526 tentry.modification_time = htonl(tentry.modification_time);
527 tentry.modification_id = htonl(caller);
528 tentry.change_password_time = tentry.modification_time;
530 if (strcmp (name, KA_TGS_NAME) == 0) maxLifetime = MAXKTCTICKETLIFETIME;
531 else if (strcmp (name, KA_ADMIN_NAME) == 0) maxLifetime = 10*3600;
532 else if (strcmp (name, AUTH_SUPERUSER) == 0) maxLifetime = 100*3600;
533 else maxLifetime = 25*3600; /* regular users */
534 tentry.max_ticket_lifetime = htonl(maxLifetime);
536 code = ThreadBlock (tt, to, &tentry);
540 /* Put actual stub routines here */
542 afs_int32 KAM_CreateUser (call, aname, ainstance, ainitpw)
543 struct rx_call *call;
546 EncryptionKey ainitpw;
550 code = kamCreateUser (call, aname, ainstance, ainitpw);
551 osi_auditU (call, AFS_KAM_CrUserEvent, code, AUD_STR, aname, AUD_STR, ainstance, AUD_END);
556 afs_int32 kamCreateUser (call, aname, ainstance, ainitpw)
557 struct rx_call *call;
560 EncryptionKey ainitpw;
562 struct ubik_trans *tt;
563 afs_int32 caller; /* Disk offset of caller's entry */
565 COUNT_REQ (CreateUser);
566 if (!des_check_key_parity (&ainitpw) ||
567 des_is_weak_key (&ainitpw)) return KABADKEY;
568 if (!name_instance_legal (aname, ainstance)) return KABADNAME;
569 if (code = InitAuthServ( &tt, LOCKWRITE, this_op)) return code;
570 code = check_auth (call, tt, 1, &caller);
576 code = create_user (tt, aname, ainstance, &ainitpw, caller, KAFNORMAL);
582 code = ubik_EndTrans(tt);
583 KALOG(aname, ainstance, NULL, NULL, NULL, call->conn->peer->host, LOG_CRUSER);
587 afs_int32 KAA_ChangePassword (call, aname, ainstance, arequest, oanswer)
588 struct rx_call *call;
596 code = ChangePassWord (call, aname, ainstance, arequest, oanswer);
597 osi_auditU(call, AFS_KAA_ChPswdEvent, code, AUD_STR, aname, AUD_STR, ainstance, AUD_END);
601 afs_int32 ChangePassWord (call, aname, ainstance, arequest, oanswer)
602 struct rx_call *call;
608 struct ubik_trans *tt;
609 afs_int32 to; /* offset of block */
610 struct kaentry tentry;
611 struct ka_cpwRequest request; /* request after decryption */
612 char *answer; /* where answer is to be put */
613 int answer_len; /* length of answer packet */
614 afs_int32 kvno; /* requested key version number */
615 des_key_schedule user_schedule; /* key schedule for user's key */
616 Date request_time; /* time request originated */
618 COUNT_REQ (ChangePassword);
619 if (!name_instance_legal (aname, ainstance)) return KABADNAME;
620 if (strcmp (ainstance, KA_ADMIN_NAME) == 0) return KABADNAME;
621 if (code = InitAuthServ (&tt, LOCKWRITE, this_op)) return code;
623 code = FindBlock (tt, aname, ainstance, &to, &tentry);
627 if (to == 0) { /* no such user */
631 if (ntohl(tentry.flags) & KAFNOCPW)
632 { code = KABADCPW; goto abort; }
634 /* decrypt request w/ user password */
635 if (code = des_key_sched (&tentry.key, user_schedule))
636 es_Report ("In KAChangePassword: key_sched returned %d\n", code);
637 des_pcbc_encrypt (arequest->SeqBody, &request,
638 min (arequest->SeqLen, sizeof(request)),
639 user_schedule, &tentry.key, DECRYPT);
641 /* validate the request */
642 request_time = ntohl (request.time); /* reorder date */
643 kvno = ntohl(request.kvno);
644 if ((abs (request_time - time(0)) > KTC_TIME_UNCERTAINTY) ||
645 strncmp (request.label, KA_CPW_REQ_LABEL, sizeof(request.label)) ||
647 (kvno > MAXKAKVNO)) { /* these are reseved */
652 /* check to see if the new password was used before, or if there has
653 * not been sufficient time since the last password change
655 code = impose_reuse_limits(&request.newpw, &tentry);
660 /* Create the Answer Packet */
661 answer_len = sizeof(Date) + KA_LABELSIZE;
662 if (oanswer->MaxSeqLen < answer_len) {
663 code = KAANSWERTOOLONG;
666 oanswer->SeqLen = answer_len;
667 answer = oanswer->SeqBody;
668 request.time = htonl (request_time+1);
669 bcopy ((char *)&request.time, answer, sizeof(Date));
670 answer += sizeof(Date);
671 bcopy (KA_CPW_ANS_LABEL, answer, KA_LABELSIZE);
673 des_pcbc_encrypt (oanswer->SeqBody, oanswer->SeqBody, answer_len,
674 user_schedule, &tentry.key, ENCRYPT);
676 code = set_password (tt, aname, ainstance, &request.newpw, kvno, 0);
682 cheader.stats.cpws = htonl(ntohl(cheader.stats.cpws)+1);
683 code = kawrite(tt, DOFFSET(0, &cheader, &cheader.stats.cpws),
684 (char *) &cheader.stats.cpws, sizeof(afs_int32));
690 code = ubik_EndTrans(tt);
695 ubik_AbortTrans (tt);
700 impose_reuse_limits ( password, tentry )
701 EncryptionKey *password;
702 struct kaentry *tentry;
711 if (!tentry->pwsums[0] && npwSums > 1 && !tentry->pwsums[1])
712 return 0; /* password reuse limits not in effect */
714 code = get_time (&now,0,0);
715 if (code) return code;
717 if ((now - ntohl(tentry->change_password_time)) < MinHours*60*60)
720 if (!bcmp(password, &(tentry->key), sizeof(EncryptionKey)))
723 code = ka_KeyCheckSum (password, &newsum);
727 newsum = newsum & 0x000000ff;
728 for (i=0; i<npwSums; i++) {
729 if (newsum == tentry->pwsums[i])
738 set_password (tt, name, instance, password, kvno, caller)
739 struct ubik_trans *tt;
742 EncryptionKey *password;
746 afs_int32 to; /* offset of block */
747 struct kaentry tentry;
753 code = FindBlock (tt, name, instance, &to, &tentry);
754 if (code) return code;
755 if (to == 0) return KANOENT; /* no such user */
757 /* if password reuse limits in effect, set the checksums, the hard way */
758 if (!tentry.pwsums[0] && npwSums > 1 && !tentry.pwsums[1]) {
759 /* do nothing, no limits */ ;
762 code = ka_KeyCheckSum (&(tentry.key), &newsum);
765 for (i=npwSums-1; i ; i--)
766 tentry.pwsums[i] = tentry.pwsums[i-1];
767 tentry.pwsums[0] = newsum & 0x000000ff;
771 if (special_name (name, instance)) { /* set key over rides key_version */
773 tentry.flags = htonl (ntohl(tentry.flags) | KAFSPECIAL);
775 if (code = ka_NewKey (tt, to, &tentry, password)) return(code);
778 bcopy (password, &tentry.key, sizeof(tentry.key));
780 kvno = ntohl(tentry.key_version);
781 if ((kvno < 1) || (kvno >= MAXKAKVNO))
786 tentry.key_version = htonl((afs_int32)kvno); /* requested key version */
791 /* no-write prevents recursive call to set_password by AuthCPW code. */
792 code = get_time (&now,0,0);
793 if (code) return code;
795 tentry.modification_time = htonl(now);
796 tentry.modification_id = htonl(caller);
799 tentry.change_password_time = htonl(now);
801 if (code = kawrite (tt, to, &tentry, sizeof(tentry))) return(KAIO);
805 afs_int32 KAM_SetPassword (call, aname, ainstance, akvno, apassword)
806 struct rx_call *call;
810 EncryptionKey apassword;
814 code = kamSetPassword (call, aname, ainstance, akvno, apassword);
815 osi_auditU (call, AFS_KAM_SetPswdEvent, code, AUD_STR, aname, AUD_STR, ainstance, AUD_END);
819 afs_int32 kamSetPassword (call, aname, ainstance, akvno, apassword)
820 struct rx_call *call;
824 EncryptionKey apassword;
826 struct ubik_trans *tt;
827 afs_int32 caller; /* Disk offset of caller's entry */
828 struct kaentry tentry;
830 COUNT_REQ (SetPassword);
831 if (akvno > MAXKAKVNO) return KABADARGUMENT;
832 if (!des_check_key_parity (&apassword) ||
833 des_is_weak_key (&apassword)) return KABADKEY;
835 if (!name_instance_legal (aname, ainstance)) return KABADNAME;
836 if (code = InitAuthServ (&tt, LOCKWRITE, this_op)) return code;
837 code = check_auth (call, tt, 0, &caller);
841 if (code = karead (tt, caller, &tentry, sizeof(tentry))) {
845 /* if the user is changing his own password or ADMIN then go ahead. */
846 if ((strcmp (tentry.userID.name, aname) == 0) &&
847 (strcmp (tentry.userID.instance, ainstance) == 0)) {
848 if (ntohl(tentry.flags) & KAFNOCPW)
851 code = impose_reuse_limits(&apassword, &tentry);
853 code = set_password (tt, aname, ainstance, &apassword, akvno, 0);
855 } else if (ntohl(tentry.flags) & KAFADMIN) {
856 code = set_password (tt, aname, ainstance, &apassword, akvno, caller);
858 else code = KANOAUTH;
859 if (code) goto abort;
861 code = ubik_EndTrans(tt);
862 KALOG(aname, ainstance, NULL, NULL, NULL, call->conn->peer->host, LOG_CHPASSWD);
871 static Date CoerseLifetime (start, end)
874 unsigned char kerberosV4Life;
875 kerberosV4Life = time_to_life (start, end);
876 end = life_to_time (start, kerberosV4Life);
880 static afs_int32 GetEndTime (start, reqEnd, expiration, caller, server, endP)
881 IN Date start; /* start time of ticket */
882 IN Date reqEnd; /* requested end time */
883 IN Date expiration; /* authorizing ticket's expiration */
884 IN struct kaentry *caller;
885 IN struct kaentry *server;
886 OUT Date *endP; /* actual end time */
892 if (ntohl(caller->flags) & KAFNOTGS)
893 return KABADUSER; /* no new tickets for this user */
894 if (expiration && (ntohl(server->flags) & KAFNOSEAL))
895 return KABADSERVER; /* can't be target of GetTicket req */
896 if (!expiration) expiration = NEVERDATE;
898 cExp = ntohl(caller->user_expiration);
899 sExp = ntohl(server->user_expiration);
900 if (cExp < start) return KAPWEXPIRED;
901 if (sExp < start) return KABADSERVER;
902 cLife = start + ntohl(caller->max_ticket_lifetime);
903 sLife = start + ntohl(server->max_ticket_lifetime);
904 end = umin (umin (reqEnd, expiration),
905 umin (umin (cLife,sLife),
907 end = CoerseLifetime (start, end);
914 (oanswer, challenge, ticket, ticketLen, sessionKey,
915 start, end, caller, server, cell, label)
920 struct ktc_encryptionKey *sessionKey;
922 struct kaentry *caller, *server;
927 struct ka_ticketAnswer *answer;
930 code = KAANSWERTOOLONG;
931 if (oanswer->MaxSeqLen < sizeof(struct ka_ticketAnswer) - 5*MAXKTCNAMELEN)
934 answer = (struct ka_ticketAnswer *)oanswer->SeqBody;
935 answer->challenge = htonl(challenge);
936 bcopy (sessionKey, &answer->sessionKey, sizeof(struct ktc_encryptionKey));
937 answer->startTime = htonl(start);
938 answer->endTime = htonl(end);
939 answer->kvno = server->key_version;
940 answer->ticketLen = htonl(ticketLen);
942 { char *ans = answer->name; /* pointer to variable part */
943 int rem; /* space remaining */
944 int len; /* macro temp. */
946 rem = oanswer->MaxSeqLen - (ans - oanswer->SeqBody);
948 #define putstr(str) len = strlen (str)+1;\
949 if (rem < len) return code;\
951 ans += len; rem -= len
952 putstr (caller->userID.name);
953 putstr (caller->userID.instance);
955 putstr (server->userID.name);
956 putstr (server->userID.instance);
957 if (rem < ticketLen+KA_LABELSIZE) return code;
958 bcopy (ticket, ans, ticketLen);
960 if (label) bcopy (label, ans, KA_LABELSIZE);
961 else bzero (ans, KA_LABELSIZE);
963 oanswer->SeqLen = (ans - oanswer->SeqBody);
966 answer->cksum = htonl(cksum);
967 oanswer->SeqLen = round_up_to_ebs(oanswer->SeqLen);
968 if (oanswer->SeqLen > oanswer->MaxSeqLen) return code;
972 /* This is used to get a ticket granting ticket or an admininstration ticket.
973 These two specific, built-in servers are special cases, which require the
974 client's key as an additional security precaution. The GetTicket operation
975 is normally disabled for these two principals. */
977 static afs_int32 Authenticate (version, call, aname, ainstance, start, end,
980 struct rx_call *call;
987 struct ubik_trans *tt;
988 afs_int32 to; /* offset of block */
990 struct kaentry server; /* entry for desired server */
991 struct ka_gettgtRequest request; /* request after decryption */
992 int tgt, adm; /* type of request */
993 char *sname; /* principal of server */
995 char ticket[MAXKTCTICKETLEN]; /* our copy of the ticket */
997 struct ktc_encryptionKey sessionKey; /* we have to invent a session key */
998 char *answer; /* where answer is to be put */
999 int answer_len; /* length of answer packet */
1000 Date answer_time; /* 1+ request time in network order */
1001 afs_int32 temp; /* for htonl conversions */
1002 des_key_schedule user_schedule; /* key schedule for user's key */
1003 afs_int32 tgskvno; /* key version of service key */
1004 struct ktc_encryptionKey tgskey; /* service key for encrypting ticket */
1006 afs_uint32 pwexpires;
1007 afs_uint32 lasttime;
1009 COUNT_REQ (Authenticate);
1010 if (!name_instance_legal (aname, ainstance)) return KABADNAME;
1011 if (code = InitAuthServ (&tt, LOCKREAD, this_op)) return code;
1012 get_time(&now, 0, 0);
1014 sname = sinst = NULL;
1016 code = FindBlock (tt, aname, ainstance, &to, &tentry);
1020 if (to == 0) { /* no such user */
1026 /* have to check for locked before verifying the password, otherwise all
1027 * KALOCKED means is "yup, you guessed the password all right, now wait a
1028 * few minutes and we'll let you in"
1030 if (kaux_islocked(to, (u_int) tentry.misc_auth_bytes[ATTEMPTS],
1031 (afs_uint32) tentry.misc_auth_bytes[LOCKTIME] << 9)) {
1037 save_principal (authPrincipal, aname, ainstance, 0);
1039 /* decrypt request w/ user password */
1040 if (code = des_key_sched (&tentry.key, user_schedule))
1041 es_Report ("In KAAuthenticate: key_sched returned %d\n", code);
1042 des_pcbc_encrypt (arequest->SeqBody, &request,
1043 min (arequest->SeqLen, sizeof(request)),
1044 user_schedule, &tentry.key, DECRYPT);
1046 request.time = ntohl (request.time); /* reorder date */
1047 tgt = !strncmp (request.label, KA_GETTGT_REQ_LABEL, sizeof(request.label));
1048 adm = !strncmp (request.label, KA_GETADM_REQ_LABEL, sizeof(request.label));
1049 if (!(tgt || adm)) {
1050 kaux_inc(to,((unsigned char) tentry.misc_auth_bytes[LOCKTIME]) << 9);
1051 code = KABADREQUEST;
1054 else kaux_write(to, 0, 0); /* reset counters */
1057 if (! tentry.misc_auth_bytes[EXPIRES]) {
1058 /* 0 in the database means never, but 0 on the network means today */
1059 /* 255 on the network means "long time, maybe never" */
1063 pwexpires = tentry.misc_auth_bytes[EXPIRES];
1065 pwexpires = ntohl(tentry.change_password_time) + 24*60*60*pwexpires;
1066 if (adm) { /* provide a little slack for admin ticket */
1067 pwexpires += 30*24*60*60; /* 30 days */
1069 if ( pwexpires < now ) {
1074 pwexpires = (pwexpires - now)/ (24*60*60);
1075 if (pwexpires > 255)
1079 #endif /* EXPIREPW */
1081 if (abs (request.time - now) > KTC_TIME_UNCERTAINTY) {
1083 if (oanswer->MaxSeqLen < sizeof(afs_int32)) code = KAANSWERTOOLONG;
1084 else { /* return our time if possible */
1085 oanswer->SeqLen = sizeof(afs_int32);
1086 request.time = htonl(now);
1087 bcopy (&request.time, oanswer->SeqBody, sizeof(afs_int32));
1093 sname = (tgt ? KA_TGS_NAME : KA_ADMIN_NAME);
1094 sinst = (tgt ? lrealm : KA_ADMIN_INST);
1095 code = FindBlock (tt, sname, sinst, &to, &server);
1096 if (code) goto abort;
1097 if (to == 0) {code = KANOENT; goto abort; }
1099 tgskvno = ntohl(server.key_version);
1100 bcopy (&server.key, &tgskey, sizeof(tgskey));
1102 code = des_random_key (&sessionKey);
1108 code = GetEndTime (start, end, 0/*!GetTicket*/, &tentry, &server, &end);
1109 if (code) goto abort;
1111 code = tkt_MakeTicket (ticket, &ticketLen, &tgskey,
1112 aname, ainstance, "", start, end, &sessionKey,
1113 rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))),
1115 if (code) goto abort;
1119 answer_len = ticketLen + sizeof(Date) +
1120 sizeof(struct ktc_encryptionKey) + 2*sizeof(afs_int32) + KA_LABELSIZE;
1121 answer_len = round_up_to_ebs(answer_len);
1122 if (answer_len > oanswer->MaxSeqLen) {
1123 code = KAANSWERTOOLONG;
1126 oanswer->SeqLen = answer_len;
1127 answer = oanswer->SeqBody;
1128 answer_time = htonl(request.time+1);
1129 bcopy ((char *)&answer_time, answer, sizeof(Date));
1130 answer += sizeof(Date);
1131 bcopy ((char *)&sessionKey, answer, sizeof(struct ktc_encryptionKey));
1132 answer += sizeof(struct ktc_encryptionKey);
1133 temp = htonl(tgskvno);
1134 bcopy ((char *)&temp, answer, sizeof(afs_int32));
1135 answer += sizeof(afs_int32);
1136 temp = htonl(ticketLen);
1137 bcopy ((char *)&temp, answer, sizeof(afs_int32));
1138 answer += sizeof(afs_int32);
1139 bcopy (ticket, answer, ticketLen);
1140 answer += ticketLen;
1141 bcopy ((tgt ? KA_GETTGT_ANS_LABEL : KA_GETADM_ANS_LABEL), answer,
1146 code = PrepareTicketAnswer
1147 (oanswer, request.time+1, ticket, ticketLen, &sessionKey,
1148 start, end, &tentry, &server, "",
1149 (tgt ? KA_GETTGT_ANS_LABEL : KA_GETADM_ANS_LABEL));
1150 if (code) goto abort;
1152 if ((version == 2) && oanswer->SeqLen < oanswer->MaxSeqLen + sizeof(afs_int32)) {
1153 temp = pwexpires << 24; /* move it into the high byte */
1154 pwexpires = htonl(temp);
1156 bcopy (&pwexpires, (char * )oanswer->SeqBody + oanswer->SeqLen, sizeof(afs_int32));
1157 oanswer->SeqLen += sizeof(afs_int32);
1158 oanswer->SeqLen = round_up_to_ebs(oanswer->SeqLen);
1159 if (oanswer->SeqLen > oanswer->MaxSeqLen) {
1160 code = KAANSWERTOOLONG;
1164 #endif /* EXPIREPW */
1168 code = KAINTERNALERROR;
1171 des_pcbc_encrypt (oanswer->SeqBody, oanswer->SeqBody, oanswer->SeqLen,
1172 user_schedule, &tentry.key, ENCRYPT);
1173 code = ubik_EndTrans(tt);
1174 KALOG(aname, ainstance, sname, sinst, NULL, call->conn->peer->host, LOG_AUTHENTICATE);
1179 ubik_AbortTrans (tt);
1180 KALOG(aname, ainstance, sname, sinst, NULL, call->conn->peer->host, LOG_AUTHFAILED);
1184 afs_int32 KAA_Authenticate_old (call, aname, ainstance, start, end,
1186 struct rx_call *call;
1195 IOMGR_Sleep (1); /* discourage use of this mechanism */
1196 code = Authenticate (0, call, aname, ainstance, start, end,
1198 osi_auditU(call, AFS_KAA_AuthOEvent, code, AUD_STR, aname, AUD_STR, ainstance, AUD_END);
1203 afs_int32 KAA_Authenticate (call, aname, ainstance, start, end, arequest, oanswer)
1204 struct rx_call *call;
1213 code = Authenticate (1, call, aname, ainstance, start, end,
1215 osi_auditU(call, AFS_KAA_AuthEvent, code, AUD_STR, aname, AUD_STR, ainstance, AUD_END);
1220 afs_int32 KAA_AuthenticateV2 (call, aname, ainstance, start, end, arequest, oanswer)
1221 struct rx_call *call;
1230 code = Authenticate (2, call, aname, ainstance, start, end,
1232 osi_auditU(call, AFS_KAA_AuthEvent, code, AUD_STR, aname, AUD_STR, ainstance, AUD_END);
1237 afs_int32 KAM_SetFields (call, aname, ainstance, aflags,
1238 aexpiration, alifetime, amaxAssociates, misc_auth_bytes, spare2)
1239 struct rx_call *call;
1244 afs_int32 alifetime;
1245 afs_int32 amaxAssociates;
1246 afs_uint32 misc_auth_bytes; /* 4 bytes, each 0 means unspecified*/
1251 code = kamSetFields (call, aname, ainstance, aflags,
1252 aexpiration, alifetime, amaxAssociates, misc_auth_bytes, spare2);
1253 osi_auditU (call, AFS_KAM_SetFldEvent, code, AUD_STR, aname,
1256 AUD_DATE, aexpiration,
1257 AUD_LONG, alifetime,
1258 AUD_LONG, amaxAssociates,
1263 afs_int32 kamSetFields (call, aname, ainstance, aflags,
1264 aexpiration, alifetime, amaxAssociates, misc_auth_bytes, spare2)
1265 struct rx_call *call;
1270 afs_int32 alifetime;
1271 afs_int32 amaxAssociates;
1272 afs_uint32 misc_auth_bytes; /* 4 bytes, each 0 means unspecified*/
1276 struct ubik_trans *tt;
1278 afs_int32 tentry_offset; /* offset of entry */
1279 struct kaentry tentry;
1280 unsigned char newvals[4], oldvals[4];
1283 COUNT_REQ (SetFields);
1286 return KABADARGUMENT; /* not supported yet... */
1288 /* make sure we're supposed to do something */
1289 if (!(aflags || aexpiration || alifetime || (amaxAssociates >= 0) ||
1290 misc_auth_bytes ) ||
1291 ((aflags & ~KAFNORMAL) & ~KAF_SETTABLE_FLAGS))
1292 return KABADARGUMENT; /* arguments no good */
1293 if (!name_instance_legal (aname, ainstance)) return KABADNAME;
1294 if (code = InitAuthServ (&tt, LOCKWRITE, this_op)) return code;
1295 code = check_auth (call, tt, 1, &caller);
1300 code = FindBlock (tt, aname, ainstance, &tentry_offset, &tentry);
1301 if (code) goto abort;
1302 if (tentry_offset == 0) { /* no such user */
1306 if ((ntohl(tentry.flags) & KAFNORMAL) == 0) return KAINTERNALERROR;
1308 /* Keep track of the total number of admin accounts. This way we can
1309 update database without any admin privilege initially */
1310 if ((aflags & KAFADMIN) != (ntohl(tentry.flags) & KAFADMIN)) {
1311 /* if admin state is changing */
1313 if (ntohl(tentry.flags) & KAFADMIN) delta = -1;
1315 if (code = update_admin_count (tt, delta)) goto abort;
1318 htonl((ntohl(tentry.flags) & ~KAF_SETTABLE_FLAGS) | aflags);
1320 if (code = get_time (&now, tt, 1)) goto abort;
1322 tentry.user_expiration = htonl(aexpiration);
1323 if (!ntohl(tentry.change_password_time)) {
1324 tentry.change_password_time = htonl(now);
1327 if (alifetime) tentry.max_ticket_lifetime = htonl(alifetime);
1329 #ifndef NOPWCONTROLS
1331 * We've packed a bunch of bytes into a long for backward compatibility.
1332 * These include password expiration time, and some failed login limits
1333 * counters. Now let's unpack them and stick them into the
1334 * kaentry struct. All the bytes have values in the range
1335 * 1..255, else they were not specified in the interface, and are
1337 * In the case of password expiration times, 1 means password never
1338 * expires (==>0), 2 means password only lives for one day (==>1),
1341 if (misc_auth_bytes) {
1342 unpack_long(misc_auth_bytes, newvals);
1343 if (newvals[EXPIRES]) {
1344 tentry.misc_auth_bytes[EXPIRES] = newvals[EXPIRES] - 1;
1347 if (newvals[REUSEFLAGS]) {
1348 if (newvals[REUSEFLAGS] & KA_REUSEPW)
1349 bzero(tentry.pwsums, KA_NPWSUMS);
1350 else if ((newvals[REUSEFLAGS] & KA_NOREUSEPW) && !tentry.pwsums[0])
1351 tentry.pwsums[0] = 0xff;
1354 if (newvals[ATTEMPTS]) {
1355 tentry.misc_auth_bytes[ATTEMPTS] = newvals[ATTEMPTS] - 1;
1357 if (newvals[LOCKTIME]) {
1358 tentry.misc_auth_bytes[LOCKTIME] = newvals[LOCKTIME] - 1;
1361 tentry.misc_auth_bytes = htonl(tentry.misc_auth_bytes);
1364 #endif /* NOPWCONTROLS */
1366 if (amaxAssociates >= 0) {
1367 if ((ntohl(tentry.flags) & KAFASSOC) ||
1368 (ntohl(tentry.flags) & KAFSPECIAL)) return KAASSOCUSER;
1369 if (((ntohl(tentry.flags) & KAFASSOCROOT) == 0) &&
1370 (amaxAssociates > 0)) /* convert normal user to assoc root */
1371 tentry.flags = htonl(ntohl(tentry.flags) | KAFASSOCROOT);
1372 tentry.misc.assocRoot.maxAssociates = htonl(amaxAssociates);
1375 tentry.modification_time = htonl(now);
1376 tentry.modification_id = htonl(caller);
1377 code = kawrite (tt, tentry_offset, &tentry, sizeof(tentry));
1378 if (code) goto abort;
1380 code = ubik_EndTrans(tt);
1381 KALOG(aname, ainstance, NULL, NULL, NULL, call->conn->peer->host, LOG_SETFIELDS);
1386 ubik_AbortTrans(tt);
1392 afs_int32 KAM_DeleteUser(call, aname, ainstance)
1393 struct rx_call *call;
1399 code = kamDeleteUser(call, aname, ainstance);
1400 osi_auditU (call, AFS_KAM_DelUserEvent, code, AUD_STR, aname, AUD_STR, ainstance, AUD_END);
1404 afs_int32 kamDeleteUser(call, aname, ainstance)
1405 struct rx_call *call;
1408 { register int code;
1409 struct ubik_trans *tt;
1412 struct kaentry tentry;
1414 afs_uint32 locktime;
1416 COUNT_REQ (DeleteUser);
1417 if (!name_instance_legal (aname, ainstance)) return KABADNAME;
1418 if (code = InitAuthServ (&tt, LOCKWRITE, this_op)) return code;
1419 code = check_auth (call, tt, 1, &caller);
1423 ubik_AbortTrans(tt);
1427 code = FindBlock(tt, aname, ainstance, &to, &tentry);
1428 if (code) goto abort;
1429 if (to == 0) { /* name not found */
1434 kaux_read( to, &nfailures, &locktime );
1435 if (nfailures || locktime)
1436 kaux_write( to, 0, 0 ); /* zero failure counters at this offset */
1438 /* track all AuthServer identities */
1439 if (special_name (aname, ainstance))
1440 if (code = ka_DelKey (tt, to, &tentry)) goto abort;
1442 if (ntohl(tentry.flags) & KAFADMIN) /* keep admin count up-to-date */
1443 if (code = update_admin_count (tt, -1)) goto abort;
1445 if ((code = UnthreadBlock (tt, &tentry)) ||
1446 (code = FreeBlock (tt, to)) ||
1447 (code = get_time (0, tt, 1)) /* update randomness */
1450 code = ubik_EndTrans(tt);
1451 KALOG(aname, ainstance, NULL, NULL, NULL, call->conn->peer->host, LOG_DELUSER);
1455 /* we set a bit in here which indicates that the user's limit of
1456 * authentication failures has been exceeded. If that bit is not set,
1457 * kas can take it on faith that the user ID is not locked. If that
1458 * bit is set, kas has to check all the servers to find one who will
1459 * report that the ID is not locked, or else to find out when the ID
1462 afs_int32 KAM_GetEntry (call, aname, ainstance, aversion, aentry)
1463 struct rx_call *call;
1466 afs_int32 aversion; /* major version assumed by caller */
1467 kaentryinfo *aentry; /* entry data copied here */
1471 code = kamGetEntry (call, aname, ainstance, aversion, aentry);
1472 osi_auditU (call, AFS_KAM_GetEntEvent, code, AUD_STR, aname, AUD_STR, ainstance, AUD_END);
1476 afs_int32 kamGetEntry (call, aname, ainstance, aversion, aentry)
1477 struct rx_call *call;
1480 afs_int32 aversion; /* major version assumed by caller */
1481 kaentryinfo *aentry; /* entry data copied here */
1482 { register afs_int32 code;
1483 struct ubik_trans *tt;
1484 afs_int32 callerIndex;
1485 struct kaentry caller;
1488 struct kaentry tentry;
1489 rxkad_level enc_level = rxkad_clear;
1490 int callerIsAdmin = 0;
1492 COUNT_REQ (GetEntry);
1493 if (aversion != KAMAJORVERSION) return KAOLDINTERFACE;
1494 if (!name_instance_legal (aname, ainstance)) return KABADNAME;
1495 if (code = InitAuthServ (&tt, LOCKREAD, this_op)) return code;
1496 code = check_auth (call, tt, 0, &callerIndex);
1500 if (noAuthenticationRequired) {
1502 else if (!callerIndex) {
1507 if (code = karead (tt, callerIndex, &caller, sizeof(caller))) {
1511 /* if the user is checking his own entry or ADMIN then go ahead. */
1512 callerIsAdmin = (ntohl(caller.flags) & KAFADMIN);
1514 if (strcmp (caller.userID.name, aname) != 0 && !callerIsAdmin) {
1520 code = FindBlock (tt, aname, ainstance, &to, &tentry);
1521 if (code) goto abort;
1522 if (to == 0) { /* entry not found */
1527 get_time (0,0,0); /* generate random update */
1529 bzero (aentry, sizeof(*aentry));
1530 aentry->minor_version = KAMINORVERSION;
1531 aentry->flags = ntohl(tentry.flags);
1532 aentry->user_expiration = ntohl(tentry.user_expiration);
1533 aentry->modification_time = ntohl(tentry.modification_time);
1534 aentry->change_password_time = ntohl(tentry.change_password_time);
1535 aentry->max_ticket_lifetime = ntohl(tentry.max_ticket_lifetime);
1536 aentry->key_version = ntohl(tentry.key_version);
1538 temp = (unsigned char) tentry.misc_auth_bytes[LOCKTIME] ;
1540 if (kaux_islocked(to, (u_int) tentry.misc_auth_bytes[ATTEMPTS], temp))
1541 tentry.misc_auth_bytes[REUSEFLAGS] |= KA_ISLOCKED; /* saves an RPC */
1543 temp = pack_long(tentry.misc_auth_bytes);
1544 aentry->misc_auth_bytes = temp;
1546 * only return user's key if security disabled or if admin and
1547 * we have an encrypted connection to the user
1549 rxkad_GetServerInfo(call->conn, &enc_level, 0, 0, 0, 0, 0);
1550 if ((noAuthenticationRequired) ||
1551 (callerIsAdmin && enc_level == rxkad_crypt))
1552 bcopy (&tentry.key, &aentry->key, sizeof(struct ktc_encryptionKey));
1553 else bzero (&aentry->key, sizeof(aentry->key));
1554 code = ka_KeyCheckSum (&tentry.key, &aentry->keyCheckSum);
1555 if (!tentry.pwsums[0] && npwSums > 1 && !tentry.pwsums[1]) {
1556 aentry->reserved3 = 0x12340000;
1558 aentry->reserved3 = 0x12340001;
1561 /* Now get entry of user who last modified this entry */
1562 if (ntohl(tentry.modification_id)) {
1563 temp = ntohl (tentry.modification_id);
1564 code = karead (tt, temp, &tentry, sizeof(tentry));
1569 aentry->modification_user = tentry.userID;
1572 strcpy (aentry->modification_user.name, "<none>");
1573 strcpy (aentry->modification_user.instance, "\0");
1575 code = ubik_EndTrans (tt);
1580 ubik_AbortTrans(tt);
1584 afs_int32 KAM_ListEntry (call, previous_index, index, count, name)
1585 struct rx_call *call;
1586 afs_int32 previous_index; /* last entry ret'd or 0 for first */
1587 afs_int32 *index; /* index of this entry */
1588 afs_int32 *count; /* total entries in database */
1589 kaident *name; /* name & instance of this entry */
1593 code = kamListEntry (call, previous_index, index, count, name);
1594 osi_auditU (call, AFS_KAM_LstEntEvent, code, AUD_LONG, *index, AUD_END);
1599 afs_int32 kamListEntry (call, previous_index, index, count, name)
1600 struct rx_call *call;
1601 afs_int32 previous_index; /* last entry ret'd or 0 for first */
1602 afs_int32 *index; /* index of this entry */
1603 afs_int32 *count; /* total entries in database */
1604 kaident *name; /* name & instance of this entry */
1605 { register int code;
1606 struct ubik_trans *tt;
1608 struct kaentry tentry;
1610 COUNT_REQ (ListEntry);
1611 if (code = InitAuthServ (&tt, LOCKREAD, this_op)) return code;
1612 code = check_auth (call, tt, 1, &caller);
1617 *index = NextBlock (tt, previous_index, &tentry, count);
1623 if (*index) { /* return name & inst of this entry */
1624 strncpy (name->name, tentry.userID.name, sizeof(name->name));
1625 strncpy (name->instance, tentry.userID.instance,
1626 sizeof(name->instance));
1629 strcpy (name->name, "\0");
1630 strcpy (name->instance, "\0");
1632 code = ubik_EndTrans(tt);
1637 ubik_AbortTrans(tt);
1641 static afs_int32 GetTicket (version, call, kvno, authDomain, aticket,
1642 sname, sinstance, atimes, oanswer)
1644 struct rx_call *call;
1650 ka_CBS *atimes; /* encrypted start & end time */
1654 struct ubik_trans *tt;
1655 struct ktc_encryptionKey tgskey;
1656 des_key_schedule schedule;
1658 char name[MAXKTCNAMELEN];
1659 char instance[MAXKTCNAMELEN];
1660 char cell[MAXKTCNAMELEN];
1662 struct kaentry caller;
1663 struct kaentry server;
1664 struct ktc_encryptionKey authSessionKey;
1665 struct ktc_encryptionKey sessionKey;
1667 char ticket[MAXKTCTICKETLEN];
1673 struct ka_getTicketTimes times;
1674 struct ka_getTicketAnswer *answer;
1676 COUNT_REQ (GetTicket);
1677 if (!name_instance_legal (sname, sinstance)) return KABADNAME;
1678 if (atimes->SeqLen != sizeof(times)) return KABADARGUMENT;
1679 if (code = InitAuthServ (&tt, LOCKREAD, this_op)) return code;
1681 export = import = 0;
1682 if ((strcmp (sname, KA_TGS_NAME) == 0) && (strcmp(sinstance, lrealm) != 0))
1684 if ((strlen(authDomain) > 0) && (strcmp (authDomain, lrealm) != 0))
1687 if (strlen(authDomain) == 0) authDomain = lrealm;
1688 code = ka_LookupKvno (tt, KA_TGS_NAME, authDomain, kvno, &tgskey);
1692 code = tkt_DecodeTicket (aticket->SeqBody, aticket->SeqLen, &tgskey,
1693 name, instance, cell,
1694 &authSessionKey, &host, &start, &expiration);
1695 if (code) {code = KANOAUTH; goto abort;}
1696 save_principal (tgsPrincipal, name, instance, cell);
1698 if (code = get_time (&now, 0, 0)) goto abort;
1700 code = tkt_CheckTimes (start, expiration, now);
1702 if (code == -1) code = RXKADEXPIRED;
1703 else code = KANOAUTH;
1706 code = des_key_sched (&authSessionKey, schedule);
1707 if (code) {code = KANOAUTH; goto abort;}
1708 celllen = strlen (cell);
1709 if (import && (celllen == 0)) {code = KABADTICKET; goto abort;}
1710 if (export && (celllen == 0)) strcpy (cell, lrealm);
1712 des_ecb_encrypt (atimes->SeqBody, ×, schedule, DECRYPT);
1713 times.start = ntohl(times.start);
1714 times.end = ntohl(times.end);
1715 code = tkt_CheckTimes (times.start, times.end, now);
1716 if (code < 0) {code = KABADREQUEST; goto abort;}
1719 strcpy (caller.userID.name, name);
1720 strcpy (caller.userID.instance, instance);
1721 caller.max_ticket_lifetime = htonl(MAXKTCTICKETLIFETIME);
1722 caller.flags = htonl(KAFNORMAL);
1723 caller.user_expiration = htonl(NEVERDATE);
1726 code = FindBlock(tt, name, instance, &to, &caller);
1727 if (code) goto abort;
1729 ka_PrintUserID ("GetTicket: User ", name, instance, " unknown.\n");
1735 /* get server's entry */
1736 code = FindBlock (tt, sname, sinstance, &to, &server);
1737 if (code) goto abort;
1738 if (to == 0) { /* entry not found */
1739 ka_PrintUserID ("GetTicket: Server ", sname, sinstance, " unknown.\n");
1743 save_principal (tgsServerPrincipal, sname, sinstance, 0);
1745 code = des_random_key (&sessionKey);
1746 if (code) {code = KANOKEYS; goto abort;}
1748 code = GetEndTime (times.start, times.end, expiration, &caller, &server,
1750 if (code) goto abort;
1752 code = tkt_MakeTicket (ticket, &ticketLen, &server.key,
1753 caller.userID.name, caller.userID.instance, cell,
1754 times.start, end, &sessionKey,
1755 rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))),
1756 server.userID.name, server.userID.instance);
1757 if (code) goto abort;
1761 code = KAANSWERTOOLONG;
1762 if (oanswer->MaxSeqLen <
1763 sizeof(struct ka_getTicketAnswer) - 5*MAXKTCNAMELEN) goto abort;
1765 answer = (struct ka_getTicketAnswer *)oanswer->SeqBody;
1766 bcopy (&sessionKey, &answer->sessionKey,
1767 sizeof(struct ktc_encryptionKey));
1768 answer->startTime = htonl(times.start);
1769 answer->endTime = htonl(end);
1770 answer->kvno = server.key_version;
1771 answer->ticketLen = htonl(ticketLen);
1773 { char *ans = answer->name; /* ptr to variable part of answer */
1776 /* space remaining */
1777 rem = oanswer->MaxSeqLen - (ans - oanswer->SeqBody);
1779 #define putstr(str) len = strlen (str)+1;\
1780 if (rem < len) goto abort;\
1782 ans += len; rem -= len
1789 if (rem < ticketLen) goto abort;
1790 bcopy (ticket, ans, ticketLen);
1791 oanswer->SeqLen = (ans - oanswer->SeqBody) + ticketLen;
1793 oanswer->SeqLen = round_up_to_ebs(oanswer->SeqLen);
1796 code = PrepareTicketAnswer
1797 (oanswer, /*challenge*/0, ticket, ticketLen, &sessionKey,
1798 times.start, end, &caller, &server, cell,
1799 KA_GETTICKET_ANS_LABEL);
1800 if (code) goto abort;
1803 code = KAINTERNALERROR;
1806 des_pcbc_encrypt (oanswer->SeqBody, oanswer->SeqBody, oanswer->SeqLen,
1807 schedule, &authSessionKey, ENCRYPT);
1808 code = ubik_EndTrans (tt);
1809 KALOG(name, instance, sname, sinstance, (import ? authDomain : NULL), call->conn->peer->host, LOG_GETTICKET);
1814 ubik_AbortTrans(tt);
1818 afs_int32 KAT_GetTicket_old (call, kvno, authDomain, aticket,
1819 sname, sinstance, atimes, oanswer)
1820 struct rx_call *call;
1826 ka_CBS *atimes; /* encrypted start & end time */
1831 sleep(1); /* strongly discourage this */
1832 code = GetTicket (0, call, kvno, authDomain, aticket,
1833 sname, sinstance, atimes, oanswer);
1835 osi_auditU( call, AFS_KAT_GetTicketOEvent, code, AUD_STR, sname, AUD_STR, sinstance, AUD_END);
1839 afs_int32 KAT_GetTicket (call, kvno, authDomain, aticket,
1840 sname, sinstance, atimes, oanswer)
1841 struct rx_call *call;
1847 ka_CBS *atimes; /* encrypted start & end time */
1852 code = GetTicket (1, call, kvno, authDomain, aticket,
1853 sname, sinstance, atimes, oanswer);
1854 osi_auditU( call, AFS_KAT_GetTicketEvent, code, AUD_STR, sname, AUD_STR, sinstance, AUD_END);
1858 afs_int32 KAM_GetStats (call, version, admin_accounts, statics, dynamics)
1859 struct rx_call *call;
1861 afs_int32 *admin_accounts;
1867 code = kamGetStats (call, version, admin_accounts, statics, dynamics);
1868 osi_auditU (call, AFS_KAM_GetStatEvent, code, AUD_END);
1872 afs_int32 kamGetStats (call, version, admin_accounts, statics, dynamics)
1873 struct rx_call *call;
1875 afs_int32 *admin_accounts;
1879 struct ubik_trans *tt;
1882 COUNT_REQ (GetStats);
1883 if (version != KAMAJORVERSION) return KAOLDINTERFACE;
1884 if (code = InitAuthServ( &tt, LOCKREAD, this_op)) return code;
1885 code = check_auth (call, tt, 1, &caller);
1888 ubik_AbortTrans(tt);
1892 *admin_accounts = ntohl(cheader.admin_accounts);
1893 /* bcopy ((char *)&cheader.stats, (char *)statics, sizeof(kasstats)); */
1894 /* these are stored in network byte order and must be copied */
1895 statics->allocs = ntohl(cheader.stats.allocs);
1896 statics->frees = ntohl(cheader.stats.frees);
1897 statics->cpws = ntohl(cheader.stats.cpws);
1898 #if KADBVERSION != 5
1899 check that the statistics command copies all the fields
1902 bcopy ((char *)&dynamic_statistics, (char *)dynamics, sizeof(kadstats));
1903 statics->minor_version = KAMINORVERSION;
1904 dynamics->minor_version = KAMINORVERSION;
1909 for (i=0; i<HASHSIZE; i++)
1910 if (cheader.nameHash[i]) used++;
1911 dynamics->hashTableUtilization = (used*10000 + HASHSIZE/2)/HASHSIZE;
1914 #if !defined(AFS_AIX_ENV) && !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV)
1916 /* Unfortunately, although aix_22 has a minimal compatibility
1917 * method of getting to some rusage fields (i.e. stime &
1918 * utime), the version that we have doesn't even have the
1919 * related include files needed for the aix vtimes() call; so
1920 * ignore this for aix till v3.1... */
1921 getrusage(RUSAGE_SELF, &ru);
1922 #if (KAMAJORVERSION>5)
1923 bcopy (&ru.ru_utime, &dynamics->utime, sizeof(struct katimeval));
1924 bcopy (&ru.ru_stime, &dynamics->stime, sizeof(struct katimeval));
1925 dynamics->dataSize = ru.ru_idrss;
1926 dynamics->stackSize = ru.ru_isrss;
1927 dynamics->pageFailts = ru.ru_majflt;
1929 dynamics->string_checks =
1930 (afs_int32)(1000.0 *
1931 ((ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1000000.0) +
1932 (ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1000000.0)));
1934 #endif /* AFS_AIX_ENV && AFS_HPUX_ENV && AFS_NT40_ENV */
1937 code = ubik_EndTrans (tt);
1941 afs_int32 KAM_GetPassword (call, name, password)
1942 struct rx_call *call;
1944 EncryptionKey *password;
1948 code = kamGetPassword (call, name, password);
1949 osi_auditU (call, AFS_KAM_GetPswdEvent, code, AUD_STR, name, AUD_END);
1953 afs_int32 kamGetPassword (call, name, password)
1954 struct rx_call *call;
1956 EncryptionKey *password;
1957 { int code = KANOAUTH;
1959 struct ubik_trans *tt;
1960 struct kaentry tentry;
1962 COUNT_REQ (GetPassword);
1964 if (!name_instance_legal (name, "")) return KABADNAME;
1965 /* only requests from this host work */
1966 if (rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))) != htonl(INADDR_LOOPBACK))
1968 if (code = InitAuthServ (&tt, LOCKREAD, this_op)) return code;
1970 /* this isn't likely to be used because of string to key problems, so since
1971 this is a temporary thing anyway, we'll use it here. */
1972 { extern char udpAuthPrincipal[256];
1973 save_principal (udpAuthPrincipal, name, 0, 0);
1975 get_time (0,0,0); /* update random value */
1976 code = FindBlock(tt, name, "", &to, &tentry);
1977 if (code) goto abort;
1982 ubik_AbortTrans(tt);
1986 bcopy (&tentry.key, password, sizeof (*password));
1987 code = ubik_EndTrans (tt);
1992 afs_int32 KAM_GetRandomKey (call, key)
1993 struct rx_call *call;
1998 code = kamGetRandomKey (call, key);
1999 osi_auditU (call, AFS_KAM_GetRndKeyEvent, code, AUD_END);
2003 afs_int32 kamGetRandomKey (call, key)
2004 struct rx_call *call;
2008 COUNT_REQ (GetRandomKey);
2009 if (code = AwaitInitialization()) return code;
2010 code = des_random_key (key);
2011 if (code) return KANOKEYS;
2015 afs_int32 KAM_Debug (call, version, checkDB, info)
2016 struct rx_call *call;
2018 int checkDB; /* start a transaction to examine DB */
2019 struct ka_debugInfo *info;
2023 code = kamDebug (call, version, checkDB, info);
2024 osi_auditU (call, AFS_KAM_DbgEvent, code, AUD_END);
2028 afs_int32 kamDebug (call, version, checkDB, info)
2029 struct rx_call *call;
2031 int checkDB; /* start a transaction to examine DB */
2032 struct ka_debugInfo *info;
2034 /* COUNT_REQ (Debug); */
2035 if (sizeof(struct kaentry) != sizeof(struct kaOldKeys))
2036 return KAINTERNALERROR;
2037 if (sizeof(struct ka_cpwRequest) % 8) return KAINTERNALERROR;
2038 if (version != KAMAJORVERSION) return KAOLDINTERFACE;
2040 bzero (info, sizeof(*info));
2042 info->minorVersion = KAMINORVERSION;
2043 info->host = dynamic_statistics.host;
2044 info->startTime = dynamic_statistics.start_time;
2046 #if (KAMAJORVERSION>5)
2052 info->noAuth = noAuthenticationRequired;
2054 info->dbVersion = ntohl(cheader.version);
2055 info->dbFreePtr = ntohl(cheader.freePtr);
2056 info->dbEofPtr = ntohl(cheader.eofPtr);
2057 info->dbKvnoPtr = ntohl(cheader.kvnoPtr);
2058 info->dbSpecialKeysVersion = ntohl(cheader.specialKeysVersion);
2060 info->dbHeaderRead = cheaderReadTime;
2061 info->lastTrans = lastTrans;
2063 lastOperation = "(Not Available)";
2064 strncpy (info->lastOperation, lastOperation, sizeof(info->lastOperation));
2065 strncpy (info->lastAuth, authPrincipal, sizeof(info->lastAuth));
2066 strncpy (info->lastTGS, tgsPrincipal, sizeof(info->lastTGS));
2067 strncpy (info->lastAdmin, adminPrincipal, sizeof(info->lastAdmin));
2068 strncpy (info->lastTGSServer, tgsServerPrincipal,
2069 sizeof(info->lastTGSServer));
2070 { extern char udpAuthPrincipal[256];
2071 extern char udptgsPrincipal[256];
2072 extern char udptgsServerPrincipal[256];
2074 strncpy (info->lastUAuth, udpAuthPrincipal, sizeof(info->lastUAuth));
2075 strncpy (info->lastUTGS, udptgsPrincipal, sizeof(info->lastUTGS));
2076 strncpy (info->lastUTGSServer, udptgsServerPrincipal,
2077 sizeof(info->lastUTGSServer));
2079 info->nextAutoCPW = nextAutoCPWTime;
2080 info->updatesRemaining = autoCPWUpdates - totalUpdates;
2081 ka_debugKeyCache (info);
2085 /* these are auxiliary routines. They don't do any Ubik stuff. They use
2086 * a tacked-on-the-side data file.
2087 * prob'ly ought to check the noauth flag.
2089 #define ABORTIF(A) {if(code= A){goto abort;}}
2090 afs_int32 KAM_Unlock (call, aname, ainstance, spare1, spare2, spare3, spare4)
2091 struct rx_call *call;
2094 afs_int32 spare1, spare2, spare3, spare4;
2097 struct ubik_trans *tt;
2100 struct kaentry tentry;
2103 if (!name_instance_legal (aname, ainstance)) {
2107 if (code = InitAuthServ (&tt, LOCKREAD, this_op))
2110 ABORTIF( check_auth (call, tt, 1, &caller) );
2111 ABORTIF( FindBlock(tt, aname, ainstance, &to, &tentry) );
2112 ABORTIF( (to == 0 ? KANOENT : 0) );
2114 kaux_write( to, 0, 0 ); /* zero failure counters at this offset */
2116 code = ubik_EndTrans(tt);
2117 KALOG(aname, ainstance, NULL, NULL, NULL, call->conn->peer->host, LOG_UNLOCK);
2122 ubik_AbortTrans(tt);
2125 osi_auditU(call, UnlockEvent, code, AUD_STR, aname, AUD_STR, ainstance, AUD_END);
2129 afs_int32 KAM_LockStatus (call, aname, ainstance, lockeduntil, spare1, spare2, spare3, spare4)
2130 struct rx_call *call;
2133 afs_int32 *lockeduntil;
2134 afs_int32 spare1, spare2, spare3, spare4;
2137 struct ubik_trans *tt;
2138 afs_int32 callerIndex;
2140 struct kaentry caller;
2141 struct kaentry tentry;
2144 COUNT_REQ (LockStatus);
2146 if (!name_instance_legal (aname, ainstance)) {
2150 if (code = InitAuthServ (&tt, LOCKREAD, this_op))
2153 if (code = check_auth (call, tt, 0, &callerIndex) )
2156 if (!noAuthenticationRequired && callerIndex) {
2157 if (karead (tt, callerIndex, &caller, sizeof(caller))) {
2161 /* if the user is checking his own entry or ADMIN then go ahead. */
2162 if ((strcmp (caller.userID.name, aname) != 0) &&
2163 !(ntohl(caller.flags) & KAFADMIN)) {
2169 if (code = FindBlock(tt, aname, ainstance, &to, &tentry) )
2177 temp = (unsigned char) tentry.misc_auth_bytes[LOCKTIME] ;
2179 *lockeduntil = kaux_islocked(to, (u_int) tentry.misc_auth_bytes[ATTEMPTS],
2182 code = ubik_EndTrans(tt);
2187 ubik_AbortTrans(tt);
2188 osi_auditU(call, LockStatusEvent, code, AUD_STR, aname, AUD_STR, ainstance, AUD_END);