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 "../libcli/security/security.h"
23 #include "../librpc/gen_ndr/ndr_security.h"
26 #define DBGC_CLASS DBGC_ACLS
29 * Security descriptor / NT Token level access check function.
31 bool can_access_file_acl(struct connection_struct
*conn
,
32 const struct smb_filename
*smb_fname
,
36 uint32_t access_granted
;
37 struct security_descriptor
*secdesc
= NULL
;
40 if (get_current_uid(conn
) == (uid_t
)0) {
41 /* I'm sorry sir, I didn't know you were root... */
45 status
= SMB_VFS_GET_NT_ACL(conn
, smb_fname
->base_name
,
50 if (!NT_STATUS_IS_OK(status
)) {
51 DEBUG(5, ("Could not get acl: %s\n", nt_errstr(status
)));
56 status
= se_access_check(secdesc
, get_current_nttok(conn
),
57 access_mask
, &access_granted
);
58 ret
= NT_STATUS_IS_OK(status
);
60 if (DEBUGLEVEL
>= 10) {
61 DEBUG(10,("can_access_file_acl for file %s "
62 "access_mask 0x%x, access_granted 0x%x "
64 smb_fname_str_dbg(smb_fname
),
65 (unsigned int)access_mask
,
66 (unsigned int)access_granted
,
67 ret
? "ALLOWED" : "DENIED" ));
68 NDR_PRINT_DEBUG(security_descriptor
, secdesc
);
75 /****************************************************************************
76 Actually emulate the in-kernel access checking for delete access. We need
77 this to successfully return ACCESS_DENIED on a file open for delete access.
78 ****************************************************************************/
80 bool can_delete_file_in_directory(connection_struct
*conn
,
81 struct smb_filename
*smb_fname
)
83 TALLOC_CTX
*ctx
= talloc_tos();
85 struct smb_filename
*smb_fname_parent
= NULL
;
89 if (!CAN_WRITE(conn
)) {
93 /* Get the parent directory permission mask and owners. */
94 if (!parent_dirname(ctx
, smb_fname
->base_name
, &dname
, NULL
)) {
98 status
= create_synthetic_smb_fname(ctx
, dname
, NULL
, NULL
,
100 if (!NT_STATUS_IS_OK(status
)) {
105 if(SMB_VFS_STAT(conn
, smb_fname_parent
) != 0) {
110 /* fast paths first */
112 if (!S_ISDIR(smb_fname_parent
->st
.st_ex_mode
)) {
116 if (get_current_uid(conn
) == (uid_t
)0) {
117 /* I'm sorry sir, I didn't know you were root... */
123 /* sticky bit means delete only by owner of file or by root or
124 * by owner of directory. */
125 if (smb_fname_parent
->st
.st_ex_mode
& S_ISVTX
) {
126 if(SMB_VFS_STAT(conn
, smb_fname
) != 0) {
127 if (errno
== ENOENT
) {
128 /* If the file doesn't already exist then
129 * yes we'll be able to delete it. */
133 DEBUG(10,("can_delete_file_in_directory: can't "
135 smb_fname_str_dbg(smb_fname
),
142 * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
143 * for bug #3348. Don't assume owning sticky bit
144 * directory means write access allowed.
145 * Fail to delete if we're not the owner of the file,
146 * or the owner of the directory as we have no possible
147 * chance of deleting. Otherwise, go on and check the ACL.
149 if ((get_current_uid(conn
) !=
150 smb_fname_parent
->st
.st_ex_uid
) &&
151 (get_current_uid(conn
) != smb_fname
->st
.st_ex_uid
)) {
152 DEBUG(10,("can_delete_file_in_directory: not "
153 "owner of file %s or directory %s",
154 smb_fname_str_dbg(smb_fname
),
155 smb_fname_str_dbg(smb_fname_parent
)));
162 /* now for ACL checks */
165 * There's two ways to get the permission to delete a file: First by
166 * having the DELETE bit on the file itself and second if that does
167 * not help, by the DELETE_CHILD bit on the containing directory.
169 * Here we only check the directory permissions, we will
170 * check the file DELETE permission separately.
173 ret
= can_access_file_acl(conn
, smb_fname_parent
, FILE_DELETE_CHILD
);
176 TALLOC_FREE(smb_fname_parent
);
180 /****************************************************************************
181 Actually emulate the in-kernel access checking for read/write access. We need
182 this to successfully check for ability to write for dos filetimes.
183 Note this doesn't take into account share write permissions.
184 ****************************************************************************/
186 bool can_access_file_data(connection_struct
*conn
,
187 const struct smb_filename
*smb_fname
,
190 if (!(access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
))) {
193 access_mask
&= (FILE_READ_DATA
|FILE_WRITE_DATA
);
195 /* some fast paths first */
197 DEBUG(10,("can_access_file_data: requesting 0x%x on file %s\n",
198 (unsigned int)access_mask
, smb_fname_str_dbg(smb_fname
)));
200 if (get_current_uid(conn
) == (uid_t
)0) {
201 /* I'm sorry sir, I didn't know you were root... */
205 SMB_ASSERT(VALID_STAT(smb_fname
->st
));
207 /* Check primary owner access. */
208 if (get_current_uid(conn
) == smb_fname
->st
.st_ex_uid
) {
209 switch (access_mask
) {
211 return (smb_fname
->st
.st_ex_mode
& S_IRUSR
) ?
214 case FILE_WRITE_DATA
:
215 return (smb_fname
->st
.st_ex_mode
& S_IWUSR
) ?
218 default: /* FILE_READ_DATA|FILE_WRITE_DATA */
220 if ((smb_fname
->st
.st_ex_mode
&
221 (S_IWUSR
|S_IRUSR
)) ==
230 /* now for ACL checks */
232 return can_access_file_acl(conn
, smb_fname
, access_mask
);
235 /****************************************************************************
236 Userspace check for write access.
237 Note this doesn't take into account share write permissions.
238 ****************************************************************************/
240 bool can_write_to_file(connection_struct
*conn
,
241 const struct smb_filename
*smb_fname
)
243 return can_access_file_data(conn
, smb_fname
, FILE_WRITE_DATA
);
246 /****************************************************************************
247 Check for an existing default Windows ACL on a directory.
248 ****************************************************************************/
250 bool directory_has_default_acl(connection_struct
*conn
, const char *fname
)
252 /* returns talloced off tos. */
253 struct security_descriptor
*secdesc
= NULL
;
255 NTSTATUS status
= SMB_VFS_GET_NT_ACL(conn
, fname
,
256 SECINFO_DACL
, &secdesc
);
258 if (!NT_STATUS_IS_OK(status
) || secdesc
== NULL
) {
262 for (i
= 0; i
< secdesc
->dacl
->num_aces
; i
++) {
263 struct security_ace
*psa
= &secdesc
->dacl
->aces
[i
];
264 if (psa
->flags
& (SEC_ACE_FLAG_OBJECT_INHERIT
|
265 SEC_ACE_FLAG_CONTAINER_INHERIT
)) {
266 TALLOC_FREE(secdesc
);
270 TALLOC_FREE(secdesc
);