Update.
[glibc.git] / nis / nss_nisplus / nisplus-hosts.c
blob6fc208a202c1212419971bcd8d8cce332b1dfdf3
1 /* Copyright (C) 1997, 1998, 1999, 2000, 2001 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 <nss.h>
21 #include <netdb.h>
22 #include <errno.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <bits/libc-lock.h>
28 #include <rpcsvc/nis.h>
30 #include "nss-nisplus.h"
32 __libc_lock_define_initialized (static, lock)
34 static nis_result *result;
35 static nis_name tablename_val;
36 static u_long tablename_len;
38 #define NISENTRYVAL(idx,col,res) \
39 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
41 #define NISENTRYLEN(idx,col,res) \
42 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
44 /* Get implementation for some internal functions. */
45 #include <resolv/mapv4v6addr.h>
47 static int
48 _nss_nisplus_parse_hostent (nis_result *result, int af, struct hostent *host,
49 char *buffer, size_t buflen, int *errnop,
50 int flags)
52 unsigned int i;
53 char *first_unused = buffer;
54 size_t room_left = buflen;
55 char *data, *p, *line;
57 if (result == NULL)
58 return 0;
60 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
61 __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
62 strcmp(result->objects.objects_val[0].EN_data.en_type,
63 "hosts_tbl") != 0 ||
64 result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 4)
65 return 0;
67 if (room_left < NISENTRYLEN (0, 2, result) + 1)
69 no_more_room:
70 *errnop = ERANGE;
71 return -1;
74 data = first_unused;
76 /* Parse address. */
77 if (af == AF_INET && inet_pton (af, NISENTRYVAL (0, 2, result), data) > 0)
79 if (flags & AI_V4MAPPED)
81 map_v4v6_address (data, data);
82 host->h_addrtype = AF_INET6;
83 host->h_length = IN6ADDRSZ;
85 else
87 host->h_addrtype = AF_INET;
88 host->h_length = INADDRSZ;
91 else if (af == AF_INET6
92 && inet_pton (AF_INET6, NISENTRYVAL (0, 2, result), data) > 0)
94 host->h_addrtype = AF_INET6;
95 host->h_length = IN6ADDRSZ;
97 else
98 /* Illegal address: ignore line. */
99 return 0;
101 first_unused+=host->h_length;
102 room_left-=host->h_length;
104 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
105 goto no_more_room;
107 p = __stpncpy (first_unused, NISENTRYVAL (0, 0, result),
108 NISENTRYLEN (0, 0, result));
109 *p = '\0';
110 room_left -= (NISENTRYLEN (0, 0, result) + 1);
111 host->h_name = first_unused;
112 first_unused += NISENTRYLEN (0, 0, result) +1;
113 p = first_unused;
115 line = p;
116 for (i = 0; i < result->objects.objects_len; ++i)
118 if (strcmp (NISENTRYVAL (i, 1, result), host->h_name) != 0)
120 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
121 goto no_more_room;
123 *p++ = ' ';
124 p = __stpncpy (p, NISENTRYVAL (i, 1, result),
125 NISENTRYLEN (i, 1, result));
126 *p = '\0';
127 room_left -= (NISENTRYLEN (i, 1, result) + 1);
130 *p++ = '\0';
131 first_unused = p;
133 /* Adjust the pointer so it is aligned for
134 storing pointers. */
135 first_unused += __alignof__ (char *) - 1;
136 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
137 host->h_addr_list = (char **) first_unused;
138 if (room_left < 2 * sizeof (char *))
139 goto no_more_room;
141 room_left -= (2 * sizeof (char *));
142 host->h_addr_list[0] = data;
143 host->h_addr_list[1] = NULL;
144 host->h_aliases = &host->h_addr_list[2];
145 host->h_aliases[0] = NULL;
147 i = 0;
148 while (*line != '\0')
150 /* Skip leading blanks. */
151 while (isspace (*line))
152 ++line;
154 if (*line == '\0')
155 break;
157 if (room_left < sizeof (char *))
158 goto no_more_room;
160 room_left -= sizeof (char *);
161 host->h_aliases[i] = line;
163 while (*line != '\0' && *line != ' ')
164 ++line;
166 if (*line == ' ')
168 *line = '\0';
169 ++line;
170 ++i;
172 else
173 host->h_aliases[i+1] = NULL;
175 return 1;
178 static enum nss_status
179 _nss_create_tablename (int *errnop)
181 if (tablename_val == NULL)
183 char buf [40 + strlen (nis_local_directory ())];
184 char *p;
186 p = __stpcpy (buf, "hosts.org_dir.");
187 p = __stpcpy (p, nis_local_directory ());
188 tablename_val = __strdup (buf);
189 if (tablename_val == NULL)
191 *errnop = errno;
192 return NSS_STATUS_TRYAGAIN;
194 tablename_len = strlen (tablename_val);
196 return NSS_STATUS_SUCCESS;
199 enum nss_status
200 _nss_nisplus_sethostent (int stayopen)
202 enum nss_status status = NSS_STATUS_SUCCESS;
203 int err;
205 __libc_lock_lock (lock);
207 if (result)
208 nis_freeresult (result);
209 result = NULL;
211 if (tablename_val == NULL)
212 status = _nss_create_tablename (&err);
214 __libc_lock_unlock (lock);
216 return status;
219 enum nss_status
220 _nss_nisplus_endhostent (void)
222 __libc_lock_lock (lock);
224 if (result)
225 nis_freeresult (result);
226 result = NULL;
228 __libc_lock_unlock (lock);
230 return NSS_STATUS_SUCCESS;
233 static enum nss_status
234 internal_nisplus_gethostent_r (struct hostent *host, char *buffer,
235 size_t buflen, int *errnop, int *herrnop)
237 int parse_res;
239 /* Get the next entry until we found a correct one. */
242 nis_result *saved_res;
244 if (result == NULL)
246 saved_res = NULL;
247 if (tablename_val == NULL)
249 enum nss_status status = _nss_create_tablename (errnop);
251 if (status != NSS_STATUS_SUCCESS)
252 return status;
255 result = nis_first_entry (tablename_val);
256 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
258 enum nss_status retval = niserr2nss (result->status);
259 if (retval == NSS_STATUS_TRYAGAIN)
261 *herrnop = NETDB_INTERNAL;
262 *errnop = errno;
264 return retval;
268 else
270 nis_result *res2;
272 saved_res = result;
273 res2 = nis_next_entry(tablename_val, &result->cookie);
274 result = res2;
275 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
277 enum nss_status retval= niserr2nss (result->status);
279 nis_freeresult (result);
280 result = saved_res;
281 if (retval == NSS_STATUS_TRYAGAIN)
283 *herrnop = NETDB_INTERNAL;
284 *errnop = errno;
286 return retval;
290 if (_res.options & RES_USE_INET6)
291 parse_res = _nss_nisplus_parse_hostent (result, AF_INET6, host, buffer,
292 buflen, errnop, AI_V4MAPPED);
293 else
294 parse_res = _nss_nisplus_parse_hostent (result, AF_INET, host, buffer,
295 buflen, errnop, 0);
297 if (parse_res == -1)
299 nis_freeresult (result);
300 result = saved_res;
301 *herrnop = NETDB_INTERNAL;
302 *errnop = ERANGE;
303 return NSS_STATUS_TRYAGAIN;
305 if (saved_res != NULL)
306 nis_freeresult (saved_res);
308 } while (!parse_res);
310 return NSS_STATUS_SUCCESS;
313 enum nss_status
314 _nss_nisplus_gethostent_r (struct hostent *result, char *buffer,
315 size_t buflen, int *errnop, int *herrnop)
317 int status;
319 __libc_lock_lock (lock);
321 status = internal_nisplus_gethostent_r (result, buffer, buflen, errnop,
322 herrnop);
324 __libc_lock_unlock (lock);
326 return status;
329 static enum nss_status
330 internal_gethostbyname2_r (const char *name, int af, struct hostent *host,
331 char *buffer, size_t buflen, int *errnop,
332 int *herrnop, int flags)
334 int parse_res, retval;
336 if (tablename_val == NULL)
338 enum nss_status status = _nss_create_tablename (errnop);
340 if (status != NSS_STATUS_SUCCESS)
342 *herrnop = NETDB_INTERNAL;
343 return NSS_STATUS_UNAVAIL;
347 if (name == NULL)
349 *errnop = EINVAL;
350 *herrnop = NETDB_INTERNAL;
351 return NSS_STATUS_NOTFOUND;
353 else
355 nis_result *result;
356 char buf[strlen (name) + 255 + tablename_len];
358 /* Search at first in the alias list, and use the correct name
359 for the next search */
360 sprintf (buf, "[name=%s],%s", name, tablename_val);
361 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
363 /* If we do not find it, try it as original name. But if the
364 database is correct, we should find it in the first case, too */
365 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
366 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
367 || strcmp (result->objects.objects_val->EN_data.en_type,
368 "hosts_tbl") != 0
369 || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
370 sprintf (buf, "[cname=%s],%s", name, tablename_val);
371 else
372 sprintf (buf, "[cname=%s],%s", NISENTRYVAL(0, 0, result),
373 tablename_val);
375 nis_freeresult (result);
376 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
378 retval = niserr2nss (result->status);
379 if (retval != NSS_STATUS_SUCCESS)
381 if (retval == NSS_STATUS_TRYAGAIN)
383 *errnop = errno;
384 *herrnop = NETDB_INTERNAL;
386 nis_freeresult (result);
387 return retval;
390 parse_res = _nss_nisplus_parse_hostent (result, af, host, buffer,
391 buflen, errnop, flags);
393 nis_freeresult (result);
395 if (parse_res > 0)
396 return NSS_STATUS_SUCCESS;
398 *herrnop = NETDB_INTERNAL;
399 if (parse_res == -1)
401 *errnop = ERANGE;
402 return NSS_STATUS_TRYAGAIN;
404 else
405 return NSS_STATUS_NOTFOUND;
409 enum nss_status
410 _nss_nisplus_gethostbyname2_r (const char *name, int af, struct hostent *host,
411 char *buffer, size_t buflen, int *errnop,
412 int *herrnop)
414 return internal_gethostbyname2_r (name, af, host, buffer, buflen, errnop,
415 herrnop,
416 ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0));
419 #if 0
420 enum nss_status
421 _nss_nisplus_getipnodebyname_r (const char *name, int af, int flags,
422 struct hostent *result, char *buffer,
423 size_t buflen, int *errnop, int *herrnop)
425 return internal_gethostbyname2_r (name, af, result, buffer, buflen,
426 errnop, herrnop, flags);
428 #endif
430 enum nss_status
431 _nss_nisplus_gethostbyname_r (const char *name, struct hostent *host,
432 char *buffer, size_t buflen, int *errnop,
433 int *h_errnop)
435 if (_res.options & RES_USE_INET6)
437 enum nss_status status;
439 status = internal_gethostbyname2_r (name, AF_INET6, host, buffer,
440 buflen, errnop, h_errnop,
441 AI_V4MAPPED);
442 if (status == NSS_STATUS_SUCCESS)
443 return status;
446 return internal_gethostbyname2_r (name, AF_INET, host, buffer,
447 buflen, errnop, h_errnop, 0);
450 enum nss_status
451 _nss_nisplus_gethostbyaddr_r (const void *addr, socklen_t addrlen, int af,
452 struct hostent *host, char *buffer,
453 size_t buflen, int *errnop, int *herrnop)
455 if (tablename_val == NULL)
457 enum nss_status status = _nss_create_tablename (errnop);
459 if (status != NSS_STATUS_SUCCESS)
460 return status;
463 if (addr == NULL)
464 return NSS_STATUS_NOTFOUND;
465 else
467 nis_result *result;
468 char buf[255 + tablename_len];
469 int retval, parse_res;
471 sprintf (buf, "[addr=%s],%s",
472 inet_ntoa (*(const struct in_addr *) addr), tablename_val);
473 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
475 retval = niserr2nss (result->status);
476 if (retval != NSS_STATUS_SUCCESS)
478 if (retval == NSS_STATUS_TRYAGAIN)
480 *errnop = errno;
481 *herrnop = NETDB_INTERNAL;
483 nis_freeresult (result);
484 return retval;
487 parse_res = _nss_nisplus_parse_hostent (result, af, host,
488 buffer, buflen, errnop,
489 ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0));
490 nis_freeresult (result);
492 if (parse_res > 0)
493 return NSS_STATUS_SUCCESS;
495 *herrnop = NETDB_INTERNAL;
496 if (parse_res == -1)
498 *errnop = ERANGE;
499 return NSS_STATUS_TRYAGAIN;
501 else
502 return NSS_STATUS_NOTFOUND;