summaryrefslogtreecommitdiffstats
path: root/lib/ns/sortlist.c
blob: 2aada90b196edb996ad7a712f514afca0d2f3fa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

/*! \file */

#include <isc/mem.h>
#include <isc/result.h>
#include <isc/util.h>

#include <dns/acl.h>
#include <dns/message.h>

#include <ns/server.h>
#include <ns/sortlist.h>

ns_sortlisttype_t
ns_sortlist_setup(dns_acl_t *acl, dns_aclenv_t *env, isc_netaddr_t *clientaddr,
		  void **argp) {
	if (acl == NULL) {
		goto dont_sort;
	}

	for (size_t i = 0; i < acl->length; i++) {
		/*
		 * 'e' refers to the current 'top level statement'
		 * in the sortlist (see ARM).
		 */
		dns_aclelement_t *e = &acl->elements[i];
		dns_aclelement_t *try_elt;
		dns_aclelement_t *order_elt = NULL;
		dns_aclelement_t *matched_elt = NULL;

		if (e->type == dns_aclelementtype_nestedacl) {
			dns_acl_t *inner = e->nestedacl;

			if (inner->length == 0) {
				try_elt = e;
			} else if (inner->length > 2) {
				goto dont_sort;
			} else if (inner->elements[0].negative) {
				goto dont_sort;
			} else {
				try_elt = &inner->elements[0];
				if (inner->length == 2) {
					order_elt = &inner->elements[1];
				}
			}
		} else {
			/*
			 * BIND 8 allows bare elements at the top level
			 * as an undocumented feature.
			 */
			try_elt = e;
		}

		if (!dns_aclelement_match(
			    clientaddr, NULL, try_elt, env,
			    (const dns_aclelement_t **)&matched_elt))
		{
			continue;
		}

		if (order_elt == NULL) {
			INSIST(matched_elt != NULL);
			*argp = matched_elt;
			return (NS_SORTLISTTYPE_1ELEMENT);
		}

		if (order_elt->type == dns_aclelementtype_nestedacl) {
			dns_acl_t *inner = NULL;
			dns_acl_attach(order_elt->nestedacl, &inner);
			*argp = inner;
			return (NS_SORTLISTTYPE_2ELEMENT);
		}

		if (order_elt->type == dns_aclelementtype_localhost) {
			dns_acl_t *inner = NULL;
			RWLOCK(&env->rwlock, isc_rwlocktype_read);
			if (env->localhost != NULL) {
				dns_acl_attach(env->localhost, &inner);
			}
			RWUNLOCK(&env->rwlock, isc_rwlocktype_read);

			if (inner != NULL) {
				*argp = inner;
				return (NS_SORTLISTTYPE_2ELEMENT);
			}
		}

		if (order_elt->type == dns_aclelementtype_localnets) {
			dns_acl_t *inner = NULL;
			RWLOCK(&env->rwlock, isc_rwlocktype_read);
			if (env->localnets != NULL) {
				dns_acl_attach(env->localnets, &inner);
			}
			RWUNLOCK(&env->rwlock, isc_rwlocktype_read);
			if (inner != NULL) {
				*argp = inner;
				return (NS_SORTLISTTYPE_2ELEMENT);
			}
		}

		/*
		 * BIND 8 allows a bare IP prefix as
		 * the 2nd element of a 2-element
		 * sortlist statement.
		 */
		*argp = order_elt;
		return (NS_SORTLISTTYPE_1ELEMENT);
	}

dont_sort:
	*argp = NULL;
	return (NS_SORTLISTTYPE_NONE);
}

int
ns_sortlist_addrorder2(const isc_netaddr_t *addr, const void *arg) {
	const dns_sortlist_arg_t *sla = (const dns_sortlist_arg_t *)arg;
	dns_aclenv_t *env = sla->env;
	const dns_acl_t *sortacl = sla->acl;
	int match;

	(void)dns_acl_match(addr, NULL, sortacl, env, &match, NULL);
	if (match > 0) {
		return (match);
	} else if (match < 0) {
		return (INT_MAX - (-match));
	} else {
		return (INT_MAX / 2);
	}
}

int
ns_sortlist_addrorder1(const isc_netaddr_t *addr, const void *arg) {
	const dns_sortlist_arg_t *sla = (const dns_sortlist_arg_t *)arg;
	dns_aclenv_t *env = sla->env;
	const dns_aclelement_t *element = sla->element;

	if (dns_aclelement_match(addr, NULL, element, env, NULL)) {
		return (0);
	}

	return (INT_MAX);
}

void
ns_sortlist_byaddrsetup(dns_acl_t *sortlist_acl, dns_aclenv_t *env,
			isc_netaddr_t *client_addr,
			dns_addressorderfunc_t *orderp, void **argp) {
	ns_sortlisttype_t sortlisttype;

	sortlisttype = ns_sortlist_setup(sortlist_acl, env, client_addr, argp);

	switch (sortlisttype) {
	case NS_SORTLISTTYPE_1ELEMENT:
		*orderp = ns_sortlist_addrorder1;
		break;
	case NS_SORTLISTTYPE_2ELEMENT:
		*orderp = ns_sortlist_addrorder2;
		break;
	case NS_SORTLISTTYPE_NONE:
		*orderp = NULL;
		break;
	default:
		UNEXPECTED_ERROR(
			"unexpected return from ns_sortlist_setup(): %d",
			sortlisttype);
		break;
	}
}