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 */
14 #include "../afs/param.h"
15 #include "../afs/sysincludes.h"
16 #include "../afs/afsincludes.h"
17 #include "../afs/stds.h"
18 #include "../afs/pthread_glock.h"
19 #include "../afs/vice.h"
20 #include "../afs/auth.h"
21 #include "../afs/venus.h"
22 #include "../afs/pthread_glock.h"
23 #include "../afs/dirpath.h"
26 #define min(a,b) ((a)<(b)?(a):(b))
27 #endif /* !defined(min) */
29 #else /* defined(UKERNEL) */
34 #include <afs/param.h>
37 #include <afs/pthread_glock.h>
38 #include <sys/types.h>
43 #include <sys/ioctl.h>
44 #include <netinet/in.h>
47 #include <sys/lockf.h>
50 #include <afs/venus.h>
51 #include <afs/afsutil.h>
53 #endif /* defined(UKERNEL) */
60 /* AFS_KERBEROS_ENV is now conditionally defined in the Makefile */
61 #define AFS_KERBEROS_ENV
64 #ifdef AFS_KERBEROS_ENV
67 extern afs_uint32 life_to_time();
68 extern unsigned char time_to_life();
69 #include "cellconfig.h"
70 static char lcell[MAXCELLCHARS];
72 #define TKT_ROOT "/tmp/tkt"
77 /* Definitions for ticket file utilities */
81 /* Error codes returned by ticket file utilities */
82 #define NO_TKT_FIL 76 /* No ticket file found */
83 #define TKT_FIL_ACC 77 /* Couldn't access tkt file */
84 #define TKT_FIL_LCK 78 /* Couldn't lock ticket file */
85 #define TKT_FIL_FMT 79 /* Bad ticket file format */
86 #define TKT_FIL_INI 80 /* tf_init not called first */
88 /* Values returned by get_credentials */
89 #define RET_TKFIL 21 /* Can't read ticket file */
98 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
99 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0, 0 };
100 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0, 0 };
101 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0, 0 };
104 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0 };
105 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0 };
106 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0 };
113 static int tf_init(), tf_get_pname(), tf_get_pinst(), tf_get_cred(), tf_save_cred();
114 static int tf_close();
115 static int tf_create(), tf_dest_tkt(), ktc_LocalCell();
116 char *ktc_tkt_string();
117 #endif /* AFS_KERBEROS_ENV */
120 #define PIOCTL afs_pioctl
121 #elif defined(UKERNEL)
122 #define PIOCTL(A,B,C,D) call_syscall(AFSCALL_PIOCTL,A,B,C,D)
124 #define PIOCTL pioctl
128 #ifdef KERNEL_KTC_COMPAT
130 /* In case there is no include file. */
134 #define KTC_SYSCALL 32
137 /* Kernel call opcode definitions */
138 #define KTC_OPCODE_BASE 4300
139 #define KTC_NO_OP (0+KTC_OPCODE_BASE)
140 #define KTC_SETTOKEN_OP (1+KTC_OPCODE_BASE)
141 #define KTC_GETTOKEN_OP (2+KTC_OPCODE_BASE)
142 #define KTC_LISTTOKENS_OP (3+KTC_OPCODE_BASE)
143 #define KTC_FORGETTOKEN_OP (4+KTC_OPCODE_BASE)
144 #define KTC_FORGETALLTOKENS_OP (5+KTC_OPCODE_BASE)
145 #define KTC_STATUS_OP (6+KTC_OPCODE_BASE)
146 #define KTC_GC_OP (7+KTC_OPCODE_BASE)
148 #define KTC_INTERFACE_VERSION 3
154 /* We have to determine if the kernel supports the ktc system call. To do so
155 * we attempt to execute its noop function. If this is successful we use the
156 * kernel calls in the future otherwise we let the old code run. */
158 /* To safely check to see whether a system call exists we have to intercept the
159 * SIGSYS signal which is caused by executing a non-existant system call. If
160 * it is ignored the syscall routine returns EINVAL. The SIGSYS is reset to
161 * its old value after the return from syscall. */
163 static int kernelKTC = 0;
165 #ifdef AFS_DECOSF_ENV
167 * SIGSYS semantics are broken on Dec AXP OSF/1 v1.2 systems. You need
168 * to ignore SIGTRAP too. It is claimed to be fixed under v1.3, but...
171 #define CHECK_KERNEL \
172 if (kernelKTC == 0) { \
176 old = (int (*)())signal(SIGSYS, SIG_IGN); \
177 old_t = (int (*)())signal(SIGTRAP, SIG_IGN); \
178 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
180 signal(SIGSYS, old); \
181 signal(SIGTRAP, old_t); \
182 if (code == 0) kernelKTC = 1; \
183 else kernelKTC = 2; \
184 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
187 #else /* AFS_DECOSF_ENV */
189 #define CHECK_KERNEL \
190 if (kernelKTC == 0) { \
193 old = (int (*)())signal(SIGSYS, SIG_IGN); \
194 code = syscall (KTC_SYSCALL, KTC_NO_OP, 0,0,0,0,0); \
196 signal(SIGSYS, old); \
197 if (code == 0) kernelKTC = 1; \
198 else kernelKTC = 2; \
199 /* printf ("returned from KTC_NO_OP kernelKTC <= %d; code=%d, errno=%d\n", kernelKTC, code, errno); */\
201 #endif /* AFS_DECOSF_ENV */
203 #define TRY_KERNEL(cmd,a1,a2,a3,a4) \
205 if (kernelKTC == 1) \
206 return syscall (KTC_SYSCALL, cmd, \
207 KTC_INTERFACE_VERSION, a1,a2,a3,a4); \
211 #define TRY_KERNEL(cmd,a1,a2,a3,a4)
212 #endif /* KERNEL_KTC_COMPAT */
214 #if !defined(UKERNEL)
215 /* this is a structure used to communicate with the afs cache mgr, but is
216 * otherwise irrelevant */
218 afs_int32 AuthHandle;
219 char HandShakeKey[8];
221 afs_int32 BeginTimestamp;
222 afs_int32 EndTimestamp;
224 #endif /* !defined(UKERNEL) */
226 #define MAXLOCALTOKENS 4
230 struct ktc_principal server;
231 struct ktc_principal client;
232 struct ktc_token token;
233 } local_tokens[MAXLOCALTOKENS] = {{0}, {0}, {0}, {0}};
235 /* new interface routines to the ticket cache. Only handle afs service right
238 static int NewSetToken (aserver, atoken, aclient, flags)
239 struct ktc_principal *aserver;
240 struct ktc_principal *aclient;
241 struct ktc_token *atoken;
244 TRY_KERNEL (KTC_SETTOKEN_OP,
245 aserver, aclient, atoken, sizeof(struct ktc_token));
246 /* no kernel ticket cache */
250 static int OldSetToken (aserver, atoken, aclient, flags)
251 struct ktc_principal *aserver, *aclient;
252 struct ktc_token *atoken;
255 struct ViceIoctl iob;
258 struct ClearToken ct;
259 register afs_int32 code;
262 if (strcmp(aserver->name, "afs") != 0) {
265 for (i=0; i<MAXLOCALTOKENS; i++)
266 if (local_tokens[i].valid) {
267 if ((strcmp (local_tokens[i].server.name, aserver->name) == 0) &&
268 (strcmp (local_tokens[i].server.instance, aserver->instance) == 0) &&
269 (strcmp (local_tokens[i].server.cell, aserver->cell) == 0)) {
270 found = i; /* replace existing entry */
273 else /* valid, but no match */ ;
274 } else found = i; /* remember this empty slot */
275 if (found == -1) return KTC_NOENT;
276 bcopy (atoken, &local_tokens[found].token, sizeof(struct ktc_token));
277 local_tokens[found].server = *aserver;
278 local_tokens[found].client = *aclient;
279 local_tokens[found].valid = 1;
282 tp = tbuffer; /* start copying here */
283 if ((atoken->ticketLen < MINKTCTICKETLEN) ||
284 (atoken->ticketLen > MAXKTCTICKETLEN)) return KTC_TOOBIG;
285 bcopy(&atoken->ticketLen, tp, sizeof(afs_int32)); /* copy in ticket length */
286 tp += sizeof(afs_int32);
287 bcopy(atoken->ticket, tp, atoken->ticketLen); /* copy in ticket */
288 tp += atoken->ticketLen;
289 /* next, copy in the "clear token", describing who we are */
290 ct.AuthHandle = atoken->kvno; /* hide auth handle here */
291 bcopy(&atoken->sessionKey, ct.HandShakeKey, 8);
293 ct.BeginTimestamp = atoken->startTime;
294 ct.EndTimestamp = atoken->endTime;
295 if (ct.BeginTimestamp == 0) ct.BeginTimestamp = 1;
297 if ((strlen(aclient->name) > strlen ("AFS ID ")) &&
298 (aclient->instance[0] == 0)) {
300 afs_int32 viceId = 0;
301 char *cp = aclient->name + strlen ("AFS ID ");
302 if (*cp == '-') { sign = -1; cp++; }
304 if (isdigit(*cp)) viceId = viceId*10 + (int)(*cp - '0');
305 else goto not_vice_id;
308 ct.ViceId = viceId * sign; /* OK to let any value here? */
309 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 0)
310 ct.BeginTimestamp++; /* force lifetime to be odd */
313 ct.ViceId = getuid(); /* wrong, but works in primary cell */
314 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1)
315 ct.BeginTimestamp++; /* force lifetime to be even */
320 * Information needed by the user space cache manager
322 u.u_expiration = ct.EndTimestamp;
323 u.u_viceid = ct.ViceId;
326 temp = sizeof(struct ClearToken);
327 bcopy(&temp, tp, sizeof(afs_int32));
328 tp += sizeof(afs_int32);
329 bcopy(&ct, tp, sizeof(struct ClearToken));
330 tp += sizeof(struct ClearToken);
332 /* next copy in primary flag */
336 * The following means that setpag will happen inside afs just before
337 * the authentication to prevent the setpag/klog race condition.
339 * The following means that setpag will affect the parent process as
340 * well as the current process.
342 if (flags & AFS_SETTOK_SETPAG)
345 bcopy(&temp, tp, sizeof(afs_int32));
346 tp += sizeof(afs_int32);
348 /* finally copy in the cell name */
349 temp = strlen(aserver->cell);
350 if (temp >= MAXKTCREALMLEN) return KTC_TOOBIG;
351 strcpy(tp, aserver->cell);
354 /* now setup for the pioctl */
356 iob.in_size = tp-tbuffer;
358 iob.out_size = sizeof(tbuffer);
360 code = PIOCTL(0, VIOCSETTOK, &iob, 0);
361 if (code) return KTC_PIOCTLFAIL;
366 ktc_SetToken (aserver, atoken, aclient, flags)
367 struct ktc_principal *aserver;
368 struct ktc_principal *aclient;
369 struct ktc_token *atoken;
375 #ifdef AFS_KERBEROS_ENV
376 if (!lcell[0]) ktc_LocalCell();
378 if (/*!strcmp(aclient->cell, lcell) && this would only store local creds*/
379 (strcmp(aserver->name, "AuthServer") ||
380 strcmp(aserver->instance, "Admin"))){
381 if (strcmp(aserver->name, "krbtgt") == 0) {
382 static char lrealm[MAXKTCREALMLEN];
384 if (!lrealm[0]) ucstring(lrealm, lcell, MAXKTCREALMLEN);
385 if (strcmp(aserver->instance, lrealm) == 0) {
386 tf_create(aclient->name, aclient->instance);
390 ncode = tf_init(ktc_tkt_string(), W_TKT_FIL);
391 if (ncode == NO_TKT_FIL) {
392 (void) tf_create(aclient->name, aclient->instance);
393 ncode = tf_init(ktc_tkt_string(), W_TKT_FIL);
397 tf_save_cred(aserver, atoken, aclient);
403 ncode = NewSetToken (aserver, atoken, aclient, flags);
404 if (ncode || /* new style failed */
405 (strcmp (aserver->name, "afs") == 0)) { /* for afs tokens do both */
406 ocode = OldSetToken (aserver, atoken, aclient, flags);
408 if (ncode && ocode) {
410 if (ocode == -1) ocode = errno;
411 else if (ocode == KTC_PIOCTLFAIL) ocode = errno;
412 if (ocode == ESRCH) return KTC_NOCELL;
413 if (ocode == EINVAL) return KTC_NOPIOCTL;
414 if (ocode == EIO) return KTC_NOCM;
415 return KTC_PIOCTLFAIL;
421 /* get token, given server we need and token buffer. aclient will eventually
422 * be set to our identity to the server.
424 ktc_GetToken(aserver, atoken, atokenLen, aclient)
425 struct ktc_principal *aserver, *aclient;
427 struct ktc_token *atoken; {
428 struct ViceIoctl iob;
430 register afs_int32 code;
432 char *stp, *cellp; /* secret token ptr */
433 struct ClearToken ct;
436 int maxLen; /* biggest ticket we can copy */
437 int tktLen; /* server ticket length */
440 TRY_KERNEL (KTC_GETTOKEN_OP, aserver, aclient, atoken, atokenLen);
442 #ifdef AFS_KERBEROS_ENV
443 if (!lcell[0]) ktc_LocalCell();
445 if (strcmp(aserver->name, "afs") != 0) {
447 /* try the local tokens */
448 for (i=0; i<MAXLOCALTOKENS; i++)
449 if (local_tokens[i].valid &&
450 (strcmp (local_tokens[i].server.name, aserver->name) == 0) &&
451 (strcmp (local_tokens[i].server.instance, aserver->instance) == 0) &&
452 (strcmp (local_tokens[i].server.cell, aserver->cell) == 0)) {
453 bcopy (&local_tokens[i].token, atoken, min (atokenLen, sizeof(struct ktc_token)));
454 *aclient = local_tokens[i].client;
458 #ifdef AFS_KERBEROS_ENV
459 if (!tf_init(ktc_tkt_string(), R_TKT_FIL) && !tf_get_pname(aclient->name) &&
460 !tf_get_pinst(aclient->instance)) {
461 struct ktc_principal cprincipal;
462 struct ktc_token ctoken;
464 while (!tf_get_cred(&cprincipal, &ctoken)) {
465 if (strcmp(cprincipal.name, aserver->name) == 0 &&
466 strcmp(cprincipal.instance, aserver->instance) == 0 &&
467 strcmp(cprincipal.cell, aserver->cell) == 0) {
469 strcpy(aclient->cell, lcell);
470 bcopy(&ctoken, atoken,
471 min (atokenLen, sizeof(struct ktc_token)));
485 for (index=0; index<200; index++) { /* sanity check in case pioctl fails */
486 iob.in = (char *) &index;
487 iob.in_size = sizeof(afs_int32);
489 iob.out_size = sizeof(tbuffer);
491 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
494 /* failed to retrieve specified token */
495 if (code < 0 && errno == EDOM) {
500 /* token retrieved; parse buffer */
503 /* get ticket length */
504 memcpy(&temp, tp, sizeof(afs_int32));
506 tp += sizeof(afs_int32);
508 /* remember where ticket is and skip over it */
512 /* get size of clear token and verify */
513 memcpy(&temp, tp, sizeof(afs_int32));
514 if (temp != sizeof(struct ClearToken)) {
518 tp += sizeof(afs_int32);
520 /* copy clear token */
521 memcpy(&ct, tp, temp);
524 /* skip over primary flag */
525 tp += sizeof(afs_int32);
527 /* remember where cell name is */
530 if ((strcmp(cellp, aserver->cell) == 0)
531 #ifdef AFS_KERBEROS_ENV
532 || (*aserver->cell == '\0' && strcmp(cellp, lcell) == 0)
535 /* got token for cell; check that it will fit */
536 maxLen = atokenLen - sizeof(struct ktc_token) + MAXKTCTICKETLEN;
537 if (maxLen < tktLen) {
542 /* set return values */
543 memcpy(atoken->ticket, stp, tktLen);
544 atoken->startTime = ct.BeginTimestamp;
545 atoken->endTime = ct.EndTimestamp;
546 if (ct.AuthHandle == -1) {
549 atoken->kvno = ct.AuthHandle;
550 memcpy(&atoken->sessionKey, ct.HandShakeKey, sizeof(struct ktc_encryptionKey));
551 atoken->ticketLen = tktLen;
554 strcpy(aclient->cell, cellp);
555 aclient->instance[0] = 0;
557 if ((atoken->kvno == 999) || /* old style bcrypt ticket */
558 (ct.BeginTimestamp && /* new w/ prserver lookup */
559 (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1))) {
560 sprintf(aclient->name, "AFS ID %d", ct.ViceId);
562 sprintf(aclient->name, "Unix UID %d", ct.ViceId);
572 if ((code < 0) && (errno == EINVAL)) return KTC_NOPIOCTL;
573 return KTC_PIOCTLFAIL; /* probable cause */
577 * Forget tokens for this server and the calling user.
578 * NOT IMPLEMENTED YET!
580 ktc_ForgetToken(aserver)
581 struct ktc_principal *aserver; {
585 TRY_KERNEL (KTC_FORGETTOKEN_OP, aserver, 0,0,0);
587 rc = ktc_ForgetAllTokens(); /* bogus, but better */
592 /* ktc_ListTokens - list all tokens. start aprevIndex at 0, it returns the
593 * next rock in (*aindex). (*aserver) is set to the relevant ticket on
596 ktc_ListTokens(aprevIndex, aindex, aserver)
597 int aprevIndex, *aindex;
598 struct ktc_principal *aserver; {
599 struct ViceIoctl iob;
601 register afs_int32 code;
603 afs_int32 temp, index;
606 TRY_KERNEL (KTC_LISTTOKENS_OP, aserver, aprevIndex, aindex, 0);
609 #ifdef AFS_KERBEROS_ENV
612 struct ktc_principal cprincipal;
613 struct ktc_token ctoken;
615 if (tf_init(ktc_tkt_string(), R_TKT_FIL) ||
616 tf_get_pname(tbuffer) ||
617 tf_get_pinst(tbuffer)) {
623 for (i=214; i<index; i++) {
624 if (tf_get_cred(&cprincipal, &ctoken)) {
632 if (tf_get_cred(&cprincipal, &ctoken)) {
639 if (!strcmp(cprincipal.name, "afs") && cprincipal.instance[0]==0) {
643 for (i=0; i < MAXLOCALTOKENS; i++) {
644 if (!strcmp(cprincipal.name, local_tokens[i].server.name) &&
645 !strcmp(cprincipal.instance, local_tokens[i].server.instance) &&
646 !strcmp(cprincipal.cell, local_tokens[i].server.cell)) {
651 *aserver = cprincipal;
659 if (index >= 123) { /* special hack for returning TCS */
660 while (index-123 < MAXLOCALTOKENS) {
661 if (local_tokens[index-123].valid) {
662 *aserver = local_tokens[index-123].server;
670 #ifdef AFS_KERBEROS_ENV
671 return ktc_ListTokens(214, aindex, aserver);
677 /* get tokens from the kernel */
678 while (index<200) { /* sanity check in case pioctl fails */
679 iob.in = (char *) &index;
680 iob.in_size = sizeof(afs_int32);
682 iob.out_size = sizeof(tbuffer);
683 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
684 if (code < 0 && errno == EDOM) {
687 rc = ktc_ListTokens (123, aindex, aserver);
696 if (code == 0) break; /* got a ticket */
697 /* otherwise we should skip this ticket slot */
702 if (errno == EINVAL) return KTC_NOPIOCTL;
703 return KTC_PIOCTLFAIL;
709 /* next iterator determined by earlier loop */
712 bcopy(tp, &temp, sizeof(afs_int32)); /* get size of secret token */
713 tp += sizeof(afs_int32);
714 tp += temp; /* skip ticket for now */
715 bcopy(tp, &temp, sizeof(afs_int32)); /* get size of clear token */
716 if (temp != sizeof(struct ClearToken)) {
720 tp += sizeof(afs_int32); /* skip length */
721 tp += temp; /* skip clear token itself */
722 tp += sizeof(afs_int32); /* skip primary flag */
723 /* tp now points to the cell name */
724 strcpy(aserver->cell, tp);
725 aserver->instance[0] = 0;
726 strcpy(aserver->name, "afs");
731 /* discard all tokens from this user's cache */
733 static int NewForgetAll ()
735 TRY_KERNEL (KTC_FORGETALLTOKENS_OP, 0,0,0,0);
739 static int OldForgetAll ()
741 struct ViceIoctl iob;
742 register afs_int32 code;
745 for (i=0; i<MAXLOCALTOKENS; i++) local_tokens[i].valid = 0;
751 code = PIOCTL(0, VIOCUNPAG, &iob, 0);
752 if (code) return KTC_PIOCTLFAIL;
756 int ktc_ForgetAllTokens()
761 #ifdef AFS_KERBEROS_ENV
762 (void) tf_dest_tkt();
765 ncode = NewForgetAll ();
766 ocode = OldForgetAll ();
767 if (ncode && ocode) {
768 if (ocode == -1) ocode = errno;
769 else if (ocode == KTC_PIOCTLFAIL) ocode = errno;
771 if (ocode == EINVAL) return KTC_NOPIOCTL;
772 return KTC_PIOCTLFAIL;
778 /* ktc_OldPioctl - returns a boolean true if the kernel supports only the old
779 * pioctl interface for delivering AFS tickets to the cache manager. */
785 #ifdef KERNEL_KTC_COMPAT
787 rc = (kernelKTC != 1); /* old style interface */
796 #ifdef AFS_KERBEROS_ENV
798 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
800 * For copying and distribution information, please see the file
807 #include <sys/types.h>
808 #include <sys/stat.h>
809 #include <sys/file.h>
814 #define TF_LCK_RETRY ((unsigned)2) /* seconds to sleep before
815 * retry if ticket file is
819 * fd must be initialized to something that won't ever occur as a real
820 * file descriptor. Since open(2) returns only non-negative numbers as
821 * valid file descriptors, and tf_init always stuffs the return value
822 * from open in here even if it is an error flag, we must
823 * a. Initialize fd to a negative number, to indicate that it is
824 * not initially valid.
825 * b. When checking for a valid fd, assume that negative values
826 * are invalid (ie. when deciding whether tf_init has been
828 * c. In tf_close, be sure it gets reinitialized to a negative
832 static curpos; /* Position in tfbfr */
833 static lastpos; /* End of tfbfr */
834 static char tfbfr[BUFSIZ]; /* Buffer for ticket data */
836 static tf_gets(), tf_read();
839 * This file contains routines for manipulating the ticket cache file.
841 * The ticket file is in the following format:
843 * principal's name (null-terminated string)
844 * principal's instance (null-terminated string)
851 * Where "CREDENTIAL_x" consists of the following fixed-length
852 * fields from the CREDENTIALS structure (see "krb.h"):
854 * char service[MAXKTCNAMELEN]
855 * char instance[MAXKTCNAMELEN]
856 * char realm[REALM_SZ]
861 * afs_int32 issue_date
863 * Short description of routines:
865 * tf_init() opens the ticket file and locks it.
867 * tf_get_pname() returns the principal's name.
869 * tf_get_pinst() returns the principal's instance (may be null).
871 * tf_get_cred() returns the next CREDENTIALS record.
873 * tf_save_cred() appends a new CREDENTIAL record to the ticket file.
875 * tf_close() closes the ticket file and releases the lock.
877 * tf_gets() returns the next null-terminated string. It's an internal
878 * routine used by tf_get_pname(), tf_get_pinst(), and tf_get_cred().
880 * tf_read() reads a given number of bytes. It's an internal routine
881 * used by tf_get_cred().
885 * tf_init() should be called before the other ticket file routines.
886 * It takes the name of the ticket file to use, "tf_name", and a
887 * read/write flag "rw" as arguments.
889 * It tries to open the ticket file, checks the mode, and if everything
890 * is okay, locks the file. If it's opened for reading, the lock is
891 * shared. If it's opened for writing, the lock is exclusive.
893 * Returns 0 if all went well, otherwise one of the following:
895 * NO_TKT_FIL - file wasn't there
896 * TKT_FIL_ACC - file was in wrong mode, etc.
897 * TKT_FIL_LCK - couldn't lock the file, even after a retry
900 static tf_init(tf_name, rw)
905 struct stat stat_buf;
917 if (lstat(tf_name, &stat_buf) < 0)
925 if ((stat_buf.st_uid != me && me != 0) ||
926 ((stat_buf.st_mode & S_IFMT) != S_IFREG))
930 * If "wflag" is set, open the ticket file in append-writeonly mode
931 * and lock the ticket file in exclusive mode. If unable to lock
932 * the file, sleep and try again. If we fail again, return with the
933 * proper error message.
936 curpos = sizeof(tfbfr);
939 fd = open(tf_name, O_RDWR, 0600);
943 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
944 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
946 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
948 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
950 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
960 * Otherwise "wflag" is not set and the ticket file should be opened
961 * for read-only operations and locked for shared access.
964 fd = open(tf_name, O_RDONLY, 0600);
968 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
969 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
971 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
973 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
975 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
986 * tf_get_pname() reads the principal's name from the ticket file. It
987 * should only be called after tf_init() has been called. The
988 * principal's name is filled into the "p" parameter. If all goes well,
989 * 0 is returned. If tf_init() wasn't called, TKT_FIL_INI is
990 * returned. If the name was null, or EOF was encountered, or the name
991 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned.
994 static tf_get_pname(p)
1000 if (tf_gets(p, MAXKTCNAMELEN) < 2) /* can't be just a null */
1006 * tf_get_pinst() reads the principal's instance from a ticket file.
1007 * It should only be called after tf_init() and tf_get_pname() have been
1008 * called. The instance is filled into the "inst" parameter. If all
1009 * goes well, 0 is returned. If tf_init() wasn't called,
1010 * TKT_FIL_INI is returned. If EOF was encountered, or the instance
1011 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned. Note that the
1012 * instance may be null.
1015 static tf_get_pinst(inst)
1021 if (tf_gets(inst, MAXKTCNAMELEN) < 1)
1027 * tf_get_cred() reads a CREDENTIALS record from a ticket file and fills
1028 * in the given structure "c". It should only be called after tf_init(),
1029 * tf_get_pname(), and tf_get_pinst() have been called. If all goes well,
1030 * 0 is returned. Possible error codes are:
1032 * TKT_FIL_INI - tf_init wasn't called first
1033 * TKT_FIL_FMT - bad format
1034 * EOF - end of file encountered
1037 static tf_get_cred(principal, token)
1038 struct ktc_principal *principal;
1039 struct ktc_token *token;
1047 if ((k_errno = tf_gets(principal->name, MAXKTCNAMELEN)) < 2)
1050 case 1: /* can't be just a null */
1055 if ((k_errno = tf_gets(principal->instance, MAXKTCNAMELEN)) < 1)
1062 if ((k_errno = tf_gets(principal->cell, MAXKTCREALMLEN)) < 2)
1065 case 1: /* can't be just a null */
1070 lcstring(principal->cell, principal->cell, MAXKTCREALMLEN);
1072 tf_read((char *) &(token->sessionKey), 8) < 1 ||
1073 tf_read((char *) &(lifetime), sizeof(lifetime)) < 1 ||
1074 tf_read((char *) &(kvno), sizeof(kvno)) < 1 ||
1075 tf_read((char *) &(token->ticketLen), sizeof(token->ticketLen))
1077 /* don't try to read a silly amount into ticket->dat */
1078 token->ticketLen > MAXKTCTICKETLEN ||
1079 tf_read((char *) (token->ticket), token->ticketLen) < 1 ||
1080 tf_read((char *) &(token->startTime), sizeof(token->startTime)) < 1
1084 token->endTime = life_to_time(token->startTime, lifetime);
1090 * tf_close() closes the ticket file and sets "fd" to -1. If "fd" is
1091 * not a valid file descriptor, it just returns. It also clears the
1092 * buffer used to read tickets.
1094 * The return value is not defined.
1100 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1101 (void) fcntl(fd, F_SETLK, &fileUlock);
1103 (void) flock(fd, LOCK_UN);
1106 fd = -1; /* see declaration of fd above */
1108 bzero(tfbfr, sizeof(tfbfr));
1112 * tf_gets() is an internal routine. It takes a string "s" and a count
1113 * "n", and reads from the file until either it has read "n" characters,
1114 * or until it reads a null byte. When finished, what has been read exists
1117 * Possible return values are:
1119 * n the number of bytes read (including null terminator)
1120 * when all goes well
1122 * 0 end of file or read error
1124 * TOO_BIG if "count" characters are read and no null is
1125 * encountered. This is an indication that the ticket
1126 * file is seriously ill.
1138 for (count = n - 1; count > 0; --count) {
1139 if (curpos >= sizeof(tfbfr)) {
1140 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1143 if (curpos == lastpos) {
1146 *s = tfbfr[curpos++];
1154 * tf_read() is an internal routine. It takes a string "s" and a count
1155 * "n", and reads from the file until "n" bytes have been read. When
1156 * finished, what has been read exists in "s".
1158 * Possible return values are:
1160 * n the number of bytes read when all goes well
1162 * 0 on end of file or read error
1172 for (count = n; count > 0; --count) {
1173 if (curpos >= sizeof(tfbfr)) {
1174 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1177 if (curpos == lastpos) {
1180 *s++ = tfbfr[curpos++];
1188 * tf_save_cred() appends an incoming ticket to the end of the ticket
1189 * file. You must call tf_init() before calling tf_save_cred().
1191 * The "service", "instance", and "realm" arguments specify the
1192 * server's name; "aticket" contains the credential.
1194 * Returns 0 if all goes well, TKT_FIL_INI if tf_init() wasn't
1195 * called previously, and KFAILURE for anything else that went wrong.
1198 static tf_save_cred(aserver, atoken, aclient)
1199 struct ktc_principal *aserver;
1200 struct ktc_principal *aclient;
1201 struct ktc_token *atoken; /* Token */
1203 char realm[MAXKTCREALMLEN+1];
1204 char junk[MAXKTCNAMELEN];
1205 struct ktc_principal principal;
1206 struct ktc_token token;
1208 off_t start, lseek();
1210 int count; /* count for write */
1212 if (fd < 0) { /* fd is ticket file as set by tf_init */
1216 ucstring(realm, aserver->cell, MAXKTCREALMLEN);
1217 realm[MAXKTCREALMLEN] = '\0';
1219 /* Look for a duplicate ticket */
1220 (void) lseek(fd, (off_t) 0L, 0);
1221 curpos = sizeof(tfbfr);
1223 if (tf_get_pname(junk) || strcmp(junk, aclient->name) ||
1224 tf_get_pinst(junk) || strcmp(junk, aclient->instance)) goto bad;
1227 start = lseek(fd, (off_t) 0L, 1) - lastpos + curpos;
1228 status = tf_get_cred(&principal, &token);
1229 } while (status == 0 &&
1230 (strcmp(aserver->name, principal.name) != 0 ||
1231 strcmp(aserver->instance, principal.instance) != 0 ||
1232 strcmp(aserver->cell, principal.cell) != 0));
1235 * Two tickets for the same user authenticating to the same service
1236 * should be the same length, but we check here just to make sure.
1238 if (status == 0 && token.ticketLen != atoken->ticketLen) return KFAILURE;
1239 if (status && status != EOF) return status;
1241 /* Position over the credential we just matched (or the EOF) */
1242 lseek(fd, start, 0);
1243 curpos = lastpos = sizeof(tfbfr);
1245 /* Write the ticket and associated data */
1247 count = strlen(aserver->name) + 1;
1248 if (write(fd, aserver->name, count) != count)
1251 count = strlen(aserver->instance) + 1;
1252 if (write(fd, aserver->instance, count) != count)
1255 count = strlen(realm) + 1;
1256 if (write(fd, realm, count) != count)
1259 if (write(fd, (char *) &atoken->sessionKey, 8) != 8)
1262 lifetime = time_to_life(atoken->startTime, atoken->endTime);
1263 if (write(fd, (char *) &lifetime, sizeof(int)) != sizeof(int))
1266 kvno = atoken->kvno;
1267 if (write(fd, (char *) &kvno, sizeof(int)) != sizeof(int))
1270 if (write(fd, (char *) &(atoken->ticketLen), sizeof(int)) !=
1274 count = atoken->ticketLen;
1275 if (write(fd, atoken->ticket, count) != count)
1278 if (write(fd, (char *) &atoken->startTime, sizeof(afs_int32))
1279 != sizeof(afs_int32))
1282 /* Actually, we should check each write for success */
1289 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
1292 * For copying and distribution information, please see the file
1293 * <mit-copyright.h>.
1299 * This routine is used to generate the name of the file that holds
1300 * the user's cache of server tickets and associated session keys.
1302 * If it is set, krb_ticket_string contains the ticket file name.
1303 * Otherwise, the filename is constructed as follows:
1305 * If it is set, the environment variable "KRBTKFILE" will be used as
1306 * the ticket file name. Otherwise TKT_ROOT (defined in "krb.h") and
1307 * the user's uid are concatenated to produce the ticket file name
1308 * (e.g., "/tmp/tkt123"). A pointer to the string containing the ticket
1309 * file name is returned.
1312 static char krb_ticket_string[4096] = "";
1314 char *ktc_tkt_string()
1319 if (!*krb_ticket_string) {
1320 if (env = getenv("KRBTKFILE")) {
1321 (void) strncpy(krb_ticket_string, env,
1322 sizeof(krb_ticket_string)-1);
1323 krb_ticket_string[sizeof(krb_ticket_string)-1] = '\0';
1325 /* 32 bits of signed integer will always fit in 11 characters
1326 (including the sign), so no need to worry about overflow */
1327 (void) sprintf(krb_ticket_string, "%s%d",TKT_ROOT,getuid());
1331 return krb_ticket_string;
1335 * This routine is used to set the name of the file that holds the user's
1336 * cache of server tickets and associated session keys.
1338 * The value passed in is copied into local storage.
1340 * NOTE: This routine should be called during initialization, before other
1341 * Kerberos routines are called; otherwise tkt_string() above may be called
1342 * and return an undesired ticket file name until this routine is called.
1346 ktc_set_tkt_string(val)
1351 (void) strncpy(krb_ticket_string, val, sizeof(krb_ticket_string)-1);
1352 krb_ticket_string[sizeof(krb_ticket_string)-1] = '\0';
1359 * tf_create() is used to initialize the ticket store. It creates the
1360 * file to contain the tickets and writes the given user's name "pname"
1361 * and instance "pinst" in the file. in_tkt() returns KSUCCESS on
1362 * success, or KFAILURE if something goes wrong.
1365 static tf_create(pname,pinst)
1372 char *file = ktc_tkt_string();
1381 if (lstat(file,&sbuf) == 0) {
1382 if ((sbuf.st_uid != me && me != 0) || ((sbuf.st_mode & S_IFMT) != S_IFREG) ||
1383 sbuf.st_mode & 077) {
1386 /* file already exists, and permissions appear ok, so nuke it */
1387 if ((fd = open(file, O_RDWR, 0)) < 0)
1388 goto out; /* can't zero it, but we can still try truncating it */
1390 bzero(zerobuf, sizeof(zerobuf));
1392 for (i = 0; i < sbuf.st_size; i += sizeof(zerobuf))
1393 if (write(fd, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf)) {
1404 /* arrange so the file is owned by the ruid
1405 (swap real & effective uid if necessary).
1406 This isn't a security problem, since the ticket file, if it already
1407 exists, has the right uid (== ruid) and mode. */
1409 if (setreuid(metoo, me) < 0) {
1413 tktfile = creat(file, 0600);
1415 if (setreuid(me, metoo) < 0) {
1416 /* can't switch??? fail! */
1423 count = strlen(pname)+1;
1424 if (write(tktfile,pname,count) != count) {
1425 (void) close(tktfile);
1428 count = strlen(pinst)+1;
1429 if (write(tktfile,pinst,count) != count) {
1430 (void) close(tktfile);
1433 (void) close(tktfile);
1438 * dest_tkt() is used to destroy the ticket store upon logout.
1439 * If the ticket file does not exist, dest_tkt() returns RET_TKFIL.
1440 * Otherwise the function returns 0 on success, KFAILURE on
1444 static tf_dest_tkt()
1446 char *file = ktc_tkt_string();
1452 if (lstat(file,&statb) < 0)
1455 if (!(statb.st_mode & S_IFREG))
1458 if ((fd = open(file, O_RDWR, 0)) < 0)
1463 for (i = 0; i < statb.st_size; i += BUFSIZ)
1464 if (write(fd, buf, BUFSIZ) != BUFSIZ) {
1473 (void) unlink(file);
1476 if (errno == ENOENT) return RET_TKFIL;
1477 else if (errno != 0) return KFAILURE;
1481 static afs_uint32 curpag()
1483 gid_t groups[NGROUPS_MAX];
1485 afs_uint32 h, l, ret;
1487 if (getgroups(sizeof groups/sizeof groups[0], groups) < 2) return 0;
1489 g0 = groups[0] & 0xffff;
1490 g1 = groups[1] & 0xffff;
1493 if (g0 < 0xc000 && g1 < 0xc000) {
1494 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
1496 h = (g1 >> 14) + h + h + h;
1497 ret = ((h << 28) | l);
1498 /* Additional testing */
1499 if (((ret >> 24) & 0xff) == 'A')
1510 extern char **environ;
1514 char fname[256], *prefix = "/ticket/";
1516 char **newenv, **senv, **denv;
1519 if (stat("/ticket", &sbuf) == -1) {
1520 prefix = "/tmp/tkt";
1523 pag = curpag() & 0xffffffff;
1525 sprintf(fname, "%s%d", prefix, getuid());
1528 sprintf(fname, "%sp%ld", prefix, pag);
1530 ktc_set_tkt_string(fname);
1532 for (senv=environ, numenv=0; *senv; senv++) numenv++;
1533 newenv = (char **)malloc((numenv+2) * sizeof(char *));
1535 for (senv=environ, denv=newenv; *senv; *senv++) {
1536 if (strncmp(*senv, "KRBTKFILE=", 10) != 0) *denv++ = *senv;
1539 *denv = (char *)malloc(10 + strlen(fname) + 1);
1540 strcpy(*denv, "KRBTKFILE=");
1541 strcat(*denv, fname);
1548 * BLETCH! We have to invoke the entire afsconf package just to
1549 * find out what the local cell is.
1551 static ktc_LocalCell()
1554 struct afsconf_dir *conf;
1556 if ((conf = afsconf_Open (AFSDIR_CLIENT_ETC_DIRPATH)) ||
1557 (conf = afsconf_Open (AFSDIR_SERVER_ETC_DIRPATH ))) {
1558 code = afsconf_GetLocalCell (conf, lcell, sizeof(lcell));
1559 afsconf_Close (conf);
1561 if (!conf || code) {
1562 printf("** Can't determine local cell name!\n");