blob: 2a0ce8261a05d162082a4ab7e05f965dcd6de6d7 [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: res_query.c,v 1.7 2006/01/24 17:41:25 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
Bernie Innocenti55864192018-08-30 04:05:20 +090073#include <arpa/inet.h>
74#include <arpa/nameser.h>
75#include <ctype.h>
76#include <errno.h>
77#include <netdb.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090078#include <netinet/in.h>
79#include <sys/param.h>
80#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090081#include "resolv_cache.h"
82#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090083#include <stdio.h>
84#include <stdlib.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090085#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090086#include <unistd.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090087
Bernie Innocenti55864192018-08-30 04:05:20 +090088/* Options. Leave them on. */
89#ifndef DEBUG
90#define DEBUG
91#endif
92
93#if PACKETSZ > 1024
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090094#define MAXPACKET PACKETSZ
Bernie Innocenti55864192018-08-30 04:05:20 +090095#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090096#define MAXPACKET 1024
Bernie Innocenti55864192018-08-30 04:05:20 +090097#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 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900109int res_nquery(res_state statp, const char* name, // domain name
110 int cl, int type, // class and type of query
111 u_char* answer, // buffer to put answer
112 int anslen) // size of answer buffer
Bernie Innocenti55864192018-08-30 04:05:20 +0900113{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114 u_char buf[MAXPACKET];
115 HEADER* hp = (HEADER*) (void*) answer;
116 int n;
117 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +0900118
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900119 oflags = statp->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +0900120
121again:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900122 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +0900123
124#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900125 if (statp->options & RES_DEBUG) printf(";; res_query(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +0900126#endif
127
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900128 n = res_nmkquery(statp, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
Bernie Innocenti55864192018-08-30 04:05:20 +0900129#ifdef RES_USE_EDNS0
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900130 if (n > 0 && (statp->_flags & RES_F_EDNS0ERR) == 0 &&
131 (statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U)
132 n = res_nopt(statp, n, buf, sizeof(buf), anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900133#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900134 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900135#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900136 if (statp->options & RES_DEBUG) printf(";; res_query: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900137#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900138 RES_SET_H_ERRNO(statp, NO_RECOVERY);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900139 return n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900140 }
141 n = res_nsend(statp, buf, n, answer, anslen);
142 if (n < 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900143#ifdef RES_USE_EDNS0
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900144 /* if the query choked with EDNS0, retry without EDNS0 */
145 if ((statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U &&
146 ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
147 statp->_flags |= RES_F_EDNS0ERR;
148 if (statp->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
149 goto again;
150 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900151#endif
152#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900153 if (statp->options & RES_DEBUG) printf(";; res_query: send error\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900154#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900155 RES_SET_H_ERRNO(statp, TRY_AGAIN);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900156 return n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900157 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900158
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900159 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900160#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900161 if (statp->options & RES_DEBUG)
162 printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n", p_rcode(hp->rcode),
163 ntohs(hp->ancount), ntohs(hp->nscount), ntohs(hp->arcount));
Bernie Innocenti55864192018-08-30 04:05:20 +0900164#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900165 switch (hp->rcode) {
166 case NXDOMAIN:
167 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
168 break;
169 case SERVFAIL:
170 RES_SET_H_ERRNO(statp, TRY_AGAIN);
171 break;
172 case NOERROR:
173 RES_SET_H_ERRNO(statp, NO_DATA);
174 break;
175 case FORMERR:
176 case NOTIMP:
177 case REFUSED:
178 default:
179 RES_SET_H_ERRNO(statp, NO_RECOVERY);
180 break;
181 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900182 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900183 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900184 return n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900185}
186
187/*
188 * Formulate a normal query, send, and retrieve answer in supplied buffer.
189 * Return the size of the response on success, -1 on error.
190 * If enabled, implement search rules until answer or unrecoverable failure
191 * is detected. Error code, if any, is left in H_ERRNO.
192 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900193int res_nsearch(res_state statp, const char* name, /* domain name */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900194 int cl, int type, /* class and type of query */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900195 u_char* answer, /* buffer to put answer */
196 int anslen) /* size of answer */
Bernie Innocenti55864192018-08-30 04:05:20 +0900197{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900198 const char *cp, *const *domain;
199 HEADER* hp = (HEADER*) (void*) answer;
200 char tmp[NS_MAXDNAME];
201 u_int dots;
202 int trailing_dot, ret, saved_herrno;
203 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
204 int tried_as_is = 0;
205 int searched = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900206
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900207 errno = 0;
208 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */
Bernie Innocenti55864192018-08-30 04:05:20 +0900209
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900210 dots = 0;
211 for (cp = name; *cp != '\0'; cp++) dots += (*cp == '.');
212 trailing_dot = 0;
213 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900214
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900215 /* If there aren't any dots, it could be a user-level alias. */
216 if (!dots && (cp = res_hostalias(statp, name, tmp, sizeof tmp)) != NULL)
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900217 return res_nquery(statp, cp, cl, type, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900218
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900219 /*
220 * If there are enough dots in the name, let's just give it a
221 * try 'as is'. The threshold can be set with the "ndots" option.
222 * Also, query 'as is', if there is a trailing dot in the name.
223 */
224 saved_herrno = -1;
225 if (dots >= statp->ndots || trailing_dot) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900226 ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen);
227 if (ret > 0 || trailing_dot) return ret;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900228 saved_herrno = statp->res_h_errno;
229 tried_as_is++;
230 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900231
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900232 /*
233 * We do at least one level of search if
234 * - there is no dot and RES_DEFNAME is set, or
235 * - there is at least one dot, there is no trailing dot,
236 * and RES_DNSRCH is set.
237 */
238 if ((!dots && (statp->options & RES_DEFNAMES) != 0U) ||
239 (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0U)) {
240 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900241
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900242 /* Unfortunately we need to load network-specific info
243 * (dns servers, search domains) before
244 * the domain stuff is tried. Will have a better
245 * fix after thread pools are used as this will
246 * be loaded once for the thread instead of each
247 * time a query is tried.
248 */
249 _resolv_populate_res_for_net(statp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900250
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900251 for (domain = (const char* const*) statp->dnsrch; *domain && !done; domain++) {
252 searched = 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900253
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900254 if (domain[0][0] == '\0' || (domain[0][0] == '.' && domain[0][1] == '\0'))
255 root_on_list++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900256
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900257 ret = res_nquerydomain(statp, name, *domain, cl, type, answer, anslen);
258 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +0900259
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900260 /*
261 * If no server present, give up.
262 * If name isn't found in this domain,
263 * keep trying higher domains in the search list
264 * (if that's enabled).
265 * On a NO_DATA error, keep trying, otherwise
266 * a wildcard entry of another type could keep us
267 * from finding this entry higher in the domain.
268 * If we get some other error (negative answer or
269 * server failure), then stop searching up,
270 * but try the input name below in case it's
271 * fully-qualified.
272 */
273 if (errno == ECONNREFUSED) {
274 RES_SET_H_ERRNO(statp, TRY_AGAIN);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900275 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900276 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900277
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900278 switch (statp->res_h_errno) {
279 case NO_DATA:
280 got_nodata++;
281 /* FALLTHROUGH */
282 case HOST_NOT_FOUND:
283 /* keep trying */
284 break;
285 case TRY_AGAIN:
286 if (hp->rcode == SERVFAIL) {
287 /* try next search element, if any */
288 got_servfail++;
289 break;
290 }
291 /* FALLTHROUGH */
292 default:
293 /* anything else implies that we're done */
294 done++;
295 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900296
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900297 /* if we got here for some reason other than DNSRCH,
298 * we only wanted one iteration of the loop, so stop.
299 */
300 if ((statp->options & RES_DNSRCH) == 0U) done++;
301 }
302 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900303
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900304 /*
305 * If the query has not already been tried as is then try it
306 * unless RES_NOTLDQUERY is set and there were no dots.
307 */
308 if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) &&
309 !(tried_as_is || root_on_list)) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900310 ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen);
311 if (ret > 0) return ret;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900312 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900313
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900314 /* if we got here, we didn't satisfy the search.
315 * if we did an initial full query, return that query's H_ERRNO
316 * (note that we wouldn't be here if that query had succeeded).
317 * else if we ever got a nodata, send that back as the reason.
318 * else send back meaningless H_ERRNO, that being the one from
319 * the last DNSRCH we did.
320 */
321 if (saved_herrno != -1)
322 RES_SET_H_ERRNO(statp, saved_herrno);
323 else if (got_nodata)
324 RES_SET_H_ERRNO(statp, NO_DATA);
325 else if (got_servfail)
326 RES_SET_H_ERRNO(statp, TRY_AGAIN);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900327 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900328}
329
330/*
331 * Perform a call on res_query on the concatenation of name and domain,
332 * removing a trailing dot from name if domain is NULL.
333 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900334int res_nquerydomain(res_state statp, const char* name, const char* domain,
335 int cl, int type, /* class and type of query */
336 u_char* answer, /* buffer to put answer */
337 int anslen) /* size of answer */
Bernie Innocenti55864192018-08-30 04:05:20 +0900338{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900339 char nbuf[MAXDNAME];
340 const char* longname = nbuf;
341 int n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +0900342
343#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900344 if (statp->options & RES_DEBUG)
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900345 printf(";; res_nquerydomain(%s, %s, %d, %d)\n", name, domain ? domain : "<Nil>", cl,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900346 type);
Bernie Innocenti55864192018-08-30 04:05:20 +0900347#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900348 if (domain == NULL) {
349 /*
350 * Check for trailing '.';
351 * copy without '.' if present.
352 */
353 n = strlen(name);
354 if (n >= MAXDNAME) {
355 RES_SET_H_ERRNO(statp, NO_RECOVERY);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900356 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900357 }
358 n--;
359 if (n >= 0 && name[n] == '.') {
360 strncpy(nbuf, name, (size_t) n);
361 nbuf[n] = '\0';
362 } else
363 longname = name;
364 } else {
365 n = strlen(name);
366 d = strlen(domain);
367 if (n + d + 1 >= MAXDNAME) {
368 RES_SET_H_ERRNO(statp, NO_RECOVERY);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900369 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900370 }
371 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
372 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900373 return res_nquery(statp, longname, cl, type, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900374}
375
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900376const char* res_hostalias(const res_state /*statp*/, const char* /*name*/, char* /*dst*/,
377 size_t /*siz*/) {
378 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900379}