gethnamaddr.c revision 1.1.1.2 1 /*
2 * ++Copyright++ 1985, 1988, 1993
3 * -
4 * Copyright (c) 1985, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 * -
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
36 *
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
43 *
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * SOFTWARE.
52 * -
53 * --Copyright--
54 */
55
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
58 static char rcsid[] = "Id: gethnamaddr.c,v 8.23 1998/04/07 04:59:46 vixie Exp ";
59 #endif /* LIBC_SCCS and not lint */
60
61 #include <sys/types.h>
62 #include <sys/param.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <arpa/nameser.h>
67
68 #include <stdio.h>
69 #include <netdb.h>
70 #include <resolv.h>
71 #include <ctype.h>
72 #include <errno.h>
73 #include <syslog.h>
74
75 #ifndef LOG_AUTH
76 # define LOG_AUTH 0
77 #endif
78
79 #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */
80
81 #if defined(BSD) && (BSD >= 199103) && defined(AF_INET6)
82 # include <stdlib.h>
83 # include <string.h>
84 #else
85 # include "../conf/portability.h"
86 #endif
87
88 #if defined(USE_OPTIONS_H)
89 # include <../conf/options.h>
90 #endif
91
92 #ifdef SPRINTF_CHAR
93 # define SPRINTF(x) strlen(sprintf/**/x)
94 #else
95 # define SPRINTF(x) ((size_t)sprintf x)
96 #endif
97
98 #define MAXALIASES 35
99 #define MAXADDRS 35
100
101 static const char AskedForGot[] =
102 "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
103
104 static char *h_addr_ptrs[MAXADDRS + 1];
105
106 static struct hostent host;
107 static char *host_aliases[MAXALIASES];
108 static char hostbuf[8*1024];
109 static u_char host_addr[16]; /* IPv4 or IPv6 */
110 static FILE *hostf = NULL;
111 static int stayopen = 0;
112
113 static void map_v4v6_address __P((const char *src, char *dst));
114 static void map_v4v6_hostent __P((struct hostent *hp, char **bp, int *len));
115
116 #ifdef RESOLVSORT
117 static void addrsort __P((char **, int));
118 #endif
119
120 #if PACKETSZ > 1024
121 #define MAXPACKET PACKETSZ
122 #else
123 #define MAXPACKET 1024
124 #endif
125
126 typedef union {
127 HEADER hdr;
128 u_char buf[MAXPACKET];
129 } querybuf;
130
131 typedef union {
132 int32_t al;
133 char ac;
134 } align;
135
136 extern int h_errno;
137
138 #ifdef DEBUG
139 static void
140 dprintf(msg, num)
141 char *msg;
142 int num;
143 {
144 if (_res.options & RES_DEBUG) {
145 int save = errno;
146
147 printf(msg, num);
148 errno = save;
149 }
150 }
151 #else
152 # define dprintf(msg, num) /*nada*/
153 #endif
154
155 #define BOUNDED_INCR(x) \
156 do { \
157 cp += x; \
158 if (cp > eom) { \
159 h_errno = NO_RECOVERY; \
160 return (NULL); \
161 } \
162 } while (0)
163
164 #define BOUNDS_CHECK(ptr, count) \
165 do { \
166 if ((ptr) + (count) > eom) { \
167 h_errno = NO_RECOVERY; \
168 return (NULL); \
169 } \
170 } while (0)
171
172 static struct hostent *
173 getanswer(answer, anslen, qname, qtype)
174 const querybuf *answer;
175 int anslen;
176 const char *qname;
177 int qtype;
178 {
179 register const HEADER *hp;
180 register const u_char *cp;
181 register int n;
182 const u_char *eom, *erdata;
183 char *bp, **ap, **hap;
184 int type, class, buflen, ancount, qdcount;
185 int haveanswer, had_error;
186 int toobig = 0;
187 char tbuf[MAXDNAME];
188 const char *tname;
189 int (*name_ok) __P((const char *));
190
191 tname = qname;
192 host.h_name = NULL;
193 eom = answer->buf + anslen;
194 switch (qtype) {
195 case T_A:
196 case T_AAAA:
197 name_ok = res_hnok;
198 break;
199 case T_PTR:
200 name_ok = res_dnok;
201 break;
202 default:
203 return (NULL); /* XXX should be abort(); */
204 }
205 /*
206 * find first satisfactory answer
207 */
208 hp = &answer->hdr;
209 ancount = ntohs(hp->ancount);
210 qdcount = ntohs(hp->qdcount);
211 bp = hostbuf;
212 buflen = sizeof hostbuf;
213 cp = answer->buf;
214 BOUNDED_INCR(HFIXEDSZ);
215 if (qdcount != 1) {
216 h_errno = NO_RECOVERY;
217 return (NULL);
218 }
219 n = dn_expand(answer->buf, eom, cp, bp, buflen);
220 if ((n < 0) || !(*name_ok)(bp)) {
221 h_errno = NO_RECOVERY;
222 return (NULL);
223 }
224 BOUNDED_INCR(n + QFIXEDSZ);
225 if (qtype == T_A || qtype == T_AAAA) {
226 /* res_send() has already verified that the query name is the
227 * same as the one we sent; this just gets the expanded name
228 * (i.e., with the succeeding search-domain tacked on).
229 */
230 n = strlen(bp) + 1; /* for the \0 */
231 if (n >= MAXHOSTNAMELEN) {
232 h_errno = NO_RECOVERY;
233 return (NULL);
234 }
235 host.h_name = bp;
236 bp += n;
237 buflen -= n;
238 /* The qname can be abbreviated, but h_name is now absolute. */
239 qname = host.h_name;
240 }
241 ap = host_aliases;
242 *ap = NULL;
243 host.h_aliases = host_aliases;
244 hap = h_addr_ptrs;
245 *hap = NULL;
246 host.h_addr_list = h_addr_ptrs;
247 haveanswer = 0;
248 had_error = 0;
249 while (ancount-- > 0 && cp < eom && !had_error) {
250 n = dn_expand(answer->buf, eom, cp, bp, buflen);
251 if ((n < 0) || !(*name_ok)(bp)) {
252 had_error++;
253 continue;
254 }
255 cp += n; /* name */
256 BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
257 type = _getshort(cp);
258 cp += INT16SZ; /* type */
259 class = _getshort(cp);
260 cp += INT16SZ + INT32SZ; /* class, TTL */
261 n = _getshort(cp);
262 cp += INT16SZ; /* len */
263 BOUNDS_CHECK(cp, n);
264 erdata = cp + n;
265 if (class != C_IN) {
266 /* XXX - debug? syslog? */
267 cp += n;
268 continue; /* XXX - had_error++ ? */
269 }
270 if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
271 if (ap >= &host_aliases[MAXALIASES-1])
272 continue;
273 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
274 if ((n < 0) || !(*name_ok)(tbuf)) {
275 had_error++;
276 continue;
277 }
278 cp += n;
279 if (cp != erdata) {
280 h_errno = NO_RECOVERY;
281 return (NULL);
282 }
283 /* Store alias. */
284 *ap++ = bp;
285 n = strlen(bp) + 1; /* for the \0 */
286 if (n >= MAXHOSTNAMELEN) {
287 had_error++;
288 continue;
289 }
290 bp += n;
291 buflen -= n;
292 /* Get canonical name. */
293 n = strlen(tbuf) + 1; /* for the \0 */
294 if (n > buflen || n >= MAXHOSTNAMELEN) {
295 had_error++;
296 continue;
297 }
298 strcpy(bp, tbuf);
299 host.h_name = bp;
300 bp += n;
301 buflen -= n;
302 continue;
303 }
304 if (qtype == T_PTR && type == T_CNAME) {
305 n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf);
306 if (n < 0 || !res_dnok(tbuf)) {
307 had_error++;
308 continue;
309 }
310 cp += n;
311 if (cp != erdata) {
312 h_errno = NO_RECOVERY;
313 return (NULL);
314 }
315 /* Get canonical name. */
316 n = strlen(tbuf) + 1; /* for the \0 */
317 if (n > buflen || n >= MAXHOSTNAMELEN) {
318 had_error++;
319 continue;
320 }
321 strcpy(bp, tbuf);
322 tname = bp;
323 bp += n;
324 buflen -= n;
325 continue;
326 }
327 if (type != qtype) {
328 syslog(LOG_NOTICE|LOG_AUTH,
329 "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"",
330 qname, p_class(C_IN), p_type(qtype),
331 p_type(type));
332 cp += n;
333 continue; /* XXX - had_error++ ? */
334 }
335 switch (type) {
336 case T_PTR:
337 if (strcasecmp(tname, bp) != 0) {
338 syslog(LOG_NOTICE|LOG_AUTH,
339 AskedForGot, qname, bp);
340 cp += n;
341 continue; /* XXX - had_error++ ? */
342 }
343 n = dn_expand(answer->buf, eom, cp, bp, buflen);
344 if ((n < 0) || !res_hnok(bp)) {
345 had_error++;
346 break;
347 }
348 #if MULTI_PTRS_ARE_ALIASES
349 cp += n;
350 if (cp != erdata) {
351 h_errno = NO_RECOVERY;
352 return (NULL);
353 }
354 if (!haveanswer)
355 host.h_name = bp;
356 else if (ap < &host_aliases[MAXALIASES-1])
357 *ap++ = bp;
358 else
359 n = -1;
360 if (n != -1) {
361 n = strlen(bp) + 1; /* for the \0 */
362 if (n >= MAXHOSTNAMELEN) {
363 had_error++;
364 break;
365 }
366 bp += n;
367 buflen -= n;
368 }
369 break;
370 #else
371 host.h_name = bp;
372 if (_res.options & RES_USE_INET6) {
373 n = strlen(bp) + 1; /* for the \0 */
374 if (n >= MAXHOSTNAMELEN) {
375 had_error++;
376 break;
377 }
378 bp += n;
379 buflen -= n;
380 map_v4v6_hostent(&host, &bp, &buflen);
381 }
382 h_errno = NETDB_SUCCESS;
383 return (&host);
384 #endif
385 case T_A:
386 case T_AAAA:
387 if (strcasecmp(host.h_name, bp) != 0) {
388 syslog(LOG_NOTICE|LOG_AUTH,
389 AskedForGot, host.h_name, bp);
390 cp += n;
391 continue; /* XXX - had_error++ ? */
392 }
393 if (n != host.h_length) {
394 cp += n;
395 continue;
396 }
397 if (!haveanswer) {
398 register int nn;
399
400 host.h_name = bp;
401 nn = strlen(bp) + 1; /* for the \0 */
402 bp += nn;
403 buflen -= nn;
404 }
405
406 bp += sizeof(align) - ((u_long)bp % sizeof(align));
407
408 if (bp + n >= &hostbuf[sizeof hostbuf]) {
409 dprintf("size (%d) too big\n", n);
410 had_error++;
411 continue;
412 }
413 if (hap >= &h_addr_ptrs[MAXADDRS-1]) {
414 if (!toobig++)
415 dprintf("Too many addresses (%d)\n",
416 MAXADDRS);
417 cp += n;
418 continue;
419 }
420 bcopy(cp, *hap++ = bp, n);
421 bp += n;
422 buflen -= n;
423 cp += n;
424 if (cp != erdata) {
425 h_errno = NO_RECOVERY;
426 return (NULL);
427 }
428 break;
429 default:
430 abort();
431 }
432 if (!had_error)
433 haveanswer++;
434 }
435 if (haveanswer) {
436 *ap = NULL;
437 *hap = NULL;
438 # if defined(RESOLVSORT)
439 /*
440 * Note: we sort even if host can take only one address
441 * in its return structures - should give it the "best"
442 * address in that case, not some random one
443 */
444 if (_res.nsort && haveanswer > 1 && qtype == T_A)
445 addrsort(h_addr_ptrs, haveanswer);
446 # endif /*RESOLVSORT*/
447 if (!host.h_name) {
448 n = strlen(qname) + 1; /* for the \0 */
449 if (n > buflen || n >= MAXHOSTNAMELEN)
450 goto no_recovery;
451 strcpy(bp, qname);
452 host.h_name = bp;
453 bp += n;
454 buflen -= n;
455 }
456 if (_res.options & RES_USE_INET6)
457 map_v4v6_hostent(&host, &bp, &buflen);
458 h_errno = NETDB_SUCCESS;
459 return (&host);
460 }
461 no_recovery:
462 h_errno = NO_RECOVERY;
463 return (NULL);
464 }
465
466 struct hostent *
467 gethostbyname(name)
468 const char *name;
469 {
470 struct hostent *hp;
471
472 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
473 h_errno = NETDB_INTERNAL;
474 return (NULL);
475 }
476 if (_res.options & RES_USE_INET6) {
477 hp = gethostbyname2(name, AF_INET6);
478 if (hp)
479 return (hp);
480 }
481 return (gethostbyname2(name, AF_INET));
482 }
483
484 struct hostent *
485 gethostbyname2(name, af)
486 const char *name;
487 int af;
488 {
489 querybuf buf;
490 register const char *cp;
491 char *bp;
492 int n, size, type, len;
493 extern struct hostent *_gethtbyname2();
494
495 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
496 h_errno = NETDB_INTERNAL;
497 return (NULL);
498 }
499
500 switch (af) {
501 case AF_INET:
502 size = INADDRSZ;
503 type = T_A;
504 break;
505 case AF_INET6:
506 size = IN6ADDRSZ;
507 type = T_AAAA;
508 break;
509 default:
510 h_errno = NETDB_INTERNAL;
511 errno = EAFNOSUPPORT;
512 return (NULL);
513 }
514
515 host.h_addrtype = af;
516 host.h_length = size;
517
518 /*
519 * if there aren't any dots, it could be a user-level alias.
520 * this is also done in res_query() since we are not the only
521 * function that looks up host names.
522 */
523 if (!strchr(name, '.') && (cp = __hostalias(name)))
524 name = cp;
525
526 /*
527 * disallow names consisting only of digits/dots, unless
528 * they end in a dot.
529 */
530 if (isdigit(name[0]))
531 for (cp = name;; ++cp) {
532 if (!*cp) {
533 if (*--cp == '.')
534 break;
535 /*
536 * All-numeric, no dot at the end.
537 * Fake up a hostent as if we'd actually
538 * done a lookup.
539 */
540 if (inet_pton(af, name, host_addr) <= 0) {
541 h_errno = HOST_NOT_FOUND;
542 return (NULL);
543 }
544 strncpy(hostbuf, name, MAXDNAME);
545 hostbuf[MAXDNAME] = '\0';
546 bp = hostbuf + MAXDNAME;
547 len = sizeof hostbuf - MAXDNAME;
548 host.h_name = hostbuf;
549 host.h_aliases = host_aliases;
550 host_aliases[0] = NULL;
551 h_addr_ptrs[0] = (char *)host_addr;
552 h_addr_ptrs[1] = NULL;
553 host.h_addr_list = h_addr_ptrs;
554 if (_res.options & RES_USE_INET6)
555 map_v4v6_hostent(&host, &bp, &len);
556 h_errno = NETDB_SUCCESS;
557 return (&host);
558 }
559 if (!isdigit(*cp) && *cp != '.')
560 break;
561 }
562 if ((isxdigit(name[0]) && strchr(name, ':') != NULL) ||
563 name[0] == ':')
564 for (cp = name;; ++cp) {
565 if (!*cp) {
566 if (*--cp == '.')
567 break;
568 /*
569 * All-IPv6-legal, no dot at the end.
570 * Fake up a hostent as if we'd actually
571 * done a lookup.
572 */
573 if (inet_pton(af, name, host_addr) <= 0) {
574 h_errno = HOST_NOT_FOUND;
575 return (NULL);
576 }
577 strncpy(hostbuf, name, MAXDNAME);
578 hostbuf[MAXDNAME] = '\0';
579 bp = hostbuf + MAXDNAME;
580 len = sizeof hostbuf - MAXDNAME;
581 host.h_name = hostbuf;
582 host.h_aliases = host_aliases;
583 host_aliases[0] = NULL;
584 h_addr_ptrs[0] = (char *)host_addr;
585 h_addr_ptrs[1] = NULL;
586 host.h_addr_list = h_addr_ptrs;
587 h_errno = NETDB_SUCCESS;
588 return (&host);
589 }
590 if (!isxdigit(*cp) && *cp != ':' && *cp != '.')
591 break;
592 }
593
594 if ((n = res_search(name, C_IN, type, buf.buf, sizeof(buf))) < 0) {
595 dprintf("res_search failed (%d)\n", n);
596 if (errno == ECONNREFUSED)
597 return (_gethtbyname2(name, af));
598 return (NULL);
599 }
600 return (getanswer(&buf, n, name, type));
601 }
602
603 struct hostent *
604 gethostbyaddr(addr, len, af)
605 const char *addr; /* XXX should have been def'd as u_char! */
606 int len, af;
607 {
608 const u_char *uaddr = (const u_char *)addr;
609 static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff };
610 static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 };
611 int n, size;
612 querybuf buf;
613 register struct hostent *hp;
614 char qbuf[MAXDNAME+1], *qp;
615 #ifdef SUNSECURITY
616 register struct hostent *rhp;
617 char **haddr;
618 u_long old_options;
619 char hname2[MAXDNAME+1];
620 #endif /*SUNSECURITY*/
621 extern struct hostent *_gethtbyaddr();
622
623 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
624 h_errno = NETDB_INTERNAL;
625 return (NULL);
626 }
627 if (af == AF_INET6 && len == IN6ADDRSZ &&
628 (!bcmp(uaddr, mapped, sizeof mapped) ||
629 !bcmp(uaddr, tunnelled, sizeof tunnelled))) {
630 /* Unmap. */
631 addr += sizeof mapped;
632 uaddr += sizeof mapped;
633 af = AF_INET;
634 len = INADDRSZ;
635 }
636 switch (af) {
637 case AF_INET:
638 size = INADDRSZ;
639 break;
640 case AF_INET6:
641 size = IN6ADDRSZ;
642 break;
643 default:
644 errno = EAFNOSUPPORT;
645 h_errno = NETDB_INTERNAL;
646 return (NULL);
647 }
648 if (size != len) {
649 errno = EINVAL;
650 h_errno = NETDB_INTERNAL;
651 return (NULL);
652 }
653 switch (af) {
654 case AF_INET:
655 (void) sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
656 (uaddr[3] & 0xff),
657 (uaddr[2] & 0xff),
658 (uaddr[1] & 0xff),
659 (uaddr[0] & 0xff));
660 break;
661 case AF_INET6:
662 qp = qbuf;
663 for (n = IN6ADDRSZ - 1; n >= 0; n--) {
664 qp += SPRINTF((qp, "%x.%x.",
665 uaddr[n] & 0xf,
666 (uaddr[n] >> 4) & 0xf));
667 }
668 strcpy(qp, "ip6.int");
669 break;
670 default:
671 abort();
672 }
673 n = res_query(qbuf, C_IN, T_PTR, (u_char *)buf.buf, sizeof buf.buf);
674 if (n < 0) {
675 dprintf("res_query failed (%d)\n", n);
676 if (errno == ECONNREFUSED)
677 return (_gethtbyaddr(addr, len, af));
678 return (NULL);
679 }
680 if (!(hp = getanswer(&buf, n, qbuf, T_PTR)))
681 return (NULL); /* h_errno was set by getanswer() */
682 #ifdef SUNSECURITY
683 if (af == AF_INET) {
684 /*
685 * turn off search as the name should be absolute,
686 * 'localhost' should be matched by defnames
687 */
688 strncpy(hname2, hp->h_name, MAXDNAME);
689 hname2[MAXDNAME] = '\0';
690 old_options = _res.options;
691 _res.options &= ~RES_DNSRCH;
692 _res.options |= RES_DEFNAMES;
693 if (!(rhp = gethostbyname(hname2))) {
694 syslog(LOG_NOTICE|LOG_AUTH,
695 "gethostbyaddr: No A record for %s (verifying [%s])",
696 hname2, inet_ntoa(*((struct in_addr *)addr)));
697 _res.options = old_options;
698 h_errno = HOST_NOT_FOUND;
699 return (NULL);
700 }
701 _res.options = old_options;
702 for (haddr = rhp->h_addr_list; *haddr; haddr++)
703 if (!memcmp(*haddr, addr, INADDRSZ))
704 break;
705 if (!*haddr) {
706 syslog(LOG_NOTICE|LOG_AUTH,
707 "gethostbyaddr: A record of %s != PTR record [%s]",
708 hname2, inet_ntoa(*((struct in_addr *)addr)));
709 h_errno = HOST_NOT_FOUND;
710 return (NULL);
711 }
712 }
713 #endif /*SUNSECURITY*/
714 hp->h_addrtype = af;
715 hp->h_length = len;
716 bcopy(addr, host_addr, len);
717 h_addr_ptrs[0] = (char *)host_addr;
718 h_addr_ptrs[1] = NULL;
719 if (af == AF_INET && (_res.options & RES_USE_INET6)) {
720 map_v4v6_address((char*)host_addr, (char*)host_addr);
721 hp->h_addrtype = AF_INET6;
722 hp->h_length = IN6ADDRSZ;
723 }
724 h_errno = NETDB_SUCCESS;
725 return (hp);
726 }
727
728 void
729 _sethtent(f)
730 int f;
731 {
732 if (!hostf)
733 hostf = fopen(_PATH_HOSTS, "r" );
734 else
735 rewind(hostf);
736 stayopen = f;
737 }
738
739 void
740 _endhtent()
741 {
742 if (hostf && !stayopen) {
743 (void) fclose(hostf);
744 hostf = NULL;
745 }
746 }
747
748 struct hostent *
749 _gethtent()
750 {
751 char *p;
752 register char *cp, **q;
753 int af, len;
754
755 if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) {
756 h_errno = NETDB_INTERNAL;
757 return (NULL);
758 }
759 again:
760 if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) {
761 h_errno = HOST_NOT_FOUND;
762 return (NULL);
763 }
764 if (*p == '#')
765 goto again;
766 if (!(cp = strpbrk(p, "#\n")))
767 goto again;
768 *cp = '\0';
769 if (!(cp = strpbrk(p, " \t")))
770 goto again;
771 *cp++ = '\0';
772 if (inet_pton(AF_INET6, p, host_addr) > 0) {
773 af = AF_INET6;
774 len = IN6ADDRSZ;
775 } else if (inet_pton(AF_INET, p, host_addr) > 0) {
776 if (_res.options & RES_USE_INET6) {
777 map_v4v6_address((char*)host_addr, (char*)host_addr);
778 af = AF_INET6;
779 len = IN6ADDRSZ;
780 } else {
781 af = AF_INET;
782 len = INADDRSZ;
783 }
784 } else {
785 goto again;
786 }
787 h_addr_ptrs[0] = (char *)host_addr;
788 h_addr_ptrs[1] = NULL;
789 host.h_addr_list = h_addr_ptrs;
790 host.h_length = len;
791 host.h_addrtype = af;
792 while (*cp == ' ' || *cp == '\t')
793 cp++;
794 host.h_name = cp;
795 q = host.h_aliases = host_aliases;
796 if (cp = strpbrk(cp, " \t"))
797 *cp++ = '\0';
798 while (cp && *cp) {
799 if (*cp == ' ' || *cp == '\t') {
800 cp++;
801 continue;
802 }
803 if (q < &host_aliases[MAXALIASES - 1])
804 *q++ = cp;
805 if (cp = strpbrk(cp, " \t"))
806 *cp++ = '\0';
807 }
808 *q = NULL;
809 h_errno = NETDB_SUCCESS;
810 return (&host);
811 }
812
813 struct hostent *
814 _gethtbyname(name)
815 const char *name;
816 {
817 extern struct hostent *_gethtbyname2();
818 struct hostent *hp;
819
820 if (_res.options & RES_USE_INET6) {
821 hp = _gethtbyname2(name, AF_INET6);
822 if (hp)
823 return (hp);
824 }
825 return (_gethtbyname2(name, AF_INET));
826 }
827
828 struct hostent *
829 _gethtbyname2(name, af)
830 const char *name;
831 int af;
832 {
833 register struct hostent *p;
834 register char **cp;
835
836 _sethtent(0);
837 while (p = _gethtent()) {
838 if (p->h_addrtype != af)
839 continue;
840 if (strcasecmp(p->h_name, name) == 0)
841 break;
842 for (cp = p->h_aliases; *cp != 0; cp++)
843 if (strcasecmp(*cp, name) == 0)
844 goto found;
845 }
846 found:
847 _endhtent();
848 return (p);
849 }
850
851 struct hostent *
852 _gethtbyaddr(addr, len, af)
853 const char *addr;
854 int len, af;
855 {
856 register struct hostent *p;
857
858 _sethtent(0);
859 while (p = _gethtent())
860 if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len))
861 break;
862 _endhtent();
863 return (p);
864 }
865
866 static void
867 map_v4v6_address(src, dst)
868 const char *src;
869 char *dst;
870 {
871 u_char *p = (u_char *)dst;
872 char tmp[INADDRSZ];
873 int i;
874
875 /* Stash a temporary copy so our caller can update in place. */
876 bcopy(src, tmp, INADDRSZ);
877 /* Mark this ipv6 addr as a mapped ipv4. */
878 for (i = 0; i < 10; i++)
879 *p++ = 0x00;
880 *p++ = 0xff;
881 *p++ = 0xff;
882 /* Retrieve the saved copy and we're done. */
883 bcopy(tmp, (void*)p, INADDRSZ);
884 }
885
886 static void
887 map_v4v6_hostent(hp, bpp, lenp)
888 struct hostent *hp;
889 char **bpp;
890 int *lenp;
891 {
892 char **ap;
893
894 if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
895 return;
896 hp->h_addrtype = AF_INET6;
897 hp->h_length = IN6ADDRSZ;
898 for (ap = hp->h_addr_list; *ap; ap++) {
899 int i = sizeof(align) - ((u_long)*bpp % sizeof(align));
900
901 if (*lenp < (i + IN6ADDRSZ)) {
902 /* Out of memory. Truncate address list here. XXX */
903 *ap = NULL;
904 return;
905 }
906 *bpp += i;
907 *lenp -= i;
908 map_v4v6_address(*ap, *bpp);
909 *ap = *bpp;
910 *bpp += IN6ADDRSZ;
911 *lenp -= IN6ADDRSZ;
912 }
913 }
914
915 #ifdef RESOLVSORT
916 static void
917 addrsort(ap, num)
918 char **ap;
919 int num;
920 {
921 int i, j;
922 char **p;
923 short aval[MAXADDRS];
924 int needsort = 0;
925
926 p = ap;
927 for (i = 0; i < num; i++, p++) {
928 for (j = 0 ; (unsigned)j < _res.nsort; j++)
929 if (_res.sort_list[j].addr.s_addr ==
930 (((struct in_addr *)(*p))->s_addr & _res.sort_list[j].mask))
931 break;
932 aval[i] = j;
933 if (needsort == 0 && i > 0 && j < aval[i-1])
934 needsort = i;
935 }
936 if (!needsort)
937 return;
938
939 while (needsort < num) {
940 for (j = needsort - 1; j >= 0; j--) {
941 if (aval[j] > aval[j+1]) {
942 char *hp;
943
944 i = aval[j];
945 aval[j] = aval[j+1];
946 aval[j+1] = i;
947
948 hp = ap[j];
949 ap[j] = ap[j+1];
950 ap[j+1] = hp;
951
952 } else
953 break;
954 }
955 needsort++;
956 }
957 }
958 #endif
959
960 #if defined(BSD43_BSD43_NFS) || defined(sun)
961 /* some libc's out there are bound internally to these names (UMIPS) */
962 void
963 ht_sethostent(stayopen)
964 int stayopen;
965 {
966 _sethtent(stayopen);
967 }
968
969 void
970 ht_endhostent()
971 {
972 _endhtent();
973 }
974
975 struct hostent *
976 ht_gethostbyname(name)
977 char *name;
978 {
979 return (_gethtbyname(name));
980 }
981
982 struct hostent *
983 ht_gethostbyaddr(addr, len, af)
984 const char *addr;
985 int len, af;
986 {
987 return (_gethtbyaddr(addr, len, af));
988 }
989
990 struct hostent *
991 gethostent()
992 {
993 return (_gethtent());
994 }
995
996 void
997 dns_service()
998 {
999 return;
1000 }
1001
1002 #undef dn_skipname
1003 dn_skipname(comp_dn, eom)
1004 const u_char *comp_dn, *eom;
1005 {
1006 return (__dn_skipname(comp_dn, eom));
1007 }
1008 #endif /*old-style libc with yp junk in it*/
1009