s3: Fix Coverity ID 2195: NO_EFFECT
[Samba.git] / source3 / smbd / file_access.c
blob28d028fcb1a1766bd21a530b41ebe2f08103f400
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"
22 #include "../libcli/security/security.h"
23 #include "../librpc/gen_ndr/ndr_security.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_ACLS
28 /**
29 * Security descriptor / NT Token level access check function.
31 bool can_access_file_acl(struct connection_struct *conn,
32 const struct smb_filename *smb_fname,
33 uint32_t access_mask)
35 NTSTATUS status;
36 uint32_t access_granted;
37 struct security_descriptor *secdesc = NULL;
38 bool ret;
40 if (get_current_uid(conn) == (uid_t)0) {
41 /* I'm sorry sir, I didn't know you were root... */
42 return true;
45 status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name,
46 (SECINFO_OWNER |
47 SECINFO_GROUP |
48 SECINFO_DACL),
49 &secdesc);
50 if (!NT_STATUS_IS_OK(status)) {
51 DEBUG(5, ("Could not get acl: %s\n", nt_errstr(status)));
52 ret = false;
53 goto out;
56 status = se_access_check(secdesc, get_current_nttok(conn),
57 access_mask, &access_granted);
58 ret = NT_STATUS_IS_OK(status);
60 if (DEBUGLEVEL >= 10) {
61 DEBUG(10,("can_access_file_acl for file %s "
62 "access_mask 0x%x, access_granted 0x%x "
63 "access %s\n",
64 smb_fname_str_dbg(smb_fname),
65 (unsigned int)access_mask,
66 (unsigned int)access_granted,
67 ret ? "ALLOWED" : "DENIED" ));
68 NDR_PRINT_DEBUG(security_descriptor, secdesc);
70 out:
71 TALLOC_FREE(secdesc);
72 return ret;
75 /****************************************************************************
76 Actually emulate the in-kernel access checking for delete access. We need
77 this to successfully return ACCESS_DENIED on a file open for delete access.
78 ****************************************************************************/
80 bool can_delete_file_in_directory(connection_struct *conn,
81 struct smb_filename *smb_fname)
83 TALLOC_CTX *ctx = talloc_tos();
84 char *dname = NULL;
85 struct smb_filename *smb_fname_parent = NULL;
86 NTSTATUS status;
87 bool ret;
89 if (!CAN_WRITE(conn)) {
90 return False;
93 /* Get the parent directory permission mask and owners. */
94 if (!parent_dirname(ctx, smb_fname->base_name, &dname, NULL)) {
95 return False;
98 status = create_synthetic_smb_fname(ctx, dname, NULL, NULL,
99 &smb_fname_parent);
100 if (!NT_STATUS_IS_OK(status)) {
101 ret = false;
102 goto out;
105 if(SMB_VFS_STAT(conn, smb_fname_parent) != 0) {
106 ret = false;
107 goto out;
110 /* fast paths first */
112 if (!S_ISDIR(smb_fname_parent->st.st_ex_mode)) {
113 ret = false;
114 goto out;
116 if (get_current_uid(conn) == (uid_t)0) {
117 /* I'm sorry sir, I didn't know you were root... */
118 ret = true;
119 goto out;
122 #ifdef S_ISVTX
123 /* sticky bit means delete only by owner of file or by root or
124 * by owner of directory. */
125 if (smb_fname_parent->st.st_ex_mode & S_ISVTX) {
126 if(SMB_VFS_STAT(conn, smb_fname) != 0) {
127 if (errno == ENOENT) {
128 /* If the file doesn't already exist then
129 * yes we'll be able to delete it. */
130 ret = true;
131 goto out;
133 DEBUG(10,("can_delete_file_in_directory: can't "
134 "stat file %s (%s)",
135 smb_fname_str_dbg(smb_fname),
136 strerror(errno) ));
137 ret = false;
138 goto out;
142 * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
143 * for bug #3348. Don't assume owning sticky bit
144 * directory means write access allowed.
145 * Fail to delete if we're not the owner of the file,
146 * or the owner of the directory as we have no possible
147 * chance of deleting. Otherwise, go on and check the ACL.
149 if ((get_current_uid(conn) !=
150 smb_fname_parent->st.st_ex_uid) &&
151 (get_current_uid(conn) != smb_fname->st.st_ex_uid)) {
152 DEBUG(10,("can_delete_file_in_directory: not "
153 "owner of file %s or directory %s",
154 smb_fname_str_dbg(smb_fname),
155 smb_fname_str_dbg(smb_fname_parent)));
156 ret = false;
157 goto out;
160 #endif
162 /* now for ACL checks */
165 * There's two ways to get the permission to delete a file: First by
166 * having the DELETE bit on the file itself and second if that does
167 * not help, by the DELETE_CHILD bit on the containing directory.
169 * Here we only check the directory permissions, we will
170 * check the file DELETE permission separately.
173 ret = can_access_file_acl(conn, smb_fname_parent, FILE_DELETE_CHILD);
174 out:
175 TALLOC_FREE(dname);
176 TALLOC_FREE(smb_fname_parent);
177 return ret;
180 /****************************************************************************
181 Actually emulate the in-kernel access checking for read/write access. We need
182 this to successfully check for ability to write for dos filetimes.
183 Note this doesn't take into account share write permissions.
184 ****************************************************************************/
186 bool can_access_file_data(connection_struct *conn,
187 const struct smb_filename *smb_fname,
188 uint32 access_mask)
190 if (!(access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))) {
191 return False;
193 access_mask &= (FILE_READ_DATA|FILE_WRITE_DATA);
195 /* some fast paths first */
197 DEBUG(10,("can_access_file_data: requesting 0x%x on file %s\n",
198 (unsigned int)access_mask, smb_fname_str_dbg(smb_fname)));
200 if (get_current_uid(conn) == (uid_t)0) {
201 /* I'm sorry sir, I didn't know you were root... */
202 return True;
205 SMB_ASSERT(VALID_STAT(smb_fname->st));
207 /* Check primary owner access. */
208 if (get_current_uid(conn) == smb_fname->st.st_ex_uid) {
209 switch (access_mask) {
210 case FILE_READ_DATA:
211 return (smb_fname->st.st_ex_mode & S_IRUSR) ?
212 True : False;
214 case FILE_WRITE_DATA:
215 return (smb_fname->st.st_ex_mode & S_IWUSR) ?
216 True : False;
218 default: /* FILE_READ_DATA|FILE_WRITE_DATA */
220 if ((smb_fname->st.st_ex_mode &
221 (S_IWUSR|S_IRUSR)) ==
222 (S_IWUSR|S_IRUSR)) {
223 return True;
224 } else {
225 return False;
230 /* now for ACL checks */
232 return can_access_file_acl(conn, smb_fname, access_mask);
235 /****************************************************************************
236 Userspace check for write access.
237 Note this doesn't take into account share write permissions.
238 ****************************************************************************/
240 bool can_write_to_file(connection_struct *conn,
241 const struct smb_filename *smb_fname)
243 return can_access_file_data(conn, smb_fname, FILE_WRITE_DATA);
246 /****************************************************************************
247 Check for an existing default Windows ACL on a directory.
248 ****************************************************************************/
250 bool directory_has_default_acl(connection_struct *conn, const char *fname)
252 /* returns talloced off tos. */
253 struct security_descriptor *secdesc = NULL;
254 unsigned int i;
255 NTSTATUS status = SMB_VFS_GET_NT_ACL(conn, fname,
256 SECINFO_DACL, &secdesc);
258 if (!NT_STATUS_IS_OK(status) || secdesc == NULL) {
259 return false;
262 for (i = 0; i < secdesc->dacl->num_aces; i++) {
263 struct security_ace *psa = &secdesc->dacl->aces[i];
264 if (psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|
265 SEC_ACE_FLAG_CONTAINER_INHERIT)) {
266 TALLOC_FREE(secdesc);
267 return true;
270 TALLOC_FREE(secdesc);
271 return false;