]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nisplus/nisplus-network.c
[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-network.c
1 /* Copyright (C) 1997,1998,2000-2003,2005,2006 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.
4
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.
9
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.
14
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. */
19
20 #include <atomic.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <netdb.h>
24 #include <nss.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <arpa/inet.h>
28 #include <rpcsvc/nis.h>
29 #include <bits/libc-lock.h>
30
31 #include "nss-nisplus.h"
32
33 __libc_lock_define_initialized (static, lock)
34
35 static nis_result *result;
36 static nis_name tablename_val;
37 static u_long tablename_len;
38
39 #define NISENTRYVAL(idx, col, res) \
40 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
41
42 #define NISENTRYLEN(idx, col, res) \
43 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
44
45
46 static int
47 _nss_nisplus_parse_netent (nis_result *result, struct netent *network,
48 char *buffer, size_t buflen, int *errnop)
49 {
50 char *first_unused = buffer;
51 size_t room_left = buflen;
52
53 if (result == NULL)
54 return 0;
55
56 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
57 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
58 || strcmp (NIS_RES_OBJECT (result)[0].EN_data.en_type,
59 "networks_tbl") != 0
60 || NIS_RES_OBJECT (result)[0].EN_data.en_cols.en_cols_len < 3)
61 return 0;
62
63 if (NISENTRYLEN (0, 0, result) >= room_left)
64 {
65 /* The line is too long for our buffer. */
66 no_more_room:
67 *errnop = ERANGE;
68 return -1;
69 }
70
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 size_t len = strlen (first_unused) + 1;
76 room_left -= len;
77 first_unused += len;
78
79 network->n_addrtype = 0;
80 network->n_net = inet_network (NISENTRYVAL (0, 2, result));
81
82 /* XXX Rewrite at some point to allocate the array first and then
83 copy the strings. It wasteful to first concatenate the strings
84 to just split them again later. */
85 char *line = first_unused;
86 for (unsigned int i = 0; i < NIS_RES_NUMOBJ (result); ++i)
87 {
88 if (strcmp (NISENTRYVAL (i, 1, result), network->n_name) != 0)
89 {
90 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
91 goto no_more_room;
92
93 *first_unused++ = ' ';
94 first_unused = __stpncpy (first_unused, NISENTRYVAL (i, 1, result),
95 NISENTRYLEN (i, 1, result));
96 room_left -= (NISENTRYLEN (i, 1, result) + 1);
97 }
98 }
99 *first_unused++ = '\0';
100
101 /* Adjust the pointer so it is aligned for
102 storing pointers. */
103 size_t adjust = ((__alignof__ (char *)
104 - (first_unused - (char *) 0) % __alignof__ (char *))
105 % __alignof__ (char *));
106 if (room_left < adjust + sizeof (char *))
107 goto no_more_room;
108 first_unused += adjust;
109 room_left -= adjust;
110 network->n_aliases = (char **) first_unused;
111
112 /* For the terminating NULL pointer. */
113 room_left -= sizeof (char *);
114
115 unsigned int i = 0;
116 while (*line != '\0')
117 {
118 /* Skip leading blanks. */
119 while (isspace (*line))
120 ++line;
121
122 if (*line == '\0')
123 break;
124
125 if (room_left < sizeof (char *))
126 goto no_more_room;
127
128 room_left -= sizeof (char *);
129 network->n_aliases[i++] = line;
130
131 while (*line != '\0' && *line != ' ')
132 ++line;
133
134 if (*line == ' ')
135 *line++ = '\0';
136 }
137 network->n_aliases[i] = NULL;
138
139 return 1;
140 }
141
142
143 static enum nss_status
144 _nss_create_tablename (int *errnop)
145 {
146 if (tablename_val == NULL)
147 {
148 const char *local_dir = nis_local_directory ();
149 size_t local_dir_len = strlen (local_dir);
150 static const char prefix[] = "networks.org_dir.";
151
152 char *p = malloc (sizeof (prefix) + local_dir_len);
153 if (p == NULL)
154 {
155 *errnop = errno;
156 return NSS_STATUS_TRYAGAIN;
157 }
158
159 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
160
161 tablename_len = sizeof (prefix) - 1 + local_dir_len;
162
163 atomic_write_barrier ();
164
165 tablename_val = p;
166 }
167
168 return NSS_STATUS_SUCCESS;
169 }
170
171 enum nss_status
172 _nss_nisplus_setnetent (int stayopen)
173 {
174 enum nss_status status = NSS_STATUS_SUCCESS;
175
176 __libc_lock_lock (lock);
177
178 if (result != NULL)
179 {
180 nis_freeresult (result);
181 result = NULL;
182 }
183
184 if (tablename_val == NULL)
185 {
186 int err;
187 status = _nss_create_tablename (&err);
188 }
189
190 __libc_lock_unlock (lock);
191
192 return status;
193 }
194
195 enum nss_status
196 _nss_nisplus_endnetent (void)
197 {
198 __libc_lock_lock (lock);
199
200 if (result != NULL)
201 {
202 nis_freeresult (result);
203 result = NULL;
204 }
205
206 __libc_lock_unlock (lock);
207
208 return NSS_STATUS_SUCCESS;
209 }
210
211 static enum nss_status
212 internal_nisplus_getnetent_r (struct netent *network, char *buffer,
213 size_t buflen, int *errnop, int *herrnop)
214 {
215 int parse_res;
216
217 /* Get the next entry until we found a correct one. */
218 do
219 {
220 nis_result *saved_res;
221
222 if (result == NULL)
223 {
224 saved_res = NULL;
225
226 if (tablename_val == NULL)
227 {
228 enum nss_status status = _nss_create_tablename (errnop);
229
230 if (status != NSS_STATUS_SUCCESS)
231 return status;
232 }
233
234 result = nis_first_entry (tablename_val);
235 if (result == NULL)
236 {
237 *errnop = errno;
238 return NSS_STATUS_TRYAGAIN;
239 }
240 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
241 {
242 int retval = niserr2nss (result->status);
243 nis_freeresult (result);
244 result = NULL;
245 if (retval == NSS_STATUS_TRYAGAIN)
246 {
247 *herrnop = NETDB_INTERNAL;
248 *errnop = errno;
249 return retval;
250 }
251 else
252 return retval;
253 }
254 }
255 else
256 {
257 saved_res = result;
258 result = nis_next_entry (tablename_val, &result->cookie);
259 if (result == NULL)
260 {
261 *errnop = errno;
262 return NSS_STATUS_TRYAGAIN;
263 }
264 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
265 {
266 int retval = niserr2nss (result->status);
267 nis_freeresult (result);
268 result = saved_res;
269 if (retval == NSS_STATUS_TRYAGAIN)
270 {
271 *herrnop = NETDB_INTERNAL;
272 *errnop = errno;
273 }
274 return retval;
275 }
276 }
277
278 parse_res = _nss_nisplus_parse_netent (result, network, buffer,
279 buflen, errnop);
280 if (parse_res == -1)
281 {
282 *herrnop = NETDB_INTERNAL;
283 return NSS_STATUS_TRYAGAIN;
284 }
285
286 }
287 while (!parse_res);
288
289 return NSS_STATUS_SUCCESS;
290 }
291
292 enum nss_status
293 _nss_nisplus_getnetent_r (struct netent *result, char *buffer,
294 size_t buflen, int *errnop, int *herrnop)
295 {
296 int status;
297
298 __libc_lock_lock (lock);
299
300 status = internal_nisplus_getnetent_r (result, buffer, buflen, errnop,
301 herrnop);
302
303 __libc_lock_unlock (lock);
304
305 return status;
306 }
307
308 enum nss_status
309 _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
310 char *buffer, size_t buflen, int *errnop,
311 int *herrnop)
312 {
313 int parse_res, retval;
314
315 if (tablename_val == NULL)
316 {
317 __libc_lock_lock (lock);
318
319 enum nss_status status = _nss_create_tablename (errnop);
320
321 __libc_lock_unlock (lock);
322
323 if (status != NSS_STATUS_SUCCESS)
324 return status;
325 }
326
327 if (name == NULL)
328 {
329 *errnop = EINVAL;
330 *herrnop = NETDB_INTERNAL;
331 return NSS_STATUS_UNAVAIL;
332 }
333
334 nis_result *result;
335 char buf[strlen (name) + 10 + tablename_len];
336 int olderr = errno;
337
338 /* Search at first in the alias list, and use the correct name
339 for the next search */
340 snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
341 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
342
343 if (result != NULL)
344 {
345 char *bufptr = buf;
346
347 /* If we do not find it, try it as original name. But if the
348 database is correct, we should find it in the first case, too */
349 if ((result->status != NIS_SUCCESS
350 && result->status != NIS_S_SUCCESS)
351 || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
352 || strcmp (result->objects.objects_val[0].EN_data.en_type,
353 "networks_tbl") != 0
354 || (result->objects.objects_val[0].EN_data.en_cols.en_cols_len
355 < 3))
356 snprintf (buf, sizeof (buf), "[cname=%s],%s", name, tablename_val);
357 else
358 {
359 /* We need to allocate a new buffer since there is no
360 guarantee the returned name has a length limit. */
361 const char *entryval = NISENTRYVAL (0, 0, result);
362 size_t buflen = strlen (entryval) + 10 + tablename_len;
363 bufptr = alloca (buflen);
364 snprintf (bufptr, buflen, "[cname=%s],%s",
365 entryval, tablename_val);
366 }
367
368 nis_freeresult (result);
369 result = nis_list (bufptr, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
370 }
371
372 if (result == NULL)
373 {
374 __set_errno (ENOMEM);
375 return NSS_STATUS_TRYAGAIN;
376 }
377
378 retval = niserr2nss (result->status);
379 if (__builtin_expect (retval != NSS_STATUS_SUCCESS, 0))
380 {
381 if (retval == NSS_STATUS_TRYAGAIN)
382 {
383 *errnop = errno;
384 *herrnop = NETDB_INTERNAL;
385 }
386 else
387 __set_errno (olderr);
388 nis_freeresult (result);
389 return retval;
390 }
391
392 parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen,
393 errnop);
394
395 nis_freeresult (result);
396
397 if (parse_res > 0)
398 return NSS_STATUS_SUCCESS;
399
400 *herrnop = NETDB_INTERNAL;
401 if (parse_res == -1)
402 {
403 *errnop = ERANGE;
404 return NSS_STATUS_TRYAGAIN;
405 }
406
407 __set_errno (olderr);
408 return NSS_STATUS_NOTFOUND;
409 }
410
411 /* XXX type is ignored, SUN's NIS+ table doesn't support it */
412 enum nss_status
413 _nss_nisplus_getnetbyaddr_r (uint32_t addr, const int type,
414 struct netent *network, char *buffer,
415 size_t buflen, int *errnop, int *herrnop)
416 {
417 if (tablename_val == NULL)
418 {
419 __libc_lock_lock (lock);
420
421 enum nss_status status = _nss_create_tablename (errnop);
422
423 __libc_lock_unlock (lock);
424
425 if (status != NSS_STATUS_SUCCESS)
426 return status;
427 }
428
429 {
430 char buf[27 + tablename_len];
431 char buf2[18];
432 int olderr = errno;
433
434 struct in_addr in = inet_makeaddr (addr, 0);
435 strcpy (buf2, inet_ntoa (in));
436 size_t b2len = strlen (buf2);
437
438 while (1)
439 {
440 snprintf (buf, sizeof (buf), "[addr=%s],%s", buf2, tablename_val);
441 nis_result *result = nis_list (buf, EXPAND_NAME, NULL, NULL);
442
443 if (result == NULL)
444 {
445 __set_errno (ENOMEM);
446 return NSS_STATUS_TRYAGAIN;
447 }
448 enum nss_status retval = niserr2nss (result->status);
449 if (__builtin_expect (retval != NSS_STATUS_SUCCESS, 0))
450 {
451 if (b2len > 2 && buf2[b2len - 2] == '.' && buf2[b2len - 1] == '0')
452 {
453 /* Try again, but with trailing dot(s)
454 removed (one by one) */
455 buf2[b2len - 2] = '\0';
456 b2len -= 2;
457 nis_freeresult (result);
458 continue;
459 }
460
461 if (retval == NSS_STATUS_TRYAGAIN)
462 {
463 *errnop = errno;
464 *herrnop = NETDB_INTERNAL;
465 }
466 else
467 __set_errno (olderr);
468 nis_freeresult (result);
469 return retval;
470 }
471
472 int parse_res = _nss_nisplus_parse_netent (result, network, buffer,
473 buflen, errnop);
474
475 nis_freeresult (result);
476
477 if (parse_res > 0)
478 return NSS_STATUS_SUCCESS;
479
480 *herrnop = NETDB_INTERNAL;
481 if (parse_res == -1)
482 {
483 *errnop = ERANGE;
484 return NSS_STATUS_TRYAGAIN;
485 }
486 else
487 {
488 __set_errno (olderr);
489 return NSS_STATUS_NOTFOUND;
490 }
491 }
492 }
493 }