Revert "source3/smbd"
[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
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                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
86         }
87
88         in_data_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
89         in_data_buffer.length = in_data_length;
90
91         status = smbd_smb2_request_verify_creditcharge(req, in_data_length);
92         if (!NT_STATUS_IS_OK(status)) {
93                 return smbd_smb2_request_error(req, status);
94         }
95
96         if (req->compat_chain_fsp) {
97                 /* skip check */
98         } else if (in_file_id_persistent != in_file_id_volatile) {
99                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
100         }
101
102         subreq = smbd_smb2_write_send(req,
103                                       req->sconn->ev_ctx,
104                                       req,
105                                       in_smbpid,
106                                       in_file_id_volatile,
107                                       in_data_buffer,
108                                       in_offset,
109                                       in_flags);
110         if (subreq == NULL) {
111                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
112         }
113         tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
114
115         return smbd_smb2_request_pending_queue(req, subreq, 500);
116 }
117
118 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
119 {
120         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
121                                         struct smbd_smb2_request);
122         DATA_BLOB outbody;
123         DATA_BLOB outdyn;
124         uint32_t out_count = 0;
125         NTSTATUS status;
126         NTSTATUS error; /* transport error */
127
128         status = smbd_smb2_write_recv(subreq, &out_count);
129         TALLOC_FREE(subreq);
130         if (!NT_STATUS_IS_OK(status)) {
131                 error = smbd_smb2_request_error(req, status);
132                 if (!NT_STATUS_IS_OK(error)) {
133                         smbd_server_connection_terminate(req->sconn,
134                                                          nt_errstr(error));
135                         return;
136                 }
137                 return;
138         }
139
140         outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
141         if (outbody.data == NULL) {
142                 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
143                 if (!NT_STATUS_IS_OK(error)) {
144                         smbd_server_connection_terminate(req->sconn,
145                                                          nt_errstr(error));
146                         return;
147                 }
148                 return;
149         }
150
151         SSVAL(outbody.data, 0x00, 0x10 + 1);    /* struct size */
152         SSVAL(outbody.data, 0x02, 0);           /* reserved */
153         SIVAL(outbody.data, 0x04, out_count);   /* count */
154         SIVAL(outbody.data, 0x08, 0);           /* remaining */
155         SSVAL(outbody.data, 0x0C, 0);           /* write channel info offset */
156         SSVAL(outbody.data, 0x0E, 0);           /* write channel info length */
157
158         outdyn = data_blob_const(NULL, 0);
159
160         error = smbd_smb2_request_done(req, outbody, &outdyn);
161         if (!NT_STATUS_IS_OK(error)) {
162                 smbd_server_connection_terminate(req->sconn, nt_errstr(error));
163                 return;
164         }
165 }
166
167 struct smbd_smb2_write_state {
168         struct smbd_smb2_request *smb2req;
169         struct smb_request *smbreq;
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         if (nwritten == -1) {
187                 status = map_nt_error_from_unix(err);
188
189                 DEBUG(2, ("smb2_write failed: fnum=[%d/%s] "
190                           "length=%lu offset=%lu nwritten=-1: %s\n",
191                           fsp->fnum,
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: fnum=[%d/%s] "
201                 "length=%lu offset=%lu wrote=%lu\n",
202                 fsp->fnum,
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         status = sync_file(fsp->conn, fsp, state->write_through);
215         if (!NT_STATUS_IS_OK(status)) {
216                 DEBUG(5,("smb2: sync_file for %s returned %s\n",
217                         fsp_str_dbg(fsp),
218                         nt_errstr(status)));
219                 return status;
220         }
221
222         state->out_count = nwritten;
223
224         return NT_STATUS_OK;
225 }
226
227 static bool smbd_smb2_write_cancel(struct tevent_req *req)
228 {
229         struct smbd_smb2_write_state *state =
230                 tevent_req_data(req,
231                 struct smbd_smb2_write_state);
232
233         state->smb2req->cancelled = true;
234
235         return cancel_smb2_aio(state->smbreq);
236 }
237
238 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
239                                                struct tevent_context *ev,
240                                                struct smbd_smb2_request *smb2req,
241                                                uint32_t in_smbpid,
242                                                uint64_t in_file_id_volatile,
243                                                DATA_BLOB in_data,
244                                                uint64_t in_offset,
245                                                uint32_t in_flags)
246 {
247         NTSTATUS status;
248         struct tevent_req *req = NULL;
249         struct smbd_smb2_write_state *state = NULL;
250         struct smb_request *smbreq = NULL;
251         connection_struct *conn = smb2req->tcon->compat_conn;
252         files_struct *fsp = NULL;
253         ssize_t nwritten;
254         struct lock_struct lock;
255
256         req = tevent_req_create(mem_ctx, &state,
257                                 struct smbd_smb2_write_state);
258         if (req == NULL) {
259                 return NULL;
260         }
261         state->smb2req = smb2req;
262         if (in_flags & SMB2_WRITEFLAG_WRITE_THROUGH) {
263                 state->write_through = true;
264         }
265         state->in_length = in_data.length;
266         state->out_count = 0;
267
268         DEBUG(10,("smbd_smb2_write: file_id[0x%016llX]\n",
269                   (unsigned long long)in_file_id_volatile));
270
271         smbreq = smbd_smb2_fake_smb_request(smb2req);
272         if (tevent_req_nomem(smbreq, req)) {
273                 return tevent_req_post(req, ev);
274         }
275         state->smbreq = smbreq;
276
277         fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
278         if (fsp == NULL) {
279                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
280                 return tevent_req_post(req, ev);
281         }
282         if (conn != fsp->conn) {
283                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
284                 return tevent_req_post(req, ev);
285         }
286         if (smb2req->session->vuid != fsp->vuid) {
287                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
288                 return tevent_req_post(req, ev);
289         }
290
291         state->fsp = fsp;
292
293         if (IS_IPC(smbreq->conn)) {
294                 struct tevent_req *subreq = NULL;
295
296                 if (!fsp_is_np(fsp)) {
297                         tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
298                         return tevent_req_post(req, ev);
299                 }
300
301                 subreq = np_write_send(state, ev,
302                                        fsp->fake_file_handle,
303                                        in_data.data,
304                                        in_data.length);
305                 if (tevent_req_nomem(subreq, req)) {
306                         return tevent_req_post(req, ev);
307                 }
308                 tevent_req_set_callback(subreq,
309                                         smbd_smb2_write_pipe_done,
310                                         req);
311                 return req;
312         }
313
314         if (!CHECK_WRITE(fsp)) {
315                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
316                 return tevent_req_post(req, ev);
317         }
318
319         /* Try and do an asynchronous write. */
320         status = schedule_aio_smb2_write(conn,
321                                         smbreq,
322                                         fsp,
323                                         in_offset,
324                                         in_data,
325                                         state->write_through);
326
327         if (NT_STATUS_IS_OK(status)) {
328                 /*
329                  * Doing an async write, allow this
330                  * request to be canceled
331                  */
332                 tevent_req_set_cancel_fn(req, smbd_smb2_write_cancel);
333                 return req;
334         }
335
336         if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
337                 /* Real error in setting up aio. Fail. */
338                 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
339                 return tevent_req_post(req, ev);
340         }
341
342         /* Fallback to synchronous. */
343         init_strict_lock_struct(fsp,
344                                 in_file_id_volatile,
345                                 in_offset,
346                                 in_data.length,
347                                 WRITE_LOCK,
348                                 &lock);
349
350         if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
351                 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
352                 return tevent_req_post(req, ev);
353         }
354
355         nwritten = write_file(smbreq, fsp,
356                               (const char *)in_data.data,
357                               in_offset,
358                               in_data.length);
359
360         status = smb2_write_complete(req, nwritten, errno);
361
362         SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
363
364         DEBUG(10,("smb2: write on "
365                 "file %s, offset %.0f, requested %u, written = %u\n",
366                 fsp_str_dbg(fsp),
367                 (double)in_offset,
368                 (unsigned int)in_data.length,
369                 (unsigned int)nwritten ));
370
371         if (!NT_STATUS_IS_OK(status)) {
372                 tevent_req_nterror(req, status);
373         } else {
374                 /* Success. */
375                 tevent_req_done(req);
376         }
377
378         return tevent_req_post(req, ev);
379 }
380
381 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
382 {
383         struct tevent_req *req = tevent_req_callback_data(subreq,
384                                  struct tevent_req);
385         struct smbd_smb2_write_state *state = tevent_req_data(req,
386                                               struct smbd_smb2_write_state);
387         NTSTATUS status;
388         ssize_t nwritten = -1;
389
390         status = np_write_recv(subreq, &nwritten);
391         TALLOC_FREE(subreq);
392         if (!NT_STATUS_IS_OK(status)) {
393                 tevent_req_nterror(req, status);
394                 return;
395         }
396
397         if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
398                 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
399                 return;
400         }
401
402         state->out_count = nwritten;
403
404         tevent_req_done(req);
405 }
406
407 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
408                                      uint32_t *out_count)
409 {
410         NTSTATUS status;
411         struct smbd_smb2_write_state *state = tevent_req_data(req,
412                                               struct smbd_smb2_write_state);
413
414         if (tevent_req_is_nterror(req, &status)) {
415                 tevent_req_received(req);
416                 return status;
417         }
418
419         *out_count = state->out_count;
420
421         tevent_req_received(req);
422         return NT_STATUS_OK;
423 }