Remove posix/glob directory (standalone glob build support).
[glibc.git] / nis / nss_nisplus / nisplus-network.c
blob902826b62a2f68b0b7793ce0a86dddf361ffe0fa
1 /* Copyright (C) 1997,1998,2000-2003,2005,2006,2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <atomic.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <netdb.h>
25 #include <nss.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <arpa/inet.h>
29 #include <rpcsvc/nis.h>
30 #include <bits/libc-lock.h>
32 #include "nss-nisplus.h"
34 __libc_lock_define_initialized (static, lock)
36 static nis_result *result;
37 static nis_name tablename_val;
38 static u_long tablename_len;
40 #define NISENTRYVAL(idx, col, res) \
41 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
43 #define NISENTRYLEN(idx, col, res) \
44 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
47 static int
48 _nss_nisplus_parse_netent (nis_result *result, struct netent *network,
49 char *buffer, size_t buflen, int *errnop)
51 char *first_unused = buffer;
52 size_t room_left = buflen;
54 if (result == NULL)
55 return 0;
57 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
58 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
59 || strcmp (NIS_RES_OBJECT (result)[0].EN_data.en_type,
60 "networks_tbl") != 0
61 || NIS_RES_OBJECT (result)[0].EN_data.en_cols.en_cols_len < 3)
62 return 0;
64 if (NISENTRYLEN (0, 0, result) >= room_left)
66 /* The line is too long for our buffer. */
67 no_more_room:
68 *errnop = ERANGE;
69 return -1;
72 strncpy (first_unused, NISENTRYVAL (0, 0, result),
73 NISENTRYLEN (0, 0, result));
74 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
75 network->n_name = first_unused;
76 size_t len = strlen (first_unused) + 1;
77 room_left -= len;
78 first_unused += len;
80 network->n_addrtype = 0;
81 network->n_net = inet_network (NISENTRYVAL (0, 2, result));
83 /* XXX Rewrite at some point to allocate the array first and then
84 copy the strings. It wasteful to first concatenate the strings
85 to just split them again later. */
86 char *line = first_unused;
87 for (unsigned int i = 0; i < NIS_RES_NUMOBJ (result); ++i)
89 if (strcmp (NISENTRYVAL (i, 1, result), network->n_name) != 0)
91 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
92 goto no_more_room;
94 *first_unused++ = ' ';
95 first_unused = __stpncpy (first_unused, NISENTRYVAL (i, 1, result),
96 NISENTRYLEN (i, 1, result));
97 room_left -= (NISENTRYLEN (i, 1, result) + 1);
100 *first_unused++ = '\0';
102 /* Adjust the pointer so it is aligned for
103 storing pointers. */
104 size_t adjust = ((__alignof__ (char *)
105 - (first_unused - (char *) 0) % __alignof__ (char *))
106 % __alignof__ (char *));
107 if (room_left < adjust + sizeof (char *))
108 goto no_more_room;
109 first_unused += adjust;
110 room_left -= adjust;
111 network->n_aliases = (char **) first_unused;
113 /* For the terminating NULL pointer. */
114 room_left -= sizeof (char *);
116 unsigned int i = 0;
117 while (*line != '\0')
119 /* Skip leading blanks. */
120 while (isspace (*line))
121 ++line;
123 if (*line == '\0')
124 break;
126 if (room_left < sizeof (char *))
127 goto no_more_room;
129 room_left -= sizeof (char *);
130 network->n_aliases[i++] = line;
132 while (*line != '\0' && *line != ' ')
133 ++line;
135 if (*line == ' ')
136 *line++ = '\0';
138 network->n_aliases[i] = NULL;
140 return 1;
144 static enum nss_status
145 _nss_create_tablename (int *errnop)
147 if (tablename_val == NULL)
149 const char *local_dir = nis_local_directory ();
150 size_t local_dir_len = strlen (local_dir);
151 static const char prefix[] = "networks.org_dir.";
153 char *p = malloc (sizeof (prefix) + local_dir_len);
154 if (p == NULL)
156 *errnop = errno;
157 return NSS_STATUS_TRYAGAIN;
160 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
162 tablename_len = sizeof (prefix) - 1 + local_dir_len;
164 atomic_write_barrier ();
166 tablename_val = p;
169 return NSS_STATUS_SUCCESS;
172 enum nss_status
173 _nss_nisplus_setnetent (int stayopen)
175 enum nss_status status = NSS_STATUS_SUCCESS;
177 __libc_lock_lock (lock);
179 if (result != NULL)
181 nis_freeresult (result);
182 result = NULL;
185 if (tablename_val == NULL)
187 int err;
188 status = _nss_create_tablename (&err);
191 __libc_lock_unlock (lock);
193 return status;
196 enum nss_status
197 _nss_nisplus_endnetent (void)
199 __libc_lock_lock (lock);
201 if (result != NULL)
203 nis_freeresult (result);
204 result = NULL;
207 __libc_lock_unlock (lock);
209 return NSS_STATUS_SUCCESS;
212 static enum nss_status
213 internal_nisplus_getnetent_r (struct netent *network, char *buffer,
214 size_t buflen, int *errnop, int *herrnop)
216 int parse_res;
218 /* Get the next entry until we found a correct one. */
221 nis_result *saved_res;
223 if (result == NULL)
225 saved_res = NULL;
227 if (tablename_val == NULL)
229 enum nss_status status = _nss_create_tablename (errnop);
231 if (status != NSS_STATUS_SUCCESS)
232 return status;
235 result = nis_first_entry (tablename_val);
236 if (result == NULL)
238 *errnop = errno;
239 return NSS_STATUS_TRYAGAIN;
241 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
243 int retval = niserr2nss (result->status);
244 nis_freeresult (result);
245 result = NULL;
246 if (retval == NSS_STATUS_TRYAGAIN)
248 *herrnop = NETDB_INTERNAL;
249 *errnop = errno;
250 return retval;
252 else
253 return retval;
256 else
258 saved_res = result;
259 result = nis_next_entry (tablename_val, &result->cookie);
260 if (result == NULL)
262 *errnop = errno;
263 return NSS_STATUS_TRYAGAIN;
265 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
267 int retval = niserr2nss (result->status);
268 nis_freeresult (result);
269 result = saved_res;
270 if (retval == NSS_STATUS_TRYAGAIN)
272 *herrnop = NETDB_INTERNAL;
273 *errnop = errno;
275 return retval;
279 parse_res = _nss_nisplus_parse_netent (result, network, buffer,
280 buflen, errnop);
281 if (parse_res == -1)
283 *herrnop = NETDB_INTERNAL;
284 return NSS_STATUS_TRYAGAIN;
288 while (!parse_res);
290 return NSS_STATUS_SUCCESS;
293 enum nss_status
294 _nss_nisplus_getnetent_r (struct netent *result, char *buffer,
295 size_t buflen, int *errnop, int *herrnop)
297 int status;
299 __libc_lock_lock (lock);
301 status = internal_nisplus_getnetent_r (result, buffer, buflen, errnop,
302 herrnop);
304 __libc_lock_unlock (lock);
306 return status;
309 enum nss_status
310 _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
311 char *buffer, size_t buflen, int *errnop,
312 int *herrnop)
314 int parse_res, retval;
316 if (tablename_val == NULL)
318 __libc_lock_lock (lock);
320 enum nss_status status = _nss_create_tablename (errnop);
322 __libc_lock_unlock (lock);
324 if (status != NSS_STATUS_SUCCESS)
325 return status;
328 if (name == NULL)
330 *errnop = EINVAL;
331 *herrnop = NETDB_INTERNAL;
332 return NSS_STATUS_UNAVAIL;
335 nis_result *result;
336 char buf[strlen (name) + 10 + tablename_len];
337 int olderr = errno;
339 /* Search at first in the alias list, and use the correct name
340 for the next search */
341 snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
342 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH | USE_DGRAM, NULL, NULL);
344 if (result != NULL)
346 char *bufptr = buf;
348 /* If we do not find it, try it as original name. But if the
349 database is correct, we should find it in the first case, too */
350 if ((result->status != NIS_SUCCESS
351 && result->status != NIS_S_SUCCESS)
352 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
353 || strcmp (result->objects.objects_val[0].EN_data.en_type,
354 "networks_tbl") != 0
355 || (result->objects.objects_val[0].EN_data.en_cols.en_cols_len
356 < 3))
357 snprintf (buf, sizeof (buf), "[cname=%s],%s", name, tablename_val);
358 else
360 /* We need to allocate a new buffer since there is no
361 guarantee the returned name has a length limit. */
362 const char *entryval = NISENTRYVAL (0, 0, result);
363 size_t buflen = strlen (entryval) + 10 + tablename_len;
364 bufptr = alloca (buflen);
365 snprintf (bufptr, buflen, "[cname=%s],%s",
366 entryval, tablename_val);
369 nis_freeresult (result);
370 result = nis_list (bufptr, FOLLOW_LINKS | FOLLOW_PATH | USE_DGRAM,
371 NULL, NULL);
374 if (result == NULL)
376 __set_errno (ENOMEM);
377 return NSS_STATUS_TRYAGAIN;
380 retval = niserr2nss (result->status);
381 if (__builtin_expect (retval != NSS_STATUS_SUCCESS, 0))
383 if (retval == NSS_STATUS_TRYAGAIN)
385 *errnop = errno;
386 *herrnop = NETDB_INTERNAL;
388 else
389 __set_errno (olderr);
390 nis_freeresult (result);
391 return retval;
394 parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen,
395 errnop);
397 nis_freeresult (result);
399 if (parse_res > 0)
400 return NSS_STATUS_SUCCESS;
402 *herrnop = NETDB_INTERNAL;
403 if (parse_res == -1)
405 *errnop = ERANGE;
406 return NSS_STATUS_TRYAGAIN;
409 __set_errno (olderr);
410 return NSS_STATUS_NOTFOUND;
413 /* XXX type is ignored, SUN's NIS+ table doesn't support it */
414 enum nss_status
415 _nss_nisplus_getnetbyaddr_r (uint32_t addr, const int type,
416 struct netent *network, char *buffer,
417 size_t buflen, int *errnop, int *herrnop)
419 if (tablename_val == NULL)
421 __libc_lock_lock (lock);
423 enum nss_status status = _nss_create_tablename (errnop);
425 __libc_lock_unlock (lock);
427 if (status != NSS_STATUS_SUCCESS)
428 return status;
432 char buf[27 + tablename_len];
433 char buf2[18];
434 int olderr = errno;
436 struct in_addr in = { .s_addr = htonl (addr) };
437 strcpy (buf2, inet_ntoa (in));
438 size_t b2len = strlen (buf2);
440 while (1)
442 snprintf (buf, sizeof (buf), "[addr=%s],%s", buf2, tablename_val);
443 nis_result *result = nis_list (buf, EXPAND_NAME | USE_DGRAM,
444 NULL, NULL);
446 if (result == NULL)
448 __set_errno (ENOMEM);
449 return NSS_STATUS_TRYAGAIN;
451 enum nss_status retval = niserr2nss (result->status);
452 if (__builtin_expect (retval != NSS_STATUS_SUCCESS, 0))
454 if (b2len > 2 && buf2[b2len - 2] == '.' && buf2[b2len - 1] == '0')
456 /* Try again, but with trailing dot(s)
457 removed (one by one) */
458 buf2[b2len - 2] = '\0';
459 b2len -= 2;
460 nis_freeresult (result);
461 continue;
464 if (retval == NSS_STATUS_TRYAGAIN)
466 *errnop = errno;
467 *herrnop = NETDB_INTERNAL;
469 else
470 __set_errno (olderr);
471 nis_freeresult (result);
472 return retval;
475 int parse_res = _nss_nisplus_parse_netent (result, network, buffer,
476 buflen, errnop);
478 nis_freeresult (result);
480 if (parse_res > 0)
481 return NSS_STATUS_SUCCESS;
483 *herrnop = NETDB_INTERNAL;
484 if (parse_res == -1)
486 *errnop = ERANGE;
487 return NSS_STATUS_TRYAGAIN;
489 else
491 __set_errno (olderr);
492 return NSS_STATUS_NOTFOUND;