]> granicus.if.org Git - apache/blobdiff - modules/aaa/mod_authz_host.c
hostname: Test and log useragent_host per-request across various modules,
[apache] / modules / aaa / mod_authz_host.c
index 79b36760e700d6138c32cb30a3e3d163013610e2..15f453996e02c24af53920ff4706ce573b91e106 100644 (file)
@@ -1,9 +1,9 @@
-/* Copyright 2002-2005 The Apache Software Foundation or its licensors, as
- * applicable.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
@@ -24,6 +24,7 @@
 #include "apr_strings.h"
 #include "apr_network_io.h"
 #include "apr_md5.h"
+#include "apr_hash.h"
 
 #define APR_WANT_STRFUNC
 #define APR_WANT_BYTEFUNC
 #include <netinet/in.h>
 #endif
 
-typedef struct {
-       int dummy;  /* just here to stop compiler warnings for now. */
-} authz_host_dir_conf;
-
-module AP_MODULE_DECLARE_DATA authz_host_module;
-
-static void *create_authz_host_dir_config(apr_pool_t *p, char *dummy)
-{
-    authz_host_dir_conf *conf =
-        (authz_host_dir_conf *)apr_pcalloc(p, sizeof(authz_host_dir_conf));
-
-    return (void *)conf;
-}
+/*
+ * To save memory if the same subnets are used in hundres of vhosts, we store
+ * each subnet only once and use this temporary hash to find it again.
+ */
+static apr_hash_t *parsed_subnets;
 
-static const command_rec authz_host_cmds[] =
-{
-    {NULL}
-};
+static apr_ipsubnet_t *localhost_v4;
+#if APR_HAVE_IPV6
+static apr_ipsubnet_t *localhost_v6;
+#endif
 
 static int in_domain(const char *domain, const char *what)
 {
@@ -90,174 +83,244 @@ static int in_domain(const char *domain, const char *what)
     }
 }
 
-static authz_status env_check_authorization(request_rec *r, const char *require_line)
+static const char *ip_parse_config(cmd_parms *cmd,
+                                   const char *require_line,
+                                   const void **parsed_require_line)
 {
     const char *t, *w;
+    int count = 0;
+    apr_ipsubnet_t **ip;
+    apr_pool_t *ptemp = cmd->temp_pool;
+    apr_pool_t *p = cmd->pool;
 
-    /* The 'env' provider will allow the configuration to specify a list of
-        env variables to check rather than a single variable.  This is different
+    /* The 'ip' provider will allow the configuration to specify a list of
+        ip addresses to check rather than a single address.  This is different
         from the previous host based syntax. */
+
     t = require_line;
-    while ((w = ap_getword_conf(r->pool, &t)) && w[0]) {
-        if (apr_table_get(r->subprocess_env, w)) {
-            return AUTHZ_GRANTED;
-        }
-    }
+    while ((w = ap_getword_conf(ptemp, &t)) && w[0])
+        count++;
 
-    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                  "access to %s failed, reason: env variable list does not meet "
-                  "'require'ments for user '%s' to be allowed access",
-                  r->uri, r->user);
+    if (count == 0)
+        return "'require ip' requires an argument";
 
-    return AUTHZ_DENIED;
-}
+    ip = apr_pcalloc(p, sizeof(apr_ipsubnet_t *) * (count + 1));
+    *parsed_require_line = ip;
 
