2.5-18.1
[glibc.git] / nis / nss_nis / nis-hosts.c
blobbde0a3291f8eb3d8451f089cef37f1ea242f0da7
1 /* Copyright (C) 1996-2000, 2002, 2003, 2006, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
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. */
20 #include <nss.h>
21 #include <ctype.h>
22 /* The following is an ugly trick to avoid a prototype declaration for
23 _nss_nis_endgrent. */
24 #define _nss_nis_endhostent _nss_nis_endhostent_XXX
25 #include <netdb.h>
26 #undef _nss_nis_endhostent
27 #include <string.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <resolv.h>
31 #include <bits/libc-lock.h>
32 #include <rpcsvc/yp.h>
33 #include <rpcsvc/ypclnt.h>
35 #include "nss-nis.h"
37 /* Get implementation for some internal functions. */
38 #include <resolv/mapv4v6addr.h>
40 #define ENTNAME hostent
41 #define DATABASE "hosts"
42 #define NEED_H_ERRNO
44 #define EXTRA_ARGS , af, flags
45 #define EXTRA_ARGS_DECL , int af, int flags
47 #define ENTDATA hostent_data
48 struct hostent_data
50 unsigned char host_addr[16]; /* IPv4 or IPv6 address. */
51 char *h_addr_ptrs[2]; /* Points to that and null terminator. */
54 #define TRAILING_LIST_MEMBER h_aliases
55 #define TRAILING_LIST_SEPARATOR_P isspace
56 #include <nss/nss_files/files-parse.c>
57 LINE_PARSER
58 ("#",
60 char *addr;
62 STRING_FIELD (addr, isspace, 1);
64 /* Parse address. */
65 if (af == AF_INET && inet_pton (AF_INET, addr, entdata->host_addr) > 0)
67 if (flags & AI_V4MAPPED)
69 map_v4v6_address ((char *) entdata->host_addr,
70 (char *) entdata->host_addr);
71 result->h_addrtype = AF_INET6;
72 result->h_length = IN6ADDRSZ;
74 else
76 result->h_addrtype = AF_INET;
77 result->h_length = INADDRSZ;
80 else if (af == AF_INET6
81 && inet_pton (AF_INET6, addr, entdata->host_addr) > 0)
83 result->h_addrtype = AF_INET6;
84 result->h_length = IN6ADDRSZ;
86 else
87 /* Illegal address: ignore line. */
88 return 0;
90 /* Store a pointer to the address in the expected form. */
91 entdata->h_addr_ptrs[0] = entdata->host_addr;
92 entdata->h_addr_ptrs[1] = NULL;
93 result->h_addr_list = entdata->h_addr_ptrs;
95 STRING_FIELD (result->h_name, isspace, 1);
99 __libc_lock_define_initialized (static, lock)
101 static bool_t new_start = 1;
102 static char *oldkey = NULL;
103 static int oldkeylen = 0;
105 enum nss_status
106 _nss_nis_sethostent (int stayopen)
108 __libc_lock_lock (lock);
110 new_start = 1;
111 if (oldkey != NULL)
113 free (oldkey);
114 oldkey = NULL;
115 oldkeylen = 0;
118 __libc_lock_unlock (lock);
120 return NSS_STATUS_SUCCESS;
122 /* Make _nss_nis_endhostent an alias of _nss_nis_sethostent. We do this
123 even though the prototypes don't match. The argument of sethostent
124 is used so this makes no difference. */
125 strong_alias (_nss_nis_sethostent, _nss_nis_endhostent)
127 /* The calling function always need to get a lock first. */
128 static enum nss_status
129 internal_nis_gethostent_r (struct hostent *host, char *buffer,
130 size_t buflen, int *errnop, int *h_errnop,
131 int af, int flags)
133 char *domain;
134 if (__builtin_expect (yp_get_default_domain (&domain), 0))
135 return NSS_STATUS_UNAVAIL;
137 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct parser_data);
138 buffer += pad;
140 struct parser_data *data = (void *) buffer;
141 if (__builtin_expect (buflen < sizeof *data + 1 + pad, 0))
143 *errnop = ERANGE;
144 *h_errnop = NETDB_INTERNAL;
145 return NSS_STATUS_TRYAGAIN;
147 buflen -= pad;
149 /* Get the next entry until we found a correct one. */
150 const size_t linebuflen = buffer + buflen - data->linebuffer;
151 int parse_res;
154 char *result;
155 int len;
156 char *outkey;
157 int keylen;
158 int yperr;
159 if (new_start)
160 yperr = yp_first (domain, "hosts.byname", &outkey, &keylen, &result,
161 &len);
162 else
163 yperr = yp_next (domain, "hosts.byname", oldkey, oldkeylen, &outkey,
164 &keylen, &result, &len);
166 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
168 enum nss_status retval = yperr2nss (yperr);
170 switch (retval)
172 case NSS_STATUS_TRYAGAIN:
173 *errnop = errno;
174 *h_errnop = TRY_AGAIN;
175 break;
176 case NSS_STATUS_NOTFOUND:
177 *h_errnop = HOST_NOT_FOUND;
178 break;
179 default:
180 *h_errnop = NO_RECOVERY;
181 break;
183 return retval;
186 if (__builtin_expect ((size_t) (len + 1) > linebuflen, 0))
188 free (result);
189 *h_errnop = NETDB_INTERNAL;
190 *errnop = ERANGE;
191 return NSS_STATUS_TRYAGAIN;
194 char *p = strncpy (data->linebuffer, result, len);
195 data->linebuffer[len] = '\0';
196 while (isspace (*p))
197 ++p;
198 free (result);
200 parse_res = parse_line (p, host, data, buflen, errnop, af, flags);
201 if (__builtin_expect (parse_res == -1, 0))
203 free (outkey);
204 *h_errnop = NETDB_INTERNAL;
205 *errnop = ERANGE;
206 return NSS_STATUS_TRYAGAIN;
208 free (oldkey);
209 oldkey = outkey;
210 oldkeylen = keylen;
211 new_start = 0;
213 while (!parse_res);
215 *h_errnop = NETDB_SUCCESS;
216 return NSS_STATUS_SUCCESS;
219 enum nss_status
220 _nss_nis_gethostent_r (struct hostent *host, char *buffer, size_t buflen,
221 int *errnop, int *h_errnop)
223 enum nss_status status;
225 __libc_lock_lock (lock);
227 status = internal_nis_gethostent_r (host, buffer, buflen, errnop, h_errnop,
228 ((_res.options & RES_USE_INET6) ? AF_INET6 : AF_INET),
229 ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0 ));
231 __libc_lock_unlock (lock);
233 return status;
236 static enum nss_status
237 internal_gethostbyname2_r (const char *name, int af, struct hostent *host,
238 char *buffer, size_t buflen, int *errnop,
239 int *h_errnop, int flags)
241 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct parser_data);
242 buffer += pad;
244 struct parser_data *data = (void *) buffer;
246 if (name == NULL)
248 *errnop = EINVAL;
249 return NSS_STATUS_UNAVAIL;
252 char *domain;
253 if (yp_get_default_domain (&domain))
254 return NSS_STATUS_UNAVAIL;
256 if (buflen < sizeof *data + 1 + pad)
258 *h_errnop = NETDB_INTERNAL;
259 *errnop = ERANGE;
260 return NSS_STATUS_TRYAGAIN;
262 buflen -= pad;
264 /* Convert name to lowercase. */
265 size_t namlen = strlen (name);
266 char name2[namlen + 1];
267 size_t i;
269 for (i = 0; i < namlen; ++i)
270 name2[i] = tolower (name[i]);
271 name2[i] = '\0';
273 char *result;
274 int len;
275 int yperr = yp_match (domain, "hosts.byname", name2, namlen, &result, &len);
277 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
279 enum nss_status retval = yperr2nss (yperr);
281 if (retval == NSS_STATUS_TRYAGAIN)
283 *h_errnop = TRY_AGAIN;
284 *errnop = errno;
286 if (retval == NSS_STATUS_NOTFOUND)
287 *h_errnop = HOST_NOT_FOUND;
288 return retval;
291 const size_t linebuflen = buffer + buflen - data->linebuffer;
292 if (__builtin_expect ((size_t) (len + 1) > linebuflen, 0))
294 free (result);
295 *h_errnop = NETDB_INTERNAL;
296 *errnop = ERANGE;
297 return NSS_STATUS_TRYAGAIN;
300 char *p = strncpy (data->linebuffer, result, len);
301 data->linebuffer[len] = '\0';
302 while (isspace (*p))
303 ++p;
304 free (result);
306 int parse_res = parse_line (p, host, data, buflen, errnop, af, flags);
308 if (__builtin_expect (parse_res < 1 || host->h_addrtype != af, 0))
310 if (parse_res == -1)
312 *h_errnop = NETDB_INTERNAL;
313 return NSS_STATUS_TRYAGAIN;
315 else
317 *h_errnop = HOST_NOT_FOUND;
318 return NSS_STATUS_NOTFOUND;
322 *h_errnop = NETDB_SUCCESS;
323 return NSS_STATUS_SUCCESS;
326 enum nss_status
327 _nss_nis_gethostbyname2_r (const char *name, int af, struct hostent *host,
328 char *buffer, size_t buflen, int *errnop,
329 int *h_errnop)
331 return internal_gethostbyname2_r (name, af, host, buffer, buflen, errnop,
332 h_errnop,
333 ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0));
336 enum nss_status
337 _nss_nis_gethostbyname_r (const char *name, struct hostent *host, char *buffer,
338 size_t buflen, int *errnop, int *h_errnop)
340 if (_res.options & RES_USE_INET6)
342 enum nss_status status;
344 status = internal_gethostbyname2_r (name, AF_INET6, host, buffer, buflen,
345 errnop, h_errnop, AI_V4MAPPED);
346 if (status == NSS_STATUS_SUCCESS)
347 return status;
350 return internal_gethostbyname2_r (name, AF_INET, host, buffer, buflen,
351 errnop, h_errnop, 0);
354 enum nss_status
355 _nss_nis_gethostbyaddr_r (const void *addr, socklen_t addrlen, int af,
356 struct hostent *host, char *buffer, size_t buflen,
357 int *errnop, int *h_errnop)
359 char *domain;
360 if (__builtin_expect (yp_get_default_domain (&domain), 0))
361 return NSS_STATUS_UNAVAIL;
363 uintptr_t pad = -(uintptr_t) buffer % __alignof__ (struct parser_data);
364 buffer += pad;
366 struct parser_data *data = (void *) buffer;
367 if (__builtin_expect (buflen < sizeof *data + 1 + pad, 0))
369 *errnop = ERANGE;
370 *h_errnop = NETDB_INTERNAL;
371 return NSS_STATUS_TRYAGAIN;
373 buflen -= pad;
375 char *buf = inet_ntoa (*(const struct in_addr *) addr);
377 char *result;
378 int len;
379 int yperr = yp_match (domain, "hosts.byaddr", buf, strlen (buf), &result,
380 &len);
382 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
384 enum nss_status retval = yperr2nss (yperr);
386 if (retval == NSS_STATUS_TRYAGAIN)
388 *h_errnop = TRY_AGAIN;
389 *errnop = errno;
391 else if (retval == NSS_STATUS_NOTFOUND)
392 *h_errnop = HOST_NOT_FOUND;
394 return retval;
397 const size_t linebuflen = buffer + buflen - data->linebuffer;
398 if (__builtin_expect ((size_t) (len + 1) > linebuflen, 0))
400 free (result);
401 *errnop = ERANGE;
402 *h_errnop = NETDB_INTERNAL;
403 return NSS_STATUS_TRYAGAIN;
406 char *p = strncpy (data->linebuffer, result, len);
407 data->linebuffer[len] = '\0';
408 while (isspace (*p))
409 ++p;
410 free (result);
412 int parse_res = parse_line (p, host, data, buflen, errnop, af,
413 ((_res.options & RES_USE_INET6)
414 ? AI_V4MAPPED : 0));
415 if (__builtin_expect (parse_res < 1, 0))
417 if (parse_res == -1)
419 *h_errnop = NETDB_INTERNAL;
420 return NSS_STATUS_TRYAGAIN;
422 else
424 *h_errnop = HOST_NOT_FOUND;
425 return NSS_STATUS_NOTFOUND;
429 *h_errnop = NETDB_SUCCESS;
430 return NSS_STATUS_SUCCESS;
433 #if 0
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)
439 return internal_gethostbyname2_r (name, af, result, buffer, buflen,
440 errnop, herrnop, flags);
442 #endif