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 cwdfname
= vfs_GetWd(talloc_tos(), conn
);
457 if (cwdfname
== NULL
) {
461 DBG_INFO("Impersonated user: uid=(%d,%d), gid=(%d,%d), cwd=[%s]\n",
466 cwdfname
->base_name
);
467 TALLOC_FREE(cwdfname
);
470 /****************************************************************************
471 Become the user of a connection number without changing the security context
472 stack, but modify the current_user entries.
473 ****************************************************************************/
475 static bool change_to_user_impersonate(connection_struct
*conn
,
476 const struct auth_session_info
*session_info
,
479 const struct loadparm_substitution
*lp_sub
=
480 loadparm_s3_global_substitution();
484 const char *force_group_name
;
487 gid_t
*group_list
= NULL
;
490 if ((current_user
.conn
== conn
) &&
491 (current_user
.vuid
== vuid
) &&
492 (current_user
.ut
.uid
== session_info
->unix_token
->uid
))
494 DBG_INFO("Skipping user change - already user\n");
498 set_current_user_info(session_info
->unix_info
->sanitized_username
,
499 session_info
->unix_info
->unix_name
,
500 session_info
->info
->domain_name
);
504 ok
= check_user_ok(conn
, vuid
, session_info
, snum
);
506 DBG_WARNING("SMB user %s (unix user %s) "
507 "not permitted access to share %s.\n",
508 session_info
->unix_info
->sanitized_username
,
509 session_info
->unix_info
->unix_name
,
510 lp_const_servicename(snum
));
514 uid
= conn
->session_info
->unix_token
->uid
;
515 gid
= conn
->session_info
->unix_token
->gid
;
516 num_groups
= conn
->session_info
->unix_token
->ngroups
;
517 group_list
= conn
->session_info
->unix_token
->groups
;
520 * See if we should force group for this service. If so this overrides
521 * any group set in the force user code.
523 force_group_name
= lp_force_group(talloc_tos(), lp_sub
, snum
);
524 group_c
= *force_group_name
;
526 if ((group_c
!= '\0') && (conn
->force_group_gid
== (gid_t
)-1)) {
528 * This can happen if "force group" is added to a
529 * share definition whilst an existing connection
530 * to that share exists. In that case, don't change
531 * the existing credentials for force group, only
532 * do so for new connections.
534 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13690
536 DBG_INFO("Not forcing group %s on existing connection to "
537 "share %s for SMB user %s (unix user %s)\n",
539 lp_const_servicename(snum
),
540 session_info
->unix_info
->sanitized_username
,
541 session_info
->unix_info
->unix_name
);
544 if((group_c
!= '\0') && (conn
->force_group_gid
!= (gid_t
)-1)) {
546 * Only force group for connections where
547 * conn->force_group_gid has already been set
548 * to the correct value (i.e. the connection
549 * happened after the 'force group' definition
550 * was added to the share definition. Connections
551 * that were made before force group was added
552 * should stay with their existing credentials.
554 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13690
557 if (group_c
== '+') {
561 * Only force group if the user is a member of the
562 * service group. Check the group memberships for this
563 * user (we already have this) to see if we should force
566 for (i
= 0; i
< num_groups
; i
++) {
567 if (group_list
[i
] == conn
->force_group_gid
) {
568 conn
->session_info
->unix_token
->gid
=
569 conn
->force_group_gid
;
570 gid
= conn
->force_group_gid
;
571 gid_to_sid(&conn
->session_info
->security_token
577 conn
->session_info
->unix_token
->gid
= conn
->force_group_gid
;
578 gid
= conn
->force_group_gid
;
579 gid_to_sid(&conn
->session_info
->security_token
->sids
[1],
588 conn
->session_info
->security_token
);
590 current_user
.conn
= conn
;
591 current_user
.vuid
= vuid
;
596 * Impersonate user and change directory to service
598 * change_to_user_and_service() is used to impersonate the user associated with
599 * the given vuid and to change the working directory of the process to the
600 * service base directory.
602 bool change_to_user_and_service(connection_struct
*conn
, uint64_t vuid
)
604 int snum
= SNUM(conn
);
605 struct auth_session_info
*si
= NULL
;
610 DBG_WARNING("Connection not open\n");
614 status
= smbXsrv_session_info_lookup(conn
->sconn
->client
,
617 if (!NT_STATUS_IS_OK(status
)) {
618 DBG_WARNING("Invalid vuid %llu used on share %s.\n",
619 (unsigned long long)vuid
,
620 lp_const_servicename(snum
));
624 ok
= change_to_user_impersonate(conn
, si
, vuid
);
629 if (conn
->tcon_done
) {
630 ok
= chdir_current_service(conn
);
636 print_impersonation_info(conn
);
641 * Impersonate user and change directory to service
643 * change_to_user_and_service_by_fsp() is used to impersonate the user
644 * associated with the given vuid and to change the working directory of the
645 * process to the service base directory.
647 bool change_to_user_and_service_by_fsp(struct files_struct
*fsp
)
649 return change_to_user_and_service(fsp
->conn
, fsp
->vuid
);
652 /****************************************************************************
653 Go back to being root without changing the security context stack,
654 but modify the current_user entries.
655 ****************************************************************************/
657 bool smbd_change_to_root_user(void)
661 DEBUG(5,("change_to_root_user: now uid=(%d,%d) gid=(%d,%d)\n",
662 (int)getuid(),(int)geteuid(),(int)getgid(),(int)getegid()));
664 current_user
.conn
= NULL
;
665 current_user
.vuid
= UID_FIELD_INVALID
;
670 /****************************************************************************
671 Become the user of an authenticated connected named pipe.
672 When this is called we are currently running as the connection
673 user. Doesn't modify current_user.
674 ****************************************************************************/
676 bool smbd_become_authenticated_pipe_user(struct auth_session_info
*session_info
)
681 set_current_user_info(session_info
->unix_info
->sanitized_username
,
682 session_info
->unix_info
->unix_name
,
683 session_info
->info
->domain_name
);
685 set_sec_ctx(session_info
->unix_token
->uid
, session_info
->unix_token
->gid
,
686 session_info
->unix_token
->ngroups
, session_info
->unix_token
->groups
,
687 session_info
->security_token
);
689 DEBUG(5, ("Impersonated user: uid=(%d,%d), gid=(%d,%d)\n",
698 /****************************************************************************
699 Unbecome the user of an authenticated connected named pipe.
700 When this is called we are running as the authenticated pipe
701 user and need to go back to being the connection user. Doesn't modify
703 ****************************************************************************/
705 bool smbd_unbecome_authenticated_pipe_user(void)
707 return pop_sec_ctx();
710 /****************************************************************************
711 Utility functions used by become_xxx/unbecome_xxx.
712 ****************************************************************************/
714 static void push_conn_ctx(void)
716 struct conn_ctx
*ctx_p
;
717 extern userdom_struct current_user_info
;
719 /* Check we don't overflow our stack */
721 if (conn_ctx_stack_ndx
== MAX_SEC_CTX_DEPTH
) {
722 DEBUG(0, ("Connection context stack overflow!\n"));
723 smb_panic("Connection context stack overflow!\n");
726 /* Store previous user context */
727 ctx_p
= &conn_ctx_stack
[conn_ctx_stack_ndx
];
729 ctx_p
->conn
= current_user
.conn
;
730 ctx_p
->vuid
= current_user
.vuid
;
731 ctx_p
->user_info
= current_user_info
;
733 DEBUG(4, ("push_conn_ctx(%llu) : conn_ctx_stack_ndx = %d\n",
734 (unsigned long long)ctx_p
->vuid
, conn_ctx_stack_ndx
));
736 conn_ctx_stack_ndx
++;
739 static void pop_conn_ctx(void)
741 struct conn_ctx
*ctx_p
;
743 /* Check for stack underflow. */
745 if (conn_ctx_stack_ndx
== 0) {
746 DEBUG(0, ("Connection context stack underflow!\n"));
747 smb_panic("Connection context stack underflow!\n");
750 conn_ctx_stack_ndx
--;
751 ctx_p
= &conn_ctx_stack
[conn_ctx_stack_ndx
];
753 set_current_user_info(ctx_p
->user_info
.smb_name
,
754 ctx_p
->user_info
.unix_name
,
755 ctx_p
->user_info
.domain
);
757 current_user
.conn
= ctx_p
->conn
;
758 current_user
.vuid
= ctx_p
->vuid
;
760 *ctx_p
= (struct conn_ctx
) {
761 .vuid
= UID_FIELD_INVALID
,
765 /****************************************************************************
766 Temporarily become a root user. Must match with unbecome_root(). Saves and
767 restores the connection context.
768 ****************************************************************************/
770 void smbd_become_root(void)
773 * no good way to handle push_sec_ctx() failing without changing
774 * the prototype of become_root()
776 if (!push_sec_ctx()) {
777 smb_panic("become_root: push_sec_ctx failed");
783 /* Unbecome the root user */
785 void smbd_unbecome_root(void)
791 /****************************************************************************
792 Push the current security context then force a change via change_to_user().
793 Saves and restores the connection context.
794 ****************************************************************************/
796 bool become_user_without_service(connection_struct
*conn
, uint64_t vuid
)
798 struct auth_session_info
*session_info
= NULL
;
799 int snum
= SNUM(conn
);
804 DBG_WARNING("Connection not open\n");
808 status
= smbXsrv_session_info_lookup(conn
->sconn
->client
,
811 if (!NT_STATUS_IS_OK(status
)) {
812 /* Invalid vuid sent */
813 DBG_WARNING("Invalid vuid %llu used on share %s.\n",
814 (unsigned long long)vuid
,
815 lp_const_servicename(snum
));
826 ok
= change_to_user_impersonate(conn
, session_info
, vuid
);
836 bool become_user_without_service_by_fsp(struct files_struct
*fsp
)
838 return become_user_without_service(fsp
->conn
, fsp
->vuid
);
841 bool become_user_without_service_by_session(connection_struct
*conn
,
842 const struct auth_session_info
*session_info
)
846 SMB_ASSERT(conn
!= NULL
);
847 SMB_ASSERT(session_info
!= NULL
);
856 ok
= change_to_user_impersonate(conn
, session_info
, UID_FIELD_INVALID
);
866 bool unbecome_user_without_service(void)
873 /****************************************************************************
874 Return the current user we are running effectively as on this connection.
875 I'd like to make this return conn->session_info->unix_token->uid, but become_root()
876 doesn't alter this value.
877 ****************************************************************************/
879 uid_t
get_current_uid(connection_struct
*conn
)
881 return current_user
.ut
.uid
;
884 /****************************************************************************
885 Return the current group we are running effectively as on this connection.
886 I'd like to make this return conn->session_info->unix_token->gid, but become_root()
887 doesn't alter this value.
888 ****************************************************************************/
890 gid_t
get_current_gid(connection_struct
*conn
)
892 return current_user
.ut
.gid
;
895 /****************************************************************************
896 Return the UNIX token we are running effectively as on this connection.
897 I'd like to make this return &conn->session_info->unix_token-> but become_root()
898 doesn't alter this value.
899 ****************************************************************************/
901 const struct security_unix_token
*get_current_utok(connection_struct
*conn
)
903 return ¤t_user
.ut
;
906 /****************************************************************************
907 Return the Windows token we are running effectively as on this connection.
908 If this is currently a NULL token as we're inside become_root() - a temporary
909 UNIX security override, then we search up the stack for the previous active
911 ****************************************************************************/
913 const struct security_token
*get_current_nttok(connection_struct
*conn
)
915 if (current_user
.nt_user_token
) {
916 return current_user
.nt_user_token
;
918 return sec_ctx_active_token();