xref: /openbsd-src/lib/libc/asr/asr.c (revision 99491d360b76118f85d20a01103f32af3a233d58)
1 /*	$OpenBSD: asr.c,v 1.23 2013/04/17 02:09:18 deraadt Exp $	*/
2 /*
3  * Copyright (c) 2010-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/stat.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <arpa/nameser.h>
23 
24 #include <err.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <netdb.h>
28 #include <resolv.h>
29 #include <poll.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 
35 #include "asr.h"
36 #include "asr_private.h"
37 
38 #ifndef ASR_OPT_THREADSAFE
39 #define ASR_OPT_THREADSAFE 1
40 #endif
41 #ifndef ASR_OPT_HOSTALIASES
42 #define ASR_OPT_HOSTALIASES 1
43 #endif
44 #ifndef ASR_OPT_ENVOPTS
45 #define ASR_OPT_ENVOPTS 1
46 #endif
47 #ifndef ASR_OPT_RELOADCONF
48 #define ASR_OPT_RELOADCONF 1
49 #endif
50 #ifndef ASR_OPT_ALTCONF
51 #define ASR_OPT_ALTCONF 1
52 #endif
53 
54 #if ASR_OPT_THREADSAFE
55 #include "thread_private.h"
56 #endif
57 
58 #define DEFAULT_CONFFILE	"/etc/resolv.conf"
59 #define DEFAULT_HOSTFILE	"/etc/hosts"
60 #define DEFAULT_CONF		"lookup file\n"
61 #define DEFAULT_LOOKUP		"lookup bind file"
62 
63 #define RELOAD_DELAY		15 /* seconds */
64 
65 static void asr_check_reload(struct asr *);
66 static struct asr_ctx *asr_ctx_create(void);
67 static void asr_ctx_ref(struct asr_ctx *);
68 static void asr_ctx_free(struct asr_ctx *);
69 static int asr_ctx_add_searchdomain(struct asr_ctx *, const char *);
70 static int asr_ctx_from_file(struct asr_ctx *, const char *);
71 static int asr_ctx_from_string(struct asr_ctx *, const char *);
72 static int asr_ctx_parse(struct asr_ctx *, const char *);
73 static int asr_parse_nameserver(struct sockaddr *, const char *);
74 static int asr_ndots(const char *);
75 static void pass0(char **, int, struct asr_ctx *);
76 static int strsplit(char *, char **, int);
77 #if ASR_OPT_HOSTALIASES
78 static char *asr_hostalias(const char *, char *, size_t);
79 #endif
80 #if ASR_OPT_ENVOPTS
81 static void asr_ctx_envopts(struct asr_ctx *);
82 #endif
83 #if ASR_OPT_THREADSAFE
84 static void *__THREAD_NAME(_asr);
85 #else
86 #	define _THREAD_PRIVATE(a, b, c)  (c)
87 #endif
88 
89 static struct asr *_asr = NULL;
90 
91 /* Allocate and configure an async "resolver". */
92 struct asr *
93 async_resolver(const char *conf)
94 {
95 	static int	 init = 0;
96 	struct asr	*asr;
97 
98 	if (init == 0) {
99 #ifdef DEBUG
100 		if (getenv("ASR_DEBUG"))
101 			asr_debug = stderr;
102 #endif
103 		init = 1;
104 	}
105 
106 	if ((asr = calloc(1, sizeof(*asr))) == NULL)
107 		goto fail;
108 
109 #if ASR_OPT_ALTCONF
110 	/* If not setuid/setgid, allow to use an alternate config. */
111 	if (conf == NULL && !issetugid())
112 		conf = getenv("ASR_CONFIG");
113 #endif
114 
115 	if (conf == NULL)
116 		conf = DEFAULT_CONFFILE;
117 
118 	if (conf[0] == '!') {
119 		/* Use the rest of the string as config file */
120 		if ((asr->a_ctx = asr_ctx_create()) == NULL)
121 			goto fail;
122 		if (asr_ctx_from_string(asr->a_ctx, conf + 1) == -1)
123 			goto fail;
124 	} else {
125 		/* Use the given config file */
126 		asr->a_path = strdup(conf);
127 		asr_check_reload(asr);
128 		if (asr->a_ctx == NULL) {
129 			if ((asr->a_ctx = asr_ctx_create()) == NULL)
130 				goto fail;
131 			if (asr_ctx_from_string(asr->a_ctx, DEFAULT_CONF) == -1)
132 				goto fail;
133 #if ASR_OPT_ENVOPTS
134 			asr_ctx_envopts(asr->a_ctx);
135 #endif
136 		}
137 	}
138 
139 #ifdef DEBUG
140 	asr_dump_config(asr_debug, asr);
141 #endif
142 	return (asr);
143 
144     fail:
145 	if (asr) {
146 		if (asr->a_ctx)
147 			asr_ctx_free(asr->a_ctx);
148 		free(asr);
149 	}
150 
151 	return (NULL);
152 }
153 
154 /*
155  * Free the "asr" async resolver (or the thread-local resolver if NULL).
156  * Drop the reference to the current context.
157  */
158 void
159 async_resolver_done(struct asr *asr)
160 {
161 	struct asr **priv;
162 
163 	if (asr == NULL) {
164 		priv = _THREAD_PRIVATE(_asr, _asr, &_asr);
165 		if (*priv == NULL)
166 			return;
167 		asr = *priv;
168 		*priv = NULL;
169 	}
170 
171 	asr_ctx_unref(asr->a_ctx);
172 	if (asr->a_path)
173 		free(asr->a_path);
174 	free(asr);
175 }
176 
177 /*
178  * Cancel an async query.
179  */
180 void
181 async_abort(struct async *as)
182 {
183 	async_free(as);
184 }
185 
186 /*
187  * Resume the "as" async query resolution.  Return one of ASYNC_COND,
188  * ASYNC_YIELD or ASYNC_DONE and put query-specific return values in
189  * the user-allocated memory at "ar".
190  */
191 int
192 async_run(struct async *as, struct async_res *ar)
193 {
194 	int	r, saved_errno = errno;
195 
196 	DPRINT("asr: async_run(%p, %p) %s ctx=[%p]\n", as, ar,
197 	    asr_querystr(as->as_type), as->as_ctx);
198 	r = as->as_run(as, ar);
199 
200 	DPRINT("asr: async_run(%p, %p) -> %s", as, ar, asr_transitionstr(r));
201 #ifdef DEBUG
202 	if (r == ASYNC_COND)
203 #endif
204 		DPRINT(" fd=%i timeout=%i", ar->ar_fd, ar->ar_timeout);
205 	DPRINT("\n");
206 	if (r == ASYNC_DONE)
207 		async_free(as);
208 
209 	errno = saved_errno;
210 
211 	return (r);
212 }
213 
214 /*
215  * Same as above, but run in a loop that handles the fd conditions result.
216  */
217 int
218 async_run_sync(struct async *as, struct async_res *ar)
219 {
220 	struct pollfd	 fds[1];
221 	int		 r, saved_errno = errno;
222 
223 	while ((r = async_run(as, ar)) == ASYNC_COND) {
224 		fds[0].fd = ar->ar_fd;
225 		fds[0].events = (ar->ar_cond == ASYNC_READ) ? POLLIN : POLLOUT;
226 	again:
227 		r = poll(fds, 1, ar->ar_timeout);
228 		if (r == -1 && errno == EINTR)
229 			goto again;
230 		/*
231 		 * Otherwise, just ignore the error and let async_run()
232 		 * catch the failure.
233 		 */
234 	}
235 
236 	errno = saved_errno;
237 
238 	return (r);
239 }
240 
241 /*
242  * Create a new async request of the given "type" on the async context "ac".
243  * Take a reference on it so it does not gets deleted while the async query
244  * is running.
245  */
246 struct async *
247 async_new(struct asr_ctx *ac, int type)
248 {
249 	struct async	*as;
250 
251 	DPRINT("asr: async_new(ctx=%p) type=%i refcount=%i\n", ac, type,
252 	    ac->ac_refcount);
253 	if ((as = calloc(1, sizeof(*as))) == NULL)
254 		return (NULL);
255 
256 	ac->ac_refcount += 1;
257 	as->as_ctx = ac;
258 	as->as_fd = -1;
259 	as->as_type = type;
260 	as->as_state = ASR_STATE_INIT;
261 
262 	return (as);
263 }
264 
265 /*
266  * Free an async query and unref the associated context.
267  */
268 void
269 async_free(struct async *as)
270 {
271 	DPRINT("asr: async_free(%p)\n", as);
272 	switch (as->as_type) {
273 	case ASR_SEND:
274 		if (as->as_fd != -1)
275 			close(as->as_fd);
276 		if (as->as.dns.obuf && !(as->as.dns.flags & ASYNC_EXTOBUF))
277 			free(as->as.dns.obuf);
278 		if (as->as.dns.ibuf && !(as->as.dns.flags & ASYNC_EXTIBUF))
279 			free(as->as.dns.ibuf);
280 		if (as->as.dns.dname)
281 			free(as->as.dns.dname);
282 		break;
283 
284 	case ASR_SEARCH:
285 		if (as->as.search.subq)
286 			async_free(as->as.search.subq);
287 		if (as->as.search.name)
288 			free(as->as.search.name);
289 		break;
290 
291 	case ASR_GETRRSETBYNAME:
292 		if (as->as.rrset.subq)
293 			async_free(as->as.rrset.subq);
294 		if (as->as.rrset.name)
295 			free(as->as.rrset.name);
296 		break;
297 
298 	case ASR_GETHOSTBYNAME:
299 	case ASR_GETHOSTBYADDR:
300 		if (as->as.hostnamadr.subq)
301 			async_free(as->as.hostnamadr.subq);
302 		if (as->as.hostnamadr.name)
303 			free(as->as.hostnamadr.name);
304 		break;
305 
306 	case ASR_GETNETBYNAME:
307 	case ASR_GETNETBYADDR:
308 		if (as->as.netnamadr.subq)
309 			async_free(as->as.netnamadr.subq);
310 		if (as->as.netnamadr.name)
311 			free(as->as.netnamadr.name);
312 		break;
313 
314 	case ASR_GETADDRINFO:
315 		if (as->as.ai.subq)
316 			async_free(as->as.ai.subq);
317 		if (as->as.ai.aifirst)
318 			freeaddrinfo(as->as.ai.aifirst);
319 		if (as->as.ai.hostname)
320 			free(as->as.ai.hostname);
321 		if (as->as.ai.servname)
322 			free(as->as.ai.servname);
323 		if (as->as.ai.fqdn)
324 			free(as->as.ai.fqdn);
325 		break;
326 
327 	case ASR_GETNAMEINFO:
328 		if (as->as.ni.subq)
329 			async_free(as->as.ni.subq);
330 		break;
331 	}
332 
333 	asr_ctx_unref(as->as_ctx);
334 	free(as);
335 }
336 
337 /*
338  * Get a context from the given resolver. This takes a new reference to
339  * the returned context, which *must* be explicitely dropped when done
340  * using this context.
341  */
342 struct asr_ctx *
343 asr_use_resolver(struct asr *asr)
344 {
345 	struct asr **priv;
346 
347 	if (asr == NULL) {
348 		DPRINT("using thread-local resolver\n");
349 		priv = _THREAD_PRIVATE(_asr, _asr, &_asr);
350 		if (*priv == NULL) {
351 			DPRINT("setting up thread-local resolver\n");
352 			*priv = async_resolver(NULL);
353 		}
354 		asr = *priv;
355 	}
356 
357 	asr_check_reload(asr);
358 	asr_ctx_ref(asr->a_ctx);
359 	return (asr->a_ctx);
360 }
361 
362 static void
363 asr_ctx_ref(struct asr_ctx *ac)
364 {
365 	DPRINT("asr: asr_ctx_ref(ctx=%p) refcount=%i\n", ac, ac->ac_refcount);
366 	ac->ac_refcount += 1;
367 }
368 
369 /*
370  * Drop a reference to an async context, freeing it if the reference
371  * count drops to 0.
372  */
373 void
374 asr_ctx_unref(struct asr_ctx *ac)
375 {
376 	DPRINT("asr: asr_ctx_unref(ctx=%p) refcount=%i\n", ac, ac->ac_refcount);
377 	if (--ac->ac_refcount)
378 		return;
379 
380 	asr_ctx_free(ac);
381 }
382 
383 static void
384 asr_ctx_free(struct asr_ctx *ac)
385 {
386 	int i;
387 
388 	if (ac->ac_domain)
389 		free(ac->ac_domain);
390 	for (i = 0; i < ac->ac_nscount; i++)
391 		free(ac->ac_ns[i]);
392 	for (i = 0; i < ac->ac_domcount; i++)
393 		free(ac->ac_dom[i]);
394 
395 	free(ac);
396 }
397 
398 /*
399  * Reload the configuration file if it has changed on disk.
400  */
401 static void
402 asr_check_reload(struct asr *asr)
403 {
404 	struct asr_ctx	*ac;
405 #if ASR_OPT_RELOADCONF
406 	struct stat	 st;
407 	struct timespec	 ts;
408 #endif
409 
410 	if (asr->a_path == NULL)
411 		return;
412 
413 #if ASR_OPT_RELOADCONF
414 	if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
415 		return;
416 
417 	if ((ts.tv_sec - asr->a_rtime) < RELOAD_DELAY && asr->a_rtime != 0)
418 		return;
419 	asr->a_rtime = ts.tv_sec;
420 
421 	DPRINT("asr: checking for update of \"%s\"\n", asr->a_path);
422 	if (stat(asr->a_path, &st) == -1 ||
423 	    asr->a_mtime == st.st_mtime ||
424 	    (ac = asr_ctx_create()) == NULL)
425 		return;
426 	asr->a_mtime = st.st_mtime;
427 #else
428 	if ((ac = asr_ctx_create()) == NULL)
429 		return;
430 #endif
431 
432 	DPRINT("asr: reloading config file\n");
433 	if (asr_ctx_from_file(ac, asr->a_path) == -1) {
434 		asr_ctx_free(ac);
435 		return;
436 	}
437 
438 #if ASR_OPT_ENVOPTS
439 	asr_ctx_envopts(ac);
440 #endif
441 	if (asr->a_ctx)
442 		asr_ctx_unref(asr->a_ctx);
443 	asr->a_ctx = ac;
444 }
445 
446 /*
447  * Construct a fully-qualified domain name for the given name and domain.
448  * If "name" ends with a '.' it is considered as a FQDN by itself.
449  * Otherwise, the domain, which must be a FQDN, is appended to "name" (it
450  * may have a leading dot which would be ignored). If the domain is null,
451  * then "." is used. Return the length of the constructed FQDN or (0) on
452  * error.
453  */
454 size_t
455 asr_make_fqdn(const char *name, const char *domain, char *buf, size_t buflen)
456 {
457 	size_t	len;
458 
459 	if (domain == NULL)
460 		domain = ".";
461 	else if ((len = strlen(domain)) == 0)
462 		return (0);
463 	else if (domain[len -1] != '.')
464 		return (0);
465 
466 	len = strlen(name);
467 	if (len == 0) {
468 		if (strlcpy(buf, domain, buflen) >= buflen)
469 			return (0);
470 	} else if (name[len - 1] !=  '.') {
471 		if (domain[0] == '.')
472 			domain += 1;
473 		if (strlcpy(buf, name, buflen) >= buflen ||
474 		    strlcat(buf, ".", buflen) >= buflen ||
475 		    strlcat(buf, domain, buflen) >= buflen)
476 			return (0);
477 	} else {
478 		if (strlcpy(buf, name, buflen) >= buflen)
479 			return (0);
480 	}
481 
482 	return (strlen(buf));
483 }
484 
485 /*
486  * Concatenate a name and a domain name. The result has no trailing dot.
487  * Return the resulting string length, or 0 in case of error.
488  */
489 size_t
490 asr_domcat(const char *name, const char *domain, char *buf, size_t buflen)
491 {
492 	size_t	r;
493 
494 	r = asr_make_fqdn(name, domain, buf, buflen);
495 	if (r == 0)
496 		return (0);
497 	buf[r - 1] = '\0';
498 
499 	return (r - 1);
500 }
501 
502 /*
503  * Count the dots in a string.
504  */
505 static int
506 asr_ndots(const char *s)
507 {
508 	int n;
509 
510 	for (n = 0; *s; s++)
511 		if (*s == '.')
512 			n += 1;
513 
514 	return (n);
515 }
516 
517 /*
518  * Allocate a new empty context.
519  */
520 static struct asr_ctx *
521 asr_ctx_create(void)
522 {
523 	struct asr_ctx	*ac;
524 
525 	if ((ac = calloc(1, sizeof(*ac))) == NULL)
526 		return (NULL);
527 
528 	ac->ac_options = RES_RECURSE | RES_DEFNAMES | RES_DNSRCH;
529 	ac->ac_refcount = 1;
530 	ac->ac_ndots = 1;
531 	ac->ac_family[0] = AF_INET;
532 	ac->ac_family[1] = AF_INET6;
533 	ac->ac_family[2] = -1;
534 
535 	ac->ac_hostfile = DEFAULT_HOSTFILE;
536 
537 	ac->ac_nscount = 0;
538 	ac->ac_nstimeout = 5;
539 	ac->ac_nsretries = 4;
540 
541 	return (ac);
542 }
543 
544 /*
545  * Add a search domain to the async context.
546  */
547 static int
548 asr_ctx_add_searchdomain(struct asr_ctx *ac, const char *domain)
549 {
550 	char buf[MAXDNAME];
551 
552 	if (ac->ac_domcount == ASR_MAXDOM)
553 		return (-1);
554 
555 	if (asr_make_fqdn(domain, NULL, buf, sizeof(buf)) == 0)
556 		return (-1);
557 
558 	if ((ac->ac_dom[ac->ac_domcount] = strdup(buf)) == NULL)
559 		return (0);
560 
561 	ac->ac_domcount += 1;
562 
563 	return (1);
564 }
565 
566 static int
567 strsplit(char *line, char **tokens, int ntokens)
568 {
569 	int	ntok;
570 	char	*cp, **tp;
571 
572 	for (cp = line, tp = tokens, ntok = 0;
573 	    ntok < ntokens && (*tp = strsep(&cp, " \t")) != NULL; )
574 		if (**tp != '\0') {
575 			tp++;
576 			ntok++;
577 		}
578 
579 	return (ntok);
580 }
581 
582 /*
583  * Pass on a split config line.
584  */
585 static void
586 pass0(char **tok, int n, struct asr_ctx *ac)
587 {
588 	int		 i, j, d;
589 	const char	*e;
590 	struct sockaddr_storage	ss;
591 
592 	if (!strcmp(tok[0], "nameserver")) {
593 		if (ac->ac_nscount == ASR_MAXNS)
594 			return;
595 		if (n != 2)
596 			return;
597 		if (asr_parse_nameserver((struct sockaddr *)&ss, tok[1]))
598 			return;
599 		if ((ac->ac_ns[ac->ac_nscount] = calloc(1, ss.ss_len)) == NULL)
600 			return;
601 		memmove(ac->ac_ns[ac->ac_nscount], &ss, ss.ss_len);
602 		ac->ac_nscount += 1;
603 
604 	} else if (!strcmp(tok[0], "domain")) {
605 		if (n != 2)
606 			return;
607 		if (ac->ac_domain)
608 			return;
609 		ac->ac_domain = strdup(tok[1]);
610 
611 	} else if (!strcmp(tok[0], "lookup")) {
612 		/* ensure that each lookup is only given once */
613 		for (i = 1; i < n; i++)
614 			for (j = i + 1; j < n; j++)
615 				if (!strcmp(tok[i], tok[j]))
616 					return;
617 		ac->ac_dbcount = 0;
618 		for (i = 1; i < n && ac->ac_dbcount < ASR_MAXDB; i++) {
619 			if (!strcmp(tok[i], "yp"))
620 				ac->ac_db[ac->ac_dbcount++] = ASR_DB_YP;
621 			else if (!strcmp(tok[i], "bind"))
622 				ac->ac_db[ac->ac_dbcount++] = ASR_DB_DNS;
623 			else if (!strcmp(tok[i], "file"))
624 				ac->ac_db[ac->ac_dbcount++] = ASR_DB_FILE;
625 		}
626 	} else if (!strcmp(tok[0], "search")) {
627 		/* resolv.conf says the last line wins */
628 		for (i = 0; i < ac->ac_domcount; i++)
629 			free(ac->ac_dom[i]);
630 		ac->ac_domcount = 0;
631 		for (i = 1; i < n; i++)
632 			asr_ctx_add_searchdomain(ac, tok[i]);
633 
634 	} else if (!strcmp(tok[0], "family")) {
635 		if (n == 1 || n > 3)
636 			return;
637 		for (i = 1; i < n; i++)
638 			if (strcmp(tok[i], "inet4") && strcmp(tok[i], "inet6"))
639 				return;
640 		for (i = 1; i < n; i++)
641 			ac->ac_family[i - 1] = strcmp(tok[i], "inet4") ? \
642 			    AF_INET6 : AF_INET;
643 		ac->ac_family[i - 1] = -1;
644 
645 	} else if (!strcmp(tok[0], "options")) {
646 		for (i = 1; i < n; i++) {
647 			if (!strcmp(tok[i], "tcp"))
648 				ac->ac_options |= RES_USEVC;
649 			else if ((!strncmp(tok[i], "ndots:", 6))) {
650 				e = NULL;
651 				d = strtonum(tok[i] + 6, 1, 16, &e);
652 				if (e == NULL)
653 					ac->ac_ndots = d;
654 			}
655 		}
656 	}
657 }
658 
659 /*
660  * Setup an async context with the config specified in the string "str".
661  */
662 static int
663 asr_ctx_from_string(struct asr_ctx *ac, const char *str)
664 {
665 	char		 buf[512], *ch;
666 
667 	asr_ctx_parse(ac, str);
668 
669 	if (ac->ac_dbcount == 0) {
670 		/* No lookup directive */
671 		asr_ctx_parse(ac, DEFAULT_LOOKUP);
672 	}
673 
674 	if (ac->ac_nscount == 0)
675 		asr_ctx_parse(ac, "nameserver 127.0.0.1");
676 
677 	if (ac->ac_domain == NULL)
678 		if (gethostname(buf, sizeof buf) == 0) {
679 			ch = strchr(buf, '.');
680 			if (ch)
681 				ac->ac_domain = strdup(ch + 1);
682 			else /* Assume root. see resolv.conf(5) */
683 				ac->ac_domain = strdup("");
684 		}
685 
686 	/* If no search domain was specified, use the local subdomains */
687 	if (ac->ac_domcount == 0)
688 		for (ch = ac->ac_domain; ch; ) {
689 			asr_ctx_add_searchdomain(ac, ch);
690 			ch = strchr(ch, '.');
691 			if (ch && asr_ndots(++ch) == 0)
692 				break;
693 		}
694 
695 	return (0);
696 }
697 
698 /*
699  * Setup the "ac" async context from the file at location "path".
700  */
701 static int
702 asr_ctx_from_file(struct asr_ctx *ac, const char *path)
703 {
704 	FILE	*cf;
705 	char	 buf[4096];
706 	ssize_t	 r;
707 
708 	cf = fopen(path, "r");
709 	if (cf == NULL)
710 		return (-1);
711 
712 	r = fread(buf, 1, sizeof buf - 1, cf);
713 	if (feof(cf) == 0) {
714 		DPRINT("asr: config file too long: \"%s\"\n", path);
715 		r = -1;
716 	}
717 	fclose(cf);
718 	if (r == -1)
719 		return (-1);
720 	buf[r] = '\0';
721 
722 	return asr_ctx_from_string(ac, buf);
723 }
724 
725 /*
726  * Parse lines in the configuration string. For each one, split it into
727  * tokens and pass them to "pass0" for processing.
728  */
729 static int
730 asr_ctx_parse(struct asr_ctx *ac, const char *str)
731 {
732 	size_t		 len;
733 	const char	*line;
734 	char		 buf[1024];
735 	char		*tok[10];
736 	int		 ntok;
737 
738 	line = str;
739 	while (*line) {
740 		len = strcspn(line, "\n\0");
741 		if (len < sizeof buf) {
742 			memmove(buf, line, len);
743 			buf[len] = '\0';
744 		} else
745 			buf[0] = '\0';
746 		line += len;
747 		if (*line == '\n')
748 			line++;
749 		buf[strcspn(buf, ";#")] = '\0';
750 		if ((ntok = strsplit(buf, tok, 10)) == 0)
751 			continue;
752 
753 		pass0(tok, ntok, ac);
754 	}
755 
756 	return (0);
757 }
758 
759 #if ASR_OPT_ENVOPTS
760 /*
761  * Check for environment variables altering the configuration as described
762  * in resolv.conf(5).  Altough not documented there, this feature is disabled
763  * for setuid/setgid programs.
764  */
765 static void
766 asr_ctx_envopts(struct asr_ctx *ac)
767 {
768 	char	buf[4096], *e;
769 	size_t	s;
770 
771 	if (issetugid()) {
772 		ac->ac_options |= RES_NOALIASES;
773 		return;
774 	}
775 
776 	if ((e = getenv("RES_OPTIONS")) != NULL) {
777 		strlcpy(buf, "options ", sizeof buf);
778 		strlcat(buf, e, sizeof buf);
779 		s = strlcat(buf, "\n", sizeof buf);
780 		s = strlcat(buf, "\n", sizeof buf);
781 		if (s < sizeof buf)
782 			asr_ctx_parse(ac, buf);
783 	}
784 
785 	if ((e = getenv("LOCALDOMAIN")) != NULL) {
786 		strlcpy(buf, "search ", sizeof buf);
787 		strlcat(buf, e, sizeof buf);
788 		s = strlcat(buf, "\n", sizeof buf);
789 		if (s < sizeof buf)
790 			asr_ctx_parse(ac, buf);
791 	}
792 }
793 #endif
794 
795 /*
796  * Parse a resolv.conf(5) nameserver string into a sockaddr.
797  */
798 static int
799 asr_parse_nameserver(struct sockaddr *sa, const char *s)
800 {
801 	const char	*estr;
802 	char		 buf[256];
803 	char		*port = NULL;
804 	in_port_t	 portno = 53;
805 
806 	if (*s == '[') {
807 		strlcpy(buf, s + 1, sizeof buf);
808 		s = buf;
809 		port = strchr(buf, ']');
810 		if (port == NULL)
811 			return (-1);
812 		*port++ = '\0';
813 		if (*port != ':')
814 			return (-1);
815 		port++;
816 	}
817 
818 	if (port) {
819 		portno = strtonum(port, 1, USHRT_MAX, &estr);
820 		if (estr)
821 			return (-1);
822 	}
823 
824 	if (sockaddr_from_str(sa, PF_UNSPEC, s) == -1)
825 		return (-1);
826 
827 	if (sa->sa_family == PF_INET)
828 		((struct sockaddr_in *)sa)->sin_port = htons(portno);
829 	else if (sa->sa_family == PF_INET6)
830 		((struct sockaddr_in6 *)sa)->sin6_port = htons(portno);
831 
832 	return (0);
833 }
834 
835 /*
836  * Turn a (uncompressed) DNS domain name into a regular nul-terminated string
837  * where labels are separated by dots. The result is put into the "buf" buffer,
838  * truncated if it exceeds "max" chars. The function returns "buf".
839  */
840 char *
841 asr_strdname(const char *_dname, char *buf, size_t max)
842 {
843 	const unsigned char *dname = _dname;
844 	char	*res;
845 	size_t	 left, n, count;
846 
847 	if (_dname[0] == 0) {
848 		strlcpy(buf, ".", max);
849 		return buf;
850 	}
851 
852 	res = buf;
853 	left = max - 1;
854 	for (n = 0; dname[0] && left; n += dname[0]) {
855 		count = (dname[0] < (left - 1)) ? dname[0] : (left - 1);
856 		memmove(buf, dname + 1, count);
857 		dname += dname[0] + 1;
858 		left -= count;
859 		buf += count;
860 		if (left) {
861 			left -= 1;
862 			*buf++ = '.';
863 		}
864 	}
865 	buf[0] = 0;
866 
867 	return (res);
868 }
869 
870 /*
871  * Read and split the next line from the given namedb file.
872  * Return -1 on error, or put the result in the "tokens" array of
873  * size "ntoken" and returns the number of token on the line.
874  */
875 int
876 asr_parse_namedb_line(FILE *file, char **tokens, int ntoken)
877 {
878 	size_t	  len;
879 	char	 *buf;
880 	int	  ntok;
881 
882     again:
883 	if ((buf = fgetln(file, &len)) == NULL)
884 		return (-1);
885 
886 	if (buf[len - 1] == '\n')
887 		len--;
888 
889 	buf[len] = '\0';
890 	buf[strcspn(buf, "#")] = '\0';
891 	if ((ntok = strsplit(buf, tokens, ntoken)) == 0)
892 		goto again;
893 
894 	return (ntok);
895 }
896 
897 /*
898  * Update the async context so that it uses the next configured DB.
899  * Return 0 on success, or -1 if no more DBs is available.
900  */
901 int
902 asr_iter_db(struct async *as)
903 {
904 	if (as->as_db_idx >= as->as_ctx->ac_dbcount) {
905 		DPRINT("asr_iter_db: done\n");
906 		return (-1);
907 	}
908 
909 	as->as_db_idx += 1;
910 	as->as_ns_idx = 0;
911 	DPRINT("asr_iter_db: %i\n", as->as_db_idx);
912 
913 	return (0);
914 }
915 
916 /*
917  * Set the async context nameserver index to the next nameserver of the
918  * currently used DB (assuming it is DNS), cycling over the list until the
919  * maximum retry counter is reached.  Return 0 on success, or -1 if all
920  * nameservers were used.
921  */
922 int
923 asr_iter_ns(struct async *as)
924 {
925 	for (;;) {
926 		if (as->as_ns_cycles >= as->as_ctx->ac_nsretries)
927 			return (-1);
928 
929 		as->as_ns_idx += 1;
930 		if (as->as_ns_idx <= as->as_ctx->ac_nscount)
931 			break;
932 		as->as_ns_idx = 0;
933 		as->as_ns_cycles++;
934 		DPRINT("asr: asr_iter_ns(): cycle %i\n", as->as_ns_cycles);
935 	}
936 
937 	as->as_timeout = 1000 * (as->as_ctx->ac_nstimeout << as->as_ns_cycles);
938 	if (as->as_ns_cycles > 0)
939 		as->as_timeout /= as->as_ctx->ac_nscount;
940 	if (as->as_timeout < 1000)
941 		as->as_timeout = 1000;
942 
943 	return (0);
944 }
945 
946 enum {
947 	DOM_INIT,
948 	DOM_DOMAIN,
949 	DOM_DONE
950 };
951 
952 /*
953  * Implement the search domain strategy.
954  *
955  * This function works as a generator that constructs complete domains in
956  * buffer "buf" of size "len" for the given host name "name", according to the
957  * search rules defined by the resolving context.  It is supposed to be called
958  * multiple times (with the same name) to generate the next possible domain
959  * name, if any.
960  *
961  * It returns -1 if all possibilities have been exhausted, 0 if there was an
962  * error generating the next name, or the resulting name length.
963  */
964 int
965 asr_iter_domain(struct async *as, const char *name, char * buf, size_t len)
966 {
967 #if ASR_OPT_HOSTALIASES
968 	char	*alias;
969 #endif
970 
971 	switch (as->as_dom_step) {
972 
973 	case DOM_INIT:
974 		/* First call */
975 
976 		/*
977 		 * If "name" is an FQDN, that's the only result and we
978 		 * don't try anything else.
979 		 */
980 		if (strlen(name) && name[strlen(name) - 1] ==  '.') {
981 			DPRINT("asr: asr_iter_domain(\"%s\") fqdn\n", name);
982 			as->as_dom_flags |= ASYNC_DOM_FQDN;
983 			as->as_dom_step = DOM_DONE;
984 			return (asr_domcat(name, NULL, buf, len));
985 		}
986 
987 #if ASR_OPT_HOSTALIASES
988 		/*
989 		 * If "name" has no dots, it might be an alias. If so,
990 		 * That's also the only result.
991 		 */
992 		if ((as->as_ctx->ac_options & RES_NOALIASES) == 0 &&
993 		    asr_ndots(name) == 0 &&
994 		    (alias = asr_hostalias(name, buf, len)) != NULL) {
995 			DPRINT("asr: asr_iter_domain(\"%s\") is alias \"%s\"\n",
996 			    name, alias);
997 			as->as_dom_flags |= ASYNC_DOM_HOSTALIAS;
998 			as->as_dom_step = DOM_DONE;
999 			return (asr_domcat(alias, NULL, buf, len));
1000 		}
1001 #endif
1002 
1003 		/*
1004 		 * Otherwise, we iterate through the specified search domains.
1005 		 */
1006 		as->as_dom_step = DOM_DOMAIN;
1007 		as->as_dom_idx = 0;
1008 
1009 		/*
1010 		 * If "name" as enough dots, use it as-is first, as indicated
1011 		 * in resolv.conf(5).
1012 		 */
1013 		if ((asr_ndots(name)) >= as->as_ctx->ac_ndots) {
1014 			DPRINT("asr: asr_iter_domain(\"%s\") ndots\n", name);
1015 			as->as_dom_flags |= ASYNC_DOM_NDOTS;
1016 			if (strlcpy(buf, name, len) >= len)
1017 				return (0);
1018 			return (strlen(buf));
1019 		}
1020 		/* Otherwise, starts using the search domains */
1021 		/* FALLTHROUGH */
1022 
1023 	case DOM_DOMAIN:
1024 		if (as->as_dom_idx < as->as_ctx->ac_domcount) {
1025 			DPRINT("asr: asr_iter_domain(\"%s\") domain \"%s\"\n",
1026 			    name, as->as_ctx->ac_dom[as->as_dom_idx]);
1027 			as->as_dom_flags |= ASYNC_DOM_DOMAIN;
1028 			return (asr_domcat(name,
1029 			    as->as_ctx->ac_dom[as->as_dom_idx++], buf, len));
1030 		}
1031 
1032 		/* No more domain to try. */
1033 
1034 		as->as_dom_step = DOM_DONE;
1035 
1036 		/*
1037 		 * If the name was not tried as an absolute name before,
1038 		 * do it now.
1039 		 */
1040 		if (!(as->as_dom_flags & ASYNC_DOM_NDOTS)) {
1041 			DPRINT("asr: asr_iter_domain(\"%s\") as is\n", name);
1042 			as->as_dom_flags |= ASYNC_DOM_ASIS;
1043 			if (strlcpy(buf, name, len) >= len)
1044 				return (0);
1045 			return (strlen(buf));
1046 		}
1047 		/* Otherwise, we are done. */
1048 
1049 	case DOM_DONE:
1050 	default:
1051 		DPRINT("asr: asr_iter_domain(\"%s\") done\n", name);
1052 		return (-1);
1053 	}
1054 }
1055 
1056 #if ASR_OPT_HOSTALIASES
1057 /*
1058  * Check if the hostname "name" is a user-defined alias as per hostname(7).
1059  * If so, copies the result in the buffer "abuf" of size "abufsz" and
1060  * return "abuf". Otherwise return NULL.
1061  */
1062 static char *
1063 asr_hostalias(const char *name, char *abuf, size_t abufsz)
1064 {
1065 	FILE	 *fp;
1066 	size_t	  len;
1067 	char	 *file, *buf, *tokens[2];
1068 	int	  ntok;
1069 
1070 	file = getenv("HOSTALIASES");
1071 	if (file == NULL || issetugid() != 0 || (fp = fopen(file, "r")) == NULL)
1072 		return (NULL);
1073 
1074 	DPRINT("asr: looking up aliases in \"%s\"\n", file);
1075 
1076 	while ((buf = fgetln(fp, &len)) != NULL) {
1077 		if (buf[len - 1] == '\n')
1078 			len--;
1079 		buf[len] = '\0';
1080 		if ((ntok = strsplit(buf, tokens, 2)) != 2)
1081 			continue;
1082 		if (!strcasecmp(tokens[0], name)) {
1083 			if (strlcpy(abuf, tokens[1], abufsz) > abufsz)
1084 				continue;
1085 			DPRINT("asr: found alias \"%s\"\n", abuf);
1086 			fclose(fp);
1087 			return (abuf);
1088 		}
1089 	}
1090 
1091 	fclose(fp);
1092 	return (NULL);
1093 }
1094 #endif
1095