Third part of fix for bug #8663 - deleting a symlink fails if the symlink target...
[Samba.git] / source3 / smbd / file_access.c
blob69f89b87428f144630f559006c9bdda011933aa6
1 /*
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/>.
21 #include "includes.h"
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_ACLS
26 /**
27 * Security descriptor / NT Token level access check function.
29 bool can_access_file_acl(struct connection_struct *conn,
30 const struct smb_filename *smb_fname,
31 uint32_t access_mask)
33 NTSTATUS status;
34 uint32_t access_granted;
35 struct security_descriptor *secdesc = NULL;
36 bool ret;
38 if (conn->server_info->utok.uid == 0 || conn->admin_user) {
39 /* I'm sorry sir, I didn't know you were root... */
40 return true;
43 if (access_mask == DELETE_ACCESS &&
44 VALID_STAT(smb_fname->st) &&
45 S_ISLNK(smb_fname->st.st_ex_mode)) {
46 /* We can always delete a symlink. */
47 return true;
50 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
51 (OWNER_SECURITY_INFORMATION |
52 GROUP_SECURITY_INFORMATION |
53 DACL_SECURITY_INFORMATION),
54 &secdesc);
55 if (!NT_STATUS_IS_OK(status)) {
56 DEBUG(5, ("Could not get acl: %s\n", nt_errstr(status)));
57 ret = false;
58 goto out;
61 status = se_access_check(secdesc, conn->server_info->ptok,
62 access_mask, &access_granted);
63 ret = NT_STATUS_IS_OK(status);
64 out:
65 TALLOC_FREE(secdesc);
66 return ret;
69 /****************************************************************************
70 Actually emulate the in-kernel access checking for delete access. We need
71 this to successfully return ACCESS_DENIED on a file open for delete access.
72 ****************************************************************************/
74 bool can_delete_file_in_directory(connection_struct *conn,
75 struct smb_filename *smb_fname)
77 TALLOC_CTX *ctx = talloc_tos();
78 char *dname = NULL;
79 struct smb_filename *smb_fname_parent = NULL;
80 NTSTATUS status;
81 bool ret;
83 if (!CAN_WRITE(conn)) {
84 return False;
87 if (!lp_acl_check_permissions(SNUM(conn))) {
88 /* This option means don't check. */
89 return true;
92 /* Get the parent directory permission mask and owners. */
93 if (!parent_dirname(ctx, smb_fname->base_name, &dname, NULL)) {
94 return False;
97 status = create_synthetic_smb_fname(ctx, dname, NULL, NULL,
98 &smb_fname_parent);
99 if (!NT_STATUS_IS_OK(status)) {
100 ret = false;
101 goto out;
104 if(SMB_VFS_STAT(conn, smb_fname_parent) != 0) {
105 ret = false;
106 goto out;
109 /* fast paths first */
111 if (!S_ISDIR(smb_fname_parent->st.st_ex_mode)) {
112 ret = false;
113 goto out;
115 if (conn->server_info->utok.uid == 0 || conn->admin_user) {
116 /* I'm sorry sir, I didn't know you were root... */
117 ret = true;
118 goto out;
121 #ifdef S_ISVTX
122 /* sticky bit means delete only by owner of file or by root or
123 * by owner of directory. */
124 if (smb_fname_parent->st.st_ex_mode & S_ISVTX) {
125 if (!VALID_STAT(smb_fname->st)) {
126 /* If the file doesn't already exist then
127 * yes we'll be able to delete it. */
128 ret = true;
129 goto out;
133 * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
134 * for bug #3348. Don't assume owning sticky bit
135 * directory means write access allowed.
136 * Fail to delete if we're not the owner of the file,
137 * or the owner of the directory as we have no possible
138 * chance of deleting. Otherwise, go on and check the ACL.
140 if ((conn->server_info->utok.uid !=
141 smb_fname_parent->st.st_ex_uid) &&
142 (conn->server_info->utok.uid != smb_fname->st.st_ex_uid)) {
143 DEBUG(10,("can_delete_file_in_directory: not "
144 "owner of file %s or directory %s",
145 smb_fname_str_dbg(smb_fname),
146 smb_fname_str_dbg(smb_fname_parent)));
147 ret = false;
148 goto out;
151 #endif
153 /* now for ACL checks */
156 * There's two ways to get the permission to delete a file: First by
157 * having the DELETE bit on the file itself and second if that does
158 * not help, by the DELETE_CHILD bit on the containing directory.
160 * Here we only check the directory permissions, we will
161 * check the file DELETE permission separately.
164 ret = can_access_file_acl(conn, smb_fname_parent, FILE_DELETE_CHILD);
165 out:
166 TALLOC_FREE(dname);
167 TALLOC_FREE(smb_fname_parent);
168 return ret;
171 /****************************************************************************
172 Actually emulate the in-kernel access checking for read/write access. We need
173 this to successfully check for ability to write for dos filetimes.
174 Note this doesn't take into account share write permissions.
175 ****************************************************************************/
177 bool can_access_file_data(connection_struct *conn,
178 const struct smb_filename *smb_fname,
179 uint32 access_mask)
181 if (!(access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))) {
182 return False;
184 access_mask &= (FILE_READ_DATA|FILE_WRITE_DATA);
186 /* some fast paths first */
188 DEBUG(10,("can_access_file_data: requesting 0x%x on file %s\n",
189 (unsigned int)access_mask, smb_fname_str_dbg(smb_fname)));
191 if (conn->server_info->utok.uid == 0 || conn->admin_user) {
192 /* I'm sorry sir, I didn't know you were root... */
193 return True;
196 SMB_ASSERT(VALID_STAT(smb_fname->st));
198 /* Check primary owner access. */
199 if (conn->server_info->utok.uid == smb_fname->st.st_ex_uid) {
200 switch (access_mask) {
201 case FILE_READ_DATA:
202 return (smb_fname->st.st_ex_mode & S_IRUSR) ?
203 True : False;
205 case FILE_WRITE_DATA:
206 return (smb_fname->st.st_ex_mode & S_IWUSR) ?
207 True : False;
209 default: /* FILE_READ_DATA|FILE_WRITE_DATA */
211 if ((smb_fname->st.st_ex_mode &
212 (S_IWUSR|S_IRUSR)) ==
213 (S_IWUSR|S_IRUSR)) {
214 return True;
215 } else {
216 return False;
221 /* now for ACL checks */
223 return can_access_file_acl(conn, smb_fname, access_mask);
226 /****************************************************************************
227 Userspace check for write access.
228 Note this doesn't take into account share write permissions.
229 ****************************************************************************/
231 bool can_write_to_file(connection_struct *conn,
232 const struct smb_filename *smb_fname)
234 return can_access_file_data(conn, smb_fname, FILE_WRITE_DATA);
237 /****************************************************************************
238 Check for an existing default Windows ACL on a directory.
239 ****************************************************************************/
241 bool directory_has_default_acl(connection_struct *conn, const char *fname)
243 /* returns talloced off tos. */
244 struct security_descriptor *secdesc = NULL;
245 unsigned int i;
246 NTSTATUS status = SMB_VFS_GET_NT_ACL(conn, fname,
247 DACL_SECURITY_INFORMATION, &secdesc);
249 if (!NT_STATUS_IS_OK(status) || secdesc == NULL) {
250 return false;
253 for (i = 0; i < secdesc->dacl->num_aces; i++) {
254 struct security_ace *psa = &secdesc->dacl->aces[i];
255 if (psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|
256 SEC_ACE_FLAG_CONTAINER_INHERIT)) {
257 TALLOC_FREE(secdesc);
258 return true;
261 TALLOC_FREE(secdesc);
262 return false;