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>
13 #include <afs/param.h>
16 #include "afsincludes.h"
25 #include <afs/pthread_glock.h>
26 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <netinet/in.h>
36 #include <sys/lockf.h>
44 #ifdef HAVE_CRT_EXTERNS_H
45 #include <crt_externs.h>
51 #include <afs/venus.h>
52 #include <afs/afsutil.h>
55 #include <afs/sys_prototypes.h>
60 #if defined(LINUX_KEYRING_SUPPORT) && defined(HAVE_SESSION_TO_PARENT)
61 #include <sys/syscall.h>
62 #define KEYCTL_SESSION_TO_PARENT 18
70 /* AFS_KERBEROS_ENV is now conditionally defined in the Makefile */
71 #define AFS_KERBEROS_ENV
74 #ifdef AFS_KERBEROS_ENV
77 #include <afs/cellconfig.h>
78 static char lcell[MAXCELLCHARS];
80 #define TKT_ROOT "/tmp/tkt"
85 /* Definitions for ticket file utilities */
89 /* Error codes returned by ticket file utilities */
90 #define NO_TKT_FIL 76 /* No ticket file found */
91 #define TKT_FIL_ACC 77 /* Couldn't access tkt file */
92 #define TKT_FIL_LCK 78 /* Couldn't lock ticket file */
93 #define TKT_FIL_FMT 79 /* Bad ticket file format */
94 #define TKT_FIL_INI 80 /* afs_tf_init not called first */
96 /* Values returned by get_credentials */
97 #define RET_TKFIL 21 /* Can't read ticket file */
106 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
107 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0, 0 };
108 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0, 0 };
109 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0, 0 };
112 static struct flock fileWlock = { F_WRLCK, 0, 0, 0, 0 };
113 static struct flock fileRlock = { F_RDLCK, 0, 0, 0, 0 };
114 static struct flock fileUlock = { F_UNLCK, 0, 0, 0, 0 };
121 /* the following routines aren't static anymore on behalf of the kerberos IV
122 * compatibility library built in subtree krb.
124 int afs_tf_init(char *, int);
125 int afs_tf_get_pname(char *);
126 int afs_tf_get_pinst(char *);
127 int afs_tf_get_cred(struct ktc_principal *, struct ktc_token *);
128 int afs_tf_save_cred(struct ktc_principal *, struct ktc_token *,
129 struct ktc_principal *);
130 int afs_tf_close(void);
131 int afs_tf_create(char *, char *);
132 int afs_tf_dest_tkt(void);
133 static void ktc_LocalCell(void);
134 #endif /* AFS_KERBEROS_ENV */
137 #define PIOCTL afs_pioctl
138 #elif defined(UKERNEL)
139 #define PIOCTL(A,B,C,D) call_syscall(AFSCALL_PIOCTL,A,B,C,D)
141 #define PIOCTL pioctl
144 #if !defined(UKERNEL)
145 /* this is a structure used to communicate with the afs cache mgr, but is
146 * otherwise irrelevant */
148 afs_int32 AuthHandle;
149 char HandShakeKey[8];
151 afs_int32 BeginTimestamp;
152 afs_int32 EndTimestamp;
154 #endif /* !defined(UKERNEL) */
156 #define MAXLOCALTOKENS 4
160 struct ktc_principal server;
161 struct ktc_principal client;
162 struct ktc_token token;
163 } local_tokens[MAXLOCALTOKENS] = { {
170 #define MAXPIOCTLTOKENLEN \
171 (3*sizeof(afs_int32)+MAXKTCTICKETLEN+sizeof(struct ClearToken)+MAXKTCREALMLEN)
174 SetToken(struct ktc_principal *aserver, struct ktc_token *atoken,
175 struct ktc_principal *aclient, afs_int32 flags)
177 struct ViceIoctl iob;
178 char tbuffer[MAXPIOCTLTOKENLEN];
180 struct ClearToken ct;
184 if (strcmp(aserver->name, "afs") != 0) {
187 for (i = 0; i < MAXLOCALTOKENS; i++)
188 if (local_tokens[i].valid) {
189 if ((strcmp(local_tokens[i].server.name, aserver->name) == 0)
192 (local_tokens[i].server.instance,
193 aserver->instance) == 0)
194 && (strcmp(local_tokens[i].server.cell, aserver->cell) ==
196 found = i; /* replace existing entry */
198 } else /* valid, but no match */
201 found = i; /* remember this empty slot */
204 memcpy(&local_tokens[found].token, atoken, sizeof(struct ktc_token));
205 local_tokens[found].server = *aserver;
206 local_tokens[found].client = *aclient;
207 local_tokens[found].valid = 1;
210 tp = tbuffer; /* start copying here */
211 if ((atoken->ticketLen < MINKTCTICKETLEN)
212 || (atoken->ticketLen > MAXKTCTICKETLEN))
214 memcpy(tp, &atoken->ticketLen, sizeof(afs_int32)); /* copy in ticket length */
215 tp += sizeof(afs_int32);
216 memcpy(tp, atoken->ticket, atoken->ticketLen); /* copy in ticket */
217 tp += atoken->ticketLen;
218 /* next, copy in the "clear token", describing who we are */
219 ct.AuthHandle = atoken->kvno; /* hide auth handle here */
220 memcpy(ct.HandShakeKey, &atoken->sessionKey, 8);
222 ct.BeginTimestamp = atoken->startTime;
223 ct.EndTimestamp = atoken->endTime;
224 if (ct.BeginTimestamp == 0)
225 ct.BeginTimestamp = 1;
227 if ((strlen(aclient->name) > strlen("AFS ID "))
228 && (aclient->instance[0] == 0)) {
230 afs_int32 viceId = 0;
231 char *cp = aclient->name + strlen("AFS ID ");
238 viceId = viceId * 10 + (int)(*cp - '0');
243 ct.ViceId = viceId * sign; /* OK to let any value here? */
244 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 0)
245 ct.BeginTimestamp++; /* force lifetime to be odd */
248 ct.ViceId = getuid(); /* wrong, but works in primary cell */
249 if (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1)
250 ct.BeginTimestamp++; /* force lifetime to be even */
255 * Information needed by the user space cache manager
257 get_user_struct()->u_expiration = ct.EndTimestamp;
258 get_user_struct()->u_viceid = ct.ViceId;
261 temp = sizeof(struct ClearToken);
262 memcpy(tp, &temp, sizeof(afs_int32));
263 tp += sizeof(afs_int32);
264 memcpy(tp, &ct, sizeof(struct ClearToken));
265 tp += sizeof(struct ClearToken);
267 /* next copy in primary flag */
271 * The following means that setpag will happen inside afs just before
272 * the authentication to prevent the setpag/klog race condition.
274 * The following means that setpag will affect the parent process as
275 * well as the current process.
277 if (flags & AFS_SETTOK_SETPAG)
280 memcpy(tp, &temp, sizeof(afs_int32));
281 tp += sizeof(afs_int32);
283 /* finally copy in the cell name */
284 temp = strlen(aserver->cell);
285 if (temp >= MAXKTCREALMLEN)
287 strcpy(tp, aserver->cell);
290 /* now setup for the pioctl */
292 iob.in_size = tp - tbuffer;
294 iob.out_size = sizeof(tbuffer);
296 #if defined(NO_AFS_CLIENT)
300 if ((tkfile = getenv("TKTFILE"))
303 open(tkfile, O_WRONLY | O_APPEND | O_TRUNC | O_CREAT,
305 printf("Writing ticket to: %s\n", tkfile);
306 code = (write(fd, iob.in, iob.in_size) != iob.in_size);
309 code = KTC_PIOCTLFAIL;
311 #else /* NO_AFS_CLIENT */
312 code = PIOCTL(0, VIOCSETTOK, &iob, 0);
313 #if defined(LINUX_KEYRING_SUPPORT) && defined(HAVE_SESSION_TO_PARENT)
315 * If we're using keyring based PAGs and the SESSION_TO_PARENT keyctl
316 * is available, use it to copy the session keyring to the parent process
318 if (flags & AFS_SETTOK_SETPAG)
319 syscall(SYS_keyctl, KEYCTL_SESSION_TO_PARENT);
321 #endif /* NO_AFS_CLIENT */
323 return KTC_PIOCTLFAIL;
328 ktc_SetTokenEx(struct ktc_setTokenData *token) {
329 struct ViceIoctl iob;
333 xdrlen_create(&xdrs);
334 if (!xdr_ktc_setTokenData(&xdrs, token))
336 iob.in_size = xdr_getpos(&xdrs);
339 iob.in = malloc(iob.in_size);
343 xdrmem_create(&xdrs, iob.in, iob.in_size, XDR_ENCODE);
344 if (!xdr_ktc_setTokenData(&xdrs, token))
351 code = PIOCTL(0, VIOC_SETTOK2, &iob, 0);
355 /* If we can't use the new pioctl, then fallback to using the old
356 * one, with just the rxkad portion of the token we're being asked to
359 if (code == -1 && errno == EINVAL) {
360 struct ktc_principal server, client;
361 struct ktc_token *rxkadToken;
364 /* With the growth of ticket sizes, a ktc_token is now 12k. Don't
365 * allocate it on the stack! */
366 rxkadToken = malloc(sizeof(*rxkadToken));
367 if (rxkadToken == NULL)
370 code = token_extractRxkad(token, rxkadToken, &flags, &client);
376 memset(&server, 0, sizeof(server));
377 strcpy(server.name, "afs");
378 strcpy(server.cell, token->cell);
379 code = ktc_SetToken(&server, rxkadToken, &client, flags);
385 return KTC_PIOCTLFAIL;
391 ktc_SetToken(struct ktc_principal *aserver,
392 struct ktc_token *atoken,
393 struct ktc_principal *aclient,
399 #ifdef AFS_KERBEROS_ENV
403 if ( /*!strcmp(aclient->cell, lcell) && this would only store local creds */
404 (strcmp(aserver->name, "AuthServer")
405 || strcmp(aserver->instance, "Admin"))) {
406 if (strcmp(aserver->name, "krbtgt") == 0) {
407 static char lrealm[MAXKTCREALMLEN];
410 ucstring(lrealm, lcell, MAXKTCREALMLEN);
411 if (strcmp(aserver->instance, lrealm) == 0) {
412 afs_tf_create(aclient->name, aclient->instance);
416 code = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
417 if (code == NO_TKT_FIL) {
418 (void)afs_tf_create(aclient->name, aclient->instance);
419 code = afs_tf_init(ktc_tkt_string(), W_TKT_FIL);
423 afs_tf_save_cred(aserver, atoken, aclient);
429 #endif /* NO_AFS_CLIENT */
433 #ifndef NO_AFS_CLIENT
434 code = SetToken(aserver, atoken, aclient, flags);
439 else if (code == KTC_PIOCTLFAIL)
447 return KTC_PIOCTLFAIL;
449 #endif /* NO_AFS_CLIENT */
455 * Get a token, given the cell that we need to get information for
458 * The name of the cell we're getting the token for - if NULL, we'll
459 * get information for the primary cell
462 ktc_GetTokenEx(char *cellName, struct ktc_setTokenData **tokenSet) {
463 struct ViceIoctl iob;
464 char tbuffer[MAXPIOCTLTOKENLEN];
471 /* If we have a cellName, write it out here */
473 memcpy(tp, cellName, strlen(cellName) +1);
474 tp += strlen(cellName)+1;
478 iob.in_size = tp - tbuffer;
480 iob.out_size = sizeof(tbuffer);
482 code = PIOCTL(0, VIOC_GETTOK2, &iob, 0);
484 /* If we can't use the new pioctl, the fall back to the old one. We then
485 * need to convert the rxkad token we get back into the new format
487 if (code == -1 && errno == EINVAL) {
488 struct ktc_principal server;
489 struct ktc_principal client;
490 struct ktc_tokenUnion token;
491 struct ktc_token *ktcToken; /* too huge for the stack */
493 memset(&server, 0, sizeof(server));
494 ktcToken = malloc(sizeof(struct ktc_token));
495 if (ktcToken == NULL)
497 memset(ktcToken, 0, sizeof(struct ktc_token));
499 strcpy(server.name, "afs");
500 strcpy(server.cell, cellName);
501 code = ktc_GetToken(&server, ktcToken, sizeof(struct ktc_token),
504 *tokenSet = token_buildTokenJar(cellName);
505 token.at_type = AFSTOKEN_UNION_KAD;
506 token.ktc_tokenUnion_u.at_kad.rk_kvno = ktcToken->kvno;
507 memcpy(token.ktc_tokenUnion_u.at_kad.rk_key,
508 ktcToken->sessionKey.data, 8);
510 token.ktc_tokenUnion_u.at_kad.rk_begintime = ktcToken->startTime;
511 token.ktc_tokenUnion_u.at_kad.rk_endtime = ktcToken->endTime;
512 token.ktc_tokenUnion_u.at_kad.rk_ticket.rk_ticket_len
513 = ktcToken->ticketLen;
514 token.ktc_tokenUnion_u.at_kad.rk_ticket.rk_ticket_val
517 token_addToken(*tokenSet, &token);
519 memset(ktcToken, 0, sizeof(struct ktc_token));
524 return KTC_PIOCTLFAIL;
526 *tokenSet = malloc(sizeof(struct ktc_setTokenData));
527 if (*tokenSet == NULL)
529 memset(*tokenSet, 0, sizeof(struct ktc_setTokenData));
531 xdrmem_create(&xdrs, iob.out, iob.out_size, XDR_DECODE);
532 if (!xdr_ktc_setTokenData(&xdrs, *tokenSet)) {
542 /* get token, given server we need and token buffer. aclient will eventually
543 * be set to our identity to the server.
546 ktc_GetToken(struct ktc_principal *aserver, struct ktc_token *atoken,
547 int atokenLen, struct ktc_principal *aclient)
549 struct ViceIoctl iob;
550 char tbuffer[MAXPIOCTLTOKENLEN];
553 char *stp, *cellp; /* secret token ptr */
554 struct ClearToken ct;
557 int maxLen; /* biggest ticket we can copy */
558 int tktLen; /* server ticket length */
559 #ifdef AFS_KERBEROS_ENV
565 #ifdef AFS_KERBEROS_ENV
569 #ifndef NO_AFS_CLIENT
570 if (strcmp(aserver->name, "afs") != 0)
571 #endif /* NO_AFS_CLIENT */
574 /* try the local tokens */
575 for (i = 0; i < MAXLOCALTOKENS; i++)
576 if (local_tokens[i].valid
577 && (strcmp(local_tokens[i].server.name, aserver->name) == 0)
578 && (strcmp(local_tokens[i].server.instance, aserver->instance)
580 && (strcmp(local_tokens[i].server.cell, aserver->cell) == 0)) {
581 memcpy(atoken, &local_tokens[i].token,
582 min(atokenLen, sizeof(struct ktc_token)));
584 *aclient = local_tokens[i].client;
588 #ifdef AFS_KERBEROS_ENV
589 if (!afs_tf_init(ktc_tkt_string(), R_TKT_FIL)) {
591 if (!afs_tf_get_pname(aclient->name)
592 && !afs_tf_get_pinst(aclient->instance))
595 char tmpstring[MAXHOSTCHARS];
596 afs_tf_get_pname(tmpstring);
597 afs_tf_get_pinst(tmpstring);
602 struct ktc_principal cprincipal;
603 struct ktc_token ctoken;
605 while (!afs_tf_get_cred(&cprincipal, &ctoken)) {
606 if (strcmp(cprincipal.name, aserver->name) == 0
607 && strcmp(cprincipal.instance, aserver->instance) == 0
608 && strcmp(cprincipal.cell, aserver->cell) == 0) {
611 strcpy(aclient->cell, lcell);
612 memcpy(atoken, &ctoken,
613 min(atokenLen, sizeof(struct ktc_token)));
626 #ifndef NO_AFS_CLIENT
627 for (index = 0; index < 200; index++) { /* sanity check in case pioctl fails */
628 iob.in = (char *)&index;
629 iob.in_size = sizeof(afs_int32);
631 iob.out_size = sizeof(tbuffer);
633 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
636 /* failed to retrieve specified token */
637 if (code < 0 && errno == EDOM) {
642 /* token retrieved; parse buffer */
645 /* get ticket length */
646 memcpy(&temp, tp, sizeof(afs_int32));
648 tp += sizeof(afs_int32);
650 /* remember where ticket is and skip over it */
654 /* get size of clear token and verify */
655 memcpy(&temp, tp, sizeof(afs_int32));
656 if (temp != sizeof(struct ClearToken)) {
660 tp += sizeof(afs_int32);
662 /* copy clear token */
663 memcpy(&ct, tp, temp);
666 /* skip over primary flag */
667 tp += sizeof(afs_int32);
669 /* remember where cell name is */
672 if ((strcmp(cellp, aserver->cell) == 0)
673 #ifdef AFS_KERBEROS_ENV
674 || (*aserver->cell == '\0' && strcmp(cellp, lcell) == 0)
677 /* got token for cell; check that it will fit */
679 atokenLen - sizeof(struct ktc_token) + MAXKTCTICKETLEN;
680 if (maxLen < tktLen) {
685 /* set return values */
686 memcpy(atoken->ticket, stp, tktLen);
687 atoken->startTime = ct.BeginTimestamp;
688 atoken->endTime = ct.EndTimestamp;
689 if (ct.AuthHandle == -1) {
692 atoken->kvno = ct.AuthHandle;
693 memcpy(&atoken->sessionKey, ct.HandShakeKey,
694 sizeof(struct ktc_encryptionKey));
695 atoken->ticketLen = tktLen;
698 strcpy(aclient->cell, cellp);
699 aclient->instance[0] = 0;
701 if ((atoken->kvno == 999) || /* old style bcrypt ticket */
702 (ct.BeginTimestamp && /* new w/ prserver lookup */
703 (((ct.EndTimestamp - ct.BeginTimestamp) & 1) == 1))) {
704 sprintf(aclient->name, "AFS ID %d", ct.ViceId);
706 sprintf(aclient->name, "Unix UID %d", ct.ViceId);
714 #endif /* NO_AFS_CLIENT */
717 if ((code < 0) && (errno == EINVAL))
719 return KTC_PIOCTLFAIL; /* probable cause */
723 * Forget tokens for this server and the calling user.
724 * NOT IMPLEMENTED YET!
726 #ifndef NO_AFS_CLIENT
728 ktc_ForgetToken(struct ktc_principal *aserver)
733 rc = ktc_ForgetAllTokens(); /* bogus, but better */
737 #endif /* NO_AFS_CLIENT */
740 ktc_ListTokensEx(int prevIndex, int *newIndex, char **cellName) {
741 struct ViceIoctl iob;
742 char tbuffer[MAXPIOCTLTOKENLEN];
745 struct ktc_setTokenData tokenSet;
748 memset(&tokenSet, 0, sizeof(tokenSet));
751 *newIndex = prevIndex;
755 while (index<100) { /* Safety, incase of pioctl failure */
756 memset(tbuffer, 0, sizeof(tbuffer));
758 memcpy(tbuffer, &index, sizeof(afs_int32));
759 iob.in_size = sizeof(afs_int32);
761 iob.out_size = sizeof(tbuffer);
763 code = PIOCTL(0, VIOC_GETTOK2, &iob, 0);
765 /* Can't use new pioctl, so must use old one */
766 if (code == -1 && errno == EINVAL) {
767 struct ktc_principal server;
769 code = ktc_ListTokens(index, newIndex, &server);
771 *cellName = strdup(server.cell);
776 /* Got a token from the pioctl. Now we throw it away,
777 * so we can return just a cellname. This is rather wasteful,
778 * but it's what the old API does. Ho hum. */
780 xdrmem_create(&xdrs, iob.out, iob.out_size, XDR_DECODE);
781 if (!xdr_ktc_setTokenData(&xdrs, &tokenSet)) {
786 *cellName = strdup(tokenSet.cell);
787 xdr_free((xdrproc_t)xdr_ktc_setTokenData, &tokenSet);
788 *newIndex = index + 1;
793 return KTC_PIOCTLFAIL;
796 /* ktc_ListTokens - list all tokens. start aprevIndex at 0, it returns the
797 * next rock in (*aindex). (*aserver) is set to the relevant ticket on
801 ktc_ListTokens(int aprevIndex,
803 struct ktc_principal *aserver)
805 struct ViceIoctl iob;
806 char tbuffer[MAXPIOCTLTOKENLEN];
809 afs_int32 temp, index;
811 memset(tbuffer, 0, sizeof(tbuffer));
819 #endif /* NO_AFS_CLIENT */
820 #ifdef AFS_KERBEROS_ENV
823 struct ktc_principal cprincipal;
824 struct ktc_token ctoken;
826 if (afs_tf_init(ktc_tkt_string(), R_TKT_FIL)
827 || afs_tf_get_pname(tbuffer) || afs_tf_get_pinst(tbuffer)) {
833 for (i = 214; i < index; i++) {
834 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
842 if (afs_tf_get_cred(&cprincipal, &ctoken)) {
849 #ifndef NO_AFS_CLIENT
850 if (!strcmp(cprincipal.name, "afs") && cprincipal.instance[0] == 0) {
853 #endif /* NO_AFS_CLIENT */
855 for (i = 0; i < MAXLOCALTOKENS; i++) {
856 if (!strcmp(cprincipal.name, local_tokens[i].server.name)
857 && !strcmp(cprincipal.instance,
858 local_tokens[i].server.instance)
859 && !strcmp(cprincipal.cell, local_tokens[i].server.cell)) {
864 *aserver = cprincipal;
872 #ifndef NO_AFS_CLIENT
873 if (index >= 123) { /* special hack for returning TCS */
874 while (index - 123 < MAXLOCALTOKENS) {
875 if (local_tokens[index - 123].valid) {
876 *aserver = local_tokens[index - 123].server;
884 #ifdef AFS_KERBEROS_ENV
885 return ktc_ListTokens(214, aindex, aserver);
891 /* get tokens from the kernel */
892 while (index < 200) { /* sanity check in case pioctl fails */
893 iob.in = (char *)&index;
894 iob.in_size = sizeof(afs_int32);
896 iob.out_size = sizeof(tbuffer);
897 code = PIOCTL(0, VIOCGETTOK, &iob, 0);
898 if (code < 0 && errno == EDOM) {
901 rc = ktc_ListTokens(123, aindex, aserver);
910 break; /* got a ticket */
911 /* otherwise we should skip this ticket slot */
918 return KTC_PIOCTLFAIL;
924 /* next iterator determined by earlier loop */
927 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of secret token */
928 tp += sizeof(afs_int32);
929 tp += temp; /* skip ticket for now */
930 memcpy(&temp, tp, sizeof(afs_int32)); /* get size of clear token */
931 if (temp != sizeof(struct ClearToken)) {
935 tp += sizeof(afs_int32); /* skip length */
936 tp += temp; /* skip clear token itself */
937 tp += sizeof(afs_int32); /* skip primary flag */
938 /* tp now points to the cell name */
939 strcpy(aserver->cell, tp);
940 aserver->instance[0] = 0;
941 strcpy(aserver->name, "afs");
942 #endif /* NO_AFS_CLIENT */
950 struct ViceIoctl iob;
954 for (i = 0; i < MAXLOCALTOKENS; i++)
955 local_tokens[i].valid = 0;
961 #ifndef NO_AFS_CLIENT
962 code = PIOCTL(0, VIOCUNPAG, &iob, 0);
964 return KTC_PIOCTLFAIL;
965 #endif /* NO_AFS_CLIENT */
970 ktc_ForgetAllTokens(void)
975 #ifdef AFS_KERBEROS_ENV
976 (void)afs_tf_dest_tkt();
983 else if (ocode == KTC_PIOCTLFAIL)
988 return KTC_PIOCTLFAIL;
994 /* ktc_OldPioctl - returns a boolean true if the kernel supports only the old
995 * pioctl interface for delivering AFS tickets to the cache manager. */
1007 struct ViceIoctl iob;
1010 /* now setup for the pioctl */
1013 iob.out = (caddr_t) &pag;
1014 iob.out_size = sizeof(afs_uint32);
1016 code = PIOCTL(0, VIOC_GETPAG, &iob, 0);
1018 #if defined(AFS_AIX52_ENV)
1019 code = getpagvalue("afs");
1020 if (code < 0 && errno == EINVAL)
1023 #elif defined(AFS_AIX51_ENV)
1026 gid_t groups[NGROUPS_MAX];
1028 afs_uint32 h, l, ret;
1030 #ifdef AFS_LINUX26_ENV
1034 ngroups = getgroups(sizeof groups / sizeof groups[0], groups);
1036 #ifdef AFS_LINUX26_ENV
1037 /* check for AFS_LINUX26_ONEGROUP_ENV PAGs */
1038 for (i = 0; i < ngroups; i++) {
1039 if (((groups[i] >> 24) & 0xff) == 'A') {
1048 g0 = groups[0] & 0xffff;
1049 g1 = groups[1] & 0xffff;
1052 if (g0 < 0xc000 && g1 < 0xc000) {
1053 l = ((g0 & 0x3fff) << 14) | (g1 & 0x3fff);
1055 h = (g1 >> 14) + h + h + h;
1056 ret = ((h << 28) | l);
1057 /* Additional testing */
1058 if (((ret >> 24) & 0xff) == 'A')
1070 #ifdef AFS_KERBEROS_ENV
1072 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
1074 * For copying and distribution information, please see the file
1075 * <mit-copyright.h>.
1081 #include <sys/types.h>
1082 #include <sys/stat.h>
1083 #include <sys/file.h>
1088 #define TF_LCK_RETRY ((unsigned)2) /* seconds to sleep before
1089 * retry if ticket file is
1093 * fd must be initialized to something that won't ever occur as a real
1094 * file descriptor. Since open(2) returns only non-negative numbers as
1095 * valid file descriptors, and afs_tf_init always stuffs the return value
1096 * from open in here even if it is an error flag, we must
1097 * a. Initialize fd to a negative number, to indicate that it is
1098 * not initially valid.
1099 * b. When checking for a valid fd, assume that negative values
1100 * are invalid (ie. when deciding whether afs_tf_init has been
1102 * c. In tf_close, be sure it gets reinitialized to a negative
1106 static int curpos; /* Position in tfbfr */
1107 static int lastpos; /* End of tfbfr */
1108 static char tfbfr[BUFSIZ]; /* Buffer for ticket data */
1110 static int tf_gets(char *, int);
1111 static int tf_read(char *, int);
1114 * This file contains routines for manipulating the ticket cache file.
1116 * The ticket file is in the following format:
1118 * principal's name (null-terminated string)
1119 * principal's instance (null-terminated string)
1126 * Where "CREDENTIAL_x" consists of the following fixed-length
1127 * fields from the CREDENTIALS structure (see "krb.h"):
1129 * char service[MAXKTCNAMELEN]
1130 * char instance[MAXKTCNAMELEN]
1131 * char realm[REALM_SZ]
1135 * KTEXT_ST ticket_st
1136 * afs_int32 issue_date
1138 * Short description of routines:
1140 * afs_tf_init() opens the ticket file and locks it.
1142 * afs_tf_get_pname() returns the principal's name.
1144 * afs_tf_get_pinst() returns the principal's instance (may be null).
1146 * afs_tf_get_cred() returns the next CREDENTIALS record.
1148 * afs_tf_save_cred() appends a new CREDENTIAL record to the ticket file.
1150 * afs_tf_close() closes the ticket file and releases the lock.
1152 * tf_gets() returns the next null-terminated string. It's an internal
1153 * routine used by afs_tf_get_pname(), afs_tf_get_pinst(), and
1154 * afs_tf_get_cred().
1156 * tf_read() reads a given number of bytes. It's an internal routine
1157 * used by afs_tf_get_cred().
1161 * afs_tf_init() should be called before the other ticket file routines.
1162 * It takes the name of the ticket file to use, "tf_name", and a
1163 * read/write flag "rw" as arguments.
1165 * It tries to open the ticket file, checks the mode, and if everything
1166 * is okay, locks the file. If it's opened for reading, the lock is
1167 * shared. If it's opened for writing, the lock is exclusive.
1169 * Returns 0 if all went well, otherwise one of the following:
1171 * NO_TKT_FIL - file wasn't there
1172 * TKT_FIL_ACC - file was in wrong mode, etc.
1173 * TKT_FIL_LCK - couldn't lock the file, even after a retry
1177 afs_tf_init(char *tf_name, int rw)
1181 struct stat stat_buf;
1193 if (lstat(tf_name, &stat_buf) < 0)
1201 if ((stat_buf.st_uid != me && me != 0)
1202 || ((stat_buf.st_mode & S_IFMT) != S_IFREG))
1206 * If "wflag" is set, open the ticket file in append-writeonly mode
1207 * and lock the ticket file in exclusive mode. If unable to lock
1208 * the file, sleep and try again. If we fail again, return with the
1209 * proper error message.
1212 curpos = sizeof(tfbfr);
1215 fd = open(tf_name, O_RDWR, 0600);
1219 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1220 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1221 sleep(TF_LCK_RETRY);
1222 if (fcntl(fd, F_SETLK, &fileWlock) == -1) {
1224 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1225 sleep(TF_LCK_RETRY);
1226 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
1236 * Otherwise "wflag" is not set and the ticket file should be opened
1237 * for read-only operations and locked for shared access.
1240 fd = open(tf_name, O_RDONLY, 0600);
1244 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1245 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1246 sleep(TF_LCK_RETRY);
1247 if (fcntl(fd, F_SETLK, &fileRlock) == -1) {
1249 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1250 sleep(TF_LCK_RETRY);
1251 if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
1262 * afs_tf_get_pname() reads the principal's name from the ticket file. It
1263 * should only be called after afs_tf_init() has been called. The
1264 * principal's name is filled into the "p" parameter. If all goes well,
1265 * 0 is returned. If afs_tf_init() wasn't called, TKT_FIL_INI is
1266 * returned. If the name was null, or EOF was encountered, or the name
1267 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned.
1271 afs_tf_get_pname(char *p)
1276 if (tf_gets(p, MAXKTCNAMELEN) < 2) /* can't be just a null */
1282 * afs_tf_get_pinst() reads the principal's instance from a ticket file.
1283 * It should only be called after afs_tf_init() and afs_tf_get_pname() have
1284 * been called. The instance is filled into the "inst" parameter. If all
1285 * goes well, 0 is returned. If afs_tf_init() wasn't called,
1286 * TKT_FIL_INI is returned. If EOF was encountered, or the instance
1287 * was longer than MAXKTCNAMELEN, TKT_FIL_FMT is returned. Note that the
1288 * instance may be null.
1292 afs_tf_get_pinst(char *inst)
1297 if (tf_gets(inst, MAXKTCNAMELEN) < 1)
1303 * afs_tf_get_cred() reads a CREDENTIALS record from a ticket file and fills
1304 * in the given structure "c". It should only be called after afs_tf_init(),
1305 * afs_tf_get_pname(), and afs_tf_get_pinst() have been called. If all goes
1306 * well, 0 is returned. Possible error codes are:
1308 * TKT_FIL_INI - afs_tf_init wasn't called first
1309 * TKT_FIL_FMT - bad format
1310 * EOF - end of file encountered
1314 afs_tf_get_cred(struct ktc_principal *principal, struct ktc_token *token)
1318 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1323 if ((k_errno = tf_gets(principal->name, MAXKTCNAMELEN)) < 2)
1326 case 1: /* can't be just a null */
1331 if ((k_errno = tf_gets(principal->instance, MAXKTCNAMELEN)) < 1)
1338 if ((k_errno = tf_gets(principal->cell, MAXKTCREALMLEN)) < 2)
1341 case 1: /* can't be just a null */
1346 lcstring(principal->cell, principal->cell, MAXKTCREALMLEN);
1347 if (tf_read((char *)&(token->sessionKey), 8) < 1
1348 || tf_read((char *)&(lifetime), sizeof(lifetime)) < 1
1349 || tf_read((char *)&(kvno), sizeof(kvno)) < 1
1350 || tf_read((char *)&(token->ticketLen), sizeof(token->ticketLen))
1352 /* don't try to read a silly amount into ticket->dat */
1353 token->ticketLen > MAXKTCTICKETLEN
1354 || tf_read((char *)(token->ticket), token->ticketLen) < 1
1355 || tf_read((char *)&mit_compat, sizeof(mit_compat)) < 1) {
1358 token->startTime = mit_compat;
1359 token->endTime = life_to_time(token->startTime, lifetime);
1365 * tf_close() closes the ticket file and sets "fd" to -1. If "fd" is
1366 * not a valid file descriptor, it just returns. It also clears the
1367 * buffer used to read tickets.
1369 * The return value is not defined.
1376 #if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)
1377 (void)fcntl(fd, F_SETLK, &fileUlock);
1379 (void)flock(fd, LOCK_UN);
1382 fd = -1; /* see declaration of fd above */
1384 memset(tfbfr, 0, sizeof(tfbfr));
1389 * tf_gets() is an internal routine. It takes a string "s" and a count
1390 * "n", and reads from the file until either it has read "n" characters,
1391 * or until it reads a null byte. When finished, what has been read exists
1394 * Possible return values are:
1396 * n the number of bytes read (including null terminator)
1397 * when all goes well
1399 * 0 end of file or read error
1401 * TOO_BIG if "count" characters are read and no null is
1402 * encountered. This is an indication that the ticket
1403 * file is seriously ill.
1407 tf_gets(char *s, int n)
1414 for (count = n - 1; count > 0; --count) {
1415 if (curpos >= sizeof(tfbfr)) {
1416 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1419 if (curpos == lastpos) {
1422 *s = tfbfr[curpos++];
1430 * tf_read() is an internal routine. It takes a string "s" and a count
1431 * "n", and reads from the file until "n" bytes have been read. When
1432 * finished, what has been read exists in "s".
1434 * Possible return values are:
1436 * n the number of bytes read when all goes well
1438 * 0 on end of file or read error
1442 tf_read(char *s, int n)
1446 for (count = n; count > 0; --count) {
1447 if (curpos >= sizeof(tfbfr)) {
1448 lastpos = read(fd, tfbfr, sizeof(tfbfr));
1451 if (curpos == lastpos) {
1454 *s++ = tfbfr[curpos++];
1460 * afs_tf_save_cred() appends an incoming ticket to the end of the ticket
1461 * file. You must call afs_tf_init() before calling afs_tf_save_cred().
1463 * The "service", "instance", and "realm" arguments specify the
1464 * server's name; "aticket" contains the credential.
1466 * Returns 0 if all goes well, TKT_FIL_INI if afs_tf_init() wasn't
1467 * called previously, and KFAILURE for anything else that went wrong.
1471 afs_tf_save_cred(struct ktc_principal *aserver,
1472 struct ktc_token *atoken,
1473 struct ktc_principal *aclient)
1475 char realm[MAXKTCREALMLEN + 1];
1476 char junk[MAXKTCNAMELEN];
1477 struct ktc_principal principal;
1478 struct ktc_token token;
1482 int count; /* count for write */
1483 long mit_compat; /* MIT Kerberos 5 with Krb4 uses a "long" for issue_date */
1485 if (fd < 0) { /* fd is ticket file as set by afs_tf_init */
1489 ucstring(realm, aserver->cell, MAXKTCREALMLEN);
1490 realm[MAXKTCREALMLEN] = '\0';
1492 /* Look for a duplicate ticket */
1493 (void)lseek(fd, (off_t) 0L, 0);
1494 curpos = sizeof(tfbfr);
1496 if (afs_tf_get_pname(junk) || strcmp(junk, aclient->name)
1497 || afs_tf_get_pinst(junk) || strcmp(junk, aclient->instance))
1501 start = lseek(fd, (off_t) 0L, 1) - lastpos + curpos;
1502 status = afs_tf_get_cred(&principal, &token);
1503 } while (status == 0
1504 && (strcmp(aserver->name, principal.name) != 0
1505 || strcmp(aserver->instance, principal.instance) != 0
1506 || strcmp(aserver->cell, principal.cell) != 0));
1509 * Two tickets for the same user authenticating to the same service
1510 * should be the same length, but we check here just to make sure.
1512 if (status == 0 && token.ticketLen != atoken->ticketLen)
1514 if (status && status != EOF)
1517 /* Position over the credential we just matched (or the EOF) */
1518 lseek(fd, start, 0);
1519 curpos = lastpos = sizeof(tfbfr);
1521 /* Write the ticket and associated data */
1523 count = strlen(aserver->name) + 1;
1524 if (write(fd, aserver->name, count) != count)
1527 count = strlen(aserver->instance) + 1;
1528 if (write(fd, aserver->instance, count) != count)
1531 count = strlen(realm) + 1;
1532 if (write(fd, realm, count) != count)
1535 if (write(fd, (char *)&atoken->sessionKey, 8) != 8)
1538 lifetime = time_to_life(atoken->startTime, atoken->endTime);
1539 if (write(fd, (char *)&lifetime, sizeof(int)) != sizeof(int))
1542 kvno = atoken->kvno;
1543 if (write(fd, (char *)&kvno, sizeof(int)) != sizeof(int))
1546 if (write(fd, (char *)&(atoken->ticketLen), sizeof(int)) != sizeof(int))
1549 count = atoken->ticketLen;
1550 if (write(fd, atoken->ticket, count) != count)
1553 mit_compat = atoken->startTime;
1554 if (write(fd, (char *)&mit_compat, sizeof(mit_compat))
1555 != sizeof(mit_compat))
1558 /* Actually, we should check each write for success */
1565 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
1568 * For copying and distribution information, please see the file
1569 * <mit-copyright.h>.
1573 * This routine is used to generate the name of the file that holds
1574 * the user's cache of server tickets and associated session keys.
1576 * If it is set, krb_ticket_string contains the ticket file name.
1577 * Otherwise, the filename is constructed as follows:
1579 * If it is set, the environment variable "KRBTKFILE" will be used as
1580 * the ticket file name. Otherwise TKT_ROOT (defined in "krb.h") and
1581 * the user's uid are concatenated to produce the ticket file name
1582 * (e.g., "/tmp/tkt123"). A pointer to the string containing the ticket
1583 * file name is returned.
1586 static char krb_ticket_string[4096] = "";
1589 ktc_tkt_string(void)
1591 return ktc_tkt_string_uid(getuid());
1595 ktc_tkt_string_uid(afs_uint32 uid)
1600 if (!*krb_ticket_string) {
1601 if ((env = getenv("KRBTKFILE"))) {
1602 (void)strncpy(krb_ticket_string, env,
1603 sizeof(krb_ticket_string) - 1);
1604 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1606 /* 32 bits of signed integer will always fit in 11 characters
1607 * (including the sign), so no need to worry about overflow */
1608 (void)sprintf(krb_ticket_string, "%s%d", TKT_ROOT, uid);
1611 UNLOCK_GLOBAL_MUTEX;
1612 return krb_ticket_string;
1616 * This routine is used to set the name of the file that holds the user's
1617 * cache of server tickets and associated session keys.
1619 * The value passed in is copied into local storage.
1621 * NOTE: This routine should be called during initialization, before other
1622 * Kerberos routines are called; otherwise tkt_string() above may be called
1623 * and return an undesired ticket file name until this routine is called.
1627 ktc_set_tkt_string(char * val)
1631 (void)strncpy(krb_ticket_string, val, sizeof(krb_ticket_string) - 1);
1632 krb_ticket_string[sizeof(krb_ticket_string) - 1] = '\0';
1633 UNLOCK_GLOBAL_MUTEX;
1638 * tf_create() is used to initialize the ticket store. It creates the
1639 * file to contain the tickets and writes the given user's name "pname"
1640 * and instance "pinst" in the file. in_tkt() returns KSUCCESS on
1641 * success, or KFAILURE if something goes wrong.
1645 afs_tf_create(char *pname, char *pinst)
1650 char *file = ktc_tkt_string();
1659 if (lstat(file, &sbuf) == 0) {
1660 if ((sbuf.st_uid != me && me != 0)
1661 || ((sbuf.st_mode & S_IFMT) != S_IFREG) || sbuf.st_mode & 077) {
1664 /* file already exists, and permissions appear ok, so nuke it */
1665 if ((fd = open(file, O_RDWR, 0)) < 0)
1666 goto out; /* can't zero it, but we can still try truncating it */
1668 memset(zerobuf, 0, sizeof(zerobuf));
1670 for (i = 0; i < sbuf.st_size; i += sizeof(zerobuf))
1671 if (write(fd, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf)) {
1682 /* arrange so the file is owned by the ruid
1683 * (swap real & effective uid if necessary).
1684 * This isn't a security problem, since the ticket file, if it already
1685 * exists, has the right uid (== ruid) and mode. */
1687 if (setreuid(metoo, me) < 0) {
1691 tktfile = creat(file, 0600);
1693 if (setreuid(me, metoo) < 0) {
1694 /* can't switch??? fail! */
1701 count = strlen(pname) + 1;
1702 if (write(tktfile, pname, count) != count) {
1703 (void)close(tktfile);
1706 count = strlen(pinst) + 1;
1707 if (write(tktfile, pinst, count) != count) {
1708 (void)close(tktfile);
1711 (void)close(tktfile);
1716 * dest_tkt() is used to destroy the ticket store upon logout.
1717 * If the ticket file does not exist, dest_tkt() returns RET_TKFIL.
1718 * Otherwise the function returns 0 on success, KFAILURE on
1723 afs_tf_dest_tkt(void)
1725 char *file = ktc_tkt_string();
1731 if (lstat(file, &statb) < 0)
1734 if (!(statb.st_mode & S_IFREG))
1737 if ((fd = open(file, O_RDWR, 0)) < 0)
1740 memset(buf, 0, BUFSIZ);
1742 for (i = 0; i < statb.st_size; i += BUFSIZ)
1743 if (write(fd, buf, BUFSIZ) != BUFSIZ) {
1755 if (errno == ENOENT)
1757 else if (errno != 0)
1765 #if !defined(AFS_DARWIN100_ENV) || defined(HAVE_CRT_EXTERNS_H)
1766 # if defined(AFS_DARWIN100_ENV)
1767 # define environ (*_NSGetEnviron())
1769 extern char **environ;
1774 char fname[256], *prefix = "/ticket/";
1775 char fname5[256], *prefix5 = "FILE:/ticket/krb5cc_";
1777 char **newenv, **senv, **denv;
1780 if (stat("/ticket", &sbuf) == -1) {
1781 prefix = "/tmp/tkt";
1782 prefix5 = "FILE:/tmp/krb5cc_";
1785 pag = ktc_curpag() & 0xffffffff;
1787 sprintf(fname, "%s%d", prefix, getuid());
1788 sprintf(fname5, "%s%d", prefix5, getuid());
1790 sprintf(fname, "%sp%lu", prefix, afs_printable_uint32_lu(pag));
1791 sprintf(fname5, "%sp%lud", prefix5, afs_printable_uint32_lu(pag));
1793 ktc_set_tkt_string(fname);
1795 for (senv = environ, numenv = 0; *senv; senv++)
1797 newenv = (char **)malloc((numenv + 2) * sizeof(char *));
1799 for (senv = environ, denv = newenv; *senv; senv++) {
1800 if (strncmp(*senv, "KRBTKFILE=", 10) != 0 &&
1801 strncmp(*senv, "KRB5CCNAME=", 11) != 0)
1805 *denv = malloc(10+11 + strlen(fname) + strlen(fname5) + 2);
1806 strcpy(*denv, "KRBTKFILE=");
1807 strcat(*denv, fname);
1808 *(denv+1) = *denv + strlen(*denv) + 1;
1810 strcpy(*denv, "KRB5CCNAME=");
1811 strcat(*denv, fname5);
1814 UNLOCK_GLOBAL_MUTEX;
1820 * BLETCH! We have to invoke the entire afsconf package just to
1821 * find out what the local cell is.
1827 struct afsconf_dir *conf;
1829 if ((conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))
1830 || (conf = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH))) {
1831 code = afsconf_GetLocalCell(conf, lcell, sizeof(lcell));
1832 afsconf_Close(conf);
1834 if (!conf || code) {
1835 printf("** Can't determine local cell name!\n");