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) */
45 #include <afs/pthread_glock.h>
46 #include <sys/types.h>
51 #include <sys/ioctl.h>
52 #include <netinet/in.h>
56 #include <sys/lockf.h>
65 #include <afs/venus.h>
66 #include <afs/afsutil.h>
68 #endif /* defined(UKERNEL) */
75 /* AFS_KERBEROS_ENV is now conditionally defined in the Makefile */
76 #define AFS_KERBEROS_ENV
79 #ifdef AFS_KERBEROS_ENV
82 #include "cellconfig.h"
83 static char lcell[MAXCELLCHARS];
85 #define TKT_ROOT "/tmp/tkt"
90 /* Definitions for ticket file utilities */
94 /* Error codes returned by ticket file utilities */
95 #define NO_TKT_FIL 76 /* No ticket file found */
96 #define TKT_FIL_ACC 77 /* Couldn't access tkt file */
97 #define TKT_FIL_LCK 78 /* Couldn't lock ticket file */
98 #define TKT_FIL_FMT 79 /* Bad ticket file format */
99 #define TKT_FIL_INI 80 /* afs_tf_init not called first */
101 /* Values returned by get_credentials */
102 #define RET_TKFIL 21 /* Can't read ticket file */
111 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
112 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0, 0 };
113 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0, 0 };
114 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0, 0 };
117 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0 };
118 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0 };
119 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0 };
126 /* the following routines aren't static anymore on behalf of the kerberos IV
127 * compatibility library built in subtree krb.
129 int afs_tf_init(char *, int);
130 int afs_tf_get_pname(char *);
131 int afs_tf_get_pinst(char *);
132 int afs_tf_get_cred(struct ktc_principal *, struct ktc_token *);
133 int afs_tf_save_cred(struct ktc_principal *, struct ktc_token *,
134 struct ktc_principal *);
135 int afs_tf_close(void);
136 int afs_tf_create(char *, char *);
137 int afs_tf_dest_tkt(void);
138 static void ktc_LocalCell(void);
139 #endif /* AFS_KERBEROS_ENV */
142 #define PIOCTL afs_pioctl
143 #elif defined(UKERNEL)
144 #define PIOCTL(A,B,C,D) call_syscall(AFSCALL_PIOCTL,A,B,C,D)
146 #define PIOCTL pioctl
150 #ifdef KERNEL_KTC_COMPAT
153 #define KTC_SYSCALL 32
156 /* Kernel call opcode definitions */
157 #define KTC_OPCODE_BASE 4300
158 #define KTC_NO_OP (0+KTC_OPCODE_BASE)
159 #define KTC_SETTOKEN_OP (1+KTC_OPCODE_BASE)
160 #define KTC_GETTOKEN_OP (2+KTC_OPCODE_BASE)
161 #define KTC_LISTTOKENS_OP (3+KTC_OPCODE_BASE)
162 #define KTC_FORGETTOKEN_OP (4+KTC_OPCODE_BASE)
163 #define KTC_FORGETALLTOKENS_OP (5+KTC_OPCODE_BASE)
164 #define KTC_STATUS_OP (6+KTC_OPCODE_BASE)
165 #define KTC_GC_OP (7+KTC_OPCODE_BASE)
167 #define KTC_INTERFACE_VERSION 3
169 /* We have to determine if the kernel supports the ktc system call. To do so
170 * we attempt to execute its noop function. If this is successful we use the
171 * kernel calls in the future otherwise we let the old code run. */
173 /* To safely check to see whether a system call exists we have to intercept the
174 * SIGSYS signal which is caused by executing a non-existant system call. If
175 * it is ignored the syscall routine returns EINVAL. The SIGSYS is reset to
176 * its old value after the return from syscall. */
178 static int kernelKTC = 0;
180 #ifdef AFS_DECOSF_ENV
182 * SIGSYS semantics are broken on Dec AXP OSF/1 v1.2 systems. You need
183 * to ignore SIGTRAP too. It is claimed to be fixed under v1.3, but...
186 #define CHECK_KERNEL \
187 if (kernelKTC == 0) { \
191 old = (int (*)())signal(SIGSYS, SIG_IGN); \
192 old_t = (int (*)())signal(SIGTRAP, SIG_IGN); \
193 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
195 signal(SIGSYS, old); \
196 signal(SIGTRAP, old_t); \
197 if (code == 0) kernelKTC = 1; \
198 else kernelKTC = 2; \
199 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
202 #else /* AFS_DECOSF_ENV */
204 #define CHECK_KERNEL \
205 if (kernelKTC == 0) { \
208 old = (int (*)())signal(SIGSYS, SIG_IGN); \
209 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
211 signal(SIGSYS, old); \
212 if (code == 0) kernelKTC = 1; \
213 else kernelKTC = 2; \
214 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
216 #endif /* AFS_DECOSF_ENV */
218 #define TRY_KERNEL(cmd,a1,a2,a3,a4) \
220 if (kernelKTC == 1) \
221 return syscall (KTC_SYSCALL, cmd, \
222 KTC_INTERFACE_VERSION, a1,a2,a3,a4); \
226 #define TRY_KERNEL(cmd,a1,a2,a3,a4)
227 #endif /* KERNEL_KTC_COMPAT */
229 #if !defined(UKERNEL)
230 /* this is a structure used to communicate with the afs cache mgr, but is
231 * otherwise irrelevant */
233 afs_int32 AuthHandle;
234 char HandShakeKey[8];
236 afs_int32 BeginTimestamp;
237 afs_int32 EndTimestamp;
239 #endif /* !defined(UKERNEL) */
241 #define MAXLOCALTOKENS 4
245 struct ktc_principal server;
246 struct ktc_principal client;
247 struct ktc_token token;
248 } local_tokens[MAXLOCALTOKENS] = { {
254 /* new interface routines to the ticket cache. Only handle afs service right
258 NewSetToken(struct ktc_principal *aserver,
259 struct ktc_token *atoken,
260 struct ktc_principal *aclient,
263 TRY_KERNEL(KTC_SETTOKEN_OP, aserver, aclient, atoken,
264 sizeof(struct ktc_token));
265 /* no kernel ticket cache */
269 #define MAXPIOCTLTOKENLEN \
270 (3*sizeof(afs_int32)+MAXKTCTICKETLEN+sizeof(struct ClearToken)+MAXKTCREALMLEN)
273 OldSetToken(struct ktc_principal *aserver, struct ktc_token *atoken,
274 struct ktc_principal *aclient, afs_int32 flags)
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(struct ktc_principal *aserver,
420 struct ktc_token *atoken,
421 struct ktc_principal *aclient,
427 #ifdef AFS_KERBEROS_ENV
431 if ( /*!strcmp(aclient->cell, lcell) && this would only store local creds */
432 (strcmp(aserver->name, "AuthServer")
433 || strcmp(aserver->instance, "Admin"))) {
434 if (strcmp(aserver->name, "krbtgt") == 0) {
435 static char lrealm[MAXKTCREALMLEN];
438 ucstring(lrealm, lcell, MAXKTCREALMLEN);
439 if (strcmp(aserver->instance, lrealm) == 0) {
440 afs_tf_create(aclient->name, aclient->instance);
444 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
445 if (ncode == NO_TKT_FIL) {
446 (void)afs_tf_create(aclient->name, aclient->instance);
447 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
451 afs_tf_save_cred(aserver, atoken, aclient);
457 #endif /* NO_AFS_CLIENT */
461 #ifndef NO_AFS_CLIENT
462 ncode = NewSetToken(aserver, atoken, aclient, flags);
463 if (ncode || /* new style failed */
464 (strcmp(aserver->name, "afs") == 0)) { /* for afs tokens do both */
465 ocode = OldSetToken(aserver, atoken, aclient, flags);
468 if (ncode && ocode) {
472 else if (ocode == KTC_PIOCTLFAIL)
480 return KTC_PIOCTLFAIL;
482 #endif /* NO_AFS_CLIENT */
487 /* get token, given server we need and token buffer. aclient will eventually
488 * be set to our identity to the server.
491 ktc_GetToken(struct ktc_principal *aserver, struct ktc_token *atoken,
492 int atokenLen, struct ktc_principal *aclient)
494 struct ViceIoctl iob;
495 char tbuffer[MAXPIOCTLTOKENLEN];
496 register afs_int32 code;
498 char *stp, *cellp; /* secret token ptr */
499 struct ClearToken ct;
502 int maxLen; /* biggest ticket we can copy */
503 int tktLen; /* server ticket length */
504 #ifdef AFS_KERBEROS_ENV
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
676 ktc_ForgetToken(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
694 ktc_ListTokens(int aprevIndex,
696 struct ktc_principal *aserver)
698 struct ViceIoctl iob;
699 char tbuffer[MAXPIOCTLTOKENLEN];
700 register afs_int32 code;
702 afs_int32 temp, index;
704 memset(tbuffer, 0, sizeof(tbuffer));
707 #ifndef NO_AFS_CLIENT
708 TRY_KERNEL(KTC_LISTTOKENS_OP, aserver, aprevIndex, aindex, 0);
709 #endif /* NO_AFS_CLIENT */
715 #endif /* NO_AFS_CLIENT */
716 #ifdef AFS_KERBEROS_ENV
719 struct ktc_principal cprincipal;
720 struct ktc_token ctoken;
722 if (afs_tf_init(ktc_tkt_string(), R_TKT_FIL)
723 || afs_tf_get_pname(tbuffer) || afs_tf_get_pinst(tbuffer)) {
729 for (i = 214; i < index; i++) {
730 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
738 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
745 #ifndef NO_AFS_CLIENT
746 if (!strcmp(cprincipal.name, "afs") && cprincipal.instance[0] == 0) {
749 #endif /* NO_AFS_CLIENT */
751 for (i = 0; i < MAXLOCALTOKENS; i++) {
752 if (!strcmp(cprincipal.name, local_tokens[i].server.name)
753 && !strcmp(cprincipal.instance,
754 local_tokens[i].server.instance)
755 && !strcmp(cprincipal.cell, local_tokens[i].server.cell)) {
760 *aserver = cprincipal;
768 #ifndef NO_AFS_CLIENT
769 if (index >= 123) { /* special hack for returning TCS */
770 while (index - 123 < MAXLOCALTOKENS) {
771 if (local_tokens[index - 123].valid) {
772 *aserver = local_tokens[index - 123].server;
780 #ifdef AFS_KERBEROS_ENV
781 return ktc_ListTokens(214, aindex, aserver);
787 /* get tokens from the kernel */
788 while (index < 200) { /* sanity check in case pioctl fails */
789 iob.in = (char *)&index;
790 iob.in_size = sizeof(afs_int32);
792 iob.out_size = sizeof(tbuffer);
793 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
794 if (code < 0 && errno == EDOM) {
797 rc = ktc_ListTokens(123, aindex, aserver);
806 break; /* got a ticket */
807 /* otherwise we should skip this ticket slot */
814 return KTC_PIOCTLFAIL;
820 /* next iterator determined by earlier loop */
823 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of secret token */
824 tp += sizeof(afs_int32);
825 tp += temp; /* skip ticket for now */
826 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of clear token */
827 if (temp != sizeof(struct ClearToken)) {
831 tp += sizeof(afs_int32); /* skip length */
832 tp += temp; /* skip clear token itself */
833 tp += sizeof(afs_int32); /* skip primary flag */
834 /* tp now points to the cell name */
835 strcpy(aserver->cell, tp);
836 aserver->instance[0] = 0;
837 strcpy(aserver->name, "afs");
838 #endif /* NO_AFS_CLIENT */
843 /* discard all tokens from this user's cache */
848 #ifndef NO_AFS_CLIENT
849 TRY_KERNEL(KTC_FORGETALLTOKENS_OP, 0, 0, 0, 0);
850 #endif /* NO_AFS_CLIENT */
857 struct ViceIoctl iob;
858 register afs_int32 code;
861 for (i = 0; i < MAXLOCALTOKENS; i++)
862 local_tokens[i].valid = 0;
868 #ifndef NO_AFS_CLIENT
869 code = PIOCTL(0, VIOCUNPAG, &iob, 0);
871 return KTC_PIOCTLFAIL;
872 #endif /* NO_AFS_CLIENT */
877 ktc_ForgetAllTokens(void)
882 #ifdef AFS_KERBEROS_ENV
883 (void)afs_tf_dest_tkt();
886 ncode = NewForgetAll();
887 ocode = OldForgetAll();
888 if (ncode && ocode) {
891 else if (ocode == KTC_PIOCTLFAIL)
896 return KTC_PIOCTLFAIL;
902 /* ktc_OldPioctl - returns a boolean true if the kernel supports only the old
903 * pioctl interface for delivering AFS tickets to the cache manager. */
910 #ifdef KERNEL_KTC_COMPAT
912 rc = (kernelKTC != 1); /* old style interface */
921 #ifdef AFS_KERBEROS_ENV
923 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
925 * For copying and distribution information, please see the file
932 #include <sys/types.h>
933 #include <sys/stat.h>
934 #include <sys/file.h>
939 #define TF_LCK_RETRY ((unsigned)2) /* seconds to sleep before
940 * retry if ticket file is
944 * fd must be initialized to something that won't ever occur as a real
945 * file descriptor. Since open(2) returns only non-negative numbers as
946 * valid file descriptors, and afs_tf_init always stuffs the return value
947 * from open in here even if it is an error flag, we must
948 * a. Initialize fd to a negative number, to indicate that it is
949 * not initially valid.
950 * b. When checking for a valid fd, assume that negative values
951 * are invalid (ie. when deciding whether afs_tf_init has been
953 * c. In tf_close, be sure it gets reinitialized to a negative
957 static int curpos; /* Position in tfbfr */
958 static int lastpos; /* End of tfbfr */
959 static char tfbfr[BUFSIZ]; /* Buffer for ticket data */
961 static int tf_gets(char *, int);
962 static int tf_read(char *, int);
965 * This file contains routines for manipulating the ticket cache file.
967 * The ticket file is in the following format:
969 * principal's name (null-terminated string)
970 * principal's instance (null-terminated string)
977 * Where "CREDENTIAL_x" consists of the following fixed-length
978 * fields from the CREDENTIALS structure (see "krb.h"):
980 * char service[MAXKTCNAMELEN]
981 * char instance[MAXKTCNAMELEN]
982 * char realm[REALM_SZ]
987 * afs_int32 issue_date
989 * Short description of routines:
991 * afs_tf_init() opens the ticket file and locks it.
993 * afs_tf_get_pname() returns the principal's name.
995 * afs_tf_get_pinst() returns the principal's instance (may be null).
997 * afs_tf_get_cred() returns the next CREDENTIALS record.
999 * afs_tf_save_cred() appends a new CREDENTIAL record to the ticket file.
1001 * afs_tf_close() closes the ticket file and releases the lock.
1003 * tf_gets() returns the next null-terminated string. It's an internal
1004 * routine used by afs_tf_get_pname(), afs_tf_get_pinst(), and
1005 * afs_tf_get_cred().
1007 * tf_read() reads a given number of bytes. It's an internal routine
1008 * used by afs_tf_get_cred().
1012 * afs_tf_init() should be called before the other ticket file routines.
1013 * It takes the name of the ticket file to use, "tf_name", and a
1014 * read/write flag "rw" as arguments.
1016 * It tries to open the ticket file, checks the mode, and if everything
1017 * is okay, locks the file. If it's opened for reading, the lock is
1018 * shared. If it's opened for writing, the lock is exclusive.
1020 * Returns 0 if all went well, otherwise one of the following:
1022 * NO_TKT_FIL - file wasn't there
1023 * TKT_FIL_ACC - file was in wrong mode, etc.
1024 * TKT_FIL_LCK - couldn't lock the file, even after a retry
1028 afs_tf_init(char *tf_name, int rw)
1032 struct stat stat_buf;
1044 if (lstat(tf_name, &stat_buf) < 0)
1052 if ((stat_buf.st_uid != me && me != 0)
1053 || ((stat_buf.st_mode & S_IFMT) != S_IFREG))
1057 * If "wflag" is set, open the ticket file in append-writeonly mode
1058 * and lock the ticket file in exclusive mode. If unable to lock
1059 * the file, sleep and try again. If we fail again, return with the
1060 * proper error message.
1063 curpos = sizeof(tfbfr);
1066 fd = open(tf_name, O_RDWR, 0600);
1070 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1071 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1072 sleep(TF_LCK_RETRY);
1073 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1075 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1076 sleep(TF_LCK_RETRY);
1077 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1087 * Otherwise "wflag" is not set and the ticket file should be opened
1088 * for read-only operations and locked for shared access.
1091 fd = open(tf_name, O_RDONLY, 0600);
1095 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1096 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1097 sleep(TF_LCK_RETRY);
1098 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1100 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1101 sleep(TF_LCK_RETRY);
1102 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1113 * afs_tf_get_pname() reads the principal's name from the ticket file. It
1114 * should only be called after afs_tf_init() has been called. The
1115 * principal's name is filled into the "p" parameter. If all goes well,
1116 * 0 is returned. If afs_tf_init() wasn't called, TKT_FIL_INI is
1117 * returned. If the name was null, or EOF was encountered, or the name
1118 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned.
1122 afs_tf_get_pname(char *p)
1127 if (tf_gets(p, MAXKTCNAMELEN) < 2) /* can't be just a null */
1133 * afs_tf_get_pinst() reads the principal's instance from a ticket file.
1134 * It should only be called after afs_tf_init() and afs_tf_get_pname() have
1135 * been called. The instance is filled into the "inst" parameter. If all
1136 * goes well, 0 is returned. If afs_tf_init() wasn't called,
1137 * TKT_FIL_INI is returned. If EOF was encountered, or the instance
1138 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned. Note that the
1139 * instance may be null.
1143 afs_tf_get_pinst(char *inst)
1148 if (tf_gets(inst, MAXKTCNAMELEN) < 1)
1154 * afs_tf_get_cred() reads a CREDENTIALS record from a ticket file and fills
1155 * in the given structure "c". It should only be called after afs_tf_init(),
1156 * afs_tf_get_pname(), and afs_tf_get_pinst() have been called. If all goes
1157 * well, 0 is returned. Possible error codes are:
1159 * TKT_FIL_INI - afs_tf_init wasn't called first
1160 * TKT_FIL_FMT - bad format
1161 * EOF - end of file encountered
1165 afs_tf_get_cred(struct ktc_principal *principal, struct ktc_token *token)
1169 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1174 if ((k_errno = tf_gets(principal->name, MAXKTCNAMELEN)) < 2)
1177 case 1: /* can't be just a null */
1182 if ((k_errno = tf_gets(principal->instance, MAXKTCNAMELEN)) < 1)
1189 if ((k_errno = tf_gets(principal->cell, MAXKTCREALMLEN)) < 2)
1192 case 1: /* can't be just a null */
1197 lcstring(principal->cell, principal->cell, MAXKTCREALMLEN);
1198 if (tf_read((char *)&(token->sessionKey), 8) < 1
1199 || tf_read((char *)&(lifetime), sizeof(lifetime)) < 1
1200 || tf_read((char *)&(kvno), sizeof(kvno)) < 1
1201 || tf_read((char *)&(token->ticketLen), sizeof(token->ticketLen))
1203 /* don't try to read a silly amount into ticket->dat */
1204 token->ticketLen > MAXKTCTICKETLEN
1205 || tf_read((char *)(token->ticket), token->ticketLen) < 1
1206 || tf_read((char *)&mit_compat, sizeof(mit_compat)) < 1) {
1209 token->startTime = mit_compat;
1210 token->endTime = life_to_time(token->startTime, lifetime);
1216 * tf_close() closes the ticket file and sets "fd" to -1. If "fd" is
1217 * not a valid file descriptor, it just returns. It also clears the
1218 * buffer used to read tickets.
1220 * The return value is not defined.
1227 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1228 (void)fcntl(fd, F_SETLK, &fileUlock);
1230 (void)flock(fd, LOCK_UN);
1233 fd = -1; /* see declaration of fd above */
1235 memset(tfbfr, 0, sizeof(tfbfr));
1240 * tf_gets() is an internal routine. It takes a string "s" and a count
1241 * "n", and reads from the file until either it has read "n" characters,
1242 * or until it reads a null byte. When finished, what has been read exists
1245 * Possible return values are:
1247 * n the number of bytes read (including null terminator)
1248 * when all goes well
1250 * 0 end of file or read error
1252 * TOO_BIG if "count" characters are read and no null is
1253 * encountered. This is an indication that the ticket
1254 * file is seriously ill.
1258 tf_gets(register char *s, int n)
1265 for (count = n - 1; count > 0; --count) {
1266 if (curpos >= sizeof(tfbfr)) {
1267 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1270 if (curpos == lastpos) {
1273 *s = tfbfr[curpos++];
1281 * tf_read() is an internal routine. It takes a string "s" and a count
1282 * "n", and reads from the file until "n" bytes have been read. When
1283 * finished, what has been read exists in "s".
1285 * Possible return values are:
1287 * n the number of bytes read when all goes well
1289 * 0 on end of file or read error
1293 tf_read(register char *s, register int n)
1297 for (count = n; count > 0; --count) {
1298 if (curpos >= sizeof(tfbfr)) {
1299 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1302 if (curpos == lastpos) {
1305 *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.
1322 afs_tf_save_cred(struct ktc_principal *aserver,
1323 struct ktc_token *atoken,
1324 struct ktc_principal *aclient)
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 */
1334 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1336 if (fd < 0) { /* fd is ticket file as set by afs_tf_init */
1340 ucstring(realm, aserver->cell, MAXKTCREALMLEN);
1341 realm[MAXKTCREALMLEN] = '\0';
1343 /* Look for a duplicate ticket */
1344 (void)lseek(fd, (off_t) 0L, 0);
1345 curpos = sizeof(tfbfr);
1347 if (afs_tf_get_pname(junk) || strcmp(junk, aclient->name)
1348 || afs_tf_get_pinst(junk) || strcmp(junk, aclient->instance))
1352 start = lseek(fd, (off_t) 0L, 1) - lastpos + curpos;
1353 status = afs_tf_get_cred(&principal, &token);
1354 } while (status == 0
1355 && (strcmp(aserver->name, principal.name) != 0
1356 || strcmp(aserver->instance, principal.instance) != 0
1357 || strcmp(aserver->cell, principal.cell) != 0));
1360 * Two tickets for the same user authenticating to the same service
1361 * should be the same length, but we check here just to make sure.
1363 if (status == 0 && token.ticketLen != atoken->ticketLen)
1365 if (status && status != EOF)
1368 /* Position over the credential we just matched (or the EOF) */
1369 lseek(fd, start, 0);
1370 curpos = lastpos = sizeof(tfbfr);
1372 /* Write the ticket and associated data */
1374 count = strlen(aserver->name) + 1;
1375 if (write(fd, aserver->name, count) != count)
1378 count = strlen(aserver->instance) + 1;
1379 if (write(fd, aserver->instance, count) != count)
1382 count = strlen(realm) + 1;
1383 if (write(fd, realm, count) != count)
1386 if (write(fd, (char *)&atoken->sessionKey, 8) != 8)
1389 lifetime = time_to_life(atoken->startTime, atoken->endTime);
1390 if (write(fd, (char *)&lifetime, sizeof(int)) != sizeof(int))
1393 kvno = atoken->kvno;
1394 if (write(fd, (char *)&kvno, sizeof(int)) != sizeof(int))
1397 if (write(fd, (char *)&(atoken->ticketLen), sizeof(int)) != sizeof(int))
1400 count = atoken->ticketLen;
1401 if (write(fd, atoken->ticket, count) != count)
1404 mit_compat = atoken->startTime;
1405 if (write(fd, (char *)&mit_compat, sizeof(mit_compat))
1406 != sizeof(mit_compat))
1409 /* Actually, we should check each write for success */
1416 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
1419 * For copying and distribution information, please see the file
1420 * <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] = "";
1440 ktc_tkt_string(void)
1442 return ktc_tkt_string_uid(getuid());
1446 ktc_tkt_string_uid(afs_uint32 uid)
1451 if (!*krb_ticket_string) {
1452 if ((env = getenv("KRBTKFILE"))) {
1453 (void)strncpy(krb_ticket_string, env,
1454 sizeof(krb_ticket_string) - 1);
1455 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1457 /* 32 bits of signed integer will always fit in 11 characters
1458 * (including the sign), so no need to worry about overflow */
1459 (void)sprintf(krb_ticket_string, "%s%d", TKT_ROOT, uid);
1462 UNLOCK_GLOBAL_MUTEX;
1463 return krb_ticket_string;
1467 * This routine is used to set the name of the file that holds the user's
1468 * cache of server tickets and associated session keys.
1470 * The value passed in is copied into local storage.
1472 * NOTE: This routine should be called during initialization, before other
1473 * Kerberos routines are called; otherwise tkt_string() above may be called
1474 * and return an undesired ticket file name until this routine is called.
1478 ktc_set_tkt_string(char * val)
1482 (void)strncpy(krb_ticket_string, val, sizeof(krb_ticket_string) - 1);
1483 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1484 UNLOCK_GLOBAL_MUTEX;
1489 * tf_create() is used to initialize the ticket store. It creates the
1490 * file to contain the tickets and writes the given user's name "pname"
1491 * and instance "pinst" in the file. in_tkt() returns KSUCCESS on
1492 * success, or KFAILURE if something goes wrong.
1496 afs_tf_create(char *pname, char *pinst)
1501 char *file = ktc_tkt_string();
1510 if (lstat(file, &sbuf) == 0) {
1511 if ((sbuf.st_uid != me && me != 0)
1512 || ((sbuf.st_mode & S_IFMT) != S_IFREG) || sbuf.st_mode & 077) {
1515 /* file already exists, and permissions appear ok, so nuke it */
1516 if ((fd = open(file, O_RDWR, 0)) < 0)
1517 goto out; /* can't zero it, but we can still try truncating it */
1519 memset(zerobuf, 0, sizeof(zerobuf));
1521 for (i = 0; i < sbuf.st_size; i += sizeof(zerobuf))
1522 if (write(fd, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf)) {
1533 /* arrange so the file is owned by the ruid
1534 * (swap real & effective uid if necessary).
1535 * This isn't a security problem, since the ticket file, if it already
1536 * exists, has the right uid (== ruid) and mode. */
1538 if (setreuid(metoo, me) < 0) {
1542 tktfile = creat(file, 0600);
1544 if (setreuid(me, metoo) < 0) {
1545 /* can't switch??? fail! */
1552 count = strlen(pname) + 1;
1553 if (write(tktfile, pname, count) != count) {
1554 (void)close(tktfile);
1557 count = strlen(pinst) + 1;
1558 if (write(tktfile, pinst, count) != count) {
1559 (void)close(tktfile);
1562 (void)close(tktfile);
1567 * dest_tkt() is used to destroy the ticket store upon logout.
1568 * If the ticket file does not exist, dest_tkt() returns RET_TKFIL.
1569 * Otherwise the function returns 0 on success, KFAILURE on
1574 afs_tf_dest_tkt(void)
1576 char *file = ktc_tkt_string();
1582 if (lstat(file, &statb) < 0)
1585 if (!(statb.st_mode & S_IFREG))
1588 if ((fd = open(file, O_RDWR, 0)) < 0)
1591 memset(buf, 0, BUFSIZ);
1593 for (i = 0; i < statb.st_size; i += BUFSIZ)
1594 if (write(fd, buf, BUFSIZ) != BUFSIZ) {
1606 if (errno == ENOENT)
1608 else if (errno != 0)
1616 #if defined(AFS_AIX51_ENV)
1619 if (get_pag(PAG_AFS, &pag) < 0 || pag == 0)
1623 gid_t groups[NGROUPS_MAX];
1625 afs_uint32 h, l, ret;
1627 if (getgroups(sizeof groups / sizeof groups[0], groups) < 2)
1630 g0 = groups[0] & 0xffff;
1631 g1 = groups[1] & 0xffff;
1634 if (g0 < 0xc000 && g1 < 0xc000) {
1635 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
1637 h = (g1 >> 14) + h + h + h;
1638 ret = ((h << 28) | l);
1639 /* Additional testing */
1640 if (((ret >> 24) & 0xff) == 'A')
1652 extern char **environ;
1656 char fname[256], *prefix = "/ticket/";
1658 char **newenv, **senv, **denv;
1661 if (stat("/ticket", &sbuf) == -1) {
1662 prefix = "/tmp/tkt";
1665 pag = curpag() & 0xffffffff;
1667 sprintf(fname, "%s%d", prefix, getuid());
1669 sprintf(fname, "%sp%ld", prefix, (long int) pag);
1671 ktc_set_tkt_string(fname);
1673 for (senv = environ, numenv = 0; *senv; senv++)
1675 newenv = (char **)malloc((numenv + 2) * sizeof(char *));
1677 for (senv = environ, denv = newenv; *senv; senv++) {
1678 if (strncmp(*senv, "KRBTKFILE=", 10) != 0)
1682 *denv = (char *)malloc(10 + strlen(fname) + 1);
1683 strcpy(*denv, "KRBTKFILE=");
1684 strcat(*denv, fname);
1687 UNLOCK_GLOBAL_MUTEX;
1692 * BLETCH! We have to invoke the entire afsconf package just to
1693 * find out what the local cell is.
1699 struct afsconf_dir *conf;
1701 if ((conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))
1702 || (conf = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH))) {
1703 code = afsconf_GetLocalCell(conf, lcell, sizeof(lcell));
1704 afsconf_Close(conf);
1706 if (!conf || code) {
1707 printf("** Can't determine local cell name!\n");