aklog: require opt-in to enable single-DES in libkrb5
[openafs.git] / src / aklog / aklog.c
index eaead44..66b4dfe 100644 (file)
@@ -317,9 +317,10 @@ redirect_errors(const char *who, afs_int32 code, const char *fmt, va_list ap)
            krb5_svc_get_msg(code,&str);
 #elif defined(HAVE_KRB5_GET_ERROR_MESSAGE)
            krb5_context context;
-           krb5_init_context(&context);
-           str = krb5_get_error_message(context, code);
-           krb5_free_context(context);
+           if (krb5_init_context(&context) == 0) {
+                str = krb5_get_error_message(context, code);
+                krb5_free_context(context);
+            }
 #else
            ; /* IRIX apparently has neither: use the string we have */
 #endif
@@ -382,19 +383,24 @@ get_cellconfig(const char *config, char *cell,
        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);
@@ -655,7 +661,8 @@ 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
@@ -663,7 +670,7 @@ out:
 static int
 rxkad_build_native_token(krb5_context context, krb5_creds *v5cred,
                         struct ktc_tokenUnion **tokenPtr, char **userPtr) {
-    char username[BUFSIZ];
+    char username[BUFSIZ]="";
     struct ktc_token token;
     int status;
 #ifdef HAVE_NO_KRB5_524
@@ -688,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),
@@ -731,7 +739,8 @@ rxkad_build_native_token(krb5_context context, krb5_creds *v5cred,
        return status;
     }
 
-    *userPtr = strdup(username);
+    if (username[0] != '\0')
+       *userPtr = strdup(username);
 
     return 0;
 }
@@ -751,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
@@ -838,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.
  *
@@ -871,12 +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 {
-       asprintf(authuser, "%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;
     }
 
@@ -998,7 +1014,10 @@ auth_to_cell(krb5_context context, const char *config,
        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 {
@@ -1069,15 +1088,24 @@ auth_to_cell(krb5_context context, const char *config,
            }
        }
 
-       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
         * 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
        token_setPag(token, afssetpag);
        status = ktc_SetTokenEx(token);
@@ -1166,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];
 
@@ -1244,7 +1272,7 @@ next_path(char *origpath)
        else
            last_comp = elast_comp;
     }
-    while(link);
+    while(link > 0);
 
     return(pathtocheck);
 }
@@ -1400,7 +1428,7 @@ usage(void)
            "[-d] [[-cell | -c] cell [-k krb_realm]] ",
            "[[-p | -path] pathname]\n",
            "    [-zsubs] [-hosts] [-noauth] [-noprdb] [-force] [-setpag] \n"
-               "    [-linked]"
+               "    [-linked] [-insecure_des]"
 #ifndef HAVE_NO_KRB5_524
                " [-524]"
 #endif
@@ -1419,6 +1447,7 @@ usage(void)
 #ifndef HAVE_NO_KRB5_524
     fprintf(stderr, "    -524 means use the 524 converter instead of V5 directly\n");
 #endif
+    fprintf(stderr, "    -insecure_des enables insecure single-DES for krb5.\n");
     fprintf(stderr, "    No commandline arguments means ");
     fprintf(stderr, "authenticate to the local cell.\n");
     fprintf(stderr, "\n");
@@ -1432,6 +1461,7 @@ main(int argc, char *argv[])
     int status = AKLOG_SUCCESS;
     int i;
     int somethingswrong = FALSE;
+    int insecure_des = 0;
 
     cellinfo_t cellinfo;
 
@@ -1479,20 +1509,25 @@ main(int argc, char *argv[])
        filepath = getenv("KRB5_CONFIG");
 
        /* only fiddle with KRB5_CONFIG if krb5-weak.conf actually exists */
-       asprintf(&newpath, "%s/krb5-weak.conf", AFSDIR_CLIENT_ETC_DIRPATH);
-       if (access(newpath, R_OK) == 0) {
+       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;
-           asprintf(&newpath, "%s:%s/krb5-weak.conf",
-                    filepath ? filepath : defaultpath,
-                    AFSDIR_CLIENT_ETC_DIRPATH);
-           setenv("KRB5_CONFIG", newpath, 1);
+           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))
-       free(newpath);
+       if (newpath)
+           free(newpath);
        if (filepath)
            setenv("KRB5_CONFIG", filepath, 1);
        else
@@ -1506,19 +1541,6 @@ main(int argc, char *argv[])
     initialize_PT_error_table();
     afs_set_com_err_hook(redirect_errors);
 
-    /*
-     * Enable DES enctypes, which are currently still required for AFS.
-     * krb5_allow_weak_crypto is MIT Kerberos 1.8.  krb5_enctype_enable is
-     * Heimdal.
-     */
-#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 */
     ll_init(&authedcells);
 
@@ -1578,6 +1600,8 @@ main(int argc, char *argv[])
            }
            else
                usage();
+       else if (strcmp(argv[i], "-insecure_des") == 0)
+           insecure_des = 1;
        else if (argv[i][0] == '-')
            usage();
        else if (!pmode && !cmode) {
@@ -1588,12 +1612,32 @@ main(int argc, char *argv[])
            }
            else {
                cmode++;
-               strlcpy(cell, argv[i], sizeof(path));
+               strlcpy(cell, argv[i], sizeof(cell));
            }
        }
        else
            usage();
 
+    /*
+     * Enable DES enctypes if requested.  This is not required when rxkad-k5
+     * is used, but some sites may not have updated.
+     * krb5_allow_weak_crypto is MIT Kerberos 1.8.  krb5_enctype_enable is
+     * Heimdal.
+     */
+    if (insecure_des) {
+#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);
+#else
+       fprintf(stderr,
+           "%s: -insecure_des is not supported by this libkrb5\n", progname);
+       exit(AKLOG_MISC);
+#endif
+    }
+
        if (cmode) {
            if (((i + 1) < argc) && (strcmp(argv[i + 1], "-k") == 0)) {
                i+=2;