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