aklog: Add -config option
[openafs.git] / src / aklog / aklog.c
index a38b66e..c437599 100644 (file)
  */
 
 #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 <sys/stat.h>
-#include <fcntl.h>
+#include <roken.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 <ctype.h>
 
-#include <afs/stds.h>
 #include <afs/ktc.h>
 #include <afs/token.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
@@ -245,7 +216,9 @@ 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)
+#ifdef AFS_DARWIN110_ENV
+#define HAVE_NO_KRB5_524 /* MITKerberosShim logs but returns success */
+#elif !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)
 #define HAVE_NO_KRB5_524
@@ -338,8 +311,10 @@ 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);
-#else
+#elif defined(HAVE_ERROR_MESSAGE)
            str = error_message(code);
+#else
+           ; /* IRIX apparently has neither: use the string we have */
 #endif
        }
        fputs(str, stderr);
@@ -378,7 +353,8 @@ copy_cellinfo(cellinfo_t *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;
@@ -392,10 +368,10 @@ get_cellconfig(char *cell, struct afsconf_cell *cellconfig, char **local_cell)
        exit(AKLOG_AFS);
     }
 
-    if (!(configdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
+    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);
     }
 
@@ -620,6 +596,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_enctypes may be required in the Kerberos configuration\n");
+       }
+#endif
        status = AKLOG_KERBEROS;
        goto out;
     }
@@ -906,7 +887,7 @@ rxkad_get_token(krb5_context context, struct afsconf_cell *cell, char *realm,
     } else {
        len = strlen(username)+strlen(realmUsed)+2;
        *authuser = malloc(len);
-       afs_snprintf(*authuser, len, "%s@%s", username, realmUsed);
+       snprintf(*authuser, len, "%s@%s", username, realmUsed);
        *foreign = 1;
     }
 
@@ -919,69 +900,14 @@ out:
     return status;
 }
 
-/*!
- * Get the set of tokens for a given cell out of the cache manager
- *
- * @param[in] cell
- *     The cellconf structure for the cell to retrieve tokens for
- * @param[out] tokenPtr
- *     The tokens held for that cell
- *
- * @returns
- *     0 on success, otherwise an error code
- */
-
-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;
-}
-
-/**
- * Return true if a pair of tokens are directly equivalent
- */
-static int
-tokens_equal(struct ktc_setTokenData *tokenA, struct ktc_token *tokenB) {
-   return 0;
-/* Bodge bodge bodge
-   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;
@@ -991,11 +917,11 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
     char *local_cell = NULL;
     struct ktc_tokenUnion *rxkadToken = NULL;
     struct ktc_setTokenData *token;
-    struct ktc_token *btoken;
+    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) {
@@ -1067,13 +993,18 @@ auth_to_cell(krb5_context context, char *cell, char *realm, char **linkedcell)
        }
 
        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
@@ -1382,7 +1313,7 @@ add_hosts(char *file)
  * 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;
@@ -1432,7 +1363,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)
@@ -1520,6 +1452,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));
 
@@ -1552,12 +1485,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 */
@@ -1613,7 +1546,12 @@ main(int argc, char *argv[])
            }
            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) {
@@ -1687,7 +1625,7 @@ 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
@@ -1696,7 +1634,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);
@@ -1730,7 +1668,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
@@ -1743,14 +1681,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) {
@@ -1762,7 +1702,8 @@ 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++;
        }
 
@@ -2014,8 +1955,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";
@@ -2197,8 +2138,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);