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 long 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);
201 * Talk about DUMB! It turns out that there is a bug in
202 * pr_Initialize -- even if you give a different cell name
203 * to it, it still uses a connection to a previous AFS server
204 * if one exists. The way to fix this is to change the
205 * _filename_ argument to pr_Initialize - that forces it to
206 * re-initialize the connection. We do this by adding and
207 * removing a "/" on the end of the configuration directory name.
210 if (lastcell[0] != '\0' && (strcmp(lastcell, aserver->cell) != 0)) {
211 int i = strlen(confname);
212 if (confname[i - 1] == '/') {
213 confname[i - 1] = '\0';
216 confname[i + 1] = '\0';
221 strcpy(lastcell, aserver->cell);
223 if (!pr_Initialize (0, confname, aserver->cell)) {
224 char sname[PR_MAXNAMELEN];
225 strncpy(sname, username, PR_MAXNAMELEN);
226 sname[PR_MAXNAMELEN-1] = '\0';
227 *status = pr_SNameToId (sname, &viceId);
233 printf("Error %d\n", *status);
235 printf("Id %d\n", viceId);
239 * This is a crock, but it is Transarc's crock, so
240 * we have to play along in order to get the
241 * functionality. The way the afs id is stored is
242 * as a string in the username field of the token.
243 * Contrary to what you may think by looking at
244 * the code for tokens, this hack (AFS ID %d) will
245 * not work if you change %d to something else.
249 * This code is taken from cklog -- it lets people
250 * automatically register with the ptserver in foreign cells
253 #ifdef ALLOW_REGISTER
255 if (viceId != ANONYMOUSID) {
256 #else /* ALLOW_REGISTER */
257 if ((*status == 0) && (viceId != ANONYMOUSID))
258 #endif /* ALLOW_REGISTER */
260 #ifdef AFS_ID_TO_NAME
261 strncpy(username_copy, username, BUFSIZ);
262 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
263 #endif /* AFS_ID_TO_NAME */
265 #ifdef ALLOW_REGISTER
266 } else if (strcmp(realm_of_user, realm_of_cell) != 0) {
268 printf("doing first-time registration of %s "
269 "at %s\n", username, cell_to_use);
272 strncpy(aclient->name, username, MAXKTCNAMELEN - 1);
273 strcpy(aclient->instance, "");
274 strncpy(aclient->cell, c->realm, MAXKTCREALMLEN - 1);
275 if ((*status = ktc_SetToken(aserver, atoken, aclient, 0))) {
276 printf("%s: unable to obtain tokens for cell %s "
277 "(status: %d).\n", progname, cell_to_use, status);
278 *status = AKLOG_TOKEN;
283 * In case you're wondering, we don't need to change the
284 * filename here because we're still connecting to the
285 * same cell -- we're just using a different authentication
289 if ((*status = pr_Initialize(1L, confname, aserver->cell))) {
290 printf("Error %d\n", status);
294 if ((*status = pr_CreateUser(username, &id))) {
295 printf("%s: unable to create remote PTS "
296 "user %s in cell %s (status: %d).\n", progname,
297 username, cell_to_use, *status);
299 printf("created cross-cell entry for %s at %s\n",
300 username, cell_to_use);
301 #ifdef AFS_ID_TO_NAME
302 strncpy(username_copy, username, BUFSIZ);
303 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
304 #endif /* AFS_ID_TO_NAME */
308 #endif /* ALLOW_REGISTER */
311 char *LastComponent(char *str)
313 char *ret = strrchr(str, DIR);
317 ret = strrchr(str, BDIR);
322 int FirstComponent(char *str)
331 void CopyPathColon(char *origpath, char *path, char *pathtocheck)
334 if (origpath[1] == DRIVECOLON)
336 strncpy(pathtocheck, origpath, 2);
337 strcpy(path, origpath+2);
341 strcpy(path, origpath);
344 int BeginsWithDir(char *str, int colon)
346 return (str[0] == DIR) ||
348 ((str[0] == BDIR) || (colon && str[1] == DRIVECOLON));
355 /* This is a pretty gross hack. Linking against the Transarc
356 * libraries pulls in some rxkad functions which use des. (I don't
357 * think they ever get called.) With Transarc-supplied libraries this
358 * creates a reliance on the symbol des_pcbc_init() which is only in
359 * Transarc's DES libraries (it's an exportability symbol-hiding
360 * thing), which we don't want to use because they don't work with
361 * MIT's krb4 routines. So export a des_pcbc_init() symbol here so we
362 * don't have to link against Transarc's des library.
369 static int get_cred(char *name, char *inst, char *realm, CREDENTIALS *c)
373 status = krb_get_cred(name, inst, realm, c);
374 if (status != KSUCCESS)
376 #ifdef DONT_HAVE_GET_AD_TKT
378 status = krb_mk_req(&ticket, name, inst, realm, 0);
380 status = get_ad_tkt(name, inst, realm, 255);
382 if (status == KSUCCESS)
383 status = krb_get_cred(name, inst, realm, c);
389 static int get_v5cred(krb5_context context,
390 char *name, char *inst, char *realm, CREDENTIALS *c,
395 static krb5_principal client_principal = 0;
397 memset((char *)&increds, 0, sizeof(increds));
399 if ((r = krb5_build_principal(context, &increds.server,
400 strlen(realm), realm,
402 (inst && strlen(inst)) ? inst : 0,
408 krb5_cc_default(context, &_krb425_ccache);
409 if (!client_principal)
410 krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
412 increds.client = client_principal;
413 increds.times.endtime = 0;
414 /* Ask for DES since that is what V4 understands */
415 increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
417 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
421 /* This requires krb524d to be running with the KDC */
423 r = krb5_524_convert_creds(context, *creds, c);
427 /* There is no header for this function. It is supposed to be private */
428 int krb_get_admhst(char *h,char *r, int n);
430 static char *afs_realm_of_cell(struct afsconf_cell *cellconfig)
432 char krbhst[MAX_HSTNM];
433 static char krbrlm[REALM_SZ+1];
438 strcpy(krbrlm, (char *) krb_realmofhost(cellconfig->hostName[0]));
440 if (krb_get_admhst(krbhst, krbrlm, 1) != KSUCCESS)
443 char *t = cellconfig->name;
457 static char *afs_realm_of_cell5(krb5_context context, struct afsconf_cell *cellconfig)
460 static char krbrlm[REALM_SZ+1];
461 krb5_error_code status;
466 status = krb5_get_host_realm( context, cellconfig->hostName[0], &krbrlms );
468 if (status == 0 && krbrlms && krbrlms[0]) {
469 strcpy(krbrlm, krbrlms[0]);
471 strcpy(krbrlm, cellconfig->name);
476 krb5_free_host_realm( context, krbrlms );
481 static char *copy_cellinfo(cellinfo_t *cellinfo)
483 cellinfo_t *new_cellinfo;
485 if (new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t)))
486 memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
488 return ((char *)new_cellinfo);
492 static char *copy_string(char *string)
496 if (new_string = (char *)calloc(strlen(string) + 1, sizeof(char)))
497 (void) strcpy(new_string, string);
503 static int get_cellconfig(char *cell, struct afsconf_cell *cellconfig,
506 int status = AKLOG_SUCCESS;
507 struct afsconf_dir *configdir = 0;
509 memset(local_cell, 0, sizeof(local_cell));
510 memset(cellconfig, 0, sizeof(*cellconfig));
512 if (GetLocalCell(&configdir, local_cell))
514 fprintf(stderr, "%s: can't determine local cell.\n", progname);
518 if ((cell == NULL) || (cell[0] == 0))
521 if (GetCellInfo(&configdir, cell, &cellconfig))
523 fprintf(stderr, "%s: Can't get information about cell %s.\n",
529 CloseConf(&configdir);
534 static int get_v5_user_realm(krb5_context context,char *realm)
536 static krb5_principal client_principal = 0;
540 krb5_cc_default(context, &_krb425_ccache);
541 if (!client_principal)
542 krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
544 i = krb5_princ_realm(context, client_principal)->length;
545 if (i < REALM_SZ-1) i = REALM_SZ-1;
546 strncpy(realm,krb5_princ_realm(context, client_principal)->data,i);
552 * Log to a cell. If the cell has already been logged to, return without
553 * doing anything. Otherwise, log to it and mark that it has been logged
555 static int auth_to_cell(krb5_context context, char *cell, char *realm)
557 int status = AKLOG_SUCCESS;
558 char username[BUFSIZ]; /* To hold client username structure */
560 char name[ANAME_SZ]; /* Name of afs key */
561 char instance[INST_SZ]; /* Instance of afs key */
562 char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
563 char realm_of_cell[REALM_SZ]; /* Kerberos realm of cell */
564 char local_cell[MAXCELLCHARS+1];
565 char cell_to_use[MAXCELLCHARS+1]; /* Cell to authenticate to */
567 krb5_creds *v5cred = NULL;
569 struct ktc_principal aserver;
570 struct ktc_principal aclient;
571 struct ktc_token atoken, btoken;
574 /* try to avoid an expensive call to get_cellconfig */
575 if (cell && ll_string_check(&authedcells, cell))
578 printf("Already authenticated to %s (or tried to)\n", cell);
579 return(AKLOG_SUCCESS);
582 memset(name, 0, sizeof(name));
583 memset(instance, 0, sizeof(instance));
584 memset(realm_of_user, 0, sizeof(realm_of_user));
585 memset(realm_of_cell, 0, sizeof(realm_of_cell));
587 /* NULL or empty cell returns information on local cell */
588 if (status = get_cellconfig(cell, &ak_cellconfig, local_cell))
591 strncpy(cell_to_use, ak_cellconfig.name, MAXCELLCHARS);
592 cell_to_use[MAXCELLCHARS] = 0;
594 if (ll_string_check(&authedcells, cell_to_use))
597 printf("Already authenticated to %s (or tried to)\n", cell_to_use);
598 return(AKLOG_SUCCESS);
602 * Record that we have attempted to log to this cell. We do this
603 * before we try rather than after so that we will not try
604 * and fail repeatedly for one cell.
606 (void)ll_add_string(&authedcells, cell_to_use);
609 printf("Authenticating to cell %s.\n", cell_to_use);
611 if (realm && realm[0])
612 strcpy(realm_of_cell, realm);
614 strcpy(realm_of_cell,
616 afs_realm_of_cell5(context, &ak_cellconfig) :
617 afs_realm_of_cell(&ak_cellconfig));
619 /* We use the afs.<cellname> convention here... */
620 strcpy(name, AFSKEY);
621 strncpy(instance, cell_to_use, sizeof(instance));
622 instance[sizeof(instance)-1] = '\0';
625 * Extract the session key from the ticket file and hand-frob an
626 * afs style authenticator.
633 if ( strchr(name,'.') != NULL ) {
634 fprintf(stderr, "%s: Can't support principal names including a dot.\n",
641 printf("Getting v5 tickets: %s/%s@%s\n", name, instance, realm_of_cell);
642 status = get_v5cred(context, name, instance, realm_of_cell,
643 use524 ? &c : NULL, &v5cred);
644 if (status == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN) {
646 printf("Getting v5 tickets: %s@%s\n", name, realm_of_cell);
647 status = get_v5cred(context, name, "", realm_of_cell,
648 use524 ? &c : NULL, &v5cred);
650 if ( status == KRB5KRB_AP_ERR_MSG_TYPE && retry ) {
658 * Try to obtain AFS tickets. Because there are two valid service
659 * names, we will try both, but trying the more specific first.
665 printf("Getting tickets: %s.%s@%s\n", name, instance, realm_of_cell);
666 status = get_cred(name, instance, realm_of_cell, &c);
667 if (status == KDC_PR_UNKNOWN)
670 printf("Getting tickets: %s@%s\n", name, realm_of_cell);
671 status = get_cred(name, "", realm_of_cell, &c);
675 /* TODO: get k5 error text */
676 if (status != KSUCCESS)
679 printf("Kerberos error code returned by get_cred: %d\n", status);
680 fprintf(stderr, "%s: Couldn't get %s AFS tickets: %s\n",
681 progname, cell_to_use,
683 krb_err_text(status));
684 return(AKLOG_KERBEROS);
687 strncpy(aserver.name, AFSKEY, MAXKTCNAMELEN - 1);
688 strncpy(aserver.instance, AFSINST, MAXKTCNAMELEN - 1);
689 strncpy(aserver.cell, cell_to_use, MAXKTCREALMLEN - 1);
691 if (usev5 && !use524) {
692 /* This code inserts the entire K5 ticket into the token
693 * No need to perform a krb524 translation which is
694 * commented out in the code below
699 len = min(v5cred->client->data[0].length,MAXKTCNAMELEN - 1);
700 strncpy(username, v5cred->client->data[0].data, len);
701 username[len] = '\0';
703 if ( v5cred->client->length > 1 ) {
704 strcat(username, ".");
705 p = username + strlen(username);
706 len = min(v5cred->client->data[1].length,MAXKTCNAMELEN - strlen(username) - 1);
707 strncpy(p, v5cred->client->data[1].data, len);
711 memset(&atoken, '\0', sizeof(atoken));
712 atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
713 atoken.startTime = v5cred->times.starttime;
714 atoken.endTime = v5cred->times.endtime;
715 memcpy(&atoken.sessionKey, v5cred->keyblock.contents, v5cred->keyblock.length);
716 atoken.ticketLen = v5cred->ticket.length;
717 memcpy(atoken.ticket, v5cred->ticket.data, atoken.ticketLen);
719 strcpy (username, c.pname);
722 strcat(username, ".");
723 strcat(username, c.pinst);
726 atoken.kvno = c.kvno;
727 atoken.startTime = c.issue_date;
728 /* ticket lifetime is in five-minutes blocks. */
729 atoken.endTime = c.issue_date + ((unsigned char)c.lifetime * 5 * 60);
731 memcpy(&atoken.sessionKey, c.session, 8);
732 atoken.ticketLen = c.ticket_st.length;
733 memcpy(atoken.ticket, c.ticket_st.dat, atoken.ticketLen);
737 !ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient) &&
738 atoken.kvno == btoken.kvno &&
739 atoken.ticketLen == btoken.ticketLen &&
740 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
741 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen))
744 printf("Identical tokens already exist; skipping.\n");
751 printf("Not resolving name %s to id (-noprdb set)\n", username);
756 if((status = get_v5_user_realm(context, realm_of_user)) != KSUCCESS) {
757 fprintf(stderr, "%s: Couldn't determine realm of user: %d\n",
759 return(AKLOG_KERBEROS);
762 if ((status = krb_get_tf_realm(TKT_FILE, realm_of_user)) != KSUCCESS)
764 fprintf(stderr, "%s: Couldn't determine realm of user: %s)",
765 progname, krb_err_text(status));
766 return(AKLOG_KERBEROS);
770 /* For Khimaira we want to always append the realm to the name */
771 if (1 /* strcmp(realm_of_user, realm_of_cell) */)
773 strcat(username, "@");
774 strcat(username, realm_of_user);
777 ViceIDToUsername(username, realm_of_user, realm_of_cell, cell_to_use, &c, &status, &aclient, &aserver, &atoken);
781 printf("Set username to %s\n", username);
783 /* Reset the "aclient" structure before we call ktc_SetToken.
784 * This structure was first set by the ktc_GetToken call when
785 * we were comparing whether identical tokens already existed.
787 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
788 strcpy(aclient.instance, "");
790 if (usev5 && !use524) {
791 int len = min(v5cred->client->realm.length,MAXKTCNAMELEN - 1);
792 strncpy(aclient.cell, v5cred->client->realm.data, len);
793 aclient.cell[len] = '\0';
795 strncpy(aclient.cell, c.realm, MAXKTCREALMLEN - 1);
798 printf("Getting tokens.\n");
799 if (status = ktc_SetToken(&aserver, &atoken, &aclient, 0))
802 "%s: unable to obtain tokens for cell %s (status: %d).\n",
803 progname, cell_to_use, status);
804 status = AKLOG_TOKEN;
810 static int get_afs_mountpoint(char *file, char *mountpoint, int size)
812 char our_file[MAXPATHLEN + 1];
814 char *last_component;
815 struct ViceIoctl vio;
816 char cellname[BUFSIZ];
818 memset(our_file, 0, sizeof(our_file));
819 strcpy(our_file, file);
821 if (last_component = LastComponent(our_file))
823 *last_component++ = 0;
824 parent_dir = our_file;
828 last_component = our_file;
832 memset(cellname, 0, sizeof(cellname));
834 vio.in = last_component;
835 vio.in_size = strlen(last_component)+1;
837 vio.out = mountpoint;
839 if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0))
841 if (strchr(mountpoint, VOLMARKER) == NULL)
844 vio.in_size = strlen(file) + 1;
845 vio.out_size = sizeof(cellname);
848 if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1))
850 strcat(cellname, VOLMARKERSTRING);
851 strcat(cellname, mountpoint + 1);
852 memset(mountpoint + 1, 0, size - 1);
853 strcpy(mountpoint + 1, cellname);
864 * This routine each time it is called returns the next directory
865 * down a pathname. It resolves all symbolic links. The first time
866 * it is called, it should be called with the name of the path
867 * to be descended. After that, it should be called with the arguemnt
870 static char *next_path(char *origpath)
872 static char path[MAXPATHLEN + 1];
873 static char pathtocheck[MAXPATHLEN + 1];
875 int link = FALSE; /* Is this a symbolic link? */
876 char linkbuf[MAXPATHLEN + 1];
877 char tmpbuf[MAXPATHLEN + 1];
879 static char *last_comp; /* last component of directory name */
880 static char *elast_comp; /* End of last component */
884 static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
886 /* If we are given something for origpath, we are initializing only. */
889 memset(path, 0, sizeof(path));
890 memset(pathtocheck, 0, sizeof(pathtocheck));
891 CopyPathColon(origpath, path, pathtocheck);
897 /* We were not given origpath; find then next path to check */
899 /* If we've gotten all the way through already, return NULL */
900 if (last_comp == NULL)
905 while (BeginsWithDir(last_comp, FALSE))
906 strncat(pathtocheck, last_comp++, 1);
907 len = (elast_comp = LastComponent(last_comp))
908 ? elast_comp - last_comp : strlen(last_comp);
909 strncat(pathtocheck, last_comp, len);
910 memset(linkbuf, 0, sizeof(linkbuf));
911 if (link = (readlink(pathtocheck, linkbuf, sizeof(linkbuf)) > 0))
913 if (++symlinkcount > MAXSYMLINKS)
915 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
918 memset(tmpbuf, 0, sizeof(tmpbuf));
920 strcpy(tmpbuf, elast_comp);
921 if (BeginsWithDir(linkbuf, FALSE))
924 * If this is a symbolic link to an absolute path,
925 * replace what we have by the absolute path.
927 memset(path, 0, strlen(path));
928 memcpy(path, linkbuf, sizeof(linkbuf));
929 strcat(path, tmpbuf);
932 memset(pathtocheck, 0, sizeof(pathtocheck));
937 * If this is a symbolic link to a relative path,
938 * replace only the last component with the link name.
940 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
941 strcat(path, tmpbuf);
943 if (t = LastComponent(pathtocheck))
946 memset(t, 0, strlen(t));
949 memset(pathtocheck, 0, sizeof(pathtocheck));
953 last_comp = elast_comp;
961 * This routine descends through a path to a directory, logging to
962 * every cell it encounters along the way.
964 static int auth_to_path(krb5_context context, char *path)
966 int status = AKLOG_SUCCESS;
967 int auth_to_cell_status = AKLOG_SUCCESS;
970 char pathtocheck[MAXPATHLEN + 1];
971 char mountpoint[MAXPATHLEN + 1];
977 if (BeginsWithDir(path, TRUE))
978 strcpy(pathtocheck, path);
981 if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL)
983 fprintf(stderr, "Unable to find current working directory:\n");
984 fprintf(stderr, "%s\n", pathtocheck);
985 fprintf(stderr, "Try an absolute pathname.\n");
990 /* in WIN32, if getcwd returns a root dir (eg: c:\), the returned string
991 * will already have a trailing slash ('\'). Otherwise, the string will
992 * end in the last directory name */
994 if(pathtocheck[strlen(pathtocheck) - 1] != BDIR)
996 strcat(pathtocheck, DIRSTRING);
997 strcat(pathtocheck, path);
1000 next_path(pathtocheck);
1002 /* Go on to the next level down the path */
1003 while (nextpath = next_path(NULL))
1005 strcpy(pathtocheck, nextpath);
1007 printf("Checking directory [%s]\n", pathtocheck);
1009 * If this is an afs mountpoint, determine what cell from
1010 * the mountpoint name which is of the form
1011 * #cellname:volumename or %cellname:volumename.
1013 if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint)))
1016 printf("Found mount point [%s]\n", mountpoint);
1017 /* skip over the '#' or '%' */
1018 cell = mountpoint + 1;
1019 if (endofcell = strchr(mountpoint, VOLMARKER))
1022 if (auth_to_cell_status = auth_to_cell(context, cell, NULL))
1024 if (status == AKLOG_SUCCESS)
1025 status = auth_to_cell_status;
1026 else if (status != auth_to_cell_status)
1027 status = AKLOG_SOMETHINGSWRONG;
1035 if (lstat(pathtocheck, &st) < 0)
1038 * If we've logged and still can't stat, there's
1041 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1042 pathtocheck, strerror(errno));
1043 return(AKLOG_BADPATH);
1045 else if (!S_ISDIR(st.st_mode))
1047 /* Allow only directories */
1048 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1050 return(AKLOG_BADPATH);
1058 /* Print usage message and exit */
1059 static void usage(void)
1061 fprintf(stderr, "\nUsage: %s %s%s%s%s\n", progname,
1062 "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1063 "[[-p | -path] pathname]\n",
1064 " [-noprdb] [-force]\n",
1067 fprintf(stderr, " -d gives debugging information.\n");
1068 fprintf(stderr, " krb_realm is the kerberos realm of a cell.\n");
1069 fprintf(stderr, " pathname is the name of a directory to which ");
1070 fprintf(stderr, "you wish to authenticate.\n");
1071 fprintf(stderr, " -noprdb means don't try to determine AFS ID.\n");
1072 fprintf(stderr, " -5 or -4 selects whether to use Kerberos V or Kerberos IV.\n"
1073 " (default is Kerberos V)\n");
1074 fprintf(stderr, " -m means use krb524d to convert Kerberos V tickets.\n");
1075 fprintf(stderr, " No commandline arguments means ");
1076 fprintf(stderr, "authenticate to the local cell.\n");
1077 fprintf(stderr, "\n");
1081 int main(int argc, char *argv[])
1083 int status = AKLOG_SUCCESS;
1085 int somethingswrong = FALSE;
1087 cellinfo_t cellinfo;
1089 extern char *progname; /* Name of this program */
1091 extern int dflag; /* Debug mode */
1093 int cmode = FALSE; /* Cellname mode */
1094 int pmode = FALSE; /* Path name mode */
1096 char realm[REALM_SZ]; /* Kerberos realm of afs server */
1097 char cell[BUFSIZ]; /* Cell to which we are authenticating */
1098 char path[MAXPATHLEN + 1]; /* Path length for path mode */
1100 linked_list cells; /* List of cells to log to */
1101 linked_list paths; /* List of paths to log to */
1104 krb5_context context = 0;
1106 memset(&cellinfo, 0, sizeof(cellinfo));
1108 memset(realm, 0, sizeof(realm));
1109 memset(cell, 0, sizeof(cell));
1110 memset(path, 0, sizeof(path));
1115 /* Store the program name here for error messages */
1116 if (progname = LastComponent(argv[0]))
1121 /* Initialize list of cells to which we have authenticated */
1122 (void)ll_init(&authedcells);
1124 /* Parse commandline arguments and make list of what to do. */
1125 for (i = 1; i < argc; i++)
1127 if (strcmp(argv[i], "-d") == 0)
1129 else if (strcmp(argv[i], "-5") == 0)
1131 else if (strcmp(argv[i], "-m") == 0)
1133 else if (strcmp(argv[i], "-4") == 0)
1135 else if (strcmp(argv[i], "-noprdb") == 0)
1137 else if (strcmp(argv[i], "-force") == 0)
1139 else if (((strcmp(argv[i], "-cell") == 0) ||
1140 (strcmp(argv[i], "-c") == 0)) && !pmode)
1145 strcpy(cell, argv[i]);
1150 else if (((strcmp(argv[i], "-path") == 0) ||
1151 (strcmp(argv[i], "-p") == 0)) && !cmode)
1156 strcpy(path, argv[i]);
1161 else if (argv[i][0] == '-')
1163 else if (!pmode && !cmode)
1165 if (FirstComponent(argv[i]) || (strcmp(argv[i], ".") == 0) ||
1166 (strcmp(argv[i], "..") == 0))
1169 strcpy(path, argv[i]);
1174 strcpy(cell, argv[i]);
1182 if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0))
1186 strcpy(realm, argv[i]);
1190 /* Add this cell to list of cells */
1191 strcpy(cellinfo.cell, cell);
1192 strcpy(cellinfo.realm, realm);
1193 if (cur_node = ll_add_node(&cells, ll_tail))
1196 if (new_cellinfo = copy_cellinfo(&cellinfo))
1197 ll_add_data(cur_node, new_cellinfo);
1200 fprintf(stderr, "%s: failure copying cellinfo.\n", progname);
1206 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1210 memset(&cellinfo, 0, sizeof(cellinfo));
1212 memset(cell, 0, sizeof(cell));
1213 memset(realm, 0, sizeof(realm));
1217 /* Add this path to list of paths */
1218 if (cur_node = ll_add_node(&paths, ll_tail))
1221 if (new_path = copy_string(path))
1222 ll_add_data(cur_node, new_path);
1225 fprintf(stderr, "%s: failure copying path name.\n",
1232 fprintf(stderr, "%s: failure adding path to paths list.\n",
1237 memset(path, 0, sizeof(path));
1242 krb5_init_context(&context);
1244 /* If nothing was given, log to the local cell. */
1245 if ((cells.nelements + paths.nelements) == 0)
1246 status = auth_to_cell(context, NULL, NULL);
1249 /* Log to all cells in the cells list first */
1250 for (cur_node = cells.first; cur_node; cur_node = cur_node->next)
1252 memcpy(&cellinfo, cur_node->data, sizeof(cellinfo));
1253 if (status = auth_to_cell(context,
1254 cellinfo.cell, cellinfo.realm))
1258 /* Then, log to all paths in the paths list */
1259 for (cur_node = paths.first; cur_node; cur_node = cur_node->next)
1261 if (status = auth_to_path(context,
1267 * If only one thing was logged to, we'll return the status
1268 * of the single call. Otherwise, we'll return a generic
1269 * something failed status.
1271 if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1272 status = AKLOG_SOMETHINGSWRONG;
1276 krb5_free_context(context);