ECLIPSE + PyDev Project
[Samba/kamenim.git] / source3 / smbd / file_access.c
blob631efce677d8fb1028cd6673e9ae09cf5f967b3f
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 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;
54 status = se_access_check(secdesc, conn->server_info->ptok,
55 access_mask, &access_granted);
56 ret = NT_STATUS_IS_OK(status);
58 if (DEBUGLEVEL >= 10) {
59 DEBUG(10,("can_access_file_acl for file %s "
60 "access_mask 0x%x, access_granted 0x%x "
61 "access %s\n",
62 smb_fname_str_dbg(smb_fname),
63 (unsigned int)access_mask,
64 (unsigned int)access_granted,
65 ret ? "ALLOWED" : "DENIED" ));
66 NDR_PRINT_DEBUG(security_descriptor, secdesc);
68 out:
69 TALLOC_FREE(secdesc);
70 return ret;
73 /****************************************************************************
74 Actually emulate the in-kernel access checking for delete access. We need
75 this to successfully return ACCESS_DENIED on a file open for delete access.
76 ****************************************************************************/
78 bool can_delete_file_in_directory(connection_struct *conn,
79 struct smb_filename *smb_fname)
81 TALLOC_CTX *ctx = talloc_tos();
82 char *dname = NULL;
83 struct smb_filename *smb_fname_parent = NULL;
84 NTSTATUS status;
85 bool ret;
87 if (!CAN_WRITE(conn)) {
88 return False;
91 /* Get the parent directory permission mask and owners. */
92 if (!parent_dirname(ctx, smb_fname->base_name, &dname, NULL)) {
93 return False;
96 status = create_synthetic_smb_fname(ctx, dname, NULL, NULL,
97 &smb_fname_parent);
98 if (!NT_STATUS_IS_OK(status)) {
99 ret = false;
100 goto out;
103 if(SMB_VFS_STAT(conn, smb_fname_parent) != 0) {
104 ret = false;
105 goto out;
108 /* fast paths first */
110 if (!S_ISDIR(smb_fname_parent->st.st_ex_mode)) {
111 ret = false;
112 goto out;
114 if (conn->server_info->utok.uid == 0 || conn->admin_user) {
115 /* I'm sorry sir, I didn't know you were root... */
116 ret = true;
117 goto out;
120 #ifdef S_ISVTX
121 /* sticky bit means delete only by owner of file or by root or
122 * by owner of directory. */
123 if (smb_fname_parent->st.st_ex_mode & S_ISVTX) {
124 if(SMB_VFS_STAT(conn, smb_fname) != 0) {
125 if (errno == ENOENT) {
126 /* If the file doesn't already exist then
127 * yes we'll be able to delete it. */
128 ret = true;
129 goto out;
131 DEBUG(10,("can_delete_file_in_directory: can't "
132 "stat file %s (%s)",
133 smb_fname_str_dbg(smb_fname),
134 strerror(errno) ));
135 ret = false;
136 goto out;
140 * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
141 * for bug #3348. Don't assume owning sticky bit
142 * directory means write access allowed.
143 * Fail to delete if we're not the owner of the file,
144 * or the owner of the directory as we have no possible
145 * chance of deleting. Otherwise, go on and check the ACL.
147 if ((conn->server_info->utok.uid !=
148 smb_fname_parent->st.st_ex_uid) &&
149 (conn->server_info->utok.uid != smb_fname->st.st_ex_uid)) {
150 DEBUG(10,("can_delete_file_in_directory: not "
151 "owner of file %s or directory %s",
152 smb_fname_str_dbg(smb_fname),
153 smb_fname_str_dbg(smb_fname_parent)));
154 ret = false;
155 goto out;
158 #endif
160 /* now for ACL checks */
163 * There's two ways to get the permission to delete a file: First by
164 * having the DELETE bit on the file itself and second if that does
165 * not help, by the DELETE_CHILD bit on the containing directory.
167 * Here we only check the directory permissions, we will
168 * check the file DELETE permission separately.
171 ret = can_access_file_acl(conn, smb_fname_parent, FILE_DELETE_CHILD);
172 out:
173 TALLOC_FREE(dname);
174 TALLOC_FREE(smb_fname_parent);
175 return ret;
178 /****************************************************************************
179 Actually emulate the in-kernel access checking for read/write access. We need
180 this to successfully check for ability to write for dos filetimes.
181 Note this doesn't take into account share write permissions.
182 ****************************************************************************/
184 bool can_access_file_data(connection_struct *conn,
185 const struct smb_filename *smb_fname,
186 uint32 access_mask)
188 if (!(access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))) {
189 return False;
191 access_mask &= (FILE_READ_DATA|FILE_WRITE_DATA);
193 /* some fast paths first */
195 DEBUG(10,("can_access_file_data: requesting 0x%x on file %s\n",
196 (unsigned int)access_mask, smb_fname_str_dbg(smb_fname)));
198 if (conn->server_info->utok.uid == 0 || conn->admin_user) {
199 /* I'm sorry sir, I didn't know you were root... */
200 return True;
203 SMB_ASSERT(VALID_STAT(smb_fname->st));
205 /* Check primary owner access. */
206 if (conn->server_info->utok.uid == smb_fname->st.st_ex_uid) {
207 switch (access_mask) {
208 case FILE_READ_DATA:
209 return (smb_fname->st.st_ex_mode & S_IRUSR) ?
210 True : False;
212 case FILE_WRITE_DATA:
213 return (smb_fname->st.st_ex_mode & S_IWUSR) ?
214 True : False;
216 default: /* FILE_READ_DATA|FILE_WRITE_DATA */
218 if ((smb_fname->st.st_ex_mode &
219 (S_IWUSR|S_IRUSR)) ==
220 (S_IWUSR|S_IRUSR)) {
221 return True;
222 } else {
223 return False;
228 /* now for ACL checks */
230 return can_access_file_acl(conn, smb_fname, access_mask);
233 /****************************************************************************
234 Userspace check for write access.
235 Note this doesn't take into account share write permissions.
236 ****************************************************************************/
238 bool can_write_to_file(connection_struct *conn,
239 const struct smb_filename *smb_fname)
241 return can_access_file_data(conn, smb_fname, FILE_WRITE_DATA);
244 /****************************************************************************
245 Check for an existing default Windows ACL on a directory.
246 ****************************************************************************/
248 bool directory_has_default_acl(connection_struct *conn, const char *fname)
250 /* returns talloced off tos. */
251 struct security_descriptor *secdesc = NULL;
252 unsigned int i;
253 NTSTATUS status = SMB_VFS_GET_NT_ACL(conn, fname,
254 DACL_SECURITY_INFORMATION, &secdesc);
256 if (!NT_STATUS_IS_OK(status) || secdesc == NULL) {
257 return false;
260 for (i = 0; i < secdesc->dacl->num_aces; i++) {
261 struct security_ace *psa = &secdesc->dacl->aces[i];
262 if (psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|
263 SEC_ACE_FLAG_CONTAINER_INHERIT)) {
264 TALLOC_FREE(secdesc);
265 return true;
268 TALLOC_FREE(secdesc);
269 return false;