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>
66 #include <afs/venus.h>
67 #include <afs/afsutil.h>
68 #include <afs/sys_prototypes.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(char *, int);
132 int afs_tf_get_pname(char *);
133 int afs_tf_get_pinst(char *);
134 int afs_tf_get_cred(struct ktc_principal *, struct ktc_token *);
135 int afs_tf_save_cred(struct ktc_principal *, struct ktc_token *,
136 struct ktc_principal *);
137 int afs_tf_close(void);
138 int afs_tf_create(char *, char *);
139 int afs_tf_dest_tkt(void);
140 static void ktc_LocalCell(void);
141 #endif /* AFS_KERBEROS_ENV */
144 #define PIOCTL afs_pioctl
145 #elif defined(UKERNEL)
146 #define PIOCTL(A,B,C,D) call_syscall(AFSCALL_PIOCTL,A,B,C,D)
148 #define PIOCTL pioctl
152 #ifdef KERNEL_KTC_COMPAT
155 #define KTC_SYSCALL 32
158 /* Kernel call opcode definitions */
159 #define KTC_OPCODE_BASE 4300
160 #define KTC_NO_OP (0+KTC_OPCODE_BASE)
161 #define KTC_SETTOKEN_OP (1+KTC_OPCODE_BASE)
162 #define KTC_GETTOKEN_OP (2+KTC_OPCODE_BASE)
163 #define KTC_LISTTOKENS_OP (3+KTC_OPCODE_BASE)
164 #define KTC_FORGETTOKEN_OP (4+KTC_OPCODE_BASE)
165 #define KTC_FORGETALLTOKENS_OP (5+KTC_OPCODE_BASE)
166 #define KTC_STATUS_OP (6+KTC_OPCODE_BASE)
167 #define KTC_GC_OP (7+KTC_OPCODE_BASE)
169 #define KTC_INTERFACE_VERSION 3
171 /* We have to determine if the kernel supports the ktc system call. To do so
172 * we attempt to execute its noop function. If this is successful we use the
173 * kernel calls in the future otherwise we let the old code run. */
175 /* To safely check to see whether a system call exists we have to intercept the
176 * SIGSYS signal which is caused by executing a non-existant system call. If
177 * it is ignored the syscall routine returns EINVAL. The SIGSYS is reset to
178 * its old value after the return from syscall. */
180 static int kernelKTC = 0;
182 #ifdef AFS_DECOSF_ENV
184 * SIGSYS semantics are broken on Dec AXP OSF/1 v1.2 systems. You need
185 * to ignore SIGTRAP too. It is claimed to be fixed under v1.3, but...
188 #define CHECK_KERNEL \
189 if (kernelKTC == 0) { \
193 old = (int (*)())signal(SIGSYS, SIG_IGN); \
194 old_t = (int (*)())signal(SIGTRAP, SIG_IGN); \
195 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
197 signal(SIGSYS, old); \
198 signal(SIGTRAP, old_t); \
199 if (code == 0) kernelKTC = 1; \
200 else kernelKTC = 2; \
201 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
204 #else /* AFS_DECOSF_ENV */
206 #define CHECK_KERNEL \
207 if (kernelKTC == 0) { \
210 old = (int (*)())signal(SIGSYS, SIG_IGN); \
211 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
213 signal(SIGSYS, old); \
214 if (code == 0) kernelKTC = 1; \
215 else kernelKTC = 2; \
216 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
218 #endif /* AFS_DECOSF_ENV */
220 #define TRY_KERNEL(cmd,a1,a2,a3,a4) \
222 if (kernelKTC == 1) \
223 return syscall (KTC_SYSCALL, cmd, \
224 KTC_INTERFACE_VERSION, a1,a2,a3,a4); \
228 #define TRY_KERNEL(cmd,a1,a2,a3,a4)
229 #endif /* KERNEL_KTC_COMPAT */
231 #if !defined(UKERNEL)
232 /* this is a structure used to communicate with the afs cache mgr, but is
233 * otherwise irrelevant */
235 afs_int32 AuthHandle;
236 char HandShakeKey[8];
238 afs_int32 BeginTimestamp;
239 afs_int32 EndTimestamp;
241 #endif /* !defined(UKERNEL) */
243 #define MAXLOCALTOKENS 4
247 struct ktc_principal server;
248 struct ktc_principal client;
249 struct ktc_token token;
250 } local_tokens[MAXLOCALTOKENS] = { {
256 /* new interface routines to the ticket cache. Only handle afs service right
260 NewSetToken(struct ktc_principal *aserver,
261 struct ktc_token *atoken,
262 struct ktc_principal *aclient,
265 TRY_KERNEL(KTC_SETTOKEN_OP, aserver, aclient, atoken,
266 sizeof(struct ktc_token));
267 /* no kernel ticket cache */
271 #define MAXPIOCTLTOKENLEN \
272 (3*sizeof(afs_int32)+MAXKTCTICKETLEN+sizeof(struct ClearToken)+MAXKTCREALMLEN)
275 OldSetToken(struct ktc_principal *aserver, struct ktc_token *atoken,
276 struct ktc_principal *aclient, afs_int32 flags)
278 struct ViceIoctl iob;
279 char tbuffer[MAXPIOCTLTOKENLEN];
281 struct ClearToken ct;
282 register afs_int32 code;
285 if (strcmp(aserver->name, "afs") != 0) {
288 for (i = 0; i < MAXLOCALTOKENS; i++)
289 if (local_tokens[i].valid) {
290 if ((strcmp(local_tokens[i].server.name, aserver->name) == 0)
293 (local_tokens[i].server.instance,
294 aserver->instance) == 0)
295 && (strcmp(local_tokens[i].server.cell, aserver->cell) ==
297 found = i; /* replace existing entry */
299 } else /* valid, but no match */
302 found = i; /* remember this empty slot */
305 memcpy(&local_tokens[found].token, atoken, sizeof(struct ktc_token));
306 local_tokens[found].server = *aserver;
307 local_tokens[found].client = *aclient;
308 local_tokens[found].valid = 1;
311 tp = tbuffer; /* start copying here */
312 if ((atoken->ticketLen < MINKTCTICKETLEN)
313 || (atoken->ticketLen > MAXKTCTICKETLEN))
315 memcpy(tp, &atoken->ticketLen, sizeof(afs_int32)); /* copy in ticket length */
316 tp += sizeof(afs_int32);
317 memcpy(tp, atoken->ticket, atoken->ticketLen); /* copy in ticket */
318 tp += atoken->ticketLen;
319 /* next, copy in the "clear token", describing who we are */
320 ct.AuthHandle = atoken->kvno; /* hide auth handle here */
321 memcpy(ct.HandShakeKey, &atoken->sessionKey, 8);
323 ct.BeginTimestamp = atoken->startTime;
324 ct.EndTimestamp = atoken->endTime;
325 if (ct.BeginTimestamp == 0)
326 ct.BeginTimestamp = 1;
328 if ((strlen(aclient->name) > strlen("AFS ID "))
329 && (aclient->instance[0] == 0)) {
331 afs_int32 viceId = 0;
332 char *cp = aclient->name + strlen("AFS ID ");
339 viceId = viceId * 10 + (int)(*cp - '0');
344 ct.ViceId = viceId * sign; /* OK to let any value here? */
345 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 0)
346 ct.BeginTimestamp++; /* force lifetime to be odd */
349 ct.ViceId = getuid(); /* wrong, but works in primary cell */
350 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1)
351 ct.BeginTimestamp++; /* force lifetime to be even */
356 * Information needed by the user space cache manager
358 u.u_expiration = ct.EndTimestamp;
359 u.u_viceid = ct.ViceId;
362 temp = sizeof(struct ClearToken);
363 memcpy(tp, &temp, sizeof(afs_int32));
364 tp += sizeof(afs_int32);
365 memcpy(tp, &ct, sizeof(struct ClearToken));
366 tp += sizeof(struct ClearToken);
368 /* next copy in primary flag */
372 * The following means that setpag will happen inside afs just before
373 * the authentication to prevent the setpag/klog race condition.
375 * The following means that setpag will affect the parent process as
376 * well as the current process.
378 if (flags & AFS_SETTOK_SETPAG)
381 memcpy(tp, &temp, sizeof(afs_int32));
382 tp += sizeof(afs_int32);
384 /* finally copy in the cell name */
385 temp = strlen(aserver->cell);
386 if (temp >= MAXKTCREALMLEN)
388 strcpy(tp, aserver->cell);
391 /* now setup for the pioctl */
393 iob.in_size = tp - tbuffer;
395 iob.out_size = sizeof(tbuffer);
397 #if defined(NO_AFS_CLIENT)
401 if ((tkfile = getenv("TKTFILE"))
404 open(tkfile, O_WRONLY | O_APPEND | O_TRUNC | O_CREAT,
406 printf("Writing ticket to: %s\n", tkfile);
407 code = (write(fd, iob.in, iob.in_size) != iob.in_size);
410 code = KTC_PIOCTLFAIL;
412 #else /* NO_AFS_CLIENT */
413 code = PIOCTL(0, VIOCSETTOK, &iob, 0);
414 #endif /* NO_AFS_CLIENT */
416 return KTC_PIOCTLFAIL;
421 ktc_SetToken(struct ktc_principal *aserver,
422 struct ktc_token *atoken,
423 struct ktc_principal *aclient,
429 #ifdef AFS_KERBEROS_ENV
433 if ( /*!strcmp(aclient->cell, lcell) && this would only store local creds */
434 (strcmp(aserver->name, "AuthServer")
435 || strcmp(aserver->instance, "Admin"))) {
436 if (strcmp(aserver->name, "krbtgt") == 0) {
437 static char lrealm[MAXKTCREALMLEN];
440 ucstring(lrealm, lcell, MAXKTCREALMLEN);
441 if (strcmp(aserver->instance, lrealm) == 0) {
442 afs_tf_create(aclient->name, aclient->instance);
446 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
447 if (ncode == NO_TKT_FIL) {
448 (void)afs_tf_create(aclient->name, aclient->instance);
449 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
453 afs_tf_save_cred(aserver, atoken, aclient);
459 #endif /* NO_AFS_CLIENT */
463 #ifndef NO_AFS_CLIENT
464 ncode = NewSetToken(aserver, atoken, aclient, flags);
465 if (ncode || /* new style failed */
466 (strcmp(aserver->name, "afs") == 0)) { /* for afs tokens do both */
467 ocode = OldSetToken(aserver, atoken, aclient, flags);
470 if (ncode && ocode) {
474 else if (ocode == KTC_PIOCTLFAIL)
482 return KTC_PIOCTLFAIL;
484 #endif /* NO_AFS_CLIENT */
489 /* get token, given server we need and token buffer. aclient will eventually
490 * be set to our identity to the server.
493 ktc_GetToken(struct ktc_principal *aserver, struct ktc_token *atoken,
494 int atokenLen, struct ktc_principal *aclient)
496 struct ViceIoctl iob;
497 char tbuffer[MAXPIOCTLTOKENLEN];
498 register afs_int32 code = 0;
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 */
506 #ifdef AFS_KERBEROS_ENV
511 #ifndef NO_AFS_CLIENT
512 TRY_KERNEL(KTC_GETTOKEN_OP, aserver, aclient, atoken, atokenLen);
513 #endif /* NO_AFS_CLIENT */
515 #ifdef AFS_KERBEROS_ENV
519 #ifndef NO_AFS_CLIENT
520 if (strcmp(aserver->name, "afs") != 0)
521 #endif /* NO_AFS_CLIENT */
524 /* try the local tokens */
525 for (i = 0; i < MAXLOCALTOKENS; i++)
526 if (local_tokens[i].valid
527 && (strcmp(local_tokens[i].server.name, aserver->name) == 0)
528 && (strcmp(local_tokens[i].server.instance, aserver->instance)
530 && (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
531 memcpy(atoken, &local_tokens[i].token,
532 min(atokenLen, sizeof(struct ktc_token)));
534 *aclient = local_tokens[i].client;
538 #ifdef AFS_KERBEROS_ENV
539 if (!afs_tf_init(ktc_tkt_string(), R_TKT_FIL)) {
541 if (!afs_tf_get_pname(aclient->name)
542 && !afs_tf_get_pinst(aclient->instance))
545 char tmpstring[MAXHOSTCHARS];
546 afs_tf_get_pname(tmpstring);
547 afs_tf_get_pinst(tmpstring);
552 struct ktc_principal cprincipal;
553 struct ktc_token ctoken;
555 while (!afs_tf_get_cred(&cprincipal, &ctoken)) {
556 if (strcmp(cprincipal.name, aserver->name) == 0
557 && strcmp(cprincipal.instance, aserver->instance) == 0
558 && strcmp(cprincipal.cell, aserver->cell) == 0) {
561 strcpy(aclient->cell, lcell);
562 memcpy(atoken, &ctoken,
563 min(atokenLen, sizeof(struct ktc_token)));
576 #ifndef NO_AFS_CLIENT
577 for (index = 0; index < 200; index++) { /* sanity check in case pioctl fails */
578 iob.in = (char *)&index;
579 iob.in_size = sizeof(afs_int32);
581 iob.out_size = sizeof(tbuffer);
583 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
586 /* failed to retrieve specified token */
587 if (code < 0 && errno == EDOM) {
592 /* token retrieved; parse buffer */
595 /* get ticket length */
596 memcpy(&temp, tp, sizeof(afs_int32));
598 tp += sizeof(afs_int32);
600 /* remember where ticket is and skip over it */
604 /* get size of clear token and verify */
605 memcpy(&temp, tp, sizeof(afs_int32));
606 if (temp != sizeof(struct ClearToken)) {
610 tp += sizeof(afs_int32);
612 /* copy clear token */
613 memcpy(&ct, tp, temp);
616 /* skip over primary flag */
617 tp += sizeof(afs_int32);
619 /* remember where cell name is */
622 if ((strcmp(cellp, aserver->cell) == 0)
623 #ifdef AFS_KERBEROS_ENV
624 || (*aserver->cell == '\0' && strcmp(cellp, lcell) == 0)
627 /* got token for cell; check that it will fit */
629 atokenLen - sizeof(struct ktc_token) + MAXKTCTICKETLEN;
630 if (maxLen < tktLen) {
635 /* set return values */
636 memcpy(atoken->ticket, stp, tktLen);
637 atoken->startTime = ct.BeginTimestamp;
638 atoken->endTime = ct.EndTimestamp;
639 if (ct.AuthHandle == -1) {
642 atoken->kvno = ct.AuthHandle;
643 memcpy(&atoken->sessionKey, ct.HandShakeKey,
644 sizeof(struct ktc_encryptionKey));
645 atoken->ticketLen = tktLen;
648 strcpy(aclient->cell, cellp);
649 aclient->instance[0] = 0;
651 if ((atoken->kvno == 999) || /* old style bcrypt ticket */
652 (ct.BeginTimestamp && /* new w/ prserver lookup */
653 (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1))) {
654 sprintf(aclient->name, "AFS ID %d", ct.ViceId);
656 sprintf(aclient->name, "Unix UID %d", ct.ViceId);
664 #endif /* NO_AFS_CLIENT */
667 if ((code < 0) && (errno == EINVAL))
669 return KTC_PIOCTLFAIL; /* probable cause */
673 * Forget tokens for this server and the calling user.
674 * NOT IMPLEMENTED YET!
676 #ifndef NO_AFS_CLIENT
678 ktc_ForgetToken(struct ktc_principal *aserver)
683 TRY_KERNEL(KTC_FORGETTOKEN_OP, aserver, 0, 0, 0);
685 rc = ktc_ForgetAllTokens(); /* bogus, but better */
689 #endif /* NO_AFS_CLIENT */
691 /* ktc_ListTokens - list all tokens. start aprevIndex at 0, it returns the
692 * next rock in (*aindex). (*aserver) is set to the relevant ticket on
696 ktc_ListTokens(int aprevIndex,
698 struct ktc_principal *aserver)
700 struct ViceIoctl iob;
701 char tbuffer[MAXPIOCTLTOKENLEN];
702 register afs_int32 code = 0 ;
704 afs_int32 temp, index;
706 memset(tbuffer, 0, sizeof(tbuffer));
709 #ifndef NO_AFS_CLIENT
710 TRY_KERNEL(KTC_LISTTOKENS_OP, aserver, aprevIndex, aindex, 0);
711 #endif /* NO_AFS_CLIENT */
717 #endif /* NO_AFS_CLIENT */
718 #ifdef AFS_KERBEROS_ENV
721 struct ktc_principal cprincipal;
722 struct ktc_token ctoken;
724 if (afs_tf_init(ktc_tkt_string(), R_TKT_FIL)
725 || afs_tf_get_pname(tbuffer) || afs_tf_get_pinst(tbuffer)) {
731 for (i = 214; i < index; i++) {
732 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
740 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
747 #ifndef NO_AFS_CLIENT
748 if (!strcmp(cprincipal.name, "afs") && cprincipal.instance[0] == 0) {
751 #endif /* NO_AFS_CLIENT */
753 for (i = 0; i < MAXLOCALTOKENS; i++) {
754 if (!strcmp(cprincipal.name, local_tokens[i].server.name)
755 && !strcmp(cprincipal.instance,
756 local_tokens[i].server.instance)
757 && !strcmp(cprincipal.cell, local_tokens[i].server.cell)) {
762 *aserver = cprincipal;
770 #ifndef NO_AFS_CLIENT
771 if (index >= 123) { /* special hack for returning TCS */
772 while (index - 123 < MAXLOCALTOKENS) {
773 if (local_tokens[index - 123].valid) {
774 *aserver = local_tokens[index - 123].server;
782 #ifdef AFS_KERBEROS_ENV
783 return ktc_ListTokens(214, aindex, aserver);
789 /* get tokens from the kernel */
790 while (index < 200) { /* sanity check in case pioctl fails */
791 iob.in = (char *)&index;
792 iob.in_size = sizeof(afs_int32);
794 iob.out_size = sizeof(tbuffer);
795 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
796 if (code < 0 && errno == EDOM) {
799 rc = ktc_ListTokens(123, aindex, aserver);
808 break; /* got a ticket */
809 /* otherwise we should skip this ticket slot */
816 return KTC_PIOCTLFAIL;
822 /* next iterator determined by earlier loop */
825 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of secret token */
826 tp += sizeof(afs_int32);
827 tp += temp; /* skip ticket for now */
828 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of clear token */
829 if (temp != sizeof(struct ClearToken)) {
833 tp += sizeof(afs_int32); /* skip length */
834 tp += temp; /* skip clear token itself */
835 tp += sizeof(afs_int32); /* skip primary flag */
836 /* tp now points to the cell name */
837 strcpy(aserver->cell, tp);
838 aserver->instance[0] = 0;
839 strcpy(aserver->name, "afs");
840 #endif /* NO_AFS_CLIENT */
845 /* discard all tokens from this user's cache */
850 #ifndef NO_AFS_CLIENT
851 TRY_KERNEL(KTC_FORGETALLTOKENS_OP, 0, 0, 0, 0);
852 #endif /* NO_AFS_CLIENT */
859 struct ViceIoctl iob;
860 register afs_int32 code;
863 for (i = 0; i < MAXLOCALTOKENS; i++)
864 local_tokens[i].valid = 0;
870 #ifndef NO_AFS_CLIENT
871 code = PIOCTL(0, VIOCUNPAG, &iob, 0);
873 return KTC_PIOCTLFAIL;
874 #endif /* NO_AFS_CLIENT */
879 ktc_ForgetAllTokens(void)
884 #ifdef AFS_KERBEROS_ENV
885 (void)afs_tf_dest_tkt();
888 ncode = NewForgetAll();
889 ocode = OldForgetAll();
890 if (ncode && ocode) {
893 else if (ocode == KTC_PIOCTLFAIL)
898 return KTC_PIOCTLFAIL;
904 /* ktc_OldPioctl - returns a boolean true if the kernel supports only the old
905 * pioctl interface for delivering AFS tickets to the cache manager. */
912 #ifdef KERNEL_KTC_COMPAT
914 rc = (kernelKTC != 1); /* old style interface */
923 #ifdef AFS_KERBEROS_ENV
925 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
927 * For copying and distribution information, please see the file
934 #include <sys/types.h>
935 #include <sys/stat.h>
936 #include <sys/file.h>
941 #define TF_LCK_RETRY ((unsigned)2) /* seconds to sleep before
942 * retry if ticket file is
946 * fd must be initialized to something that won't ever occur as a real
947 * file descriptor. Since open(2) returns only non-negative numbers as
948 * valid file descriptors, and afs_tf_init always stuffs the return value
949 * from open in here even if it is an error flag, we must
950 * a. Initialize fd to a negative number, to indicate that it is
951 * not initially valid.
952 * b. When checking for a valid fd, assume that negative values
953 * are invalid (ie. when deciding whether afs_tf_init has been
955 * c. In tf_close, be sure it gets reinitialized to a negative
959 static int curpos; /* Position in tfbfr */
960 static int lastpos; /* End of tfbfr */
961 static char tfbfr[BUFSIZ]; /* Buffer for ticket data */
963 static int tf_gets(char *, int);
964 static int tf_read(char *, int);
967 * This file contains routines for manipulating the ticket cache file.
969 * The ticket file is in the following format:
971 * principal's name (null-terminated string)
972 * principal's instance (null-terminated string)
979 * Where "CREDENTIAL_x" consists of the following fixed-length
980 * fields from the CREDENTIALS structure (see "krb.h"):
982 * char service[MAXKTCNAMELEN]
983 * char instance[MAXKTCNAMELEN]
984 * char realm[REALM_SZ]
989 * afs_int32 issue_date
991 * Short description of routines:
993 * afs_tf_init() opens the ticket file and locks it.
995 * afs_tf_get_pname() returns the principal's name.
997 * afs_tf_get_pinst() returns the principal's instance (may be null).
999 * afs_tf_get_cred() returns the next CREDENTIALS record.
1001 * afs_tf_save_cred() appends a new CREDENTIAL record to the ticket file.
1003 * afs_tf_close() closes the ticket file and releases the lock.
1005 * tf_gets() returns the next null-terminated string. It's an internal
1006 * routine used by afs_tf_get_pname(), afs_tf_get_pinst(), and
1007 * afs_tf_get_cred().
1009 * tf_read() reads a given number of bytes. It's an internal routine
1010 * used by afs_tf_get_cred().
1014 * afs_tf_init() should be called before the other ticket file routines.
1015 * It takes the name of the ticket file to use, "tf_name", and a
1016 * read/write flag "rw" as arguments.
1018 * It tries to open the ticket file, checks the mode, and if everything
1019 * is okay, locks the file. If it's opened for reading, the lock is
1020 * shared. If it's opened for writing, the lock is exclusive.
1022 * Returns 0 if all went well, otherwise one of the following:
1024 * NO_TKT_FIL - file wasn't there
1025 * TKT_FIL_ACC - file was in wrong mode, etc.
1026 * TKT_FIL_LCK - couldn't lock the file, even after a retry
1030 afs_tf_init(char *tf_name, int rw)
1034 struct stat stat_buf;
1046 if (lstat(tf_name, &stat_buf) < 0)
1054 if ((stat_buf.st_uid != me && me != 0)
1055 || ((stat_buf.st_mode & S_IFMT) != S_IFREG))
1059 * If "wflag" is set, open the ticket file in append-writeonly mode
1060 * and lock the ticket file in exclusive mode. If unable to lock
1061 * the file, sleep and try again. If we fail again, return with the
1062 * proper error message.
1065 curpos = sizeof(tfbfr);
1068 fd = open(tf_name, O_RDWR, 0600);
1072 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1073 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1074 sleep(TF_LCK_RETRY);
1075 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1077 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1078 sleep(TF_LCK_RETRY);
1079 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1089 * Otherwise "wflag" is not set and the ticket file should be opened
1090 * for read-only operations and locked for shared access.
1093 fd = open(tf_name, O_RDONLY, 0600);
1097 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1098 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1099 sleep(TF_LCK_RETRY);
1100 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1102 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1103 sleep(TF_LCK_RETRY);
1104 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1115 * afs_tf_get_pname() reads the principal's name from the ticket file. It
1116 * should only be called after afs_tf_init() has been called. The
1117 * principal's name is filled into the "p" parameter. If all goes well,
1118 * 0 is returned. If afs_tf_init() wasn't called, TKT_FIL_INI is
1119 * returned. If the name was null, or EOF was encountered, or the name
1120 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned.
1124 afs_tf_get_pname(char *p)
1129 if (tf_gets(p, MAXKTCNAMELEN) < 2) /* can't be just a null */
1135 * afs_tf_get_pinst() reads the principal's instance from a ticket file.
1136 * It should only be called after afs_tf_init() and afs_tf_get_pname() have
1137 * been called. The instance is filled into the "inst" parameter. If all
1138 * goes well, 0 is returned. If afs_tf_init() wasn't called,
1139 * TKT_FIL_INI is returned. If EOF was encountered, or the instance
1140 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned. Note that the
1141 * instance may be null.
1145 afs_tf_get_pinst(char *inst)
1150 if (tf_gets(inst, MAXKTCNAMELEN) < 1)
1156 * afs_tf_get_cred() reads a CREDENTIALS record from a ticket file and fills
1157 * in the given structure "c". It should only be called after afs_tf_init(),
1158 * afs_tf_get_pname(), and afs_tf_get_pinst() have been called. If all goes
1159 * well, 0 is returned. Possible error codes are:
1161 * TKT_FIL_INI - afs_tf_init wasn't called first
1162 * TKT_FIL_FMT - bad format
1163 * EOF - end of file encountered
1167 afs_tf_get_cred(struct ktc_principal *principal, struct ktc_token *token)
1171 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1176 if ((k_errno = tf_gets(principal->name, MAXKTCNAMELEN)) < 2)
1179 case 1: /* can't be just a null */
1184 if ((k_errno = tf_gets(principal->instance, MAXKTCNAMELEN)) < 1)
1191 if ((k_errno = tf_gets(principal->cell, MAXKTCREALMLEN)) < 2)
1194 case 1: /* can't be just a null */
1199 lcstring(principal->cell, principal->cell, MAXKTCREALMLEN);
1200 if (tf_read((char *)&(token->sessionKey), 8) < 1
1201 || tf_read((char *)&(lifetime), sizeof(lifetime)) < 1
1202 || tf_read((char *)&(kvno), sizeof(kvno)) < 1
1203 || tf_read((char *)&(token->ticketLen), sizeof(token->ticketLen))
1205 /* don't try to read a silly amount into ticket->dat */
1206 token->ticketLen > MAXKTCTICKETLEN
1207 || tf_read((char *)(token->ticket), token->ticketLen) < 1
1208 || tf_read((char *)&mit_compat, sizeof(mit_compat)) < 1) {
1211 token->startTime = mit_compat;
1212 token->endTime = life_to_time(token->startTime, lifetime);
1218 * tf_close() closes the ticket file and sets "fd" to -1. If "fd" is
1219 * not a valid file descriptor, it just returns. It also clears the
1220 * buffer used to read tickets.
1222 * The return value is not defined.
1229 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1230 (void)fcntl(fd, F_SETLK, &fileUlock);
1232 (void)flock(fd, LOCK_UN);
1235 fd = -1; /* see declaration of fd above */
1237 memset(tfbfr, 0, sizeof(tfbfr));
1242 * tf_gets() is an internal routine. It takes a string "s" and a count
1243 * "n", and reads from the file until either it has read "n" characters,
1244 * or until it reads a null byte. When finished, what has been read exists
1247 * Possible return values are:
1249 * n the number of bytes read (including null terminator)
1250 * when all goes well
1252 * 0 end of file or read error
1254 * TOO_BIG if "count" characters are read and no null is
1255 * encountered. This is an indication that the ticket
1256 * file is seriously ill.
1260 tf_gets(register char *s, int n)
1267 for (count = n - 1; count > 0; --count) {
1268 if (curpos >= sizeof(tfbfr)) {
1269 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1272 if (curpos == lastpos) {
1275 *s = tfbfr[curpos++];
1283 * tf_read() is an internal routine. It takes a string "s" and a count
1284 * "n", and reads from the file until "n" bytes have been read. When
1285 * finished, what has been read exists in "s".
1287 * Possible return values are:
1289 * n the number of bytes read when all goes well
1291 * 0 on end of file or read error
1295 tf_read(register char *s, register int n)
1299 for (count = n; count > 0; --count) {
1300 if (curpos >= sizeof(tfbfr)) {
1301 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1304 if (curpos == lastpos) {
1307 *s++ = tfbfr[curpos++];
1313 * afs_tf_save_cred() appends an incoming ticket to the end of the ticket
1314 * file. You must call afs_tf_init() before calling afs_tf_save_cred().
1316 * The "service", "instance", and "realm" arguments specify the
1317 * server's name; "aticket" contains the credential.
1319 * Returns 0 if all goes well, TKT_FIL_INI if afs_tf_init() wasn't
1320 * called previously, and KFAILURE for anything else that went wrong.
1324 afs_tf_save_cred(struct ktc_principal *aserver,
1325 struct ktc_token *atoken,
1326 struct ktc_principal *aclient)
1328 char realm[MAXKTCREALMLEN + 1];
1329 char junk[MAXKTCNAMELEN];
1330 struct ktc_principal principal;
1331 struct ktc_token token;
1335 int count; /* count for write */
1336 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1338 if (fd < 0) { /* fd is ticket file as set by afs_tf_init */
1342 ucstring(realm, aserver->cell, MAXKTCREALMLEN);
1343 realm[MAXKTCREALMLEN] = '\0';
1345 /* Look for a duplicate ticket */
1346 (void)lseek(fd, (off_t) 0L, 0);
1347 curpos = sizeof(tfbfr);
1349 if (afs_tf_get_pname(junk) || strcmp(junk, aclient->name)
1350 || afs_tf_get_pinst(junk) || strcmp(junk, aclient->instance))
1354 start = lseek(fd, (off_t) 0L, 1) - lastpos + curpos;
1355 status = afs_tf_get_cred(&principal, &token);
1356 } while (status == 0
1357 && (strcmp(aserver->name, principal.name) != 0
1358 || strcmp(aserver->instance, principal.instance) != 0
1359 || strcmp(aserver->cell, principal.cell) != 0));
1362 * Two tickets for the same user authenticating to the same service
1363 * should be the same length, but we check here just to make sure.
1365 if (status == 0 && token.ticketLen != atoken->ticketLen)
1367 if (status && status != EOF)
1370 /* Position over the credential we just matched (or the EOF) */
1371 lseek(fd, start, 0);
1372 curpos = lastpos = sizeof(tfbfr);
1374 /* Write the ticket and associated data */
1376 count = strlen(aserver->name) + 1;
1377 if (write(fd, aserver->name, count) != count)
1380 count = strlen(aserver->instance) + 1;
1381 if (write(fd, aserver->instance, count) != count)
1384 count = strlen(realm) + 1;
1385 if (write(fd, realm, count) != count)
1388 if (write(fd, (char *)&atoken->sessionKey, 8) != 8)
1391 lifetime = time_to_life(atoken->startTime, atoken->endTime);
1392 if (write(fd, (char *)&lifetime, sizeof(int)) != sizeof(int))
1395 kvno = atoken->kvno;
1396 if (write(fd, (char *)&kvno, sizeof(int)) != sizeof(int))
1399 if (write(fd, (char *)&(atoken->ticketLen), sizeof(int)) != sizeof(int))
1402 count = atoken->ticketLen;
1403 if (write(fd, atoken->ticket, count) != count)
1406 mit_compat = atoken->startTime;
1407 if (write(fd, (char *)&mit_compat, sizeof(mit_compat))
1408 != sizeof(mit_compat))
1411 /* Actually, we should check each write for success */
1418 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
1421 * For copying and distribution information, please see the file
1422 * <mit-copyright.h>.
1426 * This routine is used to generate the name of the file that holds
1427 * the user's cache of server tickets and associated session keys.
1429 * If it is set, krb_ticket_string contains the ticket file name.
1430 * Otherwise, the filename is constructed as follows:
1432 * If it is set, the environment variable "KRBTKFILE" will be used as
1433 * the ticket file name. Otherwise TKT_ROOT (defined in "krb.h") and
1434 * the user's uid are concatenated to produce the ticket file name
1435 * (e.g., "/tmp/tkt123"). A pointer to the string containing the ticket
1436 * file name is returned.
1439 static char krb_ticket_string[4096] = "";
1442 ktc_tkt_string(void)
1444 return ktc_tkt_string_uid(getuid());
1448 ktc_tkt_string_uid(afs_uint32 uid)
1453 if (!*krb_ticket_string) {
1454 if ((env = getenv("KRBTKFILE"))) {
1455 (void)strncpy(krb_ticket_string, env,
1456 sizeof(krb_ticket_string) - 1);
1457 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1459 /* 32 bits of signed integer will always fit in 11 characters
1460 * (including the sign), so no need to worry about overflow */
1461 (void)sprintf(krb_ticket_string, "%s%d", TKT_ROOT, uid);
1464 UNLOCK_GLOBAL_MUTEX;
1465 return krb_ticket_string;
1469 * This routine is used to set the name of the file that holds the user's
1470 * cache of server tickets and associated session keys.
1472 * The value passed in is copied into local storage.
1474 * NOTE: This routine should be called during initialization, before other
1475 * Kerberos routines are called; otherwise tkt_string() above may be called
1476 * and return an undesired ticket file name until this routine is called.
1480 ktc_set_tkt_string(char * val)
1484 (void)strncpy(krb_ticket_string, val, sizeof(krb_ticket_string) - 1);
1485 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1486 UNLOCK_GLOBAL_MUTEX;
1491 * tf_create() is used to initialize the ticket store. It creates the
1492 * file to contain the tickets and writes the given user's name "pname"
1493 * and instance "pinst" in the file. in_tkt() returns KSUCCESS on
1494 * success, or KFAILURE if something goes wrong.
1498 afs_tf_create(char *pname, char *pinst)
1503 char *file = ktc_tkt_string();
1512 if (lstat(file, &sbuf) == 0) {
1513 if ((sbuf.st_uid != me && me != 0)
1514 || ((sbuf.st_mode & S_IFMT) != S_IFREG) || sbuf.st_mode & 077) {
1517 /* file already exists, and permissions appear ok, so nuke it */
1518 if ((fd = open(file, O_RDWR, 0)) < 0)
1519 goto out; /* can't zero it, but we can still try truncating it */
1521 memset(zerobuf, 0, sizeof(zerobuf));
1523 for (i = 0; i < sbuf.st_size; i += sizeof(zerobuf))
1524 if (write(fd, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf)) {
1535 /* arrange so the file is owned by the ruid
1536 * (swap real & effective uid if necessary).
1537 * This isn't a security problem, since the ticket file, if it already
1538 * exists, has the right uid (== ruid) and mode. */
1540 if (setreuid(metoo, me) < 0) {
1544 tktfile = creat(file, 0600);
1546 if (setreuid(me, metoo) < 0) {
1547 /* can't switch??? fail! */
1554 count = strlen(pname) + 1;
1555 if (write(tktfile, pname, count) != count) {
1556 (void)close(tktfile);
1559 count = strlen(pinst) + 1;
1560 if (write(tktfile, pinst, count) != count) {
1561 (void)close(tktfile);
1564 (void)close(tktfile);
1569 * dest_tkt() is used to destroy the ticket store upon logout.
1570 * If the ticket file does not exist, dest_tkt() returns RET_TKFIL.
1571 * Otherwise the function returns 0 on success, KFAILURE on
1576 afs_tf_dest_tkt(void)
1578 char *file = ktc_tkt_string();
1584 if (lstat(file, &statb) < 0)
1587 if (!(statb.st_mode & S_IFREG))
1590 if ((fd = open(file, O_RDWR, 0)) < 0)
1593 memset(buf, 0, BUFSIZ);
1595 for (i = 0; i < statb.st_size; i += BUFSIZ)
1596 if (write(fd, buf, BUFSIZ) != BUFSIZ) {
1608 if (errno == ENOENT)
1610 else if (errno != 0)
1618 #if defined(AFS_AIX51_ENV)
1619 int code = getpagvalue("afs");
1620 if (code < 0 && errno == EINVAL)
1624 gid_t groups[NGROUPS_MAX];
1626 afs_uint32 h, l, ret;
1628 if (getgroups(sizeof groups / sizeof groups[0], groups) < 2)
1631 g0 = groups[0] & 0xffff;
1632 g1 = groups[1] & 0xffff;
1635 if (g0 < 0xc000 && g1 < 0xc000) {
1636 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
1638 h = (g1 >> 14) + h + h + h;
1639 ret = ((h << 28) | l);
1640 /* Additional testing */
1641 if (((ret >> 24) & 0xff) == 'A')
1653 extern char **environ;
1657 char fname[256], *prefix = "/ticket/";
1659 char **newenv, **senv, **denv;
1662 if (stat("/ticket", &sbuf) == -1) {
1663 prefix = "/tmp/tkt";
1666 pag = curpag() & 0xffffffff;
1668 sprintf(fname, "%s%d", prefix, getuid());
1670 sprintf(fname, "%sp%lu", prefix, afs_cast_uint32(pag));
1672 ktc_set_tkt_string(fname);
1674 for (senv = environ, numenv = 0; *senv; senv++)
1676 newenv = (char **)malloc((numenv + 2) * sizeof(char *));
1678 for (senv = environ, denv = newenv; *senv; senv++) {
1679 if (strncmp(*senv, "KRBTKFILE=", 10) != 0)
1683 *denv = (char *)malloc(10 + strlen(fname) + 1);
1684 strcpy(*denv, "KRBTKFILE=");
1685 strcat(*denv, fname);
1688 UNLOCK_GLOBAL_MUTEX;
1693 * BLETCH! We have to invoke the entire afsconf package just to
1694 * find out what the local cell is.
1700 struct afsconf_dir *conf;
1702 if ((conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))
1703 || (conf = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH))) {
1704 code = afsconf_GetLocalCell(conf, lcell, sizeof(lcell));
1705 afsconf_Close(conf);
1707 if (!conf || code) {
1708 printf("** Can't determine local cell name!\n");