Update.
[glibc.git] / nis / nss_compat / compat-grp.c
blob4b873d69153820b002c6ace396cafb41fb3e69da
1 /* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 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 = NULL;
36 static bool_t use_nisplus = FALSE; /* default: group_compat: nis */
37 static nis_name grptable = NULL; /* Name of the group table */
38 static size_t grptablelen = 0;
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 (void)
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;
266 save_oldkey = ent->oldkey;
267 save_oldlen = ent->oldkeylen;
268 save_nis_first = TRUE;
269 ent->oldkey = outkey;
270 ent->oldkeylen = outkeylen;
271 ent->nis_first = FALSE;
273 else
275 if (yp_next (domain, "group.byname", ent->oldkey, ent->oldkeylen,
276 &outkey, &outkeylen, &outval, &outvallen)
277 != YPERR_SUCCESS)
279 ent->nis = 0;
280 return NSS_STATUS_NOTFOUND;
283 save_oldkey = ent->oldkey;
284 save_oldlen = ent->oldkeylen;
285 save_nis_first = FALSE;
286 ent->oldkey = outkey;
287 ent->oldkeylen = outkeylen;
290 /* Copy the found data to our buffer */
291 p = strncpy (buffer, outval, buflen);
293 /* ...and free the data. */
294 free (outval);
296 while (isspace (*p))
297 ++p;
299 parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
300 if (parse_res == -1)
302 free (ent->oldkey);
303 ent->oldkey = save_oldkey;
304 ent->oldkeylen = save_oldlen;
305 ent->nis_first = save_nis_first;
306 *errnop = ERANGE;
307 return NSS_STATUS_TRYAGAIN;
309 else
311 if (!save_nis_first)
312 free (save_oldkey);
315 if (parse_res &&
316 in_blacklist (result->gr_name, strlen (result->gr_name), ent))
317 parse_res = 0; /* if result->gr_name in blacklist,search next entry */
319 while (!parse_res);
321 return NSS_STATUS_SUCCESS;
324 static enum nss_status
325 getgrent_next_nisplus (struct group *result, ent_t *ent, char *buffer,
326 size_t buflen, int *errnop)
328 int parse_res;
332 nis_result *save_oldres;
333 bool_t save_nis_first;
335 if (ent->nis_first)
337 save_oldres = ent->result;
338 save_nis_first = TRUE;
339 ent->result = nis_first_entry(grptable);
340 if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
342 ent->nis = 0;
343 return niserr2nss (ent->result->status);
345 ent->nis_first = FALSE;
347 else
349 nis_result *res;
351 save_oldres = ent->result;
352 save_nis_first = FALSE;
353 res = nis_next_entry(grptable, &ent->result->cookie);
354 ent->result = res;
355 if (niserr2nss (ent->result->status) != NSS_STATUS_SUCCESS)
357 ent->nis = 0;
358 return niserr2nss (ent->result->status);
361 parse_res = _nss_nisplus_parse_grent (ent->result, 0, result,
362 buffer, buflen, errnop);
363 if (parse_res == -1)
365 nis_freeresult (ent->result);
366 ent->result = save_oldres;
367 ent->nis_first = save_nis_first;
368 *errnop = ERANGE;
369 return NSS_STATUS_TRYAGAIN;
371 else
373 if (!save_nis_first)
374 nis_freeresult (save_oldres);
377 if (parse_res &&
378 in_blacklist (result->gr_name, strlen (result->gr_name), ent))
379 parse_res = 0; /* if result->gr_name in blacklist,search next entry */
381 while (!parse_res);
383 return NSS_STATUS_SUCCESS;
386 /* This function handle the +group entrys in /etc/group */
387 static enum nss_status
388 getgrnam_plusgroup (const char *name, struct group *result, char *buffer,
389 size_t buflen, int *errnop)
391 struct parser_data *data = (void *) buffer;
392 int parse_res;
394 if (use_nisplus) /* Do the NIS+ query here */
396 nis_result *res;
397 char buf[strlen (name) + 24 + grptablelen];
399 sprintf(buf, "[name=%s],%s", name, grptable);
400 res = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
401 if (niserr2nss (res->status) != NSS_STATUS_SUCCESS)
403 enum nss_status status = niserr2nss (res->status);
405 nis_freeresult (res);
406 return status;
408 parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer, buflen,
409 errnop);
410 if (parse_res == -1)
412 nis_freeresult (res);
413 *errnop = ERANGE;
414 return NSS_STATUS_TRYAGAIN;
416 nis_freeresult (res);
418 else /* Use NIS */
420 char *domain, *outval, *p;
421 int outvallen;
423 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
424 return NSS_STATUS_NOTFOUND;
426 if (yp_match (domain, "group.byname", name, strlen (name),
427 &outval, &outvallen) != YPERR_SUCCESS)
428 return NSS_STATUS_NOTFOUND;
430 p = strncpy (buffer, outval,
431 buflen < (size_t) outvallen ? buflen : (size_t) outvallen);
432 free (outval);
433 while (isspace (*p))
434 ++p;
435 parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
436 if (parse_res == -1)
437 return NSS_STATUS_TRYAGAIN;
440 if (parse_res)
441 /* We found the entry. */
442 return NSS_STATUS_SUCCESS;
443 else
444 return NSS_STATUS_RETURN;
447 static enum nss_status
448 getgrent_next_file (struct group *result, ent_t *ent,
449 char *buffer, size_t buflen, int *errnop)
451 struct parser_data *data = (void *) buffer;
452 while (1)
454 fpos_t pos;
455 int parse_res = 0;
456 char *p;
460 fgetpos (ent->stream, &pos);
461 p = fgets (buffer, buflen, ent->stream);
462 if (p == NULL)
464 if (feof (ent->stream))
465 return NSS_STATUS_NOTFOUND;
466 else
468 fsetpos (ent->stream, &pos);
469 *errnop = ERANGE;
470 return NSS_STATUS_TRYAGAIN;
474 /* Terminate the line for any case. */
475 buffer[buflen - 1] = '\0';
477 /* Skip leading blanks. */
478 while (isspace (*p))
479 ++p;
481 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
482 /* Parse the line. If it is invalid, loop to
483 get the next line of the file to parse. */
484 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
485 errnop)));
487 if (parse_res == -1)
489 /* The parser ran out of space. */
490 fsetpos (ent->stream, &pos);
491 *errnop = ERANGE;
492 return NSS_STATUS_TRYAGAIN;
495 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
496 /* This is a real entry. */
497 break;
499 /* -group */
500 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
501 && result->gr_name[1] != '@')
503 blacklist_store_name (&result->gr_name[1], ent);
504 continue;
507 /* +group */
508 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
509 && result->gr_name[1] != '@')
511 enum nss_status status;
513 /* Store the group in the blacklist for the "+" at the end of
514 /etc/group */
515 blacklist_store_name (&result->gr_name[1], ent);
516 status = getgrnam_plusgroup (&result->gr_name[1], result, buffer,
517 buflen, errnop);
518 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
519 break;
520 else
521 if (status == NSS_STATUS_RETURN /* We couldn't parse the entry */
522 || status == NSS_STATUS_NOTFOUND) /* No group in NIS */
523 continue;
524 else
526 if (status == NSS_STATUS_TRYAGAIN)
528 /* The parser ran out of space. */
529 fsetpos (ent->stream, &pos);
530 *errnop = ERANGE;
532 return status;
536 /* +:... */
537 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
539 ent->nis = TRUE;
540 ent->nis_first = TRUE;
542 if (use_nisplus)
543 return getgrent_next_nisplus (result, ent, buffer, buflen, errnop);
544 else
545 return getgrent_next_nis (result, ent, buffer, buflen, errnop);
549 return NSS_STATUS_SUCCESS;
553 static enum nss_status
554 internal_getgrent_r (struct group *gr, ent_t *ent, char *buffer,
555 size_t buflen, int *errnop)
557 if (ent->nis)
559 if (use_nisplus)
560 return getgrent_next_nisplus (gr, ent, buffer, buflen, errnop);
561 else
562 return getgrent_next_nis (gr, ent, buffer, buflen, errnop);
564 else
565 return getgrent_next_file (gr, ent, buffer, buflen, errnop);
568 enum nss_status
569 _nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen,
570 int *errnop)
572 enum nss_status status = NSS_STATUS_SUCCESS;
574 __libc_lock_lock (lock);
576 /* Be prepared that the setgrent function was not called before. */
577 if (ext_ent.stream == NULL)
578 status = internal_setgrent (&ext_ent);
580 if (status == NSS_STATUS_SUCCESS)
581 status = internal_getgrent_r (grp, &ext_ent, buffer, buflen, errnop);
583 __libc_lock_unlock (lock);
585 return status;
588 /* Searches in /etc/group and the NIS/NIS+ map for a special group */
589 static enum nss_status
590 internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
591 char *buffer, size_t buflen, int *errnop)
593 struct parser_data *data = (void *) buffer;
594 while (1)
596 fpos_t pos;
597 int parse_res = 0;
598 char *p;
602 fgetpos (ent->stream, &pos);
603 p = fgets (buffer, buflen, ent->stream);
604 if (p == NULL)
606 if (feof (ent->stream))
607 return NSS_STATUS_NOTFOUND;
608 else
610 fsetpos (ent->stream, &pos);
611 *errnop = ERANGE;
612 return NSS_STATUS_TRYAGAIN;
616 /* Terminate the line for any case. */
617 buffer[buflen - 1] = '\0';
619 /* Skip leading blanks. */
620 while (isspace (*p))
621 ++p;
623 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
624 /* Parse the line. If it is invalid, loop to
625 get the next line of the file to parse. */
626 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
627 errnop)));
629 if (parse_res == -1)
631 /* The parser ran out of space. */
632 fsetpos (ent->stream, &pos);
633 *errnop = ERANGE;
634 return NSS_STATUS_TRYAGAIN;
637 /* This is a real entry. */
638 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
640 if (strcmp (result->gr_name, name) == 0)
641 return NSS_STATUS_SUCCESS;
642 else
643 continue;
646 /* -group */
647 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
649 if (strcmp (&result->gr_name[1], name) == 0)
650 return NSS_STATUS_NOTFOUND;
651 else
652 continue;
655 /* +group */
656 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
658 if (strcmp (name, &result->gr_name[1]) == 0)
660 enum nss_status status;
662 status = getgrnam_plusgroup (name, result, buffer, buflen,
663 errnop);
664 if (status == NSS_STATUS_RETURN)
665 /* We couldn't parse the entry */
666 continue;
667 else
668 return status;
671 /* +:... */
672 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
674 enum nss_status status;
676 status = getgrnam_plusgroup (name, result, buffer, buflen, errnop);
677 if (status == NSS_STATUS_RETURN)
678 /* We couldn't parse the entry */
679 continue;
680 else
681 return status;
685 return NSS_STATUS_SUCCESS;
688 enum nss_status
689 _nss_compat_getgrnam_r (const char *name, struct group *grp,
690 char *buffer, size_t buflen, int *errnop)
692 ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
693 enum nss_status status;
695 if (name[0] == '-' || name[0] == '+')
696 return NSS_STATUS_NOTFOUND;
698 __libc_lock_lock (lock);
700 status = internal_setgrent (&ent);
702 __libc_lock_unlock (lock);
704 if (status != NSS_STATUS_SUCCESS)
705 return status;
707 status = internal_getgrnam_r (name, grp, &ent, buffer, buflen, errnop);
709 internal_endgrent (&ent);
711 return status;
714 /* This function handle the + entry in /etc/group */
715 static enum nss_status
716 getgrgid_plusgroup (gid_t gid, struct group *result, char *buffer,
717 size_t buflen, int *errnop)
719 struct parser_data *data = (void *) buffer;
720 int parse_res;
722 if (use_nisplus) /* Do the NIS+ query here */
724 nis_result *res;
725 char buf[24 + grptablelen];
727 sprintf(buf, "[gid=%d],%s", gid, grptable);
728 res = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
729 if (niserr2nss (res->status) != NSS_STATUS_SUCCESS)
731 enum nss_status status = niserr2nss (res->status);
733 nis_freeresult (res);
734 return status;
736 if ((parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer,
737 buflen, errnop)) == -1)
739 nis_freeresult (res);
740 *errnop = ERANGE;
741 return NSS_STATUS_TRYAGAIN;
743 nis_freeresult (res);
745 else /* Use NIS */
747 char buf[24];
748 char *domain, *outval, *p;
749 int outvallen;
751 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
753 *errnop = errno;
754 return NSS_STATUS_TRYAGAIN;
757 snprintf (buf, sizeof (buf), "%d", gid);
759 if (yp_match (domain, "group.bygid", buf, strlen (buf),
760 &outval, &outvallen) != YPERR_SUCCESS)
762 *errnop = errno;
763 return NSS_STATUS_TRYAGAIN;
765 p = strncpy (buffer, outval,
766 buflen < (size_t) outvallen ? buflen : (size_t) outvallen);
767 free (outval);
768 while (isspace (*p))
769 p++;
770 parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
771 if (parse_res == -1)
772 return NSS_STATUS_TRYAGAIN;
775 if (parse_res)
776 /* We found the entry. */
777 return NSS_STATUS_SUCCESS;
778 else
779 return NSS_STATUS_RETURN;
782 /* Searches in /etc/group and the NIS/NIS+ map for a special group id */
783 static enum nss_status
784 internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
785 char *buffer, size_t buflen, int *errnop)
787 struct parser_data *data = (void *) buffer;
788 while (1)
790 fpos_t pos;
791 int parse_res = 0;
792 char *p;
796 fgetpos (ent->stream, &pos);
797 p = fgets (buffer, buflen, ent->stream);
798 if (p == NULL)
800 if (feof (ent->stream))
801 return NSS_STATUS_NOTFOUND;
802 else
804 fsetpos (ent->stream, &pos);
805 *errnop = ERANGE;
806 return NSS_STATUS_TRYAGAIN;
810 /* Terminate the line for any case. */
811 buffer[buflen - 1] = '\0';
813 /* Skip leading blanks. */
814 while (isspace (*p))
815 ++p;
817 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
818 /* Parse the line. If it is invalid, loop to
819 get the next line of the file to parse. */
820 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
821 errnop)));
823 if (parse_res == -1)
825 /* The parser ran out of space. */
826 fsetpos (ent->stream, &pos);
827 *errnop = ERANGE;
828 return NSS_STATUS_TRYAGAIN;
831 /* This is a real entry. */
832 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
834 if (result->gr_gid == gid)
835 return NSS_STATUS_SUCCESS;
836 else
837 continue;
840 /* -group */
841 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
843 blacklist_store_name (&result->gr_name[1], ent);
844 continue;
847 /* +group */
848 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
850 enum nss_status status;
852 /* Store the group in the blacklist for the "+" at the end of
853 /etc/group */
854 blacklist_store_name (&result->gr_name[1], ent);
855 status = getgrnam_plusgroup (&result->gr_name[1], result, buffer,
856 buflen, errnop);
857 if (status == NSS_STATUS_SUCCESS && result->gr_gid == gid)
858 break;
859 else
860 continue;
862 /* +:... */
863 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
865 enum nss_status status;
867 status = getgrgid_plusgroup (gid, result, buffer, buflen, errnop);
868 if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
869 return NSS_STATUS_NOTFOUND;
870 else
871 return status;
875 return NSS_STATUS_SUCCESS;
878 enum nss_status
879 _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
880 char *buffer, size_t buflen, int *errnop)
882 ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
883 enum nss_status status;
885 __libc_lock_lock (lock);
887 status = internal_setgrent (&ent);
889 __libc_lock_unlock (lock);
891 if (status != NSS_STATUS_SUCCESS)
892 return status;
894 status = internal_getgrgid_r (gid, grp, &ent, buffer, buflen, errnop);
896 internal_endgrent (&ent);
898 return status;
902 /* Support routines for remembering -@netgroup and -user entries.
903 The names are stored in a single string with `|' as separator. */
904 static void
905 blacklist_store_name (const char *name, ent_t *ent)
907 int namelen = strlen (name);
908 char *tmp;
910 /* first call, setup cache */
911 if (ent->blacklist.size == 0)
913 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
914 ent->blacklist.data = malloc (ent->blacklist.size);
915 if (ent->blacklist.data == NULL)
916 return;
917 ent->blacklist.data[0] = '|';
918 ent->blacklist.data[1] = '\0';
919 ent->blacklist.current = 1;
921 else
923 if (in_blacklist (name, namelen, ent))
924 return; /* no duplicates */
926 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
928 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
929 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
930 if (tmp == NULL)
932 free (ent->blacklist.data);
933 ent->blacklist.size = 0;
934 return;
936 ent->blacklist.data = tmp;
940 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
941 *tmp++ = '|';
942 *tmp = '\0';
943 ent->blacklist.current += namelen + 1;
945 return;
948 /* returns TRUE if ent->blacklist contains name, else FALSE */
949 static bool_t
950 in_blacklist (const char *name, int namelen, ent_t *ent)
952 char buf[namelen + 3];
953 char *cp;
955 if (ent->blacklist.data == NULL)
956 return FALSE;
958 buf[0] = '|';
959 cp = stpcpy (&buf[1], name);
960 *cp++= '|';
961 *cp = '\0';
962 return strstr (ent->blacklist.data, buf) != NULL;