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/>.
24 #define DBGC_CLASS DBGC_ACLS
27 * Security descriptor / NT Token level access check function.
29 bool can_access_file_acl(struct connection_struct
*conn
,
34 uint32_t access_granted
;
35 struct security_descriptor
*secdesc
= NULL
;
37 if (conn
->server_info
->utok
.uid
== 0 || conn
->admin_user
) {
38 /* I'm sorry sir, I didn't know you were root... */
42 status
= SMB_VFS_GET_NT_ACL(conn
, fname
,
43 (OWNER_SECURITY_INFORMATION
|
44 GROUP_SECURITY_INFORMATION
|
45 DACL_SECURITY_INFORMATION
),
47 if (!NT_STATUS_IS_OK(status
)) {
48 DEBUG(5, ("Could not get acl: %s\n", nt_errstr(status
)));
52 status
= se_access_check(secdesc
, conn
->server_info
->ptok
,
53 access_mask
, &access_granted
);
55 return NT_STATUS_IS_OK(status
);
58 /****************************************************************************
59 Actually emulate the in-kernel access checking for delete access. We need
60 this to successfully return ACCESS_DENIED on a file open for delete access.
61 ****************************************************************************/
63 bool can_delete_file_in_directory(connection_struct
*conn
, const char *fname
)
66 TALLOC_CTX
*ctx
= talloc_tos();
69 if (!CAN_WRITE(conn
)) {
73 /* Get the parent directory permission mask and owners. */
74 if (!parent_dirname_talloc(ctx
,
80 if(SMB_VFS_STAT(conn
, dname
, &sbuf
) != 0) {
84 /* fast paths first */
86 if (!S_ISDIR(sbuf
.st_mode
)) {
89 if (conn
->server_info
->utok
.uid
== 0 || conn
->admin_user
) {
90 /* I'm sorry sir, I didn't know you were root... */
95 /* sticky bit means delete only by owner or root. */
96 if (sbuf
.st_mode
& S_ISVTX
) {
97 SMB_STRUCT_STAT sbuf_file
;
98 if(SMB_VFS_STAT(conn
, fname
, &sbuf_file
) != 0) {
99 if (errno
== ENOENT
) {
100 /* If the file doesn't already exist then
101 * yes we'll be able to delete it. */
107 * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
108 * for bug #3348. Don't assume owning sticky bit
109 * directory means write access allowed.
111 if (conn
->server_info
->utok
.uid
!= sbuf_file
.st_uid
) {
117 /* now for ACL checks */
120 * There's two ways to get the permission to delete a file: First by
121 * having the DELETE bit on the file itself and second if that does
122 * not help, by the DELETE_CHILD bit on the containing directory.
124 * Here we only check the directory permissions, we will
125 * check the file DELETE permission separately.
128 return can_access_file_acl(conn
, dname
, FILE_DELETE_CHILD
);
131 /****************************************************************************
132 Actually emulate the in-kernel access checking for read/write access. We need
133 this to successfully check for ability to write for dos filetimes.
134 Note this doesn't take into account share write permissions.
135 ****************************************************************************/
137 bool can_access_file_data(connection_struct
*conn
, const char *fname
, SMB_STRUCT_STAT
*psbuf
, uint32 access_mask
)
139 if (!(access_mask
& (FILE_READ_DATA
|FILE_WRITE_DATA
))) {
142 access_mask
&= (FILE_READ_DATA
|FILE_WRITE_DATA
);
144 /* some fast paths first */
146 DEBUG(10,("can_access_file_data: requesting 0x%x on file %s\n",
147 (unsigned int)access_mask
, fname
));
149 if (conn
->server_info
->utok
.uid
== 0 || conn
->admin_user
) {
150 /* I'm sorry sir, I didn't know you were root... */
154 if (!VALID_STAT(*psbuf
)) {
155 /* Get the file permission mask and owners. */
156 if(SMB_VFS_STAT(conn
, fname
, psbuf
) != 0) {
161 /* Check primary owner access. */
162 if (conn
->server_info
->utok
.uid
== psbuf
->st_uid
) {
163 switch (access_mask
) {
165 return (psbuf
->st_mode
& S_IRUSR
) ? True
: False
;
167 case FILE_WRITE_DATA
:
168 return (psbuf
->st_mode
& S_IWUSR
) ? True
: False
;
170 default: /* FILE_READ_DATA|FILE_WRITE_DATA */
172 if ((psbuf
->st_mode
& (S_IWUSR
|S_IRUSR
)) == (S_IWUSR
|S_IRUSR
)) {
180 /* now for ACL checks */
182 return can_access_file_acl(conn
, fname
, access_mask
);
185 /****************************************************************************
186 Userspace check for write access.
187 Note this doesn't take into account share write permissions.
188 ****************************************************************************/
190 bool can_write_to_file(connection_struct
*conn
, const char *fname
, SMB_STRUCT_STAT
*psbuf
)
192 return can_access_file_data(conn
, fname
, psbuf
, FILE_WRITE_DATA
);
195 /****************************************************************************
196 Check for an existing default Windows ACL on a directory.
197 ****************************************************************************/
199 bool directory_has_default_acl(connection_struct
*conn
, const char *fname
)
201 /* returns talloced off tos. */
202 struct security_descriptor
*secdesc
= NULL
;
204 NTSTATUS status
= SMB_VFS_GET_NT_ACL(conn
, fname
,
205 DACL_SECURITY_INFORMATION
, &secdesc
);
207 if (!NT_STATUS_IS_OK(status
) || secdesc
== NULL
) {
211 for (i
= 0; i
< secdesc
->dacl
->num_aces
; i
++) {
212 struct security_ace
*psa
= &secdesc
->dacl
->aces
[i
];
213 if (psa
->flags
& (SEC_ACE_FLAG_OBJECT_INHERIT
|
214 SEC_ACE_FLAG_CONTAINER_INHERIT
)) {
215 TALLOC_FREE(secdesc
);
219 TALLOC_FREE(secdesc
);