]> granicus.if.org Git - apache/blob - modules/aaa/mod_authz_host.c
PR59612: Print the ap_expr() error message when an expression cannot
[apache] / modules / aaa / mod_authz_host.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 #include "apr_hash.h"
28
29 #define APR_WANT_STRFUNC
30 #define APR_WANT_BYTEFUNC
31 #include "apr_want.h"
32
33 #include "ap_config.h"
34 #include "ap_provider.h"
35 #include "httpd.h"
36 #include "http_core.h"
37 #include "http_config.h"
38 #include "http_log.h"
39 #include "http_protocol.h"
40 #include "http_request.h"
41
42 #include "mod_auth.h"
43
44 #if APR_HAVE_NETINET_IN_H
45 #include <netinet/in.h>
46 #endif
47
48 /*
49  * To save memory if the same subnets are used in hundres of vhosts, we store
50  * each subnet only once and use this temporary hash to find it again.
51  */
52 static apr_hash_t *parsed_subnets;
53
54 static apr_ipsubnet_t *localhost_v4;
55 #if APR_HAVE_IPV6
56 static apr_ipsubnet_t *localhost_v6;
57 #endif
58
59 static int in_domain(const char *domain, const char *what)
60 {
61     int dl = strlen(domain);
62     int wl = strlen(what);
63
64     if ((wl - dl) >= 0) {
65         if (strcasecmp(domain, &what[wl - dl]) != 0) {
66             return 0;
67         }
68
69         /* Make sure we matched an *entire* subdomain --- if the user
70          * said 'allow from good.com', we don't want people from nogood.com
71          * to be able to get in.
72          */
73
74         if (wl == dl) {
75             return 1;                /* matched whole thing */
76         }
77         else {
78             return (domain[0] == '.' || what[wl - dl - 1] == '.');
79         }
80     }
81     else {
82         return 0;
83     }
84 }
85
86 static const char *ip_parse_config(cmd_parms *cmd,
87                                    const char *require_line,
88                                    const void **parsed_require_line)
89 {
90     const char *t, *w;
91     int count = 0;
92     apr_ipsubnet_t **ip;
93     apr_pool_t *ptemp = cmd->temp_pool;
94     apr_pool_t *p = cmd->pool;
95
96     /* The 'ip' provider will allow the configuration to specify a list of
97         ip addresses to check rather than a single address.  This is different
98         from the previous host based syntax. */
99
100     t = require_line;
101     while ((w = ap_getword_conf(ptemp, &t)) && w[0])
102         count++;
103
104     if (count == 0)
105         return "'require ip' requires an argument";
106
107     ip = apr_pcalloc(p, sizeof(apr_ipsubnet_t *) * (count + 1));
108     *parsed_require_line = ip;
109
110     t = require_line;
111     while ((w = ap_getword_conf(ptemp, &t)) && w[0]) {
112         char *addr = apr_pstrdup(ptemp, w);
113         char *mask;
114         apr_status_t rv;
115
116         if (parsed_subnets &&
117             (*ip = apr_hash_get(parsed_subnets, w, APR_HASH_KEY_STRING)) != NULL)
118         {
119             /* we already have parsed this subnet */
120             ip++;
121             continue;
122         }
123
124         if ((mask = ap_strchr(addr, '/')))
125             *mask++ = '\0';
126
127         rv = apr_ipsubnet_create(ip, addr, mask, p);
128
129         if(APR_STATUS_IS_EINVAL(rv)) {
130             /* looked nothing like an IP address */
131             return apr_psprintf(p, "ip address '%s' appears to be invalid", w);
132         }
133         else if (rv != APR_SUCCESS) {
134             return apr_psprintf(p, "ip address '%s' appears to be invalid: %pm",
135                                 w, &rv);
136         }
137
138         if (parsed_subnets)
139             apr_hash_set(parsed_subnets, w, APR_HASH_KEY_STRING, *ip);
140         ip++;
141     }
142
143     return NULL;
144 }
145
146 static authz_status ip_check_authorization(request_rec *r,
147                                            const char *require_line,
148                                            const void *parsed_require_line)
149 {
150     /* apr_ipsubnet_test should accept const but doesn't */
151     apr_ipsubnet_t **ip = (apr_ipsubnet_t **)parsed_require_line;
152
153     while (*ip) {
154         if (apr_ipsubnet_test(*ip, r->useragent_addr))
155             return AUTHZ_GRANTED;
156         ip++;
157     }
158
159     /* authz_core will log the require line and the result at DEBUG */
160     return AUTHZ_DENIED;
161 }
162
163 static authz_status host_check_authorization(request_rec *r,
164                                              const char *require_line,
165                                              const void *parsed_require_line)
166 {
167     const char *t;
168     char *w, *hash_ptr;
169     const char *remotehost = NULL;
170     int remotehost_is_ip;
171
172     remotehost = ap_get_useragent_host(r, REMOTE_DOUBLE_REV, &remotehost_is_ip);
173
174     if ((remotehost == NULL) || remotehost_is_ip) {
175         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01753)
176                       "access check of '%s' to %s failed, reason: unable to get the "
177                       "remote host name", require_line, r->uri);
178     }
179     else {
180         const char *err = NULL;
181         const ap_expr_info_t *expr = parsed_require_line;
182         const char *require;
183
184         require = ap_expr_str_exec(r, expr, &err);
185         if (err) {
186             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02593)
187                           "authz_host authorize: require host: Can't "
188                           "evaluate require expression: %s", err);
189             return AUTHZ_DENIED;
190         }
191
192         /* The 'host' provider will allow the configuration to specify a list of
193             host names to check rather than a single name.  This is different
194             from the previous host based syntax. */
195         t = require;
196         while ((w = ap_getword_conf(r->pool, &t)) && w[0]) {
197             /* '#' is not valid hostname character and admin could specify
198              * 'Require host localhost# Add example.com later'. We should not
199              * grant access to 'example.com' in that case. */
200             if ((hash_ptr = ap_strchr(w, '#'))) {
201                 if (hash_ptr == w) {
202                     break;
203                 }
204                 *hash_ptr = '\0';
205             }
206             if (in_domain(w, remotehost)) {
207                 return AUTHZ_GRANTED;
208             }
209             if (hash_ptr) {
210                 break;
211             }
212         }
213     }
214
215     /* authz_core will log the require line and the result at DEBUG */
216     return AUTHZ_DENIED;
217 }
218
219 static authz_status
220 forward_dns_check_authorization(request_rec *r,
221                                 const char *require_line,
222                                 const void *parsed_require_line)
223 {
224     const char *err = NULL;
225     const ap_expr_info_t *expr = parsed_require_line;
226     const char *require, *t;
227     char *w;
228
229     /* the require line is an expression, which is evaluated now. */
230     require = ap_expr_str_exec(r, expr, &err);
231     if (err) {
232       ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(03354)
233                     "authz_host authorize: require forward-dns: "
234                     "Can't evaluate require expression: %s", err);
235       return AUTHZ_DENIED;
236     }
237
238     /* tokenize expected list of names */
239     t = require;
240     while ((w = ap_getword_conf(r->pool, &t)) && w[0]) {
241
242         apr_sockaddr_t *sa;
243         apr_status_t rv;
244         char *hash_ptr;
245
246         /* stop on apache configuration file comments */
247         if ((hash_ptr = ap_strchr(w, '#'))) {
248             if (hash_ptr == w) {
249                 break;
250             }
251             *hash_ptr = '\0';
252         }
253
254         /* does the client ip match one of the names? */
255         rv = apr_sockaddr_info_get(&sa, w, APR_UNSPEC, 0, 0, r->pool);
256         if (rv == APR_SUCCESS) {
257
258             while (sa) {
259                 int match = apr_sockaddr_equal(sa, r->useragent_addr);
260
261                 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03355)
262                               "access check for %s as '%s': %s",
263                               r->useragent_ip, w, match? "yes": "no");
264                 if (match) {
265                     return AUTHZ_GRANTED;
266                 }
267
268                 sa = sa->next;
269             }
270         }
271         else {
272             ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(03356)
273                           "No sockaddr info for \"%s\"", w);
274         }
275
276         /* stop processing, we are in a comment */
277         if (hash_ptr) {
278             break;
279         }
280     }
281
282     return AUTHZ_DENIED;
283 }
284
285 static authz_status local_check_authorization(request_rec *r,
286                                               const char *require_line,
287                                               const void *parsed_require_line)
288 {
289      if (   apr_sockaddr_equal(r->connection->local_addr,
290                                r->useragent_addr)
291          || apr_ipsubnet_test(localhost_v4, r->useragent_addr)
292 #if APR_HAVE_IPV6
293          || apr_ipsubnet_test(localhost_v6, r->useragent_addr)
294 #endif
295         )
296      {
297         return AUTHZ_GRANTED;
298      }
299
300      return AUTHZ_DENIED;
301 }
302
303 static const char *host_parse_config(cmd_parms *cmd, const char *require_line,
304                                      const void **parsed_require_line)
305 {
306     const char *expr_err = NULL;
307     ap_expr_info_t *expr;
308
309     expr = ap_expr_parse_cmd(cmd, require_line, AP_EXPR_FLAG_STRING_RESULT,
310             &expr_err, NULL);
311
312     if (expr_err)
313         return apr_pstrcat(cmd->temp_pool,
314                            "Cannot parse expression in require line: ",
315                            expr_err, NULL);
316
317     *parsed_require_line = expr;
318
319     return NULL;
320 }
321
322 static const authz_provider authz_ip_provider =
323 {
324     &ip_check_authorization,
325     &ip_parse_config,
326 };
327
328 static const authz_provider authz_host_provider =
329 {
330     &host_check_authorization,
331     &host_parse_config,
332 };
333
334 static const authz_provider authz_forward_dns_provider =
335 {
336     &forward_dns_check_authorization,
337     &host_parse_config,
338 };
339
340 static const authz_provider authz_local_provider =
341 {
342     &local_check_authorization,
343     NULL,
344 };
345
346
347 static int authz_host_pre_config(apr_pool_t *p, apr_pool_t *plog,
348                                  apr_pool_t *ptemp)
349 {
350     /* we only use this hash in the parse config phase, ptemp is enough */
351     parsed_subnets = apr_hash_make(ptemp);
352
353     apr_ipsubnet_create(&localhost_v4, "127.0.0.0", "8", p);
354     apr_hash_set(parsed_subnets, "127.0.0.0/8", APR_HASH_KEY_STRING, localhost_v4);
355
356 #if APR_HAVE_IPV6
357     apr_ipsubnet_create(&localhost_v6, "::1", NULL, p);
358     apr_hash_set(parsed_subnets, "::1", APR_HASH_KEY_STRING, localhost_v6);
359 #endif
360
361     return OK;
362 }
363
364 static int authz_host_post_config(apr_pool_t *p, apr_pool_t *plog,
365                                   apr_pool_t *ptemp, server_rec *s)
366 {
367     /* make sure we don't use this during .htaccess parsing */
368     parsed_subnets = NULL;
369
370     return OK;
371 }
372
373 static void register_hooks(apr_pool_t *p)
374 {
375     ap_hook_pre_config(authz_host_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
376     ap_hook_post_config(authz_host_post_config, NULL, NULL, APR_HOOK_MIDDLE);
377
378     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "ip",
379                               AUTHZ_PROVIDER_VERSION,
380                               &authz_ip_provider, AP_AUTH_INTERNAL_PER_CONF);
381     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "host",
382                               AUTHZ_PROVIDER_VERSION,
383                               &authz_host_provider, AP_AUTH_INTERNAL_PER_CONF);
384     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "forward-dns",
385                               AUTHZ_PROVIDER_VERSION,
386                               &authz_forward_dns_provider,
387                               AP_AUTH_INTERNAL_PER_CONF);
388     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "local",
389                               AUTHZ_PROVIDER_VERSION,
390                               &authz_local_provider, AP_AUTH_INTERNAL_PER_CONF);
391 }
392
393 AP_DECLARE_MODULE(authz_host) =
394 {
395     STANDARD20_MODULE_STUFF,
396     NULL,                           /* dir config creater */
397     NULL,                           /* dir merger --- default is to override */
398     NULL,                           /* server config */
399     NULL,                           /* merge server config */
400     NULL,
401     register_hooks                  /* register hooks */
402 };