xref: /openbsd-src/lib/libc/asr/gethostnamadr_async.c (revision d1df930ffab53da22f3324c32bed7ac5709915e6)
1 /*	$OpenBSD: gethostnamadr_async.c,v 1.44 2018/04/28 15:16:49 schwarze Exp $	*/
2 /*
3  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <arpa/nameser.h>
23 #include <netdb.h>
24 
25 #include <asr.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <resolv.h> /* for res_hnok */
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <limits.h>
33 
34 #include "asr_private.h"
35 
36 #define MAXALIASES	35
37 #define MAXADDRS	35
38 
39 struct hostent_ext {
40 	struct hostent	 h;
41 	char		*aliases[MAXALIASES + 1];
42 	char		*addrs[MAXADDRS + 1];
43 	char		*end;
44 	char		*pos;
45 };
46 
47 struct netent_ext {
48 	struct netent	 n;
49 	char		*aliases[MAXALIASES + 1];
50 	char		*end;
51 	char		*pos;
52 };
53 
54 static int gethostnamadr_async_run(struct asr_query *, struct asr_result *);
55 static struct hostent_ext *hostent_alloc(int);
56 static int hostent_set_cname(struct hostent_ext *, const char *, int);
57 static int hostent_add_alias(struct hostent_ext *, const char *, int);
58 static int hostent_add_addr(struct hostent_ext *, const void *, size_t);
59 static struct hostent_ext *hostent_from_addr(int, const char *, const char *);
60 static struct hostent_ext *hostent_file_match(FILE *, int, int, const char *,
61     int);
62 static struct hostent_ext *hostent_from_packet(int, int, char *, size_t);
63 static void netent_from_hostent(struct asr_result *ar);
64 
65 struct asr_query *
66 gethostbyname_async(const char *name, void *asr)
67 {
68 	return gethostbyname2_async(name, AF_INET, asr);
69 }
70 DEF_WEAK(gethostbyname_async);
71 
72 struct asr_query *
73 gethostbyname2_async(const char *name, int af, void *asr)
74 {
75 	struct asr_ctx	 *ac;
76 	struct asr_query *as;
77 
78 	/* the original segfaults */
79 	if (name == NULL) {
80 		errno = EINVAL;
81 		return (NULL);
82 	}
83 
84 	ac = _asr_use_resolver(asr);
85 	if ((as = _asr_async_new(ac, ASR_GETHOSTBYNAME)) == NULL)
86 		goto abort; /* errno set */
87 	as->as_run = gethostnamadr_async_run;
88 
89 	as->as.hostnamadr.family = af;
90 	if (af == AF_INET)
91 		as->as.hostnamadr.addrlen = INADDRSZ;
92 	else if (af == AF_INET6)
93 		as->as.hostnamadr.addrlen = IN6ADDRSZ;
94 	as->as.hostnamadr.name = strdup(name);
95 	if (as->as.hostnamadr.name == NULL)
96 		goto abort; /* errno set */
97 
98 	_asr_ctx_unref(ac);
99 	return (as);
100 
101     abort:
102 	if (as)
103 		_asr_async_free(as);
104 	_asr_ctx_unref(ac);
105 	return (NULL);
106 }
107 DEF_WEAK(gethostbyname2_async);
108 
109 struct asr_query *
110 gethostbyaddr_async(const void *addr, socklen_t len, int af, void *asr)
111 {
112 	struct asr_ctx	 *ac;
113 	struct asr_query *as;
114 
115 	ac = _asr_use_resolver(asr);
116 	as = _gethostbyaddr_async_ctx(addr, len, af, ac);
117 	_asr_ctx_unref(ac);
118 
119 	return (as);
120 }
121 DEF_WEAK(gethostbyaddr_async);
122 
123 struct asr_query *
124 _gethostbyaddr_async_ctx(const void *addr, socklen_t len, int af,
125     struct asr_ctx *ac)
126 {
127 	struct asr_query *as;
128 
129 	if ((as = _asr_async_new(ac, ASR_GETHOSTBYADDR)) == NULL)
130 		goto abort; /* errno set */
131 	as->as_run = gethostnamadr_async_run;
132 
133 	as->as.hostnamadr.family = af;
134 	as->as.hostnamadr.addrlen = len;
135 	if (len > 0)
136 		memmove(as->as.hostnamadr.addr, addr, (len > 16) ? 16 : len);
137 
138 	return (as);
139 
140     abort:
141 	if (as)
142 		_asr_async_free(as);
143 	return (NULL);
144 }
145 
146 static int
147 gethostnamadr_async_run(struct asr_query *as, struct asr_result *ar)
148 {
149 	struct hostent_ext	*h;
150 	int			 r, type, saved_errno;
151 	FILE			*f;
152 	char			 name[MAXDNAME], *data, addr[16], *c;
153 
154     next:
155 	switch (as->as_state) {
156 
157 	case ASR_STATE_INIT:
158 
159 		if (as->as.hostnamadr.family != AF_INET &&
160 		    as->as.hostnamadr.family != AF_INET6) {
161 			ar->ar_h_errno = NETDB_INTERNAL;
162 			ar->ar_errno = EAFNOSUPPORT;
163 			async_set_state(as, ASR_STATE_HALT);
164 			break;
165 		}
166 
167 		if ((as->as.hostnamadr.family == AF_INET &&
168 		     as->as.hostnamadr.addrlen != INADDRSZ) ||
169 		    (as->as.hostnamadr.family == AF_INET6 &&
170 		     as->as.hostnamadr.addrlen != IN6ADDRSZ)) {
171 			ar->ar_h_errno = NETDB_INTERNAL;
172 			ar->ar_errno = EINVAL;
173 			async_set_state(as, ASR_STATE_HALT);
174 			break;
175 		}
176 
177 		if (as->as_type == ASR_GETHOSTBYNAME) {
178 
179 			if (as->as.hostnamadr.name[0] == '\0') {
180 				ar->ar_h_errno = NO_DATA;
181 				async_set_state(as, ASR_STATE_HALT);
182 				break;
183 			}
184 
185 			/* Name might be an IP address string */
186 			for (c = as->as.hostnamadr.name; *c; c++)
187 				if (!isdigit((unsigned char)*c) &&
188 				     *c != '.' && *c != ':')
189 					break;
190 			if (*c == 0 &&
191 			    inet_pton(as->as.hostnamadr.family,
192 			    as->as.hostnamadr.name, addr) == 1) {
193 				h = hostent_from_addr(as->as.hostnamadr.family,
194 				    as->as.hostnamadr.name, addr);
195 				if (h == NULL) {
196 					ar->ar_errno = errno;
197 					ar->ar_h_errno = NETDB_INTERNAL;
198 				}
199 				else {
200 					ar->ar_hostent = &h->h;
201 					ar->ar_h_errno = NETDB_SUCCESS;
202 				}
203 				async_set_state(as, ASR_STATE_HALT);
204 				break;
205 			}
206 		}
207 		async_set_state(as, ASR_STATE_NEXT_DB);
208 		break;
209 
210 	case ASR_STATE_NEXT_DB:
211 
212 		if (_asr_iter_db(as) == -1) {
213 			async_set_state(as, ASR_STATE_NOT_FOUND);
214 			break;
215 		}
216 
217 		switch (AS_DB(as)) {
218 
219 		case ASR_DB_DNS:
220 
221 			/* Create a subquery to do the DNS lookup */
222 
223 			if (as->as_type == ASR_GETHOSTBYNAME) {
224 				type = (as->as.hostnamadr.family == AF_INET) ?
225 				    T_A : T_AAAA;
226 				as->as_subq = _res_search_async_ctx(
227 				    as->as.hostnamadr.name,
228 				    C_IN, type, as->as_ctx);
229 			} else {
230 				_asr_addr_as_fqdn(as->as.hostnamadr.addr,
231 				    as->as.hostnamadr.family,
232 				    name, sizeof(name));
233 				as->as_subq = _res_query_async_ctx(
234 				    name, C_IN, T_PTR, as->as_ctx);
235 			}
236 
237 			if (as->as_subq == NULL) {
238 				ar->ar_errno = errno;
239 				ar->ar_h_errno = NETDB_INTERNAL;
240 				async_set_state(as, ASR_STATE_HALT);
241 				break;
242 			}
243 
244 			async_set_state(as, ASR_STATE_SUBQUERY);
245 			break;
246 
247 		case ASR_DB_FILE:
248 
249 			/* Try to find a match in the host file */
250 
251 			if ((f = fopen(_PATH_HOSTS, "re")) == NULL)
252 				break;
253 
254 			if (as->as_type == ASR_GETHOSTBYNAME)
255 				data = as->as.hostnamadr.name;
256 			else
257 				data = as->as.hostnamadr.addr;
258 
259 			h = hostent_file_match(f, as->as_type,
260 			    as->as.hostnamadr.family, data,
261 			    as->as.hostnamadr.addrlen);
262 			saved_errno = errno;
263 			fclose(f);
264 			errno = saved_errno;
265 
266 			if (h == NULL) {
267 				if (errno) {
268 					ar->ar_errno = errno;
269 					ar->ar_h_errno = NETDB_INTERNAL;
270 					async_set_state(as, ASR_STATE_HALT);
271 				}
272 				/* otherwise not found */
273 				break;
274 			}
275 			ar->ar_hostent = &h->h;
276 			ar->ar_h_errno = NETDB_SUCCESS;
277 			async_set_state(as, ASR_STATE_HALT);
278 			break;
279 		}
280 		break;
281 
282 	case ASR_STATE_SUBQUERY:
283 
284 		/* Run the DNS subquery. */
285 
286 		if ((r = asr_run(as->as_subq, ar)) == ASYNC_COND)
287 			return (ASYNC_COND);
288 
289 		/* Done. */
290 		as->as_subq = NULL;
291 
292 		if (ar->ar_datalen == -1) {
293 			async_set_state(as, ASR_STATE_NEXT_DB);
294 			break;
295 		}
296 
297 		/* If we got a packet but no anwser, use the next DB. */
298 		if (ar->ar_count == 0) {
299 			free(ar->ar_data);
300 			as->as.hostnamadr.subq_h_errno = ar->ar_h_errno;
301 			async_set_state(as, ASR_STATE_NEXT_DB);
302 			break;
303 		}
304 
305 		/* Read the hostent from the packet. */
306 
307 		h = hostent_from_packet(as->as_type,
308 		    as->as.hostnamadr.family, ar->ar_data, ar->ar_datalen);
309 		free(ar->ar_data);
310 		if (h == NULL) {
311 			ar->ar_errno = errno;
312 			ar->ar_h_errno = NETDB_INTERNAL;
313 			async_set_state(as, ASR_STATE_HALT);
314 			break;
315 		}
316 
317 		if (as->as_type == ASR_GETHOSTBYADDR) {
318 			if (hostent_add_addr(h, as->as.hostnamadr.addr,
319 			    as->as.hostnamadr.addrlen) == -1) {
320 				free(h);
321 				ar->ar_errno = errno;
322 				ar->ar_h_errno = NETDB_INTERNAL;
323 				async_set_state(as, ASR_STATE_HALT);
324 				break;
325 			}
326 		}
327 
328 		/*
329 		 * No valid hostname or address found in the dns packet.
330 		 * Ignore it.
331 		 */
332 		if ((as->as_type == ASR_GETHOSTBYNAME &&
333 		     h->h.h_addr_list[0] == NULL) ||
334 		    h->h.h_name == NULL) {
335 			free(h);
336 			async_set_state(as, ASR_STATE_NEXT_DB);
337 			break;
338 		}
339 
340 		ar->ar_hostent = &h->h;
341 		ar->ar_h_errno = NETDB_SUCCESS;
342 		async_set_state(as, ASR_STATE_HALT);
343 		break;
344 
345 	case ASR_STATE_NOT_FOUND:
346 		ar->ar_errno = 0;
347 		if (as->as.hostnamadr.subq_h_errno)
348 			ar->ar_h_errno = as->as.hostnamadr.subq_h_errno;
349 		else
350 			ar->ar_h_errno = HOST_NOT_FOUND;
351 		async_set_state(as, ASR_STATE_HALT);
352 		break;
353 
354 	case ASR_STATE_HALT:
355 		if (ar->ar_h_errno == NETDB_SUCCESS &&
356 		    as->as_flags & ASYNC_GETNET)
357 			netent_from_hostent(ar);
358 		if (ar->ar_h_errno) {
359 			ar->ar_hostent = NULL;
360 			ar->ar_netent = NULL;
361 		} else
362 			ar->ar_errno = 0;
363 		return (ASYNC_DONE);
364 
365 	default:
366 		ar->ar_errno = EOPNOTSUPP;
367 		ar->ar_h_errno = NETDB_INTERNAL;
368 		ar->ar_gai_errno = EAI_SYSTEM;
369 		async_set_state(as, ASR_STATE_HALT);
370 		break;
371 	}
372 	goto next;
373 }
374 
375 /*
376  * Create a hostent from a numeric address string.
377  */
378 static struct hostent_ext *
379 hostent_from_addr(int family, const char *name, const char *addr)
380 {
381 	struct	 hostent_ext *h;
382 
383 	if ((h = hostent_alloc(family)) == NULL)
384 		return (NULL);
385 	if (hostent_set_cname(h, name, 0) == -1)
386 		goto fail;
387 	if (hostent_add_addr(h, addr, h->h.h_length) == -1)
388 		goto fail;
389 	return (h);
390 fail:
391 	free(h);
392 	return (NULL);
393 }
394 
395 /*
396  * Lookup the first matching entry in the hostfile, either by address or by
397  * name depending on reqtype, and build a hostent from the line.
398  */
399 static struct hostent_ext *
400 hostent_file_match(FILE *f, int reqtype, int family, const char *data,
401     int datalen)
402 {
403 	char	*tokens[MAXTOKEN], addr[16], buf[BUFSIZ + 1];
404 	struct	 hostent_ext *h;
405 	int	 n, i;
406 
407 	for (;;) {
408 		n = _asr_parse_namedb_line(f, tokens, MAXTOKEN, buf, sizeof(buf));
409 		if (n == -1) {
410 			errno = 0; /* ignore errors reading the file */
411 			return (NULL);
412 		}
413 
414 		/* there must be an address and at least one name */
415 		if (n < 2)
416 			continue;
417 
418 		if (reqtype == ASR_GETHOSTBYNAME) {
419 			for (i = 1; i < n; i++) {
420 				if (strcasecmp(data, tokens[i]))
421 					continue;
422 				if (inet_pton(family, tokens[0], addr) == 1)
423 					goto found;
424 			}
425 		} else {
426 			if (inet_pton(family, tokens[0], addr) == 1 &&
427 			    memcmp(addr, data, datalen) == 0)
428 				goto found;
429 		}
430 	}
431 
432 found:
433 	if ((h = hostent_alloc(family)) == NULL)
434 		return (NULL);
435 	if (hostent_set_cname(h, tokens[1], 0) == -1)
436 		goto fail;
437 	for (i = 2; i < n; i ++)
438 		if (hostent_add_alias(h, tokens[i], 0) == -1)
439 			goto fail;
440 	if (hostent_add_addr(h, addr, h->h.h_length) == -1)
441 		goto fail;
442 	return (h);
443 fail:
444 	free(h);
445 	return (NULL);
446 }
447 
448 /*
449  * Fill the hostent from the given DNS packet.
450  */
451 static struct hostent_ext *
452 hostent_from_packet(int reqtype, int family, char *pkt, size_t pktlen)
453 {
454 	struct hostent_ext	*h;
455 	struct asr_unpack	 p;
456 	struct asr_dns_header	 hdr;
457 	struct asr_dns_query	 q;
458 	struct asr_dns_rr	 rr;
459 	char			 dname[MAXDNAME];
460 
461 	if ((h = hostent_alloc(family)) == NULL)
462 		return (NULL);
463 
464 	_asr_unpack_init(&p, pkt, pktlen);
465 	_asr_unpack_header(&p, &hdr);
466 	for (; hdr.qdcount; hdr.qdcount--)
467 		_asr_unpack_query(&p, &q);
468 	strlcpy(dname, q.q_dname, sizeof(dname));
469 
470 	for (; hdr.ancount; hdr.ancount--) {
471 		_asr_unpack_rr(&p, &rr);
472 		if (rr.rr_class != C_IN)
473 			continue;
474 		switch (rr.rr_type) {
475 
476 		case T_CNAME:
477 			if (reqtype == ASR_GETHOSTBYNAME) {
478 				if (hostent_add_alias(h, rr.rr_dname, 1) == -1)
479 					goto fail;
480 			} else {
481 				if (strcasecmp(rr.rr_dname, dname) == 0)
482 					strlcpy(dname, rr.rr.cname.cname,
483 					    sizeof(dname));
484 			}
485 			break;
486 
487 		case T_PTR:
488 			if (reqtype != ASR_GETHOSTBYADDR)
489 				break;
490 			if (strcasecmp(rr.rr_dname, dname) != 0)
491 				continue;
492 			if (hostent_set_cname(h, rr.rr.ptr.ptrname, 1) == -1)
493 				hostent_add_alias(h, rr.rr.ptr.ptrname, 1);
494 			break;
495 
496 		case T_A:
497 			if (reqtype != ASR_GETHOSTBYNAME)
498 				break;
499 			if (family != AF_INET)
500 				break;
501 			if (hostent_set_cname(h, rr.rr_dname, 1) == -1)
502 				;
503 			if (hostent_add_addr(h, &rr.rr.in_a.addr, 4) == -1)
504 				goto fail;
505 			break;
506 
507 		case T_AAAA:
508 			if (reqtype != ASR_GETHOSTBYNAME)
509 				break;
510 			if (family != AF_INET6)
511 				break;
512 			if (hostent_set_cname(h, rr.rr_dname, 1) == -1)
513 				;
514 			if (hostent_add_addr(h, &rr.rr.in_aaaa.addr6, 16) == -1)
515 				goto fail;
516 			break;
517 		}
518 	}
519 
520 	return (h);
521 fail:
522 	free(h);
523 	return (NULL);
524 }
525 
526 static struct hostent_ext *
527 hostent_alloc(int family)
528 {
529 	struct hostent_ext	*h;
530 	size_t			alloc;
531 
532 	alloc = sizeof(*h) + 1024;
533 	if ((h = calloc(1, alloc)) == NULL)
534 		return (NULL);
535 
536 	h->h.h_addrtype = family;
537 	h->h.h_length = (family == AF_INET) ? 4 : 16;
538 	h->h.h_aliases = h->aliases;
539 	h->h.h_addr_list = h->addrs;
540 	h->pos = (char *)(h) + sizeof(*h);
541 	h->end = h->pos + 1024;
542 
543 	return (h);
544 }
545 
546 static int
547 hostent_set_cname(struct hostent_ext *h, const char *name, int isdname)
548 {
549 	char	buf[MAXDNAME];
550 	size_t	n;
551 
552 	if (h->h.h_name)
553 		return (-1);
554 
555 	if (isdname) {
556 		_asr_strdname(name, buf, sizeof buf);
557 		buf[strlen(buf) - 1] = '\0';
558 		if (!res_hnok(buf))
559 			return (-1);
560 		name = buf;
561 	}
562 
563 	n = strlen(name) + 1;
564 	if (h->pos + n >= h->end)
565 		return (-1);
566 
567 	h->h.h_name = h->pos;
568 	memmove(h->pos, name, n);
569 	h->pos += n;
570 	return (0);
571 }
572 
573 static int
574 hostent_add_alias(struct hostent_ext *h, const char *name, int isdname)
575 {
576 	char	buf[MAXDNAME];
577 	size_t	i, n;
578 
579 	for (i = 0; i < MAXALIASES; i++)
580 		if (h->aliases[i] == NULL)
581 			break;
582 	if (i == MAXALIASES)
583 		return (0);
584 
585 	if (isdname) {
586 		_asr_strdname(name, buf, sizeof buf);
587 		buf[strlen(buf)-1] = '\0';
588 		if (!res_hnok(buf))
589 			return (-1);
590 		name = buf;
591 	}
592 
593 	n = strlen(name) + 1;
594 	if (h->pos + n >= h->end)
595 		return (0);
596 
597 	h->aliases[i] = h->pos;
598 	memmove(h->pos, name, n);
599 	h->pos += n;
600 	return (0);
601 }
602 
603 static int
604 hostent_add_addr(struct hostent_ext *h, const void *addr, size_t size)
605 {
606 	int	i;
607 
608 	for (i = 0; i < MAXADDRS; i++)
609 		if (h->addrs[i] == NULL)
610 			break;
611 	if (i == MAXADDRS)
612 		return (0);
613 
614 	if (h->pos + size >= h->end)
615 		return (0);
616 
617 	h->addrs[i] = h->pos;
618 	memmove(h->pos, addr, size);
619 	h->pos += size;
620 	return (0);
621 }
622 
623 static void
624 netent_from_hostent(struct asr_result *ar)
625 {
626 	struct in_addr		 *addr;
627 	struct netent_ext	 *n;
628 	struct hostent_ext	 *h;
629 	char			**na, **ha;
630 	size_t			  sz;
631 
632 	/* Allocate and initialize the output. */
633 	if ((n = calloc(1, sizeof(*n) + 1024)) == NULL) {
634 		ar->ar_h_errno = NETDB_INTERNAL;
635 		ar->ar_errno = errno;
636 		goto out;
637 	}
638 	n->pos = (char *)(n) + sizeof(*n);
639 	n->end = n->pos + 1024;
640 	n->n.n_name = n->pos;
641 	n->n.n_aliases = n->aliases;
642 
643 	/* Copy the fixed-size data. */
644 	h = (struct hostent_ext *)ar->ar_hostent;
645 	addr = (struct in_addr *)h->h.h_addr;
646 	n->n.n_net = ntohl(addr->s_addr);
647 	n->n.n_addrtype = h->h.h_addrtype;
648 
649 	/* Copy the network name. */
650 	sz = strlen(h->h.h_name) + 1;
651 	memcpy(n->pos, h->h.h_name, sz);
652 	n->pos += sz;
653 
654 	/*
655 	 * Copy the aliases.
656 	 * No overflow check is needed because we are merely copying
657 	 * a part of the data from a structure of the same size.
658 	 */
659 	na = n->aliases;
660 	for (ha = h->aliases; *ha != NULL; ha++) {
661 		sz = strlen(*ha) + 1;
662 		memcpy(n->pos, *ha, sz);
663 		*na++ = n->pos;
664 		n->pos += sz;
665 	}
666 	*na = NULL;
667 
668 	/* Handle the return values. */
669 	ar->ar_netent = &n->n;
670 out:
671 	free(ar->ar_hostent);
672 	ar->ar_hostent = NULL;
673 }
674