1 /* Copyright (C) 1996-2018 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, see
17 <http://www.gnu.org/licenses/>. */
25 #include <stdio_ext.h>
27 #include <libc-lock.h>
28 #include <kernel-features.h>
30 static service_user
*ni
;
31 static enum nss_status (*nss_setgrent
) (int stayopen
);
32 static enum nss_status (*nss_getgrnam_r
) (const char *name
,
33 struct group
* grp
, char *buffer
,
34 size_t buflen
, int *errnop
);
35 static enum nss_status (*nss_getgrgid_r
) (gid_t gid
, struct group
* grp
,
36 char *buffer
, size_t buflen
,
38 static enum nss_status (*nss_getgrent_r
) (struct group
* grp
, char *buffer
,
39 size_t buflen
, int *errnop
);
40 static enum nss_status (*nss_endgrent
) (void);
42 /* Get the declaration of the parser function. */
44 #define STRUCTURE group
46 #include <nss/nss_files/files-parse.c>
48 /* Structure for remembering -group members ... */
49 #define BLACKLIST_INITIAL_SIZE 512
50 #define BLACKLIST_INCREMENT 256
61 enum nss_status setent_status
;
63 struct blacklist_t blacklist
;
65 typedef struct ent_t ent_t
;
67 static ent_t ext_ent
= { true, NSS_STATUS_SUCCESS
, 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 bool 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
, int needent
)
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", "rme");
111 if (ent
->stream
== NULL
)
112 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
114 /* We take care of locking ourself. */
115 __fsetlocking (ent
->stream
, FSETLOCKING_BYCALLER
);
118 rewind (ent
->stream
);
120 if (needent
&& status
== NSS_STATUS_SUCCESS
&& nss_setgrent
)
121 ent
->setent_status
= nss_setgrent (stayopen
);
128 _nss_compat_setgrent (int stayopen
)
130 enum nss_status result
;
132 __libc_lock_lock (lock
);
135 init_nss_interface ();
137 result
= internal_setgrent (&ext_ent
, stayopen
, 1);
139 __libc_lock_unlock (lock
);
145 static enum nss_status
146 internal_endgrent (ent_t
*ent
)
148 if (ent
->stream
!= NULL
)
150 fclose (ent
->stream
);
154 if (ent
->blacklist
.data
!= NULL
)
156 ent
->blacklist
.current
= 1;
157 ent
->blacklist
.data
[0] = '|';
158 ent
->blacklist
.data
[1] = '\0';
161 ent
->blacklist
.current
= 0;
163 return NSS_STATUS_SUCCESS
;
167 _nss_compat_endgrent (void)
169 enum nss_status result
;
171 __libc_lock_lock (lock
);
176 result
= internal_endgrent (&ext_ent
);
178 __libc_lock_unlock (lock
);
183 /* get the next group from NSS (+ entry) */
184 static enum nss_status
185 getgrent_next_nss (struct group
*result
, ent_t
*ent
, char *buffer
,
186 size_t buflen
, int *errnop
)
189 return NSS_STATUS_UNAVAIL
;
191 /* If the setgrent call failed, say so. */
192 if (ent
->setent_status
!= NSS_STATUS_SUCCESS
)
193 return ent
->setent_status
;
197 enum nss_status status
;
199 if ((status
= nss_getgrent_r (result
, buffer
, buflen
, errnop
)) !=
203 while (in_blacklist (result
->gr_name
, strlen (result
->gr_name
), ent
));
205 return NSS_STATUS_SUCCESS
;
208 /* This function handle the +group entrys in /etc/group */
209 static enum nss_status
210 getgrnam_plusgroup (const char *name
, struct group
*result
, ent_t
*ent
,
211 char *buffer
, size_t buflen
, int *errnop
)
214 return NSS_STATUS_UNAVAIL
;
216 enum nss_status status
= nss_getgrnam_r (name
, result
, buffer
, buflen
,
218 if (status
!= NSS_STATUS_SUCCESS
)
221 if (in_blacklist (result
->gr_name
, strlen (result
->gr_name
), ent
))
222 return NSS_STATUS_NOTFOUND
;
224 /* We found the entry. */
225 return NSS_STATUS_SUCCESS
;
228 static enum nss_status
229 getgrent_next_file (struct group
*result
, ent_t
*ent
,
230 char *buffer
, size_t buflen
, int *errnop
)
232 struct parser_data
*data
= (void *) buffer
;
241 /* We need at least 3 characters for one line. */
242 if (__glibc_unlikely (buflen
< 3))
246 return NSS_STATUS_TRYAGAIN
;
249 fgetpos (ent
->stream
, &pos
);
250 buffer
[buflen
- 1] = '\xff';
251 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
252 if (p
== NULL
&& feof_unlocked (ent
->stream
))
253 return NSS_STATUS_NOTFOUND
;
255 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
258 fsetpos (ent
->stream
, &pos
);
262 /* Terminate the line for any case. */
263 buffer
[buflen
- 1] = '\0';
265 /* Skip leading blanks. */
269 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
270 /* Parse the line. If it is invalid, loop to
271 get the next line of the file to parse. */
272 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
275 if (__glibc_unlikely (parse_res
== -1))
276 /* The parser ran out of space. */
279 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
280 /* This is a real entry. */
284 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0'
285 && result
->gr_name
[1] != '@')
287 blacklist_store_name (&result
->gr_name
[1], ent
);
292 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0'
293 && result
->gr_name
[1] != '@')
295 size_t len
= strlen (result
->gr_name
);
297 enum nss_status status
;
299 /* Store the group in the blacklist for the "+" at the end of
301 memcpy (buf
, &result
->gr_name
[1], len
);
302 status
= getgrnam_plusgroup (&result
->gr_name
[1], result
, ent
,
303 buffer
, buflen
, errnop
);
304 blacklist_store_name (buf
, ent
);
305 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
307 else if (status
== NSS_STATUS_RETURN
/* We couldn't parse the entry*/
308 || status
== NSS_STATUS_NOTFOUND
) /* No group in NIS */
312 if (status
== NSS_STATUS_TRYAGAIN
)
313 /* The parser ran out of space. */
321 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
325 return getgrent_next_nss (result
, ent
, buffer
, buflen
, errnop
);
329 return NSS_STATUS_SUCCESS
;
334 _nss_compat_getgrent_r (struct group
*grp
, char *buffer
, size_t buflen
,
337 enum nss_status result
= NSS_STATUS_SUCCESS
;
339 __libc_lock_lock (lock
);
341 /* Be prepared that the setgrent function was not called before. */
343 init_nss_interface ();
345 if (ext_ent
.stream
== NULL
)
346 result
= internal_setgrent (&ext_ent
, 1, 1);
348 if (result
== NSS_STATUS_SUCCESS
)
351 result
= getgrent_next_file (grp
, &ext_ent
, buffer
, buflen
, errnop
);
353 result
= getgrent_next_nss (grp
, &ext_ent
, buffer
, buflen
, errnop
);
355 __libc_lock_unlock (lock
);
360 /* Searches in /etc/group and the NIS/NIS+ map for a special group */
361 static enum nss_status
362 internal_getgrnam_r (const char *name
, struct group
*result
, ent_t
*ent
,
363 char *buffer
, size_t buflen
, int *errnop
)
365 struct parser_data
*data
= (void *) buffer
;
374 /* We need at least 3 characters for one line. */
375 if (__glibc_unlikely (buflen
< 3))
379 return NSS_STATUS_TRYAGAIN
;
382 fgetpos (ent
->stream
, &pos
);
383 buffer
[buflen
- 1] = '\xff';
384 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
385 if (p
== NULL
&& feof_unlocked (ent
->stream
))
386 return NSS_STATUS_NOTFOUND
;
388 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
391 fsetpos (ent
->stream
, &pos
);
395 /* Terminate the line for any case. */
396 buffer
[buflen
- 1] = '\0';
398 /* Skip leading blanks. */
402 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
403 /* Parse the line. If it is invalid, loop to
404 get the next line of the file to parse. */
405 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
408 if (__glibc_unlikely (parse_res
== -1))
409 /* The parser ran out of space. */
412 /* This is a real entry. */
413 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
415 if (strcmp (result
->gr_name
, name
) == 0)
416 return NSS_STATUS_SUCCESS
;
422 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0')
424 if (strcmp (&result
->gr_name
[1], name
) == 0)
425 return NSS_STATUS_NOTFOUND
;
431 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0')
433 if (strcmp (name
, &result
->gr_name
[1]) == 0)
435 enum nss_status status
;
437 status
= getgrnam_plusgroup (name
, result
, ent
,
438 buffer
, buflen
, errnop
);
439 if (status
== NSS_STATUS_RETURN
)
440 /* We couldn't parse the entry */
447 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
449 enum nss_status status
;
451 status
= getgrnam_plusgroup (name
, result
, ent
,
452 buffer
, buflen
, errnop
);
453 if (status
== NSS_STATUS_RETURN
)
454 /* We couldn't parse the entry */
461 return NSS_STATUS_SUCCESS
;
465 _nss_compat_getgrnam_r (const char *name
, struct group
*grp
,
466 char *buffer
, size_t buflen
, int *errnop
)
468 ent_t ent
= { true, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0 }};
469 enum nss_status result
;
471 if (name
[0] == '-' || name
[0] == '+')
472 return NSS_STATUS_NOTFOUND
;
474 __libc_lock_lock (lock
);
477 init_nss_interface ();
479 __libc_lock_unlock (lock
);
481 result
= internal_setgrent (&ent
, 0, 0);
483 if (result
== NSS_STATUS_SUCCESS
)
484 result
= internal_getgrnam_r (name
, grp
, &ent
, buffer
, buflen
, errnop
);
486 internal_endgrent (&ent
);
491 /* Searches in /etc/group and the NIS/NIS+ map for a special group id */
492 static enum nss_status
493 internal_getgrgid_r (gid_t gid
, struct group
*result
, ent_t
*ent
,
494 char *buffer
, size_t buflen
, int *errnop
)
496 struct parser_data
*data
= (void *) buffer
;
505 /* We need at least 3 characters for one line. */
506 if (__glibc_unlikely (buflen
< 3))
510 return NSS_STATUS_TRYAGAIN
;
513 fgetpos (ent
->stream
, &pos
);
514 buffer
[buflen
- 1] = '\xff';
515 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
516 if (p
== NULL
&& feof_unlocked (ent
->stream
))
517 return NSS_STATUS_NOTFOUND
;
519 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
522 fsetpos (ent
->stream
, &pos
);
526 /* Terminate the line for any case. */
527 buffer
[buflen
- 1] = '\0';
529 /* Skip leading blanks. */
533 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
534 /* Parse the line. If it is invalid, loop to
535 get the next line of the file to parse. */
536 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
539 if (__glibc_unlikely (parse_res
== -1))
540 /* The parser ran out of space. */
543 /* This is a real entry. */
544 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
546 if (result
->gr_gid
== gid
)
547 return NSS_STATUS_SUCCESS
;
553 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0')
555 blacklist_store_name (&result
->gr_name
[1], ent
);
560 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0')
562 /* Yes, no +1, see the memcpy call below. */
563 size_t len
= strlen (result
->gr_name
);
565 enum nss_status status
;
567 /* Store the group in the blacklist for the "+" at the end of
569 memcpy (buf
, &result
->gr_name
[1], len
);
570 status
= getgrnam_plusgroup (&result
->gr_name
[1], result
, ent
,
571 buffer
, buflen
, errnop
);
572 blacklist_store_name (buf
, ent
);
573 if (status
== NSS_STATUS_SUCCESS
&& result
->gr_gid
== gid
)
579 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
582 return NSS_STATUS_UNAVAIL
;
584 enum nss_status status
= nss_getgrgid_r (gid
, result
, buffer
, buflen
,
586 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
587 return NSS_STATUS_NOTFOUND
;
593 return NSS_STATUS_SUCCESS
;
597 _nss_compat_getgrgid_r (gid_t gid
, struct group
*grp
,
598 char *buffer
, size_t buflen
, int *errnop
)
600 ent_t ent
= { true, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0 }};
601 enum nss_status result
;
603 __libc_lock_lock (lock
);
606 init_nss_interface ();
608 __libc_lock_unlock (lock
);
610 result
= internal_setgrent (&ent
, 0, 0);
612 if (result
== NSS_STATUS_SUCCESS
)
613 result
= internal_getgrgid_r (gid
, grp
, &ent
, buffer
, buflen
, errnop
);
615 internal_endgrent (&ent
);
621 /* Support routines for remembering -@netgroup and -user entries.
622 The names are stored in a single string with `|' as separator. */
624 blacklist_store_name (const char *name
, ent_t
*ent
)
626 int namelen
= strlen (name
);
629 /* first call, setup cache */
630 if (ent
->blacklist
.size
== 0)
632 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
633 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
634 if (ent
->blacklist
.data
== NULL
)
636 ent
->blacklist
.data
[0] = '|';
637 ent
->blacklist
.data
[1] = '\0';
638 ent
->blacklist
.current
= 1;
642 if (in_blacklist (name
, namelen
, ent
))
643 return; /* no duplicates */
645 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
647 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
648 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
651 free (ent
->blacklist
.data
);
652 ent
->blacklist
.size
= 0;
655 ent
->blacklist
.data
= tmp
;
659 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
662 ent
->blacklist
.current
+= namelen
+ 1;
667 /* Return whether ent->blacklist contains name. */
669 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
671 char buf
[namelen
+ 3];
674 if (ent
->blacklist
.data
== NULL
)
678 cp
= stpcpy (&buf
[1], name
);
681 return strstr (ent
->blacklist
.data
, buf
) != NULL
;