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"
29 #define DBGC_CLASS DBGC_SMB2
31 static struct tevent_req
*smbd_smb2_write_send(TALLOC_CTX
*mem_ctx
,
32 struct tevent_context
*ev
,
33 struct smbd_smb2_request
*smb2req
,
34 struct files_struct
*in_fsp
,
38 static NTSTATUS
smbd_smb2_write_recv(struct tevent_req
*req
,
41 static void smbd_smb2_request_write_done(struct tevent_req
*subreq
);
42 NTSTATUS
smbd_smb2_request_process_write(struct smbd_smb2_request
*req
)
44 struct smbXsrv_connection
*xconn
= req
->xconn
;
46 const uint8_t *inbody
;
47 uint16_t in_data_offset
;
48 uint32_t in_data_length
;
49 DATA_BLOB in_data_buffer
;
51 uint64_t in_file_id_persistent
;
52 uint64_t in_file_id_volatile
;
53 struct files_struct
*in_fsp
;
55 size_t in_dyn_len
= 0;
56 uint8_t *in_dyn_ptr
= NULL
;
57 struct tevent_req
*subreq
;
59 status
= smbd_smb2_request_verify_sizes(req
, 0x31);
60 if (!NT_STATUS_IS_OK(status
)) {
61 return smbd_smb2_request_error(req
, status
);
63 inbody
= SMBD_SMB2_IN_BODY_PTR(req
);
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
+ SMBD_SMB2_IN_BODY_LEN(req
))) {
73 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
76 if (req
->smb1req
!= NULL
&& req
->smb1req
->unread_bytes
> 0) {
78 in_dyn_len
= req
->smb1req
->unread_bytes
;
80 in_dyn_ptr
= SMBD_SMB2_IN_DYN_PTR(req
);
81 in_dyn_len
= SMBD_SMB2_IN_DYN_LEN(req
);
84 if (in_data_length
> in_dyn_len
) {
85 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
88 /* check the max write size */
89 if (in_data_length
> xconn
->smb2
.server
.max_write
) {
90 DEBUG(2,("smbd_smb2_request_process_write : "
91 "client ignored max write :%s: 0x%08X: 0x%08X\n",
92 __location__
, in_data_length
, xconn
->smb2
.server
.max_write
));
93 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
97 * Note: that in_dyn_ptr is NULL for the recvfile case.
99 in_data_buffer
.data
= in_dyn_ptr
;
100 in_data_buffer
.length
= in_data_length
;
102 status
= smbd_smb2_request_verify_creditcharge(req
, in_data_length
);
103 if (!NT_STATUS_IS_OK(status
)) {
104 return smbd_smb2_request_error(req
, status
);
107 in_fsp
= file_fsp_smb2(req
, in_file_id_persistent
, in_file_id_volatile
);
108 if (in_fsp
== NULL
) {
109 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
112 subreq
= smbd_smb2_write_send(req
, req
->sconn
->ev_ctx
,
117 if (subreq
== NULL
) {
118 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
120 tevent_req_set_callback(subreq
, smbd_smb2_request_write_done
, req
);
122 return smbd_smb2_request_pending_queue(req
, subreq
, 500);
125 static void smbd_smb2_request_write_done(struct tevent_req
*subreq
)
127 struct smbd_smb2_request
*req
= tevent_req_callback_data(subreq
,
128 struct smbd_smb2_request
);
131 uint32_t out_count
= 0;
133 NTSTATUS error
; /* transport error */
135 status
= smbd_smb2_write_recv(subreq
, &out_count
);
137 if (!NT_STATUS_IS_OK(status
)) {
138 error
= smbd_smb2_request_error(req
, status
);
139 if (!NT_STATUS_IS_OK(error
)) {
140 smbd_server_connection_terminate(req
->xconn
,
147 outbody
= smbd_smb2_generate_outbody(req
, 0x10);
148 if (outbody
.data
== NULL
) {
149 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
150 if (!NT_STATUS_IS_OK(error
)) {
151 smbd_server_connection_terminate(req
->xconn
,
158 SSVAL(outbody
.data
, 0x00, 0x10 + 1); /* struct size */
159 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
160 SIVAL(outbody
.data
, 0x04, out_count
); /* count */
161 SIVAL(outbody
.data
, 0x08, 0); /* remaining */
162 SSVAL(outbody
.data
, 0x0C, 0); /* write channel info offset */
163 SSVAL(outbody
.data
, 0x0E, 0); /* write channel info length */
165 outdyn
= data_blob_const(NULL
, 0);
167 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
168 if (!NT_STATUS_IS_OK(error
)) {
169 smbd_server_connection_terminate(req
->xconn
, nt_errstr(error
));
174 struct smbd_smb2_write_state
{
175 struct smbd_smb2_request
*smb2req
;
176 struct smb_request
*smbreq
;
184 static void smbd_smb2_write_pipe_done(struct tevent_req
*subreq
);
186 static NTSTATUS
smb2_write_complete_internal(struct tevent_req
*req
,
187 ssize_t nwritten
, int err
,
191 struct smbd_smb2_write_state
*state
= tevent_req_data(req
,
192 struct smbd_smb2_write_state
);
193 files_struct
*fsp
= state
->fsp
;
195 if (nwritten
== -1) {
196 if (err
== EOVERFLOW
&& fsp_is_alternate_stream(fsp
)) {
197 status
= NT_STATUS_FILE_SYSTEM_LIMITATION
;
199 status
= map_nt_error_from_unix(err
);
202 DEBUG(2, ("smb2_write failed: %s, file %s, "
203 "length=%lu offset=%lu nwritten=-1: %s\n",
206 (unsigned long)state
->in_length
,
207 (unsigned long)state
->in_offset
,
213 DEBUG(3,("smb2: %s, file %s, "
214 "length=%lu offset=%lu wrote=%lu\n",
217 (unsigned long)state
->in_length
,
218 (unsigned long)state
->in_offset
,
219 (unsigned long)nwritten
));
221 if ((nwritten
== 0) && (state
->in_length
!= 0)) {
222 DEBUG(5,("smb2: write [%s] disk full\n",
224 return NT_STATUS_DISK_FULL
;
228 status
= sync_file(fsp
->conn
, fsp
, state
->write_through
);
229 if (!NT_STATUS_IS_OK(status
)) {
230 DEBUG(5,("smb2: sync_file for %s returned %s\n",
237 state
->out_count
= nwritten
;
242 NTSTATUS
smb2_write_complete(struct tevent_req
*req
, ssize_t nwritten
, int err
)
244 return smb2_write_complete_internal(req
, nwritten
, err
, true);
247 NTSTATUS
smb2_write_complete_nosync(struct tevent_req
*req
, ssize_t nwritten
,
250 return smb2_write_complete_internal(req
, nwritten
, err
, false);
254 static bool smbd_smb2_write_cancel(struct tevent_req
*req
)
256 struct smbd_smb2_write_state
*state
=
258 struct smbd_smb2_write_state
);
260 return cancel_smb2_aio(state
->smbreq
);
263 static struct tevent_req
*smbd_smb2_write_send(TALLOC_CTX
*mem_ctx
,
264 struct tevent_context
*ev
,
265 struct smbd_smb2_request
*smb2req
,
266 struct files_struct
*fsp
,
272 struct tevent_req
*req
= NULL
;
273 struct smbd_smb2_write_state
*state
= NULL
;
274 struct smb_request
*smbreq
= NULL
;
275 connection_struct
*conn
= smb2req
->tcon
->compat
;
277 struct lock_struct lock
;
279 req
= tevent_req_create(mem_ctx
, &state
,
280 struct smbd_smb2_write_state
);
284 state
->smb2req
= smb2req
;
285 if (smb2req
->xconn
->protocol
>= PROTOCOL_SMB3_02
) {
286 if (in_flags
& SMB2_WRITEFLAG_WRITE_UNBUFFERED
) {
287 state
->write_through
= true;
290 if (in_flags
& SMB2_WRITEFLAG_WRITE_THROUGH
) {
291 state
->write_through
= true;
293 state
->in_length
= in_data
.length
;
294 state
->in_offset
= in_offset
;
295 state
->out_count
= 0;
297 DEBUG(10,("smbd_smb2_write: %s - %s\n",
298 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
300 smbreq
= smbd_smb2_fake_smb_request(smb2req
, fsp
);
301 if (tevent_req_nomem(smbreq
, req
)) {
302 return tevent_req_post(req
, ev
);
304 state
->smbreq
= smbreq
;
308 if (IS_IPC(smbreq
->conn
)) {
309 struct tevent_req
*subreq
= NULL
;
311 if (!fsp_is_np(fsp
)) {
312 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
313 return tevent_req_post(req
, ev
);
316 subreq
= np_write_send(state
, ev
,
317 fsp
->fake_file_handle
,
320 if (tevent_req_nomem(subreq
, req
)) {
321 return tevent_req_post(req
, ev
);
323 tevent_req_set_callback(subreq
,
324 smbd_smb2_write_pipe_done
,
329 if (!CHECK_WRITE(fsp
)) {
330 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
331 return tevent_req_post(req
, ev
);
334 /* Try and do an asynchronous write. */
335 status
= schedule_aio_smb2_write(conn
,
340 state
->write_through
);
342 if (NT_STATUS_IS_OK(status
)) {
344 * Doing an async write, allow this
345 * request to be canceled
347 tevent_req_set_cancel_fn(req
, smbd_smb2_write_cancel
);
351 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
352 /* Real error in setting up aio. Fail. */
353 tevent_req_nterror(req
, status
);
354 return tevent_req_post(req
, ev
);
357 /* Fallback to synchronous. */
358 init_strict_lock_struct(fsp
,
359 fsp
->op
->global
->open_persistent_id
,
363 lp_posix_cifsu_locktype(fsp
),
366 if (!SMB_VFS_STRICT_LOCK_CHECK(conn
, fsp
, &lock
)) {
367 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
368 return tevent_req_post(req
, ev
);
372 * Note: in_data.data is NULL for the recvfile case.
374 nwritten
= write_file(smbreq
, fsp
,
375 (const char *)in_data
.data
,
379 status
= smb2_write_complete(req
, nwritten
, errno
);
381 DEBUG(10,("smb2: write on "
382 "file %s, offset %.0f, requested %u, written = %u\n",
385 (unsigned int)in_data
.length
,
386 (unsigned int)nwritten
));
388 if (!NT_STATUS_IS_OK(status
)) {
389 tevent_req_nterror(req
, status
);
392 tevent_req_done(req
);
395 return tevent_req_post(req
, ev
);
398 static void smbd_smb2_write_pipe_done(struct tevent_req
*subreq
)
400 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
402 struct smbd_smb2_write_state
*state
= tevent_req_data(req
,
403 struct smbd_smb2_write_state
);
405 ssize_t nwritten
= -1;
407 status
= np_write_recv(subreq
, &nwritten
);
409 if (!NT_STATUS_IS_OK(status
)) {
410 NTSTATUS old
= status
;
411 status
= nt_status_np_pipe(old
);
412 tevent_req_nterror(req
, status
);
416 if ((nwritten
== 0 && state
->in_length
!= 0) || (nwritten
< 0)) {
417 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
421 state
->out_count
= nwritten
;
423 tevent_req_done(req
);
426 static NTSTATUS
smbd_smb2_write_recv(struct tevent_req
*req
,
430 struct smbd_smb2_write_state
*state
= tevent_req_data(req
,
431 struct smbd_smb2_write_state
);
433 if (tevent_req_is_nterror(req
, &status
)) {
434 tevent_req_received(req
);
438 *out_count
= state
->out_count
;
440 tevent_req_received(req
);