libcli/smb: fix padding in smb2_create_blob*
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_write.c
blob03998caba1a0bf605443ce9cef611f5627a2062b
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 DATA_BLOB in_data,
33 uint64_t in_offset,
34 uint32_t in_flags);
35 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
36 uint32_t *out_count);
38 static void smbd_smb2_request_write_done(struct tevent_req *subreq);
39 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
41 NTSTATUS status;
42 const uint8_t *inbody;
43 uint16_t in_data_offset;
44 uint32_t in_data_length;
45 DATA_BLOB in_data_buffer;
46 uint64_t in_offset;
47 uint64_t in_file_id_persistent;
48 uint64_t in_file_id_volatile;
49 struct files_struct *in_fsp;
50 uint32_t in_flags;
51 struct tevent_req *subreq;
53 status = smbd_smb2_request_verify_sizes(req, 0x31);
54 if (!NT_STATUS_IS_OK(status)) {
55 return smbd_smb2_request_error(req, status);
57 inbody = SMBD_SMB2_IN_BODY_PTR(req);
59 in_data_offset = SVAL(inbody, 0x02);
60 in_data_length = IVAL(inbody, 0x04);
61 in_offset = BVAL(inbody, 0x08);
62 in_file_id_persistent = BVAL(inbody, 0x10);
63 in_file_id_volatile = BVAL(inbody, 0x18);
64 in_flags = IVAL(inbody, 0x2C);
66 if (in_data_offset != (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
67 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
70 if (in_data_length > SMBD_SMB2_IN_DYN_LEN(req)) {
71 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
74 /* check the max write size */
75 if (in_data_length > req->sconn->smb2.max_write) {
76 DEBUG(2,("smbd_smb2_request_process_write : "
77 "client ignored max write :%s: 0x%08X: 0x%08X\n",
78 __location__, in_data_length, req->sconn->smb2.max_write));
79 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
82 in_data_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
83 in_data_buffer.length = in_data_length;
85 status = smbd_smb2_request_verify_creditcharge(req, in_data_length);
86 if (!NT_STATUS_IS_OK(status)) {
87 return smbd_smb2_request_error(req, status);
90 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
91 if (in_fsp == NULL) {
92 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
95 subreq = smbd_smb2_write_send(req, req->sconn->ev_ctx,
96 req, in_fsp,
97 in_data_buffer,
98 in_offset,
99 in_flags);
100 if (subreq == NULL) {
101 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
103 tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
105 return smbd_smb2_request_pending_queue(req, subreq, 500);
108 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
110 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
111 struct smbd_smb2_request);
112 DATA_BLOB outbody;
113 DATA_BLOB outdyn;
114 uint32_t out_count = 0;
115 NTSTATUS status;
116 NTSTATUS error; /* transport error */
118 status = smbd_smb2_write_recv(subreq, &out_count);
119 TALLOC_FREE(subreq);
120 if (!NT_STATUS_IS_OK(status)) {
121 error = smbd_smb2_request_error(req, status);
122 if (!NT_STATUS_IS_OK(error)) {
123 smbd_server_connection_terminate(req->sconn,
124 nt_errstr(error));
125 return;
127 return;
130 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
131 if (outbody.data == NULL) {
132 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
133 if (!NT_STATUS_IS_OK(error)) {
134 smbd_server_connection_terminate(req->sconn,
135 nt_errstr(error));
136 return;
138 return;
141 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
142 SSVAL(outbody.data, 0x02, 0); /* reserved */
143 SIVAL(outbody.data, 0x04, out_count); /* count */
144 SIVAL(outbody.data, 0x08, 0); /* remaining */
145 SSVAL(outbody.data, 0x0C, 0); /* write channel info offset */
146 SSVAL(outbody.data, 0x0E, 0); /* write channel info length */
148 outdyn = data_blob_const(NULL, 0);
150 error = smbd_smb2_request_done(req, outbody, &outdyn);
151 if (!NT_STATUS_IS_OK(error)) {
152 smbd_server_connection_terminate(req->sconn, nt_errstr(error));
153 return;
157 struct smbd_smb2_write_state {
158 struct smbd_smb2_request *smb2req;
159 struct smb_request *smbreq;
160 files_struct *fsp;
161 bool write_through;
162 uint32_t in_length;
163 uint64_t in_offset;
164 uint32_t out_count;
167 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq);
169 static NTSTATUS smb2_write_complete_internal(struct tevent_req *req,
170 ssize_t nwritten, int err,
171 bool do_sync)
173 NTSTATUS status;
174 struct smbd_smb2_write_state *state = tevent_req_data(req,
175 struct smbd_smb2_write_state);
176 files_struct *fsp = state->fsp;
178 if (nwritten == -1) {
179 status = map_nt_error_from_unix(err);
181 DEBUG(2, ("smb2_write failed: %s, file %s, "
182 "length=%lu offset=%lu nwritten=-1: %s\n",
183 fsp_fnum_dbg(fsp),
184 fsp_str_dbg(fsp),
185 (unsigned long)state->in_length,
186 (unsigned long)state->in_offset,
187 nt_errstr(status)));
189 return status;
192 DEBUG(3,("smb2: %s, file %s, "
193 "length=%lu offset=%lu wrote=%lu\n",
194 fsp_fnum_dbg(fsp),
195 fsp_str_dbg(fsp),
196 (unsigned long)state->in_length,
197 (unsigned long)state->in_offset,
198 (unsigned long)nwritten));
200 if ((nwritten == 0) && (state->in_length != 0)) {
201 DEBUG(5,("smb2: write [%s] disk full\n",
202 fsp_str_dbg(fsp)));
203 return NT_STATUS_DISK_FULL;
206 if (do_sync) {
207 status = sync_file(fsp->conn, fsp, state->write_through);
208 if (!NT_STATUS_IS_OK(status)) {
209 DEBUG(5,("smb2: sync_file for %s returned %s\n",
210 fsp_str_dbg(fsp),
211 nt_errstr(status)));
212 return status;
216 state->out_count = nwritten;
218 return NT_STATUS_OK;
221 NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err)
223 return smb2_write_complete_internal(req, nwritten, err, true);
226 NTSTATUS smb2_write_complete_nosync(struct tevent_req *req, ssize_t nwritten,
227 int err)
229 return smb2_write_complete_internal(req, nwritten, err, false);
233 static bool smbd_smb2_write_cancel(struct tevent_req *req)
235 struct smbd_smb2_write_state *state =
236 tevent_req_data(req,
237 struct smbd_smb2_write_state);
239 state->smb2req->cancelled = true;
241 return cancel_smb2_aio(state->smbreq);
244 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
245 struct tevent_context *ev,
246 struct smbd_smb2_request *smb2req,
247 struct files_struct *fsp,
248 DATA_BLOB in_data,
249 uint64_t in_offset,
250 uint32_t in_flags)
252 NTSTATUS status;
253 struct tevent_req *req = NULL;
254 struct smbd_smb2_write_state *state = NULL;
255 struct smb_request *smbreq = NULL;
256 connection_struct *conn = smb2req->tcon->compat;
257 ssize_t nwritten;
258 struct lock_struct lock;
260 req = tevent_req_create(mem_ctx, &state,
261 struct smbd_smb2_write_state);
262 if (req == NULL) {
263 return NULL;
265 state->smb2req = smb2req;
266 if (in_flags & SMB2_WRITEFLAG_WRITE_THROUGH) {
267 state->write_through = true;
269 state->in_length = in_data.length;
270 state->out_count = 0;
272 DEBUG(10,("smbd_smb2_write: %s - %s\n",
273 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
275 smbreq = smbd_smb2_fake_smb_request(smb2req);
276 if (tevent_req_nomem(smbreq, req)) {
277 return tevent_req_post(req, ev);
279 state->smbreq = smbreq;
281 state->fsp = fsp;
283 if (IS_IPC(smbreq->conn)) {
284 struct tevent_req *subreq = NULL;
286 if (!fsp_is_np(fsp)) {
287 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
288 return tevent_req_post(req, ev);
291 subreq = np_write_send(state, ev,
292 fsp->fake_file_handle,
293 in_data.data,
294 in_data.length);
295 if (tevent_req_nomem(subreq, req)) {
296 return tevent_req_post(req, ev);
298 tevent_req_set_callback(subreq,
299 smbd_smb2_write_pipe_done,
300 req);
301 return req;
304 if (!CHECK_WRITE(fsp)) {
305 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
306 return tevent_req_post(req, ev);
309 /* Try and do an asynchronous write. */
310 status = schedule_aio_smb2_write(conn,
311 smbreq,
312 fsp,
313 in_offset,
314 in_data,
315 state->write_through);
317 if (NT_STATUS_IS_OK(status)) {
319 * Doing an async write, allow this
320 * request to be canceled
322 tevent_req_set_cancel_fn(req, smbd_smb2_write_cancel);
323 return req;
326 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
327 /* Real error in setting up aio. Fail. */
328 tevent_req_nterror(req, status);
329 return tevent_req_post(req, ev);
332 /* Fallback to synchronous. */
333 init_strict_lock_struct(fsp,
334 fsp->op->global->open_persistent_id,
335 in_offset,
336 in_data.length,
337 WRITE_LOCK,
338 &lock);
340 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
341 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
342 return tevent_req_post(req, ev);
345 nwritten = write_file(smbreq, fsp,
346 (const char *)in_data.data,
347 in_offset,
348 in_data.length);
350 status = smb2_write_complete(req, nwritten, errno);
352 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
354 DEBUG(10,("smb2: write on "
355 "file %s, offset %.0f, requested %u, written = %u\n",
356 fsp_str_dbg(fsp),
357 (double)in_offset,
358 (unsigned int)in_data.length,
359 (unsigned int)nwritten ));
361 if (!NT_STATUS_IS_OK(status)) {
362 tevent_req_nterror(req, status);
363 } else {
364 /* Success. */
365 tevent_req_done(req);
368 return tevent_req_post(req, ev);
371 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
373 struct tevent_req *req = tevent_req_callback_data(subreq,
374 struct tevent_req);
375 struct smbd_smb2_write_state *state = tevent_req_data(req,
376 struct smbd_smb2_write_state);
377 NTSTATUS status;
378 ssize_t nwritten = -1;
380 status = np_write_recv(subreq, &nwritten);
381 TALLOC_FREE(subreq);
382 if (!NT_STATUS_IS_OK(status)) {
383 NTSTATUS old = status;
384 status = nt_status_np_pipe(old);
385 tevent_req_nterror(req, status);
386 return;
389 if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
390 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
391 return;
394 state->out_count = nwritten;
396 tevent_req_done(req);
399 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
400 uint32_t *out_count)
402 NTSTATUS status;
403 struct smbd_smb2_write_state *state = tevent_req_data(req,
404 struct smbd_smb2_write_state);
406 if (tevent_req_is_nterror(req, &status)) {
407 tevent_req_received(req);
408 return status;
411 *out_count = state->out_count;
413 tevent_req_received(req);
414 return NT_STATUS_OK;