r11137: Compile with only 2 warnings (I'm still working on that code) on a gcc4
[tprouty/samba.git] / source / lib / system_smbd.c
1 /* 
2    Unix SMB/CIFS implementation.
3    system call wrapper interface.
4    Copyright (C) Andrew Tridgell 2002
5    Copyright (C) Andrew Barteltt 2002
6
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.
11    
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.
16    
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.
20 */
21
22 /* 
23    This file may assume linkage with smbd - for things like become_root()
24    etc. 
25 */
26
27 #include "includes.h"
28
29 #ifndef HAVE_GETGROUPLIST
30
31 static int int_compare( int *a, int *b )
32 {
33         if ( *a == *b )
34                 return 0;
35         else if ( *a < *b )
36                 return -1;
37         else
38                 return 1;
39 }
40
41 void remove_duplicate_gids( int *num_groups, gid_t *groups )
42 {
43         int i;
44         int count = *num_groups;
45
46         if ( *num_groups <= 0 || !groups )
47                 return;
48
49         DEBUG(8,("remove_duplicate_gids: Enter %d gids\n", *num_groups));
50
51         qsort( groups, *num_groups, sizeof(gid_t), QSORT_CAST int_compare );
52
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) );
56
57                         /* decrement the total number of groups and do not increment
58                            the loop counter */
59                         count--;
60                         continue;
61                 }
62                 i++;
63         }
64
65         *num_groups = count;
66
67         DEBUG(8,("remove_duplicate_gids: Exit %d gids\n", *num_groups));
68
69         return;
70 }
71
72 /*
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.
78
79   NOTE!! this function only works if it is called as root!
80   */
81
82 static int getgrouplist_internals(const char *user, gid_t gid, gid_t *groups, int *grpcnt)
83 {
84         gid_t *gids_saved;
85         int ret, ngrp_saved, num_gids;
86
87         if (non_root_mode()) {
88                 *grpcnt = 0;
89                 return 0;
90         }
91
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;
97         }
98
99         gids_saved = SMB_MALLOC_ARRAY(gid_t, ngrp_saved+1);
100         if (!gids_saved) {
101                 errno = ENOMEM;
102                 return -1;
103         }
104
105         ngrp_saved = getgroups(ngrp_saved, gids_saved);
106         if (ngrp_saved == -1) {
107                 SAFE_FREE(gids_saved);
108                 /* very strange! */
109                 return -1;
110         }
111
112         if (initgroups(user, gid) != 0) {
113                 DEBUG(0, ("getgrouplist_internals: initgroups() failed!\n"));
114                 SAFE_FREE(gids_saved);
115                 return -1;
116         }
117
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);
123
124         num_gids = getgroups(0, NULL);
125         if (num_gids == -1) {
126                 SAFE_FREE(gids_saved);
127                 /* very strange! */
128                 return -1;
129         }
130
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;
140                 }
141                 groups[0] = gid;
142                 *grpcnt = ret + 1;
143
144                 /* remove any duplicates gids in the list */
145                 remove_duplicate_gids( grpcnt, groups );
146         }
147
148         restore_re_gid();
149
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;
156         }
157
158         free(gids_saved);
159         return ret;
160 }
161 #endif
162
163 static int sys_getgrouplist(const char *user, gid_t gid, gid_t *groups, int *grpcnt)
164 {
165         int retval;
166
167         DEBUG(10,("sys_getgrouplist: user [%s]\n", user));
168         
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));
177         }
178
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
186
187         /* allow winbindd lookups */
188         winbind_on();
189         
190         return retval;
191 }
192
193 BOOL getgroups_user(const char *user, gid_t primary_gid,
194                     gid_t **ret_groups, size_t *p_ngroups)
195 {
196         size_t ngrp;
197         int max_grp;
198         gid_t *temp_groups;
199         gid_t *groups;
200         int i;
201
202         max_grp = groups_max();
203         temp_groups = SMB_MALLOC_ARRAY(gid_t, max_grp);
204         if (! temp_groups) {
205                 return False;
206         }
207
208         if (sys_getgrouplist(user, primary_gid, temp_groups, &max_grp) == -1) {
209                 gid_t *groups_tmp;
210
211                 groups_tmp = SMB_REALLOC_ARRAY(temp_groups, gid_t, max_grp);
212                 
213                 if (!groups_tmp) {
214                         SAFE_FREE(temp_groups);
215                         return False;
216                 }
217                 temp_groups = groups_tmp;
218                 
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;
225                 }
226         }
227         
228         ngrp = 0;
229         groups = NULL;
230
231         /* Add in primary group first */
232         add_gid_to_array_unique(NULL, primary_gid, &groups, &ngrp);
233
234         for (i=0; i<max_grp; i++)
235                 add_gid_to_array_unique(NULL, temp_groups[i], &groups, &ngrp);
236
237         *p_ngroups = ngrp;
238         *ret_groups = groups;
239         SAFE_FREE(temp_groups);
240         return True;
241 }
242
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)
249 {
250         size_t i;
251
252         if (!getgroups_user(username, primary_gid, pp_gids, p_num_groups)) {
253                 return NT_STATUS_NO_SUCH_USER;
254         }
255
256         if (*p_num_groups == 0) {
257                 smb_panic("primary group missing");
258         }
259
260         *pp_sids = SMB_MALLOC_ARRAY(DOM_SID, *p_num_groups);
261
262         if (*pp_sids == NULL) {
263                 SAFE_FREE(pp_gids);
264                 return NT_STATUS_NO_MEMORY;
265         }
266
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;
275                 }
276         }
277
278         return NT_STATUS_OK;
279 }