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/>.
23 extern struct current_user current_user;
26 #define DBGC_CLASS DBGC_ACLS
28 /****************************************************************************
29 Helper function that gets a security descriptor by connection and
31 NOTE: This is transitional, in the sense that SMB_VFS_GET_NT_ACL really
32 should *not* get a files_struct pointer but a connection_struct ptr
33 (automatic by the vfs handle) and the file name and _use_ that!
34 ****************************************************************************/
35 static NTSTATUS conn_get_nt_acl(TALLOC_CTX *mem_ctx,
36 struct connection_struct *conn,
38 SMB_STRUCT_STAT *psbuf,
39 struct security_descriptor **psd)
42 struct files_struct *fsp = NULL;
43 struct security_descriptor *secdesc = NULL;
45 if (!VALID_STAT(*psbuf)) {
46 if (SMB_VFS_STAT(conn, fname, psbuf) != 0) {
47 return map_nt_error_from_unix(errno);
51 /* fake a files_struct ptr: */
53 if (S_ISDIR(psbuf->st_mode)) {
54 status = open_directory(conn, NULL, fname, psbuf,
56 FILE_SHARE_READ|FILE_SHARE_WRITE,
59 FILE_ATTRIBUTE_DIRECTORY,
63 status = open_file_stat(conn, NULL, fname, psbuf, &fsp);
66 if (!NT_STATUS_IS_OK(status)) {
67 DEBUG(3, ("Unable to open file %s: %s\n", fname,
72 status = SMB_VFS_GET_NT_ACL(fsp, fname,
73 (OWNER_SECURITY_INFORMATION |
74 GROUP_SECURITY_INFORMATION |
75 DACL_SECURITY_INFORMATION),
77 if (!NT_STATUS_IS_OK(status)) {
78 DEBUG(5, ("Unable to get NT ACL for file %s\n", fname));
82 *psd = talloc_move(mem_ctx, &secdesc);
85 close_file(fsp, NORMAL_CLOSE);
89 static bool can_access_file_acl(struct connection_struct *conn,
90 const char * fname, SMB_STRUCT_STAT *psbuf,
95 uint32_t access_granted;
96 struct security_descriptor *secdesc = NULL;
98 status = conn_get_nt_acl(talloc_tos(), conn, fname, psbuf, &secdesc);
99 if (!NT_STATUS_IS_OK(status)) {
100 DEBUG(5, ("Could not get acl: %s\n", nt_errstr(status)));
104 result = se_access_check(secdesc, current_user.nt_user_token,
105 access_mask, &access_granted, &status);
106 TALLOC_FREE(secdesc);
110 /****************************************************************************
111 Actually emulate the in-kernel access checking for delete access. We need
112 this to successfully return ACCESS_DENIED on a file open for delete access.
113 ****************************************************************************/
115 bool can_delete_file_in_directory(connection_struct *conn, const char *fname)
117 SMB_STRUCT_STAT sbuf;
118 TALLOC_CTX *ctx = talloc_tos();
121 if (!CAN_WRITE(conn)) {
125 /* Get the parent directory permission mask and owners. */
126 if (!parent_dirname_talloc(ctx,
132 if(SMB_VFS_STAT(conn, dname, &sbuf) != 0) {
136 /* fast paths first */
138 if (!S_ISDIR(sbuf.st_mode)) {
141 if (current_user.ut.uid == 0 || conn->admin_user) {
142 /* I'm sorry sir, I didn't know you were root... */
146 /* Check primary owner write access. */
147 if (current_user.ut.uid == sbuf.st_uid) {
148 return (sbuf.st_mode & S_IWUSR) ? True : False;
152 /* sticky bit means delete only by owner or root. */
153 if (sbuf.st_mode & S_ISVTX) {
154 SMB_STRUCT_STAT sbuf_file;
155 if(SMB_VFS_STAT(conn, fname, &sbuf_file) != 0) {
156 if (errno == ENOENT) {
157 /* If the file doesn't already exist then
158 * yes we'll be able to delete it. */
164 * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
165 * for bug #3348. Don't assume owning sticky bit
166 * directory means write access allowed.
168 if (current_user.ut.uid != sbuf_file.st_uid) {
174 /* now for ACL checks */
176 return can_access_file_acl(conn, dname, &sbuf, FILE_WRITE_DATA);
179 /****************************************************************************
180 Actually emulate the in-kernel access checking for read/write access. We need
181 this to successfully check for ability to write for dos filetimes.
182 Note this doesn't take into account share write permissions.
183 ****************************************************************************/
185 bool can_access_file(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf, uint32 access_mask)
187 if (!(access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))) {
190 access_mask &= (FILE_READ_DATA|FILE_WRITE_DATA);
192 /* some fast paths first */
194 DEBUG(10,("can_access_file: requesting 0x%x on file %s\n",
195 (unsigned int)access_mask, fname ));
197 if (current_user.ut.uid == 0 || conn->admin_user) {
198 /* I'm sorry sir, I didn't know you were root... */
202 if (!VALID_STAT(*psbuf)) {
203 /* Get the file permission mask and owners. */
204 if(SMB_VFS_STAT(conn, fname, psbuf) != 0) {
209 /* Check primary owner access. */
210 if (current_user.ut.uid == psbuf->st_uid) {
211 switch (access_mask) {
213 return (psbuf->st_mode & S_IRUSR) ? True : False;
215 case FILE_WRITE_DATA:
216 return (psbuf->st_mode & S_IWUSR) ? True : False;
218 default: /* FILE_READ_DATA|FILE_WRITE_DATA */
220 if ((psbuf->st_mode & (S_IWUSR|S_IRUSR)) == (S_IWUSR|S_IRUSR)) {
228 /* now for ACL checks */
230 return can_access_file_acl(conn, fname, psbuf, 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, const char *fname, SMB_STRUCT_STAT *psbuf)
240 return can_access_file(conn, fname, psbuf, FILE_WRITE_DATA);