2.5-18.1
[glibc.git] / nis / nss_compat / compat-grp.c
blob236c84a20fb9bd68bcf4b7abec8a36865b7600ac
1 /* Copyright (C) 1996-1999,2001-2005,2006 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 enum nss_status setent_status;
63 FILE *stream;
64 struct blacklist_t blacklist;
66 typedef struct ent_t ent_t;
68 static ent_t ext_ent = { TRUE, NSS_STATUS_SUCCESS, 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 /* Initialize the NSS interface/functions. The calling function must
78 hold the lock. */
79 static void
80 init_nss_interface (void)
82 if (__nss_database_lookup ("group_compat", NULL, "nis", &ni) >= 0)
84 nss_setgrent = __nss_lookup_function (ni, "setgrent");
85 nss_getgrnam_r = __nss_lookup_function (ni, "getgrnam_r");
86 nss_getgrgid_r = __nss_lookup_function (ni, "getgrgid_r");
87 nss_getgrent_r = __nss_lookup_function (ni, "getgrent_r");
88 nss_endgrent = __nss_lookup_function (ni, "endgrent");
92 static enum nss_status
93 internal_setgrent (ent_t *ent, int stayopen, int needent)
95 enum nss_status status = NSS_STATUS_SUCCESS;
97 ent->files = TRUE;
99 if (ent->blacklist.data != NULL)
101 ent->blacklist.current = 1;
102 ent->blacklist.data[0] = '|';
103 ent->blacklist.data[1] = '\0';
105 else
106 ent->blacklist.current = 0;
108 if (ent->stream == NULL)
110 ent->stream = fopen ("/etc/group", "rm");
112 if (ent->stream == NULL)
113 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
114 else
116 /* We have to make sure the file is `closed on exec'. */
117 int result, flags;
119 result = flags = fcntl (fileno_unlocked (ent->stream), F_GETFD, 0);
120 if (result >= 0)
122 flags |= FD_CLOEXEC;
123 result = fcntl (fileno_unlocked (ent->stream), F_SETFD, flags);
125 if (result < 0)
127 /* Something went wrong. Close the stream and return a
128 failure. */
129 fclose (ent->stream);
130 ent->stream = NULL;
131 status = NSS_STATUS_UNAVAIL;
133 else
134 /* We take care of locking ourself. */
135 __fsetlocking (ent->stream, FSETLOCKING_BYCALLER);
138 else
139 rewind (ent->stream);
141 if (needent && status == NSS_STATUS_SUCCESS && nss_setgrent)
142 ent->setent_status = nss_setgrent (stayopen);
144 return status;
148 enum nss_status
149 _nss_compat_setgrent (int stayopen)
151 enum nss_status result;
153 __libc_lock_lock (lock);
155 if (ni == NULL)
156 init_nss_interface ();
158 result = internal_setgrent (&ext_ent, stayopen, 1);
160 __libc_lock_unlock (lock);
162 return result;
166 static enum nss_status
167 internal_endgrent (ent_t *ent)
169 if (nss_endgrent)
170 nss_endgrent ();
172 if (ent->stream != NULL)
174 fclose (ent->stream);
175 ent->stream = NULL;
178 if (ent->blacklist.data != NULL)
180 ent->blacklist.current = 1;
181 ent->blacklist.data[0] = '|';
182 ent->blacklist.data[1] = '\0';
184 else
185 ent->blacklist.current = 0;
187 return NSS_STATUS_SUCCESS;
190 enum nss_status
191 _nss_compat_endgrent (void)
193 enum nss_status result;
195 __libc_lock_lock (lock);
197 result = internal_endgrent (&ext_ent);
199 __libc_lock_unlock (lock);
201 return result;
204 /* get the next group from NSS (+ entry) */
205 static enum nss_status
206 getgrent_next_nss (struct group *result, ent_t *ent, char *buffer,
207 size_t buflen, int *errnop)
209 if (!nss_getgrent_r)
210 return NSS_STATUS_UNAVAIL;
212 /* If the setgrent call failed, say so. */
213 if (ent->setent_status != NSS_STATUS_SUCCESS)
214 return ent->setent_status;
218 enum nss_status status;
220 if ((status = nss_getgrent_r (result, buffer, buflen, errnop)) !=
221 NSS_STATUS_SUCCESS)
222 return status;
224 while (in_blacklist (result->gr_name, strlen (result->gr_name), ent));
226 return NSS_STATUS_SUCCESS;
229 /* This function handle the +group entrys in /etc/group */
230 static enum nss_status
231 getgrnam_plusgroup (const char *name, struct group *result, ent_t *ent,
232 char *buffer, size_t buflen, int *errnop)
234 if (!nss_getgrnam_r)
235 return NSS_STATUS_UNAVAIL;
237 enum nss_status status = nss_getgrnam_r (name, result, buffer, buflen,
238 errnop);
239 if (status != NSS_STATUS_SUCCESS)
240 return status;
242 if (in_blacklist (result->gr_name, strlen (result->gr_name), ent))
243 return NSS_STATUS_NOTFOUND;
245 /* We found the entry. */
246 return NSS_STATUS_SUCCESS;
249 static enum nss_status
250 getgrent_next_file (struct group *result, ent_t *ent,
251 char *buffer, size_t buflen, int *errnop)
253 struct parser_data *data = (void *) buffer;
254 while (1)
256 fpos_t pos;
257 int parse_res = 0;
258 char *p;
262 /* We need at least 3 characters for one line. */
263 if (__builtin_expect (buflen < 3, 0))
265 erange:
266 *errnop = ERANGE;
267 return NSS_STATUS_TRYAGAIN;
270 fgetpos (ent->stream, &pos);
271 buffer[buflen - 1] = '\xff';
272 p = fgets_unlocked (buffer, buflen, ent->stream);
273 if (p == NULL && feof_unlocked (ent->stream))
274 return NSS_STATUS_NOTFOUND;
276 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
278 erange_reset:
279 fsetpos (ent->stream, &pos);
280 goto erange;
283 /* Terminate the line for any case. */
284 buffer[buflen - 1] = '\0';
286 /* Skip leading blanks. */
287 while (isspace (*p))
288 ++p;
290 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
291 /* Parse the line. If it is invalid, loop to
292 get the next line of the file to parse. */
293 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
294 errnop)));
296 if (__builtin_expect (parse_res == -1, 0))
297 /* The parser ran out of space. */
298 goto erange_reset;
300 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
301 /* This is a real entry. */
302 break;
304 /* -group */
305 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0'
306 && result->gr_name[1] != '@')
308 blacklist_store_name (&result->gr_name[1], ent);
309 continue;
312 /* +group */
313 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0'
314 && result->gr_name[1] != '@')
316 size_t len = strlen (result->gr_name);
317 char buf[len];
318 enum nss_status status;
320 /* Store the group in the blacklist for the "+" at the end of
321 /etc/group */
322 memcpy (buf, &result->gr_name[1], len);
323 status = getgrnam_plusgroup (&result->gr_name[1], result, ent,
324 buffer, buflen, errnop);
325 blacklist_store_name (buf, ent);
326 if (status == NSS_STATUS_SUCCESS) /* We found the entry. */
327 break;
328 else if (status == NSS_STATUS_RETURN /* We couldn't parse the entry*/
329 || status == NSS_STATUS_NOTFOUND) /* No group in NIS */
330 continue;
331 else
333 if (status == NSS_STATUS_TRYAGAIN)
334 /* The parser ran out of space. */
335 goto erange_reset;
337 return status;
341 /* +:... */
342 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
344 ent->files = FALSE;
346 return getgrent_next_nss (result, ent, buffer, buflen, errnop);
350 return NSS_STATUS_SUCCESS;
354 enum nss_status
355 _nss_compat_getgrent_r (struct group *grp, char *buffer, size_t buflen,
356 int *errnop)
358 enum nss_status result = NSS_STATUS_SUCCESS;
360 __libc_lock_lock (lock);
362 /* Be prepared that the setgrent function was not called before. */
363 if (ni == NULL)
364 init_nss_interface ();
366 if (ext_ent.stream == NULL)
367 result = internal_setgrent (&ext_ent, 1, 1);
369 if (result == NSS_STATUS_SUCCESS)
371 if (ext_ent.files)
372 result = getgrent_next_file (grp, &ext_ent, buffer, buflen, errnop);
373 else
374 result = getgrent_next_nss (grp, &ext_ent, buffer, buflen, errnop);
376 __libc_lock_unlock (lock);
378 return result;
381 /* Searches in /etc/group and the NIS/NIS+ map for a special group */
382 static enum nss_status
383 internal_getgrnam_r (const char *name, struct group *result, ent_t *ent,
384 char *buffer, size_t buflen, int *errnop)
386 struct parser_data *data = (void *) buffer;
387 while (1)
389 fpos_t pos;
390 int parse_res = 0;
391 char *p;
395 /* We need at least 3 characters for one line. */
396 if (__builtin_expect (buflen < 3, 0))
398 erange:
399 *errnop = ERANGE;
400 return NSS_STATUS_TRYAGAIN;
403 fgetpos (ent->stream, &pos);
404 buffer[buflen - 1] = '\xff';
405 p = fgets_unlocked (buffer, buflen, ent->stream);
406 if (p == NULL && feof_unlocked (ent->stream))
407 return NSS_STATUS_NOTFOUND;
409 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
411 erange_reset:
412 fsetpos (ent->stream, &pos);
413 goto erange;
416 /* Terminate the line for any case. */
417 buffer[buflen - 1] = '\0';
419 /* Skip leading blanks. */
420 while (isspace (*p))
421 ++p;
423 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
424 /* Parse the line. If it is invalid, loop to
425 get the next line of the file to parse. */
426 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
427 errnop)));
429 if (__builtin_expect (parse_res == -1, 0))
430 /* The parser ran out of space. */
431 goto erange_reset;
433 /* This is a real entry. */
434 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
436 if (strcmp (result->gr_name, name) == 0)
437 return NSS_STATUS_SUCCESS;
438 else
439 continue;
442 /* -group */
443 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
445 if (strcmp (&result->gr_name[1], name) == 0)
446 return NSS_STATUS_NOTFOUND;
447 else
448 continue;
451 /* +group */
452 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
454 if (strcmp (name, &result->gr_name[1]) == 0)
456 enum nss_status status;
458 status = getgrnam_plusgroup (name, result, ent,
459 buffer, buflen, errnop);
460 if (status == NSS_STATUS_RETURN)
461 /* We couldn't parse the entry */
462 continue;
463 else
464 return status;
467 /* +:... */
468 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
470 enum nss_status status;
472 status = getgrnam_plusgroup (name, result, ent,
473 buffer, buflen, errnop);
474 if (status == NSS_STATUS_RETURN)
475 /* We couldn't parse the entry */
476 continue;
477 else
478 return status;
482 return NSS_STATUS_SUCCESS;
485 enum nss_status
486 _nss_compat_getgrnam_r (const char *name, struct group *grp,
487 char *buffer, size_t buflen, int *errnop)
489 ent_t ent = { TRUE, NSS_STATUS_SUCCESS, NULL, { NULL, 0, 0 }};
490 enum nss_status result;
492 if (name[0] == '-' || name[0] == '+')
493 return NSS_STATUS_NOTFOUND;
495 __libc_lock_lock (lock);
497 if (ni == NULL)
498 init_nss_interface ();
500 __libc_lock_unlock (lock);
502 result = internal_setgrent (&ent, 0, 0);
504 if (result == NSS_STATUS_SUCCESS)
505 result = internal_getgrnam_r (name, grp, &ent, buffer, buflen, errnop);
507 internal_endgrent (&ent);
509 return result;
512 /* Searches in /etc/group and the NIS/NIS+ map for a special group id */
513 static enum nss_status
514 internal_getgrgid_r (gid_t gid, struct group *result, ent_t *ent,
515 char *buffer, size_t buflen, int *errnop)
517 struct parser_data *data = (void *) buffer;
518 while (1)
520 fpos_t pos;
521 int parse_res = 0;
522 char *p;
526 /* We need at least 3 characters for one line. */
527 if (__builtin_expect (buflen < 3, 0))
529 erange:
530 *errnop = ERANGE;
531 return NSS_STATUS_TRYAGAIN;
534 fgetpos (ent->stream, &pos);
535 buffer[buflen - 1] = '\xff';
536 p = fgets_unlocked (buffer, buflen, ent->stream);
537 if (p == NULL && feof_unlocked (ent->stream))
538 return NSS_STATUS_NOTFOUND;
540 if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
542 erange_reset:
543 fsetpos (ent->stream, &pos);
544 goto erange;
547 /* Terminate the line for any case. */
548 buffer[buflen - 1] = '\0';
550 /* Skip leading blanks. */
551 while (isspace (*p))
552 ++p;
554 while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
555 /* Parse the line. If it is invalid, loop to
556 get the next line of the file to parse. */
557 !(parse_res = _nss_files_parse_grent (p, result, data, buflen,
558 errnop)));
560 if (__builtin_expect (parse_res == -1, 0))
561 /* The parser ran out of space. */
562 goto erange_reset;
564 /* This is a real entry. */
565 if (result->gr_name[0] != '+' && result->gr_name[0] != '-')
567 if (result->gr_gid == gid)
568 return NSS_STATUS_SUCCESS;
569 else
570 continue;
573 /* -group */
574 if (result->gr_name[0] == '-' && result->gr_name[1] != '\0')
576 blacklist_store_name (&result->gr_name[1], ent);
577 continue;
580 /* +group */
581 if (result->gr_name[0] == '+' && result->gr_name[1] != '\0')
583 /* Yes, no +1, see the memcpy call below. */
584 size_t len = strlen (result->gr_name);
585 char buf[len];
586 enum nss_status status;
588 /* Store the group in the blacklist for the "+" at the end of
589 /etc/group */
590 memcpy (buf, &result->gr_name[1], len);
591 status = getgrnam_plusgroup (&result->gr_name[1], result, ent,
592 buffer, buflen, errnop);
593 blacklist_store_name (buf, ent);
594 if (status == NSS_STATUS_SUCCESS && result->gr_gid == gid)
595 break;
596 else
597 continue;
599 /* +:... */
600 if (result->gr_name[0] == '+' && result->gr_name[1] == '\0')
602 if (!nss_getgrgid_r)
603 return NSS_STATUS_UNAVAIL;
605 enum nss_status status = nss_getgrgid_r (gid, result, buffer, buflen,
606 errnop);
607 if (status == NSS_STATUS_RETURN) /* We couldn't parse the entry */
608 return NSS_STATUS_NOTFOUND;
609 else
610 return status;
614 return NSS_STATUS_SUCCESS;
617 enum nss_status
618 _nss_compat_getgrgid_r (gid_t gid, struct group *grp,
619 char *buffer, size_t buflen, int *errnop)
621 ent_t ent = { TRUE, NSS_STATUS_SUCCESS, NULL, { NULL, 0, 0 }};
622 enum nss_status result;
624 __libc_lock_lock (lock);
626 if (ni == NULL)
627 init_nss_interface ();
629 __libc_lock_unlock (lock);
631 result = internal_setgrent (&ent, 0, 0);
633 if (result == NSS_STATUS_SUCCESS)
634 result = internal_getgrgid_r (gid, grp, &ent, buffer, buflen, errnop);
636 internal_endgrent (&ent);
638 return result;
642 /* Support routines for remembering -@netgroup and -user entries.
643 The names are stored in a single string with `|' as separator. */
644 static void
645 blacklist_store_name (const char *name, ent_t *ent)
647 int namelen = strlen (name);
648 char *tmp;
650 /* first call, setup cache */
651 if (ent->blacklist.size == 0)
653 ent->blacklist.size = MAX (BLACKLIST_INITIAL_SIZE, 2 * namelen);
654 ent->blacklist.data = malloc (ent->blacklist.size);
655 if (ent->blacklist.data == NULL)
656 return;
657 ent->blacklist.data[0] = '|';
658 ent->blacklist.data[1] = '\0';
659 ent->blacklist.current = 1;
661 else
663 if (in_blacklist (name, namelen, ent))
664 return; /* no duplicates */
666 if (ent->blacklist.current + namelen + 1 >= ent->blacklist.size)
668 ent->blacklist.size += MAX (BLACKLIST_INCREMENT, 2 * namelen);
669 tmp = realloc (ent->blacklist.data, ent->blacklist.size);
670 if (tmp == NULL)
672 free (ent->blacklist.data);
673 ent->blacklist.size = 0;
674 return;
676 ent->blacklist.data = tmp;
680 tmp = stpcpy (ent->blacklist.data + ent->blacklist.current, name);
681 *tmp++ = '|';
682 *tmp = '\0';
683 ent->blacklist.current += namelen + 1;
685 return;
688 /* returns TRUE if ent->blacklist contains name, else FALSE */
689 static bool_t
690 in_blacklist (const char *name, int namelen, ent_t *ent)
692 char buf[namelen + 3];
693 char *cp;
695 if (ent->blacklist.data == NULL)
696 return FALSE;
698 buf[0] = '|';
699 cp = stpcpy (&buf[1], name);
700 *cp++ = '|';
701 *cp = '\0';
702 return strstr (ent->blacklist.data, buf) != NULL;