Update copyright dates with scripts/update-copyrights.
[glibc.git] / nis / nss_nis / nis-grp.c
blob19fe8cfc44e92549ba4e00694ecb0603ad07542c
1 /* Copyright (C) 1996-2015 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <ctype.h>
20 #include <errno.h>
21 #include <grp.h>
22 #include <nss.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/yp.h>
26 #include <rpcsvc/ypclnt.h>
28 #include "nss-nis.h"
29 #include <libnsl.h>
31 /* Get the declaration of the parser function. */
32 #define ENTNAME grent
33 #define STRUCTURE group
34 #define EXTERN_PARSER
35 #include <nss/nss_files/files-parse.c>
37 /* Protect global state against multiple changers */
38 __libc_lock_define_initialized (static, lock)
40 static bool_t new_start = 1;
41 static char *oldkey;
42 static int oldkeylen;
43 static intern_t intern;
46 static void
47 internal_nis_endgrent (void)
49 new_start = 1;
50 if (oldkey != NULL)
52 free (oldkey);
53 oldkey = NULL;
54 oldkeylen = 0;
57 struct response_t *curr = intern.start;
59 while (curr != NULL)
61 struct response_t *last = curr;
62 curr = curr->next;
63 free (last);
66 intern.next = intern.start = NULL;
70 enum nss_status
71 _nss_nis_endgrent (void)
73 __libc_lock_lock (lock);
75 internal_nis_endgrent ();
77 __libc_lock_unlock (lock);
79 return NSS_STATUS_SUCCESS;
83 enum nss_status
84 internal_nis_setgrent (void)
86 /* We have to read all the data now. */
87 char *domain;
88 if (__glibc_unlikely (yp_get_default_domain (&domain)))
89 return NSS_STATUS_UNAVAIL;
91 struct ypall_callback ypcb;
93 ypcb.foreach = _nis_saveit;
94 ypcb.data = (char *) &intern;
95 enum nss_status status = yperr2nss (yp_all (domain, "group.byname", &ypcb));
98 /* Mark the last buffer as full. */
99 if (intern.next != NULL)
100 intern.next->size = intern.offset;
102 intern.next = intern.start;
103 intern.offset = 0;
105 return status;
109 enum nss_status
110 _nss_nis_setgrent (int stayopen)
112 enum nss_status result = NSS_STATUS_SUCCESS;
114 __libc_lock_lock (lock);
116 internal_nis_endgrent ();
118 if (_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ)
119 result = internal_nis_setgrent ();
121 __libc_lock_unlock (lock);
123 return result;
127 static enum nss_status
128 internal_nis_getgrent_r (struct group *grp, char *buffer, size_t buflen,
129 int *errnop)
131 /* If we read the entire database at setpwent time we just iterate
132 over the data we have in memory. */
133 bool batch_read = intern.start != NULL;
135 char *domain = NULL;
136 if (!batch_read && __builtin_expect (yp_get_default_domain (&domain), 0))
137 return NSS_STATUS_UNAVAIL;
139 /* Get the next entry until we found a correct one. */
140 int parse_res;
143 char *result;
144 char *outkey;
145 int len;
146 int keylen;
148 if (batch_read)
150 struct response_t *bucket;
152 handle_batch_read:
153 bucket = intern.next;
155 if (__glibc_unlikely (intern.offset >= bucket->size))
157 if (bucket->next == NULL)
158 return NSS_STATUS_NOTFOUND;
160 /* We look at all the content in the current bucket. Go on
161 to the next. */
162 bucket = intern.next = bucket->next;
163 intern.offset = 0;
166 for (result = &bucket->mem[intern.offset]; isspace (*result);
167 ++result)
168 ++intern.offset;
170 len = strlen (result);
172 else
174 int yperr;
176 if (new_start)
178 /* Maybe we should read the database in one piece. */
179 if ((_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ)
180 && internal_nis_setgrent () == NSS_STATUS_SUCCESS
181 && intern.start != NULL)
183 batch_read = true;
184 goto handle_batch_read;
187 yperr = yp_first (domain, "group.byname", &outkey, &keylen,
188 &result, &len);
190 else
191 yperr = yp_next (domain, "group.byname", oldkey, oldkeylen,
192 &outkey, &keylen, &result, &len);
194 if (__glibc_unlikely (yperr != YPERR_SUCCESS))
196 enum nss_status retval = yperr2nss (yperr);
198 if (retval == NSS_STATUS_TRYAGAIN)
199 *errnop = errno;
200 return retval;
204 if (__glibc_unlikely ((size_t) (len + 1) > buflen))
206 if (!batch_read)
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 (__glibc_unlikely (parse_res == -1))
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 (__glibc_unlikely (yp_get_default_domain (&domain)))
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 (__glibc_unlikely (yperr != YPERR_SUCCESS))
280 enum nss_status retval = yperr2nss (yperr);
282 if (retval == NSS_STATUS_TRYAGAIN)
283 *errnop = errno;
284 return retval;
287 if (__glibc_unlikely ((size_t) (len + 1) > buflen))
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 (__glibc_unlikely (yp_get_default_domain (&domain)))
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 (__glibc_unlikely (yperr != YPERR_SUCCESS))
329 enum nss_status retval = yperr2nss (yperr);
331 if (retval == NSS_STATUS_TRYAGAIN)
332 *errnop = errno;
333 return retval;
336 if (__glibc_unlikely ((size_t) (len + 1) > buflen))
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 (__glibc_unlikely (parse_res < 1))
353 if (parse_res == -1)
354 return NSS_STATUS_TRYAGAIN;
355 else
356 return NSS_STATUS_NOTFOUND;
358 return NSS_STATUS_SUCCESS;