libcli/security: remove unused sec_desc_del_sid()
[Samba/wip.git] / source3 / smbd / smb2_write.c
blobc61254f644cd5b229cf1c0468e18af2ae2aa92cc
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 size_t in_dyn_len = 0;
52 uint8_t *in_dyn_ptr = NULL;
53 struct tevent_req *subreq;
55 status = smbd_smb2_request_verify_sizes(req, 0x31);
56 if (!NT_STATUS_IS_OK(status)) {
57 return smbd_smb2_request_error(req, status);
59 inbody = SMBD_SMB2_IN_BODY_PTR(req);
61 in_data_offset = SVAL(inbody, 0x02);
62 in_data_length = IVAL(inbody, 0x04);
63 in_offset = BVAL(inbody, 0x08);
64 in_file_id_persistent = BVAL(inbody, 0x10);
65 in_file_id_volatile = BVAL(inbody, 0x18);
66 in_flags = IVAL(inbody, 0x2C);
68 if (in_data_offset != (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req))) {
69 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
72 if (req->smb1req != NULL && req->smb1req->unread_bytes > 0) {
73 in_dyn_ptr = NULL;
74 in_dyn_len = req->smb1req->unread_bytes;
75 } else {
76 in_dyn_ptr = SMBD_SMB2_IN_DYN_PTR(req);
77 in_dyn_len = SMBD_SMB2_IN_DYN_LEN(req);
80 if (in_data_length > in_dyn_len) {
81 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
84 /* check the max write size */
85 if (in_data_length > req->sconn->smb2.max_write) {
86 DEBUG(2,("smbd_smb2_request_process_write : "
87 "client ignored max write :%s: 0x%08X: 0x%08X\n",
88 __location__, in_data_length, req->sconn->smb2.max_write));
89 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
93 * Note: that in_dyn_ptr is NULL for the recvfile case.
95 in_data_buffer.data = in_dyn_ptr;
96 in_data_buffer.length = in_data_length;
98 status = smbd_smb2_request_verify_creditcharge(req, in_data_length);
99 if (!NT_STATUS_IS_OK(status)) {
100 return smbd_smb2_request_error(req, status);
103 in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
104 if (in_fsp == NULL) {
105 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
108 subreq = smbd_smb2_write_send(req, req->sconn->ev_ctx,
109 req, in_fsp,
110 in_data_buffer,
111 in_offset,
112 in_flags);
113 if (subreq == NULL) {
114 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
116 tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
118 return smbd_smb2_request_pending_queue(req, subreq, 500);
121 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
123 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
124 struct smbd_smb2_request);
125 DATA_BLOB outbody;
126 DATA_BLOB outdyn;
127 uint32_t out_count = 0;
128 NTSTATUS status;
129 NTSTATUS error; /* transport error */
131 status = smbd_smb2_write_recv(subreq, &out_count);
132 TALLOC_FREE(subreq);
133 if (!NT_STATUS_IS_OK(status)) {
134 error = smbd_smb2_request_error(req, status);
135 if (!NT_STATUS_IS_OK(error)) {
136 smbd_server_connection_terminate(req->sconn,
137 nt_errstr(error));
138 return;
140 return;
143 outbody = smbd_smb2_generate_outbody(req, 0x10);
144 if (outbody.data == NULL) {
145 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
146 if (!NT_STATUS_IS_OK(error)) {
147 smbd_server_connection_terminate(req->sconn,
148 nt_errstr(error));
149 return;
151 return;
154 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
155 SSVAL(outbody.data, 0x02, 0); /* reserved */
156 SIVAL(outbody.data, 0x04, out_count); /* count */
157 SIVAL(outbody.data, 0x08, 0); /* remaining */
158 SSVAL(outbody.data, 0x0C, 0); /* write channel info offset */
159 SSVAL(outbody.data, 0x0E, 0); /* write channel info length */
161 outdyn = data_blob_const(NULL, 0);
163 error = smbd_smb2_request_done(req, outbody, &outdyn);
164 if (!NT_STATUS_IS_OK(error)) {
165 smbd_server_connection_terminate(req->sconn, nt_errstr(error));
166 return;
170 struct smbd_smb2_write_state {
171 struct smbd_smb2_request *smb2req;
172 struct smb_request *smbreq;
173 files_struct *fsp;
174 bool write_through;
175 uint32_t in_length;
176 uint64_t in_offset;
177 uint32_t out_count;
180 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq);
182 static NTSTATUS smb2_write_complete_internal(struct tevent_req *req,
183 ssize_t nwritten, int err,
184 bool do_sync)
186 NTSTATUS status;
187 struct smbd_smb2_write_state *state = tevent_req_data(req,
188 struct smbd_smb2_write_state);
189 files_struct *fsp = state->fsp;
191 if (nwritten == -1) {
192 status = map_nt_error_from_unix(err);
194 DEBUG(2, ("smb2_write failed: %s, file %s, "
195 "length=%lu offset=%lu nwritten=-1: %s\n",
196 fsp_fnum_dbg(fsp),
197 fsp_str_dbg(fsp),
198 (unsigned long)state->in_length,
199 (unsigned long)state->in_offset,
200 nt_errstr(status)));
202 return status;
205 DEBUG(3,("smb2: %s, file %s, "
206 "length=%lu offset=%lu wrote=%lu\n",
207 fsp_fnum_dbg(fsp),
208 fsp_str_dbg(fsp),
209 (unsigned long)state->in_length,
210 (unsigned long)state->in_offset,
211 (unsigned long)nwritten));
213 if ((nwritten == 0) && (state->in_length != 0)) {
214 DEBUG(5,("smb2: write [%s] disk full\n",
215 fsp_str_dbg(fsp)));
216 return NT_STATUS_DISK_FULL;
219 if (do_sync) {
220 status = sync_file(fsp->conn, fsp, state->write_through);
221 if (!NT_STATUS_IS_OK(status)) {
222 DEBUG(5,("smb2: sync_file for %s returned %s\n",
223 fsp_str_dbg(fsp),
224 nt_errstr(status)));
225 return status;
229 state->out_count = nwritten;
231 return NT_STATUS_OK;
234 NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err)
236 return smb2_write_complete_internal(req, nwritten, err, true);
239 NTSTATUS smb2_write_complete_nosync(struct tevent_req *req, ssize_t nwritten,
240 int err)
242 return smb2_write_complete_internal(req, nwritten, err, false);
246 static bool smbd_smb2_write_cancel(struct tevent_req *req)
248 struct smbd_smb2_write_state *state =
249 tevent_req_data(req,
250 struct smbd_smb2_write_state);
252 return cancel_smb2_aio(state->smbreq);
255 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
256 struct tevent_context *ev,
257 struct smbd_smb2_request *smb2req,
258 struct files_struct *fsp,
259 DATA_BLOB in_data,
260 uint64_t in_offset,
261 uint32_t in_flags)
263 NTSTATUS status;
264 struct tevent_req *req = NULL;
265 struct smbd_smb2_write_state *state = NULL;
266 struct smb_request *smbreq = NULL;
267 connection_struct *conn = smb2req->tcon->compat;
268 ssize_t nwritten;
269 struct lock_struct lock;
271 req = tevent_req_create(mem_ctx, &state,
272 struct smbd_smb2_write_state);
273 if (req == NULL) {
274 return NULL;
276 state->smb2req = smb2req;
277 if (in_flags & SMB2_WRITEFLAG_WRITE_THROUGH) {
278 state->write_through = true;
280 state->in_length = in_data.length;
281 state->out_count = 0;
283 DEBUG(10,("smbd_smb2_write: %s - %s\n",
284 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
286 smbreq = smbd_smb2_fake_smb_request(smb2req);
287 if (tevent_req_nomem(smbreq, req)) {
288 return tevent_req_post(req, ev);
290 state->smbreq = smbreq;
292 state->fsp = fsp;
294 if (IS_IPC(smbreq->conn)) {
295 struct tevent_req *subreq = NULL;
297 if (!fsp_is_np(fsp)) {
298 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
299 return tevent_req_post(req, ev);
302 subreq = np_write_send(state, ev,
303 fsp->fake_file_handle,
304 in_data.data,
305 in_data.length);
306 if (tevent_req_nomem(subreq, req)) {
307 return tevent_req_post(req, ev);
309 tevent_req_set_callback(subreq,
310 smbd_smb2_write_pipe_done,
311 req);
312 return req;
315 if (!CHECK_WRITE(fsp)) {
316 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
317 return tevent_req_post(req, ev);
320 /* Try and do an asynchronous write. */
321 status = schedule_aio_smb2_write(conn,
322 smbreq,
323 fsp,
324 in_offset,
325 in_data,
326 state->write_through);
328 if (NT_STATUS_IS_OK(status)) {
330 * Doing an async write, allow this
331 * request to be canceled
333 tevent_req_set_cancel_fn(req, smbd_smb2_write_cancel);
334 return req;
337 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
338 /* Real error in setting up aio. Fail. */
339 tevent_req_nterror(req, status);
340 return tevent_req_post(req, ev);
343 /* Fallback to synchronous. */
344 init_strict_lock_struct(fsp,
345 fsp->op->global->open_persistent_id,
346 in_offset,
347 in_data.length,
348 WRITE_LOCK,
349 &lock);
351 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
352 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
353 return tevent_req_post(req, ev);
357 * Note: in_data.data is NULL for the recvfile case.
359 nwritten = write_file(smbreq, fsp,
360 (const char *)in_data.data,
361 in_offset,
362 in_data.length);
364 status = smb2_write_complete(req, nwritten, errno);
366 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
368 DEBUG(10,("smb2: write on "
369 "file %s, offset %.0f, requested %u, written = %u\n",
370 fsp_str_dbg(fsp),
371 (double)in_offset,
372 (unsigned int)in_data.length,
373 (unsigned int)nwritten ));
375 if (!NT_STATUS_IS_OK(status)) {
376 tevent_req_nterror(req, status);
377 } else {
378 /* Success. */
379 tevent_req_done(req);
382 return tevent_req_post(req, ev);
385 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
387 struct tevent_req *req = tevent_req_callback_data(subreq,
388 struct tevent_req);
389 struct smbd_smb2_write_state *state = tevent_req_data(req,
390 struct smbd_smb2_write_state);
391 NTSTATUS status;
392 ssize_t nwritten = -1;
394 status = np_write_recv(subreq, &nwritten);
395 TALLOC_FREE(subreq);
396 if (!NT_STATUS_IS_OK(status)) {
397 NTSTATUS old = status;
398 status = nt_status_np_pipe(old);
399 tevent_req_nterror(req, status);
400 return;
403 if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
404 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
405 return;
408 state->out_count = nwritten;
410 tevent_req_done(req);
413 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
414 uint32_t *out_count)
416 NTSTATUS status;
417 struct smbd_smb2_write_state *state = tevent_req_data(req,
418 struct smbd_smb2_write_state);
420 if (tevent_req_is_nterror(req, &status)) {
421 tevent_req_received(req);
422 return status;
425 *out_count = state->out_count;
427 tevent_req_received(req);
428 return NT_STATUS_OK;