diff options
Diffstat (limited to 'lib/libc/resolv/res_query.c')
-rw-r--r-- | lib/libc/resolv/res_query.c | 97 |
1 files changed, 73 insertions, 24 deletions
diff --git a/lib/libc/resolv/res_query.c b/lib/libc/resolv/res_query.c index c160e93..c0cf003 100644 --- a/lib/libc/resolv/res_query.c +++ b/lib/libc/resolv/res_query.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -70,8 +66,10 @@ #if defined(LIBC_SCCS) && !defined(lint) static const char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93"; -static const char rcsid[] = "$Id: res_query.c,v 1.7.18.1 2005/04/27 05:01:11 sra Exp $"; +static const char rcsid[] = "$Id: res_query.c,v 1.2.2.3.4.2 2004/03/16 12:34:19 marka Exp $"; #endif /* LIBC_SCCS and not lint */ +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); #include "port_before.h" #include <sys/types.h> @@ -86,6 +84,7 @@ static const char rcsid[] = "$Id: res_query.c,v 1.7.18.1 2005/04/27 05:01:11 sra #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include "port_after.h" /* Options. Leave them on. */ @@ -97,7 +96,7 @@ static const char rcsid[] = "$Id: res_query.c,v 1.7.18.1 2005/04/27 05:01:11 sra #define MAXPACKET 1024 #endif -/*% +/* * Formulate a normal query, send, and await answer. * Returned answer is placed in supplied buffer "answer". * Perform preliminary check of answer, returning success only @@ -109,10 +108,10 @@ static const char rcsid[] = "$Id: res_query.c,v 1.7.18.1 2005/04/27 05:01:11 sra */ int res_nquery(res_state statp, - const char *name, /*%< domain name */ - int class, int type, /*%< class and type of query */ - u_char *answer, /*%< buffer to put answer */ - int anslen) /*%< size of answer buffer */ + const char *name, /* domain name */ + int class, int type, /* class and type of query */ + u_char *answer, /* buffer to put answer */ + int anslen) /* size of answer buffer */ { u_char buf[MAXPACKET]; HEADER *hp = (HEADER *) answer; @@ -122,7 +121,8 @@ res_nquery(res_state statp, oflags = statp->_flags; again: - hp->rcode = NOERROR; /*%< default */ + hp->rcode = NOERROR; /* default */ + #ifdef DEBUG if (statp->options & RES_DEBUG) printf(";; res_query(%s, %d, %d)\n", name, class, type); @@ -194,7 +194,7 @@ again: return (n); } -/*% +/* * Formulate a normal query, send, and retrieve answer in supplied buffer. * Return the size of the response on success, -1 on error. * If enabled, implement search rules until answer or unrecoverable failure @@ -202,10 +202,10 @@ again: */ int res_nsearch(res_state statp, - const char *name, /*%< domain name */ - int class, int type, /*%< class and type of query */ - u_char *answer, /*%< buffer to put answer */ - int anslen) /*%< size of answer */ + const char *name, /* domain name */ + int class, int type, /* class and type of query */ + u_char *answer, /* buffer to put answer */ + int anslen) /* size of answer */ { const char *cp, * const *domain; HEADER *hp = (HEADER *) answer; @@ -217,7 +217,8 @@ res_nsearch(res_state statp, int searched = 0; errno = 0; - RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /*%< True if we never query. */ + RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */ + dots = 0; for (cp = name; *cp != '\0'; cp++) dots += (*cp == '.'); @@ -240,6 +241,21 @@ res_nsearch(res_state statp, answer, anslen); if (ret > 0 || trailing_dot) return (ret); + if (errno == ECONNREFUSED) { + RES_SET_H_ERRNO(statp, TRY_AGAIN); + return (-1); + } + switch (statp->res_h_errno) { + case NO_DATA: + case HOST_NOT_FOUND: + break; + case TRY_AGAIN: + if (hp->rcode == SERVFAIL) + break; + /* FALLTHROUGH */ + default: + return (-1); + } saved_herrno = statp->res_h_errno; tried_as_is++; } @@ -263,6 +279,9 @@ res_nsearch(res_state statp, (domain[0][0] == '.' && domain[0][1] == '\0')) root_on_list++; + if (root_on_list && tried_as_is) + continue; + ret = res_nquerydomain(statp, name, *domain, class, type, answer, anslen); @@ -295,9 +314,26 @@ res_nsearch(res_state statp, /* keep trying */ break; case TRY_AGAIN: + /* + * This can occur due to a server failure + * (that is, all listed servers have failed), + * or all listed servers have timed out. + * ((HEADER *)answer)->rcode may not be set + * to SERVFAIL in the case of a timeout. + * + * Either way we must return TRY_AGAIN in + * order to avoid non-deterministic + * return codes. + * For example, loaded name servers or races + * against network startup/validation (dhcp, + * ppp, etc) can cause the search to timeout + * on one search element, e.g. 'fu.bar.com', + * and return a definitive failure on the + * next search element, e.g. 'fu.'. + */ + got_servfail++; if (hp->rcode == SERVFAIL) { /* try next search element, if any */ - got_servfail++; break; } /* FALLTHROUGH */ @@ -314,6 +350,18 @@ res_nsearch(res_state statp, } } + switch (statp->res_h_errno) { + case NO_DATA: + case HOST_NOT_FOUND: + break; + case TRY_AGAIN: + if (hp->rcode == SERVFAIL) + break; + /* FALLTHROUGH */ + default: + goto giveup; + } + /* * If the query has not already been tried as is then try it * unless RES_NOTLDQUERY is set and there were no dots. @@ -333,6 +381,7 @@ res_nsearch(res_state statp, * else send back meaningless H_ERRNO, that being the one from * the last DNSRCH we did. */ +giveup: if (saved_herrno != -1) RES_SET_H_ERRNO(statp, saved_herrno); else if (got_nodata) @@ -342,7 +391,7 @@ res_nsearch(res_state statp, return (-1); } -/*% +/* * Perform a call on res_query on the concatenation of name and domain, * removing a trailing dot from name if domain is NULL. */ @@ -350,9 +399,9 @@ int res_nquerydomain(res_state statp, const char *name, const char *domain, - int class, int type, /*%< class and type of query */ - u_char *answer, /*%< buffer to put answer */ - int anslen) /*%< size of answer */ + int class, int type, /* class and type of query */ + u_char *answer, /* buffer to put answer */ + int anslen) /* size of answer */ { char nbuf[MAXDNAME]; const char *longname = nbuf; @@ -399,6 +448,8 @@ res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) { if (statp->options & RES_NOALIASES) return (NULL); + if (issetugid()) + return (NULL); file = getenv("HOSTALIASES"); if (file == NULL || (fp = fopen(file, "r")) == NULL) return (NULL); @@ -428,5 +479,3 @@ res_hostalias(const res_state statp, const char *name, char *dst, size_t siz) { fclose(fp); return (NULL); } - -/*! \file */ |