s3:libsmb/async_smb: in cli_state_notify_pending() we always disconnect
[rusty/samba.git] / source3 / libsmb / async_smb.c
index ee15caee7a555c4da5525a021d78d2d0a999fe95..43dfa22b9dfa8fe37f9b352fec737eeb4eb26b36 100644 (file)
 #include "libsmb/nmblib.h"
 #include "read_smb.h"
 
-/**
- * Fetch an error out of a NBT packet
- * @param[in] buf      The SMB packet
- * @retval             The error, converted to NTSTATUS
- */
-
-NTSTATUS cli_pull_error(char *buf)
+static NTSTATUS cli_pull_raw_error(const uint8_t *buf)
 {
        uint32_t flags2 = SVAL(buf, smb_flg2);
+       NTSTATUS status = NT_STATUS(IVAL(buf, smb_rcls));
 
-       if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
-               return NT_STATUS(IVAL(buf, smb_rcls));
+       if (NT_STATUS_IS_OK(status)) {
+               return NT_STATUS_OK;
        }
 
-       return dos_to_ntstatus(CVAL(buf, smb_rcls), SVAL(buf,smb_err));
-}
-
-/**
- * Compatibility helper for the sync APIs: Fake NTSTATUS in cli->inbuf
- * @param[in] cli      The client connection that just received an error
- * @param[in] status   The error to set on "cli"
- */
-
-void cli_set_error(struct cli_state *cli, NTSTATUS status)
-{
-       uint32_t flags2 = SVAL(cli->inbuf, smb_flg2);
-
-       if (NT_STATUS_IS_DOS(status)) {
-               SSVAL(cli->inbuf, smb_flg2,
-                     flags2 & ~FLAGS2_32_BIT_ERROR_CODES);
-               SCVAL(cli->inbuf, smb_rcls, NT_STATUS_DOS_CLASS(status));
-               SSVAL(cli->inbuf, smb_err, NT_STATUS_DOS_CODE(status));
-               return;
+       if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
+               return status;
        }
 
-       SSVAL(cli->inbuf, smb_flg2, flags2 | FLAGS2_32_BIT_ERROR_CODES);
-       SIVAL(cli->inbuf, smb_rcls, NT_STATUS_V(status));
-       return;
+       return NT_STATUS_DOS(CVAL(buf, smb_rcls), SVAL(buf,smb_err));
 }
 
 /**
@@ -118,23 +94,25 @@ struct cli_smb_state {
        int chain_num;
        int chain_length;
        struct tevent_req **chained_requests;
+
+       bool one_way;
 };
 
 static uint16_t cli_alloc_mid(struct cli_state *cli)
 {
-       int num_pending = talloc_array_length(cli->pending);
+       int num_pending = talloc_array_length(cli->conn.pending);
        uint16_t result;
 
        while (true) {
                int i;
 
-               result = cli->mid++;
+               result = cli->conn.smb1.mid++;
                if ((result == 0) || (result == 0xffff)) {
                        continue;
                }
 
                for (i=0; i<num_pending; i++) {
-                       if (result == cli_smb_req_mid(cli->pending[i])) {
+                       if (result == cli_smb_req_mid(cli->conn.pending[i])) {
                                break;
                        }
                }
@@ -150,7 +128,7 @@ void cli_smb_req_unset_pending(struct tevent_req *req)
        struct cli_smb_state *state = tevent_req_data(
                req, struct cli_smb_state);
        struct cli_state *cli = state->cli;
-       int num_pending = talloc_array_length(cli->pending);
+       int num_pending = talloc_array_length(cli->conn.pending);
        int i;
 
        if (state->mid != 0) {
@@ -161,18 +139,21 @@ void cli_smb_req_unset_pending(struct tevent_req *req)
                return;
        }
 
+       talloc_set_destructor(req, NULL);
+
        if (num_pending == 1) {
                /*
                 * The pending read_smb tevent_req is a child of
                 * cli->pending. So if nothing is pending anymore, we need to
                 * delete the socket read fde.
                 */
