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
26 #include <bits/libc-lock.h>
27 #include <rpcsvc/nis.h>
29 #include "nss-nisplus.h"
30 #include "nisplus-parser.h"
32 #include <nis_intern.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
;
48 nis_name pwd_tablename_val attribute_hidden
;
49 size_t pwd_tablename_len attribute_hidden
;
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
);
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. */
77 pwd_tablename_len
= strlen (pwd_tablename_val
);
81 return NSS_STATUS_SUCCESS
;
86 internal_nisplus_endpwent (void)
88 __nisbind_destroy (&bptr
);
89 memset (&bptr
, '\0', sizeof (bptr
));
91 nis_free_directory (dir
);
94 nis_free_request (ibreq
);
97 xdr_free ((xdrproc_t
) xdr_netobj
, (char *) &cursor
);
98 memset (&cursor
, '\0', sizeof (cursor
));
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);
119 return NSS_STATUS_TRYAGAIN
;
122 nis_error retcode
= __prepare_niscall (pwd_tablename_val
, &dir
,
124 if (retcode
!= NIS_SUCCESS
)
126 nis_free_request (ibreq
);
128 status
= niserr2nss (retcode
);
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.
147 status
= internal_nisplus_setpwent (&err
);
149 __libc_lock_unlock (lock
);
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
,
173 enum nss_status retval
= NSS_STATUS_SUCCESS
;
175 /* Get the next entry until we found a correct one. */
180 memset (&result
, '\0', sizeof (result
));
182 if (cursor
.n_bytes
== NULL
)
186 retval
= internal_nisplus_setpwent (errnop
);
187 if (retval
!= NSS_STATUS_SUCCESS
)
191 status
= __do_niscall3 (&bptr
, NIS_IBFIRST
,
192 (xdrproc_t
) _xdr_ib_request
,
194 (xdrproc_t
) _xdr_nis_result
,
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
,
206 (xdrproc_t
) _xdr_nis_result
,
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
;
227 status
= __prepare_niscall (ibreq
->ibr_name
, &newdir
, &newbptr
, 0);
228 if (status
!= NIS_SUCCESS
)
229 return niserr2nss (status
);
231 nis_free_directory (dir
);
233 __nisbind_destroy (&bptr
);
236 xdr_free ((xdrproc_t
) xdr_netobj
, (char *) &result
.cookie
);
237 result
.cookie
.n_bytes
= NULL
;
238 result
.cookie
.n_len
= 0;
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
,
248 if (__builtin_expect (parse_res
== -1, 0))
251 retval
= NSS_STATUS_TRYAGAIN
;
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;
265 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) &result
);
266 memset (&result
, '\0', sizeof (result
));
274 _nss_nisplus_getpwent_r (struct passwd
*result
, char *buffer
, size_t buflen
,
279 __libc_lock_lock (lock
);
281 status
= internal_nisplus_getpwent_r (result
, buffer
, buflen
, errnop
);
283 __libc_lock_unlock (lock
);
289 _nss_nisplus_getpwnam_r (const char *name
, struct passwd
*pw
,
290 char *buffer
, size_t buflen
, int *errnop
)
294 if (pwd_tablename_val
== NULL
)
296 enum nss_status status
= _nss_pwd_create_tablename (errnop
);
298 if (status
!= NSS_STATUS_SUCCESS
)
305 return NSS_STATUS_UNAVAIL
;
309 char buf
[strlen (name
) + 9 + pwd_tablename_len
];
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
);
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
);
332 parse_res
= _nss_nisplus_parse_pwent (result
, pw
, buffer
, buflen
, errnop
);
334 nis_freeresult (result
);
336 if (__builtin_expect (parse_res
< 1, 0))
341 return NSS_STATUS_TRYAGAIN
;
345 __set_errno (olderr
);
346 return NSS_STATUS_NOTFOUND
;
350 return NSS_STATUS_SUCCESS
;
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
)
367 char buf
[8 + 3 * sizeof (unsigned long int) + pwd_tablename_len
];
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
);
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
);
391 parse_res
= _nss_nisplus_parse_pwent (result
, pw
, buffer
, buflen
, errnop
);
393 nis_freeresult (result
);
395 if (__builtin_expect (parse_res
< 1, 0))
400 return NSS_STATUS_TRYAGAIN
;
404 __set_errno (olderr
);
405 return NSS_STATUS_NOTFOUND
;
409 return NSS_STATUS_SUCCESS
;