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 <afsconfig.h>
11 #include <afs/param.h>
17 #ifdef HAVE_SYS_RESOURCE_H
18 # include <sys/resource.h>
21 #ifdef IGNORE_SOME_GCC_WARNINGS
22 # pragma GCC diagnostic warning "-Wdeprecated-declarations"
28 #include <hcrypto/des.h>
36 #include <rx/rxkad_convert.h>
37 #include <afs/cellconfig.h>
39 #include <afs/com_err.h>
40 #include <afs/afsutil.h>
41 #include <afs/audit.h>
47 #include "kauth_internal.h"
49 #include "kadatabase.h"
52 extern struct ubik_dbase *KA_dbase;
53 struct kaheader cheader;
54 Date cheaderReadTime; /* time cheader last read in */
55 extern struct afsconf_dir *KA_conf; /* for getting cell info */
59 char lrealm[MAXKTCREALMLEN];
61 #ifndef EXPIREPW /* password expiration default yes */
65 #ifndef AUTOCPWINTERVAL
66 #define AUTOCPWINTERVAL (24*3600)
68 #ifndef AUTOCPWUPDATES
69 #define AUTOCPWUPDATES 128
74 static afs_int32 autoCPWInterval;
75 static afs_int32 autoCPWUpdates;
77 static afs_int32 set_password(struct ubik_trans *tt, char *name,
79 struct ktc_encryptionKey *password,
80 afs_int32 kvno, afs_int32 caller);
81 static afs_int32 impose_reuse_limits(EncryptionKey *password,
82 struct kaentry *tentry);
83 static int create_user(struct ubik_trans *tt, char *name, char *instance,
84 struct ktc_encryptionKey *key, afs_int32 caller,
87 /* This routine is called whenever an RPC interface needs the time. It uses
88 the current time to randomize a 128 bit value that is used to change the
89 AuthServer Admin and TGS keys automatically. */
91 static Date nextAutoCPWTime = 0;
92 static afs_int32 totalUpdates = 0;
94 /* This routine is ostensibly to get the current time, but basically its job is
95 to periodically update a random number. It also periodically updates the
96 keys for the builtin servers. This is why it needs a transaction pointer
97 and returns an error code. If the caller is in a read transaction, the tt
98 ptr should be zero and the return code need not be checked. */
101 get_time(Date *timeP,
102 struct ubik_trans *tt, /* tt != 0: a write transaction */
103 int admin) /* the caller is an admin user */
105 /* random value used to change Admin & TGS keys, this is at risk during
106 * multi-threaded operation, but I think the consequences are fairly
108 static afs_uint32 random_value[4];
111 unsigned int bit, nbit;
115 gettimeofday(&time, NULL);
116 bit = (random_value[3] >> 31) & 1; /* get high bit of high word */
117 for (i = 0; i < 4; i++) {
118 nbit = random_value[i] >> 31;
119 random_value[i] = (random_value[i] << 1) + bit;
122 /* get 60ths from usec. This is all the real randomness there is. */
123 random_value[0] += time.tv_usec / 16667;
125 if (nextAutoCPWTime == 0) { /* initialize things */
126 nextAutoCPWTime = time.tv_sec + autoCPWInterval;
127 memcpy(&random_value[0], &time, 8);
128 memcpy(&random_value[2], &time, 8);
131 if ((++totalUpdates >= autoCPWUpdates) && tt && /* a write transaction */
132 ((admin && (time.tv_sec >= nextAutoCPWTime))
133 || (time.tv_sec >= nextAutoCPWTime + autoCPWInterval))) {
134 struct ktc_encryptionKey key;
135 char buf[4 * sizeof(key) + 1];
136 struct kaentry tentry;
138 char bob[KA_TIMESTR_LEN];
140 ka_timestr(time.tv_sec, bob, KA_TIMESTR_LEN);
141 es_Report("Auto CPW at %s\n", bob);
143 es_Report(" ... even though no ADMIN user\n");
145 code = FindBlock(tt, KA_ADMIN_NAME, KA_ADMIN_INST, &to, &tentry);
148 if (to) { /* check if auto cpw is disabled */
149 if (!(ntohl(tentry.flags) & KAFNOCPW)) {
150 memcpy(&key, &random_value[0], sizeof(key));
151 DES_set_odd_parity(ktc_to_cblock(&key));
153 set_password(tt, KA_ADMIN_NAME, KA_ADMIN_INST, &key, 0,
156 DES_init_random_number_generator(ktc_to_cblock(&key));
157 ka_ConvertBytes(buf, sizeof(buf), (char *)&key,
159 es_Report("New Admin key is %s\n", buf);
162 ("in get_time: set_password failed because: %d\n",
169 code = FindBlock(tt, KA_TGS_NAME, lrealm, &to, &tentry);
172 if (to) { /* check if auto cpw is disabled */
173 if (!(ntohl(tentry.flags) & KAFNOCPW)) {
174 memcpy(&key, &random_value[2], sizeof(key));
175 DES_set_odd_parity(ktc_to_cblock(&key));
176 code = set_password(tt, KA_TGS_NAME, lrealm, &key, 0, 0);
178 ka_ConvertBytes(buf, sizeof(buf), (char *)&key,
180 es_Report("New TGS key is %s\n", buf);
183 ("in get_time: set_password failed because: %s\n",
184 afs_error_message(code));
189 code = ka_FillKeyCache(tt); /* ensure in-core copy is uptodate */
193 nextAutoCPWTime = time.tv_sec + autoCPWInterval;
197 *timeP = time.tv_sec;
201 static int noAuthenticationRequired; /* global state */
202 static int recheckNoAuth; /* global state */
204 /* kaprocsInited is sort of a lock: during a transaction only one process runs
205 while kaprocsInited is false. */
207 static int kaprocsInited = 0;
209 /* This variable is protected by the kaprocsInited flag. */
211 static int (*rebuildDatabase) (struct ubik_trans *);
213 /* This is called to initialize the database */
216 initialize_database(struct ubik_trans *tt)
218 struct ktc_encryptionKey key;
221 gettimeofday((struct timeval *)&key, NULL); /* this is just a cheap seed key */
222 DES_set_odd_parity(ktc_to_cblock(&key));
223 DES_init_random_number_generator(ktc_to_cblock(&key));
224 if ((code = DES_new_random_key(ktc_to_cblock(&key)))
226 create_user(tt, KA_ADMIN_NAME, KA_ADMIN_INST, &key, 0,
227 KAFNORMAL | KAFNOSEAL | KAFNOTGS)))
229 if ((code = DES_new_random_key(ktc_to_cblock(&key)))
231 create_user(tt, KA_TGS_NAME, lrealm, &key, 0,
232 KAFNORMAL | KAFNOSEAL | KAFNOTGS)))
237 /* This routine handles initialization required by this module. The initFlags
238 parameter passes some information about the command line arguments. */
241 init_kaprocs(const char *lclpath, int initFlags)
244 struct ubik_trans *tt;
245 struct ktc_encryptionKey key;
250 return KAINTERNALERROR;
252 return KAINTERNALERROR;
253 code = afsconf_GetLocalCell(KA_conf, lrealm, sizeof(lrealm));
255 printf("** Can't determine local cell name!\n");
258 ucstring(lrealm, lrealm, sizeof(lrealm));
262 noAuthenticationRequired = 1;
266 noAuthenticationRequired = afsconf_GetNoAuthFlag(KA_conf);
267 if (noAuthenticationRequired)
268 printf("Running server with security disabled\n");
271 autoCPWInterval = 10;
274 autoCPWInterval = AUTOCPWINTERVAL;
275 autoCPWUpdates = AUTOCPWUPDATES;
278 init_kadatabase(initFlags);
279 rebuildDatabase = initialize_database;
281 if ((code = InitAuthServ(&tt, LOCKREAD, 0))) {
282 printf("init_kaprocs: InitAuthServ failed: code = %d\n", code);
285 code = ka_LookupKey(tt, KA_ADMIN_NAME, KA_ADMIN_INST, &kvno, &key);
289 ("init_kaprocs: ka_LookupKey (code = %d) DB not initialized properly?\n",
293 DES_init_random_number_generator(ktc_to_cblock(&key));
295 code = ubik_EndTrans(tt);
297 printf("init_kaprocs: ubik_EndTrans failed: code = %d\n", code);
301 kaux_opendb((char *)lclpath);/* aux database stores failure counters */
302 rebuildDatabase = 0; /* only do this during init */
307 /* These variable are for returning debugging info about the state of the
308 server. If they get trashed during multi-threaded operation it doesn't
311 /* this is global so COUNT_REQ in krb_udp.c can refer to it. */
312 char *lastOperation = 0; /* name of last operation */
313 static Date lastTrans; /* time of last transaction */
315 static char adminPrincipal[256];
316 static char authPrincipal[256];
317 static char tgsPrincipal[256];
318 static char tgsServerPrincipal[256];
321 save_principal(char *p, char *n, char *i, char *c)
331 if (i && strlen(i)) {
341 if (c && strlen(c)) {
353 check_auth(struct rx_call *call,
354 struct ubik_trans *at,
355 int admin, /* require caller to be ADMIN */
356 afs_int32 *acaller_id)
359 char name[MAXKTCNAMELEN];
360 char instance[MAXKTCNAMELEN];
361 char cell[MAXKTCREALMLEN];
363 Date expiration; /* checked by Security Module */
364 struct kaentry tentry;
371 noAuthenticationRequired = afsconf_GetNoAuthFlag(KA_conf);
373 si = rx_SecurityClassOf(rx_ConnectionOf(call));
374 if (si == RX_SECIDX_VAB) {
375 printf("No support for VAB security module yet.\n");
377 } else if (si == RX_SECIDX_NULL) {
380 } else if (si != RX_SECIDX_KAD) {
381 es_Report("Unknown security index %d\n", si);
386 rxkad_GetServerInfo(rx_ConnectionOf(call), &level, &expiration, name,
387 instance, cell, &kvno);
391 if (level != rxkad_crypt) {
392 es_Report("Incorrect security level = %d\n", level);
397 if (!name_instance_legal(name, instance))
401 ("Authorization rejected because we don't understand intercell stuff yet: ",
403 printf("@%s\n", cell);
407 code = FindBlock(at, name, instance, acaller_id, &tentry);
410 if (*acaller_id == 0) {
411 ka_PrintUserID("User ", name, instance, " unknown.\n");
414 save_principal(adminPrincipal, name, instance, 0);
417 if (!(ntohl(tentry.flags) & KAFADMIN)) {
418 if (noAuthenticationRequired) {
419 ka_PrintUserID("Authorization approved for ", name, instance,
420 " because there is no authentication required\n");
421 osi_auditU(call, UnAuthEvent, code, AUD_STR, name, AUD_STR,
422 instance, AUD_STR, cell, AUD_END);
425 ka_PrintUserID("User ", name, instance, " is not ADMIN.\n");
428 osi_auditU(call, UseOfPrivilegeEvent, code, AUD_STR, name, AUD_STR,
429 instance, AUD_STR, cell, AUD_END);
434 if (noAuthenticationRequired) {
436 ("Caller w/o authorization approved no authentication required\n");
437 osi_auditU(call, UnAuthEvent, code, AUD_STR, name, AUD_STR, instance,
438 AUD_STR, cell, AUD_END);
441 return code; /* no auth info */
445 AwaitInitialization(void)
448 while (!kaprocsInited) {
451 else if (time(0) - start > 5)
458 /* This is called by every RPC interface to create a Ubik transaction and read
459 the database header into core */
462 InitAuthServ(struct ubik_trans **tt,
463 int lock, /* indicate read/write transaction */
464 int *this_op) /* opcode of RPC proc, for COUNT_ABO */
467 afs_int32 start = 0; /* time started waiting for quorum */
468 float wait = 0.91; /* start waiting for 1 second */
470 /* Wait for server initialization to finish if not during init_kaprocs */
472 if ((code = AwaitInitialization()))
475 for (code = UNOQUORUM; code == UNOQUORUM;) {
476 if (lock == LOCKREAD)
477 code = ubik_BeginTransReadAny(KA_dbase, UBIK_READTRANS, tt);
479 code = ubik_BeginTrans(KA_dbase, UBIK_WRITETRANS, tt);
480 if (code == UNOQUORUM) { /* no quorum elected */
484 int delay = time(0) - start;
485 if (this_op) { /* punt quickly, if RPC call */
488 } else { /* more patient during init. */
493 printf("Waiting for quorum election.\n");
496 IOMGR_Sleep((int)wait);
501 if ((code = ubik_SetLock(*tt, 1, 1, lock))) {
504 ubik_AbortTrans(*tt);
507 /* check that dbase is initialized and setup cheader */
508 if (lock == LOCKREAD) {
509 /* init but don't fix because this is read only */
510 code = CheckInit(*tt, 0);
512 ubik_AbortTrans(*tt); /* abort, since probably I/O error */
513 /* we did the check under a ReadAny transaction, but now, after
514 * getting a write transaction (and thus some real guarantees
515 * about what databases are really out there), we will check again
516 * in CheckInit before nuking the database. Since this may now get
517 * a UNOQUORUM we'll just do this from the top.
519 if ((code = InitAuthServ(tt, LOCKWRITE, this_op)))
521 if ((code = ubik_EndTrans(*tt)))
524 /* now open the read transaction that was originally requested. */
525 return InitAuthServ(tt, lock, this_op);
528 if ((code = CheckInit(*tt, rebuildDatabase))) {
531 ubik_AbortTrans(*tt);
536 ka_FillKeyCache(*tt); /* ensure in-core copy is uptodate */
540 /* returns true if name is specially known by AuthServer */
543 special_name(char *name, char *instance)
546 return ((!strcmp(name, KA_TGS_NAME) && !strcmp(instance, lrealm))
547 || (strcmp(name, KA_ADMIN_NAME) == 0));
551 create_user(struct ubik_trans *tt, char *name, char *instance,
552 struct ktc_encryptionKey *key, afs_int32 caller,
557 struct kaentry tentry;
558 afs_int32 maxLifetime;
560 code = FindBlock(tt, name, instance, &to, &tentry);
564 return KAEXIST; /* name already exists, we fail */
566 to = AllocBlock(tt, &tentry);
570 /* otherwise we have a block */
571 strncpy(tentry.userID.name, name, sizeof(tentry.userID.name));
572 strncpy(tentry.userID.instance, instance, sizeof(tentry.userID.instance));
573 tentry.flags = htonl(flags);
574 if (special_name(name, instance)) { /* this overrides key & version */
575 tentry.flags = htonl(ntohl(tentry.flags) | KAFSPECIAL);
576 tentry.key_version = htonl(-1); /* don't save this key */
577 if ((code = ka_NewKey(tt, to, &tentry, key)))
580 memcpy(&tentry.key, key, sizeof(tentry.key));
581 tentry.key_version = htonl(0);
583 tentry.user_expiration = htonl(NEVERDATE);
584 code = get_time(&tentry.modification_time, tt, 1);
588 /* time and addr of entry for guy changing this entry */
589 tentry.modification_time = htonl(tentry.modification_time);
590 tentry.modification_id = htonl(caller);
591 tentry.change_password_time = tentry.modification_time;
593 if (strcmp(name, KA_TGS_NAME) == 0)
594 maxLifetime = MAXKTCTICKETLIFETIME;
595 else if (strcmp(name, KA_ADMIN_NAME) == 0)
596 maxLifetime = 10 * 3600;
597 else if (strcmp(name, AUTH_SUPERUSER) == 0)
598 maxLifetime = 100 * 3600;
600 maxLifetime = 25 * 3600; /* regular users */
601 tentry.max_ticket_lifetime = htonl(maxLifetime);
603 code = ThreadBlock(tt, to, &tentry);
607 /* Put actual stub routines here */
610 SKAM_CreateUser(struct rx_call *call, char *aname, char *ainstance,
611 EncryptionKey ainitpw)
615 code = kamCreateUser(call, aname, ainstance, ainitpw);
616 osi_auditU(call, AFS_KAM_CrUserEvent, code, AUD_STR, aname, AUD_STR,
623 kamCreateUser(struct rx_call *call, char *aname, char *ainstance,
624 EncryptionKey ainitpw)
627 struct ubik_trans *tt;
628 afs_int32 caller; /* Disk offset of caller's entry */
630 COUNT_REQ(CreateUser);
631 if (!DES_check_key_parity(EncryptionKey_to_cblock(&ainitpw)) ||
632 DES_is_weak_key(EncryptionKey_to_cblock(&ainitpw)))
634 if (!name_instance_legal(aname, ainstance))
636 if ((code = InitAuthServ(&tt, LOCKWRITE, this_op)))
638 code = check_auth(call, tt, 1, &caller);
644 code = create_user(tt, aname, ainstance, EncryptionKey_to_ktc(&ainitpw), caller, KAFNORMAL);
650 code = ubik_EndTrans(tt);
651 KALOG(aname, ainstance, NULL, NULL, NULL,
652 rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))), LOG_CRUSER);
657 SKAA_ChangePassword(struct rx_call *call, char *aname, char *ainstance,
658 ka_CBS *arequest, ka_BBS *oanswer)
662 code = ChangePassWord(call, aname, ainstance, arequest, oanswer);
663 osi_auditU(call, AFS_KAA_ChPswdEvent, code, AUD_STR, aname, AUD_STR,
669 ChangePassWord(struct rx_call *call, char *aname, char *ainstance,
670 ka_CBS *arequest, ka_BBS *oanswer)
673 struct ubik_trans *tt;
674 afs_int32 to; /* offset of block */
675 struct kaentry tentry;
676 struct ka_cpwRequest request; /* request after decryption */
677 char *answer; /* where answer is to be put */
678 int answer_len; /* length of answer packet */
679 afs_int32 kvno; /* requested key version number */
680 DES_key_schedule user_schedule; /* key schedule for user's key */
681 Date request_time; /* time request originated */
683 COUNT_REQ(ChangePassword);
684 if (!name_instance_legal(aname, ainstance))
686 if (strcmp(ainstance, KA_ADMIN_NAME) == 0)
688 if ((code = InitAuthServ(&tt, LOCKWRITE, this_op)))
691 code = FindBlock(tt, aname, ainstance, &to, &tentry);
695 if (to == 0) { /* no such user */
699 if (ntohl(tentry.flags) & KAFNOCPW) {
704 /* decrypt request w/ user password */
705 if ((code = DES_key_sched(ktc_to_cblock(&tentry.key), &user_schedule)))
706 es_Report("In KAChangePassword: key_sched returned %d\n", code);
707 DES_pcbc_encrypt(arequest->SeqBody, &request,
708 min(arequest->SeqLen, sizeof(request)), &user_schedule,
709 ktc_to_cblockptr(&tentry.key), DECRYPT);
711 /* validate the request */
712 request_time = ntohl(request.time); /* reorder date */
713 kvno = ntohl(request.kvno);
714 if (check_ka_skew(request_time, time(NULL), KTC_TIME_UNCERTAINTY) ||
715 strncmp(request.label, KA_CPW_REQ_LABEL, sizeof(request.label)) ||
716 request.spare || kvno > MAXKAKVNO) { /* these are reserved */
721 /* check to see if the new password was used before, or if there has
722 * not been sufficient time since the last password change
724 code = impose_reuse_limits(ktc_to_EncryptionKey(&request.newpw), &tentry);
729 /* Create the Answer Packet */
730 answer_len = sizeof(Date) + KA_LABELSIZE;
731 if (oanswer->MaxSeqLen < answer_len) {
732 code = KAANSWERTOOLONG;
735 oanswer->SeqLen = answer_len;
736 answer = oanswer->SeqBody;
737 request.time = htonl(request_time + 1);
738 memcpy(answer, (char *)&request.time, sizeof(Date));
739 answer += sizeof(Date);
740 memcpy(answer, KA_CPW_ANS_LABEL, KA_LABELSIZE);
742 DES_pcbc_encrypt(oanswer->SeqBody, oanswer->SeqBody, answer_len,
743 &user_schedule, ktc_to_cblockptr(&tentry.key), ENCRYPT);
745 code = set_password(tt, aname, ainstance, &request.newpw, kvno, 0);
751 cheader.stats.cpws = htonl(ntohl(cheader.stats.cpws) + 1);
753 kawrite(tt, DOFFSET(0, &cheader, &cheader.stats.cpws),
754 (char *)&cheader.stats.cpws, sizeof(afs_int32));
760 code = ubik_EndTrans(tt);
770 impose_reuse_limits(EncryptionKey *password, struct kaentry *tentry)
778 if (!tentry->pwsums[0] && npwSums > 1 && !tentry->pwsums[1])
779 return 0; /* password reuse limits not in effect */
781 code = get_time(&now, 0, 0);
785 if ((now - ntohl(tentry->change_password_time)) < MinHours * 60 * 60)
788 if (!memcmp(password, &(tentry->key), sizeof(EncryptionKey)))
791 code = ka_KeyCheckSum((char *)password, &newsum);
795 newsum = newsum & 0x000000ff;
796 for (i = 0; i < npwSums; i++) {
797 if (newsum == tentry->pwsums[i])
806 set_password(struct ubik_trans *tt, char *name, char *instance,
807 struct ktc_encryptionKey *password, afs_int32 kvno, afs_int32 caller)
810 afs_int32 to; /* offset of block */
811 struct kaentry tentry;
817 code = FindBlock(tt, name, instance, &to, &tentry);
821 return KANOENT; /* no such user */
823 /* if password reuse limits in effect, set the checksums, the hard way */
824 if (!tentry.pwsums[0] && npwSums > 1 && !tentry.pwsums[1]) {
825 /* do nothing, no limits */ ;
827 code = ka_KeyCheckSum((char *)&(tentry.key), &newsum);
830 for (i = npwSums - 1; i; i--)
831 tentry.pwsums[i] = tentry.pwsums[i - 1];
832 tentry.pwsums[0] = newsum & 0x000000ff;
836 if (special_name(name, instance)) { /* set key over rides key_version */
837 tentry.flags = htonl(ntohl(tentry.flags) | KAFSPECIAL);
838 if ((code = ka_NewKey(tt, to, &tentry, password)))
841 memcpy(&tentry.key, password, sizeof(tentry.key));
843 kvno = ntohl(tentry.key_version);
844 if ((kvno < 1) || (kvno >= MAXKAKVNO))
849 tentry.key_version = htonl((afs_int32) kvno); /* requested key version */
854 /* no-write prevents recursive call to set_password by AuthCPW code. */
855 code = get_time(&now, 0, 0);
859 tentry.modification_time = htonl(now);
860 tentry.modification_id = htonl(caller);
863 tentry.change_password_time = htonl(now);
865 if ((code = kawrite(tt, to, (char *) &tentry, sizeof(tentry))))
871 SKAM_SetPassword(struct rx_call *call, char *aname, char *ainstance,
872 afs_int32 akvno, EncryptionKey apassword)
876 code = kamSetPassword(call, aname, ainstance, akvno, apassword);
877 osi_auditU(call, AFS_KAM_SetPswdEvent, code, AUD_STR, aname, AUD_STR,
883 kamSetPassword(struct rx_call *call, char *aname, char *ainstance,
884 afs_int32 akvno, EncryptionKey apassword)
887 struct ubik_trans *tt;
888 afs_int32 caller; /* Disk offset of caller's entry */
889 struct kaentry tentry;
891 COUNT_REQ(SetPassword);
892 if (akvno > MAXKAKVNO)
893 return KABADARGUMENT;
894 if (!DES_check_key_parity(EncryptionKey_to_cblock(&apassword)) ||
895 DES_is_weak_key(EncryptionKey_to_cblock(&apassword)))
898 if (!name_instance_legal(aname, ainstance))
900 if ((code = InitAuthServ(&tt, LOCKWRITE, this_op)))
902 code = check_auth(call, tt, 0, &caller);
906 if ((code = karead(tt, caller, (char *)&tentry, sizeof(tentry)))) {
910 /* if the user is changing his own password or ADMIN then go ahead. */
911 if ((strcmp(tentry.userID.name, aname) == 0)
912 && (strcmp(tentry.userID.instance, ainstance) == 0)) {
913 if (ntohl(tentry.flags) & KAFNOCPW)
916 code = impose_reuse_limits(&apassword, &tentry);
919 set_password(tt, aname, ainstance, EncryptionKey_to_ktc(&apassword), akvno, 0);
921 } else if (ntohl(tentry.flags) & KAFADMIN) {
922 code = set_password(tt, aname, ainstance, EncryptionKey_to_ktc(&apassword), akvno, caller);
928 code = ubik_EndTrans(tt);
929 KALOG(aname, ainstance, NULL, NULL, NULL,
930 rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))), LOG_CHPASSWD);
940 CoerseLifetime(Date start, Date end)
942 unsigned char kerberosV4Life;
943 kerberosV4Life = time_to_life(start, end);
944 end = life_to_time(start, kerberosV4Life);
949 GetEndTime(Date start, /* start time of ticket */
950 Date reqEnd, /* requested end time */
951 Date expiration, /* authorizing ticket's expiration */
952 struct kaentry *caller,
953 struct kaentry *server,
954 Date *endP) /* actual end time */
960 if (ntohl(caller->flags) & KAFNOTGS)
961 return KABADUSER; /* no new tickets for this user */
962 if (expiration && (ntohl(server->flags) & KAFNOSEAL))
963 return KABADSERVER; /* can't be target of GetTicket req */
965 expiration = NEVERDATE;
967 cExp = ntohl(caller->user_expiration);
968 sExp = ntohl(server->user_expiration);
973 cLife = start + ntohl(caller->max_ticket_lifetime);
974 sLife = start + ntohl(server->max_ticket_lifetime);
976 umin(umin(reqEnd, expiration),
977 umin(umin(cLife, sLife), umin(cExp, sExp)));
978 end = CoerseLifetime(start, end);
984 PrepareTicketAnswer(ka_BBS *oanswer, afs_int32 challenge, char *ticket,
985 afs_int32 ticketLen, struct ktc_encryptionKey *sessionKey,
986 Date start, Date end, struct kaentry *caller,
987 struct kaentry *server, char *cell, char *label)
990 struct ka_ticketAnswer *answer;
993 code = KAANSWERTOOLONG;
994 if (oanswer->MaxSeqLen <
995 sizeof(struct ka_ticketAnswer) - 5 * MAXKTCNAMELEN - MAXKTCTICKETLEN +
999 answer = (struct ka_ticketAnswer *)oanswer->SeqBody;
1000 answer->challenge = htonl(challenge);
1001 memcpy(&answer->sessionKey, sessionKey, sizeof(struct ktc_encryptionKey));
1002 answer->startTime = htonl(start);
1003 answer->endTime = htonl(end);
1004 answer->kvno = server->key_version;
1005 answer->ticketLen = htonl(ticketLen);
1008 char *ans = answer->name; /* pointer to variable part */
1009 int rem; /* space remaining */
1010 int len; /* macro temp. */
1012 rem = oanswer->MaxSeqLen - (ans - oanswer->SeqBody);
1014 #define putstr(str) len = strlen (str)+1;\
1015 if (rem < len) return code;\
1017 ans += len; rem -= len
1018 putstr(caller->userID.name);
1019 putstr(caller->userID.instance);
1021 putstr(server->userID.name);
1022 putstr(server->userID.instance);
1023 if (rem < ticketLen + KA_LABELSIZE)
1025 memcpy(ans, ticket, ticketLen);
1028 memcpy(ans, label, KA_LABELSIZE);
1030 memset(ans, 0, KA_LABELSIZE);
1031 ans += KA_LABELSIZE;
1032 oanswer->SeqLen = (ans - oanswer->SeqBody);
1035 answer->cksum = htonl(cksum);
1036 oanswer->SeqLen = round_up_to_ebs(oanswer->SeqLen);
1037 if (oanswer->SeqLen > oanswer->MaxSeqLen)
1042 /* This is used to get a ticket granting ticket or an admininstration ticket.
1043 These two specific, built-in servers are special cases, which require the
1044 client's key as an additional security precaution. The GetTicket operation
1045 is normally disabled for these two principals. */
1048 Authenticate(int version, struct rx_call *call, char *aname, char *ainstance,
1049 Date start, Date end, ka_CBS *arequest, ka_BBS *oanswer)
1052 struct ubik_trans *tt;
1053 afs_int32 to; /* offset of block */
1055 struct kaentry server; /* entry for desired server */
1056 struct ka_gettgtRequest request; /* request after decryption */
1057 int tgt, adm; /* type of request */
1058 char *sname; /* principal of server */
1060 char ticket[MAXKTCTICKETLEN]; /* our copy of the ticket */
1062 struct ktc_encryptionKey sessionKey; /* we have to invent a session key */
1063 char *answer; /* where answer is to be put */
1064 int answer_len; /* length of answer packet */
1065 Date answer_time; /* 1+ request time in network order */
1066 afs_int32 temp; /* for htonl conversions */
1067 DES_key_schedule user_schedule; /* key schedule for user's key */
1068 afs_int32 tgskvno; /* key version of service key */
1069 struct ktc_encryptionKey tgskey; /* service key for encrypting ticket */
1071 afs_uint32 pwexpires;
1073 COUNT_REQ(Authenticate);
1074 if (!name_instance_legal(aname, ainstance))
1076 if ((code = InitAuthServ(&tt, LOCKREAD, this_op)))
1078 get_time(&now, 0, 0);
1080 sname = sinst = NULL;
1082 code = FindBlock(tt, aname, ainstance, &to, &tentry);
1086 if (to == 0) { /* no such user */
1091 /* have to check for locked before verifying the password, otherwise all
1092 * KALOCKED means is "yup, you guessed the password all right, now wait a
1093 * few minutes and we'll let you in"
1096 (to, (u_int) tentry.misc_auth_bytes[ATTEMPTS],
1097 (afs_uint32) tentry.misc_auth_bytes[LOCKTIME] << 9)) {
1103 save_principal(authPrincipal, aname, ainstance, 0);
1105 /* decrypt request w/ user password */
1106 if ((code = DES_key_sched(ktc_to_cblock(&tentry.key), &user_schedule)))
1107 es_Report("In KAAuthenticate: key_sched returned %d\n", code);
1108 DES_pcbc_encrypt(arequest->SeqBody, &request,
1109 min(arequest->SeqLen, sizeof(request)), &user_schedule,
1110 ktc_to_cblockptr(&tentry.key), DECRYPT);
1112 request.time = ntohl(request.time); /* reorder date */
1113 tgt = !strncmp(request.label, KA_GETTGT_REQ_LABEL, sizeof(request.label));
1114 adm = !strncmp(request.label, KA_GETADM_REQ_LABEL, sizeof(request.label));
1115 if (!(tgt || adm)) {
1116 kaux_inc(to, ((unsigned char)tentry.misc_auth_bytes[LOCKTIME]) << 9);
1117 code = KABADREQUEST;
1120 kaux_write(to, 0, 0); /* reset counters */
1123 if (!tentry.misc_auth_bytes[EXPIRES]) {
1124 /* 0 in the database means never, but 0 on the network means today */
1125 /* 255 on the network means "long time, maybe never" */
1128 pwexpires = tentry.misc_auth_bytes[EXPIRES];
1131 ntohl(tentry.change_password_time) + 24 * 60 * 60 * pwexpires;
1132 if (adm) { /* provide a little slack for admin ticket */
1133 pwexpires += 30 * 24 * 60 * 60; /* 30 days */
1135 if (pwexpires < now) {
1139 pwexpires = (pwexpires - now) / (24 * 60 * 60);
1140 if (pwexpires > 255)
1144 #endif /* EXPIREPW */
1146 if (check_ka_skew(request.time, now, KTC_TIME_UNCERTAINTY)) {
1150 sname = (tgt ? KA_TGS_NAME : KA_ADMIN_NAME);
1151 sinst = (tgt ? lrealm : KA_ADMIN_INST);
1152 code = FindBlock(tt, sname, sinst, &to, &server);
1160 tgskvno = ntohl(server.key_version);
1161 memcpy(&tgskey, &server.key, sizeof(tgskey));
1163 code = DES_new_random_key(ktc_to_cblock(&sessionKey));
1169 code = GetEndTime(start, end, 0 /*!GetTicket */ , &tentry, &server, &end);
1174 tkt_MakeTicket(ticket, &ticketLen, &tgskey, aname, ainstance, "",
1175 start, end, &sessionKey,
1176 rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))), sname,
1184 ticketLen + sizeof(Date) + sizeof(struct ktc_encryptionKey) +
1185 2 * sizeof(afs_int32) + KA_LABELSIZE;
1186 answer_len = round_up_to_ebs(answer_len);
1187 if (answer_len > oanswer->MaxSeqLen) {
1188 code = KAANSWERTOOLONG;
1191 oanswer->SeqLen = answer_len;
1192 answer = oanswer->SeqBody;
1193 answer_time = htonl(request.time + 1);
1194 memcpy(answer, (char *)&answer_time, sizeof(Date));
1195 answer += sizeof(Date);
1196 memcpy(answer, (char *)&sessionKey, sizeof(struct ktc_encryptionKey));
1197 answer += sizeof(struct ktc_encryptionKey);
1198 temp = htonl(tgskvno);
1199 memcpy(answer, (char *)&temp, sizeof(afs_int32));
1200 answer += sizeof(afs_int32);
1201 temp = htonl(ticketLen);
1202 memcpy(answer, (char *)&temp, sizeof(afs_int32));
1203 answer += sizeof(afs_int32);
1204 memcpy(answer, ticket, ticketLen);
1205 answer += ticketLen;
1206 memcpy(answer, (tgt ? KA_GETTGT_ANS_LABEL : KA_GETADM_ANS_LABEL),
1212 PrepareTicketAnswer(oanswer, request.time + 1, ticket, ticketLen,
1213 &sessionKey, start, end, &tentry, &server, "",
1214 (tgt ? KA_GETTGT_ANS_LABEL :
1215 KA_GETADM_ANS_LABEL));
1220 && oanswer->SeqLen < oanswer->MaxSeqLen + sizeof(afs_int32)) {
1221 temp = pwexpires << 24; /* move it into the high byte */
1222 pwexpires = htonl(temp);
1224 memcpy((char *)oanswer->SeqBody + oanswer->SeqLen, &pwexpires,
1226 oanswer->SeqLen += sizeof(afs_int32);
1227 oanswer->SeqLen = round_up_to_ebs(oanswer->SeqLen);
1228 if (oanswer->SeqLen > oanswer->MaxSeqLen) {
1229 code = KAANSWERTOOLONG;
1233 #endif /* EXPIREPW */
1237 code = KAINTERNALERROR;
1240 DES_pcbc_encrypt(oanswer->SeqBody, oanswer->SeqBody, oanswer->SeqLen,
1241 &user_schedule, ktc_to_cblockptr(&tentry.key), ENCRYPT);
1242 code = ubik_EndTrans(tt);
1243 KALOG(aname, ainstance, sname, sinst, NULL,
1244 rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))), LOG_AUTHENTICATE);
1249 ubik_AbortTrans(tt);
1250 KALOG(aname, ainstance, sname, sinst, NULL,
1251 rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))), LOG_AUTHFAILED);
1256 SKAA_Authenticate_old(struct rx_call *call, char *aname, char *ainstance,
1257 Date start, Date end, ka_CBS *arequest,
1262 IOMGR_Sleep(1); /* discourage use of this mechanism */
1264 Authenticate(0, call, aname, ainstance, start, end, arequest,
1266 osi_auditU(call, AFS_KAA_AuthOEvent, code, AUD_STR, aname, AUD_STR,
1267 ainstance, AUD_END);
1273 SKAA_Authenticate(struct rx_call *call, char *aname, char *ainstance,
1274 Date start, Date end, ka_CBS *arequest, ka_BBS *oanswer)
1279 Authenticate(1, call, aname, ainstance, start, end, arequest,
1281 osi_auditU(call, AFS_KAA_AuthEvent, code, AUD_STR, aname, AUD_STR,
1282 ainstance, AUD_END);
1288 SKAA_AuthenticateV2(struct rx_call *call, char *aname, char *ainstance,
1289 Date start, Date end, ka_CBS *arequest, ka_BBS *oanswer)
1294 Authenticate(2, call, aname, ainstance, start, end, arequest,
1296 osi_auditU(call, AFS_KAA_AuthEvent, code, AUD_STR, aname, AUD_STR,
1297 ainstance, AUD_END);
1303 SKAM_SetFields(struct rx_call *call,
1308 afs_int32 alifetime,
1309 afs_int32 amaxAssociates,
1310 afs_uint32 misc_auth_bytes, /* 4 bytes, each 0 means unspecified */
1316 kamSetFields(call, aname, ainstance, aflags, aexpiration, alifetime,
1317 amaxAssociates, misc_auth_bytes, spare2);
1318 osi_auditU(call, AFS_KAM_SetFldEvent, code, AUD_STR, aname, AUD_STR,
1319 ainstance, AUD_LONG, aflags, AUD_DATE, aexpiration, AUD_LONG,
1320 alifetime, AUD_LONG, amaxAssociates, AUD_END);
1325 kamSetFields(struct rx_call *call,
1330 afs_int32 alifetime,
1331 afs_int32 amaxAssociates,
1332 afs_uint32 misc_auth_bytes, /* 4 bytes, each 0 means unspecified */
1337 struct ubik_trans *tt;
1339 afs_int32 tentry_offset; /* offset of entry */
1340 struct kaentry tentry;
1341 unsigned char newvals[4];
1343 COUNT_REQ(SetFields);
1346 return KABADARGUMENT; /* not supported yet... */
1348 /* make sure we're supposed to do something */
1349 if (!(aflags || aexpiration || alifetime || (amaxAssociates >= 0)
1351 || ((aflags & ~KAFNORMAL) & ~KAF_SETTABLE_FLAGS))
1352 return KABADARGUMENT; /* arguments no good */
1353 if (!name_instance_legal(aname, ainstance))
1355 if ((code = InitAuthServ(&tt, LOCKWRITE, this_op)))
1357 code = check_auth(call, tt, 1, &caller);
1362 code = FindBlock(tt, aname, ainstance, &tentry_offset, &tentry);
1365 if (tentry_offset == 0) { /* no such user */
1369 if ((ntohl(tentry.flags) & KAFNORMAL) == 0)
1370 return KAINTERNALERROR;
1372 /* Keep track of the total number of admin accounts. This way we can
1373 * update database without any admin privilege initially */
1374 if ((aflags & KAFADMIN) != (ntohl(tentry.flags) & KAFADMIN)) {
1375 /* if admin state is changing */
1377 if (ntohl(tentry.flags) & KAFADMIN)
1381 if ((code = update_admin_count(tt, delta)))
1385 htonl((ntohl(tentry.flags) & ~KAF_SETTABLE_FLAGS) | aflags);
1387 if ((code = get_time(&now, tt, 1)))
1390 tentry.user_expiration = htonl(aexpiration);
1391 if (!ntohl(tentry.change_password_time)) {
1392 tentry.change_password_time = htonl(now);
1396 tentry.max_ticket_lifetime = htonl(alifetime);
1398 #ifndef NOPWCONTROLS
1400 * We've packed a bunch of bytes into a long for backward compatibility.
1401 * These include password expiration time, and some failed login limits
1402 * counters. Now let's unpack them and stick them into the
1403 * kaentry struct. All the bytes have values in the range
1404 * 1..255, else they were not specified in the interface, and are
1406 * In the case of password expiration times, 1 means password never
1407 * expires (==>0), 2 means password only lives for one day (==>1),
1410 if (misc_auth_bytes) {
1411 unpack_long(misc_auth_bytes, newvals);
1412 if (newvals[EXPIRES]) {
1413 tentry.misc_auth_bytes[EXPIRES] = newvals[EXPIRES] - 1;
1416 if (newvals[REUSEFLAGS]) {
1417 if (newvals[REUSEFLAGS] & KA_REUSEPW)
1418 memset(tentry.pwsums, 0, KA_NPWSUMS);
1419 else if ((newvals[REUSEFLAGS] & KA_NOREUSEPW)
1420 && !tentry.pwsums[0])
1421 tentry.pwsums[0] = 0xff;
1424 if (newvals[ATTEMPTS]) {
1425 tentry.misc_auth_bytes[ATTEMPTS] = newvals[ATTEMPTS] - 1;
1427 if (newvals[LOCKTIME]) {
1428 tentry.misc_auth_bytes[LOCKTIME] = newvals[LOCKTIME] - 1;
1431 tentry.misc_auth_bytes = htonl(tentry.misc_auth_bytes);
1434 #endif /* NOPWCONTROLS */
1436 if (amaxAssociates >= 0) {
1437 if ((ntohl(tentry.flags) & KAFASSOC)
1438 || (ntohl(tentry.flags) & KAFSPECIAL))
1440 if (((ntohl(tentry.flags) & KAFASSOCROOT) == 0) && (amaxAssociates > 0)) /* convert normal user to assoc root */
1441 tentry.flags = htonl(ntohl(tentry.flags) | KAFASSOCROOT);
1442 tentry.misc.assocRoot.maxAssociates = htonl(amaxAssociates);
1445 tentry.modification_time = htonl(now);
1446 tentry.modification_id = htonl(caller);
1447 code = kawrite(tt, tentry_offset, (char *) &tentry, sizeof(tentry));
1451 code = ubik_EndTrans(tt);
1452 KALOG(aname, ainstance, NULL, NULL, NULL,
1453 rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))), LOG_SETFIELDS);
1458 ubik_AbortTrans(tt);
1465 SKAM_DeleteUser(struct rx_call *call, char *aname, char *ainstance)
1469 code = kamDeleteUser(call, aname, ainstance);
1470 osi_auditU(call, AFS_KAM_DelUserEvent, code, AUD_STR, aname, AUD_STR,
1471 ainstance, AUD_END);
1476 kamDeleteUser(struct rx_call *call, char *aname, char *ainstance)
1479 struct ubik_trans *tt;
1482 struct kaentry tentry;
1483 unsigned int nfailures;
1484 afs_uint32 locktime;
1486 COUNT_REQ(DeleteUser);
1487 if (!name_instance_legal(aname, ainstance))
1489 if ((code = InitAuthServ(&tt, LOCKWRITE, this_op)))
1491 code = check_auth(call, tt, 1, &caller);
1495 ubik_AbortTrans(tt);
1499 code = FindBlock(tt, aname, ainstance, &to, &tentry);
1502 if (to == 0) { /* name not found */
1507 kaux_read(to, &nfailures, &locktime);
1508 if (nfailures || locktime)
1509 kaux_write(to, 0, 0); /* zero failure counters at this offset */
1511 /* track all AuthServer identities */
1512 if (special_name(aname, ainstance))
1513 if ((code = ka_DelKey(tt, to, &tentry)))
1516 if (ntohl(tentry.flags) & KAFADMIN) /* keep admin count up-to-date */
1517 if ((code = update_admin_count(tt, -1)))
1520 if ((code = UnthreadBlock(tt, &tentry)) || (code = FreeBlock(tt, to)) || (code = get_time(0, tt, 1)) /* update randomness */
1524 code = ubik_EndTrans(tt);
1525 KALOG(aname, ainstance, NULL, NULL, NULL,
1526 rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))), LOG_DELUSER);
1530 /* we set a bit in here which indicates that the user's limit of
1531 * authentication failures has been exceeded. If that bit is not set,
1532 * kas can take it on faith that the user ID is not locked. If that
1533 * bit is set, kas has to check all the servers to find one who will
1534 * report that the ID is not locked, or else to find out when the ID
1538 SKAM_GetEntry(struct rx_call *call,
1541 afs_int32 aversion, /* major version assumed by caller */
1542 kaentryinfo *aentry) /* entry data copied here */
1546 code = kamGetEntry(call, aname, ainstance, aversion, aentry);
1547 osi_auditU(call, AFS_KAM_GetEntEvent, code, AUD_STR, aname, AUD_STR,
1548 ainstance, AUD_END);
1553 kamGetEntry(struct rx_call *call,
1556 afs_int32 aversion, /* major version assumed by caller */
1557 kaentryinfo *aentry) /* entry data copied here */
1560 struct ubik_trans *tt;
1561 afs_int32 callerIndex;
1562 struct kaentry caller;
1565 struct kaentry tentry;
1566 rxkad_level enc_level = rxkad_clear;
1567 int callerIsAdmin = 0;
1569 COUNT_REQ(GetEntry);
1570 if (aversion != KAMAJORVERSION)
1571 return KAOLDINTERFACE;
1572 if (!name_instance_legal(aname, ainstance))
1574 if ((code = InitAuthServ(&tt, LOCKREAD, this_op)))
1576 code = check_auth(call, tt, 0, &callerIndex);
1580 if (noAuthenticationRequired) {
1581 } else if (!callerIndex) {
1585 if ((code = karead(tt, callerIndex, (char *)&caller, sizeof(caller)))) {
1589 /* if the user is checking his own entry or ADMIN then go ahead. */
1590 callerIsAdmin = (ntohl(caller.flags) & KAFADMIN);
1592 if (strcmp(caller.userID.name, aname) != 0 && !callerIsAdmin) {
1598 code = FindBlock(tt, aname, ainstance, &to, &tentry);
1601 if (to == 0) { /* entry not found */
1606 get_time(0, 0, 0); /* generate random update */
1608 memset(aentry, 0, sizeof(*aentry));
1609 aentry->minor_version = KAMINORVERSION;
1610 aentry->flags = ntohl(tentry.flags);
1611 aentry->user_expiration = ntohl(tentry.user_expiration);
1612 aentry->modification_time = ntohl(tentry.modification_time);
1613 aentry->change_password_time = ntohl(tentry.change_password_time);
1614 aentry->max_ticket_lifetime = ntohl(tentry.max_ticket_lifetime);
1615 aentry->key_version = ntohl(tentry.key_version);
1617 temp = (unsigned char)tentry.misc_auth_bytes[LOCKTIME];
1619 if (kaux_islocked(to, (u_int) tentry.misc_auth_bytes[ATTEMPTS], temp))
1620 tentry.misc_auth_bytes[REUSEFLAGS] |= KA_ISLOCKED; /* saves an RPC */
1622 temp = pack_long(tentry.misc_auth_bytes);
1623 aentry->misc_auth_bytes = temp;
1625 * only return user's key if security disabled or if admin and
1626 * we have an encrypted connection to the user
1628 rxkad_GetServerInfo(rx_ConnectionOf(call), &enc_level, 0, 0, 0, 0, 0);
1629 if ((noAuthenticationRequired)
1630 || (callerIsAdmin && enc_level == rxkad_crypt))
1631 memcpy(&aentry->key, &tentry.key, sizeof(struct ktc_encryptionKey));
1633 memset(&aentry->key, 0, sizeof(aentry->key));
1635 code = ka_KeyCheckSum((char *)&tentry.key, &aentry->keyCheckSum);
1639 if (!tentry.pwsums[0] && npwSums > 1 && !tentry.pwsums[1]) {
1640 aentry->reserved3 = 0x12340000;
1642 aentry->reserved3 = 0x12340001;
1645 /* Now get entry of user who last modified this entry */
1646 if (ntohl(tentry.modification_id)) {
1647 temp = ntohl(tentry.modification_id);
1648 code = karead(tt, temp, (char *)&tentry, sizeof(tentry));
1653 aentry->modification_user = tentry.userID;
1655 strcpy(aentry->modification_user.name, "<none>");
1656 strcpy(aentry->modification_user.instance, "\0");
1658 code = ubik_EndTrans(tt);
1663 ubik_AbortTrans(tt);
1668 SKAM_ListEntry(struct rx_call *call,
1669 afs_int32 previous_index, /* last entry ret'd or 0 for first */
1670 afs_int32 *index, /* index of this entry */
1671 afs_int32 *count, /* total entries in database */
1672 kaident *name) /* name & instance of this entry */
1676 code = kamListEntry(call, previous_index, index, count, name);
1677 osi_auditU(call, AFS_KAM_LstEntEvent, code, AUD_LONG, *index, AUD_END);
1683 kamListEntry(struct rx_call *call,
1684 afs_int32 previous_index, /* last entry ret'd or 0 for first */
1685 afs_int32 *index, /* index of this entry */
1686 afs_int32 *count, /* total entries in database */
1687 kaident *name) /* name & instance of this entry */
1690 struct ubik_trans *tt;
1692 struct kaentry tentry;
1694 COUNT_REQ(ListEntry);
1695 memset(name, 0, sizeof(*name));
1696 if ((code = InitAuthServ(&tt, LOCKREAD, this_op)))
1698 code = check_auth(call, tt, 1, &caller);
1703 *index = NextBlock(tt, previous_index, &tentry, count);
1709 if (*index) { /* return name & inst of this entry */
1710 strncpy(name->name, tentry.userID.name, sizeof(name->name));
1711 strncpy(name->instance, tentry.userID.instance,
1712 sizeof(name->instance));
1714 strcpy(name->name, "\0");
1715 strcpy(name->instance, "\0");
1717 code = ubik_EndTrans(tt);
1722 ubik_AbortTrans(tt);
1727 GetTicket(int version,
1728 struct rx_call *call,
1734 ka_CBS *atimes, /* encrypted start & end time */
1739 struct ubik_trans *tt;
1740 struct ktc_encryptionKey tgskey;
1741 DES_key_schedule schedule;
1743 char name[MAXKTCNAMELEN];
1744 char instance[MAXKTCNAMELEN];
1745 char cell[MAXKTCNAMELEN];
1747 struct kaentry caller;
1748 struct kaentry server;
1749 struct ktc_encryptionKey authSessionKey;
1750 struct ktc_encryptionKey sessionKey;
1752 char ticket[MAXKTCTICKETLEN];
1758 struct ka_getTicketTimes times;
1759 struct ka_getTicketAnswer *answer;
1761 COUNT_REQ(GetTicket);
1762 if (!name_instance_legal(sname, sinstance))
1764 if (atimes->SeqLen != sizeof(times))
1765 return KABADARGUMENT;
1766 if ((code = InitAuthServ(&tt, LOCKREAD, this_op)))
1769 export = import = 0;
1770 if ((strcmp(sname, KA_TGS_NAME) == 0) && (strcmp(sinstance, lrealm) != 0))
1772 if ((strlen(authDomain) > 0) && (strcmp(authDomain, lrealm) != 0))
1775 if (strlen(authDomain) == 0)
1776 authDomain = lrealm;
1777 code = ka_LookupKvno(tt, KA_TGS_NAME, authDomain, kvno, &tgskey);
1782 tkt_DecodeTicket(aticket->SeqBody, aticket->SeqLen, &tgskey, name,
1783 instance, cell, &authSessionKey, &host, &start,
1789 save_principal(tgsPrincipal, name, instance, cell);
1791 if ((code = get_time(&now, 0, 0)))
1794 code = tkt_CheckTimes(start, expiration, now);
1797 code = RXKADEXPIRED;
1802 code = DES_key_sched(ktc_to_cblock(&authSessionKey), &schedule);
1807 celllen = strlen(cell);
1808 if (import && (celllen == 0)) {
1812 if (export && (celllen == 0))
1813 strcpy(cell, lrealm);
1815 if (!krb4_cross && celllen && strcmp(lrealm, cell) != 0) {
1820 DES_ecb_encrypt((DES_cblock *)atimes->SeqBody, (DES_cblock *)×, &schedule, DECRYPT);
1821 times.start = ntohl(times.start);
1822 times.end = ntohl(times.end);
1823 code = tkt_CheckTimes(times.start, times.end, now);
1825 code = KABADREQUEST;
1830 strcpy(caller.userID.name, name);
1831 strcpy(caller.userID.instance, instance);
1832 caller.max_ticket_lifetime = htonl(MAXKTCTICKETLIFETIME);
1833 caller.flags = htonl(KAFNORMAL);
1834 caller.user_expiration = htonl(NEVERDATE);
1836 code = FindBlock(tt, name, instance, &to, &caller);
1840 ka_PrintUserID("GetTicket: User ", name, instance, " unknown.\n");
1846 /* get server's entry */
1847 code = FindBlock(tt, sname, sinstance, &to, &server);
1850 if (to == 0) { /* entry not found */
1851 ka_PrintUserID("GetTicket: Server ", sname, sinstance, " unknown.\n");
1855 save_principal(tgsServerPrincipal, sname, sinstance, 0);
1857 code = DES_new_random_key(ktc_to_cblock(&sessionKey));
1864 GetEndTime(times.start, times.end, expiration, &caller, &server,
1870 tkt_MakeTicket(ticket, &ticketLen, &server.key, caller.userID.name,
1871 caller.userID.instance, cell, times.start, end,
1873 rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))),
1874 server.userID.name, server.userID.instance);
1880 code = KAANSWERTOOLONG;
1881 if (oanswer->MaxSeqLen <
1882 sizeof(struct ka_getTicketAnswer) - 5 * MAXKTCNAMELEN -
1883 MAXKTCTICKETLEN + ticketLen)
1886 answer = (struct ka_getTicketAnswer *)oanswer->SeqBody;
1887 memcpy(&answer->sessionKey, &sessionKey,
1888 sizeof(struct ktc_encryptionKey));
1889 answer->startTime = htonl(times.start);
1890 answer->endTime = htonl(end);
1891 answer->kvno = server.key_version;
1892 answer->ticketLen = htonl(ticketLen);
1895 char *ans = answer->name; /* ptr to variable part of answer */
1898 /* space remaining */
1899 rem = oanswer->MaxSeqLen - (ans - oanswer->SeqBody);
1901 #define putstr(str) len = strlen (str)+1;\
1902 if (rem < len) goto abort;\
1904 ans += len; rem -= len
1911 if (rem < ticketLen)
1913 memcpy(ans, ticket, ticketLen);
1914 oanswer->SeqLen = (ans - oanswer->SeqBody) + ticketLen;
1916 oanswer->SeqLen = round_up_to_ebs(oanswer->SeqLen);
1920 PrepareTicketAnswer(oanswer, /*challenge */ 0, ticket, ticketLen,
1921 &sessionKey, times.start, end, &caller,
1922 &server, cell, KA_GETTICKET_ANS_LABEL);
1927 code = KAINTERNALERROR;
1930 DES_pcbc_encrypt(oanswer->SeqBody, oanswer->SeqBody, oanswer->SeqLen,
1931 &schedule, ktc_to_cblockptr(&authSessionKey), ENCRYPT);
1932 code = ubik_EndTrans(tt);
1933 KALOG(name, instance, sname, sinstance, (import ? authDomain : NULL),
1934 rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))), LOG_GETTICKET);
1939 ubik_AbortTrans(tt);
1944 SKAT_GetTicket_old(struct rx_call *call,
1950 ka_CBS *atimes, /* encrypted start & end time */
1955 sleep(1); /* strongly discourage this */
1957 GetTicket(0, call, kvno, authDomain, aticket, sname, sinstance,
1960 osi_auditU(call, AFS_KAT_GetTicketOEvent, code, AUD_STR, sname, AUD_STR,
1961 sinstance, AUD_END);
1966 SKAT_GetTicket(struct rx_call *call,
1972 ka_CBS *atimes, /* encrypted start & end time */
1978 GetTicket(1, call, kvno, authDomain, aticket, sname, sinstance,
1980 osi_auditU(call, AFS_KAT_GetTicketEvent, code, AUD_STR, sname, AUD_STR,
1981 sinstance, AUD_END);
1986 SKAM_GetStats(struct rx_call *call, afs_int32 version,
1987 afs_int32 *admin_accounts, kasstats *statics,
1992 code = kamGetStats(call, version, admin_accounts, statics, dynamics);
1993 osi_auditU(call, AFS_KAM_GetStatEvent, code, AUD_END);
1998 kamGetStats(struct rx_call *call, afs_int32 version,
1999 afs_int32 *admin_accounts, kasstats *statics,
2003 struct ubik_trans *tt;
2006 COUNT_REQ(GetStats);
2007 if (version != KAMAJORVERSION)
2008 return KAOLDINTERFACE;
2009 if ((code = InitAuthServ(&tt, LOCKREAD, this_op)))
2011 code = check_auth(call, tt, 1, &caller);
2014 ubik_AbortTrans(tt);
2018 *admin_accounts = ntohl(cheader.admin_accounts);
2019 /* memcpy((char *)statics, (char *)&cheader.stats, sizeof(kasstats)); */
2020 /* these are stored in network byte order and must be copied */
2021 statics->allocs = ntohl(cheader.stats.allocs);
2022 statics->frees = ntohl(cheader.stats.frees);
2023 statics->cpws = ntohl(cheader.stats.cpws);
2024 #if KADBVERSION != 5
2025 check that the statistics command copies all the fields
2027 memcpy((char *)dynamics, (char *)&dynamic_statistics, sizeof(kadstats));
2028 statics->minor_version = KAMINORVERSION;
2029 dynamics->minor_version = KAMINORVERSION;
2035 for (i = 0; i < HASHSIZE; i++)
2036 if (cheader.nameHash[i])
2038 dynamics->hashTableUtilization =
2039 (used * 10000 + HASHSIZE / 2) / HASHSIZE;
2042 #if !defined(AFS_AIX_ENV) && !defined(AFS_HPUX_ENV) && !defined(AFS_NT40_ENV)
2044 /* Unfortunately, although aix_22 has a minimal compatibility
2045 * method of getting to some rusage fields (i.e. stime &
2046 * utime), the version that we have doesn't even have the
2047 * related include files needed for the aix vtimes() call; so
2048 * ignore this for aix till v3.1... */
2049 getrusage(RUSAGE_SELF, &ru);
2050 #if (KAMAJORVERSION>5)
2051 memcpy(&dynamics->utime, &ru.ru_utime, sizeof(struct katimeval));
2052 memcpy(&dynamics->stime, &ru.ru_stime, sizeof(struct katimeval));
2053 dynamics->dataSize = ru.ru_idrss;
2054 dynamics->stackSize = ru.ru_isrss;
2055 dynamics->pageFailts = ru.ru_majflt;
2057 dynamics->string_checks =
2058 (afs_int32) (1000.0 *
2059 ((ru.ru_utime.tv_sec +
2060 ru.ru_utime.tv_usec / 1000000.0) +
2061 (ru.ru_stime.tv_sec +
2062 ru.ru_stime.tv_usec / 1000000.0)));
2064 #endif /* AFS_AIX_ENV && AFS_HPUX_ENV && AFS_NT40_ENV */
2067 code = ubik_EndTrans(tt);
2072 SKAM_GetPassword(struct rx_call *call, char *name, EncryptionKey *password)
2076 code = kamGetPassword(call, name, password);
2077 osi_auditU(call, AFS_KAM_GetPswdEvent, code, AUD_STR, name, AUD_END);
2082 kamGetPassword(struct rx_call *call, char *name, EncryptionKey *password)
2084 int code = KANOAUTH;
2085 AFS_UNUSED COUNT_REQ(GetPassword);
2089 struct ubik_trans *tt;
2090 struct kaentry tentry;
2092 if (!name_instance_legal(name, ""))
2094 /* only requests from this host work */
2095 if (rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))) !=
2096 htonl(INADDR_LOOPBACK))
2098 if (code = InitAuthServ(&tt, LOCKREAD, this_op))
2101 /* this isn't likely to be used because of string to key problems, so since
2102 * this is a temporary thing anyway, we'll use it here. */
2104 extern char udpAuthPrincipal[256];
2106 save_principal(udpAuthPrincipal, name, 0, 0);
2109 get_time(0, 0, 0); /* update random value */
2110 code = FindBlock(tt, name, "", &to, &tentry);
2117 ubik_AbortTrans(tt);
2121 memcpy(password, &tentry.key, sizeof(*password));
2122 code = ubik_EndTrans(tt);
2129 SKAM_GetRandomKey(struct rx_call *call, EncryptionKey *key)
2133 code = kamGetRandomKey(call, key);
2134 osi_auditU(call, AFS_KAM_GetRndKeyEvent, code, AUD_END);
2139 kamGetRandomKey(struct rx_call *call, EncryptionKey *key)
2143 AFS_UNUSED COUNT_REQ(GetRandomKey);
2144 if ((code = AwaitInitialization()))
2146 code = DES_new_random_key(EncryptionKey_to_cblock(key));
2153 SKAM_Debug(struct rx_call *call,
2155 int checkDB, /* start a transaction to examine DB */
2156 struct ka_debugInfo *info)
2160 code = kamDebug(call, version, checkDB, info);
2161 osi_auditU(call, AFS_KAM_DbgEvent, code, AUD_END);
2166 kamDebug(struct rx_call *call,
2168 int checkDB, /* start a transaction to examine DB */
2169 struct ka_debugInfo *info)
2171 /* COUNT_REQ (Debug); */
2172 if (sizeof(struct kaentry) != sizeof(struct kaOldKeys))
2173 return KAINTERNALERROR;
2174 if (sizeof(struct ka_cpwRequest) % 8)
2175 return KAINTERNALERROR;
2176 if (version != KAMAJORVERSION)
2177 return KAOLDINTERFACE;
2179 memset(info, 0, sizeof(*info));
2181 info->minorVersion = KAMINORVERSION;
2182 info->host = dynamic_statistics.host;
2183 info->startTime = dynamic_statistics.start_time;
2185 #if (KAMAJORVERSION>5)
2191 info->noAuth = noAuthenticationRequired;
2193 info->dbVersion = ntohl(cheader.version);
2194 info->dbFreePtr = ntohl(cheader.freePtr);
2195 info->dbEofPtr = ntohl(cheader.eofPtr);
2196 info->dbKvnoPtr = ntohl(cheader.kvnoPtr);
2197 info->dbSpecialKeysVersion = ntohl(cheader.specialKeysVersion);
2199 info->dbHeaderRead = cheaderReadTime;
2200 info->lastTrans = lastTrans;
2202 lastOperation = "(Not Available)";
2203 strncpy(info->lastOperation, lastOperation, sizeof(info->lastOperation));
2204 strncpy(info->lastAuth, authPrincipal, sizeof(info->lastAuth));
2205 strncpy(info->lastTGS, tgsPrincipal, sizeof(info->lastTGS));
2206 strncpy(info->lastAdmin, adminPrincipal, sizeof(info->lastAdmin));
2207 strncpy(info->lastTGSServer, tgsServerPrincipal,
2208 sizeof(info->lastTGSServer));
2210 extern char udpAuthPrincipal[256];
2211 extern char udptgsPrincipal[256];
2212 extern char udptgsServerPrincipal[256];
2214 strncpy(info->lastUAuth, udpAuthPrincipal, sizeof(info->lastUAuth));
2215 strncpy(info->lastUTGS, udptgsPrincipal, sizeof(info->lastUTGS));
2216 strncpy(info->lastUTGSServer, udptgsServerPrincipal,
2217 sizeof(info->lastUTGSServer));
2219 info->nextAutoCPW = nextAutoCPWTime;
2220 info->updatesRemaining = autoCPWUpdates - totalUpdates;
2221 ka_debugKeyCache(info);
2225 /* these are auxiliary routines. They don't do any Ubik stuff. They use
2226 * a tacked-on-the-side data file.
2227 * prob'ly ought to check the noauth flag.
2229 #define ABORTIF(A) {if((code = A)){goto abort;}}
2231 SKAM_Unlock(struct rx_call *call,
2240 struct ubik_trans *tt;
2243 struct kaentry tentry;
2246 if (!name_instance_legal(aname, ainstance)) {
2250 if ((code = InitAuthServ(&tt, LOCKREAD, this_op)))
2253 ABORTIF(check_auth(call, tt, 1, &caller));
2254 ABORTIF(FindBlock(tt, aname, ainstance, &to, &tentry));
2255 ABORTIF((to == 0 ? KANOENT : 0));
2257 kaux_write(to, 0, 0); /* zero failure counters at this offset */
2259 code = ubik_EndTrans(tt);
2260 KALOG(aname, ainstance, NULL, NULL, NULL,
2261 rx_HostOf(rx_PeerOf(rx_ConnectionOf(call))), LOG_UNLOCK);
2266 ubik_AbortTrans(tt);
2269 osi_auditU(call, UnlockEvent, code, AUD_STR, aname, AUD_STR, ainstance,
2275 SKAM_LockStatus(struct rx_call *call,
2278 afs_int32 *lockeduntil,
2285 struct ubik_trans *tt;
2286 afs_int32 callerIndex;
2288 struct kaentry caller;
2289 struct kaentry tentry;
2292 COUNT_REQ(LockStatus);
2294 if (!name_instance_legal(aname, ainstance)) {
2298 if ((code = InitAuthServ(&tt, LOCKREAD, this_op)))
2301 if ((code = check_auth(call, tt, 0, &callerIndex)))
2304 if (!noAuthenticationRequired && callerIndex) {
2305 if (karead(tt, callerIndex, (char *)&caller, sizeof(caller))) {
2309 /* if the user is checking his own entry or ADMIN then go ahead. */
2310 if ((strcmp(caller.userID.name, aname) != 0)
2311 && !(ntohl(caller.flags) & KAFADMIN)) {
2317 if ((code = FindBlock(tt, aname, ainstance, &to, &tentry)))
2325 temp = (unsigned char)tentry.misc_auth_bytes[LOCKTIME];
2328 kaux_islocked(to, (u_int) tentry.misc_auth_bytes[ATTEMPTS], temp);
2330 code = ubik_EndTrans(tt);
2335 ubik_AbortTrans(tt);
2336 osi_auditU(call, LockStatusEvent, code, AUD_STR, aname, AUD_STR,
2337 ainstance, AUD_END);