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