xref: /minix/lib/libc/resolv/res_query.c (revision 0a6a1f1d)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: res_query.c,v 1.16 2015/02/24 17:56:20 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Portions Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
52fe8fb19SBen Gras  * Portions Copyright (C) 1996-2001, 2003  Internet Software Consortium.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Permission to use, copy, modify, and/or distribute this software for any
82fe8fb19SBen Gras  * purpose with or without fee is hereby granted, provided that the above
92fe8fb19SBen Gras  * copyright notice and this permission notice appear in all copies.
102fe8fb19SBen Gras  *
112fe8fb19SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
122fe8fb19SBen Gras  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
132fe8fb19SBen Gras  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
142fe8fb19SBen Gras  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
152fe8fb19SBen Gras  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
162fe8fb19SBen Gras  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
172fe8fb19SBen Gras  * PERFORMANCE OF THIS SOFTWARE.
182fe8fb19SBen Gras  */
192fe8fb19SBen Gras 
202fe8fb19SBen Gras /*
212fe8fb19SBen Gras  * Copyright (c) 1988, 1993
222fe8fb19SBen Gras  *    The Regents of the University of California.  All rights reserved.
232fe8fb19SBen Gras  *
242fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
252fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
262fe8fb19SBen Gras  * are met:
272fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
282fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
292fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
302fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
312fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
32*0a6a1f1dSLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
332fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
342fe8fb19SBen Gras  *    without specific prior written permission.
352fe8fb19SBen Gras  *
362fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
372fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
382fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
392fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
402fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
412fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
422fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
432fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
442fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
452fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
462fe8fb19SBen Gras  * SUCH DAMAGE.
472fe8fb19SBen Gras  */
482fe8fb19SBen Gras 
492fe8fb19SBen Gras /*
502fe8fb19SBen Gras  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
512fe8fb19SBen Gras  *
522fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this software for any
532fe8fb19SBen Gras  * purpose with or without fee is hereby granted, provided that the above
542fe8fb19SBen Gras  * copyright notice and this permission notice appear in all copies, and that
552fe8fb19SBen Gras  * the name of Digital Equipment Corporation not be used in advertising or
562fe8fb19SBen Gras  * publicity pertaining to distribution of the document or software without
572fe8fb19SBen Gras  * specific, written prior permission.
582fe8fb19SBen Gras  *
592fe8fb19SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
602fe8fb19SBen Gras  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
612fe8fb19SBen Gras  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
622fe8fb19SBen Gras  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
632fe8fb19SBen Gras  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
642fe8fb19SBen Gras  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
652fe8fb19SBen Gras  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
662fe8fb19SBen Gras  * SOFTWARE.
672fe8fb19SBen Gras  */
682fe8fb19SBen Gras 
692fe8fb19SBen Gras /*
702fe8fb19SBen Gras  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
712fe8fb19SBen Gras  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
722fe8fb19SBen Gras  *
732fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this software for any
742fe8fb19SBen Gras  * purpose with or without fee is hereby granted, provided that the above
752fe8fb19SBen Gras  * copyright notice and this permission notice appear in all copies.
762fe8fb19SBen Gras  *
772fe8fb19SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
782fe8fb19SBen Gras  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
792fe8fb19SBen Gras  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
802fe8fb19SBen Gras  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
812fe8fb19SBen Gras  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
822fe8fb19SBen Gras  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
832fe8fb19SBen Gras  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
842fe8fb19SBen Gras  */
852fe8fb19SBen Gras 
862fe8fb19SBen Gras #include <sys/cdefs.h>
872fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
882fe8fb19SBen Gras #ifdef notdef
892fe8fb19SBen Gras static const char sccsid[] = "@(#)res_query.c	8.1 (Berkeley) 6/4/93";
902fe8fb19SBen Gras static const char rcsid[] = "Id: res_query.c,v 1.11 2008/11/14 02:36:51 marka Exp";
912fe8fb19SBen Gras #else
92*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: res_query.c,v 1.16 2015/02/24 17:56:20 christos Exp $");
932fe8fb19SBen Gras #endif
942fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
952fe8fb19SBen Gras 
962fe8fb19SBen Gras #include "port_before.h"
972fe8fb19SBen Gras 
982fe8fb19SBen Gras #include "namespace.h"
992fe8fb19SBen Gras #include <sys/types.h>
1002fe8fb19SBen Gras #include <sys/param.h>
1012fe8fb19SBen Gras #include <netinet/in.h>
1022fe8fb19SBen Gras #include <arpa/inet.h>
1032fe8fb19SBen Gras #include <arpa/nameser.h>
1042fe8fb19SBen Gras #include <ctype.h>
1052fe8fb19SBen Gras #include <errno.h>
1062fe8fb19SBen Gras #include <netdb.h>
1072fe8fb19SBen Gras #include <resolv.h>
1082fe8fb19SBen Gras #include <stdio.h>
1092fe8fb19SBen Gras #include <stdlib.h>
1102fe8fb19SBen Gras #include <unistd.h>
1112fe8fb19SBen Gras #include <string.h>
1122fe8fb19SBen Gras #include "port_after.h"
1132fe8fb19SBen Gras 
1142fe8fb19SBen Gras #if 0
1152fe8fb19SBen Gras #ifdef __weak_alias
1162fe8fb19SBen Gras __weak_alias(res_nquery,_res_nquery)
1172fe8fb19SBen Gras __weak_alias(res_nsearch,_res_nsearch)
1182fe8fb19SBen Gras __weak_alias(res_nquerydomain,__res_nquerydomain)
1192fe8fb19SBen Gras __weak_alias(res_hostalias,__res_hostalias)
1202fe8fb19SBen Gras #endif
1212fe8fb19SBen Gras #endif
1222fe8fb19SBen Gras 
1232fe8fb19SBen Gras /* Options.  Leave them on. */
1242fe8fb19SBen Gras #ifndef DEBUG
1252fe8fb19SBen Gras #define DEBUG
1262fe8fb19SBen Gras #endif
1272fe8fb19SBen Gras 
1282fe8fb19SBen Gras #if PACKETSZ > 1024
1292fe8fb19SBen Gras #define MAXPACKET	PACKETSZ
1302fe8fb19SBen Gras #else
1312fe8fb19SBen Gras #define MAXPACKET	1024
1322fe8fb19SBen Gras #endif
1332fe8fb19SBen Gras 
1342fe8fb19SBen Gras /*%
1352fe8fb19SBen Gras  * Formulate a normal query, send, and await answer.
1362fe8fb19SBen Gras  * Returned answer is placed in supplied buffer "answer".
1372fe8fb19SBen Gras  * Perform preliminary check of answer, returning success only
1382fe8fb19SBen Gras  * if no error is indicated and the answer count is nonzero.
1392fe8fb19SBen Gras  * Return the size of the response on success, -1 on error.
1402fe8fb19SBen Gras  * Error number is left in H_ERRNO.
1412fe8fb19SBen Gras  *
1422fe8fb19SBen Gras  * Caller must parse answer and determine whether it answers the question.
1432fe8fb19SBen Gras  */
1442fe8fb19SBen Gras int
res_nquery(res_state statp,const char * name,int class,int type,u_char * answer,int anslen)1452fe8fb19SBen Gras res_nquery(res_state statp,
1462fe8fb19SBen Gras 	   const char *name,	/*%< domain name */
1472fe8fb19SBen Gras 	   int class, int type,	/*%< class and type of query */
1482fe8fb19SBen Gras 	   u_char *answer,	/*%< buffer to put answer */
1492fe8fb19SBen Gras 	   int anslen)		/*%< size of answer buffer */
1502fe8fb19SBen Gras {
1512fe8fb19SBen Gras 	u_char buf[MAXPACKET];
1522fe8fb19SBen Gras 	HEADER *hp = (HEADER *)(void *)answer;
1532fe8fb19SBen Gras 	u_int oflags;
1542fe8fb19SBen Gras 	u_char *rdata;
1552fe8fb19SBen Gras 	int n;
1562fe8fb19SBen Gras 
1572fe8fb19SBen Gras 	oflags = statp->_flags;
1582fe8fb19SBen Gras 
1592fe8fb19SBen Gras again:
1602fe8fb19SBen Gras 	hp->rcode = NOERROR;	/*%< default */
1612fe8fb19SBen Gras #ifdef DEBUG
1622fe8fb19SBen Gras 	if (statp->options & RES_DEBUG)
1632fe8fb19SBen Gras 		printf(";; res_query(%s, %d, %d)\n", name, class, type);
1642fe8fb19SBen Gras #endif
1652fe8fb19SBen Gras 
1662fe8fb19SBen Gras 	n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
167f14fb602SLionel Sambuc 			 buf, (int)sizeof(buf));
1682fe8fb19SBen Gras #ifdef RES_USE_EDNS0
1692fe8fb19SBen Gras 	if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 &&
1702fe8fb19SBen Gras 	    (statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC|RES_NSID)) != 0U) {
171f14fb602SLionel Sambuc 		n = res_nopt(statp, n, buf, (int)sizeof(buf), anslen);
1722fe8fb19SBen Gras 		rdata = &buf[n];
1732fe8fb19SBen Gras 		if (n > 0 && (statp->options & RES_NSID) != 0U) {
174f14fb602SLionel Sambuc 			n = res_nopt_rdata(statp, n, buf, (int)sizeof(buf),
175f14fb602SLionel Sambuc 			    rdata, NS_OPT_NSID, 0, NULL);
1762fe8fb19SBen Gras 		}
1772fe8fb19SBen Gras 	}
1782fe8fb19SBen Gras #endif
1792fe8fb19SBen Gras 	if (n <= 0) {
1802fe8fb19SBen Gras #ifdef DEBUG
1812fe8fb19SBen Gras 		if (statp->options & RES_DEBUG)
1822fe8fb19SBen Gras 			printf(";; res_query: mkquery failed\n");
1832fe8fb19SBen Gras #endif
1842fe8fb19SBen Gras 		RES_SET_H_ERRNO(statp, NO_RECOVERY);
1852fe8fb19SBen Gras 		return (n);
1862fe8fb19SBen Gras 	}
1872fe8fb19SBen Gras 
1882fe8fb19SBen Gras 	n = res_nsend(statp, buf, n, answer, anslen);
1892fe8fb19SBen Gras 	if (n < 0) {
1902fe8fb19SBen Gras #ifdef RES_USE_EDNS0
1912fe8fb19SBen Gras 		/* if the query choked with EDNS0, retry without EDNS0 */
1922fe8fb19SBen Gras 		if ((statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U &&
1932fe8fb19SBen Gras 		    ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
1942fe8fb19SBen Gras 			statp->_flags |= RES_F_EDNS0ERR;
1952fe8fb19SBen Gras 			if (statp->options & RES_DEBUG)
1962fe8fb19SBen Gras 				printf(";; res_nquery: retry without EDNS0\n");
1972fe8fb19SBen Gras 			goto again;
1982fe8fb19SBen Gras 		}
1992fe8fb19SBen Gras #endif
2002fe8fb19SBen Gras #ifdef DEBUG
2012fe8fb19SBen Gras 		if (statp->options & RES_DEBUG)
2022fe8fb19SBen Gras 			printf(";; res_query: send error\n");
2032fe8fb19SBen Gras #endif
2042fe8fb19SBen Gras 		RES_SET_H_ERRNO(statp, TRY_AGAIN);
2052fe8fb19SBen Gras 		return (n);
2062fe8fb19SBen Gras 	}
2072fe8fb19SBen Gras 
2082fe8fb19SBen Gras 	if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
2092fe8fb19SBen Gras #ifdef DEBUG
2102fe8fb19SBen Gras 		if (statp->options & RES_DEBUG)
2112fe8fb19SBen Gras 			printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n",
2122fe8fb19SBen Gras 			       p_rcode(hp->rcode),
2132fe8fb19SBen Gras 			       ntohs(hp->ancount),
2142fe8fb19SBen Gras 			       ntohs(hp->nscount),
2152fe8fb19SBen Gras 			       ntohs(hp->arcount));
2162fe8fb19SBen Gras #endif
2172fe8fb19SBen Gras 		switch (hp->rcode) {
2182fe8fb19SBen Gras 		case NXDOMAIN:
2192fe8fb19SBen Gras 			RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
2202fe8fb19SBen Gras 			break;
2212fe8fb19SBen Gras 		case SERVFAIL:
2222fe8fb19SBen Gras 			RES_SET_H_ERRNO(statp, TRY_AGAIN);
2232fe8fb19SBen Gras 			break;
2242fe8fb19SBen Gras 		case NOERROR:
2252fe8fb19SBen Gras 			RES_SET_H_ERRNO(statp, NO_DATA);
2262fe8fb19SBen Gras 			break;
2272fe8fb19SBen Gras 		case FORMERR:
2282fe8fb19SBen Gras 		case NOTIMP:
2292fe8fb19SBen Gras 		case REFUSED:
2302fe8fb19SBen Gras 		default:
2312fe8fb19SBen Gras 			RES_SET_H_ERRNO(statp, NO_RECOVERY);
2322fe8fb19SBen Gras 			break;
2332fe8fb19SBen Gras 		}
2342fe8fb19SBen Gras 		return (-1);
2352fe8fb19SBen Gras 	}
2362fe8fb19SBen Gras 	return (n);
2372fe8fb19SBen Gras }
2382fe8fb19SBen Gras 
2392fe8fb19SBen Gras /*%
2402fe8fb19SBen Gras  * Formulate a normal query, send, and retrieve answer in supplied buffer.
2412fe8fb19SBen Gras  * Return the size of the response on success, -1 on error.
2422fe8fb19SBen Gras  * If enabled, implement search rules until answer or unrecoverable failure
2432fe8fb19SBen Gras  * is detected.  Error code, if any, is left in H_ERRNO.
2442fe8fb19SBen Gras  */
2452fe8fb19SBen Gras int
res_nsearch(res_state statp,const char * name,int class,int type,u_char * answer,int anslen)2462fe8fb19SBen Gras res_nsearch(res_state statp,
2472fe8fb19SBen Gras 	    const char *name,	/*%< domain name */
2482fe8fb19SBen Gras 	    int class, int type,	/*%< class and type of query */
2492fe8fb19SBen Gras 	    u_char *answer,	/*%< buffer to put answer */
2502fe8fb19SBen Gras 	    int anslen)		/*%< size of answer */
2512fe8fb19SBen Gras {
2522fe8fb19SBen Gras 	const char *cp, * const *domain;
2532fe8fb19SBen Gras 	HEADER *hp = (HEADER *)(void *)answer;
2542fe8fb19SBen Gras 	char tmp[NS_MAXDNAME];
2552fe8fb19SBen Gras 	u_int dots;
2562fe8fb19SBen Gras 	int trailing_dot, ret, saved_herrno;
2572fe8fb19SBen Gras 	int got_nodata = 0, got_servfail = 0, root_on_list = 0;
2582fe8fb19SBen Gras 	int tried_as_is = 0;
2592fe8fb19SBen Gras 	int searched = 0;
2602fe8fb19SBen Gras 
2612fe8fb19SBen Gras 	errno = 0;
2622fe8fb19SBen Gras 	RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);  /*%< True if we never query. */
2632fe8fb19SBen Gras 	dots = 0;
2642fe8fb19SBen Gras 	for (cp = name; *cp != '\0'; cp++)
2652fe8fb19SBen Gras 		dots += (*cp == '.');
2662fe8fb19SBen Gras 	trailing_dot = 0;
2672fe8fb19SBen Gras 	if (cp > name && *--cp == '.')
2682fe8fb19SBen Gras 		trailing_dot++;
2692fe8fb19SBen Gras 
2702fe8fb19SBen Gras 	/* If there aren't any dots, it could be a user-level alias. */
2712fe8fb19SBen Gras 	if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
2722fe8fb19SBen Gras 		return (res_nquery(statp, cp, class, type, answer, anslen));
2732fe8fb19SBen Gras 
2742fe8fb19SBen Gras 	/*
2752fe8fb19SBen Gras 	 * If there are enough dots in the name, let's just give it a
2762fe8fb19SBen Gras 	 * try 'as is'. The threshold can be set with the "ndots" option.
2772fe8fb19SBen Gras 	 * Also, query 'as is', if there is a trailing dot in the name.
2782fe8fb19SBen Gras 	 */
2792fe8fb19SBen Gras 	saved_herrno = -1;
2802fe8fb19SBen Gras 	if (dots >= statp->ndots || trailing_dot) {
2812fe8fb19SBen Gras 		ret = res_nquerydomain(statp, name, NULL, class, type,
2822fe8fb19SBen Gras 					 answer, anslen);
2832fe8fb19SBen Gras 		if (ret > 0 || trailing_dot)
2842fe8fb19SBen Gras 			return (ret);
2852fe8fb19SBen Gras 		saved_herrno = statp->res_h_errno;
2862fe8fb19SBen Gras 		tried_as_is++;
2872fe8fb19SBen Gras 	}
2882fe8fb19SBen Gras 
2892fe8fb19SBen Gras 	/*
2902fe8fb19SBen Gras 	 * We do at least one level of search if
2912fe8fb19SBen Gras 	 *	- there is no dot and RES_DEFNAME is set, or
2922fe8fb19SBen Gras 	 *	- there is at least one dot, there is no trailing dot,
2932fe8fb19SBen Gras 	 *	  and RES_DNSRCH is set.
2942fe8fb19SBen Gras 	 */
2952fe8fb19SBen Gras 	if ((!dots && (statp->options & RES_DEFNAMES) != 0U) ||
2962fe8fb19SBen Gras 	    (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0U)) {
2972fe8fb19SBen Gras 		int done = 0;
2982fe8fb19SBen Gras 
2992fe8fb19SBen Gras 		for (domain = (const char * const *)statp->dnsrch;
3002fe8fb19SBen Gras 		     *domain && !done;
3012fe8fb19SBen Gras 		     domain++) {
3022fe8fb19SBen Gras 			searched = 1;
3032fe8fb19SBen Gras 
3042fe8fb19SBen Gras 			if (domain[0][0] == '\0' ||
3052fe8fb19SBen Gras 			    (domain[0][0] == '.' && domain[0][1] == '\0'))
3062fe8fb19SBen Gras 				root_on_list++;
3072fe8fb19SBen Gras 
3082fe8fb19SBen Gras 			ret = res_nquerydomain(statp, name, *domain,
3092fe8fb19SBen Gras 					       class, type,
3102fe8fb19SBen Gras 					       answer, anslen);
3112fe8fb19SBen Gras 			if (ret > 0)
3122fe8fb19SBen Gras 				return (ret);
3132fe8fb19SBen Gras 
3142fe8fb19SBen Gras 			/*
3152fe8fb19SBen Gras 			 * If no server present, give up.
3162fe8fb19SBen Gras 			 * If name isn't found in this domain,
3172fe8fb19SBen Gras 			 * keep trying higher domains in the search list
3182fe8fb19SBen Gras 			 * (if that's enabled).
3192fe8fb19SBen Gras 			 * On a NO_DATA error, keep trying, otherwise
3202fe8fb19SBen Gras 			 * a wildcard entry of another type could keep us
3212fe8fb19SBen Gras 			 * from finding this entry higher in the domain.
3222fe8fb19SBen Gras 			 * If we get some other error (negative answer or
3232fe8fb19SBen Gras 			 * server failure), then stop searching up,
3242fe8fb19SBen Gras 			 * but try the input name below in case it's
3252fe8fb19SBen Gras 			 * fully-qualified.
3262fe8fb19SBen Gras 			 */
3272fe8fb19SBen Gras 			if (errno == ECONNREFUSED) {
3282fe8fb19SBen Gras 				RES_SET_H_ERRNO(statp, TRY_AGAIN);
3292fe8fb19SBen Gras 				return (-1);
3302fe8fb19SBen Gras 			}
3312fe8fb19SBen Gras 
3322fe8fb19SBen Gras 			switch (statp->res_h_errno) {
3332fe8fb19SBen Gras 			case NO_DATA:
3342fe8fb19SBen Gras 				got_nodata++;
3352fe8fb19SBen Gras 				/* FALLTHROUGH */
3362fe8fb19SBen Gras 			case HOST_NOT_FOUND:
3372fe8fb19SBen Gras 				/* keep trying */
3382fe8fb19SBen Gras 				break;
3392fe8fb19SBen Gras 			case TRY_AGAIN:
3402fe8fb19SBen Gras 				if (hp->rcode == SERVFAIL) {
3412fe8fb19SBen Gras 					/* try next search element, if any */
3422fe8fb19SBen Gras 					got_servfail++;
3432fe8fb19SBen Gras 					break;
3442fe8fb19SBen Gras 				}
3452fe8fb19SBen Gras 				/* FALLTHROUGH */
3462fe8fb19SBen Gras 			default:
3472fe8fb19SBen Gras 				/* anything else implies that we're done */
3482fe8fb19SBen Gras 				done++;
3492fe8fb19SBen Gras 			}
3502fe8fb19SBen Gras 
3512fe8fb19SBen Gras 			/* if we got here for some reason other than DNSRCH,
3522fe8fb19SBen Gras 			 * we only wanted one iteration of the loop, so stop.
3532fe8fb19SBen Gras 			 */
3542fe8fb19SBen Gras 			if ((statp->options & RES_DNSRCH) == 0U)
3552fe8fb19SBen Gras 				done++;
3562fe8fb19SBen Gras 		}
3572fe8fb19SBen Gras 	}
3582fe8fb19SBen Gras 
3592fe8fb19SBen Gras 	/*
3602fe8fb19SBen Gras 	 * If the query has not already been tried as is then try it
3612fe8fb19SBen Gras 	 * unless RES_NOTLDQUERY is set and there were no dots.
3622fe8fb19SBen Gras 	 */
3632fe8fb19SBen Gras 	if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) &&
3642fe8fb19SBen Gras 	    !(tried_as_is || root_on_list)) {
3652fe8fb19SBen Gras 		ret = res_nquerydomain(statp, name, NULL, class, type,
3662fe8fb19SBen Gras 				       answer, anslen);
3672fe8fb19SBen Gras 		if (ret > 0)
3682fe8fb19SBen Gras 			return (ret);
3692fe8fb19SBen Gras 	}
3702fe8fb19SBen Gras 
3712fe8fb19SBen Gras 	/* if we got here, we didn't satisfy the search.
3722fe8fb19SBen Gras 	 * if we did an initial full query, return that query's H_ERRNO
3732fe8fb19SBen Gras 	 * (note that we wouldn't be here if that query had succeeded).
3742fe8fb19SBen Gras 	 * else if we ever got a nodata, send that back as the reason.
3752fe8fb19SBen Gras 	 * else send back meaningless H_ERRNO, that being the one from
3762fe8fb19SBen Gras 	 * the last DNSRCH we did.
3772fe8fb19SBen Gras 	 */
3782fe8fb19SBen Gras 	if (saved_herrno != -1)
3792fe8fb19SBen Gras 		RES_SET_H_ERRNO(statp, saved_herrno);
3802fe8fb19SBen Gras 	else if (got_nodata)
3812fe8fb19SBen Gras 		RES_SET_H_ERRNO(statp, NO_DATA);
3822fe8fb19SBen Gras 	else if (got_servfail)
3832fe8fb19SBen Gras 		RES_SET_H_ERRNO(statp, TRY_AGAIN);
3842fe8fb19SBen Gras 	return (-1);
3852fe8fb19SBen Gras }
3862fe8fb19SBen Gras 
3872fe8fb19SBen Gras /*%
3882fe8fb19SBen Gras  * Perform a call on res_query on the concatenation of name and domain,
3892fe8fb19SBen Gras  * removing a trailing dot from name if domain is NULL.
3902fe8fb19SBen Gras  */
3912fe8fb19SBen Gras int
res_nquerydomain(res_state statp,const char * name,const char * domain,int class,int type,u_char * answer,int anslen)3922fe8fb19SBen Gras res_nquerydomain(res_state statp,
3932fe8fb19SBen Gras 	    const char *name,
3942fe8fb19SBen Gras 	    const char *domain,
3952fe8fb19SBen Gras 	    int class, int type,	/*%< class and type of query */
3962fe8fb19SBen Gras 	    u_char *answer,		/*%< buffer to put answer */
3972fe8fb19SBen Gras 	    int anslen)		/*%< size of answer */
3982fe8fb19SBen Gras {
3992fe8fb19SBen Gras 	char nbuf[MAXDNAME];
4002fe8fb19SBen Gras 	const char *longname = nbuf;
401f14fb602SLionel Sambuc 	size_t n, d;
4022fe8fb19SBen Gras 
4032fe8fb19SBen Gras #ifdef DEBUG
4042fe8fb19SBen Gras 	if (statp->options & RES_DEBUG)
4052fe8fb19SBen Gras 		printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
4062fe8fb19SBen Gras 		       name, domain?domain:"<Nil>", class, type);
4072fe8fb19SBen Gras #endif
4082fe8fb19SBen Gras 	if (domain == NULL) {
4092fe8fb19SBen Gras 		/*
4102fe8fb19SBen Gras 		 * Check for trailing '.';
4112fe8fb19SBen Gras 		 * copy without '.' if present.
4122fe8fb19SBen Gras 		 */
4132fe8fb19SBen Gras 		n = strlen(name);
4142fe8fb19SBen Gras 		if (n >= MAXDNAME) {
4152fe8fb19SBen Gras 			RES_SET_H_ERRNO(statp, NO_RECOVERY);
4162fe8fb19SBen Gras 			return (-1);
4172fe8fb19SBen Gras 		}
418f14fb602SLionel Sambuc 		if (n && name[--n] == '.') {
419f14fb602SLionel Sambuc 			strncpy(nbuf, name, n);
4202fe8fb19SBen Gras 			nbuf[n] = '\0';
4212fe8fb19SBen Gras 		} else
4222fe8fb19SBen Gras 			longname = name;
4232fe8fb19SBen Gras 	} else {
4242fe8fb19SBen Gras 		n = strlen(name);
4252fe8fb19SBen Gras 		d = strlen(domain);
4262fe8fb19SBen Gras 		if (n + d + 1 >= MAXDNAME) {
4272fe8fb19SBen Gras 			RES_SET_H_ERRNO(statp, NO_RECOVERY);
4282fe8fb19SBen Gras 			return (-1);
4292fe8fb19SBen Gras 		}
4302fe8fb19SBen Gras 		sprintf(nbuf, "%s.%s", name, domain);
4312fe8fb19SBen Gras 	}
4322fe8fb19SBen Gras 	return (res_nquery(statp, longname, class, type, answer, anslen));
4332fe8fb19SBen Gras }
4342fe8fb19SBen Gras 
4352fe8fb19SBen Gras const char *
res_hostalias(const res_state statp,const char * name,char * dst,size_t siz)4362fe8fb19SBen Gras res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
4372fe8fb19SBen Gras 	char *file, *cp1, *cp2;
4382fe8fb19SBen Gras 	char buf[BUFSIZ];
4392fe8fb19SBen Gras 	FILE *fp;
4402fe8fb19SBen Gras 
4412fe8fb19SBen Gras 	if (statp->options & RES_NOALIASES)
4422fe8fb19SBen Gras 		return (NULL);
4432fe8fb19SBen Gras 	/*
4442fe8fb19SBen Gras 	 * forbid hostaliases for setuid binary, due to possible security
4452fe8fb19SBen Gras 	 * breach.
4462fe8fb19SBen Gras 	 */
4472fe8fb19SBen Gras 	if (issetugid())
4482fe8fb19SBen Gras 		return (NULL);
4492fe8fb19SBen Gras 	file = getenv("HOSTALIASES");
450*0a6a1f1dSLionel Sambuc 	if (file == NULL || (fp = fopen(file, "re")) == NULL)
4512fe8fb19SBen Gras 		return (NULL);
4522fe8fb19SBen Gras 	buf[sizeof(buf) - 1] = '\0';
453f14fb602SLionel Sambuc 	while (fgets(buf, (int)sizeof(buf), fp)) {
4542fe8fb19SBen Gras 		for (cp1 = buf; *cp1 && !isspace((unsigned char)*cp1); ++cp1)
4552fe8fb19SBen Gras 			;
4562fe8fb19SBen Gras 		if (!*cp1)
4572fe8fb19SBen Gras 			break;
4582fe8fb19SBen Gras 		*cp1 = '\0';
4592fe8fb19SBen Gras 		if (ns_samename(buf, name) == 1) {
4602fe8fb19SBen Gras 			while (isspace((unsigned char)*++cp1))
4612fe8fb19SBen Gras 				;
4622fe8fb19SBen Gras 			if (!*cp1)
4632fe8fb19SBen Gras 				break;
4642fe8fb19SBen Gras 			for (cp2 = cp1 + 1; *cp2 &&
4652fe8fb19SBen Gras 			     !isspace((unsigned char)*cp2); ++cp2)
4662fe8fb19SBen Gras 				;
4672fe8fb19SBen Gras 			*cp2 = '\0';
4682fe8fb19SBen Gras 			strncpy(dst, cp1, siz - 1);
4692fe8fb19SBen Gras 			dst[siz - 1] = '\0';
4702fe8fb19SBen Gras 			fclose(fp);
4712fe8fb19SBen Gras 			return (dst);
4722fe8fb19SBen Gras 		}
4732fe8fb19SBen Gras 	}
4742fe8fb19SBen Gras 	fclose(fp);
4752fe8fb19SBen Gras 	return (NULL);
4762fe8fb19SBen Gras }
4772fe8fb19SBen Gras 
4782fe8fb19SBen Gras /*! \file */
479