1 /* Copyright (C) 1996-2017 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 <rpc/types.h>
28 #include <libc-lock.h>
29 #include <kernel-features.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 /* Positive if O_CLOEXEC is supported, negative if it is not supported,
74 zero if it is still undecided. This variable is shared with the
75 other compat functions. */
76 #ifdef __ASSUME_O_CLOEXEC
77 # define __compat_have_cloexec 1
80 int __compat_have_cloexec
;
82 # define __compat_have_cloexec -1
86 /* Prototypes for local functions. */
87 static void blacklist_store_name (const char *, ent_t
*);
88 static int in_blacklist (const char *, int, ent_t
*);
90 /* Initialize the NSS interface/functions. The calling function must
93 init_nss_interface (void)
95 if (__nss_database_lookup ("group_compat", NULL
, "nis", &ni
) >= 0)
97 nss_setgrent
= __nss_lookup_function (ni
, "setgrent");
98 nss_getgrnam_r
= __nss_lookup_function (ni
, "getgrnam_r");
99 nss_getgrgid_r
= __nss_lookup_function (ni
, "getgrgid_r");
100 nss_getgrent_r
= __nss_lookup_function (ni
, "getgrent_r");
101 nss_endgrent
= __nss_lookup_function (ni
, "endgrent");
105 static enum nss_status
106 internal_setgrent (ent_t
*ent
, int stayopen
, int needent
)
108 enum nss_status status
= NSS_STATUS_SUCCESS
;
112 if (ent
->blacklist
.data
!= NULL
)
114 ent
->blacklist
.current
= 1;
115 ent
->blacklist
.data
[0] = '|';
116 ent
->blacklist
.data
[1] = '\0';
119 ent
->blacklist
.current
= 0;
121 if (ent
->stream
== NULL
)
123 ent
->stream
= fopen ("/etc/group", "rme");
125 if (ent
->stream
== NULL
)
126 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
129 /* We have to make sure the file is `closed on exec'. */
132 if (__compat_have_cloexec
<= 0)
135 result
= flags
= fcntl (fileno_unlocked (ent
->stream
), F_GETFD
,
139 #if defined O_CLOEXEC && !defined __ASSUME_O_CLOEXEC
140 if (__compat_have_cloexec
== 0)
141 __compat_have_cloexec
= (flags
& FD_CLOEXEC
) ? 1 : -1;
143 if (__compat_have_cloexec
< 0)
147 result
= fcntl (fileno_unlocked (ent
->stream
), F_SETFD
,
155 /* Something went wrong. Close the stream and return a
157 fclose (ent
->stream
);
159 status
= NSS_STATUS_UNAVAIL
;
162 /* We take care of locking ourself. */
163 __fsetlocking (ent
->stream
, FSETLOCKING_BYCALLER
);
167 rewind (ent
->stream
);
169 if (needent
&& status
== NSS_STATUS_SUCCESS
&& nss_setgrent
)
170 ent
->setent_status
= nss_setgrent (stayopen
);
177 _nss_compat_setgrent (int stayopen
)
179 enum nss_status result
;
181 __libc_lock_lock (lock
);
184 init_nss_interface ();
186 result
= internal_setgrent (&ext_ent
, stayopen
, 1);
188 __libc_lock_unlock (lock
);
194 static enum nss_status
195 internal_endgrent (ent_t
*ent
)
197 if (ent
->stream
!= NULL
)
199 fclose (ent
->stream
);
203 if (ent
->blacklist
.data
!= NULL
)
205 ent
->blacklist
.current
= 1;
206 ent
->blacklist
.data
[0] = '|';
207 ent
->blacklist
.data
[1] = '\0';
210 ent
->blacklist
.current
= 0;
212 return NSS_STATUS_SUCCESS
;
216 _nss_compat_endgrent (void)
218 enum nss_status result
;
220 __libc_lock_lock (lock
);
225 result
= internal_endgrent (&ext_ent
);
227 __libc_lock_unlock (lock
);
232 /* get the next group from NSS (+ entry) */
233 static enum nss_status
234 getgrent_next_nss (struct group
*result
, ent_t
*ent
, char *buffer
,
235 size_t buflen
, int *errnop
)
238 return NSS_STATUS_UNAVAIL
;
240 /* If the setgrent call failed, say so. */
241 if (ent
->setent_status
!= NSS_STATUS_SUCCESS
)
242 return ent
->setent_status
;
246 enum nss_status status
;
248 if ((status
= nss_getgrent_r (result
, buffer
, buflen
, errnop
)) !=
252 while (in_blacklist (result
->gr_name
, strlen (result
->gr_name
), ent
));
254 return NSS_STATUS_SUCCESS
;
257 /* This function handle the +group entrys in /etc/group */
258 static enum nss_status
259 getgrnam_plusgroup (const char *name
, struct group
*result
, ent_t
*ent
,
260 char *buffer
, size_t buflen
, int *errnop
)
263 return NSS_STATUS_UNAVAIL
;
265 enum nss_status status
= nss_getgrnam_r (name
, result
, buffer
, buflen
,
267 if (status
!= NSS_STATUS_SUCCESS
)
270 if (in_blacklist (result
->gr_name
, strlen (result
->gr_name
), ent
))
271 return NSS_STATUS_NOTFOUND
;
273 /* We found the entry. */
274 return NSS_STATUS_SUCCESS
;
277 static enum nss_status
278 getgrent_next_file (struct group
*result
, ent_t
*ent
,
279 char *buffer
, size_t buflen
, int *errnop
)
281 struct parser_data
*data
= (void *) buffer
;
290 /* We need at least 3 characters for one line. */
291 if (__glibc_unlikely (buflen
< 3))
295 return NSS_STATUS_TRYAGAIN
;
298 fgetpos (ent
->stream
, &pos
);
299 buffer
[buflen
- 1] = '\xff';
300 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
301 if (p
== NULL
&& feof_unlocked (ent
->stream
))
302 return NSS_STATUS_NOTFOUND
;
304 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
307 fsetpos (ent
->stream
, &pos
);
311 /* Terminate the line for any case. */
312 buffer
[buflen
- 1] = '\0';
314 /* Skip leading blanks. */
318 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
319 /* Parse the line. If it is invalid, loop to
320 get the next line of the file to parse. */
321 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
324 if (__glibc_unlikely (parse_res
== -1))
325 /* The parser ran out of space. */
328 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
329 /* This is a real entry. */
333 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0'
334 && result
->gr_name
[1] != '@')
336 blacklist_store_name (&result
->gr_name
[1], ent
);
341 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0'
342 && result
->gr_name
[1] != '@')
344 size_t len
= strlen (result
->gr_name
);
346 enum nss_status status
;
348 /* Store the group in the blacklist for the "+" at the end of
350 memcpy (buf
, &result
->gr_name
[1], len
);
351 status
= getgrnam_plusgroup (&result
->gr_name
[1], result
, ent
,
352 buffer
, buflen
, errnop
);
353 blacklist_store_name (buf
, ent
);
354 if (status
== NSS_STATUS_SUCCESS
) /* We found the entry. */
356 else if (status
== NSS_STATUS_RETURN
/* We couldn't parse the entry*/
357 || status
== NSS_STATUS_NOTFOUND
) /* No group in NIS */
361 if (status
== NSS_STATUS_TRYAGAIN
)
362 /* The parser ran out of space. */
370 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
374 return getgrent_next_nss (result
, ent
, buffer
, buflen
, errnop
);
378 return NSS_STATUS_SUCCESS
;
383 _nss_compat_getgrent_r (struct group
*grp
, char *buffer
, size_t buflen
,
386 enum nss_status result
= NSS_STATUS_SUCCESS
;
388 __libc_lock_lock (lock
);
390 /* Be prepared that the setgrent function was not called before. */
392 init_nss_interface ();
394 if (ext_ent
.stream
== NULL
)
395 result
= internal_setgrent (&ext_ent
, 1, 1);
397 if (result
== NSS_STATUS_SUCCESS
)
400 result
= getgrent_next_file (grp
, &ext_ent
, buffer
, buflen
, errnop
);
402 result
= getgrent_next_nss (grp
, &ext_ent
, buffer
, buflen
, errnop
);
404 __libc_lock_unlock (lock
);
409 /* Searches in /etc/group and the NIS/NIS+ map for a special group */
410 static enum nss_status
411 internal_getgrnam_r (const char *name
, struct group
*result
, ent_t
*ent
,
412 char *buffer
, size_t buflen
, int *errnop
)
414 struct parser_data
*data
= (void *) buffer
;
423 /* We need at least 3 characters for one line. */
424 if (__glibc_unlikely (buflen
< 3))
428 return NSS_STATUS_TRYAGAIN
;
431 fgetpos (ent
->stream
, &pos
);
432 buffer
[buflen
- 1] = '\xff';
433 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
434 if (p
== NULL
&& feof_unlocked (ent
->stream
))
435 return NSS_STATUS_NOTFOUND
;
437 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
440 fsetpos (ent
->stream
, &pos
);
444 /* Terminate the line for any case. */
445 buffer
[buflen
- 1] = '\0';
447 /* Skip leading blanks. */
451 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
452 /* Parse the line. If it is invalid, loop to
453 get the next line of the file to parse. */
454 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
457 if (__glibc_unlikely (parse_res
== -1))
458 /* The parser ran out of space. */
461 /* This is a real entry. */
462 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
464 if (strcmp (result
->gr_name
, name
) == 0)
465 return NSS_STATUS_SUCCESS
;
471 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0')
473 if (strcmp (&result
->gr_name
[1], name
) == 0)
474 return NSS_STATUS_NOTFOUND
;
480 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0')
482 if (strcmp (name
, &result
->gr_name
[1]) == 0)
484 enum nss_status status
;
486 status
= getgrnam_plusgroup (name
, result
, ent
,
487 buffer
, buflen
, errnop
);
488 if (status
== NSS_STATUS_RETURN
)
489 /* We couldn't parse the entry */
496 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
498 enum nss_status status
;
500 status
= getgrnam_plusgroup (name
, result
, ent
,
501 buffer
, buflen
, errnop
);
502 if (status
== NSS_STATUS_RETURN
)
503 /* We couldn't parse the entry */
510 return NSS_STATUS_SUCCESS
;
514 _nss_compat_getgrnam_r (const char *name
, struct group
*grp
,
515 char *buffer
, size_t buflen
, int *errnop
)
517 ent_t ent
= { TRUE
, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0 }};
518 enum nss_status result
;
520 if (name
[0] == '-' || name
[0] == '+')
521 return NSS_STATUS_NOTFOUND
;
523 __libc_lock_lock (lock
);
526 init_nss_interface ();
528 __libc_lock_unlock (lock
);
530 result
= internal_setgrent (&ent
, 0, 0);
532 if (result
== NSS_STATUS_SUCCESS
)
533 result
= internal_getgrnam_r (name
, grp
, &ent
, buffer
, buflen
, errnop
);
535 internal_endgrent (&ent
);
540 /* Searches in /etc/group and the NIS/NIS+ map for a special group id */
541 static enum nss_status
542 internal_getgrgid_r (gid_t gid
, struct group
*result
, ent_t
*ent
,
543 char *buffer
, size_t buflen
, int *errnop
)
545 struct parser_data
*data
= (void *) buffer
;
554 /* We need at least 3 characters for one line. */
555 if (__glibc_unlikely (buflen
< 3))
559 return NSS_STATUS_TRYAGAIN
;
562 fgetpos (ent
->stream
, &pos
);
563 buffer
[buflen
- 1] = '\xff';
564 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
565 if (p
== NULL
&& feof_unlocked (ent
->stream
))
566 return NSS_STATUS_NOTFOUND
;
568 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
571 fsetpos (ent
->stream
, &pos
);
575 /* Terminate the line for any case. */
576 buffer
[buflen
- 1] = '\0';
578 /* Skip leading blanks. */
582 while (*p
== '\0' || *p
== '#' || /* Ignore empty and comment lines. */
583 /* Parse the line. If it is invalid, loop to
584 get the next line of the file to parse. */
585 !(parse_res
= _nss_files_parse_grent (p
, result
, data
, buflen
,
588 if (__glibc_unlikely (parse_res
== -1))
589 /* The parser ran out of space. */
592 /* This is a real entry. */
593 if (result
->gr_name
[0] != '+' && result
->gr_name
[0] != '-')
595 if (result
->gr_gid
== gid
)
596 return NSS_STATUS_SUCCESS
;
602 if (result
->gr_name
[0] == '-' && result
->gr_name
[1] != '\0')
604 blacklist_store_name (&result
->gr_name
[1], ent
);
609 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] != '\0')
611 /* Yes, no +1, see the memcpy call below. */
612 size_t len
= strlen (result
->gr_name
);
614 enum nss_status status
;
616 /* Store the group in the blacklist for the "+" at the end of
618 memcpy (buf
, &result
->gr_name
[1], len
);
619 status
= getgrnam_plusgroup (&result
->gr_name
[1], result
, ent
,
620 buffer
, buflen
, errnop
);
621 blacklist_store_name (buf
, ent
);
622 if (status
== NSS_STATUS_SUCCESS
&& result
->gr_gid
== gid
)
628 if (result
->gr_name
[0] == '+' && result
->gr_name
[1] == '\0')
631 return NSS_STATUS_UNAVAIL
;
633 enum nss_status status
= nss_getgrgid_r (gid
, result
, buffer
, buflen
,
635 if (status
== NSS_STATUS_RETURN
) /* We couldn't parse the entry */
636 return NSS_STATUS_NOTFOUND
;
642 return NSS_STATUS_SUCCESS
;
646 _nss_compat_getgrgid_r (gid_t gid
, struct group
*grp
,
647 char *buffer
, size_t buflen
, int *errnop
)
649 ent_t ent
= { TRUE
, NSS_STATUS_SUCCESS
, NULL
, { NULL
, 0, 0 }};
650 enum nss_status result
;
652 __libc_lock_lock (lock
);
655 init_nss_interface ();
657 __libc_lock_unlock (lock
);
659 result
= internal_setgrent (&ent
, 0, 0);
661 if (result
== NSS_STATUS_SUCCESS
)
662 result
= internal_getgrgid_r (gid
, grp
, &ent
, buffer
, buflen
, errnop
);
664 internal_endgrent (&ent
);
670 /* Support routines for remembering -@netgroup and -user entries.
671 The names are stored in a single string with `|' as separator. */
673 blacklist_store_name (const char *name
, ent_t
*ent
)
675 int namelen
= strlen (name
);
678 /* first call, setup cache */
679 if (ent
->blacklist
.size
== 0)
681 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
682 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
683 if (ent
->blacklist
.data
== NULL
)
685 ent
->blacklist
.data
[0] = '|';
686 ent
->blacklist
.data
[1] = '\0';
687 ent
->blacklist
.current
= 1;
691 if (in_blacklist (name
, namelen
, ent
))
692 return; /* no duplicates */
694 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
696 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
697 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
700 free (ent
->blacklist
.data
);
701 ent
->blacklist
.size
= 0;
704 ent
->blacklist
.data
= tmp
;
708 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
711 ent
->blacklist
.current
+= namelen
+ 1;
716 /* returns TRUE if ent->blacklist contains name, else FALSE */
718 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
720 char buf
[namelen
+ 3];
723 if (ent
->blacklist
.data
== NULL
)
727 cp
= stpcpy (&buf
[1], name
);
730 return strstr (ent
->blacklist
.data
, buf
) != NULL
;