blob: a9e4220218ec15ddd1676c4188e8feddc493a1b9 [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 <stdio.h>
82#include <stdlib.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090083#include <string.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090084#include <unistd.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090085
Bernie Innocenti189eb502018-10-01 23:10:18 +090086#include "resolv_cache.h"
87#include "resolv_private.h"
88
Bernie Innocenti55864192018-08-30 04:05:20 +090089/* Options. Leave them on. */
90#ifndef DEBUG
91#define DEBUG
92#endif
93
94#if PACKETSZ > 1024
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090095#define MAXPACKET PACKETSZ
Bernie Innocenti55864192018-08-30 04:05:20 +090096#else
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090097#define MAXPACKET 1024
Bernie Innocenti55864192018-08-30 04:05:20 +090098#endif
99
100/*
101 * Formulate a normal query, send, and await answer.
102 * Returned answer is placed in supplied buffer "answer".
103 * Perform preliminary check of answer, returning success only
104 * if no error is indicated and the answer count is nonzero.
105 * Return the size of the response on success, -1 on error.
106 * Error number is left in H_ERRNO.
107 *
108 * Caller must parse answer and determine whether it answers the question.
109 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900110int res_nquery(res_state statp, const char* name, // domain name
111 int cl, int type, // class and type of query
112 u_char* answer, // buffer to put answer
113 int anslen) // size of answer buffer
Bernie Innocenti55864192018-08-30 04:05:20 +0900114{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900115 u_char buf[MAXPACKET];
116 HEADER* hp = (HEADER*) (void*) answer;
117 int n;
118 u_int oflags;
Bernie Innocenti55864192018-08-30 04:05:20 +0900119
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900120 oflags = statp->_flags;
Bernie Innocenti55864192018-08-30 04:05:20 +0900121
122again:
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900123 hp->rcode = NOERROR; /* default */
Bernie Innocenti55864192018-08-30 04:05:20 +0900124
125#ifdef DEBUG
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900126 if (statp->options & RES_DEBUG) printf(";; res_query(%s, %d, %d)\n", name, cl, type);
Bernie Innocenti55864192018-08-30 04:05:20 +0900127#endif
128
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900129 n = res_nmkquery(statp, QUERY, name, cl, type, NULL, 0, NULL, buf, sizeof(buf));
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 Innocentif12d5bb2018-08-31 14:09:46 +0900133 if (n <= 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900134#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900135 if (statp->options & RES_DEBUG) printf(";; res_query: mkquery failed\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900136#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900137 RES_SET_H_ERRNO(statp, NO_RECOVERY);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900138 return n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900139 }
140 n = res_nsend(statp, buf, n, answer, anslen);
141 if (n < 0) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900142 /* if the query choked with EDNS0, retry without EDNS0 */
143 if ((statp->options & (RES_USE_EDNS0 | RES_USE_DNSSEC)) != 0U &&
144 ((oflags ^ statp->_flags) & RES_F_EDNS0ERR) != 0) {
145 statp->_flags |= RES_F_EDNS0ERR;
146 if (statp->options & RES_DEBUG) printf(";; res_nquery: retry without EDNS0\n");
147 goto again;
148 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900149#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900150 if (statp->options & RES_DEBUG) printf(";; res_query: send error\n");
Bernie Innocenti55864192018-08-30 04:05:20 +0900151#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900152 RES_SET_H_ERRNO(statp, TRY_AGAIN);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900153 return n;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900154 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900155
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900156 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
Bernie Innocenti55864192018-08-30 04:05:20 +0900157#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900158 if (statp->options & RES_DEBUG)
159 printf(";; rcode = (%s), counts = an:%d ns:%d ar:%d\n", p_rcode(hp->rcode),
160 ntohs(hp->ancount), ntohs(hp->nscount), ntohs(hp->arcount));
Bernie Innocenti55864192018-08-30 04:05:20 +0900161#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900162 switch (hp->rcode) {
163 case NXDOMAIN:
164 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND);
165 break;
166 case SERVFAIL:
167 RES_SET_H_ERRNO(statp, TRY_AGAIN);
168 break;
169 case NOERROR:
170 RES_SET_H_ERRNO(statp, NO_DATA);
171 break;
172 case FORMERR:
173 case NOTIMP:
174 case REFUSED:
175 default:
176 RES_SET_H_ERRNO(statp, NO_RECOVERY);
177 break;
178 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900179 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900180 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900181 return n;
Bernie Innocenti55864192018-08-30 04:05:20 +0900182}
183
184/*
185 * Formulate a normal query, send, and retrieve answer in supplied buffer.
186 * Return the size of the response on success, -1 on error.
187 * If enabled, implement search rules until answer or unrecoverable failure
188 * is detected. Error code, if any, is left in H_ERRNO.
189 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900190int res_nsearch(res_state statp, const char* name, /* domain name */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900191 int cl, int type, /* class and type of query */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900192 u_char* answer, /* buffer to put answer */
193 int anslen) /* size of answer */
Bernie Innocenti55864192018-08-30 04:05:20 +0900194{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900195 const char *cp, *const *domain;
196 HEADER* hp = (HEADER*) (void*) answer;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900197 u_int dots;
198 int trailing_dot, ret, saved_herrno;
199 int got_nodata = 0, got_servfail = 0, root_on_list = 0;
200 int tried_as_is = 0;
201 int searched = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900202
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900203 errno = 0;
204 RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); /* True if we never query. */
Bernie Innocenti55864192018-08-30 04:05:20 +0900205
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900206 dots = 0;
207 for (cp = name; *cp != '\0'; cp++) dots += (*cp == '.');
208 trailing_dot = 0;
209 if (cp > name && *--cp == '.') trailing_dot++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900210
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900211 /*
212 * If there are enough dots in the name, let's just give it a
213 * try 'as is'. The threshold can be set with the "ndots" option.
214 * Also, query 'as is', if there is a trailing dot in the name.
215 */
216 saved_herrno = -1;
217 if (dots >= statp->ndots || trailing_dot) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900218 ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen);
219 if (ret > 0 || trailing_dot) return ret;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900220 saved_herrno = statp->res_h_errno;
221 tried_as_is++;
222 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900223
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900224 /*
225 * We do at least one level of search if
226 * - there is no dot and RES_DEFNAME is set, or
227 * - there is at least one dot, there is no trailing dot,
228 * and RES_DNSRCH is set.
229 */
230 if ((!dots && (statp->options & RES_DEFNAMES) != 0U) ||
231 (dots && !trailing_dot && (statp->options & RES_DNSRCH) != 0U)) {
232 int done = 0;
Bernie Innocenti55864192018-08-30 04:05:20 +0900233
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900234 /* Unfortunately we need to load network-specific info
235 * (dns servers, search domains) before
236 * the domain stuff is tried. Will have a better
237 * fix after thread pools are used as this will
238 * be loaded once for the thread instead of each
239 * time a query is tried.
240 */
241 _resolv_populate_res_for_net(statp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900242
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900243 for (domain = (const char* const*) statp->dnsrch; *domain && !done; domain++) {
244 searched = 1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900245
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900246 if (domain[0][0] == '\0' || (domain[0][0] == '.' && domain[0][1] == '\0'))
247 root_on_list++;
Bernie Innocenti55864192018-08-30 04:05:20 +0900248
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900249 ret = res_nquerydomain(statp, name, *domain, cl, type, answer, anslen);
250 if (ret > 0) return ret;
Bernie Innocenti55864192018-08-30 04:05:20 +0900251
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900252 /*
253 * If no server present, give up.
254 * If name isn't found in this domain,
255 * keep trying higher domains in the search list
256 * (if that's enabled).
257 * On a NO_DATA error, keep trying, otherwise
258 * a wildcard entry of another type could keep us
259 * from finding this entry higher in the domain.
260 * If we get some other error (negative answer or
261 * server failure), then stop searching up,
262 * but try the input name below in case it's
263 * fully-qualified.
264 */
265 if (errno == ECONNREFUSED) {
266 RES_SET_H_ERRNO(statp, TRY_AGAIN);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900267 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900268 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900269
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900270 switch (statp->res_h_errno) {
271 case NO_DATA:
272 got_nodata++;
Bernie Innocenti11cd0202018-10-12 21:27:45 +0900273 break;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900274 case HOST_NOT_FOUND:
275 /* keep trying */
276 break;
277 case TRY_AGAIN:
278 if (hp->rcode == SERVFAIL) {
279 /* try next search element, if any */
280 got_servfail++;
281 break;
282 }
Bernie Innocenti8bb94ba2018-10-10 22:30:12 +0900283 [[fallthrough]];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900284 default:
285 /* anything else implies that we're done */
286 done++;
287 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900288
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900289 /* if we got here for some reason other than DNSRCH,
290 * we only wanted one iteration of the loop, so stop.
291 */
292 if ((statp->options & RES_DNSRCH) == 0U) done++;
293 }
294 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900295
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900296 /*
297 * If the query has not already been tried as is then try it
298 * unless RES_NOTLDQUERY is set and there were no dots.
299 */
300 if ((dots || !searched || (statp->options & RES_NOTLDQUERY) == 0U) &&
301 !(tried_as_is || root_on_list)) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900302 ret = res_nquerydomain(statp, name, NULL, cl, type, answer, anslen);
303 if (ret > 0) return ret;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900304 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900305
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900306 /* if we got here, we didn't satisfy the search.
307 * if we did an initial full query, return that query's H_ERRNO
308 * (note that we wouldn't be here if that query had succeeded).
309 * else if we ever got a nodata, send that back as the reason.
310 * else send back meaningless H_ERRNO, that being the one from
311 * the last DNSRCH we did.
312 */
313 if (saved_herrno != -1)
314 RES_SET_H_ERRNO(statp, saved_herrno);
315 else if (got_nodata)
316 RES_SET_H_ERRNO(statp, NO_DATA);
317 else if (got_servfail)
318 RES_SET_H_ERRNO(statp, TRY_AGAIN);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900319 return -1;
Bernie Innocenti55864192018-08-30 04:05:20 +0900320}
321
322/*
323 * Perform a call on res_query on the concatenation of name and domain,
324 * removing a trailing dot from name if domain is NULL.
325 */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900326int res_nquerydomain(res_state statp, const char* name, const char* domain,
327 int cl, int type, /* class and type of query */
328 u_char* answer, /* buffer to put answer */
329 int anslen) /* size of answer */
Bernie Innocenti55864192018-08-30 04:05:20 +0900330{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900331 char nbuf[MAXDNAME];
332 const char* longname = nbuf;
333 int n, d;
Bernie Innocenti55864192018-08-30 04:05:20 +0900334
335#ifdef DEBUG
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900336 if (statp->options & RES_DEBUG)
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900337 printf(";; res_nquerydomain(%s, %s, %d, %d)\n", name, domain ? domain : "<Nil>", cl,
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900338 type);
Bernie Innocenti55864192018-08-30 04:05:20 +0900339#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900340 if (domain == NULL) {
341 /*
342 * Check for trailing '.';
343 * copy without '.' if present.
344 */
345 n = strlen(name);
346 if (n >= MAXDNAME) {
347 RES_SET_H_ERRNO(statp, NO_RECOVERY);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900348 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900349 }
350 n--;
351 if (n >= 0 && name[n] == '.') {
352 strncpy(nbuf, name, (size_t) n);
353 nbuf[n] = '\0';
354 } else
355 longname = name;
356 } else {
357 n = strlen(name);
358 d = strlen(domain);
359 if (n + d + 1 >= MAXDNAME) {
360 RES_SET_H_ERRNO(statp, NO_RECOVERY);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900361 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900362 }
363 snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain);
364 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900365 return res_nquery(statp, longname, cl, type, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900366}