-static authz_status ip_check_authorization(request_rec *r, const char *require_line)
-{
-    const char *t, *w;
-
-    /* The 'ip' provider will allow the configuration to specify a list of
-        ip addresses to check rather than a single address.  This is different
-        from the previous host based syntax. */
     t = require_line;
-    while ((w = ap_getword_conf(r->pool, &t)) && w[0]) {
-        char *where = apr_pstrdup(r->pool, w);
-        char *s;
-        char msgbuf[120];
-        apr_ipsubnet_t *ip;
+    while ((w = ap_getword_conf(ptemp, &t)) && w[0]) {
+        char *addr = apr_pstrdup(ptemp, w);
+        char *mask;
         apr_status_t rv;
-        int got_ip = 0;
-
-        if ((s = ap_strchr(where, '/'))) {
-            *s++ = '\0';
-            rv = apr_ipsubnet_create(&ip, where, s, r->pool);
-            if(APR_STATUS_IS_EINVAL(rv)) {
-                /* looked nothing like an IP address */
-                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                              "an ip address 'require' list appears to be invalid ");
-            }
-            else if (rv != APR_SUCCESS) {
-                apr_strerror(rv, msgbuf, sizeof msgbuf);
-                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                              "an ip address 'require' list appears to be invalid; %s ",
-                              msgbuf);
-            }
-            else
-                got_ip = 1;
-        }
-        else if (!APR_STATUS_IS_EINVAL(rv = apr_ipsubnet_create(&ip, where,
-                                                                NULL, r->pool))) {
-            if (rv != APR_SUCCESS) {
-                apr_strerror(rv, msgbuf, sizeof msgbuf);
-                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                              "an ip address 'require' list appears to be invalid; %s ",
-                              msgbuf);
-            }
-            else 
-                got_ip = 1;
+
+        if (parsed_subnets &&
+            (*ip = apr_hash_get(parsed_subnets, w, APR_HASH_KEY_STRING)) != NULL)
+        {
+            /* we already have parsed this subnet */
+            ip++;
+            continue;
         }
 
-        if (got_ip && apr_ipsubnet_test(ip, r->connection->remote_addr)) {
-            return AUTHZ_GRANTED;
+        if ((mask = ap_strchr(addr, '/')))
+            *mask++ = '\0';
+
+        rv = apr_ipsubnet_create(ip, addr, mask, p);
+
+        if(APR_STATUS_IS_EINVAL(rv)) {
+            /* looked nothing like an IP address */
+            return apr_psprintf(p, "ip address '%s' appears to be invalid", w);
         }
+        else if (rv != APR_SUCCESS) {
+            return apr_psprintf(p, "ip address '%s' appears to be invalid: %pm",
+                                w, &rv);
+        }
+
+        if (parsed_subnets)
+            apr_hash_set(parsed_subnets, w, APR_HASH_KEY_STRING, *ip);
+        ip++;
     }
 
-    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                  "access to %s failed, reason: ip address list does not meet "
-                  "'require'ments for user '%s' to be allowed access",
-                  r->uri, r->user);
+    return NULL;
+}
+
+static authz_status ip_check_authorization(request_rec *r,
+                                           const char *require_line,
+                                           const void *parsed_require_line)
+{
+    /* apr_ipsubnet_test should accept const but doesn't */
+    apr_ipsubnet_t **ip = (apr_ipsubnet_t **)parsed_require_line;
 
+    while (*ip) {
+        if (apr_ipsubnet_test(*ip, r->useragent_addr))
+            return AUTHZ_GRANTED;
+        ip++;
+    }
+
+    /* authz_core will log the require line and the result at DEBUG */
     return AUTHZ_DENIED;
 }
 
-static authz_status host_check_authorization(request_rec *r, const char *require_line)
+static authz_status host_check_authorization(request_rec *r,
+                                             const char *require_line,
+                                             const void *parsed_require_line)
 {
-    const char *t, *w;
+    const char *t;
+    char *w, *hash_ptr;
     const char *remotehost = NULL;
     int remotehost_is_ip;
 
-    remotehost = ap_get_remote_host(r->connection,
-                                    r->per_dir_config,
-                                    REMOTE_DOUBLE_REV,
-                                    &remotehost_is_ip);
+    remotehost = ap_get_useragent_host(r, REMOTE_DOUBLE_REV, &remotehost_is_ip);
 
     if ((remotehost == NULL) || remotehost_is_ip) {
-        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                      "access to %s failed, reason: unable to get the "
-                      "remote host name", r->uri);
+        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01753)
+                      "access check of '%s' to %s failed, reason: unable to get the "
+                      "remote host name", require_line, r->uri);
     }
     else {
+        const char *err = NULL;
+        const ap_expr_info_t *expr = parsed_require_line;
+        const char *require;
+
+        require = ap_expr_str_exec(r, expr, &err);
+        if (err) {
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02593)
+                          "authz_host authorize: require host: Can't "
+                          "evaluate require expression: %s", err);
+            return AUTHZ_DENIED;
+        }
+
         /* The 'host' provider will allow the configuration to specify a list of
             host names to check rather than a single name.  This is different
             from the previous host based syntax. */
-        t = require_line;
+        t = require;
         while ((w = ap_getword_conf(r->pool, &t)) && w[0]) {
+            /* '#' is not valid hostname character and admin could specify
+             * 'Require host localhost# Add example.com later'. We should not
+             * grant access to 'example.com' in that case. */
+            if ((hash_ptr = ap_strchr(w, '#'))) {
+                if (hash_ptr == w) {
+                    break;
+                }
+                *hash_ptr = '\0';
+            }
             if (in_domain(w, remotehost)) {
                 return AUTHZ_GRANTED;
             }
+            if (hash_ptr) {
+                break;
+            }
         }
-
-        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                      "access to %s failed, reason: host name list does not meet "
-                      "'require'ments for user '%s' to be allowed access",
-                      r->uri, r->user);
     }
 
+    /* authz_core will log the require line and the result at DEBUG */
     return AUTHZ_DENIED;
 }
 
