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