1 /* Copyright (C) 1997-2018 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.
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.
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.
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/>. */
24 #include <rpc/netdb.h>
25 #include <rpcsvc/nis.h>
26 #include <libc-lock.h>
28 #include "nss-nisplus.h"
30 __libc_lock_define_initialized (static, lock
)
32 static nis_result
*result
;
33 static nis_name tablename_val
;
34 static u_long tablename_len
;
36 #define NISENTRYVAL(idx, col, res) \
37 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
39 #define NISENTRYLEN(idx, col, res) \
40 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
44 _nss_nisplus_parse_rpcent (nis_result
*result
, struct rpcent
*rpc
,
45 char *buffer
, size_t buflen
, int *errnop
)
47 char *first_unused
= buffer
;
48 size_t room_left
= buflen
;
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
, "rpc_tbl") != 0
59 || NIS_RES_OBJECT (result
)[0].EN_data
.en_cols
.en_cols_len
< 3)
62 if (NISENTRYLEN (0, 0, result
) >= room_left
)
68 strncpy (first_unused
, NISENTRYVAL (0, 0, result
),
69 NISENTRYLEN (0, 0, result
));
70 first_unused
[NISENTRYLEN (0, 0, result
)] = '\0';
71 rpc
->r_name
= first_unused
;
72 size_t len
= strlen (first_unused
) + 1;
76 rpc
->r_number
= atoi (NISENTRYVAL (0, 2, result
));
78 /* XXX Rewrite at some point to allocate the array first and then
79 copy the strings. It wasteful to first concatenate the strings
80 to just split them again later. */
82 for (i
= 0; i
< NIS_RES_NUMOBJ (result
); ++i
)
84 if (strcmp (NISENTRYVAL (i
, 1, result
), rpc
->r_name
) != 0)
86 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
88 *first_unused
++ = ' ';
89 first_unused
= __stpncpy (first_unused
, NISENTRYVAL (i
, 1, result
),
90 NISENTRYLEN (i
, 1, result
));
91 room_left
-= NISENTRYLEN (i
, 1, result
) + 1;
94 *first_unused
++ = '\0';
96 /* Adjust the pointer so it is aligned for
98 size_t adjust
= ((__alignof__ (char *)
99 - (first_unused
- (char *) 0) % __alignof__ (char *))
100 % __alignof__ (char *));
101 if (room_left
< adjust
+ sizeof (char *))
103 first_unused
+= adjust
;
105 rpc
->r_aliases
= (char **) first_unused
;
107 /* For the terminating NULL pointer. */
108 room_left
-= sizeof (char *);
111 while (*line
!= '\0')
113 /* Skip leading blanks. */
114 while (isspace (*line
))
120 if (room_left
< sizeof (char *))
123 room_left
-= sizeof (char *);
124 rpc
->r_aliases
[i
++] = line
;
126 while (*line
!= '\0' && *line
!= ' ')
132 rpc
->r_aliases
[i
] = NULL
;
138 static enum nss_status
139 _nss_create_tablename (int *errnop
)
141 if (tablename_val
== NULL
)
143 const char *local_dir
= nis_local_directory ();
144 size_t local_dir_len
= strlen (local_dir
);
145 static const char prefix
[] = "rpc.org_dir.";
147 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
151 return NSS_STATUS_TRYAGAIN
;
154 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
156 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
158 atomic_write_barrier ();
163 return NSS_STATUS_SUCCESS
;
168 _nss_nisplus_setrpcent (int stayopen
)
170 enum nss_status status
= NSS_STATUS_SUCCESS
;
172 __libc_lock_lock (lock
);
176 nis_freeresult (result
);
180 if (tablename_val
== NULL
)
183 status
= _nss_create_tablename (&err
);
186 __libc_lock_unlock (lock
);
192 _nss_nisplus_endrpcent (void)
194 __libc_lock_lock (lock
);
198 nis_freeresult (result
);
202 __libc_lock_unlock (lock
);
204 return NSS_STATUS_SUCCESS
;
207 static enum nss_status
208 internal_nisplus_getrpcent_r (struct rpcent
*rpc
, char *buffer
,
209 size_t buflen
, int *errnop
)
213 /* Get the next entry until we found a correct one. */
216 nis_result
*saved_res
;
221 if (tablename_val
== NULL
)
223 enum nss_status status
= _nss_create_tablename (errnop
);
225 if (status
!= NSS_STATUS_SUCCESS
)
229 result
= nis_first_entry (tablename_val
);
233 return NSS_STATUS_TRYAGAIN
;
235 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
236 return niserr2nss (result
->status
);
241 result
= nis_next_entry (tablename_val
, &result
->cookie
);
245 return NSS_STATUS_TRYAGAIN
;
247 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
249 nis_freeresult (saved_res
);
250 return niserr2nss (result
->status
);
254 parse_res
= _nss_nisplus_parse_rpcent (result
, rpc
, buffer
,
258 nis_freeresult (result
);
261 return NSS_STATUS_TRYAGAIN
;
266 nis_freeresult (saved_res
);
271 return NSS_STATUS_SUCCESS
;
275 _nss_nisplus_getrpcent_r (struct rpcent
*result
, char *buffer
,
276 size_t buflen
, int *errnop
)
280 __libc_lock_lock (lock
);
282 status
= internal_nisplus_getrpcent_r (result
, buffer
, buflen
, errnop
);
284 __libc_lock_unlock (lock
);
290 _nss_nisplus_getrpcbyname_r (const char *name
, struct rpcent
*rpc
,
291 char *buffer
, size_t buflen
, int *errnop
)
295 if (tablename_val
== NULL
)
297 __libc_lock_lock (lock
);
299 enum nss_status status
= _nss_create_tablename (errnop
);
301 __libc_lock_unlock (lock
);
303 if (status
!= NSS_STATUS_SUCCESS
)
308 return NSS_STATUS_NOTFOUND
;
310 char buf
[strlen (name
) + 10 + tablename_len
];
313 /* Search at first in the alias list, and use the correct name
314 for the next search */
315 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
316 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
323 /* If we did not find it, try it as original name. But if the
324 database is correct, we should find it in the first case, too */
325 if ((result
->status
!= NIS_SUCCESS
326 && result
->status
!= NIS_S_SUCCESS
)
327 || __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
328 || strcmp (result
->objects
.objects_val
->EN_data
.en_type
,
330 || result
->objects
.objects_val
->EN_data
.en_cols
.en_cols_len
< 3)
331 snprintf (buf
, sizeof (buf
), "[cname=%s],%s", name
, tablename_val
);
334 /* We need to allocate a new buffer since there is no
335 guarantee the returned name has a length limit. */
336 const char *entryval
= NISENTRYVAL (0, 0, result
);
337 size_t buflen
= strlen (entryval
) + 10 + tablename_len
;
338 bufptr
= alloca (buflen
);
339 snprintf (bufptr
, buflen
, "[cname=%s],%s",
340 entryval
, tablename_val
);
343 nis_freeresult (result
);
344 result
= nis_list (bufptr
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
351 return NSS_STATUS_TRYAGAIN
;
354 if (__glibc_unlikely (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
))
356 enum nss_status status
= niserr2nss (result
->status
);
358 __set_errno (olderr
);
360 nis_freeresult (result
);
364 parse_res
= _nss_nisplus_parse_rpcent (result
, rpc
, buffer
, buflen
,
367 nis_freeresult (result
);
374 return NSS_STATUS_TRYAGAIN
;
377 __set_errno (olderr
);
378 return NSS_STATUS_NOTFOUND
;
381 return NSS_STATUS_SUCCESS
;
385 _nss_nisplus_getrpcbynumber_r (const int number
, struct rpcent
*rpc
,
386 char *buffer
, size_t buflen
, int *errnop
)
388 if (tablename_val
== NULL
)
390 __libc_lock_lock (lock
);
392 enum nss_status status
= _nss_create_tablename (errnop
);
394 __libc_lock_unlock (lock
);
396 if (status
!= NSS_STATUS_SUCCESS
)
400 char buf
[12 + 3 * sizeof (number
) + tablename_len
];
403 snprintf (buf
, sizeof (buf
), "[number=%d],%s", number
, tablename_val
);
405 nis_result
*result
= nis_list (buf
, FOLLOW_LINKS
| FOLLOW_PATH
| USE_DGRAM
,
411 return NSS_STATUS_TRYAGAIN
;
414 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
416 enum nss_status status
= niserr2nss (result
->status
);
418 __set_errno (olderr
);
420 nis_freeresult (result
);
424 int parse_res
= _nss_nisplus_parse_rpcent (result
, rpc
, buffer
, buflen
,
427 nis_freeresult (result
);
434 return NSS_STATUS_TRYAGAIN
;
438 __set_errno (olderr
);
439 return NSS_STATUS_NOTFOUND
;
443 return NSS_STATUS_SUCCESS
;