gethnamaddr.c revision 1.58.2.3 1 /* $NetBSD: gethnamaddr.c,v 1.58.2.3 2004/11/29 06:15:48 jmc Exp $ */
2
3 /*
4 * ++Copyright++ 1985, 1988, 1993
5 * -
6 * Copyright (c) 1985, 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/cdefs.h>
55 #if defined(LIBC_SCCS) && !defined(lint)
56 #if 0
57 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
58 static char rcsid[] = "Id: gethnamaddr.c,v 8.21 1997/06/01 20:34:37 vixie Exp ";
59 #else
60 __RCSID("$NetBSD: gethnamaddr.c,v 1.58.2.3 2004/11/29 06:15:48 jmc Exp $");
61 #endif
62 #endif /* LIBC_SCCS and not lint */
63
64 #if defined(_LIBC)
65 #include "namespace.h"
66 #endif
67 #include <sys/param.h>
68 #include <sys/socket.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
71 #include <arpa/nameser.h>
72
73 #include <assert.h>
74 #include <ctype.h>
75 #include <errno.h>
76 #include <netdb.h>
77 #include <resolv.h>
78 #include <stdarg.h>
79 #include <stdio.h>
80 #include <syslog.h>
81
82 #ifndef LOG_AUTH
83 # define LOG_AUTH 0
84 #endif
85
86 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
87
88 #include <nsswitch.h>
89 #include <stdlib.h>
90 #include <string.h>
91
92 #ifdef YP
93 #include <rpc/rpc.h>
94 #include <rpcsvc/yp_prot.h>
95 #include <rpcsvc/ypclnt.h>
96 #endif
97
98 #if defined(_LIBC) && defined(__weak_alias)
99 __weak_alias(gethostbyaddr,_gethostbyaddr)
100 __weak_alias(gethostbyname,_gethostbyname)
101 __weak_alias(gethostent,_gethostent)
102 #endif
103
104 #define MAXALIASES 35
105 #define MAXADDRS 35
106
107 static const char AskedForGot[] =
108 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
109
110 static char *h_addr_ptrs[MAXADDRS + 1];
111
112 #ifdef YP
113 static char *__ypdomain;
114 #endif
115
116 static struct hostent host;
117 static char *host_aliases[MAXALIASES];
118 static char hostbuf[8*1024];
119 static u_int32_t host_addr[16 / sizeof(u_int32_t)]; /* IPv4 or IPv6 */
120 static FILE *hostf = NULL;
121 static int stayopen = 0;
122
123 #define MAXPACKET (64*1024)
124
125 typedef union {
126 HEADER hdr;
127 u_char buf[MAXPACKET];
128 } querybuf;
129
130 typedef union {
131 int32_t al;
132 char ac;
133 } align;
134
135 #ifdef DEBUG
136 static void dprintf __P((char *, ...))
137 __attribute__((__format__(__printf__, 1, 2)));
138 #endif
139 static struct hostent *getanswer __P((const querybuf *, int,
140 const char *, int));
141 static void map_v4v6_address __P((const char *, char *));
142 static void map_v4v6_hostent __P((struct hostent *, char **, char *));
143 #ifdef RESOLVSORT
144 static void addrsort __P((char **, int));
145 #endif
146
147 void _sethtent __P((int));
148 void _endhtent __P((void));
149 struct hostent *_gethtent __P((void));
150 struct hostent *_gethtbyname2 __P((const char *, int));
151 void ht_sethostent __P((int));
152 void ht_endhostent __P((void));
153 struct hostent *ht_gethostbyname __P((char *));
154 struct hostent *ht_gethostbyaddr __P((const char *, int, int ));
155 void dns_service __P((void));
156 #undef dn_skipname
157 int dn_skipname __P((const u_char *, const u_char *));
158 int _gethtbyaddr __P((void *, void *, va_list));
159 int _gethtbyname __P((void *, void *, va_list));
160 int _dns_gethtbyaddr __P((void *, void *, va_list));
161 int _dns_gethtbyname __P((void *, void *, va_list));
162 #ifdef YP
163 struct hostent *_yphostent __P((char *, int));
164 int _yp_gethtbyaddr __P((void *, void *, va_list));
165 int _yp_gethtbyname __P((void *, void *, va_list));
166 #endif
167
168 static const ns_src default_dns_files[] = {
169 { NSSRC_FILES, NS_SUCCESS },
170 { NSSRC_DNS, NS_SUCCESS },
171 { 0 }
172 };
173
174
175 #ifdef DEBUG
176 static void
177 dprintf(char *msg, ...)
178 {
179 _DIAGASSERT(msg != NULL);
180
181 if (_res.options & RES_DEBUG) {
182 int save = errno;
183 va_list ap;
184
185 va_start (ap, msg);
186 vprintf(msg, ap);
187 va_end (ap);
188
189 errno = save;
190 }
191 }
192 #else
193 # define dprintf(msg, num) /*nada*/
194 #endif
195
196 #define BOUNDED_INCR(x) \
197 do { \
198 cp += (x); \
199 if (cp > eom) { \
200 h_errno = NO_RECOVERY; \
201 return (NULL); \
202 } \
203 } while (/*CONSTCOND*/0)
204
205 #define BOUNDS_CHECK(ptr, count) \
206 do { \
207 if ((ptr) + (count) > eom) { \
208 h_errno = NO_RECOVERY; \
209 return (NULL); \
210 } \
211 } while (/*CONSTCOND*/0)
212
213 static struct hostent *
214 getanswer(answer, anslen, qname, qtype)
215 const querybuf *answer;
216 int anslen;
217 const char *qname;
218 int qtype;
219 {
220 const HEADER *hp;
221 const u_char *cp;
222 int n;
223 const u_char *eom, *erdata;
224 char *bp, **ap, **hap, *ep;
225 int type, class, ancount, qdcount;
226 int haveanswer, had_error;
227 int toobig = 0;
228 char tbuf[MAXDNAME];
229 const char *tname;
230 int (*name_ok) __P((const char *));
231
232 _DIAGASSERT(answer != NULL);
233 _DIAGASSERT(qname != NULL);
234
235 tname = qname;
236 host.h_name = NULL;
237 eom = answer->buf + anslen;
238 switch (qtype) {
239 case T_A:
240 case T_AAAA:
241 name_ok = res_hnok;
242 break;
243 case T_PTR:
244 name_ok = res_dnok;
245 break;
246 default:
247 return (NULL); /* XXX should be abort(); */
248 }
249 /*
250 * find first satisfactory answer
251 */
252 hp = &answer->hdr;
253 ancount = ntohs(hp->ancount);
254 qdcount = ntohs(hp->qdcount);
255 bp = hostbuf;
256 ep = hostbuf + sizeof hostbuf;
257 cp = answer->buf;
258 BOUNDED_INCR(HFIXEDSZ);
259 if (qdcount != 1) {
260 h_errno = NO_RECOVERY;
261 return (NULL);
262 }
263 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
264 if ((n < 0) || !(*name_ok)(bp)) {
265 h_errno = NO_RECOVERY;
266 return (NULL);
267 }
268 BOUNDED_INCR(n + QFIXEDSZ);
269 if (qtype == T_A || qtype == T_AAAA) {
270 /* res_send() has already verified that the query name is the
271 * same as the one we sent; this just gets the expanded name
272 * (i.e., with the succeeding search-domain tacked on).
273 */
274 n = strlen(bp) + 1; /* for the \0 */
275 if (n >= MAXHOSTNAMELEN) {
276 h_errno = NO_RECOVERY;
277 return (NULL);
278 }
279 host.h_name = bp;
280 bp += n;
281 /* The qname can be abbreviated, but h_name is now absolute. */
282 qname = host.h_name;
283 }
284 ap = host_aliases;
285 *ap = NULL;
286 host.h_aliases = host_aliases;
287 hap = h_addr_ptrs;
288 *hap = NULL;
289 host.h_addr_list = h_addr_ptrs;
290 haveanswer = 0;
291 had_error = 0;
292 while (ancount-- > 0 && cp < eom && !had_error) {
293 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
294 if ((n < 0) || !(*name_ok)(bp)) {
295 had_error++;
296 continue;
297 }
298 cp += n; /* name */
299 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
300 type = _getshort(cp);
301 cp += INT16SZ; /* type */
302 class = _getshort(cp);
303 cp += INT16SZ + INT32SZ; /* class, TTL */
304 n = _getshort(cp);
305 cp += INT16SZ; /* len */
306 BOUNDS_CHECK(cp, n);
307 erdata = cp + n;
308 if (class != C_IN) {
309 /* XXX - debug? syslog? */
310 cp += n;
311 continue; /* XXX - had_error++ ? */
312 }
313 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
314 if (ap >= &host_aliases[MAXALIASES-1])
315 continue;
316 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
317 if ((n < 0) || !(*name_ok)(tbuf)) {
318 had_error++;
319 continue;
320 }
321 cp += n;
322 if (cp != erdata) {
323 h_errno = NO_RECOVERY;
324 return (NULL);
325 }
326 /* Store alias. */
327 *ap++ = bp;
328 n = strlen(bp) + 1; /* for the \0 */
329 if (n >= MAXHOSTNAMELEN) {
330 had_error++;
331 continue;
332 }
333 bp += n;
334 /* Get canonical name. */
335 n = strlen(tbuf) + 1; /* for the \0 */
336 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
337 had_error++;
338 continue;
339 }
340 strlcpy(bp, tbuf, (size_t)(ep - bp));
341 host.h_name = bp;
342 bp += n;
343 continue;
344 }
345 if (qtype == T_PTR && type == T_CNAME) {
346 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
347 if (n < 0 || !res_dnok(tbuf)) {
348 had_error++;
349 continue;
350 }
351 cp += n;
352 if (cp != erdata) {
353 h_errno = NO_RECOVERY;
354 return (NULL);
355 }
356 /* Get canonical name. */
357 n = strlen(tbuf) + 1; /* for the \0 */
358 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
359 had_error++;
360 continue;
361 }
362 strlcpy(bp, tbuf, (size_t)(ep - bp));
363 tname = bp;
364 bp += n;
365 continue;
366 }
367 if (type != qtype) {
368 if (type != T_KEY && type != T_SIG)
369 syslog(LOG_NOTICE|LOG_AUTH,
370 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
371 qname, p_class(C_IN), p_type(qtype),
372 p_type(type));
373 cp += n;
374 continue; /* XXX - had_error++ ? */
375 }
376 switch (type) {
377 case T_PTR:
378 if (strcasecmp(tname, bp) != 0) {
379 syslog(LOG_NOTICE|LOG_AUTH,
380 AskedForGot, qname, bp);
381 cp += n;
382 continue; /* XXX - had_error++ ? */
383 }
384 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
385 if ((n < 0) || !res_hnok(bp)) {
386 had_error++;
387 break;
388 }
389 #if MULTI_PTRS_ARE_ALIASES
390 cp += n;
391 if (cp != erdata) {
392 h_errno = NO_RECOVERY;
393 return (NULL);
394 }
395 if (!haveanswer)
396 host.h_name = bp;
397 else if (ap < &host_aliases[MAXALIASES-1])
398 *ap++ = bp;
399 else
400 n = -1;
401 if (n != -1) {
402 n = strlen(bp) + 1; /* for the \0 */
403 if (n >= MAXHOSTNAMELEN) {
404 had_error++;
405 break;
406 }
407 bp += n;
408 }
409 break;
410 #else
411 host.h_name = bp;
412 if (_res.options & RES_USE_INET6) {
413 n = strlen(bp) + 1; /* for the \0 */
414 if (n >= MAXHOSTNAMELEN) {
415 had_error++;
416 break;
417 }
418 bp += n;
419 map_v4v6_hostent(&host, &bp, ep);
420 }
421 h_errno = NETDB_SUCCESS;
422 return (&host);
423 #endif
424 case T_A:
425 case T_AAAA:
426 if (strcasecmp(host.h_name, bp) != 0) {
427 syslog(LOG_NOTICE|LOG_AUTH,
428 AskedForGot, host.h_name, bp);
429 cp += n;
430 continue; /* XXX - had_error++ ? */
431 }
432 if (n != host.h_length) {
433 cp += n;
434 continue;
435 }
436 if (type == T_AAAA) {
437 struct in6_addr in6;
438 memcpy(&in6, cp, IN6ADDRSZ);
439 if (IN6_IS_ADDR_V4MAPPED(&in6)) {
440 cp += n;
441 continue;
442 }
443 }
444 if (!haveanswer) {
445 int nn;
446
447 host.h_name = bp;
448 nn = strlen(bp) + 1; /* for the \0 */
449 bp += nn;
450 }
451
452 bp += sizeof(align) -
453 (size_t)((u_long)bp % sizeof(align));
454
455 if (bp + n >= &hostbuf[sizeof hostbuf]) {
456 dprintf("size (%d) too big\n", n);
457 had_error++;
458 continue;
459 }
460 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
461 if (!toobig++)
462 dprintf("Too many addresses (%d)\n",
463 MAXADDRS);
464 cp += n;
465 continue;
466 }
467 (void)memcpy(*hap++ = bp, cp, (size_t)n);
468 bp += n;
469 cp += n;
470 if (cp != erdata) {
471 h_errno = NO_RECOVERY;
472 return (NULL);
473 }
474 break;
475 default:
476 abort();
477 }
478 if (!had_error)
479 haveanswer++;
480 }
481 if (haveanswer) {
482 *ap = NULL;
483 *hap = NULL;
484 # if defined(RESOLVSORT)
485 /*
486 * Note: we sort even if host can take only one address
487 * in its return structures - should give it the "best"
488 * address in that case, not some random one
489 */
490 if (_res.nsort && haveanswer > 1 && qtype == T_A)
491 addrsort(h_addr_ptrs, haveanswer);
492 # endif /*RESOLVSORT*/
493 if (!host.h_name) {
494 n = strlen(qname) + 1; /* for the \0 */
495 if (n > ep - bp || n >= MAXHOSTNAMELEN)
496 goto no_recovery;
497 strlcpy(bp, qname, (size_t)(ep - bp));
498 host.h_name = bp;
499 bp += n;
500 }
501 if (_res.options & RES_USE_INET6)
502 map_v4v6_hostent(&host, &bp, ep);
503 h_errno = NETDB_SUCCESS;
504 return (&host);
505 }
506 no_recovery:
507 h_errno = NO_RECOVERY;
508 return (NULL);
509 }
510
511 struct hostent *
512 gethostbyname(name)
513 const char *name;
514 {
515 struct hostent *hp;
516
517 _DIAGASSERT(name != NULL);
518
519 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
520 h_errno = NETDB_INTERNAL;
521 return (NULL);
522 }
523 if (_res.options & RES_USE_INET6) {
524 hp = gethostbyname2(name, AF_INET6);
525 if (hp)
526 return (hp);
527 }
528 return (gethostbyname2(name, AF_INET));
529 }
530
531 struct hostent *
532 gethostbyname2(name, af)
533 const char *name;
534 int af;
535 {
536 const char *cp;
537 char *bp, *ep;
538 int size;
539 struct hostent *hp;
540 static const ns_dtab dtab[] = {
541 NS_FILES_CB(_gethtbyname, NULL)
542 { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */
543 NS_NIS_CB(_yp_gethtbyname, NULL)
544 { 0 }
545 };
546
547 _DIAGASSERT(name != NULL);
548
549 switch (af) {
550 case AF_INET:
551 size = INADDRSZ;
552 break;
553 case AF_INET6:
554 size = IN6ADDRSZ;
555 break;
556 default:
557 h_errno = NETDB_INTERNAL;
558 errno = EAFNOSUPPORT;
559 return (NULL);
560 }
561
562 host.h_addrtype = af;
563 host.h_length = size;
564
565 /*
566 * if there aren't any dots, it could be a user-level alias.
567 * this is also done in res_query() since we are not the only
568 * function that looks up host names.
569 */
570 if (!strchr(name, '.') && (cp = __hostalias(name)))
571 name = cp;
572
573 /*
574 * disallow names consisting only of digits/dots, unless
575 * they end in a dot.
576 */
577 if (isdigit((u_char) name[0]))
578 for (cp = name;; ++cp) {
579 if (!*cp) {
580 if (*--cp == '.')
581 break;
582 /*
583 * All-numeric, no dot at the end.
584 * Fake up a hostent as if we'd actually
585 * done a lookup.
586 */
587 if (inet_pton(af, name,
588 (char *)(void *)host_addr) <= 0) {
589 h_errno = HOST_NOT_FOUND;
590 return (NULL);
591 }
592 strncpy(hostbuf, name, MAXDNAME);
593 hostbuf[MAXDNAME] = '\0';
594 bp = hostbuf + MAXDNAME;
595 ep = hostbuf + sizeof hostbuf;
596 host.h_name = hostbuf;
597 host.h_aliases = host_aliases;
598 host_aliases[0] = NULL;
599 h_addr_ptrs[0] = (char *)(void *)host_addr;
600 h_addr_ptrs[1] = NULL;
601 host.h_addr_list = h_addr_ptrs;
602 if (_res.options & RES_USE_INET6)
603 map_v4v6_hostent(&host, &bp, ep);
604 h_errno = NETDB_SUCCESS;
605 return (&host);
606 }
607 if (!isdigit((u_char) *cp) && *cp != '.')
608 break;
609 }
610 if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) ||
611 name[0] == ':')
612 for (cp = name;; ++cp) {
613 if (!*cp) {
614 if (*--cp == '.')
615 break;
616 /*
617 * All-IPv6-legal, no dot at the end.
618 * Fake up a hostent as if we'd actually
619 * done a lookup.
620 */
621 if (inet_pton(af, name,
622 (char *)(void *)host_addr) <= 0) {
623 h_errno = HOST_NOT_FOUND;
624 return (NULL);
625 }
626 strncpy(hostbuf, name, MAXDNAME);
627 hostbuf[MAXDNAME] = '\0';
628 bp = hostbuf + MAXDNAME;
629 ep = hostbuf + sizeof hostbuf;
630 host.h_name = hostbuf;
631 host.h_aliases = host_aliases;
632 host_aliases[0] = NULL;
633 h_addr_ptrs[0] = (char *)(void *)host_addr;
634 h_addr_ptrs[1] = NULL;
635 host.h_addr_list = h_addr_ptrs;
636 h_errno = NETDB_SUCCESS;
637 return (&host);
638 }
639 if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.')
640 break;
641 }
642
643 hp = (struct hostent *)NULL;
644 h_errno = NETDB_INTERNAL;
645 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyname",
646 default_dns_files, name, strlen(name), af) != NS_SUCCESS)
647 return (struct hostent *)NULL;
648 h_errno = NETDB_SUCCESS;
649 return (hp);
650 }
651
652 struct hostent *
653 gethostbyaddr(addr, len, af)
654 const char *addr; /* XXX should have been def'd as u_char! */
655 socklen_t len;
656 int af;
657 {
658 const u_char *uaddr = (const u_char *)addr;
659 socklen_t size;
660 struct hostent *hp;
661 static const ns_dtab dtab[] = {
662 NS_FILES_CB(_gethtbyaddr, NULL)
663 { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */
664 NS_NIS_CB(_yp_gethtbyaddr, NULL)
665 { 0 }
666 };
667
668 _DIAGASSERT(addr != NULL);
669
670 if (af == AF_INET6 && len == IN6ADDRSZ &&
671 (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)(const void *)uaddr) ||
672 IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)(const void *)uaddr))) {
673 h_errno = HOST_NOT_FOUND;
674 return (NULL);
675 }
676 if (af == AF_INET6 && len == IN6ADDRSZ &&
677 (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)(const void *)uaddr) ||
678 IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)(const void *)uaddr))) {
679 /* Unmap. */
680 addr += IN6ADDRSZ - INADDRSZ;
681 uaddr += IN6ADDRSZ - INADDRSZ;
682 af = AF_INET;
683 len = INADDRSZ;
684 }
685 switch (af) {
686 case AF_INET:
687 size = INADDRSZ;
688 break;
689 case AF_INET6:
690 size = IN6ADDRSZ;
691 break;
692 default:
693 errno = EAFNOSUPPORT;
694 h_errno = NETDB_INTERNAL;
695 return (NULL);
696 }
697 if (size != len) {
698 errno = EINVAL;
699 h_errno = NETDB_INTERNAL;
700 return (NULL);
701 }
702 hp = (struct hostent *)NULL;
703 h_errno = NETDB_INTERNAL;
704 if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr",
705 default_dns_files, uaddr, len, af) != NS_SUCCESS)
706 return (struct hostent *)NULL;
707 h_errno = NETDB_SUCCESS;
708 return (hp);
709 }
710
711 void
712 _sethtent(f)
713 int f;
714 {
715 if (!hostf)
716 hostf = fopen(_PATH_HOSTS, "r" );
717 else
718 rewind(hostf);
719 stayopen = f;
720 }
721
722 void
723 _endhtent()
724 {
725 if (hostf && !stayopen) {
726 (void) fclose(hostf);
727 hostf = NULL;
728 }
729 }
730
731 struct hostent *
732 _gethtent()
733 {
734 char *p;
735 char *cp, **q;
736 int af, len;
737
738 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
739 h_errno = NETDB_INTERNAL;
740 return (NULL);
741 }
742 again:
743 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
744 h_errno = HOST_NOT_FOUND;
745 return (NULL);
746 }
747 if (*p == '#')
748 goto again;
749 if (!(cp = strpbrk(p, "#\n")))
750 goto again;
751 *cp = '\0';
752 if (!(cp = strpbrk(p, " \t")))
753 goto again;
754 *cp++ = '\0';
755 if (inet_pton(AF_INET6, p, (char *)(void *)host_addr) > 0) {
756 af = AF_INET6;
757 len = IN6ADDRSZ;
758 } else if (inet_pton(AF_INET, p, (char *)(void *)host_addr) > 0) {
759 if (_res.options & RES_USE_INET6) {
760 map_v4v6_address((char *)(void *)host_addr,
761 (char *)(void *)host_addr);
762 af = AF_INET6;
763 len = IN6ADDRSZ;
764 } else {
765 af = AF_INET;
766 len = INADDRSZ;
767 }
768 } else {
769 goto again;
770 }
771 /* if this is not something we're looking for, skip it. */
772 if (host.h_addrtype != 0 && host.h_addrtype != af)
773 goto again;
774 if (host.h_length != 0 && host.h_length != len)
775 goto again;
776 h_addr_ptrs[0] = (char *)(void *)host_addr;
777 h_addr_ptrs[1] = NULL;
778 host.h_addr_list = h_addr_ptrs;
779 host.h_length = len;
780 host.h_addrtype = af;
781 while (*cp == ' ' || *cp == '\t')
782 cp++;
783 host.h_name = cp;
784 q = host.h_aliases = host_aliases;
785 if ((cp = strpbrk(cp, " \t")) != NULL)
786 *cp++ = '\0';
787 while (cp && *cp) {
788 if (*cp == ' ' || *cp == '\t') {
789 cp++;
790 continue;
791 }
792 if (q < &host_aliases[MAXALIASES - 1])
793 *q++ = cp;
794 if ((cp = strpbrk(cp, " \t")) != NULL)
795 *cp++ = '\0';
796 }
797 *q = NULL;
798 h_errno = NETDB_SUCCESS;
799 return (&host);
800 }
801
802 /*ARGSUSED*/
803 int
804 _gethtbyname(rv, cb_data, ap)
805 void *rv;
806 void *cb_data;
807 va_list ap;
808 {
809 struct hostent *hp;
810 const char *name;
811 int af;
812
813 _DIAGASSERT(rv != NULL);
814
815 name = va_arg(ap, char *);
816 /* NOSTRICT skip len */(void)va_arg(ap, int);
817 af = va_arg(ap, int);
818
819 hp = NULL;
820 #if 0
821 if (_res.options & RES_USE_INET6)
822 hp = _gethtbyname2(name, AF_INET6);
823 if (hp==NULL)
824 hp = _gethtbyname2(name, AF_INET);
825 #else
826 hp = _gethtbyname2(name, af);
827 #endif
828 *((struct hostent **)rv) = hp;
829 if (hp == NULL) {
830 h_errno = HOST_NOT_FOUND;
831 return NS_NOTFOUND;
832 }
833 return NS_SUCCESS;
834 }
835
836 struct hostent *
837 _gethtbyname2(name, af)
838 const char *name;
839 int af;
840 {
841 struct hostent *p;
842 char *tmpbuf, *ptr, **cp;
843 int num;
844 size_t len;
845
846 _DIAGASSERT(name != NULL);
847
848 _sethtent(stayopen);
849 ptr = tmpbuf = NULL;
850 num = 0;
851 while ((p = _gethtent()) != NULL && num < MAXADDRS) {
852 if (p->h_addrtype != af)
853 continue;
854 if (strcasecmp(p->h_name, name) != 0) {
855 for (cp = p->h_aliases; *cp != NULL; cp++)
856 if (strcasecmp(*cp, name) == 0)
857 break;
858 if (*cp == NULL) continue;
859 }
860
861 if (num == 0) {
862 size_t bufsize;
863 char *src;
864
865 bufsize = strlen(p->h_name) + 2 +
866 MAXADDRS * p->h_length +
867 ALIGNBYTES;
868 for (cp = p->h_aliases; *cp != NULL; cp++)
869 bufsize += strlen(*cp) + 1;
870
871 if ((tmpbuf = malloc(bufsize)) == NULL) {
872 h_errno = NETDB_INTERNAL;
873 return NULL;
874 }
875
876 ptr = tmpbuf;
877 src = p->h_name;
878 while ((*ptr++ = *src++) != '\0');
879 for (cp = p->h_aliases; *cp != NULL; cp++) {
880 src = *cp;
881 while ((*ptr++ = *src++) != '\0');
882 }
883 *ptr++ = '\0';
884
885 ptr = (char *)(void *)ALIGN(ptr);
886 }
887
888 (void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length);
889 ptr += p->h_length;
890 num++;
891 }
892 _endhtent();
893 if (num == 0) return NULL;
894
895 len = ptr - tmpbuf;
896 if (len > (sizeof(hostbuf) - ALIGNBYTES)) {
897 free(tmpbuf);
898 errno = ENOSPC;
899 h_errno = NETDB_INTERNAL;
900 return NULL;
901 }
902 ptr = memcpy((void *)ALIGN(hostbuf), tmpbuf, len);
903 free(tmpbuf);
904
905 host.h_name = ptr;
906 while (*ptr++);
907
908 cp = host_aliases;
909 while (*ptr) {
910 *cp++ = ptr;
911 while (*ptr++);
912 }
913 ptr++;
914 *cp = NULL;
915
916 ptr = (char *)(void *)ALIGN(ptr);
917 cp = h_addr_ptrs;
918 while (num--) {
919 *cp++ = ptr;
920 ptr += host.h_length;
921 }
922 *cp = NULL;
923
924 return (&host);
925 }
926
927 /*ARGSUSED*/
928 int
929 _gethtbyaddr(rv, cb_data, ap)
930 void *rv;
931 void *cb_data;
932 va_list ap;
933 {
934 struct hostent *p;
935 const unsigned char *addr;
936 int len, af;
937
938 _DIAGASSERT(rv != NULL);
939
940 addr = va_arg(ap, unsigned char *);
941 len = va_arg(ap, int);
942 af = va_arg(ap, int);
943
944 host.h_length = len;
945 host.h_addrtype = af;
946
947 _sethtent(stayopen);
948 while ((p = _gethtent()) != NULL)
949 if (p->h_addrtype == af && !memcmp(p->h_addr, addr,
950 (size_t)len))
951 break;
952 _endhtent();
953 *((struct hostent **)rv) = p;
954 if (p==NULL) {
955 h_errno = HOST_NOT_FOUND;
956 return NS_NOTFOUND;
957 }
958 return NS_SUCCESS;
959 }
960
961 static void
962 map_v4v6_address(src, dst)
963 const char *src;
964 char *dst;
965 {
966 u_char *p = (u_char *)dst;
967 char tmp[INADDRSZ];
968 int i;
969
970 _DIAGASSERT(src != NULL);
971 _DIAGASSERT(dst != NULL);
972
973 /* Stash a temporary copy so our caller can update in place. */
974 (void)memcpy(tmp, src, INADDRSZ);
975 /* Mark this ipv6 addr as a mapped ipv4. */
976 for (i = 0; i < 10; i++)
977 *p++ = 0x00;
978 *p++ = 0xff;
979 *p++ = 0xff;
980 /* Retrieve the saved copy and we're done. */
981 (void)memcpy((void *)p, tmp, INADDRSZ);
982 }
983
984 static void
985 map_v4v6_hostent(hp, bpp, ep)
986 struct hostent *hp;
987 char **bpp;
988 char *ep;
989 {
990 char **ap;
991
992 _DIAGASSERT(hp != NULL);
993 _DIAGASSERT(bpp != NULL);
994 _DIAGASSERT(ep != NULL);
995
996 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
997 return;
998 hp->h_addrtype = AF_INET6;
999 hp->h_length = IN6ADDRSZ;
1000 for (ap = hp->h_addr_list; *ap; ap++) {
1001 int i = sizeof(align) - (size_t)((u_long)*bpp % sizeof(align));
1002
1003 if (ep - *bpp < (i + IN6ADDRSZ)) {
1004 /* Out of memory. Truncate address list here. XXX */
1005 *ap = NULL;
1006 return;
1007 }
1008 *bpp += i;
1009 map_v4v6_address(*ap, *bpp);
1010 *ap = *bpp;
1011 *bpp += IN6ADDRSZ;
1012 }
1013 }
1014
1015 #ifdef RESOLVSORT
1016 static void
1017 addrsort(ap, num)
1018 char **ap;
1019 int num;
1020 {
1021 int i, j;
1022 char **p;
1023 short aval[MAXADDRS];
1024 int needsort = 0;
1025
1026 _DIAGASSERT(ap != NULL);
1027
1028 p = ap;
1029 for (i = 0; i < num; i++, p++) {
1030 for (j = 0 ; (unsigned)j < _res.nsort; j++)
1031 if (_res.sort_list[j].addr.s_addr ==
1032 (((struct in_addr *)(void *)(*p))->s_addr &
1033 _res.sort_list[j].mask))
1034 break;
1035 aval[i] = j;
1036 if (needsort == 0 && i > 0 && j < aval[i-1])
1037 needsort = i;
1038 }
1039 if (!needsort)
1040 return;
1041
1042 while (needsort < num) {
1043 for (j = needsort - 1; j >= 0; j--) {
1044 if (aval[j] > aval[j+1]) {
1045 char *hp;
1046
1047 i = aval[j];
1048 aval[j] = aval[j+1];
1049 aval[j+1] = i;
1050
1051 hp = ap[j];
1052 ap[j] = ap[j+1];
1053 ap[j+1] = hp;
1054 } else
1055 break;
1056 }
1057 needsort++;
1058 }
1059 }
1060 #endif
1061
1062 struct hostent *
1063 gethostent()
1064 {
1065 host.h_addrtype = 0;
1066 host.h_length = 0;
1067 return (_gethtent());
1068 }
1069
1070 /*ARGSUSED*/
1071 int
1072 _dns_gethtbyname(rv, cb_data, ap)
1073 void *rv;
1074 void *cb_data;
1075 va_list ap;
1076 {
1077 querybuf *buf;
1078 int n, type;
1079 struct hostent *hp;
1080 const char *name;
1081 int af;
1082
1083 _DIAGASSERT(rv != NULL);
1084
1085 name = va_arg(ap, char *);
1086 /* NOSTRICT skip len */(void)va_arg(ap, int);
1087 af = va_arg(ap, int);
1088
1089 switch (af) {
1090 case AF_INET:
1091 type = T_A;
1092 break;
1093 case AF_INET6:
1094 type = T_AAAA;
1095 break;
1096 default:
1097 return NS_UNAVAIL;
1098 }
1099 buf = malloc(sizeof(*buf));
1100 if (buf == NULL) {
1101 h_errno = NETDB_INTERNAL;
1102 return NS_NOTFOUND;
1103 }
1104 n = res_search(name, C_IN, type, buf->buf, sizeof(buf->buf));
1105 if (n < 0) {
1106 free(buf);
1107 dprintf("res_search failed (%d)\n", n);
1108 return NS_NOTFOUND;
1109 }
1110 hp = getanswer(buf, n, name, type);
1111 free(buf);
1112 if (hp == NULL)
1113 switch (h_errno) {
1114 case HOST_NOT_FOUND:
1115 return NS_NOTFOUND;
1116 case TRY_AGAIN:
1117 return NS_TRYAGAIN;
1118 default:
1119 return NS_UNAVAIL;
1120 }
1121 *((struct hostent **)rv) = hp;
1122 return NS_SUCCESS;
1123 }
1124
1125 /*ARGSUSED*/
1126 int
1127 _dns_gethtbyaddr(rv, cb_data, ap)
1128 void *rv;
1129 void *cb_data;
1130 va_list ap;
1131 {
1132 char qbuf[MAXDNAME + 1], *qp, *ep;
1133 int n;
1134 querybuf *buf;
1135 struct hostent *hp;
1136 const unsigned char *uaddr;
1137 int len, af, advance;
1138
1139 _DIAGASSERT(rv != NULL);
1140
1141 uaddr = va_arg(ap, unsigned char *);
1142 len = va_arg(ap, int);
1143 af = va_arg(ap, int);
1144
1145 switch (af) {
1146 case AF_INET:
1147 (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
1148 (uaddr[3] & 0xff), (uaddr[2] & 0xff),
1149 (uaddr[1] & 0xff), (uaddr[0] & 0xff));
1150 break;
1151
1152 case AF_INET6:
1153 qp = qbuf;
1154 ep = qbuf + sizeof(qbuf) - 1;
1155 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
1156 advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.",
1157 uaddr[n] & 0xf,
1158 ((unsigned int)uaddr[n] >> 4) & 0xf);
1159 if (advance > 0 && qp + advance < ep)
1160 qp += advance;
1161 else {
1162 h_errno = NETDB_INTERNAL;
1163 return NS_NOTFOUND;
1164 }
1165 }
1166 if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) {
1167 h_errno = NETDB_INTERNAL;
1168 return NS_NOTFOUND;
1169 }
1170 break;
1171 default:
1172 abort();
1173 }
1174
1175 buf = malloc(sizeof(*buf));
1176 if (buf == NULL) {
1177 h_errno = NETDB_INTERNAL;
1178 return NS_NOTFOUND;
1179 }
1180 n = res_query(qbuf, C_IN, T_PTR, buf->buf, sizeof(buf->buf));
1181 if (n < 0) {
1182 free(buf);
1183 dprintf("res_query failed (%d)\n", n);
1184 return NS_NOTFOUND;
1185 }
1186 hp = getanswer(buf, n, qbuf, T_PTR);
1187 free(buf);
1188 if (hp == NULL)
1189 switch (h_errno) {
1190 case HOST_NOT_FOUND:
1191 return NS_NOTFOUND;
1192 case TRY_AGAIN:
1193 return NS_TRYAGAIN;
1194 default:
1195 return NS_UNAVAIL;
1196 }
1197 hp->h_addrtype = af;
1198 hp->h_length = len;
1199 (void)memcpy(host_addr, uaddr, (size_t)len);
1200 h_addr_ptrs[0] = (char *)(void *)host_addr;
1201 h_addr_ptrs[1] = NULL;
1202 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
1203 map_v4v6_address((char *)(void *)host_addr,
1204 (char *)(void *)host_addr);
1205 hp->h_addrtype = AF_INET6;
1206 hp->h_length = IN6ADDRSZ;
1207 }
1208
1209 *((struct hostent **)rv) = hp;
1210 h_errno = NETDB_SUCCESS;
1211 return NS_SUCCESS;
1212 }
1213
1214 #ifdef YP
1215 /*ARGSUSED*/
1216 struct hostent *
1217 _yphostent(line, af)
1218 char *line;
1219 int af;
1220 {
1221 static struct in_addr host_addrs[MAXADDRS];
1222 static struct in6_addr host6_addrs[MAXADDRS];
1223 char *p = line;
1224 char *cp, **q;
1225 char **hap;
1226 int addrok;
1227 int more;
1228 size_t naddrs;
1229
1230 _DIAGASSERT(line != NULL);
1231
1232 host.h_name = NULL;
1233 host.h_addr_list = h_addr_ptrs;
1234 host.h_addrtype = af;
1235 switch (af) {
1236 case AF_INET:
1237 host.h_length = INADDRSZ;
1238 break;
1239 case AF_INET6:
1240 host.h_length = IN6ADDRSZ;
1241 break;
1242 default:
1243 return (NULL);
1244 }
1245 hap = h_addr_ptrs;
1246 q = host.h_aliases = host_aliases;
1247 naddrs = 0;
1248
1249 nextline:
1250 /* check for host_addrs overflow */
1251 if (naddrs >= sizeof(host_addrs) / sizeof(host_addrs[0]))
1252 goto done;
1253 if (naddrs >= sizeof(host6_addrs) / sizeof(host6_addrs[0]))
1254 goto done;
1255
1256 more = 0;
1257 cp = strpbrk(p, " \t");
1258 if (cp == NULL)
1259 goto done;
1260 *cp++ = '\0';
1261
1262 /* p has should have an address */
1263 switch (af) {
1264 case AF_INET:
1265 addrok = inet_aton(p, &host_addrs[naddrs]);
1266 break;
1267 case AF_INET6:
1268 addrok = inet_pton(af, p, &host6_addrs[naddrs]);
1269 break;
1270 }
1271 if (addrok != 1) {
1272 /* skip to the next line */
1273 while (cp && *cp) {
1274 if (*cp == '\n') {
1275 cp++;
1276 goto nextline;
1277 }
1278 cp++;
1279 }
1280
1281 goto done;
1282 }
1283
1284 switch (af) {
1285 case AF_INET:
1286 *hap++ = (char *)(void *)&host_addrs[naddrs++];
1287 break;
1288 case AF_INET6:
1289 *hap++ = (char *)(void *)&host6_addrs[naddrs++];
1290 break;
1291 }
1292
1293 while (*cp == ' ' || *cp == '\t')
1294 cp++;
1295 p = cp;
1296 cp = strpbrk(p, " \t\n");
1297 if (cp != NULL) {
1298 if (*cp == '\n')
1299 more = 1;
1300 *cp++ = '\0';
1301 }
1302 if (!host.h_name)
1303 host.h_name = p;
1304 else if (strcmp(host.h_name, p)==0)
1305 ;
1306 else if (q < &host_aliases[MAXALIASES - 1])
1307 *q++ = p;
1308 p = cp;
1309 if (more)
1310 goto nextline;
1311
1312 while (cp && *cp) {
1313 if (*cp == ' ' || *cp == '\t') {
1314 cp++;
1315 continue;
1316 }
1317 if (*cp == '\n') {
1318 cp++;
1319 goto nextline;
1320 }
1321 if (q < &host_aliases[MAXALIASES - 1])
1322 *q++ = cp;
1323 cp = strpbrk(cp, " \t");
1324 if (cp != NULL)
1325 *cp++ = '\0';
1326 }
1327
1328 done:
1329 if (host.h_name == NULL)
1330 return (NULL);
1331 *q = NULL;
1332 *hap = NULL;
1333 return (&host);
1334 }
1335
1336 /*ARGSUSED*/
1337 int
1338 _yp_gethtbyaddr(rv, cb_data, ap)
1339 void *rv;
1340 void *cb_data;
1341 va_list ap;
1342 {
1343 struct hostent *hp = (struct hostent *)NULL;
1344 static char *__ypcurrent;
1345 int __ypcurrentlen, r;
1346 char name[INET6_ADDRSTRLEN]; /* XXX enough? */
1347 const unsigned char *uaddr;
1348 int af;
1349 const char *map;
1350
1351 _DIAGASSERT(rv != NULL);
1352
1353 uaddr = va_arg(ap, unsigned char *);
1354 /* NOSTRICT skip len */(void)va_arg(ap, int);
1355 af = va_arg(ap, int);
1356
1357 if (!__ypdomain) {
1358 if (_yp_check(&__ypdomain) == 0)
1359 return NS_UNAVAIL;
1360 }
1361 /*
1362 * XXX unfortunately, we cannot support IPv6 extended scoped address
1363 * notation here. gethostbyaddr() is not scope-aware. too bad.
1364 */
1365 if (inet_ntop(af, uaddr, name, sizeof(name)) == NULL)
1366 return NS_UNAVAIL;
1367 if (__ypcurrent)
1368 free(__ypcurrent);
1369 __ypcurrent = NULL;
1370 switch (af) {
1371 case AF_INET:
1372 map = "hosts.byaddr";
1373 break;
1374 default:
1375 map = "ipnodes.byaddr";
1376 break;
1377 }
1378 r = yp_match(__ypdomain, map, name,
1379 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1380 if (r==0)
1381 hp = _yphostent(__ypcurrent, af);
1382 if (hp==NULL) {
1383 h_errno = HOST_NOT_FOUND;
1384 return NS_NOTFOUND;
1385 }
1386 *((struct hostent **)rv) = hp;
1387 return NS_SUCCESS;
1388 }
1389
1390 /*ARGSUSED*/
1391 int
1392 _yp_gethtbyname(rv, cb_data, ap)
1393 void *rv;
1394 void *cb_data;
1395 va_list ap;
1396 {
1397 struct hostent *hp = (struct hostent *)NULL;
1398 static char *__ypcurrent;
1399 int __ypcurrentlen, r;
1400 const char *name;
1401 int af;
1402 const char *map;
1403
1404 _DIAGASSERT(rv != NULL);
1405
1406 name = va_arg(ap, char *);
1407 /* NOSTRICT skip len */(void)va_arg(ap, int);
1408 af = va_arg(ap, int);
1409
1410 if (!__ypdomain) {
1411 if (_yp_check(&__ypdomain) == 0)
1412 return NS_UNAVAIL;
1413 }
1414 if (__ypcurrent)
1415 free(__ypcurrent);
1416 __ypcurrent = NULL;
1417 switch (af) {
1418 case AF_INET:
1419 map = "hosts.byname";
1420 break;
1421 default:
1422 map = "ipnodes.byname";
1423 break;
1424 }
1425 r = yp_match(__ypdomain, map, name,
1426 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1427 if (r==0)
1428 hp = _yphostent(__ypcurrent, af);
1429 if (hp==NULL) {
1430 h_errno = HOST_NOT_FOUND;
1431 return NS_NOTFOUND;
1432 }
1433 *((struct hostent **)rv) = hp;
1434 return NS_SUCCESS;
1435 }
1436 #endif
1437