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;
46 if (!VALID_STAT(*psbuf)) {
47 if (SMB_VFS_STAT(conn, fname, psbuf) != 0) {
48 return map_nt_error_from_unix(errno);
52 /* fake a files_struct ptr: */
54 status = open_file_stat(conn, NULL, fname, psbuf, &fsp);
55 /* Perhaps it is a directory */
56 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
57 status = open_directory(conn, NULL, fname, psbuf,
59 FILE_SHARE_READ|FILE_SHARE_WRITE,
62 FILE_ATTRIBUTE_DIRECTORY,
65 if (!NT_STATUS_IS_OK(status)) {
66 DEBUG(3, ("Unable to open file %s: %s\n", fname,
71 secdesc_size = SMB_VFS_GET_NT_ACL(fsp, fname,
72 (OWNER_SECURITY_INFORMATION |
73 GROUP_SECURITY_INFORMATION |
74 DACL_SECURITY_INFORMATION),
76 if (secdesc_size == 0) {
77 DEBUG(5, ("Unable to get NT ACL for file %s\n", fname));
78 return NT_STATUS_ACCESS_DENIED;
81 *psd = talloc_move(mem_ctx, &secdesc);
82 close_file(fsp, NORMAL_CLOSE);
86 static bool can_access_file_acl(struct connection_struct *conn,
87 const char * fname, SMB_STRUCT_STAT *psbuf,
92 uint32_t access_granted;
93 struct security_descriptor *secdesc = NULL;
95 status = conn_get_nt_acl(talloc_tos(), conn, fname, psbuf, &secdesc);
96 if (!NT_STATUS_IS_OK(status)) {
97 DEBUG(5, ("Could not get acl: %s\n", nt_errstr(status)));
101 result = se_access_check(secdesc, current_user.nt_user_token,
102 access_mask, &access_granted, &status);
103 TALLOC_FREE(secdesc);
107 /****************************************************************************
108 Actually emulate the in-kernel access checking for delete access. We need
109 this to successfully return ACCESS_DENIED on a file open for delete access.
110 ****************************************************************************/
112 bool can_delete_file_in_directory(connection_struct *conn, const char *fname)
114 SMB_STRUCT_STAT sbuf;
115 TALLOC_CTX *ctx = talloc_tos();
118 if (!CAN_WRITE(conn)) {
122 /* Get the parent directory permission mask and owners. */
123 if (!parent_dirname_talloc(ctx,
129 if(SMB_VFS_STAT(conn, dname, &sbuf) != 0) {
133 /* fast paths first */
135 if (!S_ISDIR(sbuf.st_mode)) {
138 if (current_user.ut.uid == 0 || conn->admin_user) {
139 /* I'm sorry sir, I didn't know you were root... */
143 /* Check primary owner write access. */
144 if (current_user.ut.uid == sbuf.st_uid) {
145 return (sbuf.st_mode & S_IWUSR) ? True : False;
149 /* sticky bit means delete only by owner or root. */
150 if (sbuf.st_mode & S_ISVTX) {
151 SMB_STRUCT_STAT sbuf_file;
152 if(SMB_VFS_STAT(conn, fname, &sbuf_file) != 0) {
153 if (errno == ENOENT) {
154 /* If the file doesn't already exist then
155 * yes we'll be able to delete it. */
161 * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
162 * for bug #3348. Don't assume owning sticky bit
163 * directory means write access allowed.
165 if (current_user.ut.uid != sbuf_file.st_uid) {
171 /* now for ACL checks */
173 return can_access_file_acl(conn, dname, &sbuf, FILE_WRITE_DATA);
176 /****************************************************************************
177 Actually emulate the in-kernel access checking for read/write access. We need
178 this to successfully check for ability to write for dos filetimes.
179 Note this doesn't take into account share write permissions.
180 ****************************************************************************/
182 bool can_access_file(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf, uint32 access_mask)
184 if (!(access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))) {
187 access_mask &= (FILE_READ_DATA|FILE_WRITE_DATA);
189 /* some fast paths first */
191 DEBUG(10,("can_access_file: requesting 0x%x on file %s\n",
192 (unsigned int)access_mask, fname ));
194 if (current_user.ut.uid == 0 || conn->admin_user) {
195 /* I'm sorry sir, I didn't know you were root... */
199 if (!VALID_STAT(*psbuf)) {
200 /* Get the file permission mask and owners. */
201 if(SMB_VFS_STAT(conn, fname, psbuf) != 0) {
206 /* Check primary owner access. */
207 if (current_user.ut.uid == psbuf->st_uid) {
208 switch (access_mask) {
210 return (psbuf->st_mode & S_IRUSR) ? True : False;
212 case FILE_WRITE_DATA:
213 return (psbuf->st_mode & S_IWUSR) ? True : False;
215 default: /* FILE_READ_DATA|FILE_WRITE_DATA */
217 if ((psbuf->st_mode & (S_IWUSR|S_IRUSR)) == (S_IWUSR|S_IRUSR)) {
225 /* now for ACL checks */
227 return can_access_file_acl(conn, fname, psbuf, access_mask);
230 /****************************************************************************
231 Userspace check for write access.
232 Note this doesn't take into account share write permissions.
233 ****************************************************************************/
235 bool can_write_to_file(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf)
237 return can_access_file(conn, fname, psbuf, FILE_WRITE_DATA);