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>
64 #include <afs/venus.h>
65 #include <afs/afsutil.h>
67 #endif /* defined(UKERNEL) */
74 /* AFS_KERBEROS_ENV is now conditionally defined in the Makefile */
75 #define AFS_KERBEROS_ENV
78 #ifdef AFS_KERBEROS_ENV
81 #include "cellconfig.h"
82 static char lcell[MAXCELLCHARS];
84 #define TKT_ROOT "/tmp/tkt"
89 /* Definitions for ticket file utilities */
93 /* Error codes returned by ticket file utilities */
94 #define NO_TKT_FIL 76 /* No ticket file found */
95 #define TKT_FIL_ACC 77 /* Couldn't access tkt file */
96 #define TKT_FIL_LCK 78 /* Couldn't lock ticket file */
97 #define TKT_FIL_FMT 79 /* Bad ticket file format */
98 #define TKT_FIL_INI 80 /* afs_tf_init not called first */
100 /* Values returned by get_credentials */
101 #define RET_TKFIL 21 /* Can't read ticket file */
110 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
111 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0, 0 };
112 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0, 0 };
113 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0, 0 };
116 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0 };
117 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0 };
118 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0 };
125 /* the following routines aren't static anymore on behalf of the kerberos IV
126 * compatibility library built in subtree krb.
128 int afs_tf_init(), afs_tf_get_pname(), afs_tf_get_pinst(), afs_tf_get_cred();
129 int afs_tf_save_cred(), afs_tf_close(), afs_tf_create();
130 int afs_tf_dest_tkt();
131 static void ktc_LocalCell();
132 #endif /* AFS_KERBEROS_ENV */
135 #define PIOCTL afs_pioctl
136 #elif defined(UKERNEL)
137 #define PIOCTL(A,B,C,D) call_syscall(AFSCALL_PIOCTL,A,B,C,D)
139 #define PIOCTL pioctl
143 #ifdef KERNEL_KTC_COMPAT
146 #define KTC_SYSCALL 32
149 /* Kernel call opcode definitions */
150 #define KTC_OPCODE_BASE 4300
151 #define KTC_NO_OP (0+KTC_OPCODE_BASE)
152 #define KTC_SETTOKEN_OP (1+KTC_OPCODE_BASE)
153 #define KTC_GETTOKEN_OP (2+KTC_OPCODE_BASE)
154 #define KTC_LISTTOKENS_OP (3+KTC_OPCODE_BASE)
155 #define KTC_FORGETTOKEN_OP (4+KTC_OPCODE_BASE)
156 #define KTC_FORGETALLTOKENS_OP (5+KTC_OPCODE_BASE)
157 #define KTC_STATUS_OP (6+KTC_OPCODE_BASE)
158 #define KTC_GC_OP (7+KTC_OPCODE_BASE)
160 #define KTC_INTERFACE_VERSION 3
162 /* We have to determine if the kernel supports the ktc system call. To do so
163 * we attempt to execute its noop function. If this is successful we use the
164 * kernel calls in the future otherwise we let the old code run. */
166 /* To safely check to see whether a system call exists we have to intercept the
167 * SIGSYS signal which is caused by executing a non-existant system call. If
168 * it is ignored the syscall routine returns EINVAL. The SIGSYS is reset to
169 * its old value after the return from syscall. */
171 static int kernelKTC = 0;
173 #ifdef AFS_DECOSF_ENV
175 * SIGSYS semantics are broken on Dec AXP OSF/1 v1.2 systems. You need
176 * to ignore SIGTRAP too. It is claimed to be fixed under v1.3, but...
179 #define CHECK_KERNEL \
180 if (kernelKTC == 0) { \
184 old = (int (*)())signal(SIGSYS, SIG_IGN); \
185 old_t = (int (*)())signal(SIGTRAP, SIG_IGN); \
186 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
188 signal(SIGSYS, old); \
189 signal(SIGTRAP, old_t); \
190 if (code == 0) kernelKTC = 1; \
191 else kernelKTC = 2; \
192 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
195 #else /* AFS_DECOSF_ENV */
197 #define CHECK_KERNEL \
198 if (kernelKTC == 0) { \
201 old = (int (*)())signal(SIGSYS, SIG_IGN); \
202 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
204 signal(SIGSYS, old); \
205 if (code == 0) kernelKTC = 1; \
206 else kernelKTC = 2; \
207 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
209 #endif /* AFS_DECOSF_ENV */
211 #define TRY_KERNEL(cmd,a1,a2,a3,a4) \
213 if (kernelKTC == 1) \
214 return syscall (KTC_SYSCALL, cmd, \
215 KTC_INTERFACE_VERSION, a1,a2,a3,a4); \
219 #define TRY_KERNEL(cmd,a1,a2,a3,a4)
220 #endif /* KERNEL_KTC_COMPAT */
222 #if !defined(UKERNEL)
223 /* this is a structure used to communicate with the afs cache mgr, but is
224 * otherwise irrelevant */
226 afs_int32 AuthHandle;
227 char HandShakeKey[8];
229 afs_int32 BeginTimestamp;
230 afs_int32 EndTimestamp;
232 #endif /* !defined(UKERNEL) */
234 #define MAXLOCALTOKENS 4
238 struct ktc_principal server;
239 struct ktc_principal client;
240 struct ktc_token token;
241 } local_tokens[MAXLOCALTOKENS] = { {
247 /* new interface routines to the ticket cache. Only handle afs service right
251 NewSetToken(aserver, atoken, aclient, flags)
252 struct ktc_principal *aserver;
253 struct ktc_principal *aclient;
254 struct ktc_token *atoken;
257 TRY_KERNEL(KTC_SETTOKEN_OP, aserver, aclient, atoken,
258 sizeof(struct ktc_token));
259 /* no kernel ticket cache */
263 #define MAXPIOCTLTOKENLEN \
264 (3*sizeof(afs_int32)+MAXKTCTICKETLEN+sizeof(struct ClearToken)+MAXKTCREALMLEN)
267 OldSetToken(aserver, atoken, aclient, flags)
268 struct ktc_principal *aserver, *aclient;
269 struct ktc_token *atoken;
272 struct ViceIoctl iob;
273 char tbuffer[MAXPIOCTLTOKENLEN];
275 struct ClearToken ct;
276 register afs_int32 code;
279 if (strcmp(aserver->name, "afs") != 0) {
282 for (i = 0; i < MAXLOCALTOKENS; i++)
283 if (local_tokens[i].valid) {
284 if ((strcmp(local_tokens[i].server.name, aserver->name) == 0)
287 (local_tokens[i].server.instance,
288 aserver->instance) == 0)
289 && (strcmp(local_tokens[i].server.cell, aserver->cell) ==
291 found = i; /* replace existing entry */
293 } else /* valid, but no match */
296 found = i; /* remember this empty slot */
299 memcpy(&local_tokens[found].token, atoken, sizeof(struct ktc_token));
300 local_tokens[found].server = *aserver;
301 local_tokens[found].client = *aclient;
302 local_tokens[found].valid = 1;
305 tp = tbuffer; /* start copying here */
306 if ((atoken->ticketLen < MINKTCTICKETLEN)
307 || (atoken->ticketLen > MAXKTCTICKETLEN))
309 memcpy(tp, &atoken->ticketLen, sizeof(afs_int32)); /* copy in ticket length */
310 tp += sizeof(afs_int32);
311 memcpy(tp, atoken->ticket, atoken->ticketLen); /* copy in ticket */
312 tp += atoken->ticketLen;
313 /* next, copy in the "clear token", describing who we are */
314 ct.AuthHandle = atoken->kvno; /* hide auth handle here */
315 memcpy(ct.HandShakeKey, &atoken->sessionKey, 8);
317 ct.BeginTimestamp = atoken->startTime;
318 ct.EndTimestamp = atoken->endTime;
319 if (ct.BeginTimestamp == 0)
320 ct.BeginTimestamp = 1;
322 if ((strlen(aclient->name) > strlen("AFS ID "))
323 && (aclient->instance[0] == 0)) {
325 afs_int32 viceId = 0;
326 char *cp = aclient->name + strlen("AFS ID ");
333 viceId = viceId * 10 + (int)(*cp - '0');
338 ct.ViceId = viceId * sign; /* OK to let any value here? */
339 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 0)
340 ct.BeginTimestamp++; /* force lifetime to be odd */
343 ct.ViceId = getuid(); /* wrong, but works in primary cell */
344 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1)
345 ct.BeginTimestamp++; /* force lifetime to be even */
350 * Information needed by the user space cache manager
352 u.u_expiration = ct.EndTimestamp;
353 u.u_viceid = ct.ViceId;
356 temp = sizeof(struct ClearToken);
357 memcpy(tp, &temp, sizeof(afs_int32));
358 tp += sizeof(afs_int32);
359 memcpy(tp, &ct, sizeof(struct ClearToken));
360 tp += sizeof(struct ClearToken);
362 /* next copy in primary flag */
366 * The following means that setpag will happen inside afs just before
367 * the authentication to prevent the setpag/klog race condition.
369 * The following means that setpag will affect the parent process as
370 * well as the current process.
372 if (flags & AFS_SETTOK_SETPAG)
375 memcpy(tp, &temp, sizeof(afs_int32));
376 tp += sizeof(afs_int32);
378 /* finally copy in the cell name */
379 temp = strlen(aserver->cell);
380 if (temp >= MAXKTCREALMLEN)
382 strcpy(tp, aserver->cell);
385 /* now setup for the pioctl */
387 iob.in_size = tp - tbuffer;
389 iob.out_size = sizeof(tbuffer);
391 #if defined(NO_AFS_CLIENT)
395 if ((tkfile = getenv("TKTFILE"))
398 open(tkfile, O_WRONLY | O_APPEND | O_TRUNC | O_CREAT,
400 printf("Writing ticket to: %s\n", tkfile);
401 code = (write(fd, iob.in, iob.in_size) != iob.in_size);
404 code = KTC_PIOCTLFAIL;
406 #else /* NO_AFS_CLIENT */
407 code = PIOCTL(0, VIOCSETTOK, &iob, 0);
408 #endif /* NO_AFS_CLIENT */
410 return KTC_PIOCTLFAIL;
415 ktc_SetToken(struct ktc_principal *aserver,
416 struct ktc_token *atoken,
417 struct ktc_principal *aclient,
423 #ifdef AFS_KERBEROS_ENV
427 if ( /*!strcmp(aclient->cell, lcell) && this would only store local creds */
428 (strcmp(aserver->name, "AuthServer")
429 || strcmp(aserver->instance, "Admin"))) {
430 if (strcmp(aserver->name, "krbtgt") == 0) {
431 static char lrealm[MAXKTCREALMLEN];
434 ucstring(lrealm, lcell, MAXKTCREALMLEN);
435 if (strcmp(aserver->instance, lrealm) == 0) {
436 afs_tf_create(aclient->name, aclient->instance);
440 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
441 if (ncode == NO_TKT_FIL) {
442 (void)afs_tf_create(aclient->name, aclient->instance);
443 ncode = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
447 afs_tf_save_cred(aserver, atoken, aclient);
453 #endif /* NO_AFS_CLIENT */
457 #ifndef NO_AFS_CLIENT
458 ncode = NewSetToken(aserver, atoken, aclient, flags);
459 if (ncode || /* new style failed */
460 (strcmp(aserver->name, "afs") == 0)) { /* for afs tokens do both */
461 ocode = OldSetToken(aserver, atoken, aclient, flags);
464 if (ncode && ocode) {
468 else if (ocode == KTC_PIOCTLFAIL)
476 return KTC_PIOCTLFAIL;
478 #endif /* NO_AFS_CLIENT */
483 /* get token, given server we need and token buffer. aclient will eventually
484 * be set to our identity to the server.
486 ktc_GetToken(aserver, atoken, atokenLen, aclient)
487 struct ktc_principal *aserver, *aclient;
489 struct ktc_token *atoken;
491 struct ViceIoctl iob;
492 char tbuffer[MAXPIOCTLTOKENLEN];
493 register afs_int32 code;
495 char *stp, *cellp; /* secret token ptr */
496 struct ClearToken ct;
499 int maxLen; /* biggest ticket we can copy */
500 int tktLen; /* server ticket length */
504 #ifndef NO_AFS_CLIENT
505 TRY_KERNEL(KTC_GETTOKEN_OP, aserver, aclient, atoken, atokenLen);
506 #endif /* NO_AFS_CLIENT */
508 #ifdef AFS_KERBEROS_ENV
512 #ifndef NO_AFS_CLIENT
513 if (strcmp(aserver->name, "afs") != 0)
514 #endif /* NO_AFS_CLIENT */
517 /* try the local tokens */
518 for (i = 0; i < MAXLOCALTOKENS; i++)
519 if (local_tokens[i].valid
520 && (strcmp(local_tokens[i].server.name, aserver->name) == 0)
521 && (strcmp(local_tokens[i].server.instance, aserver->instance)
523 && (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
524 memcpy(atoken, &local_tokens[i].token,
525 min(atokenLen, sizeof(struct ktc_token)));
527 *aclient = local_tokens[i].client;
531 #ifdef AFS_KERBEROS_ENV
532 if (!afs_tf_init(ktc_tkt_string(), R_TKT_FIL)) {
534 if (!afs_tf_get_pname(aclient->name)
535 && !afs_tf_get_pinst(aclient->instance))
538 char tmpstring[MAXHOSTCHARS];
539 afs_tf_get_pname(&tmpstring);
540 afs_tf_get_pinst(&tmpstring);
545 struct ktc_principal cprincipal;
546 struct ktc_token ctoken;
548 while (!afs_tf_get_cred(&cprincipal, &ctoken)) {
549 if (strcmp(cprincipal.name, aserver->name) == 0
550 && strcmp(cprincipal.instance, aserver->instance) == 0
551 && strcmp(cprincipal.cell, aserver->cell) == 0) {
554 strcpy(aclient->cell, lcell);
555 memcpy(atoken, &ctoken,
556 min(atokenLen, sizeof(struct ktc_token)));
569 #ifndef NO_AFS_CLIENT
570 for (index = 0; index < 200; index++) { /* sanity check in case pioctl fails */
571 iob.in = (char *)&index;
572 iob.in_size = sizeof(afs_int32);
574 iob.out_size = sizeof(tbuffer);
576 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
579 /* failed to retrieve specified token */
580 if (code < 0 && errno == EDOM) {
585 /* token retrieved; parse buffer */
588 /* get ticket length */
589 memcpy(&temp, tp, sizeof(afs_int32));
591 tp += sizeof(afs_int32);
593 /* remember where ticket is and skip over it */
597 /* get size of clear token and verify */
598 memcpy(&temp, tp, sizeof(afs_int32));
599 if (temp != sizeof(struct ClearToken)) {
603 tp += sizeof(afs_int32);
605 /* copy clear token */
606 memcpy(&ct, tp, temp);
609 /* skip over primary flag */
610 tp += sizeof(afs_int32);
612 /* remember where cell name is */
615 if ((strcmp(cellp, aserver->cell) == 0)
616 #ifdef AFS_KERBEROS_ENV
617 || (*aserver->cell == '\0' && strcmp(cellp, lcell) == 0)
620 /* got token for cell; check that it will fit */
622 atokenLen - sizeof(struct ktc_token) + MAXKTCTICKETLEN;
623 if (maxLen < tktLen) {
628 /* set return values */
629 memcpy(atoken->ticket, stp, tktLen);
630 atoken->startTime = ct.BeginTimestamp;
631 atoken->endTime = ct.EndTimestamp;
632 if (ct.AuthHandle == -1) {
635 atoken->kvno = ct.AuthHandle;
636 memcpy(&atoken->sessionKey, ct.HandShakeKey,
637 sizeof(struct ktc_encryptionKey));
638 atoken->ticketLen = tktLen;
641 strcpy(aclient->cell, cellp);
642 aclient->instance[0] = 0;
644 if ((atoken->kvno == 999) || /* old style bcrypt ticket */
645 (ct.BeginTimestamp && /* new w/ prserver lookup */
646 (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1))) {
647 sprintf(aclient->name, "AFS ID %d", ct.ViceId);
649 sprintf(aclient->name, "Unix UID %d", ct.ViceId);
657 #endif /* NO_AFS_CLIENT */
660 if ((code < 0) && (errno == EINVAL))
662 return KTC_PIOCTLFAIL; /* probable cause */
666 * Forget tokens for this server and the calling user.
667 * NOT IMPLEMENTED YET!
669 #ifndef NO_AFS_CLIENT
670 ktc_ForgetToken(aserver)
671 struct ktc_principal *aserver;
676 TRY_KERNEL(KTC_FORGETTOKEN_OP, aserver, 0, 0, 0);
678 rc = ktc_ForgetAllTokens(); /* bogus, but better */
682 #endif /* NO_AFS_CLIENT */
684 /* ktc_ListTokens - list all tokens. start aprevIndex at 0, it returns the
685 * next rock in (*aindex). (*aserver) is set to the relevant ticket on
689 ktc_ListTokens(int aprevIndex,
691 struct ktc_principal *aserver)
693 struct ViceIoctl iob;
694 char tbuffer[MAXPIOCTLTOKENLEN];
695 register afs_int32 code;
697 afs_int32 temp, index;
699 memset(tbuffer, 0, sizeof(tbuffer));
702 #ifndef NO_AFS_CLIENT
703 TRY_KERNEL(KTC_LISTTOKENS_OP, aserver, aprevIndex, aindex, 0);
704 #endif /* NO_AFS_CLIENT */
710 #endif /* NO_AFS_CLIENT */
711 #ifdef AFS_KERBEROS_ENV
714 struct ktc_principal cprincipal;
715 struct ktc_token ctoken;
717 if (afs_tf_init(ktc_tkt_string(), R_TKT_FIL)
718 || afs_tf_get_pname(tbuffer) || afs_tf_get_pinst(tbuffer)) {
724 for (i = 214; i < index; i++) {
725 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
733 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
740 #ifndef NO_AFS_CLIENT
741 if (!strcmp(cprincipal.name, "afs") && cprincipal.instance[0] == 0) {
744 #endif /* NO_AFS_CLIENT */
746 for (i = 0; i < MAXLOCALTOKENS; i++) {
747 if (!strcmp(cprincipal.name, local_tokens[i].server.name)
748 && !strcmp(cprincipal.instance,
749 local_tokens[i].server.instance)
750 && !strcmp(cprincipal.cell, local_tokens[i].server.cell)) {
755 *aserver = cprincipal;
763 #ifndef NO_AFS_CLIENT
764 if (index >= 123) { /* special hack for returning TCS */
765 while (index - 123 < MAXLOCALTOKENS) {
766 if (local_tokens[index - 123].valid) {
767 *aserver = local_tokens[index - 123].server;
775 #ifdef AFS_KERBEROS_ENV
776 return ktc_ListTokens(214, aindex, aserver);
782 /* get tokens from the kernel */
783 while (index < 200) { /* sanity check in case pioctl fails */
784 iob.in = (char *)&index;
785 iob.in_size = sizeof(afs_int32);
787 iob.out_size = sizeof(tbuffer);
788 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
789 if (code < 0 && errno == EDOM) {
792 rc = ktc_ListTokens(123, aindex, aserver);
801 break; /* got a ticket */
802 /* otherwise we should skip this ticket slot */
809 return KTC_PIOCTLFAIL;
815 /* next iterator determined by earlier loop */
818 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of secret token */
819 tp += sizeof(afs_int32);
820 tp += temp; /* skip ticket for now */
821 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of clear token */
822 if (temp != sizeof(struct ClearToken)) {
826 tp += sizeof(afs_int32); /* skip length */
827 tp += temp; /* skip clear token itself */
828 tp += sizeof(afs_int32); /* skip primary flag */
829 /* tp now points to the cell name */
830 strcpy(aserver->cell, tp);
831 aserver->instance[0] = 0;
832 strcpy(aserver->name, "afs");
833 #endif /* NO_AFS_CLIENT */
838 /* discard all tokens from this user's cache */
843 #ifndef NO_AFS_CLIENT
844 TRY_KERNEL(KTC_FORGETALLTOKENS_OP, 0, 0, 0, 0);
845 #endif /* NO_AFS_CLIENT */
852 struct ViceIoctl iob;
853 register afs_int32 code;
856 for (i = 0; i < MAXLOCALTOKENS; i++)
857 local_tokens[i].valid = 0;
863 #ifndef NO_AFS_CLIENT
864 code = PIOCTL(0, VIOCUNPAG, &iob, 0);
866 return KTC_PIOCTLFAIL;
867 #endif /* NO_AFS_CLIENT */
872 ktc_ForgetAllTokens(void)
877 #ifdef AFS_KERBEROS_ENV
878 (void)afs_tf_dest_tkt();
881 ncode = NewForgetAll();
882 ocode = OldForgetAll();
883 if (ncode && ocode) {
886 else if (ocode == KTC_PIOCTLFAIL)
891 return KTC_PIOCTLFAIL;
897 /* ktc_OldPioctl - returns a boolean true if the kernel supports only the old
898 * pioctl interface for delivering AFS tickets to the cache manager. */
904 #ifdef KERNEL_KTC_COMPAT
906 rc = (kernelKTC != 1); /* old style interface */
915 #ifdef AFS_KERBEROS_ENV
917 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
919 * For copying and distribution information, please see the file
926 #include <sys/types.h>
927 #include <sys/stat.h>
928 #include <sys/file.h>
933 #define TF_LCK_RETRY ((unsigned)2) /* seconds to sleep before
934 * retry if ticket file is
938 * fd must be initialized to something that won't ever occur as a real
939 * file descriptor. Since open(2) returns only non-negative numbers as
940 * valid file descriptors, and afs_tf_init always stuffs the return value
941 * from open in here even if it is an error flag, we must
942 * a. Initialize fd to a negative number, to indicate that it is
943 * not initially valid.
944 * b. When checking for a valid fd, assume that negative values
945 * are invalid (ie. when deciding whether afs_tf_init has been
947 * c. In tf_close, be sure it gets reinitialized to a negative
951 static curpos; /* Position in tfbfr */
952 static lastpos; /* End of tfbfr */
953 static char tfbfr[BUFSIZ]; /* Buffer for ticket data */
955 static tf_gets(), tf_read();
958 * This file contains routines for manipulating the ticket cache file.
960 * The ticket file is in the following format:
962 * principal's name (null-terminated string)
963 * principal's instance (null-terminated string)
970 * Where "CREDENTIAL_x" consists of the following fixed-length
971 * fields from the CREDENTIALS structure (see "krb.h"):
973 * char service[MAXKTCNAMELEN]
974 * char instance[MAXKTCNAMELEN]
975 * char realm[REALM_SZ]
980 * afs_int32 issue_date
982 * Short description of routines:
984 * afs_tf_init() opens the ticket file and locks it.
986 * afs_tf_get_pname() returns the principal's name.
988 * afs_tf_get_pinst() returns the principal's instance (may be null).
990 * afs_tf_get_cred() returns the next CREDENTIALS record.
992 * afs_tf_save_cred() appends a new CREDENTIAL record to the ticket file.
994 * afs_tf_close() closes the ticket file and releases the lock.
996 * tf_gets() returns the next null-terminated string. It's an internal
997 * routine used by afs_tf_get_pname(), afs_tf_get_pinst(), and
1000 * tf_read() reads a given number of bytes. It's an internal routine
1001 * used by afs_tf_get_cred().
1005 * afs_tf_init() should be called before the other ticket file routines.
1006 * It takes the name of the ticket file to use, "tf_name", and a
1007 * read/write flag "rw" as arguments.
1009 * It tries to open the ticket file, checks the mode, and if everything
1010 * is okay, locks the file. If it's opened for reading, the lock is
1011 * shared. If it's opened for writing, the lock is exclusive.
1013 * Returns 0 if all went well, otherwise one of the following:
1015 * NO_TKT_FIL - file wasn't there
1016 * TKT_FIL_ACC - file was in wrong mode, etc.
1017 * TKT_FIL_LCK - couldn't lock the file, even after a retry
1020 afs_tf_init(tf_name, rw)
1025 struct stat stat_buf;
1037 if (lstat(tf_name, &stat_buf) < 0)
1045 if ((stat_buf.st_uid != me && me != 0)
1046 || ((stat_buf.st_mode & S_IFMT) != S_IFREG))
1050 * If "wflag" is set, open the ticket file in append-writeonly mode
1051 * and lock the ticket file in exclusive mode. If unable to lock
1052 * the file, sleep and try again. If we fail again, return with the
1053 * proper error message.
1056 curpos = sizeof(tfbfr);
1059 fd = open(tf_name, O_RDWR, 0600);
1063 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1064 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1065 sleep(TF_LCK_RETRY);
1066 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1068 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1069 sleep(TF_LCK_RETRY);
1070 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1080 * Otherwise "wflag" is not set and the ticket file should be opened
1081 * for read-only operations and locked for shared access.
1084 fd = open(tf_name, O_RDONLY, 0600);
1088 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1089 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1090 sleep(TF_LCK_RETRY);
1091 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1093 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1094 sleep(TF_LCK_RETRY);
1095 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1106 * afs_tf_get_pname() reads the principal's name from the ticket file. It
1107 * should only be called after afs_tf_init() has been called. The
1108 * principal's name is filled into the "p" parameter. If all goes well,
1109 * 0 is returned. If afs_tf_init() wasn't called, TKT_FIL_INI is
1110 * returned. If the name was null, or EOF was encountered, or the name
1111 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned.
1120 if (tf_gets(p, MAXKTCNAMELEN) < 2) /* can't be just a null */
1126 * afs_tf_get_pinst() reads the principal's instance from a ticket file.
1127 * It should only be called after afs_tf_init() and afs_tf_get_pname() have
1128 * been called. The instance is filled into the "inst" parameter. If all
1129 * goes well, 0 is returned. If afs_tf_init() wasn't called,
1130 * TKT_FIL_INI is returned. If EOF was encountered, or the instance
1131 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned. Note that the
1132 * instance may be null.
1135 afs_tf_get_pinst(inst)
1141 if (tf_gets(inst, MAXKTCNAMELEN) < 1)
1147 * afs_tf_get_cred() reads a CREDENTIALS record from a ticket file and fills
1148 * in the given structure "c". It should only be called after afs_tf_init(),
1149 * afs_tf_get_pname(), and afs_tf_get_pinst() have been called. If all goes
1150 * well, 0 is returned. Possible error codes are:
1152 * TKT_FIL_INI - afs_tf_init wasn't called first
1153 * TKT_FIL_FMT - bad format
1154 * EOF - end of file encountered
1157 afs_tf_get_cred(principal, token)
1158 struct ktc_principal *principal;
1159 struct ktc_token *token;
1163 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1168 if ((k_errno = tf_gets(principal->name, MAXKTCNAMELEN)) < 2)
1171 case 1: /* can't be just a null */
1176 if ((k_errno = tf_gets(principal->instance, MAXKTCNAMELEN)) < 1)
1183 if ((k_errno = tf_gets(principal->cell, MAXKTCREALMLEN)) < 2)
1186 case 1: /* can't be just a null */
1191 lcstring(principal->cell, principal->cell, MAXKTCREALMLEN);
1192 if (tf_read((char *)&(token->sessionKey), 8) < 1
1193 || tf_read((char *)&(lifetime), sizeof(lifetime)) < 1
1194 || tf_read((char *)&(kvno), sizeof(kvno)) < 1
1195 || tf_read((char *)&(token->ticketLen), sizeof(token->ticketLen))
1197 /* don't try to read a silly amount into ticket->dat */
1198 token->ticketLen > MAXKTCTICKETLEN
1199 || tf_read((char *)(token->ticket), token->ticketLen) < 1
1200 || tf_read((char *)&mit_compat, sizeof(mit_compat)) < 1) {
1203 token->startTime = mit_compat;
1204 token->endTime = life_to_time(token->startTime, lifetime);
1210 * tf_close() closes the ticket file and sets "fd" to -1. If "fd" is
1211 * not a valid file descriptor, it just returns. It also clears the
1212 * buffer used to read tickets.
1214 * The return value is not defined.
1220 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1221 (void)fcntl(fd, F_SETLK, &fileUlock);
1223 (void)flock(fd, LOCK_UN);
1226 fd = -1; /* see declaration of fd above */
1228 memset(tfbfr, 0, sizeof(tfbfr));
1232 * tf_gets() is an internal routine. It takes a string "s" and a count
1233 * "n", and reads from the file until either it has read "n" characters,
1234 * or until it reads a null byte. When finished, what has been read exists
1237 * Possible return values are:
1239 * n the number of bytes read (including null terminator)
1240 * when all goes well
1242 * 0 end of file or read error
1244 * TOO_BIG if "count" characters are read and no null is
1245 * encountered. This is an indication that the ticket
1246 * file is seriously ill.
1258 for (count = n - 1; count > 0; --count) {
1259 if (curpos >= sizeof(tfbfr)) {
1260 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1263 if (curpos == lastpos) {
1266 *s = tfbfr[curpos++];
1274 * tf_read() is an internal routine. It takes a string "s" and a count
1275 * "n", and reads from the file until "n" bytes have been read. When
1276 * finished, what has been read exists in "s".
1278 * Possible return values are:
1280 * n the number of bytes read when all goes well
1282 * 0 on end of file or read error
1292 for (count = n; count > 0; --count) {
1293 if (curpos >= sizeof(tfbfr)) {
1294 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1297 if (curpos == lastpos) {
1300 *s++ = tfbfr[curpos++];
1308 * afs_tf_save_cred() appends an incoming ticket to the end of the ticket
1309 * file. You must call afs_tf_init() before calling afs_tf_save_cred().
1311 * The "service", "instance", and "realm" arguments specify the
1312 * server's name; "aticket" contains the credential.
1314 * Returns 0 if all goes well, TKT_FIL_INI if afs_tf_init() wasn't
1315 * called previously, and KFAILURE for anything else that went wrong.
1318 afs_tf_save_cred(aserver, atoken, aclient)
1319 struct ktc_principal *aserver;
1320 struct ktc_principal *aclient;
1321 struct ktc_token *atoken; /* Token */
1323 char realm[MAXKTCREALMLEN + 1];
1324 char junk[MAXKTCNAMELEN];
1325 struct ktc_principal principal;
1326 struct ktc_token token;
1330 int count; /* count for write */
1331 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1333 if (fd < 0) { /* fd is ticket file as set by afs_tf_init */
1337 ucstring(realm, aserver->cell, MAXKTCREALMLEN);
1338 realm[MAXKTCREALMLEN] = '\0';
1340 /* Look for a duplicate ticket */
1341 (void)lseek(fd, (off_t) 0L, 0);
1342 curpos = sizeof(tfbfr);
1344 if (afs_tf_get_pname(junk) || strcmp(junk, aclient->name)
1345 || afs_tf_get_pinst(junk) || strcmp(junk, aclient->instance))
1349 start = lseek(fd, (off_t) 0L, 1) - lastpos + curpos;
1350 status = afs_tf_get_cred(&principal, &token);
1351 } while (status == 0
1352 && (strcmp(aserver->name, principal.name) != 0
1353 || strcmp(aserver->instance, principal.instance) != 0
1354 || strcmp(aserver->cell, principal.cell) != 0));
1357 * Two tickets for the same user authenticating to the same service
1358 * should be the same length, but we check here just to make sure.
1360 if (status == 0 && token.ticketLen != atoken->ticketLen)
1362 if (status && status != EOF)
1365 /* Position over the credential we just matched (or the EOF) */
1366 lseek(fd, start, 0);
1367 curpos = lastpos = sizeof(tfbfr);
1369 /* Write the ticket and associated data */
1371 count = strlen(aserver->name) + 1;
1372 if (write(fd, aserver->name, count) != count)
1375 count = strlen(aserver->instance) + 1;
1376 if (write(fd, aserver->instance, count) != count)
1379 count = strlen(realm) + 1;
1380 if (write(fd, realm, count) != count)
1383 if (write(fd, (char *)&atoken->sessionKey, 8) != 8)
1386 lifetime = time_to_life(atoken->startTime, atoken->endTime);
1387 if (write(fd, (char *)&lifetime, sizeof(int)) != sizeof(int))
1390 kvno = atoken->kvno;
1391 if (write(fd, (char *)&kvno, sizeof(int)) != sizeof(int))
1394 if (write(fd, (char *)&(atoken->ticketLen), sizeof(int)) != sizeof(int))
1397 count = atoken->ticketLen;
1398 if (write(fd, atoken->ticket, count) != count)
1401 mit_compat = atoken->startTime;
1402 if (write(fd, (char *)&mit_compat, sizeof(mit_compat))
1403 != sizeof(mit_compat))
1406 /* Actually, we should check each write for success */
1413 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
1416 * For copying and distribution information, please see the file
1417 * <mit-copyright.h>.
1423 * This routine is used to generate the name of the file that holds
1424 * the user's cache of server tickets and associated session keys.
1426 * If it is set, krb_ticket_string contains the ticket file name.
1427 * Otherwise, the filename is constructed as follows:
1429 * If it is set, the environment variable "KRBTKFILE" will be used as
1430 * the ticket file name. Otherwise TKT_ROOT (defined in "krb.h") and
1431 * the user's uid are concatenated to produce the ticket file name
1432 * (e.g., "/tmp/tkt123"). A pointer to the string containing the ticket
1433 * file name is returned.
1436 static char krb_ticket_string[4096] = "";
1439 ktc_tkt_string(void)
1441 return ktc_tkt_string_uid(getuid());
1445 ktc_tkt_string_uid(afs_uint32 uid)
1450 if (!*krb_ticket_string) {
1451 if (env = getenv("KRBTKFILE")) {
1452 (void)strncpy(krb_ticket_string, env,
1453 sizeof(krb_ticket_string) - 1);
1454 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1456 /* 32 bits of signed integer will always fit in 11 characters
1457 * (including the sign), so no need to worry about overflow */
1458 (void)sprintf(krb_ticket_string, "%s%d", TKT_ROOT, uid);
1461 UNLOCK_GLOBAL_MUTEX;
1462 return krb_ticket_string;
1466 * This routine is used to set the name of the file that holds the user's
1467 * cache of server tickets and associated session keys.
1469 * The value passed in is copied into local storage.
1471 * NOTE: This routine should be called during initialization, before other
1472 * Kerberos routines are called; otherwise tkt_string() above may be called
1473 * and return an undesired ticket file name until this routine is called.
1477 ktc_set_tkt_string(char * val)
1481 (void)strncpy(krb_ticket_string, val, sizeof(krb_ticket_string) - 1);
1482 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1483 UNLOCK_GLOBAL_MUTEX;
1488 * tf_create() is used to initialize the ticket store. It creates the
1489 * file to contain the tickets and writes the given user's name "pname"
1490 * and instance "pinst" in the file. in_tkt() returns KSUCCESS on
1491 * success, or KFAILURE if something goes wrong.
1494 afs_tf_create(pname, pinst)
1501 char *file = ktc_tkt_string();
1510 if (lstat(file, &sbuf) == 0) {
1511 if ((sbuf.st_uid != me && me != 0)
1512 || ((sbuf.st_mode & S_IFMT) != S_IFREG) || sbuf.st_mode & 077) {
1515 /* file already exists, and permissions appear ok, so nuke it */
1516 if ((fd = open(file, O_RDWR, 0)) < 0)
1517 goto out; /* can't zero it, but we can still try truncating it */
1519 memset(zerobuf, 0, sizeof(zerobuf));
1521 for (i = 0; i < sbuf.st_size; i += sizeof(zerobuf))
1522 if (write(fd, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf)) {
1533 /* arrange so the file is owned by the ruid
1534 * (swap real & effective uid if necessary).
1535 * This isn't a security problem, since the ticket file, if it already
1536 * exists, has the right uid (== ruid) and mode. */
1538 if (setreuid(metoo, me) < 0) {
1542 tktfile = creat(file, 0600);
1544 if (setreuid(me, metoo) < 0) {
1545 /* can't switch??? fail! */
1552 count = strlen(pname) + 1;
1553 if (write(tktfile, pname, count) != count) {
1554 (void)close(tktfile);
1557 count = strlen(pinst) + 1;
1558 if (write(tktfile, pinst, count) != count) {
1559 (void)close(tktfile);
1562 (void)close(tktfile);
1567 * dest_tkt() is used to destroy the ticket store upon logout.
1568 * If the ticket file does not exist, dest_tkt() returns RET_TKFIL.
1569 * Otherwise the function returns 0 on success, KFAILURE on
1575 char *file = ktc_tkt_string();
1581 if (lstat(file, &statb) < 0)
1584 if (!(statb.st_mode & S_IFREG))
1587 if ((fd = open(file, O_RDWR, 0)) < 0)
1590 memset(buf, 0, BUFSIZ);
1592 for (i = 0; i < statb.st_size; i += BUFSIZ)
1593 if (write(fd, buf, BUFSIZ) != BUFSIZ) {
1605 if (errno == ENOENT)
1607 else if (errno != 0)
1615 #if defined(AFS_AIX51_ENV)
1618 if (get_pag(PAG_AFS, &pag) < 0 || pag == 0)
1622 gid_t groups[NGROUPS_MAX];
1624 afs_uint32 h, l, ret;
1626 if (getgroups(sizeof groups / sizeof groups[0], groups) < 2)
1629 g0 = groups[0] & 0xffff;
1630 g1 = groups[1] & 0xffff;
1633 if (g0 < 0xc000 && g1 < 0xc000) {
1634 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
1636 h = (g1 >> 14) + h + h + h;
1637 ret = ((h << 28) | l);
1638 /* Additional testing */
1639 if (((ret >> 24) & 0xff) == 'A')
1651 extern char **environ;
1655 char fname[256], *prefix = "/ticket/";
1657 char **newenv, **senv, **denv;
1660 if (stat("/ticket", &sbuf) == -1) {
1661 prefix = "/tmp/tkt";
1664 pag = curpag() & 0xffffffff;
1666 sprintf(fname, "%s%d", prefix, getuid());
1668 sprintf(fname, "%sp%ld", prefix, pag);
1670 ktc_set_tkt_string(fname);
1672 for (senv = environ, numenv = 0; *senv; senv++)
1674 newenv = (char **)malloc((numenv + 2) * sizeof(char *));
1676 for (senv = environ, denv = newenv; *senv; *senv++) {
1677 if (strncmp(*senv, "KRBTKFILE=", 10) != 0)
1681 *denv = (char *)malloc(10 + strlen(fname) + 1);
1682 strcpy(*denv, "KRBTKFILE=");
1683 strcat(*denv, fname);
1686 UNLOCK_GLOBAL_MUTEX;
1690 * BLETCH! We have to invoke the entire afsconf package just to
1691 * find out what the local cell is.
1697 struct afsconf_dir *conf;
1699 if ((conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))
1700 || (conf = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH))) {
1701 code = afsconf_GetLocalCell(conf, lcell, sizeof(lcell));
1702 afsconf_Close(conf);
1704 if (!conf || code) {
1705 printf("** Can't determine local cell name!\n");