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