]> granicus.if.org Git - apache/blob - modules/aaa/mod_allowmethods.c
move a temporary table from r->pool to a temporary
[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   am_conf_t* base = (am_conf_t*) a;
90   am_conf_t* add = (am_conf_t*) b;
91   am_conf_t* conf = apr_palloc(pool, sizeof(am_conf_t));
92
93   if (add->allowed_set) {
94       conf->allowed = add->allowed;
95       conf->allowed_set = add->allowed_set;
96   } else {
97       conf->allowed = base->allowed;
98       conf->allowed_set = base->allowed_set;
99   }
100
101   return conf;
102 }
103
104 static const char *am_allowmethods(cmd_parms *cmd, void *d, int argc, char *const argv[])
105 {
106   int i;
107   am_conf_t* conf = (am_conf_t*) d;
108   if (argc == 0) {
109       return "AllowMethods: No method or 'reset' keyword given";
110   }
111   if (argc == 1) {
112     if (strcasecmp("reset", argv[0]) == 0) {
113       conf->allowed = 0;
114       conf->allowed_set = 1;
115       return NULL;
116     }
117   }
118
119   for (i = 0; i < argc; i++) {
120     int m = 0;
121     m = ap_method_number_of(argv[i]);
122     if (m == M_INVALID) {
123       return apr_pstrcat(cmd->pool, "AllowMethods: Invalid Method '", argv[i], "'", NULL);
124     }
125
126     conf->allowed |= (AP_METHOD_BIT << m);
127   }
128   conf->allowed_set = 1;
129   return NULL;
130 }
131
132 static void am_register_hooks(apr_pool_t * p)
133 {
134   ap_hook_access_checker(am_check_access, NULL, NULL, APR_HOOK_REALLY_FIRST);
135 }
136
137 static const command_rec am_cmds[] = {
138   AP_INIT_TAKE_ARGV("AllowMethods", am_allowmethods, NULL,
139               ACCESS_CONF,
140               "only allow specific methods"),
141   {NULL}
142 };
143
144 AP_DECLARE_MODULE(allowmethods) = {
145   STANDARD20_MODULE_STUFF,
146   am_create_conf,
147   am_merge_conf,
148   NULL,
149   NULL,
150   am_cmds,
151   am_register_hooks,
152 };
153