1 /* Copyright (C) 1989, 1991-2017 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 <http://www.gnu.org/licenses/>. */
25 #include <sys/param.h>
26 #include <sys/types.h>
28 #include <scratch_buffer.h>
30 #include "../nscd/nscd-client.h"
31 #include "../nscd/nscd_proto.h"
34 /* Type of the lookup function. */
35 typedef enum nss_status (*initgroups_dyn_function
) (const char *, gid_t
,
36 long int *, long int *,
37 gid_t
**, long int, int *);
39 extern service_user
*__nss_group_database attribute_hidden
;
40 service_user
*__nss_initgroups_database
;
41 static bool use_initgroups_entry
;
44 #include "compat-initgroups.c"
48 internal_getgrouplist (const char *user
, gid_t group
, long int *size
,
49 gid_t
**groupsp
, long int limit
)
52 if (__nss_not_use_nscd_group
> 0
53 && ++__nss_not_use_nscd_group
> NSS_NSCD_RETRY
)
54 __nss_not_use_nscd_group
= 0;
55 if (!__nss_not_use_nscd_group
56 && !__nss_database_custom
[NSS_DBSIDX_group
])
58 int n
= __nscd_getgrouplist (user
, group
, size
, groupsp
, limit
);
62 /* nscd is not usable. */
63 __nss_not_use_nscd_group
= 1;
67 enum nss_status status
= NSS_STATUS_UNAVAIL
;
70 /* Never store more than the starting *SIZE number of elements. */
72 (*groupsp
)[0] = group
;
73 /* Start is one, because we have the first group as parameter. */
76 if (__nss_initgroups_database
== NULL
)
78 if (__nss_database_lookup ("initgroups", NULL
, "",
79 &__nss_initgroups_database
) < 0)
81 if (__nss_group_database
== NULL
)
82 no_more
= __nss_database_lookup ("group", NULL
, "compat files",
83 &__nss_group_database
);
85 __nss_initgroups_database
= __nss_group_database
;
88 use_initgroups_entry
= true;
91 /* __nss_initgroups_database might have been set through
92 __nss_configure_lookup in which case use_initgroups_entry was
94 use_initgroups_entry
= __nss_initgroups_database
!= __nss_group_database
;
96 service_user
*nip
= __nss_initgroups_database
;
99 long int prev_start
= start
;
101 initgroups_dyn_function fct
= __nss_lookup_function (nip
,
104 status
= compat_call (nip
, user
, group
, &start
, size
, groupsp
,
107 status
= DL_CALL_FCT (fct
, (user
, group
, &start
, size
, groupsp
,
110 /* Remove duplicates. */
111 long int cnt
= prev_start
;
115 for (inner
= 0; inner
< prev_start
; ++inner
)
116 if ((*groupsp
)[inner
] == (*groupsp
)[cnt
])
119 if (inner
< prev_start
)
120 (*groupsp
)[cnt
] = (*groupsp
)[--start
];
125 /* This is really only for debugging. */
126 if (NSS_STATUS_TRYAGAIN
> status
|| status
> NSS_STATUS_RETURN
)
127 __libc_fatal ("illegal status in internal_getgrouplist");
129 /* For compatibility reason we will continue to look for more
130 entries using the next service even though data has already
131 been found if the nsswitch.conf file contained only a 'groups'
132 line and no 'initgroups' line. If the latter is available
133 we always respect the status. This means that the default
134 for successful lookups is to return. */
135 if ((use_initgroups_entry
|| status
!= NSS_STATUS_SUCCESS
)
136 && nss_next_action (nip
, status
) == NSS_ACTION_RETURN
)
139 if (nip
->next
== NULL
)
148 /* Store at most *NGROUPS members of the group set for USER into
149 *GROUPS. Also include GROUP. The actual number of groups found is
150 returned in *NGROUPS. Return -1 if the if *NGROUPS is too small. */
152 getgrouplist (const char *user
, gid_t group
, gid_t
*groups
, int *ngroups
)
154 long int size
= MAX (1, *ngroups
);
156 gid_t
*newgroups
= (gid_t
*) malloc (size
* sizeof (gid_t
));
157 if (__glibc_unlikely (newgroups
== NULL
))
158 /* No more memory. */
159 // XXX This is wrong. The user provided memory, we have to use
160 // XXX it. The internal functions must be called with the user
161 // XXX provided buffer and not try to increase the size if it is
162 // XXX too small. For initgroups a flag could say: increase size.
165 int total
= internal_getgrouplist (user
, group
, &size
, &newgroups
, -1);
167 memcpy (groups
, newgroups
, MIN (*ngroups
, total
) * sizeof (gid_t
));
171 int retval
= total
> *ngroups
? -1 : total
;
177 nss_interface_function (getgrouplist
)
179 /* Initialize the group set for the current user
180 by reading the group database and using all groups
181 of which USER is a member. Also include GROUP. */
183 initgroups (const char *user
, gid_t group
)
185 #if defined NGROUPS_MAX && NGROUPS_MAX == 0
187 /* No extra groups allowed. */
197 /* We always use sysconf even if NGROUPS_MAX is defined. That way, the
198 limit can be raised in the kernel configuration without having to
200 long int limit
= __sysconf (_SC_NGROUPS_MAX
);
203 /* We limit the size of the intially allocated array. */
204 size
= MIN (limit
, 64);
206 /* No fixed limit on groups. Pick a starting buffer size. */
209 groups
= (gid_t
*) malloc (size
* sizeof (gid_t
));
210 if (__glibc_unlikely (groups
== NULL
))
211 /* No more memory. */
214 ngroups
= internal_getgrouplist (user
, group
, &size
, &groups
, limit
);
216 /* Try to set the maximum number of groups the kernel can handle. */
218 result
= setgroups (ngroups
, groups
);
219 while (result
== -1 && errno
== EINVAL
&& --ngroups
> 0);
227 nss_interface_function (initgroups
)