2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "libsmb/libsmb.h"
22 #include "../lib/async_req/async_sock.h"
23 #include "../lib/util/tevent_ntstatus.h"
24 #include "../lib/util/tevent_unix.h"
25 #include "async_smb.h"
26 #include "smb_crypt.h"
27 #include "libsmb/nmblib.h"
30 static NTSTATUS cli_pull_raw_error(const uint8_t *buf)
32 uint32_t flags2 = SVAL(buf, smb_flg2);
34 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
35 return NT_STATUS(IVAL(buf, smb_rcls));
38 return NT_STATUS_DOS(CVAL(buf, smb_rcls), SVAL(buf,smb_err));
42 * Figure out if there is an andx command behind the current one
43 * @param[in] buf The smb buffer to look at
44 * @param[in] ofs The offset to the wct field that is followed by the cmd
45 * @retval Is there a command following?
48 static bool have_andx_command(const char *buf, uint16_t ofs)
51 size_t buflen = talloc_get_size(buf);
53 if ((ofs == buflen-1) || (ofs == buflen)) {
60 * Not enough space for the command and a following pointer
64 return (CVAL(buf, ofs+1) != 0xff);
69 struct cli_smb_state {
70 struct tevent_context *ev;
71 struct cli_state *cli;
72 uint8_t header[smb_wct+1]; /* Space for the header including the wct */
75 * For normal requests, cli_smb_req_send chooses a mid. Secondary
76 * trans requests need to use the mid of the primary request, so we
77 * need a place to store it. Assume it's set if != 0.
82 uint8_t bytecount_buf[2];
84 struct iovec iov[MAX_SMB_IOV+3];
91 struct tevent_req **chained_requests;
96 static uint16_t cli_alloc_mid(struct cli_state *cli)
98 int num_pending = talloc_array_length(cli->conn.pending);
104 result = cli->smb1.mid++;
105 if ((result == 0) || (result == 0xffff)) {
109 for (i=0; i<num_pending; i++) {
110 if (result == cli_smb_req_mid(cli->conn.pending[i])) {
115 if (i == num_pending) {
121 void cli_smb_req_unset_pending(struct tevent_req *req)
123 struct cli_smb_state *state = tevent_req_data(
124 req, struct cli_smb_state);
125 struct cli_state *cli = state->cli;
126 int num_pending = talloc_array_length(cli->conn.pending);
129 if (state->mid != 0) {
131 * This is a [nt]trans[2] request which waits
132 * for more than one reply.
137 talloc_set_destructor(req, NULL);
139 if (num_pending == 1) {
141 * The pending read_smb tevent_req is a child of
142 * cli->pending. So if nothing is pending anymore, we need to
143 * delete the socket read fde.
145 TALLOC_FREE(cli->conn.pending);
146 cli->conn.read_smb_req = NULL;
150 for (i=0; i<num_pending; i++) {
151 if (req == cli->conn.pending[i]) {
155 if (i == num_pending) {
157 * Something's seriously broken. Just returning here is the
158 * right thing nevertheless, the point of this routine is to
159 * remove ourselves from cli->conn.pending.
165 * Remove ourselves from the cli->conn.pending array
167 for (; i < (num_pending - 1); i++) {
168 cli->conn.pending[i] = cli->conn.pending[i+1];
172 * No NULL check here, we're shrinking by sizeof(void *), and
173 * talloc_realloc just adjusts the size for this.
175 cli->conn.pending = talloc_realloc(NULL, cli->conn.pending,
181 static int cli_smb_req_destructor(struct tevent_req *req)
183 struct cli_smb_state *state = tevent_req_data(
184 req, struct cli_smb_state);
186 * Make sure we really remove it from
187 * the pending array on destruction.
190 cli_smb_req_unset_pending(req);
194 static bool cli_state_receive_next(struct cli_state *cli);
195 static void cli_state_notify_pending(struct cli_state *cli, NTSTATUS status);
197 bool cli_smb_req_set_pending(struct tevent_req *req)
199 struct cli_smb_state *state = tevent_req_data(
200 req, struct cli_smb_state);
201 struct cli_state *cli;
202 struct tevent_req **pending;
206 num_pending = talloc_array_length(cli->conn.pending);
208 pending = talloc_realloc(cli, cli->conn.pending, struct tevent_req *,
210 if (pending == NULL) {
213 pending[num_pending] = req;
214 cli->conn.pending = pending;
215 talloc_set_destructor(req, cli_smb_req_destructor);
217 if (!cli_state_receive_next(cli)) {
219 * the caller should notify the current request
221 * And all other pending requests get notified
222 * by cli_state_notify_pending().
224 cli_smb_req_unset_pending(req);
225 cli_state_notify_pending(cli, NT_STATUS_NO_MEMORY);
232 static void cli_smb_received(struct tevent_req *subreq);
233 static NTSTATUS cli_state_dispatch_smb1(struct cli_state *cli,
237 static bool cli_state_receive_next(struct cli_state *cli)
239 size_t num_pending = talloc_array_length(cli->conn.pending);
240 struct tevent_req *req;
241 struct cli_smb_state *state;
243 if (cli->conn.read_smb_req != NULL) {
247 if (num_pending == 0) {
251 req = cli->conn.pending[0];
252 state = tevent_req_data(req, struct cli_smb_state);
254 cli->conn.dispatch_incoming = cli_state_dispatch_smb1;
257 * We're the first ones, add the read_smb request that waits for the
258 * answer from the server
260 cli->conn.read_smb_req = read_smb_send(cli->conn.pending, state->ev,
262 if (cli->conn.read_smb_req == NULL) {
265 tevent_req_set_callback(cli->conn.read_smb_req, cli_smb_received, cli);
269 static void cli_state_notify_pending(struct cli_state *cli, NTSTATUS status)
271 cli_state_disconnect(cli);
274 * Cancel all pending requests. We do not do a for-loop walking
275 * cli->conn.pending because that array changes in
276 * cli_smb_req_destructor().
278 while (talloc_array_length(cli->conn.pending) > 0) {
279 struct tevent_req *req;
280 struct cli_smb_state *state;
282 req = cli->conn.pending[0];
283 state = tevent_req_data(req, struct cli_smb_state);
285 cli_smb_req_unset_pending(req);
288 * we need to defer the callback, because we may notify more
291 tevent_req_defer_callback(req, state->ev);
292 tevent_req_nterror(req, status);
297 * Fetch a smb request's mid. Only valid after the request has been sent by
298 * cli_smb_req_send().
300 uint16_t cli_smb_req_mid(struct tevent_req *req)
302 struct cli_smb_state *state = tevent_req_data(
303 req, struct cli_smb_state);
304 return SVAL(state->header, smb_mid);
307 void cli_smb_req_set_mid(struct tevent_req *req, uint16_t mid)
309 struct cli_smb_state *state = tevent_req_data(
310 req, struct cli_smb_state);
314 uint32_t cli_smb_req_seqnum(struct tevent_req *req)
316 struct cli_smb_state *state = tevent_req_data(
317 req, struct cli_smb_state);
318 return state->seqnum;
321 void cli_smb_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
323 struct cli_smb_state *state = tevent_req_data(
324 req, struct cli_smb_state);
325 state->seqnum = seqnum;
328 static size_t iov_len(const struct iovec *iov, int count)
332 for (i=0; i<count; i++) {
333 result += iov[i].iov_len;
338 static uint8_t *iov_concat(TALLOC_CTX *mem_ctx, const struct iovec *iov,
341 size_t len = iov_len(iov, count);
346 buf = talloc_array(mem_ctx, uint8_t, len);
351 for (i=0; i<count; i++) {
352 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
353 copied += iov[i].iov_len;
358 struct tevent_req *cli_smb_req_create(TALLOC_CTX *mem_ctx,
359 struct event_context *ev,
360 struct cli_state *cli,
362 uint8_t additional_flags,
363 uint8_t wct, uint16_t *vwv,
365 struct iovec *bytes_iov)
367 struct tevent_req *result;
368 struct cli_smb_state *state;
369 struct timeval endtime;
371 if (iov_count > MAX_SMB_IOV) {
373 * Should not happen :-)
378 result = tevent_req_create(mem_ctx, &state, struct cli_smb_state);
379 if (result == NULL) {
384 state->mid = 0; /* Set to auto-choose in cli_smb_req_send */
385 state->chain_num = 0;
386 state->chained_requests = NULL;
388 cli_setup_packet_buf(cli, (char *)state->header);
389 SCVAL(state->header, smb_com, smb_command);
390 SSVAL(state->header, smb_tid, cli->smb1.tid);
391 SCVAL(state->header, smb_wct, wct);
395 SSVAL(state->bytecount_buf, 0, iov_len(bytes_iov, iov_count));
397 state->iov[0].iov_base = (void *)state->header;
398 state->iov[0].iov_len = sizeof(state->header);
399 state->iov[1].iov_base = (void *)state->vwv;
400 state->iov[1].iov_len = wct * sizeof(uint16_t);
401 state->iov[2].iov_base = (void *)state->bytecount_buf;
402 state->iov[2].iov_len = sizeof(uint16_t);
404 if (iov_count != 0) {
405 memcpy(&state->iov[3], bytes_iov,
406 iov_count * sizeof(*bytes_iov));
408 state->iov_count = iov_count + 3;
411 endtime = timeval_current_ofs_msec(cli->timeout);
412 if (!tevent_req_set_endtime(result, ev, endtime)) {
413 tevent_req_oom(result);
417 switch (smb_command) {
422 state->one_way = true;
426 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
427 state->one_way = true;
435 static NTSTATUS cli_signv(struct cli_state *cli, struct iovec *iov, int count,
441 * Obvious optimization: Make cli_calculate_sign_mac work with struct
442 * iovec directly. MD5Update would do that just fine.
445 if ((count <= 0) || (iov[0].iov_len < smb_wct)) {
446 return NT_STATUS_INVALID_PARAMETER;
449 buf = iov_concat(talloc_tos(), iov, count);
451 return NT_STATUS_NO_MEMORY;
454 cli_calculate_sign_mac(cli, (char *)buf, seqnum);
455 memcpy(iov[0].iov_base, buf, iov[0].iov_len);
461 static void cli_smb_sent(struct tevent_req *subreq);
463 static NTSTATUS cli_smb_req_iov_send(struct tevent_req *req,
464 struct cli_smb_state *state,
465 struct iovec *iov, int iov_count)
467 struct tevent_req *subreq;
470 if (!cli_state_is_connected(state->cli)) {
471 return NT_STATUS_CONNECTION_INVALID;
474 if (iov[0].iov_len < smb_wct) {
475 return NT_STATUS_INVALID_PARAMETER;
478 if (state->mid != 0) {
479 SSVAL(iov[0].iov_base, smb_mid, state->mid);
481 uint16_t mid = cli_alloc_mid(state->cli);
482 SSVAL(iov[0].iov_base, smb_mid, mid);
485 smb_setlen((char *)iov[0].iov_base, iov_len(iov, iov_count) - 4);
487 status = cli_signv(state->cli, iov, iov_count, &state->seqnum);
489 if (!NT_STATUS_IS_OK(status)) {
493 if (cli_encryption_on(state->cli)) {
496 buf = (char *)iov_concat(talloc_tos(), iov, iov_count);
498 return NT_STATUS_NO_MEMORY;
500 status = common_encrypt_buffer(state->cli->trans_enc_state,
501 (char *)buf, &enc_buf);
503 if (!NT_STATUS_IS_OK(status)) {
504 DEBUG(0, ("Error in encrypting client message: %s\n",
508 buf = (char *)talloc_memdup(state, enc_buf,
512 return NT_STATUS_NO_MEMORY;
514 iov[0].iov_base = (void *)buf;
515 iov[0].iov_len = talloc_get_size(buf);
518 subreq = writev_send(state, state->ev, state->cli->conn.outgoing,
519 state->cli->conn.fd, false, iov, iov_count);
520 if (subreq == NULL) {
521 return NT_STATUS_NO_MEMORY;
523 tevent_req_set_callback(subreq, cli_smb_sent, req);
527 NTSTATUS cli_smb_req_send(struct tevent_req *req)
529 struct cli_smb_state *state = tevent_req_data(
530 req, struct cli_smb_state);
532 return cli_smb_req_iov_send(req, state, state->iov, state->iov_count);
535 struct tevent_req *cli_smb_send(TALLOC_CTX *mem_ctx,
536 struct event_context *ev,
537 struct cli_state *cli,
539 uint8_t additional_flags,
540 uint8_t wct, uint16_t *vwv,
542 const uint8_t *bytes)
544 struct tevent_req *req;
548 iov.iov_base = discard_const_p(void, bytes);
549 iov.iov_len = num_bytes;
551 req = cli_smb_req_create(mem_ctx, ev, cli, smb_command,
552 additional_flags, wct, vwv, 1, &iov);
557 status = cli_smb_req_send(req);
558 if (!NT_STATUS_IS_OK(status)) {
559 tevent_req_nterror(req, status);
560 return tevent_req_post(req, ev);
565 static void cli_smb_sent(struct tevent_req *subreq)
567 struct tevent_req *req = tevent_req_callback_data(
568 subreq, struct tevent_req);
569 struct cli_smb_state *state = tevent_req_data(
570 req, struct cli_smb_state);
574 nwritten = writev_recv(subreq, &err);
576 if (nwritten == -1) {
577 NTSTATUS status = map_nt_error_from_unix(err);
578 cli_state_notify_pending(state->cli, status);
582 if (state->one_way) {
584 tevent_req_done(req);
588 if (!cli_smb_req_set_pending(req)) {
589 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
594 static void cli_smb_received(struct tevent_req *subreq)
596 struct cli_state *cli = tevent_req_callback_data(
597 subreq, struct cli_state);
598 TALLOC_CTX *frame = talloc_stackframe();
604 if (subreq != cli->conn.read_smb_req) {
605 DEBUG(1, ("Internal error: cli_smb_received called with "
606 "unexpected subreq\n"));
607 status = NT_STATUS_INTERNAL_ERROR;
608 cli_state_notify_pending(cli, status);
613 received = read_smb_recv(subreq, frame, &inbuf, &err);
615 cli->conn.read_smb_req = NULL;
616 if (received == -1) {
617 status = map_nt_error_from_unix(err);
618 cli_state_notify_pending(cli, status);
623 status = cli->conn.dispatch_incoming(cli, frame, inbuf);
625 if (NT_STATUS_IS_OK(status)) {
627 * We should not do any more processing
628 * as the dispatch function called
632 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
634 * We got an error, so notify all pending requests
636 cli_state_notify_pending(cli, status);
641 * We got NT_STATUS_RETRY, so we may ask for a
644 if (!cli_state_receive_next(cli)) {
645 cli_state_notify_pending(cli, NT_STATUS_NO_MEMORY);
649 static NTSTATUS cli_state_dispatch_smb1(struct cli_state *cli,
653 struct tevent_req *req;
654 struct cli_smb_state *state;
661 if ((IVAL(inbuf, 4) != 0x424d53ff) /* 0xFF"SMB" */
662 && (SVAL(inbuf, 4) != 0x45ff)) /* 0xFF"E" */ {
663 DEBUG(10, ("Got non-SMB PDU\n"));
664 return NT_STATUS_INVALID_NETWORK_RESPONSE;
667 if (cli_encryption_on(cli) && (CVAL(inbuf, 0) == 0)) {
668 uint16_t enc_ctx_num;
670 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
671 if (!NT_STATUS_IS_OK(status)) {
672 DEBUG(10, ("get_enc_ctx_num returned %s\n",
677 if (enc_ctx_num != cli->trans_enc_state->enc_ctx_num) {
678 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
680 cli->trans_enc_state->enc_ctx_num));
681 return NT_STATUS_INVALID_HANDLE;
684 status = common_decrypt_buffer(cli->trans_enc_state,
686 if (!NT_STATUS_IS_OK(status)) {
687 DEBUG(10, ("common_decrypt_buffer returned %s\n",
693 mid = SVAL(inbuf, smb_mid);
694 num_pending = talloc_array_length(cli->conn.pending);
696 for (i=0; i<num_pending; i++) {
697 if (mid == cli_smb_req_mid(cli->conn.pending[i])) {
701 if (i == num_pending) {
702 /* Dump unexpected reply */
703 return NT_STATUS_RETRY;
706 oplock_break = false;
710 * Paranoia checks that this is really an oplock break request.
712 oplock_break = (smb_len(inbuf) == 51); /* hdr + 8 words */
713 oplock_break &= ((CVAL(inbuf, smb_flg) & FLAG_REPLY) == 0);
714 oplock_break &= (CVAL(inbuf, smb_com) == SMBlockingX);
715 oplock_break &= (SVAL(inbuf, smb_vwv6) == 0);
716 oplock_break &= (SVAL(inbuf, smb_vwv7) == 0);
719 /* Dump unexpected reply */
720 return NT_STATUS_RETRY;
724 req = cli->conn.pending[i];
725 state = tevent_req_data(req, struct cli_smb_state);
727 if (!oplock_break /* oplock breaks are not signed */
728 && !cli_check_sign_mac(cli, (char *)inbuf, state->seqnum+1)) {
729 DEBUG(10, ("cli_check_sign_mac failed\n"));
730 return NT_STATUS_ACCESS_DENIED;
733 if (state->chained_requests != NULL) {
734 struct tevent_req **chain = talloc_move(frame,
735 &state->chained_requests);
736 int num_chained = talloc_array_length(chain);
739 * We steal the inbuf to the chain,
740 * so that it will stay until all
741 * requests of the chain are finished.
743 * Each requests in the chain will
744 * hold a talloc reference to the chain.
745 * This way we do not expose the talloc_reference()
746 * behavior to the callers.
748 talloc_steal(chain, inbuf);
750 for (i=0; i<num_chained; i++) {
751 struct tevent_req **ref;
754 state = tevent_req_data(req, struct cli_smb_state);
756 cli_smb_req_unset_pending(req);
759 * as we finish multiple requests here
760 * we need to defer the callbacks as
761 * they could destroy our current stack state.
763 tevent_req_defer_callback(req, state->ev);
765 ref = talloc_reference(state, chain);
766 if (tevent_req_nomem(ref, req)) {
770 state->inbuf = inbuf;
771 state->chain_num = i;
772 state->chain_length = num_chained;
774 tevent_req_done(req);
777 return NT_STATUS_RETRY;
780 cli_smb_req_unset_pending(req);
782 state->inbuf = talloc_move(state, &inbuf);
783 state->chain_num = 0;
784 state->chain_length = 1;
786 if (talloc_array_length(cli->conn.pending) == 0) {
787 tevent_req_done(req);
791 tevent_req_defer_callback(req, state->ev);
792 tevent_req_done(req);
793 return NT_STATUS_RETRY;
796 NTSTATUS cli_smb_recv(struct tevent_req *req,
797 TALLOC_CTX *mem_ctx, uint8_t **pinbuf,
798 uint8_t min_wct, uint8_t *pwct, uint16_t **pvwv,
799 uint32_t *pnum_bytes, uint8_t **pbytes)
801 struct cli_smb_state *state = tevent_req_data(
802 req, struct cli_smb_state);
803 NTSTATUS status = NT_STATUS_OK;
806 size_t wct_ofs, bytes_offset;
809 if (tevent_req_is_nterror(req, &status)) {
813 if (state->inbuf == NULL) {
815 return NT_STATUS_INVALID_NETWORK_RESPONSE;
832 /* This was a request without a reply */
837 cmd = CVAL(state->inbuf, smb_com);
839 for (i=0; i<state->chain_num; i++) {
840 if (i < state->chain_num-1) {
842 return NT_STATUS_REQUEST_ABORTED;
844 if (!is_andx_req(cmd)) {
845 return NT_STATUS_INVALID_NETWORK_RESPONSE;
849 if (!have_andx_command((char *)state->inbuf, wct_ofs)) {
851 * This request was not completed because a previous
852 * request in the chain had received an error.
854 return NT_STATUS_REQUEST_ABORTED;
857 wct_ofs = SVAL(state->inbuf, wct_ofs + 3);
860 * Skip the all-present length field. No overflow, we've just
861 * put a 16-bit value into a size_t.
865 if (wct_ofs+2 > talloc_get_size(state->inbuf)) {
866 return NT_STATUS_INVALID_NETWORK_RESPONSE;
869 cmd = CVAL(state->inbuf, wct_ofs + 1);
872 state->cli->raw_status = cli_pull_raw_error(state->inbuf);
873 if (NT_STATUS_IS_DOS(state->cli->raw_status)) {
874 uint8_t eclass = NT_STATUS_DOS_CLASS(state->cli->raw_status);
875 uint16_t ecode = NT_STATUS_DOS_CODE(state->cli->raw_status);
877 * TODO: is it really a good idea to do a mapping here?
879 * The old cli_pull_error() also does it, so I do not change
882 status = dos_to_ntstatus(eclass, ecode);
884 status = state->cli->raw_status;
887 if (!have_andx_command((char *)state->inbuf, wct_ofs)) {
889 if ((cmd == SMBsesssetupX)
891 status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
893 * NT_STATUS_MORE_PROCESSING_REQUIRED is a
894 * valid return code for session setup
899 if (NT_STATUS_IS_ERR(status)) {
901 * The last command takes the error code. All
902 * further commands down the requested chain
903 * will get a NT_STATUS_REQUEST_ABORTED.
911 wct = CVAL(state->inbuf, wct_ofs);
912 bytes_offset = wct_ofs + 1 + wct * sizeof(uint16_t);
913 num_bytes = SVAL(state->inbuf, bytes_offset);
916 return NT_STATUS_INVALID_NETWORK_RESPONSE;
920 * wct_ofs is a 16-bit value plus 4, wct is a 8-bit value, num_bytes
921 * is a 16-bit value. So bytes_offset being size_t should be far from
924 if ((bytes_offset + 2 > talloc_get_size(state->inbuf))
925 || (bytes_offset > 0xffff)) {
926 return NT_STATUS_INVALID_NETWORK_RESPONSE;
933 *pvwv = (uint16_t *)(state->inbuf + wct_ofs + 1);
935 if (pnum_bytes != NULL) {
936 *pnum_bytes = num_bytes;
938 if (pbytes != NULL) {
939 *pbytes = (uint8_t *)state->inbuf + bytes_offset + 2;
941 if ((mem_ctx != NULL) && (pinbuf != NULL)) {
942 if (state->chain_num == state->chain_length-1) {
943 *pinbuf = talloc_move(mem_ctx, &state->inbuf);
945 *pinbuf = state->inbuf;
952 size_t cli_smb_wct_ofs(struct tevent_req **reqs, int num_reqs)
957 wct_ofs = smb_wct - 4;
959 for (i=0; i<num_reqs; i++) {
960 struct cli_smb_state *state;
961 state = tevent_req_data(reqs[i], struct cli_smb_state);
962 wct_ofs += iov_len(state->iov+1, state->iov_count-1);
963 wct_ofs = (wct_ofs + 3) & ~3;
968 NTSTATUS cli_smb_chain_send(struct tevent_req **reqs, int num_reqs)
970 struct cli_smb_state *first_state = tevent_req_data(
971 reqs[0], struct cli_smb_state);
972 struct cli_smb_state *last_state = tevent_req_data(
973 reqs[num_reqs-1], struct cli_smb_state);
974 struct cli_smb_state *state;
976 size_t chain_padding = 0;
978 struct iovec *iov = NULL;
979 struct iovec *this_iov;
983 for (i=0; i<num_reqs; i++) {
984 state = tevent_req_data(reqs[i], struct cli_smb_state);
985 iovlen += state->iov_count;
988 iov = talloc_array(last_state, struct iovec, iovlen);
990 return NT_STATUS_NO_MEMORY;
993 first_state->chained_requests = (struct tevent_req **)talloc_memdup(
994 last_state, reqs, sizeof(*reqs) * num_reqs);
995 if (first_state->chained_requests == NULL) {
997 return NT_STATUS_NO_MEMORY;
1000 wct_offset = smb_wct - 4;
1003 for (i=0; i<num_reqs; i++) {
1004 size_t next_padding = 0;
1007 state = tevent_req_data(reqs[i], struct cli_smb_state);
1009 if (i < num_reqs-1) {
1010 if (!is_andx_req(CVAL(state->header, smb_com))
1011 || CVAL(state->header, smb_wct) < 2) {
1013 TALLOC_FREE(first_state->chained_requests);
1014 return NT_STATUS_INVALID_PARAMETER;
1018 wct_offset += iov_len(state->iov+1, state->iov_count-1) + 1;
1019 if ((wct_offset % 4) != 0) {
1020 next_padding = 4 - (wct_offset % 4);
1022 wct_offset += next_padding;
1025 if (i < num_reqs-1) {
1026 struct cli_smb_state *next_state = tevent_req_data(
1027 reqs[i+1], struct cli_smb_state);
1028 SCVAL(vwv+0, 0, CVAL(next_state->header, smb_com));
1030 SSVAL(vwv+1, 0, wct_offset);
1031 } else if (is_andx_req(CVAL(state->header, smb_com))) {
1032 /* properly end the chain */
1033 SCVAL(vwv+0, 0, 0xff);
1034 SCVAL(vwv+0, 1, 0xff);
1039 this_iov[0] = state->iov[0];
1042 * This one is a bit subtle. We have to add
1043 * chain_padding bytes between the requests, and we
1044 * have to also include the wct field of the
1045 * subsequent requests. We use the subsequent header
1046 * for the padding, it contains the wct field in its
1049 this_iov[0].iov_len = chain_padding+1;
1050 this_iov[0].iov_base = (void *)&state->header[
1051 sizeof(state->header) - this_iov[0].iov_len];
1052 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
1054 memcpy(this_iov+1, state->iov+1,
1055 sizeof(struct iovec) * (state->iov_count-1));
1056 this_iov += state->iov_count;
1057 chain_padding = next_padding;
1060 status = cli_smb_req_iov_send(reqs[0], last_state, iov, iovlen);
1061 if (!NT_STATUS_IS_OK(status)) {
1063 TALLOC_FREE(first_state->chained_requests);
1067 return NT_STATUS_OK;
1070 bool cli_has_async_calls(struct cli_state *cli)
1072 return ((tevent_queue_length(cli->conn.outgoing) != 0)
1073 || (talloc_array_length(cli->conn.pending) != 0));
1076 struct cli_smb_oplock_break_waiter_state {
1081 static void cli_smb_oplock_break_waiter_done(struct tevent_req *subreq);
1083 struct tevent_req *cli_smb_oplock_break_waiter_send(TALLOC_CTX *mem_ctx,
1084 struct event_context *ev,
1085 struct cli_state *cli)
1087 struct tevent_req *req, *subreq;
1088 struct cli_smb_oplock_break_waiter_state *state;
1089 struct cli_smb_state *smb_state;
1091 req = tevent_req_create(mem_ctx, &state,
1092 struct cli_smb_oplock_break_waiter_state);
1098 * Create a fake SMB request that we will never send out. This is only
1099 * used to be set into the pending queue with the right mid.
1101 subreq = cli_smb_req_create(mem_ctx, ev, cli, 0, 0, 0, NULL, 0, NULL);
1102 if (tevent_req_nomem(subreq, req)) {
1103 return tevent_req_post(req, ev);
1105 smb_state = tevent_req_data(subreq, struct cli_smb_state);
1106 SSVAL(smb_state->header, smb_mid, 0xffff);
1108 if (!cli_smb_req_set_pending(subreq)) {
1109 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1110 return tevent_req_post(req, ev);
1112 tevent_req_set_callback(subreq, cli_smb_oplock_break_waiter_done, req);
1116 static void cli_smb_oplock_break_waiter_done(struct tevent_req *subreq)
1118 struct tevent_req *req = tevent_req_callback_data(
1119 subreq, struct tevent_req);
1120 struct cli_smb_oplock_break_waiter_state *state = tevent_req_data(
1121 req, struct cli_smb_oplock_break_waiter_state);
1129 status = cli_smb_recv(subreq, state, &inbuf, 8, &wct, &vwv,
1130 &num_bytes, &bytes);
1131 TALLOC_FREE(subreq);
1132 if (!NT_STATUS_IS_OK(status)) {
1133 tevent_req_nterror(req, status);
1136 state->fnum = SVAL(vwv+2, 0);
1137 state->level = CVAL(vwv+3, 1);
1138 tevent_req_done(req);
1141 NTSTATUS cli_smb_oplock_break_waiter_recv(struct tevent_req *req,
1145 struct cli_smb_oplock_break_waiter_state *state = tevent_req_data(
1146 req, struct cli_smb_oplock_break_waiter_state);
1149 if (tevent_req_is_nterror(req, &status)) {
1152 *pfnum = state->fnum;
1153 *plevel = state->level;
1154 return NT_STATUS_OK;
1158 struct cli_session_request_state {
1159 struct tevent_context *ev;
1162 struct iovec iov[3];
1163 uint8_t nb_session_response;
1166 static void cli_session_request_sent(struct tevent_req *subreq);
1167 static void cli_session_request_recvd(struct tevent_req *subreq);
1169 struct tevent_req *cli_session_request_send(TALLOC_CTX *mem_ctx,
1170 struct tevent_context *ev,
1172 const struct nmb_name *called,
1173 const struct nmb_name *calling)
1175 struct tevent_req *req, *subreq;
1176 struct cli_session_request_state *state;
1178 req = tevent_req_create(mem_ctx, &state,
1179 struct cli_session_request_state);
1186 state->iov[1].iov_base = name_mangle(
1187 state, called->name, called->name_type);
1188 if (tevent_req_nomem(state->iov[1].iov_base, req)) {
1189 return tevent_req_post(req, ev);
1191 state->iov[1].iov_len = name_len(
1192 (unsigned char *)state->iov[1].iov_base,
1193 talloc_get_size(state->iov[1].iov_base));
1195 state->iov[2].iov_base = name_mangle(
1196 state, calling->name, calling->name_type);
1197 if (tevent_req_nomem(state->iov[2].iov_base, req)) {
1198 return tevent_req_post(req, ev);
1200 state->iov[2].iov_len = name_len(
1201 (unsigned char *)state->iov[2].iov_base,
1202 talloc_get_size(state->iov[2].iov_base));
1204 _smb_setlen(((char *)&state->len_hdr),
1205 state->iov[1].iov_len + state->iov[2].iov_len);
1206 SCVAL((char *)&state->len_hdr, 0, 0x81);
1208 state->iov[0].iov_base = &state->len_hdr;
1209 state->iov[0].iov_len = sizeof(state->len_hdr);
1211 subreq = writev_send(state, ev, NULL, sock, true, state->iov, 3);
1212 if (tevent_req_nomem(subreq, req)) {
1213 return tevent_req_post(req, ev);
1215 tevent_req_set_callback(subreq, cli_session_request_sent, req);
1219 static void cli_session_request_sent(struct tevent_req *subreq)
1221 struct tevent_req *req = tevent_req_callback_data(
1222 subreq, struct tevent_req);
1223 struct cli_session_request_state *state = tevent_req_data(
1224 req, struct cli_session_request_state);
1228 ret = writev_recv(subreq, &err);
1229 TALLOC_FREE(subreq);
1231 tevent_req_error(req, err);
1234 subreq = read_smb_send(state, state->ev, state->sock);
1235 if (tevent_req_nomem(subreq, req)) {
1238 tevent_req_set_callback(subreq, cli_session_request_recvd, req);
1241 static void cli_session_request_recvd(struct tevent_req *subreq)
1243 struct tevent_req *req = tevent_req_callback_data(
1244 subreq, struct tevent_req);
1245 struct cli_session_request_state *state = tevent_req_data(
1246 req, struct cli_session_request_state);
1251 ret = read_smb_recv(subreq, talloc_tos(), &buf, &err);
1252 TALLOC_FREE(subreq);
1259 tevent_req_error(req, err);
1263 * In case of an error there is more information in the data
1264 * portion according to RFC1002. We're not subtle enough to
1265 * respond to the different error conditions, so drop the
1268 state->nb_session_response = CVAL(buf, 0);
1269 tevent_req_done(req);
1272 bool cli_session_request_recv(struct tevent_req *req, int *err, uint8_t *resp)
1274 struct cli_session_request_state *state = tevent_req_data(
1275 req, struct cli_session_request_state);
1277 if (tevent_req_is_unix_error(req, err)) {
1280 *resp = state->nb_session_response;