-               TALLOC_FREE(cli->pending);
+               TALLOC_FREE(cli->conn.pending);
+               cli->conn.read_smb_req = NULL;
                return;
        }
 
        for (i=0; i<num_pending; i++) {
-               if (req == cli->pending[i]) {
+               if (req == cli->conn.pending[i]) {
                        break;
                }
        }
@@ -180,22 +161,25 @@ void cli_smb_req_unset_pending(struct tevent_req *req)
                /*
                 * Something's seriously broken. Just returning here is the
                 * right thing nevertheless, the point of this routine is to
-                * remove ourselves from cli->pending.
+                * remove ourselves from cli->conn.pending.
                 */
                return;
        }
 
        /*
-        * Remove ourselves from the cli->pending array
+        * Remove ourselves from the cli->conn.pending array
         */
-       cli->pending[i] = cli->pending[num_pending-1];
+       for (; i < (num_pending - 1); i++) {
+               cli->conn.pending[i] = cli->conn.pending[i+1];
+       }
 
        /*
         * No NULL check here, we're shrinking by sizeof(void *), and
         * talloc_realloc just adjusts the size for this.
         */
-       cli->pending = talloc_realloc(NULL, cli->pending, struct tevent_req *,
-                                     num_pending - 1);
+       cli->conn.pending = talloc_realloc(NULL, cli->conn.pending,
+                                          struct tevent_req *,
+                                          num_pending - 1);
        return;
 }
 
@@ -212,7 +196,8 @@ static int cli_smb_req_destructor(struct tevent_req *req)
        return 0;
 }
 
