1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "http_core.h"
19 #include "http_config.h"
20 #include "http_protocol.h"
21 #include "http_request.h"
23 #include "apr_strings.h"
26 * This module makes it easy to restrict what HTTP methods can be ran against
29 * It provides one comand:
31 * This command takes a list of HTTP methods to allow.
33 * The most common configuration should be like this:
35 * AllowMethods GET HEAD OPTIONS
37 * <Directory /special/cgi-bin>
38 * AllowMethods GET HEAD OPTIONS POST
40 * Non-matching methods will be returned a status 405 (method not allowed)
42 * To allow all methods, and effectively turn off mod_allowmethods, use:
46 typedef struct am_conf_t {
51 module AP_MODULE_DECLARE_DATA allowmethods_module;
53 static int am_check_access(request_rec *r)
55 int method = r->method_number;
58 conf = (am_conf_t *) ap_get_module_config(r->per_dir_config,
59 &allowmethods_module);
60 if (!conf || conf->allowed == 0) {
64 r->allowed = conf->allowed;
66 if (conf->allowed & (AP_METHOD_BIT << method)) {
70 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01623)
71 "client method denied by server configuration: '%s' to %s%s",
73 r->filename ? "" : "uri ",
74 r->filename ? r->filename : r->uri);
76 return HTTP_METHOD_NOT_ALLOWED;
79 static void *am_create_conf(apr_pool_t *p, char *dummy)
81 am_conf_t *conf = apr_pcalloc(p, sizeof(am_conf_t));
84 conf->allowed_set = 0;
88 static void *am_merge_conf(apr_pool_t *pool, void *a, void *b)
90 am_conf_t *base = (am_conf_t *)a;
91 am_conf_t *add = (am_conf_t *)b;
92 am_conf_t *conf = apr_palloc(pool, sizeof(am_conf_t));
94 if (add->allowed_set) {
95 conf->allowed = add->allowed;
96 conf->allowed_set = add->allowed_set;
99 conf->allowed = base->allowed;
100 conf->allowed_set = base->allowed_set;
106 static const char *am_allowmethods(cmd_parms *cmd, void *d, int argc,
110 am_conf_t *conf = (am_conf_t *)d;
113 return "AllowMethods: No method or 'reset' keyword given";
116 if (strcasecmp("reset", argv[0]) == 0) {
118 conf->allowed_set = 1;
123 for (i = 0; i < argc; i++) {
126 m = ap_method_number_of(argv[i]);
127 if (m == M_INVALID) {
128 return apr_pstrcat(cmd->pool, "AllowMethods: Invalid Method '",
132 conf->allowed |= (AP_METHOD_BIT << m);
134 conf->allowed_set = 1;
138 static void am_register_hooks(apr_pool_t * p)
140 ap_hook_access_checker(am_check_access, NULL, NULL, APR_HOOK_REALLY_FIRST);
143 static const command_rec am_cmds[] = {
144 AP_INIT_TAKE_ARGV("AllowMethods", am_allowmethods, NULL,
146 "only allow specific methods"),
150 AP_DECLARE_MODULE(allowmethods) = {
151 STANDARD20_MODULE_STUFF,