2006-03-05 Roland McGrath <roland@frob.com>
[glibc/history.git] / nis / nss_nisplus / nisplus-hosts.c
blob81f8a984dcda6839eb2cc096ee4ca45382ba4447
1 /* Copyright (C) 1997-2002, 2003, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
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 <atomic.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <netdb.h>
24 #include <nss.h>
25 #include <string.h>
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28 #include <rpcsvc/nis.h>
29 #include <bits/libc-lock.h>
31 #include "nss-nisplus.h"
33 __libc_lock_define_initialized (static, lock)
35 static nis_result *result;
36 static nis_name tablename_val;
37 static u_long tablename_len;
39 #define NISENTRYVAL(idx,col,res) \
40 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
42 #define NISENTRYLEN(idx,col,res) \
43 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
45 /* Get implementation for some internal functions. */
46 #include <resolv/mapv4v6addr.h>
48 static int
49 _nss_nisplus_parse_hostent (nis_result *result, int af, struct hostent *host,
50 char *buffer, size_t buflen, int *errnop,
51 int flags)
53 unsigned int i;
54 char *first_unused = buffer;
55 size_t room_left = buflen;
56 char *data, *p, *line;
58 if (result == NULL)
59 return 0;
61 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
62 __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
63 strcmp(result->objects.objects_val[0].EN_data.en_type,
64 "hosts_tbl") != 0 ||
65 result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 4)
66 return 0;
68 if (room_left < NISENTRYLEN (0, 2, result) + 1)
70 no_more_room:
71 *errnop = ERANGE;
72 return -1;
75 data = first_unused;
77 /* Parse address. */
78 if (af == AF_INET && inet_pton (af, NISENTRYVAL (0, 2, result), data) > 0)
80 if (flags & AI_V4MAPPED)
82 map_v4v6_address (data, data);
83 host->h_addrtype = AF_INET6;
84 host->h_length = IN6ADDRSZ;
86 else
88 host->h_addrtype = AF_INET;
89 host->h_length = INADDRSZ;
92 else if (af == AF_INET6
93 && inet_pton (AF_INET6, NISENTRYVAL (0, 2, result), data) > 0)
95 host->h_addrtype = AF_INET6;
96 host->h_length = IN6ADDRSZ;
98 else
99 /* Illegal address: ignore line. */
100 return 0;
102 first_unused+=host->h_length;
103 room_left-=host->h_length;
105 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
106 goto no_more_room;
108 p = __stpncpy (first_unused, NISENTRYVAL (0, 0, result),
109 NISENTRYLEN (0, 0, result));
110 *p = '\0';
111 room_left -= (NISENTRYLEN (0, 0, result) + 1);
112 host->h_name = first_unused;
113 first_unused += NISENTRYLEN (0, 0, result) +1;
114 p = first_unused;
116 line = p;
117 for (i = 0; i < result->objects.objects_len; ++i)
119 if (strcmp (NISENTRYVAL (i, 1, result), host->h_name) != 0)
121 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
122 goto no_more_room;
124 *p++ = ' ';
125 p = __stpncpy (p, NISENTRYVAL (i, 1, result),
126 NISENTRYLEN (i, 1, result));
127 *p = '\0';
128 room_left -= (NISENTRYLEN (i, 1, result) + 1);
131 *p++ = '\0';
132 first_unused = p;
134 /* Adjust the pointer so it is aligned for
135 storing pointers. */
136 first_unused += __alignof__ (char *) - 1;
137 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
138 host->h_addr_list = (char **) first_unused;
139 if (room_left < 2 * sizeof (char *))
140 goto no_more_room;
142 room_left -= (2 * sizeof (char *));
143 host->h_addr_list[0] = data;
144 host->h_addr_list[1] = NULL;
145 host->h_aliases = &host->h_addr_list[2];
146 host->h_aliases[0] = NULL;
148 i = 0;
149 while (*line != '\0')
151 /* Skip leading blanks. */
152 while (isspace (*line))
153 ++line;
155 if (*line == '\0')
156 break;
158 if (room_left < sizeof (char *))
159 goto no_more_room;
161 room_left -= sizeof (char *);
162 host->h_aliases[i] = line;
164 while (*line != '\0' && *line != ' ')
165 ++line;
167 if (*line == ' ')
169 *line = '\0';
170 ++line;
171 ++i;
173 else
174 host->h_aliases[i+1] = NULL;
176 return 1;
179 static enum nss_status
180 _nss_create_tablename (int *errnop)
182 if (tablename_val == NULL)
184 const char *local_dir = nis_local_directory ();
185 size_t local_dir_len = strlen (local_dir);
186 static const char prefix[] = "hosts.org_dir.";
188 char *p = malloc (sizeof (prefix) + local_dir_len);
189 if (tablename_val == NULL)
191 *errnop = errno;
192 return NSS_STATUS_TRYAGAIN;
195 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
197 tablename_len = sizeof (prefix) - 1 + local_dir_len;
199 atomic_write_barrier ();
201 tablename_val = p;
204 return NSS_STATUS_SUCCESS;
207 enum nss_status
208 _nss_nisplus_sethostent (int stayopen)
210 enum nss_status status = NSS_STATUS_SUCCESS;
211 int err;
213 __libc_lock_lock (lock);
215 if (result != NULL)
217 nis_freeresult (result);
218 result = NULL;
221 if (tablename_val == NULL)
222 status = _nss_create_tablename (&err);
224 __libc_lock_unlock (lock);
226 return status;
229 enum nss_status
230 _nss_nisplus_endhostent (void)
232 __libc_lock_lock (lock);
234 if (result != NULL)
236 nis_freeresult (result);
237 result = NULL;
240 __libc_lock_unlock (lock);
242 return NSS_STATUS_SUCCESS;
245 static enum nss_status
246 internal_nisplus_gethostent_r (struct hostent *host, char *buffer,
247 size_t buflen, int *errnop, int *herrnop)
249 int parse_res;
251 /* Get the next entry until we found a correct one. */
254 nis_result *saved_res;
256 if (result == NULL)
258 saved_res = NULL;
259 if (tablename_val == NULL)
261 enum nss_status status = _nss_create_tablename (errnop);
263 if (status != NSS_STATUS_SUCCESS)
264 return status;
267 result = nis_first_entry (tablename_val);
268 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
270 enum nss_status retval = niserr2nss (result->status);
271 if (retval == NSS_STATUS_TRYAGAIN)
273 *herrnop = NETDB_INTERNAL;
274 *errnop = errno;
276 return retval;
280 else
282 nis_result *res2;
284 saved_res = result;
285 res2 = nis_next_entry(tablename_val, &result->cookie);
286 result = res2;
287 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
289 enum nss_status retval= niserr2nss (result->status);
291 nis_freeresult (result);
292 result = saved_res;
293 if (retval == NSS_STATUS_TRYAGAIN)
295 *herrnop = NETDB_INTERNAL;
296 *errnop = errno;
298 return retval;
302 if (_res.options & RES_USE_INET6)
303 parse_res = _nss_nisplus_parse_hostent (result, AF_INET6, host, buffer,
304 buflen, errnop, AI_V4MAPPED);
305 else
306 parse_res = _nss_nisplus_parse_hostent (result, AF_INET, host, buffer,
307 buflen, errnop, 0);
309 if (parse_res == -1)
311 nis_freeresult (result);
312 result = saved_res;
313 *herrnop = NETDB_INTERNAL;
314 *errnop = ERANGE;
315 return NSS_STATUS_TRYAGAIN;
317 if (saved_res != NULL)
318 nis_freeresult (saved_res);
320 } while (!parse_res);
322 return NSS_STATUS_SUCCESS;
325 enum nss_status
326 _nss_nisplus_gethostent_r (struct hostent *result, char *buffer,
327 size_t buflen, int *errnop, int *herrnop)
329 int status;
331 __libc_lock_lock (lock);
333 status = internal_nisplus_gethostent_r (result, buffer, buflen, errnop,
334 herrnop);
336 __libc_lock_unlock (lock);
338 return status;
341 static enum nss_status
342 internal_gethostbyname2_r (const char *name, int af, struct hostent *host,
343 char *buffer, size_t buflen, int *errnop,
344 int *herrnop, int flags)
346 int parse_res, retval;
348 if (tablename_val == NULL)
350 __libc_lock_lock (lock);
352 enum nss_status status = _nss_create_tablename (errnop);
354 __libc_lock_unlock (lock);
356 if (status != NSS_STATUS_SUCCESS)
358 *herrnop = NETDB_INTERNAL;
359 return NSS_STATUS_UNAVAIL;
363 if (name == NULL)
365 *errnop = EINVAL;
366 *herrnop = NETDB_INTERNAL;
367 return NSS_STATUS_NOTFOUND;
370 nis_result *result;
371 char buf[strlen (name) + 10 + tablename_len];
372 int olderr = errno;
374 /* Search at first in the alias list, and use the correct name
375 for the next search. */
376 snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
377 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
379 if (result != NULL)
381 char *bufptr = buf;
383 /* If we did not find it, try it as original name. But if the
384 database is correct, we should find it in the first case, too. */
385 if ((result->status != NIS_SUCCESS
386 && result->status != NIS_S_SUCCESS)
387 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
388 || strcmp (result->objects.objects_val->EN_data.en_type,
389 "hosts_tbl") != 0
390 || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
391 snprintf (buf, sizeof (buf), "[cname=%s],%s", name, tablename_val);
392 else
394 /* We need to allocate a new buffer since there is no
395 guarantee the returned name has a length limit. */
396 const char *entryval = NISENTRYVAL(0, 0, result);
397 size_t buflen = strlen (entryval) + 10 + tablename_len;
398 bufptr = alloca (buflen);
399 snprintf (bufptr, buflen, "[cname=%s],%s",
400 entryval, tablename_val);
403 nis_freeresult (result);
404 result = nis_list (bufptr, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
407 if (result == NULL)
409 *errnop = ENOMEM;
410 return NSS_STATUS_TRYAGAIN;
413 retval = niserr2nss (result->status);
414 if (__builtin_expect (retval != NSS_STATUS_SUCCESS, 0))
416 if (retval == NSS_STATUS_TRYAGAIN)
418 *errnop = errno;
419 *herrnop = NETDB_INTERNAL;
421 else
422 __set_errno (olderr);
423 nis_freeresult (result);
424 return retval;
427 parse_res = _nss_nisplus_parse_hostent (result, af, host, buffer,
428 buflen, errnop, flags);
430 nis_freeresult (result);
432 if (parse_res > 0)
433 return NSS_STATUS_SUCCESS;
435 *herrnop = NETDB_INTERNAL;
436 if (parse_res == -1)
438 *errnop = ERANGE;
439 return NSS_STATUS_TRYAGAIN;
442 __set_errno (olderr);
443 return NSS_STATUS_NOTFOUND;
446 enum nss_status
447 _nss_nisplus_gethostbyname2_r (const char *name, int af, struct hostent *host,
448 char *buffer, size_t buflen, int *errnop,
449 int *herrnop)
451 return internal_gethostbyname2_r (name, af, host, buffer, buflen, errnop,
452 herrnop,
453 ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0));
456 enum nss_status
457 _nss_nisplus_gethostbyname_r (const char *name, struct hostent *host,
458 char *buffer, size_t buflen, int *errnop,
459 int *h_errnop)
461 if (_res.options & RES_USE_INET6)
463 enum nss_status status;
465 status = internal_gethostbyname2_r (name, AF_INET6, host, buffer,
466 buflen, errnop, h_errnop,
467 AI_V4MAPPED);
468 if (status == NSS_STATUS_SUCCESS)
469 return status;
472 return internal_gethostbyname2_r (name, AF_INET, host, buffer,
473 buflen, errnop, h_errnop, 0);
476 enum nss_status
477 _nss_nisplus_gethostbyaddr_r (const void *addr, socklen_t addrlen, int af,
478 struct hostent *host, char *buffer,
479 size_t buflen, int *errnop, int *herrnop)
481 if (tablename_val == NULL)
483 __libc_lock_lock (lock);
485 enum nss_status status = _nss_create_tablename (errnop);
487 __libc_lock_unlock (lock);
489 if (status != NSS_STATUS_SUCCESS)
490 return status;
493 if (addr == NULL)
494 return NSS_STATUS_NOTFOUND;
496 char buf[24 + tablename_len];
497 int retval, parse_res;
498 int olderr = errno;
500 snprintf (buf, sizeof (buf), "[addr=%s],%s",
501 inet_ntoa (*(const struct in_addr *) addr), tablename_val);
502 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
504 if (result == NULL)
506 __set_errno (ENOMEM);
507 return NSS_STATUS_TRYAGAIN;
510 retval = niserr2nss (result->status);
511 if (__builtin_expect (retval != NSS_STATUS_SUCCESS, 0))
513 if (retval == NSS_STATUS_TRYAGAIN)
515 *errnop = errno;
516 *herrnop = NETDB_INTERNAL;
518 else
519 __set_errno (olderr);
520 nis_freeresult (result);
521 return retval;
524 parse_res = _nss_nisplus_parse_hostent (result, af, host,
525 buffer, buflen, errnop,
526 ((_res.options & RES_USE_INET6)
527 ? AI_V4MAPPED : 0));
528 nis_freeresult (result);
530 if (parse_res > 0)
531 return NSS_STATUS_SUCCESS;
533 *herrnop = NETDB_INTERNAL;
534 if (parse_res == -1)
536 *errnop = ERANGE;
537 return NSS_STATUS_TRYAGAIN;
540 __set_errno (olderr);
541 return NSS_STATUS_NOTFOUND;