Blame modules/aaa/mod_access_compat.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Security options etc.
Packit 90a5c9
 *
Packit 90a5c9
 * Module derived from code originally written by Rob McCool
Packit 90a5c9
 *
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "apr_network_io.h"
Packit 90a5c9
#include "apr_md5.h"
Packit 90a5c9
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#define APR_WANT_BYTEFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "http_protocol.h"
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
Packit 90a5c9
#include "mod_auth.h"
Packit 90a5c9
Packit 90a5c9
#if APR_HAVE_NETINET_IN_H
Packit 90a5c9
#include <netinet/in.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
enum allowdeny_type {
Packit 90a5c9
    T_ENV,
Packit 90a5c9
    T_NENV,
Packit 90a5c9
    T_ALL,
Packit 90a5c9
    T_IP,
Packit 90a5c9
    T_HOST,
Packit 90a5c9
    T_FAIL
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
typedef struct {
Packit 90a5c9
    apr_int64_t limited;
Packit 90a5c9
    union {
Packit 90a5c9
        char *from;
Packit 90a5c9
        apr_ipsubnet_t *ip;
Packit 90a5c9
    } x;
Packit 90a5c9
    enum allowdeny_type type;
Packit 90a5c9
} allowdeny;
Packit 90a5c9
Packit 90a5c9
/* things in the 'order' array */
Packit 90a5c9
#define DENY_THEN_ALLOW 0
Packit 90a5c9
#define ALLOW_THEN_DENY 1
Packit 90a5c9
#define MUTUAL_FAILURE 2
Packit 90a5c9
Packit 90a5c9
typedef struct {
Packit 90a5c9
    int order[METHODS];
Packit 90a5c9
    apr_array_header_t *allows;
Packit 90a5c9
    apr_array_header_t *denys;
Packit 90a5c9
    int *satisfy; /* for every method one */
Packit 90a5c9
} access_compat_dir_conf;
Packit 90a5c9
Packit 90a5c9
module AP_MODULE_DECLARE_DATA access_compat_module;
Packit 90a5c9
Packit 90a5c9
static void *create_access_compat_dir_config(apr_pool_t *p, char *dummy)
Packit 90a5c9
{
Packit 90a5c9
    int i;
Packit 90a5c9
    access_compat_dir_conf *conf =
Packit 90a5c9
        (access_compat_dir_conf *)apr_pcalloc(p, sizeof(access_compat_dir_conf));
Packit 90a5c9
Packit 90a5c9
    for (i = 0; i < METHODS; ++i) {
Packit 90a5c9
        conf->order[i] = DENY_THEN_ALLOW;
Packit 90a5c9
    }
Packit 90a5c9
    conf->allows = apr_array_make(p, 1, sizeof(allowdeny));
Packit 90a5c9
    conf->denys = apr_array_make(p, 1, sizeof(allowdeny));
Packit 90a5c9
    conf->satisfy = apr_palloc(p, sizeof(*conf->satisfy) * METHODS);
Packit 90a5c9
    for (i = 0; i < METHODS; ++i) {
Packit 90a5c9
        conf->satisfy[i] = SATISFY_NOSPEC;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return (void *)conf;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *order(cmd_parms *cmd, void *dv, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    access_compat_dir_conf *d = (access_compat_dir_conf *) dv;
Packit 90a5c9
    int i, o;
Packit 90a5c9
Packit 90a5c9
    if (!strcasecmp(arg, "allow,deny"))
Packit 90a5c9
        o = ALLOW_THEN_DENY;
Packit 90a5c9
    else if (!strcasecmp(arg, "deny,allow"))
Packit 90a5c9
        o = DENY_THEN_ALLOW;
Packit 90a5c9
    else if (!strcasecmp(arg, "mutual-failure"))
Packit 90a5c9
        o = MUTUAL_FAILURE;
Packit 90a5c9
    else
Packit 90a5c9
        return "unknown order";
Packit 90a5c9
Packit 90a5c9
    for (i = 0; i < METHODS; ++i)
Packit 90a5c9
        if (cmd->limited & (AP_METHOD_BIT << i))
Packit 90a5c9
            d->order[i] = o;
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *satisfy(cmd_parms *cmd, void *dv, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    access_compat_dir_conf *d = (access_compat_dir_conf *) dv;
Packit 90a5c9
    int satisfy = SATISFY_NOSPEC;
Packit 90a5c9
    int i;
Packit 90a5c9
Packit 90a5c9
    if (!strcasecmp(arg, "all")) {
Packit 90a5c9
        satisfy = SATISFY_ALL;
Packit 90a5c9
    }
Packit 90a5c9
    else if (!strcasecmp(arg, "any")) {
Packit 90a5c9
        satisfy = SATISFY_ANY;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        return "Satisfy either 'any' or 'all'.";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    for (i = 0; i < METHODS; ++i) {
Packit 90a5c9
        if (cmd->limited & (AP_METHOD_BIT << i)) {
Packit 90a5c9
            d->satisfy[i] = satisfy;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *allow_cmd(cmd_parms *cmd, void *dv, const char *from,
Packit 90a5c9
                             const char *where_c)
Packit 90a5c9
{
Packit 90a5c9
    access_compat_dir_conf *d = (access_compat_dir_conf *) dv;
Packit 90a5c9
    allowdeny *a;
Packit 90a5c9
    char *where = apr_pstrdup(cmd->pool, where_c);
Packit 90a5c9
    char *s;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
Packit 90a5c9
    if (strcasecmp(from, "from"))
Packit 90a5c9
        return "allow and deny must be followed by 'from'";
Packit 90a5c9
Packit cc64ed
    s = ap_strchr(where, '#');
Packit cc64ed
    if (s) {
Packit cc64ed
        *s = '\0';
Packit cc64ed
    }
Packit cc64ed
Packit 90a5c9
    a = (allowdeny *) apr_array_push(cmd->info ? d->allows : d->denys);
Packit 90a5c9
    a->x.from = where;
Packit 90a5c9
    a->limited = cmd->limited;
Packit 90a5c9
Packit 90a5c9
    if (!strncasecmp(where, "env=!", 5)) {
Packit 90a5c9
        a->type = T_NENV;
Packit 90a5c9
        a->x.from += 5;
Packit 90a5c9
Packit 90a5c9
    }
Packit 90a5c9
    else if (!strncasecmp(where, "env=", 4)) {
Packit 90a5c9
        a->type = T_ENV;
Packit 90a5c9
        a->x.from += 4;
Packit 90a5c9
Packit 90a5c9
    }
Packit 90a5c9
    else if (!strcasecmp(where, "all")) {
Packit 90a5c9
        a->type = T_ALL;
Packit 90a5c9
    }
Packit 90a5c9
    else if ((s = ap_strchr(where, '/'))) {
Packit 90a5c9
        *s++ = '\0';
Packit 90a5c9
        rv = apr_ipsubnet_create(&a->x.ip, where, s, cmd->pool);
Packit 90a5c9
        if(APR_STATUS_IS_EINVAL(rv)) {
Packit 90a5c9
            /* looked nothing like an IP address */
Packit 90a5c9
            return "An IP address was expected";
Packit 90a5c9
        }
Packit 90a5c9
        else if (rv != APR_SUCCESS) {
Packit 90a5c9
            return apr_psprintf(cmd->pool, "%pm", &rv;;
Packit 90a5c9
        }
Packit 90a5c9
        a->type = T_IP;
Packit 90a5c9
    }
Packit 90a5c9
    else if (!APR_STATUS_IS_EINVAL(rv = apr_ipsubnet_create(&a->x.ip, where,
Packit 90a5c9
                                                            NULL, cmd->pool))) {
Packit 90a5c9
        if (rv != APR_SUCCESS)
Packit 90a5c9
            return apr_psprintf(cmd->pool, "%pm", &rv;;
Packit 90a5c9
        a->type = T_IP;
Packit 90a5c9
    }
Packit 90a5c9
    else if (ap_strchr(where, '#')) {
Packit 90a5c9
        return "No comments are allowed here";
Packit 90a5c9
    }
Packit 90a5c9
    else { /* no slash, didn't look like an IP address => must be a host */
Packit 90a5c9
        a->type = T_HOST;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static char its_an_allow;
Packit 90a5c9
Packit 90a5c9
static const command_rec access_compat_cmds[] =
Packit 90a5c9
{
Packit 90a5c9
    AP_INIT_TAKE1("order", order, NULL, OR_LIMIT,
Packit 90a5c9
                  "'allow,deny', 'deny,allow', or 'mutual-failure'"),
Packit 90a5c9
    AP_INIT_ITERATE2("allow", allow_cmd, &its_an_allow, OR_LIMIT,
Packit 90a5c9
                     "'from' followed by hostnames or IP-address wildcards"),
Packit 90a5c9
    AP_INIT_ITERATE2("deny", allow_cmd, NULL, OR_LIMIT,
Packit 90a5c9
                     "'from' followed by hostnames or IP-address wildcards"),
Packit 90a5c9
    AP_INIT_TAKE1("Satisfy", satisfy, NULL, OR_AUTHCFG,
Packit 90a5c9
                  "access policy if both allow and require used ('all' or 'any')"),
Packit 90a5c9
    {NULL}
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
static int in_domain(const char *domain, const char *what)
Packit 90a5c9
{
Packit 90a5c9
    int dl = strlen(domain);
Packit 90a5c9
    int wl = strlen(what);
Packit 90a5c9
Packit 90a5c9
    if ((wl - dl) >= 0) {
Packit 90a5c9
        if (strcasecmp(domain, &what[wl - dl]) != 0) {
Packit 90a5c9
            return 0;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Make sure we matched an *entire* subdomain --- if the user
Packit 90a5c9
         * said 'allow from good.com', we don't want people from nogood.com
Packit 90a5c9
         * to be able to get in.
Packit 90a5c9
         */
Packit 90a5c9
Packit 90a5c9
        if (wl == dl) {
Packit 90a5c9
            return 1;                /* matched whole thing */
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            return (domain[0] == '.' || what[wl - dl - 1] == '.');
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        return 0;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int find_allowdeny(request_rec *r, apr_array_header_t *a, int method)
Packit 90a5c9
{
Packit 90a5c9
Packit 90a5c9
    allowdeny *ap = (allowdeny *) a->elts;
Packit 90a5c9
    apr_int64_t mmask = (AP_METHOD_BIT << method);
Packit 90a5c9
    int i;
Packit 90a5c9
    int gothost = 0;
Packit 90a5c9
    const char *remotehost = NULL;
Packit 90a5c9
Packit 90a5c9
    for (i = 0; i < a->nelts; ++i) {
Packit 90a5c9
        if (!(mmask & ap[i].limited)) {
Packit 90a5c9
            continue;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        switch (ap[i].type) {
Packit 90a5c9
        case T_ENV:
Packit 90a5c9
            if (apr_table_get(r->subprocess_env, ap[i].x.from)) {
Packit 90a5c9
                return 1;
Packit 90a5c9
            }
Packit 90a5c9
            break;
Packit 90a5c9
Packit 90a5c9
        case T_NENV:
Packit 90a5c9
            if (!apr_table_get(r->subprocess_env, ap[i].x.from)) {
Packit 90a5c9
                return 1;
Packit 90a5c9
            }
Packit 90a5c9
            break;
Packit 90a5c9
Packit 90a5c9
        case T_ALL:
Packit 90a5c9
            return 1;
Packit 90a5c9
Packit 90a5c9
        case T_IP:
Packit 90a5c9
            if (apr_ipsubnet_test(ap[i].x.ip, r->useragent_addr)) {
Packit 90a5c9
                return 1;
Packit 90a5c9
            }
Packit 90a5c9
            break;
Packit 90a5c9
Packit 90a5c9
        case T_HOST:
Packit 90a5c9
            if (!gothost) {
Packit 90a5c9
                int remotehost_is_ip;
Packit 90a5c9
Packit 90a5c9
                remotehost = ap_get_useragent_host(r, REMOTE_DOUBLE_REV,
Packit 90a5c9
                                                   &remotehost_is_ip);
Packit 90a5c9
Packit 90a5c9
                if ((remotehost == NULL) || remotehost_is_ip) {
Packit 90a5c9
                    gothost = 1;
Packit 90a5c9
                }
Packit 90a5c9
                else {
Packit 90a5c9
                    gothost = 2;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if ((gothost == 2) && in_domain(ap[i].x.from, remotehost)) {
Packit 90a5c9
                return 1;
Packit 90a5c9
            }
Packit 90a5c9
            break;
Packit 90a5c9
Packit 90a5c9
        case T_FAIL:
Packit 90a5c9
            /* do nothing? */
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int access_compat_ap_satisfies(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    access_compat_dir_conf *conf = (access_compat_dir_conf *)
Packit 90a5c9
        ap_get_module_config(r->per_dir_config, &access_compat_module);
Packit 90a5c9
Packit 90a5c9
    return conf->satisfy[r->method_number];
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int check_dir_access(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    int method = r->method_number;
Packit 90a5c9
    int ret = OK;
Packit 90a5c9
    access_compat_dir_conf *a = (access_compat_dir_conf *)
Packit 90a5c9
        ap_get_module_config(r->per_dir_config, &access_compat_module);
Packit 90a5c9
Packit 90a5c9
    if (a->order[method] == ALLOW_THEN_DENY) {
Packit 90a5c9
        ret = HTTP_FORBIDDEN;
Packit 90a5c9
        if (find_allowdeny(r, a->allows, method)) {
Packit 90a5c9
            ret = OK;
Packit 90a5c9
        }
Packit 90a5c9
        if (find_allowdeny(r, a->denys, method)) {
Packit 90a5c9
            ret = HTTP_FORBIDDEN;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else if (a->order[method] == DENY_THEN_ALLOW) {
Packit 90a5c9
        if (find_allowdeny(r, a->denys, method)) {
Packit 90a5c9
            ret = HTTP_FORBIDDEN;
Packit 90a5c9
        }
Packit 90a5c9
        if (find_allowdeny(r, a->allows, method)) {
Packit 90a5c9
            ret = OK;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        if (find_allowdeny(r, a->allows, method)
Packit 90a5c9
            && !find_allowdeny(r, a->denys, method)) {
Packit 90a5c9
            ret = OK;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            ret = HTTP_FORBIDDEN;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (ret == HTTP_FORBIDDEN) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01797)
Packit 90a5c9
                      "client denied by server configuration: %s%s",
Packit 90a5c9
                      r->filename ? "" : "uri ",
Packit 90a5c9
                      r->filename ? r->filename : r->uri);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return ret;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void register_hooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    APR_REGISTER_OPTIONAL_FN(access_compat_ap_satisfies);
Packit 90a5c9
Packit 90a5c9
    /* This can be access checker since we don't require r->user to be set. */
Packit 90a5c9
    ap_hook_check_access(check_dir_access, NULL, NULL, APR_HOOK_MIDDLE,
Packit 90a5c9
                         AP_AUTH_INTERNAL_PER_CONF);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(access_compat) =
Packit 90a5c9
{
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    create_access_compat_dir_config,   /* dir config creater */
Packit 90a5c9
    NULL,                           /* dir merger --- default is to override */
Packit 90a5c9
    NULL,                           /* server config */
Packit 90a5c9
    NULL,                           /* merge server config */
Packit 90a5c9
    access_compat_cmds,
Packit 90a5c9
    register_hooks                  /* register hooks */
Packit 90a5c9
};