2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
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 /* what user is current? */
24 extern struct current_user current_user
;
26 /****************************************************************************
27 Become the guest user without changing the security context stack.
28 ****************************************************************************/
30 BOOL
change_to_guest(void)
32 static struct passwd
*pass
=NULL
;
35 /* Don't need to free() this as its stored in a static */
36 pass
= getpwnam_alloc(lp_guestaccount());
42 /* MWW: From AIX FAQ patch to WU-ftpd: call initgroups before
44 initgroups(pass
->pw_name
, pass
->pw_gid
);
47 set_sec_ctx(pass
->pw_uid
, pass
->pw_gid
, 0, NULL
, NULL
);
49 current_user
.conn
= NULL
;
50 current_user
.vuid
= UID_FIELD_INVALID
;
55 /*******************************************************************
56 Check if a username is OK.
57 ********************************************************************/
59 static BOOL
check_user_ok(connection_struct
*conn
, user_struct
*vuser
,int snum
)
62 for (i
=0;i
<conn
->uid_cache
.entries
;i
++)
63 if (conn
->uid_cache
.list
[i
] == vuser
->uid
)
66 if (!user_ok(vuser
->user
.unix_name
,snum
))
69 i
= conn
->uid_cache
.entries
% UID_CACHE_SIZE
;
70 conn
->uid_cache
.list
[i
] = vuser
->uid
;
72 if (conn
->uid_cache
.entries
< UID_CACHE_SIZE
)
73 conn
->uid_cache
.entries
++;
78 /****************************************************************************
79 Become the user of a connection number without changing the security context
80 stack, but modify the currnet_user entries.
81 ****************************************************************************/
83 BOOL
change_to_user(connection_struct
*conn
, uint16 vuid
)
85 user_struct
*vuser
= get_valid_user_struct(vuid
);
90 BOOL must_free_token
= False
;
91 NT_USER_TOKEN
*token
= NULL
;
94 DEBUG(2,("change_to_user: Connection not open\n"));
99 * We need a separate check in security=share mode due to vuid
100 * always being UID_FIELD_INVALID. If we don't do this then
101 * in share mode security we are *always* changing uid's between
102 * SMB's - this hurts performance - Badly.
105 if((lp_security() == SEC_SHARE
) && (current_user
.conn
== conn
) &&
106 (current_user
.uid
== conn
->uid
)) {
107 DEBUG(4,("change_to_user: Skipping user change - already user\n"));
109 } else if ((current_user
.conn
== conn
) &&
110 (vuser
!= 0) && (current_user
.vuid
== vuid
) &&
111 (current_user
.uid
== vuser
->uid
)) {
112 DEBUG(4,("change_to_user: Skipping user change - already user\n"));
118 if((vuser
!= NULL
) && !check_user_ok(conn
, vuser
, snum
))
121 if (conn
->force_user
||
123 (lp_security() == SEC_SHARE
)) {
126 current_user
.groups
= conn
->groups
;
127 current_user
.ngroups
= conn
->ngroups
;
128 token
= conn
->nt_user_token
;
131 DEBUG(2,("change_to_user: Invalid vuid used %d\n",vuid
));
136 current_user
.ngroups
= vuser
->n_groups
;
137 current_user
.groups
= vuser
->groups
;
138 token
= vuser
->nt_user_token
;
142 * See if we should force group for this service.
143 * If so this overrides any group set in the force
147 if((group_c
= *lp_force_group(snum
))) {
148 BOOL is_guest
= False
;
153 * Only force group if the user is a member of
154 * the service group. Check the group memberships for
155 * this user (we already have this) to
156 * see if we should force the group.
160 for (i
= 0; i
< current_user
.ngroups
; i
++) {
161 if (current_user
.groups
[i
] == conn
->gid
) {
171 * We've changed the group list in the token - we must
175 if (vuser
&& vuser
->guest
)
178 token
= create_nt_token(uid
, gid
, current_user
.ngroups
, current_user
.groups
, is_guest
, NULL
);
179 must_free_token
= True
;
182 set_sec_ctx(uid
, gid
, current_user
.ngroups
, current_user
.groups
, token
);
185 * Free the new token (as set_sec_ctx copies it).
189 delete_nt_token(&token
);
191 current_user
.conn
= conn
;
192 current_user
.vuid
= vuid
;
194 DEBUG(5,("change_to_user uid=(%d,%d) gid=(%d,%d)\n",
195 (int)getuid(),(int)geteuid(),(int)getgid(),(int)getegid()));
200 /****************************************************************************
201 Go back to being root without changing the security context stack,
202 but modify the current_user entries.
203 ****************************************************************************/
205 BOOL
change_to_root_user(void)
209 DEBUG(5,("change_to_root_user: now uid=(%d,%d) gid=(%d,%d)\n",
210 (int)getuid(),(int)geteuid(),(int)getgid(),(int)getegid()));
212 current_user
.conn
= NULL
;
213 current_user
.vuid
= UID_FIELD_INVALID
;
218 /****************************************************************************
219 Become the user of an authenticated connected named pipe.
220 When this is called we are currently running as the connection
221 user. Doesn't modify current_user.
222 ****************************************************************************/
224 BOOL
become_authenticated_pipe_user(pipes_struct
*p
)
229 set_sec_ctx(p
->pipe_user
.uid
, p
->pipe_user
.gid
,
230 p
->pipe_user
.ngroups
, p
->pipe_user
.groups
, p
->pipe_user
.nt_user_token
);
235 /****************************************************************************
236 Unbecome the user of an authenticated connected named pipe.
237 When this is called we are running as the authenticated pipe
238 user and need to go back to being the connection user. Doesn't modify
240 ****************************************************************************/
242 BOOL
unbecome_authenticated_pipe_user(void)
244 return pop_sec_ctx();
247 /****************************************************************************
248 Utility functions used by become_xxx/unbecome_xxx.
249 ****************************************************************************/
252 connection_struct
*conn
;
256 /* A stack of current_user connection contexts. */
258 static struct conn_ctx conn_ctx_stack
[MAX_SEC_CTX_DEPTH
];
259 static int conn_ctx_stack_ndx
;
261 static void push_conn_ctx(void)
263 struct conn_ctx
*ctx_p
;
265 /* Check we don't overflow our stack */
267 if (conn_ctx_stack_ndx
== MAX_SEC_CTX_DEPTH
) {
268 DEBUG(0, ("Connection context stack overflow!\n"));
269 smb_panic("Connection context stack overflow!\n");
272 /* Store previous user context */
273 ctx_p
= &conn_ctx_stack
[conn_ctx_stack_ndx
];
275 ctx_p
->conn
= current_user
.conn
;
276 ctx_p
->vuid
= current_user
.vuid
;
278 DEBUG(3, ("push_conn_ctx(%u) : conn_ctx_stack_ndx = %d\n",
279 (unsigned int)ctx_p
->vuid
, conn_ctx_stack_ndx
));
281 conn_ctx_stack_ndx
++;
284 static void pop_conn_ctx(void)
286 struct conn_ctx
*ctx_p
;
288 /* Check for stack underflow. */
290 if (conn_ctx_stack_ndx
== 0) {
291 DEBUG(0, ("Connection context stack underflow!\n"));
292 smb_panic("Connection context stack underflow!\n");
295 conn_ctx_stack_ndx
--;
296 ctx_p
= &conn_ctx_stack
[conn_ctx_stack_ndx
];
298 current_user
.conn
= ctx_p
->conn
;
299 current_user
.vuid
= ctx_p
->vuid
;
302 ctx_p
->vuid
= UID_FIELD_INVALID
;
305 void init_conn_ctx(void)
309 /* Initialise connection context stack */
310 for (i
= 0; i
< MAX_SEC_CTX_DEPTH
; i
++) {
311 conn_ctx_stack
[i
].conn
= NULL
;
312 conn_ctx_stack
[i
].vuid
= UID_FIELD_INVALID
;
316 /****************************************************************************
317 Temporarily become a root user. Must match with unbecome_root(). Saves and
318 restores the connection context.
319 ****************************************************************************/
321 void become_root(void)
328 /* Unbecome the root user */
330 void unbecome_root(void)
336 /****************************************************************************
337 Push the current security context then force a change via change_to_user().
338 Saves and restores the connection context.
339 ****************************************************************************/
341 BOOL
become_user(connection_struct
*conn
, uint16 vuid
)
348 if (!change_to_user(conn
, vuid
)) {
357 BOOL
unbecome_user(void)
364 /*****************************************************************
365 Convert the suplimentary SIDs returned in a netlogon into UNIX
366 group gid_t's. Add to the total group array.
367 *****************************************************************/
369 void add_supplementary_nt_login_groups(int *n_groups
, gid_t
**pp_groups
, NT_USER_TOKEN
**pptok
)
372 int current_n_groups
= *n_groups
;
373 gid_t
*final_groups
= NULL
;
375 NT_USER_TOKEN
*ptok
= *pptok
;
376 NT_USER_TOKEN
*new_tok
= NULL
;
378 if (!ptok
|| (ptok
->num_sids
== 0))
381 new_tok
= dup_nt_token(ptok
);
383 DEBUG(0,("add_supplementary_nt_login_groups: Failed to malloc new token\n"));
386 /* Leave the allocated space but empty the number of SIDs. */
387 new_tok
->num_sids
= 0;
389 total_groups
= current_n_groups
+ ptok
->num_sids
;
391 final_groups
= (gid_t
*)malloc(total_groups
* sizeof(gid_t
));
393 DEBUG(0,("add_supplementary_nt_login_groups: Failed to malloc new groups.\n"));
394 delete_nt_token(&new_tok
);
398 memcpy(final_groups
, *pp_groups
, current_n_groups
* sizeof(gid_t
));
399 for (i
= 0; i
< ptok
->num_sids
; i
++) {
400 enum SID_NAME_USE sid_type
;
403 if (sid_to_gid(&ptok
->user_sids
[i
], &new_grp
, &sid_type
)) {
405 * Don't add the gid_t if it is already in the current group
406 * list. Some UNIXen don't like the same group more than once.
410 for (j
= 0; j
< current_n_groups
; j
++)
411 if (final_groups
[j
] == new_grp
)
414 if ( j
== current_n_groups
) {
415 /* Group not already present. */
416 final_groups
[current_n_groups
++] = new_grp
;
419 /* SID didn't map. Copy to the new token to be saved. */
420 sid_copy(&new_tok
->user_sids
[new_tok
->num_sids
++], &ptok
->user_sids
[i
]);
424 SAFE_FREE(*pp_groups
);
425 *pp_groups
= final_groups
;
426 *n_groups
= current_n_groups
;
428 /* Replace the old token with the truncated one. */
429 delete_nt_token(&ptok
);
433 /*****************************************************************
434 *THE CANONICAL* convert name to SID function.
435 Tries local lookup first - for local domains - then uses winbind.
436 *****************************************************************/
438 BOOL
lookup_name(const char *domain
, const char *name
, DOM_SID
*psid
, enum SID_NAME_USE
*name_type
)
440 extern pstring global_myname
;
441 extern fstring global_myworkgroup
;
443 BOOL local_lookup
= False
;
445 *name_type
= SID_NAME_UNKNOWN
;
447 /* If we are looking up a domain user, make sure it is
448 for the local machine only */
450 if (strequal(global_myname
, domain
)) {
452 } else if (lp_server_role() == ROLE_DOMAIN_PDC
||
453 lp_server_role() == ROLE_DOMAIN_PDC
) {
454 if (strequal(domain
, global_myworkgroup
)) {
460 if (local_lookup_name(name
, psid
, name_type
)) {
462 ("lookup_name: (local) [%s]\\[%s] -> SID %s (type %s: %u)\n",
463 domain
, name
, sid_to_string(sid
,psid
),
464 sid_type_lookup(*name_type
), (unsigned int)*name_type
));
469 if (winbind_lookup_name(domain
, name
, psid
, name_type
)) {
471 DEBUG(10,("lookup_name (winbindd): [%s]\\[%s] -> SID %s (type %u)\n",
472 domain
, name
, sid_to_string(sid
, psid
),
473 (unsigned int)*name_type
));
478 DEBUG(10, ("lookup_name: %s lookup for [%s]\\[%s] failed\n",
479 local_lookup
? "local" : "winbind", domain
, name
));
484 /*****************************************************************
485 *THE CANONICAL* convert SID to name function.
486 Tries local lookup first - for local sids, then tries winbind.
487 *****************************************************************/
489 BOOL
lookup_sid(DOM_SID
*sid
, fstring dom_name
, fstring name
, enum SID_NAME_USE
*name_type
)
494 *name_type
= SID_NAME_UNKNOWN
;
496 /* Check if this is our own sid. This should perhaps be done by
497 winbind? For the moment handle it here. */
499 if (sid
->num_auths
== 5) {
503 sid_copy(&tmp_sid
, sid
);
504 sid_split_rid(&tmp_sid
, &rid
);
506 if (sid_equal(get_global_sam_sid(), &tmp_sid
)) {
508 return map_domain_sid_to_name(&tmp_sid
, dom_name
) &&
509 local_lookup_sid(sid
, name
, name_type
);
513 if (!winbind_lookup_sid(sid
, dom_name
, name
, name_type
)) {
518 DEBUG(10,("lookup_sid: winbind lookup for SID %s failed - trying local.\n", sid_to_string(sid_str
, sid
) ));
520 sid_copy(&tmp_sid
, sid
);
521 sid_split_rid(&tmp_sid
, &rid
);
522 return map_domain_sid_to_name(&tmp_sid
, dom_name
) &&
523 lookup_known_rid(&tmp_sid
, rid
, name
, name_type
);
528 /*****************************************************************
529 *THE CANONICAL* convert uid_t to SID function.
530 Tries winbind first - then uses local lookup.
532 *****************************************************************/
534 DOM_SID
*uid_to_sid(DOM_SID
*psid
, uid_t uid
)
539 if (lp_winbind_uid(&low
, &high
) && uid
>= low
&& uid
<= high
) {
540 if (winbind_uid_to_sid(psid
, uid
)) {
542 DEBUG(10,("uid_to_sid: winbindd %u -> %s\n",
543 (unsigned int)uid
, sid_to_string(sid
, psid
)));
549 /* Make sure we report failure, (when psid == NULL) */
551 psid
= local_uid_to_sid(psid
, uid
);
554 DEBUG(10,("uid_to_sid: local %u -> %s\n", (unsigned int)uid
, sid_to_string(sid
, psid
)));
559 /*****************************************************************
560 *THE CANONICAL* convert gid_t to SID function.
561 Tries winbind first - then uses local lookup.
563 *****************************************************************/
565 DOM_SID
*gid_to_sid(DOM_SID
*psid
, gid_t gid
)
570 if (lp_winbind_gid(&low
, &high
) && gid
>= low
&& gid
<= high
) {
571 if (winbind_gid_to_sid(psid
, gid
)) {
573 DEBUG(10,("gid_to_sid: winbindd %u -> %s\n",
574 (unsigned int)gid
, sid_to_string(sid
, psid
)));
580 /* Make sure we report failure, (when psid == NULL) */
581 psid
= local_gid_to_sid(psid
, gid
);
583 DEBUG(10,("gid_to_sid: local %u -> %s\n", (unsigned int)gid
, sid_to_string(sid
, psid
)));
588 /*****************************************************************
589 *THE CANONICAL* convert SID to uid function.
590 Tries winbind first - then uses local lookup.
591 Returns True if this name is a user sid and the conversion
592 was done correctly, False if not. sidtype is set by this function.
593 *****************************************************************/
595 BOOL
sid_to_uid(const DOM_SID
*psid
, uid_t
*puid
, enum SID_NAME_USE
*sidtype
)
599 /* if we know its local then don't try winbindd */
600 if (sid_compare_domain(get_global_sam_sid(), psid
) == 0) {
603 result
= local_sid_to_uid(puid
, psid
, sidtype
);
608 /* (tridge) I commented out the slab of code below in order to support foreign SIDs
609 Do we really need to validate the type of SID we have in this case?
612 fstring dom_name
, name
;
613 enum SID_NAME_USE name_type
;
615 *sidtype
= SID_NAME_UNKNOWN
;
617 * First we must look up the name and decide if this is a user sid.
620 if ( (!winbind_lookup_sid(psid
, dom_name
, name
, &name_type
)) || (name_type
!= SID_NAME_USER
) ) {
622 DEBUG(10,("sid_to_uid: winbind lookup for sid %s failed - trying local.\n",
623 sid_to_string(sid_str
, psid
) ));
626 result
= local_sid_to_uid(puid
, psid
, sidtype
);
632 * Ensure this is a user sid.
635 if (name_type
!= SID_NAME_USER
) {
636 DEBUG(10,("sid_to_uid: winbind lookup succeeded but SID is not a uid (%u)\n",
637 (unsigned int)name_type
));
641 *sidtype
= SID_NAME_USER
;
644 * Get the uid for this SID.
647 if (!winbind_sid_to_uid(puid
, psid
)) {
649 DEBUG(10,("sid_to_uid: winbind lookup for sid %s failed.\n",
650 sid_to_string(sid_str
, psid
) ));
652 result
= local_sid_to_uid(puid
, psid
, sidtype
);
657 DEBUG(10,("sid_to_uid: winbindd %s -> %u\n",
658 sid_to_string(sid_str
, psid
),
659 (unsigned int)*puid
));
664 /*****************************************************************
665 *THE CANONICAL* convert SID to gid function.
666 Tries winbind first - then uses local lookup.
667 Returns True if this name is a user sid and the conversion
668 was done correctly, False if not.
669 *****************************************************************/
671 BOOL
sid_to_gid(const DOM_SID
*psid
, gid_t
*pgid
, enum SID_NAME_USE
*sidtype
)
673 fstring dom_name
, name
, sid_str
;
674 enum SID_NAME_USE name_type
;
676 *sidtype
= SID_NAME_UNKNOWN
;
679 * First we must look up the name and decide if this is a group sid.
682 /* if we know its local then don't try winbindd */
683 if (sid_compare_domain(get_global_sam_sid(), psid
) == 0) {
686 result
= local_sid_to_gid(pgid
, psid
, sidtype
);
691 if (!winbind_lookup_sid(psid
, dom_name
, name
, &name_type
)) {
692 DEBUG(10,("sid_to_gid: winbind lookup for sid %s failed.\n",
693 sid_to_string(sid_str
, psid
) ));
694 /* this was probably a foreign sid - assume its a group rid
696 name_type
= SID_NAME_DOM_GRP
;
700 * Ensure this is a group sid.
703 if ((name_type
!= SID_NAME_DOM_GRP
) && (name_type
!= SID_NAME_ALIAS
) && (name_type
!= SID_NAME_WKN_GRP
)) {
704 DEBUG(10,("sid_to_gid: winbind lookup succeeded but SID is not a known group (%u)\n",
705 (unsigned int)name_type
));
710 *sidtype
= name_type
;
713 * Get the gid for this SID.
716 if (!winbind_sid_to_gid(pgid
, psid
)) {
717 DEBUG(10,("sid_to_gid: winbind lookup for sid %s failed.\n",
718 sid_to_string(sid_str
, psid
) ));
722 DEBUG(10,("sid_to_gid: winbindd %s -> %u\n",
723 sid_to_string(sid_str
, psid
),
724 (unsigned int)*pgid
));