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
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)
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
;
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
,
61 || NIS_RES_OBJECT (result
)[0].EN_data
.en_cols
.en_cols_len
< 3)
64 if (NISENTRYLEN (0, 0, result
) >= room_left
)
66 /* The line is too long for our buffer. */
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;
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
)
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
104 size_t adjust
= ((__alignof__ (char *)
105 - (first_unused
- (char *) 0) % __alignof__ (char *))
106 % __alignof__ (char *));
107 if (room_left
< adjust
+ sizeof (char *))
109 first_unused
+= adjust
;
111 network
->n_aliases
= (char **) first_unused
;
113 /* For the terminating NULL pointer. */
114 room_left
-= sizeof (char *);
117 while (*line
!= '\0')
119 /* Skip leading blanks. */
120 while (isspace (*line
))
126 if (room_left
< sizeof (char *))
129 room_left
-= sizeof (char *);
130 network
->n_aliases
[i
++] = line
;
132 while (*line
!= '\0' && *line
!= ' ')
138 network
->n_aliases
[i
] = NULL
;
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
);
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 ();
169 return NSS_STATUS_SUCCESS
;
173 _nss_nisplus_setnetent (int stayopen
)
175 enum nss_status status
= NSS_STATUS_SUCCESS
;
177 __libc_lock_lock (lock
);
181 nis_freeresult (result
);
185 if (tablename_val
== NULL
)
188 status
= _nss_create_tablename (&err
);
191 __libc_lock_unlock (lock
);
197 _nss_nisplus_endnetent (void)
199 __libc_lock_lock (lock
);
203 nis_freeresult (result
);
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
)
218 /* Get the next entry until we found a correct one. */
221 nis_result
*saved_res
;
227 if (tablename_val
== NULL
)
229 enum nss_status status
= _nss_create_tablename (errnop
);
231 if (status
!= NSS_STATUS_SUCCESS
)
235 result
= nis_first_entry (tablename_val
);
239 return NSS_STATUS_TRYAGAIN
;
241 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
243 int retval
= niserr2nss (result
->status
);
244 nis_freeresult (result
);
246 if (retval
== NSS_STATUS_TRYAGAIN
)
248 *herrnop
= NETDB_INTERNAL
;
259 result
= nis_next_entry (tablename_val
, &result
->cookie
);
263 return NSS_STATUS_TRYAGAIN
;
265 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
267 int retval
= niserr2nss (result
->status
);
268 nis_freeresult (result
);
270 if (retval
== NSS_STATUS_TRYAGAIN
)
272 *herrnop
= NETDB_INTERNAL
;
279 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
,
283 *herrnop
= NETDB_INTERNAL
;
284 return NSS_STATUS_TRYAGAIN
;
290 return NSS_STATUS_SUCCESS
;
294 _nss_nisplus_getnetent_r (struct netent
*result
, char *buffer
,
295 size_t buflen
, int *errnop
, int *herrnop
)
299 __libc_lock_lock (lock
);
301 status
= internal_nisplus_getnetent_r (result
, buffer
, buflen
, errnop
,
304 __libc_lock_unlock (lock
);
310 _nss_nisplus_getnetbyname_r (const char *name
, struct netent
*network
,
311 char *buffer
, size_t buflen
, int *errnop
,
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
)
331 *herrnop
= NETDB_INTERNAL
;
332 return NSS_STATUS_UNAVAIL
;
336 char buf
[strlen (name
) + 10 + tablename_len
];
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
);
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
,
355 || (result
->objects
.objects_val
[0].EN_data
.en_cols
.en_cols_len
357 snprintf (buf
, sizeof (buf
), "[cname=%s],%s", name
, tablename_val
);
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
,
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
)
386 *herrnop
= NETDB_INTERNAL
;
389 __set_errno (olderr
);
390 nis_freeresult (result
);
394 parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
, buflen
,
397 nis_freeresult (result
);
400 return NSS_STATUS_SUCCESS
;
402 *herrnop
= NETDB_INTERNAL
;
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 */
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
)
432 char buf
[27 + tablename_len
];
436 struct in_addr in
= { .s_addr
= htonl (addr
) };
437 strcpy (buf2
, inet_ntoa (in
));
438 size_t b2len
= strlen (buf2
);
442 snprintf (buf
, sizeof (buf
), "[addr=%s],%s", buf2
, tablename_val
);
443 nis_result
*result
= nis_list (buf
, EXPAND_NAME
| USE_DGRAM
,
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';
460 nis_freeresult (result
);
464 if (retval
== NSS_STATUS_TRYAGAIN
)
467 *herrnop
= NETDB_INTERNAL
;
470 __set_errno (olderr
);
471 nis_freeresult (result
);
475 int parse_res
= _nss_nisplus_parse_netent (result
, network
, buffer
,
478 nis_freeresult (result
);
481 return NSS_STATUS_SUCCESS
;
483 *herrnop
= NETDB_INTERNAL
;
487 return NSS_STATUS_TRYAGAIN
;
491 __set_errno (olderr
);
492 return NSS_STATUS_NOTFOUND
;