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