2 * Copyright (C) 1989,2004 by the Massachusetts Institute of Technology
4 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
5 * distribute this software and its documentation for any purpose and
6 * without fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright notice and
8 * this permission notice appear in supporting documentation, and that
9 * the name of M.I.T. not be used in advertising or publicity pertaining
10 * to distribution of the software without specific, written prior
11 * permission. M.I.T. makes no representations about the suitability of
12 * this software for any purpose. It is provided "as is" without express
13 * or implied warranty.
20 #include <sys/types.h>
26 #include <afs/ptserver.h>
27 #include <afs/ptuser.h>
32 #include <cm_config.h>
34 #include <cellconfig.h>
35 #include <pioctl_nt.h>
36 #include <smb_iocons.h>
40 #define __S_ISTYPE(mode, mask) (((mode) & _S_IFMT) == (mask))
41 #define S_ISDIR(mode) __S_ISTYPE((mode), _S_IFDIR)
43 #define DONT_HAVE_GET_AD_TKT
44 #define MAXSYMLINKS 255
46 /* Win32 uses get_krb_err_txt_entry(status) instead of krb_err_txt[status],
47 * so we use a bit of indirection like the GNU CVS sources.
49 #define krb_err_text(status) get_krb_err_txt_entry(status)
51 #define DRIVECOLON ':' /* Drive letter separator */
52 #define BDIR '\\' /* Other character that divides directories */
55 readlink(char *path, char *buf, int buffers)
60 char * getcwd(char*, size_t);
63 get_cellconfig_callback(void *cellconfig, struct sockaddr_in *addrp, char *namep)
65 struct afsconf_cell *cc = (struct afsconf_cell *) cellconfig;
67 cc->hostAddr[cc->numServers] = *addrp;
68 strcpy(cc->hostName[cc->numServers], namep);
74 #include <sys/param.h>
76 #include <arpa/inet.h>
77 #include <sys/socket.h>
80 #include <afs/param.h>
82 #include <afs/cellconfig.h>
84 #include <afs/venus.h>
85 #include <afs/ptserver.h>
87 #define krb_err_text(status) krb_err_txt[status]
89 /* Cheesy test for determining AFS 3.5. */
90 #ifndef AFSCONF_CLIENTNAME
95 #include <afs/dirpath.h>
97 #define AFSDIR_CLIENT_ETC_DIRPATH AFSCONF_CLIENTNAME
102 #include "linked_list.h"
107 #define AKLOG_SUCCESS 0
108 #define AKLOG_USAGE 1
109 #define AKLOG_SOMETHINGSWRONG 2
111 #define AKLOG_KERBEROS 4
112 #define AKLOG_TOKEN 5
113 #define AKLOG_BADPATH 6
129 #define MAXSYMLINKS 15
132 #define DIR '/' /* Character that divides directories */
133 #define DIRSTRING "/" /* String form of above */
134 #define VOLMARKER ':' /* Character separating cellname from mntpt */
135 #define VOLMARKERSTRING ":" /* String form of above */
139 char realm[REALM_SZ];
143 struct afsconf_cell ak_cellconfig; /* General information about the cell */
145 static char *progname = NULL; /* Name of this program */
146 static int dflag = FALSE; /* Give debugging information */
147 static int noprdb = FALSE; /* Skip resolving name to id? */
148 static int force = FALSE; /* Bash identical tokens? */
149 static linked_list authedcells; /* List of cells already logged to */
151 static int usev5 = TRUE; /* use kerberos 5? */
152 static int use524 = FALSE; /* use krb524? */
153 static krb5_ccache _krb425_ccache;
155 long GetLocalCell(struct afsconf_dir **pconfigdir, char *local_cell)
157 if (!(*pconfigdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)))
159 fprintf(stderr, "%s: can't get afs configuration (afsconf_Open(%s))\n",
160 progname, AFSDIR_CLIENT_ETC_DIRPATH);
164 return afsconf_GetLocalCell(*pconfigdir, local_cell, MAXCELLCHARS);
167 long GetCellInfo(struct afsconf_dir **pconfigdir, char* cell,
168 struct afsconf_cell **pcellconfig)
170 return afsconf_GetCellInfo(*pconfigdir, cell, NULL, *pcellconfig);
173 void CloseConf(struct afsconf_dir **pconfigdir)
175 (void) afsconf_Close(*pconfigdir);
178 #define ALLOW_REGISTER 1
179 void ViceIDToUsername(char *username, char *realm_of_user, char *realm_of_cell,
180 char * cell_to_use, CREDENTIALS *c,
182 struct ktc_principal *aclient, struct ktc_principal *aserver, struct ktc_token *atoken)
184 static char lastcell[MAXCELLCHARS+1] = { 0 };
185 static char confname[512] = { 0 };
186 afs_int32 viceId; /* AFS uid of user */
187 #ifdef ALLOW_REGISTER
189 #endif /* ALLOW_REGISTER */
191 if (confname[0] == '\0') {
192 strncpy(confname, AFSDIR_CLIENT_ETC_DIRPATH, sizeof(confname));
193 confname[sizeof(confname) - 2] = '\0';
197 printf("About to resolve name %s to id\n", username);
199 strcpy(lastcell, aserver->cell);
201 if (!pr_Initialize (0, confname, aserver->cell)) {
202 char sname[PR_MAXNAMELEN], *at;
204 strncpy(sname, username, PR_MAXNAMELEN);
205 sname[PR_MAXNAMELEN-1] = '\0';
207 at = strchr(sname, '@');
208 if (at && !stricmp(at+1, realm_of_cell))
210 *status = pr_SNameToId (sname, &viceId);
216 printf("Error %d\n", *status);
218 printf("Id %d\n", viceId);
222 * This is a crock, but it is Transarc's crock, so
223 * we have to play along in order to get the
224 * functionality. The way the afs id is stored is
225 * as a string in the username field of the token.
226 * Contrary to what you may think by looking at
227 * the code for tokens, this hack (AFS ID %d) will
228 * not work if you change %d to something else.
232 * This code is taken from cklog -- it lets people
233 * automatically register with the ptserver in foreign cells
236 #ifdef ALLOW_REGISTER
238 if (viceId != ANONYMOUSID) {
239 #else /* ALLOW_REGISTER */
240 if ((*status == 0) && (viceId != ANONYMOUSID))
241 #endif /* ALLOW_REGISTER */
243 #ifdef AFS_ID_TO_NAME
244 strncpy(username_copy, username, BUFSIZ);
245 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
246 #endif /* AFS_ID_TO_NAME */
248 #ifdef ALLOW_REGISTER
249 } else if (strcmp(realm_of_user, realm_of_cell) != 0) {
251 printf("doing first-time registration of %s "
252 "at %s\n", username, cell_to_use);
255 strncpy(aclient->name, username, MAXKTCNAMELEN - 1);
256 strcpy(aclient->instance, "");
257 strncpy(aclient->cell, c->realm, MAXKTCREALMLEN - 1);
258 if ((*status = ktc_SetToken(aserver, atoken, aclient, 0))) {
259 printf("%s: unable to obtain tokens for cell %s "
260 "(status: %d).\n", progname, cell_to_use, status);
261 *status = AKLOG_TOKEN;
266 * In case you're wondering, we don't need to change the
267 * filename here because we're still connecting to the
268 * same cell -- we're just using a different authentication
272 if ((*status = pr_Initialize(1L, confname, aserver->cell))) {
273 printf("Error %d\n", status);
277 if ((*status = pr_CreateUser(username, &id))) {
278 printf("%s: unable to create remote PTS "
279 "user %s in cell %s (status: %d).\n", progname,
280 username, cell_to_use, *status);
282 printf("created cross-cell entry for %s at %s\n",
283 username, cell_to_use);
284 #ifdef AFS_ID_TO_NAME
285 strncpy(username_copy, username, BUFSIZ);
286 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
287 #endif /* AFS_ID_TO_NAME */
291 #endif /* ALLOW_REGISTER */
294 char *LastComponent(char *str)
296 char *ret = strrchr(str, DIR);
300 ret = strrchr(str, BDIR);
305 int FirstComponent(char *str)
314 void CopyPathColon(char *origpath, char *path, char *pathtocheck)
317 if (origpath[1] == DRIVECOLON)
319 strncpy(pathtocheck, origpath, 2);
320 strcpy(path, origpath+2);
324 strcpy(path, origpath);
327 int BeginsWithDir(char *str, int colon)
329 return (str[0] == DIR) ||
331 ((str[0] == BDIR) || (colon && str[1] == DRIVECOLON));
338 /* This is a pretty gross hack. Linking against the Transarc
339 * libraries pulls in some rxkad functions which use des. (I don't
340 * think they ever get called.) With Transarc-supplied libraries this
341 * creates a reliance on the symbol des_pcbc_init() which is only in
342 * Transarc's DES libraries (it's an exportability symbol-hiding
343 * thing), which we don't want to use because they don't work with
344 * MIT's krb4 routines. So export a des_pcbc_init() symbol here so we
345 * don't have to link against Transarc's des library.
352 static int get_cred(char *name, char *inst, char *realm, CREDENTIALS *c)
356 status = krb_get_cred(name, inst, realm, c);
357 if (status != KSUCCESS)
359 #ifdef DONT_HAVE_GET_AD_TKT
361 status = krb_mk_req(&ticket, name, inst, realm, 0);
363 status = get_ad_tkt(name, inst, realm, 255);
365 if (status == KSUCCESS)
366 status = krb_get_cred(name, inst, realm, c);
372 static int get_v5cred(krb5_context context,
373 char *name, char *inst, char *realm, CREDENTIALS *c,
378 static krb5_principal client_principal = 0;
380 memset((char *)&increds, 0, sizeof(increds));
382 if ((r = krb5_build_principal(context, &increds.server,
383 strlen(realm), realm,
385 (inst && strlen(inst)) ? inst : 0,
391 krb5_cc_default(context, &_krb425_ccache);
392 if (!client_principal)
393 krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
395 increds.client = client_principal;
396 increds.times.endtime = 0;
397 /* Ask for DES since that is what V4 understands */
398 increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
400 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
404 /* This requires krb524d to be running with the KDC */
406 r = krb5_524_convert_creds(context, *creds, c);
410 /* There is no header for this function. It is supposed to be private */
411 int krb_get_admhst(char *h,char *r, int n);
413 static char *afs_realm_of_cell(struct afsconf_cell *cellconfig)
415 char krbhst[MAX_HSTNM];
416 static char krbrlm[REALM_SZ+1];
421 strcpy(krbrlm, (char *) krb_realmofhost(cellconfig->hostName[0]));
423 if (krb_get_admhst(krbhst, krbrlm, 1) != KSUCCESS)
426 char *t = cellconfig->name;
440 static char *afs_realm_of_cell5(krb5_context context, struct afsconf_cell *cellconfig)
443 static char krbrlm[REALM_SZ+1];
444 krb5_error_code status;
449 status = krb5_get_host_realm( context, cellconfig->hostName[0], &krbrlms );
451 if (status == 0 && krbrlms && krbrlms[0]) {
452 strcpy(krbrlm, krbrlms[0]);
454 strcpy(krbrlm, cellconfig->name);
459 krb5_free_host_realm( context, krbrlms );
464 static char *copy_cellinfo(cellinfo_t *cellinfo)
466 cellinfo_t *new_cellinfo;
468 if (new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t)))
469 memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
471 return ((char *)new_cellinfo);
475 static char *copy_string(char *string)
479 if (new_string = (char *)calloc(strlen(string) + 1, sizeof(char)))
480 (void) strcpy(new_string, string);
486 static int get_cellconfig(char *cell, struct afsconf_cell *cellconfig,
489 int status = AKLOG_SUCCESS;
490 struct afsconf_dir *configdir = 0;
492 memset(local_cell, 0, sizeof(local_cell));
493 memset(cellconfig, 0, sizeof(*cellconfig));
495 if (GetLocalCell(&configdir, local_cell))
497 fprintf(stderr, "%s: can't determine local cell.\n", progname);
501 if ((cell == NULL) || (cell[0] == 0))
504 if (GetCellInfo(&configdir, cell, &cellconfig))
506 fprintf(stderr, "%s: Can't get information about cell %s.\n",
512 CloseConf(&configdir);
517 static int get_v5_user_realm(krb5_context context,char *realm)
519 static krb5_principal client_principal = 0;
523 krb5_cc_default(context, &_krb425_ccache);
524 if (!client_principal)
525 krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
527 i = krb5_princ_realm(context, client_principal)->length;
528 if (i < REALM_SZ-1) i = REALM_SZ-1;
529 strncpy(realm,krb5_princ_realm(context, client_principal)->data,i);
535 * Log to a cell. If the cell has already been logged to, return without
536 * doing anything. Otherwise, log to it and mark that it has been logged
538 static int auth_to_cell(krb5_context context, char *cell, char *realm)
540 int status = AKLOG_SUCCESS;
541 char username[BUFSIZ]; /* To hold client username structure */
543 char name[ANAME_SZ]; /* Name of afs key */
544 char instance[INST_SZ]; /* Instance of afs key */
545 char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
546 char realm_of_cell[REALM_SZ]; /* Kerberos realm of cell */
547 char local_cell[MAXCELLCHARS+1];
548 char cell_to_use[MAXCELLCHARS+1]; /* Cell to authenticate to */
550 krb5_creds *v5cred = NULL;
552 struct ktc_principal aserver;
553 struct ktc_principal aclient;
554 struct ktc_token atoken, btoken;
557 /* try to avoid an expensive call to get_cellconfig */
558 if (cell && ll_string_check(&authedcells, cell))
561 printf("Already authenticated to %s (or tried to)\n", cell);
562 return(AKLOG_SUCCESS);
565 memset(name, 0, sizeof(name));
566 memset(instance, 0, sizeof(instance));
567 memset(realm_of_user, 0, sizeof(realm_of_user));
568 memset(realm_of_cell, 0, sizeof(realm_of_cell));
570 /* NULL or empty cell returns information on local cell */
571 if (status = get_cellconfig(cell, &ak_cellconfig, local_cell))
574 strncpy(cell_to_use, ak_cellconfig.name, MAXCELLCHARS);
575 cell_to_use[MAXCELLCHARS] = 0;
577 if (ll_string_check(&authedcells, cell_to_use))
580 printf("Already authenticated to %s (or tried to)\n", cell_to_use);
581 return(AKLOG_SUCCESS);
585 * Record that we have attempted to log to this cell. We do this
586 * before we try rather than after so that we will not try
587 * and fail repeatedly for one cell.
589 (void)ll_add_string(&authedcells, cell_to_use);
592 printf("Authenticating to cell %s.\n", cell_to_use);
594 if (realm && realm[0])
595 strcpy(realm_of_cell, realm);
597 strcpy(realm_of_cell,
599 afs_realm_of_cell5(context, &ak_cellconfig) :
600 afs_realm_of_cell(&ak_cellconfig));
602 /* We use the afs.<cellname> convention here... */
603 strcpy(name, AFSKEY);
604 strncpy(instance, cell_to_use, sizeof(instance));
605 instance[sizeof(instance)-1] = '\0';
608 * Extract the session key from the ticket file and hand-frob an
609 * afs style authenticator.
616 if ( strchr(name,'.') != NULL ) {
617 fprintf(stderr, "%s: Can't support principal names including a dot.\n",
624 printf("Getting v5 tickets: %s/%s@%s\n", name, instance, realm_of_cell);
625 status = get_v5cred(context, name, instance, realm_of_cell,
626 use524 ? &c : NULL, &v5cred);
627 if (status == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN) {
629 printf("Getting v5 tickets: %s@%s\n", name, realm_of_cell);
630 status = get_v5cred(context, name, "", realm_of_cell,
631 use524 ? &c : NULL, &v5cred);
633 if ( status == KRB5KRB_AP_ERR_MSG_TYPE && retry ) {
641 * Try to obtain AFS tickets. Because there are two valid service
642 * names, we will try both, but trying the more specific first.
648 printf("Getting tickets: %s.%s@%s\n", name, instance, realm_of_cell);
649 status = get_cred(name, instance, realm_of_cell, &c);
650 if (status == KDC_PR_UNKNOWN)
653 printf("Getting tickets: %s@%s\n", name, realm_of_cell);
654 status = get_cred(name, "", realm_of_cell, &c);
658 /* TODO: get k5 error text */
659 if (status != KSUCCESS)
662 printf("Kerberos error code returned by get_cred: %d\n", status);
663 fprintf(stderr, "%s: Couldn't get %s AFS tickets: %s\n",
664 progname, cell_to_use,
666 krb_err_text(status));
667 return(AKLOG_KERBEROS);
670 strncpy(aserver.name, AFSKEY, MAXKTCNAMELEN - 1);
671 strncpy(aserver.instance, AFSINST, MAXKTCNAMELEN - 1);
672 strncpy(aserver.cell, cell_to_use, MAXKTCREALMLEN - 1);
674 if (usev5 && !use524) {
675 /* This code inserts the entire K5 ticket into the token
676 * No need to perform a krb524 translation which is
677 * commented out in the code below
682 len = min(v5cred->client->data[0].length,MAXKTCNAMELEN - 1);
683 strncpy(username, v5cred->client->data[0].data, len);
684 username[len] = '\0';
686 if ( v5cred->client->length > 1 ) {
687 strcat(username, ".");
688 p = username + strlen(username);
689 len = min(v5cred->client->data[1].length,MAXKTCNAMELEN - strlen(username) - 1);
690 strncpy(p, v5cred->client->data[1].data, len);
694 memset(&atoken, '\0', sizeof(atoken));
695 atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
696 atoken.startTime = v5cred->times.starttime;
697 atoken.endTime = v5cred->times.endtime;
698 memcpy(&atoken.sessionKey, v5cred->keyblock.contents, v5cred->keyblock.length);
699 atoken.ticketLen = v5cred->ticket.length;
700 memcpy(atoken.ticket, v5cred->ticket.data, atoken.ticketLen);
702 strcpy (username, c.pname);
705 strcat(username, ".");
706 strcat(username, c.pinst);
709 atoken.kvno = c.kvno;
710 atoken.startTime = c.issue_date;
711 /* ticket lifetime is in five-minutes blocks. */
712 atoken.endTime = c.issue_date + ((unsigned char)c.lifetime * 5 * 60);
714 memcpy(&atoken.sessionKey, c.session, 8);
715 atoken.ticketLen = c.ticket_st.length;
716 memcpy(atoken.ticket, c.ticket_st.dat, atoken.ticketLen);
720 !ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient) &&
721 atoken.kvno == btoken.kvno &&
722 atoken.ticketLen == btoken.ticketLen &&
723 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
724 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen))
727 printf("Identical tokens already exist; skipping.\n");
734 printf("Not resolving name %s to id (-noprdb set)\n", username);
739 if((status = get_v5_user_realm(context, realm_of_user)) != KSUCCESS) {
740 fprintf(stderr, "%s: Couldn't determine realm of user: %d\n",
742 return(AKLOG_KERBEROS);
745 if ((status = krb_get_tf_realm(TKT_FILE, realm_of_user)) != KSUCCESS)
747 fprintf(stderr, "%s: Couldn't determine realm of user: %s)",
748 progname, krb_err_text(status));
749 return(AKLOG_KERBEROS);
753 /* For Khimaira we want to always append the realm to the name */
754 if (1 /* strcmp(realm_of_user, realm_of_cell) */)
756 strcat(username, "@");
757 strcat(username, realm_of_user);
760 ViceIDToUsername(username, realm_of_user, realm_of_cell, cell_to_use, &c, &status, &aclient, &aserver, &atoken);
764 printf("Set username to %s\n", username);
766 /* Reset the "aclient" structure before we call ktc_SetToken.
767 * This structure was first set by the ktc_GetToken call when
768 * we were comparing whether identical tokens already existed.
770 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
771 strcpy(aclient.instance, "");
773 if (usev5 && !use524) {
774 int len = min(v5cred->client->realm.length,MAXKTCNAMELEN - 1);
775 strncpy(aclient.cell, v5cred->client->realm.data, len);
776 aclient.cell[len] = '\0';
778 strncpy(aclient.cell, c.realm, MAXKTCREALMLEN - 1);
781 printf("Getting tokens.\n");
782 if (status = ktc_SetToken(&aserver, &atoken, &aclient, 0))
785 "%s: unable to obtain tokens for cell %s (status: %d).\n",
786 progname, cell_to_use, status);
787 status = AKLOG_TOKEN;
793 static int get_afs_mountpoint(char *file, char *mountpoint, int size)
795 char our_file[MAXPATHLEN + 1];
797 char *last_component;
798 struct ViceIoctl vio;
799 char cellname[BUFSIZ];
801 memset(our_file, 0, sizeof(our_file));
802 strcpy(our_file, file);
804 if (last_component = LastComponent(our_file))
806 *last_component++ = 0;
807 parent_dir = our_file;
811 last_component = our_file;
815 memset(cellname, 0, sizeof(cellname));
817 vio.in = last_component;
818 vio.in_size = strlen(last_component)+1;
820 vio.out = mountpoint;
822 if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0))
824 if (strchr(mountpoint, VOLMARKER) == NULL)
827 vio.in_size = strlen(file) + 1;
828 vio.out_size = sizeof(cellname);
831 if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1))
833 strcat(cellname, VOLMARKERSTRING);
834 strcat(cellname, mountpoint + 1);
835 memset(mountpoint + 1, 0, size - 1);
836 strcpy(mountpoint + 1, cellname);
847 * This routine each time it is called returns the next directory
848 * down a pathname. It resolves all symbolic links. The first time
849 * it is called, it should be called with the name of the path
850 * to be descended. After that, it should be called with the arguemnt
853 static char *next_path(char *origpath)
855 static char path[MAXPATHLEN + 1];
856 static char pathtocheck[MAXPATHLEN + 1];
858 int link = FALSE; /* Is this a symbolic link? */
859 char linkbuf[MAXPATHLEN + 1];
860 char tmpbuf[MAXPATHLEN + 1];
862 static char *last_comp; /* last component of directory name */
863 static char *elast_comp; /* End of last component */
867 static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
869 /* If we are given something for origpath, we are initializing only. */
872 memset(path, 0, sizeof(path));
873 memset(pathtocheck, 0, sizeof(pathtocheck));
874 CopyPathColon(origpath, path, pathtocheck);
880 /* We were not given origpath; find then next path to check */
882 /* If we've gotten all the way through already, return NULL */
883 if (last_comp == NULL)
888 while (BeginsWithDir(last_comp, FALSE))
889 strncat(pathtocheck, last_comp++, 1);
890 len = (elast_comp = LastComponent(last_comp))
891 ? elast_comp - last_comp : strlen(last_comp);
892 strncat(pathtocheck, last_comp, len);
893 memset(linkbuf, 0, sizeof(linkbuf));
894 if (link = (readlink(pathtocheck, linkbuf, sizeof(linkbuf)) > 0))
896 if (++symlinkcount > MAXSYMLINKS)
898 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
901 memset(tmpbuf, 0, sizeof(tmpbuf));
903 strcpy(tmpbuf, elast_comp);
904 if (BeginsWithDir(linkbuf, FALSE))
907 * If this is a symbolic link to an absolute path,
908 * replace what we have by the absolute path.
910 memset(path, 0, strlen(path));
911 memcpy(path, linkbuf, sizeof(linkbuf));
912 strcat(path, tmpbuf);
915 memset(pathtocheck, 0, sizeof(pathtocheck));
920 * If this is a symbolic link to a relative path,
921 * replace only the last component with the link name.
923 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
924 strcat(path, tmpbuf);
926 if (t = LastComponent(pathtocheck))
929 memset(t, 0, strlen(t));
932 memset(pathtocheck, 0, sizeof(pathtocheck));
936 last_comp = elast_comp;
944 * This routine descends through a path to a directory, logging to
945 * every cell it encounters along the way.
947 static int auth_to_path(krb5_context context, char *path)
949 int status = AKLOG_SUCCESS;
950 int auth_to_cell_status = AKLOG_SUCCESS;
953 char pathtocheck[MAXPATHLEN + 1];
954 char mountpoint[MAXPATHLEN + 1];
960 if (BeginsWithDir(path, TRUE))
961 strcpy(pathtocheck, path);
964 if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL)
966 fprintf(stderr, "Unable to find current working directory:\n");
967 fprintf(stderr, "%s\n", pathtocheck);
968 fprintf(stderr, "Try an absolute pathname.\n");
973 /* in WIN32, if getcwd returns a root dir (eg: c:\), the returned string
974 * will already have a trailing slash ('\'). Otherwise, the string will
975 * end in the last directory name */
977 if(pathtocheck[strlen(pathtocheck) - 1] != BDIR)
979 strcat(pathtocheck, DIRSTRING);
980 strcat(pathtocheck, path);
983 next_path(pathtocheck);
985 /* Go on to the next level down the path */
986 while (nextpath = next_path(NULL))
988 strcpy(pathtocheck, nextpath);
990 printf("Checking directory [%s]\n", pathtocheck);
992 * If this is an afs mountpoint, determine what cell from
993 * the mountpoint name which is of the form
994 * #cellname:volumename or %cellname:volumename.
996 if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint)))
999 printf("Found mount point [%s]\n", mountpoint);
1000 /* skip over the '#' or '%' */
1001 cell = mountpoint + 1;
1002 if (endofcell = strchr(mountpoint, VOLMARKER))
1005 if (auth_to_cell_status = auth_to_cell(context, cell, NULL))
1007 if (status == AKLOG_SUCCESS)
1008 status = auth_to_cell_status;
1009 else if (status != auth_to_cell_status)
1010 status = AKLOG_SOMETHINGSWRONG;
1018 if (lstat(pathtocheck, &st) < 0)
1021 * If we've logged and still can't stat, there's
1024 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1025 pathtocheck, strerror(errno));
1026 return(AKLOG_BADPATH);
1028 else if (!S_ISDIR(st.st_mode))
1030 /* Allow only directories */
1031 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1033 return(AKLOG_BADPATH);
1041 /* Print usage message and exit */
1042 static void usage(void)
1044 fprintf(stderr, "\nUsage: %s %s%s%s%s\n", progname,
1045 "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1046 "[[-p | -path] pathname]\n",
1047 " [-noprdb] [-force]\n",
1050 fprintf(stderr, " -d gives debugging information.\n");
1051 fprintf(stderr, " krb_realm is the kerberos realm of a cell.\n");
1052 fprintf(stderr, " pathname is the name of a directory to which ");
1053 fprintf(stderr, "you wish to authenticate.\n");
1054 fprintf(stderr, " -noprdb means don't try to determine AFS ID.\n");
1055 fprintf(stderr, " -5 or -4 selects whether to use Kerberos V or Kerberos IV.\n"
1056 " (default is Kerberos V)\n");
1057 fprintf(stderr, " -m means use krb524d to convert Kerberos V tickets.\n");
1058 fprintf(stderr, " No commandline arguments means ");
1059 fprintf(stderr, "authenticate to the local cell.\n");
1060 fprintf(stderr, "\n");
1064 int main(int argc, char *argv[])
1066 int status = AKLOG_SUCCESS;
1068 int somethingswrong = FALSE;
1070 cellinfo_t cellinfo;
1072 extern char *progname; /* Name of this program */
1074 extern int dflag; /* Debug mode */
1076 int cmode = FALSE; /* Cellname mode */
1077 int pmode = FALSE; /* Path name mode */
1079 char realm[REALM_SZ]; /* Kerberos realm of afs server */
1080 char cell[BUFSIZ]; /* Cell to which we are authenticating */
1081 char path[MAXPATHLEN + 1]; /* Path length for path mode */
1083 linked_list cells; /* List of cells to log to */
1084 linked_list paths; /* List of paths to log to */
1087 krb5_context context = 0;
1089 memset(&cellinfo, 0, sizeof(cellinfo));
1091 memset(realm, 0, sizeof(realm));
1092 memset(cell, 0, sizeof(cell));
1093 memset(path, 0, sizeof(path));
1098 /* Store the program name here for error messages */
1099 if (progname = LastComponent(argv[0]))
1104 /* Initialize list of cells to which we have authenticated */
1105 (void)ll_init(&authedcells);
1107 /* Parse commandline arguments and make list of what to do. */
1108 for (i = 1; i < argc; i++)
1110 if (strcmp(argv[i], "-d") == 0)
1112 else if (strcmp(argv[i], "-5") == 0)
1114 else if (strcmp(argv[i], "-m") == 0)
1116 else if (strcmp(argv[i], "-4") == 0)
1118 else if (strcmp(argv[i], "-noprdb") == 0)
1120 else if (strcmp(argv[i], "-force") == 0)
1122 else if (((strcmp(argv[i], "-cell") == 0) ||
1123 (strcmp(argv[i], "-c") == 0)) && !pmode)
1128 strcpy(cell, argv[i]);
1133 else if (((strcmp(argv[i], "-path") == 0) ||
1134 (strcmp(argv[i], "-p") == 0)) && !cmode)
1139 strcpy(path, argv[i]);
1144 else if (argv[i][0] == '-')
1146 else if (!pmode && !cmode)
1148 if (FirstComponent(argv[i]) || (strcmp(argv[i], ".") == 0) ||
1149 (strcmp(argv[i], "..") == 0))
1152 strcpy(path, argv[i]);
1157 strcpy(cell, argv[i]);
1165 if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0))
1169 strcpy(realm, argv[i]);
1173 /* Add this cell to list of cells */
1174 strcpy(cellinfo.cell, cell);
1175 strcpy(cellinfo.realm, realm);
1176 if (cur_node = ll_add_node(&cells, ll_tail))
1179 if (new_cellinfo = copy_cellinfo(&cellinfo))
1180 ll_add_data(cur_node, new_cellinfo);
1183 fprintf(stderr, "%s: failure copying cellinfo.\n", progname);
1189 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1193 memset(&cellinfo, 0, sizeof(cellinfo));
1195 memset(cell, 0, sizeof(cell));
1196 memset(realm, 0, sizeof(realm));
1200 /* Add this path to list of paths */
1201 if (cur_node = ll_add_node(&paths, ll_tail))
1204 if (new_path = copy_string(path))
1205 ll_add_data(cur_node, new_path);
1208 fprintf(stderr, "%s: failure copying path name.\n",
1215 fprintf(stderr, "%s: failure adding path to paths list.\n",
1220 memset(path, 0, sizeof(path));
1225 krb5_init_context(&context);
1227 /* If nothing was given, log to the local cell. */
1228 if ((cells.nelements + paths.nelements) == 0)
1229 status = auth_to_cell(context, NULL, NULL);
1232 /* Log to all cells in the cells list first */
1233 for (cur_node = cells.first; cur_node; cur_node = cur_node->next)
1235 memcpy(&cellinfo, cur_node->data, sizeof(cellinfo));
1236 if (status = auth_to_cell(context,
1237 cellinfo.cell, cellinfo.realm))
1241 /* Then, log to all paths in the paths list */
1242 for (cur_node = paths.first; cur_node; cur_node = cur_node->next)
1244 if (status = auth_to_path(context,
1250 * If only one thing was logged to, we'll return the status
1251 * of the single call. Otherwise, we'll return a generic
1252 * something failed status.
1254 if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1255 status = AKLOG_SOMETHINGSWRONG;
1259 krb5_free_context(context);