- removed unused variables
[mod_auth_kerb.cvs/.git] / src / mod_auth_kerb.c
index a480f47..b6169d8 100644 (file)
@@ -30,9 +30,12 @@ module AP_MODULE_DECLARE_DATA kerb_auth_module;
 
 #ifdef KRB5
 #include <krb5.h>
-#include <gssapi.h>
 #endif /* KRB5 */
 
+#ifdef GSSAPI
+#include <gssapi.h>
+#endif /* GSSAPI */
+
 #ifdef KRB4
 #include <krb.h>
 #endif /* KRB4 */
@@ -74,22 +77,29 @@ module AP_MODULE_DECLARE_DATA kerb_auth_module;
  Auth Configuration Structure
  ***************************************************************************/
 typedef struct {
-       char *krb_auth_type;
 #ifdef KRB4
        char *krb_4_srvtab;
 #endif /* KRB4 */
 #ifdef KRB5
        char *krb_5_keytab;
 #endif /* KRB5 */
+       int krb_auth_enable;
+       char *krb_auth_realms;
        int krb_authoritative;
-       char *krb_default_realm;
        int krb_fail_status;
        char *krb_force_instance;
 #ifdef KRB5
        int krb_forwardable;
 #endif /* KRB5 */
        char *krb_lifetime;
+#ifdef GSSAPI
+       int krb_method_gssapi;
+#endif /* GSSAPI */
+#ifdef KRB4
+       int krb_method_k4pass;
+#endif /* KRB4 */
 #ifdef KRB5
+       int krb_method_k5pass;
        char *krb_renewable;
 #endif /* KRB5 */
        int krb_save_credentials;
@@ -97,6 +107,13 @@ typedef struct {
        char *service_name;
 } kerb_auth_config;
 
+
+
+
+/*************************************************************************** 
+ GSSAPI Support Initialization
+ ***************************************************************************/
+#ifdef GSSAPI
 typedef struct {
    gss_ctx_id_t context;
    gss_cred_id_t server_creds;
@@ -118,6 +135,8 @@ cleanup_gss_connection(void *data)
    if (gss_conn->server_creds != GSS_C_NO_CREDENTIAL)
       gss_release_cred(&minor_status, &gss_conn->server_creds);
 }
+#endif /* GSSAPI */
+
 
 
 
@@ -130,7 +149,19 @@ static void *kerb_dir_config(MK_POOL *p, char *d)
        rec = (void *) ap_pcalloc(p, sizeof(kerb_auth_config));
        ((kerb_auth_config *)rec)->krb_fail_status = HTTP_UNAUTHORIZED;
        ((kerb_auth_config *)rec)->krb_authoritative = 0;
-       ((kerb_auth_config *)rec)->krb_auth_type = MK_PSTRDUP(p, "None");
+#ifdef GSSAPI
+       ((kerb_auth_config *)rec)->krb_method_gssapi = 0;
+#endif /* GSSAPI */
+#ifdef KRB4
+       ((kerb_auth_config *)rec)->krb_method_k4pass = 1;
+#endif /* KRB4 */
+#ifdef KRB5
+       ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
+#ifdef KRB4
+       ((kerb_auth_config *)rec)->krb_method_k4pass = 0;
+#endif /* KRB4 */
+#endif /* KRB5 */
+       ((kerb_auth_config *)rec)->krb_auth_realms = "";
        return rec;
 }
 
@@ -155,33 +186,6 @@ static const char *kerb_set_fail_slot(cmd_parms *cmd, void *struct_ptr,
        return NULL;
 }
 
