aklog: Avoid misleading AFSCELL message
[openafs.git] / src / aklog / aklog.c
index 2408547..988d54c 100644 (file)
@@ -1,87 +1,70 @@
-/* 
+/*
  * $Id$
  *
  * Copyright 1990,1991 by the Massachusetts Institute of Technology
  * For distribution and copying rights, see the file "mit-copyright.h"
  */
 /*
- * Copyright (c) 2005, 2006                                     
- * The Linux Box Corporation                                    
- * ALL RIGHTS RESERVED                                          
- *                                                              
- * Permission is granted to use, copy, create derivative works  
- * and redistribute this software and such derivative works     
- * for any purpose, so long as the name of the Linux Box        
- * Corporation is not used in any advertising or publicity      
- * pertaining to the use or distribution of this software       
- * without specific, written prior authorization.  If the       
- * above copyright notice or any other identification of the    
- * Linux Box Corporation is included in any copy of any         
- * portion of this software, then the disclaimer below must     
- * also be included.                                            
- *                                                              
- * This software is provided as is, without representation      
- * from the Linux Box Corporation as to its fitness for any     
- * purpose, and without warranty by the Linux Box Corporation   
- * of any kind, either express or implied, including            
- * without limitation the implied warranties of                 
- * merchantability and fitness for a particular purpose.  The   
- * regents of the Linux Box Corporation shall not be liable     
- * for any damages, including special, indirect, incidental, or 
- * consequential damages, with respect to any claim arising     
- * out of or in connection with the use of the software, even   
- * if it has been or is hereafter advised of the possibility of 
- * such damages.                                                
+ * Copyright (c) 2005, 2006
+ * The Linux Box Corporation
+ * ALL RIGHTS RESERVED
+ *
+ * Permission is granted to use, copy, create derivative works
+ * and redistribute this software and such derivative works
+ * for any purpose, so long as the name of the Linux Box
+ * Corporation is not used in any advertising or publicity
+ * pertaining to the use or distribution of this software
+ * without specific, written prior authorization.  If the
+ * above copyright notice or any other identification of the
+ * Linux Box Corporation is included in any copy of any
+ * portion of this software, then the disclaimer below must
+ * also be included.
+ *
+ * This software is provided as is, without representation
+ * from the Linux Box Corporation as to its fitness for any
+ * purpose, and without warranty by the Linux Box Corporation
+ * of any kind, either express or implied, including
+ * without limitation the implied warranties of
+ * merchantability and fitness for a particular purpose.  The
+ * regents of the Linux Box Corporation shall not be liable
+ * for any damages, including special, indirect, incidental, or
+ * consequential damages, with respect to any claim arising
+ * out of or in connection with the use of the software, even
+ * if it has been or is hereafter advised of the possibility of
+ * such damages.
  */
 
 #include <afsconfig.h>
+#include <afs/param.h>
+#include <afs/stds.h>
 
-#include <stdio.h>
-#include <string.h>
-#include <ctype.h>
-#include <sys/types.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#ifdef HAVE_STDLIB_H
-#include <stdlib.h>
-#endif
-#ifdef HAVE_MEMORY_H
-#include <memory.h>
-#endif
-#include <errno.h>
+#include <roken.h>
 
-#include <sys/stat.h>
-#include <fcntl.h>
+#include <ctype.h>
 
-#include <sys/param.h>
-#include <sys/errno.h>
-#include <netdb.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <pwd.h>
+#include <afs/ktc.h>
+#include <afs/token.h>
 
-#include <afs/stds.h>
+#define KERBEROS_APPLE_DEPRECATED(x)
 #include <krb5.h>
-#if defined(HAVE_ET_COM_ERR_H)
-#include <et/com_err.h>
+#ifdef HAVE_COM_ERR_H
+# include <com_err.h>
+#elif HAVE_ET_COM_ERR_H
+# include <et/com_err.h>
+#elif HAVE_KRB5_COM_ERR_H
+# include <krb5/com_err.h>
 #else
-#include <com_err.h>
+# error No com_err.h? We need some kind of com_err.h
 #endif
 
 #ifndef HAVE_KERBEROSV_HEIM_ERR_H
 #include <afs/com_err.h>
 #endif
 
-#include <afs/param.h>
 #ifdef AFS_SUN5_ENV
 #include <sys/ioccom.h>
 #endif
 
-/* Prevent inclusion of des.h to avoid conflicts with des types */
-#define NO_DES_H_INCLUDE
-
 #include <afs/auth.h>
 #include <afs/cellconfig.h>
 #include <afs/vice.h>
 #define AKLOG_BADPATH 6
 #define AKLOG_MISC 7
 
-#ifndef NULL
-#define NULL 0
-#endif
-
 #ifndef TRUE
 #define TRUE 1
 #endif
@@ -188,7 +167,7 @@ static int get_user_realm(krb5_context, char **);
 #error "Must have either krb5_princ_size or krb5_principal_get_comp_string"
 #endif
 
-#if !defined(HAVE_KRB5_ENCRYPT_TKT_PART) && defined(HAVE_ENCODE_KRB5_ENC_TKT_PART) && defined(HAVE_KRB5_C_ENCRYPT) 
+#if !defined(HAVE_KRB5_ENCRYPT_TKT_PART) && defined(HAVE_ENCODE_KRB5_ENC_TKT_PART) && defined(HAVE_KRB5_C_ENCRYPT)
 extern krb5_error_code encode_krb5_enc_tkt_part (const krb5_enc_tkt_part *rep,
                                                 krb5_data **code);
 
