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