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>
61 #include <sys/lockf.h>
70 #include <afs/venus.h>
71 #include <afs/afsutil.h>
73 #endif /* defined(UKERNEL) */
80 /* AFS_KERBEROS_ENV is now conditionally defined in the Makefile */
81 #define AFS_KERBEROS_ENV
84 #ifdef AFS_KERBEROS_ENV
87 #include "cellconfig.h"
88 static char lcell[MAXCELLCHARS];
90 #define TKT_ROOT "/tmp/tkt"
95 /* Definitions for ticket file utilities */
99 /* Error codes returned by ticket file utilities */
100 #define NO_TKT_FIL 76 /* No ticket file found */
101 #define TKT_FIL_ACC 77 /* Couldn't access tkt file */
102 #define TKT_FIL_LCK 78 /* Couldn't lock ticket file */
103 #define TKT_FIL_FMT 79 /* Bad ticket file format */
104 #define TKT_FIL_INI 80 /* afs_tf_init not called first */
106 /* Values returned by get_credentials */
107 #define RET_TKFIL 21 /* Can't read ticket file */
116 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
117 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0, 0 };
118 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0, 0 };
119 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0, 0 };
122 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0 };
123 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0 };
124 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0 };
131 /* the following routines aren't static anymore on behalf of the kerberos IV
132 * compatibility library built in subtree krb.
134 int afs_tf_init(), afs_tf_get_pname(), afs_tf_get_pinst(), afs_tf_get_cred();
135 int afs_tf_save_cred(), afs_tf_close(), afs_tf_create();
136 int afs_tf_dest_tkt();
137 static void ktc_LocalCell();
138 #endif /* AFS_KERBEROS_ENV */
141 #define PIOCTL afs_pioctl
142 #elif defined(UKERNEL)
143 #define PIOCTL(A,B,C,D) call_syscall(AFSCALL_PIOCTL,A,B,C,D)
145 #define PIOCTL pioctl
149 #ifdef KERNEL_KTC_COMPAT
152 #define KTC_SYSCALL 32
155 /* Kernel call opcode definitions */
156 #define KTC_OPCODE_BASE 4300
157 #define KTC_NO_OP (0+KTC_OPCODE_BASE)
158 #define KTC_SETTOKEN_OP (1+KTC_OPCODE_BASE)
159 #define KTC_GETTOKEN_OP (2+KTC_OPCODE_BASE)
160 #define KTC_LISTTOKENS_OP (3+KTC_OPCODE_BASE)
161 #define KTC_FORGETTOKEN_OP (4+KTC_OPCODE_BASE)
162 #define KTC_FORGETALLTOKENS_OP (5+KTC_OPCODE_BASE)
163 #define KTC_STATUS_OP (6+KTC_OPCODE_BASE)
164 #define KTC_GC_OP (7+KTC_OPCODE_BASE)
166 #define KTC_INTERFACE_VERSION 3
168 /* We have to determine if the kernel supports the ktc system call. To do so
169 * we attempt to execute its noop function. If this is successful we use the
170 * kernel calls in the future otherwise we let the old code run. */
172 /* To safely check to see whether a system call exists we have to intercept the
173 * SIGSYS signal which is caused by executing a non-existant system call. If
174 * it is ignored the syscall routine returns EINVAL. The SIGSYS is reset to
175 * its old value after the return from syscall. */
177 static int kernelKTC = 0;
179 #ifdef AFS_DECOSF_ENV
181 * SIGSYS semantics are broken on Dec AXP OSF/1 v1.2 systems. You need
182 * to ignore SIGTRAP too. It is claimed to be fixed under v1.3, but...
185 #define CHECK_KERNEL \
186 if (kernelKTC == 0) { \
190 old = (int (*)())signal(SIGSYS, SIG_IGN); \
191 old_t = (int (*)())signal(SIGTRAP, SIG_IGN); \
192 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
194 signal(SIGSYS, old); \
195 signal(SIGTRAP, old_t); \
196 if (code == 0) kernelKTC = 1; \
197 else kernelKTC = 2; \
198 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
201 #else /* AFS_DECOSF_ENV */
203 #define CHECK_KERNEL \
204 if (kernelKTC == 0) { \
207 old = (int (*)())signal(SIGSYS, SIG_IGN); \
208 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
210 signal(SIGSYS, old); \
211 if (code == 0) kernelKTC = 1; \
212 else kernelKTC = 2; \
213 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
215 #endif /* AFS_DECOSF_ENV */
217 #define TRY_KERNEL(cmd,a1,a2,a3,a4) \
219 if (kernelKTC == 1) \
220 return syscall (KTC_SYSCALL, cmd, \
221 KTC_INTERFACE_VERSION, a1,a2,a3,a4); \
225 #define TRY_KERNEL(cmd,a1,a2,a3,a4)
226 #endif /* KERNEL_KTC_COMPAT */
228 #if !defined(UKERNEL)
229 /* this is a structure used to communicate with the afs cache mgr, but is
230 * otherwise irrelevant */
232 afs_int32 AuthHandle;
233 char HandShakeKey[8];
235 afs_int32 BeginTimestamp;
236 afs_int32 EndTimestamp;
238 #endif /* !defined(UKERNEL) */
240 #define MAXLOCALTOKENS 4
244 struct ktc_principal server;
245 struct ktc_principal client;
246 struct ktc_token token;
247 } local_tokens[MAXLOCALTOKENS] = { {
253 /* new interface routines to the ticket cache. Only handle afs service right
257 NewSetToken(aserver, atoken, aclient, flags)
258 struct ktc_principal *aserver;
259 struct ktc_principal *aclient;
260 struct ktc_token *atoken;
263 TRY_KERNEL(KTC_SETTOKEN_OP, aserver, aclient, atoken,
264 sizeof(struct ktc_token));
265 /* no kernel ticket cache */
269 #define MAXPIOCTLTOKENLEN \
270 (3*sizeof(afs_int32)+MAXKTCTICKETLEN+sizeof(struct ClearToken)+MAXKTCREALMLEN)
273 OldSetToken(aserver, atoken, aclient, flags)
274 struct ktc_principal *aserver, *aclient;
275 struct ktc_token *atoken;
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.
492 ktc_GetToken(aserver, atoken, atokenLen, aclient)
493 struct ktc_principal *aserver, *aclient;
495 struct ktc_token *atoken;
497 struct ViceIoctl iob;
498 char tbuffer[MAXPIOCTLTOKENLEN];
499 register afs_int32 code;
501 char *stp, *cellp; /* secret token ptr */
502 struct ClearToken ct;
505 int maxLen; /* biggest ticket we can copy */
506 int tktLen; /* server ticket length */
510 #ifndef NO_AFS_CLIENT
511 TRY_KERNEL(KTC_GETTOKEN_OP, aserver, aclient, atoken, atokenLen);
512 #endif /* NO_AFS_CLIENT */
514 #ifdef AFS_KERBEROS_ENV
518 #ifndef NO_AFS_CLIENT
519 if (strcmp(aserver->name, "afs") != 0)
520 #endif /* NO_AFS_CLIENT */
523 /* try the local tokens */
524 for (i = 0; i < MAXLOCALTOKENS; i++)
525 if (local_tokens[i].valid
526 && (strcmp(local_tokens[i].server.name, aserver->name) == 0)
527 && (strcmp(local_tokens[i].server.instance, aserver->instance)
529 && (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
530 memcpy(atoken, &local_tokens[i].token,
531 min(atokenLen, sizeof(struct ktc_token)));
533 *aclient = local_tokens[i].client;
537 #ifdef AFS_KERBEROS_ENV
538 if (!afs_tf_init(ktc_tkt_string(), R_TKT_FIL)) {
540 if (!afs_tf_get_pname(aclient->name)
541 && !afs_tf_get_pinst(aclient->instance))
544 char tmpstring[MAXHOSTCHARS];
545 afs_tf_get_pname(&tmpstring);
546 afs_tf_get_pinst(&tmpstring);
551 struct ktc_principal cprincipal;
552 struct ktc_token ctoken;
554 while (!afs_tf_get_cred(&cprincipal, &ctoken)) {
555 if (strcmp(cprincipal.name, aserver->name) == 0
556 && strcmp(cprincipal.instance, aserver->instance) == 0
557 && strcmp(cprincipal.cell, aserver->cell) == 0) {
560 strcpy(aclient->cell, lcell);
561 memcpy(atoken, &ctoken,
562 min(atokenLen, sizeof(struct ktc_token)));
575 #ifndef NO_AFS_CLIENT
576 for (index = 0; index < 200; index++) { /* sanity check in case pioctl fails */
577 iob.in = (char *)&index;
578 iob.in_size = sizeof(afs_int32);
580 iob.out_size = sizeof(tbuffer);
582 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
585 /* failed to retrieve specified token */
586 if (code < 0 && errno == EDOM) {
591 /* token retrieved; parse buffer */
594 /* get ticket length */
595 memcpy(&temp, tp, sizeof(afs_int32));
597 tp += sizeof(afs_int32);
599 /* remember where ticket is and skip over it */
603 /* get size of clear token and verify */
604 memcpy(&temp, tp, sizeof(afs_int32));
605 if (temp != sizeof(struct ClearToken)) {
609 tp += sizeof(afs_int32);
611 /* copy clear token */
612 memcpy(&ct, tp, temp);
615 /* skip over primary flag */
616 tp += sizeof(afs_int32);
618 /* remember where cell name is */
621 if ((strcmp(cellp, aserver->cell) == 0)
622 #ifdef AFS_KERBEROS_ENV
623 || (*aserver->cell == '\0' && strcmp(cellp, lcell) == 0)
626 /* got token for cell; check that it will fit */
628 atokenLen - sizeof(struct ktc_token) + MAXKTCTICKETLEN;
629 if (maxLen < tktLen) {
634 /* set return values */
635 memcpy(atoken->ticket, stp, tktLen);
636 atoken->startTime = ct.BeginTimestamp;
637 atoken->endTime = ct.EndTimestamp;
638 if (ct.AuthHandle == -1) {
641 atoken->kvno = ct.AuthHandle;
642 memcpy(&atoken->sessionKey, ct.HandShakeKey,
643 sizeof(struct ktc_encryptionKey));
644 atoken->ticketLen = tktLen;
647 strcpy(aclient->cell, cellp);
648 aclient->instance[0] = 0;
650 if ((atoken->kvno == 999) || /* old style bcrypt ticket */
651 (ct.BeginTimestamp && /* new w/ prserver lookup */
652 (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1))) {
653 sprintf(aclient->name, "AFS ID %d", ct.ViceId);
655 sprintf(aclient->name, "Unix UID %d", ct.ViceId);
663 #endif /* NO_AFS_CLIENT */
666 if ((code < 0) && (errno == EINVAL))
668 return KTC_PIOCTLFAIL; /* probable cause */
672 * Forget tokens for this server and the calling user.
673 * NOT IMPLEMENTED YET!
675 #ifndef NO_AFS_CLIENT
676 ktc_ForgetToken(aserver)
677 struct ktc_principal *aserver;
682 TRY_KERNEL(KTC_FORGETTOKEN_OP, aserver, 0, 0, 0);
684 rc = ktc_ForgetAllTokens(); /* bogus, but better */
688 #endif /* NO_AFS_CLIENT */
690 /* ktc_ListTokens - list all tokens. start aprevIndex at 0, it returns the
691 * next rock in (*aindex). (*aserver) is set to the relevant ticket on
695 ktc_ListTokens(int aprevIndex,
697 struct ktc_principal *aserver)
699 struct ViceIoctl iob;
700 char tbuffer[MAXPIOCTLTOKENLEN];
701 register afs_int32 code;
703 afs_int32 temp, index;
705 memset(tbuffer, 0, sizeof(tbuffer));
708 #ifndef NO_AFS_CLIENT
709 TRY_KERNEL(KTC_LISTTOKENS_OP, aserver, aprevIndex, aindex, 0);
710 #endif /* NO_AFS_CLIENT */
716 #endif /* NO_AFS_CLIENT */
717 #ifdef AFS_KERBEROS_ENV
720 struct ktc_principal cprincipal;
721 struct ktc_token ctoken;
723 if (afs_tf_init(ktc_tkt_string(), R_TKT_FIL)
724 || afs_tf_get_pname(tbuffer) || afs_tf_get_pinst(tbuffer)) {
730 for (i = 214; i < index; i++) {
731 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
739 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
746 #ifndef NO_AFS_CLIENT
747 if (!strcmp(cprincipal.name, "afs") && cprincipal.instance[0] == 0) {
750 #endif /* NO_AFS_CLIENT */
752 for (i = 0; i < MAXLOCALTOKENS; i++) {
753 if (!strcmp(cprincipal.name, local_tokens[i].server.name)
754 && !strcmp(cprincipal.instance,
755 local_tokens[i].server.instance)
756 && !strcmp(cprincipal.cell, local_tokens[i].server.cell)) {
761 *aserver = cprincipal;
769 #ifndef NO_AFS_CLIENT
770 if (index >= 123) { /* special hack for returning TCS */
771 while (index - 123 < MAXLOCALTOKENS) {
772 if (local_tokens[index - 123].valid) {
773 *aserver = local_tokens[index - 123].server;
781 #ifdef AFS_KERBEROS_ENV
782 return ktc_ListTokens(214, aindex, aserver);
788 /* get tokens from the kernel */
789 while (index < 200) { /* sanity check in case pioctl fails */
790 iob.in = (char *)&index;
791 iob.in_size = sizeof(afs_int32);
793 iob.out_size = sizeof(tbuffer);
794 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
795 if (code < 0 && errno == EDOM) {
798 rc = ktc_ListTokens(123, aindex, aserver);
807 break; /* got a ticket */
808 /* otherwise we should skip this ticket slot */
815 return KTC_PIOCTLFAIL;
821 /* next iterator determined by earlier loop */
824 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of secret token */
825 tp += sizeof(afs_int32);
826 tp += temp; /* skip ticket for now */
827 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of clear token */
828 if (temp != sizeof(struct ClearToken)) {
832 tp += sizeof(afs_int32); /* skip length */
833 tp += temp; /* skip clear token itself */
834 tp += sizeof(afs_int32); /* skip primary flag */
835 /* tp now points to the cell name */
836 strcpy(aserver->cell, tp);
837 aserver->instance[0] = 0;
838 strcpy(aserver->name, "afs");
839 #endif /* NO_AFS_CLIENT */
844 /* discard all tokens from this user's cache */
849 #ifndef NO_AFS_CLIENT
850 TRY_KERNEL(KTC_FORGETALLTOKENS_OP, 0, 0, 0, 0);
851 #endif /* NO_AFS_CLIENT */
858 struct ViceIoctl iob;
859 register afs_int32 code;
862 for (i = 0; i < MAXLOCALTOKENS; i++)
863 local_tokens[i].valid = 0;
869 #ifndef NO_AFS_CLIENT
870 code = PIOCTL(0, VIOCUNPAG, &iob, 0);
872 return KTC_PIOCTLFAIL;
873 #endif /* NO_AFS_CLIENT */
878 ktc_ForgetAllTokens(void)
883 #ifdef AFS_KERBEROS_ENV
884 (void)afs_tf_dest_tkt();
887 ncode = NewForgetAll();
888 ocode = OldForgetAll();
889 if (ncode && ocode) {
892 else if (ocode == KTC_PIOCTLFAIL)
897 return KTC_PIOCTLFAIL;
903 /* ktc_OldPioctl - returns a boolean true if the kernel supports only the old
904 * pioctl interface for delivering AFS tickets to the cache manager. */
910 #ifdef KERNEL_KTC_COMPAT
912 rc = (kernelKTC != 1); /* old style interface */
921 #ifdef AFS_KERBEROS_ENV
923 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
925 * For copying and distribution information, please see the file
932 #include <sys/types.h>
933 #include <sys/stat.h>
934 #include <sys/file.h>
939 #define TF_LCK_RETRY ((unsigned)2) /* seconds to sleep before
940 * retry if ticket file is
944 * fd must be initialized to something that won't ever occur as a real
945 * file descriptor. Since open(2) returns only non-negative numbers as
946 * valid file descriptors, and afs_tf_init always stuffs the return value
947 * from open in here even if it is an error flag, we must
948 * a. Initialize fd to a negative number, to indicate that it is
949 * not initially valid.
950 * b. When checking for a valid fd, assume that negative values
951 * are invalid (ie. when deciding whether afs_tf_init has been
953 * c. In tf_close, be sure it gets reinitialized to a negative
957 static curpos; /* Position in tfbfr */
958 static lastpos; /* End of tfbfr */
959 static char tfbfr[BUFSIZ]; /* Buffer for ticket data */
961 static tf_gets(), tf_read();
964 * This file contains routines for manipulating the ticket cache file.
966 * The ticket file is in the following format:
968 * principal's name (null-terminated string)
969 * principal's instance (null-terminated string)
976 * Where "CREDENTIAL_x" consists of the following fixed-length
977 * fields from the CREDENTIALS structure (see "krb.h"):
979 * char service[MAXKTCNAMELEN]
980 * char instance[MAXKTCNAMELEN]
981 * char realm[REALM_SZ]
986 * afs_int32 issue_date
988 * Short description of routines:
990 * afs_tf_init() opens the ticket file and locks it.
992 * afs_tf_get_pname() returns the principal's name.
994 * afs_tf_get_pinst() returns the principal's instance (may be null).
996 * afs_tf_get_cred() returns the next CREDENTIALS record.
998 * afs_tf_save_cred() appends a new CREDENTIAL record to the ticket file.
1000 * afs_tf_close() closes the ticket file and releases the lock.
1002 * tf_gets() returns the next null-terminated string. It's an internal
1003 * routine used by afs_tf_get_pname(), afs_tf_get_pinst(), and
1004 * afs_tf_get_cred().
1006 * tf_read() reads a given number of bytes. It's an internal routine
1007 * used by afs_tf_get_cred().
1011 * afs_tf_init() should be called before the other ticket file routines.
1012 * It takes the name of the ticket file to use, "tf_name", and a
1013 * read/write flag "rw" as arguments.
1015 * It tries to open the ticket file, checks the mode, and if everything
1016 * is okay, locks the file. If it's opened for reading, the lock is
1017 * shared. If it's opened for writing, the lock is exclusive.
1019 * Returns 0 if all went well, otherwise one of the following:
1021 * NO_TKT_FIL - file wasn't there
1022 * TKT_FIL_ACC - file was in wrong mode, etc.
1023 * TKT_FIL_LCK - couldn't lock the file, even after a retry
1026 afs_tf_init(tf_name, rw)
1031 struct stat stat_buf;
1043 if (lstat(tf_name, &stat_buf) < 0)
1051 if ((stat_buf.st_uid != me && me != 0)
1052 || ((stat_buf.st_mode & S_IFMT) != S_IFREG))
1056 * If "wflag" is set, open the ticket file in append-writeonly mode
1057 * and lock the ticket file in exclusive mode. If unable to lock
1058 * the file, sleep and try again. If we fail again, return with the
1059 * proper error message.
1062 curpos = sizeof(tfbfr);
1065 fd = open(tf_name, O_RDWR, 0600);
1069 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1070 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1071 sleep(TF_LCK_RETRY);
1072 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1074 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1075 sleep(TF_LCK_RETRY);
1076 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1086 * Otherwise "wflag" is not set and the ticket file should be opened
1087 * for read-only operations and locked for shared access.
1090 fd = open(tf_name, O_RDONLY, 0600);
1094 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1095 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1096 sleep(TF_LCK_RETRY);
1097 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1099 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1100 sleep(TF_LCK_RETRY);
1101 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1112 * afs_tf_get_pname() reads the principal's name from the ticket file. It
1113 * should only be called after afs_tf_init() has been called. The
1114 * principal's name is filled into the "p" parameter. If all goes well,
1115 * 0 is returned. If afs_tf_init() wasn't called, TKT_FIL_INI is
1116 * returned. If the name was null, or EOF was encountered, or the name
1117 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned.
1126 if (tf_gets(p, MAXKTCNAMELEN) < 2) /* can't be just a null */
1132 * afs_tf_get_pinst() reads the principal's instance from a ticket file.
1133 * It should only be called after afs_tf_init() and afs_tf_get_pname() have
1134 * been called. The instance is filled into the "inst" parameter. If all
1135 * goes well, 0 is returned. If afs_tf_init() wasn't called,
1136 * TKT_FIL_INI is returned. If EOF was encountered, or the instance
1137 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned. Note that the
1138 * instance may be null.
1141 afs_tf_get_pinst(inst)
1147 if (tf_gets(inst, MAXKTCNAMELEN) < 1)
1153 * afs_tf_get_cred() reads a CREDENTIALS record from a ticket file and fills
1154 * in the given structure "c". It should only be called after afs_tf_init(),
1155 * afs_tf_get_pname(), and afs_tf_get_pinst() have been called. If all goes
1156 * well, 0 is returned. Possible error codes are:
1158 * TKT_FIL_INI - afs_tf_init wasn't called first
1159 * TKT_FIL_FMT - bad format
1160 * EOF - end of file encountered
1163 afs_tf_get_cred(principal, token)
1164 struct ktc_principal *principal;
1165 struct ktc_token *token;
1169 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1174 if ((k_errno = tf_gets(principal->name, MAXKTCNAMELEN)) < 2)
1177 case 1: /* can't be just a null */
1182 if ((k_errno = tf_gets(principal->instance, MAXKTCNAMELEN)) < 1)
1189 if ((k_errno = tf_gets(principal->cell, MAXKTCREALMLEN)) < 2)
1192 case 1: /* can't be just a null */
1197 lcstring(principal->cell, principal->cell, MAXKTCREALMLEN);
1198 if (tf_read((char *)&(token->sessionKey), 8) < 1
1199 || tf_read((char *)&(lifetime), sizeof(lifetime)) < 1
1200 || tf_read((char *)&(kvno), sizeof(kvno)) < 1
1201 || tf_read((char *)&(token->ticketLen), sizeof(token->ticketLen))
1203 /* don't try to read a silly amount into ticket->dat */
1204 token->ticketLen > MAXKTCTICKETLEN
1205 || tf_read((char *)(token->ticket), token->ticketLen) < 1
1206 || tf_read((char *)&mit_compat, sizeof(mit_compat)) < 1) {
1209 token->startTime = mit_compat;
1210 token->endTime = life_to_time(token->startTime, lifetime);
1216 * tf_close() closes the ticket file and sets "fd" to -1. If "fd" is
1217 * not a valid file descriptor, it just returns. It also clears the
1218 * buffer used to read tickets.
1220 * The return value is not defined.
1226 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1227 (void)fcntl(fd, F_SETLK, &fileUlock);
1229 (void)flock(fd, LOCK_UN);
1232 fd = -1; /* see declaration of fd above */
1234 memset(tfbfr, 0, sizeof(tfbfr));
1238 * tf_gets() is an internal routine. It takes a string "s" and a count
1239 * "n", and reads from the file until either it has read "n" characters,
1240 * or until it reads a null byte. When finished, what has been read exists
1243 * Possible return values are:
1245 * n the number of bytes read (including null terminator)
1246 * when all goes well
1248 * 0 end of file or read error
1250 * TOO_BIG if "count" characters are read and no null is
1251 * encountered. This is an indication that the ticket
1252 * file is seriously ill.
1264 for (count = n - 1; count > 0; --count) {
1265 if (curpos >= sizeof(tfbfr)) {
1266 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1269 if (curpos == lastpos) {
1272 *s = tfbfr[curpos++];
1280 * tf_read() is an internal routine. It takes a string "s" and a count
1281 * "n", and reads from the file until "n" bytes have been read. When
1282 * finished, what has been read exists in "s".
1284 * Possible return values are:
1286 * n the number of bytes read when all goes well
1288 * 0 on end of file or read error
1298 for (count = n; count > 0; --count) {
1299 if (curpos >= sizeof(tfbfr)) {
1300 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1303 if (curpos == lastpos) {
1306 *s++ = tfbfr[curpos++];
1314 * afs_tf_save_cred() appends an incoming ticket to the end of the ticket
1315 * file. You must call afs_tf_init() before calling afs_tf_save_cred().
1317 * The "service", "instance", and "realm" arguments specify the
1318 * server's name; "aticket" contains the credential.
1320 * Returns 0 if all goes well, TKT_FIL_INI if afs_tf_init() wasn't
1321 * called previously, and KFAILURE for anything else that went wrong.
1324 afs_tf_save_cred(aserver, atoken, aclient)
1325 struct ktc_principal *aserver;
1326 struct ktc_principal *aclient;
1327 struct ktc_token *atoken; /* Token */
1329 char realm[MAXKTCREALMLEN + 1];
1330 char junk[MAXKTCNAMELEN];
1331 struct ktc_principal principal;
1332 struct ktc_token token;
1336 int count; /* count for write */
1337 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1339 if (fd < 0) { /* fd is ticket file as set by afs_tf_init */
1343 ucstring(realm, aserver->cell, MAXKTCREALMLEN);
1344 realm[MAXKTCREALMLEN] = '\0';
1346 /* Look for a duplicate ticket */
1347 (void)lseek(fd, (off_t) 0L, 0);
1348 curpos = sizeof(tfbfr);
1350 if (afs_tf_get_pname(junk) || strcmp(junk, aclient->name)
1351 || afs_tf_get_pinst(junk) || strcmp(junk, aclient->instance))
1355 start = lseek(fd, (off_t) 0L, 1) - lastpos + curpos;
1356 status = afs_tf_get_cred(&principal, &token);
1357 } while (status == 0
1358 && (strcmp(aserver->name, principal.name) != 0
1359 || strcmp(aserver->instance, principal.instance) != 0
1360 || strcmp(aserver->cell, principal.cell) != 0));
1363 * Two tickets for the same user authenticating to the same service
1364 * should be the same length, but we check here just to make sure.
1366 if (status == 0 && token.ticketLen != atoken->ticketLen)
1368 if (status && status != EOF)
1371 /* Position over the credential we just matched (or the EOF) */
1372 lseek(fd, start, 0);
1373 curpos = lastpos = sizeof(tfbfr);
1375 /* Write the ticket and associated data */
1377 count = strlen(aserver->name) + 1;
1378 if (write(fd, aserver->name, count) != count)
1381 count = strlen(aserver->instance) + 1;
1382 if (write(fd, aserver->instance, count) != count)
1385 count = strlen(realm) + 1;
1386 if (write(fd, realm, count) != count)
1389 if (write(fd, (char *)&atoken->sessionKey, 8) != 8)
1392 lifetime = time_to_life(atoken->startTime, atoken->endTime);
1393 if (write(fd, (char *)&lifetime, sizeof(int)) != sizeof(int))
1396 kvno = atoken->kvno;
1397 if (write(fd, (char *)&kvno, sizeof(int)) != sizeof(int))
1400 if (write(fd, (char *)&(atoken->ticketLen), sizeof(int)) != sizeof(int))
1403 count = atoken->ticketLen;
1404 if (write(fd, atoken->ticket, count) != count)
1407 mit_compat = atoken->startTime;
1408 if (write(fd, (char *)&mit_compat, sizeof(mit_compat))
1409 != sizeof(mit_compat))
1412 /* Actually, we should check each write for success */
1419 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
1422 * For copying and distribution information, please see the file
1423 * <mit-copyright.h>.
1429 * This routine is used to generate the name of the file that holds
1430 * the user's cache of server tickets and associated session keys.
1432 * If it is set, krb_ticket_string contains the ticket file name.
1433 * Otherwise, the filename is constructed as follows:
1435 * If it is set, the environment variable "KRBTKFILE" will be used as
1436 * the ticket file name. Otherwise TKT_ROOT (defined in "krb.h") and
1437 * the user's uid are concatenated to produce the ticket file name
1438 * (e.g., "/tmp/tkt123"). A pointer to the string containing the ticket
1439 * file name is returned.
1442 static char krb_ticket_string[4096] = "";
1445 ktc_tkt_string(void)
1447 return ktc_tkt_string_uid(getuid());
1451 ktc_tkt_string_uid(afs_uint32 uid)
1456 if (!*krb_ticket_string) {
1457 if (env = getenv("KRBTKFILE")) {
1458 (void)strncpy(krb_ticket_string, env,
1459 sizeof(krb_ticket_string) - 1);
1460 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1462 /* 32 bits of signed integer will always fit in 11 characters
1463 * (including the sign), so no need to worry about overflow */
1464 (void)sprintf(krb_ticket_string, "%s%d", TKT_ROOT, uid);
1467 UNLOCK_GLOBAL_MUTEX;
1468 return krb_ticket_string;
1472 * This routine is used to set the name of the file that holds the user's
1473 * cache of server tickets and associated session keys.
1475 * The value passed in is copied into local storage.
1477 * NOTE: This routine should be called during initialization, before other
1478 * Kerberos routines are called; otherwise tkt_string() above may be called
1479 * and return an undesired ticket file name until this routine is called.
1483 ktc_set_tkt_string(char * val)
1487 (void)strncpy(krb_ticket_string, val, sizeof(krb_ticket_string) - 1);
1488 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1489 UNLOCK_GLOBAL_MUTEX;
1494 * tf_create() is used to initialize the ticket store. It creates the
1495 * file to contain the tickets and writes the given user's name "pname"
1496 * and instance "pinst" in the file. in_tkt() returns KSUCCESS on
1497 * success, or KFAILURE if something goes wrong.
1500 afs_tf_create(pname, pinst)
1507 char *file = ktc_tkt_string();
1516 if (lstat(file, &sbuf) == 0) {
1517 if ((sbuf.st_uid != me && me != 0)
1518 || ((sbuf.st_mode & S_IFMT) != S_IFREG) || sbuf.st_mode & 077) {
1521 /* file already exists, and permissions appear ok, so nuke it */
1522 if ((fd = open(file, O_RDWR, 0)) < 0)
1523 goto out; /* can't zero it, but we can still try truncating it */
1525 memset(zerobuf, 0, sizeof(zerobuf));
1527 for (i = 0; i < sbuf.st_size; i += sizeof(zerobuf))
1528 if (write(fd, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf)) {
1539 /* arrange so the file is owned by the ruid
1540 * (swap real & effective uid if necessary).
1541 * This isn't a security problem, since the ticket file, if it already
1542 * exists, has the right uid (== ruid) and mode. */
1544 if (setreuid(metoo, me) < 0) {
1548 tktfile = creat(file, 0600);
1550 if (setreuid(me, metoo) < 0) {
1551 /* can't switch??? fail! */
1558 count = strlen(pname) + 1;
1559 if (write(tktfile, pname, count) != count) {
1560 (void)close(tktfile);
1563 count = strlen(pinst) + 1;
1564 if (write(tktfile, pinst, count) != count) {
1565 (void)close(tktfile);
1568 (void)close(tktfile);
1573 * dest_tkt() is used to destroy the ticket store upon logout.
1574 * If the ticket file does not exist, dest_tkt() returns RET_TKFIL.
1575 * Otherwise the function returns 0 on success, KFAILURE on
1581 char *file = ktc_tkt_string();
1587 if (lstat(file, &statb) < 0)
1590 if (!(statb.st_mode & S_IFREG))
1593 if ((fd = open(file, O_RDWR, 0)) < 0)
1596 memset(buf, 0, BUFSIZ);
1598 for (i = 0; i < statb.st_size; i += BUFSIZ)
1599 if (write(fd, buf, BUFSIZ) != BUFSIZ) {
1611 if (errno == ENOENT)
1613 else if (errno != 0)
1621 #if defined(AFS_AIX51_ENV)
1624 if (get_pag(PAG_AFS, &pag) < 0 || pag == 0)
1628 gid_t groups[NGROUPS_MAX];
1630 afs_uint32 h, l, ret;
1632 if (getgroups(sizeof groups / sizeof groups[0], groups) < 2)
1635 g0 = groups[0] & 0xffff;
1636 g1 = groups[1] & 0xffff;
1639 if (g0 < 0xc000 && g1 < 0xc000) {
1640 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
1642 h = (g1 >> 14) + h + h + h;
1643 ret = ((h << 28) | l);
1644 /* Additional testing */
1645 if (((ret >> 24) & 0xff) == 'A')
1657 extern char **environ;
1661 char fname[256], *prefix = "/ticket/";
1663 char **newenv, **senv, **denv;
1666 if (stat("/ticket", &sbuf) == -1) {
1667 prefix = "/tmp/tkt";
1670 pag = curpag() & 0xffffffff;
1672 sprintf(fname, "%s%d", prefix, getuid());
1674 sprintf(fname, "%sp%ld", prefix, pag);
1676 ktc_set_tkt_string(fname);
1678 for (senv = environ, numenv = 0; *senv; senv++)
1680 newenv = (char **)malloc((numenv + 2) * sizeof(char *));
1682 for (senv = environ, denv = newenv; *senv; *senv++) {
1683 if (strncmp(*senv, "KRBTKFILE=", 10) != 0)
1687 *denv = (char *)malloc(10 + strlen(fname) + 1);
1688 strcpy(*denv, "KRBTKFILE=");
1689 strcat(*denv, fname);
1692 UNLOCK_GLOBAL_MUTEX;
1696 * BLETCH! We have to invoke the entire afsconf package just to
1697 * find out what the local cell is.
1703 struct afsconf_dir *conf;
1705 if ((conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))
1706 || (conf = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH))) {
1707 code = afsconf_GetLocalCell(conf, lcell, sizeof(lcell));
1708 afsconf_Close(conf);
1710 if (!conf || code) {
1711 printf("** Can't determine local cell name!\n");