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