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.c405
1 files changed, 405 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..9c1aef1218
--- /dev/null
+++ b/src/lib/libc/net/res_query.c
@@ -0,0 +1,405 @@
1/* $OpenBSD: res_query.c,v 1.19 2002/06/27 09:55:49 itojun 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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
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 * --Copyright--
56 */
57
58#if defined(LIBC_SCCS) && !defined(lint)
59#if 0
60static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93";
61static char rcsid[] = "$From: res_query.c,v 8.9 1996/09/22 00:13:28 vixie Exp $";
62#else
63static char rcsid[] = "$OpenBSD: res_query.c,v 1.19 2002/06/27 09:55:49 itojun Exp $";
64#endif
65#endif /* LIBC_SCCS and not lint */
66
67#include <sys/types.h>
68#include <sys/param.h>
69#include <netinet/in.h>
70#include <arpa/inet.h>
71#include <arpa/nameser.h>
72
73#include <stdio.h>
74#include <netdb.h>
75#include <resolv.h>
76#include <ctype.h>
77#include <errno.h>
78#include <stdlib.h>
79#include <string.h>
80#include <unistd.h>
81
82#if PACKETSZ > 1024
83#define MAXPACKET PACKETSZ
84#else
85#define MAXPACKET 1024
86#endif
87
88const char *hostalias(const char *);
89int h_errno;
90extern int res_opt(int, u_char *, int, int);
91
92/*
93 * Formulate a normal query, send, and await answer.
94 * Returned answer is placed in supplied buffer "answer".
95 * Perform preliminary check of answer, returning success only
96 * if no error is indicated and the answer count is nonzero.
97 * Return the size of the response on success, -1 on error.
98 * Error number is left in h_errno.
99 *
100 * Caller must parse answer and determine whether it answers the question.
101 */
102int
103res_query(name, class, type, answer, anslen)
104 const char *name; /* domain name */
105 int class, type; /* class and type of query */
106 u_char *answer; /* buffer to put answer */
107 int anslen; /* size of answer buffer */
108{
109 u_char buf[MAXPACKET];
110 register HEADER *hp = (HEADER *) answer;
111 int n;
112
113 hp->rcode = NOERROR; /* default */
114
115 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
116 h_errno = NETDB_INTERNAL;
117 return (-1);
118 }
119#ifdef DEBUG
120 if (_res.options & RES_DEBUG)
121 printf(";; res_query(%s, %d, %d)\n", name, class, type);
122#endif
123
124 n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL,
125 buf, sizeof(buf));
126 if (n > 0 && ((_res.options & RES_USE_EDNS0) ||
127 (_res.options & RES_USE_DNSSEC))) {
128 n = res_opt(n, buf, sizeof(buf), anslen);
129 }
130
131 if (n <= 0) {
132#ifdef DEBUG
133 if (_res.options & RES_DEBUG)
134 printf(";; res_query: mkquery failed\n");
135#endif
136 h_errno = NO_RECOVERY;
137 return (n);
138 }
139 n = res_send(buf, n, answer, anslen);
140 if (n < 0) {
141#ifdef DEBUG
142 if (_res.options & RES_DEBUG)
143 printf(";; res_query: send error\n");
144#endif
145 h_errno = TRY_AGAIN;
146 return (n);
147 }
148
149 if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
150#ifdef DEBUG
151 if (_res.options & RES_DEBUG)
152 printf(";; rcode = %u, ancount=%u\n", hp->rcode,
153 ntohs(hp->ancount));
154#endif
155 switch (hp->rcode) {
156 case NXDOMAIN:
157 h_errno = HOST_NOT_FOUND;
158 break;
159 case SERVFAIL:
160 h_errno = TRY_AGAIN;
161 break;
162 case NOERROR:
163 h_errno = NO_DATA;
164 break;
165 case FORMERR:
166 case NOTIMP:
167 case REFUSED:
168 default:
169 h_errno = NO_RECOVERY;
170 break;
171 }
172 return (-1);
173 }
174 return (n);
175}
176
177/*
178 * Formulate a normal query, send, and retrieve answer in supplied buffer.
179 * Return the size of the response on success, -1 on error.
180 * If enabled, implement search rules until answer or unrecoverable failure
181 * is detected. Error code, if any, is left in h_errno.
182 */
183int
184res_search(name, class, type, answer, anslen)
185 const char *name; /* domain name */
186 int class, type; /* class and type of query */
187 u_char *answer; /* buffer to put answer */
188 int anslen; /* size of answer */
189{
190 register const char *cp, * const *domain;
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 ((_res.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 >= _res.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 && (_res.options & RES_DEFNAMES)) ||
235 (dots && !trailing_dot && (_res.options & RES_DNSRCH))) {
236 int done = 0;
237
238 for (domain = (const char * const *)_res.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 (!(_res.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 char nbuf[MAXDNAME*2+1+1];
330 const char *longname = nbuf;
331 int n;
332
333 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
334 h_errno = NETDB_INTERNAL;
335 return (-1);
336 }
337#ifdef DEBUG
338 if (_res.options & RES_DEBUG)
339 printf(";; res_querydomain(%s, %s, %d, %d)\n",
340 name, domain?domain:"<Nil>", class, type);
341#endif
342 if (domain == NULL) {
343 /*
344 * Check for trailing '.';
345 * copy without '.' if present.
346 */
347 n = strlen(name) - 1;
348 if (n != (0 - 1) && name[n] == '.' && n < sizeof(nbuf) - 1) {
349 bcopy(name, nbuf, n);
350 nbuf[n] = '\0';
351 } else
352 longname = name;
353 } else
354 snprintf(nbuf, sizeof nbuf, "%.*s.%.*s",
355 MAXDNAME, name, MAXDNAME, domain);
356
357 return (res_query(longname, class, type, answer, anslen));
358}
359
360const char *
361hostalias(name)
362 register const char *name;
363{
364 register char *cp1, *cp2;
365 FILE *fp;
366 char *file;
367 char buf[BUFSIZ];
368 static char abuf[MAXDNAME];
369 size_t len;
370
371 if (_res.options & RES_NOALIASES)
372 return (NULL);
373 file = getenv("HOSTALIASES");
374 if (issetugid() != 0 || file == NULL || (fp = fopen(file, "r")) == NULL)
375 return (NULL);
376 setbuf(fp, NULL);
377 while ((cp1 = fgetln(fp, &len)) != NULL) {
378 if (cp1[len-1] == '\n')
379 len--;
380 if (len >= sizeof(buf) || len == 0)
381 continue;
382 (void)memcpy(buf, cp1, len);
383 buf[len] = '\0';
384
385 for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1)
386 ;
387 if (!*cp1)
388 break;
389 *cp1 = '\0';
390 if (!strcasecmp(buf, name)) {
391 while (isspace(*++cp1))
392 ;
393 if (!*cp1)
394 break;
395 for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2)
396 ;
397 *cp2 = '\0';
398 strlcpy(abuf, cp1, sizeof(abuf));
399 fclose(fp);
400 return (abuf);
401 }
402 }
403 fclose(fp);
404 return (NULL);
405}