4 * Copyright 1990,1991 by the Massachusetts Institute of Technology
5 * For distribution and copying rights, see the file "mit-copyright.h"
8 #if !defined(lint) && !defined(SABER)
11 #endif /* lint || SABER */
16 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/errno.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
41 /* on AIX AFS has an unresolved reference to osi_audit. We will define
42 * it here as extern. It also trys to call the ntohl and htonl routines
43 * as routines rather then macros. We need a real routine here.
44 * We do this before the ntohl and htonl macros are defined in net/in.h
51 u_long htonl(u_long x)
54 u_long ntohl(u_long x)
58 #include <netinet/in.h>
59 /* #include <krb.h> */
69 #include <afs/dirpath.h>
73 #include <afs/com_err.h>
75 #include <afs/param.h>
77 #include <sys/ioccom.h>
80 #include <afs/cellconfig.h>
82 #include <afs/venus.h>
83 #include <afs/ptserver.h>
84 #include <afs/ptuser.h>
85 #include <afs/dirpath.h>
89 #include "linked_list.h"
94 #ifndef AFS_TRY_FULL_PRINC
95 #define AFS_TRY_FULL_PRINC 1
96 #endif /* AFS_TRY_FULL_PRINC */
98 #define AKLOG_SUCCESS 0
100 #define AKLOG_SOMETHINGSWRONG 2
102 #define AKLOG_KERBEROS 4
103 #define AKLOG_TOKEN 5
104 #define AKLOG_BADPATH 6
120 /* RedHat 4.x doesn't seem to define this */
121 #define MAXSYMLINKS 5
124 #define DIR '/' /* Character that divides directories */
125 #define DIRSTRING "/" /* String form of above */
126 #define VOLMARKER ':' /* Character separating cellname from mntpt */
127 #define VOLMARKERSTRING ":" /* String form of above */
131 char realm[REALM_SZ];
134 struct afsconf_cell ak_cellconfig; /* General information about the cell */
135 static char linkedcell[MAXCELLCHARS+1];
136 static char linkedcell2[MAXCELLCHARS+1];
137 static krb5_ccache _krb425_ccache = NULL;
142 extern long cm_GetRootCellName();
143 extern long cm_SearchCellFile();
145 static long cm_SearchCellFile_CallBack();
150 * Why doesn't AFS provide these prototypes?
153 extern int pioctl(char *, afs_int32, struct ViceIoctl *, afs_int32);
159 extern char *afs_realm_of_cell(krb5_context, struct afsconf_cell *);
160 static int isdir(char *, unsigned char *);
161 static krb5_error_code get_credv5(krb5_context context, char *, char *,
162 char *, krb5_creds **);
163 static int get_user_realm(krb5_context, char *);
165 #if defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
167 #define get_princ_str(c, p, n) krb5_princ_component(c, p, n)->data
168 #define get_princ_len(c, p, n) krb5_princ_component(c, p, n)->length
169 #define second_comp(c, p) (krb5_princ_size(c, p) > 1)
170 #define realm_data(c, p) krb5_princ_realm(c, p)->data
171 #define realm_len(c, p) krb5_princ_realm(c, p)->length
173 #elif defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
175 #define get_princ_str(c, p, n) krb5_principal_get_comp_string(c, p, n)
176 #define get_princ_len(c, p, n) strlen(krb5_principal_get_comp_string(c, p, n))
177 #define second_comp(c, p) (krb5_principal_get_comp_string(c, p, 1) != NULL)
178 #define realm_data(c, p) krb5_realm_data(krb5_principal_get_realm(c, p))
179 #define realm_len(c, p) krb5_realm_length(krb5_principal_get_realm(c, p))
182 #error "Must have either krb5_princ_size or krb5_principal_get_comp_string"
185 #if defined(HAVE_KRB5_CREDS_KEYBLOCK)
187 #define get_cred_keydata(c) c->keyblock.contents
188 #define get_cred_keylen(c) c->keyblock.length
189 #define get_creds_enctype(c) c->keyblock.enctype
191 #elif defined(HAVE_KRB5_CREDS_SESSION)
193 #define get_cred_keydata(c) c->session.keyvalue.data
194 #define get_cred_keylen(c) c->session.keyvalue.length
195 #define get_creds_enctype(c) c->session.keytype
198 #error "Must have either keyblock or session member of krb5_creds"
201 #if !defined(HAVE_KRB5_524_CONVERT_CREDS) && defined(HAVE_KRB524_CONVERT_CREDS_KDC)
202 #define krb5_524_convert_creds krb524_convert_creds_kdc
203 #elif !defined(HAVE_KRB5_524_CONVERT_CREDS) && !defined(HAVE_KRB524_CONVERT_CREDS_KDC)
204 #error "You must have one of krb5_524_convert_creds or krb524_convert_creds_kdc available"
210 * Provide a replacement for strerror if we don't have it
213 #ifndef HAVE_STRERROR
214 extern char *sys_errlist[];
215 #define strerror(x) sys_errlist[x]
216 #endif /* HAVE_STRERROR */
218 static char *progname = NULL; /* Name of this program */
219 static int dflag = FALSE; /* Give debugging information */
220 static int noauth = FALSE; /* If true, don't try to get tokens */
221 static int zsubs = FALSE; /* Are we keeping track of zephyr subs? */
222 static int hosts = FALSE; /* Are we keeping track of hosts? */
223 static int noprdb = FALSE; /* Skip resolving name to id? */
224 static int linked = FALSE; /* try for both AFS nodes */
225 static int afssetpag = FALSE; /* setpag for AFS */
226 static int force = FALSE; /* Bash identical tokens? */
227 static int do524 = FALSE; /* Should we do 524 instead of rxkad2b? */
228 static linked_list zsublist; /* List of zephyr subscriptions */
229 static linked_list hostlist; /* List of host addresses */
230 static linked_list authedcells; /* List of cells already logged to */
232 /* ANL - CMU lifetime convert routine */
233 /* for K5.4.1 don't use this for now. Need to see if it is needed */
234 /* maybe needed in the krb524d module as well */
235 /* extern unsigned long krb_life_to_time(); */
237 static char *copy_cellinfo(cellinfo_t *cellinfo)
239 cellinfo_t *new_cellinfo;
241 if ((new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t))))
242 memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
244 return ((char *)new_cellinfo);
248 static char *copy_string(char *string)
252 if ((new_string = (char *)calloc(strlen(string) + 1, sizeof(char))))
253 (void) strcpy(new_string, string);
259 static int get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char *local_cell, char *linkedcell)
261 int status = AKLOG_SUCCESS;
262 struct afsconf_dir *configdir;
264 memset(local_cell, 0, sizeof(local_cell));
265 memset((char *)cellconfig, 0, sizeof(*cellconfig));
269 if (!(configdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
271 "%s: can't get afs configuration (afsconf_Open(%s))\n",
272 progname, AFSDIR_CLIENT_ETC_DIRPATH);
276 if (afsconf_GetLocalCell(configdir, local_cell, MAXCELLCHARS)) {
277 fprintf(stderr, "%s: can't determine local cell.\n", progname);
281 if ((cell == NULL) || (cell[0] == 0))
284 linkedcell[0] = '\0';
285 if (afsconf_GetCellInfo(configdir, cell, NULL, cellconfig)) {
286 fprintf(stderr, "%s: Can't get information about cell %s.\n",
290 if (cellconfig->linkedCell)
291 strncpy(linkedcell,cellconfig->linkedCell,MAXCELLCHARS);
293 (void) afsconf_Close(configdir);
297 * We'll try to mimic the GetCellInfo call here and fill in as much
298 * of the afsconf_cell structure as we can.
300 if (cm_GetRootCellName(local_cell)) {
301 fprintf(stderr, "%s: can't get local cellname\n", progname);
305 if ((cell == NULL) || (cell[0] == 0))
308 strcpy(cellconfig->name, cell);
310 /* No way of figuring this out as far as I can tell */
311 linkedcell[0] = '\0';
313 /* Initialize server info */
314 cellconfig->numServers = 0;
315 cellconfig->hostName[0][0] = "\0";
318 * Get servers of cell. cm_SearchCellFile_CallBack() gets call with
321 status = (int) cm_SearchCellFile(cell, NULL, &cm_SearchCellFile_CallBack,
322 cellconfig /* rock */);
329 fprintf(stderr, "%s: GetWindowsDirectory() failed.\n", progname);
333 fprintf(stderr, "%s: Couldn't open afsdcells.ini for reading\n",
338 fprintf(stderr, "%s: Couldn't find any servers for cell %s\n",
343 fprintf(stderr, "%s: Badly formatted line in afsdcells.ini (does not begin with a \">\" or contain \"#\"\n",
348 fprintf(stderr, "%s cm_SearchCellFile returned unknown error %d\n",
356 status = AKLOG_SUCCESS;
367 * Callback function for cm_SearchCellFile() in get_cellconfig() above.
368 * This function gets called once for each server that is found for the cell.
371 cm_SearchCellFile_CallBack(void *rock /* cellconfig */,
372 struct sockaddr_in *addr, /* Not used */
375 struct afsconf_cell *cellconfig = rock;
379 * Save server name and increment count of servers
381 strcpy(cellconfig->hostName[cellconfig->numServers++], server);
391 * Log to a cell. If the cell has already been logged to, return without
392 * doing anything. Otherwise, log to it and mark that it has been logged
395 static int auth_to_cell(krb5_context context, char *cell, char *realm)
397 int status = AKLOG_SUCCESS;
398 char username[BUFSIZ]; /* To hold client username structure */
399 afs_int32 viceId; /* AFS uid of user */
401 char name[ANAME_SZ]; /* Name of afs key */
402 char primary_instance[INST_SZ]; /* Instance of afs key */
403 char secondary_instance[INST_SZ]; /* Backup instance to try */
404 int try_secondary = 0; /* Flag to indicate if we try second */
405 char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
406 char realm_of_cell[REALM_SZ]; /* Kerberos realm of cell */
407 char local_cell[MAXCELLCHARS+1];
408 char cell_to_use[MAXCELLCHARS+1]; /* Cell to authenticate to */
409 static char lastcell[MAXCELLCHARS+1] = { 0 };
411 static char confname[512] = { 0 };
413 krb5_creds *v5cred = NULL;
414 struct ktc_principal aserver;
415 struct ktc_principal aclient;
416 struct ktc_token atoken, btoken;
418 #ifdef ALLOW_REGISTER
420 #endif /* ALLOW_REGISTER */
422 memset(name, 0, sizeof(name));
423 memset(primary_instance, 0, sizeof(primary_instance));
424 memset(secondary_instance, 0, sizeof(secondary_instance));
425 memset(realm_of_user, 0, sizeof(realm_of_user));
426 memset(realm_of_cell, 0, sizeof(realm_of_cell));
429 if (confname[0] == '\0') {
430 strncpy(confname, AFSDIR_CLIENT_ETC_DIRPATH, sizeof(confname));
431 confname[sizeof(confname) - 2] = '\0';
435 /* NULL or empty cell returns information on local cell */
436 if ((status = get_cellconfig(cell, &ak_cellconfig,
437 local_cell, linkedcell)))
440 strncpy(cell_to_use, ak_cellconfig.name, MAXCELLCHARS);
441 cell_to_use[MAXCELLCHARS] = 0;
443 if (ll_string(&authedcells, ll_s_check, cell_to_use)) {
445 printf("Already authenticated to %s (or tried to)\n",
448 return(AKLOG_SUCCESS);
452 * Record that we have attempted to log to this cell. We do this
453 * before we try rather than after so that we will not try
454 * and fail repeatedly for one cell.
456 (void)ll_string(&authedcells, ll_s_add, cell_to_use);
459 * Record this cell in the list of zephyr subscriptions. We may
460 * want zephyr subscriptions even if authentication fails.
461 * If this is done after we attempt to get tokens, aklog -zsubs
462 * can return something different depending on whether or not we
463 * are in -noauth mode.
465 if (ll_string(&zsublist, ll_s_add, cell_to_use) == LL_FAILURE) {
467 "%s: failure adding cell %s to zephyr subscriptions list.\n",
468 progname, cell_to_use);
471 if (ll_string(&zsublist, ll_s_add, local_cell) == LL_FAILURE) {
473 "%s: failure adding cell %s to zephyr subscriptions list.\n",
474 progname, local_cell);
480 printf("Authenticating to cell %s (server %s).\n",
481 cell_to_use, ak_cellconfig.hostName[0]);
485 * Find out which realm we're supposed to authenticate to. If one
486 * is not included, use the kerberos realm found in the credentials
490 if (realm && realm[0]) {
491 strcpy(realm_of_cell, realm);
493 printf("We were told to authenticate to realm %s.\n", realm);
497 char *realm = afs_realm_of_cell(context, &ak_cellconfig);
501 "%s: Couldn't figure out realm for cell %s.\n",
502 progname, cell_to_use);
506 strcpy(realm_of_cell, realm);
509 printf("We've deduced that we need to authenticate to"
510 " realm %s.\n", realm_of_cell);
514 /* We use the afs.<cellname> convention here...
516 * Doug Engert's original code had principals of the form:
520 * in the KDC, so the name wouldn't conflict with DFS. Since we're
521 * not using DFS, I changed it just to look for the following
527 * Because people are transitioning from afs@realm to afs/cell,
528 * we configure things so that if the first one isn't found, we
529 * try the second one. You can select which one you prefer with
530 * a configure option.
533 strcpy(name, AFSKEY);
535 if (AFS_TRY_FULL_PRINC || strcasecmp(cell_to_use, realm_of_cell) != 0) {
536 strncpy(primary_instance, cell_to_use, sizeof(primary_instance));
537 primary_instance[sizeof(primary_instance)-1] = '\0';
538 if (strcasecmp(cell_to_use, realm_of_cell) == 0) {
540 secondary_instance[0] = '\0';
543 primary_instance[0] = '\0';
545 strncpy(secondary_instance, cell_to_use,
546 sizeof(secondary_instance));
547 secondary_instance[sizeof(secondary_instance)-1] = '\0';
551 * Extract the session key from the ticket file and hand-frob an
552 * afs style authenticator.
556 * Try to obtain AFS tickets. Because there are two valid service
557 * names, we will try both, but trying the more specific first.
559 * afs/<cell>@<realm> i.e. allow for single name with "."
564 printf("Getting tickets: %s/%s@%s\n", name,
565 primary_instance, realm_of_cell);
568 status = get_credv5(context, name, primary_instance, realm_of_cell,
571 if (status == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN) {
574 printf("Principal not found, trying alternate "
575 "service name: %s/%s@%s\n", name,
576 secondary_instance, realm_of_cell);
578 status = get_credv5(context, name, secondary_instance,
579 realm_of_cell, &v5cred);
585 printf("Kerberos error code returned by get_cred: %d\n",
588 fprintf(stderr, "%s: Couldn't get %s AFS tickets:\n",
589 progname, cell_to_use);
590 com_err(progname, status, "while getting AFS tickets");
591 return(AKLOG_KERBEROS);
594 strncpy(aserver.name, AFSKEY, MAXKTCNAMELEN - 1);
595 strncpy(aserver.instance, AFSINST, MAXKTCNAMELEN - 1);
596 strncpy(aserver.cell, cell_to_use, MAXKTCREALMLEN - 1);
599 * The default is to use rxkad2b, which means we put in a full
600 * V5 ticket. If the user specifies -524, we talk to the
601 * 524 ticket converter.
609 printf("Using Kerberos V5 ticket natively\n");
611 len = min(get_princ_len(context, v5cred->client, 0),
612 second_comp(context, v5cred->client) ?
613 MAXKTCNAMELEN - 2 : MAXKTCNAMELEN - 1);
614 strncpy(username, get_princ_str(context, v5cred->client, 0), len);
615 username[len] = '\0';
617 if (second_comp(context, v5cred->client)) {
618 strcat(username, ".");
619 p = username + strlen(username);
620 len = min(get_princ_len(context, v5cred->client, 1),
621 MAXKTCNAMELEN - strlen(username) - 1);
622 strncpy(p, get_princ_str(context, v5cred->client, 1), len);
626 memset(&atoken, 0, sizeof(atoken));
627 atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
628 atoken.startTime = v5cred->times.starttime;;
629 atoken.endTime = v5cred->times.endtime;
630 memcpy(&atoken.sessionKey, get_cred_keydata(v5cred),
631 get_cred_keylen(v5cred));
632 atoken.ticketLen = v5cred->ticket.length;
633 memcpy(atoken.ticket, v5cred->ticket.data, atoken.ticketLen);
638 printf("Using Kerberos 524 translator service\n");
640 status = krb5_524_convert_creds(context, v5cred, &cred);
643 com_err(progname, status, "while converting tickets "
644 "to Kerberos V4 format");
645 return(AKLOG_KERBEROS);
648 strcpy (username, cred.pname);
650 strcat (username, ".");
651 strcat (username, cred.pinst);
654 atoken.kvno = cred.kvno;
655 atoken.startTime = cred.issue_date;
657 * It seems silly to go through a bunch of contortions to
658 * extract the expiration time, when the v5 credentials already
659 * has the exact time! Let's use that instead.
661 * Note that this isn't a security hole, as the expiration time
662 * is also contained in the encrypted token
664 atoken.endTime = v5cred->times.endtime;
665 memcpy(&atoken.sessionKey, cred.session, 8);
666 atoken.ticketLen = cred.ticket_st.length;
667 memcpy(atoken.ticket, cred.ticket_st.dat, atoken.ticketLen);
671 !ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient) &&
672 atoken.kvno == btoken.kvno &&
673 atoken.ticketLen == btoken.ticketLen &&
674 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
675 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen)) {
678 printf("Identical tokens already exist; skipping.\n");
691 printf("Not resolving name %s to id (-noprdb set)\n",
697 if ((status = get_user_realm(context, realm_of_user))) {
698 fprintf(stderr, "%s: Couldn't determine realm of user:)",
700 com_err(progname, status, " while getting realm");
701 return(AKLOG_KERBEROS);
703 if (strcmp(realm_of_user, realm_of_cell)) {
704 strcat(username, "@");
705 strcat(username, realm_of_user);
709 printf("About to resolve name %s to id in cell %s.\n",
710 username, aserver.cell);
713 strcpy(lastcell, aserver.cell);
715 if (!pr_Initialize (0, confname, aserver.cell))
716 status = pr_SNameToId (username, &viceId);
720 printf("Error %d\n", status);
722 printf("Id %d\n", (int) viceId);
726 * This is a crock, but it is Transarc's crock, so
727 * we have to play along in order to get the
728 * functionality. The way the afs id is stored is
729 * as a string in the username field of the token.
730 * Contrary to what you may think by looking at
731 * the code for tokens, this hack (AFS ID %d) will
732 * not work if you change %d to something else.
736 * This code is taken from cklog -- it lets people
737 * automatically register with the ptserver in foreign cells
740 #ifdef ALLOW_REGISTER
742 if (viceId != ANONYMOUSID) {
743 #else /* ALLOW_REGISTER */
744 if ((status == 0) && (viceId != ANONYMOUSID))
745 #endif /* ALLOW_REGISTER */
746 sprintf (username, "AFS ID %d", (int) viceId);
747 #ifdef ALLOW_REGISTER
748 } else if (strcmp(realm_of_user, realm_of_cell) != 0) {
750 printf("doing first-time registration of %s "
751 "at %s\n", username, cell_to_use);
754 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
755 strcpy(aclient.instance, "");
756 strncpy(aclient.cell, realm_of_user, MAXKTCREALMLEN - 1);
757 if ((status = ktc_SetToken(&aserver, &atoken, &aclient, 0))) {
758 fprintf(stderr, "%s: unable to obtain tokens for cell %s "
759 "(status: %d).\n", progname, cell_to_use, status);
760 status = AKLOG_TOKEN;
764 * In case you're wondering, we don't need to change the
765 * filename here because we're still connecting to the
766 * same cell -- we're just using a different authentication
770 if ((status = pr_Initialize(1L, confname, aserver.cell))) {
771 printf("Error %d\n", status);
774 if ((status = pr_CreateUser(username, &id))) {
775 fprintf(stderr, "%s: %s so unable to create remote PTS "
776 "user %s in cell %s (status: %d).\n", progname,
777 error_message(status), username, cell_to_use,
780 printf("created cross-cell entry for %s at %s\n",
781 username, cell_to_use);
782 sprintf(username, "AFS ID %d", (int) id);
786 #endif /* ALLOW_REGISTER */
789 #endif /* !WINDOWS */
792 fprintf(stdout, "Set username to %s\n", username);
795 /* Reset the "aclient" structure before we call ktc_SetToken.
796 * This structure was first set by the ktc_GetToken call when
797 * we were comparing whether identical tokens already existed.
799 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
800 strcpy(aclient.instance, "");
801 strncpy(aclient.cell, realm_of_user, MAXKTCREALMLEN - 1);
804 printf("Setting tokens. %s / %s @ %s \n",
805 aclient.name, aclient.instance, aclient.cell );
807 /* on AIX 4.1.4 with AFS 3.4a+ if a write is not done before
808 * this routine, it will not add the token. It is not clear what
809 * is going on here! So we will do the following operation
811 write(2,"",0); /* dummy write */
813 if ((status = ktc_SetToken(&aserver, &atoken, &aclient, afssetpag))) {
815 "%s: unable to obtain tokens for cell %s (status: %d).\n",
816 progname, cell_to_use, status);
817 status = AKLOG_TOKEN;
820 /* Note switched 2nd and 3rd args */
821 if ((status = ktc_SetToken(&aserver, &atoken, &aclient, afssetpag))) {
824 fprintf(stderr, "%s: Bad ticket length", progname);
827 fprintf(stderr, "%s: Unknown error contacting AFS service",
831 fprintf(stderr, "%s: Cell name (%s) not recognized by AFS service",
832 progname, realm_of_cell);
835 fprintf(stderr, "%s: AFS service is unavailable", progname);
838 fprintf(stderr, "%s: Undocumented error (%d) contacting AFS service", progname, status);
841 status = AKLOG_TOKEN;
843 #endif /* !WINDOWS */
847 printf("Noauth mode; not authenticating.\n");
853 #ifndef WINDOWS /* struct ViceIoctl missing */
855 static int get_afs_mountpoint(char *file, char *mountpoint, int size)
858 char V ='V'; /* AFS has problem on Sun with pioctl */
860 char our_file[MAXPATHLEN + 1];
862 char *last_component;
863 struct ViceIoctl vio;
864 char cellname[BUFSIZ];
866 memset(our_file, 0, sizeof(our_file));
867 strcpy(our_file, file);
869 if ((last_component = strrchr(our_file, DIR))) {
870 *last_component++ = 0;
871 parent_dir = our_file;
874 last_component = our_file;
878 memset(cellname, 0, sizeof(cellname));
880 vio.in = last_component;
881 vio.in_size = strlen(last_component)+1;
883 vio.out = mountpoint;
885 if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0)) {
886 if (strchr(mountpoint, VOLMARKER) == NULL) {
888 vio.in_size = strlen(file) + 1;
889 vio.out_size = sizeof(cellname);
892 if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1)) {
893 strcat(cellname, VOLMARKERSTRING);
894 strcat(cellname, mountpoint + 1);
895 memset(mountpoint + 1, 0, size - 1);
896 strcpy(mountpoint + 1, cellname);
906 * This routine each time it is called returns the next directory
907 * down a pathname. It resolves all symbolic links. The first time
908 * it is called, it should be called with the name of the path
909 * to be descended. After that, it should be called with the arguemnt
912 static char *next_path(char *origpath)
914 static char path[MAXPATHLEN + 1];
915 static char pathtocheck[MAXPATHLEN + 1];
917 int link = FALSE; /* Is this a symbolic link? */
918 char linkbuf[MAXPATHLEN + 1];
919 char tmpbuf[MAXPATHLEN + 1];
921 static char *last_comp; /* last component of directory name */
922 static char *elast_comp; /* End of last component */
926 static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
928 /* If we are given something for origpath, we are initializing only. */
930 memset(path, 0, sizeof(path));
931 memset(pathtocheck, 0, sizeof(pathtocheck));
932 strcpy(path, origpath);
938 /* We were not given origpath; find then next path to check */
940 /* If we've gotten all the way through already, return NULL */
941 if (last_comp == NULL)
945 while (*last_comp == DIR)
946 strncat(pathtocheck, last_comp++, 1);
947 len = (elast_comp = strchr(last_comp, DIR))
948 ? elast_comp - last_comp : strlen(last_comp);
949 strncat(pathtocheck, last_comp, len);
950 memset(linkbuf, 0, sizeof(linkbuf));
951 if ((link = readlink(pathtocheck, linkbuf,
952 sizeof(linkbuf))) > 0) {
953 if (++symlinkcount > MAXSYMLINKS) {
954 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
957 memset(tmpbuf, 0, sizeof(tmpbuf));
959 strcpy(tmpbuf, elast_comp);
960 if (linkbuf[0] == DIR) {
962 * If this is a symbolic link to an absolute path,
963 * replace what we have by the absolute path.
965 memset(path, 0, strlen(path));
966 memcpy(path, linkbuf, sizeof(linkbuf));
967 strcat(path, tmpbuf);
970 memset(pathtocheck, 0, sizeof(pathtocheck));
974 * If this is a symbolic link to a relative path,
975 * replace only the last component with the link name.
977 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
978 strcat(path, tmpbuf);
980 if ((t = strrchr(pathtocheck, DIR))) {
982 memset(t, 0, strlen(t));
985 memset(pathtocheck, 0, sizeof(pathtocheck));
989 last_comp = elast_comp;
999 /*****************************************/
1003 char V = 'V'; /* AFS has problem on SunOS */
1005 struct ViceIoctl vio;
1006 char outbuf[BUFSIZ];
1010 memset(outbuf, 0, sizeof(outbuf));
1012 vio.out_size = sizeof(outbuf);
1013 vio.in_size = sizeof(ind);
1018 fd = open("dee.tok",O_WRONLY);
1019 while(!pioctl(0,VIOCGETTOK,&vio,0)) {
1020 write(fd,&outbuf,sizeof(outbuf));
1025 /*****************************************/
1028 #ifndef WINDOWS /* struct ViceIoctl missing */
1030 static void add_hosts(char *file)
1033 char V = 'V'; /* AFS has problem on SunOS */
1035 struct ViceIoctl vio;
1036 char outbuf[BUFSIZ];
1042 memset(outbuf, 0, sizeof(outbuf));
1044 vio.out_size = sizeof(outbuf);
1049 printf("Getting list of hosts for %s\n", file);
1051 /* Don't worry about errors. */
1052 if (!pioctl(file, VIOCWHEREIS, &vio, 1)) {
1053 phosts = (long *) outbuf;
1056 * Lists hosts that we care about. If ALLHOSTS is defined,
1057 * then all hosts that you ever may possible go through are
1058 * included in this list. If not, then only hosts that are
1059 * the only ones appear. That is, if a volume you must use
1060 * is replaced on only one server, that server is included.
1061 * If it is replicated on many servers, then none are included.
1062 * This is not perfect, but the result is that people don't
1063 * get subscribed to a lot of instances of FILSRV that they
1064 * probably won't need which reduces the instances of
1065 * people getting messages that don't apply to them.
1068 if (phosts[1] != '\0')
1071 for (i = 0; phosts[i]; i++) {
1073 in.s_addr = phosts[i];
1075 printf("Got host %s\n", inet_ntoa(in));
1077 ll_string(&hostlist, ll_s_add, (char *)inet_ntoa(in));
1079 if (zsubs && (hp=gethostbyaddr((char *) &phosts[i],sizeof(long),AF_INET))) {
1081 printf("Got host %s\n", hp->h_name);
1083 ll_string(&zsublist, ll_s_add, hp->h_name);
1089 #endif /* WINDOWS */
1091 #ifndef WINDOWS /* next_path(), get_afs_mountpoint() */
1094 * This routine descends through a path to a directory, logging to
1095 * every cell it encounters along the way.
1097 static int auth_to_path(krb5_context context, char *path)
1099 int status = AKLOG_SUCCESS;
1100 int auth_to_cell_status = AKLOG_SUCCESS;
1103 char pathtocheck[MAXPATHLEN + 1];
1104 char mountpoint[MAXPATHLEN + 1];
1113 strcpy(pathtocheck, path);
1115 if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL) {
1116 fprintf(stderr, "Unable to find current working directory:\n");
1117 fprintf(stderr, "%s\n", pathtocheck);
1118 fprintf(stderr, "Try an absolute pathname.\n");
1119 exit(AKLOG_BADPATH);
1122 strcat(pathtocheck, DIRSTRING);
1123 strcat(pathtocheck, path);
1126 next_path(pathtocheck);
1128 /* Go on to the next level down the path */
1129 while ((nextpath = next_path(NULL))) {
1130 strcpy(pathtocheck, nextpath);
1132 printf("Checking directory %s\n", pathtocheck);
1135 * If this is an afs mountpoint, determine what cell from
1136 * the mountpoint name which is of the form
1137 * #cellname:volumename or %cellname:volumename.
1139 if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint))) {
1140 /* skip over the '#' or '%' */
1141 cell = mountpoint + 1;
1142 /* Add this (cell:volumename) to the list of zsubs */
1144 ll_string(&zsublist, ll_s_add, cell);
1146 add_hosts(pathtocheck);
1147 if ((endofcell = strchr(mountpoint, VOLMARKER))) {
1149 if ((auth_to_cell_status = auth_to_cell(context, cell, NULL))) {
1150 if (status == AKLOG_SUCCESS)
1151 status = auth_to_cell_status;
1152 else if (status != auth_to_cell_status)
1153 status = AKLOG_SOMETHINGSWRONG;
1158 if (isdir(pathtocheck, &isdirectory) < 0) {
1160 * If we've logged and still can't stat, there's
1163 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1164 pathtocheck, strerror(errno));
1165 return(AKLOG_BADPATH);
1167 else if (! isdirectory) {
1168 /* Allow only directories */
1169 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1171 return(AKLOG_BADPATH);
1180 #endif /* WINDOWS */
1183 /* Print usage message and exit */
1184 static void usage(void)
1186 fprintf(stderr, "\nUsage: %s %s%s%s\n", progname,
1187 "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1188 "[[-p | -path] pathname]\n",
1189 " [-zsubs] [-hosts] [-noauth] [-noprdb] [-force] [-setpag] \n"
1190 " [-linked] [-524]\n");
1191 fprintf(stderr, " -d gives debugging information.\n");
1192 fprintf(stderr, " krb_realm is the kerberos realm of a cell.\n");
1193 fprintf(stderr, " pathname is the name of a directory to which ");
1194 fprintf(stderr, "you wish to authenticate.\n");
1195 fprintf(stderr, " -zsubs gives zephyr subscription information.\n");
1196 fprintf(stderr, " -hosts gives host address information.\n");
1197 fprintf(stderr, " -noauth does not attempt to get tokens.\n");
1198 fprintf(stderr, " -noprdb means don't try to determine AFS ID.\n");
1199 fprintf(stderr, " -force means replace identical tickets. \n");
1200 fprintf(stderr, " -linked means if AFS node is linked, try both. \n");
1201 fprintf(stderr, " -setpag set the AFS process authentication group.\n");
1202 fprintf(stderr, " -524 means use the 524 converter instead of V5 directly\n");
1203 fprintf(stderr, " No commandline arguments means ");
1204 fprintf(stderr, "authenticate to the local cell.\n");
1205 fprintf(stderr, "\n");
1209 void aklog(int argc, char *argv[])
1211 krb5_context context;
1212 int status = AKLOG_SUCCESS;
1214 int somethingswrong = FALSE;
1216 cellinfo_t cellinfo;
1218 extern char *progname; /* Name of this program */
1220 extern int dflag; /* Debug mode */
1222 int cmode = FALSE; /* Cellname mode */
1223 int pmode = FALSE; /* Path name mode */
1225 char realm[REALM_SZ]; /* Kerberos realm of afs server */
1226 char cell[BUFSIZ]; /* Cell to which we are authenticating */
1227 char path[MAXPATHLEN + 1]; /* Path length for path mode */
1229 linked_list cells; /* List of cells to log to */
1230 linked_list paths; /* List of paths to log to */
1233 memset(&cellinfo, 0, sizeof(cellinfo));
1235 memset(realm, 0, sizeof(realm));
1236 memset(cell, 0, sizeof(cell));
1237 memset(path, 0, sizeof(path));
1245 /* Store the program name here for error messages */
1246 if ((progname = strrchr(argv[0], DIR)))
1251 krb5_init_context(&context);
1253 initialize_ktc_error_table ();
1256 /* Initialize list of cells to which we have authenticated */
1257 (void)ll_init(&authedcells);
1259 /* Parse commandline arguments and make list of what to do. */
1260 for (i = 1; i < argc; i++) {
1261 if (strcmp(argv[i], "-d") == 0)
1263 else if (strcmp(argv[i], "-noauth") == 0)
1265 else if (strcmp(argv[i], "-zsubs") == 0)
1267 else if (strcmp(argv[i], "-hosts") == 0)
1269 else if (strcmp(argv[i], "-noprdb") == 0)
1271 else if (strcmp(argv[i], "-linked") == 0)
1273 else if (strcmp(argv[i], "-force") == 0)
1275 else if (strcmp(argv[i], "-524") == 0)
1277 else if (strcmp(argv[i], "-setpag") == 0)
1279 else if (((strcmp(argv[i], "-cell") == 0) ||
1280 (strcmp(argv[i], "-c") == 0)) && !pmode)
1283 strcpy(cell, argv[i]);
1287 else if (((strcmp(argv[i], "-path") == 0) ||
1288 (strcmp(argv[i], "-p") == 0)) && !cmode)
1292 strcpy(path, argv[i]);
1298 fprintf(stderr, "%s: path mode not supported.\n", progname);
1301 #endif /* WINDOWS */
1303 else if (argv[i][0] == '-')
1305 else if (!pmode && !cmode) {
1306 if (strchr(argv[i], DIR) || (strcmp(argv[i], ".") == 0) ||
1307 (strcmp(argv[i], "..") == 0)) {
1310 strcpy(path, argv[i]);
1312 fprintf(stderr, "%s: path mode not supported.\n", progname);
1314 #endif /* WINDOWS */
1318 strcpy(cell, argv[i]);
1325 if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0)) {
1328 strcpy(realm, argv[i]);
1332 /* Add this cell to list of cells */
1333 strcpy(cellinfo.cell, cell);
1334 strcpy(cellinfo.realm, realm);
1335 if ((cur_node = ll_add_node(&cells, ll_tail))) {
1337 if ((new_cellinfo = copy_cellinfo(&cellinfo)))
1338 ll_add_data(cur_node, new_cellinfo);
1341 "%s: failure copying cellinfo.\n", progname);
1346 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1350 memset(&cellinfo, 0, sizeof(cellinfo));
1352 memset(cell, 0, sizeof(cell));
1353 memset(realm, 0, sizeof(realm));
1357 /* Add this path to list of paths */
1358 if ((cur_node = ll_add_node(&paths, ll_tail))) {
1360 if ((new_path = copy_string(path)))
1361 ll_add_data(cur_node, new_path);
1363 fprintf(stderr, "%s: failure copying path name.\n",
1369 fprintf(stderr, "%s: failure adding path to paths list.\n",
1374 memset(path, 0, sizeof(path));
1376 #endif /* WINDOWS */
1380 * The code that _used_ to be here called setpag(). When you think
1381 * about this, doing this makes no sense! setpag() allocates a PAG
1382 * only for the current process, so the token installed would have
1383 * not be usable in the parent! Since ktc_SetToken() now takes a
1384 * 4th argument to control whether or not we're going to allocate
1385 * a PAG (and since when you do it _that_ way, it modifies the cred
1386 * structure of your parent)), why don't we use that instead?
1397 printf("setpag %d\n",status);
1398 j = getgroups(gidsetlen,gidset);
1399 printf("Groups(%d):",j);
1400 for (i = 0; i<j; i++) {
1401 printf("%d",gidset[i]);
1402 if((i+1)<j) printf(",");
1408 /* If nothing was given, log to the local cell. */
1409 if ((cells.nelements + paths.nelements) == 0) {
1412 status = auth_to_cell(context, NULL, NULL);
1414 /* If this cell is linked to a DCE cell, and user
1415 * requested -linked, get tokens for both
1416 * This is very usefull when the AFS cell is linked to a DFS
1417 * cell and this system does not also have DFS.
1420 if (!status && linked && linkedcell[0]) {
1421 strncpy(linkedcell2,linkedcell,MAXCELLCHARS);
1423 printf("Linked cell: %s\n", linkedcell);
1425 status = auth_to_cell(context, linkedcell2, NULL);
1430 * Local hack - if the person has a file in their home
1431 * directory called ".xlog", read that for a list of
1432 * extra cells to authenticate to
1435 if ((pwd = getpwuid(getuid())) != NULL) {
1438 char fcell[100], xlog_path[512];
1440 strcpy(xlog_path, pwd->pw_dir);
1441 strcat(xlog_path, "/.xlog");
1443 if ((stat(xlog_path, &sbuf) == 0) &&
1444 ((f = fopen(xlog_path, "r")) != NULL)) {
1447 printf("Reading %s for cells to "
1448 "authenticate to.\n", xlog_path);
1451 while (fgets(fcell, 100, f) != NULL) {
1454 fcell[strlen(fcell) - 1] = '\0';
1457 printf("Found cell %s in %s.\n",
1461 auth_status = auth_to_cell(context, fcell, NULL);
1462 if (status == AKLOG_SUCCESS)
1463 status = auth_status;
1465 status = AKLOG_SOMETHINGSWRONG;
1469 #endif /* WINDOWS */
1472 /* Log to all cells in the cells list first */
1473 for (cur_node = cells.first; cur_node; cur_node = cur_node->next) {
1474 memcpy((char *)&cellinfo, cur_node->data, sizeof(cellinfo));
1475 if ((status = auth_to_cell(context, cellinfo.cell, cellinfo.realm)))
1478 if (linked && linkedcell[0]) {
1479 strncpy(linkedcell2,linkedcell,MAXCELLCHARS);
1481 printf("Linked cell: %s\n",
1484 if ((status = auth_to_cell(context,linkedcell2,
1492 /* Then, log to all paths in the paths list */
1493 for (cur_node = paths.first; cur_node; cur_node = cur_node->next) {
1494 if ((status = auth_to_path(context, cur_node->data)))
1497 #endif /* WINDOWS */
1500 * If only one thing was logged to, we'll return the status
1501 * of the single call. Otherwise, we'll return a generic
1502 * something failed status.
1504 if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1505 status = AKLOG_SOMETHINGSWRONG;
1508 /* If we are keeping track of zephyr subscriptions, print them. */
1510 for (cur_node = zsublist.first; cur_node; cur_node = cur_node->next) {
1511 printf("zsub: %s\n", cur_node->data);
1514 /* If we are keeping track of host information, print it. */
1516 for (cur_node = hostlist.first; cur_node; cur_node = cur_node->next) {
1517 printf("host: %s\n", cur_node->data);
1523 #ifndef HAVE_ADD_TO_ERROR_TABLE
1525 #define error_table error_table_compat
1526 #include <afs/error_table.h>
1529 #ifndef HAVE_ADD_ERROR_TABLE
1530 void add_error_table (const struct error_table *);
1531 #endif /* !HAVE_ADD_ERROR_TABLE */
1534 add_to_error_table(struct et_list *new_table)
1536 add_error_table((struct error_table *) new_table->table);
1538 #endif /* HAVE_ADD_TO_ERROR_TABLE */
1540 static int isdir(char *path, unsigned char *val)
1542 struct stat statbuf;
1544 if (lstat(path, &statbuf) < 0)
1547 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
1555 static krb5_error_code get_credv5(krb5_context context,
1556 char *name, char *inst, char *realm,
1561 static krb5_principal client_principal = 0;
1563 memset((char *)&increds, 0, sizeof(increds));
1564 /* ANL - instance may be ptr to a null string. Pass null then */
1565 if ((r = krb5_build_principal(context, &increds.server,
1566 strlen(realm), realm,
1568 (inst && strlen(inst)) ? inst : (void *) NULL,
1573 if (!_krb425_ccache) {
1574 r = krb5_cc_default(context, &_krb425_ccache);
1578 if (!client_principal) {
1579 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
1584 increds.client = client_principal;
1585 increds.times.endtime = 0;
1586 /* Ask for DES since that is what V4 understands */
1587 get_creds_enctype((&increds)) = ENCTYPE_DES_CBC_CRC;
1589 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
1595 static int get_user_realm(krb5_context context, char *realm)
1597 static krb5_principal client_principal = 0;
1600 if (!_krb425_ccache)
1601 krb5_cc_default(context, &_krb425_ccache);
1602 if (!client_principal)
1603 krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
1605 i = realm_len(context, client_principal);
1606 if (i > REALM_SZ-1) i = REALM_SZ-1;
1607 strncpy(realm,realm_data(context, client_principal), i);