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