Update.
[glibc.git] / nis / nss_nisplus / nisplus-hosts.c
blob0fdeb8f3f18f4ed8be8706ad3ce23fa793977f62
1 /* Copyright (C) 1997 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 <libc-lock.h>
28 #include <rpcsvc/nis.h>
29 #include <rpcsvc/nislib.h>
31 #include "nss-nisplus.h"
33 __libc_lock_define_initialized (static, lock)
35 static nis_result *result = NULL;
36 static nis_name *names = NULL;
38 #define NISENTRYVAL(idx,col,res) \
39 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.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)].zo_data.objdata_u.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)
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 result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ ||
61 strcmp(result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type,
62 "hosts_tbl") != 0 ||
63 result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 4)
64 return 0;
66 if (room_left < NISENTRYLEN (0, 2, result) + 1)
68 __set_errno (ERANGE);
69 return 0;
72 data = first_unused;
73 if (inet_pton (af, NISENTRYVAL (0, 2, result), data) < 1)
74 /* Illegal address: ignore line. */
75 return 0;
77 host->h_addrtype = af;
78 if (af == AF_INET6)
79 host->h_length = IN6ADDRSZ;
80 else
82 if (_res.options & RES_USE_INET6)
84 map_v4v6_address (data, data);
85 host->h_addrtype = AF_INET6;
86 host->h_length = IN6ADDRSZ;
88 else
90 host->h_addrtype = AF_INET;
91 host->h_length = INADDRSZ;
94 first_unused+=host->h_length;
95 room_left-=host->h_length;
97 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
99 __set_errno (ERANGE);
100 return 0;
102 p = stpncpy (first_unused, NISENTRYVAL (0, 0, result),
103 NISENTRYLEN (0, 0, result));
104 *p = '\0';
105 room_left -= (NISENTRYLEN (0, 0, result) + 1);
106 host->h_name = first_unused;
107 first_unused += NISENTRYLEN (0, 0, result) +1;
108 p = first_unused;
110 line = p;
111 for (i = 0; i < result->objects.objects_len; i++)
113 if (strcmp (NISENTRYVAL (i, 1, result), host->h_name) != 0)
115 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
117 __set_errno (ERANGE);
118 return 0;
120 p = stpcpy(p, " ");
121 p = stpncpy (p, NISENTRYVAL (i, 1, result),
122 NISENTRYLEN (i, 1, result));
123 *p = '\0';
124 room_left -= (NISENTRYLEN (i, 1, result) + 1);
127 ++p;
128 first_unused = p;
129 /* Adjust the pointer so it is aligned for
130 storing pointers. */
131 first_unused += __alignof__ (char *) - 1;
132 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
133 host->h_addr_list = (char **) first_unused;
134 if (room_left < 2 * sizeof (char *))
136 __set_errno (ERANGE);
137 return 0;
139 room_left -= (2 * sizeof (char *));
140 host->h_addr_list[0] = data;
141 host->h_addr_list[1] = NULL;
142 host->h_aliases = &host->h_addr_list[2];
143 host->h_aliases[0] = NULL;
145 i = 0;
146 while (*line != '\0')
148 /* Skip leading blanks. */
149 while (isspace (*line))
150 line++;
152 if (*line == '\0')
153 break;
155 if (room_left < sizeof (char *))
157 __set_errno (ERANGE);
158 return 0;
161 room_left -= sizeof (char *);
162 host->h_aliases[i] = line;
164 while (*line != '\0' && *line != ' ')
165 line++;
167 if (line != host->h_aliases[i])
169 *line = '\0';
170 line++;
171 i++;
175 return 1;
179 enum nss_status
180 _nss_nisplus_sethostent (void)
182 __libc_lock_lock (lock);
184 if (result)
185 nis_freeresult (result);
186 result = NULL;
187 if (names)
189 nis_freenames (names);
190 names = NULL;
193 __libc_lock_unlock (lock);
195 return NSS_STATUS_SUCCESS;
198 enum nss_status
199 _nss_nisplus_endhostent (void)
201 __libc_lock_lock (lock);
203 if (result)
204 nis_freeresult (result);
205 result = NULL;
206 if (names)
208 nis_freenames (names);
209 names = NULL;
212 __libc_lock_unlock (lock);
214 return NSS_STATUS_SUCCESS;
217 static enum nss_status
218 internal_nisplus_gethostent_r (struct hostent *host, char *buffer,
219 size_t buflen, int *herrnop)
221 int parse_res;
223 /* Get the next entry until we found a correct one. */
226 if (result == NULL)
228 names = nis_getnames("hosts.org_dir");
229 if (names == NULL || names[0] == NULL)
230 return NSS_STATUS_UNAVAIL;
232 result = nis_first_entry(names[0]);
233 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
235 int retval;
237 retval = niserr2nss (result->status);
238 if (retval == NSS_STATUS_TRYAGAIN)
240 *herrnop = NETDB_INTERNAL;
241 __set_errno (EAGAIN);
243 return retval;
247 else
249 nis_result *res2;
251 res2 = nis_next_entry(names[0], &result->cookie);
252 nis_freeresult (result);
253 result = res2;
254 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
256 int retval;
258 retval = niserr2nss (result->status);
259 if (retval == NSS_STATUS_TRYAGAIN)
261 *herrnop = NETDB_INTERNAL;
262 __set_errno (EAGAIN);
264 return retval;
268 parse_res = _nss_nisplus_parse_hostent (result, AF_INET6,
269 host, buffer, buflen);
270 if (!parse_res && errno != ERANGE)
271 parse_res = _nss_nisplus_parse_hostent (result, AF_INET, host,
272 buffer, buflen);
273 if (!parse_res && errno == ERANGE)
275 *herrnop = NETDB_INTERNAL;
276 return NSS_STATUS_TRYAGAIN;
279 } while (!parse_res);
281 return NSS_STATUS_SUCCESS;
284 enum nss_status
285 _nss_nisplus_gethostent_r (struct hostent *result, char *buffer,
286 size_t buflen, int *herrnop)
288 int status;
290 __libc_lock_lock (lock);
292 status = internal_nisplus_gethostent_r (result, buffer, buflen, herrnop);
294 __libc_lock_unlock (lock);
296 return status;
299 enum nss_status
300 _nss_nisplus_gethostbyname2_r (const char *name, int af, struct hostent *host,
301 char *buffer, size_t buflen, int *herrnop)
303 int parse_res, retval;
305 if (name == NULL)
307 __set_errno (EINVAL);
308 *herrnop = NETDB_INTERNAL;
309 return NSS_STATUS_NOTFOUND;
311 else
313 nis_result *result;
314 char buf[strlen (name) + 255];
316 /* Search at first in the alias list, and use the correct name
317 for the next search */
318 sprintf(buf, "[name=%s],hosts.org_dir", name);
319 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
321 /* If we do not find it, try it as original name. But if the
322 database is correct, we should find it in the first case, too */
323 if ((result->status != NIS_SUCCESS &&
324 result->status != NIS_S_SUCCESS) ||
325 result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ ||
326 strcmp(result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type,
327 "hosts_tbl") != 0 ||
328 result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len
329 < 3)
330 sprintf(buf, "[cname=%s],hosts.org_dir", name);
331 else
332 sprintf(buf, "[cname=%s],hosts.org_dir", NISENTRYVAL(0, 0, result));
334 nis_freeresult (result);
335 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
337 retval = niserr2nss (result->status);
338 if (retval != NSS_STATUS_SUCCESS)
340 if (retval == NSS_STATUS_TRYAGAIN)
342 __set_errno (EAGAIN);
343 *herrnop = NETDB_INTERNAL;
345 nis_freeresult (result);
346 return retval;
349 parse_res =
350 _nss_nisplus_parse_hostent (result, af, host, buffer, buflen);
352 nis_freeresult (result);
354 if (parse_res)
355 return NSS_STATUS_SUCCESS;
357 *herrnop = NETDB_INTERNAL;
358 if (!parse_res && errno == ERANGE)
359 return NSS_STATUS_TRYAGAIN;
360 else
361 return NSS_STATUS_NOTFOUND;
365 enum nss_status
366 _nss_nisplus_gethostbyname_r (const char *name, struct hostent *host,
367 char *buffer, size_t buflen, int *h_errnop)
369 if (_res.options & RES_USE_INET6)
371 enum nss_status status;
373 status = _nss_nisplus_gethostbyname2_r (name, AF_INET6, host, buffer,
374 buflen, h_errnop);
375 if (status == NSS_STATUS_SUCCESS)
376 return status;
379 return _nss_nisplus_gethostbyname2_r (name, AF_INET, host, buffer,
380 buflen, h_errnop);
383 enum nss_status
384 _nss_nisplus_gethostbyaddr_r (const char *addr, int addrlen, int type,
385 struct hostent *host, char *buffer,
386 size_t buflen, int *herrnop)
388 if (addr == NULL)
389 return NSS_STATUS_NOTFOUND;
390 else
392 nis_result *result;
393 char buf[1025];
394 int retval, parse_res;
396 snprintf(buf, sizeof (buf) -1, "[addr=%s],hosts.org_dir",
397 inet_ntoa (*(struct in_addr *)addr));
398 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
400 retval = niserr2nss (result->status);
401 if (retval != NSS_STATUS_SUCCESS)
403 if (retval == NSS_STATUS_TRYAGAIN)
405 __set_errno (EAGAIN);
406 *herrnop = NETDB_INTERNAL;
408 nis_freeresult (result);
409 return retval;
412 parse_res = _nss_nisplus_parse_hostent (result, type, host,
413 buffer, buflen);
415 nis_freeresult (result);
417 if (parse_res)
418 return NSS_STATUS_SUCCESS;
420 *herrnop = NETDB_INTERNAL;
421 if (!parse_res && errno == ERANGE)
422 return NSS_STATUS_TRYAGAIN;
423 else
424 return NSS_STATUS_NOTFOUND;