Use different calls when generating memory ccache with Heimdal or MIT
[mod_auth_kerb.cvs/.git] / src / mod_auth_kerb.c
index 9dd6169..e3f0708 100644 (file)
@@ -72,7 +72,7 @@
 
 #include "config.h"
 
-#define MODAUTHKERB_VERSION "5.0-rc3"
+#define MODAUTHKERB_VERSION "5.0-rc4"
 
 #ifndef APXS1
 #include "ap_compat.h"
@@ -123,6 +123,7 @@ module AP_MODULE_DECLARE_DATA auth_kerb_module;
 #define MK_TABLE_GET ap_table_get
 #define MK_USER r->connection->user
 #define MK_AUTH_TYPE r->connection->ap_auth_type
+#define PROXYREQ_PROXY STD_PROXY
 #else
 #define MK_POOL apr_pool_t
 #define MK_TABLE_GET apr_table_get
@@ -192,7 +193,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 
 
@@ -200,7 +201,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
 
@@ -214,6 +215,8 @@ typedef struct {
 } gss_connection_t;
 
 static gss_connection_t *gss_connection = NULL;
+
+static const char *EMPTY_STRING = "\0";
 #endif
 
 
@@ -249,23 +252,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
 }
 
@@ -663,21 +660,21 @@ int authenticate_user_krb5pwd(request_rec *r,
       goto end;
    }
 
-   /* 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) {
+   if (sent_pw == NULL || *sent_pw == '\0') {
       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                 "mkstemp() failed: %s", strerror(errno));
-      ret = HTTP_INTERNAL_SERVER_ERROR;
+                "empty passwords are not accepted");
+      ret = HTTP_UNAUTHORIZED;
       goto end;
    }
-   close(fd);
 
-   code = krb5_cc_resolve(kcontext, ccname, &ccache);
+#ifdef HEIMDAL
+   code = krb5_cc_gen_new(kcontext, &krb5_mcc_ops, &ccache);
+#else
+   code = krb5_cc_resolve(kcontext, "MEMORY:", &ccache);
+#endif
    if (code) {
       log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                 "krb5_cc_resolve() failed: %s",
+                "generating new memory ccache failed: %s",
                  krb5_get_err_text(kcontext, code));
       ret = HTTP_INTERNAL_SERVER_ERROR;
       unlink(ccname);
@@ -783,20 +780,22 @@ get_gss_error(MK_POOL *p, OM_uint32 err_maj, OM_uint32 err_min, char *prefix)
                                     GSS_C_NO_OID,
                                     &msg_ctx,
                                     &status_string);
+      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);
       
-      if (GSS_ERROR(maj_stat) || msg_ctx == 0)
-        break;
-
       maj_stat = gss_display_status (&min_stat,
                                     err_min,
                                     GSS_C_MECH_CODE,
                                     GSS_C_NULL_OID,
                                     &msg_ctx,
                                     &status_string);
-      err_msg = ap_pstrcat(p, err_msg, ": ", (char*) status_string.value, NULL);
-      gss_release_buffer(&min_stat, &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 err_msg;
@@ -940,9 +939,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;
@@ -954,6 +952,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";
 
@@ -1038,8 +1038,7 @@ 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);
   }
 
@@ -1047,9 +1046,9 @@ authenticate_user_gss(request_rec *r,
      log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                "%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;
-     /* XXX in this case the server hasn't to return to the client the
-      * Negotiate method again! */
      goto end;
   }
 
@@ -1109,29 +1108,35 @@ 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;
+   const char *header_name = 
+      (r->proxyreq == PROXYREQ_PROXY) ? "Proxy-Authenticate" : "WWW-Authenticate";
 
    /* 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, header_name, 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));
+      ap_table_add(r->err_headers_out, header_name,
+                  ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
       set_basic = 1;
    }
 #endif
 
 #ifdef KRB4
    if (use_krb4 && conf->krb_method_k4pass && !set_basic)
-      ap_table_add(r->err_headers_out, "WWW-Authenticate",
-                  ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
+      ap_table_add(r->err_headers_out, header_name,
+                 ap_pstrcat(r->pool, "Basic realm=\"", auth_name, "\"", NULL));
 #endif
 }
 
@@ -1146,6 +1151,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);
@@ -1162,8 +1168,11 @@ 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);
-      return HTTP_UNAUTHORIZED;
+       auth_line = MK_TABLE_GET(r->headers_in, "Proxy-Authorization");
+       if (!auth_line) {
+               note_kerb_auth_failure(r, conf, use_krb4, use_krb5, "\0");
+               return HTTP_UNAUTHORIZED;
+       }
    }
    auth_type = ap_getword_white(r->pool, &auth_line);
 
@@ -1175,7 +1184,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);
@@ -1189,7 +1198,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;