2 Unix SMB/CIFS implementation.
4 Copyright (C) Tim Potter 2000
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 2 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 extern struct current_user current_user
;
33 /* A stack of security contexts. We include the current context as being
34 the first one, so there is room for another MAX_SEC_CTX_DEPTH more. */
36 static struct sec_ctx sec_ctx_stack
[MAX_SEC_CTX_DEPTH
+ 1];
37 static int sec_ctx_stack_ndx
;
39 /****************************************************************************
40 Become the specified uid.
41 ****************************************************************************/
43 static BOOL
become_uid(uid_t uid
)
45 /* Check for dodgy uid values */
47 if (uid
== (uid_t
)-1 ||
48 ((sizeof(uid_t
) == 2) && (uid
== (uid_t
)65535))) {
52 DEBUG(1,("WARNING: using uid %d is a security risk\n",
58 /* Set effective user id */
60 set_effective_uid(uid
);
62 DO_PROFILE_INC(uid_changes
);
66 /****************************************************************************
67 Become the specified gid.
68 ****************************************************************************/
70 static BOOL
become_gid(gid_t gid
)
72 /* Check for dodgy gid values */
74 if (gid
== (gid_t
)-1 || ((sizeof(gid_t
) == 2) &&
75 (gid
== (gid_t
)65535))) {
79 DEBUG(1,("WARNING: using gid %d is a security risk\n",
85 /* Set effective group id */
87 set_effective_gid(gid
);
91 /****************************************************************************
92 Become the specified uid and gid.
93 ****************************************************************************/
95 static BOOL
become_id(uid_t uid
, gid_t gid
)
97 return become_gid(gid
) && become_uid(uid
);
100 /****************************************************************************
101 Drop back to root privileges in order to change to another user.
102 ****************************************************************************/
104 static void gain_root(void)
106 if (non_root_mode()) {
110 if (geteuid() != 0) {
111 set_effective_uid(0);
113 if (geteuid() != 0) {
115 ("Warning: You appear to have a trapdoor "
120 if (getegid() != 0) {
121 set_effective_gid(0);
123 if (getegid() != 0) {
125 ("Warning: You appear to have a trapdoor "
131 /****************************************************************************
132 Get the list of current groups.
133 ****************************************************************************/
135 int get_current_groups(gid_t gid
, int *p_ngroups
, gid_t
**p_groups
)
140 gid_t
*groups
= NULL
;
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) */
149 set_effective_gid(gid
);
152 ngroups
= sys_getgroups(0,&grp
);
157 if((groups
= (gid_t
*)malloc(sizeof(gid_t
)*(ngroups
+1))) == NULL
) {
158 DEBUG(0,("setup_groups malloc fail !\n"));
162 if ((ngroups
= sys_getgroups(ngroups
,groups
)) == -1) {
168 (*p_ngroups
) = ngroups
;
169 (*p_groups
) = groups
;
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
] ) );
175 DEBUG( 3, ( "\n" ) );
185 /****************************************************************************
186 Initialize the groups a user belongs to.
187 ****************************************************************************/
189 BOOL
initialise_groups(char *user
, uid_t uid
, gid_t gid
)
191 struct sec_ctx
*prev_ctx_p
;
194 if (non_root_mode()) {
200 /* Call initgroups() to get user groups */
202 if (initgroups(user
,gid
) == -1) {
203 DEBUG(0,("Unable to initgroups. Error was %s\n", strerror(errno
) ));
205 if (gid
< 0 || gid
> 32767 || uid
< 0 || uid
> 32767) {
206 DEBUG(0,("This is probably a problem with the account %s\n", user
));
213 /* Store groups in previous user's security context. This will
214 always work as the become_root() call increments the stack
217 prev_ctx_p
= &sec_ctx_stack
[sec_ctx_stack_ndx
- 1];
219 SAFE_FREE(prev_ctx_p
->groups
);
220 prev_ctx_p
->ngroups
= 0;
222 get_current_groups(gid
, &prev_ctx_p
->ngroups
, &prev_ctx_p
->groups
);
230 /****************************************************************************
231 Create a new security context on the stack. It is the same as the old
232 one. User changes are done using the set_sec_ctx() function.
233 ****************************************************************************/
235 BOOL
push_sec_ctx(void)
237 struct sec_ctx
*ctx_p
;
239 /* Check we don't overflow our stack */
241 if (sec_ctx_stack_ndx
== MAX_SEC_CTX_DEPTH
) {
242 DEBUG(0, ("Security context stack overflow!\n"));
243 smb_panic("Security context stack overflow!\n");
246 /* Store previous user context */
250 ctx_p
= &sec_ctx_stack
[sec_ctx_stack_ndx
];
252 ctx_p
->uid
= geteuid();
253 ctx_p
->gid
= getegid();
255 DEBUG(3, ("push_sec_ctx(%u, %u) : sec_ctx_stack_ndx = %d\n",
256 (unsigned int)ctx_p
->uid
, (unsigned int)ctx_p
->gid
, sec_ctx_stack_ndx
));
258 ctx_p
->token
= dup_nt_token(sec_ctx_stack
[sec_ctx_stack_ndx
-1].token
);
260 ctx_p
->ngroups
= sys_getgroups(0, NULL
);
262 if (ctx_p
->ngroups
!= 0) {
263 if (!(ctx_p
->groups
= malloc(ctx_p
->ngroups
* sizeof(gid_t
)))) {
264 DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
265 delete_nt_token(&ctx_p
->token
);
269 sys_getgroups(ctx_p
->ngroups
, ctx_p
->groups
);
271 ctx_p
->groups
= NULL
;
277 /****************************************************************************
278 Set the current security context to a given user.
279 ****************************************************************************/
281 void set_sec_ctx(uid_t uid
, gid_t gid
, int ngroups
, gid_t
*groups
, NT_USER_TOKEN
*token
)
283 struct sec_ctx
*ctx_p
= &sec_ctx_stack
[sec_ctx_stack_ndx
];
285 /* Set the security context */
287 DEBUG(3, ("setting sec ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
288 (unsigned int)uid
, (unsigned int)gid
, sec_ctx_stack_ndx
));
290 debug_nt_user_token(DBGC_CLASS
, 5, token
);
291 debug_unix_user_token(DBGC_CLASS
, 5, uid
, gid
, ngroups
, groups
);
295 #ifdef HAVE_SETGROUPS
296 sys_setgroups(ngroups
, groups
);
299 ctx_p
->ngroups
= ngroups
;
301 SAFE_FREE(ctx_p
->groups
);
302 if (token
&& (token
== ctx_p
->token
))
303 smb_panic("DUPLICATE_TOKEN");
305 delete_nt_token(&ctx_p
->token
);
307 ctx_p
->groups
= memdup(groups
, sizeof(gid_t
) * ngroups
);
308 ctx_p
->token
= dup_nt_token(token
);
315 /* Update current_user stuff */
317 current_user
.uid
= uid
;
318 current_user
.gid
= gid
;
319 current_user
.ngroups
= ngroups
;
320 current_user
.groups
= groups
;
321 current_user
.nt_user_token
= ctx_p
->token
;
324 /****************************************************************************
326 ****************************************************************************/
328 void set_root_sec_ctx(void)
330 /* May need to worry about supplementary groups at some stage */
332 set_sec_ctx(0, 0, 0, NULL
, NULL
);
335 /****************************************************************************
336 Pop a security context from the stack.
337 ****************************************************************************/
339 BOOL
pop_sec_ctx(void)
341 struct sec_ctx
*ctx_p
;
342 struct sec_ctx
*prev_ctx_p
;
344 /* Check for stack underflow */
346 if (sec_ctx_stack_ndx
== 0) {
347 DEBUG(0, ("Security context stack underflow!\n"));
348 smb_panic("Security context stack underflow!\n");
351 ctx_p
= &sec_ctx_stack
[sec_ctx_stack_ndx
];
353 /* Clear previous user info */
355 ctx_p
->uid
= (uid_t
)-1;
356 ctx_p
->gid
= (gid_t
)-1;
358 SAFE_FREE(ctx_p
->groups
);
361 delete_nt_token(&ctx_p
->token
);
363 /* Pop back previous user */
369 prev_ctx_p
= &sec_ctx_stack
[sec_ctx_stack_ndx
];
371 #ifdef HAVE_SETGROUPS
372 sys_setgroups(prev_ctx_p
->ngroups
, prev_ctx_p
->groups
);
375 become_id(prev_ctx_p
->uid
, prev_ctx_p
->gid
);
377 /* Update current_user stuff */
379 current_user
.uid
= prev_ctx_p
->uid
;
380 current_user
.gid
= prev_ctx_p
->gid
;
381 current_user
.ngroups
= prev_ctx_p
->ngroups
;
382 current_user
.groups
= prev_ctx_p
->groups
;
383 current_user
.nt_user_token
= prev_ctx_p
->token
;
385 DEBUG(3, ("pop_sec_ctx (%u, %u) - sec_ctx_stack_ndx = %d\n",
386 (unsigned int)geteuid(), (unsigned int)getegid(), sec_ctx_stack_ndx
));
391 /* Initialise the security context system */
393 void init_sec_ctx(void)
396 struct sec_ctx
*ctx_p
;
398 /* Initialise security context stack */
400 memset(sec_ctx_stack
, 0, sizeof(struct sec_ctx
) * MAX_SEC_CTX_DEPTH
);
402 for (i
= 0; i
< MAX_SEC_CTX_DEPTH
; i
++) {
403 sec_ctx_stack
[i
].uid
= (uid_t
)-1;
404 sec_ctx_stack
[i
].gid
= (gid_t
)-1;
407 /* Initialise first level of stack. It is the current context */
408 ctx_p
= &sec_ctx_stack
[0];
410 ctx_p
->uid
= geteuid();
411 ctx_p
->gid
= getegid();
413 get_current_groups(ctx_p
->gid
, &ctx_p
->ngroups
, &ctx_p
->groups
);
415 ctx_p
->token
= NULL
; /* Maps to guest user. */
417 /* Initialise current_user global */
419 current_user
.uid
= ctx_p
->uid
;
420 current_user
.gid
= ctx_p
->gid
;
421 current_user
.ngroups
= ctx_p
->ngroups
;
422 current_user
.groups
= ctx_p
->groups
;
424 /* The conn and vuid are usually taken care of by other modules.
425 We initialise them here. */
427 current_user
.conn
= NULL
;
428 current_user
.vuid
= UID_FIELD_INVALID
;
429 current_user
.nt_user_token
= NULL
;