-/* these are either char *struct_ptr, char *arg or void *struct_ptr, const char *arg */
-static const char *kerb_set_type_slot(cmd_parms *cmd, void *struct_ptr,
-                                       const char *arg)
-{
-       int offset = (int) (long) cmd->info;
-#ifdef KRB5
-       if (!strncasecmp(arg, "v5", 2))
-               *(char **) ((char *)struct_ptr + offset) = MK_PSTRDUP(cmd->pool, "KerberosV5");
-       else
-#endif /* KRB5 */
-#ifdef KRB4
-       if (!strncasecmp(arg, "v4", 2))
-               *(char **) ((char *)struct_ptr + offset) = MK_PSTRDUP(cmd->pool, "KerberosV4");
-       else
-#endif /* KRB4 */
-       if (!strncasecmp(arg, "dualv5v4", 8))
-               *(char **) ((char *)struct_ptr + offset) = MK_PSTRDUP(cmd->pool, "KerberosDualV5V4");
-       else if
-          (!strncasecmp(arg, "dualv4v5", 8))
-               *(char **) ((char *)struct_ptr + offset) = MK_PSTRDUP(cmd->pool, "KerberosDualV4V5");
-#if defined(KRB4) && defined(KRB5)
-#endif /* KRB4 && KRB5 */
-       else
-               return "AuthKerberos must be V5, V4, DualV4V5, or DualV5V4.";
-       return NULL;
-}
-
 
 
 
