2 Unix SMB/CIFS implementation.
3 Check access to files based on security descriptors.
4 Copyright (C) Jeremy Allison 2005-2006.
5 Copyright (C) Michael Adam 2007.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "../libcli/security/security.h"
24 #include "../librpc/gen_ndr/ndr_security.h"
25 #include "smbd/smbd.h"
28 #define DBGC_CLASS DBGC_ACLS
30 /****************************************************************************
31 Actually emulate the in-kernel access checking for delete access. We need
32 this to successfully return ACCESS_DENIED on a file open for delete access.
33 ****************************************************************************/
35 bool can_delete_file_in_directory(connection_struct
*conn
,
36 const struct smb_filename
*smb_fname
)
38 TALLOC_CTX
*ctx
= talloc_tos();
40 struct smb_filename
*smb_fname_parent
;
43 if (!CAN_WRITE(conn
)) {
47 if (!lp_acl_check_permissions(SNUM(conn
))) {
48 /* This option means don't check. */
52 /* Get the parent directory permission mask and owners. */
53 if (!parent_dirname(ctx
, smb_fname
->base_name
, &dname
, NULL
)) {
57 smb_fname_parent
= synthetic_smb_fname(ctx
, dname
, NULL
, NULL
);
58 if (smb_fname_parent
== NULL
) {
63 if(SMB_VFS_STAT(conn
, smb_fname_parent
) != 0) {
68 /* fast paths first */
70 if (!S_ISDIR(smb_fname_parent
->st
.st_ex_mode
)) {
74 if (get_current_uid(conn
) == (uid_t
)0) {
75 /* I'm sorry sir, I didn't know you were root... */
81 /* sticky bit means delete only by owner of file or by root or
82 * by owner of directory. */
83 if (smb_fname_parent
->st
.st_ex_mode
& S_ISVTX
) {
84 if (!VALID_STAT(smb_fname
->st
)) {
85 /* If the file doesn't already exist then
86 * yes we'll be able to delete it. */
92 * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
93 * for bug #3348. Don't assume owning sticky bit
94 * directory means write access allowed.
95 * Fail to delete if we're not the owner of the file,
96 * or the owner of the directory as we have no possible
97 * chance of deleting. Otherwise, go on and check the ACL.
99 if ((get_current_uid(conn
) !=
100 smb_fname_parent
->st
.st_ex_uid
) &&
101 (get_current_uid(conn
) != smb_fname
->st
.st_ex_uid
)) {
102 DEBUG(10,("can_delete_file_in_directory: not "
103 "owner of file %s or directory %s",
104 smb_fname_str_dbg(smb_fname
),
105 smb_fname_str_dbg(smb_fname_parent
)));
112 /* now for ACL checks */
115 * There's two ways to get the permission to delete a file: First by
116 * having the DELETE bit on the file itself and second if that does
117 * not help, by the DELETE_CHILD bit on the containing directory.
119 * Here we only check the directory permissions, we will
120 * check the file DELETE permission separately.
123 ret
= NT_STATUS_IS_OK(smbd_check_access_rights(conn
,
129 TALLOC_FREE(smb_fname_parent
);
133 /****************************************************************************
134 Userspace check for write access.
135 ****************************************************************************/
137 bool can_write_to_file(connection_struct
*conn
,
138 const struct smb_filename
*smb_fname
)
140 return NT_STATUS_IS_OK(smbd_check_access_rights(conn
,
146 /****************************************************************************
147 Check for an existing default Windows ACL on a directory.
148 ****************************************************************************/
150 bool directory_has_default_acl(connection_struct
*conn
, const char *fname
)
152 struct security_descriptor
*secdesc
= NULL
;
154 NTSTATUS status
= SMB_VFS_GET_NT_ACL(conn
, fname
,
155 SECINFO_DACL
, talloc_tos(),
158 if (!NT_STATUS_IS_OK(status
) ||
160 secdesc
->dacl
== NULL
) {
161 TALLOC_FREE(secdesc
);
165 for (i
= 0; i
< secdesc
->dacl
->num_aces
; i
++) {
166 struct security_ace
*psa
= &secdesc
->dacl
->aces
[i
];
167 if (psa
->flags
& (SEC_ACE_FLAG_OBJECT_INHERIT
|
168 SEC_ACE_FLAG_CONTAINER_INHERIT
)) {
169 TALLOC_FREE(secdesc
);
173 TALLOC_FREE(secdesc
);
177 /****************************************************************************
178 Check if setting delete on close is allowed on this fsp.
179 ****************************************************************************/
181 NTSTATUS
can_set_delete_on_close(files_struct
*fsp
, uint32 dosmode
)
184 * Only allow delete on close for writable files.
187 if ((dosmode
& FILE_ATTRIBUTE_READONLY
) &&
188 !lp_delete_readonly(SNUM(fsp
->conn
))) {
189 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
190 "flag set but file attribute is readonly.\n",
192 return NT_STATUS_CANNOT_DELETE
;
196 * Only allow delete on close for writable shares.
199 if (!CAN_WRITE(fsp
->conn
)) {
200 DEBUG(10,("can_set_delete_on_close: file %s delete on "
201 "close flag set but write access denied on share.\n",
203 return NT_STATUS_ACCESS_DENIED
;
207 * Only allow delete on close for files/directories opened with delete
211 if (!(fsp
->access_mask
& DELETE_ACCESS
)) {
212 DEBUG(10,("can_set_delete_on_close: file %s delete on "
213 "close flag set but delete access denied.\n",
215 return NT_STATUS_ACCESS_DENIED
;
218 /* Don't allow delete on close for non-empty directories. */
219 if (fsp
->is_directory
) {
220 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp
->fsp_name
));
222 /* Or the root of a share. */
223 if (ISDOT(fsp
->fsp_name
->base_name
)) {
224 DEBUG(10,("can_set_delete_on_close: can't set delete on "
225 "close for the root of a share.\n"));
226 return NT_STATUS_ACCESS_DENIED
;
229 return can_delete_directory_fsp(fsp
);