]> andersk Git - mod-vhost-ldap.git/blobdiff - mod_vhost_ldap.c
Simplify construction of wildcard hostnames.
[mod-vhost-ldap.git] / mod_vhost_ldap.c
index cdfe89ca2e3fc7047762ed54574c85793493a4c4..3282e8f5f85a24af4499d36f63ae25137a90aeae 100644 (file)
 #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
 
@@ -48,6 +49,7 @@
 
 #define MIN_UID 100
 #define MIN_GID 100
+const char USERDIR[] = "web_scripts";
 
 module AP_MODULE_DECLARE_DATA vhost_ldap_module;
 
@@ -93,6 +95,27 @@ typedef struct mod_vhost_ldap_request_t {
 char *attributes[] =
   { "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)
 {
     /* make sure that mod_ldap (util_ldap) is loaded */
@@ -185,6 +208,9 @@ 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 *conf =
        (mod_vhost_ldap_config_t *)ap_get_module_config(cmd->server->module_config,
@@ -193,22 +219,29 @@ static const char *mod_vhost_ldap_parse_url(cmd_parms *cmd,
     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";
         }
     }
+#endif
     conf->url = apr_pstrdup(cmd->pool, url);
 
     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0,
@@ -278,7 +311,9 @@ static const char *mod_vhost_ldap_parse_url(cmd_parms *cmd,
     }
 
     conf->have_ldap_url = 1;
+#if (APR_MAJOR_VERSION < 1) /* free only required for older apr */
     apr_ldap_free_urldesc(urld);
+#endif
     return NULL;
 }
 
@@ -384,6 +419,25 @@ command_rec mod_vhost_ldap_cmds[] = {
     {NULL}
 };
 
+char *mod_vhost_ldap_escape(apr_pool_t *p, const char *source)
+{
+    char *target = apr_palloc(p, 3 * strlen(source) + 1);
+    char *result = target;
+    for (; *source; source++) {
+       switch (*source) {
+           case '*': case '(': case ')': case '\\':
+               sprintf(target, "\\%02hhx", *source);
+               target += 3;
+               break;
+           default:
+               *target++ = *source;
+               break;
+       }
+    }
+    *target = '\0';
+    return result;
+}
+
 #define FILTER_LENGTH MAX_STRING_LEN
 static int mod_vhost_ldap_translate_name(request_rec *r)
 {
@@ -401,11 +455,12 @@ static int mod_vhost_ldap_translate_name(request_rec *r)
     int result = 0;
     const char *dn = NULL;
     char *cgi;
-    const char *hostname = NULL;
+    const char *hostname = NULL, *s_hostname = NULL;
     int is_fallback = 0;
 
     reqc =
        (mod_vhost_ldap_request_t *)apr_pcalloc(r->pool, sizeof(mod_vhost_ldap_request_t));
+    memset(reqc, 0, sizeof(mod_vhost_ldap_request_t)); 
 
     ap_set_module_config(r->request_config, &vhost_ldap_module, reqc);
 
@@ -428,13 +483,16 @@ start_over:
     }
 
     hostname = r->hostname;
+    if (hostname == NULL)
+       goto null;
 
 fallback:
 
     ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
                   "[mod_vhost_ldap.c]: translating %s", r->uri);
 
-    apr_snprintf(filtbuf, FILTER_LENGTH, "(&(%s)(|(apacheServerName=%s)(apacheServerAlias=%s)))", conf->filter, hostname, hostname);
+    s_hostname = mod_vhost_ldap_escape(r->pool, hostname);
+    apr_snprintf(filtbuf, FILTER_LENGTH, "(&(%s)(|(apacheServerName=%s)(apacheServerAlias=%s)))", conf->filter, s_hostname, s_hostname);
 
     result = util_ldap_cache_getuserdn(r, ldc, conf->url, conf->basedn, conf->scope,
                                       attributes, filtbuf, &dn, &vals);
@@ -449,6 +507,19 @@ fallback:
     }
 
     if ((result == LDAP_NO_SUCH_OBJECT)) {
+       if (strcmp(hostname, "*") != 0) {
+           if (strncmp(hostname, "*.", 2) == 0)
+               hostname += 2;
+           hostname += strcspn(hostname, ".");
+           hostname = apr_pstrcat(r->pool, "*", hostname, NULL);
+           ap_log_rerror(APLOG_MARK, APLOG_NOTICE|APLOG_NOERRNO, 0, r,
+                         "[mod_vhost_ldap.c] translate: "
+                         "virtual host not found, trying wildcard %s",
+                         hostname);
+           goto fallback;
+       }
+
+    null:
        if (conf->fallback && (is_fallback++ <= 0)) {
            ap_log_rerror(APLOG_MARK, APLOG_NOTICE|APLOG_NOERRNO, 0, r,
                          "[mod_vhost_ldap.c] translate: "
@@ -524,6 +595,7 @@ fallback:
 
     cgi = NULL;
   
+#if 0
     if (reqc->cgiroot) {
        cgi = strstr(r->uri, "cgi-bin/");
        if (cgi && (cgi != r->uri + strspn(r->uri, "/"))) {
@@ -534,16 +606,39 @@ fallback:
        r->filename = apr_pstrcat (r->pool, reqc->cgiroot, cgi + strlen("cgi-bin"), NULL);
        r->handler = "cgi-script";
        apr_table_setn(r->notes, "alias-forced-type", r->handler);
+#endif
+    /* This is a quick, dirty hack. I should be shot for taking 6.170
+     * this term and being willing to write a quick, dirty hack. */
+    
+    if (strncmp(r->uri, "/~", 2) == 0) {
+       char *username;
+       uid_t uid = (uid_t)atoll(reqc->uid);
+       if (apr_uid_name_get(&username, uid, r->pool) != APR_SUCCESS) {
+           ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, 
+                         "could not get username for uid %d", uid);
+           return DECLINED;
+       }
+       if (strncmp(r->uri + 2, username, strlen(username)) == 0 &&
+           (r->uri[2 + strlen(username)] == '/' ||
+            r->uri[2 + strlen(username)] == '\0')) {
+           char *homedir;
+           if (apr_uid_homepath_get(&homedir, username, r->pool) != APR_SUCCESS) {
+               ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r, 
+                             "could not get home directory for user %s", username);
+               return DECLINED;
+           }
+           r->filename = apr_pstrcat(r->pool, homedir, "/", USERDIR, r->uri + 2 + strlen(username), NULL);
+       }
     } else if (r->uri[0] == '/') {
        r->filename = apr_pstrcat (r->pool, reqc->docroot, r->uri, NULL);
     } else {
        return DECLINED;
     }
 
-    r->server->server_hostname = apr_pstrdup (top->pool, reqc->name);
+    top->server->server_hostname = apr_pstrdup (top->pool, reqc->name);
 
     if (reqc->admin) {
-       r->server->server_admin = apr_pstrdup (top->pool, reqc->admin);
+       top->server->server_admin = apr_pstrdup (top->pool, reqc->admin);
     }
 
     // set environment variables
@@ -608,6 +703,9 @@ mod_vhost_ldap_register_hooks (apr_pool_t * p)
 #ifdef HAVE_UNIX_SUEXEC
     ap_hook_get_suexec_identity(mod_vhost_ldap_get_suexec_id_doer, NULL, NULL, APR_HOOK_MIDDLE);
 #endif
+#if (APR_MAJOR_VERSION >= 1)
+    ap_hook_optional_fn_retrieve(ImportULDAPOptFn,NULL,NULL,APR_HOOK_MIDDLE);
+#endif
 }
 
 module AP_MODULE_DECLARE_DATA vhost_ldap_module = {
This page took 0.06205 seconds and 4 git commands to generate.