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, see
18 <http://www.gnu.org/licenses/>. */
25 #include <rpc/netdb.h>
26 #include <rpcsvc/nis.h>
27 #include <bits/libc-lock.h>
29 #include "nss-nisplus.h"
31 __libc_lock_define_initialized (static, lock
)
33 static nis_result
*result
;
34 static nis_name tablename_val
;
35 static u_long tablename_len
;
37 #define NISENTRYVAL(idx, col, res) \
38 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
40 #define NISENTRYLEN(idx, col, res) \
41 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
45 _nss_nisplus_parse_rpcent (nis_result
*result
, struct rpcent
*rpc
,
46 char *buffer
, size_t buflen
, int *errnop
)
48 char *first_unused
= buffer
;
49 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
, "rpc_tbl") != 0
60 || NIS_RES_OBJECT (result
)[0].EN_data
.en_cols
.en_cols_len
< 3)
63 if (NISENTRYLEN (0, 0, result
) >= room_left
)
69 strncpy (first_unused
, NISENTRYVAL (0, 0, result
),
70 NISENTRYLEN (0, 0, result
));
71 first_unused
[NISENTRYLEN (0, 0, result
)] = '\0';
72 rpc
->r_name
= first_unused
;
73 size_t len
= strlen (first_unused
) + 1;
77 rpc
->r_number
= atoi (NISENTRYVAL (0, 2, result
));
79 /* XXX Rewrite at some point to allocate the array first and then
80 copy the strings. It wasteful to first concatenate the strings
81 to just split them again later. */
83 for (i
= 0; i
< NIS_RES_NUMOBJ (result
); ++i
)
85 if (strcmp (NISENTRYVAL (i
, 1, result
), rpc
->r_name
) != 0)
87 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
89 *first_unused
++ = ' ';
90 first_unused
= __stpncpy (first_unused
, NISENTRYVAL (i
, 1, result
),
91 NISENTRYLEN (i
, 1, result
));
92 room_left
-= NISENTRYLEN (i
, 1, result
) + 1;
95 *first_unused
++ = '\0';
97 /* Adjust the pointer so it is aligned for
99 size_t adjust
= ((__alignof__ (char *)
100 - (first_unused
- (char *) 0) % __alignof__ (char *))
101 % __alignof__ (char *));
102 if (room_left
< adjust
+ sizeof (char *))
104 first_unused
+= adjust
;
106 rpc
->r_aliases
= (char **) first_unused
;
108 /* For the terminating NULL pointer. */
109 room_left
-= sizeof (char *);
112 while (*line
!= '\0')
114 /* Skip leading blanks. */
115 while (isspace (*line
))
121 if (room_left
< sizeof (char *))
124 room_left
-= sizeof (char *);
125 rpc
->r_aliases
[i
++] = line
;
127 while (*line
!= '\0' && *line
!= ' ')
133 rpc
->r_aliases
[i
] = NULL
;
139 static enum nss_status
140 _nss_create_tablename (int *errnop
)
142 if (tablename_val
== NULL
)
144 const char *local_dir
= nis_local_directory ();
145 size_t local_dir_len
= strlen (local_dir
);
146 static const char prefix
[] = "rpc.org_dir.";
148 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
152 return NSS_STATUS_TRYAGAIN
;
155 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
157 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
159 atomic_write_barrier ();
164 return NSS_STATUS_SUCCESS
;
169 _nss_nisplus_setrpcent (int stayopen
)
171 enum nss_status status
= NSS_STATUS_SUCCESS
;
173 __libc_lock_lock (lock
);
177 nis_freeresult (result
);
181 if (tablename_val
== NULL
)
184 status
= _nss_create_tablename (&err
);
187 __libc_lock_unlock (lock
);
193 _nss_nisplus_endrpcent (void)
195 __libc_lock_lock (lock
);
199 nis_freeresult (result
);
203 __libc_lock_unlock (lock
);
205 return NSS_STATUS_SUCCESS
;
208 static enum nss_status
209 internal_nisplus_getrpcent_r (struct rpcent
*rpc
, char *buffer
,
210 size_t buflen
, int *errnop
)
214 /* Get the next entry until we found a correct one. */
217 nis_result
*saved_res
;
222 if (tablename_val
== NULL
)
224 enum nss_status status
= _nss_create_tablename (errnop
);
226 if (status
!= NSS_STATUS_SUCCESS
)
230 result
= nis_first_entry (tablename_val
);
234 return NSS_STATUS_TRYAGAIN
;
236 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
237 return niserr2nss (result
->status
);
242 result
= nis_next_entry (tablename_val
, &result
->cookie
);
246 return NSS_STATUS_TRYAGAIN
;
248 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
250 nis_freeresult (saved_res
);
251 return niserr2nss (result
->status
);
255 parse_res
= _nss_nisplus_parse_rpcent (result
, rpc
, buffer
,
259 nis_freeresult (result
);
262 return NSS_STATUS_TRYAGAIN
;
267 nis_freeresult (saved_res
);
272 return NSS_STATUS_SUCCESS
;
276 _nss_nisplus_getrpcent_r (struct rpcent
*result
, char *buffer
,
277 size_t buflen
, int *errnop
)
281 __libc_lock_lock (lock
);
283 status
= internal_nisplus_getrpcent_r (result
, buffer
, buflen
, errnop
);
285 __libc_lock_unlock (lock
);
291 _nss_nisplus_getrpcbyname_r (const char *name
, struct rpcent
*rpc
,
292 char *buffer
, size_t buflen
, int *errnop
)
296 if (tablename_val
== NULL
)
298 __libc_lock_lock (lock
);
300 enum nss_status status
= _nss_create_tablename (errnop
);
302 __libc_lock_unlock (lock
);
304 if (status
!= NSS_STATUS_SUCCESS
)
309 return NSS_STATUS_NOTFOUND
;
311 char buf
[strlen (name
) + 10 + tablename_len
];
314 /* Search at first in the alias list, and use the correct name
315 for the next search */
316 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
317 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
324 /* If we did not find it, try it as original name. But if the
325 database is correct, we should find it in the first case, too */
326 if ((result
->status
!= NIS_SUCCESS
327 && result
->status
!= NIS_S_SUCCESS
)
328 || __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
329 || strcmp (result
->objects
.objects_val
->EN_data
.en_type
,
331 || result
->objects
.objects_val
->EN_data
.en_cols
.en_cols_len
< 3)
332 snprintf (buf
, sizeof (buf
), "[cname=%s],%s", name
, tablename_val
);
335 /* We need to allocate a new buffer since there is no
336 guarantee the returned name has a length limit. */
337 const char *entryval
= NISENTRYVAL (0, 0, result
);
338 size_t buflen
= strlen (entryval
) + 10 + tablename_len
;
339 bufptr
= alloca (buflen
);
340 snprintf (bufptr
, buflen
, "[cname=%s],%s",
341 entryval
, tablename_val
);
344 nis_freeresult (result
);
345 result
= nis_list (bufptr
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
352 return NSS_STATUS_TRYAGAIN
;
355 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
357 enum nss_status status
= niserr2nss (result
->status
);
359 __set_errno (olderr
);
361 nis_freeresult (result
);
365 parse_res
= _nss_nisplus_parse_rpcent (result
, rpc
, buffer
, buflen
,
368 nis_freeresult (result
);
375 return NSS_STATUS_TRYAGAIN
;
378 __set_errno (olderr
);
379 return NSS_STATUS_NOTFOUND
;
382 return NSS_STATUS_SUCCESS
;
386 _nss_nisplus_getrpcbynumber_r (const int number
, struct rpcent
*rpc
,
387 char *buffer
, size_t buflen
, int *errnop
)
389 if (tablename_val
== NULL
)
391 __libc_lock_lock (lock
);
393 enum nss_status status
= _nss_create_tablename (errnop
);
395 __libc_lock_unlock (lock
);
397 if (status
!= NSS_STATUS_SUCCESS
)
401 char buf
[12 + 3 * sizeof (number
) + tablename_len
];
404 snprintf (buf
, sizeof (buf
), "[number=%d],%s", number
, tablename_val
);
406 nis_result
*result
= nis_list (buf
, FOLLOW_LINKS
| FOLLOW_PATH
| USE_DGRAM
,
412 return NSS_STATUS_TRYAGAIN
;
415 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
417 enum nss_status status
= niserr2nss (result
->status
);
419 __set_errno (olderr
);
421 nis_freeresult (result
);
425 int parse_res
= _nss_nisplus_parse_rpcent (result
, rpc
, buffer
, buflen
,
428 nis_freeresult (result
);
435 return NSS_STATUS_TRYAGAIN
;
439 __set_errno (olderr
);
440 return NSS_STATUS_NOTFOUND
;
444 return NSS_STATUS_SUCCESS
;