]> granicus.if.org Git - apache/blob - modules/aaa/mod_allowmethods.c
Added many log numbers to log statements that
[apache] / modules / aaa / mod_allowmethods.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "httpd.h"
18 #include "http_core.h"
19 #include "http_config.h"
20 #include "http_protocol.h"
21 #include "http_request.h"
22 #include "http_log.h"
23 #include "apr_strings.h"
24
25 /**
26  * This module makes it easy to restrict what HTTP methods can be ran against
27  * a server.
28  *
29  * It provides one comand:
30  *    AllowMethods
31  * This command takes a list of HTTP methods to allow.
32  *
33  *  The most common configuration should be like this:
34  *   <Directory />
35  *    AllowMethods GET HEAD OPTIONS
36  *   </Directory>
37  *   <Directory /special/cgi-bin>
38  *      AllowMethods GET HEAD OPTIONS POST
39  *   </Directory>
40  *  Non-matching methods will be returned a status 405 (method not allowed)
41  *
42  *  To allow all methods, and effectively turn off mod_allowmethods, use:
43  *    AllowMethods reset
44  */
45
46 typedef struct am_conf_t {
47     int allowed_set;
48     apr_int64_t allowed;
49 } am_conf_t;
50
51 module AP_MODULE_DECLARE_DATA allowmethods_module;
52
53 static int am_check_access(request_rec *r)
54 {
55     int method = r->method_number;
56     am_conf_t *conf;
57
58     conf = (am_conf_t *) ap_get_module_config(r->per_dir_config,
59                                               &allowmethods_module);
60     if (!conf || conf->allowed == 0) {
61         return DECLINED;
62     }
63
64     r->allowed = conf->allowed;
65
66     if (conf->allowed & (AP_METHOD_BIT << method)) {
67         return DECLINED;
68     }
69
70     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01623)
71                   "client method denied by server configuration: '%s' to %s%s",
72                   r->method,
73                   r->filename ? "" : "uri ",
74                   r->filename ? r->filename : r->uri);
75
76     return HTTP_METHOD_NOT_ALLOWED;
77 }
78
79 static void *am_create_conf(apr_pool_t *p, char *dummy)
80 {
81     am_conf_t *conf = apr_pcalloc(p, sizeof(am_conf_t));
82
83     conf->allowed = 0;
84     conf->allowed_set = 0;
85     return conf;
86 }
87
88 static void *am_merge_conf(apr_pool_t *pool, void *a, void *b)
89 {
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));
93
94     if (add->allowed_set) {
95         conf->allowed = add->allowed;
96         conf->allowed_set = add->allowed_set;
97     }
98     else {
99         conf->allowed = base->allowed;
100         conf->allowed_set = base->allowed_set;
101     }
102
103     return conf;
104 }
105
106 static const char *am_allowmethods(cmd_parms *cmd, void *d, int argc,
107                                    char *const argv[])
108 {
109     int i;
110     am_conf_t *conf = (am_conf_t *)d;
111
112     if (argc == 0) {
113         return "AllowMethods: No method or 'reset' keyword given";
114     }
115     if (argc == 1) {
116         if (strcasecmp("reset", argv[0]) == 0) {
117             conf->allowed = 0;
118             conf->allowed_set = 1;
119             return NULL;
120         }
121     }
122
123     for (i = 0; i < argc; i++) {
124         int m;
125
126         m = ap_method_number_of(argv[i]);
127         if (m == M_INVALID) {
128             return apr_pstrcat(cmd->pool, "AllowMethods: Invalid Method '",
129                                argv[i], "'", NULL);
130         }
131
132         conf->allowed |= (AP_METHOD_BIT << m);
133     }
134     conf->allowed_set = 1;
135     return NULL;
136 }
137
138 static void am_register_hooks(apr_pool_t * p)
139 {
140     ap_hook_access_checker(am_check_access, NULL, NULL, APR_HOOK_REALLY_FIRST);
141 }
142
143 static const command_rec am_cmds[] = {
144     AP_INIT_TAKE_ARGV("AllowMethods", am_allowmethods, NULL,
145                       ACCESS_CONF,
146                       "only allow specific methods"),
147     {NULL}
148 };
149
150 AP_DECLARE_MODULE(allowmethods) = {
151     STANDARD20_MODULE_STUFF,
152     am_create_conf,
153     am_merge_conf,
154     NULL,
155     NULL,
156     am_cmds,
157     am_register_hooks,
158 };