gethnamaddr.c revision 1.46 1 /* $NetBSD: gethnamaddr.c,v 1.46 2002/06/26 06:00:07 itojun 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. 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 #include <sys/cdefs.h>
59 #if defined(LIBC_SCCS) && !defined(lint)
60 #if 0
61 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
62 static char rcsid[] = "Id: gethnamaddr.c,v 8.21 1997/06/01 20:34:37 vixie Exp ";
63 #else
64 __RCSID("$NetBSD: gethnamaddr.c,v 1.46 2002/06/26 06:00:07 itojun Exp $");
65 #endif
66 #endif /* LIBC_SCCS and not lint */
67
68 #if defined(_LIBC)
69 #include "namespace.h"
70 #endif
71 #include <sys/param.h>
72 #include <sys/socket.h>
73 #include <netinet/in.h>
74 #include <arpa/inet.h>
75 #include <arpa/nameser.h>
76
77 #include <assert.h>
78 #include <ctype.h>
79 #include <errno.h>
80 #include <netdb.h>
81 #include <resolv.h>
82 #include <stdarg.h>
83 #include <stdio.h>
84 #include <syslog.h>
85
86 #ifndef LOG_AUTH
87 # define LOG_AUTH 0
88 #endif
89
90 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
91
92 #include <nsswitch.h>
93 #include <stdlib.h>
94 #include <string.h>
95
96 #ifdef YP
97 #include <rpc/rpc.h>
98 #include <rpcsvc/yp_prot.h>
99 #include <rpcsvc/ypclnt.h>
100 #endif
101
102 #if defined(_LIBC) && defined(__weak_alias)
103 __weak_alias(gethostbyaddr,_gethostbyaddr)
104 __weak_alias(gethostbyname,_gethostbyname)
105 #endif
106
107 #define MAXALIASES 35
108 #define MAXADDRS 35
109
110 static const char AskedForGot[] =
111 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
112
113 static char *h_addr_ptrs[MAXADDRS + 1];
114
115 #ifdef YP
116 static char *__ypdomain;
117 #endif
118
119 static struct hostent host;
120 static char *host_aliases[MAXALIASES];
121 static char hostbuf[8*1024];
122 static u_int32_t host_addr[16 / sizeof(u_int32_t)]; /* IPv4 or IPv6 */
123 static FILE *hostf = NULL;
124 static int stayopen = 0;
125
126
127 #if PACKETSZ > 1024
128 #define MAXPACKET PACKETSZ
129 #else
130 #define MAXPACKET 1024
131 #endif
132
133 typedef union {
134 HEADER hdr;
135 u_char buf[MAXPACKET];
136 } querybuf;
137
138 typedef union {
139 int32_t al;
140 char ac;
141 } align;
142
143 #ifdef DEBUG
144 static void dprintf __P((char *, ...))
145 __attribute__((__format__(__printf__, 1, 2)));
146 #endif
147 static struct hostent *getanswer __P((const querybuf *, int,
148 const char *, int));
149 static void map_v4v6_address __P((const char *, char *));
150 static void map_v4v6_hostent __P((struct hostent *, char **, char *));
151 #ifdef RESOLVSORT
152 static void addrsort __P((char **, int));
153 #endif
154
155 void _sethtent __P((int));
156 void _endhtent __P((void));
157 struct hostent *_gethtent __P((void));
158 struct hostent *_gethtbyname2 __P((const char *, int));
159 void ht_sethostent __P((int));
160 void ht_endhostent __P((void));
161 struct hostent *ht_gethostbyname __P((char *));
162 struct hostent *ht_gethostbyaddr __P((const char *, int, int ));
163 void dns_service __P((void));
164 #undef dn_skipname
165 int dn_skipname __P((const u_char *, const u_char *));
166 int _gethtbyaddr __P((void *, void *, va_list));
167 int _gethtbyname __P((void *, void *, va_list));
168 int _dns_gethtbyaddr __P((void *, void *, va_list));
169 int _dns_gethtbyname __P((void *, void *, va_list));
170 #ifdef YP
171 struct hostent *_yphostent __P((char *, int));
172 int _yp_gethtbyaddr __P((void *, void *, va_list));
173 int _yp_gethtbyname __P((void *, void *, va_list));
174 #endif
175
176 static const ns_src default_dns_files[] = {
177 { NSSRC_FILES, NS_SUCCESS },
178 { NSSRC_DNS, NS_SUCCESS },
179 { 0 }
180 };
181
182
183 #ifdef DEBUG
184 static void
185 dprintf(char *msg, ...)
186 {
187 _DIAGASSERT(msg != NULL);
188
189 if (_res.options & RES_DEBUG) {
190 int save = errno;
191 va_list ap;
192
193 va_start (ap, msg);
194 vprintf(msg, ap);
195 va_end (ap);
196
197 errno = save;
198 }
199 }
200 #else
201 # define dprintf(msg, num) /*nada*/
202 #endif
203
204 #define BOUNDED_INCR(x) \
205 do { \
206 cp += x; \
207 if (cp > eom) { \
208 h_errno = NO_RECOVERY; \
209 return (NULL); \
210 } \
211 } while (/*CONSTCOND*/0)
212
213 #define BOUNDS_CHECK(ptr, count) \
214 do { \
215 if ((ptr) + (count) > eom) { \
216 h_errno = NO_RECOVERY; \
217 return (NULL); \
218 } \
219 } while (/*CONSTCOND*/0)
220
221 static struct hostent *
222 getanswer(answer, anslen, qname, qtype)
223 const querybuf *answer;
224 int anslen;
225 const char *qname;
226 int qtype;
227 {
228 const HEADER *hp;
229 const u_char *cp;
230 int n;
231 const u_char *eom, *erdata;
232 char *bp, **ap, **hap, *ep;
233 int type, class, ancount, qdcount;
234 int haveanswer, had_error;
235 int toobig = 0;
236 char tbuf[MAXDNAME];
237 const char *tname;
238 int (*name_ok) __P((const char *));
239
240 _DIAGASSERT(answer != NULL);
241 _DIAGASSERT(qname != NULL);
242
243 tname = qname;
244 host.h_name = NULL;
245 eom = answer->buf + anslen;
246 switch (qtype) {
247 case T_A:
248 case T_AAAA:
249 name_ok = res_hnok;
250 break;
251 case T_PTR:
252 name_ok = res_dnok;
253 break;
254 default:
255 return (NULL); /* XXX should be abort(); */
256 }
257 /*
258 * find first satisfactory answer
259 */
260 hp = &answer->hdr;
261 ancount = ntohs(hp->ancount);
262 qdcount = ntohs(hp->qdcount);
263 bp = hostbuf;
264 ep = hostbuf + sizeof hostbuf;
265 cp = answer->buf;
266 BOUNDED_INCR(HFIXEDSZ);
267 if (qdcount != 1) {
268 h_errno = NO_RECOVERY;
269 return (NULL);
270 }
271 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
272 if ((n < 0) || !(*name_ok)(bp)) {
273 h_errno = NO_RECOVERY;
274 return (NULL);
275 }
276 BOUNDED_INCR(n + QFIXEDSZ);
277 if (qtype == T_A || qtype == T_AAAA) {
278 /* res_send() has already verified that the query name is the
279 * same as the one we sent; this just gets the expanded name
280 * (i.e., with the succeeding search-domain tacked on).
281 */
282 n = strlen(bp) + 1; /* for the \0 */
283 if (n >= MAXHOSTNAMELEN) {
284 h_errno = NO_RECOVERY;
285 return (NULL);
286 }
287 host.h_name = bp;
288 bp += n;
289 /* The qname can be abbreviated, but h_name is now absolute. */
290 qname = host.h_name;
291 }
292 ap = host_aliases;
293 *ap = NULL;
294 host.h_aliases = host_aliases;
295 hap = h_addr_ptrs;
296 *hap = NULL;
297 host.h_addr_list = h_addr_ptrs;
298 haveanswer = 0;
299 had_error = 0;
300 while (ancount-- > 0 && cp < eom && !had_error) {
301 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
302 if ((n < 0) || !(*name_ok)(bp)) {
303 had_error++;
304 continue;
305 }
306 cp += n; /* name */
307 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
308 type = _getshort(cp);
309 cp += INT16SZ; /* type */
310 class = _getshort(cp);
311 cp += INT16SZ + INT32SZ; /* class, TTL */
312 n = _getshort(cp);
313 cp += INT16SZ; /* len */
314 BOUNDS_CHECK(cp, n);
315 erdata = cp + n;
316 if (class != C_IN) {
317 /* XXX - debug? syslog? */
318 cp += n;
319 continue; /* XXX - had_error++ ? */
320 }
321 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
322 if (ap >= &host_aliases[MAXALIASES-1])
323 continue;
324 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
325 if ((n < 0) || !(*name_ok)(tbuf)) {
326 had_error++;
327 continue;
328 }
329 cp += n;
330 if (cp != erdata) {
331 h_errno = NO_RECOVERY;
332 return (NULL);
333 }
334 /* Store alias. */
335 *ap++ = bp;
336 n = strlen(bp) + 1; /* for the \0 */
337 if (n >= MAXHOSTNAMELEN) {
338 had_error++;
339 continue;
340 }
341 bp += n;
342 /* Get canonical name. */
343 n = strlen(tbuf) + 1; /* for the \0 */
344 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
345 had_error++;
346 continue;
347 }
348 strcpy(bp, tbuf);
349 host.h_name = bp;
350 bp += n;
351 continue;
352 }
353 if (qtype == T_PTR && type == T_CNAME) {
354 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
355 if (n < 0 || !res_dnok(tbuf)) {
356 had_error++;
357 continue;
358 }
359 cp += n;
360 if (cp != erdata) {
361 h_errno = NO_RECOVERY;
362 return (NULL);
363 }
364 /* Get canonical name. */
365 n = strlen(tbuf) + 1; /* for the \0 */
366 if (n > ep - bp || n >= MAXHOSTNAMELEN) {
367 had_error++;
368 continue;
369 }
370 strcpy(bp, tbuf);
371 tname = bp;
372 bp += n;
373 continue;
374 }
375 if (type != qtype) {
376 if (type != T_KEY && type != T_SIG)
377 syslog(LOG_NOTICE|LOG_AUTH,
378 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
379 qname, p_class(C_IN), p_type(qtype),
380 p_type(type));
381 cp += n;
382 continue; /* XXX - had_error++ ? */
383 }
384 switch (type) {
385 case T_PTR:
386 if (strcasecmp(tname, bp) != 0) {
387 syslog(LOG_NOTICE|LOG_AUTH,
388 AskedForGot, qname, bp);
389 cp += n;
390 continue; /* XXX - had_error++ ? */
391 }
392 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
393 if ((n < 0) || !res_hnok(bp)) {
394 had_error++;
395 break;
396 }
397 #if MULTI_PTRS_ARE_ALIASES
398 cp += n;
399 if (cp != erdata) {
400 h_errno = NO_RECOVERY;
401 return (NULL);
402 }
403 if (!haveanswer)
404 host.h_name = bp;
405 else if (ap < &host_aliases[MAXALIASES-1])
406 *ap++ = bp;
407 else
408 n = -1;
409 if (n != -1) {
410 n = strlen(bp) + 1; /* for the \0 */
411 if (n >= MAXHOSTNAMELEN) {
412 had_error++;
413 break;
414 }
415 bp += n;
416 }
417 break;
418 #else
419 host.h_name = bp;
420 if (_res.options & RES_USE_INET6) {
421 n = strlen(bp) + 1; /* for the \0 */
422 if (n >= MAXHOSTNAMELEN) {
423 had_error++;
424 break;
425 }
426 bp += n;
427 map_v4v6_hostent(&host, &bp, ep);
428 }
429 h_errno = NETDB_SUCCESS;
430 return (&host);
431 #endif
432 case T_A:
433 case T_AAAA:
434 if (strcasecmp(host.h_name, bp) != 0) {
435 syslog(LOG_NOTICE|LOG_AUTH,
436 AskedForGot, host.h_name, bp);
437 cp += n;
438 continue; /* XXX - had_error++ ? */
439 }
440 if (n != host.h_length) {
441 cp += n;
442 continue;
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 strcpy(bp, qname);
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 int 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 != af)
773 goto again;
774 if (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(0);
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(0);
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(lenp != 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 #if defined(BSD43_BSD43_NFS) || defined(sun)
1063 /* XXX: should we remove this cruft? - lukem */
1064 /* some libc's out there are bound internally to these names (UMIPS) */
1065 void
1066 ht_sethostent(stayopen)
1067 int stayopen;
1068 {
1069 _sethtent(stayopen);
1070 }
1071
1072 void
1073 ht_endhostent()
1074 {
1075 _endhtent();
1076 }
1077
1078 struct hostent *
1079 ht_gethostbyname(name)
1080 char *name;
1081 {
1082 return (_gethtbyname(name));
1083 }
1084
1085 struct hostent *
1086 ht_gethostbyaddr(addr, len, af)
1087 const char *addr;
1088 int len, af;
1089 {
1090 return (_gethtbyaddr(addr, len, af));
1091 }
1092
1093 struct hostent *
1094 gethostent()
1095 {
1096 return (_gethtent());
1097 }
1098
1099 void
1100 dns_service()
1101 {
1102 return;
1103 }
1104
1105 int
1106 dn_skipname(comp_dn, eom)
1107 const u_char *comp_dn, *eom;
1108 {
1109 return (__dn_skipname(comp_dn, eom));
1110 }
1111 #endif /*old-style libc with yp junk in it*/
1112
1113 /*ARGSUSED*/
1114 int
1115 _dns_gethtbyname(rv, cb_data, ap)
1116 void *rv;
1117 void *cb_data;
1118 va_list ap;
1119 {
1120 querybuf buf;
1121 int n, type;
1122 struct hostent *hp;
1123 const char *name;
1124 int af;
1125
1126 _DIAGASSERT(rv != NULL);
1127
1128 name = va_arg(ap, char *);
1129 /* NOSTRICT skip len */(void)va_arg(ap, int);
1130 af = va_arg(ap, int);
1131
1132 switch (af) {
1133 case AF_INET:
1134 type = T_A;
1135 break;
1136 case AF_INET6:
1137 type = T_AAAA;
1138 break;
1139 default:
1140 return NS_UNAVAIL;
1141 }
1142 if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) {
1143 dprintf("res_search failed (%d)\n", n);
1144 return NS_NOTFOUND;
1145 }
1146 hp = getanswer(&buf, n, name, type);
1147 if (hp == NULL)
1148 switch (h_errno) {
1149 case HOST_NOT_FOUND:
1150 return NS_NOTFOUND;
1151 case TRY_AGAIN:
1152 return NS_TRYAGAIN;
1153 default:
1154 return NS_UNAVAIL;
1155 }
1156 *((struct hostent **)rv) = hp;
1157 return NS_SUCCESS;
1158 }
1159
1160 /*ARGSUSED*/
1161 int
1162 _dns_gethtbyaddr(rv, cb_data, ap)
1163 void *rv;
1164 void *cb_data;
1165 va_list ap;
1166 {
1167 char qbuf[MAXDNAME + 1], *qp;
1168 int n;
1169 querybuf buf;
1170 struct hostent *hp;
1171 const unsigned char *uaddr;
1172 int len, af, advance;
1173
1174 _DIAGASSERT(rv != NULL);
1175
1176 uaddr = va_arg(ap, unsigned char *);
1177 len = va_arg(ap, int);
1178 af = va_arg(ap, int);
1179
1180 switch (af) {
1181 case AF_INET:
1182 (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
1183 (uaddr[3] & 0xff), (uaddr[2] & 0xff),
1184 (uaddr[1] & 0xff), (uaddr[0] & 0xff));
1185 break;
1186
1187 case AF_INET6:
1188 qp = qbuf;
1189 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
1190 advance = sprintf(qp, "%x.%x.", uaddr[n] & 0xf,
1191 ((unsigned int)uaddr[n] >> 4) & 0xf);
1192 if (advance > 0 &&
1193 qp + advance < qbuf + sizeof(qbuf) - 1)
1194 qp += advance;
1195 else
1196 return NS_NOTFOUND;
1197 }
1198 strlcat(qbuf, "ip6.int", sizeof(qbuf));
1199 break;
1200 default:
1201 abort();
1202 }
1203
1204 n = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf, sizeof(buf));
1205 if (n < 0) {
1206 dprintf("res_query failed (%d)\n", n);
1207 return NS_NOTFOUND;
1208 }
1209 hp = getanswer(&buf, n, qbuf, T_PTR);
1210 if (hp == NULL)
1211 switch (h_errno) {
1212 case HOST_NOT_FOUND:
1213 return NS_NOTFOUND;
1214 case TRY_AGAIN:
1215 return NS_TRYAGAIN;
1216 default:
1217 return NS_UNAVAIL;
1218 }
1219 hp->h_addrtype = af;
1220 hp->h_length = len;
1221 (void)memcpy(host_addr, uaddr, (size_t)len);
1222 h_addr_ptrs[0] = (char *)(void *)host_addr;
1223 h_addr_ptrs[1] = NULL;
1224 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
1225 map_v4v6_address((char *)(void *)host_addr,
1226 (char *)(void *)host_addr);
1227 hp->h_addrtype = AF_INET6;
1228 hp->h_length = IN6ADDRSZ;
1229 }
1230
1231 *((struct hostent **)rv) = hp;
1232 h_errno = NETDB_SUCCESS;
1233 return NS_SUCCESS;
1234 }
1235
1236 #ifdef YP
1237 /*ARGSUSED*/
1238 struct hostent *
1239 _yphostent(line, af)
1240 char *line;
1241 int af;
1242 {
1243 static struct in_addr host_addrs[MAXADDRS];
1244 static struct in6_addr host6_addrs[MAXADDRS];
1245 char *p = line;
1246 char *cp, **q;
1247 char **hap;
1248 int addrok;
1249 int more;
1250 int naddrs;
1251
1252 _DIAGASSERT(line != NULL);
1253
1254 host.h_name = NULL;
1255 host.h_addr_list = h_addr_ptrs;
1256 host.h_addrtype = af;
1257 switch (af) {
1258 case AF_INET:
1259 host.h_length = INADDRSZ;
1260 break;
1261 case AF_INET6:
1262 host.h_length = IN6ADDRSZ;
1263 break;
1264 default:
1265 return (NULL);
1266 }
1267 hap = h_addr_ptrs;
1268 q = host.h_aliases = host_aliases;
1269 naddrs = 0;
1270
1271 nextline:
1272 /* check for host_addrs overflow */
1273 if (naddrs >= sizeof(host_addrs) / sizeof(host_addrs[0]))
1274 goto done;
1275 if (naddrs >= sizeof(host6_addrs) / sizeof(host6_addrs[0]))
1276 goto done;
1277
1278 more = 0;
1279 cp = strpbrk(p, " \t");
1280 if (cp == NULL)
1281 goto done;
1282 *cp++ = '\0';
1283
1284 /* p has should have an address */
1285 switch (af) {
1286 case AF_INET:
1287 addrok = inet_aton(p, &host_addrs[naddrs]);
1288 break;
1289 case AF_INET6:
1290 addrok = inet_pton(af, p, &host6_addrs[naddrs]);
1291 break;
1292 }
1293 if (addrok != 1) {
1294 /* skip to the next line */
1295 while (cp && *cp) {
1296 if (*cp == '\n') {
1297 cp++;
1298 goto nextline;
1299 }
1300 cp++;
1301 }
1302
1303 goto done;
1304 }
1305
1306 switch (af) {
1307 case AF_INET:
1308 *hap++ = (char *)(void *)&host_addrs[naddrs++];
1309 break;
1310 case AF_INET6:
1311 *hap++ = (char *)(void *)&host6_addrs[naddrs++];
1312 break;
1313 }
1314
1315 while (*cp == ' ' || *cp == '\t')
1316 cp++;
1317 p = cp;
1318 cp = strpbrk(p, " \t\n");
1319 if (cp != NULL) {
1320 if (*cp == '\n')
1321 more = 1;
1322 *cp++ = '\0';
1323 }
1324 if (!host.h_name)
1325 host.h_name = p;
1326 else if (strcmp(host.h_name, p)==0)
1327 ;
1328 else if (q < &host_aliases[MAXALIASES - 1])
1329 *q++ = p;
1330 p = cp;
1331 if (more)
1332 goto nextline;
1333
1334 while (cp && *cp) {
1335 if (*cp == ' ' || *cp == '\t') {
1336 cp++;
1337 continue;
1338 }
1339 if (*cp == '\n') {
1340 cp++;
1341 goto nextline;
1342 }
1343 if (q < &host_aliases[MAXALIASES - 1])
1344 *q++ = cp;
1345 cp = strpbrk(cp, " \t");
1346 if (cp != NULL)
1347 *cp++ = '\0';
1348 }
1349
1350 done:
1351 if (host.h_name == NULL)
1352 return (NULL);
1353 *q = NULL;
1354 *hap = NULL;
1355 return (&host);
1356 }
1357
1358 /*ARGSUSED*/
1359 int
1360 _yp_gethtbyaddr(rv, cb_data, ap)
1361 void *rv;
1362 void *cb_data;
1363 va_list ap;
1364 {
1365 struct hostent *hp = (struct hostent *)NULL;
1366 static char *__ypcurrent;
1367 int __ypcurrentlen, r;
1368 char name[INET6_ADDRSTRLEN]; /* XXX enough? */
1369 const unsigned char *uaddr;
1370 int af;
1371 const char *map;
1372
1373 _DIAGASSERT(rv != NULL);
1374
1375 uaddr = va_arg(ap, unsigned char *);
1376 /* NOSTRICT skip len */(void)va_arg(ap, int);
1377 af = va_arg(ap, int);
1378
1379 if (!__ypdomain) {
1380 if (_yp_check(&__ypdomain) == 0)
1381 return NS_UNAVAIL;
1382 }
1383 /*
1384 * XXX unfortunately, we cannot support IPv6 extended scoped address
1385 * notation here. gethostbyaddr() is not scope-aware. too bad.
1386 */
1387 if (inet_ntop(af, uaddr, name, sizeof(name)) == NULL)
1388 return NS_UNAVAIL;
1389 if (__ypcurrent)
1390 free(__ypcurrent);
1391 __ypcurrent = NULL;
1392 switch (af) {
1393 case AF_INET:
1394 map = "hosts.byaddr";
1395 break;
1396 default:
1397 map = "ipnodes.byaddr";
1398 break;
1399 }
1400 r = yp_match(__ypdomain, map, name,
1401 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1402 if (r==0)
1403 hp = _yphostent(__ypcurrent, af);
1404 if (hp==NULL) {
1405 h_errno = HOST_NOT_FOUND;
1406 return NS_NOTFOUND;
1407 }
1408 *((struct hostent **)rv) = hp;
1409 return NS_SUCCESS;
1410 }
1411
1412 /*ARGSUSED*/
1413 int
1414 _yp_gethtbyname(rv, cb_data, ap)
1415 void *rv;
1416 void *cb_data;
1417 va_list ap;
1418 {
1419 struct hostent *hp = (struct hostent *)NULL;
1420 static char *__ypcurrent;
1421 int __ypcurrentlen, r;
1422 const char *name;
1423 int af;
1424 const char *map;
1425
1426 _DIAGASSERT(rv != NULL);
1427
1428 name = va_arg(ap, char *);
1429 /* NOSTRICT skip len */(void)va_arg(ap, int);
1430 af = va_arg(ap, int);
1431
1432 if (!__ypdomain) {
1433 if (_yp_check(&__ypdomain) == 0)
1434 return NS_UNAVAIL;
1435 }
1436 if (__ypcurrent)
1437 free(__ypcurrent);
1438 __ypcurrent = NULL;
1439 switch (af) {
1440 case AF_INET:
1441 map = "hosts.byname";
1442 break;
1443 default:
1444 map = "ipnodes.byname";
1445 break;
1446 }
1447 r = yp_match(__ypdomain, map, name,
1448 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1449 if (r==0)
1450 hp = _yphostent(__ypcurrent, af);
1451 if (hp==NULL) {
1452 h_errno = HOST_NOT_FOUND;
1453 return NS_NOTFOUND;
1454 }
1455 *((struct hostent **)rv) = hp;
1456 return NS_SUCCESS;
1457 }
1458 #endif
1459