]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_compat/compat-grp.c
4b873d69153820b002c6ace396cafb41fb3e69da
[thirdparty/glibc.git] / nis / nss_compat / compat-grp.c
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.
4
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.
9
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.
14
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. */
19
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>
31
32 #include "nss-nisplus.h"
33 #include "nisplus-parser.h"
34
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;
39
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>
45
46 /* Structure for remembering -group members ... */
47 #define BLACKLIST_INITIAL_SIZE 512
48 #define BLACKLIST_INCREMENT 256
49 struct blacklist_t
50 {
51 char *data;
52 int current;
53 int size;
54 };
55
56 struct ent_t
57 {
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;
65 };
66 typedef struct ent_t ent_t;
67
68 static ent_t ext_ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
69
70 /* Protect global state against multiple changers. */
71 __libc_lock_define_initialized (static, lock)
72
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 *);
76
77 static enum nss_status
78 _nss_first_init (void)
79 {
80 if (ni == NULL)
81 {
82 __nss_database_lookup ("group_compat", NULL, "nis", &ni);
83 use_nisplus = (strcmp (ni->name, "nisplus") == 0);
84 }
85
86 if (grptable == NULL)
87 {
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);
91
92 grptable = malloc (sizeof (key) + len_local_dir);
93 if (grptable == NULL)
94 return NSS_STATUS_TRYAGAIN;
95
96 grptablelen = ((char *) mempcpy (mempcpy (grptable,
97 key, sizeof (key) - 1),
98 local_dir, len_local_dir + 1)
99 - grptable) - 1;
100 }
101
102 return NSS_STATUS_SUCCESS;
103 }
104
105 static enum nss_status
106 internal_setgrent (ent_t *ent)
107 {
108 enum nss_status status = NSS_STATUS_SUCCESS;
109
110 ent->nis = ent->nis_first = 0;
111
112 if (_nss_first_init () != NSS_STATUS_SUCCESS)
113 return NSS_STATUS_UNAVAIL;
114
115 if (ent->oldkey != NULL)
116 {
117 free (ent->oldkey);
118 ent->oldkey = NULL;
119 ent->oldkeylen = 0;
120 }
121
122 if (ent->result != NULL)
123 {
124 nis_freeresult (ent->result);
125 ent->result = NULL;
126 }
127
128 if (ent->blacklist.data != NULL)
129 {
130 ent->blacklist.current = 1;
131 ent->blacklist.data[0] = '|';
132 ent->blacklist.data[1] = '\0';
133 }
134 else
135 ent->blacklist.current = 0;
136
137 if (ent->stream == NULL)
138 {
139 ent->stream = fopen ("/etc/group", "r");
140
141 if (ent->stream == NULL)
142 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
143 else
144 {
145 /* We have to make sure the file is `closed on exec'. */
146 int result, flags;
147
148 result = flags = fcntl (fileno (ent->stream), F_GETFD, 0);
149 if (result >= 0)
150 {
151 flags |= FD_CLOEXEC;
152 result = fcntl (fileno (ent->stream), F_SETFD, flags);
153 }
154 if (result < 0)
155 {
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;
161 }
162 }
163 }
164 else
165 rewind (ent->stream);
166
167 return status;
168 }
169
170
171 enum nss_status
172 _nss_compat_setgrent (void)
173 {
174 enum nss_status result;
175
176 __libc_lock_lock (lock);
177
178 result = internal_setgrent (&ext_ent);
179
180 __libc_lock_unlock (lock);
181
182 return result;
183 }
184
185
186 static enum nss_status
187 internal_endgrent (ent_t *ent)
188 {
189 if (ent->stream != NULL)
190 {
191 fclose (ent->stream);
192 ent->stream = NULL;
193 }
194
195 ent->nis = ent->nis_first = 0;
196
197 if (ent->oldkey != NULL)
198 {
199 free (ent->oldkey);
200 ent->oldkey = NULL;
201 ent->oldkeylen = 0;
202 }
203
204 if (ent->result != NULL)
205 {
206 nis_freeresult (ent->result);
207 ent->result = NULL;
208 }
209
210 if (ent->blacklist.data != NULL)
211 {
212 ent->blacklist.current = 1;
213 ent->blacklist.data[0] = '|';
214 ent->blacklist.data[1] = '\0';
215 }
216 else
217 ent->blacklist.current = 0;
218
219 return NSS_STATUS_SUCCESS;
220 }
221
222 enum nss_status
223 _nss_compat_endgrent (void)
224 {
225 enum nss_status result;
226
227 __libc_lock_lock (lock);
228
229 result = internal_endgrent (&ext_ent);
230
231 __libc_lock_unlock (lock);
232
233 return result;
234 }
235
236 static enum nss_status
237 getgrent_next_nis (struct group *result, ent_t *ent, char *buffer,
238 size_t buflen, int *errnop)
239 {
240 struct parser_data *data = (void *) buffer;
241 char *domain;
242 char *outkey, *outval;
243 int outkeylen, outvallen, parse_res;
244 char *p;
245
246 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
247 {
248 ent->nis = 0;
249 return NSS_STATUS_NOTFOUND;
250 }
251
252 do
253 {
254 char *save_oldkey;
255 int save_oldlen;
256 bool_t save_nis_first;
257
258 if (ent->nis_first)
259 {
260 if (yp_first (domain, "group.byname", &outkey, &outkeylen,
261 &outval, &outvallen) != YPERR_SUCCESS)
262 {
263 ent->nis = 0;
264 return NSS_STATUS_UNAVAIL;
265 }
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;
272 }
273 else
274 {
275 if (yp_next (domain, "group.byname", ent->oldkey, ent->oldkeylen,
276 &outkey, &outkeylen, &outval, &outvallen)
277 != YPERR_SUCCESS)
278 {
279 ent->nis = 0;
280 return NSS_STATUS_NOTFOUND;
281 }
282
283 save_oldkey = ent->oldkey;
284 save_oldlen = ent->oldkeylen;
285 save_nis_first = FALSE;
286 ent->oldkey = outkey;
287 ent->oldkeylen = outkeylen;
288 }
289
290 /* Copy the found data to our buffer */
291 p = strncpy (buffer, outval, buflen);
292
293 /* ...and free the data. */
294 free (outval);
295
296 while (isspace (*p))
297 ++p;
298
299 parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
300 if (parse_res == -1)
301 {
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;
308 }
309 else
310 {
311 if (!save_nis_first)
312 free (save_oldkey);
313 }
314
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 */
318 }
319 while (!parse_res);
320
321 return NSS_STATUS_SUCCESS;
322 }
323
324 static enum nss_status
325 getgrent_next_nisplus (struct group *result, ent_t *ent, char *buffer,
326 size_t buflen, int *errnop)
327 {
328 int parse_res;
329
330 do
331 {
332 nis_result *save_oldres;
333 bool_t save_nis_first;
334
335 if (ent->nis_first)
336 {
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)
341 {
342 ent->nis = 0;
343 return niserr2nss (ent->result->status);
344 }
345 ent->nis_first = FALSE;
346 }
347 else
348 {
349 nis_result *res;
350
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)
356 {
357 ent->nis = 0;
358 return niserr2nss (ent->result->status);
359 }
360 }
361 parse_res = _nss_nisplus_parse_grent (ent->result, 0, result,
362 buffer, buflen, errnop);
363 if (parse_res == -1)
364 {
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;
370 }
371 else
372 {
373 if (!save_nis_first)
374 nis_freeresult (save_oldres);
375 }
376
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 */
380 }
381 while (!parse_res);
382
383 return NSS_STATUS_SUCCESS;
384 }
385
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)
390 {
391 struct parser_data *data = (void *) buffer;
392 int parse_res;
393
394 if (use_nisplus) /* Do the NIS+ query here */
395 {
396 nis_result *res;
397 char buf[strlen (name) + 24 + grptablelen];
398
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)
402 {
403 enum nss_status status = niserr2nss (res->status);
404
405 nis_freeresult (res);
406 return status;
407 }
408 parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer, buflen,
409 errnop);
410 if (parse_res == -1)
411 {
412 nis_freeresult (res);
413 *errnop = ERANGE;
414 return NSS_STATUS_TRYAGAIN;
415 }
416 nis_freeresult (res);
417 }
418 else /* Use NIS */
419 {
420 char *domain, *outval, *p;
421 int outvallen;
422
423 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
424 return NSS_STATUS_NOTFOUND;
425
426 if (yp_match (domain, "group.byname", name, strlen (name),
427 &outval, &outvallen) != YPERR_SUCCESS)
428 return NSS_STATUS_NOTFOUND;
429
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;
438 }
439
440 if (parse_res)
441 /* We found the entry. */
442 return NSS_STATUS_SUCCESS;
443 else
444 return NSS_STATUS_RETURN;
445 }
446
447 static enum nss_status
448 getgrent_next_file (struct group *result, ent_t *ent,
449 char *buffer, size_t buflen, int *errnop)
450 {
451 struct parser_data *data = (void *) buffer;
452 while (1)
453 {
454 fpos_t pos;
455 int parse_res = 0;
456 char *p;
457
458 do
459 {
460 fgetpos (ent->stream, &pos);
461 p = fgets (buffer, buflen, ent->stream);
462 if (p == NULL)
463 {
464 if (feof (ent->stream))
465 return NSS_STATUS_NOTFOUND;
466 else
467 {
468 fsetpos (ent->stream, &pos);
469 *errnop = ERANGE;
470 return NSS_STATUS_TRYAGAIN;
471 }
472 }
473
474 /* Terminate the line for any case. */
475 buffer[buflen - 1] = '\0';
476
477 /* Skip leading blanks. */
478 while (isspace (*p))
479 ++p;
480 }
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)));
486
487 if (parse_res == -1)
488 {
489 /* The parser ran out of space. */
490 fsetpos (ent->stream, &pos);
491 *errnop = ERANGE;
492 return NSS_STATUS_TRYAGAIN;
493 }
494
495 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
496 /* This is a real entry. */
497 break;
498
499 /* -group */
500 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
501 && result->gr_name[1] != '@')
502 {
503 blacklist_store_name (&result->gr_name[1], ent);
504 continue;
505 }
506
507 /* +group */
508 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
509 && result->gr_name[1] != '@')
510 {
511 enum nss_status status;
512
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
525 {
526 if (status == NSS_STATUS_TRYAGAIN)
527 {
528 /* The parser ran out of space. */
529 fsetpos (ent->stream, &pos);
530 *errnop = ERANGE;
531 }
532 return status;
533 }
534 }
535
536 /* +:... */
537 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
538 {
539 ent->nis = TRUE;
540 ent->nis_first = TRUE;
541
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);
546 }
547 }
548
549 return NSS_STATUS_SUCCESS;
550 }
551
552
553 static enum nss_status
554 internal_getgrent_r (struct group *gr, ent_t *ent, char *buffer,
555 size_t buflen, int *errnop)
556 {
557 if (ent->nis)
558 {
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);
563 }
564 else
565 return getgrent_next_file (gr, ent, buffer, buflen, errnop);
566 }
567
568 enum nss_status
569 _nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen,
570 int *errnop)
571 {
572 enum nss_status status = NSS_STATUS_SUCCESS;
573
574 __libc_lock_lock (lock);
575
576 /* Be prepared that the setgrent function was not called before. */
577 if (ext_ent.stream == NULL)
578 status = internal_setgrent (&ext_ent);
579
580 if (status == NSS_STATUS_SUCCESS)
581 status = internal_getgrent_r (grp, &ext_ent, buffer, buflen, errnop);
582
583 __libc_lock_unlock (lock);
584
585 return status;
586 }
587
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)
592 {
593 struct parser_data *data = (void *) buffer;
594 while (1)
595 {
596 fpos_t pos;
597 int parse_res = 0;
598 char *p;
599
600 do
601 {
602 fgetpos (ent->stream, &pos);
603 p = fgets (buffer, buflen, ent->stream);
604 if (p == NULL)
605 {
606 if (feof (ent->stream))
607 return NSS_STATUS_NOTFOUND;
608 else
609 {
610 fsetpos (ent->stream, &pos);
611 *errnop = ERANGE;
612 return NSS_STATUS_TRYAGAIN;
613 }
614 }
615
616 /* Terminate the line for any case. */
617 buffer[buflen - 1] = '\0';
618
619 /* Skip leading blanks. */
620 while (isspace (*p))
621 ++p;
622 }
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)));
628
629 if (parse_res == -1)
630 {
631 /* The parser ran out of space. */
632 fsetpos (ent->stream, &pos);
633 *errnop = ERANGE;
634 return NSS_STATUS_TRYAGAIN;
635 }
636
637 /* This is a real entry. */
638 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
639 {
640 if (strcmp (result->gr_name, name) == 0)
641 return NSS_STATUS_SUCCESS;
642 else
643 continue;
644 }
645
646 /* -group */
647 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
648 {
649 if (strcmp (&result->gr_name[1], name) == 0)
650 return NSS_STATUS_NOTFOUND;
651 else
652 continue;
653 }
654
655 /* +group */
656 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
657 {
658 if (strcmp (name, &result->gr_name[1]) == 0)
659 {
660 enum nss_status status;
661
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;
669 }
670 }
671 /* +:... */
672 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
673 {
674 enum nss_status status;
675
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;
682 }
683 }
684
685 return NSS_STATUS_SUCCESS;
686 }
687
688 enum nss_status
689 _nss_compat_getgrnam_r (const char *name, struct group *grp,
690 char *buffer, size_t buflen, int *errnop)
691 {
692 ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
693 enum nss_status status;
694
695 if (name[0] == '-' || name[0] == '+')
696 return NSS_STATUS_NOTFOUND;
697
698 __libc_lock_lock (lock);
699
700 status = internal_setgrent (&ent);
701
702 __libc_lock_unlock (lock);
703
704 if (status != NSS_STATUS_SUCCESS)
705 return status;
706
707 status = internal_getgrnam_r (name, grp, &ent, buffer, buflen, errnop);
708
709 internal_endgrent (&ent);
710
711 return status;
712 }
713
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)
718 {
719 struct parser_data *data = (void *) buffer;
720 int parse_res;
721
722 if (use_nisplus) /* Do the NIS+ query here */
723 {
724 nis_result *res;
725 char buf[24 + grptablelen];
726
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)
730 {
731 enum nss_status status = niserr2nss (res->status);
732
733 nis_freeresult (res);
734 return status;
735 }
736 if ((parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer,
737 buflen, errnop)) == -1)
738 {
739 nis_freeresult (res);
740 *errnop = ERANGE;
741 return NSS_STATUS_TRYAGAIN;
742 }
743 nis_freeresult (res);
744 }
745 else /* Use NIS */
746 {
747 char buf[24];
748 char *domain, *outval, *p;
749 int outvallen;
750
751 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
752 {
753 *errnop = errno;
754 return NSS_STATUS_TRYAGAIN;
755 }
756
757 snprintf (buf, sizeof (buf), "%d", gid);
758
759 if (yp_match (domain, "group.bygid", buf, strlen (buf),
760 &outval, &outvallen) != YPERR_SUCCESS)
761 {
762 *errnop = errno;
763 return NSS_STATUS_TRYAGAIN;
764 }
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;
773 }
774
775 if (parse_res)
776 /* We found the entry. */
777 return NSS_STATUS_SUCCESS;
778 else
779 return NSS_STATUS_RETURN;
780 }
781
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)
786 {
787 struct parser_data *data = (void *) buffer;
788 while (1)
789 {
790 fpos_t pos;
791 int parse_res = 0;
792 char *p;
793
794 do
795 {
796 fgetpos (ent->stream, &pos);
797 p = fgets (buffer, buflen, ent->stream);
798 if (p == NULL)
799 {
800 if (feof (ent->stream))
801 return NSS_STATUS_NOTFOUND;
802 else
803 {
804 fsetpos (ent->stream, &pos);
805 *errnop = ERANGE;
806 return NSS_STATUS_TRYAGAIN;
807 }
808 }
809
810 /* Terminate the line for any case. */
811 buffer[buflen - 1] = '\0';
812
813 /* Skip leading blanks. */
814 while (isspace (*p))
815 ++p;
816 }
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)));
822
823 if (parse_res == -1)
824 {
825 /* The parser ran out of space. */
826 fsetpos (ent->stream, &pos);
827 *errnop = ERANGE;
828 return NSS_STATUS_TRYAGAIN;
829 }
830
831 /* This is a real entry. */
832 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
833 {
834 if (result->gr_gid == gid)
835 return NSS_STATUS_SUCCESS;
836 else
837 continue;
838 }
839
840 /* -group */
841 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
842 {
843 blacklist_store_name (&result->gr_name[1], ent);
844 continue;
845 }
846
847 /* +group */
848 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
849 {
850 enum nss_status status;
851
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;
861 }
862 /* +:... */
863 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
864 {
865 enum nss_status status;
866
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;
872 }
873 }
874
875 return NSS_STATUS_SUCCESS;
876 }
877
878 enum nss_status
879 _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
880 char *buffer, size_t buflen, int *errnop)
881 {
882 ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
883 enum nss_status status;
884
885 __libc_lock_lock (lock);
886
887 status = internal_setgrent (&ent);
888
889 __libc_lock_unlock (lock);
890
891 if (status != NSS_STATUS_SUCCESS)
892 return status;
893
894 status = internal_getgrgid_r (gid, grp, &ent, buffer, buflen, errnop);
895
896 internal_endgrent (&ent);
897
898 return status;
899 }
900
901
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)
906 {
907 int namelen = strlen (name);
908 char *tmp;
909
910 /* first call, setup cache */
911 if (ent->blacklist.size == 0)
912 {
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;
920 }
921 else
922 {
923 if (in_blacklist (name, namelen, ent))
924 return; /* no duplicates */
925
926 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
927 {
928 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
929 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
930 if (tmp == NULL)
931 {
932 free (ent->blacklist.data);
933 ent->blacklist.size = 0;
934 return;
935 }
936 ent->blacklist.data = tmp;
937 }
938 }
939
940 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
941 *tmp++ = '|';
942 *tmp = '\0';
943 ent->blacklist.current += namelen + 1;
944
945 return;
946 }
947
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)
951 {
952 char buf[namelen + 3];
953 char *cp;
954
955 if (ent->blacklist.data == NULL)
956 return FALSE;
957
958 buf[0] = '|';
959 cp = stpcpy (&buf[1], name);
960 *cp++= '|';
961 *cp = '\0';
962 return strstr (ent->blacklist.data, buf) != NULL;
963 }