Move some access check functions that are not posix-acl specific
[obnox/samba/samba-obnox.git] / source / 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
23 extern struct current_user current_user;
24
25 #undef  DBGC_CLASS
26 #define DBGC_CLASS DBGC_ACLS
27
28 /****************************************************************************
29  Helper function that gets a security descriptor by connection and
30  file name.
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,
37                                 const char *fname,
38                                 SMB_STRUCT_STAT *psbuf,
39                                 struct security_descriptor **psd)
40 {
41         NTSTATUS status;
42         struct files_struct *fsp = NULL;
43         struct security_descriptor *secdesc = NULL;
44         size_t secdesc_size;
45
46         if (!VALID_STAT(*psbuf)) {
47                 if (SMB_VFS_STAT(conn, fname, psbuf) != 0) {
48                         return map_nt_error_from_unix(errno);
49                 }
50         }
51
52         /* fake a files_struct ptr: */
53
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,
58                                         READ_CONTROL_ACCESS,
59                                         FILE_SHARE_READ|FILE_SHARE_WRITE,
60                                         FILE_OPEN,
61                                         0,
62                                         FILE_ATTRIBUTE_DIRECTORY,
63                                         NULL, &fsp);
64         }
65         if (!NT_STATUS_IS_OK(status)) {
66                 DEBUG(3, ("Unable to open file %s: %s\n", fname,
67                           nt_errstr(status)));
68                 return status;
69         }
70
71         secdesc_size = SMB_VFS_GET_NT_ACL(fsp, fname,
72                                           (OWNER_SECURITY_INFORMATION |
73                                            GROUP_SECURITY_INFORMATION |
74                                            DACL_SECURITY_INFORMATION),
75                                           &secdesc);
76         if (secdesc_size == 0) {
77                 DEBUG(5, ("Unable to get NT ACL for file %s\n", fname));
78                 return NT_STATUS_ACCESS_DENIED;
79         }
80
81         *psd = talloc_move(mem_ctx, &secdesc);
82         close_file(fsp, NORMAL_CLOSE);
83         return NT_STATUS_OK;
84 }
85
86 static bool can_access_file_acl(struct connection_struct *conn,
87                                 const char * fname, SMB_STRUCT_STAT *psbuf,
88                                 uint32_t access_mask)
89 {
90         bool result;
91         NTSTATUS status;
92         uint32_t access_granted;
93         struct security_descriptor *secdesc = NULL;
94
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)));
98                 return false;
99         }
100
101         result = se_access_check(secdesc, current_user.nt_user_token,
102                                  access_mask, &access_granted, &status);
103         TALLOC_FREE(secdesc);
104         return result;
105 }
106
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 ****************************************************************************/
111
112 bool can_delete_file_in_directory(connection_struct *conn, const char *fname)
113 {
114         SMB_STRUCT_STAT sbuf;
115         TALLOC_CTX *ctx = talloc_tos();
116         char *dname = NULL;
117
118         if (!CAN_WRITE(conn)) {
119                 return False;
120         }
121
122         /* Get the parent directory permission mask and owners. */
123         if (!parent_dirname_talloc(ctx,
124                                 fname,
125                                 &dname,
126                                 NULL)) {
127                 return False;
128         }
129         if(SMB_VFS_STAT(conn, dname, &sbuf) != 0) {
130                 return False;
131         }
132
133         /* fast paths first */
134
135         if (!S_ISDIR(sbuf.st_mode)) {
136                 return False;
137         }
138         if (current_user.ut.uid == 0 || conn->admin_user) {
139                 /* I'm sorry sir, I didn't know you were root... */
140                 return True;
141         }
142
143         /* Check primary owner write access. */
144         if (current_user.ut.uid == sbuf.st_uid) {
145                 return (sbuf.st_mode & S_IWUSR) ? True : False;
146         }
147
148 #ifdef S_ISVTX
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. */
156                                 return True;
157                         }
158                         return False;
159                 }
160                 /*
161                  * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
162                  * for bug #3348. Don't assume owning sticky bit
163                  * directory means write access allowed.
164                  */
165                 if (current_user.ut.uid != sbuf_file.st_uid) {
166                         return False;
167                 }
168         }
169 #endif
170
171         /* now for ACL checks */
172
173         return can_access_file_acl(conn, dname, &sbuf, FILE_WRITE_DATA);
174 }
175
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 ****************************************************************************/
181
182 bool can_access_file(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf, uint32 access_mask)
183 {
184         if (!(access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))) {
185                 return False;
186         }
187         access_mask &= (FILE_READ_DATA|FILE_WRITE_DATA);
188
189         /* some fast paths first */
190
191         DEBUG(10,("can_access_file: requesting 0x%x on file %s\n",
192                 (unsigned int)access_mask, fname ));
193
194         if (current_user.ut.uid == 0 || conn->admin_user) {
195                 /* I'm sorry sir, I didn't know you were root... */
196                 return True;
197         }
198
199         if (!VALID_STAT(*psbuf)) {
200                 /* Get the file permission mask and owners. */
201                 if(SMB_VFS_STAT(conn, fname, psbuf) != 0) {
202                         return False;
203                 }
204         }
205
206         /* Check primary owner access. */
207         if (current_user.ut.uid == psbuf->st_uid) {
208                 switch (access_mask) {
209                         case FILE_READ_DATA:
210                                 return (psbuf->st_mode & S_IRUSR) ? True : False;
211
212                         case FILE_WRITE_DATA:
213                                 return (psbuf->st_mode & S_IWUSR) ? True : False;
214
215                         default: /* FILE_READ_DATA|FILE_WRITE_DATA */
216
217                                 if ((psbuf->st_mode & (S_IWUSR|S_IRUSR)) == (S_IWUSR|S_IRUSR)) {
218                                         return True;
219                                 } else {
220                                         return False;
221                                 }
222                 }
223         }
224
225         /* now for ACL checks */
226
227         return can_access_file_acl(conn, fname, psbuf, access_mask);
228 }
229
230 /****************************************************************************
231  Userspace check for write access.
232  Note this doesn't take into account share write permissions.
233 ****************************************************************************/
234
235 bool can_write_to_file(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf)
236 {
237         return can_access_file(conn, fname, psbuf, FILE_WRITE_DATA);
238 }
239