-static void cli_smb_received(struct tevent_req *subreq);
+static bool cli_state_receive_next(struct cli_state *cli);
+static void cli_state_notify_pending(struct cli_state *cli, NTSTATUS status);
 
 bool cli_smb_req_set_pending(struct tevent_req *req)
 {
@@ -221,37 +206,104 @@ bool cli_smb_req_set_pending(struct tevent_req *req)
        struct cli_state *cli;
        struct tevent_req **pending;
        int num_pending;
-       struct tevent_req *subreq;
 
        cli = state->cli;
-       num_pending = talloc_array_length(cli->pending);
+       num_pending = talloc_array_length(cli->conn.pending);
 
-       pending = talloc_realloc(cli, cli->pending, struct tevent_req *,
+       pending = talloc_realloc(cli, cli->conn.pending, struct tevent_req *,
                                 num_pending+1);
        if (pending == NULL) {
                return false;
        }
        pending[num_pending] = req;
-       cli->pending = pending;
+       cli->conn.pending = pending;
        talloc_set_destructor(req, cli_smb_req_destructor);
 
-       if (num_pending > 0) {
+       if (!cli_state_receive_next(cli)) {
+               /*
+                * the caller should notify the current request
+                *
+                * And all other pending requests get notified
+                * by cli_state_notify_pending().
+                */
+               cli_smb_req_unset_pending(req);
+               cli_state_notify_pending(cli, NT_STATUS_NO_MEMORY);
+               return false;
+       }
+
+       return true;
+}
+
+static void cli_smb_received(struct tevent_req *subreq);
+static NTSTATUS cli_state_dispatch_smb1(struct cli_state *cli,
+                                       TALLOC_CTX *frame,
+                                       uint8_t *inbuf);
+
+static bool cli_state_receive_next(struct cli_state *cli)
+{
+       size_t num_pending = talloc_array_length(cli->conn.pending);
+       struct tevent_req *req;
+       struct cli_smb_state *state;
+
+       if (cli->conn.read_smb_req != NULL) {
                return true;
        }
 
+       if (num_pending == 0) {
+               return true;
+       }
+
+       req = cli->conn.pending[0];
+       state = tevent_req_data(req, struct cli_smb_state);
+
+       cli->conn.dispatch_incoming = cli_state_dispatch_smb1;
+
        /*
         * We're the first ones, add the read_smb request that waits for the
         * answer from the server
         */
-       subreq = read_smb_send(cli->pending, state->ev, cli->fd);
-       if (subreq == NULL) {
-               cli_smb_req_unset_pending(req);
+       cli->conn.read_smb_req = read_smb_send(cli->conn.pending, state->ev,
+                                              cli->conn.fd);
+       if (cli->conn.read_smb_req == NULL) {
                return false;
        }
-       tevent_req_set_callback(subreq, cli_smb_received, cli);
+       tevent_req_set_callback(cli->conn.read_smb_req, cli_smb_received, cli);
        return true;
 }
 
+static void cli_state_notify_pending(struct cli_state *cli, NTSTATUS status)
+{
+       cli_state_disconnect(cli);
+
+       /*
+        * Cancel all pending requests. We do not do a for-loop walking
+        * cli->conn.pending because that array changes in
+        * cli_smb_req_destructor().
+        */
+       while (talloc_array_length(cli->conn.pending) > 0) {
+               struct tevent_req *req;
+               struct cli_smb_state *state;
+
+               req = cli->conn.pending[0];
+               state = tevent_req_data(req, struct cli_smb_state);
+
+               /*
+                * We're dead. No point waiting for trans2
+                * replies.
+                */
+               state->mid = 0;
+
+               cli_smb_req_unset_pending(req);
+
+               /*
+                * we need to defer the callback, because we may notify more
+                * then one caller.
+                */
+               tevent_req_defer_callback(req, state->ev);
+               tevent_req_nterror(req, status);
+       }
+}
+
 /*
  * Fetch a smb request's mid. Only valid after the request has been sent by
  * cli_smb_req_send().
@@ -260,6 +312,11 @@ uint16_t cli_smb_req_mid(struct tevent_req *req)
 {
        struct cli_smb_state *state = tevent_req_data(
                req, struct cli_smb_state);
+
+       if (state->mid != 0) {
+               return state->mid;
+       }
+
        return SVAL(state->header, smb_mid);
 }
 
@@ -346,7 +403,7 @@ struct tevent_req *cli_smb_req_create(TALLOC_CTX *mem_ctx,
 
        cli_setup_packet_buf(cli, (char *)state->header);
        SCVAL(state->header, smb_com, smb_command);
-       SSVAL(state->header, smb_tid, cli->cnum);
+       SSVAL(state->header, smb_tid, cli->smb1.tid);
        SCVAL(state->header, smb_wct, wct);
 
        state->vwv = vwv;
@@ -369,9 +426,25 @@ struct tevent_req *cli_smb_req_create(TALLOC_CTX *mem_ctx,
        if (cli->timeout) {
                endtime = timeval_current_ofs_msec(cli->timeout);
                if (!tevent_req_set_endtime(result, ev, endtime)) {
-                       tevent_req_oom(result);
+                       return result;
+               }
+       }
+
+       switch (smb_command) {
+       case SMBtranss:
+       case SMBtranss2:
+       case SMBnttranss:
+       case SMBntcancel:
+               state->one_way = true;
+               break;
+       case SMBlockingX:
+               if ((wct == 8) &&
+                   (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
+                       state->one_way = true;
                }
+               break;
        }
+
        return result;
 }
 
@@ -410,8 +483,8 @@ static NTSTATUS cli_smb_req_iov_send(struct tevent_req *req,
        struct tevent_req *subreq;
        NTSTATUS status;
 
-       if (state->cli->fd == -1) {
-               return NT_STATUS_CONNECTION_INVALID;
+       if (!cli_state_is_connected(state->cli)) {
+               return NT_STATUS_CONNECTION_DISCONNECTED;
        }
 
        if (iov[0].iov_len < smb_wct) {
@@ -433,7 +506,7 @@ static NTSTATUS cli_smb_req_iov_send(struct tevent_req *req,
                return status;
        }
 
-       if (cli_encryption_on(state->cli)) {
+       if (cli_state_encryption_on(state->cli)) {
                char *buf, *enc_buf;
 
                buf = (char *)iov_concat(talloc_tos(), iov, iov_count);
@@ -458,8 +531,8 @@ static NTSTATUS cli_smb_req_iov_send(struct tevent_req *req,
                iov[0].iov_len = talloc_get_size(buf);
                iov_count = 1;
        }
-       subreq = writev_send(state, state->ev, state->cli->outgoing,
-                            state->cli->fd, false, iov, iov_count);
+       subreq = writev_send(state, state->ev, state->cli->conn.outgoing,
+                            state->cli->conn.fd, false, iov, iov_count);
        if (subreq == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -472,6 +545,10 @@ NTSTATUS cli_smb_req_send(struct tevent_req *req)
        struct cli_smb_state *state = tevent_req_data(
                req, struct cli_smb_state);
 
+       if (!tevent_req_is_in_progress(req)) {
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
        return cli_smb_req_iov_send(req, state, state->iov, state->iov_count);
 }
 
@@ -496,7 +573,9 @@ struct tevent_req *cli_smb_send(TALLOC_CTX *mem_ctx,
        if (req == NULL) {
                return NULL;
        }
-
+       if (!tevent_req_is_in_progress(req)) {
+               return tevent_req_post(req, ev);
+       }
        status = cli_smb_req_send(req);
        if (!NT_STATUS_IS_OK(status)) {
                tevent_req_nterror(req, status);
@@ -517,29 +596,15 @@ static void cli_smb_sent(struct tevent_req *subreq)
        nwritten = writev_recv(subreq, &err);
        TALLOC_FREE(subreq);
        if (nwritten == -1) {
-               if (state->cli->fd != -1) {
-                       close(state->cli->fd);
-                       state->cli->fd = -1;
-               }
-               tevent_req_nterror(req, map_nt_error_from_unix(err));
+               NTSTATUS status = map_nt_error_from_unix(err);
+               cli_state_notify_pending(state->cli, status);
                return;
        }
 
-       switch (CVAL(state->header, smb_com)) {
-       case SMBtranss:
-       case SMBtranss2:
-       case SMBnttranss:
-       case SMBntcancel:
+       if (state->one_way) {
                state->inbuf = NULL;
                tevent_req_done(req);
                return;
-       case SMBlockingX:
-               if ((CVAL(state->header, smb_wct) == 8) &&
-                   (CVAL(state->vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
-                       state->inbuf = NULL;
-                       tevent_req_done(req);
-                       return;
-               }
        }
 
        if (!cli_smb_req_set_pending(req)) {
@@ -552,50 +617,90 @@ static void cli_smb_received(struct tevent_req *subreq)
 {
        struct cli_state *cli = tevent_req_callback_data(
                subreq, struct cli_state);
-       struct tevent_req *req;
-       struct cli_smb_state *state;
+       TALLOC_CTX *frame = talloc_stackframe();
        NTSTATUS status;
        uint8_t *inbuf;
        ssize_t received;
-       int num_pending;
-       int i, err;
-       uint16_t mid;
-       bool oplock_break;
+       int err;
 
-       received = read_smb_recv(subreq, talloc_tos(), &inbuf, &err);
+       if (subreq != cli->conn.read_smb_req) {
+               DEBUG(1, ("Internal error: cli_smb_received called with "
+                         "unexpected subreq\n"));
+               status = NT_STATUS_INTERNAL_ERROR;
+               cli_state_notify_pending(cli, status);
+               TALLOC_FREE(frame);
+               return;
+       }
+
+       received = read_smb_recv(subreq, frame, &inbuf, &err);
        TALLOC_FREE(subreq);
+       cli->conn.read_smb_req = NULL;
        if (received == -1) {
-               if (cli->fd != -1) {
-                       close(cli->fd);
-                       cli->fd = -1;
-               }
                status = map_nt_error_from_unix(err);
-               goto fail;
+               cli_state_notify_pending(cli, status);
+               TALLOC_FREE(frame);
+               return;
+       }
+
+       status = cli->conn.dispatch_incoming(cli, frame, inbuf);
+       TALLOC_FREE(frame);
+       if (NT_STATUS_IS_OK(status)) {
+               /*
+                * We should not do any more processing
+                * as the dispatch function called
+                * tevent_req_done().
+                */
+               return;
+       } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
+               /*
+                * We got an error, so notify all pending requests
+                */
+               cli_state_notify_pending(cli, status);
+               return;
        }
 
+       /*
+        * We got NT_STATUS_RETRY, so we may ask for a
+        * next incoming pdu.
+        */
+       if (!cli_state_receive_next(cli)) {
+               cli_state_notify_pending(cli, NT_STATUS_NO_MEMORY);
+       }
+}
+
+static NTSTATUS cli_state_dispatch_smb1(struct cli_state *cli,
+                                       TALLOC_CTX *frame,
+                                       uint8_t *inbuf)
+{
+       struct tevent_req *req;
+       struct cli_smb_state *state;
+       NTSTATUS status;
+       int num_pending;
+       int i;
+       uint16_t mid;
+       bool oplock_break;
+
        if ((IVAL(inbuf, 4) != 0x424d53ff) /* 0xFF"SMB" */
            && (SVAL(inbuf, 4) != 0x45ff)) /* 0xFF"E" */ {
                DEBUG(10, ("Got non-SMB PDU\n"));
-               status = NT_STATUS_INVALID_NETWORK_RESPONSE;
-               goto fail;
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
        }
 
-       if (cli_encryption_on(cli) && (CVAL(inbuf, 0) == 0)) {
+       if (cli_state_encryption_on(cli) && (CVAL(inbuf, 0) == 0)) {
                uint16_t enc_ctx_num;
 
                status = get_enc_ctx_num(inbuf, &enc_ctx_num);
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(10, ("get_enc_ctx_num returned %s\n",
                                   nt_errstr(status)));
-                       goto fail;
+                       return status;
                }
 
                if (enc_ctx_num != cli->trans_enc_state->enc_ctx_num) {
                        DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
                                   enc_ctx_num,
                                   cli->trans_enc_state->enc_ctx_num));
-                       status = NT_STATUS_INVALID_HANDLE;
-                       goto fail;
+                       return NT_STATUS_INVALID_HANDLE;
                }
 
                status = common_decrypt_buffer(cli->trans_enc_state,
@@ -603,22 +708,21 @@ static void cli_smb_received(struct tevent_req *subreq)
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(10, ("common_decrypt_buffer returned %s\n",
                                   nt_errstr(status)));
-                       goto fail;
+                       return status;
                }
        }
 
        mid = SVAL(inbuf, smb_mid);
