Merge branch 'master' of ssh://jht@git.samba.org/data/git/samba
[ira/wip.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
23 #undef  DBGC_CLASS
24 #define DBGC_CLASS DBGC_ACLS
25
26 /**
27  * Security descriptor / NT Token level access check function.
28  */
29 bool can_access_file_acl(struct connection_struct *conn,
30                          const struct smb_filename *smb_fname,
31                          uint32_t access_mask)
32 {
33         NTSTATUS status;
34         uint32_t access_granted;
35         struct security_descriptor *secdesc = NULL;
36         bool ret;
37
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;
41         }
42
43         status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
44                                     (OWNER_SECURITY_INFORMATION |
45                                      GROUP_SECURITY_INFORMATION |
46                                      DACL_SECURITY_INFORMATION),
47                                     &secdesc);
48         if (!NT_STATUS_IS_OK(status)) {
49                 DEBUG(5, ("Could not get acl: %s\n", nt_errstr(status)));
50                 ret = false;
51                 goto out;
52         }
53
54         status = se_access_check(secdesc, conn->server_info->ptok,
55                                  access_mask, &access_granted);
56         ret = NT_STATUS_IS_OK(status);
57  out:
58         TALLOC_FREE(secdesc);
59         return ret;
60 }
61
62 /****************************************************************************
63  Actually emulate the in-kernel access checking for delete access. We need
64  this to successfully return ACCESS_DENIED on a file open for delete access.
65 ****************************************************************************/
66
67 bool can_delete_file_in_directory(connection_struct *conn,
68                                   struct smb_filename *smb_fname)
69 {
70         TALLOC_CTX *ctx = talloc_tos();
71         char *dname = NULL;
72         struct smb_filename *smb_fname_parent = NULL;
73         NTSTATUS status;
74         bool ret;
75
76         if (!CAN_WRITE(conn)) {
77                 return False;
78         }
79
80         /* Get the parent directory permission mask and owners. */
81         if (!parent_dirname(ctx, smb_fname->base_name, &dname, NULL)) {
82                 return False;
83         }
84
85         status = create_synthetic_smb_fname(ctx, dname, NULL, NULL,
86                                             &smb_fname_parent);
87         if (!NT_STATUS_IS_OK(status)) {
88                 ret = false;
89                 goto out;
90         }
91
92         if(SMB_VFS_STAT(conn, smb_fname_parent) != 0) {
93                 ret = false;
94                 goto out;
95         }
96
97         /* fast paths first */
98
99         if (!S_ISDIR(smb_fname_parent->st.st_ex_mode)) {
100                 ret = false;
101                 goto out;
102         }
103         if (conn->server_info->utok.uid == 0 || conn->admin_user) {
104                 /* I'm sorry sir, I didn't know you were root... */
105                 ret = true;
106                 goto out;
107         }
108
109 #ifdef S_ISVTX
110         /* sticky bit means delete only by owner of file or by root or
111          * by owner of directory. */
112         if (smb_fname_parent->st.st_ex_mode & S_ISVTX) {
113                 if(SMB_VFS_STAT(conn, smb_fname) != 0) {
114                         if (errno == ENOENT) {
115                                 /* If the file doesn't already exist then
116                                  * yes we'll be able to delete it. */
117                                 ret = true;
118                                 goto out;
119                         }
120                         DEBUG(10,("can_delete_file_in_directory: can't "
121                                   "stat file %s (%s)",
122                                   smb_fname_str_dbg(smb_fname),
123                                   strerror(errno) ));
124                         ret = false;
125                         goto out;
126                 }
127
128                 /*
129                  * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
130                  * for bug #3348. Don't assume owning sticky bit
131                  * directory means write access allowed.
132                  * Fail to delete if we're not the owner of the file,
133                  * or the owner of the directory as we have no possible
134                  * chance of deleting. Otherwise, go on and check the ACL.
135                  */
136                 if ((conn->server_info->utok.uid !=
137                         smb_fname_parent->st.st_ex_uid) &&
138                     (conn->server_info->utok.uid != smb_fname->st.st_ex_uid)) {
139                         DEBUG(10,("can_delete_file_in_directory: not "
140                                   "owner of file %s or directory %s",
141                                   smb_fname_str_dbg(smb_fname),
142                                   smb_fname_str_dbg(smb_fname_parent)));
143                         ret = false;
144                         goto out;
145                 }
146         }
147 #endif
148
149         /* now for ACL checks */
150
151         /*
152          * There's two ways to get the permission to delete a file: First by
153          * having the DELETE bit on the file itself and second if that does
154          * not help, by the DELETE_CHILD bit on the containing directory.
155          *
156          * Here we only check the directory permissions, we will
157          * check the file DELETE permission separately.
158          */
159
160         ret = can_access_file_acl(conn, smb_fname_parent, FILE_DELETE_CHILD);
161  out:
162         TALLOC_FREE(dname);
163         TALLOC_FREE(smb_fname_parent);
164         return ret;
165 }
166
167 /****************************************************************************
168  Actually emulate the in-kernel access checking for read/write access. We need
169  this to successfully check for ability to write for dos filetimes.
170  Note this doesn't take into account share write permissions.
171 ****************************************************************************/
172
173 bool can_access_file_data(connection_struct *conn,
174                           const struct smb_filename *smb_fname,
175                           uint32 access_mask)
176 {
177         if (!(access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))) {
178                 return False;
179         }
180         access_mask &= (FILE_READ_DATA|FILE_WRITE_DATA);
181
182         /* some fast paths first */
183
184         DEBUG(10,("can_access_file_data: requesting 0x%x on file %s\n",
185                   (unsigned int)access_mask, smb_fname_str_dbg(smb_fname)));
186
187         if (conn->server_info->utok.uid == 0 || conn->admin_user) {
188                 /* I'm sorry sir, I didn't know you were root... */
189                 return True;
190         }
191
192         SMB_ASSERT(VALID_STAT(smb_fname->st));
193
194         /* Check primary owner access. */
195         if (conn->server_info->utok.uid == smb_fname->st.st_ex_uid) {
196                 switch (access_mask) {
197                         case FILE_READ_DATA:
198                                 return (smb_fname->st.st_ex_mode & S_IRUSR) ?
199                                     True : False;
200
201                         case FILE_WRITE_DATA:
202                                 return (smb_fname->st.st_ex_mode & S_IWUSR) ?
203                                     True : False;
204
205                         default: /* FILE_READ_DATA|FILE_WRITE_DATA */
206
207                                 if ((smb_fname->st.st_ex_mode &
208                                         (S_IWUSR|S_IRUSR)) ==
209                                     (S_IWUSR|S_IRUSR)) {
210                                         return True;
211                                 } else {
212                                         return False;
213                                 }
214                 }
215         }
216
217         /* now for ACL checks */
218
219         return can_access_file_acl(conn, smb_fname, access_mask);
220 }
221
222 /****************************************************************************
223  Userspace check for write access.
224  Note this doesn't take into account share write permissions.
225 ****************************************************************************/
226
227 bool can_write_to_file(connection_struct *conn,
228                        const struct smb_filename *smb_fname)
229 {
230         return can_access_file_data(conn, smb_fname, FILE_WRITE_DATA);
231 }
232
233 /****************************************************************************
234  Check for an existing default Windows ACL on a directory.
235 ****************************************************************************/
236
237 bool directory_has_default_acl(connection_struct *conn, const char *fname)
238 {
239         /* returns talloced off tos. */
240         struct security_descriptor *secdesc = NULL;
241         unsigned int i;
242         NTSTATUS status = SMB_VFS_GET_NT_ACL(conn, fname,
243                                 DACL_SECURITY_INFORMATION, &secdesc);
244
245         if (!NT_STATUS_IS_OK(status) || secdesc == NULL) {
246                 return false;
247         }
248
249         for (i = 0; i < secdesc->dacl->num_aces; i++) {
250                 struct security_ace *psa = &secdesc->dacl->aces[i];
251                 if (psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|
252                                 SEC_ACE_FLAG_CONTAINER_INHERIT)) {
253                         TALLOC_FREE(secdesc);
254                         return true;
255                 }
256         }
257         TALLOC_FREE(secdesc);
258         return false;
259 }