smbd: replace CHECK_WRITE() macro with calls to check_any_access_fsp()
[Samba.git] / source3 / smbd / smb2_write.c
blob2675997b0fdcccf7de8f3f786ac9de8e4b37f66d
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
5 Copyright (C) Stefan Metzmacher 2009
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.
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.
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/>.
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 "libcli/security/security.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_SMB2
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);
42 static void smbd_smb2_request_write_done(struct tevent_req *subreq);
43 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
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;
60 status = smbd_smb2_request_verify_sizes(req, 0x31);
61 if (!NT_STATUS_IS_OK(status)) {
62 return smbd_smb2_request_error(req, status);
64 inbody = SMBD_SMB2_IN_BODY_PTR(req);
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);
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);
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);
85 if (in_data_length > in_dyn_len) {
86 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
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);
98 * Note: that in_dyn_ptr is NULL for the recvfile case.
100 in_data_buffer.data = in_dyn_ptr;
101 in_data_buffer.length = in_data_length;
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);
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);
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);
121 tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
123 return smbd_smb2_request_pending_queue(req, subreq, 500);
126 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
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 */
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;
145 return;
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;
156 return;
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 */
166 outdyn = data_blob_const(NULL, 0);
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;
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;
185 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq);
187 static NTSTATUS smb2_write_complete_internal(struct tevent_req *req,
188 ssize_t nwritten, int err,
189 bool do_sync)
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;
196 if (nwritten == -1) {
197 if (err == EOVERFLOW && fsp_is_alternate_stream(fsp)) {
198 status = NT_STATUS_FILE_SYSTEM_LIMITATION;
199 } else {
200 status = map_nt_error_from_unix(err);
203 DEBUG(2, ("smb2_write failed: %s, file %s, "
204 "length=%lu offset=%lu nwritten=-1: %s\n",
205 fsp_fnum_dbg(fsp),
206 fsp_str_dbg(fsp),
207 (unsigned long)state->in_length,
208 (unsigned long)state->in_offset,
209 nt_errstr(status)));
211 return status;
214 DEBUG(3,("smb2: %s, file %s, "
215 "length=%lu offset=%lu wrote=%lu\n",
216 fsp_fnum_dbg(fsp),
217 fsp_str_dbg(fsp),
218 (unsigned long)state->in_length,
219 (unsigned long)state->in_offset,
220 (unsigned long)nwritten));
222 if ((nwritten == 0) && (state->in_length != 0)) {
223 DEBUG(5,("smb2: write [%s] disk full\n",
224 fsp_str_dbg(fsp)));
225 return NT_STATUS_DISK_FULL;
228 if (do_sync) {
229 status = sync_file(fsp->conn, fsp, state->write_through);
230 if (!NT_STATUS_IS_OK(status)) {
231 DEBUG(5,("smb2: sync_file for %s returned %s\n",
232 fsp_str_dbg(fsp),
233 nt_errstr(status)));
234 return status;
238 state->out_count = nwritten;
240 return NT_STATUS_OK;
243 NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err)
245 return smb2_write_complete_internal(req, nwritten, err, true);
248 NTSTATUS smb2_write_complete_nosync(struct tevent_req *req, ssize_t nwritten,
249 int err)
251 return smb2_write_complete_internal(req, nwritten, err, false);
255 static bool smbd_smb2_write_cancel(struct tevent_req *req)
257 struct smbd_smb2_write_state *state =
258 tevent_req_data(req,
259 struct smbd_smb2_write_state);
261 return cancel_smb2_aio(state->smbreq);
264 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
265 struct tevent_context *ev,
266 struct smbd_smb2_request *smb2req,
267 struct files_struct *fsp,
268 DATA_BLOB in_data,
269 uint64_t in_offset,
270 uint32_t in_flags)
272 NTSTATUS status;
273 struct tevent_req *req = NULL;
274 struct smbd_smb2_write_state *state = NULL;
275 struct smb_request *smbreq = NULL;
276 connection_struct *conn = smb2req->tcon->compat;
277 ssize_t nwritten;
278 struct lock_struct lock;
280 req = tevent_req_create(mem_ctx, &state,
281 struct smbd_smb2_write_state);
282 if (req == NULL) {
283 return NULL;
285 state->smb2req = smb2req;
286 if (smb2req->xconn->protocol >= PROTOCOL_SMB3_02) {
287 if (in_flags & SMB2_WRITEFLAG_WRITE_UNBUFFERED) {
288 state->write_through = true;
291 if (in_flags & SMB2_WRITEFLAG_WRITE_THROUGH) {
292 state->write_through = true;
294 state->in_length = in_data.length;
295 state->in_offset = in_offset;
296 state->out_count = 0;
298 DEBUG(10,("smbd_smb2_write: %s - %s\n",
299 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
301 smbreq = smbd_smb2_fake_smb_request(smb2req, fsp);
302 if (tevent_req_nomem(smbreq, req)) {
303 return tevent_req_post(req, ev);
305 state->smbreq = smbreq;
307 state->fsp = fsp;
309 if (IS_IPC(smbreq->conn)) {
310 struct tevent_req *subreq = NULL;
311 bool ok;
313 if (!fsp_is_np(fsp)) {
314 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
315 return tevent_req_post(req, ev);
318 subreq = np_write_send(state, ev,
319 fsp->fake_file_handle,
320 in_data.data,
321 in_data.length);
322 if (tevent_req_nomem(subreq, req)) {
323 return tevent_req_post(req, ev);
325 tevent_req_set_callback(subreq,
326 smbd_smb2_write_pipe_done,
327 req);
330 * Make sure we mark the fsp as having outstanding async
331 * activity so we don't crash on shutdown close.
334 ok = aio_add_req_to_fsp(fsp, req);
335 if (!ok) {
336 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
337 return tevent_req_post(req, ev);
340 return req;
343 status = check_any_access_fsp(fsp, FILE_WRITE_DATA|FILE_APPEND_DATA);
344 if (!NT_STATUS_IS_OK(status)) {
345 tevent_req_nterror(req, status);
346 return tevent_req_post(req, ev);
349 /* Try and do an asynchronous write. */
350 status = schedule_aio_smb2_write(conn,
351 smbreq,
352 fsp,
353 in_offset,
354 in_data,
355 state->write_through);
357 if (NT_STATUS_IS_OK(status)) {
359 * Doing an async write, allow this
360 * request to be canceled
362 tevent_req_set_cancel_fn(req, smbd_smb2_write_cancel);
363 return req;
366 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
367 /* Real error in setting up aio. Fail. */
368 tevent_req_nterror(req, status);
369 return tevent_req_post(req, ev);
372 /* Fallback to synchronous. */
373 init_strict_lock_struct(fsp,
374 fsp->op->global->open_persistent_id,
375 in_offset,
376 in_data.length,
377 WRITE_LOCK,
378 lp_posix_cifsu_locktype(fsp),
379 &lock);
381 if (!SMB_VFS_STRICT_LOCK_CHECK(conn, fsp, &lock)) {
382 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
383 return tevent_req_post(req, ev);
387 * Note: in_data.data is NULL for the recvfile case.
389 nwritten = write_file(smbreq, fsp,
390 (const char *)in_data.data,
391 in_offset,
392 in_data.length);
394 status = smb2_write_complete(req, nwritten, errno);
396 DEBUG(10,("smb2: write on "
397 "file %s, offset %.0f, requested %u, written = %u\n",
398 fsp_str_dbg(fsp),
399 (double)in_offset,
400 (unsigned int)in_data.length,
401 (unsigned int)nwritten ));
403 if (!NT_STATUS_IS_OK(status)) {
404 tevent_req_nterror(req, status);
405 } else {
406 /* Success. */
407 tevent_req_done(req);
410 return tevent_req_post(req, ev);
413 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
415 struct tevent_req *req = tevent_req_callback_data(subreq,
416 struct tevent_req);
417 struct smbd_smb2_write_state *state = tevent_req_data(req,
418 struct smbd_smb2_write_state);
419 NTSTATUS status;
420 ssize_t nwritten = -1;
422 status = np_write_recv(subreq, &nwritten);
423 TALLOC_FREE(subreq);
424 if (!NT_STATUS_IS_OK(status)) {
425 NTSTATUS old = status;
426 status = nt_status_np_pipe(old);
427 tevent_req_nterror(req, status);
428 return;
431 if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
432 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
433 return;
436 state->out_count = nwritten;
438 tevent_req_done(req);
441 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
442 uint32_t *out_count)
444 NTSTATUS status;
445 struct smbd_smb2_write_state *state = tevent_req_data(req,
446 struct smbd_smb2_write_state);
448 if (tevent_req_is_nterror(req, &status)) {
449 tevent_req_received(req);
450 return status;
453 *out_count = state->out_count;
455 tevent_req_received(req);
456 return NT_STATUS_OK;