]> granicus.if.org Git - apache/blob - modules/aaa/mod_authz_host.c
Further clarify the naming of the entity that originates the request by
[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             char msgbuf[120];
135             apr_strerror(rv, msgbuf, sizeof msgbuf);
136             return apr_psprintf(p, "ip address '%s' appears to be invalid: %s",
137                                 w, msgbuf);
138         }
139
140         if (parsed_subnets)
141             apr_hash_set(parsed_subnets, w, APR_HASH_KEY_STRING, *ip);
142         ip++;
143     }
144
145     return NULL;
146 }
147
148 static authz_status ip_check_authorization(request_rec *r,
149                                            const char *require_line,
150                                            const void *parsed_require_line)
151 {
152     /* apr_ipsubnet_test should accept const but doesn't */
153     apr_ipsubnet_t **ip = (apr_ipsubnet_t **)parsed_require_line;
154
155     while (*ip) {
156         if (apr_ipsubnet_test(*ip, r->useragent_addr))
157             return AUTHZ_GRANTED;
158         ip++;
159     }
160
161     /* authz_core will log the require line and the result at DEBUG */
162     return AUTHZ_DENIED;
163 }
164
165 static authz_status host_check_authorization(request_rec *r,
166                                              const char *require_line,
167                                              const void *parsed_require_line)
168 {
169     const char *t, *w;
170     const char *remotehost = NULL;
171     int remotehost_is_ip;
172
173     remotehost = ap_get_remote_host(r->connection,
174                                     r->per_dir_config,
175                                     REMOTE_DOUBLE_REV,
176                                     &remotehost_is_ip);
177
178     if ((remotehost == NULL) || remotehost_is_ip) {
179         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01753)
180                       "access check of '%s' to %s failed, reason: unable to get the "
181                       "remote host name", require_line, r->uri);
182     }
183     else {
184         /* The 'host' provider will allow the configuration to specify a list of
185             host names to check rather than a single name.  This is different
186             from the previous host based syntax. */
187         t = require_line;
188         while ((w = ap_getword_conf(r->pool, &t)) && w[0]) {
189             if (in_domain(w, remotehost)) {
190                 return AUTHZ_GRANTED;
191             }
192         }
193     }
194
195     /* authz_core will log the require line and the result at DEBUG */
196     return AUTHZ_DENIED;
197 }
198
199 static authz_status local_check_authorization(request_rec *r,
200                                               const char *require_line,
201                                               const void *parsed_require_line)
202 {
203      if (   apr_sockaddr_equal(r->connection->local_addr,
204                                r->useragent_addr)
205          || apr_ipsubnet_test(localhost_v4, r->useragent_addr)
206 #if APR_HAVE_IPV6
207          || apr_ipsubnet_test(localhost_v6, r->useragent_addr)
208 #endif
209         )
210      {
211         return AUTHZ_GRANTED;
212      }
213
214      return AUTHZ_DENIED;
215 }
216
217 static const authz_provider authz_ip_provider =
218 {
219     &ip_check_authorization,
220     &ip_parse_config,
221 };
222
223 static const authz_provider authz_host_provider =
224 {
225     &host_check_authorization,
226     NULL,
227 };
228
229 static const authz_provider authz_local_provider =
230 {
231     &local_check_authorization,
232     NULL,
233 };
234
235
236 static int authz_host_pre_config(apr_pool_t *p, apr_pool_t *plog,
237                                  apr_pool_t *ptemp)
238 {
239     /* we only use this hash in the parse config phase, ptemp is enough */
240     parsed_subnets = apr_hash_make(ptemp);
241
242     apr_ipsubnet_create(&localhost_v4, "127.0.0.0", "8", p);
243     apr_hash_set(parsed_subnets, "127.0.0.0/8", APR_HASH_KEY_STRING, localhost_v4);
244
245 #if APR_HAVE_IPV6
246     apr_ipsubnet_create(&localhost_v6, "::1", NULL, p);
247     apr_hash_set(parsed_subnets, "::1", APR_HASH_KEY_STRING, localhost_v6);
248 #endif
249
250     return OK;
251 }
252
253 static int authz_host_post_config(apr_pool_t *p, apr_pool_t *plog,
254                                   apr_pool_t *ptemp, server_rec *s)
255 {
256     /* make sure we don't use this during .htaccess parsing */
257     parsed_subnets = NULL;
258
259     return OK;
260 }
261
262 static void register_hooks(apr_pool_t *p)
263 {
264     ap_hook_pre_config(authz_host_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
265     ap_hook_post_config(authz_host_post_config, NULL, NULL, APR_HOOK_MIDDLE);
266
267     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "ip",
268                               AUTHZ_PROVIDER_VERSION,
269                               &authz_ip_provider, AP_AUTH_INTERNAL_PER_CONF);
270     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "host",
271                               AUTHZ_PROVIDER_VERSION,
272                               &authz_host_provider, AP_AUTH_INTERNAL_PER_CONF);
273     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "local",
274                               AUTHZ_PROVIDER_VERSION,
275                               &authz_local_provider, AP_AUTH_INTERNAL_PER_CONF);
276 }
277
278 AP_DECLARE_MODULE(authz_host) =
279 {
280     STANDARD20_MODULE_STUFF,
281     NULL,                           /* dir config creater */
282     NULL,                           /* dir merger --- default is to override */
283     NULL,                           /* server config */
284     NULL,                           /* merge server config */
285     NULL,
286     register_hooks                  /* register hooks */
287 };