(tests): Add tst-sigset2.
[glibc/history.git] / nis / nss_compat / compat-grp.c
blobd51eb6d0f235f2f5907eb2651cc78d85eb47c985
1 /* Copyright (C) 1996-1999,2001-2004,2005 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 <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <grp.h>
24 #include <nss.h>
25 #include <nsswitch.h>
26 #include <stdio_ext.h>
27 #include <string.h>
28 #include <rpc/types.h>
29 #include <bits/libc-lock.h>
31 static service_user *ni;
32 static enum nss_status (*nss_setgrent) (int stayopen);
33 static enum nss_status (*nss_getgrnam_r) (const char *name,
34 struct group * grp, char *buffer,
35 size_t buflen, int *errnop);
36 static enum nss_status (*nss_getgrgid_r) (gid_t gid, struct group * grp,
37 char *buffer, size_t buflen,
38 int *errnop);
39 static enum nss_status (*nss_getgrent_r) (struct group * grp, char *buffer,
40 size_t buflen, int *errnop);
41 static enum nss_status (*nss_endgrent) (void);
43 /* Get the declaration of the parser function. */
44 #define ENTNAME grent
45 #define STRUCTURE group
46 #define EXTERN_PARSER
47 #include <nss/nss_files/files-parse.c>
49 /* Structure for remembering -group members ... */
50 #define BLACKLIST_INITIAL_SIZE 512
51 #define BLACKLIST_INCREMENT 256
52 struct blacklist_t
54 char *data;
55 int current;
56 int size;
59 struct ent_t
61 bool_t files;
62 FILE *stream;
63 struct blacklist_t blacklist;
65 typedef struct ent_t ent_t;
67 static ent_t ext_ent = {TRUE, NULL, {NULL, 0, 0}};
69 /* Protect global state against multiple changers. */
70 __libc_lock_define_initialized (static, lock)
72 /* Prototypes for local functions. */
73 static void blacklist_store_name (const char *, ent_t *);
74 static int in_blacklist (const char *, int, ent_t *);
76 /* Initialize the NSS interface/functions. The calling function must
77 hold the lock. */
78 static void
79 init_nss_interface (void)
81 if (__nss_database_lookup ("group_compat", NULL, "nis", &ni) >= 0)
83 nss_setgrent = __nss_lookup_function (ni, "setgrent");
84 nss_getgrnam_r = __nss_lookup_function (ni, "getgrnam_r");
85 nss_getgrgid_r = __nss_lookup_function (ni, "getgrgid_r");
86 nss_getgrent_r = __nss_lookup_function (ni, "getgrent_r");
87 nss_endgrent = __nss_lookup_function (ni, "endgrent");
91 static enum nss_status
92 internal_setgrent (ent_t *ent, int stayopen)
94 enum nss_status status = NSS_STATUS_SUCCESS;
96 ent->files = TRUE;
98 if (ent->blacklist.data != NULL)
100 ent->blacklist.current = 1;
101 ent->blacklist.data[0] = '|';
102 ent->blacklist.data[1] = '\0';
104 else
105 ent->blacklist.current = 0;
107 if (ent->stream == NULL)
109 ent->stream = fopen ("/etc/group", "rm");
111 if (ent->stream == NULL)
112 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
113 else
115 /* We have to make sure the file is `closed on exec'. */
116 int result, flags;
118 result = flags = fcntl (fileno_unlocked (ent->stream), F_GETFD, 0);
119 if (result >= 0)
121 flags |= FD_CLOEXEC;
122 result = fcntl (fileno_unlocked (ent->stream), F_SETFD, flags);
124 if (result < 0)
126 /* Something went wrong. Close the stream and return a
127 failure. */
128 fclose (ent->stream);
129 ent->stream = NULL;
130 status = NSS_STATUS_UNAVAIL;
132 else
133 /* We take care of locking ourself. */
134 __fsetlocking (ent->stream, FSETLOCKING_BYCALLER);
137 else
138 rewind (ent->stream);
140 if (status == NSS_STATUS_SUCCESS && nss_setgrent)
141 return nss_setgrent (stayopen);
143 return status;
147 enum nss_status
148 _nss_compat_setgrent (int stayopen)
150 enum nss_status result;
152 __libc_lock_lock (lock);
154 if (ni == NULL)
155 init_nss_interface ();
157 result = internal_setgrent (&ext_ent, stayopen);
159 __libc_lock_unlock (lock);
161 return result;
165 static enum nss_status
166 internal_endgrent (ent_t *ent)
168 if (nss_endgrent)
169 nss_endgrent ();
171 if (ent->stream != NULL)
173 fclose (ent->stream);
174 ent->stream = NULL;
177 if (ent->blacklist.data != NULL)
179 ent->blacklist.current = 1;
180 ent->blacklist.data[0] = '|';
181 ent->blacklist.data[1] = '\0';
183 else
184 ent->blacklist.current = 0;
186 return NSS_STATUS_SUCCESS;
189 enum nss_status
190 _nss_compat_endgrent (void)
192 enum nss_status result;
194 __libc_lock_lock (lock);
196 result = internal_endgrent (&ext_ent);
198 __libc_lock_unlock (lock);
200 return result;
203 /* get the next group from NSS (+ entry) */
204 static enum nss_status
205 getgrent_next_nss (struct group *result, ent_t *ent, char *buffer,
206 size_t buflen, int *errnop)
208 if (!nss_getgrent_r)
209 return NSS_STATUS_UNAVAIL;
213 enum nss_status status;
215 if ((status = nss_getgrent_r (result, buffer, buflen, errnop)) !=
216 NSS_STATUS_SUCCESS)
217 return status;
219 while (in_blacklist (result->gr_name, strlen (result->gr_name), ent));
221 return NSS_STATUS_SUCCESS;
224 /* This function handle the +group entrys in /etc/group */
225 static enum nss_status
226 getgrnam_plusgroup (const char *name, struct group *result, ent_t *ent,
227 char *buffer, size_t buflen, int *errnop)
229 if (!nss_getgrnam_r)
230 return NSS_STATUS_UNAVAIL;
232 enum nss_status status = nss_getgrnam_r (name, result, buffer, buflen,
233 errnop);
234 if (status != NSS_STATUS_SUCCESS)
235 return status;
237 if (in_blacklist (result->gr_name, strlen (result->gr_name), ent))
238 return NSS_STATUS_NOTFOUND;
240 /* We found the entry. */
241 return NSS_STATUS_SUCCESS;
244 static enum nss_status
245 getgrent_next_file (struct group *result, ent_t *ent,
246 char *buffer, size_t buflen, int *errnop)
248 struct parser_data *data = (void *) buffer;
249 while (1)
251 fpos_t pos;
252 int parse_res = 0;
253 char *p;
257 /* We need at least 3 characters for one line. */
258 if (__builtin_expect (buflen < 3, 0))
260 erange:
261 *errnop = ERANGE;
262 return NSS_STATUS_TRYAGAIN;
265 fgetpos (ent->stream, &pos);
266 buffer[buflen - 1] = '\xff';
267 p = fgets_unlocked (buffer, buflen, ent->stream);
268 if (p == NULL && feof_unlocked (ent->stream))
269 return NSS_STATUS_NOTFOUND;
271 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
273 erange_reset:
274 fsetpos (ent->stream, &pos);
275 goto erange;
278 /* Terminate the line for any case. */
279 buffer[buflen - 1] = '\0';
281 /* Skip leading blanks. */
282 while (isspace (*p))
283 ++p;
285 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
286 /* Parse the line. If it is invalid, loop to
287 get the next line of the file to parse. */
288 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
289 errnop)));
291 if (__builtin_expect (parse_res == -1, 0))
292 /* The parser ran out of space. */
293 goto erange_reset;
295 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
296 /* This is a real entry. */
297 break;
299 /* -group */
300 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
301 && result->gr_name[1] != '@')
303 blacklist_store_name (&result->gr_name[1], ent);
304 continue;
307 /* +group */
308 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
309 && result->gr_name[1] != '@')
311 size_t len = strlen (result->gr_name);
312 char buf[len];
313 enum nss_status status;
315 /* Store the group in the blacklist for the "+" at the end of
316 /etc/group */
317 memcpy (buf, &result->gr_name[1], len);
318 status = getgrnam_plusgroup (&result->gr_name[1], result, ent,
319 buffer, buflen, errnop);
320 blacklist_store_name (buf, ent);
321 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
322 break;
323 else if (status == NSS_STATUS_RETURN /* We couldn't parse the entry*/
324 || status == NSS_STATUS_NOTFOUND) /* No group in NIS */
325 continue;
326 else
328 if (status == NSS_STATUS_TRYAGAIN)
329 /* The parser ran out of space. */
330 goto erange_reset;
332 return status;
336 /* +:... */
337 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
339 ent->files = FALSE;
341 return getgrent_next_nss (result, ent, buffer, buflen, errnop);
345 return NSS_STATUS_SUCCESS;
349 enum nss_status
350 _nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen,
351 int *errnop)
353 enum nss_status result = NSS_STATUS_SUCCESS;
355 __libc_lock_lock (lock);
357 /* Be prepared that the setgrent function was not called before. */
358 if (ni == NULL)
359 init_nss_interface ();
361 if (ext_ent.stream == NULL)
362 result = internal_setgrent (&ext_ent, 1);
364 if (result == NSS_STATUS_SUCCESS)
366 if (ext_ent.files)
367 result = getgrent_next_file (grp, &ext_ent, buffer, buflen, errnop);
368 else
369 result = getgrent_next_nss (grp, &ext_ent, buffer, buflen, errnop);
371 __libc_lock_unlock (lock);
373 return result;
376 /* Searches in /etc/group and the NIS/NIS+ map for a special group */
377 static enum nss_status
378 internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
379 char *buffer, size_t buflen, int *errnop)
381 struct parser_data *data = (void *) buffer;
382 while (1)
384 fpos_t pos;
385 int parse_res = 0;
386 char *p;
390 /* We need at least 3 characters for one line. */
391 if (__builtin_expect (buflen < 3, 0))
393 erange:
394 *errnop = ERANGE;
395 return NSS_STATUS_TRYAGAIN;
398 fgetpos (ent->stream, &pos);
399 buffer[buflen - 1] = '\xff';
400 p = fgets_unlocked (buffer, buflen, ent->stream);
401 if (p == NULL && feof_unlocked (ent->stream))
402 return NSS_STATUS_NOTFOUND;
404 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
406 erange_reset:
407 fsetpos (ent->stream, &pos);
408 goto erange;
411 /* Terminate the line for any case. */
412 buffer[buflen - 1] = '\0';
414 /* Skip leading blanks. */
415 while (isspace (*p))
416 ++p;
418 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
419 /* Parse the line. If it is invalid, loop to
420 get the next line of the file to parse. */
421 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
422 errnop)));
424 if (__builtin_expect (parse_res == -1, 0))
425 /* The parser ran out of space. */
426 goto erange_reset;
428 /* This is a real entry. */
429 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
431 if (strcmp (result->gr_name, name) == 0)
432 return NSS_STATUS_SUCCESS;
433 else
434 continue;
437 /* -group */
438 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
440 if (strcmp (&result->gr_name[1], name) == 0)
441 return NSS_STATUS_NOTFOUND;
442 else
443 continue;
446 /* +group */
447 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
449 if (strcmp (name, &result->gr_name[1]) == 0)
451 enum nss_status status;
453 status = getgrnam_plusgroup (name, result, ent,
454 buffer, buflen, errnop);
455 if (status == NSS_STATUS_RETURN)
456 /* We couldn't parse the entry */
457 continue;
458 else
459 return status;
462 /* +:... */
463 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
465 enum nss_status status;
467 status = getgrnam_plusgroup (name, result, ent,
468 buffer, buflen, errnop);
469 if (status == NSS_STATUS_RETURN)
470 /* We couldn't parse the entry */
471 continue;
472 else
473 return status;
477 return NSS_STATUS_SUCCESS;
480 enum nss_status
481 _nss_compat_getgrnam_r (const char *name, struct group *grp,
482 char *buffer, size_t buflen, int *errnop)
484 ent_t ent = {TRUE, NULL, {NULL, 0, 0}};
485 enum nss_status result;
487 if (name[0] == '-' || name[0] == '+')
488 return NSS_STATUS_NOTFOUND;
490 __libc_lock_lock (lock);
492 if (ni == NULL)
493 init_nss_interface ();
495 __libc_lock_unlock (lock);
497 result = internal_setgrent (&ent, 0);
499 if (result == NSS_STATUS_SUCCESS)
500 result = internal_getgrnam_r (name, grp, &ent, buffer, buflen, errnop);
502 internal_endgrent (&ent);
504 return result;
507 /* Searches in /etc/group and the NIS/NIS+ map for a special group id */
508 static enum nss_status
509 internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
510 char *buffer, size_t buflen, int *errnop)
512 struct parser_data *data = (void *) buffer;
513 while (1)
515 fpos_t pos;
516 int parse_res = 0;
517 char *p;
521 /* We need at least 3 characters for one line. */
522 if (__builtin_expect (buflen < 3, 0))
524 erange:
525 *errnop = ERANGE;
526 return NSS_STATUS_TRYAGAIN;
529 fgetpos (ent->stream, &pos);
530 buffer[buflen - 1] = '\xff';
531 p = fgets_unlocked (buffer, buflen, ent->stream);
532 if (p == NULL && feof_unlocked (ent->stream))
533 return NSS_STATUS_NOTFOUND;
535 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
537 erange_reset:
538 fsetpos (ent->stream, &pos);
539 goto erange;
542 /* Terminate the line for any case. */
543 buffer[buflen - 1] = '\0';
545 /* Skip leading blanks. */
546 while (isspace (*p))
547 ++p;
549 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
550 /* Parse the line. If it is invalid, loop to
551 get the next line of the file to parse. */
552 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
553 errnop)));
555 if (__builtin_expect (parse_res == -1, 0))
556 /* The parser ran out of space. */
557 goto erange_reset;
559 /* This is a real entry. */
560 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
562 if (result->gr_gid == gid)
563 return NSS_STATUS_SUCCESS;
564 else
565 continue;
568 /* -group */
569 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
571 blacklist_store_name (&result->gr_name[1], ent);
572 continue;
575 /* +group */
576 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
578 enum nss_status status;
580 /* Store the group in the blacklist for the "+" at the end of
581 /etc/group */
582 blacklist_store_name (&result->gr_name[1], ent);
583 status = getgrnam_plusgroup (&result->gr_name[1], result, ent,
584 buffer, buflen, errnop);
585 if (status == NSS_STATUS_SUCCESS && result->gr_gid == gid)
586 break;
587 else
588 continue;
590 /* +:... */
591 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
593 if (!nss_getgrgid_r)
594 return NSS_STATUS_UNAVAIL;
596 enum nss_status status = nss_getgrgid_r (gid, result, buffer, buflen,
597 errnop);
598 if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
599 return NSS_STATUS_NOTFOUND;
600 else
601 return status;
605 return NSS_STATUS_SUCCESS;
608 enum nss_status
609 _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
610 char *buffer, size_t buflen, int *errnop)
612 ent_t ent = {TRUE, NULL, {NULL, 0, 0}};
613 enum nss_status result;
615 __libc_lock_lock (lock);
617 if (ni == NULL)
618 init_nss_interface ();
620 __libc_lock_unlock (lock);
622 result = internal_setgrent (&ent, 0);
624 if (result == NSS_STATUS_SUCCESS)
625 result = internal_getgrgid_r (gid, grp, &ent, buffer, buflen, errnop);
627 internal_endgrent (&ent);
629 return result;
633 /* Support routines for remembering -@netgroup and -user entries.
634 The names are stored in a single string with `|' as separator. */
635 static void
636 blacklist_store_name (const char *name, ent_t *ent)
638 int namelen = strlen (name);
639 char *tmp;
641 /* first call, setup cache */
642 if (ent->blacklist.size == 0)
644 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
645 ent->blacklist.data = malloc (ent->blacklist.size);
646 if (ent->blacklist.data == NULL)
647 return;
648 ent->blacklist.data[0] = '|';
649 ent->blacklist.data[1] = '\0';
650 ent->blacklist.current = 1;
652 else
654 if (in_blacklist (name, namelen, ent))
655 return; /* no duplicates */
657 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
659 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
660 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
661 if (tmp == NULL)
663 free (ent->blacklist.data);
664 ent->blacklist.size = 0;
665 return;
667 ent->blacklist.data = tmp;
671 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
672 *tmp++ = '|';
673 *tmp = '\0';
674 ent->blacklist.current += namelen + 1;
676 return;
679 /* returns TRUE if ent->blacklist contains name, else FALSE */
680 static bool_t
681 in_blacklist (const char *name, int namelen, ent_t *ent)
683 char buf[namelen + 3];
684 char *cp;
686 if (ent->blacklist.data == NULL)
687 return FALSE;
689 buf[0] = '|';
690 cp = stpcpy (&buf[1], name);
691 *cp++ = '|';
692 *cp = '\0';
693 return strstr (ent->blacklist.data, buf) != NULL;