NaCl: Fix thinko in last change.
[glibc.git] / nis / nss_compat / compat-grp.c
blobd8ef4be63be421840f07a60da23d711cc3b2a004
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 <fcntl.h>
22 #include <grp.h>
23 #include <nss.h>
24 #include <nsswitch.h>
25 #include <stdio_ext.h>
26 #include <string.h>
27 #include <rpc/types.h>
28 #include <bits/libc-lock.h>
29 #include <kernel-features.h>
31 static service_user *ni;
32 static enum nss_status (*nss_setgrent) (int stayopen);
33 static enum nss_status (*nss_getgrnam_r) (const char *name,
34 struct group * grp, char *buffer,
35 size_t buflen, int *errnop);
36 static enum nss_status (*nss_getgrgid_r) (gid_t gid, struct group * grp,
37 char *buffer, size_t buflen,
38 int *errnop);
39 static enum nss_status (*nss_getgrent_r) (struct group * grp, char *buffer,
40 size_t buflen, int *errnop);
41 static enum nss_status (*nss_endgrent) (void);
43 /* Get the declaration of the parser function. */
44 #define ENTNAME grent
45 #define STRUCTURE group
46 #define EXTERN_PARSER
47 #include <nss/nss_files/files-parse.c>
49 /* Structure for remembering -group members ... */
50 #define BLACKLIST_INITIAL_SIZE 512
51 #define BLACKLIST_INCREMENT 256
52 struct blacklist_t
54 char *data;
55 int current;
56 int size;
59 struct ent_t
61 bool_t files;
62 enum nss_status setent_status;
63 FILE *stream;
64 struct blacklist_t blacklist;
66 typedef struct ent_t ent_t;
68 static ent_t ext_ent = { TRUE, NSS_STATUS_SUCCESS, NULL, { NULL, 0, 0 }};
70 /* Protect global state against multiple changers. */
71 __libc_lock_define_initialized (static, lock)
73 /* Positive if O_CLOEXEC is supported, negative if it is not supported,
74 zero if it is still undecided. This variable is shared with the
75 other compat functions. */
76 #ifdef __ASSUME_O_CLOEXEC
77 # define __compat_have_cloexec 1
78 #else
79 # ifdef O_CLOEXEC
80 int __compat_have_cloexec;
81 # else
82 # define __compat_have_cloexec -1
83 # endif
84 #endif
86 /* Prototypes for local functions. */
87 static void blacklist_store_name (const char *, ent_t *);
88 static int in_blacklist (const char *, int, ent_t *);
90 /* Initialize the NSS interface/functions. The calling function must
91 hold the lock. */
92 static void
93 init_nss_interface (void)
95 if (__nss_database_lookup ("group_compat", NULL, "nis", &ni) >= 0)
97 nss_setgrent = __nss_lookup_function (ni, "setgrent");
98 nss_getgrnam_r = __nss_lookup_function (ni, "getgrnam_r");
99 nss_getgrgid_r = __nss_lookup_function (ni, "getgrgid_r");
100 nss_getgrent_r = __nss_lookup_function (ni, "getgrent_r");
101 nss_endgrent = __nss_lookup_function (ni, "endgrent");
105 static enum nss_status
106 internal_setgrent (ent_t *ent, int stayopen, int needent)
108 enum nss_status status = NSS_STATUS_SUCCESS;
110 ent->files = TRUE;
112 if (ent->blacklist.data != NULL)
114 ent->blacklist.current = 1;
115 ent->blacklist.data[0] = '|';
116 ent->blacklist.data[1] = '\0';
118 else
119 ent->blacklist.current = 0;
121 if (ent->stream == NULL)
123 ent->stream = fopen ("/etc/group", "rme");
125 if (ent->stream == NULL)
126 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
127 else
129 /* We have to make sure the file is `closed on exec'. */
130 int result = 0;
132 if (__compat_have_cloexec <= 0)
134 int flags;
135 result = flags = fcntl (fileno_unlocked (ent->stream), F_GETFD,
137 if (result >= 0)
139 #if defined O_CLOEXEC && !defined __ASSUME_O_CLOEXEC
140 if (__compat_have_cloexec == 0)
141 __compat_have_cloexec = (flags & FD_CLOEXEC) ? 1 : -1;
143 if (__compat_have_cloexec < 0)
144 #endif
146 flags |= FD_CLOEXEC;
147 result = fcntl (fileno_unlocked (ent->stream), F_SETFD,
148 flags);
153 if (result < 0)
155 /* Something went wrong. Close the stream and return a
156 failure. */
157 fclose (ent->stream);
158 ent->stream = NULL;
159 status = NSS_STATUS_UNAVAIL;
161 else
162 /* We take care of locking ourself. */
163 __fsetlocking (ent->stream, FSETLOCKING_BYCALLER);
166 else
167 rewind (ent->stream);
169 if (needent && status == NSS_STATUS_SUCCESS && nss_setgrent)
170 ent->setent_status = nss_setgrent (stayopen);
172 return status;
176 enum nss_status
177 _nss_compat_setgrent (int stayopen)
179 enum nss_status result;
181 __libc_lock_lock (lock);
183 if (ni == NULL)
184 init_nss_interface ();
186 result = internal_setgrent (&ext_ent, stayopen, 1);
188 __libc_lock_unlock (lock);
190 return result;
194 static enum nss_status
195 internal_endgrent (ent_t *ent)
197 if (ent->stream != NULL)
199 fclose (ent->stream);
200 ent->stream = NULL;
203 if (ent->blacklist.data != NULL)
205 ent->blacklist.current = 1;
206 ent->blacklist.data[0] = '|';
207 ent->blacklist.data[1] = '\0';
209 else
210 ent->blacklist.current = 0;
212 return NSS_STATUS_SUCCESS;
215 enum nss_status
216 _nss_compat_endgrent (void)
218 enum nss_status result;
220 __libc_lock_lock (lock);
222 if (nss_endgrent)
223 nss_endgrent ();
225 result = internal_endgrent (&ext_ent);
227 __libc_lock_unlock (lock);
229 return result;
232 /* get the next group from NSS (+ entry) */
233 static enum nss_status
234 getgrent_next_nss (struct group *result, ent_t *ent, char *buffer,
235 size_t buflen, int *errnop)
237 if (!nss_getgrent_r)
238 return NSS_STATUS_UNAVAIL;
240 /* If the setgrent call failed, say so. */
241 if (ent->setent_status != NSS_STATUS_SUCCESS)
242 return ent->setent_status;
246 enum nss_status status;
248 if ((status = nss_getgrent_r (result, buffer, buflen, errnop)) !=
249 NSS_STATUS_SUCCESS)
250 return status;
252 while (in_blacklist (result->gr_name, strlen (result->gr_name), ent));
254 return NSS_STATUS_SUCCESS;
257 /* This function handle the +group entrys in /etc/group */
258 static enum nss_status
259 getgrnam_plusgroup (const char *name, struct group *result, ent_t *ent,
260 char *buffer, size_t buflen, int *errnop)
262 if (!nss_getgrnam_r)
263 return NSS_STATUS_UNAVAIL;
265 enum nss_status status = nss_getgrnam_r (name, result, buffer, buflen,
266 errnop);
267 if (status != NSS_STATUS_SUCCESS)
268 return status;
270 if (in_blacklist (result->gr_name, strlen (result->gr_name), ent))
271 return NSS_STATUS_NOTFOUND;
273 /* We found the entry. */
274 return NSS_STATUS_SUCCESS;
277 static enum nss_status
278 getgrent_next_file (struct group *result, ent_t *ent,
279 char *buffer, size_t buflen, int *errnop)
281 struct parser_data *data = (void *) buffer;
282 while (1)
284 fpos_t pos;
285 int parse_res = 0;
286 char *p;
290 /* We need at least 3 characters for one line. */
291 if (__glibc_unlikely (buflen < 3))
293 erange:
294 *errnop = ERANGE;
295 return NSS_STATUS_TRYAGAIN;
298 fgetpos (ent->stream, &pos);
299 buffer[buflen - 1] = '\xff';
300 p = fgets_unlocked (buffer, buflen, ent->stream);
301 if (p == NULL && feof_unlocked (ent->stream))
302 return NSS_STATUS_NOTFOUND;
304 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
306 erange_reset:
307 fsetpos (ent->stream, &pos);
308 goto erange;
311 /* Terminate the line for any case. */
312 buffer[buflen - 1] = '\0';
314 /* Skip leading blanks. */
315 while (isspace (*p))
316 ++p;
318 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
319 /* Parse the line. If it is invalid, loop to
320 get the next line of the file to parse. */
321 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
322 errnop)));
324 if (__glibc_unlikely (parse_res == -1))
325 /* The parser ran out of space. */
326 goto erange_reset;
328 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
329 /* This is a real entry. */
330 break;
332 /* -group */
333 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
334 && result->gr_name[1] != '@')
336 blacklist_store_name (&result->gr_name[1], ent);
337 continue;
340 /* +group */
341 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
342 && result->gr_name[1] != '@')
344 size_t len = strlen (result->gr_name);
345 char buf[len];
346 enum nss_status status;
348 /* Store the group in the blacklist for the "+" at the end of
349 /etc/group */
350 memcpy (buf, &result->gr_name[1], len);
351 status = getgrnam_plusgroup (&result->gr_name[1], result, ent,
352 buffer, buflen, errnop);
353 blacklist_store_name (buf, ent);
354 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
355 break;
356 else if (status == NSS_STATUS_RETURN /* We couldn't parse the entry*/
357 || status == NSS_STATUS_NOTFOUND) /* No group in NIS */
358 continue;
359 else
361 if (status == NSS_STATUS_TRYAGAIN)
362 /* The parser ran out of space. */
363 goto erange_reset;
365 return status;
369 /* +:... */
370 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
372 ent->files = FALSE;
374 return getgrent_next_nss (result, ent, buffer, buflen, errnop);
378 return NSS_STATUS_SUCCESS;
382 enum nss_status
383 _nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen,
384 int *errnop)
386 enum nss_status result = NSS_STATUS_SUCCESS;
388 __libc_lock_lock (lock);
390 /* Be prepared that the setgrent function was not called before. */
391 if (ni == NULL)
392 init_nss_interface ();
394 if (ext_ent.stream == NULL)
395 result = internal_setgrent (&ext_ent, 1, 1);
397 if (result == NSS_STATUS_SUCCESS)
399 if (ext_ent.files)
400 result = getgrent_next_file (grp, &ext_ent, buffer, buflen, errnop);
401 else
402 result = getgrent_next_nss (grp, &ext_ent, buffer, buflen, errnop);
404 __libc_lock_unlock (lock);
406 return result;
409 /* Searches in /etc/group and the NIS/NIS+ map for a special group */
410 static enum nss_status
411 internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
412 char *buffer, size_t buflen, int *errnop)
414 struct parser_data *data = (void *) buffer;
415 while (1)
417 fpos_t pos;
418 int parse_res = 0;
419 char *p;
423 /* We need at least 3 characters for one line. */
424 if (__glibc_unlikely (buflen < 3))
426 erange:
427 *errnop = ERANGE;
428 return NSS_STATUS_TRYAGAIN;
431 fgetpos (ent->stream, &pos);
432 buffer[buflen - 1] = '\xff';
433 p = fgets_unlocked (buffer, buflen, ent->stream);
434 if (p == NULL && feof_unlocked (ent->stream))
435 return NSS_STATUS_NOTFOUND;
437 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
439 erange_reset:
440 fsetpos (ent->stream, &pos);
441 goto erange;
444 /* Terminate the line for any case. */
445 buffer[buflen - 1] = '\0';
447 /* Skip leading blanks. */
448 while (isspace (*p))
449 ++p;
451 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
452 /* Parse the line. If it is invalid, loop to
453 get the next line of the file to parse. */
454 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
455 errnop)));
457 if (__glibc_unlikely (parse_res == -1))
458 /* The parser ran out of space. */
459 goto erange_reset;
461 /* This is a real entry. */
462 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
464 if (strcmp (result->gr_name, name) == 0)
465 return NSS_STATUS_SUCCESS;
466 else
467 continue;
470 /* -group */
471 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
473 if (strcmp (&result->gr_name[1], name) == 0)
474 return NSS_STATUS_NOTFOUND;
475 else
476 continue;
479 /* +group */
480 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
482 if (strcmp (name, &result->gr_name[1]) == 0)
484 enum nss_status status;
486 status = getgrnam_plusgroup (name, result, ent,
487 buffer, buflen, errnop);
488 if (status == NSS_STATUS_RETURN)
489 /* We couldn't parse the entry */
490 continue;
491 else
492 return status;
495 /* +:... */
496 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
498 enum nss_status status;
500 status = getgrnam_plusgroup (name, result, ent,
501 buffer, buflen, errnop);
502 if (status == NSS_STATUS_RETURN)
503 /* We couldn't parse the entry */
504 continue;
505 else
506 return status;
510 return NSS_STATUS_SUCCESS;
513 enum nss_status
514 _nss_compat_getgrnam_r (const char *name, struct group *grp,
515 char *buffer, size_t buflen, int *errnop)
517 ent_t ent = { TRUE, NSS_STATUS_SUCCESS, NULL, { NULL, 0, 0 }};
518 enum nss_status result;
520 if (name[0] == '-' || name[0] == '+')
521 return NSS_STATUS_NOTFOUND;
523 __libc_lock_lock (lock);
525 if (ni == NULL)
526 init_nss_interface ();
528 __libc_lock_unlock (lock);
530 result = internal_setgrent (&ent, 0, 0);
532 if (result == NSS_STATUS_SUCCESS)
533 result = internal_getgrnam_r (name, grp, &ent, buffer, buflen, errnop);
535 internal_endgrent (&ent);
537 return result;
540 /* Searches in /etc/group and the NIS/NIS+ map for a special group id */
541 static enum nss_status
542 internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
543 char *buffer, size_t buflen, int *errnop)
545 struct parser_data *data = (void *) buffer;
546 while (1)
548 fpos_t pos;
549 int parse_res = 0;
550 char *p;
554 /* We need at least 3 characters for one line. */
555 if (__glibc_unlikely (buflen < 3))
557 erange:
558 *errnop = ERANGE;
559 return NSS_STATUS_TRYAGAIN;
562 fgetpos (ent->stream, &pos);
563 buffer[buflen - 1] = '\xff';
564 p = fgets_unlocked (buffer, buflen, ent->stream);
565 if (p == NULL && feof_unlocked (ent->stream))
566 return NSS_STATUS_NOTFOUND;
568 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
570 erange_reset:
571 fsetpos (ent->stream, &pos);
572 goto erange;
575 /* Terminate the line for any case. */
576 buffer[buflen - 1] = '\0';
578 /* Skip leading blanks. */
579 while (isspace (*p))
580 ++p;
582 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
583 /* Parse the line. If it is invalid, loop to
584 get the next line of the file to parse. */
585 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
586 errnop)));
588 if (__glibc_unlikely (parse_res == -1))
589 /* The parser ran out of space. */
590 goto erange_reset;
592 /* This is a real entry. */
593 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
595 if (result->gr_gid == gid)
596 return NSS_STATUS_SUCCESS;
597 else
598 continue;
601 /* -group */
602 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
604 blacklist_store_name (&result->gr_name[1], ent);
605 continue;
608 /* +group */
609 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
611 /* Yes, no +1, see the memcpy call below. */
612 size_t len = strlen (result->gr_name);
613 char buf[len];
614 enum nss_status status;
616 /* Store the group in the blacklist for the "+" at the end of
617 /etc/group */
618 memcpy (buf, &result->gr_name[1], len);
619 status = getgrnam_plusgroup (&result->gr_name[1], result, ent,
620 buffer, buflen, errnop);
621 blacklist_store_name (buf, ent);
622 if (status == NSS_STATUS_SUCCESS && result->gr_gid == gid)
623 break;
624 else
625 continue;
627 /* +:... */
628 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
630 if (!nss_getgrgid_r)
631 return NSS_STATUS_UNAVAIL;
633 enum nss_status status = nss_getgrgid_r (gid, result, buffer, buflen,
634 errnop);
635 if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
636 return NSS_STATUS_NOTFOUND;
637 else
638 return status;
642 return NSS_STATUS_SUCCESS;
645 enum nss_status
646 _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
647 char *buffer, size_t buflen, int *errnop)
649 ent_t ent = { TRUE, NSS_STATUS_SUCCESS, NULL, { NULL, 0, 0 }};
650 enum nss_status result;
652 __libc_lock_lock (lock);
654 if (ni == NULL)
655 init_nss_interface ();
657 __libc_lock_unlock (lock);
659 result = internal_setgrent (&ent, 0, 0);
661 if (result == NSS_STATUS_SUCCESS)
662 result = internal_getgrgid_r (gid, grp, &ent, buffer, buflen, errnop);
664 internal_endgrent (&ent);
666 return result;
670 /* Support routines for remembering -@netgroup and -user entries.
671 The names are stored in a single string with `|' as separator. */
672 static void
673 blacklist_store_name (const char *name, ent_t *ent)
675 int namelen = strlen (name);
676 char *tmp;
678 /* first call, setup cache */
679 if (ent->blacklist.size == 0)
681 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
682 ent->blacklist.data = malloc (ent->blacklist.size);
683 if (ent->blacklist.data == NULL)
684 return;
685 ent->blacklist.data[0] = '|';
686 ent->blacklist.data[1] = '\0';
687 ent->blacklist.current = 1;
689 else
691 if (in_blacklist (name, namelen, ent))
692 return; /* no duplicates */
694 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
696 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
697 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
698 if (tmp == NULL)
700 free (ent->blacklist.data);
701 ent->blacklist.size = 0;
702 return;
704 ent->blacklist.data = tmp;
708 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
709 *tmp++ = '|';
710 *tmp = '\0';
711 ent->blacklist.current += namelen + 1;
713 return;
716 /* returns TRUE if ent->blacklist contains name, else FALSE */
717 static bool_t
718 in_blacklist (const char *name, int namelen, ent_t *ent)
720 char buf[namelen + 3];
721 char *cp;
723 if (ent->blacklist.data == NULL)
724 return FALSE;
726 buf[0] = '|';
727 cp = stpcpy (&buf[1], name);
728 *cp++ = '|';
729 *cp = '\0';
730 return strstr (ent->blacklist.data, buf) != NULL;