summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/res_query.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/res_query.c')
-rw-r--r--src/lib/libc/net/res_query.c407
1 files changed, 407 insertions, 0 deletions
diff --git a/src/lib/libc/net/res_query.c b/src/lib/libc/net/res_query.c
new file mode 100644
index 0000000000..3cdb7a7477
--- /dev/null
+++ b/src/lib/libc/net/res_query.c
@@ -0,0 +1,407 @@
1/* $OpenBSD: res_query.c,v 1.21 2003/06/02 20:18:36 millert Exp $ */
2
3/*
4 * ++Copyright++ 1988, 1993
5 * -
6 * Copyright (c) 1988, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 * -
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53
54#if defined(LIBC_SCCS) && !defined(lint)
55#if 0
56static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93";
57static char rcsid[] = "$From: res_query.c,v 8.9 1996/09/22 00:13:28 vixie Exp $";
58#else
59static char rcsid[] = "$OpenBSD: res_query.c,v 1.21 2003/06/02 20:18:36 millert Exp $";
60#endif
61#endif /* LIBC_SCCS and not lint */
62
63#include <sys/types.h>
64#include <sys/param.h>
65#include <netinet/in.h>
66#include <arpa/inet.h>
67#include <arpa/nameser.h>
68
69#include <stdio.h>
70#include <netdb.h>
71#include <resolv.h>
72#include <ctype.h>
73#include <errno.h>
74#include <stdlib.h>
75#include <string.h>
76#include <unistd.h>
77
78#include "thread_private.h"
79
80#if PACKETSZ > 1024
81#define MAXPACKET PACKETSZ
82#else
83#define MAXPACKET 1024
84#endif
85
86const char *hostalias(const char *);
87int h_errno;
88extern int res_opt(int, u_char *, int, int);
89
90/*
91 * Formulate a normal query, send, and await answer.
92 * Returned answer is placed in supplied buffer "answer".
93 * Perform preliminary check of answer, returning success only
94 * if no error is indicated and the answer count is nonzero.
95 * Return the size of the response on success, -1 on error.
96 * Error number is left in h_errno.
97 *
98 * Caller must parse answer and determine whether it answers the question.
99 */
100int
101res_query(name, class, type, answer, anslen)
102 const char *name; /* domain name */
103 int class, type; /* class and type of query */
104 u_char *answer; /* buffer to put answer */
105 int anslen; /* size of answer buffer */
106{
107 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
108 u_char buf[MAXPACKET];
109 register HEADER *hp = (HEADER *) answer;
110 int n;
111
112 hp->rcode = NOERROR; /* default */
113
114 if ((_resp->options & RES_INIT) == 0 && res_init() == -1) {
115 h_errno = NETDB_INTERNAL;
116 return (-1);
117 }
118#ifdef DEBUG
119 if (_resp->options & RES_DEBUG)
120 printf(";; res_query(%s, %d, %d)\n", name, class, type);
121#endif
122
123 n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,
124 buf, sizeof(buf));
125 if (n > 0 && ((_resp->options & RES_USE_EDNS0) ||
126 (_resp->options & RES_USE_DNSSEC))) {
127 n = res_opt(n, buf, sizeof(buf), anslen);
128 }
129
130 if (n <= 0) {
131#ifdef DEBUG
132 if (_resp->options & RES_DEBUG)
133 printf(";; res_query: mkquery failed\n");
134#endif
135 h_errno = NO_RECOVERY;
136 return (n);
137 }
138 n = res_send(buf, n, answer, anslen);
139 if (n < 0) {
140#ifdef DEBUG
141 if (_resp->options & RES_DEBUG)
142 printf(";; res_query: send error\n");
143#endif
144 h_errno = TRY_AGAIN;
145 return (n);
146 }
147
148 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
149#ifdef DEBUG
150 if (_resp->options & RES_DEBUG)
151 printf(";; rcode = %u, ancount=%u\n", hp->rcode,
152 ntohs(hp->ancount));
153#endif
154 switch (hp->rcode) {
155 case NXDOMAIN:
156 h_errno = HOST_NOT_FOUND;
157 break;
158 case SERVFAIL:
159 h_errno = TRY_AGAIN;
160 break;
161 case NOERROR:
162 h_errno = NO_DATA;
163 break;
164 case FORMERR:
165 case NOTIMP:
166 case REFUSED:
167 default:
168 h_errno = NO_RECOVERY;
169 break;
170 }
171 return (-1);
172 }
173 return (n);
174}
175
176/*
177 * Formulate a normal query, send, and retrieve answer in supplied buffer.
178 * Return the size of the response on success, -1 on error.
179 * If enabled, implement search rules until answer or unrecoverable failure
180 * is detected. Error code, if any, is left in h_errno.
181 */
182int
183res_search(name, class, type, answer, anslen)
184 const char *name; /* domain name */
185 int class, type; /* class and type of query */
186 u_char *answer; /* buffer to put answer */
187 int anslen; /* size of answer */
188{
189 register const char *cp, * const *domain;
190 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
191 HEADER *hp = (HEADER *) answer;
192 u_int dots;
193 int trailing_dot, ret, saved_herrno;
194 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
195
196 if ((_resp->options & RES_INIT) == 0 && res_init() == -1) {
197 h_errno = NETDB_INTERNAL;
198 return (-1);
199 }
200 errno = 0;
201 h_errno = HOST_NOT_FOUND; /* default, if we never query */
202 dots = 0;
203 for (cp = name; *cp; cp++)
204 dots += (*cp == '.');
205 trailing_dot = 0;
206 if (cp > name && *--cp == '.')
207 trailing_dot++;
208
209 /*
210 * if there aren't any dots, it could be a user-level alias
211 */
212 if (!dots && (cp = __hostalias(name)) != NULL)
213 return (res_query(cp, class, type, answer, anslen));
214
215 /*
216 * If there are dots in the name already, let's just give it a try
217 * 'as is'. The threshold can be set with the "ndots" option.
218 */
219 saved_herrno = -1;
220 if (dots >= _resp->ndots) {
221 ret = res_querydomain(name, NULL, class, type, answer, anslen);
222 if (ret > 0)
223 return (ret);
224 saved_herrno = h_errno;
225 tried_as_is++;
226 }
227
228 /*
229 * We do at least one level of search if
230 * - there is no dot and RES_DEFNAME is set, or
231 * - there is at least one dot, there is no trailing dot,
232 * and RES_DNSRCH is set.
233 */
234 if ((!dots && (_resp->options & RES_DEFNAMES)) ||
235 (dots && !trailing_dot && (_resp->options & RES_DNSRCH))) {
236 int done = 0;
237
238 for (domain = (const char * const *)_resp->dnsrch;
239 *domain && !done;
240 domain++) {
241
242 ret = res_querydomain(name, *domain, class, type,
243 answer, anslen);
244 if (ret > 0)
245 return (ret);
246
247 /*
248 * If no server present, give up.
249 * If name isn't found in this domain,
250 * keep trying higher domains in the search list
251 * (if that's enabled).
252 * On a NO_DATA error, keep trying, otherwise
253 * a wildcard entry of another type could keep us
254 * from finding this entry higher in the domain.
255 * If we get some other error (negative answer or
256 * server failure), then stop searching up,
257 * but try the input name below in case it's
258 * fully-qualified.
259 */
260 if (errno == ECONNREFUSED) {
261 h_errno = TRY_AGAIN;
262 return (-1);
263 }
264
265 switch (h_errno) {
266 case NO_DATA:
267 got_nodata++;
268 /* FALLTHROUGH */
269 case HOST_NOT_FOUND:
270 /* keep trying */
271 break;
272 case TRY_AGAIN:
273 if (hp->rcode == SERVFAIL) {
274 /* try next search element, if any */
275 got_servfail++;
276 break;
277 }
278 /* FALLTHROUGH */
279 default:
280 /* anything else implies that we're done */
281 done++;
282 }
283
284 /* if we got here for some reason other than DNSRCH,
285 * we only wanted one iteration of the loop, so stop.
286 */
287 if (!(_resp->options & RES_DNSRCH))
288 done++;
289 }
290 }
291
292 /* if we have not already tried the name "as is", do that now.
293 * note that we do this regardless of how many dots were in the
294 * name or whether it ends with a dot.
295 */
296 if (!tried_as_is) {
297 ret = res_querydomain(name, NULL, class, type, answer, anslen);
298 if (ret > 0)
299 return (ret);
300 }
301
302 /* if we got here, we didn't satisfy the search.
303 * if we did an initial full query, return that query's h_errno
304 * (note that we wouldn't be here if that query had succeeded).
305 * else if we ever got a nodata, send that back as the reason.
306 * else send back meaningless h_errno, that being the one from
307 * the last DNSRCH we did.
308 */
309 if (saved_herrno != -1)
310 h_errno = saved_herrno;
311 else if (got_nodata)
312 h_errno = NO_DATA;
313 else if (got_servfail)
314 h_errno = TRY_AGAIN;
315 return (-1);
316}
317
318/*
319 * Perform a call on res_query on the concatenation of name and domain,
320 * removing a trailing dot from name if domain is NULL.
321 */
322int
323res_querydomain(name, domain, class, type, answer, anslen)
324 const char *name, *domain;
325 int class, type; /* class and type of query */
326 u_char *answer; /* buffer to put answer */
327 int anslen; /* size of answer */
328{
329 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
330 char nbuf[MAXDNAME*2+1+1];
331 const char *longname = nbuf;
332 int n;
333
334 if ((_resp->options & RES_INIT) == 0 && res_init() == -1) {
335 h_errno = NETDB_INTERNAL;
336 return (-1);
337 }
338#ifdef DEBUG
339 if (_resp->options & RES_DEBUG)
340 printf(";; res_querydomain(%s, %s, %d, %d)\n",
341 name, domain?domain:"<Nil>", class, type);
342#endif
343 if (domain == NULL) {
344 /*
345 * Check for trailing '.';
346 * copy without '.' if present.
347 */
348 n = strlen(name) - 1;
349 if (n != (0 - 1) && name[n] == '.' && n < sizeof(nbuf) - 1) {
350 bcopy(name, nbuf, n);
351 nbuf[n] = '\0';
352 } else
353 longname = name;
354 } else
355 snprintf(nbuf, sizeof nbuf, "%.*s.%.*s",
356 MAXDNAME, name, MAXDNAME, domain);
357
358 return (res_query(longname, class, type, answer, anslen));
359}
360
361const char *
362hostalias(name)
363 register const char *name;
364{
365 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
366 register char *cp1, *cp2;
367 FILE *fp;
368 char *file;
369 char buf[BUFSIZ];
370 static char abuf[MAXDNAME];
371 size_t len;
372
373 if (_resp->options & RES_NOALIASES)
374 return (NULL);
375 file = getenv("HOSTALIASES");
376 if (issetugid() != 0 || file == NULL || (fp = fopen(file, "r")) == NULL)
377 return (NULL);
378 setbuf(fp, NULL);
379 while ((cp1 = fgetln(fp, &len)) != NULL) {
380 if (cp1[len-1] == '\n')
381 len--;
382 if (len >= sizeof(buf) || len == 0)
383 continue;
384 (void)memcpy(buf, cp1, len);
385 buf[len] = '\0';
386
387 for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)
388 ;
389 if (!*cp1)
390 break;
391 *cp1 = '\0';
392 if (!strcasecmp(buf, name)) {
393 while (isspace(*++cp1))
394 ;
395 if (!*cp1)
396 break;
397 for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)
398 ;
399 *cp2 = '\0';
400 strlcpy(abuf, cp1, sizeof(abuf));
401 fclose(fp);
402 return (abuf);
403 }
404 }
405 fclose(fp);
406 return (NULL);
407}