1 /* Copyright (C) 1997, 2001, 2002, 2003, 2005, 2006
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
27 #include <bits/libc-lock.h>
28 #include <rpcsvc/nis.h>
30 #include "nss-nisplus.h"
31 #include "nisplus-parser.h"
33 #include <nis_intern.h>
37 __libc_lock_define_initialized (static, lock
);
39 /* Connection information. */
40 static ib_request
*ibreq
;
41 static directory_obj
*dir
;
42 static dir_binding bptr
;
43 static char *tablepath
;
44 static char *tableptr
;
49 nis_name grp_tablename_val attribute_hidden
;
50 size_t grp_tablename_len attribute_hidden
;
53 _nss_grp_create_tablename (int *errnop
)
55 if (grp_tablename_val
== NULL
)
57 const char *local_dir
= nis_local_directory ();
58 size_t local_dir_len
= strlen (local_dir
);
59 static const char prefix
[] = "group.org_dir.";
61 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
65 return NSS_STATUS_TRYAGAIN
;
68 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
70 grp_tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
72 atomic_write_barrier ();
74 if (atomic_compare_and_exchange_bool_acq (&grp_tablename_val
, p
, NULL
))
76 /* Another thread already installed the value. */
78 grp_tablename_len
= strlen (grp_tablename_val
);
82 return NSS_STATUS_SUCCESS
;
87 internal_endgrent (void)
89 __nisbind_destroy (&bptr
);
90 memset (&bptr
, '\0', sizeof (bptr
));
92 nis_free_directory (dir
);
95 nis_free_request (ibreq
);
98 xdr_free ((xdrproc_t
) xdr_netobj
, (char *) &cursor
);
99 memset (&cursor
, '\0', sizeof (cursor
));
102 tableptr
= tablepath
= NULL
;
106 static enum nss_status
107 internal_setgrent (int *errnop
)
109 enum nss_status status
= NSS_STATUS_SUCCESS
;
111 if (grp_tablename_val
== NULL
)
112 status
= _nss_grp_create_tablename (errnop
);
114 if (status
== NSS_STATUS_SUCCESS
)
116 ibreq
= __create_ib_request (grp_tablename_val
, 0);
120 return NSS_STATUS_TRYAGAIN
;
123 nis_error retcode
= __prepare_niscall (grp_tablename_val
, &dir
, &bptr
, 0);
124 if (retcode
!= NIS_SUCCESS
)
126 nis_free_request (ibreq
);
128 status
= niserr2nss (retcode
);
137 _nss_nisplus_setgrent (int stayopen
)
139 enum nss_status status
;
141 __libc_lock_lock (lock
);
143 internal_endgrent ();
145 // XXX We need to be able to set errno. Pass in new parameter.
147 status
= internal_setgrent (&err
);
149 __libc_lock_unlock (lock
);
156 _nss_nisplus_endgrent (void)
158 __libc_lock_lock (lock
);
160 internal_endgrent ();
162 __libc_lock_unlock (lock
);
164 return NSS_STATUS_SUCCESS
;
168 static enum nss_status
169 internal_nisplus_getgrent_r (struct group
*gr
, 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_setgrent (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_grent (&result
, gr
,
246 buffer
, buflen
, errnop
);
247 if (__builtin_expect (parse_res
== -1, 0))
250 retval
= NSS_STATUS_TRYAGAIN
;
255 /* Free the old cursor. */
256 xdr_free ((xdrproc_t
) xdr_netobj
, (char *) &cursor
);
257 /* Remember the new one. */
258 cursor
.n_bytes
= result
.cookie
.n_bytes
;
259 cursor
.n_len
= result
.cookie
.n_len
;
260 /* Free the result structure. NB: we do not remove the cookie. */
261 result
.cookie
.n_bytes
= NULL
;
262 result
.cookie
.n_len
= 0;
264 xdr_free ((xdrproc_t
) _xdr_nis_result
, (char *) &result
);
265 memset (&result
, '\0', sizeof (result
));
273 _nss_nisplus_getgrent_r (struct group
*result
, char *buffer
, size_t buflen
,
278 __libc_lock_lock (lock
);
280 status
= internal_nisplus_getgrent_r (result
, buffer
, buflen
, errnop
);
282 __libc_lock_unlock (lock
);
288 _nss_nisplus_getgrnam_r (const char *name
, struct group
*gr
,
289 char *buffer
, size_t buflen
, int *errnop
)
293 if (grp_tablename_val
== NULL
)
295 enum nss_status status
= _nss_grp_create_tablename (errnop
);
297 if (status
!= NSS_STATUS_SUCCESS
)
304 return NSS_STATUS_NOTFOUND
;
308 char buf
[strlen (name
) + 9 + grp_tablename_len
];
311 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, grp_tablename_val
);
313 result
= nis_list (buf
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
318 return NSS_STATUS_TRYAGAIN
;
321 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
323 enum nss_status status
= niserr2nss (result
->status
);
325 nis_freeresult (result
);
329 parse_res
= _nss_nisplus_parse_grent (result
, gr
, buffer
, buflen
, errnop
);
330 nis_freeresult (result
);
331 if (__builtin_expect (parse_res
< 1, 0))
336 return NSS_STATUS_TRYAGAIN
;
340 __set_errno (olderr
);
341 return NSS_STATUS_NOTFOUND
;
345 return NSS_STATUS_SUCCESS
;
349 _nss_nisplus_getgrgid_r (const gid_t gid
, struct group
*gr
,
350 char *buffer
, size_t buflen
, int *errnop
)
352 if (grp_tablename_val
== NULL
)
354 enum nss_status status
= _nss_grp_create_tablename (errnop
);
356 if (status
!= NSS_STATUS_SUCCESS
)
362 char buf
[8 + 3 * sizeof (unsigned long int) + grp_tablename_len
];
365 snprintf (buf
, sizeof (buf
), "[gid=%lu],%s",
366 (unsigned long int) gid
, grp_tablename_val
);
368 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
373 return NSS_STATUS_TRYAGAIN
;
376 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
378 enum nss_status status
= niserr2nss (result
->status
);
380 __set_errno (olderr
);
382 nis_freeresult (result
);
386 parse_res
= _nss_nisplus_parse_grent (result
, gr
, buffer
, buflen
, errnop
);
388 nis_freeresult (result
);
389 if (__builtin_expect (parse_res
< 1, 0))
391 __set_errno (olderr
);
396 return NSS_STATUS_TRYAGAIN
;
399 return NSS_STATUS_NOTFOUND
;
402 return NSS_STATUS_SUCCESS
;