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>
68 #include <afs/venus.h>
69 #include <afs/afsutil.h>
70 #include <afs/sys_prototypes.h>
72 #endif /* defined(UKERNEL) */
79 /* AFS_KERBEROS_ENV is now conditionally defined in the Makefile */
80 #define AFS_KERBEROS_ENV
83 #ifdef AFS_KERBEROS_ENV
86 #include "cellconfig.h"
87 static char lcell[MAXCELLCHARS];
89 #define TKT_ROOT "/tmp/tkt"
94 /* Definitions for ticket file utilities */
98 /* Error codes returned by ticket file utilities */
99 #define NO_TKT_FIL 76 /* No ticket file found */
100 #define TKT_FIL_ACC 77 /* Couldn't access tkt file */
101 #define TKT_FIL_LCK 78 /* Couldn't lock ticket file */
102 #define TKT_FIL_FMT 79 /* Bad ticket file format */
103 #define TKT_FIL_INI 80 /* afs_tf_init not called first */
105 /* Values returned by get_credentials */
106 #define RET_TKFIL 21 /* Can't read ticket file */
115 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
116 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0, 0 };
117 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0, 0 };
118 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0, 0 };
121 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0 };
122 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0 };
123 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0 };
130 /* the following routines aren't static anymore on behalf of the kerberos IV
131 * compatibility library built in subtree krb.
133 int afs_tf_init(char *, int);
134 int afs_tf_get_pname(char *);
135 int afs_tf_get_pinst(char *);
136 int afs_tf_get_cred(struct ktc_principal *, struct ktc_token *);
137 int afs_tf_save_cred(struct ktc_principal *, struct ktc_token *,
138 struct ktc_principal *);
139 int afs_tf_close(void);
140 int afs_tf_create(char *, char *);
141 int afs_tf_dest_tkt(void);
142 static void ktc_LocalCell(void);
143 #endif /* AFS_KERBEROS_ENV */
146 #define PIOCTL afs_pioctl
147 #elif defined(UKERNEL)
148 #define PIOCTL(A,B,C,D) call_syscall(AFSCALL_PIOCTL,A,B,C,D)
150 #define PIOCTL pioctl
154 #ifdef KERNEL_KTC_COMPAT
157 #define KTC_SYSCALL 32
160 /* Kernel call opcode definitions */
161 #define KTC_OPCODE_BASE 4300
162 #define KTC_NO_OP (0+KTC_OPCODE_BASE)
163 #define KTC_SETTOKEN_OP (1+KTC_OPCODE_BASE)
164 #define KTC_GETTOKEN_OP (2+KTC_OPCODE_BASE)
165 #define KTC_LISTTOKENS_OP (3+KTC_OPCODE_BASE)
166 #define KTC_FORGETTOKEN_OP (4+KTC_OPCODE_BASE)
167 #define KTC_FORGETALLTOKENS_OP (5+KTC_OPCODE_BASE)
168 #define KTC_STATUS_OP (6+KTC_OPCODE_BASE)
169 #define KTC_GC_OP (7+KTC_OPCODE_BASE)
171 #define KTC_INTERFACE_VERSION 3
173 /* We have to determine if the kernel supports the ktc system call. To do so
174 * we attempt to execute its noop function. If this is successful we use the
175 * kernel calls in the future otherwise we let the old code run. */
177 /* To safely check to see whether a system call exists we have to intercept the
178 * SIGSYS signal which is caused by executing a non-existant system call. If
179 * it is ignored the syscall routine returns EINVAL. The SIGSYS is reset to
180 * its old value after the return from syscall. */
182 static int kernelKTC = 0;
184 #ifdef AFS_DECOSF_ENV
186 * SIGSYS semantics are broken on Dec AXP OSF/1 v1.2 systems. You need
187 * to ignore SIGTRAP too. It is claimed to be fixed under v1.3, but...
190 #define CHECK_KERNEL \
191 if (kernelKTC == 0) { \
195 old = (int (*)())signal(SIGSYS, SIG_IGN); \
196 old_t = (int (*)())signal(SIGTRAP, SIG_IGN); \
197 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
199 signal(SIGSYS, old); \
200 signal(SIGTRAP, old_t); \
201 if (code == 0) kernelKTC = 1; \
202 else kernelKTC = 2; \
203 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
206 #else /* AFS_DECOSF_ENV */
208 #define CHECK_KERNEL \
209 if (kernelKTC == 0) { \
212 old = (int (*)())signal(SIGSYS, SIG_IGN); \
213 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
215 signal(SIGSYS, old); \
216 if (code == 0) kernelKTC = 1; \
217 else kernelKTC = 2; \
218 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
220 #endif /* AFS_DECOSF_ENV */
222 #define TRY_KERNEL(cmd,a1,a2,a3,a4) \
224 if (kernelKTC == 1) \
225 return syscall (KTC_SYSCALL, cmd, \
226 KTC_INTERFACE_VERSION, a1,a2,a3,a4); \
230 #define TRY_KERNEL(cmd,a1,a2,a3,a4)
231 #endif /* KERNEL_KTC_COMPAT */
233 #if !defined(UKERNEL)
234 /* this is a structure used to communicate with the afs cache mgr, but is
235 * otherwise irrelevant */
237 afs_int32 AuthHandle;
238 char HandShakeKey[8];
240 afs_int32 BeginTimestamp;
241 afs_int32 EndTimestamp;
243 #endif /* !defined(UKERNEL) */
245 #define MAXLOCALTOKENS 4
249 struct ktc_principal server;
250 struct ktc_principal client;
251 struct ktc_token token;
252 } local_tokens[MAXLOCALTOKENS] = { {
258 /* new interface routines to the ticket cache. Only handle afs service right
262 NewSetToken(struct ktc_principal *aserver,
263 struct ktc_token *atoken,
264 struct ktc_principal *aclient,
267 TRY_KERNEL(KTC_SETTOKEN_OP, aserver, aclient, atoken,
268 sizeof(struct ktc_token));
269 /* no kernel ticket cache */
273 #define MAXPIOCTLTOKENLEN \
274 (3*sizeof(afs_int32)+MAXKTCTICKETLEN+sizeof(struct ClearToken)+MAXKTCREALMLEN)
277 OldSetToken(struct ktc_principal *aserver, struct ktc_token *atoken,
278 struct ktc_principal *aclient, afs_int32 flags)
280 struct ViceIoctl iob;
281 char tbuffer[MAXPIOCTLTOKENLEN];
283 struct ClearToken ct;
284 register afs_int32 code;
287 if (strcmp(aserver->name, "afs") != 0) {
290 for (i = 0; i < MAXLOCALTOKENS; i++)
291 if (local_tokens[i].valid) {
292 if ((strcmp(local_tokens[i].server.name, aserver->name) == 0)
295 (local_tokens[i].server.instance,
296 aserver->instance) == 0)
297 && (strcmp(local_tokens[i].server.cell, aserver->cell) ==
299 found = i; /* replace existing entry */
301 } else /* valid, but no match */
304 found = i; /* remember this empty slot */
307 memcpy(&local_tokens[found].token, atoken, sizeof(struct ktc_token));
308 local_tokens[found].server = *aserver;
309 local_tokens[found].client = *aclient;
310 local_tokens[found].valid = 1;
313 tp = tbuffer; /* start copying here */
314 if ((atoken->ticketLen < MINKTCTICKETLEN)
315 || (atoken->ticketLen > MAXKTCTICKETLEN))
317 memcpy(tp, &atoken->ticketLen, sizeof(afs_int32)); /* copy in ticket length */
318 tp += sizeof(afs_int32);
319 memcpy(tp, atoken->ticket, atoken->ticketLen); /* copy in ticket */
320 tp += atoken->ticketLen;
321 /* next, copy in the "clear token", describing who we are */
322 ct.AuthHandle = atoken->kvno; /* hide auth handle here */
323 memcpy(ct.HandShakeKey, &atoken->sessionKey, 8);
325 ct.BeginTimestamp = atoken->startTime;
326 ct.EndTimestamp = atoken->endTime;
327 if (ct.BeginTimestamp == 0)
328 ct.BeginTimestamp = 1;
330 if ((strlen(aclient->name) > strlen("AFS ID "))
331 && (aclient->instance[0] == 0)) {
333 afs_int32 viceId = 0;
334 char *cp = aclient->name + strlen("AFS ID ");
341 viceId = viceId * 10 + (int)(*cp - '0');
346 ct.ViceId = viceId * sign; /* OK to let any value here? */
347 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 0)
348 ct.BeginTimestamp++; /* force lifetime to be odd */
351 ct.ViceId = getuid(); /* wrong, but works in primary cell */
352 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1)
353 ct.BeginTimestamp++; /* force lifetime to be even */
358 * Information needed by the user space cache manager
360 u.u_expiration = ct.EndTimestamp;
361 u.u_viceid = ct.ViceId;
364 temp = sizeof(struct ClearToken);
365 memcpy(tp, &temp, sizeof(afs_int32));
366 tp += sizeof(afs_int32);
367 memcpy(tp, &ct, sizeof(struct ClearToken));
368 tp += sizeof(struct ClearToken);
370 /* next copy in primary flag */
374 * The following means that setpag will happen inside afs just before
375 * the authentication to prevent the setpag/klog race condition.
377 * The following means that setpag will affect the parent process as
378 * well as the current process.
380 if (flags & AFS_SETTOK_SETPAG)
383 memcpy(tp, &temp, sizeof(afs_int32));
384 tp += sizeof(afs_int32);
386 /* finally copy in the cell name */
387 temp = strlen(aserver->cell);
388 if (temp >= MAXKTCREALMLEN)
390 strcpy(tp, aserver->cell);
393 /* now setup for the pioctl */
395 iob.in_size = tp - tbuffer;
397 iob.out_size = sizeof(tbuffer);
399 #if defined(NO_AFS_CLIENT)
403 if ((tkfile = getenv("TKTFILE"))
406 open(tkfile, O_WRONLY | O_APPEND | O_TRUNC | O_CREAT,
408 printf("Writing ticket to: %s\n", tkfile);
409 code = (write(fd, iob.in, iob.in_size) != iob.in_size);
412 code = KTC_PIOCTLFAIL;
414 #else /* NO_AFS_CLIENT */
415 code = PIOCTL(0, VIOCSETTOK, &iob, 0);
416 #endif /* NO_AFS_CLIENT */
418 return KTC_PIOCTLFAIL;
423 ktc_SetToken(struct ktc_principal *aserver,
424 struct ktc_token *atoken,
425 struct ktc_principal *aclient,
431 #ifdef AFS_KERBEROS_ENV
435 if ( /*!strcmp(aclient->cell, lcell) && this would only store local creds */
436 (strcmp(aserver->name, "AuthServer")
437 || strcmp(aserver->instance, "Admin"))) {
438 if (strcmp(aserver->name, "krbtgt") == 0) {
439 static char lrealm[MAXKTCREALMLEN];
442 ucstring(lrealm, lcell, MAXKTCREALMLEN);
443 if (strcmp(aserver->instance, lrealm) == 0) {
444 afs_tf_create(aclient->name, aclient->instance);
448 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
449 if (ncode == NO_TKT_FIL) {
450 (void)afs_tf_create(aclient->name, aclient->instance);
451 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
455 afs_tf_save_cred(aserver, atoken, aclient);
461 #endif /* NO_AFS_CLIENT */
465 #ifndef NO_AFS_CLIENT
466 ncode = NewSetToken(aserver, atoken, aclient, flags);
467 if (ncode || /* new style failed */
468 (strcmp(aserver->name, "afs") == 0)) { /* for afs tokens do both */
469 ocode = OldSetToken(aserver, atoken, aclient, flags);
472 if (ncode && ocode) {
476 else if (ocode == KTC_PIOCTLFAIL)
484 return KTC_PIOCTLFAIL;
486 #endif /* NO_AFS_CLIENT */
491 /* get token, given server we need and token buffer. aclient will eventually
492 * be set to our identity to the server.
495 ktc_GetToken(struct ktc_principal *aserver, struct ktc_token *atoken,
496 int atokenLen, struct ktc_principal *aclient)
498 struct ViceIoctl iob;
499 char tbuffer[MAXPIOCTLTOKENLEN];
500 register afs_int32 code = 0;
502 char *stp, *cellp; /* secret token ptr */
503 struct ClearToken ct;
506 int maxLen; /* biggest ticket we can copy */
507 int tktLen; /* server ticket length */
508 #ifdef AFS_KERBEROS_ENV
513 #ifndef NO_AFS_CLIENT
514 TRY_KERNEL(KTC_GETTOKEN_OP, aserver, aclient, atoken, atokenLen);
515 #endif /* NO_AFS_CLIENT */
517 #ifdef AFS_KERBEROS_ENV
521 #ifndef NO_AFS_CLIENT
522 if (strcmp(aserver->name, "afs") != 0)
523 #endif /* NO_AFS_CLIENT */
526 /* try the local tokens */
527 for (i = 0; i < MAXLOCALTOKENS; i++)
528 if (local_tokens[i].valid
529 && (strcmp(local_tokens[i].server.name, aserver->name) == 0)
530 && (strcmp(local_tokens[i].server.instance, aserver->instance)
532 && (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
533 memcpy(atoken, &local_tokens[i].token,
534 min(atokenLen, sizeof(struct ktc_token)));
536 *aclient = local_tokens[i].client;
540 #ifdef AFS_KERBEROS_ENV
541 if (!afs_tf_init(ktc_tkt_string(), R_TKT_FIL)) {
543 if (!afs_tf_get_pname(aclient->name)
544 && !afs_tf_get_pinst(aclient->instance))
547 char tmpstring[MAXHOSTCHARS];
548 afs_tf_get_pname(tmpstring);
549 afs_tf_get_pinst(tmpstring);
554 struct ktc_principal cprincipal;
555 struct ktc_token ctoken;
557 while (!afs_tf_get_cred(&cprincipal, &ctoken)) {
558 if (strcmp(cprincipal.name, aserver->name) == 0
559 && strcmp(cprincipal.instance, aserver->instance) == 0
560 && strcmp(cprincipal.cell, aserver->cell) == 0) {
563 strcpy(aclient->cell, lcell);
564 memcpy(atoken, &ctoken,
565 min(atokenLen, sizeof(struct ktc_token)));
578 #ifndef NO_AFS_CLIENT
579 for (index = 0; index < 200; index++) { /* sanity check in case pioctl fails */
580 iob.in = (char *)&index;
581 iob.in_size = sizeof(afs_int32);
583 iob.out_size = sizeof(tbuffer);
585 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
588 /* failed to retrieve specified token */
589 if (code < 0 && errno == EDOM) {
594 /* token retrieved; parse buffer */
597 /* get ticket length */
598 memcpy(&temp, tp, sizeof(afs_int32));
600 tp += sizeof(afs_int32);
602 /* remember where ticket is and skip over it */
606 /* get size of clear token and verify */
607 memcpy(&temp, tp, sizeof(afs_int32));
608 if (temp != sizeof(struct ClearToken)) {
612 tp += sizeof(afs_int32);
614 /* copy clear token */
615 memcpy(&ct, tp, temp);
618 /* skip over primary flag */
619 tp += sizeof(afs_int32);
621 /* remember where cell name is */
624 if ((strcmp(cellp, aserver->cell) == 0)
625 #ifdef AFS_KERBEROS_ENV
626 || (*aserver->cell == '\0' && strcmp(cellp, lcell) == 0)
629 /* got token for cell; check that it will fit */
631 atokenLen - sizeof(struct ktc_token) + MAXKTCTICKETLEN;
632 if (maxLen < tktLen) {
637 /* set return values */
638 memcpy(atoken->ticket, stp, tktLen);
639 atoken->startTime = ct.BeginTimestamp;
640 atoken->endTime = ct.EndTimestamp;
641 if (ct.AuthHandle == -1) {
644 atoken->kvno = ct.AuthHandle;
645 memcpy(&atoken->sessionKey, ct.HandShakeKey,
646 sizeof(struct ktc_encryptionKey));
647 atoken->ticketLen = tktLen;
650 strcpy(aclient->cell, cellp);
651 aclient->instance[0] = 0;
653 if ((atoken->kvno == 999) || /* old style bcrypt ticket */
654 (ct.BeginTimestamp && /* new w/ prserver lookup */
655 (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1))) {
656 sprintf(aclient->name, "AFS ID %d", ct.ViceId);
658 sprintf(aclient->name, "Unix UID %d", ct.ViceId);
666 #endif /* NO_AFS_CLIENT */
669 if ((code < 0) && (errno == EINVAL))
671 return KTC_PIOCTLFAIL; /* probable cause */
675 * Forget tokens for this server and the calling user.
676 * NOT IMPLEMENTED YET!
678 #ifndef NO_AFS_CLIENT
680 ktc_ForgetToken(struct ktc_principal *aserver)
685 TRY_KERNEL(KTC_FORGETTOKEN_OP, aserver, 0, 0, 0);
687 rc = ktc_ForgetAllTokens(); /* bogus, but better */
691 #endif /* NO_AFS_CLIENT */
693 /* ktc_ListTokens - list all tokens. start aprevIndex at 0, it returns the
694 * next rock in (*aindex). (*aserver) is set to the relevant ticket on
698 ktc_ListTokens(int aprevIndex,
700 struct ktc_principal *aserver)
702 struct ViceIoctl iob;
703 char tbuffer[MAXPIOCTLTOKENLEN];
704 register afs_int32 code = 0 ;
706 afs_int32 temp, index;
708 memset(tbuffer, 0, sizeof(tbuffer));
711 #ifndef NO_AFS_CLIENT
712 TRY_KERNEL(KTC_LISTTOKENS_OP, aserver, aprevIndex, aindex, 0);
713 #endif /* NO_AFS_CLIENT */
719 #endif /* NO_AFS_CLIENT */
720 #ifdef AFS_KERBEROS_ENV
723 struct ktc_principal cprincipal;
724 struct ktc_token ctoken;
726 if (afs_tf_init(ktc_tkt_string(), R_TKT_FIL)
727 || afs_tf_get_pname(tbuffer) || afs_tf_get_pinst(tbuffer)) {
733 for (i = 214; i < index; i++) {
734 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
742 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
749 #ifndef NO_AFS_CLIENT
750 if (!strcmp(cprincipal.name, "afs") && cprincipal.instance[0] == 0) {
753 #endif /* NO_AFS_CLIENT */
755 for (i = 0; i < MAXLOCALTOKENS; i++) {
756 if (!strcmp(cprincipal.name, local_tokens[i].server.name)
757 && !strcmp(cprincipal.instance,
758 local_tokens[i].server.instance)
759 && !strcmp(cprincipal.cell, local_tokens[i].server.cell)) {
764 *aserver = cprincipal;
772 #ifndef NO_AFS_CLIENT
773 if (index >= 123) { /* special hack for returning TCS */
774 while (index - 123 < MAXLOCALTOKENS) {
775 if (local_tokens[index - 123].valid) {
776 *aserver = local_tokens[index - 123].server;
784 #ifdef AFS_KERBEROS_ENV
785 return ktc_ListTokens(214, aindex, aserver);
791 /* get tokens from the kernel */
792 while (index < 200) { /* sanity check in case pioctl fails */
793 iob.in = (char *)&index;
794 iob.in_size = sizeof(afs_int32);
796 iob.out_size = sizeof(tbuffer);
797 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
798 if (code < 0 && errno == EDOM) {
801 rc = ktc_ListTokens(123, aindex, aserver);
810 break; /* got a ticket */
811 /* otherwise we should skip this ticket slot */
818 return KTC_PIOCTLFAIL;
824 /* next iterator determined by earlier loop */
827 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of secret token */
828 tp += sizeof(afs_int32);
829 tp += temp; /* skip ticket for now */
830 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of clear token */
831 if (temp != sizeof(struct ClearToken)) {
835 tp += sizeof(afs_int32); /* skip length */
836 tp += temp; /* skip clear token itself */
837 tp += sizeof(afs_int32); /* skip primary flag */
838 /* tp now points to the cell name */
839 strcpy(aserver->cell, tp);
840 aserver->instance[0] = 0;
841 strcpy(aserver->name, "afs");
842 #endif /* NO_AFS_CLIENT */
847 /* discard all tokens from this user's cache */
852 #ifndef NO_AFS_CLIENT
853 TRY_KERNEL(KTC_FORGETALLTOKENS_OP, 0, 0, 0, 0);
854 #endif /* NO_AFS_CLIENT */
861 struct ViceIoctl iob;
862 register afs_int32 code;
865 for (i = 0; i < MAXLOCALTOKENS; i++)
866 local_tokens[i].valid = 0;
872 #ifndef NO_AFS_CLIENT
873 code = PIOCTL(0, VIOCUNPAG, &iob, 0);
875 return KTC_PIOCTLFAIL;
876 #endif /* NO_AFS_CLIENT */
881 ktc_ForgetAllTokens(void)
886 #ifdef AFS_KERBEROS_ENV
887 (void)afs_tf_dest_tkt();
890 ncode = NewForgetAll();
891 ocode = OldForgetAll();
892 if (ncode && ocode) {
895 else if (ocode == KTC_PIOCTLFAIL)
900 return KTC_PIOCTLFAIL;
906 /* ktc_OldPioctl - returns a boolean true if the kernel supports only the old
907 * pioctl interface for delivering AFS tickets to the cache manager. */
914 #ifdef KERNEL_KTC_COMPAT
916 rc = (kernelKTC != 1); /* old style interface */
925 #ifdef AFS_KERBEROS_ENV
927 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
929 * For copying and distribution information, please see the file
936 #include <sys/types.h>
937 #include <sys/stat.h>
938 #include <sys/file.h>
943 #define TF_LCK_RETRY ((unsigned)2) /* seconds to sleep before
944 * retry if ticket file is
948 * fd must be initialized to something that won't ever occur as a real
949 * file descriptor. Since open(2) returns only non-negative numbers as
950 * valid file descriptors, and afs_tf_init always stuffs the return value
951 * from open in here even if it is an error flag, we must
952 * a. Initialize fd to a negative number, to indicate that it is
953 * not initially valid.
954 * b. When checking for a valid fd, assume that negative values
955 * are invalid (ie. when deciding whether afs_tf_init has been
957 * c. In tf_close, be sure it gets reinitialized to a negative
961 static int curpos; /* Position in tfbfr */
962 static int lastpos; /* End of tfbfr */
963 static char tfbfr[BUFSIZ]; /* Buffer for ticket data */
965 static int tf_gets(char *, int);
966 static int tf_read(char *, int);
969 * This file contains routines for manipulating the ticket cache file.
971 * The ticket file is in the following format:
973 * principal's name (null-terminated string)
974 * principal's instance (null-terminated string)
981 * Where "CREDENTIAL_x" consists of the following fixed-length
982 * fields from the CREDENTIALS structure (see "krb.h"):
984 * char service[MAXKTCNAMELEN]
985 * char instance[MAXKTCNAMELEN]
986 * char realm[REALM_SZ]
991 * afs_int32 issue_date
993 * Short description of routines:
995 * afs_tf_init() opens the ticket file and locks it.
997 * afs_tf_get_pname() returns the principal's name.
999 * afs_tf_get_pinst() returns the principal's instance (may be null).
1001 * afs_tf_get_cred() returns the next CREDENTIALS record.
1003 * afs_tf_save_cred() appends a new CREDENTIAL record to the ticket file.
1005 * afs_tf_close() closes the ticket file and releases the lock.
1007 * tf_gets() returns the next null-terminated string. It's an internal
1008 * routine used by afs_tf_get_pname(), afs_tf_get_pinst(), and
1009 * afs_tf_get_cred().
1011 * tf_read() reads a given number of bytes. It's an internal routine
1012 * used by afs_tf_get_cred().
1016 * afs_tf_init() should be called before the other ticket file routines.
1017 * It takes the name of the ticket file to use, "tf_name", and a
1018 * read/write flag "rw" as arguments.
1020 * It tries to open the ticket file, checks the mode, and if everything
1021 * is okay, locks the file. If it's opened for reading, the lock is
1022 * shared. If it's opened for writing, the lock is exclusive.
1024 * Returns 0 if all went well, otherwise one of the following:
1026 * NO_TKT_FIL - file wasn't there
1027 * TKT_FIL_ACC - file was in wrong mode, etc.
1028 * TKT_FIL_LCK - couldn't lock the file, even after a retry
1032 afs_tf_init(char *tf_name, int rw)
1036 struct stat stat_buf;
1048 if (lstat(tf_name, &stat_buf) < 0)
1056 if ((stat_buf.st_uid != me && me != 0)
1057 || ((stat_buf.st_mode & S_IFMT) != S_IFREG))
1061 * If "wflag" is set, open the ticket file in append-writeonly mode
1062 * and lock the ticket file in exclusive mode. If unable to lock
1063 * the file, sleep and try again. If we fail again, return with the
1064 * proper error message.
1067 curpos = sizeof(tfbfr);
1070 fd = open(tf_name, O_RDWR, 0600);
1074 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1075 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1076 sleep(TF_LCK_RETRY);
1077 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1079 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1080 sleep(TF_LCK_RETRY);
1081 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1091 * Otherwise "wflag" is not set and the ticket file should be opened
1092 * for read-only operations and locked for shared access.
1095 fd = open(tf_name, O_RDONLY, 0600);
1099 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1100 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1101 sleep(TF_LCK_RETRY);
1102 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1104 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1105 sleep(TF_LCK_RETRY);
1106 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1117 * afs_tf_get_pname() reads the principal's name from the ticket file. It
1118 * should only be called after afs_tf_init() has been called. The
1119 * principal's name is filled into the "p" parameter. If all goes well,
1120 * 0 is returned. If afs_tf_init() wasn't called, TKT_FIL_INI is
1121 * returned. If the name was null, or EOF was encountered, or the name
1122 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned.
1126 afs_tf_get_pname(char *p)
1131 if (tf_gets(p, MAXKTCNAMELEN) < 2) /* can't be just a null */
1137 * afs_tf_get_pinst() reads the principal's instance from a ticket file.
1138 * It should only be called after afs_tf_init() and afs_tf_get_pname() have
1139 * been called. The instance is filled into the "inst" parameter. If all
1140 * goes well, 0 is returned. If afs_tf_init() wasn't called,
1141 * TKT_FIL_INI is returned. If EOF was encountered, or the instance
1142 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned. Note that the
1143 * instance may be null.
1147 afs_tf_get_pinst(char *inst)
1152 if (tf_gets(inst, MAXKTCNAMELEN) < 1)
1158 * afs_tf_get_cred() reads a CREDENTIALS record from a ticket file and fills
1159 * in the given structure "c". It should only be called after afs_tf_init(),
1160 * afs_tf_get_pname(), and afs_tf_get_pinst() have been called. If all goes
1161 * well, 0 is returned. Possible error codes are:
1163 * TKT_FIL_INI - afs_tf_init wasn't called first
1164 * TKT_FIL_FMT - bad format
1165 * EOF - end of file encountered
1169 afs_tf_get_cred(struct ktc_principal *principal, struct ktc_token *token)
1173 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1178 if ((k_errno = tf_gets(principal->name, MAXKTCNAMELEN)) < 2)
1181 case 1: /* can't be just a null */
1186 if ((k_errno = tf_gets(principal->instance, MAXKTCNAMELEN)) < 1)
1193 if ((k_errno = tf_gets(principal->cell, MAXKTCREALMLEN)) < 2)
1196 case 1: /* can't be just a null */
1201 lcstring(principal->cell, principal->cell, MAXKTCREALMLEN);
1202 if (tf_read((char *)&(token->sessionKey), 8) < 1
1203 || tf_read((char *)&(lifetime), sizeof(lifetime)) < 1
1204 || tf_read((char *)&(kvno), sizeof(kvno)) < 1
1205 || tf_read((char *)&(token->ticketLen), sizeof(token->ticketLen))
1207 /* don't try to read a silly amount into ticket->dat */
1208 token->ticketLen > MAXKTCTICKETLEN
1209 || tf_read((char *)(token->ticket), token->ticketLen) < 1
1210 || tf_read((char *)&mit_compat, sizeof(mit_compat)) < 1) {
1213 token->startTime = mit_compat;
1214 token->endTime = life_to_time(token->startTime, lifetime);
1220 * tf_close() closes the ticket file and sets "fd" to -1. If "fd" is
1221 * not a valid file descriptor, it just returns. It also clears the
1222 * buffer used to read tickets.
1224 * The return value is not defined.
1231 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1232 (void)fcntl(fd, F_SETLK, &fileUlock);
1234 (void)flock(fd, LOCK_UN);
1237 fd = -1; /* see declaration of fd above */
1239 memset(tfbfr, 0, sizeof(tfbfr));
1244 * tf_gets() is an internal routine. It takes a string "s" and a count
1245 * "n", and reads from the file until either it has read "n" characters,
1246 * or until it reads a null byte. When finished, what has been read exists
1249 * Possible return values are:
1251 * n the number of bytes read (including null terminator)
1252 * when all goes well
1254 * 0 end of file or read error
1256 * TOO_BIG if "count" characters are read and no null is
1257 * encountered. This is an indication that the ticket
1258 * file is seriously ill.
1262 tf_gets(register char *s, int n)
1269 for (count = n - 1; count > 0; --count) {
1270 if (curpos >= sizeof(tfbfr)) {
1271 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1274 if (curpos == lastpos) {
1277 *s = tfbfr[curpos++];
1285 * tf_read() is an internal routine. It takes a string "s" and a count
1286 * "n", and reads from the file until "n" bytes have been read. When
1287 * finished, what has been read exists in "s".
1289 * Possible return values are:
1291 * n the number of bytes read when all goes well
1293 * 0 on end of file or read error
1297 tf_read(register char *s, register int n)
1301 for (count = n; count > 0; --count) {
1302 if (curpos >= sizeof(tfbfr)) {
1303 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1306 if (curpos == lastpos) {
1309 *s++ = tfbfr[curpos++];
1315 * afs_tf_save_cred() appends an incoming ticket to the end of the ticket
1316 * file. You must call afs_tf_init() before calling afs_tf_save_cred().
1318 * The "service", "instance", and "realm" arguments specify the
1319 * server's name; "aticket" contains the credential.
1321 * Returns 0 if all goes well, TKT_FIL_INI if afs_tf_init() wasn't
1322 * called previously, and KFAILURE for anything else that went wrong.
1326 afs_tf_save_cred(struct ktc_principal *aserver,
1327 struct ktc_token *atoken,
1328 struct ktc_principal *aclient)
1330 char realm[MAXKTCREALMLEN + 1];
1331 char junk[MAXKTCNAMELEN];
1332 struct ktc_principal principal;
1333 struct ktc_token token;
1337 int count; /* count for write */
1338 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1340 if (fd < 0) { /* fd is ticket file as set by afs_tf_init */
1344 ucstring(realm, aserver->cell, MAXKTCREALMLEN);
1345 realm[MAXKTCREALMLEN] = '\0';
1347 /* Look for a duplicate ticket */
1348 (void)lseek(fd, (off_t) 0L, 0);
1349 curpos = sizeof(tfbfr);
1351 if (afs_tf_get_pname(junk) || strcmp(junk, aclient->name)
1352 || afs_tf_get_pinst(junk) || strcmp(junk, aclient->instance))
1356 start = lseek(fd, (off_t) 0L, 1) - lastpos + curpos;
1357 status = afs_tf_get_cred(&principal, &token);
1358 } while (status == 0
1359 && (strcmp(aserver->name, principal.name) != 0
1360 || strcmp(aserver->instance, principal.instance) != 0
1361 || strcmp(aserver->cell, principal.cell) != 0));
1364 * Two tickets for the same user authenticating to the same service
1365 * should be the same length, but we check here just to make sure.
1367 if (status == 0 && token.ticketLen != atoken->ticketLen)
1369 if (status && status != EOF)
1372 /* Position over the credential we just matched (or the EOF) */
1373 lseek(fd, start, 0);
1374 curpos = lastpos = sizeof(tfbfr);
1376 /* Write the ticket and associated data */
1378 count = strlen(aserver->name) + 1;
1379 if (write(fd, aserver->name, count) != count)
1382 count = strlen(aserver->instance) + 1;
1383 if (write(fd, aserver->instance, count) != count)
1386 count = strlen(realm) + 1;
1387 if (write(fd, realm, count) != count)
1390 if (write(fd, (char *)&atoken->sessionKey, 8) != 8)
1393 lifetime = time_to_life(atoken->startTime, atoken->endTime);
1394 if (write(fd, (char *)&lifetime, sizeof(int)) != sizeof(int))
1397 kvno = atoken->kvno;
1398 if (write(fd, (char *)&kvno, sizeof(int)) != sizeof(int))
1401 if (write(fd, (char *)&(atoken->ticketLen), sizeof(int)) != sizeof(int))
1404 count = atoken->ticketLen;
1405 if (write(fd, atoken->ticket, count) != count)
1408 mit_compat = atoken->startTime;
1409 if (write(fd, (char *)&mit_compat, sizeof(mit_compat))
1410 != sizeof(mit_compat))
1413 /* Actually, we should check each write for success */
1420 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
1423 * For copying and distribution information, please see the file
1424 * <mit-copyright.h>.
1428 * This routine is used to generate the name of the file that holds
1429 * the user's cache of server tickets and associated session keys.
1431 * If it is set, krb_ticket_string contains the ticket file name.
1432 * Otherwise, the filename is constructed as follows:
1434 * If it is set, the environment variable "KRBTKFILE" will be used as
1435 * the ticket file name. Otherwise TKT_ROOT (defined in "krb.h") and
1436 * the user's uid are concatenated to produce the ticket file name
1437 * (e.g., "/tmp/tkt123"). A pointer to the string containing the ticket
1438 * file name is returned.
1441 static char krb_ticket_string[4096] = "";
1444 ktc_tkt_string(void)
1446 return ktc_tkt_string_uid(getuid());
1450 ktc_tkt_string_uid(afs_uint32 uid)
1455 if (!*krb_ticket_string) {
1456 if ((env = getenv("KRBTKFILE"))) {
1457 (void)strncpy(krb_ticket_string, env,
1458 sizeof(krb_ticket_string) - 1);
1459 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1461 /* 32 bits of signed integer will always fit in 11 characters
1462 * (including the sign), so no need to worry about overflow */
1463 (void)sprintf(krb_ticket_string, "%s%d", TKT_ROOT, uid);
1466 UNLOCK_GLOBAL_MUTEX;
1467 return krb_ticket_string;
1471 * This routine is used to set the name of the file that holds the user's
1472 * cache of server tickets and associated session keys.
1474 * The value passed in is copied into local storage.
1476 * NOTE: This routine should be called during initialization, before other
1477 * Kerberos routines are called; otherwise tkt_string() above may be called
1478 * and return an undesired ticket file name until this routine is called.
1482 ktc_set_tkt_string(char * val)
1486 (void)strncpy(krb_ticket_string, val, sizeof(krb_ticket_string) - 1);
1487 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1488 UNLOCK_GLOBAL_MUTEX;
1493 * tf_create() is used to initialize the ticket store. It creates the
1494 * file to contain the tickets and writes the given user's name "pname"
1495 * and instance "pinst" in the file. in_tkt() returns KSUCCESS on
1496 * success, or KFAILURE if something goes wrong.
1500 afs_tf_create(char *pname, char *pinst)
1505 char *file = ktc_tkt_string();
1514 if (lstat(file, &sbuf) == 0) {
1515 if ((sbuf.st_uid != me && me != 0)
1516 || ((sbuf.st_mode & S_IFMT) != S_IFREG) || sbuf.st_mode & 077) {
1519 /* file already exists, and permissions appear ok, so nuke it */
1520 if ((fd = open(file, O_RDWR, 0)) < 0)
1521 goto out; /* can't zero it, but we can still try truncating it */
1523 memset(zerobuf, 0, sizeof(zerobuf));
1525 for (i = 0; i < sbuf.st_size; i += sizeof(zerobuf))
1526 if (write(fd, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf)) {
1537 /* arrange so the file is owned by the ruid
1538 * (swap real & effective uid if necessary).
1539 * This isn't a security problem, since the ticket file, if it already
1540 * exists, has the right uid (== ruid) and mode. */
1542 if (setreuid(metoo, me) < 0) {
1546 tktfile = creat(file, 0600);
1548 if (setreuid(me, metoo) < 0) {
1549 /* can't switch??? fail! */
1556 count = strlen(pname) + 1;
1557 if (write(tktfile, pname, count) != count) {
1558 (void)close(tktfile);
1561 count = strlen(pinst) + 1;
1562 if (write(tktfile, pinst, count) != count) {
1563 (void)close(tktfile);
1566 (void)close(tktfile);
1571 * dest_tkt() is used to destroy the ticket store upon logout.
1572 * If the ticket file does not exist, dest_tkt() returns RET_TKFIL.
1573 * Otherwise the function returns 0 on success, KFAILURE on
1578 afs_tf_dest_tkt(void)
1580 char *file = ktc_tkt_string();
1586 if (lstat(file, &statb) < 0)
1589 if (!(statb.st_mode & S_IFREG))
1592 if ((fd = open(file, O_RDWR, 0)) < 0)
1595 memset(buf, 0, BUFSIZ);
1597 for (i = 0; i < statb.st_size; i += BUFSIZ)
1598 if (write(fd, buf, BUFSIZ) != BUFSIZ) {
1610 if (errno == ENOENT)
1612 else if (errno != 0)
1620 #if defined(AFS_AIX51_ENV)
1621 int code = getpagvalue("afs");
1622 if (code < 0 && errno == EINVAL)
1626 gid_t groups[NGROUPS_MAX];
1628 afs_uint32 h, l, ret;
1630 if (getgroups(sizeof groups / sizeof groups[0], groups) < 2)
1633 g0 = groups[0] & 0xffff;
1634 g1 = groups[1] & 0xffff;
1637 if (g0 < 0xc000 && g1 < 0xc000) {
1638 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
1640 h = (g1 >> 14) + h + h + h;
1641 ret = ((h << 28) | l);
1642 /* Additional testing */
1643 if (((ret >> 24) & 0xff) == 'A')
1655 extern char **environ;
1659 char fname[256], *prefix = "/ticket/";
1661 char **newenv, **senv, **denv;
1664 if (stat("/ticket", &sbuf) == -1) {
1665 prefix = "/tmp/tkt";
1668 pag = curpag() & 0xffffffff;
1670 sprintf(fname, "%s%d", prefix, getuid());
1672 sprintf(fname, "%sp%lu", prefix, afs_cast_uint32(pag));
1674 ktc_set_tkt_string(fname);
1676 for (senv = environ, numenv = 0; *senv; senv++)
1678 newenv = (char **)malloc((numenv + 2) * sizeof(char *));
1680 for (senv = environ, denv = newenv; *senv; senv++) {
1681 if (strncmp(*senv, "KRBTKFILE=", 10) != 0)
1685 *denv = (char *)malloc(10 + strlen(fname) + 1);
1686 strcpy(*denv, "KRBTKFILE=");
1687 strcat(*denv, fname);
1690 UNLOCK_GLOBAL_MUTEX;
1695 * BLETCH! We have to invoke the entire afsconf package just to
1696 * find out what the local cell is.
1702 struct afsconf_dir *conf;
1704 if ((conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))
1705 || (conf = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH))) {
1706 code = afsconf_GetLocalCell(conf, lcell, sizeof(lcell));
1707 afsconf_Close(conf);
1709 if (!conf || code) {
1710 printf("** Can't determine local cell name!\n");