1 /* Copyright (C) 1996-1999,2001,2002,2003,2004 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
26 #include <stdio_ext.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
,
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. */
45 #define STRUCTURE group
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
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
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
;
98 if (ent
->blacklist
.data
!= NULL
)
100 ent
->blacklist
.current
= 1;
101 ent
->blacklist
.data
[0] = '|';
102 ent
->blacklist
.data
[1] = '\0';
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
;
115 /* We have to make sure the file is `closed on exec'. */
118 result
= flags
= fcntl (fileno_unlocked (ent
->stream
), F_GETFD
, 0);
122 result
= fcntl (fileno_unlocked (ent
->stream
), F_SETFD
, flags
);
126 /* Something went wrong. Close the stream and return a
128 fclose (ent
->stream
);
130 status
= NSS_STATUS_UNAVAIL
;
133 /* We take care of locking ourself. */
134 __fsetlocking (ent
->stream
, FSETLOCKING_BYCALLER
);
138 rewind (ent
->stream
);
140 if (status
== NSS_STATUS_SUCCESS
&& nss_setgrent
)
141 return nss_setgrent (stayopen
);
148 _nss_compat_setgrent (int stayopen
)
150 enum nss_status result
;
152 __libc_lock_lock (lock
);
155 init_nss_interface ();
157 result
= internal_setgrent (&ext_ent
, stayopen
);
159 __libc_lock_unlock (lock
);
165 static enum nss_status
166 internal_endgrent (ent_t
*ent
)
171 if (ent
->stream
!= NULL
)
173 fclose (ent
->stream
);
177 if (ent
->blacklist
.data
!= NULL
)
179 ent
->blacklist
.current
= 1;
180 ent
->blacklist
.data
[0] = '|';
181 ent
->blacklist
.data
[1] = '\0';
184 ent
->blacklist
.current
= 0;
186 return NSS_STATUS_SUCCESS
;
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
);
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
)
209 return NSS_STATUS_UNAVAIL
;
213 enum nss_status status
;
215 if ((status
= nss_getgrent_r (result
, buffer
, buflen
, errnop
)) !=
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
)
230 return NSS_STATUS_UNAVAIL
;
232 if (nss_getgrnam_r (name
, result
, buffer
, buflen
, errnop
) !=
234 return NSS_STATUS_NOTFOUND
;
236 if (in_blacklist (result
->gr_name
, strlen (result
->gr_name
), ent
))
237 return NSS_STATUS_NOTFOUND
;
239 /* We found the entry. */
240 return NSS_STATUS_SUCCESS
;
243 static enum nss_status
244 getgrent_next_file (struct group
*result
, ent_t
*ent
,
245 char *buffer
, size_t buflen
, int *errnop
)
247 struct parser_data
*data
= (void *) buffer
;
256 /* We need at least 3 characters for one line. */
257 if (__builtin_expect (buflen
< 3, 0))
261 return NSS_STATUS_TRYAGAIN
;
264 fgetpos (ent
->stream
, &pos
);
265 buffer
[buflen
- 1] = '\xff';
266 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
267 if (p
== NULL
&& feof_unlocked (ent
->stream
))
268 return NSS_STATUS_NOTFOUND
;
270 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
273 fsetpos (ent
->stream
, &pos
);
277 /* Terminate the line for any case. */
278 buffer
[buflen
- 1] = '\0';
280 /* Skip leading blanks. */
284 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
285 /* Parse the line. If it is invalid, loop to
286 get the next line of the file to parse. */
287 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
290 if (__builtin_expect (parse_res
== -1, 0))
291 /* The parser ran out of space. */
294 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
295 /* This is a real entry. */
299 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0'
300 && result
->gr_name
[1] != '@')
302 blacklist_store_name (&result
->gr_name
[1], ent
);
307 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0'
308 && result
->gr_name
[1] != '@')
310 size_t len
= strlen (result
->gr_name
);
312 enum nss_status status
;
314 /* Store the group in the blacklist for the "+" at the end of
316 memcpy (buf
, &result
->gr_name
[1], len
);
317 status
= getgrnam_plusgroup (&result
->gr_name
[1], result
, ent
,
318 buffer
, buflen
, errnop
);
319 blacklist_store_name (buf
, ent
);
320 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
322 else if (status
== NSS_STATUS_RETURN
/* We couldn't parse the entry*/
323 || status
== NSS_STATUS_NOTFOUND
) /* No group in NIS */
327 if (status
== NSS_STATUS_TRYAGAIN
)
328 /* The parser ran out of space. */
336 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
340 return getgrent_next_nss (result
, ent
, buffer
, buflen
, errnop
);
344 return NSS_STATUS_SUCCESS
;
349 _nss_compat_getgrent_r (struct group
*grp
, char *buffer
, size_t buflen
,
352 enum nss_status result
= NSS_STATUS_SUCCESS
;
354 __libc_lock_lock (lock
);
356 /* Be prepared that the setgrent function was not called before. */
358 init_nss_interface ();
360 if (ext_ent
.stream
== NULL
)
361 result
= internal_setgrent (&ext_ent
, 1);
363 if (result
== NSS_STATUS_SUCCESS
)
366 result
= getgrent_next_file (grp
, &ext_ent
, buffer
, buflen
, errnop
);
368 result
= getgrent_next_nss (grp
, &ext_ent
, buffer
, buflen
, errnop
);
370 __libc_lock_unlock (lock
);
375 /* Searches in /etc/group and the NIS/NIS+ map for a special group */
376 static enum nss_status
377 internal_getgrnam_r (const char *name
, struct group
*result
, ent_t
*ent
,
378 char *buffer
, size_t buflen
, int *errnop
)
380 struct parser_data
*data
= (void *) buffer
;
389 /* We need at least 3 characters for one line. */
390 if (__builtin_expect (buflen
< 3, 0))
394 return NSS_STATUS_TRYAGAIN
;
397 fgetpos (ent
->stream
, &pos
);
398 buffer
[buflen
- 1] = '\xff';
399 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
400 if (p
== NULL
&& feof_unlocked (ent
->stream
))
401 return NSS_STATUS_NOTFOUND
;
403 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
406 fsetpos (ent
->stream
, &pos
);
410 /* Terminate the line for any case. */
411 buffer
[buflen
- 1] = '\0';
413 /* Skip leading blanks. */
417 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
418 /* Parse the line. If it is invalid, loop to
419 get the next line of the file to parse. */
420 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
423 if (__builtin_expect (parse_res
== -1, 0))
424 /* The parser ran out of space. */
427 /* This is a real entry. */
428 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
430 if (strcmp (result
->gr_name
, name
) == 0)
431 return NSS_STATUS_SUCCESS
;
437 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0')
439 if (strcmp (&result
->gr_name
[1], name
) == 0)
440 return NSS_STATUS_NOTFOUND
;
446 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0')
448 if (strcmp (name
, &result
->gr_name
[1]) == 0)
450 enum nss_status status
;
452 status
= getgrnam_plusgroup (name
, result
, ent
,
453 buffer
, buflen
, errnop
);
454 if (status
== NSS_STATUS_RETURN
)
455 /* We couldn't parse the entry */
462 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
464 enum nss_status status
;
466 status
= getgrnam_plusgroup (name
, result
, ent
,
467 buffer
, buflen
, errnop
);
468 if (status
== NSS_STATUS_RETURN
)
469 /* We couldn't parse the entry */
476 return NSS_STATUS_SUCCESS
;
480 _nss_compat_getgrnam_r (const char *name
, struct group
*grp
,
481 char *buffer
, size_t buflen
, int *errnop
)
483 ent_t ent
= {TRUE
, NULL
, {NULL
, 0, 0}};
484 enum nss_status result
;
486 if (name
[0] == '-' || name
[0] == '+')
487 return NSS_STATUS_NOTFOUND
;
489 __libc_lock_lock (lock
);
492 init_nss_interface ();
494 __libc_lock_unlock (lock
);
496 result
= internal_setgrent (&ent
, 0);
498 if (result
== NSS_STATUS_SUCCESS
)
499 result
= internal_getgrnam_r (name
, grp
, &ent
, buffer
, buflen
, errnop
);
501 internal_endgrent (&ent
);
506 /* Searches in /etc/group and the NIS/NIS+ map for a special group id */
507 static enum nss_status
508 internal_getgrgid_r (gid_t gid
, struct group
*result
, ent_t
*ent
,
509 char *buffer
, size_t buflen
, int *errnop
)
511 struct parser_data
*data
= (void *) buffer
;
520 /* We need at least 3 characters for one line. */
521 if (__builtin_expect (buflen
< 3, 0))
525 return NSS_STATUS_TRYAGAIN
;
528 fgetpos (ent
->stream
, &pos
);
529 buffer
[buflen
- 1] = '\xff';
530 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
531 if (p
== NULL
&& feof_unlocked (ent
->stream
))
532 return NSS_STATUS_NOTFOUND
;
534 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
537 fsetpos (ent
->stream
, &pos
);
541 /* Terminate the line for any case. */
542 buffer
[buflen
- 1] = '\0';
544 /* Skip leading blanks. */
548 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
549 /* Parse the line. If it is invalid, loop to
550 get the next line of the file to parse. */
551 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
555 /* The parser ran out of space. */
558 /* This is a real entry. */
559 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
561 if (result
->gr_gid
== gid
)
562 return NSS_STATUS_SUCCESS
;
568 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0')
570 blacklist_store_name (&result
->gr_name
[1], ent
);
575 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0')
577 enum nss_status status
;
579 /* Store the group in the blacklist for the "+" at the end of
581 blacklist_store_name (&result
->gr_name
[1], ent
);
582 status
= getgrnam_plusgroup (&result
->gr_name
[1], result
, ent
,
583 buffer
, buflen
, errnop
);
584 if (status
== NSS_STATUS_SUCCESS
&& result
->gr_gid
== gid
)
590 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
592 enum nss_status status
;
594 status
= nss_getgrgid_r (gid
, result
, buffer
, buflen
, errnop
);
595 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
596 return NSS_STATUS_NOTFOUND
;
602 return NSS_STATUS_SUCCESS
;
606 _nss_compat_getgrgid_r (gid_t gid
, struct group
*grp
,
607 char *buffer
, size_t buflen
, int *errnop
)
609 ent_t ent
= {TRUE
, NULL
, {NULL
, 0, 0}};
610 enum nss_status result
;
612 __libc_lock_lock (lock
);
615 init_nss_interface ();
617 __libc_lock_unlock (lock
);
619 result
= internal_setgrent (&ent
, 0);
621 if (result
== NSS_STATUS_SUCCESS
)
622 result
= internal_getgrgid_r (gid
, grp
, &ent
, buffer
, buflen
, errnop
);
624 internal_endgrent (&ent
);
630 /* Support routines for remembering -@netgroup and -user entries.
631 The names are stored in a single string with `|' as separator. */
633 blacklist_store_name (const char *name
, ent_t
*ent
)
635 int namelen
= strlen (name
);
638 /* first call, setup cache */
639 if (ent
->blacklist
.size
== 0)
641 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
642 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
643 if (ent
->blacklist
.data
== NULL
)
645 ent
->blacklist
.data
[0] = '|';
646 ent
->blacklist
.data
[1] = '\0';
647 ent
->blacklist
.current
= 1;
651 if (in_blacklist (name
, namelen
, ent
))
652 return; /* no duplicates */
654 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
656 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
657 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
660 free (ent
->blacklist
.data
);
661 ent
->blacklist
.size
= 0;
664 ent
->blacklist
.data
= tmp
;
668 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
671 ent
->blacklist
.current
+= namelen
+ 1;
676 /* returns TRUE if ent->blacklist contains name, else FALSE */
678 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
680 char buf
[namelen
+ 3];
683 if (ent
->blacklist
.data
== NULL
)
687 cp
= stpcpy (&buf
[1], name
);
690 return strstr (ent
->blacklist
.data
, buf
) != NULL
;