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