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-2008 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.
44 #include <afsconfig.h>
45 #include <afs/param.h>
52 #include <sys/types.h>
56 #include <afs/com_err.h>
65 #define CREDENTIALS void
68 #include <afs/ptserver.h>
69 #include <afs/ptuser.h>
70 #include <afs/pterror.h>
75 #include <afs\cm_config.h>
77 #include <afs\cellconfig.h>
78 #include <afs\pioctl_nt.h>
79 #include <afs\smb_iocons.h>
80 #include <WINNT\afsreg.h>
81 #include <krbcompat_delayload.h>
83 #define DONT_HAVE_GET_AD_TKT
84 #define MAXSYMLINKS 255
87 /* Win32 uses get_krb_err_txt_entry(status) instead of krb_err_txt[status],
88 * so we use a bit of indirection like the GNU CVS sources.
90 #define krb_err_text(status) get_krb_err_txt_entry(status)
93 #define DRIVECOLON ':' /* Drive letter separator */
94 #define BDIR '\\' /* Other character that divides directories */
97 readlink(char *path, char *buf, int buffers)
102 char * getcwd(char*, size_t);
105 get_cellconfig_callback(void *cellconfig, struct sockaddr_in *addrp, char *namep)
107 struct afsconf_cell *cc = (struct afsconf_cell *) cellconfig;
109 cc->hostAddr[cc->numServers] = *addrp;
110 strcpy(cc->hostName[cc->numServers], namep);
116 #include <sys/param.h>
118 #include <arpa/inet.h>
119 #include <sys/socket.h>
122 #include <afs/param.h>
123 #include <afs/auth.h>
124 #include <afs/cellconfig.h>
125 #include <afs/vice.h>
126 #include <afs/venus.h>
127 #include <afs/ptserver.h>
129 #define krb_err_text(status) krb_err_txt[status]
131 /* Cheesy test for determining AFS 3.5. */
132 #ifndef AFSCONF_CLIENTNAME
137 #include <afs/dirpath.h>
139 #define AFSDIR_CLIENT_ETC_DIRPATH AFSCONF_CLIENTNAME
144 #include "linked_list.h"
149 #define AKLOG_SUCCESS 0
150 #define AKLOG_USAGE 1
151 #define AKLOG_SOMETHINGSWRONG 2
153 #define AKLOG_KERBEROS 4
154 #define AKLOG_TOKEN 5
155 #define AKLOG_BADPATH 6
157 #define AKLOG_KFW_NOT_INSTALLED 8
172 #define MAXSYMLINKS 15
175 #define DIR '/' /* Character that divides directories */
176 #define DIRSTRING "/" /* String form of above */
177 #define VOLMARKER ':' /* Character separating cellname from mntpt */
178 #define VOLMARKERSTRING ":" /* String form of above */
182 char realm[REALM_SZ];
186 static char *progname = NULL; /* Name of this program */
187 static int dflag = FALSE; /* Give debugging information */
188 static int noprdb = FALSE; /* Skip resolving name to id? */
189 static int force = FALSE; /* Bash identical tokens? */
190 static linked_list authedcells; /* List of cells already logged to */
192 static int usev5 = TRUE; /* use kerberos 5? */
193 static int use524 = FALSE; /* use krb524? */
194 static krb5_context context = 0;
195 static krb5_ccache _krb425_ccache = 0;
197 void akexit(int exit_code)
200 krb5_cc_close(context, _krb425_ccache);
202 krb5_free_context(context);
206 /* A com_error bodge. The idea here is that this routine lets us lookup
207 * things in the system com_err, if the AFS one just tells us the error
212 redirect_errors(const char *who, afs_int32 code, const char *fmt, va_list ap)
214 krb5_context context;
222 char *str = (char *)afs_error_message(code);
223 if (strncmp(str, "unknown", strlen(str)) == 0) {
224 if (!krb5_init_context(&context))
226 str = krb5_get_error_message(NULL, code);
233 krb5_free_error_message(context, str);
234 krb5_free_context(context);
238 vfprintf(stderr, fmt, ap);
244 long GetLocalCell(struct afsconf_dir **pconfigdir, char *local_cell)
246 if (!(*pconfigdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)))
248 fprintf(stderr, "%s: can't get afs configuration (afsconf_Open(%s))\n",
249 progname, AFSDIR_CLIENT_ETC_DIRPATH);
253 return afsconf_GetLocalCell(*pconfigdir, local_cell, MAXCELLCHARS);
256 long GetCellInfo(struct afsconf_dir **pconfigdir, char* cell,
257 struct afsconf_cell **pcellconfig)
259 return afsconf_GetCellInfo(*pconfigdir, cell, NULL, *pcellconfig);
262 void CloseConf(struct afsconf_dir **pconfigdir)
264 (void) afsconf_Close(*pconfigdir);
267 #define ALLOW_REGISTER 1
268 void ViceIDToUsername(char *username, char *realm_of_user, char *realm_of_cell,
269 char * cell_to_use, CREDENTIALS *c,
271 struct ktc_principal *aclient, struct ktc_principal *aserver, struct ktc_token *atoken)
273 static char lastcell[MAXCELLCHARS+1] = { 0 };
274 static char confname[512] = { 0 };
275 char username_copy[BUFSIZ];
276 afs_int32 viceId; /* AFS uid of user */
278 if (confname[0] == '\0') {
279 strncpy(confname, AFSDIR_CLIENT_ETC_DIRPATH, sizeof(confname));
280 confname[sizeof(confname) - 2] = '\0';
284 printf("About to resolve name %s to id\n", username);
286 strcpy(lastcell, aserver->cell);
288 if (!pr_Initialize (0, confname, aserver->cell)) {
289 char sname[PR_MAXNAMELEN], *at;
291 strncpy(sname, username, PR_MAXNAMELEN);
292 sname[PR_MAXNAMELEN-1] = '\0';
294 at = strchr(sname, '@');
295 if (at && !stricmp(at+1, realm_of_cell))
297 *status = pr_SNameToId (sname, &viceId);
303 printf("pr_SNameToId Error %s\n", afs_error_message(*status));
305 printf("Id %d\n", viceId);
309 * This code is taken from cklog -- it lets people
310 * automatically register with the ptserver in foreign cells
313 #ifdef ALLOW_REGISTER
315 if (viceId != ANONYMOUSID) {
316 #else /* ALLOW_REGISTER */
317 if ((*status == 0) && (viceId != ANONYMOUSID))
318 #endif /* ALLOW_REGISTER */
320 #ifdef AFS_ID_TO_NAME
321 strncpy(username_copy, username, BUFSIZ);
322 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
323 #endif /* AFS_ID_TO_NAME */
325 #ifdef ALLOW_REGISTER
326 } else if (strcmp(realm_of_user, realm_of_cell) != 0) {
329 char * smbname = NULL;
332 printf("doing first-time registration of %s "
333 "at %s\n", username, cell_to_use);
335 strncpy(aclient->name, username, MAXKTCNAMELEN - 1);
336 aclient->name[MAXKTCNAMELEN - 1] = '\0';
337 strcpy(aclient->instance, "");
338 strncpy(aclient->cell, cell_to_use, MAXKTCREALMLEN - 1);
339 aclient->cell[MAXKTCREALMLEN - 1] = '\0';
341 for ( i=0; aclient->cell[i]; i++ ) {
342 if ( islower(aclient->cell[i]) )
343 aclient->cell[i] = toupper(aclient->cell[i]);
346 smbname = getenv("AFS_SMBNAME");
348 strncpy(aclient->smbname, smbname, MAXKTCNAMELEN - 1);
349 aclient->smbname[MAXKTCNAMELEN - 1] = '\0';
350 flags = AFS_SETTOK_LOGON;
352 printf("obtaining tokens for %s\n", aclient->smbname);
355 if ((*status = ktc_SetToken(aserver, atoken, aclient, flags))) {
356 afs_com_err(progname, *status,
357 "while obtaining tokens for cell %s\n",
359 *status = AKLOG_TOKEN;
364 * In case you're wondering, we don't need to change the
365 * filename here because we're still connecting to the
366 * same cell -- we're just using a different authentication
370 if ((*status = pr_Initialize(1L, confname, aserver->cell))) {
371 printf("pr_Initialize Error %s\n", afs_error_message(*status));
375 /* copy the name because pr_CreateUser lowercases the realm */
376 strncpy(username_copy, username, BUFSIZ);
379 *status = pr_CreateUser(username_copy, &viceId);
382 printf("%s: unable to create remote PTS "
383 "user %s in cell %s (status: %s).\n", progname,
384 username_copy, cell_to_use, afs_error_message(*status));
386 printf("created cross-cell entry for %s (Id %d) at %s\n",
387 username_copy, viceId, cell_to_use);
388 #ifdef AFS_ID_TO_NAME
389 snprintf (username, BUFSIZ, "%s (AFS ID %d)", username_copy, (int) viceId);
390 #endif /* AFS_ID_TO_NAME */
394 #endif /* ALLOW_REGISTER */
397 char *LastComponent(char *str)
399 char *ret = strrchr(str, DIR);
403 ret = strrchr(str, BDIR);
408 int FirstComponent(char *str)
417 void CopyPathColon(char *origpath, char *path, char *pathtocheck)
420 if (origpath[1] == DRIVECOLON)
422 strncpy(pathtocheck, origpath, 2);
423 strcpy(path, origpath+2);
427 strcpy(path, origpath);
430 int BeginsWithDir(char *str, int colon)
432 return (str[0] == DIR) ||
434 ((str[0] == BDIR) || (colon && str[1] == DRIVECOLON));
441 /* This is a pretty gross hack. Linking against the Transarc
442 * libraries pulls in some rxkad functions which use des. (I don't
443 * think they ever get called.) With Transarc-supplied libraries this
444 * creates a reliance on the symbol des_pcbc_init() which is only in
445 * Transarc's DES libraries (it's an exportability symbol-hiding
446 * thing), which we don't want to use because they don't work with
447 * MIT's krb4 routines. So export a des_pcbc_init() symbol here so we
448 * don't have to link against Transarc's des library.
453 return 0; /* avoid warning */
457 static int get_cred(char *name, char *inst, char *realm, CREDENTIALS *c)
461 status = krb_get_cred(name, inst, realm, c);
462 if (status != KSUCCESS)
464 #ifdef DONT_HAVE_GET_AD_TKT
466 status = krb_mk_req(&ticket, name, inst, realm, 0);
468 status = get_ad_tkt(name, inst, realm, 255);
470 if (status == KSUCCESS)
471 status = krb_get_cred(name, inst, realm, c);
478 static int get_v5cred(krb5_context context,
479 char *name, char *inst, char *realm, CREDENTIALS *c,
484 static krb5_principal client_principal = 0;
486 if (client_principal) {
487 krb5_free_principal(context, client_principal);
488 client_principal = 0;
491 memset(&increds, 0, sizeof(increds));
493 if ((r = krb5_build_principal(context, &increds.server,
494 (int)strlen(realm), realm,
496 (inst && strlen(inst)) ? inst : 0,
501 if (!_krb425_ccache) {
502 if ((r = krb5_cc_default(context, &_krb425_ccache)))
505 if (!client_principal) {
506 if ((r = krb5_cc_get_principal(context, _krb425_ccache, &client_principal))) {
507 krb5_cc_close(context, _krb425_ccache);
512 increds.client = client_principal;
513 increds.times.endtime = 0;
514 /* Ask for DES since that is what V4 understands */
516 increds.keyblock.enctype = ENCTYPE_DES_CBC_CRC;
518 r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
523 /* This requires krb524d to be running with the KDC */
525 r = krb5_524_convert_creds(context, *creds, c);
532 /* There is no header for this function. It is supposed to be private */
533 int krb_get_admhst(char *h,char *r, int n);
535 static char *afs_realm_of_cell(struct afsconf_cell *cellconfig)
537 char krbhst[MAX_HSTNM];
538 static char krbrlm[REALM_SZ+1];
543 strcpy(krbrlm, (char *) krb_realmofhost(cellconfig->hostName[0]));
545 if (krb_get_admhst(krbhst, krbrlm, 1) != KSUCCESS)
548 char *t = cellconfig->name;
563 /* As of MIT Kerberos 1.6, krb5_get_host_realm() will return the NUL-string
564 * if there is no domain_realm mapping for the hostname's domain. This is
565 * used as a trigger indicating that referrals should be used within the
566 * krb5_get_credentials() call. However, if the KDC does not support referrals
567 * that will result in a KRB5_ERR_HOST_REALM_UNKNOWN error and we will have
568 * to manually fallback to mapping the domain of the host as a realm name.
569 * Hence, the new fallback parameter.
571 static char *afs_realm_of_cell5(krb5_context context, struct afsconf_cell *cellconfig, int fallback)
574 static char krbrlm[REALM_SZ+1];
575 krb5_error_code status;
582 p = strchr(cellconfig->hostName[0], '.');
586 strcpy(krbrlm, cellconfig->name);
589 status = krb5_get_host_realm( context, cellconfig->hostName[0], &krbrlms );
590 if (status == 0 && krbrlms && krbrlms[0]) {
591 strcpy(krbrlm, krbrlms[0]);
593 strcpy(krbrlm, cellconfig->name);
598 krb5_free_host_realm( context, krbrlms );
603 static char *copy_cellinfo(cellinfo_t *cellinfo)
605 cellinfo_t *new_cellinfo;
607 if (new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t)))
608 memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
610 return ((char *)new_cellinfo);
614 static int get_cellconfig(char *cell, struct afsconf_cell *cellconfig,
617 int status = AKLOG_SUCCESS;
618 struct afsconf_dir *configdir = 0;
620 memset(local_cell, 0, sizeof(local_cell));
621 memset(cellconfig, 0, sizeof(*cellconfig));
623 if (GetLocalCell(&configdir, local_cell))
625 fprintf(stderr, "%s: can't determine local cell.\n", progname);
629 if ((cell == NULL) || (cell[0] == 0))
632 if (GetCellInfo(&configdir, cell, &cellconfig))
634 fprintf(stderr, "%s: Can't get information about cell %s.\n",
639 if (cellconfig->linkedCell)
640 cellconfig->linkedCell = strdup(cellconfig->linkedCell);
642 CloseConf(&configdir);
647 static int get_v5_user_realm(krb5_context context,char *realm)
649 static krb5_principal client_principal = 0;
650 krb5_error_code code;
652 if (!_krb425_ccache) {
653 code = krb5_cc_default(context, &_krb425_ccache);
657 if (!client_principal) {
658 code = krb5_cc_get_principal(context, _krb425_ccache, &client_principal);
662 strncpy(realm, krb5_principal_get_realm(context, client_principal), REALM_SZ - 1);
663 realm[REALM_SZ - 1] = 0;
668 copy_realm_of_ticket(krb5_context context, char * dest, size_t destlen, krb5_creds *v5cred) {
673 ret = decode_Ticket(v5cred->ticket.data, v5cred->ticket.length,
676 strncpy(dest, ticket.realm, destlen - 1);
677 dest[destlen - 1] = '\0';
679 free_Ticket(&ticket);
683 typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
687 static int init = TRUE;
688 static int bIsWow64 = FALSE;
692 LPFN_ISWOW64PROCESS fnIsWow64Process = NULL;
694 hModule = GetModuleHandle(TEXT("kernel32"));
696 fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(hModule, "IsWow64Process");
698 if (NULL != fnIsWow64Process)
700 if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
702 // on error, assume FALSE.
703 // in other words, do nothing.
706 FreeLibrary(hModule);
714 accept_dotted_usernames(void)
720 code = RegOpenKeyEx(HKEY_CURRENT_USER, AFSREG_USER_OPENAFS_SUBKEY,
721 0, (is_wow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
722 if (code == ERROR_SUCCESS) {
724 code = RegQueryValueEx(parmKey, "AcceptDottedPrincipalNames", NULL, NULL,
725 (BYTE *) &value, &len);
726 RegCloseKey(parmKey);
728 if (code != ERROR_SUCCESS) {
729 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY,
730 0, (is_wow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
731 if (code == ERROR_SUCCESS) {
733 code = RegQueryValueEx(parmKey, "AcceptDottedPrincipalNames", NULL, NULL,
734 (BYTE *) &value, &len);
735 RegCloseKey (parmKey);
743 * Log to a cell. If the cell has already been logged to, return without
744 * doing anything. Otherwise, log to it and mark that it has been logged
746 static int auth_to_cell(krb5_context context, char *cell, char *realm)
748 int status = AKLOG_SUCCESS;
749 char username[BUFSIZ]; /* To hold client username structure */
751 char name[ANAME_SZ]; /* Name of afs key */
752 char instance[INST_SZ]; /* Instance of afs key */
753 char realm_of_user[REALM_SZ]; /* Kerberos realm of user */
754 char realm_of_cell[REALM_SZ]; /* Kerberos realm of cell */
755 char local_cell[MAXCELLCHARS+1];
756 char cell_to_use[MAXCELLCHARS+1]; /* Cell to authenticate to */
758 krb5_creds *v5cred = NULL;
762 struct ktc_principal aserver;
763 struct ktc_principal aclient;
764 struct ktc_token atoken, btoken;
765 struct afsconf_cell ak_cellconfig; /* General information about the cell */
767 int getLinkedCell = 0;
769 char * smbname = getenv("AFS_SMBNAME");
771 /* try to avoid an expensive call to get_cellconfig */
772 if (cell && ll_string_check(&authedcells, cell))
775 printf("Already authenticated to %s (or tried to)\n", cell);
776 return(AKLOG_SUCCESS);
779 memset(name, 0, sizeof(name));
780 memset(instance, 0, sizeof(instance));
781 memset(realm_of_user, 0, sizeof(realm_of_user));
782 memset(realm_of_cell, 0, sizeof(realm_of_cell));
783 memset(&ak_cellconfig, 0, sizeof(ak_cellconfig));
785 /* NULL or empty cell returns information on local cell */
786 if (status = get_cellconfig(cell, &ak_cellconfig, local_cell))
791 strncpy(cell_to_use, ak_cellconfig.linkedCell, MAXCELLCHARS);
793 strncpy(cell_to_use, ak_cellconfig.name, MAXCELLCHARS);
794 cell_to_use[MAXCELLCHARS] = 0;
796 if (ll_string_check(&authedcells, cell_to_use))
799 printf("Already authenticated to %s (or tried to)\n", cell_to_use);
800 status = AKLOG_SUCCESS;
805 * Record that we have attempted to log to this cell. We do this
806 * before we try rather than after so that we will not try
807 * and fail repeatedly for one cell.
809 (void)ll_add_string(&authedcells, cell_to_use);
812 printf("Authenticating to cell %s.\n", cell_to_use);
814 /* We use the afs.<cellname> convention here... */
815 strcpy(name, AFSKEY);
816 strncpy(instance, cell_to_use, sizeof(instance));
817 instance[sizeof(instance)-1] = '\0';
820 * Extract the session key from the ticket file and hand-frob an
821 * afs style authenticator.
827 int realm_fallback = 0;
829 if ((status = get_v5_user_realm(context, realm_of_user)) != KSUCCESS) {
832 msg = krb5_get_error_message(context, status);
833 fprintf(stderr, "%s: Couldn't determine realm of user: %s\n",
835 krb5_free_error_message(context, msg);
836 status = AKLOG_KERBEROS;
840 if ( strchr(name,'.') != NULL && !accept_dotted_usernames()) {
841 fprintf(stderr, "%s: Can't support principal names including a dot.\n",
848 if (realm && realm[0]) {
850 printf("Getting v5 tickets: %s/%s@%s\n", name, instance, realm);
851 status = get_v5cred(context, name, instance, realm,
858 strcpy(realm_of_cell, realm);
860 strcpy(realm_of_cell,
861 afs_realm_of_cell5(context, &ak_cellconfig, realm_fallback));
863 if (retry == 1 && realm_fallback == 0) {
864 /* Only try the realm_of_user once */
867 printf("Getting v5 tickets: %s/%s@%s\n", name, instance, realm_of_user);
868 status = get_v5cred(context, name, instance, realm_of_user,
876 /* we have determined that the client realm
877 * is a valid cell realm
879 strcpy(realm_of_cell, realm_of_user);
883 if (status != 0 && (!retry || retry && strcmp(realm_of_user,realm_of_cell))) {
885 printf("Getting v5 tickets: %s/%s@%s\n", name, instance, realm_of_cell);
886 status = get_v5cred(context, name, instance, realm_of_cell,
893 if (!status && !strlen(realm_of_cell))
894 copy_realm_of_ticket(context, realm_of_cell, sizeof(realm_of_cell), v5cred);
898 if (!realm_fallback && status == KRB5_ERR_HOST_REALM_UNKNOWN) {
901 } else if (status == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN) {
902 if (!realm_fallback && !realm_of_cell[0]) {
907 printf("Getting v5 tickets: %s@%s\n", name, realm_of_cell);
908 status = get_v5cred(context, name, "", realm_of_cell,
915 if (!status && !strlen(realm_of_cell))
916 copy_realm_of_ticket(context, realm_of_cell, sizeof(realm_of_cell), v5cred);
919 if ( status == KRB5KRB_AP_ERR_MSG_TYPE && retry ) {
928 if (realm && realm[0])
929 strcpy(realm_of_cell, realm);
931 strcpy(realm_of_cell, afs_realm_of_cell(&ak_cellconfig));
934 * Try to obtain AFS tickets. Because there are two valid service
935 * names, we will try both, but trying the more specific first.
941 printf("Getting tickets: %s.%s@%s\n", name, instance, realm_of_cell);
942 status = get_cred(name, instance, realm_of_cell, &c);
943 if (status == KDC_PR_UNKNOWN)
946 printf("Getting tickets: %s@%s\n", name, realm_of_cell);
947 status = get_cred(name, "", realm_of_cell, &c);
955 if (status != KSUCCESS)
959 printf("Kerberos error code returned by get_cred: %d\n", status);
962 msg = krb5_get_error_message(context, status);
966 msg = krb_err_text(status);
968 fprintf(stderr, "%s: Couldn't get %s AFS tickets: %s\n",
969 progname, cell_to_use, msg?msg:"(unknown error)");
971 krb5_free_error_message(context, msg);
972 status = AKLOG_KERBEROS;
976 strncpy(aserver.name, AFSKEY, MAXKTCNAMELEN - 1);
977 strncpy(aserver.instance, AFSINST, MAXKTCNAMELEN - 1);
978 strncpy(aserver.cell, cell_to_use, MAXKTCREALMLEN - 1);
980 if (usev5 && !use524) {
981 /* This code inserts the entire K5 ticket into the token
982 * No need to perform a krb524 translation which is
983 * commented out in the code below
989 un = krb5_principal_get_comp_string(context, v5cred->client, 0);
990 strncpy(username, un, MAXKTCNAMELEN - 1);
991 username[MAXKTCNAMELEN - 1] = '\0';
993 if ( krb5_principal_get_num_comp(context, v5cred->client) > 1 ) {
994 strcat(username, ".");
995 p = username + strlen(username);
996 len = (unsigned int)(MAXKTCNAMELEN - strlen(username) - 1);
997 strncpy(p, krb5_principal_get_comp_string(context, v5cred->client, 1), len);
1001 memset(&atoken, '\0', sizeof(atoken));
1002 atoken.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
1003 atoken.startTime = v5cred->times.starttime;
1004 atoken.endTime = v5cred->times.endtime;
1005 if (tkt_DeriveDesKey(v5cred->session.keytype,
1006 v5cred->session.keyvalue.data,
1007 v5cred->session.keyvalue.length, &atoken.sessionKey)) {
1008 status = AKLOG_MISC;
1011 atoken.ticketLen = v5cred->ticket.length;
1012 memcpy(atoken.ticket, v5cred->ticket.data, atoken.ticketLen);
1015 strcpy (username, c.pname);
1018 strcat(username, ".");
1019 strcat(username, c.pinst);
1022 atoken.kvno = c.kvno;
1023 atoken.startTime = c.issue_date;
1024 /* ticket lifetime is in five-minutes blocks. */
1025 atoken.endTime = c.issue_date + ((unsigned char)c.lifetime * 5 * 60);
1027 memcpy(&atoken.sessionKey, c.session, 8);
1028 atoken.ticketLen = c.ticket_st.length;
1029 memcpy(atoken.ticket, c.ticket_st.dat, atoken.ticketLen);
1031 status = AKLOG_MISC;
1038 !ktc_GetToken(&aserver, &btoken, sizeof(btoken), &aclient) &&
1039 atoken.kvno == btoken.kvno &&
1040 atoken.ticketLen == btoken.ticketLen &&
1041 !memcmp(&atoken.sessionKey, &btoken.sessionKey, sizeof(atoken.sessionKey)) &&
1042 !memcmp(atoken.ticket, btoken.ticket, atoken.ticketLen))
1045 printf("Identical tokens already exist; skipping.\n");
1046 status = AKLOG_SUCCESS;
1053 printf("Not resolving name %s to id (-noprdb set)\n", username);
1059 if ((status = krb_get_tf_realm(TKT_FILE, realm_of_user)) != KSUCCESS)
1061 fprintf(stderr, "%s: Couldn't determine realm of user: %s)",
1062 progname, krb_err_text(status));
1063 status = AKLOG_KERBEROS;
1067 status = AKLOG_MISC;
1072 /* For Network Identity Manager append the realm to the name */
1073 strcat(username, "@");
1074 strcat(username, realm_of_user);
1076 ViceIDToUsername(username, realm_of_user, realm_of_cell, cell_to_use,
1082 &status, &aclient, &aserver, &atoken);
1086 printf("Set username to %s\n", username);
1088 /* Reset the "aclient" structure before we call ktc_SetToken.
1089 * This structure was first set by the ktc_GetToken call when
1090 * we were comparing whether identical tokens already existed.
1092 strncpy(aclient.name, username, MAXKTCNAMELEN - 1);
1093 strcpy(aclient.instance, "");
1095 if (usev5 && !use524) {
1096 strncpy(aclient.cell,
1097 krb5_principal_get_realm(context, v5cred->client), MAXKTCNAMELEN - 1);
1098 aclient.cell[MAXKTCNAMELEN - 1] = '\0';
1102 strncpy(aclient.cell, c.realm, MAXKTCREALMLEN - 1);
1105 for ( i=0; aclient.cell[i]; i++ ) {
1106 if ( islower(aclient.cell[i]) )
1107 aclient.cell[i] = toupper(aclient.cell[i]);
1112 printf("Setting tokens for %s.\n", smbname);
1114 strncpy(aclient.smbname, smbname, MAXKTCNAMELEN - 1);
1115 aclient.smbname[MAXKTCNAMELEN - 1] = '\0';
1116 flags = AFS_SETTOK_LOGON;
1119 printf("Setting tokens.\n");
1122 if (status = ktc_SetToken(&aserver, &atoken, &aclient, flags))
1124 afs_com_err(progname, status,
1125 "while setting token for cell %s\n",
1127 status = AKLOG_TOKEN;
1131 if (ak_cellconfig.linkedCell && !getLinkedCell) {
1139 * intentionally leak the linkedCell field because it was allocated
1140 * using a different C RTL version.
1142 if (ak_cellconfig.linkedCell)
1143 free(ak_cellconfig.linkedCell);
1148 static int get_afs_mountpoint(char *file, char *mountpoint, int size)
1150 char our_file[MAXPATHLEN + 1];
1152 char *last_component;
1153 struct ViceIoctl vio;
1154 char cellname[BUFSIZ];
1156 memset(our_file, 0, sizeof(our_file));
1157 strcpy(our_file, file);
1159 if (last_component = LastComponent(our_file))
1161 *last_component++ = 0;
1162 parent_dir = our_file;
1166 last_component = our_file;
1170 memset(cellname, 0, sizeof(cellname));
1172 vio.in = last_component;
1173 vio.in_size = (long)strlen(last_component)+1;
1174 vio.out_size = size;
1175 vio.out = mountpoint;
1177 if (!pioctl(parent_dir, VIOC_AFS_STAT_MT_PT, &vio, 0))
1179 if (strchr(mountpoint, VOLMARKER) == NULL)
1182 vio.in_size = (long)strlen(file) + 1;
1183 vio.out_size = sizeof(cellname);
1186 if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1))
1188 strcat(cellname, VOLMARKERSTRING);
1189 strcat(cellname, mountpoint + 1);
1190 memset(mountpoint + 1, 0, size - 1);
1191 strcpy(mountpoint + 1, cellname);
1202 * This routine each time it is called returns the next directory
1203 * down a pathname. It resolves all symbolic links. The first time
1204 * it is called, it should be called with the name of the path
1205 * to be descended. After that, it should be called with the arguemnt
1208 static char *next_path(char *origpath)
1210 static char path[MAXPATHLEN + 1];
1211 static char pathtocheck[MAXPATHLEN + 1];
1213 int link = FALSE; /* Is this a symbolic link? */
1214 char linkbuf[MAXPATHLEN + 1];
1215 char tmpbuf[MAXPATHLEN + 1];
1217 static char *last_comp; /* last component of directory name */
1218 static char *elast_comp; /* End of last component */
1222 static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
1224 /* If we are given something for origpath, we are initializing only. */
1227 memset(path, 0, sizeof(path));
1228 memset(pathtocheck, 0, sizeof(pathtocheck));
1229 CopyPathColon(origpath, path, pathtocheck);
1235 /* We were not given origpath; find then next path to check */
1237 /* If we've gotten all the way through already, return NULL */
1238 if (last_comp == NULL)
1243 while (BeginsWithDir(last_comp, FALSE))
1244 strncat(pathtocheck, last_comp++, 1);
1245 len = (int) ((elast_comp = LastComponent(last_comp))
1246 ? elast_comp - last_comp : strlen(last_comp));
1247 strncat(pathtocheck, last_comp, len);
1248 memset(linkbuf, 0, sizeof(linkbuf));
1249 if (link = (readlink(pathtocheck, linkbuf, sizeof(linkbuf)) > 0))
1251 if (++symlinkcount > MAXSYMLINKS)
1253 fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
1254 akexit(AKLOG_BADPATH);
1256 memset(tmpbuf, 0, sizeof(tmpbuf));
1258 strcpy(tmpbuf, elast_comp);
1259 if (BeginsWithDir(linkbuf, FALSE))
1262 * If this is a symbolic link to an absolute path,
1263 * replace what we have by the absolute path.
1265 memset(path, 0, strlen(path));
1266 memcpy(path, linkbuf, sizeof(linkbuf));
1267 strcat(path, tmpbuf);
1270 memset(pathtocheck, 0, sizeof(pathtocheck));
1275 * If this is a symbolic link to a relative path,
1276 * replace only the last component with the link name.
1278 strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
1279 strcat(path, tmpbuf);
1281 if (t = LastComponent(pathtocheck))
1284 memset(t, 0, strlen(t));
1287 memset(pathtocheck, 0, sizeof(pathtocheck));
1291 last_comp = elast_comp;
1295 return(pathtocheck);
1299 * This routine descends through a path to a directory, logging to
1300 * every cell it encounters along the way.
1302 static int auth_to_path(krb5_context context, char *path)
1304 int status = AKLOG_SUCCESS;
1305 int auth_to_cell_status = AKLOG_SUCCESS;
1308 char pathtocheck[MAXPATHLEN + 1];
1309 char mountpoint[MAXPATHLEN + 1];
1315 if (BeginsWithDir(path, TRUE))
1316 strcpy(pathtocheck, path);
1319 if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL)
1321 fprintf(stderr, "Unable to find current working directory:\n");
1322 fprintf(stderr, "%s\n", pathtocheck);
1323 fprintf(stderr, "Try an absolute pathname.\n");
1324 akexit(AKLOG_BADPATH);
1328 /* in WIN32, if getcwd returns a root dir (eg: c:\), the returned string
1329 * will already have a trailing slash ('\'). Otherwise, the string will
1330 * end in the last directory name */
1332 if(pathtocheck[strlen(pathtocheck) - 1] != BDIR)
1334 strcat(pathtocheck, DIRSTRING);
1335 strcat(pathtocheck, path);
1338 next_path(pathtocheck);
1340 /* Go on to the next level down the path */
1341 while (nextpath = next_path(NULL))
1343 strcpy(pathtocheck, nextpath);
1345 printf("Checking directory [%s]\n", pathtocheck);
1347 * If this is an afs mountpoint, determine what cell from
1348 * the mountpoint name which is of the form
1349 * #cellname:volumename or %cellname:volumename.
1351 if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint)))
1354 printf("Found mount point [%s]\n", mountpoint);
1355 /* skip over the '#' or '%' */
1356 cell = mountpoint + 1;
1357 if (endofcell = strchr(mountpoint, VOLMARKER))
1360 if (auth_to_cell_status = auth_to_cell(context, cell, NULL))
1362 if (status == AKLOG_SUCCESS)
1363 status = auth_to_cell_status;
1364 else if (status != auth_to_cell_status)
1365 status = AKLOG_SOMETHINGSWRONG;
1373 if (lstat(pathtocheck, &st) < 0)
1376 * If we've logged and still can't stat, there's
1379 fprintf(stderr, "%s: stat(%s): %s\n", progname,
1380 pathtocheck, strerror(errno));
1381 return(AKLOG_BADPATH);
1383 else if (!S_ISDIR(st.st_mode))
1385 /* Allow only directories */
1386 fprintf(stderr, "%s: %s: %s\n", progname, pathtocheck,
1388 return(AKLOG_BADPATH);
1396 /* Print usage message and exit */
1397 static void usage(void)
1399 fprintf(stderr, "\nUsage: %s %s%s%s%s\n", progname,
1400 "[-d] [[-cell | -c] cell [-k krb_realm]] ",
1401 "[[-p | -path] pathname]\n",
1402 " [-noprdb] [-force]\n",
1409 fprintf(stderr, " -d gives debugging information.\n");
1410 fprintf(stderr, " krb_realm is the kerberos realm of a cell.\n");
1411 fprintf(stderr, " pathname is the name of a directory to which ");
1412 fprintf(stderr, "you wish to authenticate.\n");
1413 fprintf(stderr, " -noprdb means don't try to determine AFS ID.\n");
1415 fprintf(stderr, " -5 or -4 selects whether to use Kerberos v5 or Kerberos v4.\n"
1416 " (default is Kerberos v5)\n");
1417 fprintf(stderr, " -m means use krb524d to convert Kerberos v5 tickets.\n");
1419 fprintf(stderr, " -5 use Kerberos v5.\n"
1420 " (only Kerberos v5 is available)\n");
1422 fprintf(stderr, " No commandline arguments means ");
1423 fprintf(stderr, "authenticate to the local cell.\n");
1424 fprintf(stderr, "\n");
1425 akexit(AKLOG_USAGE);
1429 validate_krb5_availability(void)
1431 if (!DelayLoadHeimdal()) {
1432 fprintf(stderr, "Kerberos for Windows or Heimdal is not available.\n");
1433 akexit(AKLOG_KFW_NOT_INSTALLED);
1438 validate_krb4_availability(void)
1441 HINSTANCE h = LoadLibrary("krbv4w32.dll");
1445 fprintf(stderr, "Kerberos for Windows library krbv4w32.dll is not available.\n");
1446 akexit(AKLOG_KFW_NOT_INSTALLED);
1449 fprintf(stderr, "Kerberos v4 is not available in this build of aklog.\n");
1450 akexit(AKLOG_USAGE);
1454 int main(int argc, char *argv[])
1456 int status = AKLOG_SUCCESS;
1458 int somethingswrong = FALSE;
1460 cellinfo_t cellinfo;
1462 extern char *progname; /* Name of this program */
1464 extern int dflag; /* Debug mode */
1466 int cmode = FALSE; /* Cellname mode */
1467 int pmode = FALSE; /* Path name mode */
1469 char realm[REALM_SZ]; /* Kerberos realm of afs server */
1470 char cell[BUFSIZ]; /* Cell to which we are authenticating */
1471 char path[MAXPATHLEN + 1]; /* Path length for path mode */
1473 linked_list cells; /* List of cells to log to */
1474 linked_list paths; /* List of paths to log to */
1477 memset(&cellinfo, 0, sizeof(cellinfo));
1479 memset(realm, 0, sizeof(realm));
1480 memset(cell, 0, sizeof(cell));
1481 memset(path, 0, sizeof(path));
1486 /* Store the program name here for error messages */
1487 if (progname = LastComponent(argv[0]))
1492 /* Initialize list of cells to which we have authenticated */
1493 (void)ll_init(&authedcells);
1495 /* Parse commandline arguments and make list of what to do. */
1496 for (i = 1; i < argc; i++)
1498 if (strcmp(argv[i], "-d") == 0)
1500 else if (strcmp(argv[i], "-5") == 0)
1503 else if (strcmp(argv[i], "-m") == 0)
1505 else if (strcmp(argv[i], "-4") == 0)
1508 else if (strcmp(argv[i], "-noprdb") == 0)
1510 else if (strcmp(argv[i], "-force") == 0)
1512 else if (((strcmp(argv[i], "-cell") == 0) ||
1513 (strcmp(argv[i], "-c") == 0)) && !pmode)
1518 strcpy(cell, argv[i]);
1523 else if (((strcmp(argv[i], "-path") == 0) ||
1524 (strcmp(argv[i], "-p") == 0)) && !cmode)
1529 strcpy(path, argv[i]);
1534 else if (argv[i][0] == '-')
1536 else if (!pmode && !cmode)
1538 if (FirstComponent(argv[i]) || (strcmp(argv[i], ".") == 0) ||
1539 (strcmp(argv[i], "..") == 0))
1542 strcpy(path, argv[i]);
1547 strcpy(cell, argv[i]);
1555 if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0))
1559 strcpy(realm, argv[i]);
1563 /* Add this cell to list of cells */
1564 strcpy(cellinfo.cell, cell);
1565 strcpy(cellinfo.realm, realm);
1566 if (cur_node = ll_add_node(&cells, ll_tail))
1569 if (new_cellinfo = copy_cellinfo(&cellinfo))
1570 ll_add_data(cur_node, new_cellinfo);
1573 fprintf(stderr, "%s: failure copying cellinfo.\n", progname);
1579 fprintf(stderr, "%s: failure adding cell to cells list.\n",
1583 memset(&cellinfo, 0, sizeof(cellinfo));
1585 memset(cell, 0, sizeof(cell));
1586 memset(realm, 0, sizeof(realm));
1590 /* Add this path to list of paths */
1591 if (cur_node = ll_add_node(&paths, ll_tail))
1594 if (new_path = strdup(path))
1595 ll_add_data(cur_node, new_path);
1598 fprintf(stderr, "%s: failure copying path name.\n",
1605 fprintf(stderr, "%s: failure adding path to paths list.\n",
1610 memset(path, 0, sizeof(path));
1615 initialize_PT_error_table();
1618 validate_krb5_availability();
1619 if (krb5_init_context(&context))
1620 return(AKLOG_KERBEROS);
1621 if (krb5_enctype_valid(context, ETYPE_DES_CBC_CRC))
1622 krb5_enctype_enable(context, ETYPE_DES_CBC_CRC);
1624 validate_krb4_availability();
1625 afs_set_com_err_hook(redirect_errors);
1627 /* If nothing was given, log to the local cell. */
1628 if ((cells.nelements + paths.nelements) == 0)
1629 status = auth_to_cell(context, NULL, NULL);
1632 /* Log to all cells in the cells list first */
1633 for (cur_node = cells.first; cur_node; cur_node = cur_node->next)
1635 memcpy(&cellinfo, cur_node->data, sizeof(cellinfo));
1636 if (status = auth_to_cell(context,
1637 cellinfo.cell, cellinfo.realm))
1641 /* Then, log to all paths in the paths list */
1642 for (cur_node = paths.first; cur_node; cur_node = cur_node->next)
1644 if (status = auth_to_path(context,
1650 * If only one thing was logged to, we'll return the status
1651 * of the single call. Otherwise, we'll return a generic
1652 * something failed status.
1654 if (somethingswrong && ((cells.nelements + paths.nelements) > 1))
1655 status = AKLOG_SOMETHINGSWRONG;