s3/smbd: update some more DEBUG macros in smbd_smb2_create_send
[nivanova/samba-autobuild/.git] / source3 / smbd / file_access.c
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.
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "../libcli/security/security.h"
24 #include "../librpc/gen_ndr/ndr_security.h"
25 #include "smbd/smbd.h"
26
27 #undef  DBGC_CLASS
28 #define DBGC_CLASS DBGC_ACLS
29
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 ****************************************************************************/
34
35 bool can_delete_file_in_directory(connection_struct *conn,
36                                   const struct smb_filename *smb_fname)
37 {
38         TALLOC_CTX *ctx = talloc_tos();
39         char *dname = NULL;
40         struct smb_filename *smb_fname_parent;
41         bool ret;
42
43         if (!CAN_WRITE(conn)) {
44                 return False;
45         }
46
47         if (!lp_acl_check_permissions(SNUM(conn))) {
48                 /* This option means don't check. */
49                 return true;
50         }
51
52         /* Get the parent directory permission mask and owners. */
53         if (!parent_dirname(ctx, smb_fname->base_name, &dname, NULL)) {
54                 return False;
55         }
56
57         smb_fname_parent = synthetic_smb_fname(ctx,
58                                 dname,
59                                 NULL,
60                                 NULL,
61                                 smb_fname->flags);
62         if (smb_fname_parent == NULL) {
63                 ret = false;
64                 goto out;
65         }
66
67         if(SMB_VFS_STAT(conn, smb_fname_parent) != 0) {
68                 ret = false;
69                 goto out;
70         }
71
72         /* fast paths first */
73
74         if (!S_ISDIR(smb_fname_parent->st.st_ex_mode)) {
75                 ret = false;
76                 goto out;
77         }
78         if (get_current_uid(conn) == (uid_t)0) {
79                 /* I'm sorry sir, I didn't know you were root... */
80                 ret = true;
81                 goto out;
82         }
83
84 #ifdef S_ISVTX
85         /* sticky bit means delete only by owner of file or by root or
86          * by owner of directory. */
87         if (smb_fname_parent->st.st_ex_mode & S_ISVTX) {
88                 if (!VALID_STAT(smb_fname->st)) {
89                         /* If the file doesn't already exist then
90                          * yes we'll be able to delete it. */
91                         ret = true;
92                         goto out;
93                 }
94
95                 /*
96                  * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
97                  * for bug #3348. Don't assume owning sticky bit
98                  * directory means write access allowed.
99                  * Fail to delete if we're not the owner of the file,
100                  * or the owner of the directory as we have no possible
101                  * chance of deleting. Otherwise, go on and check the ACL.
102                  */
103                 if ((get_current_uid(conn) !=
104                         smb_fname_parent->st.st_ex_uid) &&
105                     (get_current_uid(conn) != smb_fname->st.st_ex_uid)) {
106                         DEBUG(10,("can_delete_file_in_directory: not "
107                                   "owner of file %s or directory %s",
108                                   smb_fname_str_dbg(smb_fname),
109                                   smb_fname_str_dbg(smb_fname_parent)));
110                         ret = false;
111                         goto out;
112                 }
113         }
114 #endif
115
116         /* now for ACL checks */
117
118         /*
119          * There's two ways to get the permission to delete a file: First by
120          * having the DELETE bit on the file itself and second if that does
121          * not help, by the DELETE_CHILD bit on the containing directory.
122          *
123          * Here we only check the directory permissions, we will
124          * check the file DELETE permission separately.
125          */
126
127         ret = NT_STATUS_IS_OK(smbd_check_access_rights(conn,
128                                 smb_fname_parent,
129                                 false,
130                                 FILE_DELETE_CHILD));
131  out:
132         TALLOC_FREE(dname);
133         TALLOC_FREE(smb_fname_parent);
134         return ret;
135 }
136
137 /****************************************************************************
138  Userspace check for write access.
139 ****************************************************************************/
140
141 bool can_write_to_file(connection_struct *conn,
142                        const struct smb_filename *smb_fname)
143 {
144         return NT_STATUS_IS_OK(smbd_check_access_rights(conn,
145                                 smb_fname,
146                                 false,
147                                 FILE_WRITE_DATA));
148 }
149
150 /****************************************************************************
151  Check for an existing default Windows ACL on a directory.
152 ****************************************************************************/
153
154 bool directory_has_default_acl(connection_struct *conn, const char *fname)
155 {
156         struct security_descriptor *secdesc = NULL;
157         unsigned int i;
158         NTSTATUS status;
159         struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
160                                                 fname,
161                                                 NULL,
162                                                 NULL,
163                                                 0);
164
165         if (smb_fname == NULL) {
166                 return false;
167         }
168
169         status = SMB_VFS_GET_NT_ACL(conn, smb_fname,
170                                              SECINFO_DACL, talloc_tos(),
171                                              &secdesc);
172
173         if (!NT_STATUS_IS_OK(status) ||
174                         secdesc == NULL ||
175                         secdesc->dacl == NULL) {
176                 TALLOC_FREE(secdesc);
177                 return false;
178         }
179
180         for (i = 0; i < secdesc->dacl->num_aces; i++) {
181                 struct security_ace *psa = &secdesc->dacl->aces[i];
182                 if (psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|
183                                 SEC_ACE_FLAG_CONTAINER_INHERIT)) {
184                         TALLOC_FREE(secdesc);
185                         return true;
186                 }
187         }
188         TALLOC_FREE(secdesc);
189         return false;
190 }
191
192 /****************************************************************************
193  Check if setting delete on close is allowed on this fsp.
194 ****************************************************************************/
195
196 NTSTATUS can_set_delete_on_close(files_struct *fsp, uint32_t dosmode)
197 {
198         /*
199          * Only allow delete on close for writable files.
200          */
201
202         if ((dosmode & FILE_ATTRIBUTE_READONLY) &&
203             !lp_delete_readonly(SNUM(fsp->conn))) {
204                 DEBUG(10,("can_set_delete_on_close: file %s delete on close "
205                           "flag set but file attribute is readonly.\n",
206                           fsp_str_dbg(fsp)));
207                 return NT_STATUS_CANNOT_DELETE;
208         }
209
210         /*
211          * Only allow delete on close for writable shares.
212          */
213
214         if (!CAN_WRITE(fsp->conn)) {
215                 DEBUG(10,("can_set_delete_on_close: file %s delete on "
216                           "close flag set but write access denied on share.\n",
217                           fsp_str_dbg(fsp)));
218                 return NT_STATUS_ACCESS_DENIED;
219         }
220
221         /*
222          * Only allow delete on close for files/directories opened with delete
223          * intent.
224          */
225
226         if (!(fsp->access_mask & DELETE_ACCESS)) {
227                 DEBUG(10,("can_set_delete_on_close: file %s delete on "
228                           "close flag set but delete access denied.\n",
229                           fsp_str_dbg(fsp)));
230                 return NT_STATUS_ACCESS_DENIED;
231         }
232
233         /* Don't allow delete on close for non-empty directories. */
234         if (fsp->is_directory) {
235                 SMB_ASSERT(!is_ntfs_stream_smb_fname(fsp->fsp_name));
236
237                 /* Or the root of a share. */
238                 if (ISDOT(fsp->fsp_name->base_name)) {
239                         DEBUG(10,("can_set_delete_on_close: can't set delete on "
240                                   "close for the root of a share.\n"));
241                         return NT_STATUS_ACCESS_DENIED;
242                 }
243
244                 return can_delete_directory_fsp(fsp);
245         }
246
247         return NT_STATUS_OK;
248 }