a8ebac873707a0f5db2befcaa1fc9d308c4de2f9
[nivanova/samba-autobuild/.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/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../lib/util/tevent_ntstatus.h"
26 #include "rpc_server/srv_pipe_hnd.h"
27
28 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
29                                                struct tevent_context *ev,
30                                                struct smbd_smb2_request *smb2req,
31                                                struct files_struct *in_fsp,
32                                                DATA_BLOB in_data,
33                                                uint64_t in_offset,
34                                                uint32_t in_flags);
35 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
36                                      uint32_t *out_count);
37
38 static void smbd_smb2_request_write_done(struct tevent_req *subreq);
39 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
40 {
41         struct smbXsrv_connection *xconn = req->xconn;
42         NTSTATUS status;
43         const uint8_t *inbody;
44         uint16_t in_data_offset;
45         uint32_t in_data_length;
46         DATA_BLOB in_data_buffer;
47         uint64_t in_offset;
48         uint64_t in_file_id_persistent;
49         uint64_t in_file_id_volatile;
50         struct files_struct *in_fsp;
51         uint32_t in_flags;
52         size_t in_dyn_len = 0;
53         uint8_t *in_dyn_ptr = NULL;
54         struct tevent_req *subreq;
55
56         status = smbd_smb2_request_verify_sizes(req, 0x31);
57         if (!NT_STATUS_IS_OK(status)) {
58                 return smbd_smb2_request_error(req, status);
59         }
60         inbody = SMBD_SMB2_IN_BODY_PTR(req);
61
62         in_data_offset          = SVAL(inbody, 0x02);
63         in_data_length          = IVAL(inbody, 0x04);
64         in_offset               = BVAL(inbody, 0x08);
65         in_file_id_persistent   = BVAL(inbody, 0x10);
66         in_file_id_volatile     = BVAL(inbody, 0x18);
67         in_flags                = IVAL(inbody, 0x2C);
68
69         if (in_data_offset != (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
70                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
71         }
72
73         if (req->smb1req != NULL && req->smb1req->unread_bytes > 0) {
74                 in_dyn_ptr = NULL;
75                 in_dyn_len = req->smb1req->unread_bytes;
76         } else {
77                 in_dyn_ptr = SMBD_SMB2_IN_DYN_PTR(req);
78                 in_dyn_len = SMBD_SMB2_IN_DYN_LEN(req);
79         }
80
81         if (in_data_length > in_dyn_len) {
82                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
83         }
84
85         /* check the max write size */
86         if (in_data_length > xconn->smb2.server.max_write) {
87                 DEBUG(2,("smbd_smb2_request_process_write : "
88                         "client ignored max write :%s: 0x%08X: 0x%08X\n",
89                         __location__, in_data_length, xconn->smb2.server.max_write));
90                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
91         }
92
93         /*
94          * Note: that in_dyn_ptr is NULL for the recvfile case.
95          */
96         in_data_buffer.data = in_dyn_ptr;
97         in_data_buffer.length = in_data_length;
98
99         status = smbd_smb2_request_verify_creditcharge(req, in_data_length);
100         if (!NT_STATUS_IS_OK(status)) {
101                 return smbd_smb2_request_error(req, status);
102         }
103
104         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
105         if (in_fsp == NULL) {
106                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
107         }
108
109         subreq = smbd_smb2_write_send(req, req->sconn->ev_ctx,
110                                       req, in_fsp,
111                                       in_data_buffer,
112                                       in_offset,
113                                       in_flags);
114         if (subreq == NULL) {
115                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
116         }
117         tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
118
119         return smbd_smb2_request_pending_queue(req, subreq, 500);
120 }
121
122 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
123 {
124         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
125                                         struct smbd_smb2_request);
126         DATA_BLOB outbody;
127         DATA_BLOB outdyn;
128         uint32_t out_count = 0;
129         NTSTATUS status;
130         NTSTATUS error; /* transport error */
131
132         status = smbd_smb2_write_recv(subreq, &out_count);
133         TALLOC_FREE(subreq);
134         if (!NT_STATUS_IS_OK(status)) {
135                 error = smbd_smb2_request_error(req, status);
136                 if (!NT_STATUS_IS_OK(error)) {
137                         smbd_server_connection_terminate(req->xconn,
138                                                          nt_errstr(error));
139                         return;
140                 }
141                 return;
142         }
143
144         outbody = smbd_smb2_generate_outbody(req, 0x10);
145         if (outbody.data == NULL) {
146                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
147                 if (!NT_STATUS_IS_OK(error)) {
148                         smbd_server_connection_terminate(req->xconn,
149                                                          nt_errstr(error));
150                         return;
151                 }
152                 return;
153         }
154
155         SSVAL(outbody.data, 0x00, 0x10 + 1);    /* struct size */
156         SSVAL(outbody.data, 0x02, 0);           /* reserved */
157         SIVAL(outbody.data, 0x04, out_count);   /* count */
158         SIVAL(outbody.data, 0x08, 0);           /* remaining */
159         SSVAL(outbody.data, 0x0C, 0);           /* write channel info offset */
160         SSVAL(outbody.data, 0x0E, 0);           /* write channel info length */
161
162         outdyn = data_blob_const(NULL, 0);
163
164         error = smbd_smb2_request_done(req, outbody, &outdyn);
165         if (!NT_STATUS_IS_OK(error)) {
166                 smbd_server_connection_terminate(req->xconn, nt_errstr(error));
167                 return;
168         }
169 }
170
171 struct smbd_smb2_write_state {
172         struct smbd_smb2_request *smb2req;
173         struct smb_request *smbreq;
174         files_struct *fsp;
175         bool write_through;
176         uint32_t in_length;
177         uint64_t in_offset;
178         uint32_t out_count;
179 };
180
181 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq);
182
183 static NTSTATUS smb2_write_complete_internal(struct tevent_req *req,
184                                              ssize_t nwritten, int err,
185                                              bool do_sync)
186 {
187         NTSTATUS status;
188         struct smbd_smb2_write_state *state = tevent_req_data(req,
189                                         struct smbd_smb2_write_state);
190         files_struct *fsp = state->fsp;
191
192         if (nwritten == -1) {
193                 status = map_nt_error_from_unix(err);
194
195                 DEBUG(2, ("smb2_write failed: %s, file %s, "
196                           "length=%lu offset=%lu nwritten=-1: %s\n",
197                           fsp_fnum_dbg(fsp),
198                           fsp_str_dbg(fsp),
199                           (unsigned long)state->in_length,
200                           (unsigned long)state->in_offset,
201                           nt_errstr(status)));
202
203                 return status;
204         }
205
206         DEBUG(3,("smb2: %s, file %s, "
207                 "length=%lu offset=%lu wrote=%lu\n",
208                 fsp_fnum_dbg(fsp),
209                 fsp_str_dbg(fsp),
210                 (unsigned long)state->in_length,
211                 (unsigned long)state->in_offset,
212                 (unsigned long)nwritten));
213
214         if ((nwritten == 0) && (state->in_length != 0)) {
215                 DEBUG(5,("smb2: write [%s] disk full\n",
216                         fsp_str_dbg(fsp)));
217                 return NT_STATUS_DISK_FULL;
218         }
219
220         if (do_sync) {
221                 status = sync_file(fsp->conn, fsp, state->write_through);
222                 if (!NT_STATUS_IS_OK(status)) {
223                         DEBUG(5,("smb2: sync_file for %s returned %s\n",
224                                  fsp_str_dbg(fsp),
225                                  nt_errstr(status)));
226                         return status;
227                 }
228         }
229
230         state->out_count = nwritten;
231
232         return NT_STATUS_OK;
233 }
234
235 NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err)
236 {
237         return smb2_write_complete_internal(req, nwritten, err, true);
238 }
239
240 NTSTATUS smb2_write_complete_nosync(struct tevent_req *req, ssize_t nwritten,
241                                     int err)
242 {
243         return smb2_write_complete_internal(req, nwritten, err, false);
244 }
245
246
247 static bool smbd_smb2_write_cancel(struct tevent_req *req)
248 {
249         struct smbd_smb2_write_state *state =
250                 tevent_req_data(req,
251                 struct smbd_smb2_write_state);
252
253         return cancel_smb2_aio(state->smbreq);
254 }
255
256 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
257                                                struct tevent_context *ev,
258                                                struct smbd_smb2_request *smb2req,
259                                                struct files_struct *fsp,
260                                                DATA_BLOB in_data,
261                                                uint64_t in_offset,
262                                                uint32_t in_flags)
263 {
264         NTSTATUS status;
265         struct tevent_req *req = NULL;
266         struct smbd_smb2_write_state *state = NULL;
267         struct smb_request *smbreq = NULL;
268         connection_struct *conn = smb2req->tcon->compat;
269         ssize_t nwritten;
270         struct lock_struct lock;
271
272         req = tevent_req_create(mem_ctx, &state,
273                                 struct smbd_smb2_write_state);
274         if (req == NULL) {
275                 return NULL;
276         }
277         state->smb2req = smb2req;
278         if (smb2req->xconn->protocol >= PROTOCOL_SMB3_02) {
279                 if (in_flags & SMB2_WRITEFLAG_WRITE_UNBUFFERED) {
280                         state->write_through = true;
281                 }
282         }
283         if (in_flags & SMB2_WRITEFLAG_WRITE_THROUGH) {
284                 state->write_through = true;
285         }
286         state->in_length = in_data.length;
287         state->out_count = 0;
288
289         DEBUG(10,("smbd_smb2_write: %s - %s\n",
290                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
291
292         smbreq = smbd_smb2_fake_smb_request(smb2req);
293         if (tevent_req_nomem(smbreq, req)) {
294                 return tevent_req_post(req, ev);
295         }
296         state->smbreq = smbreq;
297
298         state->fsp = fsp;
299
300         if (IS_IPC(smbreq->conn)) {
301                 struct tevent_req *subreq = NULL;
302
303                 if (!fsp_is_np(fsp)) {
304                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
305                         return tevent_req_post(req, ev);
306                 }
307
308                 subreq = np_write_send(state, ev,
309                                        fsp->fake_file_handle,
310                                        in_data.data,
311                                        in_data.length);
312                 if (tevent_req_nomem(subreq, req)) {
313                         return tevent_req_post(req, ev);
314                 }
315                 tevent_req_set_callback(subreq,
316                                         smbd_smb2_write_pipe_done,
317                                         req);
318                 return req;
319         }
320
321         if (!CHECK_WRITE(fsp)) {
322                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
323                 return tevent_req_post(req, ev);
324         }
325
326         /* Try and do an asynchronous write. */
327         status = schedule_aio_smb2_write(conn,
328                                         smbreq,
329                                         fsp,
330                                         in_offset,
331                                         in_data,
332                                         state->write_through);
333
334         if (NT_STATUS_IS_OK(status)) {
335                 /*
336                  * Doing an async write, allow this
337                  * request to be canceled
338                  */
339                 tevent_req_set_cancel_fn(req, smbd_smb2_write_cancel);
340                 return req;
341         }
342
343         if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
344                 /* Real error in setting up aio. Fail. */
345                 tevent_req_nterror(req, status);
346                 return tevent_req_post(req, ev);
347         }
348
349         /* Fallback to synchronous. */
350         init_strict_lock_struct(fsp,
351                                 fsp->op->global->open_persistent_id,
352                                 in_offset,
353                                 in_data.length,
354                                 WRITE_LOCK,
355                                 &lock);
356
357         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
358                 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
359                 return tevent_req_post(req, ev);
360         }
361
362         /*
363          * Note: in_data.data is NULL for the recvfile case.
364          */
365         nwritten = write_file(smbreq, fsp,
366                               (const char *)in_data.data,
367                               in_offset,
368                               in_data.length);
369
370         status = smb2_write_complete(req, nwritten, errno);
371
372         DEBUG(10,("smb2: write on "
373                 "file %s, offset %.0f, requested %u, written = %u\n",
374                 fsp_str_dbg(fsp),
375                 (double)in_offset,
376                 (unsigned int)in_data.length,
377                 (unsigned int)nwritten ));
378
379         if (!NT_STATUS_IS_OK(status)) {
380                 tevent_req_nterror(req, status);
381         } else {
382                 /* Success. */
383                 tevent_req_done(req);
384         }
385
386         return tevent_req_post(req, ev);
387 }
388
389 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
390 {
391         struct tevent_req *req = tevent_req_callback_data(subreq,
392                                  struct tevent_req);
393         struct smbd_smb2_write_state *state = tevent_req_data(req,
394                                               struct smbd_smb2_write_state);
395         NTSTATUS status;
396         ssize_t nwritten = -1;
397
398         status = np_write_recv(subreq, &nwritten);
399         TALLOC_FREE(subreq);
400         if (!NT_STATUS_IS_OK(status)) {
401                 NTSTATUS old = status;
402                 status = nt_status_np_pipe(old);
403                 tevent_req_nterror(req, status);
404                 return;
405         }
406
407         if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
408                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
409                 return;
410         }
411
412         state->out_count = nwritten;
413
414         tevent_req_done(req);
415 }
416
417 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
418                                      uint32_t *out_count)
419 {
420         NTSTATUS status;
421         struct smbd_smb2_write_state *state = tevent_req_data(req,
422                                               struct smbd_smb2_write_state);
423
424         if (tevent_req_is_nterror(req, &status)) {
425                 tevent_req_received(req);
426                 return status;
427         }
428
429         *out_count = state->out_count;
430
431         tevent_req_received(req);
432         return NT_STATUS_OK;
433 }