r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[sharpe/samba-autobuild/.git] / source3 / smbd / sec_ctx.c
1 /* 
2    Unix SMB/CIFS implementation.
3    uid/user handling
4    Copyright (C) Tim Potter 2000
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22 extern struct current_user current_user;
23
24 struct sec_ctx {
25         UNIX_USER_TOKEN ut;
26         NT_USER_TOKEN *token;
27 };
28
29 /* A stack of security contexts.  We include the current context as being
30    the first one, so there is room for another MAX_SEC_CTX_DEPTH more. */
31
32 static struct sec_ctx sec_ctx_stack[MAX_SEC_CTX_DEPTH + 1];
33 static int sec_ctx_stack_ndx;
34
35 /****************************************************************************
36  Become the specified uid.
37 ****************************************************************************/
38
39 static BOOL become_uid(uid_t uid)
40 {
41         /* Check for dodgy uid values */
42
43         if (uid == (uid_t)-1 || 
44             ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
45                 static int done;
46  
47                 if (!done) {
48                         DEBUG(1,("WARNING: using uid %d is a security risk\n",
49                                  (int)uid));
50                         done = 1;
51                 }
52         }
53
54         /* Set effective user id */
55
56         set_effective_uid(uid);
57
58         DO_PROFILE_INC(uid_changes);
59         return True;
60 }
61
62 /****************************************************************************
63  Become the specified gid.
64 ****************************************************************************/
65
66 static BOOL become_gid(gid_t gid)
67 {
68         /* Check for dodgy gid values */
69
70         if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) && 
71                                  (gid == (gid_t)65535))) {
72                 static int done;
73                 
74                 if (!done) {
75                         DEBUG(1,("WARNING: using gid %d is a security risk\n",
76                                  (int)gid));  
77                         done = 1;
78                 }
79         }
80   
81         /* Set effective group id */
82
83         set_effective_gid(gid);
84         return True;
85 }
86
87 /****************************************************************************
88  Become the specified uid and gid.
89 ****************************************************************************/
90
91 static BOOL become_id(uid_t uid, gid_t gid)
92 {
93         return become_gid(gid) && become_uid(uid);
94 }
95
96 /****************************************************************************
97  Drop back to root privileges in order to change to another user.
98 ****************************************************************************/
99
100 static void gain_root(void)
101 {
102         if (non_root_mode()) {
103                 return;
104         }
105
106         if (geteuid() != 0) {
107                 set_effective_uid(0);
108
109                 if (geteuid() != 0) {
110                         DEBUG(0,
111                               ("Warning: You appear to have a trapdoor "
112                                "uid system\n"));
113                 }
114         }
115
116         if (getegid() != 0) {
117                 set_effective_gid(0);
118
119                 if (getegid() != 0) {
120                         DEBUG(0,
121                               ("Warning: You appear to have a trapdoor "
122                                "gid system\n"));
123                 }
124         }
125 }
126
127 /****************************************************************************
128  Get the list of current groups.
129 ****************************************************************************/
130
131 static int get_current_groups(gid_t gid, int *p_ngroups, gid_t **p_groups)
132 {
133         int i;
134         gid_t grp;
135         int ngroups;
136         gid_t *groups = NULL;
137
138         (*p_ngroups) = 0;
139         (*p_groups) = NULL;
140
141         /* this looks a little strange, but is needed to cope with
142            systems that put the current egid in the group list
143            returned from getgroups() (tridge) */
144         save_re_gid();
145         set_effective_gid(gid);
146         setgid(gid);
147
148         ngroups = sys_getgroups(0,&grp);
149         if (ngroups <= 0) {
150                 goto fail;
151         }
152
153         if((groups = SMB_MALLOC_ARRAY(gid_t, ngroups+1)) == NULL) {
154                 DEBUG(0,("setup_groups malloc fail !\n"));
155                 goto fail;
156         }
157
158         if ((ngroups = sys_getgroups(ngroups,groups)) == -1) {
159                 goto fail;
160         }
161
162         restore_re_gid();
163
164         (*p_ngroups) = ngroups;
165         (*p_groups) = groups;
166
167         DEBUG( 3, ( "get_current_groups: user is in %u groups: ", ngroups));
168         for (i = 0; i < ngroups; i++ ) {
169                 DEBUG( 3, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
170         }
171         DEBUG( 3, ( "\n" ) );
172
173         return ngroups;
174
175 fail:
176         SAFE_FREE(groups);
177         restore_re_gid();
178         return -1;
179 }
180
181 /****************************************************************************
182  Create a new security context on the stack.  It is the same as the old
183  one.  User changes are done using the set_sec_ctx() function.
184 ****************************************************************************/
185
186 BOOL push_sec_ctx(void)
187 {
188         struct sec_ctx *ctx_p;
189
190         /* Check we don't overflow our stack */
191
192         if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
193                 DEBUG(0, ("Security context stack overflow!\n"));
194                 smb_panic("Security context stack overflow!");
195         }
196
197         /* Store previous user context */
198
199         sec_ctx_stack_ndx++;
200
201         ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
202
203         ctx_p->ut.uid = geteuid();
204         ctx_p->ut.gid = getegid();
205
206         DEBUG(3, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n", 
207                   (unsigned int)ctx_p->ut.uid, (unsigned int)ctx_p->ut.gid, sec_ctx_stack_ndx ));
208
209         ctx_p->token = dup_nt_token(NULL,
210                                     sec_ctx_stack[sec_ctx_stack_ndx-1].token);
211
212         ctx_p->ut.ngroups = sys_getgroups(0, NULL);
213
214         if (ctx_p->ut.ngroups != 0) {
215                 if (!(ctx_p->ut.groups = SMB_MALLOC_ARRAY(gid_t, ctx_p->ut.ngroups))) {
216                         DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
217                         TALLOC_FREE(ctx_p->token);
218                         return False;
219                 }
220
221                 sys_getgroups(ctx_p->ut.ngroups, ctx_p->ut.groups);
222         } else {
223                 ctx_p->ut.groups = NULL;
224         }
225
226         return True;
227 }
228
229 /****************************************************************************
230  Change UNIX security context. Calls panic if not successful so no return value.
231 ****************************************************************************/
232
233 #ifndef HAVE_DARWIN_INITGROUPS
234
235 /* Normal credential switch path. */
236
237 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
238 {
239         /* Start context switch */
240         gain_root();
241 #ifdef HAVE_SETGROUPS
242         sys_setgroups(gid, ngroups, groups);
243 #endif
244         become_id(uid, gid);
245         /* end context switch */
246 }
247
248 #else /* HAVE_DARWIN_INITGROUPS */
249
250 /* The Darwin groups implementation is a little unusual. The list of
251 * groups in the kernel credential is not exhaustive, but more like
252 * a cache. The full group list is held in userspace and checked
253 * dynamically.
254 *
255 * This is an optional mechanism, and setgroups(2) opts out
256 * of it. That is, if you call setgroups, then the list of groups you
257 * set are the only groups that are ever checked. This is not what we
258 * want. We want to opt in to the dynamic resolution mechanism, so we
259 * need to specify the uid of the user whose group list (cache) we are
260 * setting.
261 *
262 * The Darwin rules are:
263 *  1. Thou shalt setegid, initgroups and seteuid IN THAT ORDER
264 *  2. Thou shalt not pass more that NGROUPS_MAX to initgroups
265 *  3. Thou shalt leave the first entry in the groups list well alone
266 */
267
268 #include <sys/syscall.h>
269
270 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
271 {
272         int max = groups_max();
273
274         /* Start context switch */
275         gain_root();
276
277         become_gid(gid);
278
279
280         if (syscall(SYS_initgroups, (ngroups > max) ? max : ngroups,
281                         groups, uid) == 1) {
282                 DEBUG(0, ("WARNING: failed to set group list "
283                         "(%d groups) for UID %ld: %s\n",
284                         ngroups, uid, strerror(errno)));
285         }
286
287         become_uid(uid);
288         /* end context switch */
289 }
290
291 #endif /* HAVE_DARWIN_INITGROUPS */
292
293 /****************************************************************************
294  Set the current security context to a given user.
295 ****************************************************************************/
296
297 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, NT_USER_TOKEN *token)
298 {
299         struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
300         
301         /* Set the security context */
302
303         DEBUG(3, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n", 
304                 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
305
306         debug_nt_user_token(DBGC_CLASS, 5, token);
307         debug_unix_user_token(DBGC_CLASS, 5, uid, gid, ngroups, groups);
308
309         /* Change uid, gid and supplementary group list. */
310         set_unix_security_ctx(uid, gid, ngroups, groups);
311
312         ctx_p->ut.ngroups = ngroups;
313
314         SAFE_FREE(ctx_p->ut.groups);
315         if (token && (token == ctx_p->token)) {
316                 smb_panic("DUPLICATE_TOKEN");
317         }
318
319         TALLOC_FREE(ctx_p->token);
320         
321         if (ngroups) {
322                 ctx_p->ut.groups = (gid_t *)memdup(groups,
323                                                    sizeof(gid_t) * ngroups);
324                 if (!ctx_p->ut.groups) {
325                         smb_panic("memdup failed");
326                 }
327         } else {
328                 ctx_p->ut.groups = NULL;
329         }
330
331         if (token) {
332                 ctx_p->token = dup_nt_token(NULL, token);
333                 if (!ctx_p->token) {
334                         smb_panic("dup_nt_token failed");
335                 }
336         } else {
337                 ctx_p->token = NULL;
338         }
339
340         ctx_p->ut.uid = uid;
341         ctx_p->ut.gid = gid;
342
343         /* Update current_user stuff */
344
345         current_user.ut.uid = uid;
346         current_user.ut.gid = gid;
347         current_user.ut.ngroups = ngroups;
348         current_user.ut.groups = groups;
349         current_user.nt_user_token = ctx_p->token;
350 }
351
352 /****************************************************************************
353  Become root context.
354 ****************************************************************************/
355
356 void set_root_sec_ctx(void)
357 {
358         /* May need to worry about supplementary groups at some stage */
359
360         set_sec_ctx(0, 0, 0, NULL, NULL);
361 }
362
363 /****************************************************************************
364  Pop a security context from the stack.
365 ****************************************************************************/
366
367 BOOL pop_sec_ctx(void)
368 {
369         struct sec_ctx *ctx_p;
370         struct sec_ctx *prev_ctx_p;
371
372         /* Check for stack underflow */
373
374         if (sec_ctx_stack_ndx == 0) {
375                 DEBUG(0, ("Security context stack underflow!\n"));
376                 smb_panic("Security context stack underflow!");
377         }
378
379         ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
380
381         /* Clear previous user info */
382
383         ctx_p->ut.uid = (uid_t)-1;
384         ctx_p->ut.gid = (gid_t)-1;
385
386         SAFE_FREE(ctx_p->ut.groups);
387         ctx_p->ut.ngroups = 0;
388
389         TALLOC_FREE(ctx_p->token);
390
391         /* Pop back previous user */
392
393         sec_ctx_stack_ndx--;
394
395         prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
396
397         /* Change uid, gid and supplementary group list. */
398         set_unix_security_ctx(prev_ctx_p->ut.uid,
399                         prev_ctx_p->ut.gid,
400                         prev_ctx_p->ut.ngroups,
401                         prev_ctx_p->ut.groups);
402
403         /* Update current_user stuff */
404
405         current_user.ut.uid = prev_ctx_p->ut.uid;
406         current_user.ut.gid = prev_ctx_p->ut.gid;
407         current_user.ut.ngroups = prev_ctx_p->ut.ngroups;
408         current_user.ut.groups = prev_ctx_p->ut.groups;
409         current_user.nt_user_token = prev_ctx_p->token;
410
411         DEBUG(3, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n", 
412                 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
413
414         return True;
415 }
416
417 /* Initialise the security context system */
418
419 void init_sec_ctx(void)
420 {
421         int i;
422         struct sec_ctx *ctx_p;
423
424         /* Initialise security context stack */
425
426         memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
427
428         for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
429                 sec_ctx_stack[i].ut.uid = (uid_t)-1;
430                 sec_ctx_stack[i].ut.gid = (gid_t)-1;
431         }
432
433         /* Initialise first level of stack.  It is the current context */
434         ctx_p = &sec_ctx_stack[0];
435
436         ctx_p->ut.uid = geteuid();
437         ctx_p->ut.gid = getegid();
438
439         get_current_groups(ctx_p->ut.gid, &ctx_p->ut.ngroups, &ctx_p->ut.groups);
440
441         ctx_p->token = NULL; /* Maps to guest user. */
442
443         /* Initialise current_user global */
444
445         current_user.ut.uid = ctx_p->ut.uid;
446         current_user.ut.gid = ctx_p->ut.gid;
447         current_user.ut.ngroups = ctx_p->ut.ngroups;
448         current_user.ut.groups = ctx_p->ut.groups;
449
450         /* The conn and vuid are usually taken care of by other modules.
451            We initialise them here. */
452
453         current_user.conn = NULL;
454         current_user.vuid = UID_FIELD_INVALID;
455         current_user.nt_user_token = NULL;
456 }