1 /* Copyright (C) 1997, 2001, 2002, 2003, 2005 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, write to the Free
17 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"
33 __libc_lock_define_initialized (static, lock
);
35 static nis_result
*result
;
36 static unsigned long next_entry
;
37 static nis_name tablename_val
;
38 static size_t tablename_len
;
40 static enum nss_status
41 _nss_create_tablename (int *errnop
)
43 if (tablename_val
== NULL
)
45 const char *local_dir
= nis_local_directory ();
46 size_t local_dir_len
= strlen (local_dir
);
47 static const char prefix
[] = "group.org_dir.";
49 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
50 if (tablename_val
== NULL
)
53 return NSS_STATUS_TRYAGAIN
;
56 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
58 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
60 atomic_write_barrier ();
65 return NSS_STATUS_SUCCESS
;
68 static enum nss_status
69 internal_setgrent (void)
71 enum nss_status status
;
75 nis_freeresult (result
);
80 if (tablename_val
== NULL
)
83 if (_nss_create_tablename (&err
) != NSS_STATUS_SUCCESS
)
84 return NSS_STATUS_UNAVAIL
;
87 result
= nis_list (tablename_val
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
90 status
= NSS_STATUS_TRYAGAIN
;
95 status
= niserr2nss (result
->status
);
96 if (status
!= NSS_STATUS_SUCCESS
)
98 nis_freeresult (result
);
106 _nss_nisplus_setgrent (int stayopen
)
108 enum nss_status status
;
110 __libc_lock_lock (lock
);
112 status
= internal_setgrent ();
114 __libc_lock_unlock (lock
);
120 _nss_nisplus_endgrent (void)
122 __libc_lock_lock (lock
);
126 nis_freeresult (result
);
130 __libc_lock_unlock (lock
);
132 return NSS_STATUS_SUCCESS
;
135 static enum nss_status
136 internal_nisplus_getgrent_r (struct group
*gr
, char *buffer
, size_t buflen
,
143 enum nss_status status
;
145 status
= internal_setgrent ();
146 if (result
== NULL
|| status
!= NSS_STATUS_SUCCESS
)
150 /* Get the next entry until we found a correct one. */
153 if (next_entry
>= result
->objects
.objects_len
)
154 return NSS_STATUS_NOTFOUND
;
156 parse_res
= _nss_nisplus_parse_grent (result
, next_entry
, gr
,
157 buffer
, buflen
, errnop
);
159 return NSS_STATUS_TRYAGAIN
;
165 return NSS_STATUS_SUCCESS
;
169 _nss_nisplus_getgrent_r (struct group
*result
, char *buffer
, size_t buflen
,
174 __libc_lock_lock (lock
);
176 status
= internal_nisplus_getgrent_r (result
, buffer
, buflen
, errnop
);
178 __libc_lock_unlock (lock
);
184 _nss_nisplus_getgrnam_r (const char *name
, struct group
*gr
,
185 char *buffer
, size_t buflen
, int *errnop
)
189 if (tablename_val
== NULL
)
191 __libc_lock_lock (lock
);
193 enum nss_status status
= _nss_create_tablename (errnop
);
195 __libc_lock_unlock (lock
);
197 if (status
!= NSS_STATUS_SUCCESS
)
204 return NSS_STATUS_NOTFOUND
;
208 char buf
[strlen (name
) + 9 + tablename_len
];
211 snprintf (buf
, sizeof (buf
), "[name=%s],%s", name
, tablename_val
);
213 result
= nis_list (buf
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
218 return NSS_STATUS_TRYAGAIN
;
221 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
223 enum nss_status status
= niserr2nss (result
->status
);
225 nis_freeresult (result
);
229 parse_res
= _nss_nisplus_parse_grent (result
, 0, gr
, buffer
, buflen
, errnop
);
230 nis_freeresult (result
);
231 if (__builtin_expect (parse_res
< 1, 0))
236 return NSS_STATUS_TRYAGAIN
;
240 __set_errno (olderr
);
241 return NSS_STATUS_NOTFOUND
;
245 return NSS_STATUS_SUCCESS
;
249 _nss_nisplus_getgrgid_r (const gid_t gid
, struct group
*gr
,
250 char *buffer
, size_t buflen
, int *errnop
)
252 if (tablename_val
== NULL
)
254 __libc_lock_lock (lock
);
256 enum nss_status status
= _nss_create_tablename (errnop
);
258 __libc_lock_unlock (lock
);
260 if (status
!= NSS_STATUS_SUCCESS
)
266 char buf
[8 + 3 * sizeof (unsigned long int) + tablename_len
];
269 snprintf (buf
, sizeof (buf
), "[gid=%lu],%s",
270 (unsigned long int) gid
, tablename_val
);
272 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
277 return NSS_STATUS_TRYAGAIN
;
280 if (__builtin_expect (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
, 0))
282 enum nss_status status
= niserr2nss (result
->status
);
284 __set_errno (olderr
);
286 nis_freeresult (result
);
290 parse_res
= _nss_nisplus_parse_grent (result
, 0, gr
, buffer
, buflen
, errnop
);
292 nis_freeresult (result
);
293 if (__builtin_expect (parse_res
< 1, 0))
295 __set_errno (olderr
);
300 return NSS_STATUS_TRYAGAIN
;
303 return NSS_STATUS_NOTFOUND
;
306 return NSS_STATUS_SUCCESS
;