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