@@ -200,7 +179,7 @@ krb5_encrypt_tkt_part(krb5_context context,
     krb5_data *data = 0;
     int code;
     size_t enclen;
-    
+
     if ((code = encode_krb5_enc_tkt_part(ticket->enc_part2, &data)))
        goto Done;
     if ((code = krb5_c_encrypt_length(context, key->enctype,
@@ -242,10 +221,11 @@ Done:
 #error "Must have either keyblock or session member of krb5_creds"
 #endif
 
-#if !defined(HAVE_KRB5_524_CONVERT_CREDS) && defined(HAVE_KRB524_CONVERT_CREDS_KDC)
-#define krb5_524_convert_creds krb524_convert_creds_kdc
-#elif !defined(HAVE_KRB5_524_CONVERT_CREDS) && !defined(HAVE_KRB524_CONVERT_CREDS_KDC)
+/* MITKerberosShim logs but returns success */
+#if !defined(HAVE_KRB5_524_CONV_PRINCIPAL) || defined(AFS_DARWIN110_ENV) || (!defined(HAVE_KRB5_524_CONVERT_CREDS) && !defined(HAVE_KRB524_CONVERT_CREDS_KDC))
 #define HAVE_NO_KRB5_524
+#elif !defined(HAVE_KRB5_524_CONVERT_CREDS) && defined(HAVE_KRB524_CONVERT_CREDS_KDC)
+#define krb5_524_convert_creds krb524_convert_creds_kdc
 #endif
 
 #if USING_HEIMDAL
@@ -335,8 +315,14 @@ redirect_errors(const char *who, afs_int32 code, const char *fmt, va_list ap)
        if (strncmp(str, "unknown", strlen("unknown")) == 0) {
 #ifdef HAVE_KRB5_SVC_GET_MSG
            krb5_svc_get_msg(code,&str);
+#elif defined(HAVE_KRB5_GET_ERROR_MESSAGE)
+           krb5_context context;
+           if (krb5_init_context(&context) == 0) {
+                str = krb5_get_error_message(context, code);
+                krb5_free_context(context);
+            }
 #else
-           str = error_message(code);
+           ; /* IRIX apparently has neither: use the string we have */
 #endif
        }
        fputs(str, stderr);
@@ -367,15 +353,16 @@ copy_cellinfo(cellinfo_t *cellinfo)
 {
     cellinfo_t *new_cellinfo;
 
-    if ((new_cellinfo = (cellinfo_t *)malloc(sizeof(cellinfo_t))))
+    if ((new_cellinfo = malloc(sizeof(cellinfo_t))))
        memcpy(new_cellinfo, cellinfo, sizeof(cellinfo_t));
-    
+
     return ((char *)new_cellinfo);
 }
 
 
 static int
-get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char **local_cell)
+get_cellconfig(const char *config, char *cell,
+              struct afsconf_cell *cellconfig, char **local_cell)
 {
     int status = AKLOG_SUCCESS;
     struct afsconf_dir *configdir;
@@ -389,26 +376,31 @@ get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char **local_cell)
        exit(AKLOG_AFS);
     }
 
-    if (!(configdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
-       fprintf(stderr, 
+    if (!(configdir = afsconf_Open(config))) {
+       fprintf(stderr,
                "%s: can't get afs configuration (afsconf_Open(%s))\n",
-               progname, AFSDIR_CLIENT_ETC_DIRPATH);
+               progname, config);
        exit(AKLOG_AFS);
     }
 
-    if (afsconf_GetLocalCell(configdir, *local_cell, MAXCELLCHARS)) {
-       fprintf(stderr, "%s: can't determine local cell.\n", progname);
-       exit(AKLOG_AFS);
+    if (cell != NULL && cell[0] == '\0') {
+       /* Use the local cell */
+       cell = NULL;
     }
 
-    if ((cell == NULL) || (cell[0] == 0))
-       cell = *local_cell;
-
     /* XXX - This function modifies 'cell' by passing it through lcstring */
     if (afsconf_GetCellInfo(configdir, cell, NULL, cellconfig)) {
-       fprintf(stderr, "%s: Can't get information about cell %s.\n",
-               progname, cell);
+       if (cell != NULL) {
+           fprintf(stderr, "%s: Can't get information about cell %s.\n",
+                   progname, cell);
+       } else {
+           fprintf(stderr, "%s: Can't get information about the local cell.\n",
+                   progname);
+       }
        status = AKLOG_AFS;
+    } else if (afsconf_GetLocalCell(configdir, *local_cell, MAXCELLCHARS)) {
+       fprintf(stderr, "%s: can't determine local cell.\n", progname);
+       exit(AKLOG_AFS);
     }
 
     afsconf_Close(configdir);
@@ -583,9 +575,9 @@ rxkad_get_ticket(krb5_context context, char *realm,
                    }
                    afs_dprintf("We've deduced that we need to authenticate"
                            " to realm %s.\n", realm_of_cell);
+                   status = get_credv5(context, AFSKEY, cell->name,
+                                       realm_of_cell, v5cred);
                }
-               status = get_credv5(context, AFSKEY, cell->name,
-                                   realm_of_cell, v5cred);
            }
 
            /* If the realm and cell name match, then try without an
@@ -617,6 +609,11 @@ rxkad_get_ticket(krb5_context context, char *realm,
        fprintf(stderr, "%s: Couldn't get %s AFS tickets:\n",
                progname, cell->name);
        afs_com_err(progname, status, "while getting AFS tickets");
+#ifdef KRB5_CC_NOT_KTYPE
+       if (status == KRB5_CC_NOT_KTYPE) {
+           fprintf(stderr, "allow_weak_crypto may be required in the Kerberos configuration\n");
+       }
+#endif
        status = AKLOG_KERBEROS;
        goto out;
     }
@@ -664,25 +661,28 @@ out:
  *     structure which should be freed by the caller.
  * @param[out[ userPtr
  *     A string containing the principal of the user to whom the token was
- *     issued. This is a malloc'd block which should be freed by the caller.
+ *     issued. This is a malloc'd block which should be freed by the caller,
+ *      if set.
  *
  * @returns
  *     0 on success, an error value upon failure
  */
 static int
 rxkad_build_native_token(krb5_context context, krb5_creds *v5cred,
-                        struct ktc_token **tokenPtr, char **userPtr) {
-    char username[BUFSIZ];
-    struct ktc_token *token;
+                        struct ktc_tokenUnion **tokenPtr, char **userPtr) {
+    char username[BUFSIZ]="";
+    struct ktc_token token;
+    int status;
 #ifdef HAVE_NO_KRB5_524
     char *p;
     int len;
 #else
-    int status;
     char k4name[ANAME_SZ];
     char k4inst[INST_SZ];
     char k4realm[REALM_SZ];
 #endif
+    void *inkey = get_cred_keydata(v5cred);
+    size_t inkey_sz = get_cred_keylen(v5cred);
 
     afs_dprintf("Using Kerberos V5 ticket natively\n");
 
@@ -695,14 +695,15 @@ rxkad_build_native_token(krb5_context context, krb5_creds *v5cred,
                                      (char *) &k4inst,
                                      (char *) &k4realm);
     if (status) {
-       afs_com_err(progname, status, "while converting principal "
-                   "to Kerberos V4 format");
-       return AKLOG_KERBEROS;
-    }
-    strcpy (username, k4name);
-    if (k4inst[0]) {
-       strcat (username, ".");
-       strcat (username, k4inst);
+       if (!noprdb)
+           afs_com_err(progname, status,
+                       "while converting principal to Kerberos V4 format");
+    } else {
+       strcpy (username, k4name);
+       if (k4inst[0]) {
+           strcat (username, ".");
+           strcat (username, k4inst);
+       }
     }
 #else
     len = min(get_princ_len(context, v5cred->client, 0),
@@ -721,22 +722,25 @@ rxkad_build_native_token(krb5_context context, krb5_creds *v5cred,
     }
 #endif
 
-    token = malloc(sizeof(struct ktc_token));
-    if (token == NULL)
-       return ENOMEM;
+    memset(&token, 0, sizeof(struct ktc_token));
 
-    memset(token, 0, sizeof(struct ktc_token));
+    token.kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
+    token.startTime = v5cred->times.starttime;;
+    token.endTime = v5cred->times.endtime;
+    if (tkt_DeriveDesKey(get_creds_enctype(v5cred), inkey, inkey_sz,
+                        &token.sessionKey) != 0) {
+       return RXKADBADKEY;
+    }
+    token.ticketLen = v5cred->ticket.length;
+    memcpy(token.ticket, v5cred->ticket.data, token.ticketLen);
 
-    token->kvno = RXKAD_TKT_TYPE_KERBEROS_V5;
-    token->startTime = v5cred->times.starttime;;
-    token->endTime = v5cred->times.endtime;
-    memcpy(&token->sessionKey, get_cred_keydata(v5cred),
-          get_cred_keylen(v5cred));
-    token->ticketLen = v5cred->ticket.length;
-    memcpy(token->ticket, v5cred->ticket.data, token->ticketLen);
+    status = token_importRxkadViceId(tokenPtr, &token, 0);
+    if (status) {
+       return status;
+    }
 
-    *tokenPtr = token;
-    *userPtr = strdup(username);
+    if (username[0] != '\0')
+       *userPtr = strdup(username);
 
     return 0;
 }
@@ -756,7 +760,8 @@ rxkad_build_native_token(krb5_context context, krb5_creds *v5cred,
  *     structure which should be freed by the caller.
  * @param[out[ userPtr
  *     A string containing the principal of the user to whom the token was
- *     issued. This is a malloc'd block which should be freed by the caller.
+ *     issued. This is a malloc'd block which should be freed by the caller,
+ *      if set.
  *
  * @returns
  *     0 on success, an error value upon failure
@@ -765,7 +770,7 @@ rxkad_build_native_token(krb5_context context, krb5_creds *v5cred,
 #ifdef HAVE_NO_KRB5_524
 static int
 rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
-                         struct ktc_token **tokenPtr, char **userPtr) {
+                         struct ktc_tokenUnion **tokenPtr, char **userPtr) {
     *tokenPtr = NULL;
     *userPtr = NULL;
 
@@ -774,10 +779,10 @@ rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
 #else
 static int
 rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
-                         struct ktc_token **tokenPtr, char **userPtr) {
+                         struct ktc_tokenUnion **tokenPtr, char **userPtr) {
     CREDENTIALS cred;
     char username[BUFSIZ];
-    struct ktc_token *token;
+    struct ktc_token token;
     int status;
 
     *tokenPtr = NULL;
@@ -799,11 +804,10 @@ rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
        strcat (username, cred.pinst);
     }
 
-    token = malloc(sizeof(struct ktc_token));
-    memset(token, 0, sizeof(struct ktc_token));
+    memset(&token, 0, sizeof(struct ktc_token));
 
-    token->kvno = cred.kvno;
-    token->startTime = cred.issue_date;
+    token.kvno = cred.kvno;
+    token.startTime = cred.issue_date;
     /*
      * It seems silly to go through a bunch of contortions to
      * extract the expiration time, when the v5 credentials already
@@ -812,10 +816,17 @@ rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
      * Note that this isn't a security hole, as the expiration time
      * is also contained in the encrypted token
      */
-    token->endTime = v5cred->times.endtime;
-    memcpy(&token->sessionKey, cred.session, 8);
-    token->ticketLen = cred.ticket_st.length;
-    memcpy(token->ticket, cred.ticket_st.dat, token->ticketLen);
+    token.endTime = v5cred->times.endtime;
+    memcpy(&token.sessionKey, cred.session, 8);
+    token.ticketLen = cred.ticket_st.length;
+    memcpy(token.ticket, cred.ticket_st.dat, token.ticketLen);
+
+    status = token_importRxkadViceId(tokenPtr, &token, 0);
+    if (status) {
+       return status;
+    }
+
+    *userPtr = strdup(username);
 
     return 0;
 }
@@ -837,7 +848,8 @@ rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
  *     be freed by the caller.
  * @parma[out] authuser
  *     A string containing the principal of the user to whom the token was
- *     issued. This is a malloc'd block which should be freed by the caller.
+ *     issued. This is a malloc'd block which should be freed by the caller,
+ *      if set.
  * @param[out] foreign
  *     Whether the user is considered as 'foreign' to the realm of the cell.
  *
@@ -846,12 +858,11 @@ rxkad_get_converted_token(krb5_context context, krb5_creds *v5cred,
  */
 static int
 rxkad_get_token(krb5_context context, struct afsconf_cell *cell, char *realm,
-               struct ktc_token **token, char **authuser, int *foreign) {
+               struct ktc_tokenUnion **token, char **authuser, int *foreign) {
     krb5_creds *v5cred;
     char *realmUsed = NULL;
     char *username = NULL;
     int status;
-    size_t len;
 
     *token = NULL;
     *authuser = NULL;
@@ -871,14 +882,17 @@ rxkad_get_token(krb5_context context, struct afsconf_cell *cell, char *realm,
 
     /* We now have the username, plus the realm name, so stitch them together
      * to give us the name that the ptserver will know the user by */
-    if (realmUsed == NULL) {
+    if (realmUsed == NULL || username == NULL) {
        *authuser = username;
        username = NULL;
        *foreign = 0;
     } else {
-       len = strlen(username)+strlen(realmUsed)+2;
-       *authuser = malloc(len);
-       afs_snprintf(*authuser, len, "%s@%s", username, realmUsed);
+       if (asprintf(authuser, "%s@%s", username, realmUsed) < 0) {
+           fprintf(stderr, "%s: Out of memory building PTS name\n", progname);
+           *authuser = NULL;
+           status = AKLOG_MISC;
+           goto out;
+       }
        *foreign = 1;
     }
 
@@ -891,68 +905,14 @@ out:
     return status;
 }
 
-static int
-get_kernel_token(struct afsconf_cell *cell, struct ktc_token **tokenPtr) {
-    struct ktc_principal client, server;
-    struct ktc_token *token;
-    int ret;
-
-    *tokenPtr = NULL;
-
-    strncpy(server.name, AFSKEY, MAXKTCNAMELEN - 1);
-    strncpy(server.instance, AFSINST, MAXKTCNAMELEN - 1);
-    strncpy(server.cell, cell->name, MAXKTCREALMLEN - 1);
-
-    token = malloc(sizeof(struct ktc_token));
-    if (token == NULL)
-       return ENOMEM;
-
-    memset(token, 0, sizeof(struct ktc_token));
-
-    ret = ktc_GetToken(&server, token, sizeof(struct ktc_token), &client);
-    if (ret) {
-       free(token);
-       return ret;
-    }
-
-    *tokenPtr = token;
-    return 0;
-}
-
-static int
-set_kernel_token(struct afsconf_cell *cell, char *username,
-                struct ktc_token *token, int setpag)
-{
-    struct ktc_principal client, server;
-
-    strncpy(client.name, username, MAXKTCNAMELEN - 1);
-    strcpy(client.instance, "");
-    strncpy(client.cell, cell->name, MAXKTCREALMLEN - 1);
-
-    strncpy(server.name, AFSKEY, MAXKTCNAMELEN - 1);
-    strncpy(server.instance, AFSINST, MAXKTCNAMELEN - 1);
-    strncpy(server.cell, cell->name, MAXKTCREALMLEN - 1);
-
-    return ktc_SetToken(&server, token, &client, setpag);
-}
-
-static int
-tokens_equal(struct ktc_token *tokenA, struct ktc_token *tokenB) {
-    return (tokenA != NULL && tokenB != NULL &&
-           tokenA->kvno == tokenB->kvno &&
-           tokenA->ticketLen == tokenB->ticketLen &&
-           !memcmp(&tokenA->sessionKey, &tokenB->sessionKey,
-                   sizeof(tokenA->sessionKey)) &&
-           !memcmp(tokenA->ticket, tokenB->ticket, tokenA->ticketLen));
-}
-
 /* 
  * Log to a cell.  If the cell has already been logged to, return without
  * doing anything.  Otherwise, log to it and mark that it has been logged
  * to.
  */
 static int
-auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
+auth_to_cell(krb5_context context, const char *config, 
+            char *cell, char *realm, char **linkedcell)
 {
     int status = AKLOG_SUCCESS;
     int isForeign = 0;
@@ -960,19 +920,24 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
     afs_int32 viceId;          /* AFS uid of user */
 
     char *local_cell = NULL;
-    struct ktc_token *token;
-    struct ktc_token *btoken;
+    struct ktc_tokenUnion *rxkadToken = NULL;
+    struct ktc_setTokenData *token;
+    struct ktc_setTokenData *btoken = NULL;
     struct afsconf_cell cellconf;
 
     /* NULL or empty cell returns information on local cell */
-    if ((status = get_cellconfig(cell, &cellconf, &local_cell)))
+    if ((status = get_cellconfig(config, cell, &cellconf, &local_cell)))
        return(status);
 
-    if (linkedcell != NULL && cellconf.linkedCell != NULL) {
-       *linkedcell = strdup(cellconf.linkedCell);
-       if (*linkedcell == NULL) {
-           status = ENOMEM;
-           goto out;
+    if (linkedcell != NULL) {
+       if (cellconf.linkedCell != NULL) {
+           *linkedcell = strdup(cellconf.linkedCell);
+           if (*linkedcell == NULL) {
+               status = ENOMEM;
+               goto out;
+           }
+       } else {
+           *linkedcell = NULL;
        }
     }
 
@@ -982,14 +947,14 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
        goto out;
     }
 
-    /* 
+    /*
      * Record that we have attempted to log to this cell.  We do this
      * before we try rather than after so that we will not try
      * and fail repeatedly for one cell.
      */
     ll_string(&authedcells, ll_s_add, cellconf.name);
 
-    /* 
+    /*
      * Record this cell in the list of zephyr subscriptions.  We may
      * want zephyr subscriptions even if authentication fails.
      * If this is done after we attempt to get tokens, aklog -zsubs
@@ -997,13 +962,13 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
      * are in -noauth mode.
      */
     if (ll_string(&zsublist, ll_s_add, cellconf.name) == LL_FAILURE) {
-       fprintf(stderr, 
+       fprintf(stderr,
                "%s: failure adding cell %s to zephyr subscriptions list.\n",
                progname, cellconf.name);
        exit(AKLOG_MISC);
     }
     if (ll_string(&zsublist, ll_s_add, local_cell) == LL_FAILURE) {
-       fprintf(stderr, 
+       fprintf(stderr,
                "%s: failure adding cell %s to zephyr subscriptions list.\n",
                progname, local_cell);
        exit(AKLOG_MISC);
@@ -1013,25 +978,46 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
        afs_dprintf("Authenticating to cell %s (server %s).\n", cellconf.name,
                cellconf.hostName[0]);
 
-       status = rxkad_get_token(context, &cellconf, realm, &token,
+       token = token_buildTokenJar(cellconf.name);
+       if (token == NULL) {
+           status = ENOMEM;
+           goto out;
+       }
+
+       status = rxkad_get_token(context, &cellconf, realm, &rxkadToken,
                                 &username, &isForeign);
        if (status)
-           return status;
+           goto out;
 
+       /* We need to keep the token structure around so that we can stick
+        * the viceId into it (once we know it) */
+       status = token_addToken(token, rxkadToken);
+       if (status) {
+           afs_dprintf("Add Token failed with %d", status);
+           goto out;
+       }
 
        if (!force &&
-           !get_kernel_token(&cellconf, &btoken) &&
-           tokens_equal(token, btoken)) {
+           ktc_GetTokenEx(cellconf.name, &btoken) == 0 &&
+           token_SetsEquivalent(token, btoken)) {
+
+           token_FreeSet(&btoken);
            afs_dprintf("Identical tokens already exist; skipping.\n");
            status = AKLOG_SUCCESS;
            goto out;
        }
 
+       if (btoken)
+           token_FreeSet(&btoken);
+
 #ifdef FORCE_NOPRDB
        noprdb = 1;
 #endif
 
-       if (noprdb) {
+       if (username == NULL) {
+           afs_dprintf("Not resolving name to id\n");
+       }
+       else if (noprdb) {
            afs_dprintf("Not resolving name %s to id (-noprdb set)\n", username);
        }
        else {
@@ -1045,7 +1031,7 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
                afs_dprintf("Error %d\n", status);
            else
                afs_dprintf("Id %d\n", (int) viceId);
-           
+
 
            /*
             * This code is taken from cklog -- it lets people
@@ -1058,7 +1044,7 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
                        username, cellconf.name);
                viceId = 0;
 
-               status = set_kernel_token(&cellconf, username, token, 0);
+               status = ktc_SetTokenEx(token);
                if (status) {
                    afs_com_err(progname, status,
                                "while obtaining tokens for cell %s",
@@ -1069,7 +1055,7 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
                /*
                 * In case you're wondering, we don't need to change the
                 * filename here because we're still connecting to the
-                * same cell -- we're just using a different authentication
+                * same cell -- we're just using a different authenticat ion
                 * level
                 */
 
@@ -1091,40 +1077,40 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
            }
 #endif /* ALLOW_REGISTER */
 
-           /*
-            * This is a crock, but it is Transarc's crock, so we have to play
-            * along in order to get the functionality.  The way the afs id is
-            * stored is as a string in the username field of the token.
-            * Contrary to what you may think by looking at the code for
-            * tokens, this hack (AFS ID %d) will not work if you change %d
-            * to something else.
-            */
-
            if ((status == 0) && (viceId != ANONYMOUSID)) {
-               free(username);
-               if (afs_asprintf(&username, "AFS ID %d", (int) viceId) < 0) {
-                   status = ENOMEM;
-                   username = NULL;
-                   goto out;
+               status = token_setRxkadViceId(rxkadToken, viceId);
+               if (status) {
+                   fprintf(stderr, "Error %d setting rxkad ViceId\n", status);
+                   status = AKLOG_SUCCESS;
+               } else {
+                   token_replaceToken(token, rxkadToken);
                }
            }
        }
 
-       afs_dprintf("Set username to %s\n", username);
-
-       afs_dprintf("Setting tokens. %s @ %s \n", username, cellconf.name);
+       if (username) {
+           afs_dprintf("Setting tokens. %s @ %s\n",
+                       username, cellconf.name);
+       } else {
+           afs_dprintf("Setting tokens for cell %s\n", cellconf.name);
+       }
 
 #ifndef AFS_AIX51_ENV
        /* on AIX 4.1.4 with AFS 3.4a+ if a write is not done before
-        * this routine, it will not add the token. It is not clear what 
+        * this routine, it will not add the token. It is not clear what
         * is going on here! So we will do the following operation.
         * On AIX 5, it causes the parent program to die, so we won't.
+        * We don't care about the return value, but need to collect it
+        * to avoid compiler warnings.
         */
-       write(2,"",0); /* dummy write */
+       if (write(2,"",0) < 0) {
+           /* dummy write, don't care */
+       }
 #endif
-       status = set_kernel_token(&cellconf, username, token, afssetpag);
+       token_setPag(token, afssetpag);
+       status = ktc_SetTokenEx(token);
        if (status) {
-           afs_com_err(progname, status, "while obtaining tokens for cell %s",
+           afs_com_err(progname, status, "while setting tokens for cell %s",
                        cellconf.name);
            status = AKLOG_TOKEN;
        }
@@ -1133,6 +1119,10 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
        afs_dprintf("Noauth mode; not authenticating.\n");
 
 out:
+    if (rxkadToken) {
+       token_freeToken(&rxkadToken);
+    }
+
     if (local_cell)
        free(local_cell);
     if (username)
@@ -1153,8 +1143,7 @@ get_afs_mountpoint(char *file, char *mountpoint, int size)
     struct ViceIoctl vio;
     char cellname[BUFSIZ];
 
-    memset(our_file, 0, sizeof(our_file));
-    strcpy(our_file, file);
+    strlcpy(our_file, file, sizeof(our_file));
 
     if ((last_component = strrchr(our_file, DIR))) {
        *last_component++ = 0;
@@ -1163,8 +1152,8 @@ get_afs_mountpoint(char *file, char *mountpoint, int size)
     else {
        last_component = our_file;
        parent_dir = ".";
-    }    
-    
+    }
+
     memset(cellname, 0, sizeof(cellname));
 
     vio.in = last_component;
@@ -1178,10 +1167,10 @@ get_afs_mountpoint(char *file, char *mountpoint, int size)
            vio.in_size = strlen(file) + 1;
            vio.out_size = sizeof(cellname);
            vio.out = cellname;
-           
+
            if (!pioctl(file, VIOC_FILE_CELL_NAME, &vio, 1)) {
-               strcat(cellname, VOLMARKERSTRING);
-               strcat(cellname, mountpoint + 1);
+               strlcat(cellname, VOLMARKERSTRING, sizeof(cellname));
+               strlcat(cellname, mountpoint + 1, sizeof(cellname));
                memset(mountpoint + 1, 0, size - 1);
                strcpy(mountpoint + 1, cellname);
            }
@@ -1192,8 +1181,8 @@ get_afs_mountpoint(char *file, char *mountpoint, int size)
        return(FALSE);
 }
 
-/* 
- * This routine each time it is called returns the next directory 
+/*
+ * This routine each time it is called returns the next directory
  * down a pathname.  It resolves all symbolic links.  The first time
  * it is called, it should be called with the name of the path
  * to be descended.  After that, it should be called with the arguemnt
@@ -1205,7 +1194,7 @@ next_path(char *origpath)
     static char path[MAXPATHLEN + 1];
     static char pathtocheck[MAXPATHLEN + 1];
 
-    int link = FALSE;          /* Is this a symbolic link? */
+    ssize_t link;              /* Return value from readlink */
     char linkbuf[MAXPATHLEN + 1];
     char tmpbuf[MAXPATHLEN + 1];
 
@@ -1213,21 +1202,21 @@ next_path(char *origpath)
     static char *elast_comp;   /* End of last component */
     char *t;
     int len;
-    
+
     static int symlinkcount = 0; /* We can't exceed MAXSYMLINKS */
-    
+
     /* If we are given something for origpath, we are initializing only. */
     if (origpath) {
        memset(path, 0, sizeof(path));
        memset(pathtocheck, 0, sizeof(pathtocheck));
-       strcpy(path, origpath);
+       strlcpy(path, origpath, sizeof(path));
        last_comp = path;
        symlinkcount = 0;
        return(NULL);
     }
 
     /* We were not given origpath; find then next path to check */
-    
+
     /* If we've gotten all the way through already, return NULL */
     if (last_comp == NULL)
        return(NULL);
@@ -1235,22 +1224,26 @@ next_path(char *origpath)
     do {
        while (*last_comp == DIR)
            strncat(pathtocheck, last_comp++, 1);
-       len = (elast_comp = strchr(last_comp, DIR)) 
+       len = (elast_comp = strchr(last_comp, DIR))
            ? elast_comp - last_comp : strlen(last_comp);
        strncat(pathtocheck, last_comp, len);
        memset(linkbuf, 0, sizeof(linkbuf));
-       if ((link = (readlink(pathtocheck, linkbuf, 
-                                   sizeof(linkbuf)) > 0))) {
+       link = readlink(pathtocheck, linkbuf, sizeof(linkbuf)-1);
+
+       if (link > 0) {
+           linkbuf[link] = '\0'; /* NUL terminate string */
+
            if (++symlinkcount > MAXSYMLINKS) {
                fprintf(stderr, "%s: %s\n", progname, strerror(ELOOP));
                exit(AKLOG_BADPATH);
            }
+
            memset(tmpbuf, 0, sizeof(tmpbuf));
            if (elast_comp)
-               strcpy(tmpbuf, elast_comp);
+               strlcpy(tmpbuf, elast_comp, sizeof(tmpbuf));
            if (linkbuf[0] == DIR) {
-               /* 
-                * If this is a symbolic link to an absolute path, 
+               /*
+                * If this is a symbolic link to an absolute path,
                 * replace what we have by the absolute path.
                 */
                memset(path, 0, strlen(path));
@@ -1261,8 +1254,8 @@ next_path(char *origpath)
                memset(pathtocheck, 0, sizeof(pathtocheck));
            }
            else {
-               /* 
-                * If this is a symbolic link to a relative path, 
+               /*
+                * If this is a symbolic link to a relative path,
                 * replace only the last component with the link name.
                 */
                strncpy(last_comp, linkbuf, strlen(linkbuf) + 1);
@@ -1279,7 +1272,7 @@ next_path(char *origpath)
        else
            last_comp = elast_comp;
     }
-    while(link);
+    while(link > 0);
 
     return(pathtocheck);
 }
@@ -1296,7 +1289,7 @@ add_hosts(char *file)
     int i;
     struct hostent *hp;
     struct in_addr in;
-    
+
     memset(outbuf, 0, sizeof(outbuf));
 
     vio.out_size = sizeof(outbuf);
@@ -1318,7 +1311,7 @@ add_hosts(char *file)
         * If it is replicated on many servers, then none are included.
         * This is not perfect, but the result is that people don't
         * get subscribed to a lot of instances of FILSRV that they
-        * probably won't need which reduces the instances of 
+        * probably won't need which reduces the instances of
         * people getting messages that don't apply to them.
         */
 #ifndef ALLHOSTS
@@ -1340,11 +1333,11 @@ add_hosts(char *file)
 }
 
 /*
- * This routine descends through a path to a directory, logging to 
+ * This routine descends through a path to a directory, logging to
  * every cell it encounters along the way.
  */
 static int
-auth_to_path(krb5_context context, char *path)
+auth_to_path(krb5_context context, const char *config, char *path)
 {
     int status = AKLOG_SUCCESS;
     int auth_status = AKLOG_SUCCESS;
@@ -1360,7 +1353,7 @@ auth_to_path(krb5_context context, char *path)
 
     /* Initialize */
     if (path[0] == DIR)
-       strcpy(pathtocheck, path);
+       strlcpy(pathtocheck, path, sizeof(pathtocheck));
     else {
        if (getcwd(pathtocheck, sizeof(pathtocheck)) == NULL) {
            fprintf(stderr, "Unable to find current working directory:\n");
@@ -1369,19 +1362,19 @@ auth_to_path(krb5_context context, char *path)
            exit(AKLOG_BADPATH);
        }
        else {
-           strcat(pathtocheck, DIRSTRING);
-           strcat(pathtocheck, path);
+           strlcat(pathtocheck, DIRSTRING, sizeof(pathtocheck));
+           strlcat(pathtocheck, path, sizeof(pathtocheck));
        }
     }
     next_path(pathtocheck);
 
     /* Go on to the next level down the path */
     while ((nextpath = next_path(NULL))) {
-       strcpy(pathtocheck, nextpath);
+       strlcpy(pathtocheck, nextpath, sizeof(pathtocheck));
        afs_dprintf("Checking directory %s\n", pathtocheck);
-       /* 
-        * If this is an afs mountpoint, determine what cell from 
-        * the mountpoint name which is of the form 
+       /*
+        * If this is an afs mountpoint, determine what cell from
+        * the mountpoint name which is of the form
         * #cellname:volumename or %cellname:volumename.
         */
        if (get_afs_mountpoint(pathtocheck, mountpoint, sizeof(mountpoint))) {
@@ -1394,7 +1387,8 @@ auth_to_path(krb5_context context, char *path)
                add_hosts(pathtocheck);
            if ((endofcell = strchr(mountpoint, VOLMARKER))) {
                *endofcell = '\0';
-               if ((auth_status = auth_to_cell(context, cell, NULL, NULL))) {
+               auth_status = auth_to_cell(context, config, cell, NULL, NULL);
+               if (auth_status) {
                    if (status == AKLOG_SUCCESS)
                        status = auth_status;
                    else if (status != auth_status)
@@ -1406,9 +1400,9 @@ auth_to_path(krb5_context context, char *path)
            if (isdir(pathtocheck, &isdirectory) < 0) {
                /*
                 * If we've logged and still can't stat, there's
-                * a problem... 
+                * a problem...
                 */
-               fprintf(stderr, "%s: stat(%s): %s\n", progname, 
+               fprintf(stderr, "%s: stat(%s): %s\n", progname,
                        pathtocheck, strerror(errno));
                return(AKLOG_BADPATH);
            }
@@ -1420,7 +1414,7 @@ auth_to_path(krb5_context context, char *path)
            }
        }
     }
-    
+
 
     return(status);
 }
@@ -1482,6 +1476,7 @@ main(int argc, char *argv[])
     linked_list paths;         /* List of paths to log to */
     ll_node *cur_node;
     char *linkedcell;
+    const char *config = AFSDIR_CLIENT_ETC_DIRPATH;
 
     memset(&cellinfo, 0, sizeof(cellinfo));
 
@@ -1501,7 +1496,42 @@ main(int argc, char *argv[])
     else
        progname = argv[0];
 
-    krb5_init_context(&context);
+#if defined(KRB5_PROG_ETYPE_NOSUPP) && !(defined(HAVE_KRB5_ENCTYPE_ENABLE) || defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO))
+    {
+       char *filepath = NULL, *newpath = NULL;
+#ifndef AFS_DARWIN_ENV
+       char *defaultpath = "/etc/krb5.conf:/etc/krb5/krb5.conf";
+#else
+       char *defaultpath = "~/Library/Preferences/edu.mit.Kerberos:/Library/Preferences/edu.mit.Kerberos";
+#endif
+       filepath = getenv("KRB5_CONFIG");
+
+       /* only fiddle with KRB5_CONFIG if krb5-weak.conf actually exists */
+       if (asprintf(&newpath, "%s/krb5-weak.conf",
+                    AFSDIR_CLIENT_ETC_DIRPATH) < 0)
+           newpath = NULL;
+       if (newpath != NULL && access(newpath, R_OK) == 0) {
+           free(newpath);
+           newpath = NULL;
+           if (asprintf(&newpath, "%s:%s/krb5-weak.conf",
+                        filepath ? filepath : defaultpath,
+                        AFSDIR_CLIENT_ETC_DIRPATH) < 0)
+               newpath = NULL;
+           else
+               setenv("KRB5_CONFIG", newpath, 1);
+       }
+#endif
+       krb5_init_context(&context);
+
+#if defined(KRB5_PROG_ETYPE_NOSUPP) && !(defined(HAVE_KRB5_ENCTYPE_ENABLE) || defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO))
+       if (newpath)
+           free(newpath);
+       if (filepath)
+           setenv("KRB5_CONFIG", filepath, 1);
+       else
+           unsetenv("KRB5_CONFIG");
+    }
+#endif
     initialize_KTC_error_table ();
     initialize_U_error_table();
     initialize_RXK_error_table();
@@ -1514,12 +1544,12 @@ main(int argc, char *argv[])
      * krb5_allow_weak_crypto is MIT Kerberos 1.8.  krb5_enctype_enable is
      * Heimdal.
      */
-#if defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO)
-    krb5_allow_weak_crypto(context, 1);
-#elif defined(HAVE_KRB5_ENCTYPE_ENABLE)
+#if defined(HAVE_KRB5_ENCTYPE_ENABLE)
     i = krb5_enctype_valid(context, ETYPE_DES_CBC_CRC);
     if (i)
         krb5_enctype_enable(context, ETYPE_DES_CBC_CRC);
+#elif defined(HAVE_KRB5_ALLOW_WEAK_CRYPTO)
+    krb5_allow_weak_crypto(context, 1);
 #endif
 
     /* Initialize list of cells to which we have authenticated */
@@ -1529,7 +1559,7 @@ main(int argc, char *argv[])
     for (i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-d") == 0)
            dflag++;
-       else if (strcmp(argv[i], "-noauth") == 0) 
+       else if (strcmp(argv[i], "-noauth") == 0)
            noauth++;
        else if (strcmp(argv[i], "-zsubs") == 0)
            zsubs++;
@@ -1551,7 +1581,7 @@ main(int argc, char *argv[])
                  (strcmp(argv[i], "-c") == 0)) && !pmode)
            if (++i < argc) {
                cmode++;
-               strcpy(cell, argv[i]);
+               strlcpy(cell, argv[i], sizeof(cell));
            }
            else
                usage();
@@ -1571,22 +1601,27 @@ main(int argc, char *argv[])
                  (strcmp(argv[i], "-p") == 0)) && !cmode)
            if (++i < argc) {
                pmode++;
-               strcpy(path, argv[i]);
+               strlcpy(path, argv[i], sizeof(path));
+           }
+           else
+               usage();
+       else if (strcmp(argv[i], "-config") == 0)
+           if (++i < argc) {
+               config = argv[i];
            }
            else
                usage();
-           
        else if (argv[i][0] == '-')
            usage();
        else if (!pmode && !cmode) {
            if (strchr(argv[i], DIR) || (strcmp(argv[i], ".") == 0) ||
                (strcmp(argv[i], "..") == 0)) {
                pmode++;
-               strcpy(path, argv[i]);
+               strlcpy(path, argv[i], sizeof(path));
            }
-           else { 
+           else {
                cmode++;
-               strcpy(cell, argv[i]);
+               strlcpy(cell, argv[i], sizeof(cell));
            }
        }
        else
@@ -1596,7 +1631,7 @@ main(int argc, char *argv[])
            if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0)) {
                i+=2;
                if (i < argc)
-                   strcpy(realm, argv[i]);
+                   strlcpy(realm, argv[i], sizeof(realm));
                else
                    usage();
            }
@@ -1608,7 +1643,7 @@ main(int argc, char *argv[])
                if ((new_cellinfo = copy_cellinfo(&cellinfo)))
                    ll_add_data(cur_node, new_cellinfo);
                else {
-                   fprintf(stderr, 
+                   fprintf(stderr,
                            "%s: failure copying cellinfo.\n", progname);
                    exit(AKLOG_MISC);
                }
@@ -1649,8 +1684,8 @@ main(int argc, char *argv[])
     if ((cells.nelements + paths.nelements) == 0) {
        struct passwd *pwd;
 
-       status = auth_to_cell(context, NULL, NULL, &linkedcell);
-       
+       status = auth_to_cell(context, config, NULL, NULL, &linkedcell);
+
        /* If this cell is linked to a DCE cell, and user requested -linked,
         * get tokens for both. This is very useful when the AFS cell is
         * linked to a DFS cell and this system does not also have DFS.
@@ -1658,7 +1693,7 @@ main(int argc, char *argv[])
 
        if (!status && linked && linkedcell != NULL) {
            afs_dprintf("Linked cell: %s\n", linkedcell);
-           status = auth_to_cell(context, linkedcell, NULL, NULL);
+           status = auth_to_cell(context, config, linkedcell, NULL, NULL);
        }
        if (linkedcell) {
            free(linkedcell);
@@ -1676,8 +1711,8 @@ main(int argc, char *argv[])
            FILE *f;
            char fcell[100], xlog_path[512];
 
-           strcpy(xlog_path, pwd->pw_dir);
-           strcat(xlog_path, "/.xlog");
+           strlcpy(xlog_path, pwd->pw_dir, sizeof(xlog_path));
+           strlcat(xlog_path, "/.xlog", sizeof(xlog_path));
 
            if ((stat(xlog_path, &sbuf) == 0) &&
                ((f = fopen(xlog_path, "r")) != NULL)) {
@@ -1692,7 +1727,7 @@ main(int argc, char *argv[])
 
                    afs_dprintf("Found cell %s in %s.\n", fcell, xlog_path);
 
-                   auth_status = auth_to_cell(context, fcell, NULL, NULL);
+                   auth_status = auth_to_cell(context, config, fcell, NULL, NULL);
                    if (status == AKLOG_SUCCESS)
                        status = auth_status;
                    else
@@ -1705,14 +1740,16 @@ main(int argc, char *argv[])
        /* Log to all cells in the cells list first */
        for (cur_node = cells.first; cur_node; cur_node = cur_node->next) {
            memcpy((char *)&cellinfo, cur_node->data, sizeof(cellinfo));
-           if ((status = auth_to_cell(context, cellinfo.cell, cellinfo.realm,
-                                      &linkedcell)))
+           status = auth_to_cell(context, config, cellinfo.cell,
+                                 cellinfo.realm, &linkedcell);
+           if (status) {
                somethingswrong++;
-           else {
+           } else {
                if (linked && linkedcell != NULL) {
                    afs_dprintf("Linked cell: %s\n", linkedcell);
-                   if ((status = auth_to_cell(context, linkedcell,
-                                              cellinfo.realm, NULL)))
+                   status = auth_to_cell(context, config, linkedcell,
+                                         cellinfo.realm, NULL);
+                   if (status)
                        somethingswrong++;
                }
                if (linkedcell != NULL) {
@@ -1724,12 +1761,13 @@ main(int argc, char *argv[])
 
        /* Then, log to all paths in the paths list */
        for (cur_node = paths.first; cur_node; cur_node = cur_node->next) {
-           if ((status = auth_to_path(context, cur_node->data)))
+           status = auth_to_path(context, config, cur_node->data);
+           if (status)
                somethingswrong++;
        }
-       
-       /* 
-        * If only one thing was logged to, we'll return the status 
+
+       /*
+        * If only one thing was logged to, we'll return the status
         * of the single call.  Otherwise, we'll return a generic
         * something failed status.
         */
@@ -1738,7 +1776,7 @@ main(int argc, char *argv[])
     }
 
     /* If we are keeping track of zephyr subscriptions, print them. */
-    if (zsubs) 
+    if (zsubs)
        for (cur_node = zsublist.first; cur_node; cur_node = cur_node->next) {
            printf("zsub: %s\n", cur_node->data);
        }
@@ -1760,12 +1798,12 @@ isdir(char *path, unsigned char *val)
     if (lstat(path, &statbuf) < 0)
        return (-1);
     else {
-       if ((statbuf.st_mode & S_IFMT) == S_IFDIR) 
+       if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
            *val = TRUE;
        else
            *val = FALSE;
        return (0);
-    }  
+    }
 }
 
 static krb5_error_code
@@ -1794,7 +1832,7 @@ get_credv5_akimpersonate(krb5_context context,
     EncTicketPart enc_tkt_reply[1];
     krb5_address address[30];
     krb5_addresses faddr[1];
-    int temp_vno[1];
+    unsigned int temp_vno[1];
     time_t temp_time[2];
 #else
     krb5_ticket ticket_reply[1];
@@ -1851,7 +1889,7 @@ get_credv5_akimpersonate(krb5_context context,
         krb5_keytab_entry new[1];
         int best = -1;
         memset(new, 0, sizeof *new);
-        if ((code == krb5_kt_start_seq_get(context, kt, cursor))) {
+        if ((code = krb5_kt_start_seq_get(context, kt, cursor))) {
             afs_com_err(progname, code, "while starting keytab scan");
             goto cleanup;
         }
@@ -1924,7 +1962,7 @@ get_credv5_akimpersonate(krb5_context context,
     enc_tkt_reply->times.starttime = starttime; /* krb524init needs this */
     enc_tkt_reply->times.endtime = endtime;
 #endif  /* USING_HEIMDAL */
-    /* NB:  We will discard address for now--ignoring caddr field               
+    /* NB:  We will discard address for now--ignoring caddr field
        in any case.  MIT branch does what it always did. */
 
     if (paddress && *paddress) {
@@ -1976,8 +2014,8 @@ get_credv5_akimpersonate(krb5_context context,
 
         if(buf_len != buf_size) {
             afs_com_err(progname, code,
-                   "%d != %d while encoding ticket (internal ASN.1 encoder error",
-                   buf_len, buf_size);
+                   "%u != %u while encoding ticket (internal ASN.1 encoder error",
+                   (unsigned int)buf_len, (unsigned int)buf_size);
             goto cleanup;
         }
         what = "krb5_crypto_init";
@@ -1998,7 +2036,7 @@ get_credv5_akimpersonate(krb5_context context,
         }
     } /* crypto block */
     ticket_reply->enc_part.etype = deref_entry_enctype(entry);
-    ticket_reply->enc_part.kvno = temp_vno;
+    ticket_reply->enc_part.kvno = (void *)temp_vno;
     *ticket_reply->enc_part.kvno = entry->vno;
     ticket_reply->tkt_vno = 5;
 #else
@@ -2099,17 +2137,17 @@ get_credv5(krb5_context context, char *name, char *inst, char *realm,
 
     afs_dprintf("Getting tickets: %s%s%s@%s\n", name,
            (inst && inst[0]) ? "/" : "", inst ? inst : "", realm);
-    
+
     memset(&increds, 0, sizeof(increds));
 /* ANL - instance may be ptr to a null string. Pass null then */
     if ((r = krb5_build_principal(context, &increds.server,
                                  strlen(realm), realm,
                                  name,
-                                 (inst && strlen(inst)) ? inst : (void *) NULL,
-                                 (void *) NULL))) {
+                                 (inst && strlen(inst)) ? inst : NULL,
+                                 NULL))) {
         return r;
     }
-    
+
 
     if (!_krb425_ccache) {
         r = krb5_cc_default(context, &_krb425_ccache);
@@ -2125,12 +2163,13 @@ get_credv5(krb5_context context, char *name, char *inst, char *realm,
        if (r)
            return r;
     }
-    
+
     increds.client = client_principal;
     increds.times.endtime = 0;
-    /* Ask for DES since that is what V4 understands */
-    get_creds_enctype((&increds)) = ENCTYPE_DES_CBC_CRC;
-    
+    if (do524)
+       /* Ask for DES since that is what V4 understands */
+       get_creds_enctype((&increds)) = ENCTYPE_DES_CBC_CRC;
+
     if (keytab) {
        int allowed_enctypes[] = {
            ENCTYPE_DES_CBC_CRC, 0
@@ -2146,7 +2185,7 @@ get_credv5(krb5_context context, char *name, char *inst, char *realm,
                                     creds /* out */);
     } else {
        r = krb5_get_credentials(context, 0, _krb425_ccache, &increds, creds);
-    }    
+    }
     return r;
 }
 
@@ -2159,8 +2198,11 @@ get_user_realm(krb5_context context, char **realm)
 
     *realm = NULL;
 
-    if (!_krb425_ccache)
-        krb5_cc_default(context, &_krb425_ccache);
+    if (!_krb425_ccache) {
+       r = krb5_cc_default(context, &_krb425_ccache);
+       if (r)
+           return r;
+    }
     if (!client_principal) {
        if (client) {
            r = krb5_parse_name(context, client,  &client_principal);