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"
28 #define APR_WANT_STRFUNC
29 #define APR_WANT_BYTEFUNC
32 #include "ap_config.h"
34 #include "http_core.h"
35 #include "http_config.h"
37 #include "http_request.h"
39 #if APR_HAVE_NETINET_IN_H
40 #include <netinet/in.h>
57 enum allowdeny_type type;
60 /* things in the 'order' array */
61 #define DENY_THEN_ALLOW 0
62 #define ALLOW_THEN_DENY 1
63 #define MUTUAL_FAILURE 2
67 apr_array_header_t *allows;
68 apr_array_header_t *denys;
71 module AP_MODULE_DECLARE_DATA access_module;
73 static void *create_access_dir_config(apr_pool_t *p, char *dummy)
76 access_dir_conf *conf =
77 (access_dir_conf *)apr_pcalloc(p, sizeof(access_dir_conf));
79 for (i = 0; i < METHODS; ++i) {
80 conf->order[i] = DENY_THEN_ALLOW;
82 conf->allows = apr_array_make(p, 1, sizeof(allowdeny));
83 conf->denys = apr_array_make(p, 1, sizeof(allowdeny));
88 static const char *order(cmd_parms *cmd, void *dv, const char *arg)
90 access_dir_conf *d = (access_dir_conf *) dv;
93 if (!strcasecmp(arg, "allow,deny"))
95 else if (!strcasecmp(arg, "deny,allow"))
97 else if (!strcasecmp(arg, "mutual-failure"))
100 return "unknown order";
102 for (i = 0; i < METHODS; ++i)
103 if (cmd->limited & (AP_METHOD_BIT << i))
109 static const char *allow_cmd(cmd_parms *cmd, void *dv, const char *from,
112 access_dir_conf *d = (access_dir_conf *) dv;
114 char *where = apr_pstrdup(cmd->pool, where_c);
119 if (strcasecmp(from, "from"))
120 return "allow and deny must be followed by 'from'";
122 a = (allowdeny *) apr_array_push(cmd->info ? d->allows : d->denys);
124 a->limited = cmd->limited;
126 if (!strncasecmp(where, "env=", 4)) {
131 else if (!strcasecmp(where, "all")) {
134 else if ((s = strchr(where, '/'))) {
136 rv = apr_ipsubnet_create(&a->x.ip, where, s, cmd->pool);
137 if(APR_STATUS_IS_EINVAL(rv)) {
138 /* looked nothing like an IP address */
139 return "An IP address was expected";
141 else if (rv != APR_SUCCESS) {
142 apr_strerror(rv, msgbuf, sizeof msgbuf);
143 return apr_pstrdup(cmd->pool, msgbuf);
147 else if (!APR_STATUS_IS_EINVAL(rv = apr_ipsubnet_create(&a->x.ip, where, NULL, cmd->pool))) {
148 if (rv != APR_SUCCESS) {
149 apr_strerror(rv, msgbuf, sizeof msgbuf);
150 return apr_pstrdup(cmd->pool, msgbuf);
154 else { /* no slash, didn't look like an IP address => must be a host */
161 static char its_an_allow;
163 static const command_rec access_cmds[] =
165 AP_INIT_TAKE1("order", order, NULL, OR_LIMIT,
166 "'allow,deny', 'deny,allow', or 'mutual-failure'"),
167 AP_INIT_ITERATE2("allow", allow_cmd, &its_an_allow, OR_LIMIT,
168 "'from' followed by hostnames or IP-address wildcards"),
169 AP_INIT_ITERATE2("deny", allow_cmd, NULL, OR_LIMIT,
170 "'from' followed by hostnames or IP-address wildcards"),
174 static int in_domain(const char *domain, const char *what)
176 int dl = strlen(domain);
177 int wl = strlen(what);
179 if ((wl - dl) >= 0) {
180 if (strcasecmp(domain, &what[wl - dl]) != 0)
183 /* Make sure we matched an *entire* subdomain --- if the user
184 * said 'allow from good.com', we don't want people from nogood.com
185 * to be able to get in.
189 return 1; /* matched whole thing */
191 return (domain[0] == '.' || what[wl - dl - 1] == '.');
197 static int find_allowdeny(request_rec *r, apr_array_header_t *a, int method)
200 allowdeny *ap = (allowdeny *) a->elts;
201 apr_int64_t mmask = (AP_METHOD_BIT << method);
204 const char *remotehost = NULL;
206 for (i = 0; i < a->nelts; ++i) {
207 if (!(mmask & ap[i].limited))
210 switch (ap[i].type) {
212 if (apr_table_get(r->subprocess_env, ap[i].x.from)) {
221 if (apr_ipsubnet_test(ap[i].x.ip, r->connection->remote_addr)) {
228 int remotehost_is_ip;
230 remotehost = ap_get_remote_host(r->connection, r->per_dir_config,
231 REMOTE_DOUBLE_REV, &remotehost_is_ip);
233 if ((remotehost == NULL) || remotehost_is_ip)
239 if ((gothost == 2) && in_domain(ap[i].x.from, remotehost))
252 static int check_dir_access(request_rec *r)
254 int method = r->method_number;
256 access_dir_conf *a = (access_dir_conf *)
257 ap_get_module_config(r->per_dir_config, &access_module);
259 if (a->order[method] == ALLOW_THEN_DENY) {
260 ret = HTTP_FORBIDDEN;
261 if (find_allowdeny(r, a->allows, method))
263 if (find_allowdeny(r, a->denys, method))
264 ret = HTTP_FORBIDDEN;
266 else if (a->order[method] == DENY_THEN_ALLOW) {
267 if (find_allowdeny(r, a->denys, method))
268 ret = HTTP_FORBIDDEN;
269 if (find_allowdeny(r, a->allows, method))
273 if (find_allowdeny(r, a->allows, method)
274 && !find_allowdeny(r, a->denys, method))
277 ret = HTTP_FORBIDDEN;
280 if (ret == HTTP_FORBIDDEN
281 && (ap_satisfies(r) != SATISFY_ANY || !ap_some_auth_required(r))) {
282 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
283 "client denied by server configuration: %s",
290 static void register_hooks(apr_pool_t *p)
292 ap_hook_access_checker(check_dir_access,NULL,NULL,APR_HOOK_MIDDLE);
295 module AP_MODULE_DECLARE_DATA access_module =
297 STANDARD20_MODULE_STUFF,
298 create_access_dir_config, /* dir config creater */
299 NULL, /* dir merger --- default is to override */
300 NULL, /* server config */
301 NULL, /* merge server config */
303 register_hooks /* register hooks */