*/
#include "includes.h"
+#include "libsmb/libsmb.h"
+#include "../lib/util/tevent_ntstatus.h"
+#include "async_smb.h"
+#include "trans2.h"
/****************************************************************************
Calculate the recommended read buffer size
static size_t cli_read_max_bufsize(struct cli_state *cli)
{
if (!client_is_signing_on(cli) && !cli_encryption_on(cli)
- && (cli->posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
+ && (cli->server_posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
return CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
}
if (cli->capabilities & CAP_LARGE_READX) {
if (write_mode == 0 &&
!client_is_signing_on(cli) &&
!cli_encryption_on(cli) &&
- (cli->posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
+ (cli->server_posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
(cli->capabilities & CAP_LARGE_FILES)) {
/* Only do massive writes if we can do them direct
* with no signing or encrypting - not on a pipe. */
{
struct tevent_req *req, *subreq;
struct cli_read_andx_state *state;
- bool bigoffset = False;
uint8_t wct = 10;
if (size > cli_read_max_bufsize(cli)) {
SSVAL(state->vwv + 9, 0, 0);
if ((uint64_t)offset >> 32) {
- bigoffset = true;
SIVAL(state->vwv + 10, 0,
(((uint64_t)offset)>>32) & 0xffffffff);
wct += 2;
off_t offset, size_t size)
{
struct tevent_req *req, *subreq;
+ NTSTATUS status;
req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
&subreq);
- if ((req == NULL) || !cli_smb_req_send(subreq)) {
- TALLOC_FREE(req);
+ if (req == NULL) {
return NULL;
}
+
+ status = cli_smb_req_send(subreq);
+ if (tevent_req_nterror(req, status)) {
+ return tevent_req_post(req, ev);
+ }
return req;
}
uint32_t num_bytes;
uint8_t *bytes;
- state->status = cli_smb_recv(subreq, 12, &wct, &vwv, &num_bytes,
- &bytes);
+ state->status = cli_smb_recv(subreq, state, &inbuf, 12, &wct, &vwv,
+ &num_bytes, &bytes);
+ TALLOC_FREE(subreq);
if (NT_STATUS_IS_ERR(state->status)) {
tevent_req_nterror(req, state->status);
return;
return;
}
- inbuf = cli_smb_inbuf(subreq);
- state->buf = (uint8_t *)smb_base(inbuf) + SVAL(vwv+6, 0);
+ state->buf = discard_const_p(uint8_t, smb_base(inbuf)) + SVAL(vwv+6, 0);
if (trans_oob(smb_len(inbuf), SVAL(vwv+6, 0), state->received)
- || (state->buf < bytes)) {
+ || ((state->received != 0) && (state->buf < bytes))) {
DEBUG(5, ("server returned invalid read&x data offset\n"));
tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
return;
return NT_STATUS_OK;
}
+struct cli_readall_state {
+ struct tevent_context *ev;
+ struct cli_state *cli;
+ uint16_t fnum;
+ off_t start_offset;
+ size_t size;
+ size_t received;
+ uint8_t *buf;
+};
+
+static void cli_readall_done(struct tevent_req *subreq);
+
+static struct tevent_req *cli_readall_send(TALLOC_CTX *mem_ctx,
+ struct event_context *ev,
+ struct cli_state *cli,
+ uint16_t fnum,
+ off_t offset, size_t size)
+{
+ struct tevent_req *req, *subreq;
+ struct cli_readall_state *state;
+
+ req = tevent_req_create(mem_ctx, &state, struct cli_readall_state);
+ if (req == NULL) {
+ return NULL;
+ }
+ state->ev = ev;
+ state->cli = cli;
+ state->fnum = fnum;
+ state->start_offset = offset;
+ state->size = size;
+ state->received = 0;
+ state->buf = NULL;
+
+ subreq = cli_read_andx_send(state, ev, cli, fnum, offset, size);
+ if (tevent_req_nomem(subreq, req)) {
+ return tevent_req_post(req, ev);
+ }
+ tevent_req_set_callback(subreq, cli_readall_done, req);
+ return req;
+}
+
+static void cli_readall_done(struct tevent_req *subreq)
+{
+ struct tevent_req *req = tevent_req_callback_data(
+ subreq, struct tevent_req);
+ struct cli_readall_state *state = tevent_req_data(
+ req, struct cli_readall_state);
+ ssize_t received;
+ uint8_t *buf;
+ NTSTATUS status;
+
+ status = cli_read_andx_recv(subreq, &received, &buf);
+ if (tevent_req_nterror(req, status)) {
+ return;
+ }
+
+ if (received == 0) {
+ /* EOF */
+ tevent_req_done(req);
+ return;
+ }
+
+ if ((state->received == 0) && (received == state->size)) {
+ /* Ideal case: Got it all in one run */
+ state->buf = buf;
+ state->received += received;
+ tevent_req_done(req);
+ return;
+ }
+
+ /*
+ * We got a short read, issue a read for the
+ * rest. Unfortunately we have to allocate the buffer
+ * ourselves now, as our caller expects to receive a single
+ * buffer. cli_read_andx does it from the buffer received from
+ * the net, but with a short read we have to put it together
+ * from several reads.
+ */
+
+ if (state->buf == NULL) {
+ state->buf = talloc_array(state, uint8_t, state->size);
+ if (tevent_req_nomem(state->buf, req)) {
+ return;
+ }
+ }
+ memcpy(state->buf + state->received, buf, received);
+ state->received += received;
+
+ TALLOC_FREE(subreq);
+
+ if (state->received >= state->size) {
+ tevent_req_done(req);
+ return;
+ }
+
+ subreq = cli_read_andx_send(state, state->ev, state->cli, state->fnum,
+ state->start_offset + state->received,
+ state->size - state->received);
+ if (tevent_req_nomem(subreq, req)) {
+ return;
+ }
+ tevent_req_set_callback(subreq, cli_readall_done, req);
+}
+
+static NTSTATUS cli_readall_recv(struct tevent_req *req, ssize_t *received,
+ uint8_t **rcvbuf)
+{
+ struct cli_readall_state *state = tevent_req_data(
+ req, struct cli_readall_state);
+ NTSTATUS status;
+
+ if (tevent_req_is_nterror(req, &status)) {
+ return status;
+ }
+ *received = state->received;
+ *rcvbuf = state->buf;
+ return NT_STATUS_OK;
+}
+
struct cli_pull_subreq {
struct tevent_req *req;
ssize_t received;
req, struct cli_pull_state);
char *result;
- result = tevent_req_print(mem_ctx, req);
+ result = tevent_req_default_print(req, mem_ctx);
if (result == NULL) {
return NULL;
}
size_left = size - state->requested;
request_thistime = MIN(size_left, state->chunk_size);
- subreq->req = cli_read_andx_send(
+ subreq->req = cli_readall_send(
state->reqs, ev, cli, fnum,
state->start_offset + state->requested,
request_thistime);
return;
}
- status = cli_read_andx_recv(subreq, &pull_subreq->received,
- &pull_subreq->buf);
+ status = cli_readall_recv(subreq, &pull_subreq->received,
+ &pull_subreq->buf);
if (!NT_STATUS_IS_OK(status)) {
tevent_req_nterror(state->req, status);
return;
status = state->sink((char *)top_subreq->buf,
top_subreq->received, state->priv);
- if (!NT_STATUS_IS_OK(status)) {
- tevent_req_nterror(state->req, status);
+ if (tevent_req_nterror(state->req, status)) {
return;
}
state->pushed += top_subreq->received;
+ state->requested),
state->top_req));
- new_req = cli_read_andx_send(
+ new_req = cli_readall_send(
state->reqs, state->ev, state->cli,
state->fnum,
state->start_offset + state->requested,
status = cli_pull_recv(req, received);
fail:
TALLOC_FREE(frame);
- if (!NT_STATUS_IS_OK(status)) {
- cli_set_error(cli, status);
- }
return status;
}
status = cli_pull(cli, fnum, offset, size, size,
cli_read_sink, &buf, &ret);
if (!NT_STATUS_IS_OK(status)) {
- cli_set_error(cli, status);
return -1;
}
return ret;
}
/****************************************************************************
- Issue a single SMBwrite and don't wait for a reply.
+ write to a file using a SMBwrite and not bypassing 0 byte writes
****************************************************************************/
-static bool cli_issue_write(struct cli_state *cli,
- uint16_t fnum,
- off_t offset,
- uint16 mode,
- const char *buf,
- size_t size,
- int i)
+NTSTATUS cli_smbwrite(struct cli_state *cli, uint16_t fnum, char *buf,
+ off_t offset, size_t size1, size_t *ptotal)
{
- char *p;
- bool large_writex = false;
- /* We can only do direct writes if not signing and not encrypting. */
- bool direct_writes = !client_is_signing_on(cli) && !cli_encryption_on(cli);
-
- if (!direct_writes && size + 1 > cli->bufsize) {
- cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
- if (!cli->outbuf) {
- return False;
- }
- cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
- if (cli->inbuf == NULL) {
- SAFE_FREE(cli->outbuf);
- return False;
- }
- cli->bufsize = size + 1024;
- }
-
- memset(cli->outbuf,'\0',smb_size);
- memset(cli->inbuf,'\0',smb_size);
-
- if (cli->capabilities & CAP_LARGE_FILES) {
- large_writex = True;
- }
-
- if (large_writex) {
- cli_set_message(cli->outbuf,14,0,True);
- } else {
- cli_set_message(cli->outbuf,12,0,True);
- }
-
- SCVAL(cli->outbuf,smb_com,SMBwriteX);
- SSVAL(cli->outbuf,smb_tid,cli->cnum);
- cli_setup_packet(cli);
-
- SCVAL(cli->outbuf,smb_vwv0,0xFF);
- SSVAL(cli->outbuf,smb_vwv2,fnum);
-
- SIVAL(cli->outbuf,smb_vwv3,offset);
- SIVAL(cli->outbuf,smb_vwv5,0);
- SSVAL(cli->outbuf,smb_vwv7,mode);
+ uint8_t *bytes;
+ ssize_t total = 0;
- SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
/*
- * According to CIFS-TR-1p00, this following field should only
- * be set if CAP_LARGE_WRITEX is set. We should check this
- * locally. However, this check might already have been
- * done by our callers.
+ * 3 bytes prefix
*/
- SSVAL(cli->outbuf,smb_vwv9,(size>>16));
- SSVAL(cli->outbuf,smb_vwv10,size);
- /* +1 is pad byte. */
- SSVAL(cli->outbuf,smb_vwv11,
- smb_buf(cli->outbuf) - smb_base(cli->outbuf) + 1);
-
- if (large_writex) {
- SIVAL(cli->outbuf,smb_vwv12,(((uint64_t)offset)>>32) & 0xffffffff);
- }
- p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11) -1;
- *p++ = '\0'; /* pad byte. */
- if (!direct_writes) {
- memcpy(p, buf, size);
+ bytes = TALLOC_ARRAY(talloc_tos(), uint8_t, 3);
+ if (bytes == NULL) {
+ return NT_STATUS_NO_MEMORY;
}
- if (size > 0x1FFFF) {
- /* This is a POSIX 14 word large write. */
- set_message_bcc(cli->outbuf, 0); /* Set bcc to zero. */
- _smb_setlen_large(cli->outbuf,smb_size + 28 + 1 /* pad */ + size - 4);
- } else {
- cli_setup_bcc(cli, p+size);
- }
-
- SSVAL(cli->outbuf,smb_mid,cli->mid + i);
+ bytes[0] = 1;
- show_msg(cli->outbuf);
- if (direct_writes) {
- /* For direct writes we now need to write the data
- * directly out of buf. */
- return cli_send_smb_direct_writeX(cli, buf, size);
- } else {
- return cli_send_smb(cli);
- }
-}
-
-/****************************************************************************
- write to a file
- write_mode: 0x0001 disallow write cacheing
- 0x0002 return bytes remaining
- 0x0004 use raw named pipe protocol
- 0x0008 start of message mode named pipe protocol
-****************************************************************************/
-
-ssize_t cli_write(struct cli_state *cli,
- uint16_t fnum, uint16 write_mode,
- const char *buf, off_t offset, size_t size)
-{
- ssize_t bwritten = 0;
- unsigned int issued = 0;
- unsigned int received = 0;
- int mpx = 1;
- size_t writesize;
- int blocks;
-
- if(cli->max_mux > 1) {
- mpx = cli->max_mux-1;
- } else {
- mpx = 1;
- }
-
- writesize = cli_write_max_bufsize(cli, write_mode);
-
- blocks = (size + (writesize-1)) / writesize;
-
- while (received < blocks) {
-
- while ((issued - received < mpx) && (issued < blocks)) {
- ssize_t bsent = issued * writesize;
- ssize_t size1 = MIN(writesize, size - bsent);
-
- if (!cli_issue_write(cli, fnum, offset + bsent,
- write_mode,
- buf + bsent,
- size1, issued))
- return -1;
- issued++;
+ do {
+ size_t size = MIN(size1, cli->max_xmit - 48);
+ struct tevent_req *req;
+ uint16_t vwv[5];
+ uint16_t *ret_vwv;
+ NTSTATUS status;
+
+ SSVAL(vwv+0, 0, fnum);
+ SSVAL(vwv+1, 0, size);
+ SIVAL(vwv+2, 0, offset);
+ SSVAL(vwv+4, 0, 0);
+
+ bytes = talloc_realloc(talloc_tos(), bytes, uint8_t,
+ size+3);
+ if (bytes == NULL) {
+ return NT_STATUS_NO_MEMORY;
}
+ SSVAL(bytes, 1, size);
+ memcpy(bytes + 3, buf + total, size);
- if (!cli_receive_smb(cli)) {
- return bwritten;
+ status = cli_smb(talloc_tos(), cli, SMBwrite, 0, 5, vwv,
+ size+3, bytes, &req, 1, NULL, &ret_vwv,
+ NULL, NULL);
+ if (!NT_STATUS_IS_OK(status)) {
+ TALLOC_FREE(bytes);
+ return status;
}
- received++;
-
- if (cli_is_error(cli))
+ size = SVAL(ret_vwv+0, 0);
+ TALLOC_FREE(req);
+ if (size == 0) {
break;
-
- bwritten += SVAL(cli->inbuf, smb_vwv2);
- if (writesize > 0xFFFF) {
- bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
}
- }
-
- while (received < issued && cli_receive_smb(cli)) {
- received++;
- }
-
- return bwritten;
-}
-
-/****************************************************************************
- write to a file using a SMBwrite and not bypassing 0 byte writes
-****************************************************************************/
-
-ssize_t cli_smbwrite(struct cli_state *cli,
- uint16_t fnum, char *buf, off_t offset, size_t size1)
-{
- char *p;
- ssize_t total = 0;
-
- do {
- size_t size = MIN(size1, cli->max_xmit - 48);
-
- memset(cli->outbuf,'\0',smb_size);
- memset(cli->inbuf,'\0',smb_size);
-
- cli_set_message(cli->outbuf,5, 0,True);
-
- SCVAL(cli->outbuf,smb_com,SMBwrite);
- SSVAL(cli->outbuf,smb_tid,cli->cnum);
- cli_setup_packet(cli);
-
- SSVAL(cli->outbuf,smb_vwv0,fnum);
- SSVAL(cli->outbuf,smb_vwv1,size);
- SIVAL(cli->outbuf,smb_vwv2,offset);
- SSVAL(cli->outbuf,smb_vwv4,0);
-
- p = smb_buf(cli->outbuf);
- *p++ = 1;
- SSVAL(p, 0, size); p += 2;
- memcpy(p, buf + total, size); p += size;
-
- cli_setup_bcc(cli, p);
-
- if (!cli_send_smb(cli))
- return -1;
-
- if (!cli_receive_smb(cli))
- return -1;
-
- if (cli_is_error(cli))
- return -1;
-
- size = SVAL(cli->inbuf,smb_vwv0);
- if (size == 0)
- break;
-
size1 -= size;
total += size;
offset += size;
} while (size1);
- return total;
+ TALLOC_FREE(bytes);
+
+ if (ptotal != NULL) {
+ *ptotal = total;
+ }
+ return NT_STATUS_OK;
}
/*
}
state->pad = 0;
- state->iov[0].iov_base = &state->pad;
+ state->iov[0].iov_base = (void *)&state->pad;
state->iov[0].iov_len = 1;
- state->iov[1].iov_base = CONST_DISCARD(uint8_t *, buf);
+ state->iov[1].iov_base = discard_const_p(void, buf);
state->iov[1].iov_len = size;
subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
off_t offset, size_t size)
{
struct tevent_req *req, *subreq;
+ NTSTATUS status;
req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
size, NULL, 0, &subreq);
- if ((req == NULL) || !cli_smb_req_send(subreq)) {
- TALLOC_FREE(req);
+ if (req == NULL) {
return NULL;
}
+
+ status = cli_smb_req_send(subreq);
+ if (tevent_req_nterror(req, status)) {
+ return tevent_req_post(req, ev);
+ }
return req;
}
req, struct cli_write_andx_state);
uint8_t wct;
uint16_t *vwv;
+ uint8_t *inbuf;
NTSTATUS status;
- status = cli_smb_recv(subreq, 6, &wct, &vwv, NULL, NULL);
+ status = cli_smb_recv(subreq, state, &inbuf, 6, &wct, &vwv,
+ NULL, NULL);
+ TALLOC_FREE(subreq);
if (NT_STATUS_IS_ERR(status)) {
- TALLOC_FREE(subreq);
tevent_req_nterror(req, status);
return;
}
status = cli_write_andx_recv(subreq, &written);
TALLOC_FREE(subreq);
- if (!NT_STATUS_IS_OK(status)) {
- tevent_req_nterror(req, status);
+ if (tevent_req_nterror(req, status)) {
return;
}
tevent_req_set_callback(subreq, cli_writeall_written, req);
}
-static NTSTATUS cli_writeall_recv(struct tevent_req *req)
+static NTSTATUS cli_writeall_recv(struct tevent_req *req,
+ size_t *pwritten)
{
- return tevent_req_simple_recv_ntstatus(req);
+ struct cli_writeall_state *state = tevent_req_data(
+ req, struct cli_writeall_state);
+ NTSTATUS status;
+
+ if (tevent_req_is_nterror(req, &status)) {
+ return status;
+ }
+ if (pwritten != NULL) {
+ *pwritten = state->written;
+ }
+ return NT_STATUS_OK;
+}
+
+NTSTATUS cli_writeall(struct cli_state *cli, uint16_t fnum, uint16_t mode,
+ const uint8_t *buf, off_t offset, size_t size,
+ size_t *pwritten)
+{
+ TALLOC_CTX *frame = talloc_stackframe();
+ struct event_context *ev;
+ struct tevent_req *req;
+ NTSTATUS status = NT_STATUS_NO_MEMORY;
+
+ if (cli_has_async_calls(cli)) {
+ /*
+ * Can't use sync call while an async call is in flight
+ */
+ status = NT_STATUS_INVALID_PARAMETER;
+ goto fail;
+ }
+ ev = event_context_init(frame);
+ if (ev == NULL) {
+ goto fail;
+ }
+ req = cli_writeall_send(frame, ev, cli, fnum, mode, buf, offset, size);
+ if (req == NULL) {
+ goto fail;
+ }
+ if (!tevent_req_poll(req, ev)) {
+ status = map_nt_error_from_unix(errno);
+ goto fail;
+ }
+ status = cli_writeall_recv(req, pwritten);
+ fail:
+ TALLOC_FREE(frame);
+ return status;
}
struct cli_push_write_state {
state->reqs[idx] = NULL;
state->pending -= 1;
- status = cli_writeall_recv(subreq);
+ status = cli_writeall_recv(subreq, NULL);
TALLOC_FREE(subreq);
TALLOC_FREE(substate);
- if (!NT_STATUS_IS_OK(status)) {
- tevent_req_nterror(req, status);
+ if (tevent_req_nterror(req, status)) {
return;
}
status = cli_push_recv(req);
fail:
TALLOC_FREE(frame);
- if (!NT_STATUS_IS_OK(status)) {
- cli_set_error(cli, status);
- }
return status;
}