can_write_to_file() does now take share permissions into account. Fix comment.
[kai/samba.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 = NULL;
41         NTSTATUS status;
42         bool ret;
43
44         if (!CAN_WRITE(conn)) {
45                 return False;
46         }
47
48         if (!lp_acl_check_permissions(SNUM(conn))) {
49                 /* This option means don't check. */
50                 return true;
51         }
52
53         /* Get the parent directory permission mask and owners. */
54         if (!parent_dirname(ctx, smb_fname->base_name, &dname, NULL)) {
55                 return False;
56         }
57
58         status = create_synthetic_smb_fname(ctx, dname, NULL, NULL,
59                                             &smb_fname_parent);
60         if (!NT_STATUS_IS_OK(status)) {
61                 ret = false;
62                 goto out;
63         }
64
65         if(SMB_VFS_STAT(conn, smb_fname_parent) != 0) {
66                 ret = false;
67                 goto out;
68         }
69
70         /* fast paths first */
71
72         if (!S_ISDIR(smb_fname_parent->st.st_ex_mode)) {
73                 ret = false;
74                 goto out;
75         }
76         if (get_current_uid(conn) == (uid_t)0) {
77                 /* I'm sorry sir, I didn't know you were root... */
78                 ret = true;
79                 goto out;
80         }
81
82 #ifdef S_ISVTX
83         /* sticky bit means delete only by owner of file or by root or
84          * by owner of directory. */
85         if (smb_fname_parent->st.st_ex_mode & S_ISVTX) {
86                 if (!VALID_STAT(smb_fname->st)) {
87                         /* If the file doesn't already exist then
88                          * yes we'll be able to delete it. */
89                         ret = true;
90                         goto out;
91                 }
92
93                 /*
94                  * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
95                  * for bug #3348. Don't assume owning sticky bit
96                  * directory means write access allowed.
97                  * Fail to delete if we're not the owner of the file,
98                  * or the owner of the directory as we have no possible
99                  * chance of deleting. Otherwise, go on and check the ACL.
100                  */
101                 if ((get_current_uid(conn) !=
102                         smb_fname_parent->st.st_ex_uid) &&
103                     (get_current_uid(conn) != smb_fname->st.st_ex_uid)) {
104                         DEBUG(10,("can_delete_file_in_directory: not "
105                                   "owner of file %s or directory %s",
106                                   smb_fname_str_dbg(smb_fname),
107                                   smb_fname_str_dbg(smb_fname_parent)));
108                         ret = false;
109                         goto out;
110                 }
111         }
112 #endif
113
114         /* now for ACL checks */
115
116         /*
117          * There's two ways to get the permission to delete a file: First by
118          * having the DELETE bit on the file itself and second if that does
119          * not help, by the DELETE_CHILD bit on the containing directory.
120          *
121          * Here we only check the directory permissions, we will
122          * check the file DELETE permission separately.
123          */
124
125         ret = NT_STATUS_IS_OK(smbd_check_access_rights(conn,
126                                 smb_fname_parent,
127                                 FILE_DELETE_CHILD));
128  out:
129         TALLOC_FREE(dname);
130         TALLOC_FREE(smb_fname_parent);
131         return ret;
132 }
133
134 /****************************************************************************
135  Userspace check for write access.
136 ****************************************************************************/
137
138 bool can_write_to_file(connection_struct *conn,
139                        const struct smb_filename *smb_fname)
140 {
141         return NT_STATUS_IS_OK(smbd_check_access_rights(conn,
142                                 smb_fname,
143                                 FILE_WRITE_DATA));
144 }
145
146 /****************************************************************************
147  Check for an existing default Windows ACL on a directory.
148 ****************************************************************************/
149
150 bool directory_has_default_acl(connection_struct *conn, const char *fname)
151 {
152         /* returns talloced off tos. */
153         struct security_descriptor *secdesc = NULL;
154         unsigned int i;
155         NTSTATUS status = SMB_VFS_GET_NT_ACL(conn, fname,
156                                 SECINFO_DACL, &secdesc);
157
158         if (!NT_STATUS_IS_OK(status) || secdesc == NULL) {
159                 return false;
160         }
161
162         for (i = 0; i < secdesc->dacl->num_aces; i++) {
163                 struct security_ace *psa = &secdesc->dacl->aces[i];
164                 if (psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|
165                                 SEC_ACE_FLAG_CONTAINER_INHERIT)) {
166                         TALLOC_FREE(secdesc);
167                         return true;
168                 }
169         }
170         TALLOC_FREE(secdesc);
171         return false;
172 }