-       num_pending = talloc_array_length(cli->pending);
+       num_pending = talloc_array_length(cli->conn.pending);
 
        for (i=0; i<num_pending; i++) {
-               if (mid == cli_smb_req_mid(cli->pending[i])) {
+               if (mid == cli_smb_req_mid(cli->conn.pending[i])) {
                        break;
                }
        }
        if (i == num_pending) {
                /* Dump unexpected reply */
-               TALLOC_FREE(inbuf);
-               goto done;
+               return NT_STATUS_RETRY;
        }
 
        oplock_break = false;
@@ -635,74 +739,80 @@ static void cli_smb_received(struct tevent_req *subreq)
 
                if (!oplock_break) {
                        /* Dump unexpected reply */
-                       TALLOC_FREE(inbuf);
-                       goto done;
+                       return NT_STATUS_RETRY;
                }
        }
 
-       req = cli->pending[i];
+       req = cli->conn.pending[i];
        state = tevent_req_data(req, struct cli_smb_state);
 
        if (!oplock_break /* oplock breaks are not signed */
            && !cli_check_sign_mac(cli, (char *)inbuf, state->seqnum+1)) {
                DEBUG(10, ("cli_check_sign_mac failed\n"));
-               TALLOC_FREE(inbuf);
-               status = NT_STATUS_ACCESS_DENIED;
-               close(cli->fd);
-               cli->fd = -1;
-               goto fail;
+               return NT_STATUS_ACCESS_DENIED;
        }
 