-static authz_status all_check_authorization(request_rec *r, const char *require_line)
+static authz_status local_check_authorization(request_rec *r,
+                                              const char *require_line,
+                                              const void *parsed_require_line)
 {
-    /* If the argument to the 'all' provider is 'granted' then just let 
-        everybody in. This would be equivalent to the previous syntax of
-        'allow from all'. If the argument is anything else, this would
-        be equivalent to 'deny from all' Of course the opposite would be 
-        true if the 'all' provider is invoked by the 'reject' directive */
-    if (strcasecmp(require_line, "granted") == 0) {
+     if (   apr_sockaddr_equal(r->connection->local_addr,
+                               r->useragent_addr)
+         || apr_ipsubnet_test(localhost_v4, r->useragent_addr)
+#if APR_HAVE_IPV6
+         || apr_ipsubnet_test(localhost_v6, r->useragent_addr)
+#endif
+        )
+     {
         return AUTHZ_GRANTED;
-    }
-    return AUTHZ_DENIED;
+     }
+
+     return AUTHZ_DENIED;
 }
 
-static const authz_provider authz_env_provider =
+static const char *host_parse_config(cmd_parms *cmd, const char *require_line,
+                                     const void **parsed_require_line)
 {
-    &env_check_authorization,
-};
+    const char *expr_err = NULL;
+    ap_expr_info_t *expr;
+
+    expr = ap_expr_parse_cmd(cmd, require_line, AP_EXPR_FLAG_STRING_RESULT,
+            &expr_err, NULL);
+
+    if (expr_err)
+        return apr_pstrcat(cmd->temp_pool,
+                           "Cannot parse expression in require line: ",
+                           expr_err, NULL);
+
+    *parsed_require_line = expr;
+
+    return NULL;
+}
 
 static const authz_provider authz_ip_provider =
 {
     &ip_check_authorization,
+    &ip_parse_config,
 };
 
 static const authz_provider authz_host_provider =
 {
     &host_check_authorization,
+    &host_parse_config,
 };
 
-static const authz_provider authz_all_provider =
+static const authz_provider authz_local_provider =
 {
-    &all_check_authorization,
+    &local_check_authorization,
+    NULL,
 };
 
+
+static int authz_host_pre_config(apr_pool_t *p, apr_pool_t *plog,
+                                 apr_pool_t *ptemp)
+{
+    /* we only use this hash in the parse config phase, ptemp is enough */
+    parsed_subnets = apr_hash_make(ptemp);
+
+    apr_ipsubnet_create(&localhost_v4, "127.0.0.0", "8", p);
+    apr_hash_set(parsed_subnets, "127.0.0.0/8", APR_HASH_KEY_STRING, localhost_v4);
+
+#if APR_HAVE_IPV6
+    apr_ipsubnet_create(&localhost_v6, "::1", NULL, p);
+    apr_hash_set(parsed_subnets, "::1", APR_HASH_KEY_STRING, localhost_v6);
+#endif
+
+    return OK;
+}
+
+static int authz_host_post_config(apr_pool_t *p, apr_pool_t *plog,
+                                  apr_pool_t *ptemp, server_rec *s)
+{
+    /* make sure we don't use this during .htaccess parsing */
+    parsed_subnets = NULL;
+
+    return OK;
+}
+
 static void register_hooks(apr_pool_t *p)
 {
-    ap_register_provider(p, AUTHZ_PROVIDER_GROUP, "env", "0",
-                         &authz_env_provider);
-    ap_register_provider(p, AUTHZ_PROVIDER_GROUP, "ip", "0",
-                         &authz_ip_provider);
-    ap_register_provider(p, AUTHZ_PROVIDER_GROUP, "host", "0",
-                         &authz_host_provider);
-    ap_register_provider(p, AUTHZ_PROVIDER_GROUP, "all", "0",
-                         &authz_all_provider);
+    ap_hook_pre_config(authz_host_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
+    ap_hook_post_config(authz_host_post_config, NULL, NULL, APR_HOOK_MIDDLE);
+
+    ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "ip",
+                              AUTHZ_PROVIDER_VERSION,
+                              &authz_ip_provider, AP_AUTH_INTERNAL_PER_CONF);
+    ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "host",
+                              AUTHZ_PROVIDER_VERSION,
+                              &authz_host_provider, AP_AUTH_INTERNAL_PER_CONF);
+    ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "local",
+                              AUTHZ_PROVIDER_VERSION,
+                              &authz_local_provider, AP_AUTH_INTERNAL_PER_CONF);
 }
 
-module AP_MODULE_DECLARE_DATA authz_host_module =
+AP_DECLARE_MODULE(authz_host) =
 {
     STANDARD20_MODULE_STUFF,
-    create_authz_host_dir_config,   /* dir config creater */
+    NULL,                           /* dir config creater */
     NULL,                           /* dir merger --- default is to override */
     NULL,                           /* server config */
     NULL,                           /* merge server config */
-    authz_host_cmds,
+    NULL,
     register_hooks                  /* register hooks */
 };