Update.
[glibc.git] / nis / nss_nisplus / nisplus-hosts.c
blobd1aa5ef78792771f2f2607a6cc73dd71b4e49823
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 <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)
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 __set_errno (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;
125 first_unused = p;
126 /* Adjust the pointer so it is aligned for
127 storing pointers. */
128 first_unused += __alignof__ (char *) - 1;
129 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
130 host->h_addr_list = (char **) first_unused;
131 if (room_left < 2 * sizeof (char *))
132 goto no_more_room;
134 room_left -= (2 * sizeof (char *));
135 host->h_addr_list[0] = data;
136 host->h_addr_list[1] = NULL;
137 host->h_aliases = &host->h_addr_list[2];
138 host->h_aliases[0] = NULL;
140 i = 0;
141 while (*line != '\0')
143 /* Skip leading blanks. */
144 while (isspace (*line))
145 line++;
147 if (*line == '\0')
148 break;
150 if (room_left < sizeof (char *))
151 goto no_more_room;
153 room_left -= sizeof (char *);
154 host->h_aliases[i] = line;
156 while (*line != '\0' && *line != ' ')
157 ++line;
159 if (*line == ' ')
161 *line = '\0';
162 ++line;
163 ++i;
165 else
166 host->h_aliases[i+1] = NULL;
168 return 1;
171 static enum nss_status
172 _nss_create_tablename (void)
174 if (tablename_val == NULL)
176 char buf [40 + strlen (nis_local_directory ())];
177 char *p;
179 p = __stpcpy (buf, "hosts.org_dir.");
180 p = __stpcpy (p, nis_local_directory ());
181 tablename_val = __strdup (buf);
182 if (tablename_val == NULL)
183 return NSS_STATUS_TRYAGAIN;
184 tablename_len = strlen (tablename_val);
186 return NSS_STATUS_SUCCESS;
189 enum nss_status
190 _nss_nisplus_sethostent (void)
192 enum nss_status status = NSS_STATUS_SUCCESS;
194 __libc_lock_lock (lock);
196 if (result)
197 nis_freeresult (result);
198 result = NULL;
200 if (tablename_val == NULL)
201 if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
202 status = NSS_STATUS_UNAVAIL;
204 __libc_lock_unlock (lock);
206 return status;
209 enum nss_status
210 _nss_nisplus_endhostent (void)
212 __libc_lock_lock (lock);
214 if (result)
215 nis_freeresult (result);
216 result = NULL;
218 __libc_lock_unlock (lock);
220 return NSS_STATUS_SUCCESS;
223 static enum nss_status
224 internal_nisplus_gethostent_r (struct hostent *host, char *buffer,
225 size_t buflen, int *herrnop)
227 int parse_res;
229 /* Get the next entry until we found a correct one. */
232 nis_result *saved_res;
234 if (result == NULL)
236 saved_res = NULL;
237 if (tablename_val == NULL)
238 if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
239 return NSS_STATUS_UNAVAIL;
241 result = nis_first_entry(tablename_val);
242 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
244 enum nss_status retval = niserr2nss (result->status);
245 if (retval == NSS_STATUS_TRYAGAIN)
247 *herrnop = NETDB_INTERNAL;
248 __set_errno (EAGAIN);
250 return retval;
254 else
256 nis_result *res2;
258 saved_res = result;
259 res2 = nis_next_entry(tablename_val, &result->cookie);
260 result = res2;
261 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
263 enum nss_status retval= niserr2nss (result->status);
265 nis_freeresult (result);
266 result = saved_res;
267 if (retval == NSS_STATUS_TRYAGAIN)
269 *herrnop = NETDB_INTERNAL;
270 __set_errno (EAGAIN);
272 return retval;
276 parse_res = _nss_nisplus_parse_hostent (result, AF_INET6,
277 host, buffer, buflen);
278 if (parse_res < 1 && errno != ERANGE)
279 parse_res = _nss_nisplus_parse_hostent (result, AF_INET, host,
280 buffer, buflen);
281 if (parse_res < 1 && errno == ERANGE)
283 nis_freeresult (result);
284 result = saved_res;
285 *herrnop = NETDB_INTERNAL;
286 return NSS_STATUS_TRYAGAIN;
288 if (saved_res != NULL)
289 nis_freeresult (saved_res);
291 } while (!parse_res);
293 return NSS_STATUS_SUCCESS;
296 enum nss_status
297 _nss_nisplus_gethostent_r (struct hostent *result, char *buffer,
298 size_t buflen, int *herrnop)
300 int status;
302 __libc_lock_lock (lock);
304 status = internal_nisplus_gethostent_r (result, buffer, buflen, herrnop);
306 __libc_lock_unlock (lock);
308 return status;
311 enum nss_status
312 _nss_nisplus_gethostbyname2_r (const char *name, int af, struct hostent *host,
313 char *buffer, size_t buflen, int *herrnop)
315 int parse_res, retval;
317 if (tablename_val == NULL)
318 if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
320 *herrnop = NETDB_INTERNAL;
321 return NSS_STATUS_UNAVAIL;
324 if (name == NULL)
326 __set_errno (EINVAL);
327 *herrnop = NETDB_INTERNAL;
328 return NSS_STATUS_NOTFOUND;
330 else
332 nis_result *result;
333 char buf[strlen (name) + 255 + tablename_len];
335 /* Search at first in the alias list, and use the correct name
336 for the next search */
337 sprintf(buf, "[name=%s],%s", name, tablename_val);
338 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
340 /* If we do not find it, try it as original name. But if the
341 database is correct, we should find it in the first case, too */
342 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
343 __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ ||
344 strcmp(result->objects.objects_val->EN_data.en_type,
345 "hosts_tbl") != 0 ||
346 result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
347 sprintf(buf, "[cname=%s],%s", name, tablename_val);
348 else
349 sprintf(buf, "[cname=%s],%s", NISENTRYVAL(0, 0, result),
350 tablename_val);
352 nis_freeresult (result);
353 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
355 retval = niserr2nss (result->status);
356 if (retval != NSS_STATUS_SUCCESS)
358 if (retval == NSS_STATUS_TRYAGAIN)
360 __set_errno (EAGAIN);
361 *herrnop = NETDB_INTERNAL;
363 nis_freeresult (result);
364 return retval;
367 parse_res =
368 _nss_nisplus_parse_hostent (result, af, host, buffer, buflen);
370 nis_freeresult (result);
372 if (parse_res > 0)
373 return NSS_STATUS_SUCCESS;
375 *herrnop = NETDB_INTERNAL;
376 if (parse_res == -1)
377 return NSS_STATUS_TRYAGAIN;
378 else
379 return NSS_STATUS_NOTFOUND;
383 enum nss_status
384 _nss_nisplus_gethostbyname_r (const char *name, struct hostent *host,
385 char *buffer, size_t buflen, int *h_errnop)
387 if (_res.options & RES_USE_INET6)
389 enum nss_status status;
391 status = _nss_nisplus_gethostbyname2_r (name, AF_INET6, host, buffer,
392 buflen, h_errnop);
393 if (status == NSS_STATUS_SUCCESS)
394 return status;
397 return _nss_nisplus_gethostbyname2_r (name, AF_INET, host, buffer,
398 buflen, h_errnop);
401 enum nss_status
402 _nss_nisplus_gethostbyaddr_r (const char *addr, int addrlen, int type,
403 struct hostent *host, char *buffer,
404 size_t buflen, int *herrnop)
406 if (tablename_val == NULL)
407 if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
408 return NSS_STATUS_UNAVAIL;
410 if (addr == NULL)
411 return NSS_STATUS_NOTFOUND;
412 else
414 nis_result *result;
415 char buf[255 + tablename_len];
416 int retval, parse_res;
418 sprintf (buf, "[addr=%s],%s",
419 inet_ntoa (*(struct in_addr *)addr), tablename_val);
420 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
422 retval = niserr2nss (result->status);
423 if (retval != NSS_STATUS_SUCCESS)
425 if (retval == NSS_STATUS_TRYAGAIN)
427 __set_errno (EAGAIN);
428 *herrnop = NETDB_INTERNAL;
430 nis_freeresult (result);
431 return retval;
434 parse_res = _nss_nisplus_parse_hostent (result, type, host,
435 buffer, buflen);
436 nis_freeresult (result);
438 if (parse_res > 0)
439 return NSS_STATUS_SUCCESS;
441 *herrnop = NETDB_INTERNAL;
442 if (parse_res == -1)
443 return NSS_STATUS_TRYAGAIN;
444 else
445 return NSS_STATUS_NOTFOUND;