lib-util: rename memdup to smb_memdup and fix all callers
[samba.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 #include "system/passwd.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "libcli/security/security_token.h"
25 #include "auth.h"
26 #include "smbprofile.h"
27 #include "../lib/util/setid.h"
28
29 extern struct current_user current_user;
30
31 /****************************************************************************
32  Are two UNIX tokens equal ?
33 ****************************************************************************/
34
35 bool unix_token_equal(const struct security_unix_token *t1, const struct security_unix_token *t2)
36 {
37         if (t1->uid != t2->uid || t1->gid != t2->gid ||
38                         t1->ngroups != t2->ngroups) {
39                 return false;
40         }
41         if (memcmp(t1->groups, t2->groups,
42                         t1->ngroups*sizeof(gid_t)) != 0) {
43                 return false;
44         }
45         return true;
46 }
47
48 /****************************************************************************
49  Become the specified uid.
50 ****************************************************************************/
51
52 static bool become_uid(uid_t uid)
53 {
54         /* Check for dodgy uid values */
55
56         if (uid == (uid_t)-1 || 
57             ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
58                 if (!become_uid_done) {
59                         DEBUG(1,("WARNING: using uid %d is a security risk\n",
60                                  (int)uid));
61                         become_uid_done = true;
62                 }
63         }
64
65         /* Set effective user id */
66
67         set_effective_uid(uid);
68
69         DO_PROFILE_INC(uid_changes);
70         return True;
71 }
72
73 /****************************************************************************
74  Become the specified gid.
75 ****************************************************************************/
76
77 static bool become_gid(gid_t gid)
78 {
79         /* Check for dodgy gid values */
80
81         if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) && 
82                                  (gid == (gid_t)65535))) {
83                 if (!become_gid_done) {
84                         DEBUG(1,("WARNING: using gid %d is a security risk\n",
85                                  (int)gid));  
86                         become_gid_done = true;
87                 }
88         }
89
90         /* Set effective group id */
91
92         set_effective_gid(gid);
93         return True;
94 }
95
96 /****************************************************************************
97  Become the specified uid and gid.
98 ****************************************************************************/
99
100 static bool become_id(uid_t uid, gid_t gid)
101 {
102         return become_gid(gid) && become_uid(uid);
103 }
104
105 /****************************************************************************
106  Drop back to root privileges in order to change to another user.
107 ****************************************************************************/
108
109 static void gain_root(void)
110 {
111         if (non_root_mode()) {
112                 return;
113         }
114
115         if (geteuid() != 0) {
116                 set_effective_uid(0);
117
118                 if (geteuid() != 0) {
119                         DEBUG(0,
120                               ("Warning: You appear to have a trapdoor "
121                                "uid system\n"));
122                 }
123         }
124
125         if (getegid() != 0) {
126                 set_effective_gid(0);
127
128                 if (getegid() != 0) {
129                         DEBUG(0,
130                               ("Warning: You appear to have a trapdoor "
131                                "gid system\n"));
132                 }
133         }
134 }
135
136 /****************************************************************************
137  Get the list of current groups.
138 ****************************************************************************/
139
140 static int get_current_groups(gid_t gid, uint32_t *p_ngroups, gid_t **p_groups)
141 {
142         int i;
143         gid_t grp;
144         int ngroups;
145         gid_t *groups = NULL;
146
147         (*p_ngroups) = 0;
148         (*p_groups) = NULL;
149
150         /* this looks a little strange, but is needed to cope with
151            systems that put the current egid in the group list
152            returned from getgroups() (tridge) */
153         save_re_gid();
154         set_effective_gid(gid);
155         samba_setgid(gid);
156
157         ngroups = sys_getgroups(0,&grp);
158         if (ngroups <= 0) {
159                 goto fail;
160         }
161
162         if((groups = SMB_MALLOC_ARRAY(gid_t, ngroups+1)) == NULL) {
163                 DEBUG(0,("setup_groups malloc fail !\n"));
164                 goto fail;
165         }
166
167         if ((ngroups = sys_getgroups(ngroups,groups)) == -1) {
168                 goto fail;
169         }
170
171         restore_re_gid();
172
173         (*p_ngroups) = ngroups;
174         (*p_groups) = groups;
175
176         DEBUG( 4, ( "get_current_groups: user is in %u groups: ", ngroups));
177         for (i = 0; i < ngroups; i++ ) {
178                 DEBUG( 4, ( "%s%d", (i ? ", " : ""), (int)groups[i] ) );
179         }
180         DEBUG( 4, ( "\n" ) );
181
182         return ngroups;
183
184 fail:
185         SAFE_FREE(groups);
186         restore_re_gid();
187         return -1;
188 }
189
190 /****************************************************************************
191  Create a new security context on the stack.  It is the same as the old
192  one.  User changes are done using the set_sec_ctx() function.
193 ****************************************************************************/
194
195 bool push_sec_ctx(void)
196 {
197         struct sec_ctx *ctx_p;
198
199         /* Check we don't overflow our stack */
200
201         if (sec_ctx_stack_ndx == MAX_SEC_CTX_DEPTH) {
202                 DEBUG(0, ("Security context stack overflow!\n"));
203                 smb_panic("Security context stack overflow!");
204         }
205
206         /* Store previous user context */
207
208         sec_ctx_stack_ndx++;
209
210         ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
211
212         ctx_p->ut.uid = geteuid();
213         ctx_p->ut.gid = getegid();
214
215         DEBUG(4, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n", 
216                   (unsigned int)ctx_p->ut.uid, (unsigned int)ctx_p->ut.gid, sec_ctx_stack_ndx ));
217
218         ctx_p->token = dup_nt_token(NULL,
219                                     sec_ctx_stack[sec_ctx_stack_ndx-1].token);
220
221         ctx_p->ut.ngroups = sys_getgroups(0, NULL);
222
223         if (ctx_p->ut.ngroups != 0) {
224                 if (!(ctx_p->ut.groups = SMB_MALLOC_ARRAY(gid_t, ctx_p->ut.ngroups))) {
225                         DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
226                         TALLOC_FREE(ctx_p->token);
227                         return False;
228                 }
229
230                 sys_getgroups(ctx_p->ut.ngroups, ctx_p->ut.groups);
231         } else {
232                 ctx_p->ut.groups = NULL;
233         }
234
235         return True;
236 }
237
238 /****************************************************************************
239  Change UNIX security context. Calls panic if not successful so no return value.
240 ****************************************************************************/
241
242 #ifndef HAVE_DARWIN_INITGROUPS
243
244 /* Normal credential switch path. */
245
246 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
247 {
248         /* Start context switch */
249         gain_root();
250 #ifdef HAVE_SETGROUPS
251         if (sys_setgroups(gid, ngroups, groups) != 0 && !non_root_mode()) {
252                 smb_panic("sys_setgroups failed");
253         }
254 #endif
255         become_id(uid, gid);
256         /* end context switch */
257 }
258
259 #else /* HAVE_DARWIN_INITGROUPS */
260
261 /* The Darwin groups implementation is a little unusual. The list of
262 * groups in the kernel credential is not exhaustive, but more like
263 * a cache. The full group list is held in userspace and checked
264 * dynamically.
265 *
266 * This is an optional mechanism, and setgroups(2) opts out
267 * of it. That is, if you call setgroups, then the list of groups you
268 * set are the only groups that are ever checked. This is not what we
269 * want. We want to opt in to the dynamic resolution mechanism, so we
270 * need to specify the uid of the user whose group list (cache) we are
271 * setting.
272 *
273 * The Darwin rules are:
274 *  1. Thou shalt setegid, initgroups and seteuid IN THAT ORDER
275 *  2. Thou shalt not pass more that NGROUPS_MAX to initgroups
276 *  3. Thou shalt leave the first entry in the groups list well alone
277 */
278
279 #include <sys/syscall.h>
280
281 static void set_unix_security_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
282 {
283         int max = groups_max();
284
285         /* Start context switch */
286         gain_root();
287
288         become_gid(gid);
289
290
291         if (syscall(SYS_initgroups, (ngroups > max) ? max : ngroups,
292                         groups, uid) == -1 && !non_root_mode()) {
293                 DEBUG(0, ("WARNING: failed to set group list "
294                         "(%d groups) for UID %d: %s\n",
295                         ngroups, uid, strerror(errno)));
296                 smb_panic("sys_setgroups failed");
297         }
298
299         become_uid(uid);
300         /* end context switch */
301 }
302
303 #endif /* HAVE_DARWIN_INITGROUPS */
304
305 /****************************************************************************
306  Set the current security context to a given user.
307 ****************************************************************************/
308
309 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups, const struct security_token *token)
310 {
311         struct sec_ctx *ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
312
313         /* Set the security context */
314
315         DEBUG(4, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n", 
316                 (unsigned int)uid, (unsigned int)gid, sec_ctx_stack_ndx));
317
318         security_token_debug(DBGC_CLASS, 5, token);
319         debug_unix_user_token(DBGC_CLASS, 5, uid, gid, ngroups, groups);
320
321         /* Change uid, gid and supplementary group list. */
322         set_unix_security_ctx(uid, gid, ngroups, groups);
323
324         ctx_p->ut.ngroups = ngroups;
325
326         SAFE_FREE(ctx_p->ut.groups);
327         if (token && (token == ctx_p->token)) {
328                 smb_panic("DUPLICATE_TOKEN");
329         }
330
331         TALLOC_FREE(ctx_p->token);
332
333         if (ngroups) {
334                 ctx_p->ut.groups = (gid_t *)smb_memdup(groups,
335                                                        sizeof(gid_t) * ngroups);
336                 if (!ctx_p->ut.groups) {
337                         smb_panic("memdup failed");
338                 }
339         } else {
340                 ctx_p->ut.groups = NULL;
341         }
342
343         if (token) {
344                 ctx_p->token = dup_nt_token(NULL, token);
345                 if (!ctx_p->token) {
346                         smb_panic("dup_nt_token failed");
347                 }
348         } else {
349                 ctx_p->token = NULL;
350         }
351
352         ctx_p->ut.uid = uid;
353         ctx_p->ut.gid = gid;
354
355         /* Update current_user stuff */
356
357         current_user.ut.uid = uid;
358         current_user.ut.gid = gid;
359         current_user.ut.ngroups = ngroups;
360         current_user.ut.groups = groups;
361         current_user.nt_user_token = ctx_p->token;
362 }
363
364 /****************************************************************************
365  Become root context.
366 ****************************************************************************/
367
368 void set_root_sec_ctx(void)
369 {
370         /* May need to worry about supplementary groups at some stage */
371
372         set_sec_ctx(0, 0, 0, NULL, NULL);
373 }
374
375 /****************************************************************************
376  Pop a security context from the stack.
377 ****************************************************************************/
378
379 bool pop_sec_ctx(void)
380 {
381         struct sec_ctx *ctx_p;
382         struct sec_ctx *prev_ctx_p;
383
384         /* Check for stack underflow */
385
386         if (sec_ctx_stack_ndx == 0) {
387                 DEBUG(0, ("Security context stack underflow!\n"));
388                 smb_panic("Security context stack underflow!");
389         }
390
391         ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
392
393         /* Clear previous user info */
394
395         ctx_p->ut.uid = (uid_t)-1;
396         ctx_p->ut.gid = (gid_t)-1;
397
398         SAFE_FREE(ctx_p->ut.groups);
399         ctx_p->ut.ngroups = 0;
400
401         TALLOC_FREE(ctx_p->token);
402
403         /* Pop back previous user */
404
405         sec_ctx_stack_ndx--;
406
407         prev_ctx_p = &sec_ctx_stack[sec_ctx_stack_ndx];
408
409         /* Change uid, gid and supplementary group list. */
410         set_unix_security_ctx(prev_ctx_p->ut.uid,
411                         prev_ctx_p->ut.gid,
412                         prev_ctx_p->ut.ngroups,
413                         prev_ctx_p->ut.groups);
414
415         /* Update current_user stuff */
416
417         current_user.ut.uid = prev_ctx_p->ut.uid;
418         current_user.ut.gid = prev_ctx_p->ut.gid;
419         current_user.ut.ngroups = prev_ctx_p->ut.ngroups;
420         current_user.ut.groups = prev_ctx_p->ut.groups;
421         current_user.nt_user_token = prev_ctx_p->token;
422
423         DEBUG(4, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n", 
424                 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx));
425
426         return True;
427 }
428
429 /* Initialise the security context system */
430
431 void init_sec_ctx(void)
432 {
433         int i;
434         struct sec_ctx *ctx_p;
435
436         /* Initialise security context stack */
437
438         memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
439
440         for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
441                 sec_ctx_stack[i].ut.uid = (uid_t)-1;
442                 sec_ctx_stack[i].ut.gid = (gid_t)-1;
443         }
444
445         /* Initialise first level of stack.  It is the current context */
446         ctx_p = &sec_ctx_stack[0];
447
448         ctx_p->ut.uid = geteuid();
449         ctx_p->ut.gid = getegid();
450
451         get_current_groups(ctx_p->ut.gid, &ctx_p->ut.ngroups, &ctx_p->ut.groups);
452
453         ctx_p->token = NULL; /* Maps to guest user. */
454
455         /* Initialise current_user global */
456
457         current_user.ut.uid = ctx_p->ut.uid;
458         current_user.ut.gid = ctx_p->ut.gid;
459         current_user.ut.ngroups = ctx_p->ut.ngroups;
460         current_user.ut.groups = ctx_p->ut.groups;
461
462         /* The conn and vuid are usually taken care of by other modules.
463            We initialise them here. */
464
465         current_user.conn = NULL;
466         current_user.vuid = UID_FIELD_INVALID;
467         current_user.nt_user_token = NULL;
468 }
469
470 /*************************************************************
471  Called when we're inside a become_root() temporary escalation
472  of privileges and the nt_user_token is NULL. Return the last
473  active token on the context stack. We know there is at least
474  one valid non-NULL token on the stack so panic if we underflow.
475 *************************************************************/
476
477 const struct security_token *sec_ctx_active_token(void)
478 {
479         int stack_index = sec_ctx_stack_ndx;
480         struct sec_ctx *ctx_p = &sec_ctx_stack[stack_index];
481
482         while (ctx_p->token == NULL) {
483                 stack_index--;
484                 if (stack_index < 0) {
485                         DEBUG(0, ("Security context active token "
486                                   "stack underflow!\n"));
487                         smb_panic("Security context active token "
488                                   "stack underflow!");
489                 }
490                 ctx_p = &sec_ctx_stack[stack_index];
491         }
492         return ctx_p->token;
493 }