-       if (state->chained_requests == NULL) {
-               state->inbuf = talloc_move(state, &inbuf);
-               talloc_set_destructor(req, NULL);
-               cli_smb_req_unset_pending(req);
-               state->chain_num = 0;
-               state->chain_length = 1;
-               tevent_req_done(req);
-       } else {
-               struct tevent_req **chain = talloc_move(
-                       talloc_tos(), &state->chained_requests);
+       if (state->chained_requests != NULL) {
+               struct tevent_req **chain = talloc_move(frame,
+                                           &state->chained_requests);
                int num_chained = talloc_array_length(chain);
 
+               /*
+                * We steal the inbuf to the chain,
+                * so that it will stay until all
+                * requests of the chain are finished.
+                *
+                * Each requests in the chain will
+                * hold a talloc reference to the chain.
+                * This way we do not expose the talloc_reference()
+                * behavior to the callers.
+                */
+               talloc_steal(chain, inbuf);
+
                for (i=0; i<num_chained; i++) {
-                       state = tevent_req_data(chain[i], struct
-                                               cli_smb_state);
+                       struct tevent_req **ref;
+
+                       req = chain[i];
+                       state = tevent_req_data(req, struct cli_smb_state);
+
+                       cli_smb_req_unset_pending(req);
+
+                       /*
+                        * as we finish multiple requests here
+                        * we need to defer the callbacks as
+                        * they could destroy our current stack state.
+                        */
+                       tevent_req_defer_callback(req, state->ev);
+
+                       ref = talloc_reference(state, chain);
+                       if (tevent_req_nomem(ref, req)) {
+                               continue;
+                       }
+
                        state->inbuf = inbuf;
                        state->chain_num = i;
                        state->chain_length = num_chained;
-                       tevent_req_done(chain[i]);
-               }
-               TALLOC_FREE(inbuf);
-               TALLOC_FREE(chain);
-       }
- done:
-       if (talloc_array_length(cli->pending) > 0) {
-               /*
-                * Set up another read request for the other pending cli_smb
-                * requests
-                */
-               state = tevent_req_data(cli->pending[0], struct cli_smb_state);
-               subreq = read_smb_send(cli->pending, state->ev, cli->fd);
-               if (subreq == NULL) {
-                       status = NT_STATUS_NO_MEMORY;
-                       goto fail;
+
+                       tevent_req_done(req);
                }
-               tevent_req_set_callback(subreq, cli_smb_received, cli);
+
+               return NT_STATUS_RETRY;
        }
-       return;
- fail:
-       /*
-        * Cancel all pending requests. We don't do a for-loop walking
-        * cli->pending because that array changes in
-        * cli_smb_req_destructor().
-        */
-       while (talloc_array_length(cli->pending) > 0) {
-               req = cli->pending[0];
-               talloc_set_destructor(req, NULL);
-               cli_smb_req_unset_pending(req);
-               tevent_req_nterror(req, status);
+
+       cli_smb_req_unset_pending(req);
+
+       state->inbuf = talloc_move(state, &inbuf);
+       state->chain_num = 0;
+       state->chain_length = 1;
+
+       if (talloc_array_length(cli->conn.pending) == 0) {
+               tevent_req_done(req);
+               return NT_STATUS_OK;
        }
+
+       tevent_req_defer_callback(req, state->ev);
+       tevent_req_done(req);
+       return NT_STATUS_RETRY;
 }
 
 NTSTATUS cli_smb_recv(struct tevent_req *req,
@@ -781,9 +891,20 @@ NTSTATUS cli_smb_recv(struct tevent_req *req,
                cmd = CVAL(state->inbuf, wct_ofs + 1);
        }
 
-       status = cli_pull_error((char *)state->inbuf);
-
-       cli_set_error(state->cli, status);
+       state->cli->raw_status = cli_pull_raw_error(state->inbuf);
+       if (NT_STATUS_IS_DOS(state->cli->raw_status)) {
+               uint8_t eclass = NT_STATUS_DOS_CLASS(state->cli->raw_status);
+               uint16_t ecode = NT_STATUS_DOS_CODE(state->cli->raw_status);
+               /*
+                * TODO: is it really a good idea to do a mapping here?
+                *
+                * The old cli_pull_error() also does it, so I do not change
+                * the behavior yet.
+                */
+               status = dos_to_ntstatus(eclass, ecode);
+       } else {
+               status = state->cli->raw_status;
+       }
 
        if (!have_andx_command((char *)state->inbuf, wct_ofs)) {
 
@@ -882,6 +1003,10 @@ NTSTATUS cli_smb_chain_send(struct tevent_req **reqs, int num_reqs)
 
        iovlen = 0;
        for (i=0; i<num_reqs; i++) {
+               if (!tevent_req_is_in_progress(reqs[i])) {
+                       return NT_STATUS_INTERNAL_ERROR;
+               }
+
                state = tevent_req_data(reqs[i], struct cli_smb_state);
                iovlen += state->iov_count;
        }
@@ -968,223 +1093,8 @@ NTSTATUS cli_smb_chain_send(struct tevent_req **reqs, int num_reqs)
        return NT_STATUS_OK;
 }
 
-uint8_t *cli_smb_inbuf(struct tevent_req *req)
-{
-       struct cli_smb_state *state = tevent_req_data(
-               req, struct cli_smb_state);
-       return state->inbuf;
-}
-
 bool cli_has_async_calls(struct cli_state *cli)
 {
-       return ((tevent_queue_length(cli->outgoing) != 0)
-               || (talloc_array_length(cli->pending) != 0));
-}
-
-struct cli_smb_oplock_break_waiter_state {
-       uint16_t fnum;
-       uint8_t level;
-};
-
-static void cli_smb_oplock_break_waiter_done(struct tevent_req *subreq);
-
-struct tevent_req *cli_smb_oplock_break_waiter_send(TALLOC_CTX *mem_ctx,
-                                                   struct event_context *ev,
-                                                   struct cli_state *cli)
-{
-       struct tevent_req *req, *subreq;
-       struct cli_smb_oplock_break_waiter_state *state;
-       struct cli_smb_state *smb_state;
-
-       req = tevent_req_create(mem_ctx, &state,
-                               struct cli_smb_oplock_break_waiter_state);
-       if (req == NULL) {
-               return NULL;
-       }
-
-       /*
-        * Create a fake SMB request that we will never send out. This is only
-        * used to be set into the pending queue with the right mid.
-        */
-       subreq = cli_smb_req_create(mem_ctx, ev, cli, 0, 0, 0, NULL, 0, NULL);
-       if (tevent_req_nomem(subreq, req)) {
-               return tevent_req_post(req, ev);
-       }
-       smb_state = tevent_req_data(subreq, struct cli_smb_state);
-       SSVAL(smb_state->header, smb_mid, 0xffff);
-
-       if (!cli_smb_req_set_pending(subreq)) {
-               tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
-               return tevent_req_post(req, ev);
-       }
-       tevent_req_set_callback(subreq, cli_smb_oplock_break_waiter_done, req);
-       return req;
-}
-
-static void cli_smb_oplock_break_waiter_done(struct tevent_req *subreq)
-{
-       struct tevent_req *req = tevent_req_callback_data(
-               subreq, struct tevent_req);
-       struct cli_smb_oplock_break_waiter_state *state = tevent_req_data(
-               req, struct cli_smb_oplock_break_waiter_state);
-       uint8_t wct;
-       uint16_t *vwv;
-       uint32_t num_bytes;
-       uint8_t *bytes;
-       uint8_t *inbuf;
-       NTSTATUS status;
-
-       status = cli_smb_recv(subreq, state, &inbuf, 8, &wct, &vwv,
-                             &num_bytes, &bytes);
-       TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               tevent_req_nterror(req, status);
-               return;
-       }
-       state->fnum = SVAL(vwv+2, 0);
-       state->level = CVAL(vwv+3, 1);
-       tevent_req_done(req);
-}
-
-NTSTATUS cli_smb_oplock_break_waiter_recv(struct tevent_req *req,
-                                         uint16_t *pfnum,
-                                         uint8_t *plevel)
-{
-       struct cli_smb_oplock_break_waiter_state *state = tevent_req_data(
-               req, struct cli_smb_oplock_break_waiter_state);
-       NTSTATUS status;
-
-       if (tevent_req_is_nterror(req, &status)) {
-               return status;
-       }
-       *pfnum = state->fnum;
-       *plevel = state->level;
-       return NT_STATUS_OK;
-}
-
-
-struct cli_session_request_state {
-       struct tevent_context *ev;
-       int sock;
-       uint32 len_hdr;
-       struct iovec iov[3];
-       uint8_t nb_session_response;
-};
-
-static void cli_session_request_sent(struct tevent_req *subreq);
-static void cli_session_request_recvd(struct tevent_req *subreq);
-
-struct tevent_req *cli_session_request_send(TALLOC_CTX *mem_ctx,
-                                           struct tevent_context *ev,
-                                           int sock,
-                                           const struct nmb_name *called,
-                                           const struct nmb_name *calling)
-{
-       struct tevent_req *req, *subreq;
-       struct cli_session_request_state *state;
-
-       req = tevent_req_create(mem_ctx, &state,
-                               struct cli_session_request_state);
-       if (req == NULL) {
-               return NULL;
-       }
-       state->ev = ev;
-       state->sock = sock;
-
-       state->iov[1].iov_base = name_mangle(
-               state, called->name, called->name_type);
-       if (tevent_req_nomem(state->iov[1].iov_base, req)) {
-               return tevent_req_post(req, ev);
-       }
-       state->iov[1].iov_len = name_len(
-               (unsigned char *)state->iov[1].iov_base,
-               talloc_get_size(state->iov[1].iov_base));
-
-       state->iov[2].iov_base = name_mangle(
-               state, calling->name, calling->name_type);
-       if (tevent_req_nomem(state->iov[2].iov_base, req)) {
-               return tevent_req_post(req, ev);
-       }
-       state->iov[2].iov_len = name_len(
-               (unsigned char *)state->iov[2].iov_base,
-               talloc_get_size(state->iov[2].iov_base));
-
-       _smb_setlen(((char *)&state->len_hdr),
-                   state->iov[1].iov_len + state->iov[2].iov_len);
-       SCVAL((char *)&state->len_hdr, 0, 0x81);
-
-       state->iov[0].iov_base = &state->len_hdr;
-       state->iov[0].iov_len = sizeof(state->len_hdr);
-
-       subreq = writev_send(state, ev, NULL, sock, true, state->iov, 3);
-       if (tevent_req_nomem(subreq, req)) {
-               return tevent_req_post(req, ev);
-       }
-       tevent_req_set_callback(subreq, cli_session_request_sent, req);
-       return req;
-}
-
-static void cli_session_request_sent(struct tevent_req *subreq)
-{
-       struct tevent_req *req = tevent_req_callback_data(
-               subreq, struct tevent_req);
-       struct cli_session_request_state *state = tevent_req_data(
-               req, struct cli_session_request_state);
-       ssize_t ret;
-       int err;
-
-       ret = writev_recv(subreq, &err);
-       TALLOC_FREE(subreq);
-       if (ret == -1) {
-               tevent_req_error(req, err);
-               return;
-       }
-       subreq = read_smb_send(state, state->ev, state->sock);
-       if (tevent_req_nomem(subreq, req)) {
-               return;
-       }
-       tevent_req_set_callback(subreq, cli_session_request_recvd, req);
-}
-
-static void cli_session_request_recvd(struct tevent_req *subreq)
-{
-       struct tevent_req *req = tevent_req_callback_data(
-               subreq, struct tevent_req);
-       struct cli_session_request_state *state = tevent_req_data(
-               req, struct cli_session_request_state);
-       uint8_t *buf;
-       ssize_t ret;
-       int err;
-
-       ret = read_smb_recv(subreq, talloc_tos(), &buf, &err);
-       TALLOC_FREE(subreq);
-
-       if (ret < 4) {
-               ret = -1;
-               err = EIO;
-       }
-       if (ret == -1) {
-               tevent_req_error(req, err);
-               return;
-       }
-       /*
-        * In case of an error there is more information in the data
-        * portion according to RFC1002. We're not subtle enough to
-        * respond to the different error conditions, so drop the
-        * error info here.
-        */
-       state->nb_session_response = CVAL(buf, 0);
-       tevent_req_done(req);
-}
-
-bool cli_session_request_recv(struct tevent_req *req, int *err, uint8_t *resp)
-{
-       struct cli_session_request_state *state = tevent_req_data(
-               req, struct cli_session_request_state);
-
-       if (tevent_req_is_unix_error(req, err)) {
-               return false;
-       }
-       *resp = state->nb_session_response;
-       return true;
+       return ((tevent_queue_length(cli->conn.outgoing) != 0)
+               || (talloc_array_length(cli->conn.pending) != 0));
 }