@@ -192,10 +196,10 @@ static const char *kerb_set_type_slot(cmd_parms *cmd, void *struct_ptr,
 command_rec kerb_auth_cmds[] = {
        {
                "AuthKerberos",
-               kerb_set_type_slot,
-               (void*)XtOffsetOf(kerb_auth_config, krb_auth_type),
+               ap_set_flag_slot,
+               (void*)XtOffsetOf(kerb_auth_config, krb_auth_enable),
                OR_AUTHCFG,
-               TAKE1,
+               FLAG,
                "Permit Kerberos auth without AuthType requirement."
        },
 
@@ -231,12 +235,12 @@ command_rec kerb_auth_cmds[] = {
        },
 
        {
-               "KrbDefaultRealm",
+               "KrbAuthRealm",
                ap_set_string_slot,
-               (void*)XtOffsetOf(kerb_auth_config, krb_default_realm),
+               (void*)XtOffsetOf(kerb_auth_config, krb_auth_realms),
                OR_AUTHCFG,
-               TAKE1,
-               "Default realm to authenticate users against."
+               ITERATE,
+               "Realms to attempt authentication against (can be multiple)."
        },
 
        {
@@ -277,8 +281,39 @@ command_rec kerb_auth_cmds[] = {
                "Lifetime of tickets retrieved."
        },
 
+#ifdef GSSAPI
+       {
+               "KrbMethodGSSAPI",
+               ap_set_flag_slot,
+               (void*)XtOffsetOf(kerb_auth_config, krb_method_gssapi),
+               OR_AUTHCFG,
+               FLAG,
+               "Enable GSSAPI authentication."
+       },
+#endif /* GSSAPI */
+
+#ifdef KRB4
+       {
+               "KrbMethodK4Pass",
+               ap_set_flag_slot,
+               (void*)XtOffsetOf(kerb_auth_config, krb_method_k4pass),
+               OR_AUTHCFG,
+               FLAG,
+               "Enable Kerberos V4 password authentication."
+       },
+#endif /* KRB4 */
+
 #ifdef KRB5
        {
+               "KrbMethodK5Pass",
+               ap_set_flag_slot,
+               (void*)XtOffsetOf(kerb_auth_config, krb_method_k5pass),
+               OR_AUTHCFG,
+               FLAG,
+               "Enable Kerberos V5 password authentication."
+       },
+
+       {
                "KrbRenewable",
                ap_set_string_slot,
                (void*)XtOffsetOf(kerb_auth_config, krb_renewable),
@@ -320,10 +355,10 @@ command_rec kerb_auth_cmds[] = {
 #else
 #ifdef APXS2
 static const command_rec kerb_auth_cmds[] = {
-       AP_INIT_TAKE1(
+       AP_INIT_FLAG(
                "AuthKerberos",
                kerb_set_type_slot,
-               (void*)APR_XtOffsetOf(kerb_auth_config, krb_auth_type),
+               (void*)APR_XtOffsetOf(kerb_auth_config, krb_auth_enable),
                OR_AUTHCFG,
                "Permit Kerberos auth without AuthType requirement."
        ),
@@ -356,12 +391,12 @@ static const command_rec kerb_auth_cmds[] = {
                "Refuse to pass request down to lower modules."
        ),
 
-       AP_INIT_TAKE1(
-               "KrbDefaultRealm",
+       AP_INIT_ITERATE(
+               "KrbAuthRealm",
                ap_set_string_slot,
-               (void*)APR_XtOffsetOf(kerb_auth_config, krb_default_realm),
+               (void*)APR_XtOffsetOf(kerb_auth_config, krb_auth_realms),
                OR_AUTHCFG,
-               "Default realm to authenticate users against."
+               "Realm to attempt authentication against (can be multiple)."
        ),
 
        AP_INIT_TAKE1(
@@ -398,7 +433,35 @@ static const command_rec kerb_auth_cmds[] = {
                "Lifetime of tickets retrieved."
        ),
 
+#ifdef GSSAPI
+       AP_INIT_FLAG(
+               "KrbMethodGSSAPI",
+               ap_set_flag_slot,
+               (void*)APR_XtOffsetOf(kerb_auth_config, krb_method_gssapi),
+               OR_AUTHCFG,
+               "Enable GSSAPI authentication."
+       ),
+#endif /* GSSAPI */
+
+#ifdef KRB4
+       AP_INIT_FLAG(
+               "KrbMethodK4Pass",
+               ap_set_flag_slot,
+               (void*)APR_XtOffsetOf(kerb_auth_config, krb_method_k4pass),
+               OR_AUTHCFG,
+               "Enable Kerberos V4 password authentication."
+       ),
+#endif /* KRB4 */
+
 #ifdef KRB5
+       AP_INIT_FLAG(
+               "KrbMethodK5Pass",
+               ap_set_flag_slot,
+               (void*)APR_XtOffsetOf(kerb_auth_config, krb_method_k5pass),
+               OR_AUTHCFG,
+               "Enable Kerberos V5 password authentication."
+       ),
+
        AP_INIT_TAKE1(
                "KrbRenewable",
                ap_set_string_slot,
@@ -444,13 +507,18 @@ krb5_verify_user(krb5_context context, krb5_principal principal,
                 krb5_ccache ccache, const char *password, krb5_boolean secure,
                 const char *service)
 {
-   int problem;
+   int ret;
+   krb5_context kcontext;
+   krb5_principal server, client;
+   krb5_timestamp now;
    krb5_creds my_creds;
+   krb5_flags options = 0;
+   krb5_principal me = NULL;
    krb5_data tgtname = {
       0,
       KRB5_TGS_NAME_SIZE,
       KRB5_TGS_NAME
-   }
+   };
 
    memset((char *)&my_creds, 0, sizeof(my_creds));
    my_creds.client = principal;
@@ -476,7 +544,7 @@ krb5_verify_user(krb5_context context, krb5_principal principal,
    */
 
    ret = krb5_get_in_tkt_with_password(kcontext, options, 0, NULL, 0,
-                                      pass, ccache, &my_creds, 0);
+                                      password, ccache, &my_creds, 0);
    if (ret) {
        return ret;
    }
@@ -490,31 +558,146 @@ krb5_verify_user(krb5_context context, krb5_principal principal,
  Username/Password Validation
  ***************************************************************************/
 #ifdef KRB5
+static void
+krb5_cache_cleanup(void *data)
+{
+   krb5_context context;
+   krb5_ccache  cache;
+   krb5_error_code problem;
+   char *cache_name = (char *) data;
+
+   problem = krb5_init_context(&context);
+   if (problem) {
+      ap_log_error(APLOG_MARK, APLOG_ERR, NULL, "krb5_init_context() failed");
+      return;
+   }
+
+   problem = krb5_cc_resolve(context, cache_name, &cache);
+   if (problem) {
+      ap_log_error(APLOG_MARK, APLOG_ERR, NULL, 
+                   "krb5_cc_resolve() failed (%s: %s)",
+                  cache_name, krb5_get_err_text(context, problem)); 
+      return;
+   }
+
+   krb5_cc_destroy(context, cache);
+   krb5_free_context(context);
+}
+
+static int
+store_krb5_creds(krb5_context kcontext,
+                request_rec *r,
+                kerb_auth_config *conf,
+                krb5_ccache delegated_cred)
+{
+       char *c, ccname[MAX_STRING_LEN];
+       krb5_error_code problem;
+       char errstr[1024];
+       int ret;
+       krb5_ccache ccache = NULL;
+       krb5_principal me = NULL;
+
+       sprintf(ccname, "FILE:%s/k5cc_ap_%s",
+               conf->krb_tmp_dir ? conf->krb_tmp_dir : "/tmp",
+               MK_USER);
+
+       for (c = ccname + strlen(conf->krb_tmp_dir ? conf->krb_tmp_dir :
+            "/tmp") + 1; *c; c++) {
+               if (*c == '/')
+                       *c = '.';
+         }
+
+       problem = krb5_cc_set_default_name(kcontext, ccname);
+       if (problem) {
+               snprintf(errstr, sizeof(errstr),
+                          "krb5_cc_set_default_name() failed: %s",
+                          krb5_get_err_text(kcontext, problem));
+               ap_log_reason (errstr, r->uri, r);
+               ret = SERVER_ERROR;
+               goto end;
+         }
+#if 0
+         /* XXX Dan: Why is this done? Cleanup? But the file would not be 
+          * accessible from another processes (CGI) */
+         unlink(ccname+strlen("FILE:"));
+#endif
+
+         problem = krb5_cc_resolve(kcontext, ccname, &ccache);
+         if (problem) {
+               snprintf(errstr, sizeof(errstr),
+                        "krb5_cc_resolve() failed: %s",
+                        krb5_get_err_text(kcontext, problem));
+               ap_log_reason (errstr, r->uri, r);
+               ret = SERVER_ERROR;
+               goto end;
+         }
+
+         problem = krb5_cc_get_principal(kcontext, delegated_cred, &me);
+         if (problem) {
+                 snprintf(errstr, sizeof(errstr),
+                          "krb5_cc_get_principal() failed: %s",
+                          krb5_get_err_text(kcontext, problem));
+                 ap_log_reason (errstr, r->uri, r);
+                 ret = SERVER_ERROR;
+                 goto end;
+         }
+
+         problem = krb5_cc_initialize(kcontext, ccache, me);
+         if (problem) {
+               snprintf(errstr, sizeof(errstr),
+                        "krb5_cc_initialize() failed: %s",
+                        krb5_get_err_text(kcontext, problem));
+               ap_log_reason (errstr, r->uri, r);
+               ret = SERVER_ERROR;
+               goto end;
+         }
+
+         problem = krb5_cc_copy_cache(kcontext, delegated_cred, ccache);
+         if (problem) {
+            snprintf(errstr, sizeof(errstr),
+                     "krb5_cc_copy_cache failed: %s",
+                     krb5_get_err_text(kcontext, problem));
+            ap_log_reason (errstr, r->uri, r);
+            ret = SERVER_ERROR;
+            goto end;
+         }
+         ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
+         ap_register_cleanup(r->pool, ccname,
+                             krb5_cache_cleanup, ap_null_cleanup);
+
+         krb5_cc_close(kcontext, ccache);
+
+         ret = OK;
+
+end:
+
+         return ret; /* XXX */
+}
+
 int kerb5_password_validate(request_rec *r, const char *user, const char *pass)
 {
+
        kerb_auth_config *conf =
                (kerb_auth_config *)ap_get_module_config(r->per_dir_config,
                                        &kerb_auth_module);
        int ret;
        krb5_context kcontext;
-       krb5_principal server, me;
-       krb5_creds my_creds;
-       krb5_timestamp now;
+       krb5_principal client;
        krb5_ccache ccache = NULL;
        krb5_deltat lifetime = 300;     /* 5 minutes */
        krb5_deltat renewal = 0;
        krb5_flags options = 0;
-       krb5_data tgtname = {
-#ifndef HEIMDAL
-               0,
-#endif
-               KRB5_TGS_NAME_SIZE,
-               KRB5_TGS_NAME
-       };
-       char *c, ccname[MAX_STRING_LEN];
-
-       if (krb5_init_context(&kcontext))
+       char errstr[1024];
+       krb5_error_code code;
+       const char *realms;
+
+       if (krb5_init_context(&kcontext)) {
+               snprintf(errstr, sizeof(errstr),
+                        "Cannot initialize Kerberos5 context");
+               ap_log_reason (errstr, r->uri, r);
+               ret = SERVER_ERROR;
                return 0;
+       }
 
        if (conf->krb_forwardable) {
           options |= KDC_OPT_FORWARDABLE;
@@ -529,16 +712,20 @@ int kerb5_password_validate(request_rec *r, const char *user, const char *pass)
           lifetime = atoi(conf->krb_lifetime);
        }
 
+#ifdef HEIMDAL
        code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
+#else
+       code = krb5_mcc_generate_new(kcontext, &ccache);
+#endif
        if (code) {
-          snprintf(errstr, sizeof(errstr), "krb5_cc_gen_new(): %.100s",
+          snprintf(errstr, sizeof(errstr), "Cannot generate new ccache: %.100s",
                    krb5_get_err_text(kcontext, code));
           ap_log_reason (errstr, r->uri, r);
           ret = SERVER_ERROR;
           goto end;
        }
 
-       realms = conf->krb5_auth_realm;
+       realms = conf->krb_auth_realms;
        do {
           code = 0;
           if (realms) {
@@ -548,12 +735,13 @@ int kerb5_password_validate(request_rec *r, const char *user, const char *pass)
                 continue;
           }
 
-          code = krb5_parse_name(kcontext, r->connection->user, &princ);
+          code = krb5_parse_name(kcontext, r->connection->user, &client);
           if (code)
              continue;
 
-          code = krb5_verify_user(kcontext, princ, ccache, sent_pw,
+          code = krb5_verify_user(kcontext, client, ccache, pass,
                                   1, "khttp");
+          krb5_free_principal(kcontext, client);
           if (code == 0)
              break;
 
@@ -568,42 +756,23 @@ int kerb5_password_validate(request_rec *r, const char *user, const char *pass)
                    krb5_get_err_text(kcontext, code));
           ap_log_reason (errstr, r->uri, r);
           ret = HTTP_UNAUTHORIZED;
-          return 0;
+          goto end;
        }
 
        if (conf->krb_save_credentials) {
-               sprintf(ccname, "FILE:%s/k5cc_ap_%s",
-                       conf->krb_tmp_dir ? conf->krb_tmp_dir : "/tmp",
-                       MK_USER);
-
-               for (c = ccname + strlen(conf->krb_tmp_dir ? conf->krb_tmp_dir :                                "/tmp") + 1; *c; c++) {
-                       if (*c == '/')
-                               *c = '.';
-               }
-
-               ap_table_setn(r->subprocess_env, "KRB5CCNAME", ccname);
-               if (krb5_cc_set_default_name(kcontext, ccname)) {
-                       return 0;
-               }
-               unlink(ccname+strlen("FILE:"));
-
-               if (krb5_cc_resolve(kcontext, ccname, &ccache))
-                       return 0;
-
-               problem = krb5_cc_get_principal(krb_ctx, mem_ccache, &me);
-
-               if (krb5_cc_initialize(kcontext, ccache, me))
-                       return 0;
+               ret = store_krb5_creds(kcontext, r, conf, ccache);
+               if (ret)
+                       goto end;
+       }
 
-               problem = krb5_cc_copy_cache(krb_ctx, delegated_cred, ccache);
-               if (problem) {
-                  return 0;
-               }
+       ret = 1; /* XXX should be OK ? */
 
-               krb5_cc_close(krb_ctx, ccache);
-       }
+end:
+       if (ccache)
+          krb5_cc_destroy(kcontext, ccache);
+       krb5_free_context(kcontext);
 
-       return 1;
+       return (ret != 1) ? 0 : 1; /* XXX */
 }
 #endif /* KRB5 */
 
@@ -696,6 +865,41 @@ int kerb4_password_validate(request_rec *r, const char *user, const char *pass)
 }
 #endif /* KRB4 */
 
+
+
+
+/*************************************************************************** 
+ GSSAPI Validation
+ ***************************************************************************/
+#ifdef GSSAPI
+static const char *
+get_gss_error(pool *p, OM_uint32 error_status, char *prefix)
+{
+   OM_uint32 maj_stat, min_stat;
+   OM_uint32 msg_ctx = 0;
+   gss_buffer_desc status_string;
+   char buf[1024];
+   size_t len;
+
+   snprintf(buf, sizeof(buf), "%s: ", prefix);
+   len = strlen(buf);
+   do {
+      maj_stat = gss_display_status (&min_stat,
+                                    error_status,
+                                    GSS_C_MECH_CODE,
+                                    GSS_C_NO_OID,
+                                    &msg_ctx,
+                                    &status_string);
+      if (sizeof(buf) > len + status_string.length + 1) {
+         sprintf(buf+len, "%s:", (char*) status_string.value);
+         len += status_string.length;
+      }
+      gss_release_buffer(&min_stat, &status_string);
+   } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
+
+   return (ap_pstrdup(p, buf));
+}
+
 static int
 get_gss_creds(request_rec *r,
               kerb_auth_config *conf,
@@ -763,17 +967,15 @@ negotiate_authenticate_user(request_rec *r,
   gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
   gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
   const char *auth_param = NULL;
-  krb5_context krb_ctx = NULL;
   int ret;
   gss_name_t client_name = GSS_C_NO_NAME;
   gss_cred_id_t delegated_cred = GSS_C_NO_CREDENTIAL;
-  char *p;
 
   if (gss_connection == NULL) {
      gss_connection = ap_pcalloc(r->connection->pool, sizeof(*gss_connection));
      if (gss_connection == NULL) {
-       ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, r,
-                     "ap_pcalloc() failed");
+       ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
+                     "ap_pcalloc() failed (not enough memory)");
        ret = SERVER_ERROR;
        goto end;
      }
@@ -790,8 +992,8 @@ negotiate_authenticate_user(request_rec *r,
   /* ap_getword() shifts parameter */
   auth_param = ap_getword_white(r->pool, &auth_line);
   if (auth_param == NULL) {
-     ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, r,
-                  "No Authorization parameter from client");
+     ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
+                  "No Authorization parameter in request from client");
      ret = HTTP_UNAUTHORIZED;
      goto end;
   }
@@ -799,8 +1001,8 @@ negotiate_authenticate_user(request_rec *r,
   input_token.length = ap_base64decode_len(auth_param);
   input_token.value = ap_pcalloc(r->connection->pool, input_token.length);
   if (input_token.value == NULL) {
-     ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, r,
-                  "Not enough memory");
+     ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
+                  "ap_pcalloc() failed (not enough memory)");
      ret = SERVER_ERROR;
      goto end;
   }
@@ -824,8 +1026,8 @@ negotiate_authenticate_user(request_rec *r,
      len = ap_base64encode_len(output_token.length);
      token = ap_pcalloc(r->connection->pool, len + 1);
      if (token == NULL) {
-       ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, r,
-                    "Not enough memory");
+       ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
+                    "ap_pcalloc() failed (not enough memory)");
         ret = SERVER_ERROR;
        gss_release_buffer(&minor_status2, &output_token);
        goto end;
@@ -834,12 +1036,11 @@ negotiate_authenticate_user(request_rec *r,
      token[len] = '\0';
      ap_table_set(r->err_headers_out, "WWW-Authenticate",
                  ap_pstrcat(r->pool, "GSS-Negotiate ", token, NULL));
-     free(token);
      gss_release_buffer(&minor_status2, &output_token);
   }
 
   if (GSS_ERROR(major_status)) {
-     ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, r,
+     ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
                   "%s", get_gss_error(r->pool, minor_status,
                                       "gss_accept_sec_context() failed"));
      ret = HTTP_UNAUTHORIZED;
@@ -847,10 +1048,8 @@ negotiate_authenticate_user(request_rec *r,
   }
 
   if (major_status & GSS_S_CONTINUE_NEEDED) {
-#if 0
-     ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, r,
-                  "only one authentication iteration allowed"); 
-#endif
+     /* Some GSSAPI mechanism (eg GSI from Globus) may require multiple 
+      * iterations to establish authentication */
      ret = HTTP_UNAUTHORIZED;
      goto end;
   }
@@ -858,7 +1057,7 @@ negotiate_authenticate_user(request_rec *r,
   major_status = gss_export_name(&minor_status, client_name, &output_token);
   gss_release_name(&minor_status, &client_name); 
   if (GSS_ERROR(major_status)) {
-    ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, r,
+    ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
                  "%s", get_gss_error(r->pool, minor_status, 
                                      "gss_export_name() failed"));
     ret = SERVER_ERROR;
@@ -918,6 +1117,9 @@ end:
 
   return ret;
 }
+#endif /* GSSAPI */
+
+
 
 
 /*************************************************************************** 
@@ -927,9 +1129,6 @@ int kerb_authenticate_user(request_rec *r)
 {
        const char *name;               /* AuthName specified */
        const char *type;               /* AuthType specified */
-       int KerberosV5 = 0;             /* Kerberos V5 check enabled */
-       int KerberosV4 = 0;             /* Kerberos V4 check enabled */
-       int KerberosV4first = 0;        /* Kerberos V4 check first */
        const char *sent_pw;            /* Password sent by browser */
        int res;                        /* Response holder */
        int retcode;                    /* Return code holder */
@@ -945,38 +1144,8 @@ int kerb_authenticate_user(request_rec *r)
 
        type = ap_auth_type(r);
 
-       if (type != NULL) {
-#ifdef KRB5
-               if ((strncasecmp(type, "KerberosV5", 10) == 0) ||
-                   (strncasecmp(conf->krb_auth_type, "KerberosV5", 10) == 0)) {
-                       KerberosV5 = 1;
-               }
-#endif /* KRB5 */
-
-#ifdef KRB4
-               if ((strncasecmp(type, "KerberosV4", 10) == 0) ||
-                   (strncasecmp(conf->krb_auth_type, "KerberosV4", 10) == 0)) {
-                       KerberosV4 = 1;
-               }
-#endif /* KRB4 */
-
-#if defined(KRB5) && defined(KRB4)
-               if ((strncasecmp(type, "KerberosDualV5V4", 15) == 0) ||
-                   (strncasecmp(conf->krb_auth_type, "KerberosDualV5V4", 15) == 0)) {
-                       KerberosV5 = 1;
-                       KerberosV4 = 1;
-               }
-
-               if ((strncasecmp(type, "KerberosDualV4V5", 15) == 0) ||
-                   (strncasecmp(conf->krb_auth_type, "KerberosDualV4V5", 15) == 0)) {
-                       KerberosV5 = 1;
-                       KerberosV4 = 1;
-                       KerberosV4first = 1;
-               }
-#endif /* KRB5 && KRB4 */
-       }
-
-       if (!KerberosV4 && !KerberosV5) {
+       if (!conf->krb_auth_enable &&
+           (type == NULL || (strncasecmp(type, "Kerberos", 8) != 0))) {
                if (conf->krb_authoritative) {
                        return HTTP_UNAUTHORIZED;
                }
@@ -1005,20 +1174,31 @@ int kerb_authenticate_user(request_rec *r)
 
        retcode = DECLINED;
 
+#ifdef GSSAPI
+       if (conf->krb_method_gssapi && retcode != OK) {
+               MK_AUTH_TYPE = "Negotiate";
+               if (negotiate_authenticate_user(r, conf, auth_line))
+                       retcode = OK;
+               else
+                       retcode = conf->krb_fail_status;
+       }
+#endif /* GSSAPI */
+
 #ifdef KRB5
-       if (KerberosV5 && !KerberosV4first && retcode != OK) {
+       if (conf->krb_method_k5pass && retcode != OK) {
                MK_AUTH_TYPE = "KerberosV5";
                if (kerb5_password_validate(r, MK_USER, sent_pw)) {
                        retcode = OK;
                }
                else {
                        retcode = conf->krb_fail_status;
+                       /* XXX should SERVER_ERROR be overriden too? */
                }
        }
 #endif /* KRB5 */
 
 #ifdef KRB4
-       if (KerberosV4 && retcode != OK) {
+       if (conf->krb_method_k4pass && retcode != OK) {
                MK_AUTH_TYPE = "KerberosV4";
                if (kerb4_password_validate(r, MK_USER, sent_pw)) {
                        retcode = OK;
@@ -1029,18 +1209,6 @@ int kerb_authenticate_user(request_rec *r)
        }
 #endif /* KRB4 */
 
-#if defined(KRB5) && defined(KRB4)
-       if (KerberosV5 && KerberosV4first && retcode != OK) {
-               MK_AUTH_TYPE = "KerberosV5";
-               if (kerb5_password_validate(r, MK_USER, sent_pw)) {
-                       retcode = OK;
-               }
-               else {
-                       retcode = conf->krb_fail_status;
-               }
-       }
-#endif /* KRB5 && KRB4 */
-
        if (conf->krb_authoritative && retcode == DECLINED) {
                return HTTP_UNAUTHORIZED;
        }