Remove duplicate entries.
[glibc/history.git] / nis / nss_nisplus / nisplus-pwd.c
blobcd33aebbcc25a19d2316e2cc3277e65f3ec3d54c
1 /* Copyright (C) 1997, 1999, 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
19 02111-1307 USA. */
21 #include <atomic.h>
22 #include <nss.h>
23 #include <errno.h>
24 #include <pwd.h>
25 #include <string.h>
26 #include <bits/libc-lock.h>
27 #include <rpcsvc/nis.h>
29 #include "nss-nisplus.h"
30 #include "nisplus-parser.h"
31 #include <libnsl.h>
32 #include <nis_intern.h>
33 #include <nis_xdr.h>
36 __libc_lock_define_initialized (static, lock)
38 /* Connection information. */
39 static ib_request *ibreq;
40 static directory_obj *dir;
41 static dir_binding bptr;
42 static char *tablepath;
43 static char *tableptr;
44 /* Cursor. */
45 static netobj cursor;
48 nis_name pwd_tablename_val attribute_hidden;
49 size_t pwd_tablename_len attribute_hidden;
51 enum nss_status
52 _nss_pwd_create_tablename (int *errnop)
54 if (pwd_tablename_val == NULL)
56 const char *local_dir = nis_local_directory ();
57 size_t local_dir_len = strlen (local_dir);
58 static const char prefix[] = "passwd.org_dir.";
60 char *p = malloc (sizeof (prefix) + local_dir_len);
61 if (p == NULL)
63 *errnop = errno;
64 return NSS_STATUS_TRYAGAIN;
67 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
69 pwd_tablename_len = sizeof (prefix) - 1 + local_dir_len;
71 atomic_write_barrier ();
73 if (atomic_compare_and_exchange_bool_acq (&pwd_tablename_val, p, NULL))
75 /* Another thread already installed the value. */
76 free (p);
77 pwd_tablename_len = strlen (pwd_tablename_val);
81 return NSS_STATUS_SUCCESS;
85 static void
86 internal_nisplus_endpwent (void)
88 __nisbind_destroy (&bptr);
89 memset (&bptr, '\0', sizeof (bptr));
91 nis_free_directory (dir);
92 dir = NULL;
94 nis_free_request (ibreq);
95 ibreq = NULL;
97 xdr_free ((xdrproc_t) xdr_netobj, (char *) &cursor);
98 memset (&cursor, '\0', sizeof (cursor));
100 free (tablepath);
101 tableptr = tablepath = NULL;
105 static enum nss_status
106 internal_nisplus_setpwent (int *errnop)
108 enum nss_status status = NSS_STATUS_SUCCESS;
110 if (pwd_tablename_val == NULL)
111 status = _nss_pwd_create_tablename (errnop);
113 if (status == NSS_STATUS_SUCCESS)
115 ibreq = __create_ib_request (pwd_tablename_val, 0);
116 if (ibreq == NULL)
118 *errnop = errno;
119 return NSS_STATUS_TRYAGAIN;
122 nis_error retcode = __prepare_niscall (pwd_tablename_val, &dir,
123 &bptr, 0);
124 if (retcode != NIS_SUCCESS)
126 nis_free_request (ibreq);
127 ibreq = NULL;
128 status = niserr2nss (retcode);
132 return status;
136 enum nss_status
137 _nss_nisplus_setpwent (int stayopen)
139 enum nss_status status;
141 __libc_lock_lock (lock);
143 internal_nisplus_endpwent ();
145 // XXX We need to be able to set errno. Pass in new parameter.
146 int err;
147 status = internal_nisplus_setpwent (&err);
149 __libc_lock_unlock (lock);
151 return status;
155 enum nss_status
156 _nss_nisplus_endpwent (void)
158 __libc_lock_lock (lock);
160 internal_nisplus_endpwent ();
162 __libc_lock_unlock (lock);
164 return NSS_STATUS_SUCCESS;
168 static enum nss_status
169 internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen,
170 int *errnop)
172 int parse_res = -1;
173 enum nss_status retval = NSS_STATUS_SUCCESS;
175 /* Get the next entry until we found a correct one. */
178 nis_error status;
179 nis_result result;
180 memset (&result, '\0', sizeof (result));
182 if (cursor.n_bytes == NULL)
184 if (ibreq == NULL)
186 retval = internal_nisplus_setpwent (errnop);
187 if (retval != NSS_STATUS_SUCCESS)
188 return retval;
191 status = __do_niscall3 (&bptr, NIS_IBFIRST,
192 (xdrproc_t) _xdr_ib_request,
193 (caddr_t) ibreq,
194 (xdrproc_t) _xdr_nis_result,
195 (caddr_t) &result,
196 0, NULL);
198 else
200 ibreq->ibr_cookie.n_bytes = cursor.n_bytes;
201 ibreq->ibr_cookie.n_len = cursor.n_len;
203 status = __do_niscall3 (&bptr, NIS_IBNEXT,
204 (xdrproc_t) _xdr_ib_request,
205 (caddr_t) ibreq,
206 (xdrproc_t) _xdr_nis_result,
207 (caddr_t) &result,
208 0, NULL);
210 ibreq->ibr_cookie.n_bytes = NULL;
211 ibreq->ibr_cookie.n_len = 0;
214 if (status != NIS_SUCCESS)
215 return niserr2nss (status);
217 if (NIS_RES_STATUS (&result) == NIS_NOTFOUND)
219 /* No more entries on this server. This means we have to go
220 to the next server on the path. */
221 status = __follow_path (&tablepath, &tableptr, ibreq, &bptr);
222 if (status != NIS_SUCCESS)
223 return niserr2nss (status);
225 directory_obj *newdir = NULL;
226 dir_binding newbptr;
227 status = __prepare_niscall (ibreq->ibr_name, &newdir, &newbptr, 0);
228 if (status != NIS_SUCCESS)
229 return niserr2nss (status);
231 nis_free_directory (dir);
232 dir = newdir;
233 __nisbind_destroy (&bptr);
234 bptr = newbptr;
236 xdr_free ((xdrproc_t) xdr_netobj, (char *) &result.cookie);
237 result.cookie.n_bytes = NULL;
238 result.cookie.n_len = 0;
239 parse_res = 0;
240 goto next;
242 else if (NIS_RES_STATUS (&result) != NIS_SUCCESS)
243 return niserr2nss (NIS_RES_STATUS (&result));
245 parse_res = _nss_nisplus_parse_pwent (&result, pw, buffer,
246 buflen, errnop);
248 if (__builtin_expect (parse_res == -1, 0))
250 *errnop = ERANGE;
251 retval = NSS_STATUS_TRYAGAIN;
252 goto freeres;
255 next:
256 /* Free the old cursor. */
257 xdr_free ((xdrproc_t) xdr_netobj, (char *) &cursor);
258 /* Remember the new one. */
259 cursor.n_bytes = result.cookie.n_bytes;
260 cursor.n_len = result.cookie.n_len;
261 /* Free the result structure. NB: we do not remove the cookie. */
262 result.cookie.n_bytes = NULL;
263 result.cookie.n_len = 0;
264 freeres:
265 xdr_free ((xdrproc_t) _xdr_nis_result, (char *) &result);
266 memset (&result, '\0', sizeof (result));
268 while (!parse_res);
270 return retval;
273 enum nss_status
274 _nss_nisplus_getpwent_r (struct passwd *result, char *buffer, size_t buflen,
275 int *errnop)
277 int status;
279 __libc_lock_lock (lock);
281 status = internal_nisplus_getpwent_r (result, buffer, buflen, errnop);
283 __libc_lock_unlock (lock);
285 return status;
288 enum nss_status
289 _nss_nisplus_getpwnam_r (const char *name, struct passwd *pw,
290 char *buffer, size_t buflen, int *errnop)
292 int parse_res;
294 if (pwd_tablename_val == NULL)
296 enum nss_status status = _nss_pwd_create_tablename (errnop);
298 if (status != NSS_STATUS_SUCCESS)
299 return status;
302 if (name == NULL)
304 *errnop = EINVAL;
305 return NSS_STATUS_UNAVAIL;
308 nis_result *result;
309 char buf[strlen (name) + 9 + pwd_tablename_len];
310 int olderr = errno;
312 snprintf (buf, sizeof (buf), "[name=%s],%s", name, pwd_tablename_val);
314 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM, NULL, NULL);
316 if (result == NULL)
318 *errnop = ENOMEM;
319 return NSS_STATUS_TRYAGAIN;
322 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
324 enum nss_status status = niserr2nss (result->status);
326 __set_errno (olderr);
328 nis_freeresult (result);
329 return status;
332 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen, errnop);
334 nis_freeresult (result);
336 if (__builtin_expect (parse_res < 1, 0))
338 if (parse_res == -1)
340 *errnop = ERANGE;
341 return NSS_STATUS_TRYAGAIN;
343 else
345 __set_errno (olderr);
346 return NSS_STATUS_NOTFOUND;
350 return NSS_STATUS_SUCCESS;
353 enum nss_status
354 _nss_nisplus_getpwuid_r (const uid_t uid, struct passwd *pw,
355 char *buffer, size_t buflen, int *errnop)
357 if (pwd_tablename_val == NULL)
359 enum nss_status status = _nss_pwd_create_tablename (errnop);
361 if (status != NSS_STATUS_SUCCESS)
362 return status;
365 int parse_res;
366 nis_result *result;
367 char buf[8 + 3 * sizeof (unsigned long int) + pwd_tablename_len];
368 int olderr = errno;
370 snprintf (buf, sizeof (buf), "[uid=%lu],%s",
371 (unsigned long int) uid, pwd_tablename_val);
373 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM, NULL, NULL);
375 if (result == NULL)
377 *errnop = ENOMEM;
378 return NSS_STATUS_TRYAGAIN;
381 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
383 enum nss_status status = niserr2nss (result->status);
385 __set_errno (olderr);
387 nis_freeresult (result);
388 return status;
391 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen, errnop);
393 nis_freeresult (result);
395 if (__builtin_expect (parse_res < 1, 0))
397 if (parse_res == -1)
399 *errnop = ERANGE;
400 return NSS_STATUS_TRYAGAIN;
402 else
404 __set_errno (olderr);
405 return NSS_STATUS_NOTFOUND;
409 return NSS_STATUS_SUCCESS;