(sem_timedwait): Fix a typo.
[glibc/history.git] / nis / nss_compat / compat-grp.c
blobc9750ae1be92e7489ff69c743219d08a6d8df48a
1 /* Copyright (C) 1996,1997,1998,1999,2001,2002 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 return NSS_STATUS_NOTFOUND;
254 char *save_oldkey;
255 int save_oldlen;
256 bool_t save_nis_first;
258 if (ent->nis_first)
260 if (yp_first (domain, "group.byname", &outkey, &outkeylen,
261 &outval, &outvallen) != YPERR_SUCCESS)
263 ent->nis = 0;
264 return NSS_STATUS_UNAVAIL;
267 if ( buflen < ((size_t) outvallen + 1))
269 free (outval);
270 *errnop = ERANGE;
271 return NSS_STATUS_TRYAGAIN;
274 save_oldkey = ent->oldkey;
275 save_oldlen = ent->oldkeylen;
276 save_nis_first = TRUE;
277 ent->oldkey = outkey;
278 ent->oldkeylen = outkeylen;
279 ent->nis_first = FALSE;
281 else
283 if (yp_next (domain, "group.byname", ent->oldkey, ent->oldkeylen,
284 &outkey, &outkeylen, &outval, &outvallen)
285 != YPERR_SUCCESS)
287 ent->nis = 0;
288 return NSS_STATUS_NOTFOUND;
291 if ( buflen < ((size_t) outvallen + 1))
293 free (outval);
294 *errnop = ERANGE;
295 return NSS_STATUS_TRYAGAIN;
298 save_oldkey = ent->oldkey;
299 save_oldlen = ent->oldkeylen;
300 save_nis_first = FALSE;
301 ent->oldkey = outkey;
302 ent->oldkeylen = outkeylen;
305 /* Copy the found data to our buffer... */
306 p = strncpy (buffer, outval, buflen);
308 /* ...and free the data. */
309 free (outval);
311 while (isspace (*p))
312 ++p;
314 parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
315 if (parse_res == -1)
317 free (ent->oldkey);
318 ent->oldkey = save_oldkey;
319 ent->oldkeylen = save_oldlen;
320 ent->nis_first = save_nis_first;
321 *errnop = ERANGE;
322 return NSS_STATUS_TRYAGAIN;
324 else
326 if (!save_nis_first)
327 free (save_oldkey);
330 if (parse_res &&
331 in_blacklist (result->gr_name, strlen (result->gr_name), ent))
332 parse_res = 0; /* if result->gr_name in blacklist,search next entry */
334 while (!parse_res);
336 return NSS_STATUS_SUCCESS;
339 static enum nss_status
340 getgrent_next_nisplus (struct group *result, ent_t *ent, char *buffer,
341 size_t buflen, int *errnop)
343 int parse_res;
347 nis_result *save_oldres;
348 bool_t save_nis_first;
350 if (ent->nis_first)
352 save_oldres = ent->result;
353 save_nis_first = TRUE;
354 ent->result = nis_first_entry(grptable);
355 if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
357 ent->nis = 0;
358 return niserr2nss (ent->result->status);
360 ent->nis_first = FALSE;
362 else
364 nis_result *res;
366 save_oldres = ent->result;
367 save_nis_first = FALSE;
368 res = nis_next_entry(grptable, &ent->result->cookie);
369 ent->result = res;
370 if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
372 ent->nis = 0;
373 return niserr2nss (ent->result->status);
376 parse_res = _nss_nisplus_parse_grent (ent->result, 0, result,
377 buffer, buflen, errnop);
378 if (parse_res == -1)
380 nis_freeresult (ent->result);
381 ent->result = save_oldres;
382 ent->nis_first = save_nis_first;
383 *errnop = ERANGE;
384 return NSS_STATUS_TRYAGAIN;
386 else
388 if (!save_nis_first)
389 nis_freeresult (save_oldres);
392 if (parse_res &&
393 in_blacklist (result->gr_name, strlen (result->gr_name), ent))
394 parse_res = 0; /* if result->gr_name in blacklist,search next entry */
396 while (!parse_res);
398 return NSS_STATUS_SUCCESS;
401 /* This function handle the +group entrys in /etc/group */
402 static enum nss_status
403 getgrnam_plusgroup (const char *name, struct group *result, char *buffer,
404 size_t buflen, int *errnop)
406 struct parser_data *data = (void *) buffer;
407 int parse_res;
409 if (use_nisplus) /* Do the NIS+ query here */
411 nis_result *res;
412 char buf[strlen (name) + 24 + grptablelen];
414 sprintf(buf, "[name=%s],%s", name, grptable);
415 res = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
416 if (niserr2nss (res->status) != NSS_STATUS_SUCCESS)
418 enum nss_status status = niserr2nss (res->status);
420 nis_freeresult (res);
421 return status;
423 parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer, buflen,
424 errnop);
425 if (parse_res == -1)
427 nis_freeresult (res);
428 *errnop = ERANGE;
429 return NSS_STATUS_TRYAGAIN;
431 nis_freeresult (res);
433 else /* Use NIS */
435 char *domain, *outval, *p;
436 int outvallen;
438 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
439 return NSS_STATUS_NOTFOUND;
441 if (yp_match (domain, "group.byname", name, strlen (name),
442 &outval, &outvallen) != YPERR_SUCCESS)
443 return NSS_STATUS_NOTFOUND;
445 if (buflen < ((size_t) outvallen + 1))
447 free (outval);
448 *errnop = ERANGE;
449 return NSS_STATUS_TRYAGAIN;
452 /* Copy the found data to our buffer... */
453 p = strncpy (buffer, outval, buflen);
455 /* ... and free the data. */
456 free (outval);
457 while (isspace (*p))
458 ++p;
459 parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
460 if (parse_res == -1)
461 return NSS_STATUS_TRYAGAIN;
464 if (parse_res)
465 /* We found the entry. */
466 return NSS_STATUS_SUCCESS;
467 else
468 return NSS_STATUS_RETURN;
471 static enum nss_status
472 getgrent_next_file (struct group *result, ent_t *ent,
473 char *buffer, size_t buflen, int *errnop)
475 struct parser_data *data = (void *) buffer;
476 while (1)
478 fpos_t pos;
479 int parse_res = 0;
480 char *p;
484 fgetpos (ent->stream, &pos);
485 buffer[buflen - 1] = '\xff';
486 p = fgets (buffer, buflen, ent->stream);
487 if (p == NULL && feof (ent->stream))
488 return NSS_STATUS_NOTFOUND;
490 if (p == NULL || buffer[buflen - 1] != '\xff')
492 fsetpos (ent->stream, &pos);
493 *errnop = ERANGE;
494 return NSS_STATUS_TRYAGAIN;
497 /* Terminate the line for any case. */
498 buffer[buflen - 1] = '\0';
500 /* Skip leading blanks. */
501 while (isspace (*p))
502 ++p;
504 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
505 /* Parse the line. If it is invalid, loop to
506 get the next line of the file to parse. */
507 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
508 errnop)));
510 if (parse_res == -1)
512 /* The parser ran out of space. */
513 fsetpos (ent->stream, &pos);
514 *errnop = ERANGE;
515 return NSS_STATUS_TRYAGAIN;
518 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
519 /* This is a real entry. */
520 break;
522 /* -group */
523 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
524 && result->gr_name[1] != '@')
526 blacklist_store_name (&result->gr_name[1], ent);
527 continue;
530 /* +group */
531 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
532 && result->gr_name[1] != '@')
534 enum nss_status status;
536 /* Store the group in the blacklist for the "+" at the end of
537 /etc/group */
538 blacklist_store_name (&result->gr_name[1], ent);
539 status = getgrnam_plusgroup (&result->gr_name[1], result, buffer,
540 buflen, errnop);
541 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
542 break;
543 else
544 if (status == NSS_STATUS_RETURN /* We couldn't parse the entry */
545 || status == NSS_STATUS_NOTFOUND) /* No group in NIS */
546 continue;
547 else
549 if (status == NSS_STATUS_TRYAGAIN)
551 /* The parser ran out of space. */
552 fsetpos (ent->stream, &pos);
553 *errnop = ERANGE;
555 return status;
559 /* +:... */
560 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
562 ent->nis = TRUE;
563 ent->nis_first = TRUE;
565 if (use_nisplus)
566 return getgrent_next_nisplus (result, ent, buffer, buflen, errnop);
567 else
568 return getgrent_next_nis (result, ent, buffer, buflen, errnop);
572 return NSS_STATUS_SUCCESS;
576 static enum nss_status
577 internal_getgrent_r (struct group *gr, ent_t *ent, char *buffer,
578 size_t buflen, int *errnop)
580 if (ent->nis)
582 if (use_nisplus)
583 return getgrent_next_nisplus (gr, ent, buffer, buflen, errnop);
584 else
585 return getgrent_next_nis (gr, ent, buffer, buflen, errnop);
587 else
588 return getgrent_next_file (gr, ent, buffer, buflen, errnop);
591 enum nss_status
592 _nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen,
593 int *errnop)
595 enum nss_status status = NSS_STATUS_SUCCESS;
597 __libc_lock_lock (lock);
599 /* Be prepared that the setgrent function was not called before. */
600 if (ext_ent.stream == NULL)
601 status = internal_setgrent (&ext_ent);
603 if (status == NSS_STATUS_SUCCESS)
604 status = internal_getgrent_r (grp, &ext_ent, buffer, buflen, errnop);
606 __libc_lock_unlock (lock);
608 return status;
611 /* Searches in /etc/group and the NIS/NIS+ map for a special group */
612 static enum nss_status
613 internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
614 char *buffer, size_t buflen, int *errnop)
616 struct parser_data *data = (void *) buffer;
617 while (1)
619 fpos_t pos;
620 int parse_res = 0;
621 char *p;
625 fgetpos (ent->stream, &pos);
626 buffer[buflen - 1] = '\xff';
627 p = fgets (buffer, buflen, ent->stream);
628 if (p == NULL && feof (ent->stream))
629 return NSS_STATUS_NOTFOUND;
631 if (p == NULL || buffer[buflen - 1] != '\xff')
633 fsetpos (ent->stream, &pos);
634 *errnop = ERANGE;
635 return NSS_STATUS_TRYAGAIN;
638 /* Terminate the line for any case. */
639 buffer[buflen - 1] = '\0';
641 /* Skip leading blanks. */
642 while (isspace (*p))
643 ++p;
645 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
646 /* Parse the line. If it is invalid, loop to
647 get the next line of the file to parse. */
648 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
649 errnop)));
651 if (parse_res == -1)
653 /* The parser ran out of space. */
654 fsetpos (ent->stream, &pos);
655 *errnop = ERANGE;
656 return NSS_STATUS_TRYAGAIN;
659 /* This is a real entry. */
660 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
662 if (strcmp (result->gr_name, name) == 0)
663 return NSS_STATUS_SUCCESS;
664 else
665 continue;
668 /* -group */
669 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
671 if (strcmp (&result->gr_name[1], name) == 0)
672 return NSS_STATUS_NOTFOUND;
673 else
674 continue;
677 /* +group */
678 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
680 if (strcmp (name, &result->gr_name[1]) == 0)
682 enum nss_status status;
684 status = getgrnam_plusgroup (name, result, buffer, buflen,
685 errnop);
686 if (status == NSS_STATUS_RETURN)
687 /* We couldn't parse the entry */
688 continue;
689 else
690 return status;
693 /* +:... */
694 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
696 enum nss_status status;
698 status = getgrnam_plusgroup (name, result, buffer, buflen, errnop);
699 if (status == NSS_STATUS_RETURN)
700 /* We couldn't parse the entry */
701 continue;
702 else
703 return status;
707 return NSS_STATUS_SUCCESS;
710 enum nss_status
711 _nss_compat_getgrnam_r (const char *name, struct group *grp,
712 char *buffer, size_t buflen, int *errnop)
714 ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
715 enum nss_status status;
717 if (name[0] == '-' || name[0] == '+')
718 return NSS_STATUS_NOTFOUND;
720 __libc_lock_lock (lock);
722 status = internal_setgrent (&ent);
724 __libc_lock_unlock (lock);
726 if (status != NSS_STATUS_SUCCESS)
727 return status;
729 status = internal_getgrnam_r (name, grp, &ent, buffer, buflen, errnop);
731 internal_endgrent (&ent);
733 return status;
736 /* This function handle the + entry in /etc/group */
737 static enum nss_status
738 getgrgid_plusgroup (gid_t gid, struct group *result, char *buffer,
739 size_t buflen, int *errnop)
741 struct parser_data *data = (void *) buffer;
742 int parse_res;
744 if (use_nisplus) /* Do the NIS+ query here */
746 nis_result *res;
747 char buf[24 + grptablelen];
749 sprintf(buf, "[gid=%lu],%s", (unsigned long int) gid, grptable);
750 res = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
751 if (niserr2nss (res->status) != NSS_STATUS_SUCCESS)
753 enum nss_status status = niserr2nss (res->status);
755 nis_freeresult (res);
756 return status;
758 if ((parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer,
759 buflen, errnop)) == -1)
761 nis_freeresult (res);
762 *errnop = ERANGE;
763 return NSS_STATUS_TRYAGAIN;
765 nis_freeresult (res);
767 else /* Use NIS */
769 char buf[24];
770 char *domain, *outval, *p;
771 int outvallen;
773 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
774 return NSS_STATUS_NOTFOUND;
776 snprintf (buf, sizeof (buf), "%lu", (unsigned long int) gid);
778 if (yp_match (domain, "group.bygid", buf, strlen (buf),
779 &outval, &outvallen) != YPERR_SUCCESS)
780 return NSS_STATUS_NOTFOUND;
782 if (buflen < ((size_t) outvallen + 1))
784 free (outval);
785 *errnop = ERANGE;
786 return NSS_STATUS_TRYAGAIN;
789 /* Copy the found data to our buffer... */
790 p = strncpy (buffer, outval, buflen);
792 /* ... and free the data. */
793 free (outval);
795 while (isspace (*p))
796 p++;
797 parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
798 if (parse_res == -1)
799 return NSS_STATUS_TRYAGAIN;
802 if (parse_res)
803 /* We found the entry. */
804 return NSS_STATUS_SUCCESS;
805 else
806 return NSS_STATUS_RETURN;
809 /* Searches in /etc/group and the NIS/NIS+ map for a special group id */
810 static enum nss_status
811 internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
812 char *buffer, size_t buflen, int *errnop)
814 struct parser_data *data = (void *) buffer;
815 while (1)
817 fpos_t pos;
818 int parse_res = 0;
819 char *p;
823 fgetpos (ent->stream, &pos);
824 buffer[buflen - 1] = '\xff';
825 p = fgets (buffer, buflen, ent->stream);
826 if (p == NULL && feof (ent->stream))
827 return NSS_STATUS_NOTFOUND;
829 if (p == NULL || buffer[buflen - 1] != '\xff')
831 fsetpos (ent->stream, &pos);
832 *errnop = ERANGE;
833 return NSS_STATUS_TRYAGAIN;
836 /* Terminate the line for any case. */
837 buffer[buflen - 1] = '\0';
839 /* Skip leading blanks. */
840 while (isspace (*p))
841 ++p;
843 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
844 /* Parse the line. If it is invalid, loop to
845 get the next line of the file to parse. */
846 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
847 errnop)));
849 if (parse_res == -1)
851 /* The parser ran out of space. */
852 fsetpos (ent->stream, &pos);
853 *errnop = ERANGE;
854 return NSS_STATUS_TRYAGAIN;
857 /* This is a real entry. */
858 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
860 if (result->gr_gid == gid)
861 return NSS_STATUS_SUCCESS;
862 else
863 continue;
866 /* -group */
867 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
869 blacklist_store_name (&result->gr_name[1], ent);
870 continue;
873 /* +group */
874 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
876 enum nss_status status;
878 /* Store the group in the blacklist for the "+" at the end of
879 /etc/group */
880 blacklist_store_name (&result->gr_name[1], ent);
881 status = getgrnam_plusgroup (&result->gr_name[1], result, buffer,
882 buflen, errnop);
883 if (status == NSS_STATUS_SUCCESS && result->gr_gid == gid)
884 break;
885 else
886 continue;
888 /* +:... */
889 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
891 enum nss_status status;
893 status = getgrgid_plusgroup (gid, result, buffer, buflen, errnop);
894 if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
895 return NSS_STATUS_NOTFOUND;
896 else
897 return status;
901 return NSS_STATUS_SUCCESS;
904 enum nss_status
905 _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
906 char *buffer, size_t buflen, int *errnop)
908 ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
909 enum nss_status status;
911 __libc_lock_lock (lock);
913 status = internal_setgrent (&ent);
915 __libc_lock_unlock (lock);
917 if (status != NSS_STATUS_SUCCESS)
918 return status;
920 status = internal_getgrgid_r (gid, grp, &ent, buffer, buflen, errnop);
922 internal_endgrent (&ent);
924 return status;
928 /* Support routines for remembering -@netgroup and -user entries.
929 The names are stored in a single string with `|' as separator. */
930 static void
931 blacklist_store_name (const char *name, ent_t *ent)
933 int namelen = strlen (name);
934 char *tmp;
936 /* first call, setup cache */
937 if (ent->blacklist.size == 0)
939 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
940 ent->blacklist.data = malloc (ent->blacklist.size);
941 if (ent->blacklist.data == NULL)
942 return;
943 ent->blacklist.data[0] = '|';
944 ent->blacklist.data[1] = '\0';
945 ent->blacklist.current = 1;
947 else
949 if (in_blacklist (name, namelen, ent))
950 return; /* no duplicates */
952 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
954 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
955 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
956 if (tmp == NULL)
958 free (ent->blacklist.data);
959 ent->blacklist.size = 0;
960 return;
962 ent->blacklist.data = tmp;
966 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
967 *tmp++ = '|';
968 *tmp = '\0';
969 ent->blacklist.current += namelen + 1;
971 return;
974 /* returns TRUE if ent->blacklist contains name, else FALSE */
975 static bool_t
976 in_blacklist (const char *name, int namelen, ent_t *ent)
978 char buf[namelen + 3];
979 char *cp;
981 if (ent->blacklist.data == NULL)
982 return FALSE;
984 buf[0] = '|';
985 cp = stpcpy (&buf[1], name);
986 *cp++= '|';
987 *cp = '\0';
988 return strstr (ent->blacklist.data, buf) != NULL;