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.c400
1 files changed, 400 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..c8a45a5df5
--- /dev/null
+++ b/src/lib/libc/net/res_query.c
@@ -0,0 +1,400 @@
1/* $OpenBSD: res_query.c,v 1.25 2007/05/16 04:14:23 ray 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#include <sys/types.h>
55#include <sys/param.h>
56#include <netinet/in.h>
57#include <arpa/inet.h>
58#include <arpa/nameser.h>
59
60#include <stdio.h>
61#include <netdb.h>
62#include <resolv.h>
63#include <ctype.h>
64#include <errno.h>
65#include <stdlib.h>
66#include <string.h>
67#include <unistd.h>
68
69#include "thread_private.h"
70
71#if PACKETSZ > 1024
72#define MAXPACKET PACKETSZ
73#else
74#define MAXPACKET 1024
75#endif
76
77const char *hostalias(const char *);
78int h_errno;
79extern int res_opt(int, u_char *, int, int);
80
81/*
82 * Formulate a normal query, send, and await answer.
83 * Returned answer is placed in supplied buffer "answer".
84 * Perform preliminary check of answer, returning success only
85 * if no error is indicated and the answer count is nonzero.
86 * Return the size of the response on success, -1 on error.
87 * Error number is left in h_errno.
88 *
89 * Caller must parse answer and determine whether it answers the question.
90 */
91int
92res_query(const char *name,
93 int class, /* domain name */
94 int type, /* class and type of query */
95 u_char *answer, /* buffer to put answer */
96 int anslen) /* size of answer buffer */
97{
98 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
99 u_char buf[MAXPACKET];
100 HEADER *hp = (HEADER *) answer;
101 int n;
102
103 hp->rcode = NOERROR; /* default */
104
105 if (_res_init(0) == -1) {
106 h_errno = NETDB_INTERNAL;
107 return (-1);
108 }
109#ifdef DEBUG
110 if (_resp->options & RES_DEBUG)
111 printf(";; res_query(%s, %d, %d)\n", name, class, type);
112#endif
113
114 n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,
115 buf, sizeof(buf));
116 if (n > 0 && ((_resp->options & RES_USE_EDNS0) ||
117 (_resp->options & RES_USE_DNSSEC))) {
118 n = res_opt(n, buf, sizeof(buf), anslen);
119 }
120
121 if (n <= 0) {
122#ifdef DEBUG
123 if (_resp->options & RES_DEBUG)
124 printf(";; res_query: mkquery failed\n");
125#endif
126 h_errno = NO_RECOVERY;
127 return (n);
128 }
129 n = res_send(buf, n, answer, anslen);
130 if (n < 0) {
131#ifdef DEBUG
132 if (_resp->options & RES_DEBUG)
133 printf(";; res_query: send error\n");
134#endif
135 h_errno = TRY_AGAIN;
136 return (n);
137 }
138
139 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
140#ifdef DEBUG
141 if (_resp->options & RES_DEBUG)
142 printf(";; rcode = %u, ancount=%u\n", hp->rcode,
143 ntohs(hp->ancount));
144#endif
145 switch (hp->rcode) {
146 case NXDOMAIN:
147 h_errno = HOST_NOT_FOUND;
148 break;
149 case SERVFAIL:
150 h_errno = TRY_AGAIN;
151 break;
152 case NOERROR:
153 h_errno = NO_DATA;
154 break;
155 case FORMERR:
156 case NOTIMP:
157 case REFUSED:
158 default:
159 h_errno = NO_RECOVERY;
160 break;
161 }
162 return (-1);
163 }
164 return (n);
165}
166
167/*
168 * Formulate a normal query, send, and retrieve answer in supplied buffer.
169 * Return the size of the response on success, -1 on error.
170 * If enabled, implement search rules until answer or unrecoverable failure
171 * is detected. Error code, if any, is left in h_errno.
172 */
173int
174res_search(const char *name,
175 int class, /* domain name */
176 int type, /* class and type of query */
177 u_char *answer, /* buffer to put answer */
178 int anslen) /* size of answer */
179{
180 const char *cp, * const *domain;
181 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
182 HEADER *hp = (HEADER *) answer;
183 u_int dots;
184 int trailing_dot, ret, saved_herrno;
185 int got_nodata = 0, got_servfail = 0, tried_as_is = 0;
186
187 if (_res_init(0) == -1) {
188 h_errno = NETDB_INTERNAL;
189 return (-1);
190 }
191 errno = 0;
192 h_errno = HOST_NOT_FOUND; /* default, if we never query */
193 dots = 0;
194 for (cp = name; *cp; cp++)
195 dots += (*cp == '.');
196 trailing_dot = 0;
197 if (cp > name && *--cp == '.')
198 trailing_dot++;
199
200 /*
201 * if there aren't any dots, it could be a user-level alias
202 */
203 if (!dots && (cp = __hostalias(name)) != NULL)
204 return (res_query(cp, class, type, answer, anslen));
205
206 /*
207 * If there are dots in the name already, let's just give it a try
208 * 'as is'. The threshold can be set with the "ndots" option.
209 */
210 saved_herrno = -1;
211 if (dots >= _resp->ndots) {
212 ret = res_querydomain(name, NULL, class, type, answer, anslen);
213 if (ret > 0)
214 return (ret);
215 saved_herrno = h_errno;
216 tried_as_is++;
217 }
218
219 /*
220 * We do at least one level of search if
221 * - there is no dot and RES_DEFNAME is set, or
222 * - there is at least one dot, there is no trailing dot,
223 * and RES_DNSRCH is set.
224 */
225 if ((!dots && (_resp->options & RES_DEFNAMES)) ||
226 (dots && !trailing_dot && (_resp->options & RES_DNSRCH))) {
227 int done = 0;
228
229 for (domain = (const char * const *)_resp->dnsrch;
230 *domain && !done;
231 domain++) {
232
233 ret = res_querydomain(name, *domain, class, type,
234 answer, anslen);
235 if (ret > 0)
236 return (ret);
237
238 /*
239 * If no server present, give up.
240 * If name isn't found in this domain,
241 * keep trying higher domains in the search list
242 * (if that's enabled).
243 * On a NO_DATA error, keep trying, otherwise
244 * a wildcard entry of another type could keep us
245 * from finding this entry higher in the domain.
246 * If we get some other error (negative answer or
247 * server failure), then stop searching up,
248 * but try the input name below in case it's
249 * fully-qualified.
250 */
251 if (errno == ECONNREFUSED) {
252 h_errno = TRY_AGAIN;
253 return (-1);
254 }
255
256 switch (h_errno) {
257 case NO_DATA:
258 got_nodata++;
259 /* FALLTHROUGH */
260 case HOST_NOT_FOUND:
261 /* keep trying */
262 break;
263 case TRY_AGAIN:
264 if (hp->rcode == SERVFAIL) {
265 /* try next search element, if any */
266 got_servfail++;
267 break;
268 }
269 /* FALLTHROUGH */
270 default:
271 /* anything else implies that we're done */
272 done++;
273 }
274
275 /* if we got here for some reason other than DNSRCH,
276 * we only wanted one iteration of the loop, so stop.
277 */
278 if (!(_resp->options & RES_DNSRCH))
279 done++;
280 }
281 }
282
283 /* if we have not already tried the name "as is", do that now.
284 * note that we do this regardless of how many dots were in the
285 * name or whether it ends with a dot.
286 */
287 if (!tried_as_is) {
288 ret = res_querydomain(name, NULL, class, type, answer, anslen);
289 if (ret > 0)
290 return (ret);
291 }
292
293 /* if we got here, we didn't satisfy the search.
294 * if we did an initial full query, return that query's h_errno
295 * (note that we wouldn't be here if that query had succeeded).
296 * else if we ever got a nodata, send that back as the reason.
297 * else send back meaningless h_errno, that being the one from
298 * the last DNSRCH we did.
299 */
300 if (saved_herrno != -1)
301 h_errno = saved_herrno;
302 else if (got_nodata)
303 h_errno = NO_DATA;
304 else if (got_servfail)
305 h_errno = TRY_AGAIN;
306 return (-1);
307}
308
309/*
310 * Perform a call on res_query on the concatenation of name and domain,
311 * removing a trailing dot from name if domain is NULL.
312 */
313int
314res_querydomain(const char *name,
315 const char *domain,
316 int class, /* class and type of query */
317 int type,
318 u_char *answer, /* buffer to put answer */
319 int anslen) /* size of answer */
320{
321#ifdef DEBUG
322 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
323#endif
324 char nbuf[MAXDNAME*2+1+1];
325 const char *longname = nbuf;
326 int n;
327
328 if (_res_init(0) == -1) {
329 h_errno = NETDB_INTERNAL;
330 return (-1);
331 }
332#ifdef DEBUG
333 if (_resp->options & RES_DEBUG)
334 printf(";; res_querydomain(%s, %s, %d, %d)\n",
335 name, domain?domain:"<Nil>", class, type);
336#endif
337 if (domain == NULL) {
338 /*
339 * Check for trailing '.';
340 * copy without '.' if present.
341 */
342 n = strlen(name) - 1;
343 if (n != (0 - 1) && name[n] == '.' && n < sizeof(nbuf) - 1) {
344 bcopy(name, nbuf, n);
345 nbuf[n] = '\0';
346 } else
347 longname = name;
348 } else
349 snprintf(nbuf, sizeof nbuf, "%.*s.%.*s",
350 MAXDNAME, name, MAXDNAME, domain);
351
352 return (res_query(longname, class, type, answer, anslen));
353}
354
355const char *
356hostalias(const char *name)
357{
358 struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res);
359 char *cp1, *cp2;
360 FILE *fp;
361 char *file;
362 char buf[BUFSIZ];
363 static char abuf[MAXDNAME];
364 size_t len;
365
366 if (_resp->options & RES_NOALIASES)
367 return (NULL);
368 file = getenv("HOSTALIASES");
369 if (issetugid() != 0 || file == NULL || (fp = fopen(file, "r")) == NULL)
370 return (NULL);
371 setbuf(fp, NULL);
372 while ((cp1 = fgetln(fp, &len)) != NULL) {
373 if (cp1[len-1] == '\n')
374 len--;
375 if (len >= sizeof(buf) || len == 0)
376 continue;
377 (void)memcpy(buf, cp1, len);
378 buf[len] = '\0';
379
380 for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)
381 ;
382 if (!*cp1)
383 break;
384 *cp1 = '\0';
385 if (!strcasecmp(buf, name)) {
386 while (isspace(*++cp1))
387 ;
388 if (!*cp1)
389 break;
390 for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)
391 ;
392 *cp2 = '\0';
393 strlcpy(abuf, cp1, sizeof(abuf));
394 fclose(fp);
395 return (abuf);
396 }
397 }
398 fclose(fp);
399 return (NULL);
400}