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.
17 * Copyright (c) 2007 Secure Endpoints Inc.
19 * All rights reserved.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
25 * * Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * * Neither the name of the Secure Endpoints Inc. nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
37 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 #include <sys/types.h>
64 #define CREDENTIALS void
67 #include <afs/ptserver.h>
68 #include <afs/ptuser.h>
73 #include <cm_config.h>
75 #include <cellconfig.h>
76 #include <pioctl_nt.h>
77 #include <smb_iocons.h>
81 #define __S_ISTYPE(mode, mask) (((mode) & _S_IFMT) == (mask))
82 #define S_ISDIR(mode) __S_ISTYPE((mode), _S_IFDIR)
84 #define DONT_HAVE_GET_AD_TKT
85 #define MAXSYMLINKS 255
88 /* Win32 uses get_krb_err_txt_entry(status) instead of krb_err_txt[status],
89 * so we use a bit of indirection like the GNU CVS sources.
91 #define krb_err_text(status) get_krb_err_txt_entry(status)
94 #define DRIVECOLON ':' /* Drive letter separator */
95 #define BDIR '\\' /* Other character that divides directories */
98 readlink(char *path, char *buf, int buffers)
103 char * getcwd(char*, size_t);
106 get_cellconfig_callback(void *cellconfig, struct sockaddr_in *addrp, char *namep)
108 struct afsconf_cell *cc = (struct afsconf_cell *) cellconfig;
110 cc->hostAddr[cc->numServers] = *addrp;
111 strcpy(cc->hostName[cc->numServers], namep);
117 #include <sys/param.h>
119 #include <arpa/inet.h>
120 #include <sys/socket.h>
123 #include <afs/param.h>
124 #include <afs/auth.h>
125 #include <afs/cellconfig.h>
126 #include <afs/vice.h>
127 #include <afs/venus.h>
128 #include <afs/ptserver.h>
130 #define krb_err_text(status) krb_err_txt[status]
132 /* Cheesy test for determining AFS 3.5. */
133 #ifndef AFSCONF_CLIENTNAME
138 #include <afs/dirpath.h>
140 #define AFSDIR_CLIENT_ETC_DIRPATH AFSCONF_CLIENTNAME
145 #include "linked_list.h"
150 #define AKLOG_SUCCESS 0
151 #define AKLOG_USAGE 1
152 #define AKLOG_SOMETHINGSWRONG 2
154 #define AKLOG_KERBEROS 4
155 #define AKLOG_TOKEN 5
156 #define AKLOG_BADPATH 6
158 #define AKLOG_KFW_NOT_INSTALLED 8
173 #define MAXSYMLINKS 15
176 #define DIR '/' /* Character that divides directories */
177 #define DIRSTRING "/" /* String form of above */
178 #define VOLMARKER ':' /* Character separating cellname from mntpt */
179 #define VOLMARKERSTRING ":" /* String form of above */
183 char realm[REALM_SZ];
187 struct afsconf_cell ak_cellconfig; /* General information about the cell */
189 static char *progname = NULL; /* Name of this program */
190 static int dflag = FALSE; /* Give debugging information */
191 static int noprdb = FALSE; /* Skip resolving name to id? */
192 static int force = FALSE; /* Bash identical tokens? */
193 static linked_list authedcells; /* List of cells already logged to */
195 static int usev5 = TRUE; /* use kerberos 5? */
196 static int use524 = FALSE; /* use krb524? */
197 static krb5_ccache _krb425_ccache;
199 long GetLocalCell(struct afsconf_dir **pconfigdir, char *local_cell)
201 if (!(*pconfigdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)))
203 fprintf(stderr, "%s: can't get afs configuration (afsconf_Open(%s))\n",
204 progname, AFSDIR_CLIENT_ETC_DIRPATH);
208 return afsconf_GetLocalCell(*pconfigdir, local_cell, MAXCELLCHARS);
211 long GetCellInfo(struct afsconf_dir **pconfigdir, char* cell,
212 struct afsconf_cell **pcellconfig)
214 return afsconf_GetCellInfo(*pconfigdir, cell, NULL, *pcellconfig);
217 void CloseConf(struct afsconf_dir **pconfigdir)
219 (void) afsconf_Close(*pconfigdir);
222 #define ALLOW_REGISTER 1
223 void ViceIDToUsername(char *username, char *realm_of_user, char *realm_of_cell,
224 char * cell_to_use, CREDENTIALS *c,
226 struct ktc_principal *aclient, struct ktc_principal *aserver, struct ktc_token *atoken)
228 static char lastcell[MAXCELLCHARS+1] = { 0 };
229 static char confname[512] = { 0 };
230 char username_copy[BUFSIZ];
231 afs_int32 viceId; /* AFS uid of user */
232 #ifdef ALLOW_REGISTER
234 #endif /* ALLOW_REGISTER */
236 if (confname[0] == '\0') {
237 strncpy(confname, AFSDIR_CLIENT_ETC_DIRPATH, sizeof(confname));
238 confname[sizeof(confname) - 2] = '\0';
242 printf("About to resolve name %s to id\n", username);
244 strcpy(lastcell, aserver->cell);
246 if (!pr_Initialize (0, confname, aserver->cell)) {
247 char sname[PR_MAXNAMELEN], *at;
249 strncpy(sname, username, PR_MAXNAMELEN);
250 sname[PR_MAXNAMELEN-1] = '\0';
252 at = strchr(sname, '@');
253 if (at && !stricmp(at+1, realm_of_cell))
255 *status = pr_SNameToId (sname, &viceId);
261 printf("Error %d\n", *status);
263 printf("Id %d\n", viceId);
267 * This code is taken from cklog -- it lets people
268 * automatically register with the ptserver in foreign cells
271 #ifdef ALLOW_REGISTER
273 if (viceId != ANONYMOUSID) {
274 #else /* ALLOW_REGISTER */
275 if ((*status == 0) && (viceId != ANONYMOUSID))
276 #endif /* ALLOW_REGISTER */
278 #ifdef AFS_ID_TO_NAME
279 strncpy(username_copy, username, BUFSIZ);
280 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
281 #endif /* AFS_ID_TO_NAME */
283 #ifdef ALLOW_REGISTER
284 } else if (strcmp(realm_of_user, realm_of_cell) != 0) {
287 printf("doing first-time registration of %s "
288 "at %s\n", username, cell_to_use);
291 strncpy(aclient->name, username, MAXKTCNAMELEN - 1);
292 aclient->name[MAXKTCNAMELEN - 1] = '\0';
293 strcpy(aclient->instance, "");
294 strncpy(aclient->cell, cell_to_use, MAXKTCREALMLEN - 1);
295 aclient->cell[MAXKTCREALMLEN - 1] = '\0';
297 for ( i=0; aclient->cell[i]; i++ ) {
298 if ( islower(aclient->cell[i]) )
299 aclient->cell[i] = toupper(aclient->cell[i]);
302 if ((*status = ktc_SetToken(aserver, atoken, aclient, 0))) {
303 printf("%s: unable to set tokens for cell %s "
304 "(status: %d).\n", progname, cell_to_use, *status);
305 *status = AKLOG_TOKEN;
310 * In case you're wondering, we don't need to change the
311 * filename here because we're still connecting to the
312 * same cell -- we're just using a different authentication
316 if ((*status = pr_Initialize(1L, confname, aserver->cell))) {
317 printf("Error %d\n", *status);
321 /* copy the name because pr_CreateUser lowercases the realm */
322 strncpy(username_copy, username, BUFSIZ);
324 *status = pr_CreateUser(username, &id);
326 /* and restore the name to the original state */
327 strncpy(username, username_copy, BUFSIZ);
330 printf("%s: unable to create remote PTS "
331 "user %s in cell %s (status: %d).\n", progname,
332 username, cell_to_use, *status);
334 printf("created cross-cell entry for %s (Id %d) at %s\n",
335 username, viceId, cell_to_use);
336 #ifdef AFS_ID_TO_NAME
337 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
338 #endif /* AFS_ID_TO_NAME */
342 #endif /* ALLOW_REGISTER */
345 char *LastComponent(char *str)
347 char *ret = strrchr(str, DIR);
351 ret = strrchr(str, BDIR);
356 int FirstComponent(char *str)
365 void CopyPathColon(char *origpath, char *path, char *pathtocheck)
368 if (origpath[1] == DRIVECOLON)
370 strncpy(pathtocheck, origpath, 2);
371 strcpy(path, origpath+2);
375 strcpy(path, origpath);
378 int BeginsWithDir(char *str, int colon)
380 return (str[0] == DIR) ||
382 ((str[0] == BDIR) || (colon && str[1] == DRIVECOLON));
389 /* This is a pretty gross hack. Linking against the Transarc
390 * libraries pulls in some rxkad functions which use des. (I don't
391 * think they ever get called.) With Transarc-supplied libraries this
392 * creates a reliance on the symbol des_pcbc_init() which is only in
393 * Transarc's DES libraries (it's an exportability symbol-hiding
394 * thing), which we don't want to use because they don't work with
395 * MIT's krb4 routines. So export a des_pcbc_init() symbol here so we
396 * don't have to link against Transarc's des library.
404 static int get_cred(char *name, char *inst, char *realm, CREDENTIALS *c)
408 status = krb_get_cred(name, inst, realm, c);
409 if (status != KSUCCESS)
411 #ifdef DONT_HAVE_GET_AD_TKT
413 status = krb_mk_req(&ticket, name, inst, realm, 0);
415 status = get_ad_tkt(name, inst, realm, 255);
417 if (status == KSUCCESS)
418 status = krb_get_cred(name, inst, realm, c);
425 static int get_v5cred(krb5_context context,
426 char *name, char *inst, char *realm, CREDENTIALS *c,
431 static krb5_principal client_principal = 0;
433 memset((char *)&increds, 0, sizeof(increds));
435 if ((r = krb5_build_principal(context, &increds.server,
436 (int)strlen(realm), realm,
438 (inst && strlen(inst)) ? inst : 0,
444 krb5_cc_default(context, &_krb425_ccache);
445 if (!client_principal)
446 krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
448 increds.client = client_principal;
449 increds.times.endtime = 0;
450 /* Ask for DES since that is what V4 understands */
451 increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
453 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
457 /* This requires krb524d to be running with the KDC */
459 r = krb5_524_convert_creds(context, *creds, c);
464 /* There is no header for this function. It is supposed to be private */
465 int krb_get_admhst(char *h,char *r, int n);
467 static char *afs_realm_of_cell(struct afsconf_cell *cellconfig)
469 char krbhst[MAX_HSTNM];
470 static char krbrlm[REALM_SZ+1];
475 strcpy(krbrlm, (char *) krb_realmofhost(cellconfig->hostName[0]));
477 if (krb_get_admhst(krbhst, krbrlm, 1) != KSUCCESS)
480 char *t = cellconfig->name;
495 /* As of MIT Kerberos 1.6, krb5_get_host_realm() will return the NUL-string
496 * if there is no domain_realm mapping for the hostname's domain. This is
497 * used as a trigger indicating that referrals should be used within the
498 * krb5_get_credentials() call. However, if the KDC does not support referrals
499 * that will result in a KRB5_ERR_HOST_REALM_UNKNOWN error and we will have
500 * to manually fallback to mapping the domain of the host as a realm name.
501 * Hence, the new fallback parameter.
503 static char *afs_realm_of_cell5(krb5_context context, struct afsconf_cell *cellconfig, int fallback)
506 static char krbrlm[REALM_SZ+1];
507 krb5_error_code status;
514 p = strchr(cellconfig->hostName[0], '.');
518 strcpy(krbrlm, cellconfig->name);
521 status = krb5_get_host_realm( context, cellconfig->hostName[0], &krbrlms );
522 if (status == 0 && krbrlms && krbrlms[0]) {
523 strcpy(krbrlm, krbrlms[0]);
525 strcpy(krbrlm, cellconfig->name);
530 krb5_free_host_realm( context, krbrlms );
535 static char *copy_cellinfo(cellinfo_t *cellinfo)
537 cellinfo_t *new_cellinfo;
539 if (new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t)))
540 memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
542 return ((char *)new_cellinfo);
546 static char *copy_string(char *string)
550 if (new_string = (char *)calloc(strlen(string) + 1, sizeof(char)))
551 (void) strcpy(new_string, string);
557 static int get_cellconfig(char *cell, struct afsconf_cell *cellconfig,
560 int status = AKLOG_SUCCESS;
561 struct afsconf_dir *configdir = 0;
563 memset(local_cell, 0, sizeof(local_cell));
564 memset(cellconfig, 0, sizeof(*cellconfig));
566 if (GetLocalCell(&configdir, local_cell))
568 fprintf(stderr, "%s: can't determine local cell.\n", progname);
572 if ((cell == NULL) || (cell[0] == 0))
575 if (GetCellInfo(&configdir, cell, &cellconfig))
577 fprintf(stderr, "%s: Can't get information about cell %s.\n",
583 CloseConf(&configdir);
588 static int get_v5_user_realm(krb5_context context,char *realm)
590 static krb5_principal client_principal = 0;
594 krb5_cc_default(context, &_krb425_ccache);
595 if (!client_principal)
596 krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
598 i = krb5_princ_realm(context, client_principal)->length;
599 if (i < REALM_SZ-1) i = REALM_SZ-1;
600 strncpy(realm,krb5_princ_realm(context, client_principal)->data,i);
606 * Log to a cell. If the cell has already been logged to, return without
607 * doing anything. Otherwise, log to it and mark that it has been logged
609 static int auth_to_cell(krb5_context context, char *cell, char *realm)
611 int status = AKLOG_SUCCESS;
612 char username[BUFSIZ]; /* To hold client username structure */
614 char name[ANAME_SZ]; /* Name of afs key */
615 char instance[INST_SZ]; /* Instance of afs key */
616 char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
617 char realm_of_cell[REALM_SZ]; /* Kerberos realm of cell */
618 char local_cell[MAXCELLCHARS+1];
619 char cell_to_use[MAXCELLCHARS+1]; /* Cell to authenticate to */
621 krb5_creds *v5cred = NULL;
625 struct ktc_principal aserver;
626 struct ktc_principal aclient;
627 struct ktc_token atoken, btoken;
630 /* try to avoid an expensive call to get_cellconfig */
631 if (cell && ll_string_check(&authedcells, cell))
634 printf("Already authenticated to %s (or tried to)\n", cell);
635 return(AKLOG_SUCCESS);
638 memset(name, 0, sizeof(name));
639 memset(instance, 0, sizeof(instance));
640 memset(realm_of_user, 0, sizeof(realm_of_user));
641 memset(realm_of_cell, 0, sizeof(realm_of_cell));
643 /* NULL or empty cell returns information on local cell */
644 if (status = get_cellconfig(cell, &ak_cellconfig, local_cell))
647 strncpy(cell_to_use, ak_cellconfig.name, MAXCELLCHARS);
648 cell_to_use[MAXCELLCHARS] = 0;
650 if (ll_string_check(&authedcells, cell_to_use))
653 printf("Already authenticated to %s (or tried to)\n", cell_to_use);
654 return(AKLOG_SUCCESS);
658 * Record that we have attempted to log to this cell. We do this
659 * before we try rather than after so that we will not try
660 * and fail repeatedly for one cell.
662 (void)ll_add_string(&authedcells, cell_to_use);
665 printf("Authenticating to cell %s.\n", cell_to_use);
667 /* We use the afs.<cellname> convention here... */
668 strcpy(name, AFSKEY);
669 strncpy(instance, cell_to_use, sizeof(instance));
670 instance[sizeof(instance)-1] = '\0';
673 * Extract the session key from the ticket file and hand-frob an
674 * afs style authenticator.
680 int realm_fallback = 0;
682 if ( strchr(name,'.') != NULL ) {
683 fprintf(stderr, "%s: Can't support principal names including a dot.\n",
689 if (realm && realm[0])
690 strcpy(realm_of_cell, realm);
692 strcpy(realm_of_cell,
693 afs_realm_of_cell5(context, &ak_cellconfig, realm_fallback));
696 printf("Getting v5 tickets: %s/%s@%s\n", name, instance, realm_of_cell);
697 status = get_v5cred(context, name, instance, realm_of_cell,
704 if (status == KRB5_ERR_HOST_REALM_UNKNOWN) {
707 } else if (status == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN) {
708 if (!realm_of_cell[0]) {
713 printf("Getting v5 tickets: %s@%s\n", name, realm_of_cell);
714 status = get_v5cred(context, name, "", realm_of_cell,
722 if ( status == KRB5KRB_AP_ERR_MSG_TYPE && retry ) {
731 if (realm && realm[0])
732 strcpy(realm_of_cell, realm);
734 strcpy(realm_of_cell, afs_realm_of_cell(&ak_cellconfig));
737 * Try to obtain AFS tickets. Because there are two valid service
738 * names, we will try both, but trying the more specific first.
744 printf("Getting tickets: %s.%s@%s\n", name, instance, realm_of_cell);
745 status = get_cred(name, instance, realm_of_cell, &c);
746 if (status == KDC_PR_UNKNOWN)
749 printf("Getting tickets: %s@%s\n", name, realm_of_cell);
750 status = get_cred(name, "", realm_of_cell, &c);
757 /* TODO: get k5 error text */
758 if (status != KSUCCESS)
761 printf("Kerberos error code returned by get_cred: %d\n", status);
762 fprintf(stderr, "%s: Couldn't get %s AFS tickets: %s\n",
763 progname, cell_to_use,
765 (usev5)?"":krb_err_text(status)
770 return(AKLOG_KERBEROS);
773 strncpy(aserver.name, AFSKEY, MAXKTCNAMELEN - 1);
774 strncpy(aserver.instance, AFSINST, MAXKTCNAMELEN - 1);
775 strncpy(aserver.cell, cell_to_use, MAXKTCREALMLEN - 1);
777 if (usev5 && !use524) {
778 /* This code inserts the entire K5 ticket into the token
779 * No need to perform a krb524 translation which is
780 * commented out in the code below
785 len = min(v5cred->client->data[0].length,MAXKTCNAMELEN - 1);
786 strncpy(username, v5cred->client->data[0].data, len);
787 username[len] = '\0';
789 if ( v5cred->client->length > 1 ) {
790 strcat(username, ".");
791 p = username + strlen(username);
792 len = min(v5cred->client->data[1].length,MAXKTCNAMELEN - strlen(username) - 1);
793 strncpy(p, v5cred->client->data[1].data, len);
797 memset(&atoken, '\0', sizeof(atoken));
798 atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
799 atoken.startTime = v5cred->times.starttime;
800 atoken.endTime = v5cred->times.endtime;
801 memcpy(&atoken.sessionKey, v5cred->keyblock.contents, v5cred->keyblock.length);
802 atoken.ticketLen = v5cred->ticket.length;
803 memcpy(atoken.ticket, v5cred->ticket.data, atoken.ticketLen);
806 strcpy (username, c.pname);
809 strcat(username, ".");
810 strcat(username, c.pinst);
813 atoken.kvno = c.kvno;
814 atoken.startTime = c.issue_date;
815 /* ticket lifetime is in five-minutes blocks. */
816 atoken.endTime = c.issue_date + ((unsigned char)c.lifetime * 5 * 60);
818 memcpy(&atoken.sessionKey, c.session, 8);
819 atoken.ticketLen = c.ticket_st.length;
820 memcpy(atoken.ticket, c.ticket_st.dat, atoken.ticketLen);
827 !ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient) &&
828 atoken.kvno == btoken.kvno &&
829 atoken.ticketLen == btoken.ticketLen &&
830 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
831 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen))
834 printf("Identical tokens already exist; skipping.\n");
841 printf("Not resolving name %s to id (-noprdb set)\n", username);
846 if((status = get_v5_user_realm(context, realm_of_user)) != KSUCCESS) {
847 fprintf(stderr, "%s: Couldn't determine realm of user: %d\n",
849 return(AKLOG_KERBEROS);
853 if ((status = krb_get_tf_realm(TKT_FILE, realm_of_user)) != KSUCCESS)
855 fprintf(stderr, "%s: Couldn't determine realm of user: %s)",
856 progname, krb_err_text(status));
857 return(AKLOG_KERBEROS);
864 /* For Khimaira we want to always append the realm to the name */
865 if (1 /* strcmp(realm_of_user, realm_of_cell) */)
867 strcat(username, "@");
868 strcat(username, realm_of_user);
871 ViceIDToUsername(username, realm_of_user, realm_of_cell, cell_to_use,
877 &status, &aclient, &aserver, &atoken);
881 printf("Set username to %s\n", username);
883 /* Reset the "aclient" structure before we call ktc_SetToken.
884 * This structure was first set by the ktc_GetToken call when
885 * we were comparing whether identical tokens already existed.
887 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
888 strcpy(aclient.instance, "");
890 if (usev5 && !use524) {
891 int len = min(v5cred->client->realm.length,MAXKTCNAMELEN - 1);
892 strncpy(aclient.cell, v5cred->client->realm.data, len);
893 aclient.cell[len] = '\0';
897 strncpy(aclient.cell, c.realm, MAXKTCREALMLEN - 1);
900 for ( i=0; aclient.cell[i]; i++ ) {
901 if ( islower(aclient.cell[i]) )
902 aclient.cell[i] = toupper(aclient.cell[i]);
906 printf("Getting tokens.\n");
907 if (status = ktc_SetToken(&aserver, &atoken, &aclient, 0))
910 "%s: unable to obtain tokens for cell %s (status: %d).\n",
911 progname, cell_to_use, status);
912 status = AKLOG_TOKEN;
918 static int get_afs_mountpoint(char *file, char *mountpoint, int size)
920 char our_file[MAXPATHLEN + 1];
922 char *last_component;
923 struct ViceIoctl vio;
924 char cellname[BUFSIZ];
926 memset(our_file, 0, sizeof(our_file));
927 strcpy(our_file, file);
929 if (last_component = LastComponent(our_file))
931 *last_component++ = 0;
932 parent_dir = our_file;
936 last_component = our_file;
940 memset(cellname, 0, sizeof(cellname));
942 vio.in = last_component;
943 vio.in_size = strlen(last_component)+1;
945 vio.out = mountpoint;
947 if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0))
949 if (strchr(mountpoint, VOLMARKER) == NULL)
952 vio.in_size = strlen(file) + 1;
953 vio.out_size = sizeof(cellname);
956 if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1))
958 strcat(cellname, VOLMARKERSTRING);
959 strcat(cellname, mountpoint + 1);
960 memset(mountpoint + 1, 0, size - 1);
961 strcpy(mountpoint + 1, cellname);
972 * This routine each time it is called returns the next directory
973 * down a pathname. It resolves all symbolic links. The first time
974 * it is called, it should be called with the name of the path
975 * to be descended. After that, it should be called with the arguemnt
978 static char *next_path(char *origpath)
980 static char path[MAXPATHLEN + 1];
981 static char pathtocheck[MAXPATHLEN + 1];
983 int link = FALSE; /* Is this a symbolic link? */
984 char linkbuf[MAXPATHLEN + 1];
985 char tmpbuf[MAXPATHLEN + 1];
987 static char *last_comp; /* last component of directory name */
988 static char *elast_comp; /* End of last component */
992 static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
994 /* If we are given something for origpath, we are initializing only. */
997 memset(path, 0, sizeof(path));
998 memset(pathtocheck, 0, sizeof(pathtocheck));
999 CopyPathColon(origpath, path, pathtocheck);
1005 /* We were not given origpath; find then next path to check */
1007 /* If we've gotten all the way through already, return NULL */
1008 if (last_comp == NULL)
1013 while (BeginsWithDir(last_comp, FALSE))
1014 strncat(pathtocheck, last_comp++, 1);
1015 len = (elast_comp = LastComponent(last_comp))
1016 ? elast_comp - last_comp : strlen(last_comp);
1017 strncat(pathtocheck, last_comp, len);
1018 memset(linkbuf, 0, sizeof(linkbuf));
1019 if (link = (readlink(pathtocheck, linkbuf, sizeof(linkbuf)) > 0))
1021 if (++symlinkcount > MAXSYMLINKS)
1023 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
1024 exit(AKLOG_BADPATH);
1026 memset(tmpbuf, 0, sizeof(tmpbuf));
1028 strcpy(tmpbuf, elast_comp);
1029 if (BeginsWithDir(linkbuf, FALSE))
1032 * If this is a symbolic link to an absolute path,
1033 * replace what we have by the absolute path.
1035 memset(path, 0, strlen(path));
1036 memcpy(path, linkbuf, sizeof(linkbuf));
1037 strcat(path, tmpbuf);
1040 memset(pathtocheck, 0, sizeof(pathtocheck));
1045 * If this is a symbolic link to a relative path,
1046 * replace only the last component with the link name.
1048 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
1049 strcat(path, tmpbuf);
1051 if (t = LastComponent(pathtocheck))
1054 memset(t, 0, strlen(t));
1057 memset(pathtocheck, 0, sizeof(pathtocheck));
1061 last_comp = elast_comp;
1065 return(pathtocheck);
1069 * This routine descends through a path to a directory, logging to
1070 * every cell it encounters along the way.
1072 static int auth_to_path(krb5_context context, char *path)
1074 int status = AKLOG_SUCCESS;
1075 int auth_to_cell_status = AKLOG_SUCCESS;
1078 char pathtocheck[MAXPATHLEN + 1];
1079 char mountpoint[MAXPATHLEN + 1];
1085 if (BeginsWithDir(path, TRUE))
1086 strcpy(pathtocheck, path);
1089 if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL)
1091 fprintf(stderr, "Unable to find current working directory:\n");
1092 fprintf(stderr, "%s\n", pathtocheck);
1093 fprintf(stderr, "Try an absolute pathname.\n");
1094 exit(AKLOG_BADPATH);
1098 /* in WIN32, if getcwd returns a root dir (eg: c:\), the returned string
1099 * will already have a trailing slash ('\'). Otherwise, the string will
1100 * end in the last directory name */
1102 if(pathtocheck[strlen(pathtocheck) - 1] != BDIR)
1104 strcat(pathtocheck, DIRSTRING);
1105 strcat(pathtocheck, path);
1108 next_path(pathtocheck);
1110 /* Go on to the next level down the path */
1111 while (nextpath = next_path(NULL))
1113 strcpy(pathtocheck, nextpath);
1115 printf("Checking directory [%s]\n", pathtocheck);
1117 * If this is an afs mountpoint, determine what cell from
1118 * the mountpoint name which is of the form
1119 * #cellname:volumename or %cellname:volumename.
1121 if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint)))
1124 printf("Found mount point [%s]\n", mountpoint);
1125 /* skip over the '#' or '%' */
1126 cell = mountpoint + 1;
1127 if (endofcell = strchr(mountpoint, VOLMARKER))
1130 if (auth_to_cell_status = auth_to_cell(context, cell, NULL))
1132 if (status == AKLOG_SUCCESS)
1133 status = auth_to_cell_status;
1134 else if (status != auth_to_cell_status)
1135 status = AKLOG_SOMETHINGSWRONG;
1143 if (lstat(pathtocheck, &st) < 0)
1146 * If we've logged and still can't stat, there's
1149 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1150 pathtocheck, strerror(errno));
1151 return(AKLOG_BADPATH);
1153 else if (!S_ISDIR(st.st_mode))
1155 /* Allow only directories */
1156 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1158 return(AKLOG_BADPATH);
1166 /* Print usage message and exit */
1167 static void usage(void)
1169 fprintf(stderr, "\nUsage: %s %s%s%s%s\n", progname,
1170 "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1171 "[[-p | -path] pathname]\n",
1172 " [-noprdb] [-force]\n",
1179 fprintf(stderr, " -d gives debugging information.\n");
1180 fprintf(stderr, " krb_realm is the kerberos realm of a cell.\n");
1181 fprintf(stderr, " pathname is the name of a directory to which ");
1182 fprintf(stderr, "you wish to authenticate.\n");
1183 fprintf(stderr, " -noprdb means don't try to determine AFS ID.\n");
1185 fprintf(stderr, " -5 or -4 selects whether to use Kerberos v5 or Kerberos v4.\n"
1186 " (default is Kerberos v5)\n");
1187 fprintf(stderr, " -m means use krb524d to convert Kerberos v5 tickets.\n");
1189 fprintf(stderr, " -5 use Kerberos v5.\n"
1190 " (only Kerberos v5 is available)\n");
1192 fprintf(stderr, " No commandline arguments means ");
1193 fprintf(stderr, "authenticate to the local cell.\n");
1194 fprintf(stderr, "\n");
1199 validate_krb5_availability(void)
1202 #define KRB5LIB "krb5_32.dll"
1204 #define KRB5LIB "krb5_64.dll"
1206 HINSTANCE h = LoadLibrary(KRB5LIB);
1210 fprintf(stderr, "Kerberos for Windows library %s is not available.\n", KRB5LIB);
1211 exit(AKLOG_KFW_NOT_INSTALLED);
1216 validate_krb4_availability(void)
1219 HINSTANCE h = LoadLibrary("krbv4w32.dll");
1223 fprintf(stderr, "Kerberos for Windows library krbv4w32.dll is not available.\n");
1224 exit(AKLOG_KFW_NOT_INSTALLED);
1227 fprintf(stderr, "Kerberos v4 is not available in this build of aklog.\n");
1232 int main(int argc, char *argv[])
1234 int status = AKLOG_SUCCESS;
1236 int somethingswrong = FALSE;
1238 cellinfo_t cellinfo;
1240 extern char *progname; /* Name of this program */
1242 extern int dflag; /* Debug mode */
1244 int cmode = FALSE; /* Cellname mode */
1245 int pmode = FALSE; /* Path name mode */
1247 char realm[REALM_SZ]; /* Kerberos realm of afs server */
1248 char cell[BUFSIZ]; /* Cell to which we are authenticating */
1249 char path[MAXPATHLEN + 1]; /* Path length for path mode */
1251 linked_list cells; /* List of cells to log to */
1252 linked_list paths; /* List of paths to log to */
1255 krb5_context context = 0;
1257 memset(&cellinfo, 0, sizeof(cellinfo));
1259 memset(realm, 0, sizeof(realm));
1260 memset(cell, 0, sizeof(cell));
1261 memset(path, 0, sizeof(path));
1266 /* Store the program name here for error messages */
1267 if (progname = LastComponent(argv[0]))
1272 /* Initialize list of cells to which we have authenticated */
1273 (void)ll_init(&authedcells);
1275 /* Parse commandline arguments and make list of what to do. */
1276 for (i = 1; i < argc; i++)
1278 if (strcmp(argv[i], "-d") == 0)
1280 else if (strcmp(argv[i], "-5") == 0)
1283 else if (strcmp(argv[i], "-m") == 0)
1285 else if (strcmp(argv[i], "-4") == 0)
1288 else if (strcmp(argv[i], "-noprdb") == 0)
1290 else if (strcmp(argv[i], "-force") == 0)
1292 else if (((strcmp(argv[i], "-cell") == 0) ||
1293 (strcmp(argv[i], "-c") == 0)) && !pmode)
1298 strcpy(cell, argv[i]);
1303 else if (((strcmp(argv[i], "-path") == 0) ||
1304 (strcmp(argv[i], "-p") == 0)) && !cmode)
1309 strcpy(path, argv[i]);
1314 else if (argv[i][0] == '-')
1316 else if (!pmode && !cmode)
1318 if (FirstComponent(argv[i]) || (strcmp(argv[i], ".") == 0) ||
1319 (strcmp(argv[i], "..") == 0))
1322 strcpy(path, argv[i]);
1327 strcpy(cell, argv[i]);
1335 if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0))
1339 strcpy(realm, argv[i]);
1343 /* Add this cell to list of cells */
1344 strcpy(cellinfo.cell, cell);
1345 strcpy(cellinfo.realm, realm);
1346 if (cur_node = ll_add_node(&cells, ll_tail))
1349 if (new_cellinfo = copy_cellinfo(&cellinfo))
1350 ll_add_data(cur_node, new_cellinfo);
1353 fprintf(stderr, "%s: failure copying cellinfo.\n", progname);
1359 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1363 memset(&cellinfo, 0, sizeof(cellinfo));
1365 memset(cell, 0, sizeof(cell));
1366 memset(realm, 0, sizeof(realm));
1370 /* Add this path to list of paths */
1371 if (cur_node = ll_add_node(&paths, ll_tail))
1374 if (new_path = copy_string(path))
1375 ll_add_data(cur_node, new_path);
1378 fprintf(stderr, "%s: failure copying path name.\n",
1385 fprintf(stderr, "%s: failure adding path to paths list.\n",
1390 memset(path, 0, sizeof(path));
1395 validate_krb5_availability();
1397 validate_krb4_availability();
1400 krb5_init_context(&context);
1402 /* If nothing was given, log to the local cell. */
1403 if ((cells.nelements + paths.nelements) == 0)
1404 status = auth_to_cell(context, NULL, NULL);
1407 /* Log to all cells in the cells list first */
1408 for (cur_node = cells.first; cur_node; cur_node = cur_node->next)
1410 memcpy(&cellinfo, cur_node->data, sizeof(cellinfo));
1411 if (status = auth_to_cell(context,
1412 cellinfo.cell, cellinfo.realm))
1416 /* Then, log to all paths in the paths list */
1417 for (cur_node = paths.first; cur_node; cur_node = cur_node->next)
1419 if (status = auth_to_path(context,
1425 * If only one thing was logged to, we'll return the status
1426 * of the single call. Otherwise, we'll return a generic
1427 * something failed status.
1429 if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1430 status = AKLOG_SOMETHINGSWRONG;
1434 krb5_free_context(context);