s3:smbd: convert connections.c to use only dbrwap wrapper functions
[Samba/gbeck.git] / source3 / smbd / smb2_write.c
blobb47f8a02a1c4ce5da802d293d91d9a7f4dd9d80e
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 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);
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 uint32_t in_flags;
54 struct tevent_req *subreq;
56 status = smbd_smb2_request_verify_sizes(req, 0x31);
57 if (!NT_STATUS_IS_OK(status)) {
58 return smbd_smb2_request_error(req, status);
60 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
61 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
63 in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
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);
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);
76 if (in_data_length > req->in.vector[i+2].iov_len) {
77 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
80 /* check the max write size */
81 if (in_data_length > lp_smb2_max_write()) {
82 /* This is a warning. */
83 DEBUG(2,("smbd_smb2_request_process_write : "
84 "client ignored max write :%s: 0x%08X: 0x%08X\n",
85 __location__, in_data_length, lp_smb2_max_write()));
86 #if 0
87 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
88 #endif
91 in_data_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
92 in_data_buffer.length = in_data_length;
94 if (req->compat_chain_fsp) {
95 /* skip check */
96 } else if (in_file_id_persistent != in_file_id_volatile) {
97 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
100 subreq = smbd_smb2_write_send(req,
101 req->sconn->smb2.event_ctx,
102 req,
103 in_smbpid,
104 in_file_id_volatile,
105 in_data_buffer,
106 in_offset,
107 in_flags);
108 if (subreq == NULL) {
109 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
111 tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
113 return smbd_smb2_request_pending_queue(req, subreq);
116 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
118 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
119 struct smbd_smb2_request);
120 int i = req->current_idx;
121 uint8_t *outhdr;
122 DATA_BLOB outbody;
123 DATA_BLOB outdyn;
124 uint32_t out_count = 0;
125 NTSTATUS status;
126 NTSTATUS error; /* transport error */
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;
137 return;
140 outhdr = (uint8_t *)req->out.vector[i].iov_base;
142 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
143 if (outbody.data == NULL) {
144 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
145 if (!NT_STATUS_IS_OK(error)) {
146 smbd_server_connection_terminate(req->sconn,
147 nt_errstr(error));
148 return;
150 return;
153 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
154 SSVAL(outbody.data, 0x02, 0); /* reserved */
155 SIVAL(outbody.data, 0x04, out_count); /* count */
156 SIVAL(outbody.data, 0x08, 0); /* remaining */
157 SSVAL(outbody.data, 0x0C, 0); /* write channel info offset */
158 SSVAL(outbody.data, 0x0E, 0); /* write channel info length */
160 outdyn = data_blob_const(NULL, 0);
162 error = smbd_smb2_request_done(req, outbody, &outdyn);
163 if (!NT_STATUS_IS_OK(error)) {
164 smbd_server_connection_terminate(req->sconn, nt_errstr(error));
165 return;
169 struct smbd_smb2_write_state {
170 struct smbd_smb2_request *smb2req;
171 files_struct *fsp;
172 bool write_through;
173 uint32_t in_length;
174 uint64_t in_offset;
175 uint32_t out_count;
178 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq);
180 NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err)
182 NTSTATUS status;
183 struct smbd_smb2_write_state *state = tevent_req_data(req,
184 struct smbd_smb2_write_state);
185 files_struct *fsp = state->fsp;
187 DEBUG(3,("smb2: fnum=[%d/%s] "
188 "length=%lu offset=%lu wrote=%lu\n",
189 fsp->fnum,
190 fsp_str_dbg(fsp),
191 (unsigned long)state->in_length,
192 (unsigned long)state->in_offset,
193 (unsigned long)nwritten));
195 if (nwritten == -1) {
196 return map_nt_error_from_unix(err);
199 if ((nwritten == 0) && (state->in_length != 0)) {
200 DEBUG(5,("smb2: write [%s] disk full\n",
201 fsp_str_dbg(fsp)));
202 return NT_STATUS_DISK_FULL;
205 status = sync_file(fsp->conn, fsp, state->write_through);
206 if (!NT_STATUS_IS_OK(status)) {
207 DEBUG(5,("smb2: sync_file for %s returned %s\n",
208 fsp_str_dbg(fsp),
209 nt_errstr(status)));
210 return status;
213 state->out_count = nwritten;
215 return NT_STATUS_OK;
218 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
219 struct tevent_context *ev,
220 struct smbd_smb2_request *smb2req,
221 uint32_t in_smbpid,
222 uint64_t in_file_id_volatile,
223 DATA_BLOB in_data,
224 uint64_t in_offset,
225 uint32_t in_flags)
227 NTSTATUS status;
228 struct tevent_req *req = NULL;
229 struct smbd_smb2_write_state *state = NULL;
230 struct smb_request *smbreq = NULL;
231 connection_struct *conn = smb2req->tcon->compat_conn;
232 files_struct *fsp = NULL;
233 ssize_t nwritten;
234 struct lock_struct lock;
236 req = tevent_req_create(mem_ctx, &state,
237 struct smbd_smb2_write_state);
238 if (req == NULL) {
239 return NULL;
241 state->smb2req = smb2req;
242 if (in_flags & SMB2_WRITEFLAG_WRITE_THROUGH) {
243 state->write_through = true;
245 state->in_length = in_data.length;
246 state->out_count = 0;
248 DEBUG(10,("smbd_smb2_write: file_id[0x%016llX]\n",
249 (unsigned long long)in_file_id_volatile));
251 smbreq = smbd_smb2_fake_smb_request(smb2req);
252 if (tevent_req_nomem(smbreq, req)) {
253 return tevent_req_post(req, ev);
256 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
257 if (fsp == NULL) {
258 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
259 return tevent_req_post(req, ev);
261 if (conn != fsp->conn) {
262 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
263 return tevent_req_post(req, ev);
265 if (smb2req->session->vuid != fsp->vuid) {
266 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
267 return tevent_req_post(req, ev);
270 state->fsp = fsp;
272 if (IS_IPC(smbreq->conn)) {
273 struct tevent_req *subreq = NULL;
275 if (!fsp_is_np(fsp)) {
276 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
277 return tevent_req_post(req, ev);
280 subreq = np_write_send(state, server_event_context(),
281 fsp->fake_file_handle,
282 in_data.data,
283 in_data.length);
284 if (tevent_req_nomem(subreq, req)) {
285 return tevent_req_post(req, ev);
287 tevent_req_set_callback(subreq,
288 smbd_smb2_write_pipe_done,
289 req);
290 return req;
293 if (!CHECK_WRITE(fsp)) {
294 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
295 return tevent_req_post(req, ev);
298 /* Try and do an asynchronous write. */
299 status = schedule_aio_smb2_write(conn,
300 smbreq,
301 fsp,
302 in_offset,
303 in_data,
304 state->write_through);
306 if (NT_STATUS_IS_OK(status)) {
308 * Doing an async write. Don't
309 * send a "gone async" message
310 * as we expect this to be less
311 * than the client timeout period.
312 * JRA. FIXME for offline files..
313 * FIXME - add cancel code..
315 smb2req->async = true;
316 return req;
319 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
320 /* Real error in setting up aio. Fail. */
321 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
322 return tevent_req_post(req, ev);
325 /* Fallback to synchronous. */
326 init_strict_lock_struct(fsp,
327 in_file_id_volatile,
328 in_offset,
329 in_data.length,
330 WRITE_LOCK,
331 &lock);
333 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
334 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
335 return tevent_req_post(req, ev);
338 nwritten = write_file(smbreq, fsp,
339 (const char *)in_data.data,
340 in_offset,
341 in_data.length);
343 status = smb2_write_complete(req, nwritten, errno);
345 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
347 DEBUG(10,("smb2: write on "
348 "file %s, offset %.0f, requested %u, written = %u\n",
349 fsp_str_dbg(fsp),
350 (double)in_offset,
351 (unsigned int)in_data.length,
352 (unsigned int)nwritten ));
354 if (!NT_STATUS_IS_OK(status)) {
355 tevent_req_nterror(req, status);
356 } else {
357 /* Success. */
358 tevent_req_done(req);
361 return tevent_req_post(req, ev);
364 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
366 struct tevent_req *req = tevent_req_callback_data(subreq,
367 struct tevent_req);
368 struct smbd_smb2_write_state *state = tevent_req_data(req,
369 struct smbd_smb2_write_state);
370 NTSTATUS status;
371 ssize_t nwritten = -1;
373 status = np_write_recv(subreq, &nwritten);
374 TALLOC_FREE(subreq);
375 if (!NT_STATUS_IS_OK(status)) {
376 tevent_req_nterror(req, status);
377 return;
380 if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
381 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
382 return;
385 state->out_count = nwritten;
387 tevent_req_done(req);
390 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
391 uint32_t *out_count)
393 NTSTATUS status;
394 struct smbd_smb2_write_state *state = tevent_req_data(req,
395 struct smbd_smb2_write_state);
397 if (tevent_req_is_nterror(req, &status)) {
398 tevent_req_received(req);
399 return status;
402 *out_count = state->out_count;
404 tevent_req_received(req);
405 return NT_STATUS_OK;