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