1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 1994, Garrett Wollman |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND |
---|
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
25 | * SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #include <sys/cdefs.h> |
---|
29 | __FBSDID("$FreeBSD$"); |
---|
30 | |
---|
31 | #include <rtems/bsd/sys/param.h> |
---|
32 | #include <sys/socket.h> |
---|
33 | #include <netinet/in.h> |
---|
34 | #include <arpa/inet.h> |
---|
35 | #include <arpa/nameser.h> |
---|
36 | #include <netdb.h> |
---|
37 | #include <stdio.h> |
---|
38 | #include <stdlib.h> |
---|
39 | #include <ctype.h> |
---|
40 | #include <errno.h> |
---|
41 | #include <string.h> |
---|
42 | #include <stdarg.h> |
---|
43 | #include <nsswitch.h> |
---|
44 | #include <resolv.h> /* XXX */ |
---|
45 | #ifdef YP |
---|
46 | #include <rpc/rpc.h> |
---|
47 | #include <rpcsvc/yp_prot.h> |
---|
48 | #include <rpcsvc/ypclnt.h> |
---|
49 | #endif |
---|
50 | #include "netdb_private.h" |
---|
51 | |
---|
52 | #ifdef YP |
---|
53 | static int |
---|
54 | _gethostbynis(const char *name, char *map, int af, struct hostent *he, |
---|
55 | struct hostent_data *hed) |
---|
56 | { |
---|
57 | char *p, *bp, *ep; |
---|
58 | char *cp, **q; |
---|
59 | char *result; |
---|
60 | int resultlen, size, addrok = 0; |
---|
61 | char ypbuf[YPMAXRECORD + 2]; |
---|
62 | res_state statp; |
---|
63 | |
---|
64 | statp = __res_state(); |
---|
65 | switch(af) { |
---|
66 | case AF_INET: |
---|
67 | size = NS_INADDRSZ; |
---|
68 | break; |
---|
69 | case AF_INET6: |
---|
70 | size = NS_IN6ADDRSZ; |
---|
71 | break; |
---|
72 | default: |
---|
73 | errno = EAFNOSUPPORT; |
---|
74 | RES_SET_H_ERRNO(statp, NETDB_INTERNAL); |
---|
75 | return (-1); |
---|
76 | } |
---|
77 | |
---|
78 | if (hed->yp_domain == (char *)NULL) |
---|
79 | if (yp_get_default_domain (&hed->yp_domain)) { |
---|
80 | RES_SET_H_ERRNO(statp, NETDB_INTERNAL); |
---|
81 | return (-1); |
---|
82 | } |
---|
83 | |
---|
84 | if (yp_match(hed->yp_domain, map, name, strlen(name), &result, |
---|
85 | &resultlen)) { |
---|
86 | RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); |
---|
87 | return (-1); |
---|
88 | } |
---|
89 | |
---|
90 | /* avoid potential memory leak */ |
---|
91 | bcopy((char *)result, (char *)&ypbuf, resultlen); |
---|
92 | ypbuf[resultlen] = '\0'; |
---|
93 | free(result); |
---|
94 | result = (char *)&ypbuf; |
---|
95 | |
---|
96 | if ((cp = index(result, '\n'))) |
---|
97 | *cp = '\0'; |
---|
98 | |
---|
99 | cp = strpbrk(result, " \t"); |
---|
100 | *cp++ = '\0'; |
---|
101 | he->h_addr_list = hed->h_addr_ptrs; |
---|
102 | he->h_addr = (char *)hed->host_addr; |
---|
103 | switch (af) { |
---|
104 | case AF_INET: |
---|
105 | addrok = inet_aton(result, (struct in_addr *)hed->host_addr); |
---|
106 | if (addrok != 1) |
---|
107 | break; |
---|
108 | if (statp->options & RES_USE_INET6) { |
---|
109 | _map_v4v6_address((char *)hed->host_addr, |
---|
110 | (char *)hed->host_addr); |
---|
111 | af = AF_INET6; |
---|
112 | size = NS_IN6ADDRSZ; |
---|
113 | } |
---|
114 | break; |
---|
115 | case AF_INET6: |
---|
116 | addrok = inet_pton(af, result, hed->host_addr); |
---|
117 | break; |
---|
118 | } |
---|
119 | if (addrok != 1) { |
---|
120 | RES_SET_H_ERRNO(statp, HOST_NOT_FOUND); |
---|
121 | return (-1); |
---|
122 | } |
---|
123 | he->h_addr_list[1] = NULL; |
---|
124 | he->h_length = size; |
---|
125 | he->h_addrtype = af; |
---|
126 | while (*cp == ' ' || *cp == '\t') |
---|
127 | cp++; |
---|
128 | bp = hed->hostbuf; |
---|
129 | ep = hed->hostbuf + sizeof hed->hostbuf; |
---|
130 | he->h_name = bp; |
---|
131 | q = he->h_aliases = hed->host_aliases; |
---|
132 | p = strpbrk(cp, " \t"); |
---|
133 | if (p != NULL) |
---|
134 | *p++ = '\0'; |
---|
135 | size = strlen(cp) + 1; |
---|
136 | if (ep - bp < size) { |
---|
137 | RES_SET_H_ERRNO(statp, NO_RECOVERY); |
---|
138 | return (-1); |
---|
139 | } |
---|
140 | strlcpy(bp, cp, ep - bp); |
---|
141 | bp += size; |
---|
142 | cp = p; |
---|
143 | while (cp && *cp) { |
---|
144 | if (*cp == ' ' || *cp == '\t') { |
---|
145 | cp++; |
---|
146 | continue; |
---|
147 | } |
---|
148 | if (q >= &hed->host_aliases[_MAXALIASES - 1]) |
---|
149 | break; |
---|
150 | p = strpbrk(cp, " \t"); |
---|
151 | if (p != NULL) |
---|
152 | *p++ = '\0'; |
---|
153 | size = strlen(cp) + 1; |
---|
154 | if (ep - bp < size) |
---|
155 | break; |
---|
156 | strlcpy(bp, cp, ep - bp); |
---|
157 | *q++ = bp; |
---|
158 | bp += size; |
---|
159 | cp = p; |
---|
160 | } |
---|
161 | *q = NULL; |
---|
162 | return (0); |
---|
163 | } |
---|
164 | |
---|
165 | static int |
---|
166 | _gethostbynisname_r(const char *name, int af, struct hostent *he, |
---|
167 | struct hostent_data *hed) |
---|
168 | { |
---|
169 | char *map; |
---|
170 | |
---|
171 | switch (af) { |
---|
172 | case AF_INET: |
---|
173 | map = "hosts.byname"; |
---|
174 | break; |
---|
175 | default: |
---|
176 | map = "ipnodes.byname"; |
---|
177 | break; |
---|
178 | } |
---|
179 | return (_gethostbynis(name, map, af, he, hed)); |
---|
180 | } |
---|
181 | |
---|
182 | static int |
---|
183 | _gethostbynisaddr_r(const void *addr, socklen_t len, int af, |
---|
184 | struct hostent *he, struct hostent_data *hed) |
---|
185 | { |
---|
186 | char *map; |
---|
187 | char numaddr[46]; |
---|
188 | |
---|
189 | switch (af) { |
---|
190 | case AF_INET: |
---|
191 | map = "hosts.byaddr"; |
---|
192 | break; |
---|
193 | default: |
---|
194 | map = "ipnodes.byaddr"; |
---|
195 | break; |
---|
196 | } |
---|
197 | if (inet_ntop(af, addr, numaddr, sizeof(numaddr)) == NULL) |
---|
198 | return (-1); |
---|
199 | return (_gethostbynis(numaddr, map, af, he, hed)); |
---|
200 | } |
---|
201 | #endif /* YP */ |
---|
202 | |
---|
203 | /* XXX _gethostbynisname/_gethostbynisaddr only used by getipnodeby*() */ |
---|
204 | struct hostent * |
---|
205 | _gethostbynisname(const char *name, int af) |
---|
206 | { |
---|
207 | #ifdef YP |
---|
208 | struct hostent *he; |
---|
209 | struct hostent_data *hed; |
---|
210 | u_long oresopt; |
---|
211 | int error; |
---|
212 | res_state statp; |
---|
213 | |
---|
214 | statp = __res_state(); |
---|
215 | if ((he = __hostent_init()) == NULL || |
---|
216 | (hed = __hostent_data_init()) == NULL) { |
---|
217 | RES_SET_H_ERRNO(statp, NETDB_INTERNAL); |
---|
218 | return (NULL); |
---|
219 | } |
---|
220 | |
---|
221 | oresopt = statp->options; |
---|
222 | statp->options &= ~RES_USE_INET6; |
---|
223 | error = _gethostbynisname_r(name, af, he, hed); |
---|
224 | statp->options = oresopt; |
---|
225 | return (error == 0) ? he : NULL; |
---|
226 | #else |
---|
227 | return (NULL); |
---|
228 | #endif |
---|
229 | } |
---|
230 | |
---|
231 | struct hostent * |
---|
232 | _gethostbynisaddr(const void *addr, socklen_t len, int af) |
---|
233 | { |
---|
234 | #ifdef YP |
---|
235 | struct hostent *he; |
---|
236 | struct hostent_data *hed; |
---|
237 | u_long oresopt; |
---|
238 | int error; |
---|
239 | res_state statp; |
---|
240 | |
---|
241 | statp = __res_state(); |
---|
242 | if ((he = __hostent_init()) == NULL || |
---|
243 | (hed = __hostent_data_init()) == NULL) { |
---|
244 | RES_SET_H_ERRNO(statp, NETDB_INTERNAL); |
---|
245 | return (NULL); |
---|
246 | } |
---|
247 | |
---|
248 | oresopt = statp->options; |
---|
249 | statp->options &= ~RES_USE_INET6; |
---|
250 | error = _gethostbynisaddr_r(addr, len, af, he, hed); |
---|
251 | statp->options = oresopt; |
---|
252 | return (error == 0) ? he : NULL; |
---|
253 | #else |
---|
254 | return (NULL); |
---|
255 | #endif |
---|
256 | } |
---|
257 | |
---|
258 | int |
---|
259 | _nis_gethostbyname(void *rval, void *cb_data, va_list ap) |
---|
260 | { |
---|
261 | #ifdef YP |
---|
262 | const char *name; |
---|
263 | int af; |
---|
264 | char *buffer; |
---|
265 | size_t buflen; |
---|
266 | int *errnop, *h_errnop; |
---|
267 | struct hostent *hptr, he; |
---|
268 | struct hostent_data *hed; |
---|
269 | res_state statp; |
---|
270 | |
---|
271 | name = va_arg(ap, const char *); |
---|
272 | af = va_arg(ap, int); |
---|
273 | hptr = va_arg(ap, struct hostent *); |
---|
274 | buffer = va_arg(ap, char *); |
---|
275 | buflen = va_arg(ap, size_t); |
---|
276 | errnop = va_arg(ap, int *); |
---|
277 | h_errnop = va_arg(ap, int *); |
---|
278 | |
---|
279 | *((struct hostent **)rval) = NULL; |
---|
280 | |
---|
281 | statp = __res_state(); |
---|
282 | if ((hed = __hostent_data_init()) == NULL) { |
---|
283 | RES_SET_H_ERRNO(statp, NETDB_INTERNAL); |
---|
284 | *h_errnop = statp->res_h_errno; |
---|
285 | return (NS_NOTFOUND); |
---|
286 | } |
---|
287 | |
---|
288 | if (_gethostbynisname_r(name, af, &he, hed) != 0) { |
---|
289 | *h_errnop = statp->res_h_errno; |
---|
290 | return (NS_NOTFOUND); |
---|
291 | } |
---|
292 | if (__copy_hostent(&he, hptr, buffer, buflen) != 0) { |
---|
293 | *errnop = errno; |
---|
294 | RES_SET_H_ERRNO(statp, NETDB_INTERNAL); |
---|
295 | *h_errnop = statp->res_h_errno; |
---|
296 | return (NS_RETURN); |
---|
297 | } |
---|
298 | *((struct hostent **)rval) = hptr; |
---|
299 | return (NS_SUCCESS); |
---|
300 | #else |
---|
301 | *((struct hostent **)rval) = NULL; |
---|
302 | return (NS_UNAVAIL); |
---|
303 | #endif |
---|
304 | } |
---|
305 | |
---|
306 | int |
---|
307 | _nis_gethostbyaddr(void *rval, void *cb_data, va_list ap) |
---|
308 | { |
---|
309 | #ifdef YP |
---|
310 | const void *addr; |
---|
311 | socklen_t len; |
---|
312 | int af; |
---|
313 | char *buffer; |
---|
314 | size_t buflen; |
---|
315 | int *errnop, *h_errnop; |
---|
316 | struct hostent *hptr, he; |
---|
317 | struct hostent_data *hed; |
---|
318 | res_state statp; |
---|
319 | |
---|
320 | addr = va_arg(ap, const void *); |
---|
321 | len = va_arg(ap, socklen_t); |
---|
322 | af = va_arg(ap, int); |
---|
323 | hptr = va_arg(ap, struct hostent *); |
---|
324 | buffer = va_arg(ap, char *); |
---|
325 | buflen = va_arg(ap, size_t); |
---|
326 | errnop = va_arg(ap, int *); |
---|
327 | h_errnop = va_arg(ap, int *); |
---|
328 | |
---|
329 | *((struct hostent **)rval) = NULL; |
---|
330 | |
---|
331 | statp = __res_state(); |
---|
332 | if ((hed = __hostent_data_init()) == NULL) { |
---|
333 | RES_SET_H_ERRNO(statp, NETDB_INTERNAL); |
---|
334 | *h_errnop = statp->res_h_errno; |
---|
335 | return (NS_NOTFOUND); |
---|
336 | } |
---|
337 | |
---|
338 | if (_gethostbynisaddr_r(addr, len, af, &he, hed) != 0) { |
---|
339 | *h_errnop = statp->res_h_errno; |
---|
340 | return (NS_NOTFOUND); |
---|
341 | } |
---|
342 | if (__copy_hostent(&he, hptr, buffer, buflen) != 0) { |
---|
343 | *errnop = errno; |
---|
344 | RES_SET_H_ERRNO(statp, NETDB_INTERNAL); |
---|
345 | *h_errnop = statp->res_h_errno; |
---|
346 | return (NS_RETURN); |
---|
347 | } |
---|
348 | *((struct hostent **)rval) = hptr; |
---|
349 | return (NS_SUCCESS); |
---|
350 | #else |
---|
351 | *((struct hostent **)rval) = NULL; |
---|
352 | return (NS_UNAVAIL); |
---|
353 | #endif |
---|
354 | } |
---|