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