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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "system/passwd.h"
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "source3/smbd/smbXsrv_session.h"
25 #include "../librpc/gen_ndr/netlogon.h"
26 #include "libcli/security/security.h"
27 #include "passdb/lookup_sid.h"
29 #include "../auth/auth_util.h"
30 #include "source3/lib/substitute.h"
32 /* what user is current? */
33 extern struct current_user current_user
;
35 /****************************************************************************
36 Become the guest user without changing the security context stack.
37 ****************************************************************************/
39 bool change_to_guest(void)
43 pass
= Get_Pwnam_alloc(talloc_tos(), lp_guest_account());
49 /* MWW: From AIX FAQ patch to WU-ftpd: call initgroups before
51 initgroups(pass
->pw_name
, pass
->pw_gid
);
54 set_sec_ctx(pass
->pw_uid
, pass
->pw_gid
, 0, NULL
, NULL
);
56 current_user
.conn
= NULL
;
57 current_user
.vuid
= UID_FIELD_INVALID
;
64 /****************************************************************************
65 talloc free the conn->session_info if not used in the vuid cache.
66 ****************************************************************************/
68 static void free_conn_state_if_unused(connection_struct
*conn
)
72 for (i
= 0; i
< VUID_CACHE_SIZE
; i
++) {
73 struct vuid_cache_entry
*ent
;
74 ent
= &conn
->vuid_cache
->array
[i
];
75 if (ent
->vuid
!= UID_FIELD_INVALID
&&
76 conn
->session_info
== ent
->session_info
) {
80 if (i
== VUID_CACHE_SIZE
) {
81 /* Not used, safe to free. */
82 TALLOC_FREE(conn
->session_info
);
86 /****************************************************************************
87 Setup the share access mask for a connection.
88 ****************************************************************************/
90 static uint32_t create_share_access_mask(int snum
,
92 const struct security_token
*token
)
94 uint32_t share_access
= 0;
96 share_access_check(token
,
97 lp_const_servicename(snum
),
98 MAXIMUM_ALLOWED_ACCESS
,
101 if (readonly_share
) {
103 ~(SEC_FILE_WRITE_DATA
| SEC_FILE_APPEND_DATA
|
104 SEC_FILE_WRITE_EA
| SEC_FILE_WRITE_ATTRIBUTE
|
105 SEC_DIR_DELETE_CHILD
);
108 if (security_token_has_privilege(token
, SEC_PRIV_SECURITY
)) {
109 share_access
|= SEC_FLAG_SYSTEM_SECURITY
;
111 if (security_token_has_privilege(token
, SEC_PRIV_RESTORE
)) {
112 share_access
|= SEC_RIGHTS_PRIV_RESTORE
;
114 if (security_token_has_privilege(token
, SEC_PRIV_BACKUP
)) {
115 share_access
|= SEC_RIGHTS_PRIV_BACKUP
;
117 if (security_token_has_privilege(token
, SEC_PRIV_TAKE_OWNERSHIP
)) {
118 share_access
|= SEC_STD_WRITE_OWNER
;
124 /*******************************************************************
125 Calculate access mask and if this user can access this share.
126 ********************************************************************/
128 NTSTATUS
check_user_share_access(connection_struct
*conn
,
129 const struct auth_session_info
*session_info
,
130 uint32_t *p_share_access
,
131 bool *p_readonly_share
)
133 int snum
= SNUM(conn
);
134 uint32_t share_access
= 0;
135 bool readonly_share
= false;
138 if (!user_ok_token(session_info
->unix_info
->unix_name
,
139 session_info
->info
->domain_name
,
140 session_info
->security_token
, snum
)) {
141 return NT_STATUS_ACCESS_DENIED
;
144 ok
= is_share_read_only_for_token(
145 session_info
->unix_info
->unix_name
,
146 session_info
->info
->domain_name
,
147 session_info
->security_token
,
151 return NT_STATUS_ACCESS_DENIED
;
154 share_access
= create_share_access_mask(snum
,
156 session_info
->security_token
);
158 if ((share_access
& (FILE_READ_DATA
|FILE_WRITE_DATA
)) == 0) {
159 /* No access, read or write. */
160 DBG_NOTICE("user %s connection to %s denied due to share "
161 "security descriptor.\n",
162 session_info
->unix_info
->unix_name
,
163 lp_const_servicename(snum
));
164 return NT_STATUS_ACCESS_DENIED
;
167 if (!readonly_share
&&
168 !(share_access
& FILE_WRITE_DATA
)) {
169 /* smb.conf allows r/w, but the security descriptor denies
170 * write. Fall back to looking at readonly. */
171 readonly_share
= true;
172 DBG_INFO("falling back to read-only access-evaluation due to "
173 "security descriptor\n");
176 *p_share_access
= share_access
;
177 *p_readonly_share
= readonly_share
;
182 struct scan_file_list_state
{
184 const struct loadparm_substitution
*lp_sub
;
186 const char *param_type
;
187 struct security_token
*token
;
188 struct name_compare_entry
**list
;
192 static bool scan_file_list_cb(const char *string
,
193 regmatch_t matches
[],
196 struct scan_file_list_state
*state
= private_data
;
198 if (matches
[1].rm_so
== -1) {
199 DBG_WARNING("Found match, but no name??\n");
202 if (matches
[1].rm_eo
<= matches
[1].rm_so
) {
203 DBG_WARNING("Invalid match\n");
208 regoff_t len
= matches
[1].rm_eo
- matches
[1].rm_so
;
213 memcpy(name
, string
+ matches
[1].rm_so
, len
);
216 DBG_DEBUG("Found name \"%s : %s\"\n", state
->param_type
, name
);
218 ok
= token_contains_name(talloc_tos(),
229 return false; /* don't stop traverse */
232 files
= lp_parm_substituted_string(state
->mem_ctx
,
242 ok
= append_to_namearray(state
->mem_ctx
,
249 return false; /* don't stop traverse */
254 return true; /* stop traverse */
257 /*******************************************************************
258 Check if a username is OK.
260 This sets up conn->session_info with a copy related to this vuser that
261 later code can then mess with.
262 ********************************************************************/
264 static bool check_user_ok(connection_struct
*conn
,
266 const struct auth_session_info
*session_info
,
269 const struct loadparm_substitution
*lp_sub
=
270 loadparm_s3_global_substitution();
271 bool readonly_share
= false;
272 bool admin_user
= false;
273 struct vuid_cache_entry
*ent
= NULL
;
274 uint32_t share_access
= 0;
278 if (vuid
!= UID_FIELD_INVALID
) {
281 for (i
=0; i
<VUID_CACHE_SIZE
; i
++) {
282 ent
= &conn
->vuid_cache
->array
[i
];
283 if (ent
->vuid
== vuid
) {
284 free_conn_state_if_unused(conn
);
285 conn
->session_info
= ent
->session_info
;
286 conn
->read_only
= ent
->read_only
;
287 conn
->share_access
= ent
->share_access
;
288 conn
->vuid
= ent
->vuid
;
289 conn
->veto_list
= ent
->veto_list
;
290 conn
->hide_list
= ent
->hide_list
;
296 status
= check_user_share_access(conn
,
300 if (!NT_STATUS_IS_OK(status
)) {
304 ok
= token_contains_name_in_list(
305 session_info
->unix_info
->unix_name
,
306 session_info
->info
->domain_name
,
308 session_info
->security_token
,
309 lp_admin_users(snum
),
312 /* Log, but move on */
313 DBG_ERR("Couldn't apply 'admin users'\n");
316 ent
= &conn
->vuid_cache
->array
[conn
->vuid_cache
->next_entry
];
318 conn
->vuid_cache
->next_entry
=
319 (conn
->vuid_cache
->next_entry
+ 1) % VUID_CACHE_SIZE
;
321 TALLOC_FREE(ent
->session_info
);
322 TALLOC_FREE(ent
->veto_list
);
323 TALLOC_FREE(ent
->hide_list
);
326 * If force_user was set, all session_info's are based on the same
327 * username-based faked one.
330 ent
->session_info
= copy_session_info(
331 conn
, conn
->force_user
? conn
->session_info
: session_info
);
333 if (ent
->session_info
== NULL
) {
334 ent
->vuid
= UID_FIELD_INVALID
;
339 DEBUG(2,("check_user_ok: user %s is an admin user. "
340 "Setting uid as %d\n",
341 ent
->session_info
->unix_info
->unix_name
,
342 sec_initial_uid() ));
343 ent
->session_info
->unix_token
->uid
= sec_initial_uid();
347 * It's actually OK to call check_user_ok() with
348 * vuid == UID_FIELD_INVALID as called from become_user_by_session().
349 * All this will do is throw away one entry in the cache.
353 ent
->read_only
= readonly_share
;
354 ent
->share_access
= share_access
;
356 /* Add veto/hide lists */
357 if (!IS_IPC(conn
) && !IS_PRINT(conn
)) {
358 struct scan_file_list_state state
= {
362 .token
= session_info
->security_token
,
367 ok
= set_namearray(conn
,
368 lp_veto_files(talloc_tos(), lp_sub
, snum
),
375 * A bit of boilerplate code duplication for userlevel
376 * hide and veto files in the share and global
377 * sections, but not enough to justify putting this
378 * into functions for now :-)
381 state
.param_type
= "veto files";
382 state
.list
= &ent
->veto_list
;
384 ret
= lp_wi_scan_global_parametrics("vetofiles:\\(.*\\)",
388 if ((ret
!= 0) || !state
.ok
) {
391 ret
= lp_wi_scan_share_parametrics(snum
,
392 "vetofiles:\\(.*\\)",
396 if ((ret
!= 0) || !state
.ok
) {
400 ok
= set_namearray(conn
,
401 lp_hide_files(talloc_tos(), lp_sub
, snum
),
407 state
.param_type
= "hide files";
408 state
.list
= &ent
->hide_list
;
410 ret
= lp_wi_scan_global_parametrics("hidefiles:\\(.*\\)",
414 if ((ret
!= 0) || !state
.ok
) {
417 ret
= lp_wi_scan_share_parametrics(snum
,
418 "hidefiles:\\(.*\\)",
422 if ((ret
!= 0) || !state
.ok
) {
427 free_conn_state_if_unused(conn
);
428 conn
->session_info
= ent
->session_info
;
429 conn
->veto_list
= ent
->veto_list
;
430 conn
->hide_list
= ent
->hide_list
;
431 conn
->vuid
= ent
->vuid
;
432 if (vuid
== UID_FIELD_INVALID
) {
434 * Not strictly needed, just make it really
435 * clear this entry is actually an unused one.
437 ent
->read_only
= false;
438 ent
->share_access
= 0;
439 ent
->session_info
= NULL
;
442 conn
->read_only
= readonly_share
;
443 conn
->share_access
= share_access
;
448 static void print_impersonation_info(connection_struct
*conn
)
450 struct smb_filename
*cwdfname
= NULL
;
452 if (!CHECK_DEBUGLVL(DBGLVL_INFO
)) {
456 if (conn
->tcon_done
) {
457 cwdfname
= vfs_GetWd(talloc_tos(), conn
);
460 DBG_INFO("Impersonated user: uid=(%d,%d), gid=(%d,%d), cwd=[%s]\n",
465 cwdfname
? cwdfname
->base_name
: "no cwd");
466 TALLOC_FREE(cwdfname
);
469 /****************************************************************************
470 Become the user of a connection number without changing the security context
471 stack, but modify the current_user entries.
472 ****************************************************************************/
474 static bool change_to_user_impersonate(connection_struct
*conn
,
475 const struct auth_session_info
*session_info
,
478 const struct loadparm_substitution
*lp_sub
=
479 loadparm_s3_global_substitution();
483 const char *force_group_name
;
486 gid_t
*group_list
= NULL
;
489 if ((current_user
.conn
== conn
) &&
490 (current_user
.vuid
== vuid
) &&
491 (current_user
.ut
.uid
== session_info
->unix_token
->uid
))
493 DBG_INFO("Skipping user change - already user\n");
497 set_current_user_info(session_info
->unix_info
->sanitized_username
,
498 session_info
->unix_info
->unix_name
,
499 session_info
->info
->domain_name
);
503 ok
= check_user_ok(conn
, vuid
, session_info
, snum
);
505 DBG_WARNING("SMB user %s (unix user %s) "
506 "not permitted access to share %s.\n",
507 session_info
->unix_info
->sanitized_username
,
508 session_info
->unix_info
->unix_name
,
509 lp_const_servicename(snum
));
513 uid
= conn
->session_info
->unix_token
->uid
;
514 gid
= conn
->session_info
->unix_token
->gid
;
515 num_groups
= conn
->session_info
->unix_token
->ngroups
;
516 group_list
= conn
->session_info
->unix_token
->groups
;
519 * See if we should force group for this service. If so this overrides
520 * any group set in the force user code.
522 force_group_name
= lp_force_group(talloc_tos(), lp_sub
, snum
);
523 group_c
= *force_group_name
;
525 if ((group_c
!= '\0') && (conn
->force_group_gid
== (gid_t
)-1)) {
527 * This can happen if "force group" is added to a
528 * share definition whilst an existing connection
529 * to that share exists. In that case, don't change
530 * the existing credentials for force group, only
531 * do so for new connections.
533 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13690
535 DBG_INFO("Not forcing group %s on existing connection to "
536 "share %s for SMB user %s (unix user %s)\n",
538 lp_const_servicename(snum
),
539 session_info
->unix_info
->sanitized_username
,
540 session_info
->unix_info
->unix_name
);
543 if((group_c
!= '\0') && (conn
->force_group_gid
!= (gid_t
)-1)) {
545 * Only force group for connections where
546 * conn->force_group_gid has already been set
547 * to the correct value (i.e. the connection
548 * happened after the 'force group' definition
549 * was added to the share definition. Connections
550 * that were made before force group was added
551 * should stay with their existing credentials.
553 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13690
556 if (group_c
== '+') {
560 * Only force group if the user is a member of the
561 * service group. Check the group memberships for this
562 * user (we already have this) to see if we should force
565 for (i
= 0; i
< num_groups
; i
++) {
566 if (group_list
[i
] == conn
->force_group_gid
) {
567 conn
->session_info
->unix_token
->gid
=
568 conn
->force_group_gid
;
569 gid
= conn
->force_group_gid
;
570 gid_to_sid(&conn
->session_info
->security_token
576 conn
->session_info
->unix_token
->gid
= conn
->force_group_gid
;
577 gid
= conn
->force_group_gid
;
578 gid_to_sid(&conn
->session_info
->security_token
->sids
[1],
587 conn
->session_info
->security_token
);
589 current_user
.conn
= conn
;
590 current_user
.vuid
= vuid
;
595 * Impersonate user and change directory to service
597 * change_to_user_and_service() is used to impersonate the user associated with
598 * the given vuid and to change the working directory of the process to the
599 * service base directory.
601 bool change_to_user_and_service(connection_struct
*conn
, uint64_t vuid
)
603 int snum
= SNUM(conn
);
604 struct auth_session_info
*si
= NULL
;
609 DBG_WARNING("Connection not open\n");
613 status
= smbXsrv_session_info_lookup(conn
->sconn
->client
,
616 if (!NT_STATUS_IS_OK(status
)) {
617 DBG_WARNING("Invalid vuid %llu used on share %s.\n",
618 (unsigned long long)vuid
,
619 lp_const_servicename(snum
));
623 ok
= change_to_user_impersonate(conn
, si
, vuid
);
628 if (conn
->tcon_done
) {
629 ok
= chdir_current_service(conn
);
635 print_impersonation_info(conn
);
640 * Impersonate user and change directory to service
642 * change_to_user_and_service_by_fsp() is used to impersonate the user
643 * associated with the given vuid and to change the working directory of the
644 * process to the service base directory.
646 bool change_to_user_and_service_by_fsp(struct files_struct
*fsp
)
648 return change_to_user_and_service(fsp
->conn
, fsp
->vuid
);
651 /****************************************************************************
652 Go back to being root without changing the security context stack,
653 but modify the current_user entries.
654 ****************************************************************************/
656 bool smbd_change_to_root_user(void)
660 DEBUG(5,("change_to_root_user: now uid=(%d,%d) gid=(%d,%d)\n",
661 (int)getuid(),(int)geteuid(),(int)getgid(),(int)getegid()));
663 current_user
.conn
= NULL
;
664 current_user
.vuid
= UID_FIELD_INVALID
;
669 /****************************************************************************
670 Become the user of an authenticated connected named pipe.
671 When this is called we are currently running as the connection
672 user. Doesn't modify current_user.
673 ****************************************************************************/
675 bool smbd_become_authenticated_pipe_user(struct auth_session_info
*session_info
)
680 set_current_user_info(session_info
->unix_info
->sanitized_username
,
681 session_info
->unix_info
->unix_name
,
682 session_info
->info
->domain_name
);
684 set_sec_ctx(session_info
->unix_token
->uid
, session_info
->unix_token
->gid
,
685 session_info
->unix_token
->ngroups
, session_info
->unix_token
->groups
,
686 session_info
->security_token
);
688 DEBUG(5, ("Impersonated user: uid=(%d,%d), gid=(%d,%d)\n",
697 /****************************************************************************
698 Unbecome the user of an authenticated connected named pipe.
699 When this is called we are running as the authenticated pipe
700 user and need to go back to being the connection user. Doesn't modify
702 ****************************************************************************/
704 bool smbd_unbecome_authenticated_pipe_user(void)
706 return pop_sec_ctx();
709 /****************************************************************************
710 Utility functions used by become_xxx/unbecome_xxx.
711 ****************************************************************************/
713 static void push_conn_ctx(void)
715 struct conn_ctx
*ctx_p
;
716 extern userdom_struct current_user_info
;
718 /* Check we don't overflow our stack */
720 if (conn_ctx_stack_ndx
== MAX_SEC_CTX_DEPTH
) {
721 DEBUG(0, ("Connection context stack overflow!\n"));
722 smb_panic("Connection context stack overflow!\n");
725 /* Store previous user context */
726 ctx_p
= &conn_ctx_stack
[conn_ctx_stack_ndx
];
728 ctx_p
->conn
= current_user
.conn
;
729 ctx_p
->vuid
= current_user
.vuid
;
730 ctx_p
->user_info
= current_user_info
;
732 DEBUG(4, ("push_conn_ctx(%llu) : conn_ctx_stack_ndx = %d\n",
733 (unsigned long long)ctx_p
->vuid
, conn_ctx_stack_ndx
));
735 conn_ctx_stack_ndx
++;
738 static void pop_conn_ctx(void)
740 struct conn_ctx
*ctx_p
;
742 /* Check for stack underflow. */
744 if (conn_ctx_stack_ndx
== 0) {
745 DEBUG(0, ("Connection context stack underflow!\n"));
746 smb_panic("Connection context stack underflow!\n");
749 conn_ctx_stack_ndx
--;
750 ctx_p
= &conn_ctx_stack
[conn_ctx_stack_ndx
];
752 set_current_user_info(ctx_p
->user_info
.smb_name
,
753 ctx_p
->user_info
.unix_name
,
754 ctx_p
->user_info
.domain
);
756 current_user
.conn
= ctx_p
->conn
;
757 current_user
.vuid
= ctx_p
->vuid
;
759 *ctx_p
= (struct conn_ctx
) {
760 .vuid
= UID_FIELD_INVALID
,
764 /****************************************************************************
765 Temporarily become a root user. Must match with unbecome_root(). Saves and
766 restores the connection context.
767 ****************************************************************************/
769 void smbd_become_root(void)
772 * no good way to handle push_sec_ctx() failing without changing
773 * the prototype of become_root()
775 if (!push_sec_ctx()) {
776 smb_panic("become_root: push_sec_ctx failed");
782 /* Unbecome the root user */
784 void smbd_unbecome_root(void)
790 /****************************************************************************
791 Push the current security context then force a change via change_to_user().
792 Saves and restores the connection context.
793 ****************************************************************************/
795 bool become_user_without_service(connection_struct
*conn
, uint64_t vuid
)
797 struct auth_session_info
*session_info
= NULL
;
798 int snum
= SNUM(conn
);
803 DBG_WARNING("Connection not open\n");
807 status
= smbXsrv_session_info_lookup(conn
->sconn
->client
,
810 if (!NT_STATUS_IS_OK(status
)) {
811 /* Invalid vuid sent */
812 DBG_WARNING("Invalid vuid %llu used on share %s.\n",
813 (unsigned long long)vuid
,
814 lp_const_servicename(snum
));
825 ok
= change_to_user_impersonate(conn
, session_info
, vuid
);
835 bool become_user_without_service_by_fsp(struct files_struct
*fsp
)
837 return become_user_without_service(fsp
->conn
, fsp
->vuid
);
840 bool become_user_without_service_by_session(connection_struct
*conn
,
841 const struct auth_session_info
*session_info
)
845 SMB_ASSERT(conn
!= NULL
);
846 SMB_ASSERT(session_info
!= NULL
);
855 ok
= change_to_user_impersonate(conn
, session_info
, UID_FIELD_INVALID
);
865 bool unbecome_user_without_service(void)
872 /****************************************************************************
873 Return the current user we are running effectively as on this connection.
874 I'd like to make this return conn->session_info->unix_token->uid, but become_root()
875 doesn't alter this value.
876 ****************************************************************************/
878 uid_t
get_current_uid(connection_struct
*conn
)
880 return current_user
.ut
.uid
;
883 /****************************************************************************
884 Return the current group we are running effectively as on this connection.
885 I'd like to make this return conn->session_info->unix_token->gid, but become_root()
886 doesn't alter this value.
887 ****************************************************************************/
889 gid_t
get_current_gid(connection_struct
*conn
)
891 return current_user
.ut
.gid
;
894 /****************************************************************************
895 Return the UNIX token we are running effectively as on this connection.
896 I'd like to make this return &conn->session_info->unix_token-> but become_root()
897 doesn't alter this value.
898 ****************************************************************************/
900 const struct security_unix_token
*get_current_utok(connection_struct
*conn
)
902 return ¤t_user
.ut
;
905 /****************************************************************************
906 Return the Windows token we are running effectively as on this connection.
907 If this is currently a NULL token as we're inside become_root() - a temporary
908 UNIX security override, then we search up the stack for the previous active
910 ****************************************************************************/
912 const struct security_token
*get_current_nttok(connection_struct
*conn
)
914 if (current_user
.nt_user_token
) {
915 return current_user
.nt_user_token
;
917 return sec_ctx_active_token();