From f73bcf4934be89f83e86459bc695b7d28348565c Mon Sep 17 00:00:00 2001 From: Ross Lagerwall Date: Wed, 27 May 2015 23:13:15 +0100 Subject: s3: libsmbclient: Add server-side copy support Introduce a new operation, splice, which copies data from one SMBCFILE to another. Implement this operation using FSCTL_SRV_COPYCHUNK_WRITE for SMB2+ protocols and using read+write for older protocols. Since the operation may be long running, it takes a callback which gets called periodically to indicate progress to the application and given an opportunity to stop it. Signed-off-by: Ross Lagerwall Reviewed-by: David Disseldorp Reviewed-by: Jeremy Allison --- libcli/smb/smb2cli_ioctl.c | 51 ++++++++++++++++++++++++++++++++++++++++++---- libcli/smb/smbXcli_base.c | 32 +++++++++++++++++++++++++++++ libcli/smb/smbXcli_base.h | 6 ++++++ 3 files changed, 85 insertions(+), 4 deletions(-) (limited to 'libcli') diff --git a/libcli/smb/smb2cli_ioctl.c b/libcli/smb/smb2cli_ioctl.c index b0f8eea65b0..42a424e6b9f 100644 --- a/libcli/smb/smb2cli_ioctl.c +++ b/libcli/smb/smb2cli_ioctl.c @@ -23,6 +23,7 @@ #include "smb_common.h" #include "smbXcli_base.h" #include "librpc/ndr/libndr.h" +#include "librpc/gen_ndr/ioctl.h" struct smb2cli_ioctl_state { uint8_t fixed[0x38]; @@ -32,6 +33,7 @@ struct smb2cli_ioctl_state { struct iovec *recv_iov; DATA_BLOB out_input_buffer; DATA_BLOB out_output_buffer; + uint32_t ctl_code; }; static void smb2cli_ioctl_done(struct tevent_req *subreq); @@ -69,6 +71,7 @@ struct tevent_req *smb2cli_ioctl_send(TALLOC_CTX *mem_ctx, if (req == NULL) { return NULL; } + state->ctl_code = in_ctl_code; state->max_input_length = in_max_input_length; state->max_output_length = in_max_output_length; @@ -158,6 +161,32 @@ struct tevent_req *smb2cli_ioctl_send(TALLOC_CTX *mem_ctx, return req; } +/* + * 3.3.4.4 Sending an Error Response + * An error code other than one of the following indicates a failure: + */ +static bool smb2cli_ioctl_is_failure(uint32_t ctl_code, NTSTATUS status, + size_t data_size) +{ + if (NT_STATUS_IS_OK(status)) { + return false; + } + + /* + * STATUS_INVALID_PARAMETER in a FSCTL_SRV_COPYCHUNK or + * FSCTL_SRV_COPYCHUNK_WRITE Response, when returning an + * SRV_COPYCHUNK_RESPONSE as described in section 2.2.32.1. + */ + if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER) && + (ctl_code == FSCTL_SRV_COPYCHUNK || + ctl_code == FSCTL_SRV_COPYCHUNK_WRITE) && + data_size == sizeof(struct srv_copychunk_rsp)) { + return false; + } + + return true; +} + static void smb2cli_ioctl_done(struct tevent_req *subreq) { struct tevent_req *req = @@ -195,12 +224,16 @@ static void smb2cli_ioctl_done(struct tevent_req *subreq) .status = NT_STATUS_FILE_CLOSED, .body_size = 0x09, }, + { + .status = NT_STATUS_INVALID_PARAMETER, + .body_size = 0x31 + }, }; status = smb2cli_req_recv(subreq, state, &iov, expected, ARRAY_SIZE(expected)); TALLOC_FREE(subreq); - if (tevent_req_nterror(req, status)) { + if (iov == NULL && tevent_req_nterror(req, status)) { return; } @@ -214,6 +247,11 @@ static void smb2cli_ioctl_done(struct tevent_req *subreq) output_buffer_offset = IVAL(fixed, 0x20); output_buffer_length = IVAL(fixed, 0x24); + if (smb2cli_ioctl_is_failure(state->ctl_code, status, output_buffer_length) && + tevent_req_nterror(req, status)) { + return; + } + if ((input_buffer_offset > 0) && (input_buffer_length > 0)) { uint32_t ofs; @@ -294,6 +332,10 @@ static void smb2cli_ioctl_done(struct tevent_req *subreq) state->out_output_buffer.length = output_buffer_length; } + if (tevent_req_nterror(req, status)) { + return; + } + tevent_req_done(req); } @@ -305,9 +347,10 @@ NTSTATUS smb2cli_ioctl_recv(struct tevent_req *req, struct smb2cli_ioctl_state *state = tevent_req_data(req, struct smb2cli_ioctl_state); - NTSTATUS status; + NTSTATUS status = NT_STATUS_OK; - if (tevent_req_is_nterror(req, &status)) { + if (tevent_req_is_nterror(req, &status) && + smb2cli_ioctl_is_failure(state->ctl_code, status, state->out_output_buffer.length)) { tevent_req_received(req); return status; } @@ -321,7 +364,7 @@ NTSTATUS smb2cli_ioctl_recv(struct tevent_req *req, } tevent_req_received(req); - return NT_STATUS_OK; + return status; } NTSTATUS smb2cli_ioctl(struct smbXcli_conn *conn, diff --git a/libcli/smb/smbXcli_base.c b/libcli/smb/smbXcli_base.c index 075420323d0..2f47fe60d4f 100644 --- a/libcli/smb/smbXcli_base.c +++ b/libcli/smb/smbXcli_base.c @@ -130,6 +130,9 @@ struct smbXcli_conn { uint16_t cur_credits; uint16_t max_credits; + uint32_t cc_chunk_len; + uint32_t cc_max_chunks; + uint8_t io_priority; uint8_t preauth_sha512[64]; @@ -409,6 +412,13 @@ struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx, conn->smb2.max_credits = 0; conn->smb2.io_priority = 1; + /* + * Samba and Windows servers accept a maximum of 16 MiB with a maximum + * chunk length of 1 MiB. + */ + conn->smb2.cc_chunk_len = 1024 * 1024; + conn->smb2.cc_max_chunks = 16; + talloc_set_destructor(conn, smbXcli_conn_destructor); return conn; @@ -2595,6 +2605,28 @@ void smb2cli_conn_set_io_priority(struct smbXcli_conn *conn, conn->smb2.io_priority = io_priority; } +uint32_t smb2cli_conn_cc_chunk_len(struct smbXcli_conn *conn) +{ + return conn->smb2.cc_chunk_len; +} + +void smb2cli_conn_set_cc_chunk_len(struct smbXcli_conn *conn, + uint32_t chunk_len) +{ + conn->smb2.cc_chunk_len = chunk_len; +} + +uint32_t smb2cli_conn_cc_max_chunks(struct smbXcli_conn *conn) +{ + return conn->smb2.cc_max_chunks; +} + +void smb2cli_conn_set_cc_max_chunks(struct smbXcli_conn *conn, + uint32_t max_chunks) +{ + conn->smb2.cc_max_chunks = max_chunks; +} + static void smb2cli_req_cancel_done(struct tevent_req *subreq); static bool smb2cli_req_cancel(struct tevent_req *req) diff --git a/libcli/smb/smbXcli_base.h b/libcli/smb/smbXcli_base.h index 8f27c20557e..cf93135cd97 100644 --- a/libcli/smb/smbXcli_base.h +++ b/libcli/smb/smbXcli_base.h @@ -306,6 +306,12 @@ void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn, uint8_t smb2cli_conn_get_io_priority(struct smbXcli_conn *conn); void smb2cli_conn_set_io_priority(struct smbXcli_conn *conn, uint8_t io_priority); +uint32_t smb2cli_conn_cc_chunk_len(struct smbXcli_conn *conn); +void smb2cli_conn_set_cc_chunk_len(struct smbXcli_conn *conn, + uint32_t chunk_len); +uint32_t smb2cli_conn_cc_max_chunks(struct smbXcli_conn *conn); +void smb2cli_conn_set_cc_max_chunks(struct smbXcli_conn *conn, + uint32_t max_chunks); struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx, struct tevent_context *ev, -- cgit v1.2.1