xref: /freebsd/crypto/openssh/addrmatch.c (revision 462c32cb8d7a451c999a3f1e7d00f9c89e96700c)
1*462c32cbSDag-Erling Smørgrav /*	$OpenBSD: addrmatch.c,v 1.6 2012/06/21 00:16:07 dtucker Exp $ */
2d4af9e69SDag-Erling Smørgrav 
3d4af9e69SDag-Erling Smørgrav /*
4d4af9e69SDag-Erling Smørgrav  * Copyright (c) 2004-2008 Damien Miller <djm@mindrot.org>
5d4af9e69SDag-Erling Smørgrav  *
6d4af9e69SDag-Erling Smørgrav  * Permission to use, copy, modify, and distribute this software for any
7d4af9e69SDag-Erling Smørgrav  * purpose with or without fee is hereby granted, provided that the above
8d4af9e69SDag-Erling Smørgrav  * copyright notice and this permission notice appear in all copies.
9d4af9e69SDag-Erling Smørgrav  *
10d4af9e69SDag-Erling Smørgrav  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11d4af9e69SDag-Erling Smørgrav  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12d4af9e69SDag-Erling Smørgrav  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13d4af9e69SDag-Erling Smørgrav  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14d4af9e69SDag-Erling Smørgrav  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15d4af9e69SDag-Erling Smørgrav  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16d4af9e69SDag-Erling Smørgrav  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17d4af9e69SDag-Erling Smørgrav  */
18d4af9e69SDag-Erling Smørgrav 
19d4af9e69SDag-Erling Smørgrav #include "includes.h"
20d4af9e69SDag-Erling Smørgrav 
21d4af9e69SDag-Erling Smørgrav #include <sys/types.h>
22d4af9e69SDag-Erling Smørgrav #include <sys/socket.h>
23d4af9e69SDag-Erling Smørgrav #include <netinet/in.h>
24d4af9e69SDag-Erling Smørgrav #include <arpa/inet.h>
25d4af9e69SDag-Erling Smørgrav 
26d4af9e69SDag-Erling Smørgrav #include <netdb.h>
27d4af9e69SDag-Erling Smørgrav #include <string.h>
28d4af9e69SDag-Erling Smørgrav #include <stdlib.h>
29d4af9e69SDag-Erling Smørgrav #include <stdio.h>
30d4af9e69SDag-Erling Smørgrav #include <stdarg.h>
31d4af9e69SDag-Erling Smørgrav 
32d4af9e69SDag-Erling Smørgrav #include "match.h"
33d4af9e69SDag-Erling Smørgrav #include "log.h"
34cce7d346SDag-Erling Smørgrav #include "xmalloc.h"
35d4af9e69SDag-Erling Smørgrav 
36d4af9e69SDag-Erling Smørgrav struct xaddr {
37d4af9e69SDag-Erling Smørgrav 	sa_family_t	af;
38d4af9e69SDag-Erling Smørgrav 	union {
39d4af9e69SDag-Erling Smørgrav 		struct in_addr		v4;
40d4af9e69SDag-Erling Smørgrav 		struct in6_addr		v6;
41d4af9e69SDag-Erling Smørgrav 		u_int8_t		addr8[16];
42d4af9e69SDag-Erling Smørgrav 		u_int32_t		addr32[4];
43d4af9e69SDag-Erling Smørgrav 	} xa;		    /* 128-bit address */
44d4af9e69SDag-Erling Smørgrav 	u_int32_t	scope_id;	/* iface scope id for v6 */
45d4af9e69SDag-Erling Smørgrav #define v4	xa.v4
46d4af9e69SDag-Erling Smørgrav #define v6	xa.v6
47d4af9e69SDag-Erling Smørgrav #define addr8	xa.addr8
48d4af9e69SDag-Erling Smørgrav #define addr32	xa.addr32
49d4af9e69SDag-Erling Smørgrav };
50d4af9e69SDag-Erling Smørgrav 
51d4af9e69SDag-Erling Smørgrav static int
52d4af9e69SDag-Erling Smørgrav addr_unicast_masklen(int af)
53d4af9e69SDag-Erling Smørgrav {
54d4af9e69SDag-Erling Smørgrav 	switch (af) {
55d4af9e69SDag-Erling Smørgrav 	case AF_INET:
56d4af9e69SDag-Erling Smørgrav 		return 32;
57d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
58d4af9e69SDag-Erling Smørgrav 		return 128;
59d4af9e69SDag-Erling Smørgrav 	default:
60d4af9e69SDag-Erling Smørgrav 		return -1;
61d4af9e69SDag-Erling Smørgrav 	}
62d4af9e69SDag-Erling Smørgrav }
63d4af9e69SDag-Erling Smørgrav 
64d4af9e69SDag-Erling Smørgrav static inline int
65d4af9e69SDag-Erling Smørgrav masklen_valid(int af, u_int masklen)
66d4af9e69SDag-Erling Smørgrav {
67d4af9e69SDag-Erling Smørgrav 	switch (af) {
68d4af9e69SDag-Erling Smørgrav 	case AF_INET:
69d4af9e69SDag-Erling Smørgrav 		return masklen <= 32 ? 0 : -1;
70d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
71d4af9e69SDag-Erling Smørgrav 		return masklen <= 128 ? 0 : -1;
72d4af9e69SDag-Erling Smørgrav 	default:
73d4af9e69SDag-Erling Smørgrav 		return -1;
74d4af9e69SDag-Erling Smørgrav 	}
75d4af9e69SDag-Erling Smørgrav }
76d4af9e69SDag-Erling Smørgrav 
77d4af9e69SDag-Erling Smørgrav /*
78d4af9e69SDag-Erling Smørgrav  * Convert struct sockaddr to struct xaddr
79d4af9e69SDag-Erling Smørgrav  * Returns 0 on success, -1 on failure.
80d4af9e69SDag-Erling Smørgrav  */
81d4af9e69SDag-Erling Smørgrav static int
82d4af9e69SDag-Erling Smørgrav addr_sa_to_xaddr(struct sockaddr *sa, socklen_t slen, struct xaddr *xa)
83d4af9e69SDag-Erling Smørgrav {
84d4af9e69SDag-Erling Smørgrav 	struct sockaddr_in *in4 = (struct sockaddr_in *)sa;
85d4af9e69SDag-Erling Smørgrav 	struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
86d4af9e69SDag-Erling Smørgrav 
87d4af9e69SDag-Erling Smørgrav 	memset(xa, '\0', sizeof(*xa));
88d4af9e69SDag-Erling Smørgrav 
89d4af9e69SDag-Erling Smørgrav 	switch (sa->sa_family) {
90d4af9e69SDag-Erling Smørgrav 	case AF_INET:
91d4af9e69SDag-Erling Smørgrav 		if (slen < sizeof(*in4))
92d4af9e69SDag-Erling Smørgrav 			return -1;
93d4af9e69SDag-Erling Smørgrav 		xa->af = AF_INET;
94d4af9e69SDag-Erling Smørgrav 		memcpy(&xa->v4, &in4->sin_addr, sizeof(xa->v4));
95d4af9e69SDag-Erling Smørgrav 		break;
96d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
97d4af9e69SDag-Erling Smørgrav 		if (slen < sizeof(*in6))
98d4af9e69SDag-Erling Smørgrav 			return -1;
99d4af9e69SDag-Erling Smørgrav 		xa->af = AF_INET6;
100d4af9e69SDag-Erling Smørgrav 		memcpy(&xa->v6, &in6->sin6_addr, sizeof(xa->v6));
101cce7d346SDag-Erling Smørgrav #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
102d4af9e69SDag-Erling Smørgrav 		xa->scope_id = in6->sin6_scope_id;
103cce7d346SDag-Erling Smørgrav #endif
104d4af9e69SDag-Erling Smørgrav 		break;
105d4af9e69SDag-Erling Smørgrav 	default:
106d4af9e69SDag-Erling Smørgrav 		return -1;
107d4af9e69SDag-Erling Smørgrav 	}
108d4af9e69SDag-Erling Smørgrav 
109d4af9e69SDag-Erling Smørgrav 	return 0;
110d4af9e69SDag-Erling Smørgrav }
111d4af9e69SDag-Erling Smørgrav 
112d4af9e69SDag-Erling Smørgrav /*
113d4af9e69SDag-Erling Smørgrav  * Calculate a netmask of length 'l' for address family 'af' and
114d4af9e69SDag-Erling Smørgrav  * store it in 'n'.
115d4af9e69SDag-Erling Smørgrav  * Returns 0 on success, -1 on failure.
116d4af9e69SDag-Erling Smørgrav  */
117d4af9e69SDag-Erling Smørgrav static int
118d4af9e69SDag-Erling Smørgrav addr_netmask(int af, u_int l, struct xaddr *n)
119d4af9e69SDag-Erling Smørgrav {
120d4af9e69SDag-Erling Smørgrav 	int i;
121d4af9e69SDag-Erling Smørgrav 
122d4af9e69SDag-Erling Smørgrav 	if (masklen_valid(af, l) != 0 || n == NULL)
123d4af9e69SDag-Erling Smørgrav 		return -1;
124d4af9e69SDag-Erling Smørgrav 
125d4af9e69SDag-Erling Smørgrav 	memset(n, '\0', sizeof(*n));
126d4af9e69SDag-Erling Smørgrav 	switch (af) {
127d4af9e69SDag-Erling Smørgrav 	case AF_INET:
128d4af9e69SDag-Erling Smørgrav 		n->af = AF_INET;
129b15c8340SDag-Erling Smørgrav 		if (l == 0)
130b15c8340SDag-Erling Smørgrav 			return 0;
131d4af9e69SDag-Erling Smørgrav 		n->v4.s_addr = htonl((0xffffffff << (32 - l)) & 0xffffffff);
132d4af9e69SDag-Erling Smørgrav 		return 0;
133d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
134d4af9e69SDag-Erling Smørgrav 		n->af = AF_INET6;
135d4af9e69SDag-Erling Smørgrav 		for (i = 0; i < 4 && l >= 32; i++, l -= 32)
136d4af9e69SDag-Erling Smørgrav 			n->addr32[i] = 0xffffffffU;
137d4af9e69SDag-Erling Smørgrav 		if (i < 4 && l != 0)
138d4af9e69SDag-Erling Smørgrav 			n->addr32[i] = htonl((0xffffffff << (32 - l)) &
139d4af9e69SDag-Erling Smørgrav 			    0xffffffff);
140d4af9e69SDag-Erling Smørgrav 		return 0;
141d4af9e69SDag-Erling Smørgrav 	default:
142d4af9e69SDag-Erling Smørgrav 		return -1;
143d4af9e69SDag-Erling Smørgrav 	}
144d4af9e69SDag-Erling Smørgrav }
145d4af9e69SDag-Erling Smørgrav 
146d4af9e69SDag-Erling Smørgrav /*
147d4af9e69SDag-Erling Smørgrav  * Perform logical AND of addresses 'a' and 'b', storing result in 'dst'.
148d4af9e69SDag-Erling Smørgrav  * Returns 0 on success, -1 on failure.
149d4af9e69SDag-Erling Smørgrav  */
150d4af9e69SDag-Erling Smørgrav static int
151d4af9e69SDag-Erling Smørgrav addr_and(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b)
152d4af9e69SDag-Erling Smørgrav {
153d4af9e69SDag-Erling Smørgrav 	int i;
154d4af9e69SDag-Erling Smørgrav 
155d4af9e69SDag-Erling Smørgrav 	if (dst == NULL || a == NULL || b == NULL || a->af != b->af)
156d4af9e69SDag-Erling Smørgrav 		return -1;
157d4af9e69SDag-Erling Smørgrav 
158d4af9e69SDag-Erling Smørgrav 	memcpy(dst, a, sizeof(*dst));
159d4af9e69SDag-Erling Smørgrav 	switch (a->af) {
160d4af9e69SDag-Erling Smørgrav 	case AF_INET:
161d4af9e69SDag-Erling Smørgrav 		dst->v4.s_addr &= b->v4.s_addr;
162d4af9e69SDag-Erling Smørgrav 		return 0;
163d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
164d4af9e69SDag-Erling Smørgrav 		dst->scope_id = a->scope_id;
165d4af9e69SDag-Erling Smørgrav 		for (i = 0; i < 4; i++)
166d4af9e69SDag-Erling Smørgrav 			dst->addr32[i] &= b->addr32[i];
167d4af9e69SDag-Erling Smørgrav 		return 0;
168d4af9e69SDag-Erling Smørgrav 	default:
169d4af9e69SDag-Erling Smørgrav 		return -1;
170d4af9e69SDag-Erling Smørgrav 	}
171d4af9e69SDag-Erling Smørgrav }
172d4af9e69SDag-Erling Smørgrav 
173d4af9e69SDag-Erling Smørgrav /*
174d4af9e69SDag-Erling Smørgrav  * Compare addresses 'a' and 'b'
175d4af9e69SDag-Erling Smørgrav  * Return 0 if addresses are identical, -1 if (a < b) or 1 if (a > b)
176d4af9e69SDag-Erling Smørgrav  */
177d4af9e69SDag-Erling Smørgrav static int
178d4af9e69SDag-Erling Smørgrav addr_cmp(const struct xaddr *a, const struct xaddr *b)
179d4af9e69SDag-Erling Smørgrav {
180d4af9e69SDag-Erling Smørgrav 	int i;
181d4af9e69SDag-Erling Smørgrav 
182d4af9e69SDag-Erling Smørgrav 	if (a->af != b->af)
183d4af9e69SDag-Erling Smørgrav 		return a->af == AF_INET6 ? 1 : -1;
184d4af9e69SDag-Erling Smørgrav 
185d4af9e69SDag-Erling Smørgrav 	switch (a->af) {
186d4af9e69SDag-Erling Smørgrav 	case AF_INET:
187d4af9e69SDag-Erling Smørgrav 		if (a->v4.s_addr == b->v4.s_addr)
188d4af9e69SDag-Erling Smørgrav 			return 0;
189d4af9e69SDag-Erling Smørgrav 		return ntohl(a->v4.s_addr) > ntohl(b->v4.s_addr) ? 1 : -1;
190d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
191d4af9e69SDag-Erling Smørgrav 		for (i = 0; i < 16; i++)
192d4af9e69SDag-Erling Smørgrav 			if (a->addr8[i] - b->addr8[i] != 0)
193d4af9e69SDag-Erling Smørgrav 				return a->addr8[i] > b->addr8[i] ? 1 : -1;
194d4af9e69SDag-Erling Smørgrav 		if (a->scope_id == b->scope_id)
195d4af9e69SDag-Erling Smørgrav 			return 0;
196d4af9e69SDag-Erling Smørgrav 		return a->scope_id > b->scope_id ? 1 : -1;
197d4af9e69SDag-Erling Smørgrav 	default:
198d4af9e69SDag-Erling Smørgrav 		return -1;
199d4af9e69SDag-Erling Smørgrav 	}
200d4af9e69SDag-Erling Smørgrav }
201d4af9e69SDag-Erling Smørgrav 
202d4af9e69SDag-Erling Smørgrav /*
203d4af9e69SDag-Erling Smørgrav  * Parse string address 'p' into 'n'
204d4af9e69SDag-Erling Smørgrav  * Returns 0 on success, -1 on failure.
205d4af9e69SDag-Erling Smørgrav  */
206d4af9e69SDag-Erling Smørgrav static int
207d4af9e69SDag-Erling Smørgrav addr_pton(const char *p, struct xaddr *n)
208d4af9e69SDag-Erling Smørgrav {
209d4af9e69SDag-Erling Smørgrav 	struct addrinfo hints, *ai;
210d4af9e69SDag-Erling Smørgrav 
211d4af9e69SDag-Erling Smørgrav 	memset(&hints, '\0', sizeof(hints));
212d4af9e69SDag-Erling Smørgrav 	hints.ai_flags = AI_NUMERICHOST;
213d4af9e69SDag-Erling Smørgrav 
214d4af9e69SDag-Erling Smørgrav 	if (p == NULL || getaddrinfo(p, NULL, &hints, &ai) != 0)
215d4af9e69SDag-Erling Smørgrav 		return -1;
216d4af9e69SDag-Erling Smørgrav 
217d4af9e69SDag-Erling Smørgrav 	if (ai == NULL || ai->ai_addr == NULL)
218d4af9e69SDag-Erling Smørgrav 		return -1;
219d4af9e69SDag-Erling Smørgrav 
220d4af9e69SDag-Erling Smørgrav 	if (n != NULL &&
221d4af9e69SDag-Erling Smørgrav 	    addr_sa_to_xaddr(ai->ai_addr, ai->ai_addrlen, n) == -1) {
222d4af9e69SDag-Erling Smørgrav 		freeaddrinfo(ai);
223d4af9e69SDag-Erling Smørgrav 		return -1;
224d4af9e69SDag-Erling Smørgrav 	}
225d4af9e69SDag-Erling Smørgrav 
226d4af9e69SDag-Erling Smørgrav 	freeaddrinfo(ai);
227d4af9e69SDag-Erling Smørgrav 	return 0;
228d4af9e69SDag-Erling Smørgrav }
229d4af9e69SDag-Erling Smørgrav 
230d4af9e69SDag-Erling Smørgrav /*
231d4af9e69SDag-Erling Smørgrav  * Perform bitwise negation of address
232d4af9e69SDag-Erling Smørgrav  * Returns 0 on success, -1 on failure.
233d4af9e69SDag-Erling Smørgrav  */
234d4af9e69SDag-Erling Smørgrav static int
235d4af9e69SDag-Erling Smørgrav addr_invert(struct xaddr *n)
236d4af9e69SDag-Erling Smørgrav {
237d4af9e69SDag-Erling Smørgrav 	int i;
238d4af9e69SDag-Erling Smørgrav 
239d4af9e69SDag-Erling Smørgrav 	if (n == NULL)
240d4af9e69SDag-Erling Smørgrav 		return (-1);
241d4af9e69SDag-Erling Smørgrav 
242d4af9e69SDag-Erling Smørgrav 	switch (n->af) {
243d4af9e69SDag-Erling Smørgrav 	case AF_INET:
244d4af9e69SDag-Erling Smørgrav 		n->v4.s_addr = ~n->v4.s_addr;
245d4af9e69SDag-Erling Smørgrav 		return (0);
246d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
247d4af9e69SDag-Erling Smørgrav 		for (i = 0; i < 4; i++)
248d4af9e69SDag-Erling Smørgrav 			n->addr32[i] = ~n->addr32[i];
249d4af9e69SDag-Erling Smørgrav 		return (0);
250d4af9e69SDag-Erling Smørgrav 	default:
251d4af9e69SDag-Erling Smørgrav 		return (-1);
252d4af9e69SDag-Erling Smørgrav 	}
253d4af9e69SDag-Erling Smørgrav }
254d4af9e69SDag-Erling Smørgrav 
255d4af9e69SDag-Erling Smørgrav /*
256d4af9e69SDag-Erling Smørgrav  * Calculate a netmask of length 'l' for address family 'af' and
257d4af9e69SDag-Erling Smørgrav  * store it in 'n'.
258d4af9e69SDag-Erling Smørgrav  * Returns 0 on success, -1 on failure.
259d4af9e69SDag-Erling Smørgrav  */
260d4af9e69SDag-Erling Smørgrav static int
261d4af9e69SDag-Erling Smørgrav addr_hostmask(int af, u_int l, struct xaddr *n)
262d4af9e69SDag-Erling Smørgrav {
263d4af9e69SDag-Erling Smørgrav 	if (addr_netmask(af, l, n) == -1 || addr_invert(n) == -1)
264d4af9e69SDag-Erling Smørgrav 		return (-1);
265d4af9e69SDag-Erling Smørgrav 	return (0);
266d4af9e69SDag-Erling Smørgrav }
267d4af9e69SDag-Erling Smørgrav 
268d4af9e69SDag-Erling Smørgrav /*
269d4af9e69SDag-Erling Smørgrav  * Test whether address 'a' is all zeros (i.e. 0.0.0.0 or ::)
270d4af9e69SDag-Erling Smørgrav  * Returns 0 on if address is all-zeros, -1 if not all zeros or on failure.
271d4af9e69SDag-Erling Smørgrav  */
272d4af9e69SDag-Erling Smørgrav static int
273d4af9e69SDag-Erling Smørgrav addr_is_all0s(const struct xaddr *a)
274d4af9e69SDag-Erling Smørgrav {
275d4af9e69SDag-Erling Smørgrav 	int i;
276d4af9e69SDag-Erling Smørgrav 
277d4af9e69SDag-Erling Smørgrav 	switch (a->af) {
278d4af9e69SDag-Erling Smørgrav 	case AF_INET:
279d4af9e69SDag-Erling Smørgrav 		return (a->v4.s_addr == 0 ? 0 : -1);
280d4af9e69SDag-Erling Smørgrav 	case AF_INET6:;
281d4af9e69SDag-Erling Smørgrav 		for (i = 0; i < 4; i++)
282d4af9e69SDag-Erling Smørgrav 			if (a->addr32[i] != 0)
283d4af9e69SDag-Erling Smørgrav 				return (-1);
284d4af9e69SDag-Erling Smørgrav 		return (0);
285d4af9e69SDag-Erling Smørgrav 	default:
286d4af9e69SDag-Erling Smørgrav 		return (-1);
287d4af9e69SDag-Erling Smørgrav 	}
288d4af9e69SDag-Erling Smørgrav }
289d4af9e69SDag-Erling Smørgrav 
290d4af9e69SDag-Erling Smørgrav /*
291d4af9e69SDag-Erling Smørgrav  * Test whether host portion of address 'a', as determined by 'masklen'
292d4af9e69SDag-Erling Smørgrav  * is all zeros.
293d4af9e69SDag-Erling Smørgrav  * Returns 0 on if host portion of address is all-zeros,
294d4af9e69SDag-Erling Smørgrav  * -1 if not all zeros or on failure.
295d4af9e69SDag-Erling Smørgrav  */
296d4af9e69SDag-Erling Smørgrav static int
297d4af9e69SDag-Erling Smørgrav addr_host_is_all0s(const struct xaddr *a, u_int masklen)
298d4af9e69SDag-Erling Smørgrav {
299d4af9e69SDag-Erling Smørgrav 	struct xaddr tmp_addr, tmp_mask, tmp_result;
300d4af9e69SDag-Erling Smørgrav 
301d4af9e69SDag-Erling Smørgrav 	memcpy(&tmp_addr, a, sizeof(tmp_addr));
302d4af9e69SDag-Erling Smørgrav 	if (addr_hostmask(a->af, masklen, &tmp_mask) == -1)
303d4af9e69SDag-Erling Smørgrav 		return (-1);
304d4af9e69SDag-Erling Smørgrav 	if (addr_and(&tmp_result, &tmp_addr, &tmp_mask) == -1)
305d4af9e69SDag-Erling Smørgrav 		return (-1);
306d4af9e69SDag-Erling Smørgrav 	return (addr_is_all0s(&tmp_result));
307d4af9e69SDag-Erling Smørgrav }
308d4af9e69SDag-Erling Smørgrav 
309d4af9e69SDag-Erling Smørgrav /*
310d4af9e69SDag-Erling Smørgrav  * Parse a CIDR address (x.x.x.x/y or xxxx:yyyy::/z).
311d4af9e69SDag-Erling Smørgrav  * Return -1 on parse error, -2 on inconsistency or 0 on success.
312d4af9e69SDag-Erling Smørgrav  */
313d4af9e69SDag-Erling Smørgrav static int
314d4af9e69SDag-Erling Smørgrav addr_pton_cidr(const char *p, struct xaddr *n, u_int *l)
315d4af9e69SDag-Erling Smørgrav {
316d4af9e69SDag-Erling Smørgrav 	struct xaddr tmp;
317d4af9e69SDag-Erling Smørgrav 	long unsigned int masklen = 999;
318d4af9e69SDag-Erling Smørgrav 	char addrbuf[64], *mp, *cp;
319d4af9e69SDag-Erling Smørgrav 
320d4af9e69SDag-Erling Smørgrav 	/* Don't modify argument */
321*462c32cbSDag-Erling Smørgrav 	if (p == NULL || strlcpy(addrbuf, p, sizeof(addrbuf)) >= sizeof(addrbuf))
322d4af9e69SDag-Erling Smørgrav 		return -1;
323d4af9e69SDag-Erling Smørgrav 
324d4af9e69SDag-Erling Smørgrav 	if ((mp = strchr(addrbuf, '/')) != NULL) {
325d4af9e69SDag-Erling Smørgrav 		*mp = '\0';
326d4af9e69SDag-Erling Smørgrav 		mp++;
327d4af9e69SDag-Erling Smørgrav 		masklen = strtoul(mp, &cp, 10);
328d4af9e69SDag-Erling Smørgrav 		if (*mp == '\0' || *cp != '\0' || masklen > 128)
329d4af9e69SDag-Erling Smørgrav 			return -1;
330d4af9e69SDag-Erling Smørgrav 	}
331d4af9e69SDag-Erling Smørgrav 
332d4af9e69SDag-Erling Smørgrav 	if (addr_pton(addrbuf, &tmp) == -1)
333d4af9e69SDag-Erling Smørgrav 		return -1;
334d4af9e69SDag-Erling Smørgrav 
335d4af9e69SDag-Erling Smørgrav 	if (mp == NULL)
336d4af9e69SDag-Erling Smørgrav 		masklen = addr_unicast_masklen(tmp.af);
337d4af9e69SDag-Erling Smørgrav 	if (masklen_valid(tmp.af, masklen) == -1)
338d4af9e69SDag-Erling Smørgrav 		return -2;
339d4af9e69SDag-Erling Smørgrav 	if (addr_host_is_all0s(&tmp, masklen) != 0)
340d4af9e69SDag-Erling Smørgrav 		return -2;
341d4af9e69SDag-Erling Smørgrav 
342d4af9e69SDag-Erling Smørgrav 	if (n != NULL)
343d4af9e69SDag-Erling Smørgrav 		memcpy(n, &tmp, sizeof(*n));
344d4af9e69SDag-Erling Smørgrav 	if (l != NULL)
345d4af9e69SDag-Erling Smørgrav 		*l = masklen;
346d4af9e69SDag-Erling Smørgrav 
347d4af9e69SDag-Erling Smørgrav 	return 0;
348d4af9e69SDag-Erling Smørgrav }
349d4af9e69SDag-Erling Smørgrav 
350d4af9e69SDag-Erling Smørgrav static int
351d4af9e69SDag-Erling Smørgrav addr_netmatch(const struct xaddr *host, const struct xaddr *net, u_int masklen)
352d4af9e69SDag-Erling Smørgrav {
353d4af9e69SDag-Erling Smørgrav 	struct xaddr tmp_mask, tmp_result;
354d4af9e69SDag-Erling Smørgrav 
355d4af9e69SDag-Erling Smørgrav 	if (host->af != net->af)
356d4af9e69SDag-Erling Smørgrav 		return -1;
357d4af9e69SDag-Erling Smørgrav 
358d4af9e69SDag-Erling Smørgrav 	if (addr_netmask(host->af, masklen, &tmp_mask) == -1)
359d4af9e69SDag-Erling Smørgrav 		return -1;
360d4af9e69SDag-Erling Smørgrav 	if (addr_and(&tmp_result, host, &tmp_mask) == -1)
361d4af9e69SDag-Erling Smørgrav 		return -1;
362d4af9e69SDag-Erling Smørgrav 	return addr_cmp(&tmp_result, net);
363d4af9e69SDag-Erling Smørgrav }
364d4af9e69SDag-Erling Smørgrav 
365d4af9e69SDag-Erling Smørgrav /*
366d4af9e69SDag-Erling Smørgrav  * Match "addr" against list pattern list "_list", which may contain a
367d4af9e69SDag-Erling Smørgrav  * mix of CIDR addresses and old-school wildcards.
368d4af9e69SDag-Erling Smørgrav  *
369d4af9e69SDag-Erling Smørgrav  * If addr is NULL, then no matching is performed, but _list is parsed
370d4af9e69SDag-Erling Smørgrav  * and checked for well-formedness.
371d4af9e69SDag-Erling Smørgrav  *
372d4af9e69SDag-Erling Smørgrav  * Returns 1 on match found (never returned when addr == NULL).
373d4af9e69SDag-Erling Smørgrav  * Returns 0 on if no match found, or no errors found when addr == NULL.
374d4af9e69SDag-Erling Smørgrav  * Returns -1 on negated match found (never returned when addr == NULL).
375d4af9e69SDag-Erling Smørgrav  * Returns -2 on invalid list entry.
376d4af9e69SDag-Erling Smørgrav  */
377d4af9e69SDag-Erling Smørgrav int
378d4af9e69SDag-Erling Smørgrav addr_match_list(const char *addr, const char *_list)
379d4af9e69SDag-Erling Smørgrav {
380d4af9e69SDag-Erling Smørgrav 	char *list, *cp, *o;
381d4af9e69SDag-Erling Smørgrav 	struct xaddr try_addr, match_addr;
382d4af9e69SDag-Erling Smørgrav 	u_int masklen, neg;
383d4af9e69SDag-Erling Smørgrav 	int ret = 0, r;
384d4af9e69SDag-Erling Smørgrav 
385d4af9e69SDag-Erling Smørgrav 	if (addr != NULL && addr_pton(addr, &try_addr) != 0) {
386d4af9e69SDag-Erling Smørgrav 		debug2("%s: couldn't parse address %.100s", __func__, addr);
387d4af9e69SDag-Erling Smørgrav 		return 0;
388d4af9e69SDag-Erling Smørgrav 	}
389d4af9e69SDag-Erling Smørgrav 	if ((o = list = strdup(_list)) == NULL)
390d4af9e69SDag-Erling Smørgrav 		return -1;
391d4af9e69SDag-Erling Smørgrav 	while ((cp = strsep(&list, ",")) != NULL) {
392d4af9e69SDag-Erling Smørgrav 		neg = *cp == '!';
393d4af9e69SDag-Erling Smørgrav 		if (neg)
394d4af9e69SDag-Erling Smørgrav 			cp++;
395d4af9e69SDag-Erling Smørgrav 		if (*cp == '\0') {
396d4af9e69SDag-Erling Smørgrav 			ret = -2;
397d4af9e69SDag-Erling Smørgrav 			break;
398d4af9e69SDag-Erling Smørgrav 		}
399d4af9e69SDag-Erling Smørgrav 		/* Prefer CIDR address matching */
400d4af9e69SDag-Erling Smørgrav 		r = addr_pton_cidr(cp, &match_addr, &masklen);
401d4af9e69SDag-Erling Smørgrav 		if (r == -2) {
402d4af9e69SDag-Erling Smørgrav 			error("Inconsistent mask length for "
403d4af9e69SDag-Erling Smørgrav 			    "network \"%.100s\"", cp);
404d4af9e69SDag-Erling Smørgrav 			ret = -2;
405d4af9e69SDag-Erling Smørgrav 			break;
406d4af9e69SDag-Erling Smørgrav 		} else if (r == 0) {
407d4af9e69SDag-Erling Smørgrav 			if (addr != NULL && addr_netmatch(&try_addr,
408d4af9e69SDag-Erling Smørgrav                            &match_addr, masklen) == 0) {
409d4af9e69SDag-Erling Smørgrav  foundit:
410d4af9e69SDag-Erling Smørgrav 				if (neg) {
411d4af9e69SDag-Erling Smørgrav 					ret = -1;
412d4af9e69SDag-Erling Smørgrav 					break;
413d4af9e69SDag-Erling Smørgrav 				}
414d4af9e69SDag-Erling Smørgrav 				ret = 1;
415d4af9e69SDag-Erling Smørgrav 			}
416d4af9e69SDag-Erling Smørgrav 			continue;
417d4af9e69SDag-Erling Smørgrav 		} else {
418d4af9e69SDag-Erling Smørgrav 			/* If CIDR parse failed, try wildcard string match */
419d4af9e69SDag-Erling Smørgrav 			if (addr != NULL && match_pattern(addr, cp) == 1)
420d4af9e69SDag-Erling Smørgrav 				goto foundit;
421d4af9e69SDag-Erling Smørgrav 		}
422d4af9e69SDag-Erling Smørgrav 	}
423cce7d346SDag-Erling Smørgrav 	xfree(o);
424d4af9e69SDag-Erling Smørgrav 
425d4af9e69SDag-Erling Smørgrav 	return ret;
426d4af9e69SDag-Erling Smørgrav }
427b15c8340SDag-Erling Smørgrav 
428b15c8340SDag-Erling Smørgrav /*
429b15c8340SDag-Erling Smørgrav  * Match "addr" against list CIDR list "_list". Lexical wildcards and
430b15c8340SDag-Erling Smørgrav  * negation are not supported. If "addr" == NULL, will verify structure
431b15c8340SDag-Erling Smørgrav  * of "_list".
432b15c8340SDag-Erling Smørgrav  *
433b15c8340SDag-Erling Smørgrav  * Returns 1 on match found (never returned when addr == NULL).
434b15c8340SDag-Erling Smørgrav  * Returns 0 on if no match found, or no errors found when addr == NULL.
435b15c8340SDag-Erling Smørgrav  * Returns -1 on error
436b15c8340SDag-Erling Smørgrav  */
437b15c8340SDag-Erling Smørgrav int
438b15c8340SDag-Erling Smørgrav addr_match_cidr_list(const char *addr, const char *_list)
439b15c8340SDag-Erling Smørgrav {
440b15c8340SDag-Erling Smørgrav 	char *list, *cp, *o;
441b15c8340SDag-Erling Smørgrav 	struct xaddr try_addr, match_addr;
442b15c8340SDag-Erling Smørgrav 	u_int masklen;
443b15c8340SDag-Erling Smørgrav 	int ret = 0, r;
444b15c8340SDag-Erling Smørgrav 
445b15c8340SDag-Erling Smørgrav 	if (addr != NULL && addr_pton(addr, &try_addr) != 0) {
446b15c8340SDag-Erling Smørgrav 		debug2("%s: couldn't parse address %.100s", __func__, addr);
447b15c8340SDag-Erling Smørgrav 		return 0;
448b15c8340SDag-Erling Smørgrav 	}
449b15c8340SDag-Erling Smørgrav 	if ((o = list = strdup(_list)) == NULL)
450b15c8340SDag-Erling Smørgrav 		return -1;
451b15c8340SDag-Erling Smørgrav 	while ((cp = strsep(&list, ",")) != NULL) {
452b15c8340SDag-Erling Smørgrav 		if (*cp == '\0') {
453b15c8340SDag-Erling Smørgrav 			error("%s: empty entry in list \"%.100s\"",
454b15c8340SDag-Erling Smørgrav 			    __func__, o);
455b15c8340SDag-Erling Smørgrav 			ret = -1;
456b15c8340SDag-Erling Smørgrav 			break;
457b15c8340SDag-Erling Smørgrav 		}
458b15c8340SDag-Erling Smørgrav 
459b15c8340SDag-Erling Smørgrav 		/*
460b15c8340SDag-Erling Smørgrav 		 * NB. This function is called in pre-auth with untrusted data,
461b15c8340SDag-Erling Smørgrav 		 * so be extra paranoid about junk reaching getaddrino (via
462b15c8340SDag-Erling Smørgrav 		 * addr_pton_cidr).
463b15c8340SDag-Erling Smørgrav 		 */
464b15c8340SDag-Erling Smørgrav 
465b15c8340SDag-Erling Smørgrav 		/* Stop junk from reaching getaddrinfo. +3 is for masklen */
466b15c8340SDag-Erling Smørgrav 		if (strlen(cp) > INET6_ADDRSTRLEN + 3) {
467b15c8340SDag-Erling Smørgrav 			error("%s: list entry \"%.100s\" too long",
468b15c8340SDag-Erling Smørgrav 			    __func__, cp);
469b15c8340SDag-Erling Smørgrav 			ret = -1;
470b15c8340SDag-Erling Smørgrav 			break;
471b15c8340SDag-Erling Smørgrav 		}
472b15c8340SDag-Erling Smørgrav #define VALID_CIDR_CHARS "0123456789abcdefABCDEF.:/"
473b15c8340SDag-Erling Smørgrav 		if (strspn(cp, VALID_CIDR_CHARS) != strlen(cp)) {
474b15c8340SDag-Erling Smørgrav 			error("%s: list entry \"%.100s\" contains invalid "
475b15c8340SDag-Erling Smørgrav 			    "characters", __func__, cp);
476b15c8340SDag-Erling Smørgrav 			ret = -1;
477b15c8340SDag-Erling Smørgrav 		}
478b15c8340SDag-Erling Smørgrav 
479b15c8340SDag-Erling Smørgrav 		/* Prefer CIDR address matching */
480b15c8340SDag-Erling Smørgrav 		r = addr_pton_cidr(cp, &match_addr, &masklen);
481b15c8340SDag-Erling Smørgrav 		if (r == -1) {
482b15c8340SDag-Erling Smørgrav 			error("Invalid network entry \"%.100s\"", cp);
483b15c8340SDag-Erling Smørgrav 			ret = -1;
484b15c8340SDag-Erling Smørgrav 			break;
485b15c8340SDag-Erling Smørgrav 		} else if (r == -2) {
486b15c8340SDag-Erling Smørgrav 			error("Inconsistent mask length for "
487b15c8340SDag-Erling Smørgrav 			    "network \"%.100s\"", cp);
488b15c8340SDag-Erling Smørgrav 			ret = -1;
489b15c8340SDag-Erling Smørgrav 			break;
490b15c8340SDag-Erling Smørgrav 		} else if (r == 0 && addr != NULL) {
491b15c8340SDag-Erling Smørgrav 			if (addr_netmatch(&try_addr, &match_addr,
492b15c8340SDag-Erling Smørgrav 			    masklen) == 0)
493b15c8340SDag-Erling Smørgrav 				ret = 1;
494b15c8340SDag-Erling Smørgrav 			continue;
495b15c8340SDag-Erling Smørgrav 		}
496b15c8340SDag-Erling Smørgrav 	}
497b15c8340SDag-Erling Smørgrav 	xfree(o);
498b15c8340SDag-Erling Smørgrav 
499b15c8340SDag-Erling Smørgrav 	return ret;
500b15c8340SDag-Erling Smørgrav }
501