Change the port number used to call on.
****************************************************************************/
-int cli_set_port(struct cli_state *cli, int port)
+void cli_set_port(struct cli_state *cli, int port)
{
cli->port = port;
- return port;
}
/****************************************************************************
- Read an smb from a fd ignoring all keepalive packets. Note that the buffer
- *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
+ convenience routine to find if we negotiated ucs2
+****************************************************************************/
+
+bool cli_ucs2(struct cli_state *cli)
+{
+ return ((cli->capabilities & CAP_UNICODE) != 0);
+}
+
+
+/****************************************************************************
+ Read an smb from a fd ignoring all keepalive packets.
The timeout is in milliseconds
This is exactly the same as receive_smb except that it never returns
set_smb_read_error(&cli->smb_rw_error, SMB_READ_OK);
- status = receive_smb_raw(cli->fd, cli->inbuf, cli->timeout,
- maxlen, &len);
+ status = receive_smb_raw(cli->fd, cli->inbuf, cli->bufsize,
+ cli->timeout, maxlen, &len);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(10,("client_receive_smb failed\n"));
show_msg(cli->inbuf);
return -1;
}
- if (len < 0) {
+ /*
+ * I don't believe len can be < 0 with NT_STATUS_OK
+ * returned above, but this check doesn't hurt. JRA.
+ */
+
+ if ((ssize_t)len < 0) {
return len;
}
return false;
}
- if (!cli_check_sign_mac(cli)) {
+ if (!cli_check_sign_mac(cli, cli->inbuf)) {
/*
* If we get a signature failure in sessionsetup, then
* the server sometimes just reflects the sent signature
return -1;
}
-/****************************************************************************
- Read a smb readX header.
- We can only use this if encryption and signing are off.
-****************************************************************************/
-
-bool cli_receive_smb_readX_header(struct cli_state *cli)
-{
- ssize_t len, offset;
-
- if (cli->fd == -1)
- return false;
-
- again:
-
- /* Read up to the size of a readX header reply. */
- len = client_receive_smb(cli, (smb_size - 4) + 24);
-
- if (len > 0) {
- /* it might be an oplock break request */
- if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) &&
- CVAL(cli->inbuf,smb_com) == SMBlockingX &&
- SVAL(cli->inbuf,smb_vwv6) == 0 &&
- SVAL(cli->inbuf,smb_vwv7) == 0) {
- ssize_t total_len = smb_len(cli->inbuf);
-
- if (total_len > CLI_SAMBA_MAX_LARGE_READX_SIZE+SAFETY_MARGIN) {
- goto read_err;
- }
-
- /* Read the rest of the data. */
- if ((total_len - len > 0) &&
- !cli_receive_smb_data(cli,cli->inbuf+len,total_len - len)) {
- goto read_err;
- }
-
- if (cli->oplock_handler) {
- int fnum = SVAL(cli->inbuf,smb_vwv2);
- unsigned char level = CVAL(cli->inbuf,smb_vwv3+1);
- if (!cli->oplock_handler(cli, fnum, level)) return false;
- }
- /* try to prevent loops */
- SCVAL(cli->inbuf,smb_com,0xFF);
- goto again;
- }
- }
-
- /* If it's not the above size it probably was an error packet. */
-
- if ((len == (smb_size - 4) + 24) && !cli_is_error(cli)) {
- /* Check it's a non-chained readX reply. */
- if (!(CVAL(cli->inbuf, smb_flg) & FLAG_REPLY) ||
- (CVAL(cli->inbuf,smb_vwv0) != 0xFF) ||
- (CVAL(cli->inbuf,smb_com) != SMBreadX)) {
- /*
- * We're not coping here with asnyc replies to
- * other calls. Punt here - we need async client
- * libs for this.
- */
- goto read_err;
- }
-
- /*
- * We know it's a readX reply - ensure we've read the
- * padding bytes also.
- */
-
- offset = SVAL(cli->inbuf,smb_vwv6);
- if (offset > len) {
- ssize_t ret;
- size_t padbytes = offset - len;
- ret = cli_receive_smb_data(cli,smb_buf(cli->inbuf),padbytes);
- if (ret != padbytes) {
- goto read_err;
- }
- }
- }
-
- return true;
-
- read_err:
-
- cli->smb_rw_error = SMB_READ_ERROR;
- close(cli->fd);
- cli->fd = -1;
- return false;
-}
-
static ssize_t write_socket(int fd, const char *buf, size_t len)
{
ssize_t ret=0;
if (cli->fd == -1)
return false;
- cli_calculate_sign_mac(cli);
+ cli_calculate_sign_mac(cli, cli->outbuf);
if (enc_on) {
- NTSTATUS status = cli_encrypt_message(cli, &buf_out);
+ NTSTATUS status = cli_encrypt_message(cli, cli->outbuf,
+ &buf_out);
if (!NT_STATUS_IS_OK(status)) {
close(cli->fd);
cli->fd = -1;
/* First length to send is the offset to the data. */
size_t len = SVAL(cli->outbuf,smb_vwv11) + 4;
size_t nwritten=0;
- ssize_t ret;
+ struct iovec iov[2];
/* fd == -1 causes segfaults -- Tom (tom@ninja.nl) */
if (cli->fd == -1) {
return false;
}
- while (nwritten < len) {
- ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
- if (ret <= 0) {
- close(cli->fd);
- cli->fd = -1;
- cli->smb_rw_error = SMB_WRITE_ERROR;
- DEBUG(0,("Error writing %d bytes to client. %d (%s)\n",
- (int)len,(int)ret, strerror(errno) ));
- return false;
- }
- nwritten += ret;
- }
+ iov[0].iov_base = cli->outbuf;
+ iov[0].iov_len = len;
+ iov[1].iov_base = CONST_DISCARD(char *, p);
+ iov[1].iov_len = extradata;
- /* Now write the extra data. */
- nwritten=0;
- while (nwritten < extradata) {
- ret = write_socket(cli->fd,p+nwritten,extradata - nwritten);
- if (ret <= 0) {
- close(cli->fd);
- cli->fd = -1;
- cli->smb_rw_error = SMB_WRITE_ERROR;
- DEBUG(0,("Error writing %d extradata "
- "bytes to client. %d (%s)\n",
- (int)extradata,(int)ret, strerror(errno) ));
- return false;
- }
- nwritten += ret;
+ nwritten = write_data_iov(cli->fd, iov, 2);
+ if (nwritten < (len + extradata)) {
+ close(cli->fd);
+ cli->fd = -1;
+ cli->smb_rw_error = SMB_WRITE_ERROR;
+ DEBUG(0,("Error writing %d bytes to client. (%s)\n",
+ (int)(len+extradata), strerror(errno)));
+ return false;
}
/* Increment the mid so we can tell between responses. */
Setup basics in a outgoing packet.
****************************************************************************/
-void cli_setup_packet(struct cli_state *cli)
+void cli_setup_packet_buf(struct cli_state *cli, char *buf)
{
+ uint16 flags2;
cli->rap_error = 0;
- SSVAL(cli->outbuf,smb_pid,cli->pid);
- SSVAL(cli->outbuf,smb_uid,cli->vuid);
- SSVAL(cli->outbuf,smb_mid,cli->mid);
- if (cli->protocol > PROTOCOL_CORE) {
- uint16 flags2;
- if (cli->case_sensitive) {
- SCVAL(cli->outbuf,smb_flg,0x0);
- } else {
- /* Default setting, case insensitive. */
- SCVAL(cli->outbuf,smb_flg,0x8);
- }
- flags2 = FLAGS2_LONG_PATH_COMPONENTS;
- if (cli->capabilities & CAP_UNICODE)
- flags2 |= FLAGS2_UNICODE_STRINGS;
- if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
- flags2 |= FLAGS2_DFS_PATHNAMES;
- if (cli->capabilities & CAP_STATUS32)
- flags2 |= FLAGS2_32_BIT_ERROR_CODES;
- if (cli->use_spnego)
- flags2 |= FLAGS2_EXTENDED_SECURITY;
- SSVAL(cli->outbuf,smb_flg2, flags2);
+ SIVAL(buf,smb_rcls,0);
+ SSVAL(buf,smb_pid,cli->pid);
+ memset(buf+smb_pidhigh, 0, 12);
+ SSVAL(buf,smb_uid,cli->vuid);
+ SSVAL(buf,smb_mid,cli->mid);
+
+ if (cli->protocol <= PROTOCOL_CORE) {
+ return;
}
+
+ if (cli->case_sensitive) {
+ SCVAL(buf,smb_flg,0x0);
+ } else {
+ /* Default setting, case insensitive. */
+ SCVAL(buf,smb_flg,0x8);
+ }
+ flags2 = FLAGS2_LONG_PATH_COMPONENTS;
+ if (cli->capabilities & CAP_UNICODE)
+ flags2 |= FLAGS2_UNICODE_STRINGS;
+ if ((cli->capabilities & CAP_DFS) && cli->dfsroot)
+ flags2 |= FLAGS2_DFS_PATHNAMES;
+ if (cli->capabilities & CAP_STATUS32)
+ flags2 |= FLAGS2_32_BIT_ERROR_CODES;
+ if (cli->use_spnego)
+ flags2 |= FLAGS2_EXTENDED_SECURITY;
+ SSVAL(buf,smb_flg2, flags2);
+}
+
+void cli_setup_packet(struct cli_state *cli)
+{
+ cli_setup_packet_buf(cli, cli->outbuf);
}
/****************************************************************************
return NULL;
}
- cli = SMB_MALLOC_P(struct cli_state);
+ cli = TALLOC_ZERO_P(NULL, struct cli_state);
if (!cli) {
return NULL;
}
- ZERO_STRUCTP(cli);
-
cli->port = 0;
cli->fd = -1;
cli->cnum = -1;
return NULL;
}
-/****************************************************************************
- External interface.
- Close an open named pipe over SMB. Free any authentication data.
- Returns false if the cli_close call failed.
- ****************************************************************************/
-
-bool cli_rpc_pipe_close(struct rpc_pipe_client *cli)
-{
- bool ret;
-
- if (!cli) {
- return false;
- }
-
- ret = cli_close(cli->cli, cli->fnum);
-
- if (!ret) {
- DEBUG(1,("cli_rpc_pipe_close: cli_close failed on pipe %s, "
- "fnum 0x%x "
- "to machine %s. Error was %s\n",
- cli->pipe_name,
- (int) cli->fnum,
- cli->cli->desthost,
- cli_errstr(cli->cli)));
- }
-
- if (cli->auth.cli_auth_data_free_func) {
- (*cli->auth.cli_auth_data_free_func)(&cli->auth);
- }
-
- DEBUG(10,("cli_rpc_pipe_close: closed pipe %s to machine %s\n",
- cli->pipe_name, cli->cli->desthost ));
-
- DLIST_REMOVE(cli->cli->pipe_list, cli);
- talloc_destroy(cli->mem_ctx);
- return ret;
-}
-
/****************************************************************************
Close all pipes open on this session.
****************************************************************************/
void cli_nt_pipes_close(struct cli_state *cli)
{
- struct rpc_pipe_client *cp, *next;
-
- for (cp = cli->pipe_list; cp; cp = next) {
- next = cp->next;
- cli_rpc_pipe_close(cp);
+ while (cli->pipe_list != NULL) {
+ /*
+ * No TALLOC_FREE here!
+ */
+ talloc_free(cli->pipe_list);
}
}
cli->fd = -1;
cli->smb_rw_error = SMB_READ_OK;
- SAFE_FREE(cli);
+ TALLOC_FREE(cli);
}
/****************************************************************************
return true;
}
-/****************************************************************************
- Send/receive a SMBecho command: ping the server
-****************************************************************************/
+/**
+ * @brief: Collect a echo reply
+ * @param[in] req The corresponding async request
+ *
+ * There might be more than one echo reply. This helper pulls the reply out of
+ * the data stream. If all expected replies have arrived, declare the
+ * async_req done.
+ */
-bool cli_echo(struct cli_state *cli, uint16 num_echos,
- unsigned char *data, size_t length)
+static void cli_echo_recv_helper(struct async_req *req)
{
- char *p;
- int i;
+ struct cli_request *cli_req;
+ uint8_t wct;
+ uint16_t *vwv;
+ uint16_t num_bytes;
+ uint8_t *bytes;
+ NTSTATUS status;
- SMB_ASSERT(length < 1024);
+ status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
+ if (!NT_STATUS_IS_OK(status)) {
+ async_req_nterror(req, status);
+ return;
+ }
- memset(cli->outbuf,'\0',smb_size);
- cli_set_message(cli->outbuf,1,length,true);
- SCVAL(cli->outbuf,smb_com,SMBecho);
- SSVAL(cli->outbuf,smb_tid,65535);
- SSVAL(cli->outbuf,smb_vwv0,num_echos);
- cli_setup_packet(cli);
- p = smb_buf(cli->outbuf);
- memcpy(p, data, length);
- p += length;
+ cli_req = talloc_get_type_abort(req->private_data, struct cli_request);
- cli_setup_bcc(cli, p);
+ if ((num_bytes != cli_req->data.echo.data.length)
+ || (memcmp(cli_req->data.echo.data.data, bytes,
+ num_bytes) != 0)) {
+ async_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+ return;
+ }
- cli_send_smb(cli);
+ cli_req->data.echo.num_echos -= 1;
- for (i=0; i<num_echos; i++) {
- if (!cli_receive_smb(cli)) {
- return false;
- }
+ if (cli_req->data.echo.num_echos == 0) {
+ client_set_trans_sign_state_off(cli_req->cli, cli_req->mid);
+ async_req_done(req);
+ return;
+ }
- if (cli_is_error(cli)) {
- return false;
- }
+ return;
+}
+
+/**
+ * @brief Send SMBEcho requests
+ * @param[in] mem_ctx The memory context to put the async_req on
+ * @param[in] ev The event context that will call us back
+ * @param[in] cli The connection to send the echo to
+ * @param[in] num_echos How many times do we want to get the reply?
+ * @param[in] data The data we want to get back
+ * @retval The async request
+ */
+
+struct async_req *cli_echo_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
+ struct cli_state *cli, uint16_t num_echos,
+ DATA_BLOB data)
+{
+ uint16_t vwv[1];
+ uint8_t *data_copy;
+ struct async_req *result;
+ struct cli_request *req;
+
+ SSVAL(vwv, 0, num_echos);
+
+ data_copy = (uint8_t *)talloc_memdup(mem_ctx, data.data, data.length);
+ if (data_copy == NULL) {
+ return NULL;
}
- return true;
+ result = cli_request_send(mem_ctx, ev, cli, SMBecho, 0, 1, vwv, 0,
+ data.length, data.data);
+ if (result == NULL) {
+ TALLOC_FREE(data_copy);
+ return NULL;
+ }
+ req = talloc_get_type_abort(result->private_data, struct cli_request);
+
+ client_set_trans_sign_state_on(cli, req->mid);
+
+ req->data.echo.num_echos = num_echos;
+ req->data.echo.data.data = talloc_move(req, &data_copy);
+ req->data.echo.data.length = data.length;
+
+ req->recv_helper.fn = cli_echo_recv_helper;
+
+ return result;
+}
+
+/**
+ * Get the result out from an echo request
+ * @param[in] req The async_req from cli_echo_send
+ * @retval Did the server reply correctly?
+ */
+
+NTSTATUS cli_echo_recv(struct async_req *req)
+{
+ return async_req_simple_recv_ntstatus(req);
+}
+
+/**
+ * @brief Send/Receive SMBEcho requests
+ * @param[in] mem_ctx The memory context to put the async_req on
+ * @param[in] ev The event context that will call us back
+ * @param[in] cli The connection to send the echo to
+ * @param[in] num_echos How many times do we want to get the reply?
+ * @param[in] data The data we want to get back
+ * @retval Did the server reply correctly?
+ */
+
+NTSTATUS cli_echo(struct cli_state *cli, uint16_t num_echos, DATA_BLOB data)
+{
+ TALLOC_CTX *frame = talloc_stackframe();
+ struct event_context *ev;
+ struct async_req *req;
+ NTSTATUS status = NT_STATUS_NO_MEMORY;
+
+ if (cli->fd_event != NULL) {
+ /*
+ * Can't use sync call while an async call is in flight
+ */
+ cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
+ goto fail;
+ }
+
+ ev = event_context_init(frame);
+ if (ev == NULL) {
+ goto fail;
+ }
+
+ req = cli_echo_send(frame, ev, cli, num_echos, data);
+ if (req == NULL) {
+ goto fail;
+ }
+
+ while (req->state < ASYNC_REQ_DONE) {
+ event_loop_once(ev);
+ }
+
+ status = cli_echo_recv(req);
+
+ fail:
+ TALLOC_FREE(frame);
+ return status;
+}
+
+/**
+ * Is the SMB command able to hold an AND_X successor
+ * @param[in] cmd The SMB command in question
+ * @retval Can we add a chained request after "cmd"?
+ */
+bool is_andx_req(uint8_t cmd)
+{
+ switch (cmd) {
+ case SMBtconX:
+ case SMBlockingX:
+ case SMBopenX:
+ case SMBreadX:
+ case SMBwriteX:
+ case SMBsesssetupX:
+ case SMBulogoffX:
+ case SMBntcreateX:
+ return true;
+ break;
+ default:
+ break;
+ }
+
+ return false;
}