xref: /openbsd-src/lib/libc/asr/gethostnamadr_async.c (revision b8fce260d60f6211b2f80fb68257eded12d7294d)
1 /*	$OpenBSD: gethostnamadr_async.c,v 1.47 2023/11/14 08:27:33 florian 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 			if (!hnok_lenient(as->as.hostnamadr.name)) {
208 				ar->ar_gai_errno = EAI_FAIL;
209 				async_set_state(as, ASR_STATE_HALT);
210 				break;
211 			}
212 		}
213 		async_set_state(as, ASR_STATE_NEXT_DB);
214 		break;
215 
216 	case ASR_STATE_NEXT_DB:
217 
218 		if (_asr_iter_db(as) == -1) {
219 			async_set_state(as, ASR_STATE_NOT_FOUND);
220 			break;
221 		}
222 
223 		switch (AS_DB(as)) {
224 
225 		case ASR_DB_DNS:
226 
227 			/* Create a subquery to do the DNS lookup */
228 
229 			if (as->as_type == ASR_GETHOSTBYNAME) {
230 				type = (as->as.hostnamadr.family == AF_INET) ?
231 				    T_A : T_AAAA;
232 				as->as_subq = _res_search_async_ctx(
233 				    as->as.hostnamadr.name,
234 				    C_IN, type, as->as_ctx);
235 			} else {
236 				_asr_addr_as_fqdn(as->as.hostnamadr.addr,
237 				    as->as.hostnamadr.family,
238 				    name, sizeof(name));
239 				as->as_subq = _res_query_async_ctx(
240 				    name, C_IN, T_PTR, as->as_ctx);
241 			}
242 
243 			if (as->as_subq == NULL) {
244 				ar->ar_errno = errno;
245 				ar->ar_h_errno = NETDB_INTERNAL;
246 				async_set_state(as, ASR_STATE_HALT);
247 				break;
248 			}
249 
250 			async_set_state(as, ASR_STATE_SUBQUERY);
251 			break;
252 
253 		case ASR_DB_FILE:
254 
255 			/* Try to find a match in the host file */
256 
257 			if ((f = fopen(_PATH_HOSTS, "re")) == NULL)
258 				break;
259 
260 			if (as->as_type == ASR_GETHOSTBYNAME)
261 				data = as->as.hostnamadr.name;
262 			else
263 				data = as->as.hostnamadr.addr;
264 
265 			h = hostent_file_match(f, as->as_type,
266 			    as->as.hostnamadr.family, data,
267 			    as->as.hostnamadr.addrlen);
268 			saved_errno = errno;
269 			fclose(f);
270 			errno = saved_errno;
271 
272 			if (h == NULL) {
273 				if (errno) {
274 					ar->ar_errno = errno;
275 					ar->ar_h_errno = NETDB_INTERNAL;
276 					async_set_state(as, ASR_STATE_HALT);
277 				}
278 				/* otherwise not found */
279 				break;
280 			}
281 			ar->ar_hostent = &h->h;
282 			ar->ar_h_errno = NETDB_SUCCESS;
283 			async_set_state(as, ASR_STATE_HALT);
284 			break;
285 		}
286 		break;
287 
288 	case ASR_STATE_SUBQUERY:
289 
290 		/* Run the DNS subquery. */
291 
292 		if ((r = asr_run(as->as_subq, ar)) == ASYNC_COND)
293 			return (ASYNC_COND);
294 
295 		/* Done. */
296 		as->as_subq = NULL;
297 
298 		/*
299 		 * We either got no packet or a packet without an answer.
300 		 * Saveguard the h_errno and use the next DB.
301 		 */
302 		if (ar->ar_count == 0) {
303 			free(ar->ar_data);
304 			as->as.hostnamadr.subq_h_errno = ar->ar_h_errno;
305 			async_set_state(as, ASR_STATE_NEXT_DB);
306 			break;
307 		}
308 
309 		/* Read the hostent from the packet. */
310 
311 		h = hostent_from_packet(as->as_type,
312 		    as->as.hostnamadr.family, ar->ar_data, ar->ar_datalen);
313 		free(ar->ar_data);
314 		if (h == NULL) {
315 			ar->ar_errno = errno;
316 			ar->ar_h_errno = NETDB_INTERNAL;
317 			async_set_state(as, ASR_STATE_HALT);
318 			break;
319 		}
320 
321 		if (as->as_type == ASR_GETHOSTBYADDR) {
322 			if (hostent_add_addr(h, as->as.hostnamadr.addr,
323 			    as->as.hostnamadr.addrlen) == -1) {
324 				free(h);
325 				ar->ar_errno = errno;
326 				ar->ar_h_errno = NETDB_INTERNAL;
327 				async_set_state(as, ASR_STATE_HALT);
328 				break;
329 			}
330 		}
331 
332 		/*
333 		 * No valid hostname or address found in the dns packet.
334 		 * Ignore it.
335 		 */
336 		if ((as->as_type == ASR_GETHOSTBYNAME &&
337 		     h->h.h_addr_list[0] == NULL) ||
338 		    h->h.h_name == NULL) {
339 			free(h);
340 			async_set_state(as, ASR_STATE_NEXT_DB);
341 			break;
342 		}
343 
344 		ar->ar_hostent = &h->h;
345 		ar->ar_h_errno = NETDB_SUCCESS;
346 		async_set_state(as, ASR_STATE_HALT);
347 		break;
348 
349 	case ASR_STATE_NOT_FOUND:
350 		ar->ar_errno = 0;
351 		if (as->as.hostnamadr.subq_h_errno)
352 			ar->ar_h_errno = as->as.hostnamadr.subq_h_errno;
353 		else
354 			ar->ar_h_errno = HOST_NOT_FOUND;
355 		async_set_state(as, ASR_STATE_HALT);
356 		break;
357 
358 	case ASR_STATE_HALT:
359 		if (ar->ar_h_errno == NETDB_SUCCESS &&
360 		    as->as_flags & ASYNC_GETNET)
361 			netent_from_hostent(ar);
362 		if (ar->ar_h_errno) {
363 			ar->ar_hostent = NULL;
364 			ar->ar_netent = NULL;
365 		} else
366 			ar->ar_errno = 0;
367 		return (ASYNC_DONE);
368 
369 	default:
370 		ar->ar_errno = EOPNOTSUPP;
371 		ar->ar_h_errno = NETDB_INTERNAL;
372 		ar->ar_gai_errno = EAI_SYSTEM;
373 		async_set_state(as, ASR_STATE_HALT);
374 		break;
375 	}
376 	goto next;
377 }
378 
379 /*
380  * Create a hostent from a numeric address string.
381  */
382 static struct hostent_ext *
383 hostent_from_addr(int family, const char *name, const char *addr)
384 {
385 	struct	 hostent_ext *h;
386 
387 	if ((h = hostent_alloc(family)) == NULL)
388 		return (NULL);
389 	if (hostent_set_cname(h, name, 0) == -1)
390 		goto fail;
391 	if (hostent_add_addr(h, addr, h->h.h_length) == -1)
392 		goto fail;
393 	return (h);
394 fail:
395 	free(h);
396 	return (NULL);
397 }
398 
399 /*
400  * Lookup the first matching entry in the hostfile, either by address or by
401  * name depending on reqtype, and build a hostent from the line.
402  */
403 static struct hostent_ext *
404 hostent_file_match(FILE *f, int reqtype, int family, const char *data,
405     int datalen)
406 {
407 	char	*tokens[MAXTOKEN], addr[16], buf[BUFSIZ + 1];
408 	struct	 hostent_ext *h;
409 	int	 n, i;
410 
411 	for (;;) {
412 		n = _asr_parse_namedb_line(f, tokens, MAXTOKEN, buf, sizeof(buf));
413 		if (n == -1) {
414 			errno = 0; /* ignore errors reading the file */
415 			return (NULL);
416 		}
417 
418 		/* there must be an address and at least one name */
419 		if (n < 2)
420 			continue;
421 
422 		if (reqtype == ASR_GETHOSTBYNAME) {
423 			for (i = 1; i < n; i++) {
424 				if (strcasecmp(data, tokens[i]))
425 					continue;
426 				if (inet_pton(family, tokens[0], addr) == 1)
427 					goto found;
428 			}
429 		} else {
430 			if (inet_pton(family, tokens[0], addr) == 1 &&
431 			    memcmp(addr, data, datalen) == 0)
432 				goto found;
433 		}
434 	}
435 
436 found:
437 	if ((h = hostent_alloc(family)) == NULL)
438 		return (NULL);
439 	if (hostent_set_cname(h, tokens[1], 0) == -1)
440 		goto fail;
441 	for (i = 2; i < n; i ++)
442 		if (hostent_add_alias(h, tokens[i], 0) == -1)
443 			goto fail;
444 	if (hostent_add_addr(h, addr, h->h.h_length) == -1)
445 		goto fail;
446 	return (h);
447 fail:
448 	free(h);
449 	return (NULL);
450 }
451 
452 /*
453  * Fill the hostent from the given DNS packet.
454  */
455 static struct hostent_ext *
456 hostent_from_packet(int reqtype, int family, char *pkt, size_t pktlen)
457 {
458 	struct hostent_ext	*h;
459 	struct asr_unpack	 p;
460 	struct asr_dns_header	 hdr;
461 	struct asr_dns_query	 q;
462 	struct asr_dns_rr	 rr;
463 	char			 dname[MAXDNAME];
464 
465 	if ((h = hostent_alloc(family)) == NULL)
466 		return (NULL);
467 
468 	_asr_unpack_init(&p, pkt, pktlen);
469 	_asr_unpack_header(&p, &hdr);
470 	for (; hdr.qdcount; hdr.qdcount--)
471 		_asr_unpack_query(&p, &q);
472 	strlcpy(dname, q.q_dname, sizeof(dname));
473 
474 	for (; hdr.ancount; hdr.ancount--) {
475 		_asr_unpack_rr(&p, &rr);
476 		if (rr.rr_class != C_IN)
477 			continue;
478 		switch (rr.rr_type) {
479 
480 		case T_CNAME:
481 			if (reqtype == ASR_GETHOSTBYNAME) {
482 				if (hostent_add_alias(h, rr.rr_dname, 1) == -1)
483 					goto fail;
484 			} else {
485 				if (strcasecmp(rr.rr_dname, dname) == 0)
486 					strlcpy(dname, rr.rr.cname.cname,
487 					    sizeof(dname));
488 			}
489 			break;
490 
491 		case T_PTR:
492 			if (reqtype != ASR_GETHOSTBYADDR)
493 				break;
494 			if (strcasecmp(rr.rr_dname, dname) != 0)
495 				continue;
496 			if (hostent_set_cname(h, rr.rr.ptr.ptrname, 1) == -1)
497 				hostent_add_alias(h, rr.rr.ptr.ptrname, 1);
498 			break;
499 
500 		case T_A:
501 			if (reqtype != ASR_GETHOSTBYNAME)
502 				break;
503 			if (family != AF_INET)
504 				break;
505 			if (hostent_set_cname(h, rr.rr_dname, 1) == -1)
506 				;
507 			if (hostent_add_addr(h, &rr.rr.in_a.addr, 4) == -1)
508 				goto fail;
509 			break;
510 
511 		case T_AAAA:
512 			if (reqtype != ASR_GETHOSTBYNAME)
513 				break;
514 			if (family != AF_INET6)
515 				break;
516 			if (hostent_set_cname(h, rr.rr_dname, 1) == -1)
517 				;
518 			if (hostent_add_addr(h, &rr.rr.in_aaaa.addr6, 16) == -1)
519 				goto fail;
520 			break;
521 		}
522 	}
523 
524 	return (h);
525 fail:
526 	free(h);
527 	return (NULL);
528 }
529 
530 static struct hostent_ext *
531 hostent_alloc(int family)
532 {
533 	struct hostent_ext	*h;
534 	size_t			alloc;
535 
536 	alloc = sizeof(*h) + 1024;
537 	if ((h = calloc(1, alloc)) == NULL)
538 		return (NULL);
539 
540 	h->h.h_addrtype = family;
541 	h->h.h_length = (family == AF_INET) ? 4 : 16;
542 	h->h.h_aliases = h->aliases;
543 	h->h.h_addr_list = h->addrs;
544 	h->pos = (char *)(h) + sizeof(*h);
545 	h->end = h->pos + 1024;
546 
547 	return (h);
548 }
549 
550 static int
551 hostent_set_cname(struct hostent_ext *h, const char *name, int isdname)
552 {
553 	char	buf[MAXDNAME];
554 	size_t	n;
555 
556 	if (h->h.h_name)
557 		return (-1);
558 
559 	if (isdname) {
560 		_asr_strdname(name, buf, sizeof buf);
561 		buf[strlen(buf) - 1] = '\0';
562 		if (!res_hnok(buf))
563 			return (-1);
564 		name = buf;
565 	}
566 
567 	n = strlen(name) + 1;
568 	if (h->pos + n >= h->end)
569 		return (-1);
570 
571 	h->h.h_name = h->pos;
572 	memmove(h->pos, name, n);
573 	h->pos += n;
574 	return (0);
575 }
576 
577 static int
578 hostent_add_alias(struct hostent_ext *h, const char *name, int isdname)
579 {
580 	char	buf[MAXDNAME];
581 	size_t	i, n;
582 
583 	for (i = 0; i < MAXALIASES; i++)
584 		if (h->aliases[i] == NULL)
585 			break;
586 	if (i == MAXALIASES)
587 		return (0);
588 
589 	if (isdname) {
590 		_asr_strdname(name, buf, sizeof buf);
591 		buf[strlen(buf)-1] = '\0';
592 		if (!res_hnok(buf))
593 			return (-1);
594 		name = buf;
595 	}
596 
597 	n = strlen(name) + 1;
598 	if (h->pos + n >= h->end)
599 		return (0);
600 
601 	h->aliases[i] = h->pos;
602 	memmove(h->pos, name, n);
603 	h->pos += n;
604 	return (0);
605 }
606 
607 static int
608 hostent_add_addr(struct hostent_ext *h, const void *addr, size_t size)
609 {
610 	int	i;
611 
612 	for (i = 0; i < MAXADDRS; i++)
613 		if (h->addrs[i] == NULL)
614 			break;
615 	if (i == MAXADDRS)
616 		return (0);
617 
618 	if (h->pos + size >= h->end)
619 		return (0);
620 
621 	h->addrs[i] = h->pos;
622 	memmove(h->pos, addr, size);
623 	h->pos += size;
624 	return (0);
625 }
626 
627 static void
628 netent_from_hostent(struct asr_result *ar)
629 {
630 	struct in_addr		 *addr;
631 	struct netent_ext	 *n;
632 	struct hostent_ext	 *h;
633 	char			**na, **ha;
634 	size_t			  sz;
635 
636 	/* Allocate and initialize the output. */
637 	if ((n = calloc(1, sizeof(*n) + 1024)) == NULL) {
638 		ar->ar_h_errno = NETDB_INTERNAL;
639 		ar->ar_errno = errno;
640 		goto out;
641 	}
642 	n->pos = (char *)(n) + sizeof(*n);
643 	n->end = n->pos + 1024;
644 	n->n.n_name = n->pos;
645 	n->n.n_aliases = n->aliases;
646 
647 	/* Copy the fixed-size data. */
648 	h = (struct hostent_ext *)ar->ar_hostent;
649 	addr = (struct in_addr *)h->h.h_addr;
650 	n->n.n_net = ntohl(addr->s_addr);
651 	n->n.n_addrtype = h->h.h_addrtype;
652 
653 	/* Copy the network name. */
654 	sz = strlen(h->h.h_name) + 1;
655 	memcpy(n->pos, h->h.h_name, sz);
656 	n->pos += sz;
657 
658 	/*
659 	 * Copy the aliases.
660 	 * No overflow check is needed because we are merely copying
661 	 * a part of the data from a structure of the same size.
662 	 */
663 	na = n->aliases;
664 	for (ha = h->aliases; *ha != NULL; ha++) {
665 		sz = strlen(*ha) + 1;
666 		memcpy(n->pos, *ha, sz);
667 		*na++ = n->pos;
668 		n->pos += sz;
669 	}
670 	*na = NULL;
671 
672 	/* Handle the return values. */
673 	ar->ar_netent = &n->n;
674 out:
675 	free(ar->ar_hostent);
676 	ar->ar_hostent = NULL;
677 }
678