Blame modules/aaa/mod_allowmethods.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
#include "httpd.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_protocol.h"
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
Packit 90a5c9
/**
Packit 90a5c9
 * This module makes it easy to restrict what HTTP methods can be ran against
Packit 90a5c9
 * a server.
Packit 90a5c9
 *
Packit 90a5c9
 * It provides one command:
Packit 90a5c9
 *    AllowMethods
Packit 90a5c9
 * This command takes a list of HTTP methods to allow.
Packit 90a5c9
 *
Packit 90a5c9
 *  The most common configuration should be like this:
Packit 90a5c9
 *   <Directory />
Packit 90a5c9
 *    AllowMethods GET HEAD OPTIONS
Packit 90a5c9
 *   </Directory>
Packit 90a5c9
 *   <Directory /special/cgi-bin>
Packit 90a5c9
 *      AllowMethods GET HEAD OPTIONS POST
Packit 90a5c9
 *   </Directory>
Packit 90a5c9
 *  Non-matching methods will be returned a status 405 (method not allowed)
Packit 90a5c9
 *
Packit 90a5c9
 *  To allow all methods, and effectively turn off mod_allowmethods, use:
Packit 90a5c9
 *    AllowMethods reset
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
typedef struct am_conf_t {
Packit 90a5c9
    int allowed_set;
Packit 90a5c9
    apr_int64_t allowed;
Packit 90a5c9
} am_conf_t;
Packit 90a5c9
Packit 90a5c9
module AP_MODULE_DECLARE_DATA allowmethods_module;
Packit 90a5c9
Packit 90a5c9
static int am_check_access(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    int method = r->method_number;
Packit 90a5c9
    am_conf_t *conf;
Packit 90a5c9
Packit 90a5c9
    conf = (am_conf_t *) ap_get_module_config(r->per_dir_config,
Packit 90a5c9
                                              &allowmethods_module);
Packit 90a5c9
    if (!conf || conf->allowed == 0) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    r->allowed = conf->allowed;
Packit 90a5c9
Packit 90a5c9
    if (conf->allowed & (AP_METHOD_BIT << method)) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01623)
Packit 90a5c9
                  "client method denied by server configuration: '%s' to %s%s",
Packit 90a5c9
                  r->method,
Packit 90a5c9
                  r->filename ? "" : "uri ",
Packit 90a5c9
                  r->filename ? r->filename : r->uri);
Packit 90a5c9
Packit 90a5c9
    return HTTP_METHOD_NOT_ALLOWED;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *am_create_conf(apr_pool_t *p, char *dummy)
Packit 90a5c9
{
Packit 90a5c9
    am_conf_t *conf = apr_pcalloc(p, sizeof(am_conf_t));
Packit 90a5c9
Packit 90a5c9
    conf->allowed = 0;
Packit 90a5c9
    conf->allowed_set = 0;
Packit 90a5c9
    return conf;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *am_merge_conf(apr_pool_t *pool, void *a, void *b)
Packit 90a5c9
{
Packit 90a5c9
    am_conf_t *base = (am_conf_t *)a;
Packit 90a5c9
    am_conf_t *add = (am_conf_t *)b;
Packit 90a5c9
    am_conf_t *conf = apr_palloc(pool, sizeof(am_conf_t));
Packit 90a5c9
Packit 90a5c9
    if (add->allowed_set) {
Packit 90a5c9
        conf->allowed = add->allowed;
Packit 90a5c9
        conf->allowed_set = add->allowed_set;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        conf->allowed = base->allowed;
Packit 90a5c9
        conf->allowed_set = base->allowed_set;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return conf;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *am_allowmethods(cmd_parms *cmd, void *d, int argc,
Packit 90a5c9
                                   char *const argv[])
Packit 90a5c9
{
Packit 90a5c9
    int i;
Packit 90a5c9
    am_conf_t *conf = (am_conf_t *)d;
Packit 90a5c9
Packit 90a5c9
    if (argc == 0) {
Packit 90a5c9
        return "AllowMethods: No method or 'reset' keyword given";
Packit 90a5c9
    }
Packit 90a5c9
    if (argc == 1) {
Packit 90a5c9
        if (strcasecmp("reset", argv[0]) == 0) {
Packit 90a5c9
            conf->allowed = 0;
Packit 90a5c9
            conf->allowed_set = 1;
Packit 90a5c9
            return NULL;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    for (i = 0; i < argc; i++) {
Packit 90a5c9
        int m;
Packit 90a5c9
Packit 90a5c9
        m = ap_method_number_of(argv[i]);
Packit 90a5c9
        if (m == M_INVALID) {
Packit 90a5c9
            return apr_pstrcat(cmd->pool, "AllowMethods: Invalid Method '",
Packit 90a5c9
                               argv[i], "'", NULL);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        conf->allowed |= (AP_METHOD_BIT << m);
Packit 90a5c9
    }
Packit 90a5c9
    conf->allowed_set = 1;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void am_register_hooks(apr_pool_t * p)
Packit 90a5c9
{
Packit 90a5c9
    ap_hook_access_checker(am_check_access, NULL, NULL, APR_HOOK_REALLY_FIRST);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const command_rec am_cmds[] = {
Packit 90a5c9
    AP_INIT_TAKE_ARGV("AllowMethods", am_allowmethods, NULL,
Packit 90a5c9
                      ACCESS_CONF,
Packit 90a5c9
                      "only allow specific methods"),
Packit 90a5c9
    {NULL}
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(allowmethods) = {
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    am_create_conf,
Packit 90a5c9
    am_merge_conf,
Packit 90a5c9
    NULL,
Packit 90a5c9
    NULL,
Packit 90a5c9
    am_cmds,
Packit 90a5c9
    am_register_hooks,
Packit 90a5c9
};