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 * Read an smb packet asynchronously, discard keepalives
33 struct read_smb_state
{
34 struct tevent_context
*ev
;
39 static ssize_t
read_smb_more(uint8_t *buf
, size_t buflen
, void *private_data
);
40 static void read_smb_done(struct tevent_req
*subreq
);
42 static struct tevent_req
*read_smb_send(TALLOC_CTX
*mem_ctx
,
43 struct tevent_context
*ev
,
46 struct tevent_req
*result
, *subreq
;
47 struct read_smb_state
*state
;
49 result
= tevent_req_create(mem_ctx
, &state
, struct read_smb_state
);
56 subreq
= read_packet_send(state
, ev
, fd
, 4, read_smb_more
, NULL
);
60 tevent_req_set_callback(subreq
, read_smb_done
, result
);
67 static ssize_t
read_smb_more(uint8_t *buf
, size_t buflen
, void *private_data
)
70 return 0; /* We've been here, we're done */
72 return smb_len_large(buf
);
75 static void read_smb_done(struct tevent_req
*subreq
)
77 struct tevent_req
*req
= tevent_req_callback_data(
78 subreq
, struct tevent_req
);
79 struct read_smb_state
*state
= tevent_req_data(
80 req
, struct read_smb_state
);
84 len
= read_packet_recv(subreq
, state
, &state
->buf
, &err
);
87 tevent_req_error(req
, err
);
91 if (CVAL(state
->buf
, 0) == SMBkeepalive
) {
92 subreq
= read_packet_send(state
, state
->ev
, state
->fd
, 4,
94 if (tevent_req_nomem(subreq
, req
)) {
97 tevent_req_set_callback(subreq
, read_smb_done
, req
);
100 tevent_req_done(req
);
103 static ssize_t
read_smb_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
104 uint8_t **pbuf
, int *perrno
)
106 struct read_smb_state
*state
= tevent_req_data(
107 req
, struct read_smb_state
);
109 if (tevent_req_is_unix_error(req
, perrno
)) {
112 *pbuf
= talloc_move(mem_ctx
, &state
->buf
);
113 return talloc_get_size(*pbuf
);
117 * Fetch an error out of a NBT packet
118 * @param[in] buf The SMB packet
119 * @retval The error, converted to NTSTATUS
122 NTSTATUS
cli_pull_error(char *buf
)
124 uint32_t flags2
= SVAL(buf
, smb_flg2
);
126 if (flags2
& FLAGS2_32_BIT_ERROR_CODES
) {
127 return NT_STATUS(IVAL(buf
, smb_rcls
));
130 return dos_to_ntstatus(CVAL(buf
, smb_rcls
), SVAL(buf
,smb_err
));
134 * Compatibility helper for the sync APIs: Fake NTSTATUS in cli->inbuf
135 * @param[in] cli The client connection that just received an error
136 * @param[in] status The error to set on "cli"
139 void cli_set_error(struct cli_state
*cli
, NTSTATUS status
)
141 uint32_t flags2
= SVAL(cli
->inbuf
, smb_flg2
);
143 if (NT_STATUS_IS_DOS(status
)) {
144 SSVAL(cli
->inbuf
, smb_flg2
,
145 flags2
& ~FLAGS2_32_BIT_ERROR_CODES
);
146 SCVAL(cli
->inbuf
, smb_rcls
, NT_STATUS_DOS_CLASS(status
));
147 SSVAL(cli
->inbuf
, smb_err
, NT_STATUS_DOS_CODE(status
));
151 SSVAL(cli
->inbuf
, smb_flg2
, flags2
| FLAGS2_32_BIT_ERROR_CODES
);
152 SIVAL(cli
->inbuf
, smb_rcls
, NT_STATUS_V(status
));
157 * Figure out if there is an andx command behind the current one
158 * @param[in] buf The smb buffer to look at
159 * @param[in] ofs The offset to the wct field that is followed by the cmd
160 * @retval Is there a command following?
163 static bool have_andx_command(const char *buf
, uint16_t ofs
)
166 size_t buflen
= talloc_get_size(buf
);
168 if ((ofs
== buflen
-1) || (ofs
== buflen
)) {
172 wct
= CVAL(buf
, ofs
);
175 * Not enough space for the command and a following pointer
179 return (CVAL(buf
, ofs
+1) != 0xff);
182 #define MAX_SMB_IOV 5
184 struct cli_smb_state
{
185 struct tevent_context
*ev
;
186 struct cli_state
*cli
;
187 uint8_t header
[smb_wct
+1]; /* Space for the header including the wct */
190 * For normal requests, cli_smb_req_send chooses a mid. Secondary
191 * trans requests need to use the mid of the primary request, so we
192 * need a place to store it. Assume it's set if != 0.
197 uint8_t bytecount_buf
[2];
199 struct iovec iov
[MAX_SMB_IOV
+3];
206 struct tevent_req
**chained_requests
;
209 static uint16_t cli_alloc_mid(struct cli_state
*cli
)
211 int num_pending
= talloc_array_length(cli
->pending
);
218 if ((result
== 0) || (result
== 0xffff)) {
222 for (i
=0; i
<num_pending
; i
++) {
223 if (result
== cli_smb_req_mid(cli
->pending
[i
])) {
228 if (i
== num_pending
) {
234 void cli_smb_req_unset_pending(struct tevent_req
*req
)
236 struct cli_smb_state
*state
= tevent_req_data(
237 req
, struct cli_smb_state
);
238 struct cli_state
*cli
= state
->cli
;
239 int num_pending
= talloc_array_length(cli
->pending
);
242 if (num_pending
== 1) {
244 * The pending read_smb tevent_req is a child of
245 * cli->pending. So if nothing is pending anymore, we need to
246 * delete the socket read fde.
248 TALLOC_FREE(cli
->pending
);
252 for (i
=0; i
<num_pending
; i
++) {
253 if (req
== cli
->pending
[i
]) {
257 if (i
== num_pending
) {
259 * Something's seriously broken. Just returning here is the
260 * right thing nevertheless, the point of this routine is to
261 * remove ourselves from cli->pending.
267 * Remove ourselves from the cli->pending array
269 if (num_pending
> 1) {
270 cli
->pending
[i
] = cli
->pending
[num_pending
-1];
274 * No NULL check here, we're shrinking by sizeof(void *), and
275 * talloc_realloc just adjusts the size for this.
277 cli
->pending
= talloc_realloc(NULL
, cli
->pending
, struct tevent_req
*,
282 static int cli_smb_req_destructor(struct tevent_req
*req
)
284 cli_smb_req_unset_pending(req
);
288 static void cli_smb_received(struct tevent_req
*subreq
);
290 bool cli_smb_req_set_pending(struct tevent_req
*req
)
292 struct cli_smb_state
*state
= tevent_req_data(
293 req
, struct cli_smb_state
);
294 struct cli_state
*cli
;
295 struct tevent_req
**pending
;
297 struct tevent_req
*subreq
;
300 num_pending
= talloc_array_length(cli
->pending
);
302 pending
= talloc_realloc(cli
, cli
->pending
, struct tevent_req
*,
304 if (pending
== NULL
) {
307 pending
[num_pending
] = req
;
308 cli
->pending
= pending
;
309 talloc_set_destructor(req
, cli_smb_req_destructor
);
311 if (num_pending
> 0) {
316 * We're the first ones, add the read_smb request that waits for the
317 * answer from the server
319 subreq
= read_smb_send(cli
->pending
, state
->ev
, cli
->fd
);
320 if (subreq
== NULL
) {
321 cli_smb_req_unset_pending(req
);
324 tevent_req_set_callback(subreq
, cli_smb_received
, cli
);
329 * Fetch a smb request's mid. Only valid after the request has been sent by
330 * cli_smb_req_send().
332 uint16_t cli_smb_req_mid(struct tevent_req
*req
)
334 struct cli_smb_state
*state
= tevent_req_data(
335 req
, struct cli_smb_state
);
336 return SVAL(state
->header
, smb_mid
);
339 void cli_smb_req_set_mid(struct tevent_req
*req
, uint16_t mid
)
341 struct cli_smb_state
*state
= tevent_req_data(
342 req
, struct cli_smb_state
);
346 static size_t iov_len(const struct iovec
*iov
, int count
)
350 for (i
=0; i
<count
; i
++) {
351 result
+= iov
[i
].iov_len
;
356 static uint8_t *iov_concat(TALLOC_CTX
*mem_ctx
, const struct iovec
*iov
,
359 size_t len
= iov_len(iov
, count
);
364 buf
= talloc_array(mem_ctx
, uint8_t, len
);
369 for (i
=0; i
<count
; i
++) {
370 memcpy(buf
+copied
, iov
[i
].iov_base
, iov
[i
].iov_len
);
371 copied
+= iov
[i
].iov_len
;
376 struct tevent_req
*cli_smb_req_create(TALLOC_CTX
*mem_ctx
,
377 struct event_context
*ev
,
378 struct cli_state
*cli
,
380 uint8_t additional_flags
,
381 uint8_t wct
, uint16_t *vwv
,
383 struct iovec
*bytes_iov
)
385 struct tevent_req
*result
;
386 struct cli_smb_state
*state
;
387 struct timeval endtime
;
389 if (iov_count
> MAX_SMB_IOV
) {
391 * Should not happen :-)
396 result
= tevent_req_create(mem_ctx
, &state
, struct cli_smb_state
);
397 if (result
== NULL
) {
402 state
->mid
= 0; /* Set to auto-choose in cli_smb_req_send */
403 state
->chain_num
= 0;
404 state
->chained_requests
= NULL
;
406 cli_setup_packet_buf(cli
, (char *)state
->header
);
407 SCVAL(state
->header
, smb_com
, smb_command
);
408 SSVAL(state
->header
, smb_tid
, cli
->cnum
);
409 SCVAL(state
->header
, smb_wct
, wct
);
413 SSVAL(state
->bytecount_buf
, 0, iov_len(bytes_iov
, iov_count
));
415 state
->iov
[0].iov_base
= (void *)state
->header
;
416 state
->iov
[0].iov_len
= sizeof(state
->header
);
417 state
->iov
[1].iov_base
= (void *)state
->vwv
;
418 state
->iov
[1].iov_len
= wct
* sizeof(uint16_t);
419 state
->iov
[2].iov_base
= (void *)state
->bytecount_buf
;
420 state
->iov
[2].iov_len
= sizeof(uint16_t);
422 if (iov_count
!= 0) {
423 memcpy(&state
->iov
[3], bytes_iov
,
424 iov_count
* sizeof(*bytes_iov
));
426 state
->iov_count
= iov_count
+ 3;
429 endtime
= timeval_current_ofs(cli
->timeout
/ 1000,
430 (cli
->timeout
% 1000) * 1000);
431 if (!tevent_req_set_endtime(result
, ev
, endtime
)) {
432 tevent_req_nomem(NULL
, result
);
438 static NTSTATUS
cli_signv(struct cli_state
*cli
, struct iovec
*iov
, int count
,
444 * Obvious optimization: Make cli_calculate_sign_mac work with struct
445 * iovec directly. MD5Update would do that just fine.
448 if ((count
<= 0) || (iov
[0].iov_len
< smb_wct
)) {
449 return NT_STATUS_INVALID_PARAMETER
;
452 buf
= iov_concat(talloc_tos(), iov
, count
);
454 return NT_STATUS_NO_MEMORY
;
457 cli_calculate_sign_mac(cli
, (char *)buf
, seqnum
);
458 memcpy(iov
[0].iov_base
, buf
, iov
[0].iov_len
);
464 static void cli_smb_sent(struct tevent_req
*subreq
);
466 static NTSTATUS
cli_smb_req_iov_send(struct tevent_req
*req
,
467 struct cli_smb_state
*state
,
468 struct iovec
*iov
, int iov_count
)
470 struct tevent_req
*subreq
;
473 if (state
->cli
->fd
== -1) {
474 return NT_STATUS_CONNECTION_INVALID
;
477 if (iov
[0].iov_len
< smb_wct
) {
478 return NT_STATUS_INVALID_PARAMETER
;
481 if (state
->mid
!= 0) {
482 SSVAL(iov
[0].iov_base
, smb_mid
, state
->mid
);
484 uint16_t mid
= cli_alloc_mid(state
->cli
);
485 SSVAL(iov
[0].iov_base
, smb_mid
, mid
);
488 smb_setlen((char *)iov
[0].iov_base
, iov_len(iov
, iov_count
) - 4);
490 status
= cli_signv(state
->cli
, iov
, iov_count
, &state
->seqnum
);
492 if (!NT_STATUS_IS_OK(status
)) {
496 if (cli_encryption_on(state
->cli
)) {
499 buf
= (char *)iov_concat(talloc_tos(), iov
, iov_count
);
501 return NT_STATUS_NO_MEMORY
;
503 status
= cli_encrypt_message(state
->cli
, (char *)buf
,
506 if (!NT_STATUS_IS_OK(status
)) {
507 DEBUG(0, ("Error in encrypting client message: %s\n",
511 buf
= (char *)talloc_memdup(state
, enc_buf
,
515 return NT_STATUS_NO_MEMORY
;
517 iov
[0].iov_base
= (void *)buf
;
518 iov
[0].iov_len
= talloc_get_size(buf
);
521 subreq
= writev_send(state
, state
->ev
, state
->cli
->outgoing
,
522 state
->cli
->fd
, false, iov
, iov_count
);
523 if (subreq
== NULL
) {
524 return NT_STATUS_NO_MEMORY
;
526 tevent_req_set_callback(subreq
, cli_smb_sent
, req
);
530 NTSTATUS
cli_smb_req_send(struct tevent_req
*req
)
532 struct cli_smb_state
*state
= tevent_req_data(
533 req
, struct cli_smb_state
);
535 return cli_smb_req_iov_send(req
, state
, state
->iov
, state
->iov_count
);
538 struct tevent_req
*cli_smb_send(TALLOC_CTX
*mem_ctx
,
539 struct event_context
*ev
,
540 struct cli_state
*cli
,
542 uint8_t additional_flags
,
543 uint8_t wct
, uint16_t *vwv
,
545 const uint8_t *bytes
)
547 struct tevent_req
*req
;
551 iov
.iov_base
= CONST_DISCARD(void *, bytes
);
552 iov
.iov_len
= num_bytes
;
554 req
= cli_smb_req_create(mem_ctx
, ev
, cli
, smb_command
,
555 additional_flags
, wct
, vwv
, 1, &iov
);
560 status
= cli_smb_req_send(req
);
561 if (!NT_STATUS_IS_OK(status
)) {
562 tevent_req_nterror(req
, status
);
563 return tevent_req_post(req
, ev
);
568 static void cli_smb_sent(struct tevent_req
*subreq
)
570 struct tevent_req
*req
= tevent_req_callback_data(
571 subreq
, struct tevent_req
);
572 struct cli_smb_state
*state
= tevent_req_data(
573 req
, struct cli_smb_state
);
577 nwritten
= writev_recv(subreq
, &err
);
579 if (nwritten
== -1) {
580 if (state
->cli
->fd
!= -1) {
581 close(state
->cli
->fd
);
584 tevent_req_nterror(req
, map_nt_error_from_unix(err
));
588 switch (CVAL(state
->header
, smb_com
)) {
594 tevent_req_done(req
);
597 if ((CVAL(state
->header
, smb_wct
) == 8) &&
598 (CVAL(state
->vwv
+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE
)) {
600 tevent_req_done(req
);
605 if (!cli_smb_req_set_pending(req
)) {
606 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
611 static void cli_smb_received(struct tevent_req
*subreq
)
613 struct cli_state
*cli
= tevent_req_callback_data(
614 subreq
, struct cli_state
);
615 struct tevent_req
*req
;
616 struct cli_smb_state
*state
;
625 received
= read_smb_recv(subreq
, talloc_tos(), &inbuf
, &err
);
627 if (received
== -1) {
632 status
= map_nt_error_from_unix(err
);
636 if ((IVAL(inbuf
, 4) != 0x424d53ff) /* 0xFF"SMB" */
637 && (SVAL(inbuf
, 4) != 0x45ff)) /* 0xFF"E" */ {
638 DEBUG(10, ("Got non-SMB PDU\n"));
639 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
643 if (cli_encryption_on(cli
) && (CVAL(inbuf
, 0) == 0)) {
644 uint16_t enc_ctx_num
;
646 status
= get_enc_ctx_num(inbuf
, &enc_ctx_num
);
647 if (!NT_STATUS_IS_OK(status
)) {
648 DEBUG(10, ("get_enc_ctx_num returned %s\n",
653 if (enc_ctx_num
!= cli
->trans_enc_state
->enc_ctx_num
) {
654 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
656 cli
->trans_enc_state
->enc_ctx_num
));
657 status
= NT_STATUS_INVALID_HANDLE
;
661 status
= common_decrypt_buffer(cli
->trans_enc_state
,
663 if (!NT_STATUS_IS_OK(status
)) {
664 DEBUG(10, ("common_decrypt_buffer returned %s\n",
670 mid
= SVAL(inbuf
, smb_mid
);
671 num_pending
= talloc_array_length(cli
->pending
);
673 for (i
=0; i
<num_pending
; i
++) {
674 if (mid
== cli_smb_req_mid(cli
->pending
[i
])) {
678 if (i
== num_pending
) {
679 /* Dump unexpected reply */
684 oplock_break
= false;
688 * Paranoia checks that this is really an oplock break request.
690 oplock_break
= (smb_len(inbuf
) == 51); /* hdr + 8 words */
691 oplock_break
&= ((CVAL(inbuf
, smb_flg
) & FLAG_REPLY
) == 0);
692 oplock_break
&= (CVAL(inbuf
, smb_com
) == SMBlockingX
);
693 oplock_break
&= (SVAL(inbuf
, smb_vwv6
) == 0);
694 oplock_break
&= (SVAL(inbuf
, smb_vwv7
) == 0);
697 /* Dump unexpected reply */
703 req
= cli
->pending
[i
];
704 state
= tevent_req_data(req
, struct cli_smb_state
);
706 if (!oplock_break
/* oplock breaks are not signed */
707 && !cli_check_sign_mac(cli
, (char *)inbuf
, state
->seqnum
+1)) {
708 DEBUG(10, ("cli_check_sign_mac failed\n"));
710 status
= NT_STATUS_ACCESS_DENIED
;
716 if (state
->chained_requests
== NULL
) {
717 state
->inbuf
= talloc_move(state
, &inbuf
);
718 talloc_set_destructor(req
, NULL
);
719 cli_smb_req_destructor(req
);
720 state
->chain_num
= 0;
721 state
->chain_length
= 1;
722 tevent_req_done(req
);
724 struct tevent_req
**chain
= talloc_move(
725 talloc_tos(), &state
->chained_requests
);
726 int num_chained
= talloc_array_length(chain
);
728 for (i
=0; i
<num_chained
; i
++) {
729 state
= tevent_req_data(chain
[i
], struct
731 state
->inbuf
= inbuf
;
732 state
->chain_num
= i
;
733 state
->chain_length
= num_chained
;
734 tevent_req_done(chain
[i
]);
740 if (talloc_array_length(cli
->pending
) > 0) {
742 * Set up another read request for the other pending cli_smb
745 state
= tevent_req_data(cli
->pending
[0], struct cli_smb_state
);
746 subreq
= read_smb_send(cli
->pending
, state
->ev
, cli
->fd
);
747 if (subreq
== NULL
) {
748 status
= NT_STATUS_NO_MEMORY
;
751 tevent_req_set_callback(subreq
, cli_smb_received
, cli
);
756 * Cancel all pending requests. We don't do a for-loop walking
757 * cli->pending because that array changes in
758 * cli_smb_req_destructor().
760 while (talloc_array_length(cli
->pending
) > 0) {
761 req
= cli
->pending
[0];
762 talloc_set_destructor(req
, NULL
);
763 cli_smb_req_destructor(req
);
764 tevent_req_nterror(req
, status
);
768 NTSTATUS
cli_smb_recv(struct tevent_req
*req
,
769 TALLOC_CTX
*mem_ctx
, uint8_t **pinbuf
,
770 uint8_t min_wct
, uint8_t *pwct
, uint16_t **pvwv
,
771 uint32_t *pnum_bytes
, uint8_t **pbytes
)
773 struct cli_smb_state
*state
= tevent_req_data(
774 req
, struct cli_smb_state
);
775 NTSTATUS status
= NT_STATUS_OK
;
778 size_t wct_ofs
, bytes_offset
;
781 if (tevent_req_is_nterror(req
, &status
)) {
785 if (state
->inbuf
== NULL
) {
786 /* This was a request without a reply */
791 cmd
= CVAL(state
->inbuf
, smb_com
);
793 for (i
=0; i
<state
->chain_num
; i
++) {
794 if (i
< state
->chain_num
-1) {
796 return NT_STATUS_REQUEST_ABORTED
;
798 if (!is_andx_req(cmd
)) {
799 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
803 if (!have_andx_command((char *)state
->inbuf
, wct_ofs
)) {
805 * This request was not completed because a previous
806 * request in the chain had received an error.
808 return NT_STATUS_REQUEST_ABORTED
;
811 wct_ofs
= SVAL(state
->inbuf
, wct_ofs
+ 3);
814 * Skip the all-present length field. No overflow, we've just
815 * put a 16-bit value into a size_t.
819 if (wct_ofs
+2 > talloc_get_size(state
->inbuf
)) {
820 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
823 cmd
= CVAL(state
->inbuf
, wct_ofs
+ 1);
826 status
= cli_pull_error((char *)state
->inbuf
);
828 if (!have_andx_command((char *)state
->inbuf
, wct_ofs
)) {
830 if ((cmd
== SMBsesssetupX
)
832 status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
834 * NT_STATUS_MORE_PROCESSING_REQUIRED is a
835 * valid return code for session setup
840 if (NT_STATUS_IS_ERR(status
)) {
842 * The last command takes the error code. All
843 * further commands down the requested chain
844 * will get a NT_STATUS_REQUEST_ABORTED.
852 wct
= CVAL(state
->inbuf
, wct_ofs
);
853 bytes_offset
= wct_ofs
+ 1 + wct
* sizeof(uint16_t);
854 num_bytes
= SVAL(state
->inbuf
, bytes_offset
);
857 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
861 * wct_ofs is a 16-bit value plus 4, wct is a 8-bit value, num_bytes
862 * is a 16-bit value. So bytes_offset being size_t should be far from
865 if ((bytes_offset
+ 2 > talloc_get_size(state
->inbuf
))
866 || (bytes_offset
> 0xffff)) {
867 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
874 *pvwv
= (uint16_t *)(state
->inbuf
+ wct_ofs
+ 1);
876 if (pnum_bytes
!= NULL
) {
877 *pnum_bytes
= num_bytes
;
879 if (pbytes
!= NULL
) {
880 *pbytes
= (uint8_t *)state
->inbuf
+ bytes_offset
+ 2;
882 if ((mem_ctx
!= NULL
) && (pinbuf
!= NULL
)) {
883 if (state
->chain_num
== state
->chain_length
-1) {
884 *pinbuf
= talloc_move(mem_ctx
, &state
->inbuf
);
886 *pinbuf
= state
->inbuf
;
893 size_t cli_smb_wct_ofs(struct tevent_req
**reqs
, int num_reqs
)
898 wct_ofs
= smb_wct
- 4;
900 for (i
=0; i
<num_reqs
; i
++) {
901 struct cli_smb_state
*state
;
902 state
= tevent_req_data(reqs
[i
], struct cli_smb_state
);
903 wct_ofs
+= iov_len(state
->iov
+1, state
->iov_count
-1);
904 wct_ofs
= (wct_ofs
+ 3) & ~3;
909 NTSTATUS
cli_smb_chain_send(struct tevent_req
**reqs
, int num_reqs
)
911 struct cli_smb_state
*first_state
= tevent_req_data(
912 reqs
[0], struct cli_smb_state
);
913 struct cli_smb_state
*last_state
= tevent_req_data(
914 reqs
[num_reqs
-1], struct cli_smb_state
);
915 struct cli_smb_state
*state
;
917 size_t chain_padding
= 0;
919 struct iovec
*iov
= NULL
;
920 struct iovec
*this_iov
;
924 for (i
=0; i
<num_reqs
; i
++) {
925 state
= tevent_req_data(reqs
[i
], struct cli_smb_state
);
926 iovlen
+= state
->iov_count
;
929 iov
= talloc_array(last_state
, struct iovec
, iovlen
);
931 return NT_STATUS_NO_MEMORY
;
934 first_state
->chained_requests
= (struct tevent_req
**)talloc_memdup(
935 last_state
, reqs
, sizeof(*reqs
) * num_reqs
);
936 if (first_state
->chained_requests
== NULL
) {
938 return NT_STATUS_NO_MEMORY
;
941 wct_offset
= smb_wct
- 4;
944 for (i
=0; i
<num_reqs
; i
++) {
945 size_t next_padding
= 0;
948 state
= tevent_req_data(reqs
[i
], struct cli_smb_state
);
950 if (i
< num_reqs
-1) {
951 if (!is_andx_req(CVAL(state
->header
, smb_com
))
952 || CVAL(state
->header
, smb_wct
) < 2) {
954 TALLOC_FREE(first_state
->chained_requests
);
955 return NT_STATUS_INVALID_PARAMETER
;
959 wct_offset
+= iov_len(state
->iov
+1, state
->iov_count
-1) + 1;
960 if ((wct_offset
% 4) != 0) {
961 next_padding
= 4 - (wct_offset
% 4);
963 wct_offset
+= next_padding
;
966 if (i
< num_reqs
-1) {
967 struct cli_smb_state
*next_state
= tevent_req_data(
968 reqs
[i
+1], struct cli_smb_state
);
969 SCVAL(vwv
+0, 0, CVAL(next_state
->header
, smb_com
));
971 SSVAL(vwv
+1, 0, wct_offset
);
972 } else if (is_andx_req(CVAL(state
->header
, smb_com
))) {
973 /* properly end the chain */
974 SCVAL(vwv
+0, 0, 0xff);
975 SCVAL(vwv
+0, 1, 0xff);
980 this_iov
[0] = state
->iov
[0];
983 * This one is a bit subtle. We have to add
984 * chain_padding bytes between the requests, and we
985 * have to also include the wct field of the
986 * subsequent requests. We use the subsequent header
987 * for the padding, it contains the wct field in its
990 this_iov
[0].iov_len
= chain_padding
+1;
991 this_iov
[0].iov_base
= (void *)&state
->header
[
992 sizeof(state
->header
) - this_iov
[0].iov_len
];
993 memset(this_iov
[0].iov_base
, 0, this_iov
[0].iov_len
-1);
995 memcpy(this_iov
+1, state
->iov
+1,
996 sizeof(struct iovec
) * (state
->iov_count
-1));
997 this_iov
+= state
->iov_count
;
998 chain_padding
= next_padding
;
1001 status
= cli_smb_req_iov_send(reqs
[0], last_state
, iov
, iovlen
);
1002 if (!NT_STATUS_IS_OK(status
)) {
1004 TALLOC_FREE(first_state
->chained_requests
);
1008 return NT_STATUS_OK
;
1011 uint8_t *cli_smb_inbuf(struct tevent_req
*req
)
1013 struct cli_smb_state
*state
= tevent_req_data(
1014 req
, struct cli_smb_state
);
1015 return state
->inbuf
;
1018 bool cli_has_async_calls(struct cli_state
*cli
)
1020 return ((tevent_queue_length(cli
->outgoing
) != 0)
1021 || (talloc_array_length(cli
->pending
) != 0));
1024 struct cli_smb_oplock_break_waiter_state
{
1029 static void cli_smb_oplock_break_waiter_done(struct tevent_req
*subreq
);
1031 struct tevent_req
*cli_smb_oplock_break_waiter_send(TALLOC_CTX
*mem_ctx
,
1032 struct event_context
*ev
,
1033 struct cli_state
*cli
)
1035 struct tevent_req
*req
, *subreq
;
1036 struct cli_smb_oplock_break_waiter_state
*state
;
1037 struct cli_smb_state
*smb_state
;
1039 req
= tevent_req_create(mem_ctx
, &state
,
1040 struct cli_smb_oplock_break_waiter_state
);
1046 * Create a fake SMB request that we will never send out. This is only
1047 * used to be set into the pending queue with the right mid.
1049 subreq
= cli_smb_req_create(mem_ctx
, ev
, cli
, 0, 0, 0, NULL
, 0, NULL
);
1050 if (tevent_req_nomem(subreq
, req
)) {
1051 return tevent_req_post(req
, ev
);
1053 smb_state
= tevent_req_data(subreq
, struct cli_smb_state
);
1054 SSVAL(smb_state
->header
, smb_mid
, 0xffff);
1056 if (!cli_smb_req_set_pending(subreq
)) {
1057 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1058 return tevent_req_post(req
, ev
);
1060 tevent_req_set_callback(subreq
, cli_smb_oplock_break_waiter_done
, req
);
1064 static void cli_smb_oplock_break_waiter_done(struct tevent_req
*subreq
)
1066 struct tevent_req
*req
= tevent_req_callback_data(
1067 subreq
, struct tevent_req
);
1068 struct cli_smb_oplock_break_waiter_state
*state
= tevent_req_data(
1069 req
, struct cli_smb_oplock_break_waiter_state
);
1077 status
= cli_smb_recv(subreq
, state
, &inbuf
, 8, &wct
, &vwv
,
1078 &num_bytes
, &bytes
);
1079 TALLOC_FREE(subreq
);
1080 if (!NT_STATUS_IS_OK(status
)) {
1081 tevent_req_nterror(req
, status
);
1084 state
->fnum
= SVAL(vwv
+2, 0);
1085 state
->level
= CVAL(vwv
+3, 1);
1086 tevent_req_done(req
);
1089 NTSTATUS
cli_smb_oplock_break_waiter_recv(struct tevent_req
*req
,
1093 struct cli_smb_oplock_break_waiter_state
*state
= tevent_req_data(
1094 req
, struct cli_smb_oplock_break_waiter_state
);
1097 if (tevent_req_is_nterror(req
, &status
)) {
1100 *pfnum
= state
->fnum
;
1101 *plevel
= state
->level
;
1102 return NT_STATUS_OK
;
1106 struct cli_session_request_state
{
1107 struct tevent_context
*ev
;
1110 struct iovec iov
[3];
1111 uint8_t nb_session_response
;
1114 static void cli_session_request_sent(struct tevent_req
*subreq
);
1115 static void cli_session_request_recvd(struct tevent_req
*subreq
);
1117 struct tevent_req
*cli_session_request_send(TALLOC_CTX
*mem_ctx
,
1118 struct tevent_context
*ev
,
1120 const struct nmb_name
*called
,
1121 const struct nmb_name
*calling
)
1123 struct tevent_req
*req
, *subreq
;
1124 struct cli_session_request_state
*state
;
1126 req
= tevent_req_create(mem_ctx
, &state
,
1127 struct cli_session_request_state
);
1134 state
->iov
[1].iov_base
= name_mangle(
1135 state
, called
->name
, called
->name_type
);
1136 if (tevent_req_nomem(state
->iov
[1].iov_base
, req
)) {
1137 return tevent_req_post(req
, ev
);
1139 state
->iov
[1].iov_len
= name_len(
1140 (unsigned char *)state
->iov
[1].iov_base
,
1141 talloc_get_size(state
->iov
[1].iov_base
));
1143 state
->iov
[2].iov_base
= name_mangle(
1144 state
, calling
->name
, calling
->name_type
);
1145 if (tevent_req_nomem(state
->iov
[2].iov_base
, req
)) {
1146 return tevent_req_post(req
, ev
);
1148 state
->iov
[2].iov_len
= name_len(
1149 (unsigned char *)state
->iov
[2].iov_base
,
1150 talloc_get_size(state
->iov
[2].iov_base
));
1152 _smb_setlen(((char *)&state
->len_hdr
),
1153 state
->iov
[1].iov_len
+ state
->iov
[2].iov_len
);
1154 SCVAL((char *)&state
->len_hdr
, 0, 0x81);
1156 state
->iov
[0].iov_base
= &state
->len_hdr
;
1157 state
->iov
[0].iov_len
= sizeof(state
->len_hdr
);
1159 subreq
= writev_send(state
, ev
, NULL
, sock
, true, state
->iov
, 3);
1160 if (tevent_req_nomem(subreq
, req
)) {
1161 return tevent_req_post(req
, ev
);
1163 tevent_req_set_callback(subreq
, cli_session_request_sent
, req
);
1167 static void cli_session_request_sent(struct tevent_req
*subreq
)
1169 struct tevent_req
*req
= tevent_req_callback_data(
1170 subreq
, struct tevent_req
);
1171 struct cli_session_request_state
*state
= tevent_req_data(
1172 req
, struct cli_session_request_state
);
1176 ret
= writev_recv(subreq
, &err
);
1177 TALLOC_FREE(subreq
);
1179 tevent_req_error(req
, err
);
1182 subreq
= read_smb_send(state
, state
->ev
, state
->sock
);
1183 if (tevent_req_nomem(subreq
, req
)) {
1186 tevent_req_set_callback(subreq
, cli_session_request_recvd
, req
);
1189 static void cli_session_request_recvd(struct tevent_req
*subreq
)
1191 struct tevent_req
*req
= tevent_req_callback_data(
1192 subreq
, struct tevent_req
);
1193 struct cli_session_request_state
*state
= tevent_req_data(
1194 req
, struct cli_session_request_state
);
1199 ret
= read_smb_recv(subreq
, talloc_tos(), &buf
, &err
);
1200 TALLOC_FREE(subreq
);
1207 tevent_req_error(req
, err
);
1211 * In case of an error there is more information in the data
1212 * portion according to RFC1002. We're not subtle enough to
1213 * respond to the different error conditions, so drop the
1216 state
->nb_session_response
= CVAL(buf
, 0);
1217 tevent_req_done(req
);
1220 bool cli_session_request_recv(struct tevent_req
*req
, int *err
, uint8_t *resp
)
1222 struct cli_session_request_state
*state
= tevent_req_data(
1223 req
, struct cli_session_request_state
);
1225 if (tevent_req_is_unix_error(req
, err
)) {
1228 *resp
= state
->nb_session_response
;