s3:smb2-server: session setup replies should always be signed (except for guest sessions)
[Samba.git] / source3 / smbd / smb2_write.c
blobb2360caacec8dd63fd3c679b14c0174fe0d01a05
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 > req->sconn->smb2.max_write) {
82 DEBUG(2,("smbd_smb2_request_process_write : "
83 "client ignored max write :%s: 0x%08X: 0x%08X\n",
84 __location__, in_data_length, req->sconn->smb2.max_write));
85 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
88 in_data_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
89 in_data_buffer.length = in_data_length;
91 if (req->compat_chain_fsp) {
92 /* skip check */
93 } else if (in_file_id_persistent != in_file_id_volatile) {
94 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
97 subreq = smbd_smb2_write_send(req,
98 req->sconn->smb2.event_ctx,
99 req,
100 in_smbpid,
101 in_file_id_volatile,
102 in_data_buffer,
103 in_offset,
104 in_flags);
105 if (subreq == NULL) {
106 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
108 tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
110 return smbd_smb2_request_pending_queue(req, subreq);
113 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
115 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
116 struct smbd_smb2_request);
117 int i = req->current_idx;
118 uint8_t *outhdr;
119 DATA_BLOB outbody;
120 DATA_BLOB outdyn;
121 uint32_t out_count = 0;
122 NTSTATUS status;
123 NTSTATUS error; /* transport error */
125 status = smbd_smb2_write_recv(subreq, &out_count);
126 TALLOC_FREE(subreq);
127 if (!NT_STATUS_IS_OK(status)) {
128 error = smbd_smb2_request_error(req, status);
129 if (!NT_STATUS_IS_OK(error)) {
130 smbd_server_connection_terminate(req->sconn,
131 nt_errstr(error));
132 return;
134 return;
137 outhdr = (uint8_t *)req->out.vector[i].iov_base;
139 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
140 if (outbody.data == NULL) {
141 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
142 if (!NT_STATUS_IS_OK(error)) {
143 smbd_server_connection_terminate(req->sconn,
144 nt_errstr(error));
145 return;
147 return;
150 SSVAL(outbody.data, 0x00, 0x10 + 1); /* struct size */
151 SSVAL(outbody.data, 0x02, 0); /* reserved */
152 SIVAL(outbody.data, 0x04, out_count); /* count */
153 SIVAL(outbody.data, 0x08, 0); /* remaining */
154 SSVAL(outbody.data, 0x0C, 0); /* write channel info offset */
155 SSVAL(outbody.data, 0x0E, 0); /* write channel info length */
157 outdyn = data_blob_const(NULL, 0);
159 error = smbd_smb2_request_done(req, outbody, &outdyn);
160 if (!NT_STATUS_IS_OK(error)) {
161 smbd_server_connection_terminate(req->sconn, nt_errstr(error));
162 return;
166 struct smbd_smb2_write_state {
167 struct smbd_smb2_request *smb2req;
168 files_struct *fsp;
169 bool write_through;
170 uint32_t in_length;
171 uint64_t in_offset;
172 uint32_t out_count;
175 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq);
177 NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err)
179 NTSTATUS status;
180 struct smbd_smb2_write_state *state = tevent_req_data(req,
181 struct smbd_smb2_write_state);
182 files_struct *fsp = state->fsp;
184 DEBUG(3,("smb2: fnum=[%d/%s] "
185 "length=%lu offset=%lu wrote=%lu\n",
186 fsp->fnum,
187 fsp_str_dbg(fsp),
188 (unsigned long)state->in_length,
189 (unsigned long)state->in_offset,
190 (unsigned long)nwritten));
192 if (nwritten == -1) {
193 return map_nt_error_from_unix(err);
196 if ((nwritten == 0) && (state->in_length != 0)) {
197 DEBUG(5,("smb2: write [%s] disk full\n",
198 fsp_str_dbg(fsp)));
199 return NT_STATUS_DISK_FULL;
202 status = sync_file(fsp->conn, fsp, state->write_through);
203 if (!NT_STATUS_IS_OK(status)) {
204 DEBUG(5,("smb2: sync_file for %s returned %s\n",
205 fsp_str_dbg(fsp),
206 nt_errstr(status)));
207 return status;
210 state->out_count = nwritten;
212 return NT_STATUS_OK;
215 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
216 struct tevent_context *ev,
217 struct smbd_smb2_request *smb2req,
218 uint32_t in_smbpid,
219 uint64_t in_file_id_volatile,
220 DATA_BLOB in_data,
221 uint64_t in_offset,
222 uint32_t in_flags)
224 NTSTATUS status;
225 struct tevent_req *req = NULL;
226 struct smbd_smb2_write_state *state = NULL;
227 struct smb_request *smbreq = NULL;
228 connection_struct *conn = smb2req->tcon->compat_conn;
229 files_struct *fsp = NULL;
230 ssize_t nwritten;
231 struct lock_struct lock;
233 req = tevent_req_create(mem_ctx, &state,
234 struct smbd_smb2_write_state);
235 if (req == NULL) {
236 return NULL;
238 state->smb2req = smb2req;
239 if (in_flags & 0x00000001) {
240 state->write_through = true;
242 state->in_length = in_data.length;
243 state->out_count = 0;
245 DEBUG(10,("smbd_smb2_write: file_id[0x%016llX]\n",
246 (unsigned long long)in_file_id_volatile));
248 smbreq = smbd_smb2_fake_smb_request(smb2req);
249 if (tevent_req_nomem(smbreq, req)) {
250 return tevent_req_post(req, ev);
253 fsp = file_fsp(smbreq, (uint16_t)in_file_id_volatile);
254 if (fsp == NULL) {
255 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
256 return tevent_req_post(req, ev);
258 if (conn != fsp->conn) {
259 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
260 return tevent_req_post(req, ev);
262 if (smb2req->session->vuid != fsp->vuid) {
263 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
264 return tevent_req_post(req, ev);
267 state->fsp = fsp;
269 if (IS_IPC(smbreq->conn)) {
270 struct tevent_req *subreq = NULL;
272 if (!fsp_is_np(fsp)) {
273 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
274 return tevent_req_post(req, ev);
277 subreq = np_write_send(state, smbd_event_context(),
278 fsp->fake_file_handle,
279 in_data.data,
280 in_data.length);
281 if (tevent_req_nomem(subreq, req)) {
282 return tevent_req_post(req, ev);
284 tevent_req_set_callback(subreq,
285 smbd_smb2_write_pipe_done,
286 req);
287 return req;
290 if (!CHECK_WRITE(fsp)) {
291 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
292 return tevent_req_post(req, ev);
295 /* Try and do an asynchronous write. */
296 status = schedule_aio_smb2_write(conn,
297 smbreq,
298 fsp,
299 in_offset,
300 in_data,
301 state->write_through);
303 if (NT_STATUS_IS_OK(status)) {
305 * Doing an async write. Don't
306 * send a "gone async" message
307 * as we expect this to be less
308 * than the client timeout period.
309 * JRA. FIXME for offline files..
310 * FIXME - add cancel code..
312 smb2req->async = true;
313 return req;
316 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
317 /* Real error in setting up aio. Fail. */
318 tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
319 return tevent_req_post(req, ev);
322 /* Fallback to synchronous. */
323 init_strict_lock_struct(fsp,
324 in_file_id_volatile,
325 in_offset,
326 in_data.length,
327 WRITE_LOCK,
328 &lock);
330 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
331 tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
332 return tevent_req_post(req, ev);
335 nwritten = write_file(smbreq, fsp,
336 (const char *)in_data.data,
337 in_offset,
338 in_data.length);
340 status = smb2_write_complete(req, nwritten, errno);
342 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
344 DEBUG(10,("smb2: write on "
345 "file %s, offset %.0f, requested %u, written = %u\n",
346 fsp_str_dbg(fsp),
347 (double)in_offset,
348 (unsigned int)in_data.length,
349 (unsigned int)nwritten ));
351 if (!NT_STATUS_IS_OK(status)) {
352 tevent_req_nterror(req, status);
353 } else {
354 /* Success. */
355 tevent_req_done(req);
358 return tevent_req_post(req, ev);
361 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq)
363 struct tevent_req *req = tevent_req_callback_data(subreq,
364 struct tevent_req);
365 struct smbd_smb2_write_state *state = tevent_req_data(req,
366 struct smbd_smb2_write_state);
367 NTSTATUS status;
368 ssize_t nwritten = -1;
370 status = np_write_recv(subreq, &nwritten);
371 TALLOC_FREE(subreq);
372 if (!NT_STATUS_IS_OK(status)) {
373 tevent_req_nterror(req, status);
374 return;
377 if ((nwritten == 0 && state->in_length != 0) || (nwritten < 0)) {
378 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
379 return;
382 state->out_count = nwritten;
384 tevent_req_done(req);
387 static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
388 uint32_t *out_count)
390 NTSTATUS status;
391 struct smbd_smb2_write_state *state = tevent_req_data(req,
392 struct smbd_smb2_write_state);
394 if (tevent_req_is_nterror(req, &status)) {
395 tevent_req_received(req);
396 return status;
399 *out_count = state->out_count;
401 tevent_req_received(req);
402 return NT_STATUS_OK;