]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_compat/compat-grp.c
Update.
[thirdparty/glibc.git] / nis / nss_compat / compat-grp.c
1 /* Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.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 *errnop = ENOENT;
250 return NSS_STATUS_NOTFOUND;
251 }
252
253 do
254 {
255 char *save_oldkey;
256 int save_oldlen;
257 bool_t save_nis_first;
258
259 if (ent->nis_first)
260 {
261 if (yp_first (domain, "group.byname", &outkey, &outkeylen,
262 &outval, &outvallen) != YPERR_SUCCESS)
263 {
264 ent->nis = 0;
265 return NSS_STATUS_UNAVAIL;
266 }
267
268 if ( buflen < ((size_t) outvallen + 1))
269 {
270 free (outval);
271 *errnop = ERANGE;
272 return NSS_STATUS_TRYAGAIN;
273 }
274
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;
281 }
282 else
283 {
284 if (yp_next (domain, "group.byname", ent->oldkey, ent->oldkeylen,
285 &outkey, &outkeylen, &outval, &outvallen)
286 != YPERR_SUCCESS)
287 {
288 ent->nis = 0;
289 *errnop = ENOENT;
290 return NSS_STATUS_NOTFOUND;
291 }
292
293 if ( buflen < ((size_t) outvallen + 1))
294 {
295 free (outval);
296 *errnop = ERANGE;
297 return NSS_STATUS_TRYAGAIN;
298 }
299
300 save_oldkey = ent->oldkey;
301 save_oldlen = ent->oldkeylen;
302 save_nis_first = FALSE;
303 ent->oldkey = outkey;
304 ent->oldkeylen = outkeylen;
305 }
306
307 /* Copy the found data to our buffer... */
308 p = strncpy (buffer, outval, buflen);
309
310 /* ...and free the data. */
311 free (outval);
312
313 while (isspace (*p))
314 ++p;
315
316 parse_res = _nss_files_parse_grent (p, result, data, buflen, errnop);
317 if (parse_res == -1)
318 {
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;
325 }
326 else
327 {
328 if (!save_nis_first)
329 free (save_oldkey);
330 }
331
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 */
335 }
336 while (!parse_res);
337
338 return NSS_STATUS_SUCCESS;
339 }
340
341 static enum nss_status
342 getgrent_next_nisplus (struct group *result, ent_t *ent, char *buffer,
343 size_t buflen, int *errnop)
344 {
345 int parse_res;
346
347 do
348 {
349 nis_result *save_oldres;
350 bool_t save_nis_first;
351
352 if (ent->nis_first)
353 {
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)
358 {
359 ent->nis = 0;
360 return niserr2nss (ent->result->status);
361 }
362 ent->nis_first = FALSE;
363 }
364 else
365 {
366 nis_result *res;
367
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)
373 {
374 ent->nis = 0;
375 return niserr2nss (ent->result->status);
376 }
377 }
378 parse_res = _nss_nisplus_parse_grent (ent->result, 0, result,
379 buffer, buflen, errnop);
380 if (parse_res == -1)
381 {
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;
387 }
388 else
389 {
390 if (!save_nis_first)
391 nis_freeresult (save_oldres);
392 }
393
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 */
397 }
398 while (!parse_res);
399
400 return NSS_STATUS_SUCCESS;
401 }
402
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)
407 {
408 struct parser_data *data = (void *) buffer;
409 int parse_res;
410
411 if (use_nisplus) /* Do the NIS+ query here */
412 {
413 nis_result *res;
414 char buf[strlen (name) + 24 + grptablelen];
415
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)
419 {
420 enum nss_status status = niserr2nss (res->status);
421
422 nis_freeresult (res);
423 return status;
424 }
425 parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer, buflen,
426 errnop);
427 if (parse_res == -1)
428 {
429 nis_freeresult (res);
430 *errnop = ERANGE;
431 return NSS_STATUS_TRYAGAIN;
432 }
433 nis_freeresult (res);
434 }
435 else /* Use NIS */
436 {
437 char *domain, *outval, *p;
438 int outvallen;
439
440 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
441 {
442 *errnop = ENOENT;
443 return NSS_STATUS_NOTFOUND;
444 }
445
446 if (yp_match (domain, "group.byname", name, strlen (name),
447 &outval, &outvallen) != YPERR_SUCCESS)
448 {
449 *errnop = ENOENT;
450 return NSS_STATUS_NOTFOUND;
451 }
452
453 if (buflen < ((size_t) outvallen + 1))
454 {
455 free (outval);
456 *errnop = ERANGE;
457 return NSS_STATUS_TRYAGAIN;
458 }
459
460 /* Copy the found data to our buffer... */
461 p = strncpy (buffer, outval, buflen);
462
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;
470 }
471
472 if (parse_res)
473 /* We found the entry. */
474 return NSS_STATUS_SUCCESS;
475 else
476 return NSS_STATUS_RETURN;
477 }
478
479 static enum nss_status
480 getgrent_next_file (struct group *result, ent_t *ent,
481 char *buffer, size_t buflen, int *errnop)
482 {
483 struct parser_data *data = (void *) buffer;
484 while (1)
485 {
486 fpos_t pos;
487 int parse_res = 0;
488 char *p;
489
490 do
491 {
492 fgetpos (ent->stream, &pos);
493 buffer[buflen - 1] = '\xff';
494 p = fgets (buffer, buflen, ent->stream);
495 if (p == NULL && feof (ent->stream))
496 {
497 *errnop = ENOENT;
498 return NSS_STATUS_NOTFOUND;
499 }
500 if (p == NULL || buffer[buflen - 1] != '\xff')
501 {
502 fsetpos (ent->stream, &pos);
503 *errnop = ERANGE;
504 return NSS_STATUS_TRYAGAIN;
505 }
506
507 /* Terminate the line for any case. */
508 buffer[buflen - 1] = '\0';
509
510 /* Skip leading blanks. */
511 while (isspace (*p))
512 ++p;
513 }
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)));
519
520 if (parse_res == -1)
521 {
522 /* The parser ran out of space. */
523 fsetpos (ent->stream, &pos);
524 *errnop = ERANGE;
525 return NSS_STATUS_TRYAGAIN;
526 }
527
528 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
529 /* This is a real entry. */
530 break;
531
532 /* -group */
533 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
534 && result->gr_name[1] != '@')
535 {
536 blacklist_store_name (&result->gr_name[1], ent);
537 continue;
538 }
539
540 /* +group */
541 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
542 && result->gr_name[1] != '@')
543 {
544 enum nss_status status;
545
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
558 {
559 if (status == NSS_STATUS_TRYAGAIN)
560 {
561 /* The parser ran out of space. */
562 fsetpos (ent->stream, &pos);
563 *errnop = ERANGE;
564 }
565 return status;
566 }
567 }
568
569 /* +:... */
570 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
571 {
572 ent->nis = TRUE;
573 ent->nis_first = TRUE;
574
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);
579 }
580 }
581
582 return NSS_STATUS_SUCCESS;
583 }
584
585
586 static enum nss_status
587 internal_getgrent_r (struct group *gr, ent_t *ent, char *buffer,
588 size_t buflen, int *errnop)
589 {
590 if (ent->nis)
591 {
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);
596 }
597 else
598 return getgrent_next_file (gr, ent, buffer, buflen, errnop);
599 }
600
601 enum nss_status
602 _nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen,
603 int *errnop)
604 {
605 enum nss_status status = NSS_STATUS_SUCCESS;
606
607 __libc_lock_lock (lock);
608
609 /* Be prepared that the setgrent function was not called before. */
610 if (ext_ent.stream == NULL)
611 status = internal_setgrent (&ext_ent);
612
613 if (status == NSS_STATUS_SUCCESS)
614 status = internal_getgrent_r (grp, &ext_ent, buffer, buflen, errnop);
615
616 __libc_lock_unlock (lock);
617
618 return status;
619 }
620
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)
625 {
626 struct parser_data *data = (void *) buffer;
627 while (1)
628 {
629 fpos_t pos;
630 int parse_res = 0;
631 char *p;
632
633 do
634 {
635 fgetpos (ent->stream, &pos);
636 buffer[buflen - 1] = '\xff';
637 p = fgets (buffer, buflen, ent->stream);
638 if (p == NULL && feof (ent->stream))
639 {
640 *errnop = ENOENT;
641 return NSS_STATUS_NOTFOUND;
642 }
643 if (p == NULL || buffer[buflen - 1] != '\xff')
644 {
645 fsetpos (ent->stream, &pos);
646 *errnop = ERANGE;
647 return NSS_STATUS_TRYAGAIN;
648 }
649
650 /* Terminate the line for any case. */
651 buffer[buflen - 1] = '\0';
652
653 /* Skip leading blanks. */
654 while (isspace (*p))
655 ++p;
656 }
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)));
662
663 if (parse_res == -1)
664 {
665 /* The parser ran out of space. */
666 fsetpos (ent->stream, &pos);
667 *errnop = ERANGE;
668 return NSS_STATUS_TRYAGAIN;
669 }
670
671 /* This is a real entry. */
672 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
673 {
674 if (strcmp (result->gr_name, name) == 0)
675 return NSS_STATUS_SUCCESS;
676 else
677 continue;
678 }
679
680 /* -group */
681 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
682 {
683 if (strcmp (&result->gr_name[1], name) == 0)
684 {
685 *errnop = ENOENT;
686 return NSS_STATUS_NOTFOUND;
687 }
688 else
689 continue;
690 }
691
692 /* +group */
693 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
694 {
695 if (strcmp (name, &result->gr_name[1]) == 0)
696 {
697 enum nss_status status;
698
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;
706 }
707 }
708 /* +:... */
709 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
710 {
711 enum nss_status status;
712
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;
719 }
720 }
721
722 return NSS_STATUS_SUCCESS;
723 }
724
725 enum nss_status
726 _nss_compat_getgrnam_r (const char *name, struct group *grp,
727 char *buffer, size_t buflen, int *errnop)
728 {
729 ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
730 enum nss_status status;
731
732 if (name[0] == '-' || name[0] == '+')
733 {
734 *errnop = ENOENT;
735 return NSS_STATUS_NOTFOUND;
736 }
737
738 __libc_lock_lock (lock);
739
740 status = internal_setgrent (&ent);
741
742 __libc_lock_unlock (lock);
743
744 if (status != NSS_STATUS_SUCCESS)
745 return status;
746
747 status = internal_getgrnam_r (name, grp, &ent, buffer, buflen, errnop);
748
749 internal_endgrent (&ent);
750
751 return status;
752 }
753
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)
758 {
759 struct parser_data *data = (void *) buffer;
760 int parse_res;
761
762 if (use_nisplus) /* Do the NIS+ query here */
763 {
764 nis_result *res;
765 char buf[24 + grptablelen];
766
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)
770 {
771 enum nss_status status = niserr2nss (res->status);
772
773 nis_freeresult (res);
774 return status;
775 }
776 if ((parse_res = _nss_nisplus_parse_grent (res, 0, result, buffer,
777 buflen, errnop)) == -1)
778 {
779 nis_freeresult (res);
780 *errnop = ERANGE;
781 return NSS_STATUS_TRYAGAIN;
782 }
783 nis_freeresult (res);
784 }
785 else /* Use NIS */
786 {
787 char buf[24];
788 char *domain, *outval, *p;
789 int outvallen;
790
791 if (yp_get_default_domain (&domain) != YPERR_SUCCESS)
792 {
793 *errnop = ENOENT;
794 return NSS_STATUS_NOTFOUND;
795 }
796
797 snprintf (buf, sizeof (buf), "%d", gid);
798
799 if (yp_match (domain, "group.bygid", buf, strlen (buf),
800 &outval, &outvallen) != YPERR_SUCCESS)
801 {
802 *errnop = ENOENT;
803 return NSS_STATUS_NOTFOUND;
804 }
805
806 if (buflen < ((size_t) outvallen + 1))
807 {
808 free (outval);
809 *errnop = ERANGE;
810 return NSS_STATUS_TRYAGAIN;
811 }
812
813 /* Copy the found data to our buffer... */
814 p = strncpy (buffer, outval, buflen);
815
816 /* ... and free the data. */
817 free (outval);
818
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;
824 }
825
826 if (parse_res)
827 /* We found the entry. */
828 return NSS_STATUS_SUCCESS;
829 else
830 return NSS_STATUS_RETURN;
831 }
832
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)
837 {
838 struct parser_data *data = (void *) buffer;
839 while (1)
840 {
841 fpos_t pos;
842 int parse_res = 0;
843 char *p;
844
845 do
846 {
847 fgetpos (ent->stream, &pos);
848 buffer[buflen - 1] = '\xff';
849 p = fgets (buffer, buflen, ent->stream);
850 if (p == NULL && feof (ent->stream))
851 {
852 *errnop = ENOENT;
853 return NSS_STATUS_NOTFOUND;
854 }
855 if (p == NULL || buffer[buflen - 1] != '\xff')
856 {
857 fsetpos (ent->stream, &pos);
858 *errnop = ERANGE;
859 return NSS_STATUS_TRYAGAIN;
860 }
861
862 /* Terminate the line for any case. */
863 buffer[buflen - 1] = '\0';
864
865 /* Skip leading blanks. */
866 while (isspace (*p))
867 ++p;
868 }
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)));
874
875 if (parse_res == -1)
876 {
877 /* The parser ran out of space. */
878 fsetpos (ent->stream, &pos);
879 *errnop = ERANGE;
880 return NSS_STATUS_TRYAGAIN;
881 }
882
883 /* This is a real entry. */
884 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
885 {
886 if (result->gr_gid == gid)
887 return NSS_STATUS_SUCCESS;
888 else
889 continue;
890 }
891
892 /* -group */
893 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
894 {
895 blacklist_store_name (&result->gr_name[1], ent);
896 continue;
897 }
898
899 /* +group */
900 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
901 {
902 enum nss_status status;
903
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;
913 }
914 /* +:... */
915 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
916 {
917 enum nss_status status;
918
919 status = getgrgid_plusgroup (gid, result, buffer, buflen, errnop);
920 if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
921 {
922 *errnop = ENOENT;
923 return NSS_STATUS_NOTFOUND;
924 }
925 else
926 return status;
927 }
928 }
929
930 return NSS_STATUS_SUCCESS;
931 }
932
933 enum nss_status
934 _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
935 char *buffer, size_t buflen, int *errnop)
936 {
937 ent_t ent = {0, 0, NULL, 0, NULL, NULL, {NULL, 0, 0}};
938 enum nss_status status;
939
940 __libc_lock_lock (lock);
941
942 status = internal_setgrent (&ent);
943
944 __libc_lock_unlock (lock);
945
946 if (status != NSS_STATUS_SUCCESS)
947 return status;
948
949 status = internal_getgrgid_r (gid, grp, &ent, buffer, buflen, errnop);
950
951 internal_endgrent (&ent);
952
953 return status;
954 }
955
956
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)
961 {
962 int namelen = strlen (name);
963 char *tmp;
964
965 /* first call, setup cache */
966 if (ent->blacklist.size == 0)
967 {
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;
975 }
976 else
977 {
978 if (in_blacklist (name, namelen, ent))
979 return; /* no duplicates */
980
981 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
982 {
983 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
984 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
985 if (tmp == NULL)
986 {
987 free (ent->blacklist.data);
988 ent->blacklist.size = 0;
989 return;
990 }
991 ent->blacklist.data = tmp;
992 }
993 }
994
995 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
996 *tmp++ = '|';
997 *tmp = '\0';
998 ent->blacklist.current += namelen + 1;
999
1000 return;
1001 }
1002
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)
1006 {
1007 char buf[namelen + 3];
1008 char *cp;
1009
1010 if (ent->blacklist.data == NULL)
1011 return FALSE;
1012
1013 buf[0] = '|';
1014 cp = stpcpy (&buf[1], name);
1015 *cp++= '|';
1016 *cp = '\0';
1017 return strstr (ent->blacklist.data, buf) != NULL;
1018 }