Update.
[glibc.git] / nis / nss_nisplus / nisplus-hosts.c
blob375315b2edbd30b1c827429ace6d5eb387776d0a
1 /* Copyright (C) 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 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 = NULL;
35 static nis_name tablename_val = NULL;
36 static u_long tablename_len = 0;
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)
51 unsigned int i;
52 char *first_unused = buffer;
53 size_t room_left = buflen;
54 char *data, *p, *line;
56 if (result == NULL)
57 return 0;
59 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
60 __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
61 strcmp(result->objects.objects_val[0].EN_data.en_type,
62 "hosts_tbl") != 0 ||
63 result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 4)
64 return 0;
66 if (room_left < NISENTRYLEN (0, 2, result) + 1)
68 no_more_room:
69 *errnop = ERANGE;
70 return -1;
73 data = first_unused;
74 if (inet_pton (af, NISENTRYVAL (0, 2, result), data) < 1)
75 /* Illegal address: ignore line. */
76 return 0;
78 host->h_addrtype = af;
79 if (af == AF_INET6)
80 host->h_length = IN6ADDRSZ;
81 else
83 if (_res.options & RES_USE_INET6)
85 map_v4v6_address (data, data);
86 host->h_addrtype = AF_INET6;
87 host->h_length = IN6ADDRSZ;
89 else
91 host->h_addrtype = AF_INET;
92 host->h_length = INADDRSZ;
95 first_unused+=host->h_length;
96 room_left-=host->h_length;
98 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
99 goto no_more_room;
101 p = __stpncpy (first_unused, NISENTRYVAL (0, 0, result),
102 NISENTRYLEN (0, 0, result));
103 *p = '\0';
104 room_left -= (NISENTRYLEN (0, 0, result) + 1);
105 host->h_name = first_unused;
106 first_unused += NISENTRYLEN (0, 0, result) +1;
107 p = first_unused;
109 line = p;
110 for (i = 0; i < result->objects.objects_len; ++i)
112 if (strcmp (NISENTRYVAL (i, 1, result), host->h_name) != 0)
114 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
115 goto no_more_room;
117 *p++ = ' ';
118 p = __stpncpy (p, NISENTRYVAL (i, 1, result),
119 NISENTRYLEN (i, 1, result));
120 *p = '\0';
121 room_left -= (NISENTRYLEN (i, 1, result) + 1);
124 *p++ = '\0';
125 first_unused = p;
127 /* Adjust the pointer so it is aligned for
128 storing pointers. */
129 first_unused += __alignof__ (char *) - 1;
130 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
131 host->h_addr_list = (char **) first_unused;
132 if (room_left < 2 * sizeof (char *))
133 goto no_more_room;
135 room_left -= (2 * sizeof (char *));
136 host->h_addr_list[0] = data;
137 host->h_addr_list[1] = NULL;
138 host->h_aliases = &host->h_addr_list[2];
139 host->h_aliases[0] = NULL;
141 i = 0;
142 while (*line != '\0')
144 /* Skip leading blanks. */
145 while (isspace (*line))
146 ++line;
148 if (*line == '\0')
149 break;
151 if (room_left < sizeof (char *))
152 goto no_more_room;
154 room_left -= sizeof (char *);
155 host->h_aliases[i] = line;
157 while (*line != '\0' && *line != ' ')
158 ++line;
160 if (*line == ' ')
162 *line = '\0';
163 ++line;
164 ++i;
166 else
167 host->h_aliases[i+1] = NULL;
169 return 1;
172 static enum nss_status
173 _nss_create_tablename (int *errnop)
175 if (tablename_val == NULL)
177 char buf [40 + strlen (nis_local_directory ())];
178 char *p;
180 p = __stpcpy (buf, "hosts.org_dir.");
181 p = __stpcpy (p, nis_local_directory ());
182 tablename_val = __strdup (buf);
183 if (tablename_val == NULL)
185 *errnop = errno;
186 return NSS_STATUS_TRYAGAIN;
188 tablename_len = strlen (tablename_val);
190 return NSS_STATUS_SUCCESS;
193 enum nss_status
194 _nss_nisplus_sethostent (void)
196 enum nss_status status = NSS_STATUS_SUCCESS;
197 int err;
199 __libc_lock_lock (lock);
201 if (result)
202 nis_freeresult (result);
203 result = NULL;
205 if (tablename_val == NULL)
206 status = _nss_create_tablename (&err);
208 __libc_lock_unlock (lock);
210 return status;
213 enum nss_status
214 _nss_nisplus_endhostent (void)
216 __libc_lock_lock (lock);
218 if (result)
219 nis_freeresult (result);
220 result = NULL;
222 __libc_lock_unlock (lock);
224 return NSS_STATUS_SUCCESS;
227 static enum nss_status
228 internal_nisplus_gethostent_r (struct hostent *host, char *buffer,
229 size_t buflen, int *errnop, int *herrnop)
231 int parse_res;
233 /* Get the next entry until we found a correct one. */
236 nis_result *saved_res;
238 if (result == NULL)
240 saved_res = NULL;
241 if (tablename_val == NULL)
243 enum nss_status status = _nss_create_tablename (errnop);
245 if (status != NSS_STATUS_SUCCESS)
246 return status;
249 result = nis_first_entry (tablename_val);
250 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
252 enum nss_status retval = niserr2nss (result->status);
253 if (retval == NSS_STATUS_TRYAGAIN)
255 *herrnop = NETDB_INTERNAL;
256 *errnop = errno;
258 return retval;
262 else
264 nis_result *res2;
266 saved_res = result;
267 res2 = nis_next_entry(tablename_val, &result->cookie);
268 result = res2;
269 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
271 enum nss_status retval= niserr2nss (result->status);
273 nis_freeresult (result);
274 result = saved_res;
275 if (retval == NSS_STATUS_TRYAGAIN)
277 *herrnop = NETDB_INTERNAL;
278 *errnop = errno;
280 return retval;
284 parse_res = _nss_nisplus_parse_hostent (result, AF_INET6, host, buffer,
285 buflen, errnop);
286 if (parse_res < 1 && *errnop != ERANGE)
287 parse_res = _nss_nisplus_parse_hostent (result, AF_INET, host,
288 buffer, buflen, errnop);
289 if (parse_res == -1)
291 nis_freeresult (result);
292 result = saved_res;
293 *herrnop = NETDB_INTERNAL;
294 *errnop = ERANGE;
295 return NSS_STATUS_TRYAGAIN;
297 if (saved_res != NULL)
298 nis_freeresult (saved_res);
300 } while (!parse_res);
302 return NSS_STATUS_SUCCESS;
305 enum nss_status
306 _nss_nisplus_gethostent_r (struct hostent *result, char *buffer,
307 size_t buflen, int *errnop, int *herrnop)
309 int status;
311 __libc_lock_lock (lock);
313 status = internal_nisplus_gethostent_r (result, buffer, buflen, errnop,
314 herrnop);
316 __libc_lock_unlock (lock);
318 return status;
321 enum nss_status
322 _nss_nisplus_gethostbyname2_r (const char *name, int af, struct hostent *host,
323 char *buffer, size_t buflen, int *errnop,
324 int *herrnop)
326 int parse_res, retval;
328 if (tablename_val == NULL)
330 enum nss_status status = _nss_create_tablename (errnop);
332 if (status != NSS_STATUS_SUCCESS)
334 *herrnop = NETDB_INTERNAL;
335 return NSS_STATUS_UNAVAIL;
339 if (name == NULL)
341 *errnop = EINVAL;
342 *herrnop = NETDB_INTERNAL;
343 return NSS_STATUS_NOTFOUND;
345 else
347 nis_result *result;
348 char buf[strlen (name) + 255 + tablename_len];
350 /* Search at first in the alias list, and use the correct name
351 for the next search */
352 sprintf (buf, "[name=%s],%s", name, tablename_val);
353 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
355 /* If we do not find it, try it as original name. But if the
356 database is correct, we should find it in the first case, too */
357 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
358 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
359 || strcmp (result->objects.objects_val->EN_data.en_type,
360 "hosts_tbl") != 0
361 || result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
362 sprintf (buf, "[cname=%s],%s", name, tablename_val);
363 else
364 sprintf (buf, "[cname=%s],%s", NISENTRYVAL(0, 0, result),
365 tablename_val);
367 nis_freeresult (result);
368 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
370 retval = niserr2nss (result->status);
371 if (retval != NSS_STATUS_SUCCESS)
373 if (retval == NSS_STATUS_TRYAGAIN)
375 *errnop = errno;
376 *herrnop = NETDB_INTERNAL;
378 nis_freeresult (result);
379 return retval;
382 parse_res = _nss_nisplus_parse_hostent (result, af, host, buffer,
383 buflen, errnop);
385 nis_freeresult (result);
387 if (parse_res > 0)
388 return NSS_STATUS_SUCCESS;
390 *herrnop = NETDB_INTERNAL;
391 if (parse_res == -1)
393 *errnop = ERANGE;
394 return NSS_STATUS_TRYAGAIN;
396 else
397 return NSS_STATUS_NOTFOUND;
401 enum nss_status
402 _nss_nisplus_gethostbyname_r (const char *name, struct hostent *host,
403 char *buffer, size_t buflen, int *errnop,
404 int *h_errnop)
406 if (_res.options & RES_USE_INET6)
408 enum nss_status status;
410 status = _nss_nisplus_gethostbyname2_r (name, AF_INET6, host, buffer,
411 buflen, errnop, h_errnop);
412 if (status == NSS_STATUS_SUCCESS)
413 return status;
416 return _nss_nisplus_gethostbyname2_r (name, AF_INET, host, buffer,
417 buflen, errnop, h_errnop);
420 enum nss_status
421 _nss_nisplus_gethostbyaddr_r (const char *addr, int addrlen, int type,
422 struct hostent *host, char *buffer,
423 size_t buflen, int *errnop, int *herrnop)
425 if (tablename_val == NULL)
427 enum nss_status status = _nss_create_tablename (errnop);
429 if (status != NSS_STATUS_SUCCESS)
430 return status;
433 if (addr == NULL)
434 return NSS_STATUS_NOTFOUND;
435 else
437 nis_result *result;
438 char buf[255 + tablename_len];
439 int retval, parse_res;
441 sprintf (buf, "[addr=%s],%s",
442 inet_ntoa (*(struct in_addr *) addr), tablename_val);
443 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
445 retval = niserr2nss (result->status);
446 if (retval != NSS_STATUS_SUCCESS)
448 if (retval == NSS_STATUS_TRYAGAIN)
450 *errnop = errno;
451 *herrnop = NETDB_INTERNAL;
453 nis_freeresult (result);
454 return retval;
457 parse_res = _nss_nisplus_parse_hostent (result, type, host,
458 buffer, buflen, errnop);
459 nis_freeresult (result);
461 if (parse_res > 0)
462 return NSS_STATUS_SUCCESS;
464 *herrnop = NETDB_INTERNAL;
465 if (parse_res == -1)
467 *errnop = ERANGE;
468 return NSS_STATUS_TRYAGAIN;
470 else
471 return NSS_STATUS_NOTFOUND;