2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* ticket caching code */
12 #include <afsconfig.h>
14 #include "afs/param.h"
16 #include <afs/param.h>
23 #include "afs/sysincludes.h"
24 #include "afsincludes.h"
26 #include "afs/pthread_glock.h"
29 #include "afs/venus.h"
30 #include "afs/pthread_glock.h"
31 #include "afs/dirpath.h"
34 #define min(a,b) ((a)<(b)?(a):(b))
35 #endif /* !defined(min) */
37 #else /* defined(UKERNEL) */
44 #include <afs/pthread_glock.h>
45 #include <sys/types.h>
50 #include <sys/ioctl.h>
51 #include <netinet/in.h>
55 #include <sys/lockf.h>
61 #include <afs/venus.h>
62 #include <afs/afsutil.h>
64 #endif /* defined(UKERNEL) */
71 /* AFS_KERBEROS_ENV is now conditionally defined in the Makefile */
72 #define AFS_KERBEROS_ENV
75 #ifdef AFS_KERBEROS_ENV
78 #include "cellconfig.h"
79 static char lcell[MAXCELLCHARS];
81 #define TKT_ROOT "/tmp/tkt"
86 /* Definitions for ticket file utilities */
90 /* Error codes returned by ticket file utilities */
91 #define NO_TKT_FIL 76 /* No ticket file found */
92 #define TKT_FIL_ACC 77 /* Couldn't access tkt file */
93 #define TKT_FIL_LCK 78 /* Couldn't lock ticket file */
94 #define TKT_FIL_FMT 79 /* Bad ticket file format */
95 #define TKT_FIL_INI 80 /* afs_tf_init not called first */
97 /* Values returned by get_credentials */
98 #define RET_TKFIL 21 /* Can't read ticket file */
107 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
108 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0, 0 };
109 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0, 0 };
110 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0, 0 };
113 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0 };
114 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0 };
115 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0 };
122 /* the following routines aren't static anymore on behalf of the kerberos IV
123 * compatibility library built in subtree krb.
125 int afs_tf_init(), afs_tf_get_pname(), afs_tf_get_pinst(), afs_tf_get_cred();
126 int afs_tf_save_cred(), afs_tf_close(), afs_tf_create();
127 int afs_tf_dest_tkt();
128 static void ktc_LocalCell();
129 #endif /* AFS_KERBEROS_ENV */
132 #define PIOCTL afs_pioctl
133 #elif defined(UKERNEL)
134 #define PIOCTL(A,B,C,D) call_syscall(AFSCALL_PIOCTL,A,B,C,D)
136 #define PIOCTL pioctl
140 #ifdef KERNEL_KTC_COMPAT
143 #define KTC_SYSCALL 32
146 /* Kernel call opcode definitions */
147 #define KTC_OPCODE_BASE 4300
148 #define KTC_NO_OP (0+KTC_OPCODE_BASE)
149 #define KTC_SETTOKEN_OP (1+KTC_OPCODE_BASE)
150 #define KTC_GETTOKEN_OP (2+KTC_OPCODE_BASE)
151 #define KTC_LISTTOKENS_OP (3+KTC_OPCODE_BASE)
152 #define KTC_FORGETTOKEN_OP (4+KTC_OPCODE_BASE)
153 #define KTC_FORGETALLTOKENS_OP (5+KTC_OPCODE_BASE)
154 #define KTC_STATUS_OP (6+KTC_OPCODE_BASE)
155 #define KTC_GC_OP (7+KTC_OPCODE_BASE)
157 #define KTC_INTERFACE_VERSION 3
159 /* We have to determine if the kernel supports the ktc system call. To do so
160 * we attempt to execute its noop function. If this is successful we use the
161 * kernel calls in the future otherwise we let the old code run. */
163 /* To safely check to see whether a system call exists we have to intercept the
164 * SIGSYS signal which is caused by executing a non-existant system call. If
165 * it is ignored the syscall routine returns EINVAL. The SIGSYS is reset to
166 * its old value after the return from syscall. */
168 static int kernelKTC = 0;
170 #ifdef AFS_DECOSF_ENV
172 * SIGSYS semantics are broken on Dec AXP OSF/1 v1.2 systems. You need
173 * to ignore SIGTRAP too. It is claimed to be fixed under v1.3, but...
176 #define CHECK_KERNEL \
177 if (kernelKTC == 0) { \
181 old = (int (*)())signal(SIGSYS, SIG_IGN); \
182 old_t = (int (*)())signal(SIGTRAP, SIG_IGN); \
183 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
185 signal(SIGSYS, old); \
186 signal(SIGTRAP, old_t); \
187 if (code == 0) kernelKTC = 1; \
188 else kernelKTC = 2; \
189 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
192 #else /* AFS_DECOSF_ENV */
194 #define CHECK_KERNEL \
195 if (kernelKTC == 0) { \
198 old = (int (*)())signal(SIGSYS, SIG_IGN); \
199 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
201 signal(SIGSYS, old); \
202 if (code == 0) kernelKTC = 1; \
203 else kernelKTC = 2; \
204 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
206 #endif /* AFS_DECOSF_ENV */
208 #define TRY_KERNEL(cmd,a1,a2,a3,a4) \
210 if (kernelKTC == 1) \
211 return syscall (KTC_SYSCALL, cmd, \
212 KTC_INTERFACE_VERSION, a1,a2,a3,a4); \
216 #define TRY_KERNEL(cmd,a1,a2,a3,a4)
217 #endif /* KERNEL_KTC_COMPAT */
219 #if !defined(UKERNEL)
220 /* this is a structure used to communicate with the afs cache mgr, but is
221 * otherwise irrelevant */
223 afs_int32 AuthHandle;
224 char HandShakeKey[8];
226 afs_int32 BeginTimestamp;
227 afs_int32 EndTimestamp;
229 #endif /* !defined(UKERNEL) */
231 #define MAXLOCALTOKENS 4
235 struct ktc_principal server;
236 struct ktc_principal client;
237 struct ktc_token token;
238 } local_tokens[MAXLOCALTOKENS] = { {
244 /* new interface routines to the ticket cache. Only handle afs service right
248 NewSetToken(aserver, atoken, aclient, flags)
249 struct ktc_principal *aserver;
250 struct ktc_principal *aclient;
251 struct ktc_token *atoken;
254 TRY_KERNEL(KTC_SETTOKEN_OP, aserver, aclient, atoken,
255 sizeof(struct ktc_token));
256 /* no kernel ticket cache */
260 #define MAXPIOCTLTOKENLEN \
261 (3*sizeof(afs_int32)+MAXKTCTICKETLEN+sizeof(struct ClearToken)+MAXKTCREALMLEN)
264 OldSetToken(aserver, atoken, aclient, flags)
265 struct ktc_principal *aserver, *aclient;
266 struct ktc_token *atoken;
269 struct ViceIoctl iob;
270 char tbuffer[MAXPIOCTLTOKENLEN];
272 struct ClearToken ct;
273 register afs_int32 code;
276 if (strcmp(aserver->name, "afs") != 0) {
279 for (i = 0; i < MAXLOCALTOKENS; i++)
280 if (local_tokens[i].valid) {
281 if ((strcmp(local_tokens[i].server.name, aserver->name) == 0)
284 (local_tokens[i].server.instance,
285 aserver->instance) == 0)
286 && (strcmp(local_tokens[i].server.cell, aserver->cell) ==
288 found = i; /* replace existing entry */
290 } else /* valid, but no match */
293 found = i; /* remember this empty slot */
296 memcpy(&local_tokens[found].token, atoken, sizeof(struct ktc_token));
297 local_tokens[found].server = *aserver;
298 local_tokens[found].client = *aclient;
299 local_tokens[found].valid = 1;
302 tp = tbuffer; /* start copying here */
303 if ((atoken->ticketLen < MINKTCTICKETLEN)
304 || (atoken->ticketLen > MAXKTCTICKETLEN))
306 memcpy(tp, &atoken->ticketLen, sizeof(afs_int32)); /* copy in ticket length */
307 tp += sizeof(afs_int32);
308 memcpy(tp, atoken->ticket, atoken->ticketLen); /* copy in ticket */
309 tp += atoken->ticketLen;
310 /* next, copy in the "clear token", describing who we are */
311 ct.AuthHandle = atoken->kvno; /* hide auth handle here */
312 memcpy(ct.HandShakeKey, &atoken->sessionKey, 8);
314 ct.BeginTimestamp = atoken->startTime;
315 ct.EndTimestamp = atoken->endTime;
316 if (ct.BeginTimestamp == 0)
317 ct.BeginTimestamp = 1;
319 if ((strlen(aclient->name) > strlen("AFS ID "))
320 && (aclient->instance[0] == 0)) {
322 afs_int32 viceId = 0;
323 char *cp = aclient->name + strlen("AFS ID ");
330 viceId = viceId * 10 + (int)(*cp - '0');
335 ct.ViceId = viceId * sign; /* OK to let any value here? */
336 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 0)
337 ct.BeginTimestamp++; /* force lifetime to be odd */
340 ct.ViceId = getuid(); /* wrong, but works in primary cell */
341 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1)
342 ct.BeginTimestamp++; /* force lifetime to be even */
347 * Information needed by the user space cache manager
349 u.u_expiration = ct.EndTimestamp;
350 u.u_viceid = ct.ViceId;
353 temp = sizeof(struct ClearToken);
354 memcpy(tp, &temp, sizeof(afs_int32));
355 tp += sizeof(afs_int32);
356 memcpy(tp, &ct, sizeof(struct ClearToken));
357 tp += sizeof(struct ClearToken);
359 /* next copy in primary flag */
363 * The following means that setpag will happen inside afs just before
364 * the authentication to prevent the setpag/klog race condition.
366 * The following means that setpag will affect the parent process as
367 * well as the current process.
369 if (flags & AFS_SETTOK_SETPAG)
372 memcpy(tp, &temp, sizeof(afs_int32));
373 tp += sizeof(afs_int32);
375 /* finally copy in the cell name */
376 temp = strlen(aserver->cell);
377 if (temp >= MAXKTCREALMLEN)
379 strcpy(tp, aserver->cell);
382 /* now setup for the pioctl */
384 iob.in_size = tp - tbuffer;
386 iob.out_size = sizeof(tbuffer);
388 #if defined(NO_AFS_CLIENT)
392 if ((tkfile = getenv("TKTFILE"))
395 open(tkfile, O_WRONLY | O_APPEND | O_TRUNC | O_CREAT,
397 printf("Writing ticket to: %s\n", tkfile);
398 code = (write(fd, iob.in, iob.in_size) != iob.in_size);
401 code = KTC_PIOCTLFAIL;
403 #else /* NO_AFS_CLIENT */
404 code = PIOCTL(0, VIOCSETTOK, &iob, 0);
405 #endif /* NO_AFS_CLIENT */
407 return KTC_PIOCTLFAIL;
412 ktc_SetToken(struct ktc_principal *aserver,
413 struct ktc_token *atoken,
414 struct ktc_principal *aclient,
420 #ifdef AFS_KERBEROS_ENV
424 if ( /*!strcmp(aclient->cell, lcell) && this would only store local creds */
425 (strcmp(aserver->name, "AuthServer")
426 || strcmp(aserver->instance, "Admin"))) {
427 if (strcmp(aserver->name, "krbtgt") == 0) {
428 static char lrealm[MAXKTCREALMLEN];
431 ucstring(lrealm, lcell, MAXKTCREALMLEN);
432 if (strcmp(aserver->instance, lrealm) == 0) {
433 afs_tf_create(aclient->name, aclient->instance);
437 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
438 if (ncode == NO_TKT_FIL) {
439 (void)afs_tf_create(aclient->name, aclient->instance);
440 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
444 afs_tf_save_cred(aserver, atoken, aclient);
450 #endif /* NO_AFS_CLIENT */
454 #ifndef NO_AFS_CLIENT
455 ncode = NewSetToken(aserver, atoken, aclient, flags);
456 if (ncode || /* new style failed */
457 (strcmp(aserver->name, "afs") == 0)) { /* for afs tokens do both */
458 ocode = OldSetToken(aserver, atoken, aclient, flags);
461 if (ncode && ocode) {
465 else if (ocode == KTC_PIOCTLFAIL)
473 return KTC_PIOCTLFAIL;
475 #endif /* NO_AFS_CLIENT */
480 /* get token, given server we need and token buffer. aclient will eventually
481 * be set to our identity to the server.
483 ktc_GetToken(aserver, atoken, atokenLen, aclient)
484 struct ktc_principal *aserver, *aclient;
486 struct ktc_token *atoken;
488 struct ViceIoctl iob;
489 char tbuffer[MAXPIOCTLTOKENLEN];
490 register afs_int32 code;
492 char *stp, *cellp; /* secret token ptr */
493 struct ClearToken ct;
496 int maxLen; /* biggest ticket we can copy */
497 int tktLen; /* server ticket length */
501 #ifndef NO_AFS_CLIENT
502 TRY_KERNEL(KTC_GETTOKEN_OP, aserver, aclient, atoken, atokenLen);
503 #endif /* NO_AFS_CLIENT */
505 #ifdef AFS_KERBEROS_ENV
509 #ifndef NO_AFS_CLIENT
510 if (strcmp(aserver->name, "afs") != 0)
511 #endif /* NO_AFS_CLIENT */
514 /* try the local tokens */
515 for (i = 0; i < MAXLOCALTOKENS; i++)
516 if (local_tokens[i].valid
517 && (strcmp(local_tokens[i].server.name, aserver->name) == 0)
518 && (strcmp(local_tokens[i].server.instance, aserver->instance)
520 && (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
521 memcpy(atoken, &local_tokens[i].token,
522 min(atokenLen, sizeof(struct ktc_token)));
524 *aclient = local_tokens[i].client;
528 #ifdef AFS_KERBEROS_ENV
529 if (!afs_tf_init(ktc_tkt_string(), R_TKT_FIL)) {
531 if (!afs_tf_get_pname(aclient->name)
532 && !afs_tf_get_pinst(aclient->instance))
535 char tmpstring[MAXHOSTCHARS];
536 afs_tf_get_pname(&tmpstring);
537 afs_tf_get_pinst(&tmpstring);
542 struct ktc_principal cprincipal;
543 struct ktc_token ctoken;
545 while (!afs_tf_get_cred(&cprincipal, &ctoken)) {
546 if (strcmp(cprincipal.name, aserver->name) == 0
547 && strcmp(cprincipal.instance, aserver->instance) == 0
548 && strcmp(cprincipal.cell, aserver->cell) == 0) {
551 strcpy(aclient->cell, lcell);
552 memcpy(atoken, &ctoken,
553 min(atokenLen, sizeof(struct ktc_token)));
566 #ifndef NO_AFS_CLIENT
567 for (index = 0; index < 200; index++) { /* sanity check in case pioctl fails */
568 iob.in = (char *)&index;
569 iob.in_size = sizeof(afs_int32);
571 iob.out_size = sizeof(tbuffer);
573 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
576 /* failed to retrieve specified token */
577 if (code < 0 && errno == EDOM) {
582 /* token retrieved; parse buffer */
585 /* get ticket length */
586 memcpy(&temp, tp, sizeof(afs_int32));
588 tp += sizeof(afs_int32);
590 /* remember where ticket is and skip over it */
594 /* get size of clear token and verify */
595 memcpy(&temp, tp, sizeof(afs_int32));
596 if (temp != sizeof(struct ClearToken)) {
600 tp += sizeof(afs_int32);
602 /* copy clear token */
603 memcpy(&ct, tp, temp);
606 /* skip over primary flag */
607 tp += sizeof(afs_int32);
609 /* remember where cell name is */
612 if ((strcmp(cellp, aserver->cell) == 0)
613 #ifdef AFS_KERBEROS_ENV
614 || (*aserver->cell == '\0' && strcmp(cellp, lcell) == 0)
617 /* got token for cell; check that it will fit */
619 atokenLen - sizeof(struct ktc_token) + MAXKTCTICKETLEN;
620 if (maxLen < tktLen) {
625 /* set return values */
626 memcpy(atoken->ticket, stp, tktLen);
627 atoken->startTime = ct.BeginTimestamp;
628 atoken->endTime = ct.EndTimestamp;
629 if (ct.AuthHandle == -1) {
632 atoken->kvno = ct.AuthHandle;
633 memcpy(&atoken->sessionKey, ct.HandShakeKey,
634 sizeof(struct ktc_encryptionKey));
635 atoken->ticketLen = tktLen;
638 strcpy(aclient->cell, cellp);
639 aclient->instance[0] = 0;
641 if ((atoken->kvno == 999) || /* old style bcrypt ticket */
642 (ct.BeginTimestamp && /* new w/ prserver lookup */
643 (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1))) {
644 sprintf(aclient->name, "AFS ID %d", ct.ViceId);
646 sprintf(aclient->name, "Unix UID %d", ct.ViceId);
654 #endif /* NO_AFS_CLIENT */
657 if ((code < 0) && (errno == EINVAL))
659 return KTC_PIOCTLFAIL; /* probable cause */
663 * Forget tokens for this server and the calling user.
664 * NOT IMPLEMENTED YET!
666 #ifndef NO_AFS_CLIENT
667 ktc_ForgetToken(aserver)
668 struct ktc_principal *aserver;
673 TRY_KERNEL(KTC_FORGETTOKEN_OP, aserver, 0, 0, 0);
675 rc = ktc_ForgetAllTokens(); /* bogus, but better */
679 #endif /* NO_AFS_CLIENT */
681 /* ktc_ListTokens - list all tokens. start aprevIndex at 0, it returns the
682 * next rock in (*aindex). (*aserver) is set to the relevant ticket on
686 ktc_ListTokens(int aprevIndex,
688 struct ktc_principal *aserver)
690 struct ViceIoctl iob;
691 char tbuffer[MAXPIOCTLTOKENLEN];
692 register afs_int32 code;
694 afs_int32 temp, index;
696 memset(tbuffer, 0, sizeof(tbuffer));
699 #ifndef NO_AFS_CLIENT
700 TRY_KERNEL(KTC_LISTTOKENS_OP, aserver, aprevIndex, aindex, 0);
701 #endif /* NO_AFS_CLIENT */
707 #endif /* NO_AFS_CLIENT */
708 #ifdef AFS_KERBEROS_ENV
711 struct ktc_principal cprincipal;
712 struct ktc_token ctoken;
714 if (afs_tf_init(ktc_tkt_string(), R_TKT_FIL)
715 || afs_tf_get_pname(tbuffer) || afs_tf_get_pinst(tbuffer)) {
721 for (i = 214; i < index; i++) {
722 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
730 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
737 #ifndef NO_AFS_CLIENT
738 if (!strcmp(cprincipal.name, "afs") && cprincipal.instance[0] == 0) {
741 #endif /* NO_AFS_CLIENT */
743 for (i = 0; i < MAXLOCALTOKENS; i++) {
744 if (!strcmp(cprincipal.name, local_tokens[i].server.name)
745 && !strcmp(cprincipal.instance,
746 local_tokens[i].server.instance)
747 && !strcmp(cprincipal.cell, local_tokens[i].server.cell)) {
752 *aserver = cprincipal;
760 #ifndef NO_AFS_CLIENT
761 if (index >= 123) { /* special hack for returning TCS */
762 while (index - 123 < MAXLOCALTOKENS) {
763 if (local_tokens[index - 123].valid) {
764 *aserver = local_tokens[index - 123].server;
772 #ifdef AFS_KERBEROS_ENV
773 return ktc_ListTokens(214, aindex, aserver);
779 /* get tokens from the kernel */
780 while (index < 200) { /* sanity check in case pioctl fails */
781 iob.in = (char *)&index;
782 iob.in_size = sizeof(afs_int32);
784 iob.out_size = sizeof(tbuffer);
785 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
786 if (code < 0 && errno == EDOM) {
789 rc = ktc_ListTokens(123, aindex, aserver);
798 break; /* got a ticket */
799 /* otherwise we should skip this ticket slot */
806 return KTC_PIOCTLFAIL;
812 /* next iterator determined by earlier loop */
815 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of secret token */
816 tp += sizeof(afs_int32);
817 tp += temp; /* skip ticket for now */
818 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of clear token */
819 if (temp != sizeof(struct ClearToken)) {
823 tp += sizeof(afs_int32); /* skip length */
824 tp += temp; /* skip clear token itself */
825 tp += sizeof(afs_int32); /* skip primary flag */
826 /* tp now points to the cell name */
827 strcpy(aserver->cell, tp);
828 aserver->instance[0] = 0;
829 strcpy(aserver->name, "afs");
830 #endif /* NO_AFS_CLIENT */
835 /* discard all tokens from this user's cache */
840 #ifndef NO_AFS_CLIENT
841 TRY_KERNEL(KTC_FORGETALLTOKENS_OP, 0, 0, 0, 0);
842 #endif /* NO_AFS_CLIENT */
849 struct ViceIoctl iob;
850 register afs_int32 code;
853 for (i = 0; i < MAXLOCALTOKENS; i++)
854 local_tokens[i].valid = 0;
860 #ifndef NO_AFS_CLIENT
861 code = PIOCTL(0, VIOCUNPAG, &iob, 0);
863 return KTC_PIOCTLFAIL;
864 #endif /* NO_AFS_CLIENT */
869 ktc_ForgetAllTokens(void)
874 #ifdef AFS_KERBEROS_ENV
875 (void)afs_tf_dest_tkt();
878 ncode = NewForgetAll();
879 ocode = OldForgetAll();
880 if (ncode && ocode) {
883 else if (ocode == KTC_PIOCTLFAIL)
888 return KTC_PIOCTLFAIL;
894 /* ktc_OldPioctl - returns a boolean true if the kernel supports only the old
895 * pioctl interface for delivering AFS tickets to the cache manager. */
901 #ifdef KERNEL_KTC_COMPAT
903 rc = (kernelKTC != 1); /* old style interface */
912 #ifdef AFS_KERBEROS_ENV
914 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
916 * For copying and distribution information, please see the file
923 #include <sys/types.h>
924 #include <sys/stat.h>
925 #include <sys/file.h>
930 #define TF_LCK_RETRY ((unsigned)2) /* seconds to sleep before
931 * retry if ticket file is
935 * fd must be initialized to something that won't ever occur as a real
936 * file descriptor. Since open(2) returns only non-negative numbers as
937 * valid file descriptors, and afs_tf_init always stuffs the return value
938 * from open in here even if it is an error flag, we must
939 * a. Initialize fd to a negative number, to indicate that it is
940 * not initially valid.
941 * b. When checking for a valid fd, assume that negative values
942 * are invalid (ie. when deciding whether afs_tf_init has been
944 * c. In tf_close, be sure it gets reinitialized to a negative
948 static curpos; /* Position in tfbfr */
949 static lastpos; /* End of tfbfr */
950 static char tfbfr[BUFSIZ]; /* Buffer for ticket data */
952 static tf_gets(), tf_read();
955 * This file contains routines for manipulating the ticket cache file.
957 * The ticket file is in the following format:
959 * principal's name (null-terminated string)
960 * principal's instance (null-terminated string)
967 * Where "CREDENTIAL_x" consists of the following fixed-length
968 * fields from the CREDENTIALS structure (see "krb.h"):
970 * char service[MAXKTCNAMELEN]
971 * char instance[MAXKTCNAMELEN]
972 * char realm[REALM_SZ]
977 * afs_int32 issue_date
979 * Short description of routines:
981 * afs_tf_init() opens the ticket file and locks it.
983 * afs_tf_get_pname() returns the principal's name.
985 * afs_tf_get_pinst() returns the principal's instance (may be null).
987 * afs_tf_get_cred() returns the next CREDENTIALS record.
989 * afs_tf_save_cred() appends a new CREDENTIAL record to the ticket file.
991 * afs_tf_close() closes the ticket file and releases the lock.
993 * tf_gets() returns the next null-terminated string. It's an internal
994 * routine used by afs_tf_get_pname(), afs_tf_get_pinst(), and
997 * tf_read() reads a given number of bytes. It's an internal routine
998 * used by afs_tf_get_cred().
1002 * afs_tf_init() should be called before the other ticket file routines.
1003 * It takes the name of the ticket file to use, "tf_name", and a
1004 * read/write flag "rw" as arguments.
1006 * It tries to open the ticket file, checks the mode, and if everything
1007 * is okay, locks the file. If it's opened for reading, the lock is
1008 * shared. If it's opened for writing, the lock is exclusive.
1010 * Returns 0 if all went well, otherwise one of the following:
1012 * NO_TKT_FIL - file wasn't there
1013 * TKT_FIL_ACC - file was in wrong mode, etc.
1014 * TKT_FIL_LCK - couldn't lock the file, even after a retry
1017 afs_tf_init(tf_name, rw)
1022 struct stat stat_buf;
1034 if (lstat(tf_name, &stat_buf) < 0)
1042 if ((stat_buf.st_uid != me && me != 0)
1043 || ((stat_buf.st_mode & S_IFMT) != S_IFREG))
1047 * If "wflag" is set, open the ticket file in append-writeonly mode
1048 * and lock the ticket file in exclusive mode. If unable to lock
1049 * the file, sleep and try again. If we fail again, return with the
1050 * proper error message.
1053 curpos = sizeof(tfbfr);
1056 fd = open(tf_name, O_RDWR, 0600);
1060 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1061 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1062 sleep(TF_LCK_RETRY);
1063 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1065 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1066 sleep(TF_LCK_RETRY);
1067 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1077 * Otherwise "wflag" is not set and the ticket file should be opened
1078 * for read-only operations and locked for shared access.
1081 fd = open(tf_name, O_RDONLY, 0600);
1085 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1086 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1087 sleep(TF_LCK_RETRY);
1088 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1090 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1091 sleep(TF_LCK_RETRY);
1092 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1103 * afs_tf_get_pname() reads the principal's name from the ticket file. It
1104 * should only be called after afs_tf_init() has been called. The
1105 * principal's name is filled into the "p" parameter. If all goes well,
1106 * 0 is returned. If afs_tf_init() wasn't called, TKT_FIL_INI is
1107 * returned. If the name was null, or EOF was encountered, or the name
1108 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned.
1117 if (tf_gets(p, MAXKTCNAMELEN) < 2) /* can't be just a null */
1123 * afs_tf_get_pinst() reads the principal's instance from a ticket file.
1124 * It should only be called after afs_tf_init() and afs_tf_get_pname() have
1125 * been called. The instance is filled into the "inst" parameter. If all
1126 * goes well, 0 is returned. If afs_tf_init() wasn't called,
1127 * TKT_FIL_INI is returned. If EOF was encountered, or the instance
1128 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned. Note that the
1129 * instance may be null.
1132 afs_tf_get_pinst(inst)
1138 if (tf_gets(inst, MAXKTCNAMELEN) < 1)
1144 * afs_tf_get_cred() reads a CREDENTIALS record from a ticket file and fills
1145 * in the given structure "c". It should only be called after afs_tf_init(),
1146 * afs_tf_get_pname(), and afs_tf_get_pinst() have been called. If all goes
1147 * well, 0 is returned. Possible error codes are:
1149 * TKT_FIL_INI - afs_tf_init wasn't called first
1150 * TKT_FIL_FMT - bad format
1151 * EOF - end of file encountered
1154 afs_tf_get_cred(principal, token)
1155 struct ktc_principal *principal;
1156 struct ktc_token *token;
1160 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1165 if ((k_errno = tf_gets(principal->name, MAXKTCNAMELEN)) < 2)
1168 case 1: /* can't be just a null */
1173 if ((k_errno = tf_gets(principal->instance, MAXKTCNAMELEN)) < 1)
1180 if ((k_errno = tf_gets(principal->cell, MAXKTCREALMLEN)) < 2)
1183 case 1: /* can't be just a null */
1188 lcstring(principal->cell, principal->cell, MAXKTCREALMLEN);
1189 if (tf_read((char *)&(token->sessionKey), 8) < 1
1190 || tf_read((char *)&(lifetime), sizeof(lifetime)) < 1
1191 || tf_read((char *)&(kvno), sizeof(kvno)) < 1
1192 || tf_read((char *)&(token->ticketLen), sizeof(token->ticketLen))
1194 /* don't try to read a silly amount into ticket->dat */
1195 token->ticketLen > MAXKTCTICKETLEN
1196 || tf_read((char *)(token->ticket), token->ticketLen) < 1
1197 || tf_read((char *)&mit_compat, sizeof(mit_compat)) < 1) {
1200 token->startTime = mit_compat;
1201 token->endTime = life_to_time(token->startTime, lifetime);
1207 * tf_close() closes the ticket file and sets "fd" to -1. If "fd" is
1208 * not a valid file descriptor, it just returns. It also clears the
1209 * buffer used to read tickets.
1211 * The return value is not defined.
1217 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1218 (void)fcntl(fd, F_SETLK, &fileUlock);
1220 (void)flock(fd, LOCK_UN);
1223 fd = -1; /* see declaration of fd above */
1225 memset(tfbfr, 0, sizeof(tfbfr));
1229 * tf_gets() is an internal routine. It takes a string "s" and a count
1230 * "n", and reads from the file until either it has read "n" characters,
1231 * or until it reads a null byte. When finished, what has been read exists
1234 * Possible return values are:
1236 * n the number of bytes read (including null terminator)
1237 * when all goes well
1239 * 0 end of file or read error
1241 * TOO_BIG if "count" characters are read and no null is
1242 * encountered. This is an indication that the ticket
1243 * file is seriously ill.
1255 for (count = n - 1; count > 0; --count) {
1256 if (curpos >= sizeof(tfbfr)) {
1257 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1260 if (curpos == lastpos) {
1263 *s = tfbfr[curpos++];
1271 * tf_read() is an internal routine. It takes a string "s" and a count
1272 * "n", and reads from the file until "n" bytes have been read. When
1273 * finished, what has been read exists in "s".
1275 * Possible return values are:
1277 * n the number of bytes read when all goes well
1279 * 0 on end of file or read error
1289 for (count = n; count > 0; --count) {
1290 if (curpos >= sizeof(tfbfr)) {
1291 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1294 if (curpos == lastpos) {
1297 *s++ = tfbfr[curpos++];
1305 * afs_tf_save_cred() appends an incoming ticket to the end of the ticket
1306 * file. You must call afs_tf_init() before calling afs_tf_save_cred().
1308 * The "service", "instance", and "realm" arguments specify the
1309 * server's name; "aticket" contains the credential.
1311 * Returns 0 if all goes well, TKT_FIL_INI if afs_tf_init() wasn't
1312 * called previously, and KFAILURE for anything else that went wrong.
1315 afs_tf_save_cred(aserver, atoken, aclient)
1316 struct ktc_principal *aserver;
1317 struct ktc_principal *aclient;
1318 struct ktc_token *atoken; /* Token */
1320 char realm[MAXKTCREALMLEN + 1];
1321 char junk[MAXKTCNAMELEN];
1322 struct ktc_principal principal;
1323 struct ktc_token token;
1327 int count; /* count for write */
1328 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1330 if (fd < 0) { /* fd is ticket file as set by afs_tf_init */
1334 ucstring(realm, aserver->cell, MAXKTCREALMLEN);
1335 realm[MAXKTCREALMLEN] = '\0';
1337 /* Look for a duplicate ticket */
1338 (void)lseek(fd, (off_t) 0L, 0);
1339 curpos = sizeof(tfbfr);
1341 if (afs_tf_get_pname(junk) || strcmp(junk, aclient->name)
1342 || afs_tf_get_pinst(junk) || strcmp(junk, aclient->instance))
1346 start = lseek(fd, (off_t) 0L, 1) - lastpos + curpos;
1347 status = afs_tf_get_cred(&principal, &token);
1348 } while (status == 0
1349 && (strcmp(aserver->name, principal.name) != 0
1350 || strcmp(aserver->instance, principal.instance) != 0
1351 || strcmp(aserver->cell, principal.cell) != 0));
1354 * Two tickets for the same user authenticating to the same service
1355 * should be the same length, but we check here just to make sure.
1357 if (status == 0 && token.ticketLen != atoken->ticketLen)
1359 if (status && status != EOF)
1362 /* Position over the credential we just matched (or the EOF) */
1363 lseek(fd, start, 0);
1364 curpos = lastpos = sizeof(tfbfr);
1366 /* Write the ticket and associated data */
1368 count = strlen(aserver->name) + 1;
1369 if (write(fd, aserver->name, count) != count)
1372 count = strlen(aserver->instance) + 1;
1373 if (write(fd, aserver->instance, count) != count)
1376 count = strlen(realm) + 1;
1377 if (write(fd, realm, count) != count)
1380 if (write(fd, (char *)&atoken->sessionKey, 8) != 8)
1383 lifetime = time_to_life(atoken->startTime, atoken->endTime);
1384 if (write(fd, (char *)&lifetime, sizeof(int)) != sizeof(int))
1387 kvno = atoken->kvno;
1388 if (write(fd, (char *)&kvno, sizeof(int)) != sizeof(int))
1391 if (write(fd, (char *)&(atoken->ticketLen), sizeof(int)) != sizeof(int))
1394 count = atoken->ticketLen;
1395 if (write(fd, atoken->ticket, count) != count)
1398 mit_compat = atoken->startTime;
1399 if (write(fd, (char *)&mit_compat, sizeof(mit_compat))
1400 != sizeof(mit_compat))
1403 /* Actually, we should check each write for success */
1410 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
1413 * For copying and distribution information, please see the file
1414 * <mit-copyright.h>.
1420 * This routine is used to generate the name of the file that holds
1421 * the user's cache of server tickets and associated session keys.
1423 * If it is set, krb_ticket_string contains the ticket file name.
1424 * Otherwise, the filename is constructed as follows:
1426 * If it is set, the environment variable "KRBTKFILE" will be used as
1427 * the ticket file name. Otherwise TKT_ROOT (defined in "krb.h") and
1428 * the user's uid are concatenated to produce the ticket file name
1429 * (e.g., "/tmp/tkt123"). A pointer to the string containing the ticket
1430 * file name is returned.
1433 static char krb_ticket_string[4096] = "";
1436 ktc_tkt_string(void)
1438 return ktc_tkt_string_uid(getuid());
1442 ktc_tkt_string_uid(afs_uint32 uid)
1447 if (!*krb_ticket_string) {
1448 if (env = getenv("KRBTKFILE")) {
1449 (void)strncpy(krb_ticket_string, env,
1450 sizeof(krb_ticket_string) - 1);
1451 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1453 /* 32 bits of signed integer will always fit in 11 characters
1454 * (including the sign), so no need to worry about overflow */
1455 (void)sprintf(krb_ticket_string, "%s%d", TKT_ROOT, uid);
1458 UNLOCK_GLOBAL_MUTEX;
1459 return krb_ticket_string;
1463 * This routine is used to set the name of the file that holds the user's
1464 * cache of server tickets and associated session keys.
1466 * The value passed in is copied into local storage.
1468 * NOTE: This routine should be called during initialization, before other
1469 * Kerberos routines are called; otherwise tkt_string() above may be called
1470 * and return an undesired ticket file name until this routine is called.
1474 ktc_set_tkt_string(char * val)
1478 (void)strncpy(krb_ticket_string, val, sizeof(krb_ticket_string) - 1);
1479 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1480 UNLOCK_GLOBAL_MUTEX;
1485 * tf_create() is used to initialize the ticket store. It creates the
1486 * file to contain the tickets and writes the given user's name "pname"
1487 * and instance "pinst" in the file. in_tkt() returns KSUCCESS on
1488 * success, or KFAILURE if something goes wrong.
1491 afs_tf_create(pname, pinst)
1498 char *file = ktc_tkt_string();
1507 if (lstat(file, &sbuf) == 0) {
1508 if ((sbuf.st_uid != me && me != 0)
1509 || ((sbuf.st_mode & S_IFMT) != S_IFREG) || sbuf.st_mode & 077) {
1512 /* file already exists, and permissions appear ok, so nuke it */
1513 if ((fd = open(file, O_RDWR, 0)) < 0)
1514 goto out; /* can't zero it, but we can still try truncating it */
1516 memset(zerobuf, 0, sizeof(zerobuf));
1518 for (i = 0; i < sbuf.st_size; i += sizeof(zerobuf))
1519 if (write(fd, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf)) {
1530 /* arrange so the file is owned by the ruid
1531 * (swap real & effective uid if necessary).
1532 * This isn't a security problem, since the ticket file, if it already
1533 * exists, has the right uid (== ruid) and mode. */
1535 if (setreuid(metoo, me) < 0) {
1539 tktfile = creat(file, 0600);
1541 if (setreuid(me, metoo) < 0) {
1542 /* can't switch??? fail! */
1549 count = strlen(pname) + 1;
1550 if (write(tktfile, pname, count) != count) {
1551 (void)close(tktfile);
1554 count = strlen(pinst) + 1;
1555 if (write(tktfile, pinst, count) != count) {
1556 (void)close(tktfile);
1559 (void)close(tktfile);
1564 * dest_tkt() is used to destroy the ticket store upon logout.
1565 * If the ticket file does not exist, dest_tkt() returns RET_TKFIL.
1566 * Otherwise the function returns 0 on success, KFAILURE on
1572 char *file = ktc_tkt_string();
1578 if (lstat(file, &statb) < 0)
1581 if (!(statb.st_mode & S_IFREG))
1584 if ((fd = open(file, O_RDWR, 0)) < 0)
1587 memset(buf, 0, BUFSIZ);
1589 for (i = 0; i < statb.st_size; i += BUFSIZ)
1590 if (write(fd, buf, BUFSIZ) != BUFSIZ) {
1602 if (errno == ENOENT)
1604 else if (errno != 0)
1612 #if defined(AFS_AIX51_ENV)
1615 if (kcred_getpag(cred, PAG_AFS, &pag) < 0 || pag == 0)
1619 gid_t groups[NGROUPS_MAX];
1621 afs_uint32 h, l, ret;
1623 if (getgroups(sizeof groups / sizeof groups[0], groups) < 2)
1626 g0 = groups[0] & 0xffff;
1627 g1 = groups[1] & 0xffff;
1630 if (g0 < 0xc000 && g1 < 0xc000) {
1631 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
1633 h = (g1 >> 14) + h + h + h;
1634 ret = ((h << 28) | l);
1635 /* Additional testing */
1636 if (((ret >> 24) & 0xff) == 'A')
1648 extern char **environ;
1652 char fname[256], *prefix = "/ticket/";
1654 char **newenv, **senv, **denv;
1657 if (stat("/ticket", &sbuf) == -1) {
1658 prefix = "/tmp/tkt";
1661 pag = curpag() & 0xffffffff;
1663 sprintf(fname, "%s%d", prefix, getuid());
1665 sprintf(fname, "%sp%ld", prefix, pag);
1667 ktc_set_tkt_string(fname);
1669 for (senv = environ, numenv = 0; *senv; senv++)
1671 newenv = (char **)malloc((numenv + 2) * sizeof(char *));
1673 for (senv = environ, denv = newenv; *senv; *senv++) {
1674 if (strncmp(*senv, "KRBTKFILE=", 10) != 0)
1678 *denv = (char *)malloc(10 + strlen(fname) + 1);
1679 strcpy(*denv, "KRBTKFILE=");
1680 strcat(*denv, fname);
1683 UNLOCK_GLOBAL_MUTEX;
1687 * BLETCH! We have to invoke the entire afsconf package just to
1688 * find out what the local cell is.
1694 struct afsconf_dir *conf;
1696 if ((conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))
1697 || (conf = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH))) {
1698 code = afsconf_GetLocalCell(conf, lcell, sizeof(lcell));
1699 afsconf_Close(conf);
1701 if (!conf || code) {
1702 printf("** Can't determine local cell name!\n");