Home | History | Annotate | Line # | Download | only in resolv
res_query.c revision 1.11.6.2
      1  1.11.6.2  christos /*	$NetBSD: res_query.c,v 1.11.6.2 2008/06/21 23:37:54 christos Exp $	*/
      2  1.11.6.2  christos 
      3  1.11.6.2  christos /*
      4  1.11.6.2  christos  * Copyright (c) 1988, 1993
      5  1.11.6.2  christos  *    The Regents of the University of California.  All rights reserved.
      6  1.11.6.2  christos  *
      7  1.11.6.2  christos  * Redistribution and use in source and binary forms, with or without
      8  1.11.6.2  christos  * modification, are permitted provided that the following conditions
      9  1.11.6.2  christos  * are met:
     10  1.11.6.2  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.11.6.2  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.11.6.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.11.6.2  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.11.6.2  christos  *    documentation and/or other materials provided with the distribution.
     15  1.11.6.2  christos  * 3. All advertising materials mentioning features or use of this software
     16  1.11.6.2  christos  *    must display the following acknowledgement:
     17  1.11.6.2  christos  * 	This product includes software developed by the University of
     18  1.11.6.2  christos  * 	California, Berkeley and its contributors.
     19  1.11.6.2  christos  * 4. Neither the name of the University nor the names of its contributors
     20  1.11.6.2  christos  *    may be used to endorse or promote products derived from this software
     21  1.11.6.2  christos  *    without specific prior written permission.
     22  1.11.6.2  christos  *
     23  1.11.6.2  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.11.6.2  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.11.6.2  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.11.6.2  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.11.6.2  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.11.6.2  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.11.6.2  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.11.6.2  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.11.6.2  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.11.6.2  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.11.6.2  christos  * SUCH DAMAGE.
     34  1.11.6.2  christos  */
     35  1.11.6.2  christos 
     36  1.11.6.2  christos /*
     37  1.11.6.2  christos  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
     38  1.11.6.2  christos  *
     39  1.11.6.2  christos  * Permission to use, copy, modify, and distribute this software for any
     40  1.11.6.2  christos  * purpose with or without fee is hereby granted, provided that the above
     41  1.11.6.2  christos  * copyright notice and this permission notice appear in all copies, and that
     42  1.11.6.2  christos  * the name of Digital Equipment Corporation not be used in advertising or
     43  1.11.6.2  christos  * publicity pertaining to distribution of the document or software without
     44  1.11.6.2  christos  * specific, written prior permission.
     45  1.11.6.2  christos  *
     46  1.11.6.2  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
     47  1.11.6.2  christos  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
     48  1.11.6.2  christos  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
     49  1.11.6.2  christos  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
     50  1.11.6.2  christos  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
     51  1.11.6.2  christos  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
     52  1.11.6.2  christos  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
     53  1.11.6.2  christos  * SOFTWARE.
     54  1.11.6.2  christos  */
     55  1.11.6.2  christos 
     56  1.11.6.2  christos /*
     57  1.11.6.2  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
     58  1.11.6.2  christos  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
     59  1.11.6.2  christos  *
     60  1.11.6.2  christos  * Permission to use, copy, modify, and distribute this software for any
     61  1.11.6.2  christos  * purpose with or without fee is hereby granted, provided that the above
     62  1.11.6.2  christos  * copyright notice and this permission notice appear in all copies.
     63  1.11.6.2  christos  *
     64  1.11.6.2  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     65  1.11.6.2  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     66  1.11.6.2  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     67  1.11.6.2  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     68  1.11.6.2  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     69  1.11.6.2  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     70  1.11.6.2  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     71  1.11.6.2  christos  */
     72  1.11.6.2  christos 
     73  1.11.6.2  christos #include <sys/cdefs.h>
     74  1.11.6.2  christos #if defined(LIBC_SCCS) && !defined(lint)
     75  1.11.6.2  christos #ifdef notdef
     76  1.11.6.2  christos static const char sccsid[] = "@(#)res_query.c	8.1 (Berkeley) 6/4/93";
     77  1.11.6.2  christos static const char rcsid[] = "Id: res_query.c,v 1.8.672.2 2008/04/03 10:49:22 marka Exp";
     78  1.11.6.2  christos #else
     79  1.11.6.2  christos __RCSID("$NetBSD: res_query.c,v 1.11.6.2 2008/06/21 23:37:54 christos Exp $");
     80  1.11.6.2  christos #endif
     81  1.11.6.2  christos #endif /* LIBC_SCCS and not lint */
     82  1.11.6.2  christos 
     83  1.11.6.2  christos #include "port_before.h"
     84  1.11.6.2  christos 
     85  1.11.6.2  christos #include "namespace.h"
     86  1.11.6.2  christos #include <sys/types.h>
     87  1.11.6.2  christos #include <sys/param.h>
     88  1.11.6.2  christos #include <netinet/in.h>
     89  1.11.6.2  christos #include <arpa/inet.h>
     90  1.11.6.2  christos #include <arpa/nameser.h>
     91  1.11.6.2  christos #include <ctype.h>
     92  1.11.6.2  christos #include <errno.h>
     93  1.11.6.2  christos #include <netdb.h>
     94  1.11.6.2  christos #include <resolv.h>
     95  1.11.6.2  christos #include <stdio.h>
     96  1.11.6.2  christos #include <stdlib.h>
     97  1.11.6.2  christos #include <unistd.h>
     98  1.11.6.2  christos #include <string.h>
     99  1.11.6.2  christos #include "port_after.h"
    100  1.11.6.2  christos 
    101  1.11.6.2  christos #if 0
    102  1.11.6.2  christos #ifdef __weak_alias
    103  1.11.6.2  christos __weak_alias(res_nquery,_res_nquery)
    104  1.11.6.2  christos __weak_alias(res_nsearch,_res_nsearch)
    105  1.11.6.2  christos __weak_alias(res_nquerydomain,__res_nquerydomain)
    106  1.11.6.2  christos __weak_alias(res_hostalias,__res_hostalias)
    107  1.11.6.2  christos #endif
    108  1.11.6.2  christos #endif
    109  1.11.6.2  christos 
    110  1.11.6.2  christos /* Options.  Leave them on. */
    111  1.11.6.2  christos #ifndef DEBUG
    112  1.11.6.2  christos #define DEBUG
    113  1.11.6.2  christos #endif
    114  1.11.6.2  christos 
    115  1.11.6.2  christos #if PACKETSZ > 1024
    116  1.11.6.2  christos #define MAXPACKET	PACKETSZ
    117  1.11.6.2  christos #else
    118  1.11.6.2  christos #define MAXPACKET	1024
    119  1.11.6.2  christos #endif
    120  1.11.6.2  christos 
    121  1.11.6.2  christos /*%
    122  1.11.6.2  christos  * Formulate a normal query, send, and await answer.
    123  1.11.6.2  christos  * Returned answer is placed in supplied buffer "answer".
    124  1.11.6.2  christos  * Perform preliminary check of answer, returning success only
    125  1.11.6.2  christos  * if no error is indicated and the answer count is nonzero.
    126  1.11.6.2  christos  * Return the size of the response on success, -1 on error.
    127  1.11.6.2  christos  * Error number is left in H_ERRNO.
    128  1.11.6.2  christos  *
    129  1.11.6.2  christos  * Caller must parse answer and determine whether it answers the question.
    130  1.11.6.2  christos  */
    131  1.11.6.2  christos int
    132  1.11.6.2  christos res_nquery(res_state statp,
    133  1.11.6.2  christos 	   const char *name,	/*%< domain name */
    134  1.11.6.2  christos 	   int class, int type,	/*%< class and type of query */
    135  1.11.6.2  christos 	   u_char *answer,	/*%< buffer to put answer */
    136  1.11.6.2  christos 	   int anslen)		/*%< size of answer buffer */
    137  1.11.6.2  christos {
    138  1.11.6.2  christos 	u_char buf[MAXPACKET];
    139  1.11.6.2  christos 	HEADER *hp = (HEADER *)(void *)answer;
    140  1.11.6.2  christos 	int n;
    141  1.11.6.2  christos 	u_int oflags;
    142  1.11.6.2  christos 	u_char *rdata;
    143  1.11.6.2  christos 
    144  1.11.6.2  christos 	oflags = statp->_flags;
    145  1.11.6.2  christos 
    146  1.11.6.2  christos again:
    147  1.11.6.2  christos 	hp->rcode = NOERROR;	/*%< default */
    148  1.11.6.2  christos #ifdef DEBUG
    149  1.11.6.2  christos 	if (statp->options & RES_DEBUG)
    150  1.11.6.2  christos 		printf(";; res_query(%s, %d, %d)\n", name, class, type);
    151  1.11.6.2  christos #endif
    152  1.11.6.2  christos 
    153  1.11.6.2  christos 	n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL,
    154  1.11.6.2  christos 			 buf, sizeof(buf));
    155  1.11.6.2  christos #ifdef RES_USE_EDNS0
    156  1.11.6.2  christos 	if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 &&
    157  1.11.6.2  christos 	    (statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC|RES_NSID)) != 0U) {
    158  1.11.6.2  christos 		n = res_nopt(statp, n, buf, sizeof(buf), anslen);
    159  1.11.6.2  christos 		rdata = &buf[n];
    160  1.11.6.2  christos 		if (n > 0 && (statp->options & RES_NSID) != 0U) {
    161  1.11.6.2  christos 			n = res_nopt_rdata(statp, n, buf, sizeof(buf), rdata,
    162  1.11.6.2  christos 					   NS_OPT_NSID, 0, NULL);
    163  1.11.6.2  christos 		}
    164  1.11.6.2  christos 	}
    165  1.11.6.2  christos #endif
    166  1.11.6.2  christos 	if (n <= 0) {
    167  1.11.6.2  christos #ifdef DEBUG
    168  1.11.6.2  christos 		if (statp->options & RES_DEBUG)
    169  1.11.6.2  christos 			printf(";; res_query: mkquery failed\n");
    170  1.11.6.2  christos #endif
    171  1.11.6.2  christos 		RES_SET_H_ERRNO(statp, NO_RECOVERY);
    172  1.11.6.2  christos 		return (n);
    173  1.11.6.2  christos 	}
    174  1.11.6.2  christos 
    175  1.11.6.2  christos 	n = res_nsend(statp, buf, n, answer, anslen);
    176  1.11.6.2  christos 	if (n < 0) {
    177  1.11.6.2  christos #ifdef RES_USE_EDNS0
    178  1.11.6.2  christos 		/* if the query choked with EDNS0, retry without EDNS0 */
    179  1.11.6.2  christos 		if ((statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U &&
    180  1.11.6.2  christos 		    ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
    181  1.11.6.2  christos 			statp->_flags |= RES_F_EDNS0ERR;
    182  1.11.6.2  christos 			if (statp->options & RES_DEBUG)
    183  1.11.6.2  christos 				printf(";; res_nquery: retry without EDNS0\n");
    184  1.11.6.2  christos 			goto again;
    185  1.11.6.2  christos 		}
    186  1.11.6.2  christos #endif
    187  1.11.6.2  christos #ifdef DEBUG
    188  1.11.6.2  christos 		if (statp->options & RES_DEBUG)
    189  1.11.6.2  christos 			printf(";; res_query: send error\n");
    190  1.11.6.2  christos #endif
    191  1.11.6.2  christos 		RES_SET_H_ERRNO(statp, TRY_AGAIN);
    192  1.11.6.2  christos 		return (n);
    193  1.11.6.2  christos 	}
    194  1.11.6.2  christos 
    195  1.11.6.2  christos 	if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
    196  1.11.6.2  christos #ifdef DEBUG
    197  1.11.6.2  christos 		if (statp->options & RES_DEBUG)
    198  1.11.6.2  christos 			printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n",
    199  1.11.6.2  christos 			       p_rcode(hp->rcode),
    200  1.11.6.2  christos 			       ntohs(hp->ancount),
    201  1.11.6.2  christos 			       ntohs(hp->nscount),
    202  1.11.6.2  christos 			       ntohs(hp->arcount));
    203  1.11.6.2  christos #endif
    204  1.11.6.2  christos 		switch (hp->rcode) {
    205  1.11.6.2  christos 		case NXDOMAIN:
    206  1.11.6.2  christos 			RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
    207  1.11.6.2  christos 			break;
    208  1.11.6.2  christos 		case SERVFAIL:
    209  1.11.6.2  christos 			RES_SET_H_ERRNO(statp, TRY_AGAIN);
    210  1.11.6.2  christos 			break;
    211  1.11.6.2  christos 		case NOERROR:
    212  1.11.6.2  christos 			RES_SET_H_ERRNO(statp, NO_DATA);
    213  1.11.6.2  christos 			break;
    214  1.11.6.2  christos 		case FORMERR:
    215  1.11.6.2  christos 		case NOTIMP:
    216  1.11.6.2  christos 		case REFUSED:
    217  1.11.6.2  christos 		default:
    218  1.11.6.2  christos 			RES_SET_H_ERRNO(statp, NO_RECOVERY);
    219  1.11.6.2  christos 			break;
    220  1.11.6.2  christos 		}
    221  1.11.6.2  christos 		return (-1);
    222  1.11.6.2  christos 	}
    223  1.11.6.2  christos 	return (n);
    224  1.11.6.2  christos }
    225  1.11.6.2  christos 
    226  1.11.6.2  christos /*%
    227  1.11.6.2  christos  * Formulate a normal query, send, and retrieve answer in supplied buffer.
    228  1.11.6.2  christos  * Return the size of the response on success, -1 on error.
    229  1.11.6.2  christos  * If enabled, implement search rules until answer or unrecoverable failure
    230  1.11.6.2  christos  * is detected.  Error code, if any, is left in H_ERRNO.
    231  1.11.6.2  christos  */
    232  1.11.6.2  christos int
    233  1.11.6.2  christos res_nsearch(res_state statp,
    234  1.11.6.2  christos 	    const char *name,	/*%< domain name */
    235  1.11.6.2  christos 	    int class, int type,	/*%< class and type of query */
    236  1.11.6.2  christos 	    u_char *answer,	/*%< buffer to put answer */
    237  1.11.6.2  christos 	    int anslen)		/*%< size of answer */
    238  1.11.6.2  christos {
    239  1.11.6.2  christos 	const char *cp, * const *domain;
    240  1.11.6.2  christos 	HEADER *hp = (HEADER *)(void *)answer;
    241  1.11.6.2  christos 	char tmp[NS_MAXDNAME];
    242  1.11.6.2  christos 	u_int dots;
    243  1.11.6.2  christos 	int trailing_dot, ret, saved_herrno;
    244  1.11.6.2  christos 	int got_nodata = 0, got_servfail = 0, root_on_list = 0;
    245  1.11.6.2  christos 	int tried_as_is = 0;
    246  1.11.6.2  christos 	int searched = 0;
    247  1.11.6.2  christos 
    248  1.11.6.2  christos 	errno = 0;
    249  1.11.6.2  christos 	RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);  /*%< True if we never query. */
    250  1.11.6.2  christos 	dots = 0;
    251  1.11.6.2  christos 	for (cp = name; *cp != '\0'; cp++)
    252  1.11.6.2  christos 		dots += (*cp == '.');
    253  1.11.6.2  christos 	trailing_dot = 0;
    254  1.11.6.2  christos 	if (cp > name && *--cp == '.')
    255  1.11.6.2  christos 		trailing_dot++;
    256  1.11.6.2  christos 
    257  1.11.6.2  christos 	/* If there aren't any dots, it could be a user-level alias. */
    258  1.11.6.2  christos 	if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL)
    259  1.11.6.2  christos 		return (res_nquery(statp, cp, class, type, answer, anslen));
    260  1.11.6.2  christos 
    261  1.11.6.2  christos 	/*
    262  1.11.6.2  christos 	 * If there are enough dots in the name, let's just give it a
    263  1.11.6.2  christos 	 * try 'as is'. The threshold can be set with the "ndots" option.
    264  1.11.6.2  christos 	 * Also, query 'as is', if there is a trailing dot in the name.
    265  1.11.6.2  christos 	 */
    266  1.11.6.2  christos 	saved_herrno = -1;
    267  1.11.6.2  christos 	if (dots >= statp->ndots || trailing_dot) {
    268  1.11.6.2  christos 		ret = res_nquerydomain(statp, name, NULL, class, type,
    269  1.11.6.2  christos 					 answer, anslen);
    270  1.11.6.2  christos 		if (ret > 0 || trailing_dot)
    271  1.11.6.2  christos 			return (ret);
    272  1.11.6.2  christos 		saved_herrno = statp->res_h_errno;
    273  1.11.6.2  christos 		tried_as_is++;
    274  1.11.6.2  christos 	}
    275  1.11.6.2  christos 
    276  1.11.6.2  christos 	/*
    277  1.11.6.2  christos 	 * We do at least one level of search if
    278  1.11.6.2  christos 	 *	- there is no dot and RES_DEFNAME is set, or
    279  1.11.6.2  christos 	 *	- there is at least one dot, there is no trailing dot,
    280  1.11.6.2  christos 	 *	  and RES_DNSRCH is set.
    281  1.11.6.2  christos 	 */
    282  1.11.6.2  christos 	if ((!dots && (statp->options & RES_DEFNAMES) != 0U) ||
    283  1.11.6.2  christos 	    (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0U)) {
    284  1.11.6.2  christos 		int done = 0;
    285  1.11.6.2  christos 
    286  1.11.6.2  christos 		for (domain = (const char * const *)statp->dnsrch;
    287  1.11.6.2  christos 		     *domain && !done;
    288  1.11.6.2  christos 		     domain++) {
    289  1.11.6.2  christos 			searched = 1;
    290  1.11.6.2  christos 
    291  1.11.6.2  christos 			if (domain[0][0] == '\0' ||
    292  1.11.6.2  christos 			    (domain[0][0] == '.' && domain[0][1] == '\0'))
    293  1.11.6.2  christos 				root_on_list++;
    294  1.11.6.2  christos 
    295  1.11.6.2  christos 			ret = res_nquerydomain(statp, name, *domain,
    296  1.11.6.2  christos 					       class, type,
    297  1.11.6.2  christos 					       answer, anslen);
    298  1.11.6.2  christos 			if (ret > 0)
    299  1.11.6.2  christos 				return (ret);
    300  1.11.6.2  christos 
    301  1.11.6.2  christos 			/*
    302  1.11.6.2  christos 			 * If no server present, give up.
    303  1.11.6.2  christos 			 * If name isn't found in this domain,
    304  1.11.6.2  christos 			 * keep trying higher domains in the search list
    305  1.11.6.2  christos 			 * (if that's enabled).
    306  1.11.6.2  christos 			 * On a NO_DATA error, keep trying, otherwise
    307  1.11.6.2  christos 			 * a wildcard entry of another type could keep us
    308  1.11.6.2  christos 			 * from finding this entry higher in the domain.
    309  1.11.6.2  christos 			 * If we get some other error (negative answer or
    310  1.11.6.2  christos 			 * server failure), then stop searching up,
    311  1.11.6.2  christos 			 * but try the input name below in case it's
    312  1.11.6.2  christos 			 * fully-qualified.
    313  1.11.6.2  christos 			 */
    314  1.11.6.2  christos 			if (errno == ECONNREFUSED) {
    315  1.11.6.2  christos 				RES_SET_H_ERRNO(statp, TRY_AGAIN);
    316  1.11.6.2  christos 				return (-1);
    317  1.11.6.2  christos 			}
    318  1.11.6.2  christos 
    319  1.11.6.2  christos 			switch (statp->res_h_errno) {
    320  1.11.6.2  christos 			case NO_DATA:
    321  1.11.6.2  christos 				got_nodata++;
    322  1.11.6.2  christos 				/* FALLTHROUGH */
    323  1.11.6.2  christos 			case HOST_NOT_FOUND:
    324  1.11.6.2  christos 				/* keep trying */
    325  1.11.6.2  christos 				break;
    326  1.11.6.2  christos 			case TRY_AGAIN:
    327  1.11.6.2  christos 				if (hp->rcode == SERVFAIL) {
    328  1.11.6.2  christos 					/* try next search element, if any */
    329  1.11.6.2  christos 					got_servfail++;
    330  1.11.6.2  christos 					break;
    331  1.11.6.2  christos 				}
    332  1.11.6.2  christos 				/* FALLTHROUGH */
    333  1.11.6.2  christos 			default:
    334  1.11.6.2  christos 				/* anything else implies that we're done */
    335  1.11.6.2  christos 				done++;
    336  1.11.6.2  christos 			}
    337  1.11.6.2  christos 
    338  1.11.6.2  christos 			/* if we got here for some reason other than DNSRCH,
    339  1.11.6.2  christos 			 * we only wanted one iteration of the loop, so stop.
    340  1.11.6.2  christos 			 */
    341  1.11.6.2  christos 			if ((statp->options & RES_DNSRCH) == 0U)
    342  1.11.6.2  christos 				done++;
    343  1.11.6.2  christos 		}
    344  1.11.6.2  christos 	}
    345  1.11.6.2  christos 
    346  1.11.6.2  christos 	/*
    347  1.11.6.2  christos 	 * If the query has not already been tried as is then try it
    348  1.11.6.2  christos 	 * unless RES_NOTLDQUERY is set and there were no dots.
    349  1.11.6.2  christos 	 */
    350  1.11.6.2  christos 	if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) &&
    351  1.11.6.2  christos 	    !(tried_as_is || root_on_list)) {
    352  1.11.6.2  christos 		ret = res_nquerydomain(statp, name, NULL, class, type,
    353  1.11.6.2  christos 				       answer, anslen);
    354  1.11.6.2  christos 		if (ret > 0)
    355  1.11.6.2  christos 			return (ret);
    356  1.11.6.2  christos 	}
    357  1.11.6.2  christos 
    358  1.11.6.2  christos 	/* if we got here, we didn't satisfy the search.
    359  1.11.6.2  christos 	 * if we did an initial full query, return that query's H_ERRNO
    360  1.11.6.2  christos 	 * (note that we wouldn't be here if that query had succeeded).
    361  1.11.6.2  christos 	 * else if we ever got a nodata, send that back as the reason.
    362  1.11.6.2  christos 	 * else send back meaningless H_ERRNO, that being the one from
    363  1.11.6.2  christos 	 * the last DNSRCH we did.
    364  1.11.6.2  christos 	 */
    365  1.11.6.2  christos 	if (saved_herrno != -1)
    366  1.11.6.2  christos 		RES_SET_H_ERRNO(statp, saved_herrno);
    367  1.11.6.2  christos 	else if (got_nodata)
    368  1.11.6.2  christos 		RES_SET_H_ERRNO(statp, NO_DATA);
    369  1.11.6.2  christos 	else if (got_servfail)
    370  1.11.6.2  christos 		RES_SET_H_ERRNO(statp, TRY_AGAIN);
    371  1.11.6.2  christos 	return (-1);
    372  1.11.6.2  christos }
    373  1.11.6.2  christos 
    374  1.11.6.2  christos /*%
    375  1.11.6.2  christos  * Perform a call on res_query on the concatenation of name and domain,
    376  1.11.6.2  christos  * removing a trailing dot from name if domain is NULL.
    377  1.11.6.2  christos  */
    378  1.11.6.2  christos int
    379  1.11.6.2  christos res_nquerydomain(res_state statp,
    380  1.11.6.2  christos 	    const char *name,
    381  1.11.6.2  christos 	    const char *domain,
    382  1.11.6.2  christos 	    int class, int type,	/*%< class and type of query */
    383  1.11.6.2  christos 	    u_char *answer,		/*%< buffer to put answer */
    384  1.11.6.2  christos 	    int anslen)		/*%< size of answer */
    385  1.11.6.2  christos {
    386  1.11.6.2  christos 	char nbuf[MAXDNAME];
    387  1.11.6.2  christos 	const char *longname = nbuf;
    388  1.11.6.2  christos 	int n, d;
    389  1.11.6.2  christos 
    390  1.11.6.2  christos #ifdef DEBUG
    391  1.11.6.2  christos 	if (statp->options & RES_DEBUG)
    392  1.11.6.2  christos 		printf(";; res_nquerydomain(%s, %s, %d, %d)\n",
    393  1.11.6.2  christos 		       name, domain?domain:"<Nil>", class, type);
    394  1.11.6.2  christos #endif
    395  1.11.6.2  christos 	if (domain == NULL) {
    396  1.11.6.2  christos 		/*
    397  1.11.6.2  christos 		 * Check for trailing '.';
    398  1.11.6.2  christos 		 * copy without '.' if present.
    399  1.11.6.2  christos 		 */
    400  1.11.6.2  christos 		n = strlen(name);
    401  1.11.6.2  christos 		if (n >= MAXDNAME) {
    402  1.11.6.2  christos 			RES_SET_H_ERRNO(statp, NO_RECOVERY);
    403  1.11.6.2  christos 			return (-1);
    404  1.11.6.2  christos 		}
    405  1.11.6.2  christos 		n--;
    406  1.11.6.2  christos 		if (n >= 0 && name[n] == '.') {
    407  1.11.6.2  christos 			strncpy(nbuf, name, (size_t)n);
    408  1.11.6.2  christos 			nbuf[n] = '\0';
    409  1.11.6.2  christos 		} else
    410  1.11.6.2  christos 			longname = name;
    411  1.11.6.2  christos 	} else {
    412  1.11.6.2  christos 		n = strlen(name);
    413  1.11.6.2  christos 		d = strlen(domain);
    414  1.11.6.2  christos 		if (n + d + 1 >= MAXDNAME) {
    415  1.11.6.2  christos 			RES_SET_H_ERRNO(statp, NO_RECOVERY);
    416  1.11.6.2  christos 			return (-1);
    417  1.11.6.2  christos 		}
    418  1.11.6.2  christos 		sprintf(nbuf, "%s.%s", name, domain);
    419  1.11.6.2  christos 	}
    420  1.11.6.2  christos 	return (res_nquery(statp, longname, class, type, answer, anslen));
    421  1.11.6.2  christos }
    422  1.11.6.2  christos 
    423  1.11.6.2  christos const char *
    424  1.11.6.2  christos res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) {
    425  1.11.6.2  christos 	char *file, *cp1, *cp2;
    426  1.11.6.2  christos 	char buf[BUFSIZ];
    427  1.11.6.2  christos 	FILE *fp;
    428  1.11.6.2  christos 
    429  1.11.6.2  christos 	if (statp->options & RES_NOALIASES)
    430  1.11.6.2  christos 		return (NULL);
    431  1.11.6.2  christos 	/*
    432  1.11.6.2  christos 	 * forbid hostaliases for setuid binary, due to possible security
    433  1.11.6.2  christos 	 * breach.
    434  1.11.6.2  christos 	 */
    435  1.11.6.2  christos 	if (issetugid())
    436  1.11.6.2  christos 		return (NULL);
    437  1.11.6.2  christos 	file = getenv("HOSTALIASES");
    438  1.11.6.2  christos 	if (file == NULL || (fp = fopen(file, "r")) == NULL)
    439  1.11.6.2  christos 		return (NULL);
    440  1.11.6.2  christos 	setbuf(fp, NULL);
    441  1.11.6.2  christos 	buf[sizeof(buf) - 1] = '\0';
    442  1.11.6.2  christos 	while (fgets(buf, sizeof(buf), fp)) {
    443  1.11.6.2  christos 		for (cp1 = buf; *cp1 && !isspace((unsigned char)*cp1); ++cp1)
    444  1.11.6.2  christos 			;
    445  1.11.6.2  christos 		if (!*cp1)
    446  1.11.6.2  christos 			break;
    447  1.11.6.2  christos 		*cp1 = '\0';
    448  1.11.6.2  christos 		if (ns_samename(buf, name) == 1) {
    449  1.11.6.2  christos 			while (isspace((unsigned char)*++cp1))
    450  1.11.6.2  christos 				;
    451  1.11.6.2  christos 			if (!*cp1)
    452  1.11.6.2  christos 				break;
    453  1.11.6.2  christos 			for (cp2 = cp1 + 1; *cp2 &&
    454  1.11.6.2  christos 			     !isspace((unsigned char)*cp2); ++cp2)
    455  1.11.6.2  christos 				;
    456  1.11.6.2  christos 			*cp2 = '\0';
    457  1.11.6.2  christos 			strncpy(dst, cp1, siz - 1);
    458  1.11.6.2  christos 			dst[siz - 1] = '\0';
    459  1.11.6.2  christos 			fclose(fp);
    460  1.11.6.2  christos 			return (dst);
    461  1.11.6.2  christos 		}
    462  1.11.6.2  christos 	}
    463  1.11.6.2  christos 	fclose(fp);
    464  1.11.6.2  christos 	return (NULL);
    465  1.11.6.2  christos }
    466  1.11.6.2  christos 
    467  1.11.6.2  christos /*! \file */
    468