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
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
62 enum nss_status setent_status
;
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
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
;
99 if (ent
->blacklist
.data
!= NULL
)
101 ent
->blacklist
.current
= 1;
102 ent
->blacklist
.data
[0] = '|';
103 ent
->blacklist
.data
[1] = '\0';
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
;
116 /* We have to make sure the file is `closed on exec'. */
119 result
= flags
= fcntl (fileno_unlocked (ent
->stream
), F_GETFD
, 0);
123 result
= fcntl (fileno_unlocked (ent
->stream
), F_SETFD
, flags
);
127 /* Something went wrong. Close the stream and return a
129 fclose (ent
->stream
);
131 status
= NSS_STATUS_UNAVAIL
;
134 /* We take care of locking ourself. */
135 __fsetlocking (ent
->stream
, FSETLOCKING_BYCALLER
);
139 rewind (ent
->stream
);
141 if (needent
&& status
== NSS_STATUS_SUCCESS
&& nss_setgrent
)
142 ent
->setent_status
= nss_setgrent (stayopen
);
149 _nss_compat_setgrent (int stayopen
)
151 enum nss_status result
;
153 __libc_lock_lock (lock
);
156 init_nss_interface ();
158 result
= internal_setgrent (&ext_ent
, stayopen
, 1);
160 __libc_lock_unlock (lock
);
166 static enum nss_status
167 internal_endgrent (ent_t
*ent
)
172 if (ent
->stream
!= NULL
)
174 fclose (ent
->stream
);
178 if (ent
->blacklist
.data
!= NULL
)
180 ent
->blacklist
.current
= 1;
181 ent
->blacklist
.data
[0] = '|';
182 ent
->blacklist
.data
[1] = '\0';
185 ent
->blacklist
.current
= 0;
187 return NSS_STATUS_SUCCESS
;
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
);
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
)
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
)) !=
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
)
235 return NSS_STATUS_UNAVAIL
;
237 enum nss_status status
= nss_getgrnam_r (name
, result
, buffer
, buflen
,
239 if (status
!= NSS_STATUS_SUCCESS
)
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
;
262 /* We need at least 3 characters for one line. */
263 if (__builtin_expect (buflen
< 3, 0))
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))
279 fsetpos (ent
->stream
, &pos
);
283 /* Terminate the line for any case. */
284 buffer
[buflen
- 1] = '\0';
286 /* Skip leading blanks. */
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
,
296 if (__builtin_expect (parse_res
== -1, 0))
297 /* The parser ran out of space. */
300 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
301 /* This is a real entry. */
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
);
313 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0'
314 && result
->gr_name
[1] != '@')
316 size_t len
= strlen (result
->gr_name
);
318 enum nss_status status
;
320 /* Store the group in the blacklist for the "+" at the end of
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. */
328 else if (status
== NSS_STATUS_RETURN
/* We couldn't parse the entry*/
329 || status
== NSS_STATUS_NOTFOUND
) /* No group in NIS */
333 if (status
== NSS_STATUS_TRYAGAIN
)
334 /* The parser ran out of space. */
342 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
346 return getgrent_next_nss (result
, ent
, buffer
, buflen
, errnop
);
350 return NSS_STATUS_SUCCESS
;
355 _nss_compat_getgrent_r (struct group
*grp
, char *buffer
, size_t buflen
,
358 enum nss_status result
= NSS_STATUS_SUCCESS
;
360 __libc_lock_lock (lock
);
362 /* Be prepared that the setgrent function was not called before. */
364 init_nss_interface ();
366 if (ext_ent
.stream
== NULL
)
367 result
= internal_setgrent (&ext_ent
, 1, 1);
369 if (result
== NSS_STATUS_SUCCESS
)
372 result
= getgrent_next_file (grp
, &ext_ent
, buffer
, buflen
, errnop
);
374 result
= getgrent_next_nss (grp
, &ext_ent
, buffer
, buflen
, errnop
);
376 __libc_lock_unlock (lock
);
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
;
395 /* We need at least 3 characters for one line. */
396 if (__builtin_expect (buflen
< 3, 0))
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))
412 fsetpos (ent
->stream
, &pos
);
416 /* Terminate the line for any case. */
417 buffer
[buflen
- 1] = '\0';
419 /* Skip leading blanks. */
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
,
429 if (__builtin_expect (parse_res
== -1, 0))
430 /* The parser ran out of space. */
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
;
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
;
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 */
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 */
482 return NSS_STATUS_SUCCESS
;
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
);
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
);
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
;
526 /* We need at least 3 characters for one line. */
527 if (__builtin_expect (buflen
< 3, 0))
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))
543 fsetpos (ent
->stream
, &pos
);
547 /* Terminate the line for any case. */
548 buffer
[buflen
- 1] = '\0';
550 /* Skip leading blanks. */
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
,
560 if (__builtin_expect (parse_res
== -1, 0))
561 /* The parser ran out of space. */
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
;
574 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0')
576 blacklist_store_name (&result
->gr_name
[1], ent
);
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
);
586 enum nss_status status
;
588 /* Store the group in the blacklist for the "+" at the end of
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
)
600 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
603 return NSS_STATUS_UNAVAIL
;
605 enum nss_status status
= nss_getgrgid_r (gid
, result
, buffer
, buflen
,
607 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
608 return NSS_STATUS_NOTFOUND
;
614 return NSS_STATUS_SUCCESS
;
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
);
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
);
642 /* Support routines for remembering -@netgroup and -user entries.
643 The names are stored in a single string with `|' as separator. */
645 blacklist_store_name (const char *name
, ent_t
*ent
)
647 int namelen
= strlen (name
);
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
)
657 ent
->blacklist
.data
[0] = '|';
658 ent
->blacklist
.data
[1] = '\0';
659 ent
->blacklist
.current
= 1;
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
);
672 free (ent
->blacklist
.data
);
673 ent
->blacklist
.size
= 0;
676 ent
->blacklist
.data
= tmp
;
680 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
683 ent
->blacklist
.current
+= namelen
+ 1;
688 /* returns TRUE if ent->blacklist contains name, else FALSE */
690 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
692 char buf
[namelen
+ 3];
695 if (ent
->blacklist
.data
== NULL
)
699 cp
= stpcpy (&buf
[1], name
);
702 return strstr (ent
->blacklist
.data
, buf
) != NULL
;