* posix/execl.c: Fix last argument of memcpy. Reported by Brian
[glibc.git] / nis / nss_nisplus / nisplus-network.c
blob201c1547699e35fcf69bc2f9a11e62dc07a34550
1 /* Copyright (C) 1997, 1998, 2000, 2001 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 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 <stdint.h>
25 #include <string.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)
45 static int
46 _nss_nisplus_parse_netent (nis_result *result, struct netent *network,
47 char *buffer, size_t buflen, int *errnop)
49 char *first_unused = buffer;
50 size_t room_left = buflen;
51 unsigned int i;
52 char *p, *line;
54 if (result == NULL)
55 return 0;
57 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
58 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
59 || strcmp (result->objects.objects_val[0].EN_data.en_type,
60 "networks_tbl") != 0
61 || result->objects.objects_val[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 room_left -= (strlen (first_unused) +1);
77 first_unused += strlen (first_unused) +1;
78 network->n_addrtype = 0;
79 network->n_net = inet_network (NISENTRYVAL (0, 2, result));
80 p = first_unused;
82 line = p;
83 for (i = 0; i < result->objects.objects_len; ++i)
85 if (strcmp (NISENTRYVAL (i, 1, result), network->n_name) != 0)
87 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
88 goto no_more_room;
90 *p++ = ' ';
91 p = __stpncpy (p, NISENTRYVAL (i, 1, result),
92 NISENTRYLEN (i, 1, result));
93 *p = '\0';
94 room_left -= (NISENTRYLEN (i, 1, result) + 1);
97 *p++ = '\0';
98 first_unused = p;
100 /* Adjust the pointer so it is aligned for
101 storing pointers. */
102 first_unused += __alignof__ (char *) - 1;
103 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
104 network->n_aliases = (char **) first_unused;
105 if (room_left < 2 * sizeof (char *))
106 goto no_more_room;
107 room_left -= (2 * sizeof (char *));
108 network->n_aliases[0] = NULL;
110 i = 0;
111 while (*line != '\0')
113 /* Skip leading blanks. */
114 while (isspace (*line))
115 line++;
117 if (*line == '\0')
118 break;
120 if (room_left < sizeof (char *))
121 goto no_more_room;
123 room_left -= sizeof (char *);
124 network->n_aliases[i] = line;
126 while (*line != '\0' && *line != ' ')
127 ++line;
129 if (*line == ' ')
131 *line = '\0';
132 ++line;
133 ++i;
135 else
136 network->n_aliases[i+1] = NULL;
139 return 1;
142 static enum nss_status
143 _nss_create_tablename (int *errnop)
145 if (tablename_val == NULL)
147 char buf [40 + strlen (nis_local_directory ())];
148 char *p;
150 p = __stpcpy (buf, "networks.org_dir.");
151 p = __stpcpy (p, nis_local_directory ());
152 tablename_val = __strdup (buf);
153 if (tablename_val == NULL)
155 *errnop = errno;
156 return NSS_STATUS_TRYAGAIN;
158 tablename_len = strlen (tablename_val);
160 return NSS_STATUS_SUCCESS;
163 enum nss_status
164 _nss_nisplus_setnetent (int stayopen)
166 enum nss_status status = NSS_STATUS_SUCCESS;
167 int err;
169 __libc_lock_lock (lock);
171 if (result)
172 nis_freeresult (result);
173 result = NULL;
175 if (tablename_val == NULL)
176 status = _nss_create_tablename (&err);
178 __libc_lock_unlock (lock);
180 return status;
183 enum nss_status
184 _nss_nisplus_endnetent (void)
186 __libc_lock_lock (lock);
188 if (result)
189 nis_freeresult (result);
190 result = NULL;
192 __libc_lock_unlock (lock);
194 return NSS_STATUS_SUCCESS;
197 static enum nss_status
198 internal_nisplus_getnetent_r (struct netent *network, char *buffer,
199 size_t buflen, int *errnop, int *herrnop)
201 int parse_res;
203 /* Get the next entry until we found a correct one. */
206 nis_result *saved_res;
208 if (result == NULL)
210 saved_res = NULL;
212 if (tablename_val == NULL)
214 enum nss_status status = _nss_create_tablename (errnop);
216 if (status != NSS_STATUS_SUCCESS)
217 return status;
220 result = nis_first_entry (tablename_val);
221 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
223 int retval;
225 retval = niserr2nss (result->status);
226 nis_freeresult (result);
227 result = NULL;
228 if (retval == NSS_STATUS_TRYAGAIN)
230 *herrnop = NETDB_INTERNAL;
231 *errnop = errno;
232 return retval;
234 else
235 return retval;
238 else
240 nis_result *res;
242 res = nis_next_entry (tablename_val, &result->cookie);
243 saved_res = result;
244 result = res;
245 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
247 int retval;
249 retval = niserr2nss (result->status);
250 nis_freeresult (result);
251 result = saved_res;
252 if (retval == NSS_STATUS_TRYAGAIN)
254 *herrnop = NETDB_INTERNAL;
255 *errnop = errno;
257 return retval;
261 parse_res = _nss_nisplus_parse_netent (result, network, buffer,
262 buflen, errnop);
263 if (parse_res == -1)
265 *herrnop = NETDB_INTERNAL;
266 return NSS_STATUS_TRYAGAIN;
269 } while (!parse_res);
271 return NSS_STATUS_SUCCESS;
274 enum nss_status
275 _nss_nisplus_getnetent_r (struct netent *result, char *buffer,
276 size_t buflen, int *errnop, int *herrnop)
278 int status;
280 __libc_lock_lock (lock);
282 status = internal_nisplus_getnetent_r (result, buffer, buflen, errnop,
283 herrnop);
285 __libc_lock_unlock (lock);
287 return status;
290 enum nss_status
291 _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
292 char *buffer, size_t buflen, int *errnop,
293 int *herrnop)
295 int parse_res, retval;
297 if (tablename_val == NULL)
299 enum nss_status status = _nss_create_tablename (errnop);
301 if (status != NSS_STATUS_SUCCESS)
302 return status;
305 if (name == NULL)
307 *errnop = EINVAL;
308 *herrnop = NETDB_INTERNAL;
309 return NSS_STATUS_UNAVAIL;
311 else
313 nis_result *result;
314 char buf[strlen (name) + 255 + tablename_len];
316 /* Search at first in the alias list, and use the correct name
317 for the next search */
318 sprintf (buf, "[name=%s],%s", name, tablename_val);
319 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, 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 && result->status != NIS_S_SUCCESS)
324 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
325 || strcmp (result->objects.objects_val[0].EN_data.en_type,
326 "networks_tbl") != 0
327 || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3)
328 sprintf (buf, "[cname=%s],%s", name, tablename_val);
329 else
330 sprintf (buf, "[cname=%s],%s", NISENTRYVAL (0, 0, result),
331 tablename_val);
333 nis_freeresult (result);
334 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
336 retval = niserr2nss (result->status);
337 if (retval != NSS_STATUS_SUCCESS)
339 if (retval == NSS_STATUS_TRYAGAIN)
341 *errnop = errno;
342 *herrnop = NETDB_INTERNAL;
344 nis_freeresult (result);
345 return retval;
348 parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen,
349 errnop);
351 nis_freeresult (result);
353 if (parse_res > 0)
354 return NSS_STATUS_SUCCESS;
356 *herrnop = NETDB_INTERNAL;
357 if (parse_res == -1)
359 *errnop = ERANGE;
360 return NSS_STATUS_TRYAGAIN;
362 else
363 return NSS_STATUS_NOTFOUND;
367 /* XXX type is ignored, SUN's NIS+ table doesn't support it */
368 enum nss_status
369 _nss_nisplus_getnetbyaddr_r (uint32_t addr, const int type,
370 struct netent *network, char *buffer,
371 size_t buflen, int *errnop, int *herrnop)
373 if (tablename_val == NULL)
375 enum nss_status status = _nss_create_tablename (errnop);
377 if (status != NSS_STATUS_SUCCESS)
378 return status;
382 int parse_res, retval;
383 nis_result *result;
384 char buf[1024 + tablename_len];
385 struct in_addr in;
386 char buf2[256];
387 int b2len;
389 in = inet_makeaddr (addr, 0);
390 strcpy (buf2, inet_ntoa (in));
391 b2len = strlen (buf2);
393 while (1)
395 sprintf (buf, "[addr=%s],%s", buf2, tablename_val);
396 result = nis_list (buf, EXPAND_NAME, NULL, NULL);
398 retval = niserr2nss (result->status);
399 if (retval != NSS_STATUS_SUCCESS)
401 if (buf2[b2len -2] == '.' && buf2[b2len -1] == '0')
403 /* Try again, but with trailing dot(s)
404 removed (one by one) */
405 buf2[b2len - 2] = '\0';
406 b2len -= 2;
407 continue;
409 else
410 return NSS_STATUS_NOTFOUND;
412 if (retval == NSS_STATUS_TRYAGAIN)
414 *errnop = errno;
415 *herrnop = NETDB_INTERNAL;
417 nis_freeresult (result);
418 return retval;
421 parse_res = _nss_nisplus_parse_netent (result, network, buffer,
422 buflen, errnop);
424 nis_freeresult (result);
426 if (parse_res > 0)
427 return NSS_STATUS_SUCCESS;
429 *herrnop = NETDB_INTERNAL;
430 if (parse_res == -1)
432 *errnop = ERANGE;
433 return NSS_STATUS_TRYAGAIN;
435 else
436 return NSS_STATUS_NOTFOUND;