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/>. */
24 #include <libc-lock.h>
25 #include <rpcsvc/nis.h>
27 #include "nss-nisplus.h"
28 #include "nisplus-parser.h"
30 #include <nis_intern.h>
34 __libc_lock_define_initialized (static, lock)
36 /* Connection information. */
37 static ib_request *ibreq;
38 static directory_obj *dir;
39 static dir_binding bptr;
40 static char *tablepath;
41 static char *tableptr;
46 nis_name pwd_tablename_val attribute_hidden;
47 size_t pwd_tablename_len attribute_hidden;
50 _nss_pwd_create_tablename (int *errnop)
52 if (pwd_tablename_val == NULL)
54 const char *local_dir = nis_local_directory ();
55 size_t local_dir_len = strlen (local_dir);
56 static const char prefix[] = "passwd.org_dir.";
58 char *p = malloc (sizeof (prefix) + local_dir_len);
62 return NSS_STATUS_TRYAGAIN;
65 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
67 pwd_tablename_len = sizeof (prefix) - 1 + local_dir_len;
69 atomic_write_barrier ();
71 if (atomic_compare_and_exchange_bool_acq (&pwd_tablename_val, p, NULL))
73 /* Another thread already installed the value. */
75 pwd_tablename_len = strlen (pwd_tablename_val);
79 return NSS_STATUS_SUCCESS;
84 internal_nisplus_endpwent (void)
86 __nisbind_destroy (&bptr);
87 memset (&bptr, '\0', sizeof (bptr));
89 nis_free_directory (dir);
92 nis_free_request (ibreq);
95 xdr_free ((xdrproc_t) xdr_netobj, (char *) &cursor);
96 memset (&cursor, '\0', sizeof (cursor));
99 tableptr = tablepath = NULL;
103 static enum nss_status
104 internal_nisplus_setpwent (int *errnop)
106 enum nss_status status = NSS_STATUS_SUCCESS;
108 if (pwd_tablename_val == NULL)
109 status = _nss_pwd_create_tablename (errnop);
111 if (status == NSS_STATUS_SUCCESS)
113 ibreq = __create_ib_request (pwd_tablename_val, 0);
117 return NSS_STATUS_TRYAGAIN;
120 nis_error retcode = __prepare_niscall (pwd_tablename_val, &dir,
122 if (retcode != NIS_SUCCESS)
124 nis_free_request (ibreq);
126 status = niserr2nss (retcode);
135 _nss_nisplus_setpwent (int stayopen)
137 enum nss_status status;
139 __libc_lock_lock (lock);
141 internal_nisplus_endpwent ();
143 // XXX We need to be able to set errno. Pass in new parameter.
145 status = internal_nisplus_setpwent (&err);
147 __libc_lock_unlock (lock);
154 _nss_nisplus_endpwent (void)
156 __libc_lock_lock (lock);
158 internal_nisplus_endpwent ();
160 __libc_lock_unlock (lock);
162 return NSS_STATUS_SUCCESS;
166 static enum nss_status
167 internal_nisplus_getpwent_r (struct passwd *pw, 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_nisplus_setpwent (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_pwent (&result, pw, buffer,
246 if (__glibc_unlikely (parse_res == -1))
249 retval = NSS_STATUS_TRYAGAIN;
254 /* Free the old cursor. */
255 xdr_free ((xdrproc_t) xdr_netobj, (char *) &cursor);
256 /* Remember the new one. */
257 cursor.n_bytes = result.cookie.n_bytes;
258 cursor.n_len = result.cookie.n_len;
259 /* Free the result structure. NB: we do not remove the cookie. */
260 result.cookie.n_bytes = NULL;
261 result.cookie.n_len = 0;
263 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) &result);
264 memset (&result, '\0', sizeof (result));
272 _nss_nisplus_getpwent_r (struct passwd *result, char *buffer, size_t buflen,
277 __libc_lock_lock (lock);
279 status = internal_nisplus_getpwent_r (result, buffer, buflen, errnop);
281 __libc_lock_unlock (lock);
287 _nss_nisplus_getpwnam_r (const char *name, struct passwd *pw,
288 char *buffer, size_t buflen, int *errnop)
292 if (pwd_tablename_val == NULL)
294 enum nss_status status = _nss_pwd_create_tablename (errnop);
296 if (status != NSS_STATUS_SUCCESS)
303 return NSS_STATUS_UNAVAIL;
307 char buf[strlen (name) + 9 + pwd_tablename_len];
310 snprintf (buf, sizeof (buf), "[name=%s],%s", name, pwd_tablename_val);
312 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM, NULL, NULL);
317 return NSS_STATUS_TRYAGAIN;
320 if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
322 enum nss_status status = niserr2nss (result->status);
324 __set_errno (olderr);
326 nis_freeresult (result);
330 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen, errnop);
332 nis_freeresult (result);
334 if (__glibc_unlikely (parse_res < 1))
339 return NSS_STATUS_TRYAGAIN;
343 __set_errno (olderr);
344 return NSS_STATUS_NOTFOUND;
348 return NSS_STATUS_SUCCESS;
352 _nss_nisplus_getpwuid_r (const uid_t uid, struct passwd *pw,
353 char *buffer, size_t buflen, int *errnop)
355 if (pwd_tablename_val == NULL)
357 enum nss_status status = _nss_pwd_create_tablename (errnop);
359 if (status != NSS_STATUS_SUCCESS)
365 char buf[8 + 3 * sizeof (unsigned long int) + pwd_tablename_len];
368 snprintf (buf, sizeof (buf), "[uid=%lu],%s",
369 (unsigned long int) uid, pwd_tablename_val);
371 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM, NULL, NULL);
376 return NSS_STATUS_TRYAGAIN;
379 if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
381 enum nss_status status = niserr2nss (result->status);
383 __set_errno (olderr);
385 nis_freeresult (result);
389 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen, errnop);
391 nis_freeresult (result);
393 if (__glibc_unlikely (parse_res < 1))
398 return NSS_STATUS_TRYAGAIN;
402 __set_errno (olderr);
403 return NSS_STATUS_NOTFOUND;
407 return NSS_STATUS_SUCCESS;