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_context context = 0;
198 static krb5_ccache _krb425_ccache;
200 void akexit(int exit_code)
203 krb5_cc_close(context, _krb425_ccache);
205 krb5_free_context(context);
209 long GetLocalCell(struct afsconf_dir **pconfigdir, char *local_cell)
211 if (!(*pconfigdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)))
213 fprintf(stderr, "%s: can't get afs configuration (afsconf_Open(%s))\n",
214 progname, AFSDIR_CLIENT_ETC_DIRPATH);
218 return afsconf_GetLocalCell(*pconfigdir, local_cell, MAXCELLCHARS);
221 long GetCellInfo(struct afsconf_dir **pconfigdir, char* cell,
222 struct afsconf_cell **pcellconfig)
224 return afsconf_GetCellInfo(*pconfigdir, cell, NULL, *pcellconfig);
227 void CloseConf(struct afsconf_dir **pconfigdir)
229 (void) afsconf_Close(*pconfigdir);
232 #define ALLOW_REGISTER 1
233 void ViceIDToUsername(char *username, char *realm_of_user, char *realm_of_cell,
234 char * cell_to_use, CREDENTIALS *c,
236 struct ktc_principal *aclient, struct ktc_principal *aserver, struct ktc_token *atoken)
238 static char lastcell[MAXCELLCHARS+1] = { 0 };
239 static char confname[512] = { 0 };
240 char username_copy[BUFSIZ];
241 afs_int32 viceId; /* AFS uid of user */
242 #ifdef ALLOW_REGISTER
244 #endif /* ALLOW_REGISTER */
246 if (confname[0] == '\0') {
247 strncpy(confname, AFSDIR_CLIENT_ETC_DIRPATH, sizeof(confname));
248 confname[sizeof(confname) - 2] = '\0';
252 printf("About to resolve name %s to id\n", username);
254 strcpy(lastcell, aserver->cell);
256 if (!pr_Initialize (0, confname, aserver->cell)) {
257 char sname[PR_MAXNAMELEN], *at;
259 strncpy(sname, username, PR_MAXNAMELEN);
260 sname[PR_MAXNAMELEN-1] = '\0';
262 at = strchr(sname, '@');
263 if (at && !stricmp(at+1, realm_of_cell))
265 *status = pr_SNameToId (sname, &viceId);
271 printf("Error %d\n", *status);
273 printf("Id %d\n", viceId);
277 * This code is taken from cklog -- it lets people
278 * automatically register with the ptserver in foreign cells
281 #ifdef ALLOW_REGISTER
283 if (viceId != ANONYMOUSID) {
284 #else /* ALLOW_REGISTER */
285 if ((*status == 0) && (viceId != ANONYMOUSID))
286 #endif /* ALLOW_REGISTER */
288 #ifdef AFS_ID_TO_NAME
289 strncpy(username_copy, username, BUFSIZ);
290 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
291 #endif /* AFS_ID_TO_NAME */
293 #ifdef ALLOW_REGISTER
294 } else if (strcmp(realm_of_user, realm_of_cell) != 0) {
297 printf("doing first-time registration of %s "
298 "at %s\n", username, cell_to_use);
301 strncpy(aclient->name, username, MAXKTCNAMELEN - 1);
302 aclient->name[MAXKTCNAMELEN - 1] = '\0';
303 strcpy(aclient->instance, "");
304 strncpy(aclient->cell, cell_to_use, MAXKTCREALMLEN - 1);
305 aclient->cell[MAXKTCREALMLEN - 1] = '\0';
307 for ( i=0; aclient->cell[i]; i++ ) {
308 if ( islower(aclient->cell[i]) )
309 aclient->cell[i] = toupper(aclient->cell[i]);
312 if ((*status = ktc_SetToken(aserver, atoken, aclient, 0))) {
313 printf("%s: unable to set tokens for cell %s "
314 "(status: %d).\n", progname, cell_to_use, *status);
315 *status = AKLOG_TOKEN;
320 * In case you're wondering, we don't need to change the
321 * filename here because we're still connecting to the
322 * same cell -- we're just using a different authentication
326 if ((*status = pr_Initialize(1L, confname, aserver->cell))) {
327 printf("Error %d\n", *status);
331 /* copy the name because pr_CreateUser lowercases the realm */
332 strncpy(username_copy, username, BUFSIZ);
334 *status = pr_CreateUser(username, &id);
336 /* and restore the name to the original state */
337 strncpy(username, username_copy, BUFSIZ);
340 printf("%s: unable to create remote PTS "
341 "user %s in cell %s (status: %d).\n", progname,
342 username, cell_to_use, *status);
344 printf("created cross-cell entry for %s (Id %d) at %s\n",
345 username, viceId, cell_to_use);
346 #ifdef AFS_ID_TO_NAME
347 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
348 #endif /* AFS_ID_TO_NAME */
352 #endif /* ALLOW_REGISTER */
355 char *LastComponent(char *str)
357 char *ret = strrchr(str, DIR);
361 ret = strrchr(str, BDIR);
366 int FirstComponent(char *str)
375 void CopyPathColon(char *origpath, char *path, char *pathtocheck)
378 if (origpath[1] == DRIVECOLON)
380 strncpy(pathtocheck, origpath, 2);
381 strcpy(path, origpath+2);
385 strcpy(path, origpath);
388 int BeginsWithDir(char *str, int colon)
390 return (str[0] == DIR) ||
392 ((str[0] == BDIR) || (colon && str[1] == DRIVECOLON));
399 /* This is a pretty gross hack. Linking against the Transarc
400 * libraries pulls in some rxkad functions which use des. (I don't
401 * think they ever get called.) With Transarc-supplied libraries this
402 * creates a reliance on the symbol des_pcbc_init() which is only in
403 * Transarc's DES libraries (it's an exportability symbol-hiding
404 * thing), which we don't want to use because they don't work with
405 * MIT's krb4 routines. So export a des_pcbc_init() symbol here so we
406 * don't have to link against Transarc's des library.
411 return 0; /* avoid warning */
415 static int get_cred(char *name, char *inst, char *realm, CREDENTIALS *c)
419 status = krb_get_cred(name, inst, realm, c);
420 if (status != KSUCCESS)
422 #ifdef DONT_HAVE_GET_AD_TKT
424 status = krb_mk_req(&ticket, name, inst, realm, 0);
426 status = get_ad_tkt(name, inst, realm, 255);
428 if (status == KSUCCESS)
429 status = krb_get_cred(name, inst, realm, c);
436 static int get_v5cred(krb5_context context,
437 char *name, char *inst, char *realm, CREDENTIALS *c,
442 static krb5_principal client_principal = 0;
444 if (client_principal) {
445 krb5_free_principal(context, client_principal);
446 client_principal = 0;
449 memset((char *)&increds, 0, sizeof(increds));
451 if ((r = krb5_build_principal(context, &increds.server,
452 (int)strlen(realm), realm,
454 (inst && strlen(inst)) ? inst : 0,
459 if (!_krb425_ccache) {
460 if ((r = krb5_cc_default(context, &_krb425_ccache)))
463 if (!client_principal) {
464 if ((r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal))) {
465 krb5_cc_close(context, _krb425_ccache);
470 increds.client = client_principal;
471 increds.times.endtime = 0;
472 /* Ask for DES since that is what V4 understands */
473 increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
475 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
479 /* This requires krb524d to be running with the KDC */
481 r = krb5_524_convert_creds(context, *creds, c);
487 /* There is no header for this function. It is supposed to be private */
488 int krb_get_admhst(char *h,char *r, int n);
490 static char *afs_realm_of_cell(struct afsconf_cell *cellconfig)
492 char krbhst[MAX_HSTNM];
493 static char krbrlm[REALM_SZ+1];
498 strcpy(krbrlm, (char *) krb_realmofhost(cellconfig->hostName[0]));
500 if (krb_get_admhst(krbhst, krbrlm, 1) != KSUCCESS)
503 char *t = cellconfig->name;
518 /* As of MIT Kerberos 1.6, krb5_get_host_realm() will return the NUL-string
519 * if there is no domain_realm mapping for the hostname's domain. This is
520 * used as a trigger indicating that referrals should be used within the
521 * krb5_get_credentials() call. However, if the KDC does not support referrals
522 * that will result in a KRB5_ERR_HOST_REALM_UNKNOWN error and we will have
523 * to manually fallback to mapping the domain of the host as a realm name.
524 * Hence, the new fallback parameter.
526 static char *afs_realm_of_cell5(krb5_context context, struct afsconf_cell *cellconfig, int fallback)
529 static char krbrlm[REALM_SZ+1];
530 krb5_error_code status;
537 p = strchr(cellconfig->hostName[0], '.');
541 strcpy(krbrlm, cellconfig->name);
544 status = krb5_get_host_realm( context, cellconfig->hostName[0], &krbrlms );
545 if (status == 0 && krbrlms && krbrlms[0]) {
546 strcpy(krbrlm, krbrlms[0]);
548 strcpy(krbrlm, cellconfig->name);
553 krb5_free_host_realm( context, krbrlms );
558 static char *copy_cellinfo(cellinfo_t *cellinfo)
560 cellinfo_t *new_cellinfo;
562 if (new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t)))
563 memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
565 return ((char *)new_cellinfo);
569 static char *copy_string(char *string)
573 if (new_string = (char *)calloc(strlen(string) + 1, sizeof(char)))
574 (void) strcpy(new_string, string);
580 static int get_cellconfig(char *cell, struct afsconf_cell *cellconfig,
583 int status = AKLOG_SUCCESS;
584 struct afsconf_dir *configdir = 0;
586 memset(local_cell, 0, sizeof(local_cell));
587 memset(cellconfig, 0, sizeof(*cellconfig));
589 if (GetLocalCell(&configdir, local_cell))
591 fprintf(stderr, "%s: can't determine local cell.\n", progname);
595 if ((cell == NULL) || (cell[0] == 0))
598 if (GetCellInfo(&configdir, cell, &cellconfig))
600 fprintf(stderr, "%s: Can't get information about cell %s.\n",
606 CloseConf(&configdir);
611 static int get_v5_user_realm(krb5_context context,char *realm)
613 static krb5_principal client_principal = 0;
617 krb5_cc_default(context, &_krb425_ccache);
618 if (!client_principal)
619 krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
621 i = krb5_princ_realm(context, client_principal)->length;
622 if (i < REALM_SZ-1) i = REALM_SZ-1;
623 strncpy(realm,krb5_princ_realm(context, client_principal)->data,i);
629 * Log to a cell. If the cell has already been logged to, return without
630 * doing anything. Otherwise, log to it and mark that it has been logged
632 static int auth_to_cell(krb5_context context, char *cell, char *realm)
634 int status = AKLOG_SUCCESS;
635 char username[BUFSIZ]; /* To hold client username structure */
637 char name[ANAME_SZ]; /* Name of afs key */
638 char instance[INST_SZ]; /* Instance of afs key */
639 char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
640 char realm_of_cell[REALM_SZ]; /* Kerberos realm of cell */
641 char local_cell[MAXCELLCHARS+1];
642 char cell_to_use[MAXCELLCHARS+1]; /* Cell to authenticate to */
644 krb5_creds *v5cred = NULL;
648 struct ktc_principal aserver;
649 struct ktc_principal aclient;
650 struct ktc_token atoken, btoken;
653 /* try to avoid an expensive call to get_cellconfig */
654 if (cell && ll_string_check(&authedcells, cell))
657 printf("Already authenticated to %s (or tried to)\n", cell);
658 return(AKLOG_SUCCESS);
661 memset(name, 0, sizeof(name));
662 memset(instance, 0, sizeof(instance));
663 memset(realm_of_user, 0, sizeof(realm_of_user));
664 memset(realm_of_cell, 0, sizeof(realm_of_cell));
666 /* NULL or empty cell returns information on local cell */
667 if (status = get_cellconfig(cell, &ak_cellconfig, local_cell))
670 strncpy(cell_to_use, ak_cellconfig.name, MAXCELLCHARS);
671 cell_to_use[MAXCELLCHARS] = 0;
673 if (ll_string_check(&authedcells, cell_to_use))
676 printf("Already authenticated to %s (or tried to)\n", cell_to_use);
677 return(AKLOG_SUCCESS);
681 * Record that we have attempted to log to this cell. We do this
682 * before we try rather than after so that we will not try
683 * and fail repeatedly for one cell.
685 (void)ll_add_string(&authedcells, cell_to_use);
688 printf("Authenticating to cell %s.\n", cell_to_use);
690 /* We use the afs.<cellname> convention here... */
691 strcpy(name, AFSKEY);
692 strncpy(instance, cell_to_use, sizeof(instance));
693 instance[sizeof(instance)-1] = '\0';
696 * Extract the session key from the ticket file and hand-frob an
697 * afs style authenticator.
703 int realm_fallback = 0;
705 if ( strchr(name,'.') != NULL ) {
706 fprintf(stderr, "%s: Can't support principal names including a dot.\n",
712 if (realm && realm[0])
713 strcpy(realm_of_cell, realm);
715 strcpy(realm_of_cell,
716 afs_realm_of_cell5(context, &ak_cellconfig, realm_fallback));
719 printf("Getting v5 tickets: %s/%s@%s\n", name, instance, realm_of_cell);
720 status = get_v5cred(context, name, instance, realm_of_cell,
728 if (status == 0 && strcmp(realm_of_cell, "") == 0) {
729 krb5_error_code code;
732 code = krb5_decode_ticket(&v5cred->ticket, &ticket);
736 "%s: Couldn't decode ticket to determine realm for "
738 progname, cell_to_use);
740 int len = krb5_princ_realm(context, ticket->server)->length;
741 /* This really shouldn't happen. */
742 if (len > REALM_SZ-1)
745 strncpy(realm_of_cell, krb5_princ_realm(context, ticket->server)->data, len);
746 realm_of_cell[len] = 0;
748 krb5_free_ticket(context, ticket);
752 if (status == KRB5_ERR_HOST_REALM_UNKNOWN) {
755 } else if (status == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN) {
756 if (!realm_of_cell[0]) {
761 printf("Getting v5 tickets: %s@%s\n", name, realm_of_cell);
762 status = get_v5cred(context, name, "", realm_of_cell,
770 if ( status == KRB5KRB_AP_ERR_MSG_TYPE && retry ) {
779 if (realm && realm[0])
780 strcpy(realm_of_cell, realm);
782 strcpy(realm_of_cell, afs_realm_of_cell(&ak_cellconfig));
785 * Try to obtain AFS tickets. Because there are two valid service
786 * names, we will try both, but trying the more specific first.
792 printf("Getting tickets: %s.%s@%s\n", name, instance, realm_of_cell);
793 status = get_cred(name, instance, realm_of_cell, &c);
794 if (status == KDC_PR_UNKNOWN)
797 printf("Getting tickets: %s@%s\n", name, realm_of_cell);
798 status = get_cred(name, "", realm_of_cell, &c);
805 /* TODO: get k5 error text */
806 if (status != KSUCCESS)
809 printf("Kerberos error code returned by get_cred: %d\n", status);
810 fprintf(stderr, "%s: Couldn't get %s AFS tickets: %s\n",
811 progname, cell_to_use,
813 (usev5)?"":krb_err_text(status)
818 return(AKLOG_KERBEROS);
821 strncpy(aserver.name, AFSKEY, MAXKTCNAMELEN - 1);
822 strncpy(aserver.instance, AFSINST, MAXKTCNAMELEN - 1);
823 strncpy(aserver.cell, cell_to_use, MAXKTCREALMLEN - 1);
825 if (usev5 && !use524) {
826 /* This code inserts the entire K5 ticket into the token
827 * No need to perform a krb524 translation which is
828 * commented out in the code below
833 len = min(v5cred->client->data[0].length,MAXKTCNAMELEN - 1);
834 strncpy(username, v5cred->client->data[0].data, len);
835 username[len] = '\0';
837 if ( v5cred->client->length > 1 ) {
838 strcat(username, ".");
839 p = username + strlen(username);
840 len = min(v5cred->client->data[1].length,MAXKTCNAMELEN - strlen(username) - 1);
841 strncpy(p, v5cred->client->data[1].data, len);
845 memset(&atoken, '\0', sizeof(atoken));
846 atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
847 atoken.startTime = v5cred->times.starttime;
848 atoken.endTime = v5cred->times.endtime;
849 memcpy(&atoken.sessionKey, v5cred->keyblock.contents, v5cred->keyblock.length);
850 atoken.ticketLen = v5cred->ticket.length;
851 memcpy(atoken.ticket, v5cred->ticket.data, atoken.ticketLen);
854 strcpy (username, c.pname);
857 strcat(username, ".");
858 strcat(username, c.pinst);
861 atoken.kvno = c.kvno;
862 atoken.startTime = c.issue_date;
863 /* ticket lifetime is in five-minutes blocks. */
864 atoken.endTime = c.issue_date + ((unsigned char)c.lifetime * 5 * 60);
866 memcpy(&atoken.sessionKey, c.session, 8);
867 atoken.ticketLen = c.ticket_st.length;
868 memcpy(atoken.ticket, c.ticket_st.dat, atoken.ticketLen);
875 !ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient) &&
876 atoken.kvno == btoken.kvno &&
877 atoken.ticketLen == btoken.ticketLen &&
878 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
879 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen))
882 printf("Identical tokens already exist; skipping.\n");
889 printf("Not resolving name %s to id (-noprdb set)\n", username);
894 if((status = get_v5_user_realm(context, realm_of_user)) != KSUCCESS) {
895 fprintf(stderr, "%s: Couldn't determine realm of user: %d\n",
897 return(AKLOG_KERBEROS);
901 if ((status = krb_get_tf_realm(TKT_FILE, realm_of_user)) != KSUCCESS)
903 fprintf(stderr, "%s: Couldn't determine realm of user: %s)",
904 progname, krb_err_text(status));
905 return(AKLOG_KERBEROS);
912 /* For Khimaira we want to always append the realm to the name */
913 if (1 /* strcmp(realm_of_user, realm_of_cell) */)
915 strcat(username, "@");
916 strcat(username, realm_of_user);
919 ViceIDToUsername(username, realm_of_user, realm_of_cell, cell_to_use,
925 &status, &aclient, &aserver, &atoken);
929 printf("Set username to %s\n", username);
931 /* Reset the "aclient" structure before we call ktc_SetToken.
932 * This structure was first set by the ktc_GetToken call when
933 * we were comparing whether identical tokens already existed.
935 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
936 strcpy(aclient.instance, "");
938 if (usev5 && !use524) {
939 int len = min(v5cred->client->realm.length,MAXKTCNAMELEN - 1);
940 strncpy(aclient.cell, v5cred->client->realm.data, len);
941 aclient.cell[len] = '\0';
945 strncpy(aclient.cell, c.realm, MAXKTCREALMLEN - 1);
948 for ( i=0; aclient.cell[i]; i++ ) {
949 if ( islower(aclient.cell[i]) )
950 aclient.cell[i] = toupper(aclient.cell[i]);
954 printf("Getting tokens.\n");
955 if (status = ktc_SetToken(&aserver, &atoken, &aclient, 0))
958 "%s: unable to obtain tokens for cell %s (status: %d).\n",
959 progname, cell_to_use, status);
960 status = AKLOG_TOKEN;
966 static int get_afs_mountpoint(char *file, char *mountpoint, int size)
968 char our_file[MAXPATHLEN + 1];
970 char *last_component;
971 struct ViceIoctl vio;
972 char cellname[BUFSIZ];
974 memset(our_file, 0, sizeof(our_file));
975 strcpy(our_file, file);
977 if (last_component = LastComponent(our_file))
979 *last_component++ = 0;
980 parent_dir = our_file;
984 last_component = our_file;
988 memset(cellname, 0, sizeof(cellname));
990 vio.in = last_component;
991 vio.in_size = strlen(last_component)+1;
993 vio.out = mountpoint;
995 if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0))
997 if (strchr(mountpoint, VOLMARKER) == NULL)
1000 vio.in_size = strlen(file) + 1;
1001 vio.out_size = sizeof(cellname);
1004 if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1))
1006 strcat(cellname, VOLMARKERSTRING);
1007 strcat(cellname, mountpoint + 1);
1008 memset(mountpoint + 1, 0, size - 1);
1009 strcpy(mountpoint + 1, cellname);
1020 * This routine each time it is called returns the next directory
1021 * down a pathname. It resolves all symbolic links. The first time
1022 * it is called, it should be called with the name of the path
1023 * to be descended. After that, it should be called with the arguemnt
1026 static char *next_path(char *origpath)
1028 static char path[MAXPATHLEN + 1];
1029 static char pathtocheck[MAXPATHLEN + 1];
1031 int link = FALSE; /* Is this a symbolic link? */
1032 char linkbuf[MAXPATHLEN + 1];
1033 char tmpbuf[MAXPATHLEN + 1];
1035 static char *last_comp; /* last component of directory name */
1036 static char *elast_comp; /* End of last component */
1040 static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
1042 /* If we are given something for origpath, we are initializing only. */
1045 memset(path, 0, sizeof(path));
1046 memset(pathtocheck, 0, sizeof(pathtocheck));
1047 CopyPathColon(origpath, path, pathtocheck);
1053 /* We were not given origpath; find then next path to check */
1055 /* If we've gotten all the way through already, return NULL */
1056 if (last_comp == NULL)
1061 while (BeginsWithDir(last_comp, FALSE))
1062 strncat(pathtocheck, last_comp++, 1);
1063 len = (elast_comp = LastComponent(last_comp))
1064 ? elast_comp - last_comp : strlen(last_comp);
1065 strncat(pathtocheck, last_comp, len);
1066 memset(linkbuf, 0, sizeof(linkbuf));
1067 if (link = (readlink(pathtocheck, linkbuf, sizeof(linkbuf)) > 0))
1069 if (++symlinkcount > MAXSYMLINKS)
1071 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
1072 akexit(AKLOG_BADPATH);
1074 memset(tmpbuf, 0, sizeof(tmpbuf));
1076 strcpy(tmpbuf, elast_comp);
1077 if (BeginsWithDir(linkbuf, FALSE))
1080 * If this is a symbolic link to an absolute path,
1081 * replace what we have by the absolute path.
1083 memset(path, 0, strlen(path));
1084 memcpy(path, linkbuf, sizeof(linkbuf));
1085 strcat(path, tmpbuf);
1088 memset(pathtocheck, 0, sizeof(pathtocheck));
1093 * If this is a symbolic link to a relative path,
1094 * replace only the last component with the link name.
1096 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
1097 strcat(path, tmpbuf);
1099 if (t = LastComponent(pathtocheck))
1102 memset(t, 0, strlen(t));
1105 memset(pathtocheck, 0, sizeof(pathtocheck));
1109 last_comp = elast_comp;
1113 return(pathtocheck);
1117 * This routine descends through a path to a directory, logging to
1118 * every cell it encounters along the way.
1120 static int auth_to_path(krb5_context context, char *path)
1122 int status = AKLOG_SUCCESS;
1123 int auth_to_cell_status = AKLOG_SUCCESS;
1126 char pathtocheck[MAXPATHLEN + 1];
1127 char mountpoint[MAXPATHLEN + 1];
1133 if (BeginsWithDir(path, TRUE))
1134 strcpy(pathtocheck, path);
1137 if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL)
1139 fprintf(stderr, "Unable to find current working directory:\n");
1140 fprintf(stderr, "%s\n", pathtocheck);
1141 fprintf(stderr, "Try an absolute pathname.\n");
1142 akexit(AKLOG_BADPATH);
1146 /* in WIN32, if getcwd returns a root dir (eg: c:\), the returned string
1147 * will already have a trailing slash ('\'). Otherwise, the string will
1148 * end in the last directory name */
1150 if(pathtocheck[strlen(pathtocheck) - 1] != BDIR)
1152 strcat(pathtocheck, DIRSTRING);
1153 strcat(pathtocheck, path);
1156 next_path(pathtocheck);
1158 /* Go on to the next level down the path */
1159 while (nextpath = next_path(NULL))
1161 strcpy(pathtocheck, nextpath);
1163 printf("Checking directory [%s]\n", pathtocheck);
1165 * If this is an afs mountpoint, determine what cell from
1166 * the mountpoint name which is of the form
1167 * #cellname:volumename or %cellname:volumename.
1169 if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint)))
1172 printf("Found mount point [%s]\n", mountpoint);
1173 /* skip over the '#' or '%' */
1174 cell = mountpoint + 1;
1175 if (endofcell = strchr(mountpoint, VOLMARKER))
1178 if (auth_to_cell_status = auth_to_cell(context, cell, NULL))
1180 if (status == AKLOG_SUCCESS)
1181 status = auth_to_cell_status;
1182 else if (status != auth_to_cell_status)
1183 status = AKLOG_SOMETHINGSWRONG;
1191 if (lstat(pathtocheck, &st) < 0)
1194 * If we've logged and still can't stat, there's
1197 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1198 pathtocheck, strerror(errno));
1199 return(AKLOG_BADPATH);
1201 else if (!S_ISDIR(st.st_mode))
1203 /* Allow only directories */
1204 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1206 return(AKLOG_BADPATH);
1214 /* Print usage message and exit */
1215 static void usage(void)
1217 fprintf(stderr, "\nUsage: %s %s%s%s%s\n", progname,
1218 "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1219 "[[-p | -path] pathname]\n",
1220 " [-noprdb] [-force]\n",
1227 fprintf(stderr, " -d gives debugging information.\n");
1228 fprintf(stderr, " krb_realm is the kerberos realm of a cell.\n");
1229 fprintf(stderr, " pathname is the name of a directory to which ");
1230 fprintf(stderr, "you wish to authenticate.\n");
1231 fprintf(stderr, " -noprdb means don't try to determine AFS ID.\n");
1233 fprintf(stderr, " -5 or -4 selects whether to use Kerberos v5 or Kerberos v4.\n"
1234 " (default is Kerberos v5)\n");
1235 fprintf(stderr, " -m means use krb524d to convert Kerberos v5 tickets.\n");
1237 fprintf(stderr, " -5 use Kerberos v5.\n"
1238 " (only Kerberos v5 is available)\n");
1240 fprintf(stderr, " No commandline arguments means ");
1241 fprintf(stderr, "authenticate to the local cell.\n");
1242 fprintf(stderr, "\n");
1243 akexit(AKLOG_USAGE);
1247 validate_krb5_availability(void)
1250 #define KRB5LIB "krb5_32.dll"
1252 #define KRB5LIB "krb5_64.dll"
1254 HINSTANCE h = LoadLibrary(KRB5LIB);
1258 fprintf(stderr, "Kerberos for Windows library %s is not available.\n", KRB5LIB);
1259 akexit(AKLOG_KFW_NOT_INSTALLED);
1264 validate_krb4_availability(void)
1267 HINSTANCE h = LoadLibrary("krbv4w32.dll");
1271 fprintf(stderr, "Kerberos for Windows library krbv4w32.dll is not available.\n");
1272 akexit(AKLOG_KFW_NOT_INSTALLED);
1275 fprintf(stderr, "Kerberos v4 is not available in this build of aklog.\n");
1276 akexit(AKLOG_USAGE);
1280 int main(int argc, char *argv[])
1282 int status = AKLOG_SUCCESS;
1284 int somethingswrong = FALSE;
1286 cellinfo_t cellinfo;
1288 extern char *progname; /* Name of this program */
1290 extern int dflag; /* Debug mode */
1292 int cmode = FALSE; /* Cellname mode */
1293 int pmode = FALSE; /* Path name mode */
1295 char realm[REALM_SZ]; /* Kerberos realm of afs server */
1296 char cell[BUFSIZ]; /* Cell to which we are authenticating */
1297 char path[MAXPATHLEN + 1]; /* Path length for path mode */
1299 linked_list cells; /* List of cells to log to */
1300 linked_list paths; /* List of paths to log to */
1303 memset(&cellinfo, 0, sizeof(cellinfo));
1305 memset(realm, 0, sizeof(realm));
1306 memset(cell, 0, sizeof(cell));
1307 memset(path, 0, sizeof(path));
1312 /* Store the program name here for error messages */
1313 if (progname = LastComponent(argv[0]))
1318 /* Initialize list of cells to which we have authenticated */
1319 (void)ll_init(&authedcells);
1321 /* Parse commandline arguments and make list of what to do. */
1322 for (i = 1; i < argc; i++)
1324 if (strcmp(argv[i], "-d") == 0)
1326 else if (strcmp(argv[i], "-5") == 0)
1329 else if (strcmp(argv[i], "-m") == 0)
1331 else if (strcmp(argv[i], "-4") == 0)
1334 else if (strcmp(argv[i], "-noprdb") == 0)
1336 else if (strcmp(argv[i], "-force") == 0)
1338 else if (((strcmp(argv[i], "-cell") == 0) ||
1339 (strcmp(argv[i], "-c") == 0)) && !pmode)
1344 strcpy(cell, argv[i]);
1349 else if (((strcmp(argv[i], "-path") == 0) ||
1350 (strcmp(argv[i], "-p") == 0)) && !cmode)
1355 strcpy(path, argv[i]);
1360 else if (argv[i][0] == '-')
1362 else if (!pmode && !cmode)
1364 if (FirstComponent(argv[i]) || (strcmp(argv[i], ".") == 0) ||
1365 (strcmp(argv[i], "..") == 0))
1368 strcpy(path, argv[i]);
1373 strcpy(cell, argv[i]);
1381 if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0))
1385 strcpy(realm, argv[i]);
1389 /* Add this cell to list of cells */
1390 strcpy(cellinfo.cell, cell);
1391 strcpy(cellinfo.realm, realm);
1392 if (cur_node = ll_add_node(&cells, ll_tail))
1395 if (new_cellinfo = copy_cellinfo(&cellinfo))
1396 ll_add_data(cur_node, new_cellinfo);
1399 fprintf(stderr, "%s: failure copying cellinfo.\n", progname);
1405 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1409 memset(&cellinfo, 0, sizeof(cellinfo));
1411 memset(cell, 0, sizeof(cell));
1412 memset(realm, 0, sizeof(realm));
1416 /* Add this path to list of paths */
1417 if (cur_node = ll_add_node(&paths, ll_tail))
1420 if (new_path = copy_string(path))
1421 ll_add_data(cur_node, new_path);
1424 fprintf(stderr, "%s: failure copying path name.\n",
1431 fprintf(stderr, "%s: failure adding path to paths list.\n",
1436 memset(path, 0, sizeof(path));
1441 validate_krb5_availability();
1443 validate_krb4_availability();
1446 krb5_init_context(&context);
1448 /* If nothing was given, log to the local cell. */
1449 if ((cells.nelements + paths.nelements) == 0)
1450 status = auth_to_cell(context, NULL, NULL);
1453 /* Log to all cells in the cells list first */
1454 for (cur_node = cells.first; cur_node; cur_node = cur_node->next)
1456 memcpy(&cellinfo, cur_node->data, sizeof(cellinfo));
1457 if (status = auth_to_cell(context,
1458 cellinfo.cell, cellinfo.realm))
1462 /* Then, log to all paths in the paths list */
1463 for (cur_node = paths.first; cur_node; cur_node = cur_node->next)
1465 if (status = auth_to_path(context,
1471 * If only one thing was logged to, we'll return the status
1472 * of the single call. Otherwise, we'll return a generic
1473 * something failed status.
1475 if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1476 status = AKLOG_SOMETHINGSWRONG;