1238106Sdes/*
2238106Sdes * daemon/acl_list.h - client access control storage for the server.
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file helps the server keep out queries from outside sources, that
40238106Sdes * should not be answered.
41238106Sdes */
42238106Sdes#include "config.h"
43238106Sdes#include "daemon/acl_list.h"
44238106Sdes#include "util/regional.h"
45238106Sdes#include "util/log.h"
46238106Sdes#include "util/config_file.h"
47238106Sdes#include "util/net_help.h"
48238106Sdes
49238106Sdesstruct acl_list*
50238106Sdesacl_list_create(void)
51238106Sdes{
52238106Sdes	struct acl_list* acl = (struct acl_list*)calloc(1,
53238106Sdes		sizeof(struct acl_list));
54238106Sdes	if(!acl)
55238106Sdes		return NULL;
56238106Sdes	acl->region = regional_create();
57238106Sdes	if(!acl->region) {
58238106Sdes		acl_list_delete(acl);
59238106Sdes		return NULL;
60238106Sdes	}
61238106Sdes	return acl;
62238106Sdes}
63238106Sdes
64238106Sdesvoid
65238106Sdesacl_list_delete(struct acl_list* acl)
66238106Sdes{
67238106Sdes	if(!acl)
68238106Sdes		return;
69238106Sdes	regional_destroy(acl->region);
70238106Sdes	free(acl);
71238106Sdes}
72238106Sdes
73238106Sdes/** insert new address into acl_list structure */
74238106Sdesstatic int
75238106Sdesacl_list_insert(struct acl_list* acl, struct sockaddr_storage* addr,
76238106Sdes	socklen_t addrlen, int net, enum acl_access control,
77238106Sdes	int complain_duplicates)
78238106Sdes{
79238106Sdes	struct acl_addr* node = regional_alloc(acl->region,
80238106Sdes		sizeof(struct acl_addr));
81238106Sdes	if(!node)
82238106Sdes		return 0;
83238106Sdes	node->control = control;
84238106Sdes	if(!addr_tree_insert(&acl->tree, &node->node, addr, addrlen, net)) {
85238106Sdes		if(complain_duplicates)
86238106Sdes			verbose(VERB_QUERY, "duplicate acl address ignored.");
87238106Sdes	}
88238106Sdes	return 1;
89238106Sdes}
90238106Sdes
91238106Sdes/** apply acl_list string */
92238106Sdesstatic int
93238106Sdesacl_list_str_cfg(struct acl_list* acl, const char* str, const char* s2,
94238106Sdes	int complain_duplicates)
95238106Sdes{
96238106Sdes	struct sockaddr_storage addr;
97238106Sdes	int net;
98238106Sdes	socklen_t addrlen;
99238106Sdes	enum acl_access control;
100238106Sdes	if(strcmp(s2, "allow") == 0)
101238106Sdes		control = acl_allow;
102238106Sdes	else if(strcmp(s2, "deny") == 0)
103238106Sdes		control = acl_deny;
104238106Sdes	else if(strcmp(s2, "refuse") == 0)
105238106Sdes		control = acl_refuse;
106269257Sdes	else if(strcmp(s2, "deny_non_local") == 0)
107269257Sdes		control = acl_deny_non_local;
108269257Sdes	else if(strcmp(s2, "refuse_non_local") == 0)
109269257Sdes		control = acl_refuse_non_local;
110238106Sdes	else if(strcmp(s2, "allow_snoop") == 0)
111238106Sdes		control = acl_allow_snoop;
112238106Sdes	else {
113238106Sdes		log_err("access control type %s unknown", str);
114238106Sdes		return 0;
115238106Sdes	}
116238106Sdes	if(!netblockstrtoaddr(str, UNBOUND_DNS_PORT, &addr, &addrlen, &net)) {
117238106Sdes		log_err("cannot parse access control: %s %s", str, s2);
118238106Sdes		return 0;
119238106Sdes	}
120238106Sdes	if(!acl_list_insert(acl, &addr, addrlen, net, control,
121238106Sdes		complain_duplicates)) {
122238106Sdes		log_err("out of memory");
123238106Sdes		return 0;
124238106Sdes	}
125238106Sdes	return 1;
126238106Sdes}
127238106Sdes
128238106Sdes/** read acl_list config */
129238106Sdesstatic int
130238106Sdesread_acl_list(struct acl_list* acl, struct config_file* cfg)
131238106Sdes{
132238106Sdes	struct config_str2list* p;
133238106Sdes	for(p = cfg->acls; p; p = p->next) {
134238106Sdes		log_assert(p->str && p->str2);
135238106Sdes		if(!acl_list_str_cfg(acl, p->str, p->str2, 1))
136238106Sdes			return 0;
137238106Sdes	}
138238106Sdes	return 1;
139238106Sdes}
140238106Sdes
141238106Sdesint
142238106Sdesacl_list_apply_cfg(struct acl_list* acl, struct config_file* cfg)
143238106Sdes{
144238106Sdes	regional_free_all(acl->region);
145238106Sdes	addr_tree_init(&acl->tree);
146238106Sdes	if(!read_acl_list(acl, cfg))
147238106Sdes		return 0;
148238106Sdes	/* insert defaults, with '0' to ignore them if they are duplicates */
149238106Sdes	if(!acl_list_str_cfg(acl, "0.0.0.0/0", "refuse", 0))
150238106Sdes		return 0;
151238106Sdes	if(!acl_list_str_cfg(acl, "127.0.0.0/8", "allow", 0))
152238106Sdes		return 0;
153238106Sdes	if(cfg->do_ip6) {
154238106Sdes		if(!acl_list_str_cfg(acl, "::0/0", "refuse", 0))
155238106Sdes			return 0;
156238106Sdes		if(!acl_list_str_cfg(acl, "::1", "allow", 0))
157238106Sdes			return 0;
158238106Sdes		if(!acl_list_str_cfg(acl, "::ffff:127.0.0.1", "allow", 0))
159238106Sdes			return 0;
160238106Sdes	}
161238106Sdes	addr_tree_init_parents(&acl->tree);
162238106Sdes	return 1;
163238106Sdes}
164238106Sdes
165238106Sdesenum acl_access
166238106Sdesacl_list_lookup(struct acl_list* acl, struct sockaddr_storage* addr,
167238106Sdes        socklen_t addrlen)
168238106Sdes{
169238106Sdes	struct acl_addr* r = (struct acl_addr*)addr_tree_lookup(&acl->tree,
170238106Sdes		addr, addrlen);
171238106Sdes	if(r) return r->control;
172238106Sdes	return acl_deny;
173238106Sdes}
174238106Sdes
175238106Sdessize_t
176238106Sdesacl_list_get_mem(struct acl_list* acl)
177238106Sdes{
178238106Sdes	if(!acl) return 0;
179238106Sdes	return sizeof(*acl) + regional_get_mem(acl->region);
180238106Sdes}
181