1156952Sume/* 2156952Sume * Copyright (c) 1988, 1993 3156952Sume * The Regents of the University of California. All rights reserved. 4156952Sume * 5156952Sume * Redistribution and use in source and binary forms, with or without 6156952Sume * modification, are permitted provided that the following conditions 7156952Sume * are met: 8156952Sume * 1. Redistributions of source code must retain the above copyright 9156952Sume * notice, this list of conditions and the following disclaimer. 10156952Sume * 2. Redistributions in binary form must reproduce the above copyright 11156952Sume * notice, this list of conditions and the following disclaimer in the 12156952Sume * documentation and/or other materials provided with the distribution. 13156952Sume * 4. Neither the name of the University nor the names of its contributors 14156952Sume * may be used to endorse or promote products derived from this software 15156952Sume * without specific prior written permission. 16156952Sume * 17156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20156952Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27156952Sume * SUCH DAMAGE. 28156952Sume */ 29156952Sume 30156952Sume/* 31156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation. 32156952Sume * 33156952Sume * Permission to use, copy, modify, and distribute this software for any 34156952Sume * purpose with or without fee is hereby granted, provided that the above 35156952Sume * copyright notice and this permission notice appear in all copies, and that 36156952Sume * the name of Digital Equipment Corporation not be used in advertising or 37156952Sume * publicity pertaining to distribution of the document or software without 38156952Sume * specific, written prior permission. 39156952Sume * 40156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 41156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 42156952Sume * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 43156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 44156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 45156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 46156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 47156952Sume * SOFTWARE. 48156952Sume */ 49156952Sume 50156952Sume/* 51156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 52156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 53156952Sume * 54156952Sume * Permission to use, copy, modify, and distribute this software for any 55156952Sume * purpose with or without fee is hereby granted, provided that the above 56156952Sume * copyright notice and this permission notice appear in all copies. 57156952Sume * 58156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 59156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 60156952Sume * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 61156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 62156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 63156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 64156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 65156952Sume */ 66156952Sume 67156952Sume#if defined(LIBC_SCCS) && !defined(lint) 68156952Sumestatic const char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93"; 69186090Sumestatic const char rcsid[] = "$Id: res_query.c,v 1.7.18.2 2008/04/03 23:15:15 marka Exp $"; 70156952Sume#endif /* LIBC_SCCS and not lint */ 71156956Sume#include <sys/cdefs.h> 72156956Sume__FBSDID("$FreeBSD$"); 73156952Sume 74156952Sume#include "port_before.h" 75156952Sume#include <sys/types.h> 76156952Sume#include <sys/param.h> 77156952Sume#include <netinet/in.h> 78156952Sume#include <arpa/inet.h> 79156952Sume#include <arpa/nameser.h> 80156952Sume#include <ctype.h> 81156952Sume#include <errno.h> 82156952Sume#include <netdb.h> 83156952Sume#include <resolv.h> 84156952Sume#include <stdio.h> 85156952Sume#include <stdlib.h> 86156952Sume#include <string.h> 87156956Sume#include <unistd.h> 88156952Sume#include "port_after.h" 89156952Sume 90156952Sume/* Options. Leave them on. */ 91156952Sume#define DEBUG 92156952Sume 93156952Sume#if PACKETSZ > 1024 94156952Sume#define MAXPACKET PACKETSZ 95156952Sume#else 96156952Sume#define MAXPACKET 1024 97156952Sume#endif 98156952Sume 99170244Sume/*% 100156952Sume * Formulate a normal query, send, and await answer. 101156952Sume * Returned answer is placed in supplied buffer "answer". 102156952Sume * Perform preliminary check of answer, returning success only 103156952Sume * if no error is indicated and the answer count is nonzero. 104156952Sume * Return the size of the response on success, -1 on error. 105156952Sume * Error number is left in H_ERRNO. 106156952Sume * 107156952Sume * Caller must parse answer and determine whether it answers the question. 108156952Sume */ 109156952Sumeint 110156952Sumeres_nquery(res_state statp, 111170244Sume const char *name, /*%< domain name */ 112170244Sume int class, int type, /*%< class and type of query */ 113170244Sume u_char *answer, /*%< buffer to put answer */ 114170244Sume int anslen) /*%< size of answer buffer */ 115156952Sume{ 116156952Sume u_char buf[MAXPACKET]; 117156952Sume HEADER *hp = (HEADER *) answer; 118186090Sume u_int oflags; 119186090Sume u_char *rdata; 120156952Sume int n; 121156952Sume 122156952Sume oflags = statp->_flags; 123156952Sume 124156952Sumeagain: 125170244Sume hp->rcode = NOERROR; /*%< default */ 126156952Sume#ifdef DEBUG 127156952Sume if (statp->options & RES_DEBUG) 128156952Sume printf(";; res_query(%s, %d, %d)\n", name, class, type); 129156952Sume#endif 130156952Sume 131156952Sume n = res_nmkquery(statp, QUERY, name, class, type, NULL, 0, NULL, 132156952Sume buf, sizeof(buf)); 133156952Sume#ifdef RES_USE_EDNS0 134156952Sume if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 && 135186090Sume (statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC|RES_NSID))) { 136156952Sume n = res_nopt(statp, n, buf, sizeof(buf), anslen); 137186090Sume rdata = &buf[n]; 138186090Sume if (n > 0 && (statp->options & RES_NSID) != 0U) { 139186090Sume n = res_nopt_rdata(statp, n, buf, sizeof(buf), rdata, 140186090Sume NS_OPT_NSID, 0, NULL); 141186090Sume } 142186090Sume } 143156952Sume#endif 144156952Sume if (n <= 0) { 145156952Sume#ifdef DEBUG 146156952Sume if (statp->options & RES_DEBUG) 147156952Sume printf(";; res_query: mkquery failed\n"); 148156952Sume#endif 149156952Sume RES_SET_H_ERRNO(statp, NO_RECOVERY); 150156952Sume return (n); 151156952Sume } 152186090Sume 153156952Sume n = res_nsend(statp, buf, n, answer, anslen); 154156952Sume if (n < 0) { 155156952Sume#ifdef RES_USE_EDNS0 156156952Sume /* if the query choked with EDNS0, retry without EDNS0 */ 157156952Sume if ((statp->options & (RES_USE_EDNS0|RES_USE_DNSSEC)) != 0U && 158156952Sume ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) { 159156952Sume statp->_flags |= RES_F_EDNS0ERR; 160156952Sume if (statp->options & RES_DEBUG) 161156952Sume printf(";; res_nquery: retry without EDNS0\n"); 162156952Sume goto again; 163156952Sume } 164156952Sume#endif 165156952Sume#ifdef DEBUG 166156952Sume if (statp->options & RES_DEBUG) 167156952Sume printf(";; res_query: send error\n"); 168156952Sume#endif 169156952Sume RES_SET_H_ERRNO(statp, TRY_AGAIN); 170156952Sume return (n); 171156952Sume } 172156952Sume 173156952Sume if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { 174156952Sume#ifdef DEBUG 175156952Sume if (statp->options & RES_DEBUG) 176156952Sume printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n", 177156952Sume p_rcode(hp->rcode), 178156952Sume ntohs(hp->ancount), 179156952Sume ntohs(hp->nscount), 180156952Sume ntohs(hp->arcount)); 181156952Sume#endif 182156952Sume switch (hp->rcode) { 183156952Sume case NXDOMAIN: 184156952Sume RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); 185156952Sume break; 186156952Sume case SERVFAIL: 187156952Sume RES_SET_H_ERRNO(statp, TRY_AGAIN); 188156952Sume break; 189156952Sume case NOERROR: 190156952Sume RES_SET_H_ERRNO(statp, NO_DATA); 191156952Sume break; 192156952Sume case FORMERR: 193156952Sume case NOTIMP: 194156952Sume case REFUSED: 195156952Sume default: 196156952Sume RES_SET_H_ERRNO(statp, NO_RECOVERY); 197156952Sume break; 198156952Sume } 199156952Sume return (-1); 200156952Sume } 201156952Sume return (n); 202156952Sume} 203156952Sume 204170244Sume/*% 205156952Sume * Formulate a normal query, send, and retrieve answer in supplied buffer. 206156952Sume * Return the size of the response on success, -1 on error. 207156952Sume * If enabled, implement search rules until answer or unrecoverable failure 208156952Sume * is detected. Error code, if any, is left in H_ERRNO. 209156952Sume */ 210156952Sumeint 211156952Sumeres_nsearch(res_state statp, 212170244Sume const char *name, /*%< domain name */ 213170244Sume int class, int type, /*%< class and type of query */ 214170244Sume u_char *answer, /*%< buffer to put answer */ 215170244Sume int anslen) /*%< size of answer */ 216156952Sume{ 217156952Sume const char *cp, * const *domain; 218157093Sume HEADER *hp = (HEADER *) answer; 219156952Sume char tmp[NS_MAXDNAME]; 220156952Sume u_int dots; 221156952Sume int trailing_dot, ret, saved_herrno; 222156952Sume int got_nodata = 0, got_servfail = 0, root_on_list = 0; 223156952Sume int tried_as_is = 0; 224156952Sume int searched = 0; 225156952Sume 226156952Sume errno = 0; 227170244Sume RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /*%< True if we never query. */ 228156952Sume dots = 0; 229156952Sume for (cp = name; *cp != '\0'; cp++) 230156952Sume dots += (*cp == '.'); 231156952Sume trailing_dot = 0; 232156952Sume if (cp > name && *--cp == '.') 233156952Sume trailing_dot++; 234156952Sume 235156952Sume /* If there aren't any dots, it could be a user-level alias. */ 236156952Sume if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp))!= NULL) 237156952Sume return (res_nquery(statp, cp, class, type, answer, anslen)); 238156952Sume 239156952Sume /* 240156952Sume * If there are enough dots in the name, let's just give it a 241156952Sume * try 'as is'. The threshold can be set with the "ndots" option. 242156952Sume * Also, query 'as is', if there is a trailing dot in the name. 243156952Sume */ 244156952Sume saved_herrno = -1; 245156952Sume if (dots >= statp->ndots || trailing_dot) { 246156952Sume ret = res_nquerydomain(statp, name, NULL, class, type, 247156952Sume answer, anslen); 248156952Sume if (ret > 0 || trailing_dot) 249156952Sume return (ret); 250156956Sume if (errno == ECONNREFUSED) { 251156956Sume RES_SET_H_ERRNO(statp, TRY_AGAIN); 252156956Sume return (-1); 253156956Sume } 254156956Sume switch (statp->res_h_errno) { 255156956Sume case NO_DATA: 256156956Sume case HOST_NOT_FOUND: 257156956Sume break; 258157093Sume case TRY_AGAIN: 259157093Sume if (hp->rcode == SERVFAIL) 260157093Sume break; 261157093Sume /* FALLTHROUGH */ 262156956Sume default: 263156956Sume return (-1); 264156956Sume } 265156952Sume saved_herrno = statp->res_h_errno; 266156952Sume tried_as_is++; 267156952Sume } 268156952Sume 269156952Sume /* 270156952Sume * We do at least one level of search if 271156952Sume * - there is no dot and RES_DEFNAME is set, or 272156952Sume * - there is at least one dot, there is no trailing dot, 273156952Sume * and RES_DNSRCH is set. 274156952Sume */ 275156952Sume if ((!dots && (statp->options & RES_DEFNAMES) != 0U) || 276156952Sume (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0U)) { 277156952Sume int done = 0; 278156952Sume 279156952Sume for (domain = (const char * const *)statp->dnsrch; 280156952Sume *domain && !done; 281156952Sume domain++) { 282156952Sume searched = 1; 283156952Sume 284156952Sume if (domain[0][0] == '\0' || 285156952Sume (domain[0][0] == '.' && domain[0][1] == '\0')) 286156952Sume root_on_list++; 287156952Sume 288156956Sume if (root_on_list && tried_as_is) 289156956Sume continue; 290156956Sume 291156952Sume ret = res_nquerydomain(statp, name, *domain, 292156952Sume class, type, 293156952Sume answer, anslen); 294156952Sume if (ret > 0) 295156952Sume return (ret); 296156952Sume 297156952Sume /* 298156952Sume * If no server present, give up. 299156952Sume * If name isn't found in this domain, 300156952Sume * keep trying higher domains in the search list 301156952Sume * (if that's enabled). 302156952Sume * On a NO_DATA error, keep trying, otherwise 303156952Sume * a wildcard entry of another type could keep us 304156952Sume * from finding this entry higher in the domain. 305156952Sume * If we get some other error (negative answer or 306156952Sume * server failure), then stop searching up, 307156952Sume * but try the input name below in case it's 308156952Sume * fully-qualified. 309156952Sume */ 310156952Sume if (errno == ECONNREFUSED) { 311156952Sume RES_SET_H_ERRNO(statp, TRY_AGAIN); 312156952Sume return (-1); 313156952Sume } 314156952Sume 315156952Sume switch (statp->res_h_errno) { 316156952Sume case NO_DATA: 317156952Sume got_nodata++; 318156952Sume /* FALLTHROUGH */ 319156952Sume case HOST_NOT_FOUND: 320156952Sume /* keep trying */ 321156952Sume break; 322156952Sume case TRY_AGAIN: 323156956Sume /* 324156956Sume * This can occur due to a server failure 325156956Sume * (that is, all listed servers have failed), 326156956Sume * or all listed servers have timed out. 327156956Sume * ((HEADER *)answer)->rcode may not be set 328156956Sume * to SERVFAIL in the case of a timeout. 329156956Sume * 330157093Sume * Either way we must return TRY_AGAIN in 331157093Sume * order to avoid non-deterministic 332157093Sume * return codes. 333157093Sume * For example, loaded name servers or races 334156956Sume * against network startup/validation (dhcp, 335156956Sume * ppp, etc) can cause the search to timeout 336156956Sume * on one search element, e.g. 'fu.bar.com', 337156956Sume * and return a definitive failure on the 338156956Sume * next search element, e.g. 'fu.'. 339156956Sume */ 340156956Sume got_servfail++; 341157093Sume if (hp->rcode == SERVFAIL) { 342157093Sume /* try next search element, if any */ 343157093Sume break; 344157093Sume } 345156952Sume /* FALLTHROUGH */ 346156952Sume default: 347156952Sume /* anything else implies that we're done */ 348156952Sume done++; 349156952Sume } 350156952Sume 351156952Sume /* if we got here for some reason other than DNSRCH, 352156952Sume * we only wanted one iteration of the loop, so stop. 353156952Sume */ 354156952Sume if ((statp->options & RES_DNSRCH) == 0U) 355156952Sume done++; 356156952Sume } 357156952Sume } 358156952Sume 359156956Sume switch (statp->res_h_errno) { 360156956Sume case NO_DATA: 361156956Sume case HOST_NOT_FOUND: 362156956Sume break; 363157093Sume case TRY_AGAIN: 364157093Sume if (hp->rcode == SERVFAIL) 365157093Sume break; 366157093Sume /* FALLTHROUGH */ 367156956Sume default: 368156956Sume goto giveup; 369156956Sume } 370156956Sume 371156952Sume /* 372156952Sume * If the query has not already been tried as is then try it 373156952Sume * unless RES_NOTLDQUERY is set and there were no dots. 374156952Sume */ 375156952Sume if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) && 376156952Sume !(tried_as_is || root_on_list)) { 377156952Sume ret = res_nquerydomain(statp, name, NULL, class, type, 378156952Sume answer, anslen); 379156952Sume if (ret > 0) 380156952Sume return (ret); 381156952Sume } 382156952Sume 383156952Sume /* if we got here, we didn't satisfy the search. 384156952Sume * if we did an initial full query, return that query's H_ERRNO 385156952Sume * (note that we wouldn't be here if that query had succeeded). 386156952Sume * else if we ever got a nodata, send that back as the reason. 387156952Sume * else send back meaningless H_ERRNO, that being the one from 388156952Sume * the last DNSRCH we did. 389156952Sume */ 390156956Sumegiveup: 391156952Sume if (saved_herrno != -1) 392156952Sume RES_SET_H_ERRNO(statp, saved_herrno); 393156952Sume else if (got_nodata) 394156952Sume RES_SET_H_ERRNO(statp, NO_DATA); 395156952Sume else if (got_servfail) 396156952Sume RES_SET_H_ERRNO(statp, TRY_AGAIN); 397156952Sume return (-1); 398156952Sume} 399156952Sume 400170244Sume/*% 401156952Sume * Perform a call on res_query on the concatenation of name and domain, 402156952Sume * removing a trailing dot from name if domain is NULL. 403156952Sume */ 404156952Sumeint 405156952Sumeres_nquerydomain(res_state statp, 406156952Sume const char *name, 407156952Sume const char *domain, 408170244Sume int class, int type, /*%< class and type of query */ 409170244Sume u_char *answer, /*%< buffer to put answer */ 410170244Sume int anslen) /*%< size of answer */ 411156952Sume{ 412156952Sume char nbuf[MAXDNAME]; 413156952Sume const char *longname = nbuf; 414156952Sume int n, d; 415156952Sume 416156952Sume#ifdef DEBUG 417156952Sume if (statp->options & RES_DEBUG) 418156952Sume printf(";; res_nquerydomain(%s, %s, %d, %d)\n", 419156952Sume name, domain?domain:"<Nil>", class, type); 420156952Sume#endif 421156952Sume if (domain == NULL) { 422156952Sume /* 423156952Sume * Check for trailing '.'; 424156952Sume * copy without '.' if present. 425156952Sume */ 426156952Sume n = strlen(name); 427156952Sume if (n >= MAXDNAME) { 428156952Sume RES_SET_H_ERRNO(statp, NO_RECOVERY); 429156952Sume return (-1); 430156952Sume } 431156952Sume n--; 432156952Sume if (n >= 0 && name[n] == '.') { 433156952Sume strncpy(nbuf, name, n); 434156952Sume nbuf[n] = '\0'; 435156952Sume } else 436156952Sume longname = name; 437156952Sume } else { 438156952Sume n = strlen(name); 439156952Sume d = strlen(domain); 440156952Sume if (n + d + 1 >= MAXDNAME) { 441156952Sume RES_SET_H_ERRNO(statp, NO_RECOVERY); 442156952Sume return (-1); 443156952Sume } 444156952Sume sprintf(nbuf, "%s.%s", name, domain); 445156952Sume } 446156952Sume return (res_nquery(statp, longname, class, type, answer, anslen)); 447156952Sume} 448156952Sume 449156952Sumeconst char * 450156952Sumeres_hostalias(const res_state statp, const char *name, char *dst, size_t siz) { 451156952Sume char *file, *cp1, *cp2; 452156952Sume char buf[BUFSIZ]; 453156952Sume FILE *fp; 454156952Sume 455156952Sume if (statp->options & RES_NOALIASES) 456156952Sume return (NULL); 457156956Sume if (issetugid()) 458156956Sume return (NULL); 459156952Sume file = getenv("HOSTALIASES"); 460254700Sjilles if (file == NULL || (fp = fopen(file, "re")) == NULL) 461156952Sume return (NULL); 462156952Sume setbuf(fp, NULL); 463156952Sume buf[sizeof(buf) - 1] = '\0'; 464156952Sume while (fgets(buf, sizeof(buf), fp)) { 465156952Sume for (cp1 = buf; *cp1 && !isspace((unsigned char)*cp1); ++cp1) 466156952Sume ; 467156952Sume if (!*cp1) 468156952Sume break; 469156952Sume *cp1 = '\0'; 470156952Sume if (ns_samename(buf, name) == 1) { 471156952Sume while (isspace((unsigned char)*++cp1)) 472156952Sume ; 473156952Sume if (!*cp1) 474156952Sume break; 475156952Sume for (cp2 = cp1 + 1; *cp2 && 476156952Sume !isspace((unsigned char)*cp2); ++cp2) 477156952Sume ; 478156952Sume *cp2 = '\0'; 479156952Sume strncpy(dst, cp1, siz - 1); 480156952Sume dst[siz - 1] = '\0'; 481156952Sume fclose(fp); 482156952Sume return (dst); 483156952Sume } 484156952Sume } 485156952Sume fclose(fp); 486156952Sume return (NULL); 487156952Sume} 488170244Sume 489170244Sume/*! \file */ 490