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