* include/features.h (__GLIBC_MINOR__): Bump to 7.
[glibc.git] / nis / nss_nis / nis-grp.c
blob6e36cf828f1a9bb3f6ecdd470577e52e39956944
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
18 02111-1307 USA. */
20 #include <ctype.h>
21 #include <errno.h>
22 #include <grp.h>
23 #include <nss.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
29 #include "nss-nis.h"
30 #include <libnsl.h>
32 /* Get the declaration of the parser function. */
33 #define ENTNAME grent
34 #define STRUCTURE group
35 #define EXTERN_PARSER
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;
42 static char *oldkey;
43 static int oldkeylen;
44 static intern_t intern;
47 static void
48 internal_nis_endgrent (void)
50 new_start = 1;
51 if (oldkey != NULL)
53 free (oldkey);
54 oldkey = NULL;
55 oldkeylen = 0;
58 struct response_t *curr = intern.next;
60 while (curr != NULL)
62 struct response_t *last = curr;
63 curr = curr->next;
64 free (last);
67 intern.next = intern.start = NULL;
71 enum nss_status
72 _nss_nis_endgrent (void)
74 __libc_lock_lock (lock);
76 internal_nis_endgrent ();
78 __libc_lock_unlock (lock);
80 return NSS_STATUS_SUCCESS;
84 enum nss_status
85 internal_nis_setgrent (void)
87 /* We have to read all the data now. */
88 char *domain;
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;
104 intern.offset = 0;
106 return status;
110 enum nss_status
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);
124 return result;
128 static enum nss_status
129 internal_nis_getgrent_r (struct group *grp, char *buffer, size_t buflen,
130 int *errnop)
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;
136 char *domain = 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. */
141 int parse_res;
144 char *result;
145 char *outkey;
146 int len;
147 int keylen;
149 if (batch_read)
151 struct response_t *bucket;
153 handle_batch_read:
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
162 to the next. */
163 bucket = intern.next = bucket->next;
164 intern.offset = 0;
167 for (result = &bucket->mem[intern.offset]; isspace (*result);
168 ++result)
169 ++intern.offset;
171 len = strlen (result);
173 else
175 int yperr;
177 if (new_start)
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)
184 batch_read = true;
185 goto handle_batch_read;
188 yperr = yp_first (domain, "group.byname", &outkey, &keylen,
189 &result, &len);
191 else
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)
200 *errnop = errno;
201 return retval;
205 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
207 free (result);
208 *errnop = ERANGE;
209 return NSS_STATUS_TRYAGAIN;
212 char *p = strncpy (buffer, result, len);
213 buffer[len] = '\0';
214 while (isspace (*p))
215 ++p;
216 if (!batch_read)
217 free (result);
219 parse_res = _nss_files_parse_grent (p, grp, (void *) buffer, buflen,
220 errnop);
221 if (__builtin_expect (parse_res == -1, 0))
223 if (!batch_read)
224 free (outkey);
225 *errnop = ERANGE;
226 return NSS_STATUS_TRYAGAIN;
229 if (batch_read)
230 intern.offset += len + 1;
231 else
233 free (oldkey);
234 oldkey = outkey;
235 oldkeylen = keylen;
236 new_start = 0;
239 while (parse_res < 1);
241 return NSS_STATUS_SUCCESS;
244 enum nss_status
245 _nss_nis_getgrent_r (struct group *result, char *buffer, size_t buflen,
246 int *errnop)
248 int status;
250 __libc_lock_lock (lock);
252 status = internal_nis_getgrent_r (result, buffer, buflen, errnop);
254 __libc_lock_unlock (lock);
256 return status;
259 enum nss_status
260 _nss_nis_getgrnam_r (const char *name, struct group *grp,
261 char *buffer, size_t buflen, int *errnop)
263 if (name == NULL)
265 *errnop = EINVAL;
266 return NSS_STATUS_UNAVAIL;
269 char *domain;
270 if (__builtin_expect (yp_get_default_domain (&domain), 0))
271 return NSS_STATUS_UNAVAIL;
273 char *result;
274 int len;
275 int yperr = yp_match (domain, "group.byname", name, strlen (name), &result,
276 &len);
278 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
280 enum nss_status retval = yperr2nss (yperr);
282 if (retval == NSS_STATUS_TRYAGAIN)
283 *errnop = errno;
284 return retval;
287 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
289 free (result);
290 *errnop = ERANGE;
291 return NSS_STATUS_TRYAGAIN;
294 char *p = strncpy (buffer, result, len);
295 buffer[len] = '\0';
296 while (isspace (*p))
297 ++p;
298 free (result);
300 int parse_res = _nss_files_parse_grent (p, grp, (void *) buffer, buflen,
301 errnop);
302 if (__builtin_expect (parse_res < 1, 0))
304 if (parse_res == -1)
305 return NSS_STATUS_TRYAGAIN;
306 else
307 return NSS_STATUS_NOTFOUND;
309 return NSS_STATUS_SUCCESS;
312 enum nss_status
313 _nss_nis_getgrgid_r (gid_t gid, struct group *grp,
314 char *buffer, size_t buflen, int *errnop)
316 char *domain;
317 if (__builtin_expect (yp_get_default_domain (&domain), 0))
318 return NSS_STATUS_UNAVAIL;
320 char buf[32];
321 int nlen = sprintf (buf, "%lu", (unsigned long int) gid);
323 char *result;
324 int len;
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)
332 *errnop = errno;
333 return retval;
336 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
338 free (result);
339 *errnop = ERANGE;
340 return NSS_STATUS_TRYAGAIN;
343 char *p = strncpy (buffer, result, len);
344 buffer[len] = '\0';
345 while (isspace (*p))
346 ++p;
347 free (result);
349 int parse_res = _nss_files_parse_grent (p, grp, (void *) buffer, buflen,
350 errnop);
351 if (__builtin_expect (parse_res < 1, 0))
353 if (parse_res == -1)
354 return NSS_STATUS_TRYAGAIN;
355 else
356 return NSS_STATUS_NOTFOUND;
358 return NSS_STATUS_SUCCESS;