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