r11137: Compile with only 2 warnings (I'm still working on that code) on a gcc4
[Samba.git] / source / lib / system_smbd.c
blob1afd44b70916daf5a70263b1cd56050e5fd509f4
1 /*
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.
22 /*
23 This file may assume linkage with smbd - for things like become_root()
24 etc.
27 #include "includes.h"
29 #ifndef HAVE_GETGROUPLIST
31 static int int_compare( int *a, int *b )
33 if ( *a == *b )
34 return 0;
35 else if ( *a < *b )
36 return -1;
37 else
38 return 1;
41 void remove_duplicate_gids( int *num_groups, gid_t *groups )
43 int i;
44 int count = *num_groups;
46 if ( *num_groups <= 0 || !groups )
47 return;
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
58 the loop counter */
59 count--;
60 continue;
62 i++;
65 *num_groups = count;
67 DEBUG(8,("remove_duplicate_gids: Exit %d gids\n", *num_groups));
69 return;
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)
84 gid_t *gids_saved;
85 int ret, ngrp_saved, num_gids;
87 if (non_root_mode()) {
88 *grpcnt = 0;
89 return 0;
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 */
96 return -1;
99 gids_saved = SMB_MALLOC_ARRAY(gid_t, ngrp_saved+1);
100 if (!gids_saved) {
101 errno = ENOMEM;
102 return -1;
105 ngrp_saved = getgroups(ngrp_saved, gids_saved);
106 if (ngrp_saved == -1) {
107 SAFE_FREE(gids_saved);
108 /* very strange! */
109 return -1;
112 if (initgroups(user, gid) != 0) {
113 DEBUG(0, ("getgrouplist_internals: initgroups() failed!\n"));
114 SAFE_FREE(gids_saved);
115 return -1;
118 /* this must be done to cope with systems that put the current egid in the
119 return from getgroups() */
120 save_re_gid();
121 set_effective_gid(gid);
122 setgid(gid);
124 num_gids = getgroups(0, NULL);
125 if (num_gids == -1) {
126 SAFE_FREE(gids_saved);
127 /* very strange! */
128 return -1;
131 if (num_gids + 1 > *grpcnt) {
132 *grpcnt = num_gids + 1;
133 ret = -1;
134 } else {
135 ret = getgroups(*grpcnt - 1, &groups[1]);
136 if (ret < 0) {
137 SAFE_FREE(gids_saved);
138 /* very strange! */
139 return -1;
141 groups[0] = gid;
142 *grpcnt = ret + 1;
144 /* remove any duplicates gids in the list */
145 remove_duplicate_gids( grpcnt, groups );
148 restore_re_gid();
150 if (sys_setgroups(ngrp_saved, gids_saved) != 0) {
151 /* yikes! */
152 DEBUG(0,("ERROR: getgrouplist: failed to reset group list!\n"));
153 smb_panic("getgrouplist: failed to reset group list!\n");
154 free(gids_saved);
155 return -1;
158 free(gids_saved);
159 return ret;
161 #endif
163 static int sys_getgrouplist(const char *user, gid_t gid, gid_t *groups, int *grpcnt)
165 int retval;
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",
173 WINBINDD_DONT_ENV));
174 else
175 DEBUG(10,("sys_getgrouplist(): disabled winbindd for group lookup [user == %s]\n",
176 user));
179 #ifdef HAVE_GETGROUPLIST
180 retval = getgrouplist(user, gid, groups, grpcnt);
181 #else
182 become_root();
183 retval = getgrouplist_internals(user, gid, groups, grpcnt);
184 unbecome_root();
185 #endif
187 /* allow winbindd lookups */
188 winbind_on();
190 return retval;
193 BOOL getgroups_user(const char *user, gid_t primary_gid,
194 gid_t **ret_groups, size_t *p_ngroups)
196 size_t ngrp;
197 int max_grp;
198 gid_t *temp_groups;
199 gid_t *groups;
200 int i;
202 max_grp = groups_max();
203 temp_groups = SMB_MALLOC_ARRAY(gid_t, max_grp);
204 if (! temp_groups) {
205 return False;
208 if (sys_getgrouplist(user, primary_gid, temp_groups, &max_grp) == -1) {
209 gid_t *groups_tmp;
211 groups_tmp = SMB_REALLOC_ARRAY(temp_groups, gid_t, max_grp);
213 if (!groups_tmp) {
214 SAFE_FREE(temp_groups);
215 return False;
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 "
222 "group list\n"));
223 SAFE_FREE(temp_groups);
224 return False;
228 ngrp = 0;
229 groups = NULL;
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);
237 *p_ngroups = ngrp;
238 *ret_groups = groups;
239 SAFE_FREE(temp_groups);
240 return True;
243 NTSTATUS pdb_default_enum_group_memberships(struct pdb_methods *methods,
244 const char *username,
245 gid_t primary_gid,
246 DOM_SID **pp_sids,
247 gid_t **pp_gids,
248 size_t *p_num_groups)
250 size_t i;
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) {
263 SAFE_FREE(pp_gids);
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]));
272 SAFE_FREE(*pp_sids);
273 SAFE_FREE(*pp_gids);
274 return NT_STATUS_NO_SUCH_USER;
278 return NT_STATUS_OK;