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
,
36 static NTSTATUS
smbd_smb2_write_recv(struct tevent_req
*req
,
39 static void smbd_smb2_request_write_done(struct tevent_req
*subreq
);
40 NTSTATUS
smbd_smb2_request_process_write(struct smbd_smb2_request
*req
)
44 const uint8_t *inbody
;
45 int i
= req
->current_idx
;
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 struct tevent_req
*subreq
;
57 status
= smbd_smb2_request_verify_sizes(req
, 0x31);
58 if (!NT_STATUS_IS_OK(status
)) {
59 return smbd_smb2_request_error(req
, status
);
61 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
62 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
64 in_smbpid
= IVAL(inhdr
, SMB2_HDR_PID
);
66 in_data_offset
= SVAL(inbody
, 0x02);
67 in_data_length
= IVAL(inbody
, 0x04);
68 in_offset
= BVAL(inbody
, 0x08);
69 in_file_id_persistent
= BVAL(inbody
, 0x10);
70 in_file_id_volatile
= BVAL(inbody
, 0x18);
71 in_flags
= IVAL(inbody
, 0x2C);
73 if (in_data_offset
!= (SMB2_HDR_BODY
+ req
->in
.vector
[i
+1].iov_len
)) {
74 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
77 if (in_data_length
> req
->in
.vector
[i
+2].iov_len
) {
78 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
81 /* check the max write size */
82 if (in_data_length
> req
->sconn
->smb2
.max_write
) {
83 DEBUG(2,("smbd_smb2_request_process_write : "
84 "client ignored max write :%s: 0x%08X: 0x%08X\n",
85 __location__
, in_data_length
, req
->sconn
->smb2
.max_write
));
86 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
89 in_data_buffer
.data
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
90 in_data_buffer
.length
= in_data_length
;
92 status
= smbd_smb2_request_verify_creditcharge(req
, in_data_length
);
93 if (!NT_STATUS_IS_OK(status
)) {
94 return smbd_smb2_request_error(req
, status
);
97 in_fsp
= file_fsp_smb2(req
, in_file_id_persistent
, in_file_id_volatile
);
99 return smbd_smb2_request_error(req
, NT_STATUS_FILE_CLOSED
);
102 subreq
= smbd_smb2_write_send(req
, req
->sconn
->ev_ctx
,
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
, 500);
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
);
122 uint32_t out_count
= 0;
124 NTSTATUS error
; /* transport error */
126 status
= smbd_smb2_write_recv(subreq
, &out_count
);
128 if (!NT_STATUS_IS_OK(status
)) {
129 error
= smbd_smb2_request_error(req
, status
);
130 if (!NT_STATUS_IS_OK(error
)) {
131 smbd_server_connection_terminate(req
->sconn
,
138 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x10);
139 if (outbody
.data
== NULL
) {
140 error
= smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
141 if (!NT_STATUS_IS_OK(error
)) {
142 smbd_server_connection_terminate(req
->sconn
,
149 SSVAL(outbody
.data
, 0x00, 0x10 + 1); /* struct size */
150 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
151 SIVAL(outbody
.data
, 0x04, out_count
); /* count */
152 SIVAL(outbody
.data
, 0x08, 0); /* remaining */
153 SSVAL(outbody
.data
, 0x0C, 0); /* write channel info offset */
154 SSVAL(outbody
.data
, 0x0E, 0); /* write channel info length */
156 outdyn
= data_blob_const(NULL
, 0);
158 error
= smbd_smb2_request_done(req
, outbody
, &outdyn
);
159 if (!NT_STATUS_IS_OK(error
)) {
160 smbd_server_connection_terminate(req
->sconn
, nt_errstr(error
));
165 struct smbd_smb2_write_state
{
166 struct smbd_smb2_request
*smb2req
;
167 struct smb_request
*smbreq
;
175 static void smbd_smb2_write_pipe_done(struct tevent_req
*subreq
);
177 static NTSTATUS
smb2_write_complete_internal(struct tevent_req
*req
,
178 ssize_t nwritten
, int err
,
182 struct smbd_smb2_write_state
*state
= tevent_req_data(req
,
183 struct smbd_smb2_write_state
);
184 files_struct
*fsp
= state
->fsp
;
186 if (nwritten
== -1) {
187 status
= map_nt_error_from_unix(err
);
189 DEBUG(2, ("smb2_write failed: %s, file %s, "
190 "length=%lu offset=%lu nwritten=-1: %s\n",
193 (unsigned long)state
->in_length
,
194 (unsigned long)state
->in_offset
,
200 DEBUG(3,("smb2: %s, file %s, "
201 "length=%lu offset=%lu wrote=%lu\n",
204 (unsigned long)state
->in_length
,
205 (unsigned long)state
->in_offset
,
206 (unsigned long)nwritten
));
208 if ((nwritten
== 0) && (state
->in_length
!= 0)) {
209 DEBUG(5,("smb2: write [%s] disk full\n",
211 return NT_STATUS_DISK_FULL
;
215 status
= sync_file(fsp
->conn
, fsp
, state
->write_through
);
216 if (!NT_STATUS_IS_OK(status
)) {
217 DEBUG(5,("smb2: sync_file for %s returned %s\n",
224 state
->out_count
= nwritten
;
229 NTSTATUS
smb2_write_complete(struct tevent_req
*req
, ssize_t nwritten
, int err
)
231 return smb2_write_complete_internal(req
, nwritten
, err
, true);
234 NTSTATUS
smb2_write_complete_nosync(struct tevent_req
*req
, ssize_t nwritten
,
237 return smb2_write_complete_internal(req
, nwritten
, err
, false);
241 static bool smbd_smb2_write_cancel(struct tevent_req
*req
)
243 struct smbd_smb2_write_state
*state
=
245 struct smbd_smb2_write_state
);
247 state
->smb2req
->cancelled
= true;
249 return cancel_smb2_aio(state
->smbreq
);
252 static struct tevent_req
*smbd_smb2_write_send(TALLOC_CTX
*mem_ctx
,
253 struct tevent_context
*ev
,
254 struct smbd_smb2_request
*smb2req
,
255 struct files_struct
*fsp
,
262 struct tevent_req
*req
= NULL
;
263 struct smbd_smb2_write_state
*state
= NULL
;
264 struct smb_request
*smbreq
= NULL
;
265 connection_struct
*conn
= smb2req
->tcon
->compat
;
267 struct lock_struct lock
;
269 req
= tevent_req_create(mem_ctx
, &state
,
270 struct smbd_smb2_write_state
);
274 state
->smb2req
= smb2req
;
275 if (in_flags
& SMB2_WRITEFLAG_WRITE_THROUGH
) {
276 state
->write_through
= true;
278 state
->in_length
= in_data
.length
;
279 state
->out_count
= 0;
281 DEBUG(10,("smbd_smb2_write: %s - %s\n",
282 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
)));
284 smbreq
= smbd_smb2_fake_smb_request(smb2req
);
285 if (tevent_req_nomem(smbreq
, req
)) {
286 return tevent_req_post(req
, ev
);
288 state
->smbreq
= smbreq
;
292 if (IS_IPC(smbreq
->conn
)) {
293 struct tevent_req
*subreq
= NULL
;
295 if (!fsp_is_np(fsp
)) {
296 tevent_req_nterror(req
, NT_STATUS_FILE_CLOSED
);
297 return tevent_req_post(req
, ev
);
300 subreq
= np_write_send(state
, ev
,
301 fsp
->fake_file_handle
,
304 if (tevent_req_nomem(subreq
, req
)) {
305 return tevent_req_post(req
, ev
);
307 tevent_req_set_callback(subreq
,
308 smbd_smb2_write_pipe_done
,
313 if (!CHECK_WRITE(fsp
)) {
314 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
315 return tevent_req_post(req
, ev
);
318 /* Try and do an asynchronous write. */
319 status
= schedule_aio_smb2_write(conn
,
324 state
->write_through
);
326 if (NT_STATUS_IS_OK(status
)) {
328 * Doing an async write, allow this
329 * request to be canceled
331 tevent_req_set_cancel_fn(req
, smbd_smb2_write_cancel
);
335 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
336 /* Real error in setting up aio. Fail. */
337 tevent_req_nterror(req
, status
);
338 return tevent_req_post(req
, ev
);
341 /* Fallback to synchronous. */
342 init_strict_lock_struct(fsp
,
343 fsp
->op
->global
->open_persistent_id
,
349 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
350 tevent_req_nterror(req
, NT_STATUS_FILE_LOCK_CONFLICT
);
351 return tevent_req_post(req
, ev
);
354 nwritten
= write_file(smbreq
, fsp
,
355 (const char *)in_data
.data
,
359 status
= smb2_write_complete(req
, nwritten
, errno
);
361 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
363 DEBUG(10,("smb2: write on "
364 "file %s, offset %.0f, requested %u, written = %u\n",
367 (unsigned int)in_data
.length
,
368 (unsigned int)nwritten
));
370 if (!NT_STATUS_IS_OK(status
)) {
371 tevent_req_nterror(req
, status
);
374 tevent_req_done(req
);
377 return tevent_req_post(req
, ev
);
380 static void smbd_smb2_write_pipe_done(struct tevent_req
*subreq
)
382 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
384 struct smbd_smb2_write_state
*state
= tevent_req_data(req
,
385 struct smbd_smb2_write_state
);
387 ssize_t nwritten
= -1;
389 status
= np_write_recv(subreq
, &nwritten
);
391 if (!NT_STATUS_IS_OK(status
)) {
392 NTSTATUS old
= status
;
393 status
= nt_status_np_pipe(old
);
394 tevent_req_nterror(req
, status
);
398 if ((nwritten
== 0 && state
->in_length
!= 0) || (nwritten
< 0)) {
399 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
403 state
->out_count
= nwritten
;
405 tevent_req_done(req
);
408 static NTSTATUS
smbd_smb2_write_recv(struct tevent_req
*req
,
412 struct smbd_smb2_write_state
*state
= tevent_req_data(req
,
413 struct smbd_smb2_write_state
);
415 if (tevent_req_is_nterror(req
, &status
)) {
416 tevent_req_received(req
);
420 *out_count
= state
->out_count
;
422 tevent_req_received(req
);