2 Unix SMB/CIFS implementation.
3 system call wrapper interface.
4 Copyright (C) Andrew Tridgell 2002
5 Copyright (C) Andrew Barteltt 2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 This file may assume linkage with smbd - for things like become_root()
29 #ifndef HAVE_GETGROUPLIST
31 static int int_compare( int *a, int *b )
41 void remove_duplicate_gids( int *num_groups, gid_t *groups )
44 int count = *num_groups;
46 if ( *num_groups <= 0 || !groups )
49 DEBUG(8,("remove_duplicate_gids: Enter %d gids\n", *num_groups));
51 qsort( groups, *num_groups, sizeof(gid_t), QSORT_CAST int_compare );
53 for ( i=1; i<count; ) {
54 if ( groups[i-1] == groups[i] ) {
55 memmove( &groups[i-1], &groups[i], (count - i + 1)*sizeof(gid_t) );
57 /* decrement the total number of groups and do not increment
67 DEBUG(8,("remove_duplicate_gids: Exit %d gids\n", *num_groups));
73 This is a *much* faster way of getting the list of groups for a user
74 without changing the current supplementary group list. The old
75 method used getgrent() which could take 20 minutes on a really big
76 network with hundeds of thousands of groups and users. The new method
77 takes a couple of seconds.
79 NOTE!! this function only works if it is called as root!
82 static int getgrouplist_internals(const char *user, gid_t gid, gid_t *groups, int *grpcnt)
85 int ret, ngrp_saved, num_gids;
87 if (non_root_mode()) {
92 /* work out how many groups we need to save */
93 ngrp_saved = getgroups(0, NULL);
94 if (ngrp_saved == -1) {
95 /* this shouldn't happen */
99 gids_saved = SMB_MALLOC_ARRAY(gid_t, ngrp_saved+1);
105 ngrp_saved = getgroups(ngrp_saved, gids_saved);
106 if (ngrp_saved == -1) {
107 SAFE_FREE(gids_saved);
112 if (initgroups(user, gid) != 0) {
113 DEBUG(0, ("getgrouplist_internals: initgroups() failed!\n"));
114 SAFE_FREE(gids_saved);
118 /* this must be done to cope with systems that put the current egid in the
119 return from getgroups() */
121 set_effective_gid(gid);
124 num_gids = getgroups(0, NULL);
125 if (num_gids == -1) {
126 SAFE_FREE(gids_saved);
131 if (num_gids + 1 > *grpcnt) {
132 *grpcnt = num_gids + 1;
135 ret = getgroups(*grpcnt - 1, &groups[1]);
137 SAFE_FREE(gids_saved);
144 /* remove any duplicates gids in the list */
145 remove_duplicate_gids( grpcnt, groups );
150 if (sys_setgroups(ngrp_saved, gids_saved) != 0) {
152 DEBUG(0,("ERROR: getgrouplist: failed to reset group list!\n"));
153 smb_panic("getgrouplist: failed to reset group list!\n");
163 static int sys_getgrouplist(const char *user, gid_t gid, gid_t *groups, int *grpcnt)
167 DEBUG(10,("sys_getgrouplist: user [%s]\n", user));
169 /* see if we should disable winbindd lookups for local users */
170 if (strchr(user, *lp_winbind_separator()) == NULL) {
171 if ( !winbind_off() )
172 DEBUG(0,("sys_getgroup_list: Insufficient environment space for %s\n",
175 DEBUG(10,("sys_getgrouplist(): disabled winbindd for group lookup [user == %s]\n",
179 #ifdef HAVE_GETGROUPLIST
180 retval = getgrouplist(user, gid, groups, grpcnt);
183 retval = getgrouplist_internals(user, gid, groups, grpcnt);
187 /* allow winbindd lookups */
193 static BOOL getgroups_user(const char *user, gid_t primary_gid,
194 gid_t **ret_groups, size_t *p_ngroups)
202 max_grp = groups_max();
203 temp_groups = SMB_MALLOC_ARRAY(gid_t, max_grp);
208 if (sys_getgrouplist(user, primary_gid, temp_groups, &max_grp) == -1) {
211 groups_tmp = SMB_REALLOC_ARRAY(temp_groups, gid_t, max_grp);
214 SAFE_FREE(temp_groups);
217 temp_groups = groups_tmp;
219 if (sys_getgrouplist(user, primary_gid,
220 temp_groups, &max_grp) == -1) {
221 DEBUG(0, ("get_user_groups: failed to get the unix "
223 SAFE_FREE(temp_groups);
231 /* Add in primary group first */
232 add_gid_to_array_unique(NULL, primary_gid, &groups, &ngrp);
234 for (i=0; i<max_grp; i++)
235 add_gid_to_array_unique(NULL, temp_groups[i], &groups, &ngrp);
238 *ret_groups = groups;
239 SAFE_FREE(temp_groups);
243 NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
244 const char *username,
248 size_t *p_num_groups)
252 if (!getgroups_user(username, primary_gid, pp_gids, p_num_groups)) {
253 return NT_STATUS_NO_SUCH_USER;
256 if (*p_num_groups == 0) {
257 smb_panic("primary group missing");
260 *pp_sids = SMB_MALLOC_ARRAY(DOM_SID, *p_num_groups);
262 if (*pp_sids == NULL) {
264 return NT_STATUS_NO_MEMORY;
267 for (i=0; i<*p_num_groups; i++) {
268 if (!NT_STATUS_IS_OK(gid_to_sid(&(*pp_sids)[i], (*pp_gids)[i]))) {
269 DEBUG(1, ("get_user_groups: failed to convert "
270 "gid %ld to a sid!\n",
271 (long int)(*pp_gids)[i+1]));
274 return NT_STATUS_NO_SUCH_USER;