s3:libsmb/async_smb: in cli_state_notify_pending() we always disconnect
[rusty/samba.git] / source3 / libsmb / async_smb.c
index b07eee9e10e2b594456497850a7df61b730c80a9..43dfa22b9dfa8fe37f9b352fec737eeb4eb26b36 100644 (file)
 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 (NT_STATUS_IS_OK(status)) {
+               return NT_STATUS_OK;
+       }
 
        if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
-               return NT_STATUS(IVAL(buf, smb_rcls));
+               return status;
        }
 
        return NT_STATUS_DOS(CVAL(buf, smb_rcls), SVAL(buf,smb_err));
@@ -89,6 +94,8 @@ 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)
@@ -99,7 +106,7 @@ static uint16_t cli_alloc_mid(struct cli_state *cli)
        while (true) {
                int i;
 
-               result = cli->smb1.mid++;
+               result = cli->conn.smb1.mid++;
                if ((result == 0) || (result == 0xffff)) {
                        continue;
                }
@@ -280,6 +287,12 @@ static void cli_state_notify_pending(struct cli_state *cli, NTSTATUS status)
                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);
 
                /*
@@ -299,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);
 }
 
@@ -408,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;
 }
 
@@ -450,7 +484,7 @@ static NTSTATUS cli_smb_req_iov_send(struct tevent_req *req,
        NTSTATUS status;
 
        if (!cli_state_is_connected(state->cli)) {
-               return NT_STATUS_CONNECTION_INVALID;
+               return NT_STATUS_CONNECTION_DISCONNECTED;
        }
 
        if (iov[0].iov_len < smb_wct) {
@@ -472,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);
@@ -511,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);
 }
 
@@ -535,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);
@@ -561,21 +601,10 @@ static void cli_smb_sent(struct tevent_req *subreq)
                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)) {
@@ -657,7 +686,7 @@ static NTSTATUS cli_state_dispatch_smb1(struct cli_state *cli,
                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);
@@ -974,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;
        }
@@ -1065,211 +1098,3 @@ bool cli_has_async_calls(struct cli_state *cli)
        return ((tevent_queue_length(cli->conn.outgoing) != 0)
                || (talloc_array_length(cli->conn.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;
-}