]>
Commit | Line | Data |
---|---|---|
34816665 | 1 | /* Copyright (C) 1996,1997,1998,1999,2000,2002 Free Software Foundation, Inc. |
6259ec0d | 2 | This file is part of the GNU C Library. |
1e275b9e | 3 | Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996. |
6259ec0d UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
6259ec0d UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
6259ec0d | 14 | |
41bdb6e2 AJ |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
6259ec0d UD |
19 | |
20 | #include <nss.h> | |
21 | #include <ctype.h> | |
22 | #include <netdb.h> | |
23 | #include <string.h> | |
24 | #include <netinet/in.h> | |
25 | #include <arpa/inet.h> | |
26 | #include <resolv.h> | |
5107cf1d | 27 | #include <bits/libc-lock.h> |
6259ec0d UD |
28 | #include <rpcsvc/yp.h> |
29 | #include <rpcsvc/ypclnt.h> | |
30 | ||
31 | #include "nss-nis.h" | |
32 | ||
33 | /* Get implementation for some internal functions. */ | |
cf29ffbe | 34 | #include <resolv/mapv4v6addr.h> |
6259ec0d UD |
35 | |
36 | #define ENTNAME hostent | |
37 | #define DATABASE "hosts" | |
38 | #define NEED_H_ERRNO | |
39 | ||
1e275b9e UD |
40 | #define EXTRA_ARGS , af, flags |
41 | #define EXTRA_ARGS_DECL , int af, int flags | |
42 | ||
6259ec0d UD |
43 | #define ENTDATA hostent_data |
44 | struct hostent_data | |
45 | { | |
46 | unsigned char host_addr[16]; /* IPv4 or IPv6 address. */ | |
47 | char *h_addr_ptrs[2]; /* Points to that and null terminator. */ | |
48 | }; | |
49 | ||
50 | #define TRAILING_LIST_MEMBER h_aliases | |
51 | #define TRAILING_LIST_SEPARATOR_P isspace | |
cf29ffbe | 52 | #include <nss/nss_files/files-parse.c> |
6259ec0d UD |
53 | LINE_PARSER |
54 | ("#", | |
55 | { | |
56 | char *addr; | |
57 | ||
58 | STRING_FIELD (addr, isspace, 1); | |
59 | ||
60 | /* Parse address. */ | |
1e275b9e UD |
61 | if (af == AF_INET && inet_pton (AF_INET, addr, entdata->host_addr) > 0) |
62 | { | |
63 | if (flags & AI_V4MAPPED) | |
64 | { | |
65 | map_v4v6_address ((char *) entdata->host_addr, | |
66 | (char *) entdata->host_addr); | |
67 | result->h_addrtype = AF_INET6; | |
68 | result->h_length = IN6ADDRSZ; | |
69 | } | |
70 | else | |
71 | { | |
72 | result->h_addrtype = AF_INET; | |
73 | result->h_length = INADDRSZ; | |
74 | } | |
75 | } | |
76 | else if (af == AF_INET6 | |
77 | && inet_pton (AF_INET6, addr, entdata->host_addr) > 0) | |
6259ec0d UD |
78 | { |
79 | result->h_addrtype = AF_INET6; | |
80 | result->h_length = IN6ADDRSZ; | |
81 | } | |
82 | else | |
1e275b9e UD |
83 | /* Illegal address: ignore line. */ |
84 | return 0; | |
6259ec0d UD |
85 | |
86 | /* Store a pointer to the address in the expected form. */ | |
87 | entdata->h_addr_ptrs[0] = entdata->host_addr; | |
88 | entdata->h_addr_ptrs[1] = NULL; | |
89 | result->h_addr_list = entdata->h_addr_ptrs; | |
90 | ||
6259ec0d | 91 | STRING_FIELD (result->h_name, isspace, 1); |
1e275b9e UD |
92 | }) |
93 | ||
6259ec0d UD |
94 | |
95 | __libc_lock_define_initialized (static, lock) | |
96 | ||
97 | static bool_t new_start = 1; | |
98 | static char *oldkey = NULL; | |
99 | static int oldkeylen = 0; | |
100 | ||
101 | enum nss_status | |
51eecc4a | 102 | _nss_nis_sethostent (int stayopen) |
6259ec0d UD |
103 | { |
104 | __libc_lock_lock (lock); | |
105 | ||
106 | new_start = 1; | |
107 | if (oldkey != NULL) | |
108 | { | |
109 | free (oldkey); | |
110 | oldkey = NULL; | |
111 | oldkeylen = 0; | |
112 | } | |
113 | ||
114 | __libc_lock_unlock (lock); | |
115 | ||
116 | return NSS_STATUS_SUCCESS; | |
117 | } | |
118 | ||
119 | enum nss_status | |
120 | _nss_nis_endhostent (void) | |
121 | { | |
122 | __libc_lock_lock (lock); | |
123 | ||
124 | new_start = 1; | |
125 | if (oldkey != NULL) | |
126 | { | |
127 | free (oldkey); | |
128 | oldkey = NULL; | |
129 | oldkeylen = 0; | |
130 | } | |
131 | ||
132 | __libc_lock_unlock (lock); | |
133 | ||
134 | return NSS_STATUS_SUCCESS; | |
135 | } | |
136 | ||
1e275b9e | 137 | /* The calling function always need to get a lock first. */ |
6259ec0d UD |
138 | static enum nss_status |
139 | internal_nis_gethostent_r (struct hostent *host, char *buffer, | |
1e275b9e UD |
140 | size_t buflen, int *errnop, int *h_errnop, |
141 | int af, int flags) | |
6259ec0d UD |
142 | { |
143 | char *domain; | |
144 | char *result; | |
145 | int len, parse_res; | |
146 | char *outkey; | |
147 | int keylen; | |
148 | struct parser_data *data = (void *) buffer; | |
149 | size_t linebuflen = buffer + buflen - data->linebuffer; | |
150 | ||
151 | if (yp_get_default_domain (&domain)) | |
152 | return NSS_STATUS_UNAVAIL; | |
153 | ||
154 | if (buflen < sizeof *data + 1) | |
155 | { | |
d71b808a | 156 | *errnop = ERANGE; |
6259ec0d UD |
157 | *h_errnop = NETDB_INTERNAL; |
158 | return NSS_STATUS_TRYAGAIN; | |
159 | } | |
160 | ||
161 | /* Get the next entry until we found a correct one. */ | |
162 | do | |
163 | { | |
164 | enum nss_status retval; | |
165 | char *p; | |
166 | ||
167 | if (new_start) | |
168 | retval = yperr2nss (yp_first (domain, "hosts.byname", | |
169 | &outkey, &keylen, &result, &len)); | |
170 | else | |
171 | retval = yperr2nss ( yp_next (domain, "hosts.byname", | |
172 | oldkey, oldkeylen, | |
173 | &outkey, &keylen, &result, &len)); | |
174 | ||
175 | if (retval != NSS_STATUS_SUCCESS) | |
176 | { | |
177 | switch (retval) | |
178 | { | |
179 | case NSS_STATUS_TRYAGAIN: | |
d71b808a | 180 | *errnop = errno; |
6259ec0d UD |
181 | *h_errnop = TRY_AGAIN; |
182 | break; | |
183 | case NSS_STATUS_NOTFOUND: | |
184 | *h_errnop = HOST_NOT_FOUND; | |
185 | break; | |
186 | default: | |
187 | *h_errnop = NO_RECOVERY; | |
188 | break; | |
189 | } | |
190 | return retval; | |
191 | } | |
192 | ||
f8b87ef0 | 193 | if ((size_t) (len + 1) > linebuflen) |
6259ec0d UD |
194 | { |
195 | free (result); | |
196 | *h_errnop = NETDB_INTERNAL; | |
d71b808a | 197 | *errnop = ERANGE; |
6259ec0d UD |
198 | return NSS_STATUS_TRYAGAIN; |
199 | } | |
200 | ||
201 | p = strncpy (data->linebuffer, result, len); | |
202 | data->linebuffer[len] = '\0'; | |
203 | while (isspace (*p)) | |
204 | ++p; | |
205 | free (result); | |
206 | ||
1e275b9e | 207 | parse_res = parse_line (p, host, data, buflen, errnop, af, flags); |
b9b49b44 | 208 | if (parse_res == -1) |
6259ec0d | 209 | { |
60c96635 | 210 | free (outkey); |
d71b808a UD |
211 | *h_errnop = NETDB_INTERNAL; |
212 | *errnop = ERANGE; | |
6259ec0d UD |
213 | return NSS_STATUS_TRYAGAIN; |
214 | } | |
215 | free (oldkey); | |
216 | oldkey = outkey; | |
217 | oldkeylen = keylen; | |
218 | new_start = 0; | |
219 | } | |
220 | while (!parse_res); | |
221 | ||
222 | *h_errnop = NETDB_SUCCESS; | |
223 | return NSS_STATUS_SUCCESS; | |
224 | } | |
225 | ||
1e275b9e | 226 | enum nss_status |
6259ec0d | 227 | _nss_nis_gethostent_r (struct hostent *host, char *buffer, size_t buflen, |
d71b808a | 228 | int *errnop, int *h_errnop) |
6259ec0d | 229 | { |
1e275b9e | 230 | enum nss_status status; |
6259ec0d UD |
231 | |
232 | __libc_lock_lock (lock); | |
233 | ||
1e275b9e UD |
234 | status = internal_nis_gethostent_r (host, buffer, buflen, errnop, h_errnop, |
235 | ((_res.options & RES_USE_INET6) ? AF_INET6 : AF_INET), | |
236 | ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0 )); | |
6259ec0d UD |
237 | |
238 | __libc_lock_unlock (lock); | |
239 | ||
240 | return status; | |
241 | } | |
242 | ||
1e275b9e UD |
243 | static enum nss_status |
244 | internal_gethostbyname2_r (const char *name, int af, struct hostent *host, | |
d71b808a | 245 | char *buffer, size_t buflen, int *errnop, |
1e275b9e | 246 | int *h_errnop, int flags) |
6259ec0d UD |
247 | { |
248 | enum nss_status retval; | |
249 | char *domain, *result, *p; | |
250 | int len, parse_res; | |
251 | struct parser_data *data = (void *) buffer; | |
252 | size_t linebuflen = buffer + buflen - data->linebuffer; | |
253 | ||
254 | if (name == NULL) | |
255 | { | |
ac9f45cf | 256 | *errnop = EINVAL; |
6259ec0d UD |
257 | return NSS_STATUS_UNAVAIL; |
258 | } | |
259 | ||
260 | if (yp_get_default_domain (&domain)) | |
261 | return NSS_STATUS_UNAVAIL; | |
262 | ||
263 | if (buflen < sizeof *data + 1) | |
264 | { | |
265 | *h_errnop = NETDB_INTERNAL; | |
d71b808a | 266 | *errnop = ERANGE; |
6259ec0d UD |
267 | return NSS_STATUS_TRYAGAIN; |
268 | } | |
cd897fe7 UD |
269 | else |
270 | { | |
271 | /* Convert name to lowercase. */ | |
4775243a UD |
272 | size_t namlen = strlen (name); |
273 | char name2[namlen + 1]; | |
cd897fe7 UD |
274 | int i; |
275 | ||
4775243a | 276 | for (i = 0; i < namlen; ++i) |
cd897fe7 UD |
277 | name2[i] = tolower (name[i]); |
278 | name2[i] = '\0'; | |
279 | ||
280 | retval = yperr2nss (yp_match (domain, "hosts.byname", name2, | |
4775243a | 281 | namlen, &result, &len)); |
cd897fe7 UD |
282 | |
283 | } | |
6259ec0d UD |
284 | |
285 | if (retval != NSS_STATUS_SUCCESS) | |
286 | { | |
287 | if (retval == NSS_STATUS_TRYAGAIN) | |
288 | { | |
289 | *h_errnop = TRY_AGAIN; | |
d71b808a | 290 | *errnop = errno; |
6259ec0d UD |
291 | } |
292 | if (retval == NSS_STATUS_NOTFOUND) | |
293 | *h_errnop = HOST_NOT_FOUND; | |
294 | return retval; | |
295 | } | |
296 | ||
f8b87ef0 | 297 | if ((size_t) (len + 1) > linebuflen) |
6259ec0d UD |
298 | { |
299 | free (result); | |
300 | *h_errnop = NETDB_INTERNAL; | |
d71b808a | 301 | *errnop = ERANGE; |
6259ec0d UD |
302 | return NSS_STATUS_TRYAGAIN; |
303 | } | |
304 | ||
305 | p = strncpy (data->linebuffer, result, len); | |
306 | data->linebuffer[len] = '\0'; | |
307 | while (isspace (*p)) | |
308 | ++p; | |
309 | free (result); | |
310 | ||
1e275b9e | 311 | parse_res = parse_line (p, host, data, buflen, errnop, af, flags); |
6259ec0d | 312 | |
60c96635 | 313 | if (parse_res < 1 || host->h_addrtype != af) |
6259ec0d | 314 | { |
60c96635 | 315 | if (parse_res == -1) |
6259ec0d UD |
316 | { |
317 | *h_errnop = NETDB_INTERNAL; | |
318 | return NSS_STATUS_TRYAGAIN; | |
319 | } | |
320 | else | |
321 | { | |
322 | *h_errnop = HOST_NOT_FOUND; | |
323 | return NSS_STATUS_NOTFOUND; | |
324 | } | |
325 | } | |
326 | ||
327 | *h_errnop = NETDB_SUCCESS; | |
328 | return NSS_STATUS_SUCCESS; | |
329 | } | |
330 | ||
1e275b9e UD |
331 | enum nss_status |
332 | _nss_nis_gethostbyname2_r (const char *name, int af, struct hostent *host, | |
333 | char *buffer, size_t buflen, int *errnop, | |
334 | int *h_errnop) | |
335 | { | |
336 | return internal_gethostbyname2_r (name, af, host, buffer, buflen, errnop, | |
337 | h_errnop, | |
338 | ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0)); | |
339 | } | |
340 | ||
0d8733c4 | 341 | enum nss_status |
d71b808a UD |
342 | _nss_nis_gethostbyname_r (const char *name, struct hostent *host, char *buffer, |
343 | size_t buflen, int *errnop, int *h_errnop) | |
0d8733c4 UD |
344 | { |
345 | if (_res.options & RES_USE_INET6) | |
346 | { | |
347 | enum nss_status status; | |
348 | ||
1e275b9e UD |
349 | status = internal_gethostbyname2_r (name, AF_INET6, host, buffer, buflen, |
350 | errnop, h_errnop, AI_V4MAPPED); | |
0d8733c4 UD |
351 | if (status == NSS_STATUS_SUCCESS) |
352 | return status; | |
353 | } | |
354 | ||
1e275b9e UD |
355 | return internal_gethostbyname2_r (name, AF_INET, host, buffer, buflen, |
356 | errnop, h_errnop, 0); | |
0d8733c4 UD |
357 | } |
358 | ||
6259ec0d | 359 | enum nss_status |
9d4d69b8 | 360 | _nss_nis_gethostbyaddr_r (const void *addr, socklen_t addrlen, int af, |
6259ec0d | 361 | struct hostent *host, char *buffer, size_t buflen, |
d71b808a | 362 | int *errnop, int *h_errnop) |
6259ec0d UD |
363 | { |
364 | enum nss_status retval; | |
365 | char *domain, *result, *p; | |
366 | int len, parse_res; | |
367 | char *buf; | |
368 | struct parser_data *data = (void *) buffer; | |
369 | size_t linebuflen = buffer + buflen - data->linebuffer; | |
370 | ||
371 | if (yp_get_default_domain (&domain)) | |
372 | return NSS_STATUS_UNAVAIL; | |
373 | ||
374 | if (buflen < sizeof *data + 1) | |
375 | { | |
d71b808a | 376 | *errnop = ERANGE; |
6259ec0d UD |
377 | *h_errnop = NETDB_INTERNAL; |
378 | return NSS_STATUS_TRYAGAIN; | |
379 | } | |
380 | ||
9d4d69b8 | 381 | buf = inet_ntoa (*(const struct in_addr *) addr); |
6259ec0d UD |
382 | |
383 | retval = yperr2nss (yp_match (domain, "hosts.byaddr", buf, | |
384 | strlen (buf), &result, &len)); | |
385 | ||
386 | if (retval != NSS_STATUS_SUCCESS) | |
387 | { | |
388 | if (retval == NSS_STATUS_TRYAGAIN) | |
389 | { | |
390 | *h_errnop = TRY_AGAIN; | |
d71b808a | 391 | *errnop = errno; |
6259ec0d UD |
392 | } |
393 | if (retval == NSS_STATUS_NOTFOUND) | |
34816665 UD |
394 | *h_errnop = HOST_NOT_FOUND; |
395 | ||
6259ec0d UD |
396 | return retval; |
397 | } | |
398 | ||
f8b87ef0 | 399 | if ((size_t) (len + 1) > linebuflen) |
6259ec0d UD |
400 | { |
401 | free (result); | |
d71b808a | 402 | *errnop = ERANGE; |
6259ec0d UD |
403 | *h_errnop = NETDB_INTERNAL; |
404 | return NSS_STATUS_TRYAGAIN; | |
405 | } | |
406 | ||
407 | p = strncpy (data->linebuffer, result, len); | |
408 | data->linebuffer[len] = '\0'; | |
409 | while (isspace (*p)) | |
410 | ++p; | |
411 | free (result); | |
412 | ||
1e275b9e UD |
413 | parse_res = parse_line (p, host, data, buflen, errnop, af, |
414 | ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0)); | |
60c96635 | 415 | if (parse_res < 1) |
6259ec0d | 416 | { |
60c96635 | 417 | if (parse_res == -1) |
6259ec0d UD |
418 | { |
419 | *h_errnop = NETDB_INTERNAL; | |
420 | return NSS_STATUS_TRYAGAIN; | |
421 | } | |
422 | else | |
423 | { | |
424 | *h_errnop = HOST_NOT_FOUND; | |
425 | return NSS_STATUS_NOTFOUND; | |
426 | } | |
427 | } | |
428 | ||
429 | *h_errnop = NETDB_SUCCESS; | |
430 | return NSS_STATUS_SUCCESS; | |
431 | } | |
1e275b9e | 432 | |
eba8c0e2 | 433 | #if 0 |
1e275b9e UD |
434 | enum nss_status |
435 | _nss_nis_getipnodebyname_r (const char *name, int af, int flags, | |
436 | struct hostent *result, char *buffer, | |
437 | size_t buflen, int *errnop, int *herrnop) | |
438 | { | |
439 | return internal_gethostbyname2_r (name, af, result, buffer, buflen, | |
440 | errnop, herrnop, flags); | |
441 | } | |
eba8c0e2 | 442 | #endif |