1 | /*- |
---|
2 | * Copyright (c) 1994, Garrett Wollman |
---|
3 | * |
---|
4 | * Redistribution and use in source and binary forms, with or without |
---|
5 | * modification, are permitted provided that the following conditions |
---|
6 | * are met: |
---|
7 | * 1. Redistributions of source code must retain the above copyright |
---|
8 | * notice, this list of conditions and the following disclaimer. |
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer in the |
---|
11 | * documentation and/or other materials provided with the distribution. |
---|
12 | * |
---|
13 | * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND |
---|
14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
23 | * SUCH DAMAGE. |
---|
24 | */ |
---|
25 | |
---|
26 | #if HAVE_CONFIG_H |
---|
27 | #include "config.h" |
---|
28 | #endif |
---|
29 | |
---|
30 | #include <sys/param.h> |
---|
31 | #include <sys/socket.h> |
---|
32 | #include <netinet/in.h> |
---|
33 | #include <arpa/inet.h> |
---|
34 | #include <netdb.h> |
---|
35 | #include <stdio.h> |
---|
36 | #include <stdlib.h> |
---|
37 | #include <ctype.h> |
---|
38 | #include <errno.h> |
---|
39 | #include <string.h> |
---|
40 | #ifdef YP |
---|
41 | #include <rpc/rpc.h> |
---|
42 | #include <rpcsvc/yp_prot.h> |
---|
43 | #include <rpcsvc/ypclnt.h> |
---|
44 | #endif |
---|
45 | |
---|
46 | #define MAXALIASES 35 |
---|
47 | #define MAXADDRS 35 |
---|
48 | |
---|
49 | #ifdef YP |
---|
50 | static char *host_aliases[MAXALIASES]; |
---|
51 | static char hostaddr[MAXADDRS]; |
---|
52 | static char *host_addrs[2]; |
---|
53 | #endif /* YP */ |
---|
54 | |
---|
55 | static struct hostent * |
---|
56 | _gethostbynis( |
---|
57 | const char *name, |
---|
58 | char *map, |
---|
59 | int af) |
---|
60 | { |
---|
61 | #ifdef YP |
---|
62 | register char *cp, **q; |
---|
63 | char *result; |
---|
64 | int resultlen; |
---|
65 | static struct hostent h; |
---|
66 | static char *domain = (char *)NULL; |
---|
67 | static char ypbuf[YPMAXRECORD + 2]; |
---|
68 | |
---|
69 | switch(af) { |
---|
70 | case AF_INET: |
---|
71 | break; |
---|
72 | default: |
---|
73 | case AF_INET6: |
---|
74 | errno = EAFNOSUPPORT; |
---|
75 | return NULL; |
---|
76 | } |
---|
77 | |
---|
78 | if (domain == (char *)NULL) |
---|
79 | if (yp_get_default_domain (&domain)) |
---|
80 | return ((struct hostent *)NULL); |
---|
81 | |
---|
82 | if (yp_match(domain, map, name, strlen(name), &result, &resultlen)) |
---|
83 | return ((struct hostent *)NULL); |
---|
84 | |
---|
85 | /* avoid potential memory leak */ |
---|
86 | bcopy((char *)result, (char *)&ypbuf, resultlen); |
---|
87 | ypbuf[resultlen] = '\0'; |
---|
88 | free(result); |
---|
89 | result = (char *)&ypbuf; |
---|
90 | |
---|
91 | if ((cp = index(result, '\n'))) |
---|
92 | *cp = '\0'; |
---|
93 | |
---|
94 | cp = strpbrk(result, " \t"); |
---|
95 | *cp++ = '\0'; |
---|
96 | h.h_addr_list = host_addrs; |
---|
97 | h.h_addr = hostaddr; |
---|
98 | *((u_long *)h.h_addr) = inet_addr(result); |
---|
99 | h.h_length = sizeof(u_long); |
---|
100 | h.h_addrtype = AF_INET; |
---|
101 | while (*cp == ' ' || *cp == '\t') |
---|
102 | cp++; |
---|
103 | h.h_name = cp; |
---|
104 | q = h.h_aliases = host_aliases; |
---|
105 | cp = strpbrk(cp, " \t"); |
---|
106 | if (cp != NULL) |
---|
107 | *cp++ = '\0'; |
---|
108 | while (cp && *cp) { |
---|
109 | if (*cp == ' ' || *cp == '\t') { |
---|
110 | cp++; |
---|
111 | continue; |
---|
112 | } |
---|
113 | if (q < &host_aliases[MAXALIASES - 1]) |
---|
114 | *q++ = cp; |
---|
115 | cp = strpbrk(cp, " \t"); |
---|
116 | if (cp != NULL) |
---|
117 | *cp++ = '\0'; |
---|
118 | } |
---|
119 | *q = NULL; |
---|
120 | return (&h); |
---|
121 | #else |
---|
122 | return (NULL); |
---|
123 | #endif /* YP */ |
---|
124 | } |
---|
125 | |
---|
126 | struct hostent * |
---|
127 | _gethostbynisname( |
---|
128 | const char *name, |
---|
129 | int af) |
---|
130 | { |
---|
131 | return _gethostbynis(name, "hosts.byname", af); |
---|
132 | } |
---|
133 | |
---|
134 | struct hostent * |
---|
135 | _gethostbynisaddr( |
---|
136 | const char *addr, |
---|
137 | int len, |
---|
138 | int af) |
---|
139 | { |
---|
140 | return _gethostbynis(inet_ntoa(*(struct in_addr *)addr),"hosts.byaddr", af); |
---|
141 | } |
---|