/* Copyright (c) 2003 Martin v. Löwis. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. The end-user documentation included with the redistribution, if
any, must include the following acknowledgment: "This product includes
software developed by Martin v. Löwis."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.

4. The hosted project names must not be used to endorse or promote
products derived from this software without prior written
permission. For written permission, please contact martin@v.loewis.de.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL MARTIN V. LÖWIS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* Install with "apxs -i -a -c mod_ssl_user.c" */
/* This module copies some of the SSL authenticated user state to the
   req->user field of the Apache request object. In doing so, it enables
   readers of the field (such as Subversion) to identify the authenticated
   user. A single configuration option, SSLUserName, expects one of the
   mod_ssl variable names. If this variable is not defined by mod_ssl,
   req->user is not modified. If it is defined, prior settings of req->user
   are overwritten.
   Possible configuration:
    SSLUserName SSL_CLIENT_S_DN_CN
    SSLOptions +StdEnvVars
*/
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "http_log.h"
#include "ap_config.h"
#include "apr_strings.h"
#include "apr_tables.h"

module AP_MODULE_DECLARE_DATA ssl_user_module;

typedef struct ssl_user_config_struct {
	char *varname;
}ssl_user_config_rec;

static int ssl_user_handler(request_rec *r)
{
	ssl_user_config_rec *d;
	apr_table_t *env = r->subprocess_env;
	const char *value;

	/* Try to find the module configuration. */
	d = (ssl_user_config_rec *) ap_get_module_config(r->per_dir_config,
							  &ssl_user_module);
	if (!d) {
		/* This should not happen. */
		ap_log_perror(APLOG_MARK, APLOG_ERR, 0, r->pool,
			      "Variable %s is not set", d->varname);
		return DECLINED;
	}

	/* If SSLUserName is not given, do nothing. */
	if (!d->varname) {
		return DECLINED;
	}

	value = apr_table_get(env, d->varname);
	if (!value)
	{
		return DECLINED;
	}

	/* Copy the value if provided. */
	r->user = apr_pstrdup(r->pool, value);
	
	return OK;
}

static void *create_ssl_user_config(apr_pool_t *p, char *dummy)
{
    ssl_user_config_rec *new =
    (ssl_user_config_rec *) apr_pcalloc(p, sizeof(ssl_user_config_rec));
    return new;
}

static void *merge_ssl_user_configs(apr_pool_t *p, void *basev, void *addv)
{
    ssl_user_config_rec *base = (ssl_user_config_rec *) basev;
    ssl_user_config_rec *add = (ssl_user_config_rec *) addv;
    return add->varname?add:base;
}

static const command_rec ssl_user_cmds[] =
{
    AP_INIT_TAKE1("SSLUserName", ap_set_string_slot, 
		  (void *)APR_OFFSETOF(ssl_user_config_rec, varname),
		  OR_AUTHCFG,
                  "a variable to set the user name to"),
    {NULL}
};

static void ssl_user_register_hooks(apr_pool_t *p)
{
    ap_hook_fixups(ssl_user_handler, NULL, NULL, APR_HOOK_LAST);
}

module AP_MODULE_DECLARE_DATA ssl_user_module = {
    STANDARD20_MODULE_STUFF, 
    create_ssl_user_config,
    merge_ssl_user_configs,
    NULL,
    NULL,
    ssl_user_cmds,
    ssl_user_register_hooks
};


