Only MULTIPLE-UNLOCK test left to fix !
[samba.git] / source3 / smbd / smb2_write.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
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 #include "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
24
25 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
26                                                struct tevent_context *ev,
27                                                struct smbd_smb2_request *smb2req,
28                                                uint32_t in_smbpid,
29                                                uint64_t in_file_id_volatile,
30                                                DATA_BLOB in_data,
31                                                uint64_t in_offset,
32                                                uint32_t in_flags);
33 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
34                                      uint32_t *out_count);
35
36 static void smbd_smb2_request_write_done(struct tevent_req *subreq);
37 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
38 {
39         const uint8_t *inhdr;
40         const uint8_t *inbody;
41         int i = req->current_idx;
42         size_t expected_body_size = 0x31;
43         size_t body_size;
44         uint32_t in_smbpid;
45         uint16_t in_data_offset;
46         uint32_t in_data_length;
47         DATA_BLOB in_data_buffer;
48         uint64_t in_offset;
49         uint64_t in_file_id_persistent;
50         uint64_t in_file_id_volatile;
51         uint32_t in_flags;
52         struct tevent_req *subreq;
53
54         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
55         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
56                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
57         }
58
59         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
60
61         body_size = SVAL(inbody, 0x00);
62         if (body_size != expected_body_size) {
63                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
64         }
65
66         in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
67
68         in_data_offset          = SVAL(inbody, 0x02);
69         in_data_length          = IVAL(inbody, 0x04);
70         in_offset               = BVAL(inbody, 0x08);
71         in_file_id_persistent   = BVAL(inbody, 0x10);
72         in_file_id_volatile     = BVAL(inbody, 0x18);
73         in_flags                = IVAL(inbody, 0x2C);
74
75         if (in_data_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
76                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
77         }
78
79         if (in_data_length > req->in.vector[i+2].iov_len) {
80                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
81         }
82
83         /* check the max write size */
84         if (in_data_length > lp_smb2_max_write()) {
85                 /* This is a warning. */
86                 DEBUG(2,("smbd_smb2_request_process_write : "
87                         "client ignored max write :%s: 0x%08X: 0x%08X\n",
88                         __location__, in_data_length, lp_smb2_max_write()));
89 #if 0
90                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
91 #endif
92         }
93
94         in_data_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
95         in_data_buffer.length = in_data_length;
96
97         if (req->compat_chain_fsp) {
98                 /* skip check */
99         } else if (in_file_id_persistent != 0) {
100                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
101         }
102
103         subreq = smbd_smb2_write_send(req,
104                                       req->sconn->smb2.event_ctx,
105                                       req,
106                                       in_smbpid,
107                                       in_file_id_volatile,
108                                       in_data_buffer,
109                                       in_offset,
110                                       in_flags);
111         if (subreq == NULL) {
112                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
113         }
114         tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
115
116         return smbd_smb2_request_pending_queue(req, subreq);
117 }
118
119 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
120 {
121         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
122                                         struct smbd_smb2_request);
123         int i = req->current_idx;
124         uint8_t *outhdr;
125         DATA_BLOB outbody;
126         DATA_BLOB outdyn;
127         uint32_t out_count = 0;
128         NTSTATUS status;
129         NTSTATUS error; /* transport error */
130
131         status = smbd_smb2_write_recv(subreq, &out_count);
132         TALLOC_FREE(subreq);
133         if (!NT_STATUS_IS_OK(status)) {
134                 error = smbd_smb2_request_error(req, status);
135                 if (!NT_STATUS_IS_OK(error)) {
136                         smbd_server_connection_terminate(req->sconn,
137                                                          nt_errstr(error));
138                         return;
139                 }
140                 return;
141         }
142
143         outhdr = (uint8_t *)req->out.vector[i].iov_base;
144
145         outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
146         if (outbody.data == NULL) {
147                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
148                 if (!NT_STATUS_IS_OK(error)) {
149                         smbd_server_connection_terminate(req->sconn,
150                                                          nt_errstr(error));
151                         return;
152                 }
153                 return;
154         }
155
156         SSVAL(outbody.data, 0x00, 0x10 + 1);    /* struct size */
157         SSVAL(outbody.data, 0x02, 0);           /* reserved */
158         SIVAL(outbody.data, 0x04, out_count);   /* count */
159         SIVAL(outbody.data, 0x08, 0);           /* remaining */
160         SSVAL(outbody.data, 0x0C, 0);           /* write channel info offset */
161         SSVAL(outbody.data, 0x0E, 0);           /* write channel info length */
162
163         outdyn = data_blob_const(NULL, 0);
164
165         error = smbd_smb2_request_done(req, outbody, &outdyn);
166         if (!NT_STATUS_IS_OK(error)) {
167                 smbd_server_connection_terminate(req->sconn, nt_errstr(error));
168                 return;
169         }
170 }
171
172 struct smbd_smb2_write_state {
173         struct smbd_smb2_request *smb2req;
174         uint32_t in_length;
175         uint32_t out_count;
176 };
177
178 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq);
179
180 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
181                                                struct tevent_context *ev,
182                                                struct smbd_smb2_request *smb2req,
183                                                uint32_t in_smbpid,
184                                                uint64_t in_file_id_volatile,
185                                                DATA_BLOB in_data,
186                                                uint64_t in_offset,
187                                                uint32_t in_flags)
188 {
189         NTSTATUS status;
190         struct tevent_req *req;
191         struct smbd_smb2_write_state *state;
192         struct smb_request *smbreq;
193         connection_struct *conn = smb2req->tcon->compat_conn;
194         files_struct *fsp;
195         ssize_t nwritten;
196         bool write_through = false;
197         struct lock_struct lock;
198
199         req = tevent_req_create(mem_ctx, &state,
200                                 struct smbd_smb2_write_state);
201         if (req == NULL) {
202                 return NULL;
203         }
204         state->smb2req = smb2req;
205         state->in_length = in_data.length;
206         state->out_count = 0;
207
208         DEBUG(10,("smbd_smb2_write: file_id[0x%016llX]\n",
209                   (unsigned long long)in_file_id_volatile));
210
211         smbreq = smbd_smb2_fake_smb_request(smb2req);
212         if (tevent_req_nomem(smbreq, req)) {
213                 return tevent_req_post(req, ev);
214         }
215
216         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
217         if (fsp == NULL) {
218                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
219                 return tevent_req_post(req, ev);
220         }
221         if (conn != fsp->conn) {
222                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
223                 return tevent_req_post(req, ev);
224         }
225         if (smb2req->session->vuid != fsp->vuid) {
226                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
227                 return tevent_req_post(req, ev);
228         }
229
230         if (IS_IPC(smbreq->conn)) {
231                 struct tevent_req *subreq;
232
233                 if (!fsp_is_np(fsp)) {
234                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
235                         return tevent_req_post(req, ev);
236                 }
237
238                 subreq = np_write_send(state, smbd_event_context(),
239                                        fsp->fake_file_handle,
240                                        in_data.data,
241                                        in_data.length);
242                 if (tevent_req_nomem(subreq, req)) {
243                         return tevent_req_post(req, ev);
244                 }
245                 tevent_req_set_callback(subreq,
246                                         smbd_smb2_write_pipe_done,
247                                         req);
248                 return req;
249         }
250
251         if (!CHECK_WRITE(fsp)) {
252                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
253                 return tevent_req_post(req, ev);
254         }
255
256         init_strict_lock_struct(fsp,
257                                 in_file_id_volatile,
258                                 in_offset,
259                                 in_data.length,
260                                 WRITE_LOCK,
261                                 &lock);
262
263         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
264                 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
265                 return tevent_req_post(req, ev);
266         }
267
268         nwritten = write_file(smbreq, fsp,
269                               (const char *)in_data.data,
270                               in_offset,
271                               in_data.length);
272
273
274         DEBUG(10,("smbd_smb2_write: file %s handle [0x%016llX] offset=%llu "
275                 "len=%llu returned %lld\n",
276                 fsp_str_dbg(fsp),
277                 (unsigned long long)in_file_id_volatile,
278                 (unsigned long long)in_offset,
279                 (unsigned long long)in_data.length,
280                 (long long)nwritten));
281
282         if (((nwritten == 0) && (in_data.length != 0)) || (nwritten < 0)) {
283                 DEBUG(5,("smbd_smb2_write: write_file[%s] disk full\n",
284                          fsp_str_dbg(fsp)));
285                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
286                 tevent_req_nterror(req, NT_STATUS_DISK_FULL);
287                 return tevent_req_post(req, ev);
288         }
289
290         DEBUG(3,("smbd_smb2_write: fnum=[%d/%s] length=%d offset=%d wrote=%d\n",
291                 fsp->fnum, fsp_str_dbg(fsp), (int)in_data.length,
292                 (int)in_offset, (int)nwritten));
293
294         if (in_flags & 0x00000001) {
295                 write_through = true;
296         }
297
298         status = sync_file(conn, fsp, write_through);
299         if (!NT_STATUS_IS_OK(status)) {
300                 DEBUG(5,("smbd_smb2_write: sync_file for %s returned %s\n",
301                         fsp_str_dbg(fsp), nt_errstr(status)));
302                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
303                 tevent_req_nterror(req, status);
304                 return tevent_req_post(req, ev);
305         }
306
307         SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
308
309         state->out_count = nwritten;
310
311         tevent_req_done(req);
312         return tevent_req_post(req, ev);
313 }
314
315 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
316 {
317         struct tevent_req *req = tevent_req_callback_data(subreq,
318                                  struct tevent_req);
319         struct smbd_smb2_write_state *state = tevent_req_data(req,
320                                               struct smbd_smb2_write_state);
321         NTSTATUS status;
322         ssize_t nwritten = -1;
323
324         status = np_write_recv(subreq, &nwritten);
325         TALLOC_FREE(subreq);
326         if (!NT_STATUS_IS_OK(status)) {
327                 tevent_req_nterror(req, status);
328                 return;
329         }
330
331         if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
332                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
333                 return;
334         }
335
336         state->out_count = nwritten;
337
338         tevent_req_done(req);
339 }
340
341 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
342                                      uint32_t *out_count)
343 {
344         NTSTATUS status;
345         struct smbd_smb2_write_state *state = tevent_req_data(req,
346                                               struct smbd_smb2_write_state);
347
348         if (tevent_req_is_nterror(req, &status)) {
349                 tevent_req_received(req);
350                 return status;
351         }
352
353         *out_count = state->out_count;
354
355         tevent_req_received(req);
356         return NT_STATUS_OK;
357 }