]> granicus.if.org Git - apache/blob - modules/aaa/mod_access_compat.c
move a temporary table from r->pool to a temporary
[apache] / modules / aaa / mod_access_compat.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 /*
18  * Security options etc.
19  *
20  * Module derived from code originally written by Rob McCool
21  *
22  */
23
24 #include "apr_strings.h"
25 #include "apr_network_io.h"
26 #include "apr_md5.h"
27
28 #define APR_WANT_STRFUNC
29 #define APR_WANT_BYTEFUNC
30 #include "apr_want.h"
31
32 #include "ap_config.h"
33 #include "httpd.h"
34 #include "http_core.h"
35 #include "http_config.h"
36 #include "http_log.h"
37 #include "http_protocol.h"
38 #include "http_request.h"
39
40 #include "mod_auth.h"
41
42 #if APR_HAVE_NETINET_IN_H
43 #include <netinet/in.h>
44 #endif
45
46 enum allowdeny_type {
47     T_ENV,
48     T_NENV,
49     T_ALL,
50     T_IP,
51     T_HOST,
52     T_FAIL
53 };
54
55 typedef struct {
56     apr_int64_t limited;
57     union {
58         char *from;
59         apr_ipsubnet_t *ip;
60     } x;
61     enum allowdeny_type type;
62 } allowdeny;
63
64 /* things in the 'order' array */
65 #define DENY_THEN_ALLOW 0
66 #define ALLOW_THEN_DENY 1
67 #define MUTUAL_FAILURE 2
68
69 typedef struct {
70     int order[METHODS];
71     apr_array_header_t *allows;
72     apr_array_header_t *denys;
73     int *satisfy; /* for every method one */
74 } access_compat_dir_conf;
75
76 module AP_MODULE_DECLARE_DATA access_compat_module;
77
78 static void *create_access_compat_dir_config(apr_pool_t *p, char *dummy)
79 {
80     int i;
81     access_compat_dir_conf *conf =
82         (access_compat_dir_conf *)apr_pcalloc(p, sizeof(access_compat_dir_conf));
83
84     for (i = 0; i < METHODS; ++i) {
85         conf->order[i] = DENY_THEN_ALLOW;
86     }
87     conf->allows = apr_array_make(p, 1, sizeof(allowdeny));
88     conf->denys = apr_array_make(p, 1, sizeof(allowdeny));
89     conf->satisfy = apr_palloc(p, sizeof(*conf->satisfy) * METHODS);
90     for (i = 0; i < METHODS; ++i) {
91         conf->satisfy[i] = SATISFY_NOSPEC;
92     }
93
94     return (void *)conf;
95 }
96
97 static const char *order(cmd_parms *cmd, void *dv, const char *arg)
98 {
99     access_compat_dir_conf *d = (access_compat_dir_conf *) dv;
100     int i, o;
101
102     if (!strcasecmp(arg, "allow,deny"))
103         o = ALLOW_THEN_DENY;
104     else if (!strcasecmp(arg, "deny,allow"))
105         o = DENY_THEN_ALLOW;
106     else if (!strcasecmp(arg, "mutual-failure"))
107         o = MUTUAL_FAILURE;
108     else
109         return "unknown order";
110
111     for (i = 0; i < METHODS; ++i)
112         if (cmd->limited & (AP_METHOD_BIT << i))
113             d->order[i] = o;
114
115     return NULL;
116 }
117
118 static const char *satisfy(cmd_parms *cmd, void *dv, const char *arg)
119 {
120     access_compat_dir_conf *d = (access_compat_dir_conf *) dv;
121     int satisfy = SATISFY_NOSPEC;
122     int i;
123
124     if (!strcasecmp(arg, "all")) {
125         satisfy = SATISFY_ALL;
126     }
127     else if (!strcasecmp(arg, "any")) {
128         satisfy = SATISFY_ANY;
129     }
130     else {
131         return "Satisfy either 'any' or 'all'.";
132     }
133
134     for (i = 0; i < METHODS; ++i) {
135         if (cmd->limited & (AP_METHOD_BIT << i)) {
136             d->satisfy[i] = satisfy;
137         }
138     }
139
140     return NULL;
141 }
142
143 static const char *allow_cmd(cmd_parms *cmd, void *dv, const char *from,
144                              const char *where_c)
145 {
146     access_compat_dir_conf *d = (access_compat_dir_conf *) dv;
147     allowdeny *a;
148     char *where = apr_pstrdup(cmd->pool, where_c);
149     char *s;
150     apr_status_t rv;
151
152     if (strcasecmp(from, "from"))
153         return "allow and deny must be followed by 'from'";
154
155     a = (allowdeny *) apr_array_push(cmd->info ? d->allows : d->denys);
156     a->x.from = where;
157     a->limited = cmd->limited;
158
159     if (!strncasecmp(where, "env=!", 5)) {
160         a->type = T_NENV;
161         a->x.from += 5;
162
163     }
164     else if (!strncasecmp(where, "env=", 4)) {
165         a->type = T_ENV;
166         a->x.from += 4;
167
168     }
169     else if (!strcasecmp(where, "all")) {
170         a->type = T_ALL;
171     }
172     else if ((s = ap_strchr(where, '/'))) {
173         *s++ = '\0';
174         rv = apr_ipsubnet_create(&a->x.ip, where, s, cmd->pool);
175         if(APR_STATUS_IS_EINVAL(rv)) {
176             /* looked nothing like an IP address */
177             return "An IP address was expected";
178         }
179         else if (rv != APR_SUCCESS) {
180             return apr_psprintf(cmd->pool, "%pm", &rv);
181         }
182         a->type = T_IP;
183     }
184     else if (!APR_STATUS_IS_EINVAL(rv = apr_ipsubnet_create(&a->x.ip, where,
185                                                             NULL, cmd->pool))) {
186         if (rv != APR_SUCCESS)
187             return apr_psprintf(cmd->pool, "%pm", &rv);
188         a->type = T_IP;
189     }
190     else { /* no slash, didn't look like an IP address => must be a host */
191         a->type = T_HOST;
192     }
193
194     return NULL;
195 }
196
197 static char its_an_allow;
198
199 static const command_rec access_compat_cmds[] =
200 {
201     AP_INIT_TAKE1("order", order, NULL, OR_LIMIT,
202                   "'allow,deny', 'deny,allow', or 'mutual-failure'"),
203     AP_INIT_ITERATE2("allow", allow_cmd, &its_an_allow, OR_LIMIT,
204                      "'from' followed by hostnames or IP-address wildcards"),
205     AP_INIT_ITERATE2("deny", allow_cmd, NULL, OR_LIMIT,
206                      "'from' followed by hostnames or IP-address wildcards"),
207     AP_INIT_TAKE1("Satisfy", satisfy, NULL, OR_AUTHCFG,
208                   "access policy if both allow and require used ('all' or 'any')"),
209     {NULL}
210 };
211
212 static int in_domain(const char *domain, const char *what)
213 {
214     int dl = strlen(domain);
215     int wl = strlen(what);
216
217     if ((wl - dl) >= 0) {
218         if (strcasecmp(domain, &what[wl - dl]) != 0) {
219             return 0;
220         }
221
222         /* Make sure we matched an *entire* subdomain --- if the user
223          * said 'allow from good.com', we don't want people from nogood.com
224          * to be able to get in.
225          */
226
227         if (wl == dl) {
228             return 1;                /* matched whole thing */
229         }
230         else {
231             return (domain[0] == '.' || what[wl - dl - 1] == '.');
232         }
233     }
234     else {
235         return 0;
236     }
237 }
238
239 static int find_allowdeny(request_rec *r, apr_array_header_t *a, int method)
240 {
241
242     allowdeny *ap = (allowdeny *) a->elts;
243     apr_int64_t mmask = (AP_METHOD_BIT << method);
244     int i;
245     int gothost = 0;
246     const char *remotehost = NULL;
247
248     for (i = 0; i < a->nelts; ++i) {
249         if (!(mmask & ap[i].limited)) {
250             continue;
251         }
252
253         switch (ap[i].type) {
254         case T_ENV:
255             if (apr_table_get(r->subprocess_env, ap[i].x.from)) {
256                 return 1;
257             }
258             break;
259
260         case T_NENV:
261             if (!apr_table_get(r->subprocess_env, ap[i].x.from)) {
262                 return 1;
263             }
264             break;
265
266         case T_ALL:
267             return 1;
268
269         case T_IP:
270             if (apr_ipsubnet_test(ap[i].x.ip, r->useragent_addr)) {
271                 return 1;
272             }
273             break;
274
275         case T_HOST:
276             if (!gothost) {
277                 int remotehost_is_ip;
278
279                 remotehost = ap_get_remote_host(r->connection,
280                                                 r->per_dir_config,
281                                                 REMOTE_DOUBLE_REV,
282                                                 &remotehost_is_ip);
283
284                 if ((remotehost == NULL) || remotehost_is_ip) {
285                     gothost = 1;
286                 }
287                 else {
288                     gothost = 2;
289                 }
290             }
291
292             if ((gothost == 2) && in_domain(ap[i].x.from, remotehost)) {
293                 return 1;
294             }
295             break;
296
297         case T_FAIL:
298             /* do nothing? */
299             break;
300         }
301     }
302
303     return 0;
304 }
305
306 static int access_compat_ap_satisfies(request_rec *r)
307 {
308     access_compat_dir_conf *conf = (access_compat_dir_conf *)
309         ap_get_module_config(r->per_dir_config, &access_compat_module);
310
311     return conf->satisfy[r->method_number];
312 }
313
314 static int check_dir_access(request_rec *r)
315 {
316     int method = r->method_number;
317     int ret = OK;
318     access_compat_dir_conf *a = (access_compat_dir_conf *)
319         ap_get_module_config(r->per_dir_config, &access_compat_module);
320
321     if (a->order[method] == ALLOW_THEN_DENY) {
322         ret = HTTP_FORBIDDEN;
323         if (find_allowdeny(r, a->allows, method)) {
324             ret = OK;
325         }
326         if (find_allowdeny(r, a->denys, method)) {
327             ret = HTTP_FORBIDDEN;
328         }
329     }
330     else if (a->order[method] == DENY_THEN_ALLOW) {
331         if (find_allowdeny(r, a->denys, method)) {
332             ret = HTTP_FORBIDDEN;
333         }
334         if (find_allowdeny(r, a->allows, method)) {
335             ret = OK;
336         }
337     }
338     else {
339         if (find_allowdeny(r, a->allows, method)
340             && !find_allowdeny(r, a->denys, method)) {
341             ret = OK;
342         }
343         else {
344             ret = HTTP_FORBIDDEN;
345         }
346     }
347
348     if (ret == HTTP_FORBIDDEN) {
349         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01797)
350                       "client denied by server configuration: %s%s",
351                       r->filename ? "" : "uri ",
352                       r->filename ? r->filename : r->uri);
353     }
354
355     return ret;
356 }
357
358 static void register_hooks(apr_pool_t *p)
359 {
360     APR_REGISTER_OPTIONAL_FN(access_compat_ap_satisfies);
361
362     /* This can be access checker since we don't require r->user to be set. */
363     ap_hook_check_access(check_dir_access, NULL, NULL, APR_HOOK_MIDDLE,
364                          AP_AUTH_INTERNAL_PER_CONF);
365 }
366
367 AP_DECLARE_MODULE(access_compat) =
368 {
369     STANDARD20_MODULE_STUFF,
370     create_access_compat_dir_config,   /* dir config creater */
371     NULL,                           /* dir merger --- default is to override */
372     NULL,                           /* server config */
373     NULL,                           /* merge server config */
374     access_compat_cmds,
375     register_hooks                  /* register hooks */
376 };