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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * Security options etc.
20 * Module derived from code originally written by Rob McCool
24 #include "apr_strings.h"
25 #include "apr_network_io.h"
29 #define APR_WANT_STRFUNC
30 #define APR_WANT_BYTEFUNC
33 #include "ap_config.h"
34 #include "ap_provider.h"
36 #include "http_core.h"
37 #include "http_config.h"
39 #include "http_protocol.h"
40 #include "http_request.h"
44 #if APR_HAVE_NETINET_IN_H
45 #include <netinet/in.h>
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.
52 static apr_hash_t *parsed_subnets;
54 static apr_ipsubnet_t *localhost_v4;
56 static apr_ipsubnet_t *localhost_v6;
59 static int in_domain(const char *domain, const char *what)
61 int dl = strlen(domain);
62 int wl = strlen(what);
65 if (strcasecmp(domain, &what[wl - dl]) != 0) {
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.
75 return 1; /* matched whole thing */
78 return (domain[0] == '.' || what[wl - dl - 1] == '.');
86 static const char *ip_parse_config(cmd_parms *cmd,
87 const char *require_line,
88 const void **parsed_require_line)
93 apr_pool_t *ptemp = cmd->temp_pool;
94 apr_pool_t *p = cmd->pool;
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. */
101 while ((w = ap_getword_conf(ptemp, &t)) && w[0])
105 return "'require ip' requires an argument";
107 ip = apr_pcalloc(p, sizeof(apr_ipsubnet_t *) * (count + 1));
108 *parsed_require_line = ip;
111 while ((w = ap_getword_conf(ptemp, &t)) && w[0]) {
112 char *addr = apr_pstrdup(ptemp, w);
116 if (parsed_subnets &&
117 (*ip = apr_hash_get(parsed_subnets, w, APR_HASH_KEY_STRING)) != NULL)
119 /* we already have parsed this subnet */
124 if ((mask = ap_strchr(addr, '/')))
127 rv = apr_ipsubnet_create(ip, addr, mask, p);
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);
133 else if (rv != APR_SUCCESS) {
134 return apr_psprintf(p, "ip address '%s' appears to be invalid: %pm",
139 apr_hash_set(parsed_subnets, w, APR_HASH_KEY_STRING, *ip);
146 static authz_status ip_check_authorization(request_rec *r,
147 const char *require_line,
148 const void *parsed_require_line)
150 /* apr_ipsubnet_test should accept const but doesn't */
151 apr_ipsubnet_t **ip = (apr_ipsubnet_t **)parsed_require_line;
154 if (apr_ipsubnet_test(*ip, r->useragent_addr))
155 return AUTHZ_GRANTED;
159 /* authz_core will log the require line and the result at DEBUG */
163 static authz_status host_check_authorization(request_rec *r,
164 const char *require_line,
165 const void *parsed_require_line)
169 const char *remotehost = NULL;
170 int remotehost_is_ip;
172 remotehost = ap_get_remote_host(r->connection,
177 if ((remotehost == NULL) || remotehost_is_ip) {
178 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01753)
179 "access check of '%s' to %s failed, reason: unable to get the "
180 "remote host name", require_line, r->uri);
183 const char *err = NULL;
184 const ap_expr_info_t *expr = parsed_require_line;
187 require = ap_expr_str_exec(r, expr, &err);
189 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02593)
190 "authz_host authorize: require host: Can't "
191 "evaluate require expression: %s", err);
195 /* The 'host' provider will allow the configuration to specify a list of
196 host names to check rather than a single name. This is different
197 from the previous host based syntax. */
199 while ((w = ap_getword_conf(r->pool, &t)) && w[0]) {
200 /* '#' is not valid hostname character and admin could specify
201 * 'Require host localhost# Add example.com later'. We should not
202 * grant access to 'example.com' in that case. */
203 if ((hash_ptr = ap_strchr(w, '#'))) {
209 if (in_domain(w, remotehost)) {
210 return AUTHZ_GRANTED;
218 /* authz_core will log the require line and the result at DEBUG */
222 static authz_status local_check_authorization(request_rec *r,
223 const char *require_line,
224 const void *parsed_require_line)
226 if ( apr_sockaddr_equal(r->connection->local_addr,
228 || apr_ipsubnet_test(localhost_v4, r->useragent_addr)
230 || apr_ipsubnet_test(localhost_v6, r->useragent_addr)
234 return AUTHZ_GRANTED;
240 static const char *host_parse_config(cmd_parms *cmd, const char *require_line,
241 const void **parsed_require_line)
243 const char *expr_err = NULL;
244 ap_expr_info_t *expr;
246 expr = ap_expr_parse_cmd(cmd, require_line, AP_EXPR_FLAG_STRING_RESULT,
250 return apr_pstrcat(cmd->temp_pool,
251 "Cannot parse expression in require line: ",
254 *parsed_require_line = expr;
259 static const authz_provider authz_ip_provider =
261 &ip_check_authorization,
265 static const authz_provider authz_host_provider =
267 &host_check_authorization,
271 static const authz_provider authz_local_provider =
273 &local_check_authorization,
278 static int authz_host_pre_config(apr_pool_t *p, apr_pool_t *plog,
281 /* we only use this hash in the parse config phase, ptemp is enough */
282 parsed_subnets = apr_hash_make(ptemp);
284 apr_ipsubnet_create(&localhost_v4, "127.0.0.0", "8", p);
285 apr_hash_set(parsed_subnets, "127.0.0.0/8", APR_HASH_KEY_STRING, localhost_v4);
288 apr_ipsubnet_create(&localhost_v6, "::1", NULL, p);
289 apr_hash_set(parsed_subnets, "::1", APR_HASH_KEY_STRING, localhost_v6);
295 static int authz_host_post_config(apr_pool_t *p, apr_pool_t *plog,
296 apr_pool_t *ptemp, server_rec *s)
298 /* make sure we don't use this during .htaccess parsing */
299 parsed_subnets = NULL;
304 static void register_hooks(apr_pool_t *p)
306 ap_hook_pre_config(authz_host_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
307 ap_hook_post_config(authz_host_post_config, NULL, NULL, APR_HOOK_MIDDLE);
309 ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "ip",
310 AUTHZ_PROVIDER_VERSION,
311 &authz_ip_provider, AP_AUTH_INTERNAL_PER_CONF);
312 ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "host",
313 AUTHZ_PROVIDER_VERSION,
314 &authz_host_provider, AP_AUTH_INTERNAL_PER_CONF);
315 ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "local",
316 AUTHZ_PROVIDER_VERSION,
317 &authz_local_provider, AP_AUTH_INTERNAL_PER_CONF);
320 AP_DECLARE_MODULE(authz_host) =
322 STANDARD20_MODULE_STUFF,
323 NULL, /* dir config creater */
324 NULL, /* dir merger --- default is to override */
325 NULL, /* server config */
326 NULL, /* merge server config */
328 register_hooks /* register hooks */