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>
23 #include "afs/sysincludes.h"
24 #include "afsincludes.h"
26 #include "afs/pthread_glock.h"
29 #include "afs/venus.h"
30 #include "afs/pthread_glock.h"
31 #include "afs/dirpath.h"
34 #define min(a,b) ((a)<(b)?(a):(b))
35 #endif /* !defined(min) */
37 #else /* defined(UKERNEL) */
44 #include <afs/pthread_glock.h>
45 #include <sys/types.h>
50 #include <sys/ioctl.h>
51 #include <netinet/in.h>
61 #include <sys/lockf.h>
67 #include <afs/venus.h>
68 #include <afs/afsutil.h>
70 #endif /* defined(UKERNEL) */
77 /* AFS_KERBEROS_ENV is now conditionally defined in the Makefile */
78 #define AFS_KERBEROS_ENV
81 #ifdef AFS_KERBEROS_ENV
84 #include "cellconfig.h"
85 static char lcell[MAXCELLCHARS];
87 #define TKT_ROOT "/tmp/tkt"
92 /* Definitions for ticket file utilities */
96 /* Error codes returned by ticket file utilities */
97 #define NO_TKT_FIL 76 /* No ticket file found */
98 #define TKT_FIL_ACC 77 /* Couldn't access tkt file */
99 #define TKT_FIL_LCK 78 /* Couldn't lock ticket file */
100 #define TKT_FIL_FMT 79 /* Bad ticket file format */
101 #define TKT_FIL_INI 80 /* afs_tf_init not called first */
103 /* Values returned by get_credentials */
104 #define RET_TKFIL 21 /* Can't read ticket file */
113 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
114 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0, 0 };
115 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0, 0 };
116 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0, 0 };
119 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0 };
120 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0 };
121 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0 };
128 /* the following routines aren't static anymore on behalf of the kerberos IV
129 * compatibility library built in subtree krb.
131 int afs_tf_init(), afs_tf_get_pname(), afs_tf_get_pinst(), afs_tf_get_cred();
132 int afs_tf_save_cred(), afs_tf_close(), afs_tf_create();
133 int afs_tf_dest_tkt();
134 static void ktc_LocalCell();
135 char *ktc_tkt_string();
136 #endif /* AFS_KERBEROS_ENV */
139 #define PIOCTL afs_pioctl
140 #elif defined(UKERNEL)
141 #define PIOCTL(A,B,C,D) call_syscall(AFSCALL_PIOCTL,A,B,C,D)
143 #define PIOCTL pioctl
147 #ifdef KERNEL_KTC_COMPAT
150 #define KTC_SYSCALL 32
153 /* Kernel call opcode definitions */
154 #define KTC_OPCODE_BASE 4300
155 #define KTC_NO_OP (0+KTC_OPCODE_BASE)
156 #define KTC_SETTOKEN_OP (1+KTC_OPCODE_BASE)
157 #define KTC_GETTOKEN_OP (2+KTC_OPCODE_BASE)
158 #define KTC_LISTTOKENS_OP (3+KTC_OPCODE_BASE)
159 #define KTC_FORGETTOKEN_OP (4+KTC_OPCODE_BASE)
160 #define KTC_FORGETALLTOKENS_OP (5+KTC_OPCODE_BASE)
161 #define KTC_STATUS_OP (6+KTC_OPCODE_BASE)
162 #define KTC_GC_OP (7+KTC_OPCODE_BASE)
164 #define KTC_INTERFACE_VERSION 3
166 /* We have to determine if the kernel supports the ktc system call. To do so
167 * we attempt to execute its noop function. If this is successful we use the
168 * kernel calls in the future otherwise we let the old code run. */
170 /* To safely check to see whether a system call exists we have to intercept the
171 * SIGSYS signal which is caused by executing a non-existant system call. If
172 * it is ignored the syscall routine returns EINVAL. The SIGSYS is reset to
173 * its old value after the return from syscall. */
175 static int kernelKTC = 0;
177 #ifdef AFS_DECOSF_ENV
179 * SIGSYS semantics are broken on Dec AXP OSF/1 v1.2 systems. You need
180 * to ignore SIGTRAP too. It is claimed to be fixed under v1.3, but...
183 #define CHECK_KERNEL \
184 if (kernelKTC == 0) { \
188 old = (int (*)())signal(SIGSYS, SIG_IGN); \
189 old_t = (int (*)())signal(SIGTRAP, SIG_IGN); \
190 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
192 signal(SIGSYS, old); \
193 signal(SIGTRAP, old_t); \
194 if (code == 0) kernelKTC = 1; \
195 else kernelKTC = 2; \
196 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
199 #else /* AFS_DECOSF_ENV */
201 #define CHECK_KERNEL \
202 if (kernelKTC == 0) { \
205 old = (int (*)())signal(SIGSYS, SIG_IGN); \
206 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
208 signal(SIGSYS, old); \
209 if (code == 0) kernelKTC = 1; \
210 else kernelKTC = 2; \
211 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
213 #endif /* AFS_DECOSF_ENV */
215 #define TRY_KERNEL(cmd,a1,a2,a3,a4) \
217 if (kernelKTC == 1) \
218 return syscall (KTC_SYSCALL, cmd, \
219 KTC_INTERFACE_VERSION, a1,a2,a3,a4); \
223 #define TRY_KERNEL(cmd,a1,a2,a3,a4)
224 #endif /* KERNEL_KTC_COMPAT */
226 #if !defined(UKERNEL)
227 /* this is a structure used to communicate with the afs cache mgr, but is
228 * otherwise irrelevant */
230 afs_int32 AuthHandle;
231 char HandShakeKey[8];
233 afs_int32 BeginTimestamp;
234 afs_int32 EndTimestamp;
236 #endif /* !defined(UKERNEL) */
238 #define MAXLOCALTOKENS 4
242 struct ktc_principal server;
243 struct ktc_principal client;
244 struct ktc_token token;
245 } local_tokens[MAXLOCALTOKENS] = { {
251 /* new interface routines to the ticket cache. Only handle afs service right
255 NewSetToken(aserver, atoken, aclient, flags)
256 struct ktc_principal *aserver;
257 struct ktc_principal *aclient;
258 struct ktc_token *atoken;
261 TRY_KERNEL(KTC_SETTOKEN_OP, aserver, aclient, atoken,
262 sizeof(struct ktc_token));
263 /* no kernel ticket cache */
267 #define MAXPIOCTLTOKENLEN \
268 (3*sizeof(afs_int32)+MAXKTCTICKETLEN+sizeof(struct ClearToken)+MAXKTCREALMLEN)
271 OldSetToken(aserver, atoken, aclient, flags)
272 struct ktc_principal *aserver, *aclient;
273 struct ktc_token *atoken;
276 struct ViceIoctl iob;
277 char tbuffer[MAXPIOCTLTOKENLEN];
279 struct ClearToken ct;
280 register afs_int32 code;
283 if (strcmp(aserver->name, "afs") != 0) {
286 for (i = 0; i < MAXLOCALTOKENS; i++)
287 if (local_tokens[i].valid) {
288 if ((strcmp(local_tokens[i].server.name, aserver->name) == 0)
291 (local_tokens[i].server.instance,
292 aserver->instance) == 0)
293 && (strcmp(local_tokens[i].server.cell, aserver->cell) ==
295 found = i; /* replace existing entry */
297 } else /* valid, but no match */
300 found = i; /* remember this empty slot */
303 memcpy(&local_tokens[found].token, atoken, sizeof(struct ktc_token));
304 local_tokens[found].server = *aserver;
305 local_tokens[found].client = *aclient;
306 local_tokens[found].valid = 1;
309 tp = tbuffer; /* start copying here */
310 if ((atoken->ticketLen < MINKTCTICKETLEN)
311 || (atoken->ticketLen > MAXKTCTICKETLEN))
313 memcpy(tp, &atoken->ticketLen, sizeof(afs_int32)); /* copy in ticket length */
314 tp += sizeof(afs_int32);
315 memcpy(tp, atoken->ticket, atoken->ticketLen); /* copy in ticket */
316 tp += atoken->ticketLen;
317 /* next, copy in the "clear token", describing who we are */
318 ct.AuthHandle = atoken->kvno; /* hide auth handle here */
319 memcpy(ct.HandShakeKey, &atoken->sessionKey, 8);
321 ct.BeginTimestamp = atoken->startTime;
322 ct.EndTimestamp = atoken->endTime;
323 if (ct.BeginTimestamp == 0)
324 ct.BeginTimestamp = 1;
326 if ((strlen(aclient->name) > strlen("AFS ID "))
327 && (aclient->instance[0] == 0)) {
329 afs_int32 viceId = 0;
330 char *cp = aclient->name + strlen("AFS ID ");
337 viceId = viceId * 10 + (int)(*cp - '0');
342 ct.ViceId = viceId * sign; /* OK to let any value here? */
343 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 0)
344 ct.BeginTimestamp++; /* force lifetime to be odd */
347 ct.ViceId = getuid(); /* wrong, but works in primary cell */
348 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1)
349 ct.BeginTimestamp++; /* force lifetime to be even */
354 * Information needed by the user space cache manager
356 u.u_expiration = ct.EndTimestamp;
357 u.u_viceid = ct.ViceId;
360 temp = sizeof(struct ClearToken);
361 memcpy(tp, &temp, sizeof(afs_int32));
362 tp += sizeof(afs_int32);
363 memcpy(tp, &ct, sizeof(struct ClearToken));
364 tp += sizeof(struct ClearToken);
366 /* next copy in primary flag */
370 * The following means that setpag will happen inside afs just before
371 * the authentication to prevent the setpag/klog race condition.
373 * The following means that setpag will affect the parent process as
374 * well as the current process.
376 if (flags & AFS_SETTOK_SETPAG)
379 memcpy(tp, &temp, sizeof(afs_int32));
380 tp += sizeof(afs_int32);
382 /* finally copy in the cell name */
383 temp = strlen(aserver->cell);
384 if (temp >= MAXKTCREALMLEN)
386 strcpy(tp, aserver->cell);
389 /* now setup for the pioctl */
391 iob.in_size = tp - tbuffer;
393 iob.out_size = sizeof(tbuffer);
395 #if defined(NO_AFS_CLIENT)
399 if ((tkfile = getenv("TKTFILE"))
402 open(tkfile, O_WRONLY | O_APPEND | O_TRUNC | O_CREAT,
404 printf("Writing ticket to: %s\n", tkfile);
405 code = (write(fd, iob.in, iob.in_size) != iob.in_size);
408 code = KTC_PIOCTLFAIL;
410 #else /* NO_AFS_CLIENT */
411 code = PIOCTL(0, VIOCSETTOK, &iob, 0);
412 #endif /* NO_AFS_CLIENT */
414 return KTC_PIOCTLFAIL;
419 ktc_SetToken(aserver, atoken, aclient, flags)
420 struct ktc_principal *aserver;
421 struct ktc_principal *aclient;
422 struct ktc_token *atoken;
428 #ifdef AFS_KERBEROS_ENV
432 if ( /*!strcmp(aclient->cell, lcell) && this would only store local creds */
433 (strcmp(aserver->name, "AuthServer")
434 || strcmp(aserver->instance, "Admin"))) {
435 if (strcmp(aserver->name, "krbtgt") == 0) {
436 static char lrealm[MAXKTCREALMLEN];
439 ucstring(lrealm, lcell, MAXKTCREALMLEN);
440 if (strcmp(aserver->instance, lrealm) == 0) {
441 afs_tf_create(aclient->name, aclient->instance);
445 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
446 if (ncode == NO_TKT_FIL) {
447 (void)afs_tf_create(aclient->name, aclient->instance);
448 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
452 afs_tf_save_cred(aserver, atoken, aclient);
458 #endif /* NO_AFS_CLIENT */
462 #ifndef NO_AFS_CLIENT
463 ncode = NewSetToken(aserver, atoken, aclient, flags);
464 if (ncode || /* new style failed */
465 (strcmp(aserver->name, "afs") == 0)) { /* for afs tokens do both */
466 ocode = OldSetToken(aserver, atoken, aclient, flags);
469 if (ncode && ocode) {
473 else if (ocode == KTC_PIOCTLFAIL)
481 return KTC_PIOCTLFAIL;
483 #endif /* NO_AFS_CLIENT */
488 /* get token, given server we need and token buffer. aclient will eventually
489 * be set to our identity to the server.
491 ktc_GetToken(aserver, atoken, atokenLen, aclient)
492 struct ktc_principal *aserver, *aclient;
494 struct ktc_token *atoken;
496 struct ViceIoctl iob;
497 char tbuffer[MAXPIOCTLTOKENLEN];
498 register afs_int32 code;
500 char *stp, *cellp; /* secret token ptr */
501 struct ClearToken ct;
504 int maxLen; /* biggest ticket we can copy */
505 int tktLen; /* server ticket length */
509 #ifndef NO_AFS_CLIENT
510 TRY_KERNEL(KTC_GETTOKEN_OP, aserver, aclient, atoken, atokenLen);
511 #endif /* NO_AFS_CLIENT */
513 #ifdef AFS_KERBEROS_ENV
517 #ifndef NO_AFS_CLIENT
518 if (strcmp(aserver->name, "afs") != 0)
519 #endif /* NO_AFS_CLIENT */
522 /* try the local tokens */
523 for (i = 0; i < MAXLOCALTOKENS; i++)
524 if (local_tokens[i].valid
525 && (strcmp(local_tokens[i].server.name, aserver->name) == 0)
526 && (strcmp(local_tokens[i].server.instance, aserver->instance)
528 && (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
529 memcpy(atoken, &local_tokens[i].token,
530 min(atokenLen, sizeof(struct ktc_token)));
532 *aclient = local_tokens[i].client;
536 #ifdef AFS_KERBEROS_ENV
537 if (!afs_tf_init(ktc_tkt_string(), R_TKT_FIL)) {
539 if (!afs_tf_get_pname(aclient->name)
540 && !afs_tf_get_pinst(aclient->instance))
543 char tmpstring[MAXHOSTCHARS];
544 afs_tf_get_pname(&tmpstring);
545 afs_tf_get_pinst(&tmpstring);
550 struct ktc_principal cprincipal;
551 struct ktc_token ctoken;
553 while (!afs_tf_get_cred(&cprincipal, &ctoken)) {
554 if (strcmp(cprincipal.name, aserver->name) == 0
555 && strcmp(cprincipal.instance, aserver->instance) == 0
556 && strcmp(cprincipal.cell, aserver->cell) == 0) {
559 strcpy(aclient->cell, lcell);
560 memcpy(atoken, &ctoken,
561 min(atokenLen, sizeof(struct ktc_token)));
574 #ifndef NO_AFS_CLIENT
575 for (index = 0; index < 200; index++) { /* sanity check in case pioctl fails */
576 iob.in = (char *)&index;
577 iob.in_size = sizeof(afs_int32);
579 iob.out_size = sizeof(tbuffer);
581 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
584 /* failed to retrieve specified token */
585 if (code < 0 && errno == EDOM) {
590 /* token retrieved; parse buffer */
593 /* get ticket length */
594 memcpy(&temp, tp, sizeof(afs_int32));
596 tp += sizeof(afs_int32);
598 /* remember where ticket is and skip over it */
602 /* get size of clear token and verify */
603 memcpy(&temp, tp, sizeof(afs_int32));
604 if (temp != sizeof(struct ClearToken)) {
608 tp += sizeof(afs_int32);
610 /* copy clear token */
611 memcpy(&ct, tp, temp);
614 /* skip over primary flag */
615 tp += sizeof(afs_int32);
617 /* remember where cell name is */
620 if ((strcmp(cellp, aserver->cell) == 0)
621 #ifdef AFS_KERBEROS_ENV
622 || (*aserver->cell == '\0' && strcmp(cellp, lcell) == 0)
625 /* got token for cell; check that it will fit */
627 atokenLen - sizeof(struct ktc_token) + MAXKTCTICKETLEN;
628 if (maxLen < tktLen) {
633 /* set return values */
634 memcpy(atoken->ticket, stp, tktLen);
635 atoken->startTime = ct.BeginTimestamp;
636 atoken->endTime = ct.EndTimestamp;
637 if (ct.AuthHandle == -1) {
640 atoken->kvno = ct.AuthHandle;
641 memcpy(&atoken->sessionKey, ct.HandShakeKey,
642 sizeof(struct ktc_encryptionKey));
643 atoken->ticketLen = tktLen;
646 strcpy(aclient->cell, cellp);
647 aclient->instance[0] = 0;
649 if ((atoken->kvno == 999) || /* old style bcrypt ticket */
650 (ct.BeginTimestamp && /* new w/ prserver lookup */
651 (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1))) {
652 sprintf(aclient->name, "AFS ID %d", ct.ViceId);
654 sprintf(aclient->name, "Unix UID %d", ct.ViceId);
662 #endif /* NO_AFS_CLIENT */
665 if ((code < 0) && (errno == EINVAL))
667 return KTC_PIOCTLFAIL; /* probable cause */
671 * Forget tokens for this server and the calling user.
672 * NOT IMPLEMENTED YET!
674 #ifndef NO_AFS_CLIENT
675 ktc_ForgetToken(aserver)
676 struct ktc_principal *aserver;
681 TRY_KERNEL(KTC_FORGETTOKEN_OP, aserver, 0, 0, 0);
683 rc = ktc_ForgetAllTokens(); /* bogus, but better */
687 #endif /* NO_AFS_CLIENT */
689 /* ktc_ListTokens - list all tokens. start aprevIndex at 0, it returns the
690 * next rock in (*aindex). (*aserver) is set to the relevant ticket on
693 ktc_ListTokens(aprevIndex, aindex, aserver)
694 int aprevIndex, *aindex;
695 struct ktc_principal *aserver;
697 struct ViceIoctl iob;
698 char tbuffer[MAXPIOCTLTOKENLEN];
699 register afs_int32 code;
701 afs_int32 temp, index;
703 memset(tbuffer, 0, sizeof(tbuffer));
706 #ifndef NO_AFS_CLIENT
707 TRY_KERNEL(KTC_LISTTOKENS_OP, aserver, aprevIndex, aindex, 0);
708 #endif /* NO_AFS_CLIENT */
714 #endif /* NO_AFS_CLIENT */
715 #ifdef AFS_KERBEROS_ENV
718 struct ktc_principal cprincipal;
719 struct ktc_token ctoken;
721 if (afs_tf_init(ktc_tkt_string(), R_TKT_FIL)
722 || afs_tf_get_pname(tbuffer) || afs_tf_get_pinst(tbuffer)) {
728 for (i = 214; i < index; i++) {
729 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
737 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
744 #ifndef NO_AFS_CLIENT
745 if (!strcmp(cprincipal.name, "afs") && cprincipal.instance[0] == 0) {
748 #endif /* NO_AFS_CLIENT */
750 for (i = 0; i < MAXLOCALTOKENS; i++) {
751 if (!strcmp(cprincipal.name, local_tokens[i].server.name)
752 && !strcmp(cprincipal.instance,
753 local_tokens[i].server.instance)
754 && !strcmp(cprincipal.cell, local_tokens[i].server.cell)) {
759 *aserver = cprincipal;
767 #ifndef NO_AFS_CLIENT
768 if (index >= 123) { /* special hack for returning TCS */
769 while (index - 123 < MAXLOCALTOKENS) {
770 if (local_tokens[index - 123].valid) {
771 *aserver = local_tokens[index - 123].server;
779 #ifdef AFS_KERBEROS_ENV
780 return ktc_ListTokens(214, aindex, aserver);
786 /* get tokens from the kernel */
787 while (index < 200) { /* sanity check in case pioctl fails */
788 iob.in = (char *)&index;
789 iob.in_size = sizeof(afs_int32);
791 iob.out_size = sizeof(tbuffer);
792 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
793 if (code < 0 && errno == EDOM) {
796 rc = ktc_ListTokens(123, aindex, aserver);
805 break; /* got a ticket */
806 /* otherwise we should skip this ticket slot */
813 return KTC_PIOCTLFAIL;
819 /* next iterator determined by earlier loop */
822 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of secret token */
823 tp += sizeof(afs_int32);
824 tp += temp; /* skip ticket for now */
825 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of clear token */
826 if (temp != sizeof(struct ClearToken)) {
830 tp += sizeof(afs_int32); /* skip length */
831 tp += temp; /* skip clear token itself */
832 tp += sizeof(afs_int32); /* skip primary flag */
833 /* tp now points to the cell name */
834 strcpy(aserver->cell, tp);
835 aserver->instance[0] = 0;
836 strcpy(aserver->name, "afs");
837 #endif /* NO_AFS_CLIENT */
842 /* discard all tokens from this user's cache */
847 #ifndef NO_AFS_CLIENT
848 TRY_KERNEL(KTC_FORGETALLTOKENS_OP, 0, 0, 0, 0);
849 #endif /* NO_AFS_CLIENT */
856 struct ViceIoctl iob;
857 register afs_int32 code;
860 for (i = 0; i < MAXLOCALTOKENS; i++)
861 local_tokens[i].valid = 0;
867 #ifndef NO_AFS_CLIENT
868 code = PIOCTL(0, VIOCUNPAG, &iob, 0);
870 return KTC_PIOCTLFAIL;
871 #endif /* NO_AFS_CLIENT */
876 ktc_ForgetAllTokens()
881 #ifdef AFS_KERBEROS_ENV
882 (void)afs_tf_dest_tkt();
885 ncode = NewForgetAll();
886 ocode = OldForgetAll();
887 if (ncode && ocode) {
890 else if (ocode == KTC_PIOCTLFAIL)
895 return KTC_PIOCTLFAIL;
901 /* ktc_OldPioctl - returns a boolean true if the kernel supports only the old
902 * pioctl interface for delivering AFS tickets to the cache manager. */
908 #ifdef KERNEL_KTC_COMPAT
910 rc = (kernelKTC != 1); /* old style interface */
919 #ifdef AFS_KERBEROS_ENV
921 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
923 * For copying and distribution information, please see the file
930 #include <sys/types.h>
931 #include <sys/stat.h>
932 #include <sys/file.h>
937 #define TF_LCK_RETRY ((unsigned)2) /* seconds to sleep before
938 * retry if ticket file is
942 * fd must be initialized to something that won't ever occur as a real
943 * file descriptor. Since open(2) returns only non-negative numbers as
944 * valid file descriptors, and afs_tf_init always stuffs the return value
945 * from open in here even if it is an error flag, we must
946 * a. Initialize fd to a negative number, to indicate that it is
947 * not initially valid.
948 * b. When checking for a valid fd, assume that negative values
949 * are invalid (ie. when deciding whether afs_tf_init has been
951 * c. In tf_close, be sure it gets reinitialized to a negative
955 static curpos; /* Position in tfbfr */
956 static lastpos; /* End of tfbfr */
957 static char tfbfr[BUFSIZ]; /* Buffer for ticket data */
959 static tf_gets(), tf_read();
962 * This file contains routines for manipulating the ticket cache file.
964 * The ticket file is in the following format:
966 * principal's name (null-terminated string)
967 * principal's instance (null-terminated string)
974 * Where "CREDENTIAL_x" consists of the following fixed-length
975 * fields from the CREDENTIALS structure (see "krb.h"):
977 * char service[MAXKTCNAMELEN]
978 * char instance[MAXKTCNAMELEN]
979 * char realm[REALM_SZ]
984 * afs_int32 issue_date
986 * Short description of routines:
988 * afs_tf_init() opens the ticket file and locks it.
990 * afs_tf_get_pname() returns the principal's name.
992 * afs_tf_get_pinst() returns the principal's instance (may be null).
994 * afs_tf_get_cred() returns the next CREDENTIALS record.
996 * afs_tf_save_cred() appends a new CREDENTIAL record to the ticket file.
998 * afs_tf_close() closes the ticket file and releases the lock.
1000 * tf_gets() returns the next null-terminated string. It's an internal
1001 * routine used by afs_tf_get_pname(), afs_tf_get_pinst(), and
1002 * afs_tf_get_cred().
1004 * tf_read() reads a given number of bytes. It's an internal routine
1005 * used by afs_tf_get_cred().
1009 * afs_tf_init() should be called before the other ticket file routines.
1010 * It takes the name of the ticket file to use, "tf_name", and a
1011 * read/write flag "rw" as arguments.
1013 * It tries to open the ticket file, checks the mode, and if everything
1014 * is okay, locks the file. If it's opened for reading, the lock is
1015 * shared. If it's opened for writing, the lock is exclusive.
1017 * Returns 0 if all went well, otherwise one of the following:
1019 * NO_TKT_FIL - file wasn't there
1020 * TKT_FIL_ACC - file was in wrong mode, etc.
1021 * TKT_FIL_LCK - couldn't lock the file, even after a retry
1024 afs_tf_init(tf_name, rw)
1029 struct stat stat_buf;
1041 if (lstat(tf_name, &stat_buf) < 0)
1049 if ((stat_buf.st_uid != me && me != 0)
1050 || ((stat_buf.st_mode & S_IFMT) != S_IFREG))
1054 * If "wflag" is set, open the ticket file in append-writeonly mode
1055 * and lock the ticket file in exclusive mode. If unable to lock
1056 * the file, sleep and try again. If we fail again, return with the
1057 * proper error message.
1060 curpos = sizeof(tfbfr);
1063 fd = open(tf_name, O_RDWR, 0600);
1067 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1068 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1069 sleep(TF_LCK_RETRY);
1070 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1072 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1073 sleep(TF_LCK_RETRY);
1074 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1084 * Otherwise "wflag" is not set and the ticket file should be opened
1085 * for read-only operations and locked for shared access.
1088 fd = open(tf_name, O_RDONLY, 0600);
1092 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1093 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1094 sleep(TF_LCK_RETRY);
1095 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1097 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1098 sleep(TF_LCK_RETRY);
1099 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1110 * afs_tf_get_pname() reads the principal's name from the ticket file. It
1111 * should only be called after afs_tf_init() has been called. The
1112 * principal's name is filled into the "p" parameter. If all goes well,
1113 * 0 is returned. If afs_tf_init() wasn't called, TKT_FIL_INI is
1114 * returned. If the name was null, or EOF was encountered, or the name
1115 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned.
1124 if (tf_gets(p, MAXKTCNAMELEN) < 2) /* can't be just a null */
1130 * afs_tf_get_pinst() reads the principal's instance from a ticket file.
1131 * It should only be called after afs_tf_init() and afs_tf_get_pname() have
1132 * been called. The instance is filled into the "inst" parameter. If all
1133 * goes well, 0 is returned. If afs_tf_init() wasn't called,
1134 * TKT_FIL_INI is returned. If EOF was encountered, or the instance
1135 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned. Note that the
1136 * instance may be null.
1139 afs_tf_get_pinst(inst)
1145 if (tf_gets(inst, MAXKTCNAMELEN) < 1)
1151 * afs_tf_get_cred() reads a CREDENTIALS record from a ticket file and fills
1152 * in the given structure "c". It should only be called after afs_tf_init(),
1153 * afs_tf_get_pname(), and afs_tf_get_pinst() have been called. If all goes
1154 * well, 0 is returned. Possible error codes are:
1156 * TKT_FIL_INI - afs_tf_init wasn't called first
1157 * TKT_FIL_FMT - bad format
1158 * EOF - end of file encountered
1161 afs_tf_get_cred(principal, token)
1162 struct ktc_principal *principal;
1163 struct ktc_token *token;
1171 if ((k_errno = tf_gets(principal->name, MAXKTCNAMELEN)) < 2)
1174 case 1: /* can't be just a null */
1179 if ((k_errno = tf_gets(principal->instance, MAXKTCNAMELEN)) < 1)
1186 if ((k_errno = tf_gets(principal->cell, MAXKTCREALMLEN)) < 2)
1189 case 1: /* can't be just a null */
1194 lcstring(principal->cell, principal->cell, MAXKTCREALMLEN);
1195 if (tf_read((char *)&(token->sessionKey), 8) < 1
1196 || tf_read((char *)&(lifetime), sizeof(lifetime)) < 1
1197 || tf_read((char *)&(kvno), sizeof(kvno)) < 1
1198 || tf_read((char *)&(token->ticketLen), sizeof(token->ticketLen))
1200 /* don't try to read a silly amount into ticket->dat */
1201 token->ticketLen > MAXKTCTICKETLEN
1202 || tf_read((char *)(token->ticket), token->ticketLen) < 1
1203 || tf_read((char *)&(token->startTime),
1204 sizeof(token->startTime)) < 1) {
1207 token->endTime = life_to_time(token->startTime, lifetime);
1213 * tf_close() closes the ticket file and sets "fd" to -1. If "fd" is
1214 * not a valid file descriptor, it just returns. It also clears the
1215 * buffer used to read tickets.
1217 * The return value is not defined.
1223 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1224 (void)fcntl(fd, F_SETLK, &fileUlock);
1226 (void)flock(fd, LOCK_UN);
1229 fd = -1; /* see declaration of fd above */
1231 memset(tfbfr, 0, sizeof(tfbfr));
1235 * tf_gets() is an internal routine. It takes a string "s" and a count
1236 * "n", and reads from the file until either it has read "n" characters,
1237 * or until it reads a null byte. When finished, what has been read exists
1240 * Possible return values are:
1242 * n the number of bytes read (including null terminator)
1243 * when all goes well
1245 * 0 end of file or read error
1247 * TOO_BIG if "count" characters are read and no null is
1248 * encountered. This is an indication that the ticket
1249 * file is seriously ill.
1261 for (count = n - 1; count > 0; --count) {
1262 if (curpos >= sizeof(tfbfr)) {
1263 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1266 if (curpos == lastpos) {
1269 *s = tfbfr[curpos++];
1277 * tf_read() is an internal routine. It takes a string "s" and a count
1278 * "n", and reads from the file until "n" bytes have been read. When
1279 * finished, what has been read exists in "s".
1281 * Possible return values are:
1283 * n the number of bytes read when all goes well
1285 * 0 on end of file or read error
1295 for (count = n; count > 0; --count) {
1296 if (curpos >= sizeof(tfbfr)) {
1297 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1300 if (curpos == lastpos) {
1303 *s++ = tfbfr[curpos++];
1311 * afs_tf_save_cred() appends an incoming ticket to the end of the ticket
1312 * file. You must call afs_tf_init() before calling afs_tf_save_cred().
1314 * The "service", "instance", and "realm" arguments specify the
1315 * server's name; "aticket" contains the credential.
1317 * Returns 0 if all goes well, TKT_FIL_INI if afs_tf_init() wasn't
1318 * called previously, and KFAILURE for anything else that went wrong.
1321 afs_tf_save_cred(aserver, atoken, aclient)
1322 struct ktc_principal *aserver;
1323 struct ktc_principal *aclient;
1324 struct ktc_token *atoken; /* Token */
1326 char realm[MAXKTCREALMLEN + 1];
1327 char junk[MAXKTCNAMELEN];
1328 struct ktc_principal principal;
1329 struct ktc_token token;
1333 int count; /* count for write */
1335 if (fd < 0) { /* fd is ticket file as set by afs_tf_init */
1339 ucstring(realm, aserver->cell, MAXKTCREALMLEN);
1340 realm[MAXKTCREALMLEN] = '\0';
1342 /* Look for a duplicate ticket */
1343 (void)lseek(fd, (off_t) 0L, 0);
1344 curpos = sizeof(tfbfr);
1346 if (afs_tf_get_pname(junk) || strcmp(junk, aclient->name)
1347 || afs_tf_get_pinst(junk) || strcmp(junk, aclient->instance))
1351 start = lseek(fd, (off_t) 0L, 1) - lastpos + curpos;
1352 status = afs_tf_get_cred(&principal, &token);
1353 } while (status == 0
1354 && (strcmp(aserver->name, principal.name) != 0
1355 || strcmp(aserver->instance, principal.instance) != 0
1356 || strcmp(aserver->cell, principal.cell) != 0));
1359 * Two tickets for the same user authenticating to the same service
1360 * should be the same length, but we check here just to make sure.
1362 if (status == 0 && token.ticketLen != atoken->ticketLen)
1364 if (status && status != EOF)
1367 /* Position over the credential we just matched (or the EOF) */
1368 lseek(fd, start, 0);
1369 curpos = lastpos = sizeof(tfbfr);
1371 /* Write the ticket and associated data */
1373 count = strlen(aserver->name) + 1;
1374 if (write(fd, aserver->name, count) != count)
1377 count = strlen(aserver->instance) + 1;
1378 if (write(fd, aserver->instance, count) != count)
1381 count = strlen(realm) + 1;
1382 if (write(fd, realm, count) != count)
1385 if (write(fd, (char *)&atoken->sessionKey, 8) != 8)
1388 lifetime = time_to_life(atoken->startTime, atoken->endTime);
1389 if (write(fd, (char *)&lifetime, sizeof(int)) != sizeof(int))
1392 kvno = atoken->kvno;
1393 if (write(fd, (char *)&kvno, sizeof(int)) != sizeof(int))
1396 if (write(fd, (char *)&(atoken->ticketLen), sizeof(int)) != sizeof(int))
1399 count = atoken->ticketLen;
1400 if (write(fd, atoken->ticket, count) != count)
1403 if (write(fd, (char *)&atoken->startTime, sizeof(afs_int32))
1404 != sizeof(afs_int32))
1407 /* Actually, we should check each write for success */
1414 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
1417 * For copying and distribution information, please see the file
1418 * <mit-copyright.h>.
1424 * This routine is used to generate the name of the file that holds
1425 * the user's cache of server tickets and associated session keys.
1427 * If it is set, krb_ticket_string contains the ticket file name.
1428 * Otherwise, the filename is constructed as follows:
1430 * If it is set, the environment variable "KRBTKFILE" will be used as
1431 * the ticket file name. Otherwise TKT_ROOT (defined in "krb.h") and
1432 * the user's uid are concatenated to produce the ticket file name
1433 * (e.g., "/tmp/tkt123"). A pointer to the string containing the ticket
1434 * file name is returned.
1437 static char krb_ticket_string[4096] = "";
1445 if (!*krb_ticket_string) {
1446 if (env = getenv("KRBTKFILE")) {
1447 (void)strncpy(krb_ticket_string, env,
1448 sizeof(krb_ticket_string) - 1);
1449 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1451 /* 32 bits of signed integer will always fit in 11 characters
1452 * (including the sign), so no need to worry about overflow */
1453 (void)sprintf(krb_ticket_string, "%s%d", TKT_ROOT, getuid());
1456 UNLOCK_GLOBAL_MUTEX;
1457 return krb_ticket_string;
1461 * This routine is used to set the name of the file that holds the user's
1462 * cache of server tickets and associated session keys.
1464 * The value passed in is copied into local storage.
1466 * NOTE: This routine should be called during initialization, before other
1467 * Kerberos routines are called; otherwise tkt_string() above may be called
1468 * and return an undesired ticket file name until this routine is called.
1472 ktc_set_tkt_string(val)
1477 (void)strncpy(krb_ticket_string, val, sizeof(krb_ticket_string) - 1);
1478 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1479 UNLOCK_GLOBAL_MUTEX;
1484 * tf_create() is used to initialize the ticket store. It creates the
1485 * file to contain the tickets and writes the given user's name "pname"
1486 * and instance "pinst" in the file. in_tkt() returns KSUCCESS on
1487 * success, or KFAILURE if something goes wrong.
1490 afs_tf_create(pname, pinst)
1497 char *file = ktc_tkt_string();
1506 if (lstat(file, &sbuf) == 0) {
1507 if ((sbuf.st_uid != me && me != 0)
1508 || ((sbuf.st_mode & S_IFMT) != S_IFREG) || sbuf.st_mode & 077) {
1511 /* file already exists, and permissions appear ok, so nuke it */
1512 if ((fd = open(file, O_RDWR, 0)) < 0)
1513 goto out; /* can't zero it, but we can still try truncating it */
1515 memset(zerobuf, 0, sizeof(zerobuf));
1517 for (i = 0; i < sbuf.st_size; i += sizeof(zerobuf))
1518 if (write(fd, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf)) {
1529 /* arrange so the file is owned by the ruid
1530 * (swap real & effective uid if necessary).
1531 * This isn't a security problem, since the ticket file, if it already
1532 * exists, has the right uid (== ruid) and mode. */
1534 if (setreuid(metoo, me) < 0) {
1538 tktfile = creat(file, 0600);
1540 if (setreuid(me, metoo) < 0) {
1541 /* can't switch??? fail! */
1548 count = strlen(pname) + 1;
1549 if (write(tktfile, pname, count) != count) {
1550 (void)close(tktfile);
1553 count = strlen(pinst) + 1;
1554 if (write(tktfile, pinst, count) != count) {
1555 (void)close(tktfile);
1558 (void)close(tktfile);
1563 * dest_tkt() is used to destroy the ticket store upon logout.
1564 * If the ticket file does not exist, dest_tkt() returns RET_TKFIL.
1565 * Otherwise the function returns 0 on success, KFAILURE on
1571 char *file = ktc_tkt_string();
1577 if (lstat(file, &statb) < 0)
1580 if (!(statb.st_mode & S_IFREG))
1583 if ((fd = open(file, O_RDWR, 0)) < 0)
1586 memset(buf, 0, BUFSIZ);
1588 for (i = 0; i < statb.st_size; i += BUFSIZ)
1589 if (write(fd, buf, BUFSIZ) != BUFSIZ) {
1601 if (errno == ENOENT)
1603 else if (errno != 0)
1611 gid_t groups[NGROUPS_MAX];
1613 afs_uint32 h, l, ret;
1615 if (getgroups(sizeof groups / sizeof groups[0], groups) < 2)
1618 g0 = groups[0] & 0xffff;
1619 g1 = groups[1] & 0xffff;
1622 if (g0 < 0xc000 && g1 < 0xc000) {
1623 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
1625 h = (g1 >> 14) + h + h + h;
1626 ret = ((h << 28) | l);
1627 /* Additional testing */
1628 if (((ret >> 24) & 0xff) == 'A')
1639 extern char **environ;
1643 char fname[256], *prefix = "/ticket/";
1645 char **newenv, **senv, **denv;
1648 if (stat("/ticket", &sbuf) == -1) {
1649 prefix = "/tmp/tkt";
1652 pag = curpag() & 0xffffffff;
1654 sprintf(fname, "%s%d", prefix, getuid());
1656 sprintf(fname, "%sp%ld", prefix, pag);
1658 ktc_set_tkt_string(fname);
1660 for (senv = environ, numenv = 0; *senv; senv++)
1662 newenv = (char **)malloc((numenv + 2) * sizeof(char *));
1664 for (senv = environ, denv = newenv; *senv; *senv++) {
1665 if (strncmp(*senv, "KRBTKFILE=", 10) != 0)
1669 *denv = (char *)malloc(10 + strlen(fname) + 1);
1670 strcpy(*denv, "KRBTKFILE=");
1671 strcat(*denv, fname);
1674 UNLOCK_GLOBAL_MUTEX;
1678 * BLETCH! We have to invoke the entire afsconf package just to
1679 * find out what the local cell is.
1685 struct afsconf_dir *conf;
1687 if ((conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))
1688 || (conf = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH))) {
1689 code = afsconf_GetLocalCell(conf, lcell, sizeof(lcell));
1690 afsconf_Close(conf);
1692 if (!conf || code) {
1693 printf("** Can't determine local cell name!\n");