Update.
[glibc.git] / nis / nss_nisplus / nisplus-network.c
blobf152c49f11dd024292760671804761ad2c36f861
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 <arpa/inet.h>
26 #include <bits/libc-lock.h>
27 #include <rpcsvc/nis.h>
29 #include "nss-nisplus.h"
31 __libc_lock_define_initialized (static, lock)
33 static nis_result *result = NULL;
34 static nis_name tablename_val = NULL;
35 static u_long tablename_len = 0;
37 #define NISENTRYVAL(idx,col,res) \
38 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
40 #define NISENTRYLEN(idx,col,res) \
41 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
44 static int
45 _nss_nisplus_parse_netent (nis_result *result, struct netent *network,
46 char *buffer, size_t buflen, int *errnop)
48 char *first_unused = buffer;
49 size_t room_left = buflen;
50 unsigned int i;
51 char *p, *line;
53 if (result == NULL)
54 return 0;
56 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
57 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
58 || strcmp (result->objects.objects_val[0].EN_data.en_type,
59 "networks_tbl") != 0
60 || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3)
61 return 0;
63 if (NISENTRYLEN(0, 0, result) >= room_left)
65 /* The line is too long for our buffer. */
66 no_more_room:
67 *errnop = ERANGE;
68 return -1;
71 strncpy (first_unused, NISENTRYVAL(0, 0, result),
72 NISENTRYLEN (0, 0, result));
73 first_unused[NISENTRYLEN (0, 0, result)] = '\0';
74 network->n_name = first_unused;
75 room_left -= (strlen (first_unused) +1);
76 first_unused += strlen (first_unused) +1;
77 network->n_addrtype = 0;
78 network->n_net = inet_network (NISENTRYVAL (0, 2, result));
79 p = first_unused;
81 line = p;
82 for (i = 0; i < result->objects.objects_len; ++i)
84 if (strcmp (NISENTRYVAL (i, 1, result), network->n_name) != 0)
86 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
87 goto no_more_room;
89 *p++ = ' ';
90 p = __stpncpy (p, NISENTRYVAL (i, 1, result),
91 NISENTRYLEN (i, 1, result));
92 *p = '\0';
93 room_left -= (NISENTRYLEN (i, 1, result) + 1);
96 *p++ = '\0';
97 first_unused = p;
99 /* Adjust the pointer so it is aligned for
100 storing pointers. */
101 first_unused += __alignof__ (char *) - 1;
102 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
103 network->n_aliases = (char **) first_unused;
104 if (room_left < 2 * sizeof (char *))
105 goto no_more_room;
106 room_left -= (2 * sizeof (char *));
107 network->n_aliases[0] = NULL;
109 i = 0;
110 while (*line != '\0')
112 /* Skip leading blanks. */
113 while (isspace (*line))
114 line++;
116 if (*line == '\0')
117 break;
119 if (room_left < sizeof (char *))
120 goto no_more_room;
122 room_left -= sizeof (char *);
123 network->n_aliases[i] = line;
125 while (*line != '\0' && *line != ' ')
126 ++line;
128 if (*line == ' ')
130 *line = '\0';
131 ++line;
132 ++i;
134 else
135 network->n_aliases[i+1] = NULL;
138 return 1;
141 static enum nss_status
142 _nss_create_tablename (int *errnop)
144 if (tablename_val == NULL)
146 char buf [40 + strlen (nis_local_directory ())];
147 char *p;
149 p = __stpcpy (buf, "networks.org_dir.");
150 p = __stpcpy (p, nis_local_directory ());
151 tablename_val = __strdup (buf);
152 if (tablename_val == NULL)
154 *errnop = errno;
155 return NSS_STATUS_TRYAGAIN;
157 tablename_len = strlen (tablename_val);
159 return NSS_STATUS_SUCCESS;
162 enum nss_status
163 _nss_nisplus_setnetent (void)
165 enum nss_status status = NSS_STATUS_SUCCESS;
166 int err;
168 __libc_lock_lock (lock);
170 if (result)
171 nis_freeresult (result);
172 result = NULL;
174 if (tablename_val == NULL)
175 status = _nss_create_tablename (&err);
177 __libc_lock_unlock (lock);
179 return status;
182 enum nss_status
183 _nss_nisplus_endnetent (void)
185 __libc_lock_lock (lock);
187 if (result)
188 nis_freeresult (result);
189 result = NULL;
191 __libc_lock_unlock (lock);
193 return NSS_STATUS_SUCCESS;
196 static enum nss_status
197 internal_nisplus_getnetent_r (struct netent *network, char *buffer,
198 size_t buflen, int *errnop, int *herrnop)
200 int parse_res;
202 /* Get the next entry until we found a correct one. */
205 nis_result *saved_res;
207 if (result == NULL)
209 saved_res = NULL;
211 if (tablename_val == NULL)
213 enum nss_status status = _nss_create_tablename (errnop);
215 if (status != NSS_STATUS_SUCCESS)
216 return status;
219 result = nis_first_entry (tablename_val);
220 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
222 int retval;
224 retval = niserr2nss (result->status);
225 nis_freeresult (result);
226 result = NULL;
227 if (retval == NSS_STATUS_TRYAGAIN)
229 *herrnop = NETDB_INTERNAL;
230 *errnop = errno;
231 return retval;
233 else
234 return retval;
237 else
239 nis_result *res;
241 res = nis_next_entry (tablename_val, &result->cookie);
242 saved_res = result;
243 result = res;
244 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
246 int retval;
248 retval = niserr2nss (result->status);
249 nis_freeresult (result);
250 result = saved_res;
251 if (retval == NSS_STATUS_TRYAGAIN)
253 *herrnop = NETDB_INTERNAL;
254 *errnop = errno;
256 return retval;
260 parse_res = _nss_nisplus_parse_netent (result, network, buffer,
261 buflen, errnop);
262 if (parse_res == -1)
264 *herrnop = NETDB_INTERNAL;
265 return NSS_STATUS_TRYAGAIN;
268 } while (!parse_res);
270 return NSS_STATUS_SUCCESS;
273 enum nss_status
274 _nss_nisplus_getnetent_r (struct netent *result, char *buffer,
275 size_t buflen, int *errnop, int *herrnop)
277 int status;
279 __libc_lock_lock (lock);
281 status = internal_nisplus_getnetent_r (result, buffer, buflen, errnop,
282 herrnop);
284 __libc_lock_unlock (lock);
286 return status;
289 enum nss_status
290 _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
291 char *buffer, size_t buflen, int *errnop,
292 int *herrnop)
294 int parse_res, retval;
296 if (tablename_val == NULL)
298 enum nss_status status = _nss_create_tablename (errnop);
300 if (status != NSS_STATUS_SUCCESS)
301 return status;
304 if (name == NULL)
306 *errnop = EINVAL;
307 *herrnop = NETDB_INTERNAL;
308 return NSS_STATUS_UNAVAIL;
310 else
312 nis_result *result;
313 char buf[strlen (name) + 255 + tablename_len];
315 /* Search at first in the alias list, and use the correct name
316 for the next search */
317 sprintf (buf, "[name=%s],%s", name, tablename_val);
318 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
320 /* If we do not find it, try it as original name. But if the
321 database is correct, we should find it in the first case, too */
322 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
323 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
324 || strcmp (result->objects.objects_val[0].EN_data.en_type,
325 "networks_tbl") != 0
326 || result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 3)
327 sprintf (buf, "[cname=%s],%s", name, tablename_val);
328 else
329 sprintf (buf, "[cname=%s],%s", NISENTRYVAL (0, 0, result),
330 tablename_val);
332 nis_freeresult (result);
333 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
335 retval = niserr2nss (result->status);
336 if (retval != NSS_STATUS_SUCCESS)
338 if (retval == NSS_STATUS_TRYAGAIN)
340 *errnop = errno;
341 *herrnop = NETDB_INTERNAL;
343 nis_freeresult (result);
344 return retval;
347 parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen,
348 errnop);
350 nis_freeresult (result);
352 if (parse_res > 0)
353 return NSS_STATUS_SUCCESS;
355 *herrnop = NETDB_INTERNAL;
356 if (parse_res == -1)
358 *errnop = ERANGE;
359 return NSS_STATUS_TRYAGAIN;
361 else
362 return NSS_STATUS_NOTFOUND;
366 /* XXX type is ignored, SUN's NIS+ table doesn't support it */
367 enum nss_status
368 _nss_nisplus_getnetbyaddr_r (const unsigned long addr, const int type,
369 struct netent *network, char *buffer,
370 size_t buflen, int *errnop, int *herrnop)
372 if (tablename_val == NULL)
374 enum nss_status status = _nss_create_tablename (errnop);
376 if (status != NSS_STATUS_SUCCESS)
377 return status;
381 int parse_res, retval;
382 nis_result *result;
383 char buf[1024 + tablename_len];
384 struct in_addr in;
385 char buf2[256];
386 int b2len;
388 in = inet_makeaddr (addr, 0);
389 strcpy (buf2, inet_ntoa (in));
390 b2len = strlen (buf2);
392 while (1)
394 sprintf (buf, "[addr=%s],%s", buf2, tablename_val);
395 result = nis_list (buf, EXPAND_NAME, NULL, NULL);
397 retval = niserr2nss (result->status);
398 if (retval != NSS_STATUS_SUCCESS)
400 if (buf2[b2len -2] == '.' && buf2[b2len -1] == '0')
402 /* Try again, but with trailing dot(s)
403 removed (one by one) */
404 buf2[b2len - 2] = '\0';
405 b2len -= 2;
406 continue;
408 else
409 return NSS_STATUS_NOTFOUND;
411 if (retval == NSS_STATUS_TRYAGAIN)
413 *errnop = errno;
414 *herrnop = NETDB_INTERNAL;
416 nis_freeresult (result);
417 return retval;
420 parse_res = _nss_nisplus_parse_netent (result, network, buffer,
421 buflen, errnop);
423 nis_freeresult (result);
425 if (parse_res > 0)
426 return NSS_STATUS_SUCCESS;
428 *herrnop = NETDB_INTERNAL;
429 if (parse_res == -1)
431 *errnop = ERANGE;
432 return NSS_STATUS_TRYAGAIN;
434 else
435 return NSS_STATUS_NOTFOUND;