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"
27 #define DBGC_CLASS DBGC_ACLS
30 * Security descriptor / NT Token level access check function.
32 bool can_access_file_acl(struct connection_struct *conn,
33 const struct smb_filename *smb_fname,
37 uint32_t access_granted;
38 struct security_descriptor *secdesc = NULL;
41 if (get_current_uid(conn) == (uid_t)0) {
42 /* I'm sorry sir, I didn't know you were root... */
46 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
51 if (!NT_STATUS_IS_OK(status)) {
52 DEBUG(5, ("Could not get acl: %s\n", nt_errstr(status)));
57 status = se_access_check(secdesc, get_current_nttok(conn),
58 access_mask, &access_granted);
59 ret = NT_STATUS_IS_OK(status);
61 if (DEBUGLEVEL >= 10) {
62 DEBUG(10,("can_access_file_acl for file %s "
63 "access_mask 0x%x, access_granted 0x%x "
65 smb_fname_str_dbg(smb_fname),
66 (unsigned int)access_mask,
67 (unsigned int)access_granted,
68 ret ? "ALLOWED" : "DENIED" ));
69 NDR_PRINT_DEBUG(security_descriptor, secdesc);
76 /****************************************************************************
77 Actually emulate the in-kernel access checking for delete access. We need
78 this to successfully return ACCESS_DENIED on a file open for delete access.
79 ****************************************************************************/
81 bool can_delete_file_in_directory(connection_struct *conn,
82 struct smb_filename *smb_fname)
84 TALLOC_CTX *ctx = talloc_tos();
86 struct smb_filename *smb_fname_parent = NULL;
90 if (!CAN_WRITE(conn)) {
94 /* Get the parent directory permission mask and owners. */
95 if (!parent_dirname(ctx, smb_fname->base_name, &dname, NULL)) {
99 status = create_synthetic_smb_fname(ctx, dname, NULL, NULL,
101 if (!NT_STATUS_IS_OK(status)) {
106 if(SMB_VFS_STAT(conn, smb_fname_parent) != 0) {
111 /* fast paths first */
113 if (!S_ISDIR(smb_fname_parent->st.st_ex_mode)) {
117 if (get_current_uid(conn) == (uid_t)0) {
118 /* I'm sorry sir, I didn't know you were root... */
124 /* sticky bit means delete only by owner of file or by root or
125 * by owner of directory. */
126 if (smb_fname_parent->st.st_ex_mode & S_ISVTX) {
127 if(SMB_VFS_STAT(conn, smb_fname) != 0) {
128 if (errno == ENOENT) {
129 /* If the file doesn't already exist then
130 * yes we'll be able to delete it. */
134 DEBUG(10,("can_delete_file_in_directory: can't "
136 smb_fname_str_dbg(smb_fname),
143 * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
144 * for bug #3348. Don't assume owning sticky bit
145 * directory means write access allowed.
146 * Fail to delete if we're not the owner of the file,
147 * or the owner of the directory as we have no possible
148 * chance of deleting. Otherwise, go on and check the ACL.
150 if ((get_current_uid(conn) !=
151 smb_fname_parent->st.st_ex_uid) &&
152 (get_current_uid(conn) != smb_fname->st.st_ex_uid)) {
153 DEBUG(10,("can_delete_file_in_directory: not "
154 "owner of file %s or directory %s",
155 smb_fname_str_dbg(smb_fname),
156 smb_fname_str_dbg(smb_fname_parent)));
163 /* now for ACL checks */
166 * There's two ways to get the permission to delete a file: First by
167 * having the DELETE bit on the file itself and second if that does
168 * not help, by the DELETE_CHILD bit on the containing directory.
170 * Here we only check the directory permissions, we will
171 * check the file DELETE permission separately.
174 ret = can_access_file_acl(conn, smb_fname_parent, FILE_DELETE_CHILD);
177 TALLOC_FREE(smb_fname_parent);
181 /****************************************************************************
182 Actually emulate the in-kernel access checking for read/write access. We need
183 this to successfully check for ability to write for dos filetimes.
184 Note this doesn't take into account share write permissions.
185 ****************************************************************************/
187 bool can_access_file_data(connection_struct *conn,
188 const struct smb_filename *smb_fname,
191 if (!(access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))) {
194 access_mask &= (FILE_READ_DATA|FILE_WRITE_DATA);
196 /* some fast paths first */
198 DEBUG(10,("can_access_file_data: requesting 0x%x on file %s\n",
199 (unsigned int)access_mask, smb_fname_str_dbg(smb_fname)));
201 if (get_current_uid(conn) == (uid_t)0) {
202 /* I'm sorry sir, I didn't know you were root... */
206 SMB_ASSERT(VALID_STAT(smb_fname->st));
208 /* Check primary owner access. */
209 if (get_current_uid(conn) == smb_fname->st.st_ex_uid) {
210 switch (access_mask) {
212 return (smb_fname->st.st_ex_mode & S_IRUSR) ?
215 case FILE_WRITE_DATA:
216 return (smb_fname->st.st_ex_mode & S_IWUSR) ?
219 default: /* FILE_READ_DATA|FILE_WRITE_DATA */
221 if ((smb_fname->st.st_ex_mode &
222 (S_IWUSR|S_IRUSR)) ==
231 /* now for ACL checks */
233 return can_access_file_acl(conn, smb_fname, access_mask);
236 /****************************************************************************
237 Userspace check for write access.
238 Note this doesn't take into account share write permissions.
239 ****************************************************************************/
241 bool can_write_to_file(connection_struct *conn,
242 const struct smb_filename *smb_fname)
244 return can_access_file_data(conn, smb_fname, FILE_WRITE_DATA);
247 /****************************************************************************
248 Check for an existing default Windows ACL on a directory.
249 ****************************************************************************/
251 bool directory_has_default_acl(connection_struct *conn, const char *fname)
253 /* returns talloced off tos. */
254 struct security_descriptor *secdesc = NULL;
256 NTSTATUS status = SMB_VFS_GET_NT_ACL(conn, fname,
257 SECINFO_DACL, &secdesc);
259 if (!NT_STATUS_IS_OK(status) || secdesc == NULL) {
263 for (i = 0; i < secdesc->dacl->num_aces; i++) {
264 struct security_ace *psa = &secdesc->dacl->aces[i];
265 if (psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|
266 SEC_ACE_FLAG_CONTAINER_INHERIT)) {
267 TALLOC_FREE(secdesc);
271 TALLOC_FREE(secdesc);