Blame SOURCES/mod_auth_kerb-5.4-s4u2proxy.patch

b2b3b1
b2b3b1
Add S4U2Proxy feature:
b2b3b1
b2b3b1
http://sourceforge.net/mailarchive/forum.php?thread_name=4EE665D1.3000308%40redhat.com&forum_name=modauthkerb-help
b2b3b1
b2b3b1
The attached patches add support for using s4u2proxy 
b2b3b1
(http://k5wiki.kerberos.org/wiki/Projects/Services4User) to allow the 
b2b3b1
web service to obtain credentials on behalf of the authenticated user.
b2b3b1
b2b3b1
The first patch adds basic support for s4u2proxy. This requires the web 
b2b3b1
administrator to manually create and manage the credentails cache for 
b2b3b1
the apache user (via a cron job, for example).
b2b3b1
b2b3b1
The second patch builds on this and makes mod_auth_kerb manage the 
b2b3b1
ccache instead.
b2b3b1
b2b3b1
These are patches against the current CVS HEAD (mod_auth_krb 5.4).
b2b3b1
b2b3b1
I've added a new module option to enable this support, 
b2b3b1
KrbConstrainedDelegation. The default is off.
b2b3b1
b2b3b1
--- mod_auth_kerb-5.4.orig/README	2008-11-26 11:51:05.000000000 -0500
b2b3b1
+++ mod_auth_kerb-5.4/README	2012-01-04 11:17:22.000000000 -0500
b2b3b1
@@ -122,4 +122,16 @@ KrbSaveCredentials, the tickets will be 
b2b3b1
 credential cache that will be available for the request handler. The ticket
b2b3b1
 file will be removed after request is handled.
b2b3b1
 
b2b3b1
+Constrained Delegation
b2b3b1
+----------------------
b2b3b1
+S4U2Proxy, or constrained delegation, enables a service to use a client's
b2b3b1
+ticket to itself to request another ticket for delegation. The KDC
b2b3b1
+checks krbAllowedToDelegateTo to decide if it will issue a new ticket.
b2b3b1
+If KrbConstrainedDelegation is enabled the server will use its own credentials
b2b3b1
+to retrieve a delegated ticket for the user. For this to work the user must
b2b3b1
+have a forwardable ticket (though the delegation flag need not be set).
b2b3b1
+The server needs a valid credentials cache for this to work.
b2b3b1
+
b2b3b1
+The module itself will obtain and manage the necessary credentials.
b2b3b1
+
b2b3b1
 $Id: README,v 1.12 2008/09/17 14:01:55 baalberith Exp $
b2b3b1
diff -up --recursive mod_auth_kerb-5.4.orig/src/mod_auth_kerb.c mod_auth_kerb-5.4/src/mod_auth_kerb.c
b2b3b1
--- mod_auth_kerb-5.4.orig/src/mod_auth_kerb.c	2011-12-09 17:55:05.000000000 -0500
b2b3b1
+++ mod_auth_kerb-5.4/src/mod_auth_kerb.c	2012-03-01 14:19:40.000000000 -0500
b2b3b1
@@ -42,6 +42,31 @@
b2b3b1
  * POSSIBILITY OF SUCH DAMAGE.
b2b3b1
  */
b2b3b1
 
b2b3b1
+/*
b2b3b1
+ * Locking mechanism inspired by mod_rewrite.
b2b3b1
+ *
b2b3b1
+ * Licensed to the Apache Software Foundation (ASF) under one or more
b2b3b1
+ * contributor license agreements.  See the NOTICE file distributed with
b2b3b1
+ * this work for additional information regarding copyright ownership.
b2b3b1
+ * The ASF licenses this file to You under the Apache License, Version 2.0
b2b3b1
+ * (the "License"); you may not use this file except in compliance with
b2b3b1
+ * the License.  You may obtain a copy of the License at
b2b3b1
+ *
b2b3b1
+ *     http://www.apache.org/licenses/LICENSE-2.0
b2b3b1
+ *
b2b3b1
+ * Unless required by applicable law or agreed to in writing, software
b2b3b1
+ * distributed under the License is distributed on an "AS IS" BASIS,
b2b3b1
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b2b3b1
+ * See the License for the specific language governing permissions and
b2b3b1
+ * limitations under the License.
b2b3b1
+ */
b2b3b1
+
b2b3b1
+/*
b2b3b1
+ * S4U2Proxy code
b2b3b1
+ *
b2b3b1
+ * Copyright (C) 2012  Red Hat
b2b3b1
+ */
b2b3b1
+
b2b3b1
 #ident "$Id: mod_auth_kerb.c,v 1.150 2008/12/04 10:14:03 baalberith Exp $"
b2b3b1
 
b2b3b1
 #include "config.h"
b2b3b1
@@ -49,6 +74,7 @@
b2b3b1
 #include <stdlib.h>
b2b3b1
 #include <stdio.h>
b2b3b1
 #include <stdarg.h>
b2b3b1
+#include <unixd.h>
b2b3b1
 
b2b3b1
 #define MODAUTHKERB_VERSION "5.4"
b2b3b1
 
b2b3b1
@@ -131,6 +157,12 @@ module AP_MODULE_DECLARE_DATA auth_kerb_
b2b3b1
 module auth_kerb_module;
b2b3b1
 #endif
b2b3b1
 
b2b3b1
+#ifdef STANDARD20_MODULE_STUFF
b2b3b1
+/* s4u2proxy only supported in 2.0+ */
b2b3b1
+static const char *lockname;
b2b3b1
+static apr_global_mutex_t *s4u2proxy_lock = NULL;
b2b3b1
+#endif
b2b3b1
+
b2b3b1
 /*************************************************************************** 
b2b3b1
  Macros To Ease Compatibility
b2b3b1
  ***************************************************************************/
b2b3b1
@@ -165,6 +197,7 @@ typedef struct {
b2b3b1
 	int krb_method_gssapi;
b2b3b1
 	int krb_method_k5pass;
b2b3b1
 	int krb5_do_auth_to_local;
b2b3b1
+       int krb5_s4u2proxy;
b2b3b1
 #endif
b2b3b1
 #ifdef KRB4
b2b3b1
 	char *krb_4_srvtab;
b2b3b1
@@ -185,6 +218,11 @@ set_kerb_auth_headers(request_rec *r, co
b2b3b1
 
b2b3b1
 static const char*
b2b3b1
 krb5_save_realms(cmd_parms *cmd, void *sec, const char *arg);
b2b3b1
+static const char *
b2b3b1
+cmd_delegationlock(cmd_parms *cmd, void *dconf, const char *a1);
b2b3b1
+
b2b3b1
+static int
b2b3b1
+obtain_server_credentials(request_rec *r, const char *service_name);
b2b3b1
 
b2b3b1
 #ifdef STANDARD20_MODULE_STUFF
b2b3b1
 #define command(name, func, var, type, usage)           \
b2b3b1
@@ -237,6 +275,12 @@ static const command_rec kerb_auth_cmds[
b2b3b1
 
b2b3b1
    command("KrbLocalUserMapping", ap_set_flag_slot, krb5_do_auth_to_local,
b2b3b1
      FLAG, "Set to 'on' to have Kerberos do auth_to_local mapping of principal names to system user names."),
b2b3b1
+
b2b3b1
+   command("KrbConstrainedDelegation", ap_set_flag_slot, krb5_s4u2proxy,
b2b3b1
+     FLAG, "Set to 'on' to have Kerberos use S4U2Proxy delegation."),
b2b3b1
+
b2b3b1
+    AP_INIT_TAKE1("KrbConstrainedDelegationLock", cmd_delegationlock, NULL,
b2b3b1
+     RSRC_CONF, "the filename of a lockfile used for inter-process synchronization"),
b2b3b1
 #endif 
b2b3b1
 
b2b3b1
 #ifdef KRB4
b2b3b1
@@ -302,6 +346,7 @@ static void *kerb_dir_create_config(MK_P
b2b3b1
 #endif
b2b3b1
 #ifdef KRB5
b2b3b1
   ((kerb_auth_config *)rec)->krb5_do_auth_to_local = 0;
b2b3b1
+	((kerb_auth_config *)rec)->krb5_s4u2proxy = 0;
b2b3b1
 	((kerb_auth_config *)rec)->krb_method_k5pass = 1;
b2b3b1
 	((kerb_auth_config *)rec)->krb_method_gssapi = 1;
b2b3b1
 #endif
b2b3b1
@@ -319,6 +364,24 @@ krb5_save_realms(cmd_parms *cmd, void *v
b2b3b1
    return NULL;
b2b3b1
 }
b2b3b1
 
b2b3b1
+static const char *
b2b3b1
+cmd_delegationlock(cmd_parms *cmd, void *dconf, const char *a1)
b2b3b1
+{
b2b3b1
+    const char *error;
b2b3b1
+
b2b3b1
+    if ((error = ap_check_cmd_context(cmd, GLOBAL_ONLY)) != NULL)
b2b3b1
+        return error;
b2b3b1
+
b2b3b1
+    /* fixup the path, especially for s4u2proxylock_remove() */
b2b3b1
+    lockname = ap_server_root_relative(cmd->pool, a1);
b2b3b1
+
b2b3b1
+    if (!lockname) {
b2b3b1
+        return apr_pstrcat(cmd->pool, "Invalid KrbConstrainedDelegationLock path ", a1, NULL);
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    return NULL;
b2b3b1
+}
b2b3b1
+
b2b3b1
 static void
b2b3b1
 log_rerror(const char *file, int line, int level, int status,
b2b3b1
            const request_rec *r, const char *fmt, ...)
b2b3b1
@@ -1170,6 +1233,7 @@ get_gss_creds(request_rec *r,
b2b3b1
    gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
b2b3b1
    OM_uint32 major_status, minor_status, minor_status2;
b2b3b1
    gss_name_t server_name = GSS_C_NO_NAME;
b2b3b1
+   gss_cred_usage_t usage = GSS_C_ACCEPT;
b2b3b1
    char buf[1024];
b2b3b1
    int have_server_princ;
b2b3b1
 
b2b3b1
@@ -1212,10 +1276,14 @@ get_gss_creds(request_rec *r,
b2b3b1
 
b2b3b1
    log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "Acquiring creds for %s",
b2b3b1
 	      token.value);
b2b3b1
+   if (conf->krb5_s4u2proxy) {
b2b3b1
+       usage = GSS_C_BOTH;
b2b3b1
+       obtain_server_credentials(r, conf->krb_service_name);
b2b3b1
+   }
b2b3b1
    gss_release_buffer(&minor_status, &token);
b2b3b1
    
b2b3b1
    major_status = gss_acquire_cred(&minor_status, server_name, GSS_C_INDEFINITE,
b2b3b1
-			           GSS_C_NO_OID_SET, GSS_C_ACCEPT,
b2b3b1
+			           GSS_C_NO_OID_SET, usage,
b2b3b1
 				   server_creds, NULL, NULL);
b2b3b1
    gss_release_name(&minor_status2, &server_name);
b2b3b1
    if (GSS_ERROR(major_status)) {
b2b3b1
@@ -1257,6 +1325,293 @@ cmp_gss_type(gss_buffer_t token, gss_OID
b2b3b1
 }
b2b3b1
 #endif
b2b3b1
 
b2b3b1
+/* Renew the ticket if it will expire in under a minute */
b2b3b1
+#define RENEWAL_TIME 60
b2b3b1
+
b2b3b1
+/*
b2b3b1
+ * Services4U2Proxy lets a server prinicipal request another service
b2b3b1
+ * principal on behalf of a user. To do this the Apache service needs
b2b3b1
+ * to have its own ccache. This will ensure that the ccache has a valid
b2b3b1
+ * principal and will initialize or renew new credentials when needed.
b2b3b1
+ */
b2b3b1
+
b2b3b1
+static int
b2b3b1
+verify_server_credentials(request_rec *r,
b2b3b1
+                          krb5_context kcontext,
b2b3b1
+                          krb5_ccache ccache,
b2b3b1
+                          krb5_principal princ,
b2b3b1
+                          int *renew
b2b3b1
+)
b2b3b1
+{
b2b3b1
+    krb5_creds match_cred;
b2b3b1
+    krb5_creds creds;
b2b3b1
+    char * princ_name = NULL;
b2b3b1
+    char *tgs_princ_name = NULL;
b2b3b1
+    krb5_timestamp now;
b2b3b1
+    krb5_error_code kerr = 0;
b2b3b1
+
b2b3b1
+    *renew = 0;
b2b3b1
+
b2b3b1
+    memset (&match_cred, 0, sizeof(match_cred));
b2b3b1
+    memset (&creds, 0, sizeof(creds));
b2b3b1
+
b2b3b1
+    if (NULL == ccache || NULL == princ) {
b2b3b1
+        /* Nothing to verify */
b2b3b1
+        *renew = 1;
b2b3b1
+        goto cleanup;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    if ((kerr = krb5_unparse_name(kcontext, princ, &princ_name))) {
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b2b3b1
+                   "Could not unparse principal %s (%d)",
b2b3b1
+                   error_message(kerr), kerr);
b2b3b1
+        goto cleanup;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
b2b3b1
+        "Using principal %s for s4u2proxy", princ_name);
b2b3b1
+
b2b3b1
+    tgs_princ_name = apr_psprintf(r->pool, "%s/%.*s@%.*s", KRB5_TGS_NAME,
b2b3b1
+                                  krb5_princ_realm(kcontext, princ)->length,
b2b3b1
+                                  krb5_princ_realm(kcontext, princ)->data,
b2b3b1
+                                  krb5_princ_realm(kcontext, princ)->length,
b2b3b1
+                                  krb5_princ_realm(kcontext, princ)->data);
b2b3b1
+
b2b3b1
+    if ((kerr = krb5_parse_name(kcontext, tgs_princ_name, &match_cred.server))) 
b2b3b1
+    {
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b2b3b1
+                        "Could not parse principal %s: %s (%d)",
b2b3b1
+                        tgs_princ_name, error_message(kerr), kerr);
b2b3b1
+        goto cleanup;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    match_cred.client = princ;
b2b3b1
+
b2b3b1
+    if ((kerr = krb5_cc_retrieve_cred(kcontext, ccache, 0, &match_cred, &creds)))
b2b3b1
+    {
b2b3b1
+        krb5_unparse_name(kcontext, princ, &princ_name);
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
b2b3b1
+                   "Could not unparse principal %s: %s (%d)",
b2b3b1
+                   princ_name, error_message(kerr), kerr);
b2b3b1
+        goto cleanup;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    if ((kerr = krb5_timeofday(kcontext, &now))) {
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b2b3b1
+                        "Could not get current time: %d (%s)",
b2b3b1
+                        kerr, error_message(kerr));
b2b3b1
+        goto cleanup;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    if (now > (creds.times.endtime + RENEWAL_TIME)) {
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b2b3b1
+                   "Credentials for %s have expired or will soon "
b2b3b1
+                   "expire - now %d endtime %d",
b2b3b1
+                   princ_name, now, creds.times.endtime);
b2b3b1
+        *renew = 1;
b2b3b1
+    } else {
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
b2b3b1
+                   "Credentials for %s will expire at "
b2b3b1
+                   "%d, it is now %d", princ_name, creds.times.endtime, now);
b2b3b1
+    }
b2b3b1
+
b2b3b1
+cleanup:
b2b3b1
+    /* Closing context, ccache, etc happens elsewhere */
b2b3b1
+    if (match_cred.server) {
b2b3b1
+        krb5_free_principal(kcontext, match_cred.server);
b2b3b1
+    }
b2b3b1
+    if (creds.client) {
b2b3b1
+        krb5_free_cred_contents(kcontext, &creds);
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    return kerr;
b2b3b1
+}
b2b3b1
+
b2b3b1
+static int
b2b3b1
+obtain_server_credentials(request_rec *r,
b2b3b1
+                          const char *service_name)
b2b3b1
+{
b2b3b1
+    krb5_context kcontext = NULL;
b2b3b1
+    krb5_keytab keytab = NULL;
b2b3b1
+    krb5_ccache ccache = NULL;
b2b3b1
+    char * princ_name = NULL;
b2b3b1
+    char *tgs_princ_name = NULL;
b2b3b1
+    krb5_error_code kerr = 0;
b2b3b1
+    krb5_principal princ = NULL;
b2b3b1
+    krb5_creds creds;
b2b3b1
+    krb5_get_init_creds_opt gicopts;
b2b3b1
+    int renew = 0;
b2b3b1
+    apr_status_t rv = 0;
b2b3b1
+
b2b3b1
+    memset(&creds, 0, sizeof(creds));
b2b3b1
+
b2b3b1
+    if ((kerr = krb5_init_context(&kcontext))) {
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b2b3b1
+            "Kerberos context initialization failed: %s (%d)", error_message(kerr), kerr);
b2b3b1
+        goto done;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    if ((kerr = krb5_cc_default(kcontext, &ccache))) {
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b2b3b1
+                        "Could not get default Kerberos ccache: %s (%d)",
b2b3b1
+                        error_message(kerr), kerr);
b2b3b1
+        goto done;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    if ((kerr = krb5_cc_get_principal(kcontext, ccache, &princ))) {
b2b3b1
+        char * name = NULL;
b2b3b1
+
b2b3b1
+        if ((asprintf(&name, "%s:%s", krb5_cc_get_type(kcontext, ccache),
b2b3b1
+          krb5_cc_get_name(kcontext, ccache))) == -1) {
b2b3b1
+            kerr = KRB5_CC_NOMEM;
b2b3b1
+            goto done;
b2b3b1
+        }
b2b3b1
+
b2b3b1
+        if (KRB5_FCC_NOFILE == kerr) {
b2b3b1
+            log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
b2b3b1
+                       "Credentials cache %s not found, create one", name);
b2b3b1
+            krb5_cc_close(kcontext, ccache);
b2b3b1
+            ccache = NULL;
b2b3b1
+            free(name);
b2b3b1
+        } else {
b2b3b1
+            log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b2b3b1
+                       "Failure to open credentials cache %s: %s (%d)",
b2b3b1
+                       name, error_message(kerr), kerr);
b2b3b1
+            free(name);
b2b3b1
+            goto done;
b2b3b1
+        }
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    kerr = verify_server_credentials(r, kcontext, ccache, princ, &renew);
b2b3b1
+
b2b3b1
+    if (kerr || !renew) {
b2b3b1
+        goto done;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+#ifdef STANDARD20_MODULE_STUFF
b2b3b1
+    if (s4u2proxy_lock) {
b2b3b1
+        rv = apr_global_mutex_lock(s4u2proxy_lock);
b2b3b1
+        if (rv != APR_SUCCESS) {
b2b3b1
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
b2b3b1
+                          "apr_global_mutex_lock(s4u2proxy_lock) "
b2b3b1
+                          "failed");
b2b3b1
+        }
b2b3b1
+    }
b2b3b1
+#endif
b2b3b1
+
b2b3b1
+    /* We have the lock, check again to be sure another process hasn't already
b2b3b1
+     * renewed the ticket.
b2b3b1
+     */
b2b3b1
+    kerr = verify_server_credentials(r, kcontext, ccache, princ, &renew);
b2b3b1
+    if (kerr || !renew) {
b2b3b1
+        goto unlock;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    if (NULL == princ) {
b2b3b1
+        princ_name = apr_psprintf(r->pool, "%s/%s",
b2b3b1
+            (service_name) ? service_name : SERVICE_NAME,
b2b3b1
+            ap_get_server_name(r));
b2b3b1
+
b2b3b1
+        if ((kerr = krb5_parse_name(kcontext, princ_name, &princ))) {
b2b3b1
+            log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b2b3b1
+                       "Could not parse principal %s: %s (%d) ",
b2b3b1
+                       princ_name, error_message(kerr), kerr);
b2b3b1
+            goto unlock;
b2b3b1
+        }
b2b3b1
+    } else if (NULL == princ_name) {
b2b3b1
+        if ((kerr = krb5_unparse_name(kcontext, princ, &princ_name))) {
b2b3b1
+            log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
b2b3b1
+                       "Could not unparse principal %s: %s (%d)",
b2b3b1
+                       princ_name, error_message(kerr), kerr);
b2b3b1
+            goto unlock;
b2b3b1
+        }
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    if ((kerr = krb5_kt_default(kcontext, &keytab))) {
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b2b3b1
+                   "Unable to get default keytab: %s (%d)",
b2b3b1
+                   error_message(kerr), kerr);
b2b3b1
+        goto unlock;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
b2b3b1
+               "Obtaining new credentials for %s", princ_name);
b2b3b1
+    krb5_get_init_creds_opt_init(&gicopts);
b2b3b1
+    krb5_get_init_creds_opt_set_forwardable(&gicopts, 1);
b2b3b1
+
b2b3b1
+    tgs_princ_name = apr_psprintf(r->pool, "%s/%.*s@%.*s", KRB5_TGS_NAME,
b2b3b1
+                                  krb5_princ_realm(kcontext, princ)->length,
b2b3b1
+                                  krb5_princ_realm(kcontext, princ)->data,
b2b3b1
+                                  krb5_princ_realm(kcontext, princ)->length,
b2b3b1
+                                  krb5_princ_realm(kcontext, princ)->data);
b2b3b1
+
b2b3b1
+    if ((kerr = krb5_get_init_creds_keytab(kcontext, &creds, princ, keytab,
b2b3b1
+         0, tgs_princ_name, &gicopts))) {
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b2b3b1
+                   "Failed to obtain credentials for principal %s: "
b2b3b1
+                   "%s (%d)", princ_name, error_message(kerr), kerr);
b2b3b1
+        goto unlock;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    krb5_kt_close(kcontext, keytab);
b2b3b1
+    keytab = NULL;
b2b3b1
+
b2b3b1
+    if (NULL == ccache) {
b2b3b1
+        if ((kerr = krb5_cc_default(kcontext, &ccache))) {
b2b3b1
+            log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b2b3b1
+                      "Failed to open default ccache: %s (%d)",
b2b3b1
+                      error_message(kerr), kerr);
b2b3b1
+            goto unlock;
b2b3b1
+        }
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    if ((kerr = krb5_cc_initialize(kcontext, ccache, princ))) {
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b2b3b1
+                   "Failed to initialize ccache for %s: %s (%d)",
b2b3b1
+                   princ_name, error_message(kerr), kerr);
b2b3b1
+        goto unlock;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    if ((kerr = krb5_cc_store_cred(kcontext, ccache, &creds))) {
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b2b3b1
+                   "Failed to store %s in ccache: %s (%d)",
b2b3b1
+                   princ_name, error_message(kerr), kerr);
b2b3b1
+        goto unlock;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+unlock:
b2b3b1
+#ifdef STANDARD20_MODULE_STUFF
b2b3b1
+    if (s4u2proxy_lock) {
b2b3b1
+        apr_global_mutex_unlock(s4u2proxy_lock);
b2b3b1
+        if (rv != APR_SUCCESS) {
b2b3b1
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
b2b3b1
+                          "apr_global_mutex_unlock(s4u2proxy_lock) "
b2b3b1
+                          "failed");
b2b3b1
+        }
b2b3b1
+    }
b2b3b1
+#endif
b2b3b1
+
b2b3b1
+done:
b2b3b1
+    if (0 == kerr)
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
b2b3b1
+                   "Done obtaining credentials for s4u2proxy");
b2b3b1
+    else
b2b3b1
+        log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
b2b3b1
+                   "Failed to obtain credentials for s4u2proxy");
b2b3b1
+
b2b3b1
+    if (creds.client) {
b2b3b1
+        krb5_free_cred_contents(kcontext, &creds);
b2b3b1
+    }
b2b3b1
+    if (ccache) {
b2b3b1
+        krb5_cc_close(kcontext, ccache);
b2b3b1
+    }
b2b3b1
+    if (kcontext) {
b2b3b1
+        krb5_free_context(kcontext);
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    return kerr;
b2b3b1
+}
b2b3b1
+
b2b3b1
 static int
b2b3b1
 authenticate_user_gss(request_rec *r, kerb_auth_config *conf,
b2b3b1
 		      const char *auth_line, char **negotiate_ret_value)
b2b3b1
@@ -1697,10 +2052,60 @@ have_rcache_type(const char *type)
b2b3b1
 /*************************************************************************** 
b2b3b1
  Module Setup/Configuration
b2b3b1
  ***************************************************************************/
b2b3b1
+#ifdef STANDARD20_MODULE_STUFF
b2b3b1
+static apr_status_t
b2b3b1
+s4u2proxylock_create(server_rec *s, apr_pool_t *p)
b2b3b1
+{
b2b3b1
+    apr_status_t rc;
b2b3b1
+
b2b3b1
+    /* only operate if a lockfile is used */
b2b3b1
+    if (lockname == NULL || *(lockname) == '\0') {
b2b3b1
+        return APR_SUCCESS;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    /* create the lockfile */
b2b3b1
+    rc = apr_global_mutex_create(&s4u2proxy_lock, lockname,
b2b3b1
+                                 APR_LOCK_DEFAULT, p);
b2b3b1
+    if (rc != APR_SUCCESS) {
b2b3b1
+        ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s,
b2b3b1
+                     "Parent could not create lock file %s", lockname);
b2b3b1
+        return rc;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+#ifdef AP_NEED_SET_MUTEX_PERMS
b2b3b1
+    rc = unixd_set_global_mutex_perms(s4u2proxy_lock);
b2b3b1
+    if (rc != APR_SUCCESS) {
b2b3b1
+        ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s,
b2b3b1
+                     "mod_auth_kerb: Parent could not set permissions "
b2b3b1
+                     "on lock; check User and Group directives");
b2b3b1
+        return rc;
b2b3b1
+    }
b2b3b1
+#endif
b2b3b1
+
b2b3b1
+    return APR_SUCCESS;
b2b3b1
+}
b2b3b1
+
b2b3b1
+static apr_status_t
b2b3b1
+s4u2proxylock_remove(void *unused)
b2b3b1
+{
b2b3b1
+    /* only operate if a lockfile is used */
b2b3b1
+    if (lockname == NULL || *(lockname) == '\0') {
b2b3b1
+        return APR_SUCCESS;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    /* destroy the rewritelock */
b2b3b1
+    apr_global_mutex_destroy(s4u2proxy_lock);
b2b3b1
+    s4u2proxy_lock = NULL;
b2b3b1
+    lockname = NULL;
b2b3b1
+    return APR_SUCCESS;
b2b3b1
+}
b2b3b1
+#endif
b2b3b1
+
b2b3b1
 #ifndef STANDARD20_MODULE_STUFF
b2b3b1
 static void
b2b3b1
 kerb_module_init(server_rec *dummy, pool *p)
b2b3b1
 {
b2b3b1
+   apr_status_t status;
b2b3b1
 #ifndef HEIMDAL
b2b3b1
    /* Suppress the MIT replay cache.  Requires MIT Kerberos 1.4.0 or later.
b2b3b1
       1.3.x are covered by the hack overiding the replay calls */
b2b3b1
@@ -1741,6 +2146,7 @@ static int
b2b3b1
 kerb_init_handler(apr_pool_t *p, apr_pool_t *plog,
b2b3b1
       		  apr_pool_t *ptemp, server_rec *s)
b2b3b1
 {
b2b3b1
+   apr_status_t rv;
b2b3b1
    ap_add_version_component(p, "mod_auth_kerb/" MODAUTHKERB_VERSION);
b2b3b1
 #ifndef HEIMDAL
b2b3b1
    /* Suppress the MIT replay cache.  Requires MIT Kerberos 1.4.0 or later.
b2b3b1
@@ -1748,14 +2154,41 @@ kerb_init_handler(apr_pool_t *p, apr_poo
b2b3b1
    if (getenv("KRB5RCACHETYPE") == NULL && have_rcache_type("none"))
b2b3b1
       putenv(strdup("KRB5RCACHETYPE=none"));
b2b3b1
 #endif
b2b3b1
+#ifdef STANDARD20_MODULE_STUFF
b2b3b1
+    rv = s4u2proxylock_create(s, p);
b2b3b1
+    if (rv != APR_SUCCESS) {
b2b3b1
+        return HTTP_INTERNAL_SERVER_ERROR;
b2b3b1
+    }
b2b3b1
+
b2b3b1
+    apr_pool_cleanup_register(p, (void *)s, s4u2proxylock_remove,
b2b3b1
+                              apr_pool_cleanup_null);
b2b3b1
+#endif
b2b3b1
    
b2b3b1
    return OK;
b2b3b1
 }
b2b3b1
 
b2b3b1
 static void
b2b3b1
+initialize_child(apr_pool_t *p, server_rec *s)
b2b3b1
+{
b2b3b1
+    apr_status_t rv = 0;
b2b3b1
+
b2b3b1
+#ifdef STANDARD20_MODULE_STUFF
b2b3b1
+    if (lockname != NULL && *(lockname) != '\0') {
b2b3b1
+        rv = apr_global_mutex_child_init(&s4u2proxy_lock, lockname, p);
b2b3b1
+        if (rv != APR_SUCCESS) {
b2b3b1
+            ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
b2b3b1
+                         "mod_auth_kerb: could not init s4u2proxy_lock"
b2b3b1
+                         " in child");
b2b3b1
+        }
b2b3b1
+    }
b2b3b1
+#endif
b2b3b1
+}
b2b3b1
+
b2b3b1
+static void
b2b3b1
 kerb_register_hooks(apr_pool_t *p)
b2b3b1
 {
b2b3b1
    ap_hook_post_config(kerb_init_handler, NULL, NULL, APR_HOOK_MIDDLE);
b2b3b1
+   ap_hook_child_init(initialize_child, NULL, NULL, APR_HOOK_MIDDLE);
b2b3b1
    ap_hook_check_user_id(kerb_authenticate_user, NULL, NULL, APR_HOOK_MIDDLE);
b2b3b1
 }
b2b3b1