really log minor GSS error messages
[mod_auth_kerb.git] / src / mod_auth_kerb.c
index b0c9ea5..625b8f4 100644 (file)
@@ -72,7 +72,7 @@
 
 #include "config.h"
 
-#define MODAUTHKERB_VERSION "5.0-rc2"
+#define MODAUTHKERB_VERSION "5.0-rc4"
 
 #ifndef APXS1
 #include "ap_compat.h"
@@ -138,6 +138,8 @@ typedef struct {
        char *krb_auth_realms;
        int krb_save_credentials;
        int krb_verify_kdc;
+       char *krb_service_name;
+       int krb_authoritative;
 #ifdef KRB5
        char *krb_5_keytab;
        int krb_method_gssapi;
@@ -177,6 +179,12 @@ static const command_rec kerb_auth_cmds[] = {
    command("KrbVerifyKDC", ap_set_flag_slot, krb_verify_kdc,
      FLAG, "Verify tickets against keytab to prevent KDC spoofing attacks."),
 
+   command("KrbServiceName", ap_set_string_slot, krb_service_name,
+     TAKE1, "Service name to be used by Apache for authentication."),
+
+   command("KrbAuthoritative", ap_set_flag_slot, krb_authoritative,
+     FLAG, "Set to 'off' to allow access control to be passed along to lower modules if the UserID is not known to this module."),
+
 #ifdef KRB5
    command("Krb5Keytab", ap_set_file_slot, krb_5_keytab,
      TAKE1, "Location of Kerberos V5 keytab file."),
@@ -184,7 +192,7 @@ static const command_rec kerb_auth_cmds[] = {
    command("KrbMethodNegotiate", ap_set_flag_slot, krb_method_gssapi,
      FLAG, "Enable Negotiate authentication method."),
 
-   command("KrbMethodK5Pass", ap_set_flag_slot, krb_method_k5pass,
+   command("KrbMethodK5Passwd", ap_set_flag_slot, krb_method_k5pass,
      FLAG, "Enable Kerberos V5 password authentication."),
 #endif 
 
@@ -192,7 +200,7 @@ static const command_rec kerb_auth_cmds[] = {
    command("Krb4Srvtab", ap_set_file_slot, krb_4_srvtab,
      TAKE1, "Location of Kerberos V4 srvtab file."),
 
-   command("KrbMethodK4Pass", ap_set_flag_slot, krb_method_k4pass,
+   command("KrbMethodK4Passwd", ap_set_flag_slot, krb_method_k4pass,
      FLAG, "Enable Kerberos V4 password authentication."),
 #endif
 
@@ -206,6 +214,8 @@ typedef struct {
 } gss_connection_t;
 
 static gss_connection_t *gss_connection = NULL;
+
+static const char *EMPTY_STRING = "\0";
 #endif
 
 
@@ -218,6 +228,8 @@ static void *kerb_dir_create_config(MK_POOL *p, char *d)
 
        rec = (kerb_auth_config *) ap_pcalloc(p, sizeof(kerb_auth_config));
         ((kerb_auth_config *)rec)->krb_verify_kdc = 1;
+       ((kerb_auth_config *)rec)->krb_service_name = "HTTP";
+       ((kerb_auth_config *)rec)->krb_authoritative = 1;
 #ifdef KRB5
        ((kerb_auth_config *)rec)->krb_method_k5pass = 1;
        ((kerb_auth_config *)rec)->krb_method_gssapi = 1;
@@ -239,23 +251,17 @@ void log_rerror(const char *file, int line, int level, int status,
                 const request_rec *r, const char *fmt, ...)
 {
    char errstr[1024];
-   char errnostr[1024];
    va_list ap;
 
    va_start(ap, fmt);
    vsnprintf(errstr, sizeof(errstr), fmt, ap);
    va_end(ap);
 
-   errnostr[0] = '\0';
-   if (errno)
-      snprintf(errnostr, sizeof(errnostr), "%s: (%s)", errstr, strerror(errno));
-   else
-      snprintf(errnostr, sizeof(errnostr), "%s", errstr);
    
 #ifdef APXS1
-   ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errnostr);
+   ap_log_rerror(file, line, level | APLOG_NOERRNO, r, "%s", errstr);
 #else
-   ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errnostr);
+   ap_log_rerror(file, line, level | APLOG_NOERRNO, status, r, "%s", errstr);
 #endif
 }
 
@@ -350,6 +356,7 @@ authenticate_user_krb4pwd(request_rec *r,
    const char *realm;
    char *user;
    char lrealm[REALM_SZ];
+   int all_principals_unkown;
 
    sent_pw = ap_pbase64decode(r->pool, auth_line);
    sent_name = ap_getword (r->pool, &sent_pw, ':');
@@ -380,6 +387,7 @@ authenticate_user_krb4pwd(request_rec *r,
 
    krb_set_tkt_string(tkt_file);
 
+   all_principals_unkown = 1;
    realms = conf->krb_auth_realms;
    do {
       memset(lrealm, 0, sizeof(lrealm));
@@ -389,22 +397,32 @@ authenticate_user_krb4pwd(request_rec *r,
 
       if (realm == NULL) {
         ret = krb_get_lrealm(lrealm, 1);
+        if (ret)
+           break;
         realm = lrealm;
       }
-      if (realm == NULL || *realm == '\0')
-        break;
 
       ret = verify_krb4_user(r, (char *)sent_name, 
                             (sent_instance) ? sent_instance : "",
-                            (char *)realm, (char *)sent_pw, "khttp",
+                            (char *)realm, (char *)sent_pw,
+                            conf->krb_service_name,
                             conf->krb_4_srvtab, conf->krb_verify_kdc);
+      if (!conf->krb_authoritative && ret) {
+        /* if we're not authoritative, we allow authentication to pass on
+         * to another modules if (and only if) the user is not known to us */
+        if (all_principals_unkown && ret != KDC_PR_UNKNOWN)
+           all_principals_unkown = 0;
+      }
+
       if (ret == 0)
         break;
    } while (realms && *realms);
 
    if (ret) {
+      /* XXX log only in the verify_krb4_user() call */
       log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Verifying krb4 password failed");
-      ret = HTTP_UNAUTHORIZED;
+      ret = (!conf->krb_authoritative && all_principals_unkown == 1 && ret == KDC_PR_UNKNOWN) ?
+                DECLINED : HTTP_UNAUTHORIZED;
       goto end;
    }
 
@@ -513,29 +531,30 @@ create_krb5_ccache(krb5_context kcontext,
                   krb5_ccache *ccache)
 {
    char *ccname;
+   int fd;
    krb5_error_code problem;
    int ret;
    krb5_ccache tmp_ccache = NULL;
 
-#ifdef HAVE_KRB5_CC_GEN_NEW
-   problem = krb5_cc_gen_new(kcontext, &krb5_fcc_ops, &tmp_ccache);
-#else
-   /* only older MIT seem to not have the krb5_cc_gen_new() call, so we use
-    * MIT specific call here */
-   problem = krb5_fcc_generate_new(kcontext, &tmp_ccache);
-   /* krb5_fcc_generate_new() doesn't set KRB5_TC_OPENCLOSE, which makes 
-      krb5_cc_initialize() fail */
-   krb5_fcc_set_flags(kcontext, tmp_ccache, KRB5_TC_OPENCLOSE);
-#endif
-   if (problem) {
+   ccname = ap_psprintf(r->pool, "FILE:%s/krb5cc_apache_XXXXXX", P_tmpdir);
+   fd = mkstemp(ccname + strlen("FILE:"));
+   if (fd < 0) {
       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                "Cannot create file for new krb5 ccache: %s",
-                krb5_get_err_text(kcontext, problem));
+                 "mkstemp() failed: %s", strerror(errno));
       ret = HTTP_INTERNAL_SERVER_ERROR;
       goto end;
    }
+   close(fd);
 
-   ccname = ap_pstrdup(r->pool, krb5_cc_get_name(kcontext, tmp_ccache));
+   problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
+   if (problem) {
+      log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                 "krb5_cc_resolve() failed: %s",
+                 krb5_get_err_text(kcontext, problem));
+      ret = HTTP_INTERNAL_SERVER_ERROR;
+      unlink(ccname);
+      goto end;
+   }
 
    problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
    if (problem) {
@@ -618,7 +637,10 @@ int authenticate_user_krb5pwd(request_rec *r,
    krb5_ccache     ccache = NULL;
    krb5_keytab     keytab = NULL;
    int             ret;
-   char *name = NULL;
+   char            *name = NULL;
+   int             all_principals_unkown;
+   char            *ccname = NULL;
+   int             fd;
 
    code = krb5_init_context(&kcontext);
    if (code) {
@@ -635,26 +657,33 @@ int authenticate_user_krb5pwd(request_rec *r,
                 "specifying realm in user name is prohibited");
       ret = HTTP_UNAUTHORIZED;
       goto end;
-   } 
+   }
 
-#ifdef HAVE_KRB5_CC_GEN_NEW
-   code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
-#else
-   /* only older MIT seem to not have the krb5_cc_gen_new() call, so we use
-    * MIT specific call here */
-   code = krb5_mcc_generate_new(kcontext, &ccache);
-#endif
+   /* XXX Heimdal allows to use the MEMORY: type with empty argument ? */
+   ccname = ap_psprintf(r->pool, "MEMORY:%s/krb5cc_apache_XXXXXX", P_tmpdir);
+   fd = mkstemp(ccname + strlen("MEMORY:"));
+   if (fd < 0) {
+      log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                 "mkstemp() failed: %s", strerror(errno));
+      ret = HTTP_INTERNAL_SERVER_ERROR;
+      goto end;
+   }
+   close(fd);
+
+   code = krb5_cc_resolve(kcontext, ccname, &ccache);
    if (code) {
-      log_rerror(APLOG_MARK, APLOG_ERR, 0, r, 
-                "Cannot generate new ccache: %s",
-                krb5_get_err_text(kcontext, code));
+      log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                 "krb5_cc_resolve() failed: %s",
+                 krb5_get_err_text(kcontext, code));
       ret = HTTP_INTERNAL_SERVER_ERROR;
+      unlink(ccname);
       goto end;
    }
 
    if (conf->krb_5_keytab)
       krb5_kt_resolve(kcontext, conf->krb_5_keytab, &keytab);
 
+   all_principals_unkown = 1;
    realms = conf->krb_auth_realms;
    do {
       if (realms && (code = krb5_set_default_realm(kcontext,
@@ -669,8 +698,16 @@ int authenticate_user_krb5pwd(request_rec *r,
       if (code)
         continue;
 
-      code = verify_krb5_user(r, kcontext, client, ccache, sent_pw, "khttp",
+      code = verify_krb5_user(r, kcontext, client, ccache, sent_pw, 
+                             conf->krb_service_name, 
                              keytab, conf->krb_verify_kdc);
+      if (!conf->krb_authoritative && code) {
+        /* if we're not authoritative, we allow authentication to pass on
+         * to another modules if (and only if) the user is not known to us */
+        if (all_principals_unkown && code != KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
+           all_principals_unkown = 0;
+      }
+
       if (code == 0)
         break;
 
@@ -681,10 +718,15 @@ int authenticate_user_krb5pwd(request_rec *r,
    memset((char *)sent_pw, 0, strlen(sent_pw));
 
    if (code) {
+      /* XXX log only in the verify_krb5_user() call */
       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                 "Verifying krb5 password failed: %s",
                 krb5_get_err_text(kcontext, code));
-      ret = HTTP_UNAUTHORIZED;
+      if (!conf->krb_authoritative && all_principals_unkown == 1 && code == KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN)
+        ret = DECLINED;
+      else
+        ret = HTTP_UNAUTHORIZED;
+
       goto end;
    }
 
@@ -721,31 +763,41 @@ end:
  ********************************************************************/
 
 static const char *
-get_gss_error(MK_POOL *p, OM_uint32 error_status, char *prefix)
+get_gss_error(MK_POOL *p, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
 {
-   OM_uint32 maj_stat, min_stat;
+   OM_uint32 maj_stat, min_stat; 
    OM_uint32 msg_ctx = 0;
    gss_buffer_desc status_string;
-   char buf[1024];
+   char *err_msg;
    size_t len;
 
-   snprintf(buf, sizeof(buf), "%s", prefix);
-   len = strlen(buf);
+   err_msg = ap_pstrdup(p, prefix);
    do {
       maj_stat = gss_display_status (&min_stat,
-                                    error_status,
-                                    GSS_C_MECH_CODE,
+                                    err_maj,
+                                    GSS_C_GSS_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;
-      }
+      if (GSS_ERROR(maj_stat))
+        break;
+      err_msg = ap_pstrcat(p, err_msg, ": ", (char*) status_string.value, NULL);
       gss_release_buffer(&min_stat, &status_string);
+      
+      maj_stat = gss_display_status (&min_stat,
+                                    err_min,
+                                    GSS_C_MECH_CODE,
+                                    GSS_C_NULL_OID,
+                                    &msg_ctx,
+                                    &status_string);
+      if (!GSS_ERROR(maj_stat)) {
+        err_msg = ap_pstrcat(p, err_msg,
+                             " (", (char*) status_string.value, ")", NULL);
+        gss_release_buffer(&min_stat, &status_string);
+      }
    } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
 
-   return (ap_pstrdup(p, buf));
+   return err_msg;
 }
 
 static int
@@ -802,7 +854,7 @@ store_gss_creds(request_rec *r, kerb_auth_config *conf, char *princ_name,
    if (GSS_ERROR(maj_stat)) {
       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
         "Cannot store delegated credential (%s)", 
-        get_gss_error(r->pool, min_stat, "gss_krb5_copy_ccache"));
+        get_gss_error(r->pool, maj_stat, min_stat, "gss_krb5_copy_ccache"));
       goto end;
    }
 
@@ -829,7 +881,7 @@ get_gss_creds(request_rec *r,
    gss_name_t server_name = GSS_C_NO_NAME;
    char buf[1024];
 
-   snprintf(buf, sizeof(buf), "%s/%s", "khttp", ap_get_server_name(r));
+   snprintf(buf, sizeof(buf), "%s/%s", conf->krb_service_name, ap_get_server_name(r));
 
    input_token.value = buf;
    input_token.length = strlen(buf) + 1;
@@ -839,7 +891,7 @@ get_gss_creds(request_rec *r,
                                  &server_name);
    if (GSS_ERROR(major_status)) {
       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                "%s", get_gss_error(r->pool, minor_status,
+                "%s", get_gss_error(r->pool, major_status, minor_status,
                 "gss_import_name() failed"));
       return HTTP_INTERNAL_SERVER_ERROR;
    }
@@ -850,7 +902,7 @@ get_gss_creds(request_rec *r,
    gss_release_name(&minor_status2, &server_name);
    if (GSS_ERROR(major_status)) {
       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                "%s", get_gss_error(r->pool, minor_status,
+                "%s", get_gss_error(r->pool, major_status, minor_status,
                                     "gss_acquire_cred() failed"));
       return HTTP_INTERNAL_SERVER_ERROR;
    }
@@ -886,9 +938,8 @@ cmp_gss_type(gss_buffer_t token, gss_OID oid)
 }
 
 static int
-authenticate_user_gss(request_rec *r,
-                     kerb_auth_config *conf,
-                     const char *auth_line)
+authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
+                     const char *auth_line, char **negotiate_ret_value)
 {
   OM_uint32 major_status, minor_status, minor_status2;
   gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
@@ -900,6 +951,8 @@ authenticate_user_gss(request_rec *r,
   OM_uint32 (*accept_sec_token)();
   gss_OID_desc spnego_oid;
 
+  *negotiate_ret_value = (char *)EMPTY_STRING;
+
   spnego_oid.length = 6;
   spnego_oid.elements = (void *)"\x2b\x06\x01\x05\x05\x02";
 
@@ -984,15 +1037,16 @@ authenticate_user_gss(request_rec *r,
      }
      ap_base64encode(token, output_token.value, output_token.length);
      token[len] = '\0';
-     ap_table_set(r->err_headers_out, "WWW-Authenticate",
-                 ap_pstrcat(r->pool, "Negotiate ", token, NULL));
+     *negotiate_ret_value = token;
      gss_release_buffer(&minor_status2, &output_token);
   }
 
   if (GSS_ERROR(major_status)) {
      log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-               "%s", get_gss_error(r->pool, minor_status,
+               "%s", get_gss_error(r->pool, major_status, minor_status,
                                    "gss_accept_sec_context() failed"));
+     /* Don't offer the Negotiate method again if call to GSS layer failed */
+     *negotiate_ret_value = NULL;
      ret = HTTP_UNAUTHORIZED;
      goto end;
   }
@@ -1008,7 +1062,7 @@ authenticate_user_gss(request_rec *r,
   gss_release_name(&minor_status, &client_name); 
   if (GSS_ERROR(major_status)) {
     log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-              "%s", get_gss_error(r->pool, minor_status, 
+              "%s", get_gss_error(r->pool, major_status, minor_status,
                                   "gss_export_name() failed"));
     ret = HTTP_INTERNAL_SERVER_ERROR;
     goto end;
@@ -1053,18 +1107,22 @@ already_succeeded(request_rec *r)
 
 static void
 note_kerb_auth_failure(request_rec *r, const kerb_auth_config *conf,
-                      int use_krb4, int use_krb5)
+                      int use_krb4, int use_krb5, char *negotiate_ret_value)
 {
    const char *auth_name = NULL;
    int set_basic = 0;
+   char *negoauth_param;
 
    /* get the user realm specified in .htaccess */
    auth_name = ap_auth_name(r);
 
    /* XXX should the WWW-Authenticate header be cleared first? */
 #ifdef KRB5
-   if (use_krb5 && conf->krb_method_gssapi)
-      ap_table_add(r->err_headers_out, "WWW-Authenticate", "Negotiate");
+   if (use_krb5 && conf->krb_method_gssapi && negotiate_ret_value != NULL) {
+      negoauth_param = (*negotiate_ret_value == '\0') ? "Negotiate" :
+                 ap_pstrcat(r->pool, "Negotiate ", negotiate_ret_value, NULL);
+      ap_table_add(r->err_headers_out, "WWW-Authenticate", negoauth_param);
+   }
    if (use_krb5 && conf->krb_method_k5pass) {
       ap_table_add(r->err_headers_out, "WWW-Authenticate",
                    ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
@@ -1090,6 +1148,7 @@ int kerb_authenticate_user(request_rec *r)
    int use_krb5 = 0, use_krb4 = 0;
    int ret;
    static int last_return = HTTP_UNAUTHORIZED;
+   char *negotiate_ret_value;
 
    /* get the type specified in .htaccess */
    type = ap_auth_type(r);
@@ -1106,7 +1165,7 @@ int kerb_authenticate_user(request_rec *r)
    /* get what the user sent us in the HTTP header */
    auth_line = MK_TABLE_GET(r->headers_in, "Authorization");
    if (!auth_line) {
-      note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
+      note_kerb_auth_failure(r, conf, use_krb4, use_krb5, "\0");
       return HTTP_UNAUTHORIZED;
    }
    auth_type = ap_getword_white(r->pool, &auth_line);
@@ -1119,7 +1178,7 @@ int kerb_authenticate_user(request_rec *r)
 #ifdef KRB5
    if (use_krb5 && conf->krb_method_gssapi &&
        strcasecmp(auth_type, "Negotiate") == 0) {
-      ret = authenticate_user_gss(r, conf, auth_line);
+      ret = authenticate_user_gss(r, conf, auth_line, &negotiate_ret_value);
    } else if (use_krb5 && conf->krb_method_k5pass &&
              strcasecmp(auth_type, "Basic") == 0) {
        ret = authenticate_user_krb5pwd(r, conf, auth_line);
@@ -1133,7 +1192,7 @@ int kerb_authenticate_user(request_rec *r)
 #endif
 
    if (ret == HTTP_UNAUTHORIZED)
-      note_kerb_auth_failure(r, conf, use_krb4, use_krb5);
+      note_kerb_auth_failure(r, conf, use_krb4, use_krb5, negotiate_ret_value);
 
    last_return = ret;
    return ret;