1 /* Copyright (C) 1997-2020 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 <https://www.gnu.org/licenses/>. */
25 #include <libc-lock.h>
26 #include <rpcsvc/nis.h>
28 #include "nss-nisplus.h"
29 #include "nisplus-parser.h"
31 #include <nis_intern.h>
35 __libc_lock_define_initialized (static, lock);
37 /* Connection information. */
38 static ib_request *ibreq;
39 static directory_obj *dir;
40 static dir_binding bptr;
41 static char *tablepath;
42 static char *tableptr;
47 nis_name grp_tablename_val attribute_hidden;
48 size_t grp_tablename_len attribute_hidden;
51 _nss_grp_create_tablename (int *errnop)
53 if (grp_tablename_val == NULL)
55 const char *local_dir = nis_local_directory ();
56 size_t local_dir_len = strlen (local_dir);
57 static const char prefix[] = "group.org_dir.";
59 char *p = malloc (sizeof (prefix) + local_dir_len);
63 return NSS_STATUS_TRYAGAIN;
66 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
68 grp_tablename_len = sizeof (prefix) - 1 + local_dir_len;
70 atomic_write_barrier ();
72 if (atomic_compare_and_exchange_bool_acq (&grp_tablename_val, p, NULL))
74 /* Another thread already installed the value. */
76 grp_tablename_len = strlen (grp_tablename_val);
80 return NSS_STATUS_SUCCESS;
85 internal_endgrent (void)
87 __nisbind_destroy (&bptr);
88 memset (&bptr, '\0', sizeof (bptr));
90 nis_free_directory (dir);
93 nis_free_request (ibreq);
96 xdr_free ((xdrproc_t) xdr_netobj, (char *) &cursor);
97 memset (&cursor, '\0', sizeof (cursor));
100 tableptr = tablepath = NULL;
104 static enum nss_status
105 internal_setgrent (int *errnop)
107 enum nss_status status = NSS_STATUS_SUCCESS;
109 if (grp_tablename_val == NULL)
110 status = _nss_grp_create_tablename (errnop);
112 if (status == NSS_STATUS_SUCCESS)
114 ibreq = __create_ib_request (grp_tablename_val, 0);
118 return NSS_STATUS_TRYAGAIN;
121 nis_error retcode = __prepare_niscall (grp_tablename_val, &dir, &bptr, 0);
122 if (retcode != NIS_SUCCESS)
124 nis_free_request (ibreq);
126 status = niserr2nss (retcode);
135 _nss_nisplus_setgrent (int stayopen)
137 enum nss_status status;
139 __libc_lock_lock (lock);
141 internal_endgrent ();
143 // XXX We need to be able to set errno. Pass in new parameter.
145 status = internal_setgrent (&err);
147 __libc_lock_unlock (lock);
154 _nss_nisplus_endgrent (void)
156 __libc_lock_lock (lock);
158 internal_endgrent ();
160 __libc_lock_unlock (lock);
162 return NSS_STATUS_SUCCESS;
166 static enum nss_status
167 internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen,
171 enum nss_status retval = NSS_STATUS_SUCCESS;
173 /* Get the next entry until we found a correct one. */
178 memset (&result, '\0', sizeof (result));
180 if (cursor.n_bytes == NULL)
184 retval = internal_setgrent (errnop);
185 if (retval != NSS_STATUS_SUCCESS)
189 status = __do_niscall3 (&bptr, NIS_IBFIRST,
190 (xdrproc_t) _xdr_ib_request,
192 (xdrproc_t) _xdr_nis_result,
198 ibreq->ibr_cookie.n_bytes = cursor.n_bytes;
199 ibreq->ibr_cookie.n_len = cursor.n_len;
201 status = __do_niscall3 (&bptr, NIS_IBNEXT,
202 (xdrproc_t) _xdr_ib_request,
204 (xdrproc_t) _xdr_nis_result,
208 ibreq->ibr_cookie.n_bytes = NULL;
209 ibreq->ibr_cookie.n_len = 0;
212 if (status != NIS_SUCCESS)
213 return niserr2nss (status);
215 if (NIS_RES_STATUS (&result) == NIS_NOTFOUND)
217 /* No more entries on this server. This means we have to go
218 to the next server on the path. */
219 status = __follow_path (&tablepath, &tableptr, ibreq, &bptr);
220 if (status != NIS_SUCCESS)
221 return niserr2nss (status);
223 directory_obj *newdir = NULL;
225 status = __prepare_niscall (ibreq->ibr_name, &newdir, &newbptr, 0);
226 if (status != NIS_SUCCESS)
227 return niserr2nss (status);
229 nis_free_directory (dir);
231 __nisbind_destroy (&bptr);
234 xdr_free ((xdrproc_t) xdr_netobj, (char *) &result.cookie);
235 result.cookie.n_bytes = NULL;
236 result.cookie.n_len = 0;
240 else if (NIS_RES_STATUS (&result) != NIS_SUCCESS)
241 return niserr2nss (NIS_RES_STATUS (&result));
243 parse_res = _nss_nisplus_parse_grent (&result, gr,
244 buffer, buflen, errnop);
245 if (__glibc_unlikely (parse_res == -1))
248 retval = NSS_STATUS_TRYAGAIN;
253 /* Free the old cursor. */
254 xdr_free ((xdrproc_t) xdr_netobj, (char *) &cursor);
255 /* Remember the new one. */
256 cursor.n_bytes = result.cookie.n_bytes;
257 cursor.n_len = result.cookie.n_len;
258 /* Free the result structure. NB: we do not remove the cookie. */
259 result.cookie.n_bytes = NULL;
260 result.cookie.n_len = 0;
262 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) &result);
263 memset (&result, '\0', sizeof (result));
271 _nss_nisplus_getgrent_r (struct group *result, char *buffer, size_t buflen,
276 __libc_lock_lock (lock);
278 status = internal_nisplus_getgrent_r (result, buffer, buflen, errnop);
280 __libc_lock_unlock (lock);
286 _nss_nisplus_getgrnam_r (const char *name, struct group *gr,
287 char *buffer, size_t buflen, int *errnop)
291 if (grp_tablename_val == NULL)
293 enum nss_status status = _nss_grp_create_tablename (errnop);
295 if (status != NSS_STATUS_SUCCESS)
302 return NSS_STATUS_NOTFOUND;
306 char buf[strlen (name) + 9 + grp_tablename_len];
309 snprintf (buf, sizeof (buf), "[name=%s],%s", name, grp_tablename_val);
311 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
316 return NSS_STATUS_TRYAGAIN;
319 if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
321 enum nss_status status = niserr2nss (result->status);
323 nis_freeresult (result);
327 parse_res = _nss_nisplus_parse_grent (result, gr, buffer, buflen, errnop);
328 nis_freeresult (result);
329 if (__glibc_unlikely (parse_res < 1))
334 return NSS_STATUS_TRYAGAIN;
338 __set_errno (olderr);
339 return NSS_STATUS_NOTFOUND;
343 return NSS_STATUS_SUCCESS;
347 _nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr,
348 char *buffer, size_t buflen, int *errnop)
350 if (grp_tablename_val == NULL)
352 enum nss_status status = _nss_grp_create_tablename (errnop);
354 if (status != NSS_STATUS_SUCCESS)
360 char buf[8 + 3 * sizeof (unsigned long int) + grp_tablename_len];
363 snprintf (buf, sizeof (buf), "[gid=%lu],%s",
364 (unsigned long int) gid, grp_tablename_val);
366 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
371 return NSS_STATUS_TRYAGAIN;
374 if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
376 enum nss_status status = niserr2nss (result->status);
378 __set_errno (olderr);
380 nis_freeresult (result);
384 parse_res = _nss_nisplus_parse_grent (result, gr, buffer, buflen, errnop);
386 nis_freeresult (result);
387 if (__glibc_unlikely (parse_res < 1))
389 __set_errno (olderr);
394 return NSS_STATUS_TRYAGAIN;
397 return NSS_STATUS_NOTFOUND;
400 return NSS_STATUS_SUCCESS;