1 /* Copyright (C) 1998-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
23 #include <stdio_ext.h>
26 #include <sys/param.h>
28 #include <libc-lock.h>
29 #include <kernel-features.h>
30 #include <scratch_buffer.h>
31 #include <nss_files.h>
33 NSS_DECLARE_MODULE_FUNCTIONS (compat
)
35 static nss_action_list ni
;
36 static enum nss_status (*initgroups_dyn_impl
) (const char *, gid_t
,
37 long int *, long int *,
38 gid_t
**, long int, int *);
39 static enum nss_status (*getgrnam_r_impl
) (const char *name
,
40 struct group
* grp
, char *buffer
,
41 size_t buflen
, int *errnop
);
42 static enum nss_status (*getgrgid_r_impl
) (gid_t gid
, struct group
* grp
,
43 char *buffer
, size_t buflen
,
45 static enum nss_status (*setgrent_impl
) (int stayopen
);
46 static enum nss_status (*getgrent_r_impl
) (struct group
* grp
, char *buffer
,
47 size_t buflen
, int *errnop
);
48 static enum nss_status (*endgrent_impl
) (void);
50 /* Protect global state against multiple changers. */
51 __libc_lock_define_initialized (static, lock
)
54 /* Get the declaration of the parser function. */
56 #define STRUCTURE group
58 #include <nss/nss_files/files-parse.c>
60 /* Structure for remembering -group members ... */
61 #define BLACKLIST_INITIAL_SIZE 512
62 #define BLACKLIST_INCREMENT 256
74 bool skip_initgroups_dyn
;
76 struct blacklist_t blacklist
;
78 typedef struct ent_t ent_t
;
80 /* Prototypes for local functions. */
81 static void blacklist_store_name (const char *, ent_t
*);
82 static bool in_blacklist (const char *, int, ent_t
*);
84 /* Initialize the NSS interface/functions. The calling function must
87 init_nss_interface (void)
89 __libc_lock_lock (lock
);
93 && __nss_database_get (nss_database_group_compat
, &ni
))
95 initgroups_dyn_impl
= __nss_lookup_function (ni
, "initgroups_dyn");
96 getgrnam_r_impl
= __nss_lookup_function (ni
, "getgrnam_r");
97 getgrgid_r_impl
= __nss_lookup_function (ni
, "getgrgid_r");
98 setgrent_impl
= __nss_lookup_function (ni
, "setgrent");
99 getgrent_r_impl
= __nss_lookup_function (ni
, "getgrent_r");
100 endgrent_impl
= __nss_lookup_function (ni
, "endgrent");
103 __libc_lock_unlock (lock
);
106 static enum nss_status
107 internal_setgrent (ent_t
*ent
)
109 enum nss_status status
= NSS_STATUS_SUCCESS
;
114 init_nss_interface ();
116 if (ent
->blacklist
.data
!= NULL
)
118 ent
->blacklist
.current
= 1;
119 ent
->blacklist
.data
[0] = '|';
120 ent
->blacklist
.data
[1] = '\0';
123 ent
->blacklist
.current
= 0;
125 ent
->stream
= __nss_files_fopen ("/etc/group");
127 if (ent
->stream
== NULL
)
128 status
= errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
134 static enum nss_status __attribute_warn_unused_result__
135 internal_endgrent (ent_t
*ent
)
137 if (ent
->stream
!= NULL
)
139 fclose (ent
->stream
);
143 if (ent
->blacklist
.data
!= NULL
)
145 ent
->blacklist
.current
= 1;
146 ent
->blacklist
.data
[0] = '|';
147 ent
->blacklist
.data
[1] = '\0';
150 ent
->blacklist
.current
= 0;
152 if (ent
->need_endgrent
&& endgrent_impl
!= NULL
)
155 return NSS_STATUS_SUCCESS
;
158 /* Like internal_endgrent, but preserve errno in all cases. */
160 internal_endgrent_noerror (ent_t
*ent
)
162 int saved_errno
= errno
;
163 enum nss_status unused
__attribute__ ((unused
)) = internal_endgrent (ent
);
164 __set_errno (saved_errno
);
167 /* Add new group record. */
169 add_group (long int *start
, long int *size
, gid_t
**groupsp
, long int limit
,
172 gid_t
*groups
= *groupsp
;
174 /* Matches user. Insert this group. */
175 if (__glibc_unlikely (*start
== *size
))
177 /* Need a bigger buffer. */
181 if (limit
> 0 && *size
== limit
)
182 /* We reached the maximum. */
188 newsize
= MIN (limit
, 2 * *size
);
190 newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
191 if (newgroups
== NULL
)
193 *groupsp
= groups
= newgroups
;
197 groups
[*start
] = gid
;
201 /* This function checks, if the user is a member of this group and if
202 yes, add the group id to the list. Return nonzero is we couldn't
203 handle the group because the user is not in the member list. */
205 check_and_add_group (const char *user
, gid_t group
, long int *start
,
206 long int *size
, gid_t
**groupsp
, long int limit
,
211 /* Don't add main group to list of groups. */
212 if (grp
->gr_gid
== group
)
215 for (member
= grp
->gr_mem
; *member
!= NULL
; ++member
)
216 if (strcmp (*member
, user
) == 0)
218 add_group (start
, size
, groupsp
, limit
, grp
->gr_gid
);
225 /* Get the next group from NSS (+ entry). If the NSS module supports
226 initgroups_dyn, get all entries at once. */
227 static enum nss_status
228 getgrent_next_nss (ent_t
*ent
, char *buffer
, size_t buflen
, const char *user
,
229 gid_t group
, long int *start
, long int *size
,
230 gid_t
**groupsp
, long int limit
, int *errnop
)
232 enum nss_status status
;
235 /* Try nss_initgroups_dyn if supported. We also need getgrgid_r.
236 If this function is not supported, step through the whole group
237 database with getgrent_r. */
238 if (! ent
->skip_initgroups_dyn
)
240 long int mystart
= 0;
241 long int mysize
= limit
<= 0 ? *size
: limit
;
242 gid_t
*mygroups
= malloc (mysize
* sizeof (gid_t
));
244 if (mygroups
== NULL
)
245 return NSS_STATUS_TRYAGAIN
;
247 /* For every gid in the list we get from the NSS module,
248 get the whole group entry. We need to do this, since we
249 need the group name to check if it is in the blacklist.
250 In worst case, this is as twice as slow as stepping with
251 getgrent_r through the whole group database. But for large
252 group databases this is faster, since the user can only be
253 in a limited number of groups. */
254 if (initgroups_dyn_impl (user
, group
, &mystart
, &mysize
, &mygroups
,
255 limit
, errnop
) == NSS_STATUS_SUCCESS
)
257 status
= NSS_STATUS_NOTFOUND
;
259 /* If there is no blacklist we can trust the underlying
260 initgroups implementation. */
261 if (ent
->blacklist
.current
<= 1)
262 for (int i
= 0; i
< mystart
; i
++)
263 add_group (start
, size
, groupsp
, limit
, mygroups
[i
]);
266 /* A temporary buffer. We use the normal buffer, until we find
267 an entry, for which this buffer is to small. In this case, we
268 overwrite the pointer with one to a bigger buffer. */
269 char *tmpbuf
= buffer
;
270 size_t tmplen
= buflen
;
272 for (int i
= 0; i
< mystart
; i
++)
274 while ((status
= getgrgid_r_impl (mygroups
[i
], &grpbuf
,
275 tmpbuf
, tmplen
, errnop
))
276 == NSS_STATUS_TRYAGAIN
277 && *errnop
== ERANGE
)
279 /* Check for overflow. */
280 if (__glibc_unlikely (tmplen
* 2 < tmplen
))
282 __set_errno (ENOMEM
);
283 status
= NSS_STATUS_TRYAGAIN
;
286 /* Increase the size. Make sure that we retry
287 with a reasonable size. */
291 if (tmpbuf
!= buffer
)
293 tmpbuf
= malloc (tmplen
);
294 if (__glibc_unlikely (tmpbuf
== NULL
))
296 status
= NSS_STATUS_TRYAGAIN
;
301 if (__builtin_expect (status
!= NSS_STATUS_NOTFOUND
, 1))
303 if (__builtin_expect (status
!= NSS_STATUS_SUCCESS
, 0))
306 if (!in_blacklist (grpbuf
.gr_name
,
307 strlen (grpbuf
.gr_name
), ent
)
308 && check_and_add_group (user
, group
, start
, size
,
309 groupsp
, limit
, &grpbuf
))
311 if (setgrent_impl
!= NULL
)
314 ent
->need_endgrent
= true;
316 ent
->skip_initgroups_dyn
= true;
323 status
= NSS_STATUS_NOTFOUND
;
326 if (tmpbuf
!= buffer
)
338 /* If we come here, the NSS module does not support initgroups_dyn
339 or we were confronted with a split group. In these cases we have
340 to step through the whole list ourself. */
344 if ((status
= getgrent_r_impl (&grpbuf
, buffer
, buflen
, errnop
))
345 != NSS_STATUS_SUCCESS
)
348 while (in_blacklist (grpbuf
.gr_name
, strlen (grpbuf
.gr_name
), ent
));
350 if (status
== NSS_STATUS_SUCCESS
)
351 check_and_add_group (user
, group
, start
, size
, groupsp
, limit
, &grpbuf
);
356 static enum nss_status
357 internal_getgrent_r (ent_t
*ent
, char *buffer
, size_t buflen
, const char *user
,
358 gid_t group
, long int *start
, long int *size
,
359 gid_t
**groupsp
, long int limit
, int *errnop
)
361 struct parser_data
*data
= (void *) buffer
;
365 return getgrent_next_nss (ent
, buffer
, buflen
, user
, group
,
366 start
, size
, groupsp
, limit
, errnop
);
376 /* We need at least 3 characters for one line. */
377 if (__glibc_unlikely (buflen
< 3))
381 return NSS_STATUS_TRYAGAIN
;
384 fgetpos (ent
->stream
, &pos
);
385 buffer
[buflen
- 1] = '\xff';
386 p
= fgets_unlocked (buffer
, buflen
, ent
->stream
);
387 if (p
== NULL
&& feof_unlocked (ent
->stream
))
388 return NSS_STATUS_NOTFOUND
;
390 if (p
== NULL
|| __builtin_expect (buffer
[buflen
- 1] != '\xff', 0))
393 fsetpos (ent
->stream
, &pos
);
397 /* Terminate the line for any case. */
398 buffer
[buflen
- 1] = '\0';
400 /* Skip leading blanks. */
404 while (*p
== '\0' || *p
== '#' /* Ignore empty and comment lines. */
405 /* Parse the line. If it is invalid, loop to
406 get the next line of the file to parse. */
407 || !(parse_res
= _nss_files_parse_grent (p
, &grpbuf
, data
, buflen
,
410 if (__glibc_unlikely (parse_res
== -1))
411 /* The parser ran out of space. */
414 if (grpbuf
.gr_name
[0] != '+' && grpbuf
.gr_name
[0] != '-')
415 /* This is a real entry. */
419 if (grpbuf
.gr_name
[0] == '-' && grpbuf
.gr_name
[1] != '\0'
420 && grpbuf
.gr_name
[1] != '@')
422 blacklist_store_name (&grpbuf
.gr_name
[1], ent
);
427 if (grpbuf
.gr_name
[0] == '+' && grpbuf
.gr_name
[1] != '\0'
428 && grpbuf
.gr_name
[1] != '@')
430 if (in_blacklist (&grpbuf
.gr_name
[1],
431 strlen (&grpbuf
.gr_name
[1]), ent
))
433 /* Store the group in the blacklist for the "+" at the end of
435 blacklist_store_name (&grpbuf
.gr_name
[1], ent
);
436 if (getgrnam_r_impl
== NULL
)
437 return NSS_STATUS_UNAVAIL
;
438 else if (getgrnam_r_impl (&grpbuf
.gr_name
[1], &grpbuf
, buffer
,
439 buflen
, errnop
) != NSS_STATUS_SUCCESS
)
442 check_and_add_group (user
, group
, start
, size
, groupsp
,
445 return NSS_STATUS_SUCCESS
;
449 if (grpbuf
.gr_name
[0] == '+' && grpbuf
.gr_name
[1] == '\0')
451 /* If the selected module does not support getgrent_r or
452 initgroups_dyn, abort. We cannot find the needed group
454 if (initgroups_dyn_impl
== NULL
|| getgrgid_r_impl
== NULL
)
456 if (setgrent_impl
!= NULL
)
459 ent
->need_endgrent
= true;
461 ent
->skip_initgroups_dyn
= true;
463 if (getgrent_r_impl
== NULL
)
464 return NSS_STATUS_UNAVAIL
;
469 return getgrent_next_nss (ent
, buffer
, buflen
, user
, group
,
470 start
, size
, groupsp
, limit
, errnop
);
474 check_and_add_group (user
, group
, start
, size
, groupsp
, limit
, &grpbuf
);
476 return NSS_STATUS_SUCCESS
;
481 _nss_compat_initgroups_dyn (const char *user
, gid_t group
, long int *start
,
482 long int *size
, gid_t
**groupsp
, long int limit
,
485 enum nss_status status
;
486 ent_t intern
= { true, false, false, NULL
, {NULL
, 0, 0} };
488 status
= internal_setgrent (&intern
);
489 if (status
!= NSS_STATUS_SUCCESS
)
492 struct scratch_buffer tmpbuf
;
493 scratch_buffer_init (&tmpbuf
);
497 while ((status
= internal_getgrent_r (&intern
, tmpbuf
.data
, tmpbuf
.length
,
498 user
, group
, start
, size
,
499 groupsp
, limit
, errnop
))
500 == NSS_STATUS_TRYAGAIN
&& *errnop
== ERANGE
)
501 if (!scratch_buffer_grow (&tmpbuf
))
504 while (status
== NSS_STATUS_SUCCESS
);
506 status
= NSS_STATUS_SUCCESS
;
509 scratch_buffer_free (&tmpbuf
);
511 internal_endgrent_noerror (&intern
);
517 /* Support routines for remembering -@netgroup and -user entries.
518 The names are stored in a single string with `|' as separator. */
520 blacklist_store_name (const char *name
, ent_t
*ent
)
522 int namelen
= strlen (name
);
525 /* First call, setup cache. */
526 if (ent
->blacklist
.size
== 0)
528 ent
->blacklist
.size
= MAX (BLACKLIST_INITIAL_SIZE
, 2 * namelen
);
529 ent
->blacklist
.data
= malloc (ent
->blacklist
.size
);
530 if (ent
->blacklist
.data
== NULL
)
532 ent
->blacklist
.data
[0] = '|';
533 ent
->blacklist
.data
[1] = '\0';
534 ent
->blacklist
.current
= 1;
538 if (in_blacklist (name
, namelen
, ent
))
539 return; /* no duplicates */
541 if (ent
->blacklist
.current
+ namelen
+ 1 >= ent
->blacklist
.size
)
543 ent
->blacklist
.size
+= MAX (BLACKLIST_INCREMENT
, 2 * namelen
);
544 tmp
= realloc (ent
->blacklist
.data
, ent
->blacklist
.size
);
547 free (ent
->blacklist
.data
);
548 ent
->blacklist
.size
= 0;
551 ent
->blacklist
.data
= tmp
;
555 tmp
= stpcpy (ent
->blacklist
.data
+ ent
->blacklist
.current
, name
);
558 ent
->blacklist
.current
+= namelen
+ 1;
563 /* Return whether ent->blacklist contains name. */
565 in_blacklist (const char *name
, int namelen
, ent_t
*ent
)
567 char buf
[namelen
+ 3];
570 if (ent
->blacklist
.data
== NULL
)
574 cp
= stpcpy (&buf
[1], name
);
577 return strstr (ent
->blacklist
.data
, buf
) != NULL
;