s3-smbd: Fix flooding the logs with records we don't find in pcap.
[Samba.git] / source3 / smbd / smb2_write.c
blob5aeed8c74eb3376a3eadf848818d6ca24b707d41
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"
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);
39 static void smbd_smb2_request_write_done(struct tevent_req *subreq);
40 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
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;
57 status = smbd_smb2_request_verify_sizes(req, 0x31);
58 if (!NT_STATUS_IS_OK(status)) {
59 return smbd_smb2_request_error(req, status);
61 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
62 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
64 in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
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 + req->in.vector[i+1].iov_len)) {
74 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
77 if (in_data_length > req->in.vector[i+2].iov_len) {
78 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
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);
89 in_data_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
90 in_data_buffer.length = in_data_length;
92 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
93 if (in_fsp == NULL) {
94 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
97 subreq = smbd_smb2_write_send(req, req->sconn->smb2.event_ctx,
98 req, in_fsp,
99 in_smbpid,
100 in_data_buffer,
101 in_offset,
102 in_flags);
103 if (subreq == NULL) {
104 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
106 tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
108 return smbd_smb2_request_pending_queue(req, subreq);
111 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
113 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
114 struct smbd_smb2_request);
115 int i = req->current_idx;
116 uint8_t *outhdr;
117 DATA_BLOB outbody;
118 DATA_BLOB outdyn;
119 uint32_t out_count = 0;
120 NTSTATUS status;
121 NTSTATUS error; /* transport error */
123 status = smbd_smb2_write_recv(subreq, &out_count);
124 TALLOC_FREE(subreq);
125 if (!NT_STATUS_IS_OK(status)) {
126 error = smbd_smb2_request_error(req, status);
127 if (!NT_STATUS_IS_OK(error)) {
128 smbd_server_connection_terminate(req->sconn,
129 nt_errstr(error));
130 return;
132 return;
135 outhdr = (uint8_t *)req->out.vector[i].iov_base;
137 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
138 if (outbody.data == NULL) {
139 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
140 if (!NT_STATUS_IS_OK(error)) {
141 smbd_server_connection_terminate(req->sconn,
142 nt_errstr(error));
143 return;
145 return;
148 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
149 SSVAL(outbody.data, 0x02, 0); /* reserved */
150 SIVAL(outbody.data, 0x04, out_count); /* count */
151 SIVAL(outbody.data, 0x08, 0); /* remaining */
152 SSVAL(outbody.data, 0x0C, 0); /* write channel info offset */
153 SSVAL(outbody.data, 0x0E, 0); /* write channel info length */
155 outdyn = data_blob_const(NULL, 0);
157 error = smbd_smb2_request_done(req, outbody, &outdyn);
158 if (!NT_STATUS_IS_OK(error)) {
159 smbd_server_connection_terminate(req->sconn, nt_errstr(error));
160 return;
164 struct smbd_smb2_write_state {
165 struct smbd_smb2_request *smb2req;
166 files_struct *fsp;
167 bool write_through;
168 uint32_t in_length;
169 uint64_t in_offset;
170 uint32_t out_count;
173 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq);
175 NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err)
177 NTSTATUS status;
178 struct smbd_smb2_write_state *state = tevent_req_data(req,
179 struct smbd_smb2_write_state);
180 files_struct *fsp = state->fsp;
182 DEBUG(3,("smb2: fnum=[%d/%s] "
183 "length=%lu offset=%lu wrote=%lu\n",
184 fsp->fnum,
185 fsp_str_dbg(fsp),
186 (unsigned long)state->in_length,
187 (unsigned long)state->in_offset,
188 (unsigned long)nwritten));
190 if (nwritten == -1) {
191 return map_nt_error_from_unix(err);
194 if ((nwritten == 0) && (state->in_length != 0)) {
195 DEBUG(5,("smb2: write [%s] disk full\n",
196 fsp_str_dbg(fsp)));
197 return NT_STATUS_DISK_FULL;
200 status = sync_file(fsp->conn, fsp, state->write_through);
201 if (!NT_STATUS_IS_OK(status)) {
202 DEBUG(5,("smb2: sync_file for %s returned %s\n",
203 fsp_str_dbg(fsp),
204 nt_errstr(status)));
205 return status;
208 state->out_count = nwritten;
210 return NT_STATUS_OK;
213 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
214 struct tevent_context *ev,
215 struct smbd_smb2_request *smb2req,
216 struct files_struct *fsp,
217 uint32_t in_smbpid,
218 DATA_BLOB in_data,
219 uint64_t in_offset,
220 uint32_t in_flags)
222 NTSTATUS status;
223 struct tevent_req *req = NULL;
224 struct smbd_smb2_write_state *state = NULL;
225 struct smb_request *smbreq = NULL;
226 connection_struct *conn = smb2req->tcon->compat_conn;
227 ssize_t nwritten;
228 struct lock_struct lock;
230 req = tevent_req_create(mem_ctx, &state,
231 struct smbd_smb2_write_state);
232 if (req == NULL) {
233 return NULL;
235 state->smb2req = smb2req;
236 if (in_flags & 0x00000001) {
237 state->write_through = true;
239 state->in_length = in_data.length;
240 state->out_count = 0;
242 DEBUG(10,("smbd_smb2_write: %s - fnum[%d]\n",
243 fsp_str_dbg(fsp), fsp->fnum));
245 smbreq = smbd_smb2_fake_smb_request(smb2req);
246 if (tevent_req_nomem(smbreq, req)) {
247 return tevent_req_post(req, ev);
250 state->fsp = fsp;
252 if (IS_IPC(smbreq->conn)) {
253 struct tevent_req *subreq = NULL;
255 if (!fsp_is_np(fsp)) {
256 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
257 return tevent_req_post(req, ev);
260 subreq = np_write_send(state, smbd_event_context(),
261 fsp->fake_file_handle,
262 in_data.data,
263 in_data.length);
264 if (tevent_req_nomem(subreq, req)) {
265 return tevent_req_post(req, ev);
267 tevent_req_set_callback(subreq,
268 smbd_smb2_write_pipe_done,
269 req);
270 return req;
273 if (!CHECK_WRITE(fsp)) {
274 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
275 return tevent_req_post(req, ev);
278 /* Try and do an asynchronous write. */
279 status = schedule_aio_smb2_write(conn,
280 smbreq,
281 fsp,
282 in_offset,
283 in_data,
284 state->write_through);
286 if (NT_STATUS_IS_OK(status)) {
288 * Doing an async write. Don't
289 * send a "gone async" message
290 * as we expect this to be less
291 * than the client timeout period.
292 * JRA. FIXME for offline files..
293 * FIXME - add cancel code..
295 smb2req->async = true;
296 return req;
299 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
300 /* Real error in setting up aio. Fail. */
301 tevent_req_nterror(req, status);
302 return tevent_req_post(req, ev);
305 /* Fallback to synchronous. */
306 init_strict_lock_struct(fsp,
307 fsp->fnum,
308 in_offset,
309 in_data.length,
310 WRITE_LOCK,
311 &lock);
313 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
314 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
315 return tevent_req_post(req, ev);
318 nwritten = write_file(smbreq, fsp,
319 (const char *)in_data.data,
320 in_offset,
321 in_data.length);
323 status = smb2_write_complete(req, nwritten, errno);
325 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
327 DEBUG(10,("smb2: write on "
328 "file %s, offset %.0f, requested %u, written = %u\n",
329 fsp_str_dbg(fsp),
330 (double)in_offset,
331 (unsigned int)in_data.length,
332 (unsigned int)nwritten ));
334 if (!NT_STATUS_IS_OK(status)) {
335 tevent_req_nterror(req, status);
336 } else {
337 /* Success. */
338 tevent_req_done(req);
341 return tevent_req_post(req, ev);
344 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
346 struct tevent_req *req = tevent_req_callback_data(subreq,
347 struct tevent_req);
348 struct smbd_smb2_write_state *state = tevent_req_data(req,
349 struct smbd_smb2_write_state);
350 NTSTATUS status;
351 ssize_t nwritten = -1;
353 status = np_write_recv(subreq, &nwritten);
354 TALLOC_FREE(subreq);
355 if (!NT_STATUS_IS_OK(status)) {
356 tevent_req_nterror(req, status);
357 return;
360 if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
361 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
362 return;
365 state->out_count = nwritten;
367 tevent_req_done(req);
370 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
371 uint32_t *out_count)
373 NTSTATUS status;
374 struct smbd_smb2_write_state *state = tevent_req_data(req,
375 struct smbd_smb2_write_state);
377 if (tevent_req_is_nterror(req, &status)) {
378 tevent_req_received(req);
379 return status;
382 *out_count = state->out_count;
384 tevent_req_received(req);
385 return NT_STATUS_OK;