1 /* Copyright (C) 1997, 1998, 2001, 2002, 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
26 #include <rpc/netdb.h>
27 #include <rpcsvc/nis.h>
28 #include <bits/libc-lock.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 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
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)
46 _nss_nisplus_parse_rpcent (nis_result
*result
, struct rpcent
*rpc
,
47 char *buffer
, size_t buflen
, int *errnop
)
49 char *first_unused
= buffer
;
50 size_t room_left
= buflen
;
58 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
)
59 || __type_of (NIS_RES_OBJECT (result
)) != NIS_ENTRY_OBJ
60 || strcmp (NIS_RES_OBJECT (result
)[0].EN_data
.en_type
, "rpc_tbl") != 0
61 || NIS_RES_OBJECT (result
)[0].EN_data
.en_cols
.en_cols_len
< 3)
64 if (NISENTRYLEN (0, 0, result
) >= room_left
)
70 strncpy (first_unused
, NISENTRYVAL (0, 0, result
),
71 NISENTRYLEN (0, 0, result
));
72 first_unused
[NISENTRYLEN (0, 0, result
)] = '\0';
73 rpc
->r_name
= first_unused
;
74 size_t len
= strlen (first_unused
) + 1;
78 rpc
->r_number
= atoi (NISENTRYVAL (0, 2, result
));
80 /* XXX Rewrite at some point to allocate the array first and then
81 copy the strings. It wasteful to first concatenate the strings
82 to just split them again later. */
84 for (i
= 0; i
< NIS_RES_NUMOBJ (result
); ++i
)
86 if (strcmp (NISENTRYVAL (i
, 1, result
), rpc
->r_name
) != 0)
88 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
90 *first_unused
++ = ' ';
91 first_unused
= __stpncpy (first_unused
, NISENTRYVAL (i
, 1, result
),
92 NISENTRYLEN (i
, 1, result
));
93 room_left
-= NISENTRYLEN (i
, 1, result
) + 1;
96 *first_unused
++ = '\0';
98 /* Adjust the pointer so it is aligned for
100 size_t adjust
= ((__alignof__ (char *)
101 - (first_unused
- (char *) 0) % __alignof__ (char *))
102 % __alignof__ (char *));
103 if (room_left
< adjust
+ sizeof (char *))
105 first_unused
+= adjust
;
107 rpc
->r_aliases
= (char **) first_unused
;
109 /* For the terminating NULL pointer. */
110 room_left
-= sizeof (char *);
113 while (*line
!= '\0')
115 /* Skip leading blanks. */
116 while (isspace (*line
))
122 if (room_left
< sizeof (char *))
125 room_left
-= sizeof (char *);
126 rpc
->r_aliases
[i
++] = line
;
128 while (*line
!= '\0' && *line
!= ' ')
134 rpc
->r_aliases
[i
] = NULL
;
140 static enum nss_status
141 _nss_create_tablename (int *errnop
)
143 if (tablename_val
== NULL
)
145 const char *local_dir
= nis_local_directory ();
146 size_t local_dir_len
= strlen (local_dir
);
147 static const char prefix
[] = "rpc.org_dir.";
149 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
153 return NSS_STATUS_TRYAGAIN
;
156 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
158 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
160 atomic_write_barrier ();
165 return NSS_STATUS_SUCCESS
;
170 _nss_nisplus_setrpcent (int stayopen
)
172 enum nss_status status
= NSS_STATUS_SUCCESS
;
174 __libc_lock_lock (lock
);
178 nis_freeresult (result
);
182 if (tablename_val
== NULL
)
185 status
= _nss_create_tablename (&err
);
188 __libc_lock_unlock (lock
);
194 _nss_nisplus_endrpcent (void)
196 __libc_lock_lock (lock
);
200 nis_freeresult (result
);
204 __libc_lock_unlock (lock
);
206 return NSS_STATUS_SUCCESS
;
209 static enum nss_status
210 internal_nisplus_getrpcent_r (struct rpcent
*rpc
, char *buffer
,
211 size_t buflen
, int *errnop
)
215 /* Get the next entry until we found a correct one. */
218 nis_result
*saved_res
;
223 if (tablename_val
== NULL
)
225 enum nss_status status
= _nss_create_tablename (errnop
);
227 if (status
!= NSS_STATUS_SUCCESS
)
231 result
= nis_first_entry (tablename_val
);
235 return NSS_STATUS_TRYAGAIN
;
237 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
238 return niserr2nss (result
->status
);
243 result
= nis_next_entry (tablename_val
, &result
->cookie
);
247 return NSS_STATUS_TRYAGAIN
;
249 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
251 nis_freeresult (saved_res
);
252 return niserr2nss (result
->status
);
256 parse_res
= _nss_nisplus_parse_rpcent (result
, rpc
, buffer
,
260 nis_freeresult (result
);
263 return NSS_STATUS_TRYAGAIN
;
268 nis_freeresult (saved_res
);
273 return NSS_STATUS_SUCCESS
;
277 _nss_nisplus_getrpcent_r (struct rpcent
*result
, char *buffer
,
278 size_t buflen
, int *errnop
)
282 __libc_lock_lock (lock
);
284 status
= internal_nisplus_getrpcent_r (result
, buffer
, buflen
, errnop
);
286 __libc_lock_unlock (lock
);
292 _nss_nisplus_getrpcbyname_r (const char *name
, struct rpcent
*rpc
,
293 char *buffer
, size_t buflen
, int *errnop
)
297 if (tablename_val
== NULL
)
299 __libc_lock_lock (lock
);
301 enum nss_status status
= _nss_create_tablename (errnop
);
303 __libc_lock_unlock (lock
);
305 if (status
!= NSS_STATUS_SUCCESS
)
310 return NSS_STATUS_NOTFOUND
;
312 char buf
[strlen (name
) + 10 + tablename_len
];
315 /* Search at first in the alias list, and use the correct name
316 for the next search */
317 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
318 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
325 /* If we did not find it, try it as original name. But if the
326 database is correct, we should find it in the first case, too */
327 if ((result
->status
!= NIS_SUCCESS
328 && result
->status
!= NIS_S_SUCCESS
)
329 || __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
330 || strcmp (result
->objects
.objects_val
->EN_data
.en_type
,
332 || result
->objects
.objects_val
->EN_data
.en_cols
.en_cols_len
< 3)
333 snprintf (buf
, sizeof (buf
), "[cname=%s],%s", name
, tablename_val
);
336 /* We need to allocate a new buffer since there is no
337 guarantee the returned name has a length limit. */
338 const char *entryval
= NISENTRYVAL (0, 0, result
);
339 size_t buflen
= strlen (entryval
) + 10 + tablename_len
;
340 bufptr
= alloca (buflen
);
341 snprintf (bufptr
, buflen
, "[cname=%s],%s",
342 entryval
, tablename_val
);
345 nis_freeresult (result
);
346 result
= nis_list (bufptr
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
353 return NSS_STATUS_TRYAGAIN
;
356 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
358 enum nss_status status
= niserr2nss (result
->status
);
360 __set_errno (olderr
);
362 nis_freeresult (result
);
366 parse_res
= _nss_nisplus_parse_rpcent (result
, rpc
, buffer
, buflen
,
369 nis_freeresult (result
);
376 return NSS_STATUS_TRYAGAIN
;
379 __set_errno (olderr
);
380 return NSS_STATUS_NOTFOUND
;
383 return NSS_STATUS_SUCCESS
;
387 _nss_nisplus_getrpcbynumber_r (const int number
, struct rpcent
*rpc
,
388 char *buffer
, size_t buflen
, int *errnop
)
390 if (tablename_val
== NULL
)
392 __libc_lock_lock (lock
);
394 enum nss_status status
= _nss_create_tablename (errnop
);
396 __libc_lock_unlock (lock
);
398 if (status
!= NSS_STATUS_SUCCESS
)
402 char buf
[12 + 3 * sizeof (number
) + tablename_len
];
405 snprintf (buf
, sizeof (buf
), "[number=%d],%s", number
, tablename_val
);
407 nis_result
*result
= nis_list (buf
, FOLLOW_LINKS
| FOLLOW_PATH
| USE_DGRAM
,
413 return NSS_STATUS_TRYAGAIN
;
416 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
418 enum nss_status status
= niserr2nss (result
->status
);
420 __set_errno (olderr
);
422 nis_freeresult (result
);
426 int parse_res
= _nss_nisplus_parse_rpcent (result
, rpc
, buffer
, buflen
,
429 nis_freeresult (result
);
436 return NSS_STATUS_TRYAGAIN
;
440 __set_errno (olderr
);
441 return NSS_STATUS_NOTFOUND
;
445 return NSS_STATUS_SUCCESS
;