]>
Commit | Line | Data |
---|---|---|
28f540f4 RM |
1 | /* |
2 | * ++Copyright++ 1985, 1988, 1993 | |
3 | * - | |
4 | * Copyright (c) 1985, 1988, 1993 | |
5 | * The Regents of the University of California. All rights reserved. | |
fa0bc87c | 6 | * |
28f540f4 RM |
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. | |
fa0bc87c | 22 | * |
28f540f4 RM |
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 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | |
fa0bc87c | 36 | * |
28f540f4 RM |
37 | * Permission to use, copy, modify, and distribute this software for any |
38 | * purpose with or without fee is hereby granted, provided that the above | |
39 | * copyright notice and this permission notice appear in all copies, and that | |
40 | * the name of Digital Equipment Corporation not be used in advertising or | |
41 | * publicity pertaining to distribution of the document or software without | |
42 | * specific, written prior permission. | |
fa0bc87c | 43 | * |
28f540f4 RM |
44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | |
46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | |
47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
51 | * SOFTWARE. | |
52 | * - | |
53 | * --Copyright-- | |
54 | */ | |
55 | ||
56 | #if defined(LIBC_SCCS) && !defined(lint) | |
57 | static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93"; | |
58 | static char rcsid[] = "$Id$"; | |
59 | #endif /* LIBC_SCCS and not lint */ | |
60 | ||
df21c858 | 61 | #include <sys/types.h> |
28f540f4 RM |
62 | #include <sys/param.h> |
63 | #include <sys/socket.h> | |
64 | #include <netinet/in.h> | |
65 | #include <arpa/inet.h> | |
66 | #include <arpa/nameser.h> | |
67 | ||
68 | #include <stdio.h> | |
69 | #include <netdb.h> | |
70 | #include <resolv.h> | |
71 | #include <ctype.h> | |
72 | #include <errno.h> | |
73 | #include <syslog.h> | |
74 | ||
75 | #ifndef LOG_AUTH | |
76 | # define LOG_AUTH 0 | |
77 | #endif | |
78 | ||
79 | #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */ | |
80 | ||
fa0bc87c | 81 | #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6) |
df21c858 | 82 | # include <stdlib.h> |
28f540f4 RM |
83 | # include <string.h> |
84 | #else | |
85 | # include "../conf/portability.h" | |
86 | #endif | |
df21c858 | 87 | |
28f540f4 RM |
88 | #if defined(USE_OPTIONS_H) |
89 | # include <../conf/options.h> | |
90 | #endif | |
91 | ||
613a76ff RM |
92 | #ifdef SPRINTF_CHAR |
93 | # define SPRINTF(x) strlen(sprintf/**/x) | |
94 | #else | |
df21c858 | 95 | # define SPRINTF(x) ((size_t)sprintf x) |
613a76ff RM |
96 | #endif |
97 | ||
28f540f4 RM |
98 | #define MAXALIASES 35 |
99 | #define MAXADDRS 35 | |
100 | ||
101 | static const char AskedForGot[] = | |
102 | "gethostby*.getanswer: asked for \"%s\", got \"%s\""; | |
103 | ||
104 | static char *h_addr_ptrs[MAXADDRS + 1]; | |
105 | ||
106 | static struct hostent host; | |
107 | static char *host_aliases[MAXALIASES]; | |
5f0e6fc7 | 108 | static char hostbuf[8*1024]; |
fa0bc87c | 109 | static u_char host_addr[16]; /* IPv4 or IPv6 */ |
28f540f4 RM |
110 | static FILE *hostf = NULL; |
111 | static int stayopen = 0; | |
112 | ||
fa0bc87c RM |
113 | static void map_v4v6_address __P((const char *src, char *dst)); |
114 | static void map_v4v6_hostent __P((struct hostent *hp, char **bp, int *len)); | |
115 | ||
28f540f4 RM |
116 | #ifdef RESOLVSORT |
117 | static void addrsort __P((char **, int)); | |
118 | #endif | |
119 | ||
120 | #if PACKETSZ > 1024 | |
121 | #define MAXPACKET PACKETSZ | |
122 | #else | |
123 | #define MAXPACKET 1024 | |
124 | #endif | |
125 | ||
126 | typedef union { | |
127 | HEADER hdr; | |
128 | u_char buf[MAXPACKET]; | |
129 | } querybuf; | |
130 | ||
131 | typedef union { | |
132 | int32_t al; | |
133 | char ac; | |
134 | } align; | |
135 | ||
136 | extern int h_errno; | |
137 | ||
138 | #ifdef DEBUG | |
139 | static void | |
140 | dprintf(msg, num) | |
141 | char *msg; | |
142 | int num; | |
143 | { | |
144 | if (_res.options & RES_DEBUG) { | |
145 | int save = errno; | |
146 | ||
147 | printf(msg, num); | |
148 | errno = save; | |
149 | } | |
150 | } | |
151 | #else | |
152 | # define dprintf(msg, num) /*nada*/ | |
153 | #endif | |
154 | ||
155 | static struct hostent * | |
fa0bc87c | 156 | getanswer(answer, anslen, qname, qtype) |
28f540f4 RM |
157 | const querybuf *answer; |
158 | int anslen; | |
159 | const char *qname; | |
fa0bc87c | 160 | int qtype; |
28f540f4 RM |
161 | { |
162 | register const HEADER *hp; | |
163 | register const u_char *cp; | |
164 | register int n; | |
165 | const u_char *eom; | |
166 | char *bp, **ap, **hap; | |
167 | int type, class, buflen, ancount, qdcount; | |
168 | int haveanswer, had_error; | |
169 | int toobig = 0; | |
845dcb57 | 170 | char tbuf[MAXDNAME]; |
3cf595e5 | 171 | const char *tname; |
55707265 | 172 | int (*name_ok) __P((const char *)); |
28f540f4 | 173 | |
3cf595e5 | 174 | tname = qname; |
28f540f4 RM |
175 | host.h_name = NULL; |
176 | eom = answer->buf + anslen; | |
55707265 RM |
177 | switch (qtype) { |
178 | case T_A: | |
fa0bc87c | 179 | case T_AAAA: |
55707265 RM |
180 | name_ok = res_hnok; |
181 | break; | |
182 | case T_PTR: | |
fa0bc87c | 183 | name_ok = res_dnok; |
55707265 RM |
184 | break; |
185 | default: | |
fa0bc87c | 186 | return (NULL); /* XXX should be abort(); */ |
55707265 | 187 | } |
28f540f4 RM |
188 | /* |
189 | * find first satisfactory answer | |
190 | */ | |
191 | hp = &answer->hdr; | |
192 | ancount = ntohs(hp->ancount); | |
193 | qdcount = ntohs(hp->qdcount); | |
194 | bp = hostbuf; | |
195 | buflen = sizeof hostbuf; | |
196 | cp = answer->buf + HFIXEDSZ; | |
197 | if (qdcount != 1) { | |
198 | h_errno = NO_RECOVERY; | |
199 | return (NULL); | |
200 | } | |
55707265 RM |
201 | n = dn_expand(answer->buf, eom, cp, bp, buflen); |
202 | if ((n < 0) || !(*name_ok)(bp)) { | |
28f540f4 RM |
203 | h_errno = NO_RECOVERY; |
204 | return (NULL); | |
205 | } | |
206 | cp += n + QFIXEDSZ; | |
fa0bc87c | 207 | if (qtype == T_A || qtype == T_AAAA) { |
28f540f4 RM |
208 | /* res_send() has already verified that the query name is the |
209 | * same as the one we sent; this just gets the expanded name | |
210 | * (i.e., with the succeeding search-domain tacked on). | |
211 | */ | |
212 | n = strlen(bp) + 1; /* for the \0 */ | |
213 | host.h_name = bp; | |
214 | bp += n; | |
215 | buflen -= n; | |
216 | /* The qname can be abbreviated, but h_name is now absolute. */ | |
217 | qname = host.h_name; | |
218 | } | |
219 | ap = host_aliases; | |
220 | *ap = NULL; | |
221 | host.h_aliases = host_aliases; | |
222 | hap = h_addr_ptrs; | |
223 | *hap = NULL; | |
28f540f4 | 224 | host.h_addr_list = h_addr_ptrs; |
28f540f4 RM |
225 | haveanswer = 0; |
226 | had_error = 0; | |
227 | while (ancount-- > 0 && cp < eom && !had_error) { | |
228 | n = dn_expand(answer->buf, eom, cp, bp, buflen); | |
55707265 | 229 | if ((n < 0) || !(*name_ok)(bp)) { |
28f540f4 RM |
230 | had_error++; |
231 | continue; | |
232 | } | |
233 | cp += n; /* name */ | |
234 | type = _getshort(cp); | |
235 | cp += INT16SZ; /* type */ | |
236 | class = _getshort(cp); | |
237 | cp += INT16SZ + INT32SZ; /* class, TTL */ | |
238 | n = _getshort(cp); | |
239 | cp += INT16SZ; /* len */ | |
fa0bc87c | 240 | if (class != C_IN) { |
28f540f4 RM |
241 | /* XXX - debug? syslog? */ |
242 | cp += n; | |
243 | continue; /* XXX - had_error++ ? */ | |
244 | } | |
fa0bc87c | 245 | if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { |
28f540f4 RM |
246 | if (ap >= &host_aliases[MAXALIASES-1]) |
247 | continue; | |
248 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); | |
55707265 | 249 | if ((n < 0) || !(*name_ok)(tbuf)) { |
28f540f4 RM |
250 | had_error++; |
251 | continue; | |
252 | } | |
253 | cp += n; | |
28f540f4 RM |
254 | /* Store alias. */ |
255 | *ap++ = bp; | |
256 | n = strlen(bp) + 1; /* for the \0 */ | |
257 | bp += n; | |
258 | buflen -= n; | |
259 | /* Get canonical name. */ | |
260 | n = strlen(tbuf) + 1; /* for the \0 */ | |
261 | if (n > buflen) { | |
262 | had_error++; | |
263 | continue; | |
264 | } | |
265 | strcpy(bp, tbuf); | |
266 | host.h_name = bp; | |
267 | bp += n; | |
268 | buflen -= n; | |
269 | continue; | |
270 | } | |
3cf595e5 RM |
271 | if (qtype == T_PTR && type == T_CNAME) { |
272 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); | |
55707265 | 273 | if ((n < 0) || !res_hnok(tbuf)) { |
3cf595e5 RM |
274 | had_error++; |
275 | continue; | |
276 | } | |
277 | cp += n; | |
278 | /* Get canonical name. */ | |
279 | n = strlen(tbuf) + 1; /* for the \0 */ | |
280 | if (n > buflen) { | |
281 | had_error++; | |
282 | continue; | |
283 | } | |
284 | strcpy(bp, tbuf); | |
285 | tname = bp; | |
286 | bp += n; | |
287 | buflen -= n; | |
288 | continue; | |
289 | } | |
28f540f4 | 290 | if (type != qtype) { |
3cf595e5 | 291 | syslog(LOG_NOTICE|LOG_AUTH, |
28f540f4 | 292 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", |
fa0bc87c | 293 | qname, p_class(C_IN), p_type(qtype), |
3cf595e5 | 294 | p_type(type)); |
28f540f4 RM |
295 | cp += n; |
296 | continue; /* XXX - had_error++ ? */ | |
297 | } | |
298 | switch (type) { | |
299 | case T_PTR: | |
3cf595e5 | 300 | if (strcasecmp(tname, bp) != 0) { |
28f540f4 RM |
301 | syslog(LOG_NOTICE|LOG_AUTH, |
302 | AskedForGot, qname, bp); | |
303 | cp += n; | |
304 | continue; /* XXX - had_error++ ? */ | |
305 | } | |
306 | n = dn_expand(answer->buf, eom, cp, bp, buflen); | |
55707265 | 307 | if ((n < 0) || !res_hnok(bp)) { |
28f540f4 RM |
308 | had_error++; |
309 | break; | |
310 | } | |
311 | #if MULTI_PTRS_ARE_ALIASES | |
312 | cp += n; | |
313 | if (!haveanswer) | |
314 | host.h_name = bp; | |
315 | else if (ap < &host_aliases[MAXALIASES-1]) | |
316 | *ap++ = bp; | |
317 | else | |
318 | n = -1; | |
319 | if (n != -1) { | |
320 | n = strlen(bp) + 1; /* for the \0 */ | |
321 | bp += n; | |
322 | buflen -= n; | |
323 | } | |
324 | break; | |
325 | #else | |
326 | host.h_name = bp; | |
fa0bc87c RM |
327 | if (_res.options & RES_USE_INET6) { |
328 | n = strlen(bp) + 1; /* for the \0 */ | |
329 | bp += n; | |
330 | buflen -= n; | |
331 | map_v4v6_hostent(&host, &bp, &buflen); | |
332 | } | |
28f540f4 RM |
333 | h_errno = NETDB_SUCCESS; |
334 | return (&host); | |
335 | #endif | |
336 | case T_A: | |
fa0bc87c | 337 | case T_AAAA: |
28f540f4 RM |
338 | if (strcasecmp(host.h_name, bp) != 0) { |
339 | syslog(LOG_NOTICE|LOG_AUTH, | |
340 | AskedForGot, host.h_name, bp); | |
341 | cp += n; | |
342 | continue; /* XXX - had_error++ ? */ | |
343 | } | |
344 | if (haveanswer) { | |
345 | if (n != host.h_length) { | |
346 | cp += n; | |
347 | continue; | |
348 | } | |
349 | } else { | |
350 | register int nn; | |
351 | ||
28f540f4 RM |
352 | host.h_name = bp; |
353 | nn = strlen(bp) + 1; /* for the \0 */ | |
354 | bp += nn; | |
355 | buflen -= nn; | |
356 | } | |
357 | ||
358 | bp += sizeof(align) - ((u_long)bp % sizeof(align)); | |
359 | ||
360 | if (bp + n >= &hostbuf[sizeof hostbuf]) { | |
361 | dprintf("size (%d) too big\n", n); | |
362 | had_error++; | |
363 | continue; | |
364 | } | |
365 | if (hap >= &h_addr_ptrs[MAXADDRS-1]) { | |
366 | if (!toobig++) | |
367 | dprintf("Too many addresses (%d)\n", | |
368 | MAXADDRS); | |
369 | cp += n; | |
370 | continue; | |
371 | } | |
372 | bcopy(cp, *hap++ = bp, n); | |
373 | bp += n; | |
df21c858 | 374 | buflen -= n; |
28f540f4 RM |
375 | cp += n; |
376 | break; | |
377 | default: | |
fa0bc87c RM |
378 | abort(); |
379 | } | |
28f540f4 RM |
380 | if (!had_error) |
381 | haveanswer++; | |
fa0bc87c | 382 | } |
28f540f4 RM |
383 | if (haveanswer) { |
384 | *ap = NULL; | |
385 | *hap = NULL; | |
386 | # if defined(RESOLVSORT) | |
387 | /* | |
388 | * Note: we sort even if host can take only one address | |
389 | * in its return structures - should give it the "best" | |
390 | * address in that case, not some random one | |
391 | */ | |
fa0bc87c | 392 | if (_res.nsort && haveanswer > 1 && qtype == T_A) |
28f540f4 RM |
393 | addrsort(h_addr_ptrs, haveanswer); |
394 | # endif /*RESOLVSORT*/ | |
28f540f4 RM |
395 | if (!host.h_name) { |
396 | n = strlen(qname) + 1; /* for the \0 */ | |
fa0bc87c RM |
397 | if (n > buflen) |
398 | goto try_again; | |
28f540f4 RM |
399 | strcpy(bp, qname); |
400 | host.h_name = bp; | |
fa0bc87c RM |
401 | bp += n; |
402 | buflen -= n; | |
28f540f4 | 403 | } |
fa0bc87c RM |
404 | if (_res.options & RES_USE_INET6) |
405 | map_v4v6_hostent(&host, &bp, &buflen); | |
28f540f4 RM |
406 | h_errno = NETDB_SUCCESS; |
407 | return (&host); | |
28f540f4 | 408 | } |
fa0bc87c RM |
409 | try_again: |
410 | h_errno = TRY_AGAIN; | |
411 | return (NULL); | |
28f540f4 RM |
412 | } |
413 | ||
414 | struct hostent * | |
415 | gethostbyname(name) | |
416 | const char *name; | |
3cf595e5 RM |
417 | { |
418 | struct hostent *hp; | |
419 | ||
845dcb57 UD |
420 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) { |
421 | h_errno = NETDB_INTERNAL; | |
422 | return (NULL); | |
423 | } | |
fa0bc87c | 424 | if (_res.options & RES_USE_INET6) { |
3cf595e5 RM |
425 | hp = gethostbyname2(name, AF_INET6); |
426 | if (hp) | |
427 | return (hp); | |
428 | } | |
3cf595e5 RM |
429 | return (gethostbyname2(name, AF_INET)); |
430 | } | |
431 | ||
432 | struct hostent * | |
433 | gethostbyname2(name, af) | |
434 | const char *name; | |
435 | int af; | |
28f540f4 RM |
436 | { |
437 | querybuf buf; | |
438 | register const char *cp; | |
fa0bc87c | 439 | char *bp; |
5f0e6fc7 | 440 | int n, size, type, len; |
fa0bc87c | 441 | extern struct hostent *_gethtbyname2(); |
28f540f4 | 442 | |
3d61b63c RM |
443 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) { |
444 | h_errno = NETDB_INTERNAL; | |
445 | return (NULL); | |
446 | } | |
447 | ||
fa0bc87c RM |
448 | switch (af) { |
449 | case AF_INET: | |
450 | size = INADDRSZ; | |
451 | type = T_A; | |
452 | break; | |
453 | case AF_INET6: | |
454 | size = IN6ADDRSZ; | |
455 | type = T_AAAA; | |
456 | break; | |
457 | default: | |
458 | h_errno = NETDB_INTERNAL; | |
459 | errno = EAFNOSUPPORT; | |
460 | return (NULL); | |
461 | } | |
462 | ||
463 | host.h_addrtype = af; | |
464 | host.h_length = size; | |
465 | ||
28f540f4 RM |
466 | /* |
467 | * if there aren't any dots, it could be a user-level alias. | |
468 | * this is also done in res_query() since we are not the only | |
469 | * function that looks up host names. | |
470 | */ | |
471 | if (!strchr(name, '.') && (cp = __hostalias(name))) | |
472 | name = cp; | |
473 | ||
474 | /* | |
475 | * disallow names consisting only of digits/dots, unless | |
476 | * they end in a dot. | |
477 | */ | |
478 | if (isdigit(name[0])) | |
479 | for (cp = name;; ++cp) { | |
480 | if (!*cp) { | |
481 | if (*--cp == '.') | |
482 | break; | |
483 | /* | |
484 | * All-numeric, no dot at the end. | |
485 | * Fake up a hostent as if we'd actually | |
486 | * done a lookup. | |
487 | */ | |
613a76ff | 488 | if (inet_pton(af, name, host_addr) <= 0) { |
28f540f4 RM |
489 | h_errno = HOST_NOT_FOUND; |
490 | return (NULL); | |
491 | } | |
3d61b63c RM |
492 | strncpy(hostbuf, name, MAXDNAME); |
493 | hostbuf[MAXDNAME] = '\0'; | |
fa0bc87c RM |
494 | bp = hostbuf + MAXDNAME; |
495 | len = sizeof hostbuf - MAXDNAME; | |
3d61b63c | 496 | host.h_name = hostbuf; |
28f540f4 RM |
497 | host.h_aliases = host_aliases; |
498 | host_aliases[0] = NULL; | |
613a76ff | 499 | h_addr_ptrs[0] = (char *)host_addr; |
28f540f4 | 500 | h_addr_ptrs[1] = NULL; |
28f540f4 | 501 | host.h_addr_list = h_addr_ptrs; |
fa0bc87c RM |
502 | if (_res.options & RES_USE_INET6) |
503 | map_v4v6_hostent(&host, &bp, &len); | |
28f540f4 RM |
504 | h_errno = NETDB_SUCCESS; |
505 | return (&host); | |
506 | } | |
fa0bc87c | 507 | if (!isdigit(*cp) && *cp != '.') |
28f540f4 | 508 | break; |
845dcb57 UD |
509 | } |
510 | if (isxdigit(name[0]) || name[0] == ':') | |
511 | for (cp = name;; ++cp) { | |
512 | if (!*cp) { | |
513 | if (*--cp == '.') | |
514 | break; | |
515 | /* | |
516 | * All-IPv6-legal, no dot at the end. | |
517 | * Fake up a hostent as if we'd actually | |
518 | * done a lookup. | |
519 | */ | |
520 | if (inet_pton(af, name, host_addr) <= 0) { | |
521 | h_errno = HOST_NOT_FOUND; | |
522 | return (NULL); | |
523 | } | |
524 | strncpy(hostbuf, name, MAXDNAME); | |
525 | hostbuf[MAXDNAME] = '\0'; | |
526 | bp = hostbuf + MAXDNAME; | |
527 | len = sizeof hostbuf - MAXDNAME; | |
528 | host.h_name = hostbuf; | |
529 | host.h_aliases = host_aliases; | |
530 | host_aliases[0] = NULL; | |
531 | h_addr_ptrs[0] = (char *)host_addr; | |
532 | h_addr_ptrs[1] = NULL; | |
533 | host.h_addr_list = h_addr_ptrs; | |
534 | h_errno = NETDB_SUCCESS; | |
535 | return (&host); | |
536 | } | |
537 | if (!isxdigit(*cp) && *cp != ':' && *cp != '.') | |
538 | break; | |
28f540f4 RM |
539 | } |
540 | ||
5f0e6fc7 RM |
541 | if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) { |
542 | dprintf("res_search failed (%d)\n", n); | |
543 | if (errno == ECONNREFUSED) | |
544 | return (_gethtbyname2(name, af)); | |
545 | return (NULL); | |
28f540f4 | 546 | } |
5f0e6fc7 | 547 | return (getanswer(&buf, n, name, type)); |
28f540f4 RM |
548 | } |
549 | ||
550 | struct hostent * | |
fa0bc87c RM |
551 | gethostbyaddr(addr, len, af) |
552 | const char *addr; /* XXX should have been def'd as u_char! */ | |
553 | int len, af; | |
28f540f4 | 554 | { |
fa0bc87c RM |
555 | const u_char *uaddr = (const u_char *)addr; |
556 | static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff }; | |
557 | static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 }; | |
558 | int n, size; | |
28f540f4 RM |
559 | querybuf buf; |
560 | register struct hostent *hp; | |
fa0bc87c | 561 | char qbuf[MAXDNAME+1], *qp; |
5f0e6fc7 | 562 | #ifdef SUNSECURITY |
28f540f4 RM |
563 | register struct hostent *rhp; |
564 | char **haddr; | |
565 | u_long old_options; | |
566 | char hname2[MAXDNAME+1]; | |
5f0e6fc7 | 567 | #endif /*SUNSECURITY*/ |
28f540f4 | 568 | extern struct hostent *_gethtbyaddr(); |
fa0bc87c | 569 | |
3d61b63c RM |
570 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) { |
571 | h_errno = NETDB_INTERNAL; | |
572 | return (NULL); | |
573 | } | |
fa0bc87c RM |
574 | if (af == AF_INET6 && len == IN6ADDRSZ && |
575 | (!bcmp(uaddr, mapped, sizeof mapped) || | |
576 | !bcmp(uaddr, tunnelled, sizeof tunnelled))) { | |
577 | /* Unmap. */ | |
578 | addr += sizeof mapped; | |
579 | uaddr += sizeof mapped; | |
580 | af = AF_INET; | |
581 | len = INADDRSZ; | |
582 | } | |
583 | switch (af) { | |
584 | case AF_INET: | |
585 | size = INADDRSZ; | |
586 | break; | |
587 | case AF_INET6: | |
588 | size = IN6ADDRSZ; | |
589 | break; | |
590 | default: | |
28f540f4 RM |
591 | errno = EAFNOSUPPORT; |
592 | h_errno = NETDB_INTERNAL; | |
593 | return (NULL); | |
594 | } | |
fa0bc87c RM |
595 | if (size != len) { |
596 | errno = EINVAL; | |
597 | h_errno = NETDB_INTERNAL; | |
28f540f4 RM |
598 | return (NULL); |
599 | } | |
5f0e6fc7 RM |
600 | switch (af) { |
601 | case AF_INET: | |
602 | (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", | |
603 | (uaddr[3] & 0xff), | |
604 | (uaddr[2] & 0xff), | |
605 | (uaddr[1] & 0xff), | |
606 | (uaddr[0] & 0xff)); | |
607 | break; | |
608 | case AF_INET6: | |
609 | qp = qbuf; | |
610 | for (n = IN6ADDRSZ - 1; n >= 0; n--) { | |
611 | qp += SPRINTF((qp, "%x.%x.", | |
612 | uaddr[n] & 0xf, | |
613 | (uaddr[n] >> 4) & 0xf)); | |
fa0bc87c | 614 | } |
5f0e6fc7 RM |
615 | strcpy(qp, "ip6.int"); |
616 | break; | |
617 | default: | |
618 | abort(); | |
619 | } | |
620 | n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf); | |
621 | if (n < 0) { | |
622 | dprintf("res_query failed (%d)\n", n); | |
623 | if (errno == ECONNREFUSED) | |
624 | return (_gethtbyaddr(addr, len, af)); | |
625 | return (NULL); | |
28f540f4 | 626 | } |
5f0e6fc7 RM |
627 | if (!(hp = getanswer(&buf, n, qbuf, T_PTR))) |
628 | return (NULL); /* h_errno was set by getanswer() */ | |
629 | #ifdef SUNSECURITY | |
630 | if (af == AF_INET) { | |
631 | /* | |
632 | * turn off search as the name should be absolute, | |
633 | * 'localhost' should be matched by defnames | |
634 | */ | |
635 | strncpy(hname2, hp->h_name, MAXDNAME); | |
636 | hname2[MAXDNAME] = '\0'; | |
637 | old_options = _res.options; | |
638 | _res.options &= ~RES_DNSRCH; | |
639 | _res.options |= RES_DEFNAMES; | |
640 | if (!(rhp = gethostbyname(hname2))) { | |
641 | syslog(LOG_NOTICE|LOG_AUTH, | |
642 | "gethostbyaddr: No A record for %s (verifying [%s])", | |
643 | hname2, inet_ntoa(*((struct in_addr *)addr))); | |
644 | _res.options = old_options; | |
645 | h_errno = HOST_NOT_FOUND; | |
646 | return (NULL); | |
647 | } | |
648 | _res.options = old_options; | |
649 | for (haddr = rhp->h_addr_list; *haddr; haddr++) | |
650 | if (!memcmp(*haddr, addr, INADDRSZ)) | |
651 | break; | |
652 | if (!*haddr) { | |
653 | syslog(LOG_NOTICE|LOG_AUTH, | |
654 | "gethostbyaddr: A record of %s != PTR record [%s]", | |
655 | hname2, inet_ntoa(*((struct in_addr *)addr))); | |
fa0bc87c | 656 | h_errno = HOST_NOT_FOUND; |
5f0e6fc7 RM |
657 | return (NULL); |
658 | } | |
659 | } | |
660 | #endif /*SUNSECURITY*/ | |
661 | hp->h_addrtype = af; | |
662 | hp->h_length = len; | |
663 | bcopy(addr, host_addr, len); | |
664 | h_addr_ptrs[0] = (char *)host_addr; | |
665 | h_addr_ptrs[1] = NULL; | |
666 | if (af == AF_INET && (_res.options & RES_USE_INET6)) { | |
667 | map_v4v6_address((char*)host_addr, (char*)host_addr); | |
668 | hp->h_addrtype = AF_INET6; | |
669 | hp->h_length = IN6ADDRSZ; | |
670 | } | |
671 | h_errno = NETDB_SUCCESS; | |
672 | return (hp); | |
28f540f4 RM |
673 | } |
674 | ||
675 | void | |
676 | _sethtent(f) | |
677 | int f; | |
678 | { | |
679 | if (!hostf) | |
680 | hostf = fopen(_PATH_HOSTS, "r" ); | |
681 | else | |
682 | rewind(hostf); | |
683 | stayopen = f; | |
684 | } | |
685 | ||
686 | void | |
687 | _endhtent() | |
688 | { | |
689 | if (hostf && !stayopen) { | |
690 | (void) fclose(hostf); | |
691 | hostf = NULL; | |
692 | } | |
693 | } | |
694 | ||
695 | struct hostent * | |
696 | _gethtent() | |
697 | { | |
698 | char *p; | |
699 | register char *cp, **q; | |
fa0bc87c | 700 | int af, len; |
28f540f4 RM |
701 | |
702 | if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) { | |
703 | h_errno = NETDB_INTERNAL; | |
704 | return (NULL); | |
705 | } | |
fa0bc87c | 706 | again: |
28f540f4 RM |
707 | if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) { |
708 | h_errno = HOST_NOT_FOUND; | |
709 | return (NULL); | |
710 | } | |
711 | if (*p == '#') | |
712 | goto again; | |
713 | if (!(cp = strpbrk(p, "#\n"))) | |
714 | goto again; | |
715 | *cp = '\0'; | |
716 | if (!(cp = strpbrk(p, " \t"))) | |
717 | goto again; | |
718 | *cp++ = '\0'; | |
fa0bc87c | 719 | if ((_res.options & RES_USE_INET6) && |
613a76ff | 720 | inet_pton(AF_INET6, p, host_addr) > 0) { |
fa0bc87c RM |
721 | af = AF_INET6; |
722 | len = IN6ADDRSZ; | |
613a76ff | 723 | } else if (inet_pton(AF_INET, p, host_addr) > 0) { |
fa0bc87c | 724 | if (_res.options & RES_USE_INET6) { |
613a76ff | 725 | map_v4v6_address((char*)host_addr, (char*)host_addr); |
fa0bc87c RM |
726 | af = AF_INET6; |
727 | len = IN6ADDRSZ; | |
728 | } else { | |
729 | af = AF_INET; | |
730 | len = INADDRSZ; | |
731 | } | |
732 | } else { | |
28f540f4 | 733 | goto again; |
fa0bc87c | 734 | } |
613a76ff | 735 | h_addr_ptrs[0] = (char *)host_addr; |
28f540f4 | 736 | h_addr_ptrs[1] = NULL; |
28f540f4 | 737 | host.h_addr_list = h_addr_ptrs; |
fa0bc87c RM |
738 | host.h_length = len; |
739 | host.h_addrtype = af; | |
28f540f4 RM |
740 | while (*cp == ' ' || *cp == '\t') |
741 | cp++; | |
742 | host.h_name = cp; | |
743 | q = host.h_aliases = host_aliases; | |
744 | if (cp = strpbrk(cp, " \t")) | |
745 | *cp++ = '\0'; | |
746 | while (cp && *cp) { | |
747 | if (*cp == ' ' || *cp == '\t') { | |
748 | cp++; | |
749 | continue; | |
750 | } | |
751 | if (q < &host_aliases[MAXALIASES - 1]) | |
752 | *q++ = cp; | |
753 | if (cp = strpbrk(cp, " \t")) | |
754 | *cp++ = '\0'; | |
755 | } | |
756 | *q = NULL; | |
fa0bc87c RM |
757 | if (_res.options & RES_USE_INET6) { |
758 | char *bp = hostbuf; | |
759 | int buflen = sizeof hostbuf; | |
760 | ||
761 | map_v4v6_hostent(&host, &bp, &buflen); | |
762 | } | |
28f540f4 RM |
763 | h_errno = NETDB_SUCCESS; |
764 | return (&host); | |
765 | } | |
766 | ||
767 | struct hostent * | |
768 | _gethtbyname(name) | |
fa0bc87c RM |
769 | const char *name; |
770 | { | |
613a76ff | 771 | extern struct hostent *_gethtbyname2(); |
fa0bc87c RM |
772 | struct hostent *hp; |
773 | ||
774 | if (_res.options & RES_USE_INET6) { | |
775 | hp = _gethtbyname2(name, AF_INET6); | |
776 | if (hp) | |
777 | return (hp); | |
778 | } | |
779 | return (_gethtbyname2(name, AF_INET)); | |
780 | } | |
781 | ||
782 | struct hostent * | |
783 | _gethtbyname2(name, af) | |
784 | const char *name; | |
785 | int af; | |
28f540f4 RM |
786 | { |
787 | register struct hostent *p; | |
788 | register char **cp; | |
fa0bc87c | 789 | |
28f540f4 | 790 | _sethtent(0); |
5f0e6fc7 RM |
791 | while (p = _gethtent()) { |
792 | if (p->h_addrtype != af) | |
793 | continue; | |
794 | if (strcasecmp(p->h_name, name) == 0) | |
795 | break; | |
796 | for (cp = p->h_aliases; *cp != 0; cp++) | |
797 | if (strcasecmp(*cp, name) == 0) | |
798 | goto found; | |
28f540f4 | 799 | } |
fa0bc87c | 800 | found: |
28f540f4 RM |
801 | _endhtent(); |
802 | return (p); | |
803 | } | |
804 | ||
805 | struct hostent * | |
fa0bc87c | 806 | _gethtbyaddr(addr, len, af) |
28f540f4 | 807 | const char *addr; |
fa0bc87c | 808 | int len, af; |
28f540f4 RM |
809 | { |
810 | register struct hostent *p; | |
811 | ||
812 | _sethtent(0); | |
813 | while (p = _gethtent()) | |
fa0bc87c | 814 | if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len)) |
28f540f4 RM |
815 | break; |
816 | _endhtent(); | |
817 | return (p); | |
818 | } | |
819 | ||
fa0bc87c RM |
820 | static void |
821 | map_v4v6_address(src, dst) | |
822 | const char *src; | |
823 | char *dst; | |
824 | { | |
825 | u_char *p = (u_char *)dst; | |
826 | char tmp[INADDRSZ]; | |
827 | int i; | |
828 | ||
829 | /* Stash a temporary copy so our caller can update in place. */ | |
830 | bcopy(src, tmp, INADDRSZ); | |
831 | /* Mark this ipv6 addr as a mapped ipv4. */ | |
832 | for (i = 0; i < 10; i++) | |
833 | *p++ = 0x00; | |
834 | *p++ = 0xff; | |
835 | *p++ = 0xff; | |
836 | /* Retrieve the saved copy and we're done. */ | |
837 | bcopy(tmp, (void*)p, INADDRSZ); | |
838 | } | |
839 | ||
840 | static void | |
841 | map_v4v6_hostent(hp, bpp, lenp) | |
842 | struct hostent *hp; | |
843 | char **bpp; | |
844 | int *lenp; | |
845 | { | |
846 | char **ap; | |
847 | ||
848 | if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ) | |
849 | return; | |
850 | hp->h_addrtype = AF_INET6; | |
851 | hp->h_length = IN6ADDRSZ; | |
852 | for (ap = hp->h_addr_list; *ap; ap++) { | |
853 | int i = sizeof(align) - ((u_long)*bpp % sizeof(align)); | |
854 | ||
855 | if (*lenp < (i + IN6ADDRSZ)) { | |
856 | /* Out of memory. Truncate address list here. XXX */ | |
857 | *ap = NULL; | |
858 | return; | |
859 | } | |
860 | *bpp += i; | |
861 | *lenp -= i; | |
862 | map_v4v6_address(*ap, *bpp); | |
863 | *ap = *bpp; | |
864 | *bpp += IN6ADDRSZ; | |
865 | *lenp -= IN6ADDRSZ; | |
866 | } | |
867 | } | |
868 | ||
28f540f4 RM |
869 | #ifdef RESOLVSORT |
870 | static void | |
871 | addrsort(ap, num) | |
872 | char **ap; | |
873 | int num; | |
874 | { | |
875 | int i, j; | |
876 | char **p; | |
877 | short aval[MAXADDRS]; | |
878 | int needsort = 0; | |
879 | ||
880 | p = ap; | |
881 | for (i = 0; i < num; i++, p++) { | |
882 | for (j = 0 ; (unsigned)j < _res.nsort; j++) | |
fa0bc87c | 883 | if (_res.sort_list[j].addr.s_addr == |
28f540f4 RM |
884 | (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask)) |
885 | break; | |
886 | aval[i] = j; | |
887 | if (needsort == 0 && i > 0 && j < aval[i-1]) | |
888 | needsort = i; | |
889 | } | |
890 | if (!needsort) | |
891 | return; | |
892 | ||
893 | while (needsort < num) { | |
894 | for (j = needsort - 1; j >= 0; j--) { | |
895 | if (aval[j] > aval[j+1]) { | |
896 | char *hp; | |
897 | ||
898 | i = aval[j]; | |
899 | aval[j] = aval[j+1]; | |
900 | aval[j+1] = i; | |
901 | ||
902 | hp = ap[j]; | |
903 | ap[j] = ap[j+1]; | |
904 | ap[j+1] = hp; | |
905 | ||
906 | } else | |
907 | break; | |
908 | } | |
909 | needsort++; | |
910 | } | |
911 | } | |
912 | #endif | |
913 | ||
914 | #if defined(BSD43_BSD43_NFS) || defined(sun) | |
915 | /* some libc's out there are bound internally to these names (UMIPS) */ | |
916 | void | |
917 | ht_sethostent(stayopen) | |
918 | int stayopen; | |
919 | { | |
920 | _sethtent(stayopen); | |
921 | } | |
922 | ||
923 | void | |
924 | ht_endhostent() | |
925 | { | |
926 | _endhtent(); | |
927 | } | |
928 | ||
929 | struct hostent * | |
930 | ht_gethostbyname(name) | |
931 | char *name; | |
932 | { | |
933 | return (_gethtbyname(name)); | |
934 | } | |
935 | ||
936 | struct hostent * | |
fa0bc87c | 937 | ht_gethostbyaddr(addr, len, af) |
28f540f4 | 938 | const char *addr; |
fa0bc87c | 939 | int len, af; |
28f540f4 | 940 | { |
fa0bc87c | 941 | return (_gethtbyaddr(addr, len, af)); |
28f540f4 RM |
942 | } |
943 | ||
944 | struct hostent * | |
945 | gethostent() | |
946 | { | |
5f0e6fc7 | 947 | return (_gethtent()); |
28f540f4 RM |
948 | } |
949 | ||
950 | void | |
951 | dns_service() | |
952 | { | |
953 | return; | |
954 | } | |
955 | ||
956 | #undef dn_skipname | |
957 | dn_skipname(comp_dn, eom) | |
958 | const u_char *comp_dn, *eom; | |
959 | { | |
960 | return (__dn_skipname(comp_dn, eom)); | |
961 | } | |
962 | #endif /*old-style libc with yp junk in it*/ |