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/dirpath.h>
88 #include "linked_list.h"
93 #ifndef AFS_TRY_FULL_PRINC
94 #define AFS_TRY_FULL_PRINC 1
95 #endif /* AFS_TRY_FULL_PRINC */
97 #define AKLOG_SUCCESS 0
99 #define AKLOG_SOMETHINGSWRONG 2
101 #define AKLOG_KERBEROS 4
102 #define AKLOG_TOKEN 5
103 #define AKLOG_BADPATH 6
119 /* RedHat 4.x doesn't seem to define this */
120 #define MAXSYMLINKS 5
123 #define DIR '/' /* Character that divides directories */
124 #define DIRSTRING "/" /* String form of above */
125 #define VOLMARKER ':' /* Character separating cellname from mntpt */
126 #define VOLMARKERSTRING ":" /* String form of above */
130 char realm[REALM_SZ];
133 struct afsconf_cell ak_cellconfig; /* General information about the cell */
134 static char linkedcell[MAXCELLCHARS+1];
135 static char linkedcell2[MAXCELLCHARS+1];
136 static krb5_ccache _krb425_ccache = NULL;
141 extern long cm_GetRootCellName();
142 extern long cm_SearchCellFile();
144 static long cm_SearchCellFile_CallBack();
149 * Why doesn't AFS provide these prototypes?
152 extern int pioctl(char *, afs_int32, struct ViceIoctl *, afs_int32);
158 extern char *afs_realm_of_cell(krb5_context, struct afsconf_cell *);
159 static int isdir(char *, unsigned char *);
160 static krb5_error_code get_credv5(krb5_context context, char *, char *,
161 char *, krb5_creds **);
162 static int get_user_realm(krb5_context, char *);
167 * Provide a replacement for strerror if we don't have it
170 #ifndef HAVE_STRERROR
171 extern char *sys_errlist[];
172 #define strerror(x) sys_errlist[x]
173 #endif /* HAVE_STRERROR */
175 static char *progname = NULL; /* Name of this program */
176 static int dflag = FALSE; /* Give debugging information */
177 static int noauth = FALSE; /* If true, don't try to get tokens */
178 static int zsubs = FALSE; /* Are we keeping track of zephyr subs? */
179 static int hosts = FALSE; /* Are we keeping track of hosts? */
180 static int noprdb = FALSE; /* Skip resolving name to id? */
181 static int linked = FALSE; /* try for both AFS nodes */
182 static int afssetpag = FALSE; /* setpag for AFS */
183 static int force = FALSE; /* Bash identical tokens? */
184 static int do524 = FALSE; /* Should we do 524 instead of rxkad2b? */
185 static linked_list zsublist; /* List of zephyr subscriptions */
186 static linked_list hostlist; /* List of host addresses */
187 static linked_list authedcells; /* List of cells already logged to */
189 /* ANL - CMU lifetime convert routine */
190 /* for K5.4.1 don't use this for now. Need to see if it is needed */
191 /* maybe needed in the krb524d module as well */
192 /* extern unsigned long krb_life_to_time(); */
194 static char *copy_cellinfo(cellinfo_t *cellinfo)
196 cellinfo_t *new_cellinfo;
198 if ((new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t))))
199 memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
201 return ((char *)new_cellinfo);
205 static char *copy_string(char *string)
209 if ((new_string = (char *)calloc(strlen(string) + 1, sizeof(char))))
210 (void) strcpy(new_string, string);
216 static int get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char *local_cell, char *linkedcell)
218 int status = AKLOG_SUCCESS;
219 struct afsconf_dir *configdir;
222 memset(local_cell, 0, sizeof(local_cell));
223 memset((char *)cellconfig, 0, sizeof(*cellconfig));
227 if (!(configdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
229 "%s: can't get afs configuration (afsconf_Open(%s))\n",
230 progname, AFSDIR_CLIENT_ETC_DIRPATH);
234 if (afsconf_GetLocalCell(configdir, local_cell, MAXCELLCHARS)) {
235 fprintf(stderr, "%s: can't determine local cell.\n", progname);
239 if ((cell == NULL) || (cell[0] == 0))
242 linkedcell[0] = '\0';
243 if (afsconf_GetCellInfo(configdir, cell, NULL, cellconfig)) {
244 fprintf(stderr, "%s: Can't get information about cell %s.\n",
248 if (cellconfig->linkedCell)
249 strncpy(linkedcell,cellconfig->linkedCell,MAXCELLCHARS);
251 (void) afsconf_Close(configdir);
255 * We'll try to mimic the GetCellInfo call here and fill in as much
256 * of the afsconf_cell structure as we can.
258 if (cm_GetRootCellName(local_cell)) {
259 fprintf(stderr, "%s: can't get local cellname\n", progname);
263 if ((cell == NULL) || (cell[0] == 0))
266 strcpy(cellconfig->name, cell);
268 /* No way of figuring this out as far as I can tell */
269 linkedcell[0] = '\0';
271 /* Initialize server info */
272 cellconfig->numServers = 0;
273 cellconfig->hostName[0][0] = "\0";
276 * Get servers of cell. cm_SearchCellFile_CallBack() gets call with
279 status = (int) cm_SearchCellFile(cell, NULL, &cm_SearchCellFile_CallBack,
280 cellconfig /* rock */);
287 fprintf(stderr, "%s: GetWindowsDirectory() failed.\n", progname);
291 fprintf(stderr, "%s: Couldn't open afsdcells.ini for reading\n",
296 fprintf(stderr, "%s: Couldn't find any servers for cell %s\n",
301 fprintf(stderr, "%s: Badly formatted line in afsdcells.ini (does not begin with a \">\" or contain \"#\"\n",
306 fprintf(stderr, "%s cm_SearchCellFile returned unknown error %d\n",
314 status = AKLOG_SUCCESS;
325 * Callback function for cm_SearchCellFile() in get_cellconfig() above.
326 * This function gets called once for each server that is found for the cell.
329 cm_SearchCellFile_CallBack(void *rock /* cellconfig */,
330 struct sockaddr_in *addr, /* Not used */
333 struct afsconf_cell *cellconfig = rock;
337 * Save server name and increment count of servers
339 strcpy(cellconfig->hostName[cellconfig->numServers++], server);
349 * Log to a cell. If the cell has already been logged to, return without
350 * doing anything. Otherwise, log to it and mark that it has been logged
354 static int auth_to_cell(krb5_context context, char *cell, char *realm)
356 static int auth_to_cell(context, cell, realm)
358 krb5_context context;
361 #endif /* __STDC__ */
363 int status = AKLOG_SUCCESS;
364 char username[BUFSIZ]; /* To hold client username structure */
365 long viceId; /* AFS uid of user */
367 char name[ANAME_SZ]; /* Name of afs key */
368 char primary_instance[INST_SZ]; /* Instance of afs key */
369 char secondary_instance[INST_SZ]; /* Backup instance to try */
370 int try_secondary = 0; /* Flag to indicate if we try second */
371 char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
372 char realm_of_cell[REALM_SZ]; /* Kerberos realm of cell */
373 char local_cell[MAXCELLCHARS+1];
374 char cell_to_use[MAXCELLCHARS+1]; /* Cell to authenticate to */
375 static char lastcell[MAXCELLCHARS+1] = { 0 };
377 static char confname[512] = { 0 };
379 krb5_creds *v5cred = NULL;
380 struct ktc_principal aserver;
381 struct ktc_principal aclient;
382 struct ktc_token atoken, btoken;
384 #ifdef ALLOW_REGISTER
386 #endif /* ALLOW_REGISTER */
388 memset(name, 0, sizeof(name));
389 memset(primary_instance, 0, sizeof(primary_instance));
390 memset(secondary_instance, 0, sizeof(secondary_instance));
391 memset(realm_of_user, 0, sizeof(realm_of_user));
392 memset(realm_of_cell, 0, sizeof(realm_of_cell));
395 if (confname[0] == '\0') {
396 strncpy(confname, AFSDIR_CLIENT_ETC_DIRPATH, sizeof(confname));
397 confname[sizeof(confname) - 2] = '\0';
401 /* NULL or empty cell returns information on local cell */
402 if ((status = get_cellconfig(cell, &ak_cellconfig,
403 local_cell, linkedcell)))
406 strncpy(cell_to_use, ak_cellconfig.name, MAXCELLCHARS);
407 cell_to_use[MAXCELLCHARS] = 0;
409 if (ll_string(&authedcells, ll_s_check, cell_to_use)) {
411 printf("Already authenticated to %s (or tried to)\n",
414 return(AKLOG_SUCCESS);
418 * Record that we have attempted to log to this cell. We do this
419 * before we try rather than after so that we will not try
420 * and fail repeatedly for one cell.
422 (void)ll_string(&authedcells, ll_s_add, cell_to_use);
425 * Record this cell in the list of zephyr subscriptions. We may
426 * want zephyr subscriptions even if authentication fails.
427 * If this is done after we attempt to get tokens, aklog -zsubs
428 * can return something different depending on whether or not we
429 * are in -noauth mode.
431 if (ll_string(&zsublist, ll_s_add, cell_to_use) == LL_FAILURE) {
433 "%s: failure adding cell %s to zephyr subscriptions list.\n",
434 progname, cell_to_use);
437 if (ll_string(&zsublist, ll_s_add, local_cell) == LL_FAILURE) {
439 "%s: failure adding cell %s to zephyr subscriptions list.\n",
440 progname, local_cell);
446 printf("Authenticating to cell %s (server %s).\n",
447 cell_to_use, ak_cellconfig.hostName[0]);
451 * Find out which realm we're supposed to authenticate to. If one
452 * is not included, use the kerberos realm found in the credentials
456 if (realm && realm[0]) {
457 strcpy(realm_of_cell, realm);
459 printf("We were told to authenticate to realm %s.\n", realm);
463 char *realm = afs_realm_of_cell(context, &ak_cellconfig);
467 "%s: Couldn't figure out realm for cell %s.\n",
468 progname, cell_to_use);
472 strcpy(realm_of_cell, realm);
475 printf("We've deduced that we need to authenticate to"
476 " realm %s.\n", realm_of_cell);
480 /* We use the afs.<cellname> convention here...
482 * Doug Engert's original code had principals of the form:
486 * in the KDC, so the name wouldn't conflict with DFS. Since we're
487 * not using DFS, I changed it just to look for the following
493 * Because people are transitioning from afs@realm to afs/cell,
494 * we configure things so that if the first one isn't found, we
495 * try the second one. You can select which one you prefer with
496 * a configure option.
499 strcpy(name, AFSKEY);
501 if (AFS_TRY_FULL_PRINC || strcasecmp(cell_to_use, realm_of_cell) != 0) {
502 strncpy(primary_instance, cell_to_use, sizeof(primary_instance));
503 primary_instance[sizeof(primary_instance)-1] = '\0';
504 if (strcasecmp(cell_to_use, realm_of_cell) == 0) {
506 secondary_instance[0] = '\0';
509 primary_instance[0] = '\0';
511 strncpy(secondary_instance, cell_to_use,
512 sizeof(secondary_instance));
513 secondary_instance[sizeof(secondary_instance)-1] = '\0';
517 * Extract the session key from the ticket file and hand-frob an
518 * afs style authenticator.
522 * Try to obtain AFS tickets. Because there are two valid service
523 * names, we will try both, but trying the more specific first.
525 * afs/<cell>@<realm> i.e. allow for single name with "."
530 printf("Getting tickets: %s/%s@%s\n", name,
531 primary_instance, realm_of_cell);
534 status = get_credv5(context, name, primary_instance, realm_of_cell,
537 if (status == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN) {
540 printf("Principal not found, trying alternate "
541 "service name: %s/%s@%s\n", name,
542 secondary_instance, realm_of_cell);
544 status = get_credv5(context, name, secondary_instance,
545 realm_of_cell, &v5cred);
549 if (status != KSUCCESS) {
551 printf("Kerberos error code returned by get_cred: %d\n",
554 fprintf(stderr, "%s: Couldn't get %s AFS tickets:\n",
555 progname, cell_to_use);
556 com_err(progname, status, "while getting AFS tickets");
557 return(AKLOG_KERBEROS);
560 strncpy(aserver.name, AFSKEY, MAXKTCNAMELEN - 1);
561 strncpy(aserver.instance, AFSINST, MAXKTCNAMELEN - 1);
562 strncpy(aserver.cell, cell_to_use, MAXKTCREALMLEN - 1);
565 * The default is to use rxkad2b, which means we put in a full
566 * V5 ticket. If the user specifies -524, we talk to the
567 * 524 ticket converter.
575 printf("Using Kerberos V5 ticket natively\n");
577 len = min(v5cred->client->data[0].length,
578 v5cred->client->length > 1 ? MAXKTCNAMELEN - 2 :
580 strncpy(username, v5cred->client->data[0].data, len);
581 username[len] = '\0';
583 if (v5cred->client->length > 1) {
584 strcat(username, ".");
585 p = username + strlen(username);
586 len = min(v5cred->client->data[1].length,
587 MAXKTCNAMELEN - strlen(username) - 1);
588 strncpy(p, v5cred->client->data[1].data, len);
592 memset(&atoken, 0, sizeof(atoken));
593 atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
594 atoken.startTime = v5cred->times.starttime;;
595 atoken.endTime = v5cred->times.endtime;
596 memcpy(&atoken.sessionKey, v5cred->keyblock.contents,
597 v5cred->keyblock.length);
598 atoken.ticketLen = v5cred->ticket.length;
599 memcpy(atoken.ticket, v5cred->ticket.data, atoken.ticketLen);
604 printf("Using Kerberos 524 translator service\n");
606 status = krb5_524_convert_creds(context, v5cred, &cred);
609 com_err(progname, status, "while converting tickets "
610 "to Kerberos V4 format");
611 return(AKLOG_KERBEROS);
614 strcpy (username, cred.pname);
616 strcat (username, ".");
617 strcat (username, cred.pinst);
620 atoken.kvno = cred.kvno;
621 atoken.startTime = cred.issue_date;
623 * It seems silly to go through a bunch of contortions to
624 * extract the expiration time, when the v5 credentials already
625 * has the exact time! Let's use that instead.
627 * Note that this isn't a security hole, as the expiration time
628 * is also contained in the encrypted token
630 atoken.endTime = v5cred->times.endtime;
631 memcpy(&atoken.sessionKey, cred.session, 8);
632 atoken.ticketLen = cred.ticket_st.length;
633 memcpy(atoken.ticket, cred.ticket_st.dat, atoken.ticketLen);
637 !ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient) &&
638 atoken.kvno == btoken.kvno &&
639 atoken.ticketLen == btoken.ticketLen &&
640 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
641 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen)) {
644 printf("Identical tokens already exist; skipping.\n");
657 printf("Not resolving name %s to id (-noprdb set)\n",
663 if ((status = get_user_realm(context, realm_of_user)) != KSUCCESS) {
664 fprintf(stderr, "%s: Couldn't determine realm of user:)",
666 com_err(progname, status, " while getting realm");
667 return(AKLOG_KERBEROS);
669 if (strcmp(realm_of_user, realm_of_cell)) {
670 strcat(username, "@");
671 strcat(username, realm_of_user);
675 printf("About to resolve name %s to id in cell %s.\n",
676 username, aserver.cell);
679 strcpy(lastcell, aserver.cell);
681 if (!pr_Initialize (0, confname, aserver.cell, 0))
682 status = pr_SNameToId (username, &viceId);
686 printf("Error %d\n", status);
688 printf("Id %d\n", (int) viceId);
692 * This is a crock, but it is Transarc's crock, so
693 * we have to play along in order to get the
694 * functionality. The way the afs id is stored is
695 * as a string in the username field of the token.
696 * Contrary to what you may think by looking at
697 * the code for tokens, this hack (AFS ID %d) will
698 * not work if you change %d to something else.
702 * This code is taken from cklog -- it lets people
703 * automatically register with the ptserver in foreign cells
706 #ifdef ALLOW_REGISTER
708 if (viceId != ANONYMOUSID) {
709 #else /* ALLOW_REGISTER */
710 if ((status == 0) && (viceId != ANONYMOUSID))
711 #endif /* ALLOW_REGISTER */
712 sprintf (username, "AFS ID %d", (int) viceId);
713 #ifdef ALLOW_REGISTER
714 } else if (strcmp(realm_of_user, realm_of_cell) != 0) {
716 printf("doing first-time registration of %s "
717 "at %s\n", username, cell_to_use);
720 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
721 strcpy(aclient.instance, "");
722 strncpy(aclient.cell, realm_of_user, MAXKTCREALMLEN - 1);
723 if ((status = ktc_SetToken(&aserver, &atoken, &aclient, 0))) {
724 fprintf(stderr, "%s: unable to obtain tokens for cell %s "
725 "(status: %d).\n", progname, cell_to_use, status);
726 status = AKLOG_TOKEN;
730 * In case you're wondering, we don't need to change the
731 * filename here because we're still connecting to the
732 * same cell -- we're just using a different authentication
736 if ((status = pr_Initialize(1L, confname, aserver.cell, 0))) {
737 printf("Error %d\n", status);
740 if ((status = pr_CreateUser(username, &id))) {
741 fprintf(stderr, "%s: %s so unable to create remote PTS "
742 "user %s in cell %s (status: %d).\n", progname,
743 error_message(status), username, cell_to_use,
746 printf("created cross-cell entry for %s at %s\n",
747 username, cell_to_use);
748 sprintf(username, "AFS ID %d", (int) id);
752 #endif /* ALLOW_REGISTER */
755 #endif /* !WINDOWS */
758 fprintf(stdout, "Set username to %s\n", username);
761 /* Reset the "aclient" structure before we call ktc_SetToken.
762 * This structure was first set by the ktc_GetToken call when
763 * we were comparing whether identical tokens already existed.
765 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
766 strcpy(aclient.instance, "");
767 strncpy(aclient.cell, realm_of_user, MAXKTCREALMLEN - 1);
770 printf("Setting tokens. %s / %s @ %s \n",
771 aclient.name, aclient.instance, aclient.cell );
773 /* on AIX 4.1.4 with AFS 3.4a+ if a write is not done before
774 * this routine, it will not add the token. It is not clear what
775 * is going on here! So we will do the following operation
777 write(2,"",0); /* dummy write */
779 if ((status = ktc_SetToken(&aserver, &atoken, &aclient, afssetpag))) {
781 "%s: unable to obtain tokens for cell %s (status: %d).\n",
782 progname, cell_to_use, status);
783 status = AKLOG_TOKEN;
786 /* Note switched 2nd and 3rd args */
787 if ((status = ktc_SetToken(&aserver, &atoken, &aclient, afssetpag))) {
790 fprintf(stderr, "%s: Bad ticket length", progname);
793 fprintf(stderr, "%s: Unknown error contacting AFS service",
797 fprintf(stderr, "%s: Cell name (%s) not recognized by AFS service",
798 progname, realm_of_cell);
801 fprintf(stderr, "%s: AFS service is unavailable", progname);
804 fprintf(stderr, "%s: Undocumented error (%d) contacting AFS service", progname, status);
807 status = AKLOG_TOKEN;
809 #endif /* !WINDOWS */
813 printf("Noauth mode; not authenticating.\n");
819 #ifndef WINDOWS /* struct ViceIoctl missing */
822 static int get_afs_mountpoint(char *file, char *mountpoint, int size)
824 static int get_afs_mountpoint(file, mountpoint, size)
828 #endif /* __STDC__ */
831 char V ='V'; /* AFS has problem on Sun with pioctl */
833 char our_file[MAXPATHLEN + 1];
835 char *last_component;
836 struct ViceIoctl vio;
837 char cellname[BUFSIZ];
839 memset(our_file, 0, sizeof(our_file));
840 strcpy(our_file, file);
842 if ((last_component = strrchr(our_file, DIR))) {
843 *last_component++ = 0;
844 parent_dir = our_file;
847 last_component = our_file;
851 memset(cellname, 0, sizeof(cellname));
853 vio.in = last_component;
854 vio.in_size = strlen(last_component)+1;
856 vio.out = mountpoint;
858 if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0)) {
859 if (strchr(mountpoint, VOLMARKER) == NULL) {
861 vio.in_size = strlen(file) + 1;
862 vio.out_size = sizeof(cellname);
865 if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1)) {
866 strcat(cellname, VOLMARKERSTRING);
867 strcat(cellname, mountpoint + 1);
868 memset(mountpoint + 1, 0, size - 1);
869 strcpy(mountpoint + 1, cellname);
879 * This routine each time it is called returns the next directory
880 * down a pathname. It resolves all symbolic links. The first time
881 * it is called, it should be called with the name of the path
882 * to be descended. After that, it should be called with the arguemnt
886 static char *next_path(char *origpath)
888 static char *next_path(origpath)
890 #endif /* __STDC__ */
892 static char path[MAXPATHLEN + 1];
893 static char pathtocheck[MAXPATHLEN + 1];
895 int link = FALSE; /* Is this a symbolic link? */
896 char linkbuf[MAXPATHLEN + 1];
897 char tmpbuf[MAXPATHLEN + 1];
899 static char *last_comp; /* last component of directory name */
900 static char *elast_comp; /* End of last component */
904 static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
906 /* If we are given something for origpath, we are initializing only. */
908 memset(path, 0, sizeof(path));
909 memset(pathtocheck, 0, sizeof(pathtocheck));
910 strcpy(path, origpath);
916 /* We were not given origpath; find then next path to check */
918 /* If we've gotten all the way through already, return NULL */
919 if (last_comp == NULL)
923 while (*last_comp == DIR)
924 strncat(pathtocheck, last_comp++, 1);
925 len = (elast_comp = strchr(last_comp, DIR))
926 ? elast_comp - last_comp : strlen(last_comp);
927 strncat(pathtocheck, last_comp, len);
928 memset(linkbuf, 0, sizeof(linkbuf));
929 if ((link = readlink(pathtocheck, linkbuf,
930 sizeof(linkbuf))) > 0) {
931 if (++symlinkcount > MAXSYMLINKS) {
932 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
935 memset(tmpbuf, 0, sizeof(tmpbuf));
937 strcpy(tmpbuf, elast_comp);
938 if (linkbuf[0] == DIR) {
940 * If this is a symbolic link to an absolute path,
941 * replace what we have by the absolute path.
943 memset(path, 0, strlen(path));
944 memcpy(path, linkbuf, sizeof(linkbuf));
945 strcat(path, tmpbuf);
948 memset(pathtocheck, 0, sizeof(pathtocheck));
952 * If this is a symbolic link to a relative path,
953 * replace only the last component with the link name.
955 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
956 strcat(path, tmpbuf);
958 if ((t = strrchr(pathtocheck, DIR))) {
960 memset(t, 0, strlen(t));
963 memset(pathtocheck, 0, sizeof(pathtocheck));
967 last_comp = elast_comp;
977 /*****************************************/
981 char V = 'V'; /* AFS has problem on SunOS */
983 struct ViceIoctl vio;
988 memset(outbuf, 0, sizeof(outbuf));
990 vio.out_size = sizeof(outbuf);
991 vio.in_size = sizeof(ind);
996 fd = open("dee.tok",O_WRONLY);
997 while(!pioctl(0,VIOCGETTOK,&vio,0)) {
998 write(fd,&outbuf,sizeof(outbuf));
1003 /*****************************************/
1006 #ifndef WINDOWS /* struct ViceIoctl missing */
1009 static void add_hosts(char *file)
1011 static void add_hosts(file)
1013 #endif /* __STDC__ */
1016 char V = 'V'; /* AFS has problem on SunOS */
1018 struct ViceIoctl vio;
1019 char outbuf[BUFSIZ];
1025 memset(outbuf, 0, sizeof(outbuf));
1027 vio.out_size = sizeof(outbuf);
1032 printf("Getting list of hosts for %s\n", file);
1034 /* Don't worry about errors. */
1035 if (!pioctl(file, VIOCWHEREIS, &vio, 1)) {
1036 phosts = (long *) outbuf;
1039 * Lists hosts that we care about. If ALLHOSTS is defined,
1040 * then all hosts that you ever may possible go through are
1041 * included in this list. If not, then only hosts that are
1042 * the only ones appear. That is, if a volume you must use
1043 * is replaced on only one server, that server is included.
1044 * If it is replicated on many servers, then none are included.
1045 * This is not perfect, but the result is that people don't
1046 * get subscribed to a lot of instances of FILSRV that they
1047 * probably won't need which reduces the instances of
1048 * people getting messages that don't apply to them.
1051 if (phosts[1] != '\0')
1054 for (i = 0; phosts[i]; i++) {
1056 in.s_addr = phosts[i];
1058 printf("Got host %s\n", inet_ntoa(in));
1060 ll_string(&hostlist, ll_s_add, (char *)inet_ntoa(in));
1062 if (zsubs && (hp=gethostbyaddr((char *) &phosts[i],sizeof(long),AF_INET))) {
1064 printf("Got host %s\n", hp->h_name);
1066 ll_string(&zsublist, ll_s_add, hp->h_name);
1072 #endif /* WINDOWS */
1074 #ifndef WINDOWS /* next_path(), get_afs_mountpoint() */
1077 * This routine descends through a path to a directory, logging to
1078 * every cell it encounters along the way.
1081 static int auth_to_path(krb5_context context, char *path)
1083 static int auth_to_path(context, path)
1084 krb5_context context;
1085 char *path; /* The path to which we try to authenticate */
1086 #endif /* __STDC__ */
1088 int status = AKLOG_SUCCESS;
1089 int auth_to_cell_status = AKLOG_SUCCESS;
1092 char pathtocheck[MAXPATHLEN + 1];
1093 char mountpoint[MAXPATHLEN + 1];
1102 strcpy(pathtocheck, path);
1104 if (getwd(pathtocheck) == NULL) {
1105 fprintf(stderr, "Unable to find current working directory:\n");
1106 fprintf(stderr, "%s\n", pathtocheck);
1107 fprintf(stderr, "Try an absolute pathname.\n");
1108 exit(AKLOG_BADPATH);
1111 strcat(pathtocheck, DIRSTRING);
1112 strcat(pathtocheck, path);
1115 next_path(pathtocheck);
1117 /* Go on to the next level down the path */
1118 while ((nextpath = next_path(NULL))) {
1119 strcpy(pathtocheck, nextpath);
1121 printf("Checking directory %s\n", pathtocheck);
1124 * If this is an afs mountpoint, determine what cell from
1125 * the mountpoint name which is of the form
1126 * #cellname:volumename or %cellname:volumename.
1128 if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint))) {
1129 /* skip over the '#' or '%' */
1130 cell = mountpoint + 1;
1131 /* Add this (cell:volumename) to the list of zsubs */
1133 ll_string(&zsublist, ll_s_add, cell);
1135 add_hosts(pathtocheck);
1136 if ((endofcell = strchr(mountpoint, VOLMARKER))) {
1138 if ((auth_to_cell_status = auth_to_cell(context, cell, NULL))) {
1139 if (status == AKLOG_SUCCESS)
1140 status = auth_to_cell_status;
1141 else if (status != auth_to_cell_status)
1142 status = AKLOG_SOMETHINGSWRONG;
1147 if (isdir(pathtocheck, &isdirectory) < 0) {
1149 * If we've logged and still can't stat, there's
1152 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1153 pathtocheck, strerror(errno));
1154 return(AKLOG_BADPATH);
1156 else if (! isdirectory) {
1157 /* Allow only directories */
1158 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1160 return(AKLOG_BADPATH);
1169 #endif /* WINDOWS */
1172 /* Print usage message and exit */
1174 static void usage(void)
1177 #endif /* __STDC__ */
1179 fprintf(stderr, "\nUsage: %s %s%s%s\n", progname,
1180 "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1181 "[[-p | -path] pathname]\n",
1182 " [-zsubs] [-hosts] [-noauth] [-noprdb] [-force] [-setpag] \n"
1183 " [-linked] [-524]\n");
1184 fprintf(stderr, " -d gives debugging information.\n");
1185 fprintf(stderr, " krb_realm is the kerberos realm of a cell.\n");
1186 fprintf(stderr, " pathname is the name of a directory to which ");
1187 fprintf(stderr, "you wish to authenticate.\n");
1188 fprintf(stderr, " -zsubs gives zephyr subscription information.\n");
1189 fprintf(stderr, " -hosts gives host address information.\n");
1190 fprintf(stderr, " -noauth does not attempt to get tokens.\n");
1191 fprintf(stderr, " -noprdb means don't try to determine AFS ID.\n");
1192 fprintf(stderr, " -force means replace identical tickets. \n");
1193 fprintf(stderr, " -linked means if AFS node is linked, try both. \n");
1194 fprintf(stderr, " -setpag set the AFS process authentication group.\n");
1195 fprintf(stderr, " -524 means use the 524 converter instead of V5 directly\n");
1196 fprintf(stderr, " No commandline arguments means ");
1197 fprintf(stderr, "authenticate to the local cell.\n");
1198 fprintf(stderr, "\n");
1202 void aklog(int argc, char *argv[])
1204 krb5_context context;
1205 int status = AKLOG_SUCCESS;
1207 int somethingswrong = FALSE;
1209 cellinfo_t cellinfo;
1211 extern char *progname; /* Name of this program */
1213 extern int dflag; /* Debug mode */
1215 int cmode = FALSE; /* Cellname mode */
1216 int pmode = FALSE; /* Path name mode */
1218 char realm[REALM_SZ]; /* Kerberos realm of afs server */
1219 char cell[BUFSIZ]; /* Cell to which we are authenticating */
1220 char path[MAXPATHLEN + 1]; /* Path length for path mode */
1222 linked_list cells; /* List of cells to log to */
1223 linked_list paths; /* List of paths to log to */
1226 memset(&cellinfo, 0, sizeof(cellinfo));
1228 memset(realm, 0, sizeof(realm));
1229 memset(cell, 0, sizeof(cell));
1230 memset(path, 0, sizeof(path));
1238 /* Store the program name here for error messages */
1239 if ((progname = strrchr(argv[0], DIR)))
1244 krb5_init_context(&context);
1246 initialize_ktc_error_table ();
1249 /* Initialize list of cells to which we have authenticated */
1250 (void)ll_init(&authedcells);
1252 /* Parse commandline arguments and make list of what to do. */
1253 for (i = 1; i < argc; i++) {
1254 if (strcmp(argv[i], "-d") == 0)
1256 else if (strcmp(argv[i], "-noauth") == 0)
1258 else if (strcmp(argv[i], "-zsubs") == 0)
1260 else if (strcmp(argv[i], "-hosts") == 0)
1262 else if (strcmp(argv[i], "-noprdb") == 0)
1264 else if (strcmp(argv[i], "-linked") == 0)
1266 else if (strcmp(argv[i], "-force") == 0)
1268 else if (strcmp(argv[i], "-524") == 0)
1270 else if (strcmp(argv[i], "-setpag") == 0)
1272 else if (((strcmp(argv[i], "-cell") == 0) ||
1273 (strcmp(argv[i], "-c") == 0)) && !pmode)
1276 strcpy(cell, argv[i]);
1280 else if (((strcmp(argv[i], "-path") == 0) ||
1281 (strcmp(argv[i], "-p") == 0)) && !cmode)
1285 strcpy(path, argv[i]);
1291 fprintf(stderr, "%s: path mode not supported.\n", progname);
1294 #endif /* WINDOWS */
1296 else if (argv[i][0] == '-')
1298 else if (!pmode && !cmode) {
1299 if (strchr(argv[i], DIR) || (strcmp(argv[i], ".") == 0) ||
1300 (strcmp(argv[i], "..") == 0)) {
1303 strcpy(path, argv[i]);
1305 fprintf(stderr, "%s: path mode not supported.\n", progname);
1307 #endif /* WINDOWS */
1311 strcpy(cell, argv[i]);
1318 if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0)) {
1321 strcpy(realm, argv[i]);
1325 /* Add this cell to list of cells */
1326 strcpy(cellinfo.cell, cell);
1327 strcpy(cellinfo.realm, realm);
1328 if ((cur_node = ll_add_node(&cells, ll_tail))) {
1330 if ((new_cellinfo = copy_cellinfo(&cellinfo)))
1331 ll_add_data(cur_node, new_cellinfo);
1334 "%s: failure copying cellinfo.\n", progname);
1339 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1343 memset(&cellinfo, 0, sizeof(cellinfo));
1345 memset(cell, 0, sizeof(cell));
1346 memset(realm, 0, sizeof(realm));
1350 /* Add this path to list of paths */
1351 if ((cur_node = ll_add_node(&paths, ll_tail))) {
1353 if ((new_path = copy_string(path)))
1354 ll_add_data(cur_node, new_path);
1356 fprintf(stderr, "%s: failure copying path name.\n",
1362 fprintf(stderr, "%s: failure adding path to paths list.\n",
1367 memset(path, 0, sizeof(path));
1369 #endif /* WINDOWS */
1373 * The code that _used_ to be here called setpag(). When you think
1374 * about this, doing this makes no sense! setpag() allocates a PAG
1375 * only for the current process, so the token installed would have
1376 * not be usable in the parent! Since ktc_SetToken() now takes a
1377 * 4th argument to control whether or not we're going to allocate
1378 * a PAG (and since when you do it _that_ way, it modifies the cred
1379 * structure of your parent)), why don't we use that instead?
1390 printf("setpag %d\n",status);
1391 j = getgroups(gidsetlen,gidset);
1392 printf("Groups(%d):",j);
1393 for (i = 0; i<j; i++) {
1394 printf("%d",gidset[i]);
1395 if((i+1)<j) printf(",");
1401 /* If nothing was given, log to the local cell. */
1402 if ((cells.nelements + paths.nelements) == 0) {
1405 status = auth_to_cell(context, NULL, NULL);
1407 /* If this cell is linked to a DCE cell, and user
1408 * requested -linked, get tokens for both
1409 * This is very usefull when the AFS cell is linked to a DFS
1410 * cell and this system does not also have DFS.
1413 if (!status && linked && linkedcell[0]) {
1414 strncpy(linkedcell2,linkedcell,MAXCELLCHARS);
1416 printf("Linked cell: %s\n", linkedcell);
1418 status = auth_to_cell(context, linkedcell2, NULL);
1423 * Local hack - if the person has a file in their home
1424 * directory called ".xlog", read that for a list of
1425 * extra cells to authenticate to
1428 if ((pwd = getpwuid(getuid())) != NULL) {
1431 char fcell[100], xlog_path[512];
1433 strcpy(xlog_path, pwd->pw_dir);
1434 strcat(xlog_path, "/.xlog");
1436 if ((stat(xlog_path, &sbuf) == 0) &&
1437 ((f = fopen(xlog_path, "r")) != NULL)) {
1440 printf("Reading %s for cells to "
1441 "authenticate to.\n", xlog_path);
1444 while (fgets(fcell, 100, f) != NULL) {
1447 fcell[strlen(fcell) - 1] = '\0';
1450 printf("Found cell %s in %s.\n",
1454 auth_status = auth_to_cell(context, fcell, NULL);
1455 if (status == AKLOG_SUCCESS)
1456 status = auth_status;
1458 status = AKLOG_SOMETHINGSWRONG;
1462 #endif /* WINDOWS */
1465 /* Log to all cells in the cells list first */
1466 for (cur_node = cells.first; cur_node; cur_node = cur_node->next) {
1467 memcpy((char *)&cellinfo, cur_node->data, sizeof(cellinfo));
1468 if ((status = auth_to_cell(context, cellinfo.cell, cellinfo.realm)))
1471 if (linked && linkedcell[0]) {
1472 strncpy(linkedcell2,linkedcell,MAXCELLCHARS);
1474 printf("Linked cell: %s\n",
1477 if ((status = auth_to_cell(context,linkedcell2,
1485 /* Then, log to all paths in the paths list */
1486 for (cur_node = paths.first; cur_node; cur_node = cur_node->next) {
1487 if ((status = auth_to_path(context, cur_node->data)))
1490 #endif /* WINDOWS */
1493 * If only one thing was logged to, we'll return the status
1494 * of the single call. Otherwise, we'll return a generic
1495 * something failed status.
1497 if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1498 status = AKLOG_SOMETHINGSWRONG;
1501 /* If we are keeping track of zephyr subscriptions, print them. */
1503 for (cur_node = zsublist.first; cur_node; cur_node = cur_node->next) {
1504 printf("zsub: %s\n", cur_node->data);
1507 /* If we are keeping track of host information, print it. */
1509 for (cur_node = hostlist.first; cur_node; cur_node = cur_node->next) {
1510 printf("host: %s\n", cur_node->data);
1516 #ifndef HAVE_ADD_TO_ERROR_TABLE
1517 #include <afs/error_table.h>
1519 void add_error_table (const struct error_table *);
1522 add_to_error_table(struct et_list *new_table)
1524 add_error_table(new_table->table);
1526 #endif /* HAVE_ADD_TO_ERROR_TABLE */
1528 static int isdir(char *path, unsigned char *val)
1530 struct stat statbuf;
1532 if (lstat(path, &statbuf) < 0)
1535 if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
1543 static krb5_error_code get_credv5(krb5_context context,
1544 char *name, char *inst, char *realm,
1549 static krb5_principal client_principal = 0;
1551 memset((char *)&increds, 0, sizeof(increds));
1552 /* ANL - instance may be ptr to a null string. Pass null then */
1553 if ((r = krb5_build_principal(context, &increds.server,
1554 strlen(realm), realm,
1556 (inst && strlen(inst)) ? inst : (void *) NULL,
1561 if (!_krb425_ccache) {
1562 r = krb5_cc_default(context, &_krb425_ccache);
1566 if (!client_principal) {
1567 r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
1572 increds.client = client_principal;
1573 increds.times.endtime = 0;
1574 /* Ask for DES since that is what V4 understands */
1575 increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
1577 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
1583 static int get_user_realm(krb5_context context, char *realm)
1585 static krb5_principal client_principal = 0;
1588 if (!_krb425_ccache)
1589 krb5_cc_default(context, &_krb425_ccache);
1590 if (!client_principal)
1591 krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
1593 i = krb5_princ_realm(context, client_principal)->length;
1594 if (i > REALM_SZ-1) i = REALM_SZ-1;
1595 strncpy(realm,krb5_princ_realm(context, client_principal)->data,i);