gethnamaddr.c revision 1.42.2.5 1 /* $NetBSD: gethnamaddr.c,v 1.42.2.5 2002/07/29 15:23:35 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.5 2002/07/29 15:23:35 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(ep != 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 } else
1060 break;
1061 }
1062 needsort++;
1063 }
1064 }
1065 #endif
1066
1067 #if defined(BSD43_BSD43_NFS) || defined(sun)
1068 /* XXX: should we remove this cruft? - lukem */
1069 /* some libc's out there are bound internally to these names (UMIPS) */
1070 void
1071 ht_sethostent(stayopen)
1072 int stayopen;
1073 {
1074 _sethtent(stayopen);
1075 }
1076
1077 void
1078 ht_endhostent()
1079 {
1080 _endhtent();
1081 }
1082
1083 struct hostent *
1084 ht_gethostbyname(name)
1085 char *name;
1086 {
1087 return (_gethtbyname(name));
1088 }
1089
1090 struct hostent *
1091 ht_gethostbyaddr(addr, len, af)
1092 const char *addr;
1093 int len, af;
1094 {
1095 return (_gethtbyaddr(addr, len, af));
1096 }
1097
1098 struct hostent *
1099 gethostent()
1100 {
1101 return (_gethtent());
1102 }
1103
1104 void
1105 dns_service()
1106 {
1107 return;
1108 }
1109
1110 int
1111 dn_skipname(comp_dn, eom)
1112 const u_char *comp_dn, *eom;
1113 {
1114 return (__dn_skipname(comp_dn, eom));
1115 }
1116 #endif /*old-style libc with yp junk in it*/
1117
1118 /*ARGSUSED*/
1119 int
1120 _dns_gethtbyname(rv, cb_data, ap)
1121 void *rv;
1122 void *cb_data;
1123 va_list ap;
1124 {
1125 querybuf buf;
1126 int n, type;
1127 struct hostent *hp;
1128 const char *name;
1129 int af;
1130
1131 _DIAGASSERT(rv != NULL);
1132
1133 name = va_arg(ap, char *);
1134 /* NOSTRICT skip len */(void)va_arg(ap, int);
1135 af = va_arg(ap, int);
1136
1137 switch (af) {
1138 case AF_INET:
1139 type = T_A;
1140 break;
1141 case AF_INET6:
1142 type = T_AAAA;
1143 break;
1144 default:
1145 return NS_UNAVAIL;
1146 }
1147 if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) {
1148 dprintf("res_search failed (%d)\n", n);
1149 return NS_NOTFOUND;
1150 }
1151 hp = getanswer(&buf, n, name, type);
1152 if (hp == NULL)
1153 switch (h_errno) {
1154 case HOST_NOT_FOUND:
1155 return NS_NOTFOUND;
1156 case TRY_AGAIN:
1157 return NS_TRYAGAIN;
1158 default:
1159 return NS_UNAVAIL;
1160 }
1161 *((struct hostent **)rv) = hp;
1162 return NS_SUCCESS;
1163 }
1164
1165 /*ARGSUSED*/
1166 int
1167 _dns_gethtbyaddr(rv, cb_data, ap)
1168 void *rv;
1169 void *cb_data;
1170 va_list ap;
1171 {
1172 char qbuf[MAXDNAME + 1], *qp;
1173 int n;
1174 querybuf buf;
1175 struct hostent *hp;
1176 const unsigned char *uaddr;
1177 int len, af, advance;
1178
1179 _DIAGASSERT(rv != NULL);
1180
1181 uaddr = va_arg(ap, unsigned char *);
1182 len = va_arg(ap, int);
1183 af = va_arg(ap, int);
1184
1185 switch (af) {
1186 case AF_INET:
1187 (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
1188 (uaddr[3] & 0xff), (uaddr[2] & 0xff),
1189 (uaddr[1] & 0xff), (uaddr[0] & 0xff));
1190 break;
1191
1192 case AF_INET6:
1193 qp = qbuf;
1194 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
1195 advance = sprintf(qp, "%x.%x.", uaddr[n] & 0xf,
1196 ((unsigned int)uaddr[n] >> 4) & 0xf);
1197 if (advance > 0 &&
1198 qp + advance < qbuf + sizeof(qbuf) - 1)
1199 qp += advance;
1200 else
1201 return NS_NOTFOUND;
1202 }
1203 strlcat(qbuf, "ip6.arpa", sizeof(qbuf));
1204 break;
1205 default:
1206 abort();
1207 }
1208
1209 n = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf, sizeof(buf));
1210 if (n < 0 && af == AF_INET6) {
1211 *qp = '\0';
1212 strlcat(qbuf, "ip6.int", sizeof(qbuf));
1213 n = res_query(qbuf, C_IN, T_PTR, (u_char *)(void *)&buf,
1214 sizeof(buf));
1215 }
1216 if (n < 0) {
1217 dprintf("res_query failed (%d)\n", n);
1218 return NS_NOTFOUND;
1219 }
1220 hp = getanswer(&buf, n, qbuf, T_PTR);
1221 if (hp == NULL)
1222 switch (h_errno) {
1223 case HOST_NOT_FOUND:
1224 return NS_NOTFOUND;
1225 case TRY_AGAIN:
1226 return NS_TRYAGAIN;
1227 default:
1228 return NS_UNAVAIL;
1229 }
1230 hp->h_addrtype = af;
1231 hp->h_length = len;
1232 (void)memcpy(host_addr, uaddr, (size_t)len);
1233 h_addr_ptrs[0] = (char *)(void *)host_addr;
1234 h_addr_ptrs[1] = NULL;
1235 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
1236 map_v4v6_address((char *)(void *)host_addr,
1237 (char *)(void *)host_addr);
1238 hp->h_addrtype = AF_INET6;
1239 hp->h_length = IN6ADDRSZ;
1240 }
1241
1242 *((struct hostent **)rv) = hp;
1243 h_errno = NETDB_SUCCESS;
1244 return NS_SUCCESS;
1245 }
1246
1247 #ifdef YP
1248 /*ARGSUSED*/
1249 struct hostent *
1250 _yphostent(line, af)
1251 char *line;
1252 int af;
1253 {
1254 static struct in_addr host_addrs[MAXADDRS];
1255 static struct in6_addr host6_addrs[MAXADDRS];
1256 char *p = line;
1257 char *cp, **q;
1258 char **hap;
1259 int addrok;
1260 int more;
1261 int naddrs;
1262
1263 _DIAGASSERT(line != NULL);
1264
1265 host.h_name = NULL;
1266 host.h_addr_list = h_addr_ptrs;
1267 host.h_addrtype = af;
1268 switch (af) {
1269 case AF_INET:
1270 host.h_length = INADDRSZ;
1271 break;
1272 case AF_INET6:
1273 host.h_length = IN6ADDRSZ;
1274 break;
1275 default:
1276 return (NULL);
1277 }
1278 hap = h_addr_ptrs;
1279 q = host.h_aliases = host_aliases;
1280 naddrs = 0;
1281
1282 nextline:
1283 /* check for host_addrs overflow */
1284 if (naddrs >= sizeof(host_addrs) / sizeof(host_addrs[0]))
1285 goto done;
1286 if (naddrs >= sizeof(host6_addrs) / sizeof(host6_addrs[0]))
1287 goto done;
1288
1289 more = 0;
1290 cp = strpbrk(p, " \t");
1291 if (cp == NULL)
1292 goto done;
1293 *cp++ = '\0';
1294
1295 /* p has should have an address */
1296 switch (af) {
1297 case AF_INET:
1298 addrok = inet_aton(p, &host_addrs[naddrs]);
1299 break;
1300 case AF_INET6:
1301 addrok = inet_pton(af, p, &host6_addrs[naddrs]);
1302 break;
1303 }
1304 if (addrok != 1) {
1305 /* skip to the next line */
1306 while (cp && *cp) {
1307 if (*cp == '\n') {
1308 cp++;
1309 goto nextline;
1310 }
1311 cp++;
1312 }
1313
1314 goto done;
1315 }
1316
1317 switch (af) {
1318 case AF_INET:
1319 *hap++ = (char *)(void *)&host_addrs[naddrs++];
1320 break;
1321 case AF_INET6:
1322 *hap++ = (char *)(void *)&host6_addrs[naddrs++];
1323 break;
1324 }
1325
1326 while (*cp == ' ' || *cp == '\t')
1327 cp++;
1328 p = cp;
1329 cp = strpbrk(p, " \t\n");
1330 if (cp != NULL) {
1331 if (*cp == '\n')
1332 more = 1;
1333 *cp++ = '\0';
1334 }
1335 if (!host.h_name)
1336 host.h_name = p;
1337 else if (strcmp(host.h_name, p)==0)
1338 ;
1339 else if (q < &host_aliases[MAXALIASES - 1])
1340 *q++ = p;
1341 p = cp;
1342 if (more)
1343 goto nextline;
1344
1345 while (cp && *cp) {
1346 if (*cp == ' ' || *cp == '\t') {
1347 cp++;
1348 continue;
1349 }
1350 if (*cp == '\n') {
1351 cp++;
1352 goto nextline;
1353 }
1354 if (q < &host_aliases[MAXALIASES - 1])
1355 *q++ = cp;
1356 cp = strpbrk(cp, " \t");
1357 if (cp != NULL)
1358 *cp++ = '\0';
1359 }
1360
1361 done:
1362 if (host.h_name == NULL)
1363 return (NULL);
1364 *q = NULL;
1365 *hap = NULL;
1366 return (&host);
1367 }
1368
1369 /*ARGSUSED*/
1370 int
1371 _yp_gethtbyaddr(rv, cb_data, ap)
1372 void *rv;
1373 void *cb_data;
1374 va_list ap;
1375 {
1376 struct hostent *hp = (struct hostent *)NULL;
1377 static char *__ypcurrent;
1378 int __ypcurrentlen, r;
1379 char name[INET6_ADDRSTRLEN]; /* XXX enough? */
1380 const unsigned char *uaddr;
1381 int af;
1382 const char *map;
1383
1384 _DIAGASSERT(rv != NULL);
1385
1386 uaddr = va_arg(ap, unsigned char *);
1387 /* NOSTRICT skip len */(void)va_arg(ap, int);
1388 af = va_arg(ap, int);
1389
1390 if (!__ypdomain) {
1391 if (_yp_check(&__ypdomain) == 0)
1392 return NS_UNAVAIL;
1393 }
1394 /*
1395 * XXX unfortunately, we cannot support IPv6 extended scoped address
1396 * notation here. gethostbyaddr() is not scope-aware. too bad.
1397 */
1398 if (inet_ntop(af, uaddr, name, sizeof(name)) == NULL)
1399 return NS_UNAVAIL;
1400 if (__ypcurrent)
1401 free(__ypcurrent);
1402 __ypcurrent = NULL;
1403 switch (af) {
1404 case AF_INET:
1405 map = "hosts.byaddr";
1406 break;
1407 default:
1408 map = "ipnodes.byaddr";
1409 break;
1410 }
1411 r = yp_match(__ypdomain, map, name,
1412 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1413 if (r==0)
1414 hp = _yphostent(__ypcurrent, af);
1415 if (hp==NULL) {
1416 h_errno = HOST_NOT_FOUND;
1417 return NS_NOTFOUND;
1418 }
1419 *((struct hostent **)rv) = hp;
1420 return NS_SUCCESS;
1421 }
1422
1423 /*ARGSUSED*/
1424 int
1425 _yp_gethtbyname(rv, cb_data, ap)
1426 void *rv;
1427 void *cb_data;
1428 va_list ap;
1429 {
1430 struct hostent *hp = (struct hostent *)NULL;
1431 static char *__ypcurrent;
1432 int __ypcurrentlen, r;
1433 const char *name;
1434 int af;
1435 const char *map;
1436
1437 _DIAGASSERT(rv != NULL);
1438
1439 name = va_arg(ap, char *);
1440 /* NOSTRICT skip len */(void)va_arg(ap, int);
1441 af = va_arg(ap, int);
1442
1443 if (!__ypdomain) {
1444 if (_yp_check(&__ypdomain) == 0)
1445 return NS_UNAVAIL;
1446 }
1447 if (__ypcurrent)
1448 free(__ypcurrent);
1449 __ypcurrent = NULL;
1450 switch (af) {
1451 case AF_INET:
1452 map = "hosts.byname";
1453 break;
1454 default:
1455 map = "ipnodes.byname";
1456 break;
1457 }
1458 r = yp_match(__ypdomain, map, name,
1459 (int)strlen(name), &__ypcurrent, &__ypcurrentlen);
1460 if (r==0)
1461 hp = _yphostent(__ypcurrent, af);
1462 if (hp==NULL) {
1463 h_errno = HOST_NOT_FOUND;
1464 return NS_NOTFOUND;
1465 }
1466 *((struct hostent **)rv) = hp;
1467 return NS_SUCCESS;
1468 }
1469 #endif
1470