2 Unix SMB/CIFS implementation.
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/>.
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
,
35 static NTSTATUS
smbd_smb2_write_recv(struct tevent_req
*req
,
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 struct smbXsrv_connection
*xconn
= req
->xconn
;
43 const uint8_t *inbody
;
44 uint16_t in_data_offset
;
45 uint32_t in_data_length
;
46 DATA_BLOB in_data_buffer
;
48 uint64_t in_file_id_persistent
;
49 uint64_t in_file_id_volatile
;
50 struct files_struct
*in_fsp
;
52 size_t in_dyn_len
= 0;
53 uint8_t *in_dyn_ptr
= NULL
;
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 inbody
= SMBD_SMB2_IN_BODY_PTR(req
);
62 in_data_offset
= SVAL(inbody
, 0x02);
63 in_data_length
= IVAL(inbody
, 0x04);
64 in_offset
= BVAL(inbody
, 0x08);
65 in_file_id_persistent
= BVAL(inbody
, 0x10);
66 in_file_id_volatile
= BVAL(inbody
, 0x18);
67 in_flags
= IVAL(inbody
, 0x2C);
69 if (in_data_offset
!= (SMB2_HDR_BODY
+ SMBD_SMB2_IN_BODY_LEN(req
))) {
70 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
73 if (req
->smb1req
!= NULL
&& req
->smb1req
->unread_bytes
> 0) {
75 in_dyn_len
= req
->smb1req
->unread_bytes
;
77 in_dyn_ptr
= SMBD_SMB2_IN_DYN_PTR(req
);
78 in_dyn_len
= SMBD_SMB2_IN_DYN_LEN(req
);
81 if (in_data_length
> in_dyn_len
) {
82 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
85 /* check the max write size */
86 if (in_data_length
> xconn
->smb2
.server
.max_write
) {
87 DEBUG(2,("smbd_smb2_request_process_write : "
88 "client ignored max write :%s: 0x%08X: 0x%08X\n",
89 __location__
, in_data_length
, xconn
->smb2
.server
.max_write
));
90 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
94 * Note: that in_dyn_ptr is NULL for the recvfile case.
96 in_data_buffer
.data
= in_dyn_ptr
;
97 in_data_buffer
.length
= in_data_length
;
99 status
= smbd_smb2_request_verify_creditcharge(req
, in_data_length
);
100 if (!NT_STATUS_IS_OK(status
)) {
101 return smbd_smb2_request_error(req
, status
);
104 in_fsp
= file_fsp_smb2(req
, in_file_id_persistent
, in_file_id_volatile
);
105 if (in_fsp
== NULL
) {
106 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
109 subreq
= smbd_smb2_write_send(req
, req
->sconn
->ev_ctx
,
114 if (subreq
== NULL
) {
115 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
117 tevent_req_set_callback(subreq
, smbd_smb2_request_write_done
, req
);
119 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
122 static void smbd_smb2_request_write_done(struct tevent_req
*subreq
)
124 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
125 struct smbd_smb2_request
);
128 uint32_t out_count
= 0;
130 NTSTATUS error
; /* transport error */
132 status
= smbd_smb2_write_recv(subreq
, &out_count
);
134 if (!NT_STATUS_IS_OK(status
)) {
135 error
= smbd_smb2_request_error(req
, status
);
136 if (!NT_STATUS_IS_OK(error
)) {
137 smbd_server_connection_terminate(req
->xconn
,
144 outbody
= smbd_smb2_generate_outbody(req
, 0x10);
145 if (outbody
.data
== NULL
) {
146 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
147 if (!NT_STATUS_IS_OK(error
)) {
148 smbd_server_connection_terminate(req
->xconn
,
155 SSVAL(outbody
.data
, 0x00, 0x10 + 1); /* struct size */
156 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
157 SIVAL(outbody
.data
, 0x04, out_count
); /* count */
158 SIVAL(outbody
.data
, 0x08, 0); /* remaining */
159 SSVAL(outbody
.data
, 0x0C, 0); /* write channel info offset */
160 SSVAL(outbody
.data
, 0x0E, 0); /* write channel info length */
162 outdyn
= data_blob_const(NULL
, 0);
164 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
165 if (!NT_STATUS_IS_OK(error
)) {
166 smbd_server_connection_terminate(req
->xconn
, nt_errstr(error
));
171 struct smbd_smb2_write_state
{
172 struct smbd_smb2_request
*smb2req
;
173 struct smb_request
*smbreq
;
181 static void smbd_smb2_write_pipe_done(struct tevent_req
*subreq
);
183 static NTSTATUS
smb2_write_complete_internal(struct tevent_req
*req
,
184 ssize_t nwritten
, int err
,
188 struct smbd_smb2_write_state
*state
= tevent_req_data(req
,
189 struct smbd_smb2_write_state
);
190 files_struct
*fsp
= state
->fsp
;
192 if (nwritten
== -1) {
193 status
= map_nt_error_from_unix(err
);
195 DEBUG(2, ("smb2_write failed: %s, file %s, "
196 "length=%lu offset=%lu nwritten=-1: %s\n",
199 (unsigned long)state
->in_length
,
200 (unsigned long)state
->in_offset
,
206 DEBUG(3,("smb2: %s, file %s, "
207 "length=%lu offset=%lu wrote=%lu\n",
210 (unsigned long)state
->in_length
,
211 (unsigned long)state
->in_offset
,
212 (unsigned long)nwritten
));
214 if ((nwritten
== 0) && (state
->in_length
!= 0)) {
215 DEBUG(5,("smb2: write [%s] disk full\n",
217 return NT_STATUS_DISK_FULL
;
221 status
= sync_file(fsp
->conn
, fsp
, state
->write_through
);
222 if (!NT_STATUS_IS_OK(status
)) {
223 DEBUG(5,("smb2: sync_file for %s returned %s\n",
230 state
->out_count
= nwritten
;
235 NTSTATUS
smb2_write_complete(struct tevent_req
*req
, ssize_t nwritten
, int err
)
237 return smb2_write_complete_internal(req
, nwritten
, err
, true);
240 NTSTATUS
smb2_write_complete_nosync(struct tevent_req
*req
, ssize_t nwritten
,
243 return smb2_write_complete_internal(req
, nwritten
, err
, false);
247 static bool smbd_smb2_write_cancel(struct tevent_req
*req
)
249 struct smbd_smb2_write_state
*state
=
251 struct smbd_smb2_write_state
);
253 return cancel_smb2_aio(state
->smbreq
);
256 static struct tevent_req
*smbd_smb2_write_send(TALLOC_CTX
*mem_ctx
,
257 struct tevent_context
*ev
,
258 struct smbd_smb2_request
*smb2req
,
259 struct files_struct
*fsp
,
265 struct tevent_req
*req
= NULL
;
266 struct smbd_smb2_write_state
*state
= NULL
;
267 struct smb_request
*smbreq
= NULL
;
268 connection_struct
*conn
= smb2req
->tcon
->compat
;
270 struct lock_struct lock
;
272 req
= tevent_req_create(mem_ctx
, &state
,
273 struct smbd_smb2_write_state
);
277 state
->smb2req
= smb2req
;
278 if (smb2req
->xconn
->protocol
>= PROTOCOL_SMB3_02
) {
279 if (in_flags
& SMB2_WRITEFLAG_WRITE_UNBUFFERED
) {
280 state
->write_through
= true;
283 if (in_flags
& SMB2_WRITEFLAG_WRITE_THROUGH
) {
284 state
->write_through
= true;
286 state
->in_length
= in_data
.length
;
287 state
->out_count
= 0;
289 DEBUG(10,("smbd_smb2_write: %s - %s\n",
290 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
292 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
293 if (tevent_req_nomem(smbreq
, req
)) {
294 return tevent_req_post(req
, ev
);
296 state
->smbreq
= smbreq
;
300 if (IS_IPC(smbreq
->conn
)) {
301 struct tevent_req
*subreq
= NULL
;
303 if (!fsp_is_np(fsp
)) {
304 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
305 return tevent_req_post(req
, ev
);
308 subreq
= np_write_send(state
, ev
,
309 fsp
->fake_file_handle
,
312 if (tevent_req_nomem(subreq
, req
)) {
313 return tevent_req_post(req
, ev
);
315 tevent_req_set_callback(subreq
,
316 smbd_smb2_write_pipe_done
,
321 if (!CHECK_WRITE(fsp
)) {
322 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
323 return tevent_req_post(req
, ev
);
326 /* Try and do an asynchronous write. */
327 status
= schedule_aio_smb2_write(conn
,
332 state
->write_through
);
334 if (NT_STATUS_IS_OK(status
)) {
336 * Doing an async write, allow this
337 * request to be canceled
339 tevent_req_set_cancel_fn(req
, smbd_smb2_write_cancel
);
343 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
344 /* Real error in setting up aio. Fail. */
345 tevent_req_nterror(req
, status
);
346 return tevent_req_post(req
, ev
);
349 /* Fallback to synchronous. */
350 init_strict_lock_struct(fsp
,
351 fsp
->op
->global
->open_persistent_id
,
357 if (!SMB_VFS_STRICT_LOCK_CHECK(conn
, fsp
, &lock
)) {
358 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
359 return tevent_req_post(req
, ev
);
363 * Note: in_data.data is NULL for the recvfile case.
365 nwritten
= write_file(smbreq
, fsp
,
366 (const char *)in_data
.data
,
370 status
= smb2_write_complete(req
, nwritten
, errno
);
372 DEBUG(10,("smb2: write on "
373 "file %s, offset %.0f, requested %u, written = %u\n",
376 (unsigned int)in_data
.length
,
377 (unsigned int)nwritten
));
379 if (!NT_STATUS_IS_OK(status
)) {
380 tevent_req_nterror(req
, status
);
383 tevent_req_done(req
);
386 return tevent_req_post(req
, ev
);
389 static void smbd_smb2_write_pipe_done(struct tevent_req
*subreq
)
391 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
393 struct smbd_smb2_write_state
*state
= tevent_req_data(req
,
394 struct smbd_smb2_write_state
);
396 ssize_t nwritten
= -1;
398 status
= np_write_recv(subreq
, &nwritten
);
400 if (!NT_STATUS_IS_OK(status
)) {
401 NTSTATUS old
= status
;
402 status
= nt_status_np_pipe(old
);
403 tevent_req_nterror(req
, status
);
407 if ((nwritten
== 0 && state
->in_length
!= 0) || (nwritten
< 0)) {
408 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
412 state
->out_count
= nwritten
;
414 tevent_req_done(req
);
417 static NTSTATUS
smbd_smb2_write_recv(struct tevent_req
*req
,
421 struct smbd_smb2_write_state
*state
= tevent_req_data(req
,
422 struct smbd_smb2_write_state
);
424 if (tevent_req_is_nterror(req
, &status
)) {
425 tevent_req_received(req
);
429 *out_count
= state
->out_count
;
431 tevent_req_received(req
);