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