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 /* ticket caching code */
12 #include <afsconfig.h>
14 #include "afs/param.h"
16 #include <afs/param.h>
21 #include "afs/sysincludes.h"
22 #include "afsincludes.h"
24 #include "afs/pthread_glock.h"
27 #include "afs/venus.h"
28 #include "afs/pthread_glock.h"
29 #include "afs/dirpath.h"
33 #define min(a,b) ((a)<(b)?(a):(b))
34 #endif /* !defined(min) */
36 #else /* defined(UKERNEL) */
44 #include <afs/pthread_glock.h>
45 #include <sys/types.h>
50 #include <sys/ioctl.h>
51 #include <netinet/in.h>
55 #include <sys/lockf.h>
63 #ifdef AFS_DARWIN100_ENV
64 #include <crt_externs.h>
70 #include <afs/venus.h>
71 #include <afs/afsutil.h>
72 #include <afs/sys_prototypes.h>
74 #endif /* defined(UKERNEL) */
81 /* AFS_KERBEROS_ENV is now conditionally defined in the Makefile */
82 #define AFS_KERBEROS_ENV
85 #ifdef AFS_KERBEROS_ENV
88 #include <afs/cellconfig.h>
89 static char lcell[MAXCELLCHARS];
91 #define TKT_ROOT "/tmp/tkt"
96 /* Definitions for ticket file utilities */
100 /* Error codes returned by ticket file utilities */
101 #define NO_TKT_FIL 76 /* No ticket file found */
102 #define TKT_FIL_ACC 77 /* Couldn't access tkt file */
103 #define TKT_FIL_LCK 78 /* Couldn't lock ticket file */
104 #define TKT_FIL_FMT 79 /* Bad ticket file format */
105 #define TKT_FIL_INI 80 /* afs_tf_init not called first */
107 /* Values returned by get_credentials */
108 #define RET_TKFIL 21 /* Can't read ticket file */
117 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
118 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0, 0 };
119 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0, 0 };
120 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0, 0 };
123 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0 };
124 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0 };
125 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0 };
132 /* the following routines aren't static anymore on behalf of the kerberos IV
133 * compatibility library built in subtree krb.
135 int afs_tf_init(char *, int);
136 int afs_tf_get_pname(char *);
137 int afs_tf_get_pinst(char *);
138 int afs_tf_get_cred(struct ktc_principal *, struct ktc_token *);
139 int afs_tf_save_cred(struct ktc_principal *, struct ktc_token *,
140 struct ktc_principal *);
141 int afs_tf_close(void);
142 int afs_tf_create(char *, char *);
143 int afs_tf_dest_tkt(void);
144 static void ktc_LocalCell(void);
145 #endif /* AFS_KERBEROS_ENV */
148 #define PIOCTL afs_pioctl
149 #elif defined(UKERNEL)
150 #define PIOCTL(A,B,C,D) call_syscall(AFSCALL_PIOCTL,A,B,C,D)
152 #define PIOCTL pioctl
156 #ifdef KERNEL_KTC_COMPAT
159 #define KTC_SYSCALL 32
162 /* Kernel call opcode definitions */
163 #define KTC_OPCODE_BASE 4300
164 #define KTC_NO_OP (0+KTC_OPCODE_BASE)
165 #define KTC_SETTOKEN_OP (1+KTC_OPCODE_BASE)
166 #define KTC_GETTOKEN_OP (2+KTC_OPCODE_BASE)
167 #define KTC_LISTTOKENS_OP (3+KTC_OPCODE_BASE)
168 #define KTC_FORGETTOKEN_OP (4+KTC_OPCODE_BASE)
169 #define KTC_FORGETALLTOKENS_OP (5+KTC_OPCODE_BASE)
170 #define KTC_STATUS_OP (6+KTC_OPCODE_BASE)
171 #define KTC_GC_OP (7+KTC_OPCODE_BASE)
173 #define KTC_INTERFACE_VERSION 3
175 /* We have to determine if the kernel supports the ktc system call. To do so
176 * we attempt to execute its noop function. If this is successful we use the
177 * kernel calls in the future otherwise we let the old code run. */
179 /* To safely check to see whether a system call exists we have to intercept the
180 * SIGSYS signal which is caused by executing a non-existant system call. If
181 * it is ignored the syscall routine returns EINVAL. The SIGSYS is reset to
182 * its old value after the return from syscall. */
184 static int kernelKTC = 0;
186 #ifdef AFS_DECOSF_ENV
188 * SIGSYS semantics are broken on Dec AXP OSF/1 v1.2 systems. You need
189 * to ignore SIGTRAP too. It is claimed to be fixed under v1.3, but...
192 #define CHECK_KERNEL \
193 if (kernelKTC == 0) { \
197 old = (int (*)())signal(SIGSYS, SIG_IGN); \
198 old_t = (int (*)())signal(SIGTRAP, SIG_IGN); \
199 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
201 signal(SIGSYS, old); \
202 signal(SIGTRAP, old_t); \
203 if (code == 0) kernelKTC = 1; \
204 else kernelKTC = 2; \
205 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
208 #else /* AFS_DECOSF_ENV */
210 #define CHECK_KERNEL \
211 if (kernelKTC == 0) { \
214 old = (int (*)())signal(SIGSYS, SIG_IGN); \
215 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
217 signal(SIGSYS, old); \
218 if (code == 0) kernelKTC = 1; \
219 else kernelKTC = 2; \
220 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
222 #endif /* AFS_DECOSF_ENV */
224 #define TRY_KERNEL(cmd,a1,a2,a3,a4) \
226 if (kernelKTC == 1) \
227 return syscall (KTC_SYSCALL, cmd, \
228 KTC_INTERFACE_VERSION, a1,a2,a3,a4); \
232 #define TRY_KERNEL(cmd,a1,a2,a3,a4)
233 #endif /* KERNEL_KTC_COMPAT */
235 #if !defined(UKERNEL)
236 /* this is a structure used to communicate with the afs cache mgr, but is
237 * otherwise irrelevant */
239 afs_int32 AuthHandle;
240 char HandShakeKey[8];
242 afs_int32 BeginTimestamp;
243 afs_int32 EndTimestamp;
245 #endif /* !defined(UKERNEL) */
247 #define MAXLOCALTOKENS 4
251 struct ktc_principal server;
252 struct ktc_principal client;
253 struct ktc_token token;
254 } local_tokens[MAXLOCALTOKENS] = { {
260 /* new interface routines to the ticket cache. Only handle afs service right
264 NewSetToken(struct ktc_principal *aserver,
265 struct ktc_token *atoken,
266 struct ktc_principal *aclient,
269 TRY_KERNEL(KTC_SETTOKEN_OP, aserver, aclient, atoken,
270 sizeof(struct ktc_token));
271 /* no kernel ticket cache */
275 #define MAXPIOCTLTOKENLEN \
276 (3*sizeof(afs_int32)+MAXKTCTICKETLEN+sizeof(struct ClearToken)+MAXKTCREALMLEN)
279 OldSetToken(struct ktc_principal *aserver, struct ktc_token *atoken,
280 struct ktc_principal *aclient, afs_int32 flags)
282 struct ViceIoctl iob;
283 char tbuffer[MAXPIOCTLTOKENLEN];
285 struct ClearToken ct;
286 register afs_int32 code;
289 if (strcmp(aserver->name, "afs") != 0) {
292 for (i = 0; i < MAXLOCALTOKENS; i++)
293 if (local_tokens[i].valid) {
294 if ((strcmp(local_tokens[i].server.name, aserver->name) == 0)
297 (local_tokens[i].server.instance,
298 aserver->instance) == 0)
299 && (strcmp(local_tokens[i].server.cell, aserver->cell) ==
301 found = i; /* replace existing entry */
303 } else /* valid, but no match */
306 found = i; /* remember this empty slot */
309 memcpy(&local_tokens[found].token, atoken, sizeof(struct ktc_token));
310 local_tokens[found].server = *aserver;
311 local_tokens[found].client = *aclient;
312 local_tokens[found].valid = 1;
315 tp = tbuffer; /* start copying here */
316 if ((atoken->ticketLen < MINKTCTICKETLEN)
317 || (atoken->ticketLen > MAXKTCTICKETLEN))
319 memcpy(tp, &atoken->ticketLen, sizeof(afs_int32)); /* copy in ticket length */
320 tp += sizeof(afs_int32);
321 memcpy(tp, atoken->ticket, atoken->ticketLen); /* copy in ticket */
322 tp += atoken->ticketLen;
323 /* next, copy in the "clear token", describing who we are */
324 ct.AuthHandle = atoken->kvno; /* hide auth handle here */
325 memcpy(ct.HandShakeKey, &atoken->sessionKey, 8);
327 ct.BeginTimestamp = atoken->startTime;
328 ct.EndTimestamp = atoken->endTime;
329 if (ct.BeginTimestamp == 0)
330 ct.BeginTimestamp = 1;
332 if ((strlen(aclient->name) > strlen("AFS ID "))
333 && (aclient->instance[0] == 0)) {
335 afs_int32 viceId = 0;
336 char *cp = aclient->name + strlen("AFS ID ");
343 viceId = viceId * 10 + (int)(*cp - '0');
348 ct.ViceId = viceId * sign; /* OK to let any value here? */
349 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 0)
350 ct.BeginTimestamp++; /* force lifetime to be odd */
353 ct.ViceId = getuid(); /* wrong, but works in primary cell */
354 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1)
355 ct.BeginTimestamp++; /* force lifetime to be even */
360 * Information needed by the user space cache manager
362 u.u_expiration = ct.EndTimestamp;
363 u.u_viceid = ct.ViceId;
366 temp = sizeof(struct ClearToken);
367 memcpy(tp, &temp, sizeof(afs_int32));
368 tp += sizeof(afs_int32);
369 memcpy(tp, &ct, sizeof(struct ClearToken));
370 tp += sizeof(struct ClearToken);
372 /* next copy in primary flag */
376 * The following means that setpag will happen inside afs just before
377 * the authentication to prevent the setpag/klog race condition.
379 * The following means that setpag will affect the parent process as
380 * well as the current process.
382 if (flags & AFS_SETTOK_SETPAG)
385 memcpy(tp, &temp, sizeof(afs_int32));
386 tp += sizeof(afs_int32);
388 /* finally copy in the cell name */
389 temp = strlen(aserver->cell);
390 if (temp >= MAXKTCREALMLEN)
392 strcpy(tp, aserver->cell);
395 /* now setup for the pioctl */
397 iob.in_size = tp - tbuffer;
399 iob.out_size = sizeof(tbuffer);
401 #if defined(NO_AFS_CLIENT)
405 if ((tkfile = getenv("TKTFILE"))
408 open(tkfile, O_WRONLY | O_APPEND | O_TRUNC | O_CREAT,
410 printf("Writing ticket to: %s\n", tkfile);
411 code = (write(fd, iob.in, iob.in_size) != iob.in_size);
414 code = KTC_PIOCTLFAIL;
416 #else /* NO_AFS_CLIENT */
417 code = PIOCTL(0, VIOCSETTOK, &iob, 0);
418 #endif /* NO_AFS_CLIENT */
420 return KTC_PIOCTLFAIL;
425 ktc_SetToken(struct ktc_principal *aserver,
426 struct ktc_token *atoken,
427 struct ktc_principal *aclient,
433 #ifdef AFS_KERBEROS_ENV
437 if ( /*!strcmp(aclient->cell, lcell) && this would only store local creds */
438 (strcmp(aserver->name, "AuthServer")
439 || strcmp(aserver->instance, "Admin"))) {
440 if (strcmp(aserver->name, "krbtgt") == 0) {
441 static char lrealm[MAXKTCREALMLEN];
444 ucstring(lrealm, lcell, MAXKTCREALMLEN);
445 if (strcmp(aserver->instance, lrealm) == 0) {
446 afs_tf_create(aclient->name, aclient->instance);
450 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
451 if (ncode == NO_TKT_FIL) {
452 (void)afs_tf_create(aclient->name, aclient->instance);
453 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
457 afs_tf_save_cred(aserver, atoken, aclient);
463 #endif /* NO_AFS_CLIENT */
467 #ifndef NO_AFS_CLIENT
468 ncode = NewSetToken(aserver, atoken, aclient, flags);
469 if (ncode || /* new style failed */
470 (strcmp(aserver->name, "afs") == 0)) { /* for afs tokens do both */
471 ocode = OldSetToken(aserver, atoken, aclient, flags);
474 if (ncode && ocode) {
478 else if (ocode == KTC_PIOCTLFAIL)
486 return KTC_PIOCTLFAIL;
488 #endif /* NO_AFS_CLIENT */
493 /* get token, given server we need and token buffer. aclient will eventually
494 * be set to our identity to the server.
497 ktc_GetToken(struct ktc_principal *aserver, struct ktc_token *atoken,
498 int atokenLen, struct ktc_principal *aclient)
500 struct ViceIoctl iob;
501 char tbuffer[MAXPIOCTLTOKENLEN];
502 register afs_int32 code = 0;
504 char *stp, *cellp; /* secret token ptr */
505 struct ClearToken ct;
508 int maxLen; /* biggest ticket we can copy */
509 int tktLen; /* server ticket length */
510 #ifdef AFS_KERBEROS_ENV
515 #ifndef NO_AFS_CLIENT
516 TRY_KERNEL(KTC_GETTOKEN_OP, aserver, aclient, atoken, atokenLen);
517 #endif /* NO_AFS_CLIENT */
519 #ifdef AFS_KERBEROS_ENV
523 #ifndef NO_AFS_CLIENT
524 if (strcmp(aserver->name, "afs") != 0)
525 #endif /* NO_AFS_CLIENT */
528 /* try the local tokens */
529 for (i = 0; i < MAXLOCALTOKENS; i++)
530 if (local_tokens[i].valid
531 && (strcmp(local_tokens[i].server.name, aserver->name) == 0)
532 && (strcmp(local_tokens[i].server.instance, aserver->instance)
534 && (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
535 memcpy(atoken, &local_tokens[i].token,
536 min(atokenLen, sizeof(struct ktc_token)));
538 *aclient = local_tokens[i].client;
542 #ifdef AFS_KERBEROS_ENV
543 if (!afs_tf_init(ktc_tkt_string(), R_TKT_FIL)) {
545 if (!afs_tf_get_pname(aclient->name)
546 && !afs_tf_get_pinst(aclient->instance))
549 char tmpstring[MAXHOSTCHARS];
550 afs_tf_get_pname(tmpstring);
551 afs_tf_get_pinst(tmpstring);
556 struct ktc_principal cprincipal;
557 struct ktc_token ctoken;
559 while (!afs_tf_get_cred(&cprincipal, &ctoken)) {
560 if (strcmp(cprincipal.name, aserver->name) == 0
561 && strcmp(cprincipal.instance, aserver->instance) == 0
562 && strcmp(cprincipal.cell, aserver->cell) == 0) {
565 strcpy(aclient->cell, lcell);
566 memcpy(atoken, &ctoken,
567 min(atokenLen, sizeof(struct ktc_token)));
580 #ifndef NO_AFS_CLIENT
581 for (index = 0; index < 200; index++) { /* sanity check in case pioctl fails */
582 iob.in = (char *)&index;
583 iob.in_size = sizeof(afs_int32);
585 iob.out_size = sizeof(tbuffer);
587 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
590 /* failed to retrieve specified token */
591 if (code < 0 && errno == EDOM) {
596 /* token retrieved; parse buffer */
599 /* get ticket length */
600 memcpy(&temp, tp, sizeof(afs_int32));
602 tp += sizeof(afs_int32);
604 /* remember where ticket is and skip over it */
608 /* get size of clear token and verify */
609 memcpy(&temp, tp, sizeof(afs_int32));
610 if (temp != sizeof(struct ClearToken)) {
614 tp += sizeof(afs_int32);
616 /* copy clear token */
617 memcpy(&ct, tp, temp);
620 /* skip over primary flag */
621 tp += sizeof(afs_int32);
623 /* remember where cell name is */
626 if ((strcmp(cellp, aserver->cell) == 0)
627 #ifdef AFS_KERBEROS_ENV
628 || (*aserver->cell == '\0' && strcmp(cellp, lcell) == 0)
631 /* got token for cell; check that it will fit */
633 atokenLen - sizeof(struct ktc_token) + MAXKTCTICKETLEN;
634 if (maxLen < tktLen) {
639 /* set return values */
640 memcpy(atoken->ticket, stp, tktLen);
641 atoken->startTime = ct.BeginTimestamp;
642 atoken->endTime = ct.EndTimestamp;
643 if (ct.AuthHandle == -1) {
646 atoken->kvno = ct.AuthHandle;
647 memcpy(&atoken->sessionKey, ct.HandShakeKey,
648 sizeof(struct ktc_encryptionKey));
649 atoken->ticketLen = tktLen;
652 strcpy(aclient->cell, cellp);
653 aclient->instance[0] = 0;
655 if ((atoken->kvno == 999) || /* old style bcrypt ticket */
656 (ct.BeginTimestamp && /* new w/ prserver lookup */
657 (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1))) {
658 sprintf(aclient->name, "AFS ID %d", ct.ViceId);
660 sprintf(aclient->name, "Unix UID %d", ct.ViceId);
668 #endif /* NO_AFS_CLIENT */
671 if ((code < 0) && (errno == EINVAL))
673 return KTC_PIOCTLFAIL; /* probable cause */
677 * Forget tokens for this server and the calling user.
678 * NOT IMPLEMENTED YET!
680 #ifndef NO_AFS_CLIENT
682 ktc_ForgetToken(struct ktc_principal *aserver)
687 TRY_KERNEL(KTC_FORGETTOKEN_OP, aserver, 0, 0, 0);
689 rc = ktc_ForgetAllTokens(); /* bogus, but better */
693 #endif /* NO_AFS_CLIENT */
695 /* ktc_ListTokens - list all tokens. start aprevIndex at 0, it returns the
696 * next rock in (*aindex). (*aserver) is set to the relevant ticket on
700 ktc_ListTokens(int aprevIndex,
702 struct ktc_principal *aserver)
704 struct ViceIoctl iob;
705 char tbuffer[MAXPIOCTLTOKENLEN];
706 register afs_int32 code = 0 ;
708 afs_int32 temp, index;
710 memset(tbuffer, 0, sizeof(tbuffer));
713 #ifndef NO_AFS_CLIENT
714 TRY_KERNEL(KTC_LISTTOKENS_OP, aserver, aprevIndex, aindex, 0);
715 #endif /* NO_AFS_CLIENT */
721 #endif /* NO_AFS_CLIENT */
722 #ifdef AFS_KERBEROS_ENV
725 struct ktc_principal cprincipal;
726 struct ktc_token ctoken;
728 if (afs_tf_init(ktc_tkt_string(), R_TKT_FIL)
729 || afs_tf_get_pname(tbuffer) || afs_tf_get_pinst(tbuffer)) {
735 for (i = 214; i < index; i++) {
736 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
744 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
751 #ifndef NO_AFS_CLIENT
752 if (!strcmp(cprincipal.name, "afs") && cprincipal.instance[0] == 0) {
755 #endif /* NO_AFS_CLIENT */
757 for (i = 0; i < MAXLOCALTOKENS; i++) {
758 if (!strcmp(cprincipal.name, local_tokens[i].server.name)
759 && !strcmp(cprincipal.instance,
760 local_tokens[i].server.instance)
761 && !strcmp(cprincipal.cell, local_tokens[i].server.cell)) {
766 *aserver = cprincipal;
774 #ifndef NO_AFS_CLIENT
775 if (index >= 123) { /* special hack for returning TCS */
776 while (index - 123 < MAXLOCALTOKENS) {
777 if (local_tokens[index - 123].valid) {
778 *aserver = local_tokens[index - 123].server;
786 #ifdef AFS_KERBEROS_ENV
787 return ktc_ListTokens(214, aindex, aserver);
793 /* get tokens from the kernel */
794 while (index < 200) { /* sanity check in case pioctl fails */
795 iob.in = (char *)&index;
796 iob.in_size = sizeof(afs_int32);
798 iob.out_size = sizeof(tbuffer);
799 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
800 if (code < 0 && errno == EDOM) {
803 rc = ktc_ListTokens(123, aindex, aserver);
812 break; /* got a ticket */
813 /* otherwise we should skip this ticket slot */
820 return KTC_PIOCTLFAIL;
826 /* next iterator determined by earlier loop */
829 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of secret token */
830 tp += sizeof(afs_int32);
831 tp += temp; /* skip ticket for now */
832 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of clear token */
833 if (temp != sizeof(struct ClearToken)) {
837 tp += sizeof(afs_int32); /* skip length */
838 tp += temp; /* skip clear token itself */
839 tp += sizeof(afs_int32); /* skip primary flag */
840 /* tp now points to the cell name */
841 strcpy(aserver->cell, tp);
842 aserver->instance[0] = 0;
843 strcpy(aserver->name, "afs");
844 #endif /* NO_AFS_CLIENT */
849 /* discard all tokens from this user's cache */
854 #ifndef NO_AFS_CLIENT
855 TRY_KERNEL(KTC_FORGETALLTOKENS_OP, 0, 0, 0, 0);
856 #endif /* NO_AFS_CLIENT */
863 struct ViceIoctl iob;
864 register afs_int32 code;
867 for (i = 0; i < MAXLOCALTOKENS; i++)
868 local_tokens[i].valid = 0;
874 #ifndef NO_AFS_CLIENT
875 code = PIOCTL(0, VIOCUNPAG, &iob, 0);
877 return KTC_PIOCTLFAIL;
878 #endif /* NO_AFS_CLIENT */
883 ktc_ForgetAllTokens(void)
888 #ifdef AFS_KERBEROS_ENV
889 (void)afs_tf_dest_tkt();
892 ncode = NewForgetAll();
893 ocode = OldForgetAll();
894 if (ncode && ocode) {
897 else if (ocode == KTC_PIOCTLFAIL)
902 return KTC_PIOCTLFAIL;
908 /* ktc_OldPioctl - returns a boolean true if the kernel supports only the old
909 * pioctl interface for delivering AFS tickets to the cache manager. */
916 #ifdef KERNEL_KTC_COMPAT
918 rc = (kernelKTC != 1); /* old style interface */
930 struct ViceIoctl iob;
933 /* now setup for the pioctl */
936 iob.out = (caddr_t) &pag;
937 iob.out_size = sizeof(afs_uint32);
939 code = PIOCTL(0, VIOC_GETPAG, &iob, 0);
941 #if defined(AFS_AIX52_ENV)
942 code = getpagvalue("afs");
943 if (code < 0 && errno == EINVAL)
946 #elif defined(AFS_AIX51_ENV)
949 gid_t groups[NGROUPS_MAX];
951 afs_uint32 h, l, ret;
953 if (getgroups(sizeof groups / sizeof groups[0], groups) < 2)
956 g0 = groups[0] & 0xffff;
957 g1 = groups[1] & 0xffff;
960 if (g0 < 0xc000 && g1 < 0xc000) {
961 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
963 h = (g1 >> 14) + h + h + h;
964 ret = ((h << 28) | l);
965 /* Additional testing */
966 if (((ret >> 24) & 0xff) == 'A')
978 #ifdef AFS_KERBEROS_ENV
980 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
982 * For copying and distribution information, please see the file
989 #include <sys/types.h>
990 #include <sys/stat.h>
991 #include <sys/file.h>
996 #define TF_LCK_RETRY ((unsigned)2) /* seconds to sleep before
997 * retry if ticket file is
1001 * fd must be initialized to something that won't ever occur as a real
1002 * file descriptor. Since open(2) returns only non-negative numbers as
1003 * valid file descriptors, and afs_tf_init always stuffs the return value
1004 * from open in here even if it is an error flag, we must
1005 * a. Initialize fd to a negative number, to indicate that it is
1006 * not initially valid.
1007 * b. When checking for a valid fd, assume that negative values
1008 * are invalid (ie. when deciding whether afs_tf_init has been
1010 * c. In tf_close, be sure it gets reinitialized to a negative
1014 static int curpos; /* Position in tfbfr */
1015 static int lastpos; /* End of tfbfr */
1016 static char tfbfr[BUFSIZ]; /* Buffer for ticket data */
1018 static int tf_gets(char *, int);
1019 static int tf_read(char *, int);
1022 * This file contains routines for manipulating the ticket cache file.
1024 * The ticket file is in the following format:
1026 * principal's name (null-terminated string)
1027 * principal's instance (null-terminated string)
1034 * Where "CREDENTIAL_x" consists of the following fixed-length
1035 * fields from the CREDENTIALS structure (see "krb.h"):
1037 * char service[MAXKTCNAMELEN]
1038 * char instance[MAXKTCNAMELEN]
1039 * char realm[REALM_SZ]
1043 * KTEXT_ST ticket_st
1044 * afs_int32 issue_date
1046 * Short description of routines:
1048 * afs_tf_init() opens the ticket file and locks it.
1050 * afs_tf_get_pname() returns the principal's name.
1052 * afs_tf_get_pinst() returns the principal's instance (may be null).
1054 * afs_tf_get_cred() returns the next CREDENTIALS record.
1056 * afs_tf_save_cred() appends a new CREDENTIAL record to the ticket file.
1058 * afs_tf_close() closes the ticket file and releases the lock.
1060 * tf_gets() returns the next null-terminated string. It's an internal
1061 * routine used by afs_tf_get_pname(), afs_tf_get_pinst(), and
1062 * afs_tf_get_cred().
1064 * tf_read() reads a given number of bytes. It's an internal routine
1065 * used by afs_tf_get_cred().
1069 * afs_tf_init() should be called before the other ticket file routines.
1070 * It takes the name of the ticket file to use, "tf_name", and a
1071 * read/write flag "rw" as arguments.
1073 * It tries to open the ticket file, checks the mode, and if everything
1074 * is okay, locks the file. If it's opened for reading, the lock is
1075 * shared. If it's opened for writing, the lock is exclusive.
1077 * Returns 0 if all went well, otherwise one of the following:
1079 * NO_TKT_FIL - file wasn't there
1080 * TKT_FIL_ACC - file was in wrong mode, etc.
1081 * TKT_FIL_LCK - couldn't lock the file, even after a retry
1085 afs_tf_init(char *tf_name, int rw)
1089 struct stat stat_buf;
1101 if (lstat(tf_name, &stat_buf) < 0)
1109 if ((stat_buf.st_uid != me && me != 0)
1110 || ((stat_buf.st_mode & S_IFMT) != S_IFREG))
1114 * If "wflag" is set, open the ticket file in append-writeonly mode
1115 * and lock the ticket file in exclusive mode. If unable to lock
1116 * the file, sleep and try again. If we fail again, return with the
1117 * proper error message.
1120 curpos = sizeof(tfbfr);
1123 fd = open(tf_name, O_RDWR, 0600);
1127 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1128 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1129 sleep(TF_LCK_RETRY);
1130 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1132 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1133 sleep(TF_LCK_RETRY);
1134 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1144 * Otherwise "wflag" is not set and the ticket file should be opened
1145 * for read-only operations and locked for shared access.
1148 fd = open(tf_name, O_RDONLY, 0600);
1152 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1153 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1154 sleep(TF_LCK_RETRY);
1155 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1157 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1158 sleep(TF_LCK_RETRY);
1159 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1170 * afs_tf_get_pname() reads the principal's name from the ticket file. It
1171 * should only be called after afs_tf_init() has been called. The
1172 * principal's name is filled into the "p" parameter. If all goes well,
1173 * 0 is returned. If afs_tf_init() wasn't called, TKT_FIL_INI is
1174 * returned. If the name was null, or EOF was encountered, or the name
1175 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned.
1179 afs_tf_get_pname(char *p)
1184 if (tf_gets(p, MAXKTCNAMELEN) < 2) /* can't be just a null */
1190 * afs_tf_get_pinst() reads the principal's instance from a ticket file.
1191 * It should only be called after afs_tf_init() and afs_tf_get_pname() have
1192 * been called. The instance is filled into the "inst" parameter. If all
1193 * goes well, 0 is returned. If afs_tf_init() wasn't called,
1194 * TKT_FIL_INI is returned. If EOF was encountered, or the instance
1195 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned. Note that the
1196 * instance may be null.
1200 afs_tf_get_pinst(char *inst)
1205 if (tf_gets(inst, MAXKTCNAMELEN) < 1)
1211 * afs_tf_get_cred() reads a CREDENTIALS record from a ticket file and fills
1212 * in the given structure "c". It should only be called after afs_tf_init(),
1213 * afs_tf_get_pname(), and afs_tf_get_pinst() have been called. If all goes
1214 * well, 0 is returned. Possible error codes are:
1216 * TKT_FIL_INI - afs_tf_init wasn't called first
1217 * TKT_FIL_FMT - bad format
1218 * EOF - end of file encountered
1222 afs_tf_get_cred(struct ktc_principal *principal, struct ktc_token *token)
1226 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1231 if ((k_errno = tf_gets(principal->name, MAXKTCNAMELEN)) < 2)
1234 case 1: /* can't be just a null */
1239 if ((k_errno = tf_gets(principal->instance, MAXKTCNAMELEN)) < 1)
1246 if ((k_errno = tf_gets(principal->cell, MAXKTCREALMLEN)) < 2)
1249 case 1: /* can't be just a null */
1254 lcstring(principal->cell, principal->cell, MAXKTCREALMLEN);
1255 if (tf_read((char *)&(token->sessionKey), 8) < 1
1256 || tf_read((char *)&(lifetime), sizeof(lifetime)) < 1
1257 || tf_read((char *)&(kvno), sizeof(kvno)) < 1
1258 || tf_read((char *)&(token->ticketLen), sizeof(token->ticketLen))
1260 /* don't try to read a silly amount into ticket->dat */
1261 token->ticketLen > MAXKTCTICKETLEN
1262 || tf_read((char *)(token->ticket), token->ticketLen) < 1
1263 || tf_read((char *)&mit_compat, sizeof(mit_compat)) < 1) {
1266 token->startTime = mit_compat;
1267 token->endTime = life_to_time(token->startTime, lifetime);
1273 * tf_close() closes the ticket file and sets "fd" to -1. If "fd" is
1274 * not a valid file descriptor, it just returns. It also clears the
1275 * buffer used to read tickets.
1277 * The return value is not defined.
1284 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1285 (void)fcntl(fd, F_SETLK, &fileUlock);
1287 (void)flock(fd, LOCK_UN);
1290 fd = -1; /* see declaration of fd above */
1292 memset(tfbfr, 0, sizeof(tfbfr));
1297 * tf_gets() is an internal routine. It takes a string "s" and a count
1298 * "n", and reads from the file until either it has read "n" characters,
1299 * or until it reads a null byte. When finished, what has been read exists
1302 * Possible return values are:
1304 * n the number of bytes read (including null terminator)
1305 * when all goes well
1307 * 0 end of file or read error
1309 * TOO_BIG if "count" characters are read and no null is
1310 * encountered. This is an indication that the ticket
1311 * file is seriously ill.
1315 tf_gets(register char *s, int n)
1322 for (count = n - 1; count > 0; --count) {
1323 if (curpos >= sizeof(tfbfr)) {
1324 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1327 if (curpos == lastpos) {
1330 *s = tfbfr[curpos++];
1338 * tf_read() is an internal routine. It takes a string "s" and a count
1339 * "n", and reads from the file until "n" bytes have been read. When
1340 * finished, what has been read exists in "s".
1342 * Possible return values are:
1344 * n the number of bytes read when all goes well
1346 * 0 on end of file or read error
1350 tf_read(register char *s, register int n)
1354 for (count = n; count > 0; --count) {
1355 if (curpos >= sizeof(tfbfr)) {
1356 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1359 if (curpos == lastpos) {
1362 *s++ = tfbfr[curpos++];
1368 * afs_tf_save_cred() appends an incoming ticket to the end of the ticket
1369 * file. You must call afs_tf_init() before calling afs_tf_save_cred().
1371 * The "service", "instance", and "realm" arguments specify the
1372 * server's name; "aticket" contains the credential.
1374 * Returns 0 if all goes well, TKT_FIL_INI if afs_tf_init() wasn't
1375 * called previously, and KFAILURE for anything else that went wrong.
1379 afs_tf_save_cred(struct ktc_principal *aserver,
1380 struct ktc_token *atoken,
1381 struct ktc_principal *aclient)
1383 char realm[MAXKTCREALMLEN + 1];
1384 char junk[MAXKTCNAMELEN];
1385 struct ktc_principal principal;
1386 struct ktc_token token;
1390 int count; /* count for write */
1391 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1393 if (fd < 0) { /* fd is ticket file as set by afs_tf_init */
1397 ucstring(realm, aserver->cell, MAXKTCREALMLEN);
1398 realm[MAXKTCREALMLEN] = '\0';
1400 /* Look for a duplicate ticket */
1401 (void)lseek(fd, (off_t) 0L, 0);
1402 curpos = sizeof(tfbfr);
1404 if (afs_tf_get_pname(junk) || strcmp(junk, aclient->name)
1405 || afs_tf_get_pinst(junk) || strcmp(junk, aclient->instance))
1409 start = lseek(fd, (off_t) 0L, 1) - lastpos + curpos;
1410 status = afs_tf_get_cred(&principal, &token);
1411 } while (status == 0
1412 && (strcmp(aserver->name, principal.name) != 0
1413 || strcmp(aserver->instance, principal.instance) != 0
1414 || strcmp(aserver->cell, principal.cell) != 0));
1417 * Two tickets for the same user authenticating to the same service
1418 * should be the same length, but we check here just to make sure.
1420 if (status == 0 && token.ticketLen != atoken->ticketLen)
1422 if (status && status != EOF)
1425 /* Position over the credential we just matched (or the EOF) */
1426 lseek(fd, start, 0);
1427 curpos = lastpos = sizeof(tfbfr);
1429 /* Write the ticket and associated data */
1431 count = strlen(aserver->name) + 1;
1432 if (write(fd, aserver->name, count) != count)
1435 count = strlen(aserver->instance) + 1;
1436 if (write(fd, aserver->instance, count) != count)
1439 count = strlen(realm) + 1;
1440 if (write(fd, realm, count) != count)
1443 if (write(fd, (char *)&atoken->sessionKey, 8) != 8)
1446 lifetime = time_to_life(atoken->startTime, atoken->endTime);
1447 if (write(fd, (char *)&lifetime, sizeof(int)) != sizeof(int))
1450 kvno = atoken->kvno;
1451 if (write(fd, (char *)&kvno, sizeof(int)) != sizeof(int))
1454 if (write(fd, (char *)&(atoken->ticketLen), sizeof(int)) != sizeof(int))
1457 count = atoken->ticketLen;
1458 if (write(fd, atoken->ticket, count) != count)
1461 mit_compat = atoken->startTime;
1462 if (write(fd, (char *)&mit_compat, sizeof(mit_compat))
1463 != sizeof(mit_compat))
1466 /* Actually, we should check each write for success */
1473 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
1476 * For copying and distribution information, please see the file
1477 * <mit-copyright.h>.
1481 * This routine is used to generate the name of the file that holds
1482 * the user's cache of server tickets and associated session keys.
1484 * If it is set, krb_ticket_string contains the ticket file name.
1485 * Otherwise, the filename is constructed as follows:
1487 * If it is set, the environment variable "KRBTKFILE" will be used as
1488 * the ticket file name. Otherwise TKT_ROOT (defined in "krb.h") and
1489 * the user's uid are concatenated to produce the ticket file name
1490 * (e.g., "/tmp/tkt123"). A pointer to the string containing the ticket
1491 * file name is returned.
1494 static char krb_ticket_string[4096] = "";
1497 ktc_tkt_string(void)
1499 return ktc_tkt_string_uid(getuid());
1503 ktc_tkt_string_uid(afs_uint32 uid)
1508 if (!*krb_ticket_string) {
1509 if ((env = getenv("KRBTKFILE"))) {
1510 (void)strncpy(krb_ticket_string, env,
1511 sizeof(krb_ticket_string) - 1);
1512 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1514 /* 32 bits of signed integer will always fit in 11 characters
1515 * (including the sign), so no need to worry about overflow */
1516 (void)sprintf(krb_ticket_string, "%s%d", TKT_ROOT, uid);
1519 UNLOCK_GLOBAL_MUTEX;
1520 return krb_ticket_string;
1524 * This routine is used to set the name of the file that holds the user's
1525 * cache of server tickets and associated session keys.
1527 * The value passed in is copied into local storage.
1529 * NOTE: This routine should be called during initialization, before other
1530 * Kerberos routines are called; otherwise tkt_string() above may be called
1531 * and return an undesired ticket file name until this routine is called.
1535 ktc_set_tkt_string(char * val)
1539 (void)strncpy(krb_ticket_string, val, sizeof(krb_ticket_string) - 1);
1540 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1541 UNLOCK_GLOBAL_MUTEX;
1546 * tf_create() is used to initialize the ticket store. It creates the
1547 * file to contain the tickets and writes the given user's name "pname"
1548 * and instance "pinst" in the file. in_tkt() returns KSUCCESS on
1549 * success, or KFAILURE if something goes wrong.
1553 afs_tf_create(char *pname, char *pinst)
1558 char *file = ktc_tkt_string();
1567 if (lstat(file, &sbuf) == 0) {
1568 if ((sbuf.st_uid != me && me != 0)
1569 || ((sbuf.st_mode & S_IFMT) != S_IFREG) || sbuf.st_mode & 077) {
1572 /* file already exists, and permissions appear ok, so nuke it */
1573 if ((fd = open(file, O_RDWR, 0)) < 0)
1574 goto out; /* can't zero it, but we can still try truncating it */
1576 memset(zerobuf, 0, sizeof(zerobuf));
1578 for (i = 0; i < sbuf.st_size; i += sizeof(zerobuf))
1579 if (write(fd, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf)) {
1590 /* arrange so the file is owned by the ruid
1591 * (swap real & effective uid if necessary).
1592 * This isn't a security problem, since the ticket file, if it already
1593 * exists, has the right uid (== ruid) and mode. */
1595 if (setreuid(metoo, me) < 0) {
1599 tktfile = creat(file, 0600);
1601 if (setreuid(me, metoo) < 0) {
1602 /* can't switch??? fail! */
1609 count = strlen(pname) + 1;
1610 if (write(tktfile, pname, count) != count) {
1611 (void)close(tktfile);
1614 count = strlen(pinst) + 1;
1615 if (write(tktfile, pinst, count) != count) {
1616 (void)close(tktfile);
1619 (void)close(tktfile);
1624 * dest_tkt() is used to destroy the ticket store upon logout.
1625 * If the ticket file does not exist, dest_tkt() returns RET_TKFIL.
1626 * Otherwise the function returns 0 on success, KFAILURE on
1631 afs_tf_dest_tkt(void)
1633 char *file = ktc_tkt_string();
1639 if (lstat(file, &statb) < 0)
1642 if (!(statb.st_mode & S_IFREG))
1645 if ((fd = open(file, O_RDWR, 0)) < 0)
1648 memset(buf, 0, BUFSIZ);
1650 for (i = 0; i < statb.st_size; i += BUFSIZ)
1651 if (write(fd, buf, BUFSIZ) != BUFSIZ) {
1663 if (errno == ENOENT)
1665 else if (errno != 0)
1673 #ifdef AFS_DARWIN100_ENV
1674 #define environ (*_NSGetEnviron())
1676 extern char **environ;
1681 char fname[256], *prefix = "/ticket/";
1682 char fname5[256], *prefix5 = "FILE:/ticket/krb5cc_";
1684 char **newenv, **senv, **denv;
1687 if (stat("/ticket", &sbuf) == -1) {
1688 prefix = "/tmp/tkt";
1689 prefix5 = "FILE:/tmp/krb5cc_";
1692 pag = ktc_curpag() & 0xffffffff;
1694 sprintf(fname, "%s%d", prefix, getuid());
1695 sprintf(fname5, "%s%d", prefix5, getuid());
1697 sprintf(fname, "%sp%lu", prefix, afs_printable_uint32_lu(pag));
1698 sprintf(fname5, "%sp%lud", prefix5, afs_printable_uint32_lu(pag));
1700 ktc_set_tkt_string(fname);
1702 for (senv = environ, numenv = 0; *senv; senv++)
1704 newenv = (char **)malloc((numenv + 2) * sizeof(char *));
1706 for (senv = environ, denv = newenv; *senv; senv++) {
1707 if (strncmp(*senv, "KRBTKFILE=", 10) != 0 &&
1708 strncmp(*senv, "KRB5CCNAME=", 11) != 0)
1712 *denv = malloc(10+11 + strlen(fname) + strlen(fname5) + 2);
1713 strcpy(*denv, "KRBTKFILE=");
1714 strcat(*denv, fname);
1715 *(denv+1) = *denv + strlen(*denv) + 1;
1717 strcpy(*denv, "KRB5CCNAME=");
1718 strcat(*denv, fname5);
1721 UNLOCK_GLOBAL_MUTEX;
1726 * BLETCH! We have to invoke the entire afsconf package just to
1727 * find out what the local cell is.
1733 struct afsconf_dir *conf;
1735 if ((conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))
1736 || (conf = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH))) {
1737 code = afsconf_GetLocalCell(conf, lcell, sizeof(lcell));
1738 afsconf_Close(conf);
1740 if (!conf || code) {
1741 printf("** Can't determine local cell name!\n");