1 /* Copyright (C) 1996-1999, 2001-2004, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
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
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
32 /* Get the declaration of the parser function. */
34 #define STRUCTURE group
36 #include <nss/nss_files/files-parse.c>
38 /* Protect global state against multiple changers */
39 __libc_lock_define_initialized (static, lock
)
41 static bool_t new_start
= 1;
44 static intern_t intern
;
48 internal_nis_endgrent (void)
58 struct response_t
*curr
= intern
.next
;
62 struct response_t
*last
= curr
;
67 intern
.next
= intern
.start
= NULL
;
72 _nss_nis_endgrent (void)
74 __libc_lock_lock (lock
);
76 internal_nis_endgrent ();
78 __libc_lock_unlock (lock
);
80 return NSS_STATUS_SUCCESS
;
85 internal_nis_setgrent (void)
87 /* We have to read all the data now. */
89 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
90 return NSS_STATUS_UNAVAIL
;
92 struct ypall_callback ypcb
;
94 ypcb
.foreach
= _nis_saveit
;
95 ypcb
.data
= (char *) &intern
;
96 enum nss_status status
= yperr2nss (yp_all (domain
, "group.byname", &ypcb
));
99 /* Mark the last buffer as full. */
100 if (intern
.next
!= NULL
)
101 intern
.next
->size
= intern
.offset
;
103 intern
.next
= intern
.start
;
111 _nss_nis_setgrent (int stayopen
)
113 enum nss_status result
= NSS_STATUS_SUCCESS
;
115 __libc_lock_lock (lock
);
117 internal_nis_endgrent ();
119 if (_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ
)
120 result
= internal_nis_setgrent ();
122 __libc_lock_unlock (lock
);
128 static enum nss_status
129 internal_nis_getgrent_r (struct group
*grp
, char *buffer
, size_t buflen
,
132 /* If we read the entire database at setpwent time we just iterate
133 over the data we have in memory. */
134 bool batch_read
= intern
.start
!= NULL
;
137 if (!batch_read
&& __builtin_expect (yp_get_default_domain (&domain
), 0))
138 return NSS_STATUS_UNAVAIL
;
140 /* Get the next entry until we found a correct one. */
151 struct response_t
*bucket
;
154 bucket
= intern
.next
;
156 if (__builtin_expect (intern
.offset
>= bucket
->size
, 0))
158 if (bucket
->next
== NULL
)
159 return NSS_STATUS_NOTFOUND
;
161 /* We look at all the content in the current bucket. Go on
163 bucket
= intern
.next
= bucket
->next
;
167 for (result
= &bucket
->mem
[intern
.offset
]; isspace (*result
);
171 len
= strlen (result
);
179 /* Maybe we should read the database in one piece. */
180 if ((_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ
)
181 && internal_nis_setgrent () == NSS_STATUS_SUCCESS
182 && intern
.start
!= NULL
)
185 goto handle_batch_read
;
188 yperr
= yp_first (domain
, "group.byname", &outkey
, &keylen
,
192 yperr
= yp_next (domain
, "group.byname", oldkey
, oldkeylen
,
193 &outkey
, &keylen
, &result
, &len
);
195 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
197 enum nss_status retval
= yperr2nss (yperr
);
199 if (retval
== NSS_STATUS_TRYAGAIN
)
205 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
209 return NSS_STATUS_TRYAGAIN
;
212 char *p
= strncpy (buffer
, result
, len
);
219 parse_res
= _nss_files_parse_grent (p
, grp
, (void *) buffer
, buflen
,
221 if (__builtin_expect (parse_res
== -1, 0))
226 return NSS_STATUS_TRYAGAIN
;
230 intern
.offset
+= len
+ 1;
239 while (parse_res
< 1);
241 return NSS_STATUS_SUCCESS
;
245 _nss_nis_getgrent_r (struct group
*result
, char *buffer
, size_t buflen
,
250 __libc_lock_lock (lock
);
252 status
= internal_nis_getgrent_r (result
, buffer
, buflen
, errnop
);
254 __libc_lock_unlock (lock
);
260 _nss_nis_getgrnam_r (const char *name
, struct group
*grp
,
261 char *buffer
, size_t buflen
, int *errnop
)
266 return NSS_STATUS_UNAVAIL
;
270 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
271 return NSS_STATUS_UNAVAIL
;
275 int yperr
= yp_match (domain
, "group.byname", name
, strlen (name
), &result
,
278 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
280 enum nss_status retval
= yperr2nss (yperr
);
282 if (retval
== NSS_STATUS_TRYAGAIN
)
287 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
291 return NSS_STATUS_TRYAGAIN
;
294 char *p
= strncpy (buffer
, result
, len
);
300 int parse_res
= _nss_files_parse_grent (p
, grp
, (void *) buffer
, buflen
,
302 if (__builtin_expect (parse_res
< 1, 0))
305 return NSS_STATUS_TRYAGAIN
;
307 return NSS_STATUS_NOTFOUND
;
309 return NSS_STATUS_SUCCESS
;
313 _nss_nis_getgrgid_r (gid_t gid
, struct group
*grp
,
314 char *buffer
, size_t buflen
, int *errnop
)
317 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
318 return NSS_STATUS_UNAVAIL
;
321 int nlen
= sprintf (buf
, "%lu", (unsigned long int) gid
);
325 int yperr
= yp_match (domain
, "group.bygid", buf
, nlen
, &result
, &len
);
327 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
329 enum nss_status retval
= yperr2nss (yperr
);
331 if (retval
== NSS_STATUS_TRYAGAIN
)
336 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
340 return NSS_STATUS_TRYAGAIN
;
343 char *p
= strncpy (buffer
, result
, len
);
349 int parse_res
= _nss_files_parse_grent (p
, grp
, (void *) buffer
, buflen
,
351 if (__builtin_expect (parse_res
< 1, 0))
354 return NSS_STATUS_TRYAGAIN
;
356 return NSS_STATUS_NOTFOUND
;
358 return NSS_STATUS_SUCCESS
;