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