1 #include <sys/socket.h>
2 #include <netinet/in.h>
3 #include <netdb.h>
4 #include <net/if.h>
5 #include <arpa/inet.h>
6 #include <ctype.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <pthread.h>
12 #include <errno.h>
13 #include <resolv.h>
14 #include "lookup.h"
15 #include "stdio_impl.h"
16 #include "syscall.h"
17 
is_valid_hostname(const char * host)18 static int is_valid_hostname(const char *host)
19 {
20 	const unsigned char *s;
21 	if (strnlen(host, 255)-1 >= 254 || mbstowcs(0, host, 0) == -1) return 0;
22 	for (s=(void *)host; *s>=0x80 || *s=='.' || *s=='-' || isalnum(*s); s++);
23 	return !*s;
24 }
25 
name_from_null(struct address buf[static2],const char * name,int family,int flags)26 static int name_from_null(struct address buf[static 2], const char *name, int family, int flags)
27 {
28 	int cnt = 0;
29 	if (name) return 0;
30 	if (flags & AI_PASSIVE) {
31 		if (family != AF_INET6)
32 			buf[cnt++] = (struct address){ .family = AF_INET };
33 		if (family != AF_INET)
34 			buf[cnt++] = (struct address){ .family = AF_INET6 };
35 	} else {
36 		if (family != AF_INET6)
37 			buf[cnt++] = (struct address){ .family = AF_INET, .addr = { 127,0,0,1 } };
38 		if (family != AF_INET)
39 			buf[cnt++] = (struct address){ .family = AF_INET6, .addr = { [15] = 1 } };
40 	}
41 	return cnt;
42 }
43 
name_from_numeric(struct address buf[static1],const char * name,int family)44 static int name_from_numeric(struct address buf[static 1], const char *name, int family)
45 {
46 	return __lookup_ipliteral(buf, name, family);
47 }
48 
name_from_hosts(struct address buf[static MAXADDRS],char canon[static256],const char * name,int family)49 static int name_from_hosts(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
50 {
51 	char line[512];
52 	size_t l = strlen(name);
53 	int cnt = 0, badfam = 0;
54 	unsigned char _buf[1032];
55 	FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
56 	if (!f) switch (errno) {
57 	case ENOENT:
58 	case ENOTDIR:
59 	case EACCES:
60 		return 0;
61 	default:
62 		return EAI_SYSTEM;
63 	}
64 	while (fgets(line, sizeof line, f) && cnt < MAXADDRS) {
65 		char *p, *z;
66 
67 		if ((p=strchr(line, '#'))) *p++='\n', *p=0;
68 		for(p=line+1; (p=strstr(p, name)) &&
69 			(!isspace(p[-1]) || !isspace(p[l])); p++);
70 		if (!p) continue;
71 
72 		/* Isolate IP address to parse */
73 		for (p=line; *p && !isspace(*p); p++);
74 		*p++ = 0;
75 		switch (name_from_numeric(buf+cnt, line, family)) {
76 		case 1:
77 			cnt++;
78 			break;
79 		case 0:
80 			continue;
81 		default:
82 			badfam = EAI_NONAME;
83 			continue;
84 		}
85 
86 		/* Extract first name as canonical name */
87 		for (; *p && isspace(*p); p++);
88 		for (z=p; *z && !isspace(*z); z++);
89 		*z = 0;
90 		if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
91 	}
92 	__fclose_ca(f);
93 	return cnt ? cnt : badfam;
94 }
95 
96 struct dpc_ctx {
97 	struct address *addrs;
98 	char *canon;
99 	int cnt;
100 };
101 
102 #define RR_A 1
103 #define RR_CNAME 5
104 #define RR_AAAA 28
105 
dns_parse_callback(void * c,int rr,const void * data,int len,const void * packet)106 static int dns_parse_callback(void *c, int rr, const void *data, int len, const void *packet)
107 {
108 	char tmp[256];
109 	struct dpc_ctx *ctx = c;
110 	if (ctx->cnt >= MAXADDRS) return -1;
111 	switch (rr) {
112 	case RR_A:
113 		if (len != 4) return -1;
114 		ctx->addrs[ctx->cnt].family = AF_INET;
115 		ctx->addrs[ctx->cnt].scopeid = 0;
116 		memcpy(ctx->addrs[ctx->cnt++].addr, data, 4);
117 		break;
118 	case RR_AAAA:
119 		if (len != 16) return -1;
120 		ctx->addrs[ctx->cnt].family = AF_INET6;
121 		ctx->addrs[ctx->cnt].scopeid = 0;
122 		memcpy(ctx->addrs[ctx->cnt++].addr, data, 16);
123 		break;
124 	case RR_CNAME:
125 		if (__dn_expand(packet, (const unsigned char *)packet + 512,
126 		    data, tmp, sizeof tmp) > 0 && is_valid_hostname(tmp))
127 			strcpy(ctx->canon, tmp);
128 		break;
129 	}
130 	return 0;
131 }
132 
name_from_dns(struct address buf[static MAXADDRS],char canon[static256],const char * name,int family,const struct resolvconf * conf)133 static int name_from_dns(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, const struct resolvconf *conf)
134 {
135 	unsigned char qbuf[2][280], abuf[2][512];
136 	const unsigned char *qp[2] = { qbuf[0], qbuf[1] };
137 	unsigned char *ap[2] = { abuf[0], abuf[1] };
138 	int qlens[2], alens[2];
139 	int i, nq = 0;
140 	struct dpc_ctx ctx = { .addrs = buf, .canon = canon };
141 	static const struct { int af; int rr; } afrr[2] = {
142 		{ .af = AF_INET6, .rr = RR_A },
143 		{ .af = AF_INET, .rr = RR_AAAA },
144 	};
145 
146 	for (i=0; i<2; i++) {
147 		if (family != afrr[i].af) {
148 			qlens[nq] = __res_mkquery(0, name, 1, afrr[i].rr,
149 				0, 0, 0, qbuf[nq], sizeof *qbuf);
150 			if (qlens[nq] == -1)
151 				return EAI_NONAME;
152 			nq++;
153 		}
154 	}
155 
156 	if (__res_msend_rc(nq, qp, qlens, ap, alens, sizeof *abuf, conf) < 0)
157 		return EAI_SYSTEM;
158 
159 	for (i=0; i<nq; i++)
160 		__dns_parse(abuf[i], alens[i], dns_parse_callback, &ctx);
161 
162 	if (ctx.cnt) return ctx.cnt;
163 	if (alens[0] < 4 || (abuf[0][3] & 15) == 2) return EAI_AGAIN;
164 	if ((abuf[0][3] & 15) == 0) return EAI_NONAME;
165 	if ((abuf[0][3] & 15) == 3) return 0;
166 	return EAI_FAIL;
167 }
168 
name_from_dns_search(struct address buf[static MAXADDRS],char canon[static256],const char * name,int family)169 static int name_from_dns_search(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)
170 {
171 	char search[256];
172 	struct resolvconf conf;
173 	size_t l, dots;
174 	char *p, *z;
175 
176 	if (__get_resolv_conf(&conf, search, sizeof search) < 0) return -1;
177 
178 	/* Count dots, suppress search when >=ndots or name ends in
179 	 * a dot, which is an explicit request for global scope. */
180 	for (dots=l=0; name[l]; l++) if (name[l]=='.') dots++;
181 	if (dots >= conf.ndots || name[l-1]=='.') *search = 0;
182 
183 	/* Strip final dot for canon, fail if multiple trailing dots. */
184 	if (name[l-1]=='.') l--;
185 	if (!l || name[l-1]=='.') return EAI_NONAME;
186 
187 	/* This can never happen; the caller already checked length. */
188 	if (l >= 256) return EAI_NONAME;
189 
190 	/* Name with search domain appended is setup in canon[]. This both
191 	 * provides the desired default canonical name (if the requested
192 	 * name is not a CNAME record) and serves as a buffer for passing
193 	 * the full requested name to name_from_dns. */
194 	memcpy(canon, name, l);
195 	canon[l] = '.';
196 
197 	for (p=search; *p; p=z) {
198 		for (; isspace(*p); p++);
199 		for (z=p; *z && !isspace(*z); z++);
200 		if (z==p) break;
201 		if (z-p < 256 - l - 1) {
202 			memcpy(canon+l+1, p, z-p);
203 			canon[z-p+1+l] = 0;
204 			int cnt = name_from_dns(buf, canon, canon, family, &conf);
205 			if (cnt) return cnt;
206 		}
207 	}
208 
209 	canon[l] = 0;
210 	return name_from_dns(buf, canon, name, family, &conf);
211 }
212 
213 static const struct policy {
214 	unsigned char addr[16];
215 	unsigned char len, mask;
216 	unsigned char prec, label;
217 } defpolicy[] = {
218 	{ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1", 15, 0xff, 50, 0 },
219 	{ "\0\0\0\0\0\0\0\0\0\0\xff\xff", 11, 0xff, 35, 4 },
220 	{ "\x20\2", 1, 0xff, 30, 2 },
221 	{ "\x20\1", 3, 0xff, 5, 5 },
222 	{ "\xfc", 0, 0xfe, 3, 13 },
223 #if 0
224 	/* These are deprecated and/or returned to the address
225 	 * pool, so despite the RFC, treating them as special
226 	 * is probably wrong. */
227 	{ "", 11, 0xff, 1, 3 },
228 	{ "\xfe\xc0", 1, 0xc0, 1, 11 },
229 	{ "\x3f\xfe", 1, 0xff, 1, 12 },
230 #endif
231 	/* Last rule must match all addresses to stop loop. */
232 	{ "", 0, 0, 40, 1 },
233 };
234 
policyof(const struct in6_addr * a)235 static const struct policy *policyof(const struct in6_addr *a)
236 {
237 	int i;
238 	for (i=0; ; i++) {
239 		if (memcmp(a->s6_addr, defpolicy[i].addr, defpolicy[i].len))
240 			continue;
241 		if ((a->s6_addr[defpolicy[i].len] & defpolicy[i].mask)
242 		    != defpolicy[i].addr[defpolicy[i].len])
243 			continue;
244 		return defpolicy+i;
245 	}
246 }
247 
labelof(const struct in6_addr * a)248 static int labelof(const struct in6_addr *a)
249 {
250 	return policyof(a)->label;
251 }
252 
scopeof(const struct in6_addr * a)253 static int scopeof(const struct in6_addr *a)
254 {
255 	if (IN6_IS_ADDR_MULTICAST(a)) return a->s6_addr[1] & 15;
256 	if (IN6_IS_ADDR_LINKLOCAL(a)) return 2;
257 	if (IN6_IS_ADDR_LOOPBACK(a)) return 2;
258 	if (IN6_IS_ADDR_SITELOCAL(a)) return 5;
259 	return 14;
260 }
261 
prefixmatch(const struct in6_addr * s,const struct in6_addr * d)262 static int prefixmatch(const struct in6_addr *s, const struct in6_addr *d)
263 {
264 	/* FIXME: The common prefix length should be limited to no greater
265 	 * than the nominal length of the prefix portion of the source
266 	 * address. However the definition of the source prefix length is
267 	 * not clear and thus this limiting is not yet implemented. */
268 	unsigned i;
269 	for (i=0; i<128 && !((s->s6_addr[i/8]^d->s6_addr[i/8])&(128>>(i%8))); i++);
270 	return i;
271 }
272 
273 #define DAS_USABLE              0x40000000
274 #define DAS_MATCHINGSCOPE       0x20000000
275 #define DAS_MATCHINGLABEL       0x10000000
276 #define DAS_PREC_SHIFT          20
277 #define DAS_SCOPE_SHIFT         16
278 #define DAS_PREFIX_SHIFT        8
279 #define DAS_ORDER_SHIFT         0
280 
addrcmp(const void * _a,const void * _b)281 static int addrcmp(const void *_a, const void *_b)
282 {
283 	const struct address *a = _a, *b = _b;
284 	return b->sortkey - a->sortkey;
285 }
286 
__lookup_name(struct address buf[static MAXADDRS],char canon[static256],const char * name,int family,int flags)287 int __lookup_name(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family, int flags)
288 {
289 	int cnt = 0, i, j;
290 
291 	*canon = 0;
292 	if (name) {
293 		/* reject empty name and check len so it fits into temp bufs */
294 		size_t l = strnlen(name, 255);
295 		if (l-1 >= 254)
296 			return EAI_NONAME;
297 		memcpy(canon, name, l+1);
298 	}
299 
300 	/* Procedurally, a request for v6 addresses with the v4-mapped
301 	 * flag set is like a request for unspecified family, followed
302 	 * by filtering of the results. */
303 	if (flags & AI_V4MAPPED) {
304 		if (family == AF_INET6) family = AF_UNSPEC;
305 		else flags -= AI_V4MAPPED;
306 	}
307 
308 	/* Try each backend until there's at least one result. */
309 	cnt = name_from_null(buf, name, family, flags);
310 	if (!cnt) cnt = name_from_numeric(buf, name, family);
311 	if (!cnt && !(flags & AI_NUMERICHOST)) {
312 		cnt = name_from_hosts(buf, canon, name, family);
313 		if (!cnt) cnt = name_from_dns_search(buf, canon, name, family);
314 	}
315 	if (cnt<=0) return cnt ? cnt : EAI_NONAME;
316 
317 	/* Filter/transform results for v4-mapped lookup, if requested. */
318 	if (flags & AI_V4MAPPED) {
319 		if (!(flags & AI_ALL)) {
320 			/* If any v6 results exist, remove v4 results. */
321 			for (i=0; i<cnt && buf[i].family != AF_INET6; i++);
322 			if (i<cnt) {
323 				for (j=0; i<cnt; i++) {
324 					if (buf[i].family == AF_INET6)
325 						buf[j++] = buf[i];
326 				}
327 				cnt = i = j;
328 			}
329 		}
330 		/* Translate any remaining v4 results to v6 */
331 		for (i=0; i<cnt; i++) {
332 			if (buf[i].family != AF_INET) continue;
333 			memcpy(buf[i].addr+12, buf[i].addr, 4);
334 			memcpy(buf[i].addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
335 			buf[i].family = AF_INET6;
336 		}
337 	}
338 
339 	/* No further processing is needed if there are fewer than 2
340 	 * results or if there are only IPv4 results. */
341 	if (cnt<2 || family==AF_INET) return cnt;
342 	for (i=0; i<cnt; i++) if (buf[i].family != AF_INET) break;
343 	if (i==cnt) return cnt;
344 
345 	int cs;
346 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
347 
348 	/* The following implements a subset of RFC 3484/6724 destination
349 	 * address selection by generating a single 31-bit sort key for
350 	 * each address. Rules 3, 4, and 7 are omitted for having
351 	 * excessive runtime and code size cost and dubious benefit.
352 	 * So far the label/precedence table cannot be customized. */
353 	for (i=0; i<cnt; i++) {
354 		int family = buf[i].family;
355 		int key = 0;
356 		struct sockaddr_in6 sa6 = { 0 }, da6 = {
357 			.sin6_family = AF_INET6,
358 			.sin6_scope_id = buf[i].scopeid,
359 			.sin6_port = 65535
360 		};
361 		struct sockaddr_in sa4 = { 0 }, da4 = {
362 			.sin_family = AF_INET,
363 			.sin_port = 65535
364 		};
365 		void *sa, *da;
366 		socklen_t salen, dalen;
367 		if (family == AF_INET6) {
368 			memcpy(da6.sin6_addr.s6_addr, buf[i].addr, 16);
369 			da = &da6; dalen = sizeof da6;
370 			sa = &sa6; salen = sizeof sa6;
371 		} else {
372 			memcpy(sa6.sin6_addr.s6_addr,
373 				"\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
374 			memcpy(da6.sin6_addr.s6_addr+12, buf[i].addr, 4);
375 			memcpy(da6.sin6_addr.s6_addr,
376 				"\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
377 			memcpy(da6.sin6_addr.s6_addr+12, buf[i].addr, 4);
378 			memcpy(&da4.sin_addr, buf[i].addr, 4);
379 			da = &da4; dalen = sizeof da4;
380 			sa = &sa4; salen = sizeof sa4;
381 		}
382 		const struct policy *dpolicy = policyof(&da6.sin6_addr);
383 		int dscope = scopeof(&da6.sin6_addr);
384 		int dlabel = dpolicy->label;
385 		int dprec = dpolicy->prec;
386 		int prefixlen = 0;
387 		int fd = socket(family, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_UDP);
388 		if (fd >= 0) {
389 			if (!connect(fd, da, dalen)) {
390 				key |= DAS_USABLE;
391 				if (!getsockname(fd, sa, &salen)) {
392 					if (family == AF_INET) memcpy(
393 						sa6.sin6_addr.s6_addr+12,
394 						&sa4.sin_addr, 4);
395 					if (dscope == scopeof(&sa6.sin6_addr))
396 						key |= DAS_MATCHINGSCOPE;
397 					if (dlabel == labelof(&sa6.sin6_addr))
398 						key |= DAS_MATCHINGLABEL;
399 					prefixlen = prefixmatch(&sa6.sin6_addr,
400 						&da6.sin6_addr);
401 				}
402 			}
403 			close(fd);
404 		}
405 		key |= dprec << DAS_PREC_SHIFT;
406 		key |= (15-dscope) << DAS_SCOPE_SHIFT;
407 		key |= prefixlen << DAS_PREFIX_SHIFT;
408 		key |= (MAXADDRS-i) << DAS_ORDER_SHIFT;
409 		buf[i].sortkey = key;
410 	}
411 	qsort(buf, cnt, sizeof *buf, addrcmp);
412 
413 	pthread_setcancelstate(cs, 0);
414 
415 	return cnt;
416 }
417