X-Git-Url: http://andersk.mit.edu/gitweb/mod-vhost-ldap.git/blobdiff_plain/7f9875bb3e8210122b73b7a63644d039733d04d6..HEAD:/mod_vhost_ldap.c diff --git a/mod_vhost_ldap.c b/mod_vhost_ldap.c index c035166..f3b729a 100644 --- a/mod_vhost_ldap.c +++ b/mod_vhost_ldap.c @@ -20,6 +20,8 @@ * mod_vhost_ldap.c --- read virtual host config from LDAP directory */ +#define CORE_PRIVATE + #include #include "httpd.h" @@ -27,12 +29,13 @@ #include "http_core.h" #include "http_log.h" #include "http_request.h" +#include "apr_version.h" #include "apr_ldap.h" #include "apr_strings.h" #include "apr_reslist.h" #include "util_ldap.h" -#ifndef APU_HAS_LDAP +#if !defined(APU_HAS_LDAP) && !defined(APR_HAS_LDAP) #error mod_vhost_ldap requires APR-util to have LDAP support built in #endif @@ -44,17 +47,17 @@ #include "unixd.h" /* Contains the suexec_identity hook used on Unix */ #endif -#define MIN_UID 1000 -#define MIN_GID 1000 +#define MIN_UID 100 +#define MIN_GID 100 module AP_MODULE_DECLARE_DATA vhost_ldap_module; +typedef enum { + MVL_UNSET, MVL_DISABLED, MVL_ENABLED +} mod_vhost_ldap_status_e; + typedef struct mod_vhost_ldap_config_t { - apr_pool_t *pool; /* Pool that this config is allocated from */ -#if APR_HAS_THREADS - apr_thread_mutex_t *lock; /* Lock for this config */ -#endif - int enabled; /* Is vhost_ldap enabled? */ + mod_vhost_ldap_status_e enabled; /* Is vhost_ldap enabled? */ /* These parameters are all derived from the VhostLDAPURL directive */ char *url; /* String representation of LDAP URL */ @@ -69,9 +72,13 @@ typedef struct mod_vhost_ldap_config_t { char *binddn; /* DN to bind to server (can be NULL) */ char *bindpw; /* Password to bind to server (can be NULL) */ + int have_deref; /* Set if we have found an Deref option */ int have_ldap_url; /* Set if we have found an LDAP url */ int secure; /* True if SSL connections are requested */ + + char *fallback; /* Fallback virtual host */ + } mod_vhost_ldap_config_t; typedef struct mod_vhost_ldap_request_t { @@ -79,13 +86,34 @@ typedef struct mod_vhost_ldap_request_t { char *name; /* ServerName */ char *admin; /* ServerAdmin */ char *docroot; /* DocumentRoot */ - char *cgiroot; /* ScripAlias */ + char *cgiroot; /* ScriptAlias */ char *uid; /* Suexec Uid */ char *gid; /* Suexec Gid */ } mod_vhost_ldap_request_t; char *attributes[] = - { "apacheServerName", "apacheServerAdmin", "apacheDocumentRoot", "apacheScriptAlias", "apacheSuexecUid", "apacheSuexecGid", 0 }; + { "apacheServerName", "apacheDocumentRoot", "apacheScriptAlias", "apacheSuexecUid", "apacheSuexecGid", "apacheServerAdmin", 0 }; + +#if (APR_MAJOR_VERSION >= 1) +static APR_OPTIONAL_FN_TYPE(uldap_connection_close) *util_ldap_connection_close; +static APR_OPTIONAL_FN_TYPE(uldap_connection_find) *util_ldap_connection_find; +static APR_OPTIONAL_FN_TYPE(uldap_cache_comparedn) *util_ldap_cache_comparedn; +static APR_OPTIONAL_FN_TYPE(uldap_cache_compare) *util_ldap_cache_compare; +static APR_OPTIONAL_FN_TYPE(uldap_cache_checkuserid) *util_ldap_cache_checkuserid; +static APR_OPTIONAL_FN_TYPE(uldap_cache_getuserdn) *util_ldap_cache_getuserdn; +static APR_OPTIONAL_FN_TYPE(uldap_ssl_supported) *util_ldap_ssl_supported; + +static void ImportULDAPOptFn(void) +{ + util_ldap_connection_close = APR_RETRIEVE_OPTIONAL_FN(uldap_connection_close); + util_ldap_connection_find = APR_RETRIEVE_OPTIONAL_FN(uldap_connection_find); + util_ldap_cache_comparedn = APR_RETRIEVE_OPTIONAL_FN(uldap_cache_comparedn); + util_ldap_cache_compare = APR_RETRIEVE_OPTIONAL_FN(uldap_cache_compare); + util_ldap_cache_checkuserid = APR_RETRIEVE_OPTIONAL_FN(uldap_cache_checkuserid); + util_ldap_cache_getuserdn = APR_RETRIEVE_OPTIONAL_FN(uldap_cache_getuserdn); + util_ldap_ssl_supported = APR_RETRIEVE_OPTIONAL_FN(uldap_ssl_supported); +} +#endif static int mod_vhost_ldap_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) { @@ -98,7 +126,7 @@ static int mod_vhost_ldap_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_ } - ap_add_version_component(p, "mod_vhost_ldap/0.2.1"); + ap_add_version_component(p, MOD_VHOST_LDAP_VERSION); return OK; } @@ -106,25 +134,67 @@ static int mod_vhost_ldap_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_ static void * mod_vhost_ldap_create_server_config (apr_pool_t *p, server_rec *s) { - mod_vhost_ldap_config_t *cfg = + mod_vhost_ldap_config_t *conf = (mod_vhost_ldap_config_t *)apr_pcalloc(p, sizeof (mod_vhost_ldap_config_t)); - cfg->pool = p; + conf->enabled = MVL_UNSET; + conf->have_ldap_url = 0; + conf->have_deref = 0; + conf->binddn = NULL; + conf->bindpw = NULL; + conf->deref = always; + conf->fallback = NULL; -#if APR_HAS_THREADS - apr_thread_mutex_create(&cfg->lock, APR_THREAD_MUTEX_DEFAULT, p); -#endif + return conf; +} + +static void * +mod_vhost_ldap_merge_server_config(apr_pool_t *p, void *parentv, void *childv) +{ + mod_vhost_ldap_config_t *parent = (mod_vhost_ldap_config_t *) parentv; + mod_vhost_ldap_config_t *child = (mod_vhost_ldap_config_t *) childv; + mod_vhost_ldap_config_t *conf = + (mod_vhost_ldap_config_t *)apr_pcalloc(p, sizeof(mod_vhost_ldap_config_t)); + + if (child->enabled == MVL_UNSET) { + conf->enabled = parent->enabled; + } else { + conf->enabled = child->enabled; + } + + if (child->have_ldap_url) { + conf->have_ldap_url = child->have_ldap_url; + conf->url = child->url; + conf->host = child->host; + conf->port = child->port; + conf->basedn = child->basedn; + conf->scope = child->scope; + conf->filter = child->filter; + conf->secure = child->secure; + } else { + conf->have_ldap_url = parent->have_ldap_url; + conf->url = parent->url; + conf->host = parent->host; + conf->port = parent->port; + conf->basedn = parent->basedn; + conf->scope = parent->scope; + conf->filter = parent->filter; + conf->secure = parent->secure; + } + if (child->have_deref) { + conf->have_deref = child->have_deref; + conf->deref = child->deref; + } else { + conf->have_deref = parent->have_deref; + conf->deref = parent->deref; + } + + conf->binddn = (child->binddn ? child->binddn : parent->binddn); + conf->bindpw = (child->bindpw ? child->bindpw : parent->bindpw); - cfg->enabled = 0; - cfg->have_ldap_url = 0; - cfg->url = ""; - cfg->host = NULL; - cfg->binddn = NULL; - cfg->bindpw = NULL; - cfg->deref = always; - cfg->secure = 0; + conf->fallback = (child->fallback ? child->fallback : parent->fallback); - return cfg; + return conf; } /* @@ -137,31 +207,41 @@ static const char *mod_vhost_ldap_parse_url(cmd_parms *cmd, { int result; apr_ldap_url_desc_t *urld; +#if (APR_MAJOR_VERSION >= 1) + apr_ldap_err_t *result_err; +#endif - mod_vhost_ldap_config_t *cfg = + mod_vhost_ldap_config_t *conf = (mod_vhost_ldap_config_t *)ap_get_module_config(cmd->server->module_config, &vhost_ldap_module); ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, "[mod_vhost_ldap.c] url parse: `%s'", url); - + +#if (APR_MAJOR_VERSION >= 1) /* for apache >= 2.2 */ + result = apr_ldap_url_parse(cmd->pool, url, &(urld), &(result_err)); + if (result != LDAP_SUCCESS) { + return result_err->reason; + } +#else result = apr_ldap_url_parse(url, &(urld)); if (result != LDAP_SUCCESS) { switch (result) { - case LDAP_URL_ERR_NOTLDAP: - return "LDAP URL does not begin with ldap://"; - case LDAP_URL_ERR_NODN: - return "LDAP URL does not have a DN"; - case LDAP_URL_ERR_BADSCOPE: - return "LDAP URL has an invalid scope"; - case LDAP_URL_ERR_MEM: - return "Out of memory parsing LDAP URL"; - default: - return "Could not parse LDAP URL"; + case LDAP_URL_ERR_NOTLDAP: + return "LDAP URL does not begin with ldap://"; + case LDAP_URL_ERR_NODN: + return "LDAP URL does not have a DN"; + case LDAP_URL_ERR_BADSCOPE: + return "LDAP URL has an invalid scope"; + case LDAP_URL_ERR_MEM: + return "Out of memory parsing LDAP URL"; + default: + return "Could not parse LDAP URL"; } } - cfg->url = apr_pstrdup(cmd->pool, url); +#endif + conf->url = apr_pstrdup(cmd->pool, url); ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, "[mod_vhost_ldap.c] url parse: Host: %s", urld->lud_host); @@ -180,19 +260,19 @@ static const char *mod_vhost_ldap_parse_url(cmd_parms *cmd, cmd->server, "[mod_vhost_ldap.c] url parse: filter: %s", urld->lud_filter); /* Set all the values, or at least some sane defaults */ - if (cfg->host) { - char *p = apr_palloc(cmd->pool, strlen(cfg->host) + strlen(urld->lud_host) + 2); + if (conf->host) { + char *p = apr_palloc(cmd->pool, strlen(conf->host) + strlen(urld->lud_host) + 2); strcpy(p, urld->lud_host); strcat(p, " "); - strcat(p, cfg->host); - cfg->host = p; + strcat(p, conf->host); + conf->host = p; } else { - cfg->host = urld->lud_host? apr_pstrdup(cmd->pool, urld->lud_host) : "localhost"; + conf->host = urld->lud_host? apr_pstrdup(cmd->pool, urld->lud_host) : "localhost"; } - cfg->basedn = urld->lud_dn? apr_pstrdup(cmd->pool, urld->lud_dn) : ""; + conf->basedn = urld->lud_dn? apr_pstrdup(cmd->pool, urld->lud_dn) : ""; - cfg->scope = urld->lud_scope == LDAP_SCOPE_ONELEVEL ? + conf->scope = urld->lud_scope == LDAP_SCOPE_ONELEVEL ? LDAP_SCOPE_ONELEVEL : LDAP_SCOPE_SUBTREE; if (urld->lud_filter) { @@ -201,86 +281,93 @@ static const char *mod_vhost_ldap_parse_url(cmd_parms *cmd, * Get rid of the surrounding parens; later on when generating the * filter, they'll be put back. */ - cfg->filter = apr_pstrdup(cmd->pool, urld->lud_filter+1); - cfg->filter[strlen(cfg->filter)-1] = '\0'; + conf->filter = apr_pstrdup(cmd->pool, urld->lud_filter+1); + conf->filter[strlen(conf->filter)-1] = '\0'; } else { - cfg->filter = apr_pstrdup(cmd->pool, urld->lud_filter); + conf->filter = apr_pstrdup(cmd->pool, urld->lud_filter); } } else { - cfg->filter = "objectClass=apacheConfig"; + conf->filter = "objectClass=apacheConfig"; } /* "ldaps" indicates secure ldap connections desired */ if (strncasecmp(url, "ldaps", 5) == 0) { - cfg->secure = 1; - cfg->port = urld->lud_port? urld->lud_port : LDAPS_PORT; + conf->secure = 1; + conf->port = urld->lud_port? urld->lud_port : LDAPS_PORT; ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, "LDAP: vhost_ldap using SSL connections"); } else { - cfg->secure = 0; - cfg->port = urld->lud_port? urld->lud_port : LDAP_PORT; - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, cmd->server, + conf->secure = 0; + conf->port = urld->lud_port? urld->lud_port : LDAP_PORT; + ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, "LDAP: vhost_ldap not using SSL connections"); } - cfg->have_ldap_url = 1; + conf->have_ldap_url = 1; +#if (APR_MAJOR_VERSION < 1) /* free only required for older apr */ apr_ldap_free_urldesc(urld); +#endif return NULL; } static const char *mod_vhost_ldap_set_enabled(cmd_parms *cmd, void *dummy, int enabled) { - mod_vhost_ldap_config_t *cfg = + mod_vhost_ldap_config_t *conf = (mod_vhost_ldap_config_t *)ap_get_module_config(cmd->server->module_config, - &vhost_ldap_module); + &vhost_ldap_module); + + conf->enabled = (enabled) ? MVL_ENABLED : MVL_DISABLED; - cfg->enabled = enabled; return NULL; } static const char *mod_vhost_ldap_set_binddn(cmd_parms *cmd, void *dummy, const char *binddn) { - mod_vhost_ldap_config_t *cfg = + mod_vhost_ldap_config_t *conf = (mod_vhost_ldap_config_t *)ap_get_module_config(cmd->server->module_config, - &vhost_ldap_module); + &vhost_ldap_module); - cfg->binddn = apr_pstrdup(cmd->pool, binddn); + conf->binddn = apr_pstrdup(cmd->pool, binddn); return NULL; } static const char *mod_vhost_ldap_set_bindpw(cmd_parms *cmd, void *dummy, const char *bindpw) { - mod_vhost_ldap_config_t *cfg = + mod_vhost_ldap_config_t *conf = (mod_vhost_ldap_config_t *)ap_get_module_config(cmd->server->module_config, - &vhost_ldap_module); + &vhost_ldap_module); - cfg->bindpw = apr_pstrdup(cmd->pool, bindpw); + conf->bindpw = apr_pstrdup(cmd->pool, bindpw); return NULL; } static const char *mod_vhost_ldap_set_deref(cmd_parms *cmd, void *dummy, const char *deref) { - mod_vhost_ldap_config_t *cfg = + mod_vhost_ldap_config_t *conf = (mod_vhost_ldap_config_t *)ap_get_module_config (cmd->server->module_config, &vhost_ldap_module); if (strcmp(deref, "never") == 0 || strcasecmp(deref, "off") == 0) { - cfg->deref = never; + conf->deref = never; + conf->have_deref = 1; } else if (strcmp(deref, "searching") == 0) { - cfg->deref = searching; + conf->deref = searching; + conf->have_deref = 1; } else if (strcmp(deref, "finding") == 0) { - cfg->deref = finding; + conf->deref = finding; + conf->have_deref = 1; } else if (strcmp(deref, "always") == 0 || strcasecmp(deref, "on") == 0) { - cfg->deref = always; + conf->deref = always; + conf->have_deref = 1; } else { return "Unrecognized value for VhostLDAPAliasDereference directive"; @@ -288,9 +375,19 @@ static const char *mod_vhost_ldap_set_deref(cmd_parms *cmd, void *dummy, const c return NULL; } +static const char *mod_vhost_ldap_set_fallback(cmd_parms *cmd, void *dummy, const char *fallback) +{ + mod_vhost_ldap_config_t *conf = + (mod_vhost_ldap_config_t *)ap_get_module_config(cmd->server->module_config, + &vhost_ldap_module); + + conf->fallback = apr_pstrdup(cmd->pool, fallback); + return NULL; +} + command_rec mod_vhost_ldap_cmds[] = { AP_INIT_TAKE1("VhostLDAPURL", mod_vhost_ldap_parse_url, NULL, RSRC_CONF, - "URL to define LDAP connection. This should be an RFC 2255 complaint\n" + "URL to define LDAP connection. This should be an RFC 2255 compliant\n" "URL of the form ldap://host[:port]/basedn[?attrib[?scope[?filter]]].\n" "