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) */
76 #if defined(LINUX_KEYRING_SUPPORT) && defined(HAVE_SESSION_TO_PARENT)
77 #include <sys/syscall.h>
78 #define KEYCTL_SESSION_TO_PARENT 18
86 /* AFS_KERBEROS_ENV is now conditionally defined in the Makefile */
87 #define AFS_KERBEROS_ENV
90 #ifdef AFS_KERBEROS_ENV
93 #include <afs/cellconfig.h>
94 static char lcell[MAXCELLCHARS];
96 #define TKT_ROOT "/tmp/tkt"
101 /* Definitions for ticket file utilities */
105 /* Error codes returned by ticket file utilities */
106 #define NO_TKT_FIL 76 /* No ticket file found */
107 #define TKT_FIL_ACC 77 /* Couldn't access tkt file */
108 #define TKT_FIL_LCK 78 /* Couldn't lock ticket file */
109 #define TKT_FIL_FMT 79 /* Bad ticket file format */
110 #define TKT_FIL_INI 80 /* afs_tf_init not called first */
112 /* Values returned by get_credentials */
113 #define RET_TKFIL 21 /* Can't read ticket file */
122 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
123 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0, 0 };
124 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0, 0 };
125 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0, 0 };
128 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0 };
129 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0 };
130 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0 };
137 /* the following routines aren't static anymore on behalf of the kerberos IV
138 * compatibility library built in subtree krb.
140 int afs_tf_init(char *, int);
141 int afs_tf_get_pname(char *);
142 int afs_tf_get_pinst(char *);
143 int afs_tf_get_cred(struct ktc_principal *, struct ktc_token *);
144 int afs_tf_save_cred(struct ktc_principal *, struct ktc_token *,
145 struct ktc_principal *);
146 int afs_tf_close(void);
147 int afs_tf_create(char *, char *);
148 int afs_tf_dest_tkt(void);
149 static void ktc_LocalCell(void);
150 #endif /* AFS_KERBEROS_ENV */
153 #define PIOCTL afs_pioctl
154 #elif defined(UKERNEL)
155 #define PIOCTL(A,B,C,D) call_syscall(AFSCALL_PIOCTL,A,B,C,D)
157 #define PIOCTL pioctl
160 #if !defined(UKERNEL)
161 /* this is a structure used to communicate with the afs cache mgr, but is
162 * otherwise irrelevant */
164 afs_int32 AuthHandle;
165 char HandShakeKey[8];
167 afs_int32 BeginTimestamp;
168 afs_int32 EndTimestamp;
170 #endif /* !defined(UKERNEL) */
172 #define MAXLOCALTOKENS 4
176 struct ktc_principal server;
177 struct ktc_principal client;
178 struct ktc_token token;
179 } local_tokens[MAXLOCALTOKENS] = { {
186 #define MAXPIOCTLTOKENLEN \
187 (3*sizeof(afs_int32)+MAXKTCTICKETLEN+sizeof(struct ClearToken)+MAXKTCREALMLEN)
190 SetToken(struct ktc_principal *aserver, struct ktc_token *atoken,
191 struct ktc_principal *aclient, afs_int32 flags)
193 struct ViceIoctl iob;
194 char tbuffer[MAXPIOCTLTOKENLEN];
196 struct ClearToken ct;
197 register afs_int32 code;
200 if (strcmp(aserver->name, "afs") != 0) {
203 for (i = 0; i < MAXLOCALTOKENS; i++)
204 if (local_tokens[i].valid) {
205 if ((strcmp(local_tokens[i].server.name, aserver->name) == 0)
208 (local_tokens[i].server.instance,
209 aserver->instance) == 0)
210 && (strcmp(local_tokens[i].server.cell, aserver->cell) ==
212 found = i; /* replace existing entry */
214 } else /* valid, but no match */
217 found = i; /* remember this empty slot */
220 memcpy(&local_tokens[found].token, atoken, sizeof(struct ktc_token));
221 local_tokens[found].server = *aserver;
222 local_tokens[found].client = *aclient;
223 local_tokens[found].valid = 1;
226 tp = tbuffer; /* start copying here */
227 if ((atoken->ticketLen < MINKTCTICKETLEN)
228 || (atoken->ticketLen > MAXKTCTICKETLEN))
230 memcpy(tp, &atoken->ticketLen, sizeof(afs_int32)); /* copy in ticket length */
231 tp += sizeof(afs_int32);
232 memcpy(tp, atoken->ticket, atoken->ticketLen); /* copy in ticket */
233 tp += atoken->ticketLen;
234 /* next, copy in the "clear token", describing who we are */
235 ct.AuthHandle = atoken->kvno; /* hide auth handle here */
236 memcpy(ct.HandShakeKey, &atoken->sessionKey, 8);
238 ct.BeginTimestamp = atoken->startTime;
239 ct.EndTimestamp = atoken->endTime;
240 if (ct.BeginTimestamp == 0)
241 ct.BeginTimestamp = 1;
243 if ((strlen(aclient->name) > strlen("AFS ID "))
244 && (aclient->instance[0] == 0)) {
246 afs_int32 viceId = 0;
247 char *cp = aclient->name + strlen("AFS ID ");
254 viceId = viceId * 10 + (int)(*cp - '0');
259 ct.ViceId = viceId * sign; /* OK to let any value here? */
260 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 0)
261 ct.BeginTimestamp++; /* force lifetime to be odd */
264 ct.ViceId = getuid(); /* wrong, but works in primary cell */
265 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1)
266 ct.BeginTimestamp++; /* force lifetime to be even */
271 * Information needed by the user space cache manager
273 get_user_struct()->u_expiration = ct.EndTimestamp;
274 get_user_struct()->u_viceid = ct.ViceId;
277 temp = sizeof(struct ClearToken);
278 memcpy(tp, &temp, sizeof(afs_int32));
279 tp += sizeof(afs_int32);
280 memcpy(tp, &ct, sizeof(struct ClearToken));
281 tp += sizeof(struct ClearToken);
283 /* next copy in primary flag */
287 * The following means that setpag will happen inside afs just before
288 * the authentication to prevent the setpag/klog race condition.
290 * The following means that setpag will affect the parent process as
291 * well as the current process.
293 if (flags & AFS_SETTOK_SETPAG)
296 memcpy(tp, &temp, sizeof(afs_int32));
297 tp += sizeof(afs_int32);
299 /* finally copy in the cell name */
300 temp = strlen(aserver->cell);
301 if (temp >= MAXKTCREALMLEN)
303 strcpy(tp, aserver->cell);
306 /* now setup for the pioctl */
308 iob.in_size = tp - tbuffer;
310 iob.out_size = sizeof(tbuffer);
312 #if defined(NO_AFS_CLIENT)
316 if ((tkfile = getenv("TKTFILE"))
319 open(tkfile, O_WRONLY | O_APPEND | O_TRUNC | O_CREAT,
321 printf("Writing ticket to: %s\n", tkfile);
322 code = (write(fd, iob.in, iob.in_size) != iob.in_size);
325 code = KTC_PIOCTLFAIL;
327 #else /* NO_AFS_CLIENT */
328 code = PIOCTL(0, VIOCSETTOK, &iob, 0);
329 #if defined(LINUX_KEYRING_SUPPORT) && defined(HAVE_SESSION_TO_PARENT)
331 * If we're using keyring based PAGs and the SESSION_TO_PARENT keyctl
332 * is available, use it to copy the session keyring to the parent process
334 if (flags & AFS_SETTOK_SETPAG)
335 syscall(SYS_keyctl, KEYCTL_SESSION_TO_PARENT);
337 #endif /* NO_AFS_CLIENT */
339 return KTC_PIOCTLFAIL;
344 ktc_SetToken(struct ktc_principal *aserver,
345 struct ktc_token *atoken,
346 struct ktc_principal *aclient,
352 #ifdef AFS_KERBEROS_ENV
356 if ( /*!strcmp(aclient->cell, lcell) && this would only store local creds */
357 (strcmp(aserver->name, "AuthServer")
358 || strcmp(aserver->instance, "Admin"))) {
359 if (strcmp(aserver->name, "krbtgt") == 0) {
360 static char lrealm[MAXKTCREALMLEN];
363 ucstring(lrealm, lcell, MAXKTCREALMLEN);
364 if (strcmp(aserver->instance, lrealm) == 0) {
365 afs_tf_create(aclient->name, aclient->instance);
369 code = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
370 if (code == NO_TKT_FIL) {
371 (void)afs_tf_create(aclient->name, aclient->instance);
372 code = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
376 afs_tf_save_cred(aserver, atoken, aclient);
382 #endif /* NO_AFS_CLIENT */
386 #ifndef NO_AFS_CLIENT
387 code = SetToken(aserver, atoken, aclient, flags);
392 else if (code == KTC_PIOCTLFAIL)
400 return KTC_PIOCTLFAIL;
402 #endif /* NO_AFS_CLIENT */
407 /* get token, given server we need and token buffer. aclient will eventually
408 * be set to our identity to the server.
411 ktc_GetToken(struct ktc_principal *aserver, struct ktc_token *atoken,
412 int atokenLen, struct ktc_principal *aclient)
414 struct ViceIoctl iob;
415 char tbuffer[MAXPIOCTLTOKENLEN];
416 register afs_int32 code = 0;
418 char *stp, *cellp; /* secret token ptr */
419 struct ClearToken ct;
422 int maxLen; /* biggest ticket we can copy */
423 int tktLen; /* server ticket length */
424 #ifdef AFS_KERBEROS_ENV
430 #ifdef AFS_KERBEROS_ENV
434 #ifndef NO_AFS_CLIENT
435 if (strcmp(aserver->name, "afs") != 0)
436 #endif /* NO_AFS_CLIENT */
439 /* try the local tokens */
440 for (i = 0; i < MAXLOCALTOKENS; i++)
441 if (local_tokens[i].valid
442 && (strcmp(local_tokens[i].server.name, aserver->name) == 0)
443 && (strcmp(local_tokens[i].server.instance, aserver->instance)
445 && (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
446 memcpy(atoken, &local_tokens[i].token,
447 min(atokenLen, sizeof(struct ktc_token)));
449 *aclient = local_tokens[i].client;
453 #ifdef AFS_KERBEROS_ENV
454 if (!afs_tf_init(ktc_tkt_string(), R_TKT_FIL)) {
456 if (!afs_tf_get_pname(aclient->name)
457 && !afs_tf_get_pinst(aclient->instance))
460 char tmpstring[MAXHOSTCHARS];
461 afs_tf_get_pname(tmpstring);
462 afs_tf_get_pinst(tmpstring);
467 struct ktc_principal cprincipal;
468 struct ktc_token ctoken;
470 while (!afs_tf_get_cred(&cprincipal, &ctoken)) {
471 if (strcmp(cprincipal.name, aserver->name) == 0
472 && strcmp(cprincipal.instance, aserver->instance) == 0
473 && strcmp(cprincipal.cell, aserver->cell) == 0) {
476 strcpy(aclient->cell, lcell);
477 memcpy(atoken, &ctoken,
478 min(atokenLen, sizeof(struct ktc_token)));
491 #ifndef NO_AFS_CLIENT
492 for (index = 0; index < 200; index++) { /* sanity check in case pioctl fails */
493 iob.in = (char *)&index;
494 iob.in_size = sizeof(afs_int32);
496 iob.out_size = sizeof(tbuffer);
498 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
501 /* failed to retrieve specified token */
502 if (code < 0 && errno == EDOM) {
507 /* token retrieved; parse buffer */
510 /* get ticket length */
511 memcpy(&temp, tp, sizeof(afs_int32));
513 tp += sizeof(afs_int32);
515 /* remember where ticket is and skip over it */
519 /* get size of clear token and verify */
520 memcpy(&temp, tp, sizeof(afs_int32));
521 if (temp != sizeof(struct ClearToken)) {
525 tp += sizeof(afs_int32);
527 /* copy clear token */
528 memcpy(&ct, tp, temp);
531 /* skip over primary flag */
532 tp += sizeof(afs_int32);
534 /* remember where cell name is */
537 if ((strcmp(cellp, aserver->cell) == 0)
538 #ifdef AFS_KERBEROS_ENV
539 || (*aserver->cell == '\0' && strcmp(cellp, lcell) == 0)
542 /* got token for cell; check that it will fit */
544 atokenLen - sizeof(struct ktc_token) + MAXKTCTICKETLEN;
545 if (maxLen < tktLen) {
550 /* set return values */
551 memcpy(atoken->ticket, stp, tktLen);
552 atoken->startTime = ct.BeginTimestamp;
553 atoken->endTime = ct.EndTimestamp;
554 if (ct.AuthHandle == -1) {
557 atoken->kvno = ct.AuthHandle;
558 memcpy(&atoken->sessionKey, ct.HandShakeKey,
559 sizeof(struct ktc_encryptionKey));
560 atoken->ticketLen = tktLen;
563 strcpy(aclient->cell, cellp);
564 aclient->instance[0] = 0;
566 if ((atoken->kvno == 999) || /* old style bcrypt ticket */
567 (ct.BeginTimestamp && /* new w/ prserver lookup */
568 (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1))) {
569 sprintf(aclient->name, "AFS ID %d", ct.ViceId);
571 sprintf(aclient->name, "Unix UID %d", ct.ViceId);
579 #endif /* NO_AFS_CLIENT */
582 if ((code < 0) && (errno == EINVAL))
584 return KTC_PIOCTLFAIL; /* probable cause */
588 * Forget tokens for this server and the calling user.
589 * NOT IMPLEMENTED YET!
591 #ifndef NO_AFS_CLIENT
593 ktc_ForgetToken(struct ktc_principal *aserver)
598 rc = ktc_ForgetAllTokens(); /* bogus, but better */
602 #endif /* NO_AFS_CLIENT */
604 /* ktc_ListTokens - list all tokens. start aprevIndex at 0, it returns the
605 * next rock in (*aindex). (*aserver) is set to the relevant ticket on
609 ktc_ListTokens(int aprevIndex,
611 struct ktc_principal *aserver)
613 struct ViceIoctl iob;
614 char tbuffer[MAXPIOCTLTOKENLEN];
615 register afs_int32 code = 0 ;
617 afs_int32 temp, index;
619 memset(tbuffer, 0, sizeof(tbuffer));
627 #endif /* NO_AFS_CLIENT */
628 #ifdef AFS_KERBEROS_ENV
631 struct ktc_principal cprincipal;
632 struct ktc_token ctoken;
634 if (afs_tf_init(ktc_tkt_string(), R_TKT_FIL)
635 || afs_tf_get_pname(tbuffer) || afs_tf_get_pinst(tbuffer)) {
641 for (i = 214; i < index; i++) {
642 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
650 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
657 #ifndef NO_AFS_CLIENT
658 if (!strcmp(cprincipal.name, "afs") && cprincipal.instance[0] == 0) {
661 #endif /* NO_AFS_CLIENT */
663 for (i = 0; i < MAXLOCALTOKENS; i++) {
664 if (!strcmp(cprincipal.name, local_tokens[i].server.name)
665 && !strcmp(cprincipal.instance,
666 local_tokens[i].server.instance)
667 && !strcmp(cprincipal.cell, local_tokens[i].server.cell)) {
672 *aserver = cprincipal;
680 #ifndef NO_AFS_CLIENT
681 if (index >= 123) { /* special hack for returning TCS */
682 while (index - 123 < MAXLOCALTOKENS) {
683 if (local_tokens[index - 123].valid) {
684 *aserver = local_tokens[index - 123].server;
692 #ifdef AFS_KERBEROS_ENV
693 return ktc_ListTokens(214, aindex, aserver);
699 /* get tokens from the kernel */
700 while (index < 200) { /* sanity check in case pioctl fails */
701 iob.in = (char *)&index;
702 iob.in_size = sizeof(afs_int32);
704 iob.out_size = sizeof(tbuffer);
705 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
706 if (code < 0 && errno == EDOM) {
709 rc = ktc_ListTokens(123, aindex, aserver);
718 break; /* got a ticket */
719 /* otherwise we should skip this ticket slot */
726 return KTC_PIOCTLFAIL;
732 /* next iterator determined by earlier loop */
735 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of secret token */
736 tp += sizeof(afs_int32);
737 tp += temp; /* skip ticket for now */
738 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of clear token */
739 if (temp != sizeof(struct ClearToken)) {
743 tp += sizeof(afs_int32); /* skip length */
744 tp += temp; /* skip clear token itself */
745 tp += sizeof(afs_int32); /* skip primary flag */
746 /* tp now points to the cell name */
747 strcpy(aserver->cell, tp);
748 aserver->instance[0] = 0;
749 strcpy(aserver->name, "afs");
750 #endif /* NO_AFS_CLIENT */
758 struct ViceIoctl iob;
759 register afs_int32 code;
762 for (i = 0; i < MAXLOCALTOKENS; i++)
763 local_tokens[i].valid = 0;
769 #ifndef NO_AFS_CLIENT
770 code = PIOCTL(0, VIOCUNPAG, &iob, 0);
772 return KTC_PIOCTLFAIL;
773 #endif /* NO_AFS_CLIENT */
778 ktc_ForgetAllTokens(void)
783 #ifdef AFS_KERBEROS_ENV
784 (void)afs_tf_dest_tkt();
791 else if (ocode == KTC_PIOCTLFAIL)
796 return KTC_PIOCTLFAIL;
802 /* ktc_OldPioctl - returns a boolean true if the kernel supports only the old
803 * pioctl interface for delivering AFS tickets to the cache manager. */
815 struct ViceIoctl iob;
818 /* now setup for the pioctl */
821 iob.out = (caddr_t) &pag;
822 iob.out_size = sizeof(afs_uint32);
824 code = PIOCTL(0, VIOC_GETPAG, &iob, 0);
826 #if defined(AFS_AIX52_ENV)
827 code = getpagvalue("afs");
828 if (code < 0 && errno == EINVAL)
831 #elif defined(AFS_AIX51_ENV)
834 gid_t groups[NGROUPS_MAX];
836 afs_uint32 h, l, ret;
838 #ifdef AFS_LINUX26_ENV
842 ngroups = getgroups(sizeof groups / sizeof groups[0], groups);
844 #ifdef AFS_LINUX26_ENV
845 /* check for AFS_LINUX26_ONEGROUP_ENV PAGs */
846 for (i = 0; i < ngroups; i++) {
847 if (((groups[i] >> 24) & 0xff) == 'A') {
856 g0 = groups[0] & 0xffff;
857 g1 = groups[1] & 0xffff;
860 if (g0 < 0xc000 && g1 < 0xc000) {
861 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
863 h = (g1 >> 14) + h + h + h;
864 ret = ((h << 28) | l);
865 /* Additional testing */
866 if (((ret >> 24) & 0xff) == 'A')
878 #ifdef AFS_KERBEROS_ENV
880 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
882 * For copying and distribution information, please see the file
889 #include <sys/types.h>
890 #include <sys/stat.h>
891 #include <sys/file.h>
896 #define TF_LCK_RETRY ((unsigned)2) /* seconds to sleep before
897 * retry if ticket file is
901 * fd must be initialized to something that won't ever occur as a real
902 * file descriptor. Since open(2) returns only non-negative numbers as
903 * valid file descriptors, and afs_tf_init always stuffs the return value
904 * from open in here even if it is an error flag, we must
905 * a. Initialize fd to a negative number, to indicate that it is
906 * not initially valid.
907 * b. When checking for a valid fd, assume that negative values
908 * are invalid (ie. when deciding whether afs_tf_init has been
910 * c. In tf_close, be sure it gets reinitialized to a negative
914 static int curpos; /* Position in tfbfr */
915 static int lastpos; /* End of tfbfr */
916 static char tfbfr[BUFSIZ]; /* Buffer for ticket data */
918 static int tf_gets(char *, int);
919 static int tf_read(char *, int);
922 * This file contains routines for manipulating the ticket cache file.
924 * The ticket file is in the following format:
926 * principal's name (null-terminated string)
927 * principal's instance (null-terminated string)
934 * Where "CREDENTIAL_x" consists of the following fixed-length
935 * fields from the CREDENTIALS structure (see "krb.h"):
937 * char service[MAXKTCNAMELEN]
938 * char instance[MAXKTCNAMELEN]
939 * char realm[REALM_SZ]
944 * afs_int32 issue_date
946 * Short description of routines:
948 * afs_tf_init() opens the ticket file and locks it.
950 * afs_tf_get_pname() returns the principal's name.
952 * afs_tf_get_pinst() returns the principal's instance (may be null).
954 * afs_tf_get_cred() returns the next CREDENTIALS record.
956 * afs_tf_save_cred() appends a new CREDENTIAL record to the ticket file.
958 * afs_tf_close() closes the ticket file and releases the lock.
960 * tf_gets() returns the next null-terminated string. It's an internal
961 * routine used by afs_tf_get_pname(), afs_tf_get_pinst(), and
964 * tf_read() reads a given number of bytes. It's an internal routine
965 * used by afs_tf_get_cred().
969 * afs_tf_init() should be called before the other ticket file routines.
970 * It takes the name of the ticket file to use, "tf_name", and a
971 * read/write flag "rw" as arguments.
973 * It tries to open the ticket file, checks the mode, and if everything
974 * is okay, locks the file. If it's opened for reading, the lock is
975 * shared. If it's opened for writing, the lock is exclusive.
977 * Returns 0 if all went well, otherwise one of the following:
979 * NO_TKT_FIL - file wasn't there
980 * TKT_FIL_ACC - file was in wrong mode, etc.
981 * TKT_FIL_LCK - couldn't lock the file, even after a retry
985 afs_tf_init(char *tf_name, int rw)
989 struct stat stat_buf;
1001 if (lstat(tf_name, &stat_buf) < 0)
1009 if ((stat_buf.st_uid != me && me != 0)
1010 || ((stat_buf.st_mode & S_IFMT) != S_IFREG))
1014 * If "wflag" is set, open the ticket file in append-writeonly mode
1015 * and lock the ticket file in exclusive mode. If unable to lock
1016 * the file, sleep and try again. If we fail again, return with the
1017 * proper error message.
1020 curpos = sizeof(tfbfr);
1023 fd = open(tf_name, O_RDWR, 0600);
1027 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1028 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1029 sleep(TF_LCK_RETRY);
1030 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1032 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1033 sleep(TF_LCK_RETRY);
1034 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1044 * Otherwise "wflag" is not set and the ticket file should be opened
1045 * for read-only operations and locked for shared access.
1048 fd = open(tf_name, O_RDONLY, 0600);
1052 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1053 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1054 sleep(TF_LCK_RETRY);
1055 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1057 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1058 sleep(TF_LCK_RETRY);
1059 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1070 * afs_tf_get_pname() reads the principal's name from the ticket file. It
1071 * should only be called after afs_tf_init() has been called. The
1072 * principal's name is filled into the "p" parameter. If all goes well,
1073 * 0 is returned. If afs_tf_init() wasn't called, TKT_FIL_INI is
1074 * returned. If the name was null, or EOF was encountered, or the name
1075 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned.
1079 afs_tf_get_pname(char *p)
1084 if (tf_gets(p, MAXKTCNAMELEN) < 2) /* can't be just a null */
1090 * afs_tf_get_pinst() reads the principal's instance from a ticket file.
1091 * It should only be called after afs_tf_init() and afs_tf_get_pname() have
1092 * been called. The instance is filled into the "inst" parameter. If all
1093 * goes well, 0 is returned. If afs_tf_init() wasn't called,
1094 * TKT_FIL_INI is returned. If EOF was encountered, or the instance
1095 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned. Note that the
1096 * instance may be null.
1100 afs_tf_get_pinst(char *inst)
1105 if (tf_gets(inst, MAXKTCNAMELEN) < 1)
1111 * afs_tf_get_cred() reads a CREDENTIALS record from a ticket file and fills
1112 * in the given structure "c". It should only be called after afs_tf_init(),
1113 * afs_tf_get_pname(), and afs_tf_get_pinst() have been called. If all goes
1114 * well, 0 is returned. Possible error codes are:
1116 * TKT_FIL_INI - afs_tf_init wasn't called first
1117 * TKT_FIL_FMT - bad format
1118 * EOF - end of file encountered
1122 afs_tf_get_cred(struct ktc_principal *principal, struct ktc_token *token)
1126 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1131 if ((k_errno = tf_gets(principal->name, MAXKTCNAMELEN)) < 2)
1134 case 1: /* can't be just a null */
1139 if ((k_errno = tf_gets(principal->instance, MAXKTCNAMELEN)) < 1)
1146 if ((k_errno = tf_gets(principal->cell, MAXKTCREALMLEN)) < 2)
1149 case 1: /* can't be just a null */
1154 lcstring(principal->cell, principal->cell, MAXKTCREALMLEN);
1155 if (tf_read((char *)&(token->sessionKey), 8) < 1
1156 || tf_read((char *)&(lifetime), sizeof(lifetime)) < 1
1157 || tf_read((char *)&(kvno), sizeof(kvno)) < 1
1158 || tf_read((char *)&(token->ticketLen), sizeof(token->ticketLen))
1160 /* don't try to read a silly amount into ticket->dat */
1161 token->ticketLen > MAXKTCTICKETLEN
1162 || tf_read((char *)(token->ticket), token->ticketLen) < 1
1163 || tf_read((char *)&mit_compat, sizeof(mit_compat)) < 1) {
1166 token->startTime = mit_compat;
1167 token->endTime = life_to_time(token->startTime, lifetime);
1173 * tf_close() closes the ticket file and sets "fd" to -1. If "fd" is
1174 * not a valid file descriptor, it just returns. It also clears the
1175 * buffer used to read tickets.
1177 * The return value is not defined.
1184 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1185 (void)fcntl(fd, F_SETLK, &fileUlock);
1187 (void)flock(fd, LOCK_UN);
1190 fd = -1; /* see declaration of fd above */
1192 memset(tfbfr, 0, sizeof(tfbfr));
1197 * tf_gets() is an internal routine. It takes a string "s" and a count
1198 * "n", and reads from the file until either it has read "n" characters,
1199 * or until it reads a null byte. When finished, what has been read exists
1202 * Possible return values are:
1204 * n the number of bytes read (including null terminator)
1205 * when all goes well
1207 * 0 end of file or read error
1209 * TOO_BIG if "count" characters are read and no null is
1210 * encountered. This is an indication that the ticket
1211 * file is seriously ill.
1215 tf_gets(register char *s, int n)
1222 for (count = n - 1; count > 0; --count) {
1223 if (curpos >= sizeof(tfbfr)) {
1224 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1227 if (curpos == lastpos) {
1230 *s = tfbfr[curpos++];
1238 * tf_read() is an internal routine. It takes a string "s" and a count
1239 * "n", and reads from the file until "n" bytes have been read. When
1240 * finished, what has been read exists in "s".
1242 * Possible return values are:
1244 * n the number of bytes read when all goes well
1246 * 0 on end of file or read error
1250 tf_read(register char *s, register int n)
1254 for (count = n; count > 0; --count) {
1255 if (curpos >= sizeof(tfbfr)) {
1256 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1259 if (curpos == lastpos) {
1262 *s++ = tfbfr[curpos++];
1268 * afs_tf_save_cred() appends an incoming ticket to the end of the ticket
1269 * file. You must call afs_tf_init() before calling afs_tf_save_cred().
1271 * The "service", "instance", and "realm" arguments specify the
1272 * server's name; "aticket" contains the credential.
1274 * Returns 0 if all goes well, TKT_FIL_INI if afs_tf_init() wasn't
1275 * called previously, and KFAILURE for anything else that went wrong.
1279 afs_tf_save_cred(struct ktc_principal *aserver,
1280 struct ktc_token *atoken,
1281 struct ktc_principal *aclient)
1283 char realm[MAXKTCREALMLEN + 1];
1284 char junk[MAXKTCNAMELEN];
1285 struct ktc_principal principal;
1286 struct ktc_token token;
1290 int count; /* count for write */
1291 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1293 if (fd < 0) { /* fd is ticket file as set by afs_tf_init */
1297 ucstring(realm, aserver->cell, MAXKTCREALMLEN);
1298 realm[MAXKTCREALMLEN] = '\0';
1300 /* Look for a duplicate ticket */
1301 (void)lseek(fd, (off_t) 0L, 0);
1302 curpos = sizeof(tfbfr);
1304 if (afs_tf_get_pname(junk) || strcmp(junk, aclient->name)
1305 || afs_tf_get_pinst(junk) || strcmp(junk, aclient->instance))
1309 start = lseek(fd, (off_t) 0L, 1) - lastpos + curpos;
1310 status = afs_tf_get_cred(&principal, &token);
1311 } while (status == 0
1312 && (strcmp(aserver->name, principal.name) != 0
1313 || strcmp(aserver->instance, principal.instance) != 0
1314 || strcmp(aserver->cell, principal.cell) != 0));
1317 * Two tickets for the same user authenticating to the same service
1318 * should be the same length, but we check here just to make sure.
1320 if (status == 0 && token.ticketLen != atoken->ticketLen)
1322 if (status && status != EOF)
1325 /* Position over the credential we just matched (or the EOF) */
1326 lseek(fd, start, 0);
1327 curpos = lastpos = sizeof(tfbfr);
1329 /* Write the ticket and associated data */
1331 count = strlen(aserver->name) + 1;
1332 if (write(fd, aserver->name, count) != count)
1335 count = strlen(aserver->instance) + 1;
1336 if (write(fd, aserver->instance, count) != count)
1339 count = strlen(realm) + 1;
1340 if (write(fd, realm, count) != count)
1343 if (write(fd, (char *)&atoken->sessionKey, 8) != 8)
1346 lifetime = time_to_life(atoken->startTime, atoken->endTime);
1347 if (write(fd, (char *)&lifetime, sizeof(int)) != sizeof(int))
1350 kvno = atoken->kvno;
1351 if (write(fd, (char *)&kvno, sizeof(int)) != sizeof(int))
1354 if (write(fd, (char *)&(atoken->ticketLen), sizeof(int)) != sizeof(int))
1357 count = atoken->ticketLen;
1358 if (write(fd, atoken->ticket, count) != count)
1361 mit_compat = atoken->startTime;
1362 if (write(fd, (char *)&mit_compat, sizeof(mit_compat))
1363 != sizeof(mit_compat))
1366 /* Actually, we should check each write for success */
1373 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
1376 * For copying and distribution information, please see the file
1377 * <mit-copyright.h>.
1381 * This routine is used to generate the name of the file that holds
1382 * the user's cache of server tickets and associated session keys.
1384 * If it is set, krb_ticket_string contains the ticket file name.
1385 * Otherwise, the filename is constructed as follows:
1387 * If it is set, the environment variable "KRBTKFILE" will be used as
1388 * the ticket file name. Otherwise TKT_ROOT (defined in "krb.h") and
1389 * the user's uid are concatenated to produce the ticket file name
1390 * (e.g., "/tmp/tkt123"). A pointer to the string containing the ticket
1391 * file name is returned.
1394 static char krb_ticket_string[4096] = "";
1397 ktc_tkt_string(void)
1399 return ktc_tkt_string_uid(getuid());
1403 ktc_tkt_string_uid(afs_uint32 uid)
1408 if (!*krb_ticket_string) {
1409 if ((env = getenv("KRBTKFILE"))) {
1410 (void)strncpy(krb_ticket_string, env,
1411 sizeof(krb_ticket_string) - 1);
1412 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1414 /* 32 bits of signed integer will always fit in 11 characters
1415 * (including the sign), so no need to worry about overflow */
1416 (void)sprintf(krb_ticket_string, "%s%d", TKT_ROOT, uid);
1419 UNLOCK_GLOBAL_MUTEX;
1420 return krb_ticket_string;
1424 * This routine is used to set the name of the file that holds the user's
1425 * cache of server tickets and associated session keys.
1427 * The value passed in is copied into local storage.
1429 * NOTE: This routine should be called during initialization, before other
1430 * Kerberos routines are called; otherwise tkt_string() above may be called
1431 * and return an undesired ticket file name until this routine is called.
1435 ktc_set_tkt_string(char * val)
1439 (void)strncpy(krb_ticket_string, val, sizeof(krb_ticket_string) - 1);
1440 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1441 UNLOCK_GLOBAL_MUTEX;
1446 * tf_create() is used to initialize the ticket store. It creates the
1447 * file to contain the tickets and writes the given user's name "pname"
1448 * and instance "pinst" in the file. in_tkt() returns KSUCCESS on
1449 * success, or KFAILURE if something goes wrong.
1453 afs_tf_create(char *pname, char *pinst)
1458 char *file = ktc_tkt_string();
1467 if (lstat(file, &sbuf) == 0) {
1468 if ((sbuf.st_uid != me && me != 0)
1469 || ((sbuf.st_mode & S_IFMT) != S_IFREG) || sbuf.st_mode & 077) {
1472 /* file already exists, and permissions appear ok, so nuke it */
1473 if ((fd = open(file, O_RDWR, 0)) < 0)
1474 goto out; /* can't zero it, but we can still try truncating it */
1476 memset(zerobuf, 0, sizeof(zerobuf));
1478 for (i = 0; i < sbuf.st_size; i += sizeof(zerobuf))
1479 if (write(fd, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf)) {
1490 /* arrange so the file is owned by the ruid
1491 * (swap real & effective uid if necessary).
1492 * This isn't a security problem, since the ticket file, if it already
1493 * exists, has the right uid (== ruid) and mode. */
1495 if (setreuid(metoo, me) < 0) {
1499 tktfile = creat(file, 0600);
1501 if (setreuid(me, metoo) < 0) {
1502 /* can't switch??? fail! */
1509 count = strlen(pname) + 1;
1510 if (write(tktfile, pname, count) != count) {
1511 (void)close(tktfile);
1514 count = strlen(pinst) + 1;
1515 if (write(tktfile, pinst, count) != count) {
1516 (void)close(tktfile);
1519 (void)close(tktfile);
1524 * dest_tkt() is used to destroy the ticket store upon logout.
1525 * If the ticket file does not exist, dest_tkt() returns RET_TKFIL.
1526 * Otherwise the function returns 0 on success, KFAILURE on
1531 afs_tf_dest_tkt(void)
1533 char *file = ktc_tkt_string();
1539 if (lstat(file, &statb) < 0)
1542 if (!(statb.st_mode & S_IFREG))
1545 if ((fd = open(file, O_RDWR, 0)) < 0)
1548 memset(buf, 0, BUFSIZ);
1550 for (i = 0; i < statb.st_size; i += BUFSIZ)
1551 if (write(fd, buf, BUFSIZ) != BUFSIZ) {
1563 if (errno == ENOENT)
1565 else if (errno != 0)
1573 #ifdef AFS_DARWIN100_ENV
1574 #define environ (*_NSGetEnviron())
1576 extern char **environ;
1581 char fname[256], *prefix = "/ticket/";
1582 char fname5[256], *prefix5 = "FILE:/ticket/krb5cc_";
1584 char **newenv, **senv, **denv;
1587 if (stat("/ticket", &sbuf) == -1) {
1588 prefix = "/tmp/tkt";
1589 prefix5 = "FILE:/tmp/krb5cc_";
1592 pag = ktc_curpag() & 0xffffffff;
1594 sprintf(fname, "%s%d", prefix, getuid());
1595 sprintf(fname5, "%s%d", prefix5, getuid());
1597 sprintf(fname, "%sp%lu", prefix, afs_printable_uint32_lu(pag));
1598 sprintf(fname5, "%sp%lud", prefix5, afs_printable_uint32_lu(pag));
1600 ktc_set_tkt_string(fname);
1602 for (senv = environ, numenv = 0; *senv; senv++)
1604 newenv = (char **)malloc((numenv + 2) * sizeof(char *));
1606 for (senv = environ, denv = newenv; *senv; senv++) {
1607 if (strncmp(*senv, "KRBTKFILE=", 10) != 0 &&
1608 strncmp(*senv, "KRB5CCNAME=", 11) != 0)
1612 *denv = malloc(10+11 + strlen(fname) + strlen(fname5) + 2);
1613 strcpy(*denv, "KRBTKFILE=");
1614 strcat(*denv, fname);
1615 *(denv+1) = *denv + strlen(*denv) + 1;
1617 strcpy(*denv, "KRB5CCNAME=");
1618 strcat(*denv, fname5);
1621 UNLOCK_GLOBAL_MUTEX;
1626 * BLETCH! We have to invoke the entire afsconf package just to
1627 * find out what the local cell is.
1633 struct afsconf_dir *conf;
1635 if ((conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))
1636 || (conf = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH))) {
1637 code = afsconf_GetLocalCell(conf, lcell, sizeof(lcell));
1638 afsconf_Close(conf);
1640 if (!conf || code) {
1641 printf("** Can't determine local cell name!\n");