* posix/execl.c: Fix last argument of memcpy. Reported by Brian
[glibc.git] / nis / nss_compat / compat-grp.c
blob961c3b9b4b2ca08101217fc10e637b2d603f6c1b
1 /* Copyright (C) 1996, 1997, 1998, 1999, 2001 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 <errno.h>
21 #include <fcntl.h>
22 #include <nss.h>
23 #include <grp.h>
24 #include <ctype.h>
25 #include <bits/libc-lock.h>
26 #include <string.h>
27 #include <rpcsvc/yp.h>
28 #include <rpcsvc/ypclnt.h>
29 #include <rpcsvc/nis.h>
30 #include <nsswitch.h>
32 #include "nss-nisplus.h"
33 #include "nisplus-parser.h"
35 static service_user *ni;
36 static bool_t use_nisplus; /* default: group_compat: nis */
37 static nis_name grptable; /* Name of the group table */
38 static size_t grptablelen;
40 /* Get the declaration of the parser function. */
41 #define ENTNAME grent
42 #define STRUCTURE group
43 #define EXTERN_PARSER
44 #include <nss/nss_files/files-parse.c>
46 /* Structure for remembering -group members ... */
47 #define BLACKLIST_INITIAL_SIZE 512
48 #define BLACKLIST_INCREMENT 256
49 struct blacklist_t
51 char *data;
52 int current;
53 int size;
56 struct ent_t
58 bool_t nis;
59 bool_t nis_first;
60 char *oldkey;
61 int oldkeylen;
62 nis_result *result;
63 FILE *stream;
64 struct blacklist_t blacklist;
66 typedef struct ent_t ent_t;
68 static ent_t ext_ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
70 /* Protect global state against multiple changers. */
71 __libc_lock_define_initialized (static, lock)
73 /* Prototypes for local functions. */
74 static void blacklist_store_name (const char *, ent_t *);
75 static int in_blacklist (const char *, int, ent_t *);
77 static enum nss_status
78 _nss_first_init (void)
80 if (ni == NULL)
82 __nss_database_lookup ("group_compat", NULL, "nis", &ni);
83 use_nisplus = (strcmp (ni->name, "nisplus") == 0);
86 if (grptable == NULL)
88 static const char key[] = "group.org_dir.";
89 const char *local_dir = nis_local_directory ();
90 size_t len_local_dir = strlen (local_dir);
92 grptable = malloc (sizeof (key) + len_local_dir);
93 if (grptable == NULL)
94 return NSS_STATUS_TRYAGAIN;
96 grptablelen = ((char *) mempcpy (mempcpy (grptable,
97 key, sizeof (key) - 1),
98 local_dir, len_local_dir + 1)
99 - grptable) - 1;
102 return NSS_STATUS_SUCCESS;
105 static enum nss_status
106 internal_setgrent (ent_t *ent)
108 enum nss_status status = NSS_STATUS_SUCCESS;
110 ent->nis = ent->nis_first = 0;
112 if (_nss_first_init () != NSS_STATUS_SUCCESS)
113 return NSS_STATUS_UNAVAIL;
115 if (ent->oldkey != NULL)
117 free (ent->oldkey);
118 ent->oldkey = NULL;
119 ent->oldkeylen = 0;
122 if (ent->result != NULL)
124 nis_freeresult (ent->result);
125 ent->result = NULL;
128 if (ent->blacklist.data != NULL)
130 ent->blacklist.current = 1;
131 ent->blacklist.data[0] = '|';
132 ent->blacklist.data[1] = '\0';
134 else
135 ent->blacklist.current = 0;
137 if (ent->stream == NULL)
139 ent->stream = fopen ("/etc/group", "r");
141 if (ent->stream == NULL)
142 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
143 else
145 /* We have to make sure the file is `closed on exec'. */
146 int result, flags;
148 result = flags = fcntl (fileno (ent->stream), F_GETFD, 0);
149 if (result >= 0)
151 flags |= FD_CLOEXEC;
152 result = fcntl (fileno (ent->stream), F_SETFD, flags);
154 if (result < 0)
156 /* Something went wrong. Close the stream and return a
157 failure. */
158 fclose (ent->stream);
159 ent->stream = NULL;
160 status = NSS_STATUS_UNAVAIL;
164 else
165 rewind (ent->stream);
167 return status;
171 enum nss_status
172 _nss_compat_setgrent (int stayopen)
174 enum nss_status result;
176 __libc_lock_lock (lock);
178 result = internal_setgrent (&ext_ent);
180 __libc_lock_unlock (lock);
182 return result;
186 static enum nss_status
187 internal_endgrent (ent_t *ent)
189 if (ent->stream != NULL)
191 fclose (ent->stream);
192 ent->stream = NULL;
195 ent->nis = ent->nis_first = 0;
197 if (ent->oldkey != NULL)
199 free (ent->oldkey);
200 ent->oldkey = NULL;
201 ent->oldkeylen = 0;
204 if (ent->result != NULL)
206 nis_freeresult (ent->result);
207 ent->result = NULL;
210 if (ent->blacklist.data != NULL)
212 ent->blacklist.current = 1;
213 ent->blacklist.data[0] = '|';
214 ent->blacklist.data[1] = '\0';
216 else
217 ent->blacklist.current = 0;
219 return NSS_STATUS_SUCCESS;
222 enum nss_status
223 _nss_compat_endgrent (void)
225 enum nss_status result;
227 __libc_lock_lock (lock);
229 result = internal_endgrent (&ext_ent);
231 __libc_lock_unlock (lock);
233 return result;
236 static enum nss_status
237 getgrent_next_nis (struct group *result, ent_t *ent, char *buffer,
238 size_t buflen, int *errnop)
240 struct parser_data *data = (void *) buffer;
241 char *domain;
242 char *outkey, *outval;
243 int outkeylen, outvallen, parse_res;
244 char *p;
246 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
248 ent->nis = 0;
249 *errnop = ENOENT;
250 return NSS_STATUS_NOTFOUND;
255 char *save_oldkey;
256 int save_oldlen;
257 bool_t save_nis_first;
259 if (ent->nis_first)
261 if (yp_first (domain, "group.byname", &outkey, &outkeylen,
262 &outval, &outvallen) != YPERR_SUCCESS)
264 ent->nis = 0;
265 return NSS_STATUS_UNAVAIL;
268 if ( buflen < ((size_t) outvallen + 1))
270 free (outval);
271 *errnop = ERANGE;
272 return NSS_STATUS_TRYAGAIN;
275 save_oldkey = ent->oldkey;
276 save_oldlen = ent->oldkeylen;
277 save_nis_first = TRUE;
278 ent->oldkey = outkey;
279 ent->oldkeylen = outkeylen;
280 ent->nis_first = FALSE;
282 else
284 if (yp_next (domain, "group.byname", ent->oldkey, ent->oldkeylen,
285 &outkey, &outkeylen, &outval, &outvallen)
286 != YPERR_SUCCESS)
288 ent->nis = 0;
289 *errnop = ENOENT;
290 return NSS_STATUS_NOTFOUND;
293 if ( buflen < ((size_t) outvallen + 1))
295 free (outval);
296 *errnop = ERANGE;
297 return NSS_STATUS_TRYAGAIN;
300 save_oldkey = ent->oldkey;
301 save_oldlen = ent->oldkeylen;
302 save_nis_first = FALSE;
303 ent->oldkey = outkey;
304 ent->oldkeylen = outkeylen;
307 /* Copy the found data to our buffer... */
308 p = strncpy (buffer, outval, buflen);
310 /* ...and free the data. */
311 free (outval);
313 while (isspace (*p))
314 ++p;
316 parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
317 if (parse_res == -1)
319 free (ent->oldkey);
320 ent->oldkey = save_oldkey;
321 ent->oldkeylen = save_oldlen;
322 ent->nis_first = save_nis_first;
323 *errnop = ERANGE;
324 return NSS_STATUS_TRYAGAIN;
326 else
328 if (!save_nis_first)
329 free (save_oldkey);
332 if (parse_res &&
333 in_blacklist (result->gr_name, strlen (result->gr_name), ent))
334 parse_res = 0; /* if result->gr_name in blacklist,search next entry */
336 while (!parse_res);
338 return NSS_STATUS_SUCCESS;
341 static enum nss_status
342 getgrent_next_nisplus (struct group *result, ent_t *ent, char *buffer,
343 size_t buflen, int *errnop)
345 int parse_res;
349 nis_result *save_oldres;
350 bool_t save_nis_first;
352 if (ent->nis_first)
354 save_oldres = ent->result;
355 save_nis_first = TRUE;
356 ent->result = nis_first_entry(grptable);
357 if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
359 ent->nis = 0;
360 return niserr2nss (ent->result->status);
362 ent->nis_first = FALSE;
364 else
366 nis_result *res;
368 save_oldres = ent->result;
369 save_nis_first = FALSE;
370 res = nis_next_entry(grptable, &ent->result->cookie);
371 ent->result = res;
372 if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
374 ent->nis = 0;
375 return niserr2nss (ent->result->status);
378 parse_res = _nss_nisplus_parse_grent (ent->result, 0, result,
379 buffer, buflen, errnop);
380 if (parse_res == -1)
382 nis_freeresult (ent->result);
383 ent->result = save_oldres;
384 ent->nis_first = save_nis_first;
385 *errnop = ERANGE;
386 return NSS_STATUS_TRYAGAIN;
388 else
390 if (!save_nis_first)
391 nis_freeresult (save_oldres);
394 if (parse_res &&
395 in_blacklist (result->gr_name, strlen (result->gr_name), ent))
396 parse_res = 0; /* if result->gr_name in blacklist,search next entry */
398 while (!parse_res);
400 return NSS_STATUS_SUCCESS;
403 /* This function handle the +group entrys in /etc/group */
404 static enum nss_status
405 getgrnam_plusgroup (const char *name, struct group *result, char *buffer,
406 size_t buflen, int *errnop)
408 struct parser_data *data = (void *) buffer;
409 int parse_res;
411 if (use_nisplus) /* Do the NIS+ query here */
413 nis_result *res;
414 char buf[strlen (name) + 24 + grptablelen];
416 sprintf(buf, "[name=%s],%s", name, grptable);
417 res = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
418 if (niserr2nss (res->status) != NSS_STATUS_SUCCESS)
420 enum nss_status status = niserr2nss (res->status);
422 nis_freeresult (res);
423 return status;
425 parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer, buflen,
426 errnop);
427 if (parse_res == -1)
429 nis_freeresult (res);
430 *errnop = ERANGE;
431 return NSS_STATUS_TRYAGAIN;
433 nis_freeresult (res);
435 else /* Use NIS */
437 char *domain, *outval, *p;
438 int outvallen;
440 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
442 *errnop = ENOENT;
443 return NSS_STATUS_NOTFOUND;
446 if (yp_match (domain, "group.byname", name, strlen (name),
447 &outval, &outvallen) != YPERR_SUCCESS)
449 *errnop = ENOENT;
450 return NSS_STATUS_NOTFOUND;
453 if (buflen < ((size_t) outvallen + 1))
455 free (outval);
456 *errnop = ERANGE;
457 return NSS_STATUS_TRYAGAIN;
460 /* Copy the found data to our buffer... */
461 p = strncpy (buffer, outval, buflen);
463 /* ... and free the data. */
464 free (outval);
465 while (isspace (*p))
466 ++p;
467 parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
468 if (parse_res == -1)
469 return NSS_STATUS_TRYAGAIN;
472 if (parse_res)
473 /* We found the entry. */
474 return NSS_STATUS_SUCCESS;
475 else
476 return NSS_STATUS_RETURN;
479 static enum nss_status
480 getgrent_next_file (struct group *result, ent_t *ent,
481 char *buffer, size_t buflen, int *errnop)
483 struct parser_data *data = (void *) buffer;
484 while (1)
486 fpos_t pos;
487 int parse_res = 0;
488 char *p;
492 fgetpos (ent->stream, &pos);
493 buffer[buflen - 1] = '\xff';
494 p = fgets (buffer, buflen, ent->stream);
495 if (p == NULL && feof (ent->stream))
497 *errnop = ENOENT;
498 return NSS_STATUS_NOTFOUND;
500 if (p == NULL || buffer[buflen - 1] != '\xff')
502 fsetpos (ent->stream, &pos);
503 *errnop = ERANGE;
504 return NSS_STATUS_TRYAGAIN;
507 /* Terminate the line for any case. */
508 buffer[buflen - 1] = '\0';
510 /* Skip leading blanks. */
511 while (isspace (*p))
512 ++p;
514 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
515 /* Parse the line. If it is invalid, loop to
516 get the next line of the file to parse. */
517 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
518 errnop)));
520 if (parse_res == -1)
522 /* The parser ran out of space. */
523 fsetpos (ent->stream, &pos);
524 *errnop = ERANGE;
525 return NSS_STATUS_TRYAGAIN;
528 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
529 /* This is a real entry. */
530 break;
532 /* -group */
533 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
534 && result->gr_name[1] != '@')
536 blacklist_store_name (&result->gr_name[1], ent);
537 continue;
540 /* +group */
541 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
542 && result->gr_name[1] != '@')
544 enum nss_status status;
546 /* Store the group in the blacklist for the "+" at the end of
547 /etc/group */
548 blacklist_store_name (&result->gr_name[1], ent);
549 status = getgrnam_plusgroup (&result->gr_name[1], result, buffer,
550 buflen, errnop);
551 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
552 break;
553 else
554 if (status == NSS_STATUS_RETURN /* We couldn't parse the entry */
555 || status == NSS_STATUS_NOTFOUND) /* No group in NIS */
556 continue;
557 else
559 if (status == NSS_STATUS_TRYAGAIN)
561 /* The parser ran out of space. */
562 fsetpos (ent->stream, &pos);
563 *errnop = ERANGE;
565 return status;
569 /* +:... */
570 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
572 ent->nis = TRUE;
573 ent->nis_first = TRUE;
575 if (use_nisplus)
576 return getgrent_next_nisplus (result, ent, buffer, buflen, errnop);
577 else
578 return getgrent_next_nis (result, ent, buffer, buflen, errnop);
582 return NSS_STATUS_SUCCESS;
586 static enum nss_status
587 internal_getgrent_r (struct group *gr, ent_t *ent, char *buffer,
588 size_t buflen, int *errnop)
590 if (ent->nis)
592 if (use_nisplus)
593 return getgrent_next_nisplus (gr, ent, buffer, buflen, errnop);
594 else
595 return getgrent_next_nis (gr, ent, buffer, buflen, errnop);
597 else
598 return getgrent_next_file (gr, ent, buffer, buflen, errnop);
601 enum nss_status
602 _nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen,
603 int *errnop)
605 enum nss_status status = NSS_STATUS_SUCCESS;
607 __libc_lock_lock (lock);
609 /* Be prepared that the setgrent function was not called before. */
610 if (ext_ent.stream == NULL)
611 status = internal_setgrent (&ext_ent);
613 if (status == NSS_STATUS_SUCCESS)
614 status = internal_getgrent_r (grp, &ext_ent, buffer, buflen, errnop);
616 __libc_lock_unlock (lock);
618 return status;
621 /* Searches in /etc/group and the NIS/NIS+ map for a special group */
622 static enum nss_status
623 internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
624 char *buffer, size_t buflen, int *errnop)
626 struct parser_data *data = (void *) buffer;
627 while (1)
629 fpos_t pos;
630 int parse_res = 0;
631 char *p;
635 fgetpos (ent->stream, &pos);
636 buffer[buflen - 1] = '\xff';
637 p = fgets (buffer, buflen, ent->stream);
638 if (p == NULL && feof (ent->stream))
640 *errnop = ENOENT;
641 return NSS_STATUS_NOTFOUND;
643 if (p == NULL || buffer[buflen - 1] != '\xff')
645 fsetpos (ent->stream, &pos);
646 *errnop = ERANGE;
647 return NSS_STATUS_TRYAGAIN;
650 /* Terminate the line for any case. */
651 buffer[buflen - 1] = '\0';
653 /* Skip leading blanks. */
654 while (isspace (*p))
655 ++p;
657 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
658 /* Parse the line. If it is invalid, loop to
659 get the next line of the file to parse. */
660 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
661 errnop)));
663 if (parse_res == -1)
665 /* The parser ran out of space. */
666 fsetpos (ent->stream, &pos);
667 *errnop = ERANGE;
668 return NSS_STATUS_TRYAGAIN;
671 /* This is a real entry. */
672 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
674 if (strcmp (result->gr_name, name) == 0)
675 return NSS_STATUS_SUCCESS;
676 else
677 continue;
680 /* -group */
681 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
683 if (strcmp (&result->gr_name[1], name) == 0)
685 *errnop = ENOENT;
686 return NSS_STATUS_NOTFOUND;
688 else
689 continue;
692 /* +group */
693 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
695 if (strcmp (name, &result->gr_name[1]) == 0)
697 enum nss_status status;
699 status = getgrnam_plusgroup (name, result, buffer, buflen,
700 errnop);
701 if (status == NSS_STATUS_RETURN)
702 /* We couldn't parse the entry */
703 continue;
704 else
705 return status;
708 /* +:... */
709 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
711 enum nss_status status;
713 status = getgrnam_plusgroup (name, result, buffer, buflen, errnop);
714 if (status == NSS_STATUS_RETURN)
715 /* We couldn't parse the entry */
716 continue;
717 else
718 return status;
722 return NSS_STATUS_SUCCESS;
725 enum nss_status
726 _nss_compat_getgrnam_r (const char *name, struct group *grp,
727 char *buffer, size_t buflen, int *errnop)
729 ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
730 enum nss_status status;
732 if (name[0] == '-' || name[0] == '+')
734 *errnop = ENOENT;
735 return NSS_STATUS_NOTFOUND;
738 __libc_lock_lock (lock);
740 status = internal_setgrent (&ent);
742 __libc_lock_unlock (lock);
744 if (status != NSS_STATUS_SUCCESS)
745 return status;
747 status = internal_getgrnam_r (name, grp, &ent, buffer, buflen, errnop);
749 internal_endgrent (&ent);
751 return status;
754 /* This function handle the + entry in /etc/group */
755 static enum nss_status
756 getgrgid_plusgroup (gid_t gid, struct group *result, char *buffer,
757 size_t buflen, int *errnop)
759 struct parser_data *data = (void *) buffer;
760 int parse_res;
762 if (use_nisplus) /* Do the NIS+ query here */
764 nis_result *res;
765 char buf[24 + grptablelen];
767 sprintf(buf, "[gid=%d],%s", gid, grptable);
768 res = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
769 if (niserr2nss (res->status) != NSS_STATUS_SUCCESS)
771 enum nss_status status = niserr2nss (res->status);
773 nis_freeresult (res);
774 return status;
776 if ((parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer,
777 buflen, errnop)) == -1)
779 nis_freeresult (res);
780 *errnop = ERANGE;
781 return NSS_STATUS_TRYAGAIN;
783 nis_freeresult (res);
785 else /* Use NIS */
787 char buf[24];
788 char *domain, *outval, *p;
789 int outvallen;
791 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
793 *errnop = ENOENT;
794 return NSS_STATUS_NOTFOUND;
797 snprintf (buf, sizeof (buf), "%d", gid);
799 if (yp_match (domain, "group.bygid", buf, strlen (buf),
800 &outval, &outvallen) != YPERR_SUCCESS)
802 *errnop = ENOENT;
803 return NSS_STATUS_NOTFOUND;
806 if (buflen < ((size_t) outvallen + 1))
808 free (outval);
809 *errnop = ERANGE;
810 return NSS_STATUS_TRYAGAIN;
813 /* Copy the found data to our buffer... */
814 p = strncpy (buffer, outval, buflen);
816 /* ... and free the data. */
817 free (outval);
819 while (isspace (*p))
820 p++;
821 parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
822 if (parse_res == -1)
823 return NSS_STATUS_TRYAGAIN;
826 if (parse_res)
827 /* We found the entry. */
828 return NSS_STATUS_SUCCESS;
829 else
830 return NSS_STATUS_RETURN;
833 /* Searches in /etc/group and the NIS/NIS+ map for a special group id */
834 static enum nss_status
835 internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
836 char *buffer, size_t buflen, int *errnop)
838 struct parser_data *data = (void *) buffer;
839 while (1)
841 fpos_t pos;
842 int parse_res = 0;
843 char *p;
847 fgetpos (ent->stream, &pos);
848 buffer[buflen - 1] = '\xff';
849 p = fgets (buffer, buflen, ent->stream);
850 if (p == NULL && feof (ent->stream))
852 *errnop = ENOENT;
853 return NSS_STATUS_NOTFOUND;
855 if (p == NULL || buffer[buflen - 1] != '\xff')
857 fsetpos (ent->stream, &pos);
858 *errnop = ERANGE;
859 return NSS_STATUS_TRYAGAIN;
862 /* Terminate the line for any case. */
863 buffer[buflen - 1] = '\0';
865 /* Skip leading blanks. */
866 while (isspace (*p))
867 ++p;
869 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
870 /* Parse the line. If it is invalid, loop to
871 get the next line of the file to parse. */
872 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
873 errnop)));
875 if (parse_res == -1)
877 /* The parser ran out of space. */
878 fsetpos (ent->stream, &pos);
879 *errnop = ERANGE;
880 return NSS_STATUS_TRYAGAIN;
883 /* This is a real entry. */
884 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
886 if (result->gr_gid == gid)
887 return NSS_STATUS_SUCCESS;
888 else
889 continue;
892 /* -group */
893 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
895 blacklist_store_name (&result->gr_name[1], ent);
896 continue;
899 /* +group */
900 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
902 enum nss_status status;
904 /* Store the group in the blacklist for the "+" at the end of
905 /etc/group */
906 blacklist_store_name (&result->gr_name[1], ent);
907 status = getgrnam_plusgroup (&result->gr_name[1], result, buffer,
908 buflen, errnop);
909 if (status == NSS_STATUS_SUCCESS && result->gr_gid == gid)
910 break;
911 else
912 continue;
914 /* +:... */
915 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
917 enum nss_status status;
919 status = getgrgid_plusgroup (gid, result, buffer, buflen, errnop);
920 if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
922 *errnop = ENOENT;
923 return NSS_STATUS_NOTFOUND;
925 else
926 return status;
930 return NSS_STATUS_SUCCESS;
933 enum nss_status
934 _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
935 char *buffer, size_t buflen, int *errnop)
937 ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
938 enum nss_status status;
940 __libc_lock_lock (lock);
942 status = internal_setgrent (&ent);
944 __libc_lock_unlock (lock);
946 if (status != NSS_STATUS_SUCCESS)
947 return status;
949 status = internal_getgrgid_r (gid, grp, &ent, buffer, buflen, errnop);
951 internal_endgrent (&ent);
953 return status;
957 /* Support routines for remembering -@netgroup and -user entries.
958 The names are stored in a single string with `|' as separator. */
959 static void
960 blacklist_store_name (const char *name, ent_t *ent)
962 int namelen = strlen (name);
963 char *tmp;
965 /* first call, setup cache */
966 if (ent->blacklist.size == 0)
968 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
969 ent->blacklist.data = malloc (ent->blacklist.size);
970 if (ent->blacklist.data == NULL)
971 return;
972 ent->blacklist.data[0] = '|';
973 ent->blacklist.data[1] = '\0';
974 ent->blacklist.current = 1;
976 else
978 if (in_blacklist (name, namelen, ent))
979 return; /* no duplicates */
981 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
983 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
984 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
985 if (tmp == NULL)
987 free (ent->blacklist.data);
988 ent->blacklist.size = 0;
989 return;
991 ent->blacklist.data = tmp;
995 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
996 *tmp++ = '|';
997 *tmp = '\0';
998 ent->blacklist.current += namelen + 1;
1000 return;
1003 /* returns TRUE if ent->blacklist contains name, else FALSE */
1004 static bool_t
1005 in_blacklist (const char *name, int namelen, ent_t *ent)
1007 char buf[namelen + 3];
1008 char *cp;
1010 if (ent->blacklist.data == NULL)
1011 return FALSE;
1013 buf[0] = '|';
1014 cp = stpcpy (&buf[1], name);
1015 *cp++= '|';
1016 *cp = '\0';
1017 return strstr (ent->blacklist.data, buf) != NULL;