2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Stefan Metzmacher 2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/network.h"
23 #include "../lib/async_req/async_sock.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
26 #include "lib/util/util_net.h"
27 #include "lib/util/dlinklist.h"
28 #include "../libcli/smb/smb_common.h"
29 #include "../libcli/smb/smb_seal.h"
30 #include "../libcli/smb/smb_signing.h"
31 #include "../libcli/smb/read_smb.h"
32 #include "smbXcli_base.h"
33 #include "librpc/ndr/libndr.h"
37 struct smbXcli_session
;
42 struct sockaddr_storage local_ss
;
43 struct sockaddr_storage remote_ss
;
44 const char *remote_name
;
46 struct tevent_queue
*outgoing
;
47 struct tevent_req
**pending
;
48 struct tevent_req
*read_smb_req
;
50 enum protocol_types protocol
;
53 bool mandatory_signing
;
56 * The incoming dispatch function should return:
57 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
58 * - NT_STATUS_OK, if no more processing is desired, e.g.
59 * the dispatch function called
61 * - All other return values disconnect the connection.
63 NTSTATUS (*dispatch_incoming
)(struct smbXcli_conn
*conn
,
69 uint32_t capabilities
;
74 uint32_t capabilities
;
77 uint16_t security_mode
;
86 const char *workgroup
;
92 uint32_t capabilities
;
97 struct smb_signing_state
*signing
;
98 struct smb_trans_enc_state
*trans_enc
;
100 struct tevent_req
*read_braw_req
;
105 uint32_t capabilities
;
106 uint16_t security_mode
;
111 uint32_t capabilities
;
112 uint16_t security_mode
;
114 uint32_t max_trans_size
;
115 uint32_t max_read_size
;
116 uint32_t max_write_size
;
123 uint16_t cur_credits
;
124 uint16_t max_credits
;
127 struct smbXcli_session
*sessions
;
130 struct smbXcli_session
{
131 struct smbXcli_session
*prev
, *next
;
132 struct smbXcli_conn
*conn
;
140 uint16_t session_flags
;
141 DATA_BLOB application_key
;
142 DATA_BLOB signing_key
;
145 DATA_BLOB encryption_key
;
146 DATA_BLOB decryption_key
;
147 uint64_t channel_nonce
;
148 uint64_t channel_next
;
149 DATA_BLOB channel_signing_key
;
153 struct smbXcli_req_state
{
154 struct tevent_context
*ev
;
155 struct smbXcli_conn
*conn
;
156 struct smbXcli_session
*session
; /* maybe NULL */
158 uint8_t length_hdr
[4];
165 /* Space for the header including the wct */
166 uint8_t hdr
[HDR_VWV
];
169 * For normal requests, smb1cli_req_send chooses a mid.
170 * SecondaryV trans requests need to use the mid of the primary
171 * request, so we need a place to store it.
172 * Assume it is set if != 0.
177 uint8_t bytecount_buf
[2];
179 #define MAX_SMB_IOV 10
180 /* length_hdr, hdr, words, byte_count, buffers */
181 struct iovec iov
[1 + 3 + MAX_SMB_IOV
];
186 struct tevent_req
**chained_requests
;
189 NTSTATUS recv_status
;
190 /* always an array of 3 talloc elements */
191 struct iovec
*recv_iov
;
195 const uint8_t *fixed
;
200 uint8_t transform
[SMB2_TF_HDR_SIZE
];
201 uint8_t hdr
[SMB2_HDR_BODY
];
202 uint8_t pad
[7]; /* padding space for compounding */
205 * always an array of 3 talloc elements
206 * (without a SMB2_TRANSFORM header!)
210 struct iovec
*recv_iov
;
212 uint16_t credit_charge
;
217 bool signing_skipped
;
223 static int smbXcli_conn_destructor(struct smbXcli_conn
*conn
)
226 * NT_STATUS_OK, means we do not notify the callers
228 smbXcli_conn_disconnect(conn
, NT_STATUS_OK
);
230 while (conn
->sessions
) {
231 conn
->sessions
->conn
= NULL
;
232 DLIST_REMOVE(conn
->sessions
, conn
->sessions
);
235 if (conn
->smb1
.trans_enc
) {
236 TALLOC_FREE(conn
->smb1
.trans_enc
);
242 struct smbXcli_conn
*smbXcli_conn_create(TALLOC_CTX
*mem_ctx
,
244 const char *remote_name
,
245 enum smb_signing_setting signing_state
,
246 uint32_t smb1_capabilities
,
247 struct GUID
*client_guid
,
248 uint32_t smb2_capabilities
)
250 struct smbXcli_conn
*conn
= NULL
;
252 struct sockaddr
*sa
= NULL
;
256 conn
= talloc_zero(mem_ctx
, struct smbXcli_conn
);
262 conn
->write_fd
= dup(fd
);
263 if (conn
->write_fd
== -1) {
267 conn
->remote_name
= talloc_strdup(conn
, remote_name
);
268 if (conn
->remote_name
== NULL
) {
273 ss
= (void *)&conn
->local_ss
;
274 sa
= (struct sockaddr
*)ss
;
275 sa_length
= sizeof(conn
->local_ss
);
276 ret
= getsockname(fd
, sa
, &sa_length
);
280 ss
= (void *)&conn
->remote_ss
;
281 sa
= (struct sockaddr
*)ss
;
282 sa_length
= sizeof(conn
->remote_ss
);
283 ret
= getpeername(fd
, sa
, &sa_length
);
288 conn
->outgoing
= tevent_queue_create(conn
, "smbXcli_outgoing");
289 if (conn
->outgoing
== NULL
) {
292 conn
->pending
= NULL
;
294 conn
->protocol
= PROTOCOL_NONE
;
296 switch (signing_state
) {
297 case SMB_SIGNING_OFF
:
299 conn
->allow_signing
= false;
300 conn
->desire_signing
= false;
301 conn
->mandatory_signing
= false;
303 case SMB_SIGNING_DEFAULT
:
304 case SMB_SIGNING_IF_REQUIRED
:
305 /* if the server requires it */
306 conn
->allow_signing
= true;
307 conn
->desire_signing
= false;
308 conn
->mandatory_signing
= false;
310 case SMB_SIGNING_REQUIRED
:
312 conn
->allow_signing
= true;
313 conn
->desire_signing
= true;
314 conn
->mandatory_signing
= true;
318 conn
->smb1
.client
.capabilities
= smb1_capabilities
;
319 conn
->smb1
.client
.max_xmit
= UINT16_MAX
;
321 conn
->smb1
.capabilities
= conn
->smb1
.client
.capabilities
;
322 conn
->smb1
.max_xmit
= 1024;
326 /* initialise signing */
327 conn
->smb1
.signing
= smb_signing_init(conn
,
329 conn
->desire_signing
,
330 conn
->mandatory_signing
);
331 if (!conn
->smb1
.signing
) {
335 conn
->smb2
.client
.security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
336 if (conn
->mandatory_signing
) {
337 conn
->smb2
.client
.security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
340 conn
->smb2
.client
.guid
= *client_guid
;
342 conn
->smb2
.client
.capabilities
= smb2_capabilities
;
344 conn
->smb2
.cur_credits
= 1;
345 conn
->smb2
.max_credits
= 0;
347 talloc_set_destructor(conn
, smbXcli_conn_destructor
);
351 if (conn
->write_fd
!= -1) {
352 close(conn
->write_fd
);
358 bool smbXcli_conn_is_connected(struct smbXcli_conn
*conn
)
364 if (conn
->read_fd
== -1) {
371 enum protocol_types
smbXcli_conn_protocol(struct smbXcli_conn
*conn
)
373 return conn
->protocol
;
376 bool smbXcli_conn_use_unicode(struct smbXcli_conn
*conn
)
378 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
382 if (conn
->smb1
.capabilities
& CAP_UNICODE
) {
389 void smbXcli_conn_set_sockopt(struct smbXcli_conn
*conn
, const char *options
)
391 set_socket_options(conn
->read_fd
, options
);
394 const struct sockaddr_storage
*smbXcli_conn_local_sockaddr(struct smbXcli_conn
*conn
)
396 return &conn
->local_ss
;
399 const struct sockaddr_storage
*smbXcli_conn_remote_sockaddr(struct smbXcli_conn
*conn
)
401 return &conn
->remote_ss
;
404 const char *smbXcli_conn_remote_name(struct smbXcli_conn
*conn
)
406 return conn
->remote_name
;
409 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn
*conn
)
411 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
418 return conn
->smb1
.server
.max_mux
;
421 NTTIME
smbXcli_conn_server_system_time(struct smbXcli_conn
*conn
)
423 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
424 return conn
->smb2
.server
.system_time
;
427 return conn
->smb1
.server
.system_time
;
430 const DATA_BLOB
*smbXcli_conn_server_gss_blob(struct smbXcli_conn
*conn
)
432 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
433 return &conn
->smb2
.server
.gss_blob
;
436 return &conn
->smb1
.server
.gss_blob
;
439 const struct GUID
*smbXcli_conn_server_guid(struct smbXcli_conn
*conn
)
441 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
442 return &conn
->smb2
.server
.guid
;
445 return &conn
->smb1
.server
.guid
;
448 struct smbXcli_conn_samba_suicide_state
{
449 struct smbXcli_conn
*conn
;
454 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
);
456 struct tevent_req
*smbXcli_conn_samba_suicide_send(TALLOC_CTX
*mem_ctx
,
457 struct tevent_context
*ev
,
458 struct smbXcli_conn
*conn
,
461 struct tevent_req
*req
, *subreq
;
462 struct smbXcli_conn_samba_suicide_state
*state
;
464 req
= tevent_req_create(mem_ctx
, &state
,
465 struct smbXcli_conn_samba_suicide_state
);
470 SIVAL(state
->buf
, 4, 0x74697865);
471 SCVAL(state
->buf
, 8, exitcode
);
472 _smb_setlen_nbt(state
->buf
, sizeof(state
->buf
)-4);
474 state
->iov
.iov_base
= state
->buf
;
475 state
->iov
.iov_len
= sizeof(state
->buf
);
477 subreq
= writev_send(state
, ev
, conn
->outgoing
, conn
->write_fd
,
478 false, &state
->iov
, 1);
479 if (tevent_req_nomem(subreq
, req
)) {
480 return tevent_req_post(req
, ev
);
482 tevent_req_set_callback(subreq
, smbXcli_conn_samba_suicide_done
, req
);
486 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
)
488 struct tevent_req
*req
= tevent_req_callback_data(
489 subreq
, struct tevent_req
);
490 struct smbXcli_conn_samba_suicide_state
*state
= tevent_req_data(
491 req
, struct smbXcli_conn_samba_suicide_state
);
495 nwritten
= writev_recv(subreq
, &err
);
497 if (nwritten
== -1) {
498 NTSTATUS status
= map_nt_error_from_unix_common(err
);
499 smbXcli_conn_disconnect(state
->conn
, status
);
502 tevent_req_done(req
);
505 NTSTATUS
smbXcli_conn_samba_suicide_recv(struct tevent_req
*req
)
507 return tevent_req_simple_recv_ntstatus(req
);
510 NTSTATUS
smbXcli_conn_samba_suicide(struct smbXcli_conn
*conn
,
513 TALLOC_CTX
*frame
= talloc_stackframe();
514 struct tevent_context
*ev
;
515 struct tevent_req
*req
;
516 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
519 if (smbXcli_conn_has_async_calls(conn
)) {
521 * Can't use sync call while an async call is in flight
523 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
526 ev
= tevent_context_init(frame
);
530 req
= smbXcli_conn_samba_suicide_send(frame
, ev
, conn
, exitcode
);
534 ok
= tevent_req_poll(req
, ev
);
536 status
= map_nt_error_from_unix_common(errno
);
539 status
= smbXcli_conn_samba_suicide_recv(req
);
545 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn
*conn
)
547 return conn
->smb1
.capabilities
;
550 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn
*conn
)
552 return conn
->smb1
.max_xmit
;
555 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn
*conn
)
557 return conn
->smb1
.server
.session_key
;
560 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn
*conn
)
562 return conn
->smb1
.server
.challenge
;
565 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
567 return conn
->smb1
.server
.security_mode
;
570 bool smb1cli_conn_server_readbraw(struct smbXcli_conn
*conn
)
572 return conn
->smb1
.server
.readbraw
;
575 bool smb1cli_conn_server_writebraw(struct smbXcli_conn
*conn
)
577 return conn
->smb1
.server
.writebraw
;
580 bool smb1cli_conn_server_lockread(struct smbXcli_conn
*conn
)
582 return conn
->smb1
.server
.lockread
;
585 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn
*conn
)
587 return conn
->smb1
.server
.writeunlock
;
590 int smb1cli_conn_server_time_zone(struct smbXcli_conn
*conn
)
592 return conn
->smb1
.server
.time_zone
;
595 bool smb1cli_conn_activate_signing(struct smbXcli_conn
*conn
,
596 const DATA_BLOB user_session_key
,
597 const DATA_BLOB response
)
599 return smb_signing_activate(conn
->smb1
.signing
,
604 bool smb1cli_conn_check_signing(struct smbXcli_conn
*conn
,
605 const uint8_t *buf
, uint32_t seqnum
)
607 return smb_signing_check_pdu(conn
->smb1
.signing
, buf
, seqnum
);
610 bool smb1cli_conn_signing_is_active(struct smbXcli_conn
*conn
)
612 return smb_signing_is_active(conn
->smb1
.signing
);
615 void smb1cli_conn_set_encryption(struct smbXcli_conn
*conn
,
616 struct smb_trans_enc_state
*es
)
618 /* Replace the old state, if any. */
619 if (conn
->smb1
.trans_enc
) {
620 TALLOC_FREE(conn
->smb1
.trans_enc
);
622 conn
->smb1
.trans_enc
= es
;
625 bool smb1cli_conn_encryption_on(struct smbXcli_conn
*conn
)
627 return common_encryption_on(conn
->smb1
.trans_enc
);
631 static NTSTATUS
smb1cli_pull_raw_error(const uint8_t *hdr
)
633 uint32_t flags2
= SVAL(hdr
, HDR_FLG2
);
634 NTSTATUS status
= NT_STATUS(IVAL(hdr
, HDR_RCLS
));
636 if (NT_STATUS_IS_OK(status
)) {
640 if (flags2
& FLAGS2_32_BIT_ERROR_CODES
) {
644 return NT_STATUS_DOS(CVAL(hdr
, HDR_RCLS
), SVAL(hdr
, HDR_ERR
));
648 * Is the SMB command able to hold an AND_X successor
649 * @param[in] cmd The SMB command in question
650 * @retval Can we add a chained request after "cmd"?
652 bool smb1cli_is_andx_req(uint8_t cmd
)
672 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn
*conn
)
674 size_t num_pending
= talloc_array_length(conn
->pending
);
680 result
= conn
->smb1
.mid
++;
681 if ((result
== 0) || (result
== 0xffff)) {
685 for (i
=0; i
<num_pending
; i
++) {
686 if (result
== smb1cli_req_mid(conn
->pending
[i
])) {
691 if (i
== num_pending
) {
697 void smbXcli_req_unset_pending(struct tevent_req
*req
)
699 struct smbXcli_req_state
*state
=
701 struct smbXcli_req_state
);
702 struct smbXcli_conn
*conn
= state
->conn
;
703 size_t num_pending
= talloc_array_length(conn
->pending
);
706 if (state
->smb1
.mid
!= 0) {
708 * This is a [nt]trans[2] request which waits
709 * for more than one reply.
714 talloc_set_destructor(req
, NULL
);
716 if (num_pending
== 1) {
718 * The pending read_smb tevent_req is a child of
719 * conn->pending. So if nothing is pending anymore, we need to
720 * delete the socket read fde.
722 TALLOC_FREE(conn
->pending
);
723 conn
->read_smb_req
= NULL
;
727 for (i
=0; i
<num_pending
; i
++) {
728 if (req
== conn
->pending
[i
]) {
732 if (i
== num_pending
) {
734 * Something's seriously broken. Just returning here is the
735 * right thing nevertheless, the point of this routine is to
736 * remove ourselves from conn->pending.
742 * Remove ourselves from the conn->pending array
744 for (; i
< (num_pending
- 1); i
++) {
745 conn
->pending
[i
] = conn
->pending
[i
+1];
749 * No NULL check here, we're shrinking by sizeof(void *), and
750 * talloc_realloc just adjusts the size for this.
752 conn
->pending
= talloc_realloc(NULL
, conn
->pending
, struct tevent_req
*,
757 static int smbXcli_req_destructor(struct tevent_req
*req
)
759 struct smbXcli_req_state
*state
=
761 struct smbXcli_req_state
);
764 * Make sure we really remove it from
765 * the pending array on destruction.
768 smbXcli_req_unset_pending(req
);
772 static bool smb1cli_req_cancel(struct tevent_req
*req
);
773 static bool smb2cli_req_cancel(struct tevent_req
*req
);
775 static bool smbXcli_req_cancel(struct tevent_req
*req
)
777 struct smbXcli_req_state
*state
=
779 struct smbXcli_req_state
);
781 if (!smbXcli_conn_is_connected(state
->conn
)) {
785 if (state
->conn
->protocol
== PROTOCOL_NONE
) {
789 if (state
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
790 return smb2cli_req_cancel(req
);
793 return smb1cli_req_cancel(req
);
796 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
);
798 bool smbXcli_req_set_pending(struct tevent_req
*req
)
800 struct smbXcli_req_state
*state
=
802 struct smbXcli_req_state
);
803 struct smbXcli_conn
*conn
;
804 struct tevent_req
**pending
;
809 if (!smbXcli_conn_is_connected(conn
)) {
813 num_pending
= talloc_array_length(conn
->pending
);
815 pending
= talloc_realloc(conn
, conn
->pending
, struct tevent_req
*,
817 if (pending
== NULL
) {
820 pending
[num_pending
] = req
;
821 conn
->pending
= pending
;
822 talloc_set_destructor(req
, smbXcli_req_destructor
);
823 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
825 if (!smbXcli_conn_receive_next(conn
)) {
827 * the caller should notify the current request
829 * And all other pending requests get notified
830 * by smbXcli_conn_disconnect().
832 smbXcli_req_unset_pending(req
);
833 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
840 static void smbXcli_conn_received(struct tevent_req
*subreq
);
842 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
)
844 size_t num_pending
= talloc_array_length(conn
->pending
);
845 struct tevent_req
*req
;
846 struct smbXcli_req_state
*state
;
848 if (conn
->read_smb_req
!= NULL
) {
852 if (num_pending
== 0) {
853 if (conn
->smb2
.mid
< UINT64_MAX
) {
854 /* no more pending requests, so we are done for now */
859 * If there are no more SMB2 requests possible,
860 * because we are out of message ids,
861 * we need to disconnect.
863 smbXcli_conn_disconnect(conn
, NT_STATUS_CONNECTION_ABORTED
);
867 req
= conn
->pending
[0];
868 state
= tevent_req_data(req
, struct smbXcli_req_state
);
871 * We're the first ones, add the read_smb request that waits for the
872 * answer from the server
874 conn
->read_smb_req
= read_smb_send(conn
->pending
,
877 if (conn
->read_smb_req
== NULL
) {
880 tevent_req_set_callback(conn
->read_smb_req
, smbXcli_conn_received
, conn
);
884 void smbXcli_conn_disconnect(struct smbXcli_conn
*conn
, NTSTATUS status
)
886 tevent_queue_stop(conn
->outgoing
);
888 if (conn
->read_fd
!= -1) {
889 close(conn
->read_fd
);
891 if (conn
->write_fd
!= -1) {
892 close(conn
->write_fd
);
898 * Cancel all pending requests. We do not do a for-loop walking
899 * conn->pending because that array changes in
900 * smbXcli_req_unset_pending.
902 while (talloc_array_length(conn
->pending
) > 0) {
903 struct tevent_req
*req
;
904 struct smbXcli_req_state
*state
;
905 struct tevent_req
**chain
;
909 req
= conn
->pending
[0];
910 state
= tevent_req_data(req
, struct smbXcli_req_state
);
912 if (state
->smb1
.chained_requests
== NULL
) {
914 * We're dead. No point waiting for trans2
919 smbXcli_req_unset_pending(req
);
921 if (NT_STATUS_IS_OK(status
)) {
922 /* do not notify the callers */
927 * we need to defer the callback, because we may notify
928 * more then one caller.
930 tevent_req_defer_callback(req
, state
->ev
);
931 tevent_req_nterror(req
, status
);
935 chain
= talloc_move(conn
, &state
->smb1
.chained_requests
);
936 num_chained
= talloc_array_length(chain
);
938 for (i
=0; i
<num_chained
; i
++) {
940 state
= tevent_req_data(req
, struct smbXcli_req_state
);
943 * We're dead. No point waiting for trans2
948 smbXcli_req_unset_pending(req
);
950 if (NT_STATUS_IS_OK(status
)) {
951 /* do not notify the callers */
956 * we need to defer the callback, because we may notify
957 * more than one caller.
959 tevent_req_defer_callback(req
, state
->ev
);
960 tevent_req_nterror(req
, status
);
967 * Fetch a smb request's mid. Only valid after the request has been sent by
968 * smb1cli_req_send().
970 uint16_t smb1cli_req_mid(struct tevent_req
*req
)
972 struct smbXcli_req_state
*state
=
974 struct smbXcli_req_state
);
976 if (state
->smb1
.mid
!= 0) {
977 return state
->smb1
.mid
;
980 return SVAL(state
->smb1
.hdr
, HDR_MID
);
983 void smb1cli_req_set_mid(struct tevent_req
*req
, uint16_t mid
)
985 struct smbXcli_req_state
*state
=
987 struct smbXcli_req_state
);
989 state
->smb1
.mid
= mid
;
992 uint32_t smb1cli_req_seqnum(struct tevent_req
*req
)
994 struct smbXcli_req_state
*state
=
996 struct smbXcli_req_state
);
998 return state
->smb1
.seqnum
;
1001 void smb1cli_req_set_seqnum(struct tevent_req
*req
, uint32_t seqnum
)
1003 struct smbXcli_req_state
*state
=
1004 tevent_req_data(req
,
1005 struct smbXcli_req_state
);
1007 state
->smb1
.seqnum
= seqnum
;
1010 static size_t smbXcli_iov_len(const struct iovec
*iov
, int count
)
1014 for (i
=0; i
<count
; i
++) {
1015 result
+= iov
[i
].iov_len
;
1020 static uint8_t *smbXcli_iov_concat(TALLOC_CTX
*mem_ctx
,
1021 const struct iovec
*iov
,
1024 size_t len
= smbXcli_iov_len(iov
, count
);
1029 buf
= talloc_array(mem_ctx
, uint8_t, len
);
1034 for (i
=0; i
<count
; i
++) {
1035 memcpy(buf
+copied
, iov
[i
].iov_base
, iov
[i
].iov_len
);
1036 copied
+= iov
[i
].iov_len
;
1041 static void smb1cli_req_flags(enum protocol_types protocol
,
1042 uint32_t smb1_capabilities
,
1043 uint8_t smb_command
,
1044 uint8_t additional_flags
,
1045 uint8_t clear_flags
,
1047 uint16_t additional_flags2
,
1048 uint16_t clear_flags2
,
1052 uint16_t flags2
= 0;
1054 if (protocol
>= PROTOCOL_LANMAN1
) {
1055 flags
|= FLAG_CASELESS_PATHNAMES
;
1056 flags
|= FLAG_CANONICAL_PATHNAMES
;
1059 if (protocol
>= PROTOCOL_LANMAN2
) {
1060 flags2
|= FLAGS2_LONG_PATH_COMPONENTS
;
1061 flags2
|= FLAGS2_EXTENDED_ATTRIBUTES
;
1064 if (protocol
>= PROTOCOL_NT1
) {
1065 flags2
|= FLAGS2_IS_LONG_NAME
;
1067 if (smb1_capabilities
& CAP_UNICODE
) {
1068 flags2
|= FLAGS2_UNICODE_STRINGS
;
1070 if (smb1_capabilities
& CAP_STATUS32
) {
1071 flags2
|= FLAGS2_32_BIT_ERROR_CODES
;
1073 if (smb1_capabilities
& CAP_EXTENDED_SECURITY
) {
1074 flags2
|= FLAGS2_EXTENDED_SECURITY
;
1078 flags
|= additional_flags
;
1079 flags
&= ~clear_flags
;
1080 flags2
|= additional_flags2
;
1081 flags2
&= ~clear_flags2
;
1087 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
);
1089 static bool smb1cli_req_cancel(struct tevent_req
*req
)
1091 struct smbXcli_req_state
*state
=
1092 tevent_req_data(req
,
1093 struct smbXcli_req_state
);
1099 struct tevent_req
*subreq
;
1102 flags
= CVAL(state
->smb1
.hdr
, HDR_FLG
);
1103 flags2
= SVAL(state
->smb1
.hdr
, HDR_FLG2
);
1104 pid
= SVAL(state
->smb1
.hdr
, HDR_PID
);
1105 pid
|= SVAL(state
->smb1
.hdr
, HDR_PIDHIGH
)<<16;
1106 tid
= SVAL(state
->smb1
.hdr
, HDR_TID
);
1107 mid
= SVAL(state
->smb1
.hdr
, HDR_MID
);
1109 subreq
= smb1cli_req_create(state
, state
->ev
,
1118 0, NULL
); /* bytes */
1119 if (subreq
== NULL
) {
1122 smb1cli_req_set_mid(subreq
, mid
);
1124 status
= smb1cli_req_chain_submit(&subreq
, 1);
1125 if (!NT_STATUS_IS_OK(status
)) {
1126 TALLOC_FREE(subreq
);
1129 smb1cli_req_set_mid(subreq
, 0);
1131 tevent_req_set_callback(subreq
, smb1cli_req_cancel_done
, NULL
);
1136 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
)
1138 /* we do not care about the result */
1139 TALLOC_FREE(subreq
);
1142 struct tevent_req
*smb1cli_req_create(TALLOC_CTX
*mem_ctx
,
1143 struct tevent_context
*ev
,
1144 struct smbXcli_conn
*conn
,
1145 uint8_t smb_command
,
1146 uint8_t additional_flags
,
1147 uint8_t clear_flags
,
1148 uint16_t additional_flags2
,
1149 uint16_t clear_flags2
,
1150 uint32_t timeout_msec
,
1153 struct smbXcli_session
*session
,
1154 uint8_t wct
, uint16_t *vwv
,
1156 struct iovec
*bytes_iov
)
1158 struct tevent_req
*req
;
1159 struct smbXcli_req_state
*state
;
1161 uint16_t flags2
= 0;
1164 if (iov_count
> MAX_SMB_IOV
) {
1166 * Should not happen :-)
1171 req
= tevent_req_create(mem_ctx
, &state
,
1172 struct smbXcli_req_state
);
1178 state
->session
= session
;
1181 uid
= session
->smb1
.session_id
;
1184 state
->smb1
.recv_cmd
= 0xFF;
1185 state
->smb1
.recv_status
= NT_STATUS_INTERNAL_ERROR
;
1186 state
->smb1
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
1187 if (state
->smb1
.recv_iov
== NULL
) {
1192 smb1cli_req_flags(conn
->protocol
,
1193 conn
->smb1
.capabilities
,
1202 SIVAL(state
->smb1
.hdr
, 0, SMB_MAGIC
);
1203 SCVAL(state
->smb1
.hdr
, HDR_COM
, smb_command
);
1204 SIVAL(state
->smb1
.hdr
, HDR_RCLS
, NT_STATUS_V(NT_STATUS_OK
));
1205 SCVAL(state
->smb1
.hdr
, HDR_FLG
, flags
);
1206 SSVAL(state
->smb1
.hdr
, HDR_FLG2
, flags2
);
1207 SSVAL(state
->smb1
.hdr
, HDR_PIDHIGH
, pid
>> 16);
1208 SSVAL(state
->smb1
.hdr
, HDR_TID
, tid
);
1209 SSVAL(state
->smb1
.hdr
, HDR_PID
, pid
);
1210 SSVAL(state
->smb1
.hdr
, HDR_UID
, uid
);
1211 SSVAL(state
->smb1
.hdr
, HDR_MID
, 0); /* this comes later */
1212 SCVAL(state
->smb1
.hdr
, HDR_WCT
, wct
);
1214 state
->smb1
.vwv
= vwv
;
1216 SSVAL(state
->smb1
.bytecount_buf
, 0, smbXcli_iov_len(bytes_iov
, iov_count
));
1218 state
->smb1
.iov
[0].iov_base
= (void *)state
->length_hdr
;
1219 state
->smb1
.iov
[0].iov_len
= sizeof(state
->length_hdr
);
1220 state
->smb1
.iov
[1].iov_base
= (void *)state
->smb1
.hdr
;
1221 state
->smb1
.iov
[1].iov_len
= sizeof(state
->smb1
.hdr
);
1222 state
->smb1
.iov
[2].iov_base
= (void *)state
->smb1
.vwv
;
1223 state
->smb1
.iov
[2].iov_len
= wct
* sizeof(uint16_t);
1224 state
->smb1
.iov
[3].iov_base
= (void *)state
->smb1
.bytecount_buf
;
1225 state
->smb1
.iov
[3].iov_len
= sizeof(uint16_t);
1227 if (iov_count
!= 0) {
1228 memcpy(&state
->smb1
.iov
[4], bytes_iov
,
1229 iov_count
* sizeof(*bytes_iov
));
1231 state
->smb1
.iov_count
= iov_count
+ 4;
1233 if (timeout_msec
> 0) {
1234 struct timeval endtime
;
1236 endtime
= timeval_current_ofs_msec(timeout_msec
);
1237 if (!tevent_req_set_endtime(req
, ev
, endtime
)) {
1242 switch (smb_command
) {
1246 state
->one_way
= true;
1249 state
->one_way
= true;
1250 state
->smb1
.one_way_seqnum
= true;
1254 (CVAL(vwv
+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE
)) {
1255 state
->one_way
= true;
1263 static NTSTATUS
smb1cli_conn_signv(struct smbXcli_conn
*conn
,
1264 struct iovec
*iov
, int iov_count
,
1266 bool one_way_seqnum
)
1268 TALLOC_CTX
*frame
= NULL
;
1272 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1273 * iovec directly. MD5Update would do that just fine.
1276 if (iov_count
< 4) {
1277 return NT_STATUS_INVALID_PARAMETER_MIX
;
1279 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1280 return NT_STATUS_INVALID_PARAMETER_MIX
;
1282 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1283 return NT_STATUS_INVALID_PARAMETER_MIX
;
1285 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1286 return NT_STATUS_INVALID_PARAMETER_MIX
;
1288 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1289 return NT_STATUS_INVALID_PARAMETER_MIX
;
1292 frame
= talloc_stackframe();
1294 buf
= smbXcli_iov_concat(frame
, iov
, iov_count
);
1296 return NT_STATUS_NO_MEMORY
;
1299 *seqnum
= smb_signing_next_seqnum(conn
->smb1
.signing
,
1301 smb_signing_sign_pdu(conn
->smb1
.signing
, buf
, *seqnum
);
1302 memcpy(iov
[1].iov_base
, buf
+4, iov
[1].iov_len
);
1305 return NT_STATUS_OK
;
1308 static void smb1cli_req_writev_done(struct tevent_req
*subreq
);
1309 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1310 TALLOC_CTX
*tmp_mem
,
1313 static NTSTATUS
smb1cli_req_writev_submit(struct tevent_req
*req
,
1314 struct smbXcli_req_state
*state
,
1315 struct iovec
*iov
, int iov_count
)
1317 struct tevent_req
*subreq
;
1322 if (!smbXcli_conn_is_connected(state
->conn
)) {
1323 return NT_STATUS_CONNECTION_DISCONNECTED
;
1326 if (state
->conn
->protocol
> PROTOCOL_NT1
) {
1327 return NT_STATUS_REVISION_MISMATCH
;
1330 if (iov_count
< 4) {
1331 return NT_STATUS_INVALID_PARAMETER_MIX
;
1333 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1334 return NT_STATUS_INVALID_PARAMETER_MIX
;
1336 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1337 return NT_STATUS_INVALID_PARAMETER_MIX
;
1339 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1340 return NT_STATUS_INVALID_PARAMETER_MIX
;
1342 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1343 return NT_STATUS_INVALID_PARAMETER_MIX
;
1346 cmd
= CVAL(iov
[1].iov_base
, HDR_COM
);
1347 if (cmd
== SMBreadBraw
) {
1348 if (smbXcli_conn_has_async_calls(state
->conn
)) {
1349 return NT_STATUS_INVALID_PARAMETER_MIX
;
1351 state
->conn
->smb1
.read_braw_req
= req
;
1354 if (state
->smb1
.mid
!= 0) {
1355 mid
= state
->smb1
.mid
;
1357 mid
= smb1cli_alloc_mid(state
->conn
);
1359 SSVAL(iov
[1].iov_base
, HDR_MID
, mid
);
1361 _smb_setlen_nbt(iov
[0].iov_base
, smbXcli_iov_len(&iov
[1], iov_count
-1));
1363 status
= smb1cli_conn_signv(state
->conn
, iov
, iov_count
,
1364 &state
->smb1
.seqnum
,
1365 state
->smb1
.one_way_seqnum
);
1367 if (!NT_STATUS_IS_OK(status
)) {
1372 * If we supported multiple encrytion contexts
1373 * here we'd look up based on tid.
1375 if (common_encryption_on(state
->conn
->smb1
.trans_enc
)) {
1376 char *buf
, *enc_buf
;
1378 buf
= (char *)smbXcli_iov_concat(talloc_tos(), iov
, iov_count
);
1380 return NT_STATUS_NO_MEMORY
;
1382 status
= common_encrypt_buffer(state
->conn
->smb1
.trans_enc
,
1383 (char *)buf
, &enc_buf
);
1385 if (!NT_STATUS_IS_OK(status
)) {
1386 DEBUG(0, ("Error in encrypting client message: %s\n",
1387 nt_errstr(status
)));
1390 buf
= (char *)talloc_memdup(state
, enc_buf
,
1391 smb_len_nbt(enc_buf
)+4);
1394 return NT_STATUS_NO_MEMORY
;
1396 iov
[0].iov_base
= (void *)buf
;
1397 iov
[0].iov_len
= talloc_get_size(buf
);
1401 if (state
->conn
->dispatch_incoming
== NULL
) {
1402 state
->conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
1405 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
1407 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
1408 state
->conn
->write_fd
, false, iov
, iov_count
);
1409 if (subreq
== NULL
) {
1410 return NT_STATUS_NO_MEMORY
;
1412 tevent_req_set_callback(subreq
, smb1cli_req_writev_done
, req
);
1413 return NT_STATUS_OK
;
1416 struct tevent_req
*smb1cli_req_send(TALLOC_CTX
*mem_ctx
,
1417 struct tevent_context
*ev
,
1418 struct smbXcli_conn
*conn
,
1419 uint8_t smb_command
,
1420 uint8_t additional_flags
,
1421 uint8_t clear_flags
,
1422 uint16_t additional_flags2
,
1423 uint16_t clear_flags2
,
1424 uint32_t timeout_msec
,
1427 struct smbXcli_session
*session
,
1428 uint8_t wct
, uint16_t *vwv
,
1430 const uint8_t *bytes
)
1432 struct tevent_req
*req
;
1436 iov
.iov_base
= discard_const_p(void, bytes
);
1437 iov
.iov_len
= num_bytes
;
1439 req
= smb1cli_req_create(mem_ctx
, ev
, conn
, smb_command
,
1440 additional_flags
, clear_flags
,
1441 additional_flags2
, clear_flags2
,
1448 if (!tevent_req_is_in_progress(req
)) {
1449 return tevent_req_post(req
, ev
);
1451 status
= smb1cli_req_chain_submit(&req
, 1);
1452 if (tevent_req_nterror(req
, status
)) {
1453 return tevent_req_post(req
, ev
);
1458 static void smb1cli_req_writev_done(struct tevent_req
*subreq
)
1460 struct tevent_req
*req
=
1461 tevent_req_callback_data(subreq
,
1463 struct smbXcli_req_state
*state
=
1464 tevent_req_data(req
,
1465 struct smbXcli_req_state
);
1469 nwritten
= writev_recv(subreq
, &err
);
1470 TALLOC_FREE(subreq
);
1471 if (nwritten
== -1) {
1472 NTSTATUS status
= map_nt_error_from_unix_common(err
);
1473 smbXcli_conn_disconnect(state
->conn
, status
);
1477 if (state
->one_way
) {
1478 state
->inbuf
= NULL
;
1479 tevent_req_done(req
);
1483 if (!smbXcli_req_set_pending(req
)) {
1484 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1489 static void smbXcli_conn_received(struct tevent_req
*subreq
)
1491 struct smbXcli_conn
*conn
=
1492 tevent_req_callback_data(subreq
,
1493 struct smbXcli_conn
);
1494 TALLOC_CTX
*frame
= talloc_stackframe();
1500 if (subreq
!= conn
->read_smb_req
) {
1501 DEBUG(1, ("Internal error: cli_smb_received called with "
1502 "unexpected subreq\n"));
1503 status
= NT_STATUS_INTERNAL_ERROR
;
1504 smbXcli_conn_disconnect(conn
, status
);
1508 conn
->read_smb_req
= NULL
;
1510 received
= read_smb_recv(subreq
, frame
, &inbuf
, &err
);
1511 TALLOC_FREE(subreq
);
1512 if (received
== -1) {
1513 status
= map_nt_error_from_unix_common(err
);
1514 smbXcli_conn_disconnect(conn
, status
);
1519 status
= conn
->dispatch_incoming(conn
, frame
, inbuf
);
1521 if (NT_STATUS_IS_OK(status
)) {
1523 * We should not do any more processing
1524 * as the dispatch function called
1525 * tevent_req_done().
1528 } else if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1530 * We got an error, so notify all pending requests
1532 smbXcli_conn_disconnect(conn
, status
);
1537 * We got NT_STATUS_RETRY, so we may ask for a
1538 * next incoming pdu.
1540 if (!smbXcli_conn_receive_next(conn
)) {
1541 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
1545 static NTSTATUS
smb1cli_inbuf_parse_chain(uint8_t *buf
, TALLOC_CTX
*mem_ctx
,
1546 struct iovec
**piov
, int *pnum_iov
)
1557 buflen
= smb_len_nbt(buf
);
1560 hdr
= buf
+ NBT_HDR_SIZE
;
1562 if (buflen
< MIN_SMB_SIZE
) {
1563 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1567 * This returns iovec elements in the following order:
1582 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
1584 return NT_STATUS_NO_MEMORY
;
1586 iov
[0].iov_base
= hdr
;
1587 iov
[0].iov_len
= HDR_WCT
;
1590 cmd
= CVAL(hdr
, HDR_COM
);
1594 size_t len
= buflen
- taken
;
1596 struct iovec
*iov_tmp
;
1603 * we need at least WCT and BCC
1605 needed
= sizeof(uint8_t) + sizeof(uint16_t);
1607 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1608 __location__
, (int)len
, (int)needed
));
1613 * Now we check if the specified words are there
1615 wct
= CVAL(hdr
, wct_ofs
);
1616 needed
+= wct
* sizeof(uint16_t);
1618 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1619 __location__
, (int)len
, (int)needed
));
1624 * Now we check if the specified bytes are there
1626 bcc_ofs
= wct_ofs
+ sizeof(uint8_t) + wct
* sizeof(uint16_t);
1627 bcc
= SVAL(hdr
, bcc_ofs
);
1628 needed
+= bcc
* sizeof(uint8_t);
1630 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1631 __location__
, (int)len
, (int)needed
));
1636 * we allocate 2 iovec structures for words and bytes
1638 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
1640 if (iov_tmp
== NULL
) {
1642 return NT_STATUS_NO_MEMORY
;
1645 cur
= &iov
[num_iov
];
1648 cur
[0].iov_len
= wct
* sizeof(uint16_t);
1649 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
1650 cur
[1].iov_len
= bcc
* sizeof(uint8_t);
1651 cur
[1].iov_base
= hdr
+ (bcc_ofs
+ sizeof(uint16_t));
1655 if (!smb1cli_is_andx_req(cmd
)) {
1657 * If the current command does not have AndX chanining
1663 if (wct
== 0 && bcc
== 0) {
1665 * An empty response also ends the chain,
1666 * most likely with an error.
1672 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1673 __location__
, (int)wct
, (int)cmd
));
1676 cmd
= CVAL(cur
[0].iov_base
, 0);
1679 * If it is the end of the chain we are also done.
1683 wct_ofs
= SVAL(cur
[0].iov_base
, 2);
1685 if (wct_ofs
< taken
) {
1686 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1688 if (wct_ofs
> buflen
) {
1689 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1693 * we consumed everything up to the start of the next
1699 remaining
= buflen
- taken
;
1701 if (remaining
> 0 && num_iov
>= 3) {
1703 * The last DATA block gets the remaining
1704 * bytes, this is needed to support
1705 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1707 iov
[num_iov
-1].iov_len
+= remaining
;
1711 *pnum_iov
= num_iov
;
1712 return NT_STATUS_OK
;
1716 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1719 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1720 TALLOC_CTX
*tmp_mem
,
1723 struct tevent_req
*req
;
1724 struct smbXcli_req_state
*state
;
1731 const uint8_t *inhdr
= inbuf
+ NBT_HDR_SIZE
;
1732 struct iovec
*iov
= NULL
;
1734 struct tevent_req
**chain
= NULL
;
1735 size_t num_chained
= 0;
1736 size_t num_responses
= 0;
1738 if (conn
->smb1
.read_braw_req
!= NULL
) {
1739 req
= conn
->smb1
.read_braw_req
;
1740 conn
->smb1
.read_braw_req
= NULL
;
1741 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1743 smbXcli_req_unset_pending(req
);
1745 if (state
->smb1
.recv_iov
== NULL
) {
1747 * For requests with more than
1748 * one response, we have to readd the
1751 state
->smb1
.recv_iov
= talloc_zero_array(state
,
1754 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
1755 return NT_STATUS_OK
;
1759 state
->smb1
.recv_iov
[0].iov_base
= (void *)(inbuf
+ NBT_HDR_SIZE
);
1760 state
->smb1
.recv_iov
[0].iov_len
= smb_len_nbt(inbuf
);
1761 ZERO_STRUCT(state
->smb1
.recv_iov
[1]);
1762 ZERO_STRUCT(state
->smb1
.recv_iov
[2]);
1764 state
->smb1
.recv_cmd
= SMBreadBraw
;
1765 state
->smb1
.recv_status
= NT_STATUS_OK
;
1766 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
1768 tevent_req_done(req
);
1769 return NT_STATUS_OK
;
1772 if ((IVAL(inhdr
, 0) != SMB_MAGIC
) /* 0xFF"SMB" */
1773 && (SVAL(inhdr
, 0) != 0x45ff)) /* 0xFF"E" */ {
1774 DEBUG(10, ("Got non-SMB PDU\n"));
1775 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1779 * If we supported multiple encrytion contexts
1780 * here we'd look up based on tid.
1782 if (common_encryption_on(conn
->smb1
.trans_enc
)
1783 && (CVAL(inbuf
, 0) == 0)) {
1784 uint16_t enc_ctx_num
;
1786 status
= get_enc_ctx_num(inbuf
, &enc_ctx_num
);
1787 if (!NT_STATUS_IS_OK(status
)) {
1788 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1789 nt_errstr(status
)));
1793 if (enc_ctx_num
!= conn
->smb1
.trans_enc
->enc_ctx_num
) {
1794 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1796 conn
->smb1
.trans_enc
->enc_ctx_num
));
1797 return NT_STATUS_INVALID_HANDLE
;
1800 status
= common_decrypt_buffer(conn
->smb1
.trans_enc
,
1802 if (!NT_STATUS_IS_OK(status
)) {
1803 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1804 nt_errstr(status
)));
1809 mid
= SVAL(inhdr
, HDR_MID
);
1810 num_pending
= talloc_array_length(conn
->pending
);
1812 for (i
=0; i
<num_pending
; i
++) {
1813 if (mid
== smb1cli_req_mid(conn
->pending
[i
])) {
1817 if (i
== num_pending
) {
1818 /* Dump unexpected reply */
1819 return NT_STATUS_RETRY
;
1822 oplock_break
= false;
1824 if (mid
== 0xffff) {
1826 * Paranoia checks that this is really an oplock break request.
1828 oplock_break
= (smb_len_nbt(inbuf
) == 51); /* hdr + 8 words */
1829 oplock_break
&= ((CVAL(inhdr
, HDR_FLG
) & FLAG_REPLY
) == 0);
1830 oplock_break
&= (CVAL(inhdr
, HDR_COM
) == SMBlockingX
);
1831 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(6)) == 0);
1832 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(7)) == 0);
1834 if (!oplock_break
) {
1835 /* Dump unexpected reply */
1836 return NT_STATUS_RETRY
;
1840 req
= conn
->pending
[i
];
1841 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1843 if (!oplock_break
/* oplock breaks are not signed */
1844 && !smb_signing_check_pdu(conn
->smb1
.signing
,
1845 inbuf
, state
->smb1
.seqnum
+1)) {
1846 DEBUG(10, ("cli_check_sign_mac failed\n"));
1847 return NT_STATUS_ACCESS_DENIED
;
1850 status
= smb1cli_inbuf_parse_chain(inbuf
, tmp_mem
,
1852 if (!NT_STATUS_IS_OK(status
)) {
1853 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1854 nt_errstr(status
)));
1858 cmd
= CVAL(inhdr
, HDR_COM
);
1859 status
= smb1cli_pull_raw_error(inhdr
);
1861 if (state
->smb1
.chained_requests
== NULL
) {
1863 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1866 smbXcli_req_unset_pending(req
);
1868 if (state
->smb1
.recv_iov
== NULL
) {
1870 * For requests with more than
1871 * one response, we have to readd the
1874 state
->smb1
.recv_iov
= talloc_zero_array(state
,
1877 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
1878 return NT_STATUS_OK
;
1882 state
->smb1
.recv_cmd
= cmd
;
1883 state
->smb1
.recv_status
= status
;
1884 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
1886 state
->smb1
.recv_iov
[0] = iov
[0];
1887 state
->smb1
.recv_iov
[1] = iov
[1];
1888 state
->smb1
.recv_iov
[2] = iov
[2];
1890 if (talloc_array_length(conn
->pending
) == 0) {
1891 tevent_req_done(req
);
1892 return NT_STATUS_OK
;
1895 tevent_req_defer_callback(req
, state
->ev
);
1896 tevent_req_done(req
);
1897 return NT_STATUS_RETRY
;
1900 chain
= talloc_move(tmp_mem
, &state
->smb1
.chained_requests
);
1901 num_chained
= talloc_array_length(chain
);
1902 num_responses
= (num_iov
- 1)/2;
1904 if (num_responses
> num_chained
) {
1905 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1908 for (i
=0; i
<num_chained
; i
++) {
1909 size_t iov_idx
= 1 + (i
*2);
1910 struct iovec
*cur
= &iov
[iov_idx
];
1914 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1916 smbXcli_req_unset_pending(req
);
1919 * as we finish multiple requests here
1920 * we need to defer the callbacks as
1921 * they could destroy our current stack state.
1923 tevent_req_defer_callback(req
, state
->ev
);
1925 if (i
>= num_responses
) {
1926 tevent_req_nterror(req
, NT_STATUS_REQUEST_ABORTED
);
1930 if (state
->smb1
.recv_iov
== NULL
) {
1932 * For requests with more than
1933 * one response, we have to readd the
1936 state
->smb1
.recv_iov
= talloc_zero_array(state
,
1939 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
1944 state
->smb1
.recv_cmd
= cmd
;
1946 if (i
== (num_responses
- 1)) {
1948 * The last request in the chain gets the status
1950 state
->smb1
.recv_status
= status
;
1952 cmd
= CVAL(cur
[0].iov_base
, 0);
1953 state
->smb1
.recv_status
= NT_STATUS_OK
;
1956 state
->inbuf
= inbuf
;
1959 * Note: here we use talloc_reference() in a way
1960 * that does not expose it to the caller.
1962 inbuf_ref
= talloc_reference(state
->smb1
.recv_iov
, inbuf
);
1963 if (tevent_req_nomem(inbuf_ref
, req
)) {
1967 /* copy the related buffers */
1968 state
->smb1
.recv_iov
[0] = iov
[0];
1969 state
->smb1
.recv_iov
[1] = cur
[0];
1970 state
->smb1
.recv_iov
[2] = cur
[1];
1972 tevent_req_done(req
);
1975 return NT_STATUS_RETRY
;
1978 NTSTATUS
smb1cli_req_recv(struct tevent_req
*req
,
1979 TALLOC_CTX
*mem_ctx
,
1980 struct iovec
**piov
,
1984 uint32_t *pvwv_offset
,
1985 uint32_t *pnum_bytes
,
1987 uint32_t *pbytes_offset
,
1989 const struct smb1cli_req_expected_response
*expected
,
1990 size_t num_expected
)
1992 struct smbXcli_req_state
*state
=
1993 tevent_req_data(req
,
1994 struct smbXcli_req_state
);
1995 NTSTATUS status
= NT_STATUS_OK
;
1996 struct iovec
*recv_iov
= NULL
;
1997 uint8_t *hdr
= NULL
;
1999 uint32_t vwv_offset
= 0;
2000 uint16_t *vwv
= NULL
;
2001 uint32_t num_bytes
= 0;
2002 uint32_t bytes_offset
= 0;
2003 uint8_t *bytes
= NULL
;
2005 bool found_status
= false;
2006 bool found_size
= false;
2020 if (pvwv_offset
!= NULL
) {
2023 if (pnum_bytes
!= NULL
) {
2026 if (pbytes
!= NULL
) {
2029 if (pbytes_offset
!= NULL
) {
2032 if (pinbuf
!= NULL
) {
2036 if (state
->inbuf
!= NULL
) {
2037 recv_iov
= state
->smb1
.recv_iov
;
2038 state
->smb1
.recv_iov
= NULL
;
2039 if (state
->smb1
.recv_cmd
!= SMBreadBraw
) {
2040 hdr
= (uint8_t *)recv_iov
[0].iov_base
;
2041 wct
= recv_iov
[1].iov_len
/2;
2042 vwv
= (uint16_t *)recv_iov
[1].iov_base
;
2043 vwv_offset
= PTR_DIFF(vwv
, hdr
);
2044 num_bytes
= recv_iov
[2].iov_len
;
2045 bytes
= (uint8_t *)recv_iov
[2].iov_base
;
2046 bytes_offset
= PTR_DIFF(bytes
, hdr
);
2050 if (tevent_req_is_nterror(req
, &status
)) {
2051 for (i
=0; i
< num_expected
; i
++) {
2052 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2053 found_status
= true;
2059 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
2065 if (num_expected
== 0) {
2066 found_status
= true;
2070 status
= state
->smb1
.recv_status
;
2072 for (i
=0; i
< num_expected
; i
++) {
2073 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2077 found_status
= true;
2078 if (expected
[i
].wct
== 0) {
2083 if (expected
[i
].wct
== wct
) {
2089 if (!found_status
) {
2094 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2098 *piov
= talloc_move(mem_ctx
, &recv_iov
);
2110 if (pvwv_offset
!= NULL
) {
2111 *pvwv_offset
= vwv_offset
;
2113 if (pnum_bytes
!= NULL
) {
2114 *pnum_bytes
= num_bytes
;
2116 if (pbytes
!= NULL
) {
2119 if (pbytes_offset
!= NULL
) {
2120 *pbytes_offset
= bytes_offset
;
2122 if (pinbuf
!= NULL
) {
2123 *pinbuf
= state
->inbuf
;
2129 size_t smb1cli_req_wct_ofs(struct tevent_req
**reqs
, int num_reqs
)
2136 for (i
=0; i
<num_reqs
; i
++) {
2137 struct smbXcli_req_state
*state
;
2138 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2139 wct_ofs
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2140 state
->smb1
.iov_count
-2);
2141 wct_ofs
= (wct_ofs
+ 3) & ~3;
2146 NTSTATUS
smb1cli_req_chain_submit(struct tevent_req
**reqs
, int num_reqs
)
2148 struct smbXcli_req_state
*first_state
=
2149 tevent_req_data(reqs
[0],
2150 struct smbXcli_req_state
);
2151 struct smbXcli_req_state
*state
;
2153 size_t chain_padding
= 0;
2155 struct iovec
*iov
= NULL
;
2156 struct iovec
*this_iov
;
2160 if (num_reqs
== 1) {
2161 return smb1cli_req_writev_submit(reqs
[0], first_state
,
2162 first_state
->smb1
.iov
,
2163 first_state
->smb1
.iov_count
);
2167 for (i
=0; i
<num_reqs
; i
++) {
2168 if (!tevent_req_is_in_progress(reqs
[i
])) {
2169 return NT_STATUS_INTERNAL_ERROR
;
2172 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2174 if (state
->smb1
.iov_count
< 4) {
2175 return NT_STATUS_INVALID_PARAMETER_MIX
;
2180 * The NBT and SMB header
2193 iovlen
+= state
->smb1
.iov_count
- 2;
2196 iov
= talloc_zero_array(first_state
, struct iovec
, iovlen
);
2198 return NT_STATUS_NO_MEMORY
;
2201 first_state
->smb1
.chained_requests
= (struct tevent_req
**)talloc_memdup(
2202 first_state
, reqs
, sizeof(*reqs
) * num_reqs
);
2203 if (first_state
->smb1
.chained_requests
== NULL
) {
2205 return NT_STATUS_NO_MEMORY
;
2208 wct_offset
= HDR_WCT
;
2211 for (i
=0; i
<num_reqs
; i
++) {
2212 size_t next_padding
= 0;
2215 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2217 if (i
< num_reqs
-1) {
2218 if (!smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))
2219 || CVAL(state
->smb1
.hdr
, HDR_WCT
) < 2) {
2221 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2222 return NT_STATUS_INVALID_PARAMETER_MIX
;
2226 wct_offset
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2227 state
->smb1
.iov_count
-2) + 1;
2228 if ((wct_offset
% 4) != 0) {
2229 next_padding
= 4 - (wct_offset
% 4);
2231 wct_offset
+= next_padding
;
2232 vwv
= state
->smb1
.vwv
;
2234 if (i
< num_reqs
-1) {
2235 struct smbXcli_req_state
*next_state
=
2236 tevent_req_data(reqs
[i
+1],
2237 struct smbXcli_req_state
);
2238 SCVAL(vwv
+0, 0, CVAL(next_state
->smb1
.hdr
, HDR_COM
));
2240 SSVAL(vwv
+1, 0, wct_offset
);
2241 } else if (smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))) {
2242 /* properly end the chain */
2243 SCVAL(vwv
+0, 0, 0xff);
2244 SCVAL(vwv
+0, 1, 0xff);
2250 * The NBT and SMB header
2252 this_iov
[0] = state
->smb1
.iov
[0];
2253 this_iov
[1] = state
->smb1
.iov
[1];
2257 * This one is a bit subtle. We have to add
2258 * chain_padding bytes between the requests, and we
2259 * have to also include the wct field of the
2260 * subsequent requests. We use the subsequent header
2261 * for the padding, it contains the wct field in its
2264 this_iov
[0].iov_len
= chain_padding
+1;
2265 this_iov
[0].iov_base
= (void *)&state
->smb1
.hdr
[
2266 sizeof(state
->smb1
.hdr
) - this_iov
[0].iov_len
];
2267 memset(this_iov
[0].iov_base
, 0, this_iov
[0].iov_len
-1);
2272 * copy the words and bytes
2274 memcpy(this_iov
, state
->smb1
.iov
+2,
2275 sizeof(struct iovec
) * (state
->smb1
.iov_count
-2));
2276 this_iov
+= state
->smb1
.iov_count
- 2;
2277 chain_padding
= next_padding
;
2280 nbt_len
= smbXcli_iov_len(&iov
[1], iovlen
-1);
2281 if (nbt_len
> first_state
->conn
->smb1
.max_xmit
) {
2283 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2284 return NT_STATUS_INVALID_PARAMETER_MIX
;
2287 status
= smb1cli_req_writev_submit(reqs
[0], first_state
, iov
, iovlen
);
2288 if (!NT_STATUS_IS_OK(status
)) {
2290 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2294 return NT_STATUS_OK
;
2297 bool smbXcli_conn_has_async_calls(struct smbXcli_conn
*conn
)
2299 return ((tevent_queue_length(conn
->outgoing
) != 0)
2300 || (talloc_array_length(conn
->pending
) != 0));
2303 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn
*conn
)
2305 return conn
->smb2
.server
.capabilities
;
2308 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
2310 return conn
->smb2
.server
.security_mode
;
2313 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn
*conn
)
2315 return conn
->smb2
.server
.max_trans_size
;
2318 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn
*conn
)
2320 return conn
->smb2
.server
.max_read_size
;
2323 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn
*conn
)
2325 return conn
->smb2
.server
.max_write_size
;
2328 void smb2cli_conn_set_max_credits(struct smbXcli_conn
*conn
,
2329 uint16_t max_credits
)
2331 conn
->smb2
.max_credits
= max_credits
;
2334 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
);
2336 static bool smb2cli_req_cancel(struct tevent_req
*req
)
2338 struct smbXcli_req_state
*state
=
2339 tevent_req_data(req
,
2340 struct smbXcli_req_state
);
2341 uint32_t flags
= IVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
2342 uint32_t pid
= IVAL(state
->smb2
.hdr
, SMB2_HDR_PID
);
2343 uint32_t tid
= IVAL(state
->smb2
.hdr
, SMB2_HDR_TID
);
2344 uint64_t mid
= BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
);
2345 uint64_t aid
= BVAL(state
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
);
2346 struct smbXcli_session
*session
= state
->session
;
2347 uint8_t *fixed
= state
->smb2
.pad
;
2348 uint16_t fixed_len
= 4;
2349 struct tevent_req
*subreq
;
2350 struct smbXcli_req_state
*substate
;
2353 SSVAL(fixed
, 0, 0x04);
2356 subreq
= smb2cli_req_create(state
, state
->ev
,
2364 if (subreq
== NULL
) {
2367 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
2369 if (flags
& SMB2_HDR_FLAG_ASYNC
) {
2373 SIVAL(substate
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
2374 SIVAL(substate
->smb2
.hdr
, SMB2_HDR_PID
, pid
);
2375 SIVAL(substate
->smb2
.hdr
, SMB2_HDR_TID
, tid
);
2376 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
2377 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, aid
);
2379 status
= smb2cli_req_compound_submit(&subreq
, 1);
2380 if (!NT_STATUS_IS_OK(status
)) {
2381 TALLOC_FREE(subreq
);
2385 tevent_req_set_callback(subreq
, smb2cli_req_cancel_done
, NULL
);
2390 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
)
2392 /* we do not care about the result */
2393 TALLOC_FREE(subreq
);
2396 struct tevent_req
*smb2cli_req_create(TALLOC_CTX
*mem_ctx
,
2397 struct tevent_context
*ev
,
2398 struct smbXcli_conn
*conn
,
2400 uint32_t additional_flags
,
2401 uint32_t clear_flags
,
2402 uint32_t timeout_msec
,
2405 struct smbXcli_session
*session
,
2406 const uint8_t *fixed
,
2411 struct tevent_req
*req
;
2412 struct smbXcli_req_state
*state
;
2416 req
= tevent_req_create(mem_ctx
, &state
,
2417 struct smbXcli_req_state
);
2424 state
->session
= session
;
2427 uid
= session
->smb2
.session_id
;
2429 state
->smb2
.should_sign
= session
->smb2
.should_sign
;
2430 state
->smb2
.should_encrypt
= session
->smb2
.should_encrypt
;
2432 /* TODO: turn on encryption based on the tree connect. */
2434 if (cmd
== SMB2_OP_SESSSETUP
&&
2435 session
->smb2
.signing_key
.length
!= 0) {
2436 state
->smb2
.should_sign
= true;
2439 if (cmd
== SMB2_OP_SESSSETUP
&&
2440 session
->smb2
.channel_signing_key
.length
== 0) {
2441 state
->smb2
.should_encrypt
= false;
2444 if (state
->smb2
.should_encrypt
) {
2445 state
->smb2
.should_sign
= false;
2449 state
->smb2
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
2450 if (state
->smb2
.recv_iov
== NULL
) {
2455 flags
|= additional_flags
;
2456 flags
&= ~clear_flags
;
2458 state
->smb2
.fixed
= fixed
;
2459 state
->smb2
.fixed_len
= fixed_len
;
2460 state
->smb2
.dyn
= dyn
;
2461 state
->smb2
.dyn_len
= dyn_len
;
2463 if (state
->smb2
.should_encrypt
) {
2464 SIVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
2465 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
, uid
);
2468 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PROTOCOL_ID
, SMB2_MAGIC
);
2469 SSVAL(state
->smb2
.hdr
, SMB2_HDR_LENGTH
, SMB2_HDR_BODY
);
2470 SSVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
, cmd
);
2471 SIVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
2472 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PID
, pid
);
2473 SIVAL(state
->smb2
.hdr
, SMB2_HDR_TID
, tid
);
2474 SBVAL(state
->smb2
.hdr
, SMB2_HDR_SESSION_ID
, uid
);
2477 case SMB2_OP_CANCEL
:
2478 state
->one_way
= true;
2482 * If this is a dummy request, it will have
2483 * UINT64_MAX as message id.
2484 * If we send on break acknowledgement,
2485 * this gets overwritten later.
2487 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, UINT64_MAX
);
2491 if (timeout_msec
> 0) {
2492 struct timeval endtime
;
2494 endtime
= timeval_current_ofs_msec(timeout_msec
);
2495 if (!tevent_req_set_endtime(req
, ev
, endtime
)) {
2503 void smb2cli_req_set_notify_async(struct tevent_req
*req
)
2505 struct smbXcli_req_state
*state
=
2506 tevent_req_data(req
,
2507 struct smbXcli_req_state
);
2509 state
->smb2
.notify_async
= true;
2512 static void smb2cli_req_writev_done(struct tevent_req
*subreq
);
2513 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
2514 TALLOC_CTX
*tmp_mem
,
2517 NTSTATUS
smb2cli_req_compound_submit(struct tevent_req
**reqs
,
2520 struct smbXcli_req_state
*state
;
2521 struct tevent_req
*subreq
;
2523 int i
, num_iov
, nbt_len
;
2526 * 1 for the nbt length
2527 * per request: TRANSFORM, HDR, fixed, dyn, padding
2528 * -1 because the last one does not need padding
2531 iov
= talloc_array(reqs
[0], struct iovec
, 1 + 5*num_reqs
- 1);
2533 return NT_STATUS_NO_MEMORY
;
2539 for (i
=0; i
<num_reqs
; i
++) {
2549 const DATA_BLOB
*signing_key
= NULL
;
2550 const DATA_BLOB
*encryption_key
= NULL
;
2552 if (!tevent_req_is_in_progress(reqs
[i
])) {
2553 return NT_STATUS_INTERNAL_ERROR
;
2556 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2558 if (!smbXcli_conn_is_connected(state
->conn
)) {
2559 return NT_STATUS_CONNECTION_DISCONNECTED
;
2562 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
2563 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
2564 return NT_STATUS_REVISION_MISMATCH
;
2567 opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
2568 if (opcode
== SMB2_OP_CANCEL
) {
2572 avail
= UINT64_MAX
- state
->conn
->smb2
.mid
;
2574 return NT_STATUS_CONNECTION_ABORTED
;
2577 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2578 charge
= (MAX(state
->smb2
.dyn_len
, 1) - 1)/ 65536 + 1;
2583 charge
= MAX(state
->smb2
.credit_charge
, charge
);
2585 avail
= MIN(avail
, state
->conn
->smb2
.cur_credits
);
2586 if (avail
< charge
) {
2587 return NT_STATUS_INTERNAL_ERROR
;
2591 if (state
->conn
->smb2
.max_credits
> state
->conn
->smb2
.cur_credits
) {
2592 credits
= state
->conn
->smb2
.max_credits
-
2593 state
->conn
->smb2
.cur_credits
;
2595 if (state
->conn
->smb2
.max_credits
>= state
->conn
->smb2
.cur_credits
) {
2599 mid
= state
->conn
->smb2
.mid
;
2600 state
->conn
->smb2
.mid
+= charge
;
2601 state
->conn
->smb2
.cur_credits
-= charge
;
2603 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2604 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT_CHARGE
, charge
);
2606 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT
, credits
);
2607 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
2610 if (state
->session
) {
2612 * We prefer the channel signing key if it is
2615 if (state
->smb2
.should_sign
) {
2616 signing_key
= &state
->session
->smb2
.channel_signing_key
;
2620 * If it is a channel binding, we already have the main
2621 * signing key and try that one.
2623 if (signing_key
&& signing_key
->length
== 0) {
2624 signing_key
= &state
->session
->smb2
.signing_key
;
2628 * If we do not have any session key yet, we skip the
2629 * signing of SMB2_OP_SESSSETUP requests.
2631 if (signing_key
&& signing_key
->length
== 0) {
2635 if (state
->smb2
.should_encrypt
) {
2636 encryption_key
= &state
->session
->smb2
.encryption_key
;
2640 if (encryption_key
) {
2642 iov
[num_iov
].iov_base
= state
->smb2
.transform
;
2643 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.transform
);
2648 iov
[num_iov
].iov_base
= state
->smb2
.hdr
;
2649 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.hdr
);
2652 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.fixed
);
2653 iov
[num_iov
].iov_len
= state
->smb2
.fixed_len
;
2656 if (state
->smb2
.dyn
!= NULL
) {
2657 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.dyn
);
2658 iov
[num_iov
].iov_len
= state
->smb2
.dyn_len
;
2662 reqlen
= sizeof(state
->smb2
.hdr
);
2663 reqlen
+= state
->smb2
.fixed_len
;
2664 reqlen
+= state
->smb2
.dyn_len
;
2666 if (i
< num_reqs
-1) {
2667 if ((reqlen
% 8) > 0) {
2668 uint8_t pad
= 8 - (reqlen
% 8);
2669 iov
[num_iov
].iov_base
= state
->smb2
.pad
;
2670 iov
[num_iov
].iov_len
= pad
;
2674 SIVAL(state
->smb2
.hdr
, SMB2_HDR_NEXT_COMMAND
, reqlen
);
2677 if (encryption_key
) {
2682 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
, mid
);
2683 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
+8,
2684 state
->session
->smb2
.channel_nonce
);
2685 SBVAL(state
->smb2
.transform
, SMB2_TF_MSG_SIZE
,
2688 buf
= talloc_array(iov
, uint8_t, reqlen
);
2690 return NT_STATUS_NO_MEMORY
;
2693 reqlen
+= SMB2_TF_HDR_SIZE
;
2696 * We copy the buffers before encrypting them,
2697 * this is at least currently needed for the
2698 * to keep state->smb2.hdr.
2700 * Also the callers may expect there buffers
2703 for (vi
= hdr_iov
; vi
< num_iov
; vi
++) {
2704 struct iovec
*v
= &iov
[vi
];
2705 const uint8_t *o
= (const uint8_t *)v
->iov_base
;
2707 memcpy(buf
, o
, v
->iov_len
);
2708 v
->iov_base
= (void *)buf
;
2712 status
= smb2_signing_encrypt_pdu(*encryption_key
,
2713 state
->session
->conn
->protocol
,
2714 &iov
[tf_iov
], num_iov
- tf_iov
);
2715 if (!NT_STATUS_IS_OK(status
)) {
2718 } else if (signing_key
) {
2721 status
= smb2_signing_sign_pdu(*signing_key
,
2722 state
->session
->conn
->protocol
,
2723 &iov
[hdr_iov
], num_iov
- hdr_iov
);
2724 if (!NT_STATUS_IS_OK(status
)) {
2731 ret
= smbXcli_req_set_pending(reqs
[i
]);
2733 return NT_STATUS_NO_MEMORY
;
2737 state
= tevent_req_data(reqs
[0], struct smbXcli_req_state
);
2738 _smb_setlen_tcp(state
->length_hdr
, nbt_len
);
2739 iov
[0].iov_base
= state
->length_hdr
;
2740 iov
[0].iov_len
= sizeof(state
->length_hdr
);
2742 if (state
->conn
->dispatch_incoming
== NULL
) {
2743 state
->conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
2746 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
2747 state
->conn
->write_fd
, false, iov
, num_iov
);
2748 if (subreq
== NULL
) {
2749 return NT_STATUS_NO_MEMORY
;
2751 tevent_req_set_callback(subreq
, smb2cli_req_writev_done
, reqs
[0]);
2752 return NT_STATUS_OK
;
2755 void smb2cli_req_set_credit_charge(struct tevent_req
*req
, uint16_t charge
)
2757 struct smbXcli_req_state
*state
=
2758 tevent_req_data(req
,
2759 struct smbXcli_req_state
);
2761 state
->smb2
.credit_charge
= charge
;
2764 struct tevent_req
*smb2cli_req_send(TALLOC_CTX
*mem_ctx
,
2765 struct tevent_context
*ev
,
2766 struct smbXcli_conn
*conn
,
2768 uint32_t additional_flags
,
2769 uint32_t clear_flags
,
2770 uint32_t timeout_msec
,
2773 struct smbXcli_session
*session
,
2774 const uint8_t *fixed
,
2779 struct tevent_req
*req
;
2782 req
= smb2cli_req_create(mem_ctx
, ev
, conn
, cmd
,
2783 additional_flags
, clear_flags
,
2786 fixed
, fixed_len
, dyn
, dyn_len
);
2790 if (!tevent_req_is_in_progress(req
)) {
2791 return tevent_req_post(req
, ev
);
2793 status
= smb2cli_req_compound_submit(&req
, 1);
2794 if (tevent_req_nterror(req
, status
)) {
2795 return tevent_req_post(req
, ev
);
2800 static void smb2cli_req_writev_done(struct tevent_req
*subreq
)
2802 struct tevent_req
*req
=
2803 tevent_req_callback_data(subreq
,
2805 struct smbXcli_req_state
*state
=
2806 tevent_req_data(req
,
2807 struct smbXcli_req_state
);
2811 nwritten
= writev_recv(subreq
, &err
);
2812 TALLOC_FREE(subreq
);
2813 if (nwritten
== -1) {
2814 /* here, we need to notify all pending requests */
2815 NTSTATUS status
= map_nt_error_from_unix_common(err
);
2816 smbXcli_conn_disconnect(state
->conn
, status
);
2821 static NTSTATUS
smb2cli_inbuf_parse_compound(struct smbXcli_conn
*conn
,
2824 TALLOC_CTX
*mem_ctx
,
2825 struct iovec
**piov
, int *pnum_iov
)
2830 uint8_t *first_hdr
= buf
;
2832 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
2834 return NT_STATUS_NO_MEMORY
;
2837 while (taken
< buflen
) {
2840 size_t len
= buflen
- taken
;
2841 uint8_t *hdr
= first_hdr
+ taken
;
2844 size_t next_command_ofs
;
2846 struct iovec
*iov_tmp
;
2849 DEBUG(10, ("%d bytes left, expected at least %d\n",
2853 if (IVAL(hdr
, 0) == SMB2_TF_MAGIC
) {
2854 struct smbXcli_session
*s
;
2856 struct iovec tf_iov
[2];
2859 if (len
< SMB2_TF_HDR_SIZE
) {
2860 DEBUG(10, ("%d bytes left, expected at least %d\n",
2861 (int)len
, SMB2_TF_HDR_SIZE
));
2865 tf_len
= SMB2_TF_HDR_SIZE
;
2868 hdr
= first_hdr
+ taken
;
2869 len
= IVAL(tf
, SMB2_TF_MSG_SIZE
);
2870 uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
2873 for (; s
; s
= s
->next
) {
2874 if (s
->smb2
.session_id
!= uid
) {
2881 DEBUG(10, ("unknown session_id %llu\n",
2882 (unsigned long long)uid
));
2886 tf_iov
[0].iov_base
= (void *)tf
;
2887 tf_iov
[0].iov_len
= tf_len
;
2888 tf_iov
[1].iov_base
= (void *)hdr
;
2889 tf_iov
[1].iov_len
= len
;
2891 status
= smb2_signing_decrypt_pdu(s
->smb2
.decryption_key
,
2894 if (!NT_STATUS_IS_OK(status
)) {
2901 * We need the header plus the body length field
2904 if (len
< SMB2_HDR_BODY
+ 2) {
2905 DEBUG(10, ("%d bytes left, expected at least %d\n",
2906 (int)len
, SMB2_HDR_BODY
));
2909 if (IVAL(hdr
, 0) != SMB2_MAGIC
) {
2910 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2914 if (SVAL(hdr
, 4) != SMB2_HDR_BODY
) {
2915 DEBUG(10, ("Got HDR len %d, expected %d\n",
2916 SVAL(hdr
, 4), SMB2_HDR_BODY
));
2921 next_command_ofs
= IVAL(hdr
, SMB2_HDR_NEXT_COMMAND
);
2922 body_size
= SVAL(hdr
, SMB2_HDR_BODY
);
2924 if (next_command_ofs
!= 0) {
2925 if (next_command_ofs
< (SMB2_HDR_BODY
+ 2)) {
2928 if (next_command_ofs
> full_size
) {
2931 if (tf
&& next_command_ofs
< len
) {
2934 full_size
= next_command_ofs
;
2936 if (body_size
< 2) {
2939 body_size
&= 0xfffe;
2941 if (body_size
> (full_size
- SMB2_HDR_BODY
)) {
2945 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
2947 if (iov_tmp
== NULL
) {
2949 return NT_STATUS_NO_MEMORY
;
2952 cur
= &iov
[num_iov
];
2955 cur
[0].iov_base
= tf
;
2956 cur
[0].iov_len
= tf_len
;
2957 cur
[1].iov_base
= hdr
;
2958 cur
[1].iov_len
= SMB2_HDR_BODY
;
2959 cur
[2].iov_base
= hdr
+ SMB2_HDR_BODY
;
2960 cur
[2].iov_len
= body_size
;
2961 cur
[3].iov_base
= hdr
+ SMB2_HDR_BODY
+ body_size
;
2962 cur
[3].iov_len
= full_size
- (SMB2_HDR_BODY
+ body_size
);
2968 *pnum_iov
= num_iov
;
2969 return NT_STATUS_OK
;
2973 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2976 static struct tevent_req
*smb2cli_conn_find_pending(struct smbXcli_conn
*conn
,
2979 size_t num_pending
= talloc_array_length(conn
->pending
);
2982 for (i
=0; i
<num_pending
; i
++) {
2983 struct tevent_req
*req
= conn
->pending
[i
];
2984 struct smbXcli_req_state
*state
=
2985 tevent_req_data(req
,
2986 struct smbXcli_req_state
);
2988 if (mid
== BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
)) {
2995 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
2996 TALLOC_CTX
*tmp_mem
,
2999 struct tevent_req
*req
;
3000 struct smbXcli_req_state
*state
= NULL
;
3005 struct smbXcli_session
*last_session
= NULL
;
3006 size_t inbuf_len
= smb_len_tcp(inbuf
);
3008 status
= smb2cli_inbuf_parse_compound(conn
,
3009 inbuf
+ NBT_HDR_SIZE
,
3013 if (!NT_STATUS_IS_OK(status
)) {
3017 for (i
=0; i
<num_iov
; i
+=4) {
3018 uint8_t *inbuf_ref
= NULL
;
3019 struct iovec
*cur
= &iov
[i
];
3020 uint8_t *inhdr
= (uint8_t *)cur
[1].iov_base
;
3021 uint16_t opcode
= SVAL(inhdr
, SMB2_HDR_OPCODE
);
3022 uint32_t flags
= IVAL(inhdr
, SMB2_HDR_FLAGS
);
3023 uint64_t mid
= BVAL(inhdr
, SMB2_HDR_MESSAGE_ID
);
3024 uint16_t req_opcode
;
3026 uint16_t credits
= SVAL(inhdr
, SMB2_HDR_CREDIT
);
3027 uint32_t new_credits
;
3028 struct smbXcli_session
*session
= NULL
;
3029 const DATA_BLOB
*signing_key
= NULL
;
3031 new_credits
= conn
->smb2
.cur_credits
;
3032 new_credits
+= credits
;
3033 if (new_credits
> UINT16_MAX
) {
3034 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3036 conn
->smb2
.cur_credits
+= credits
;
3038 req
= smb2cli_conn_find_pending(conn
, mid
);
3040 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3042 state
= tevent_req_data(req
, struct smbXcli_req_state
);
3044 state
->smb2
.got_async
= false;
3046 req_opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
3047 if (opcode
!= req_opcode
) {
3048 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3050 req_flags
= SVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
3052 if (!(flags
& SMB2_HDR_FLAG_REDIRECT
)) {
3053 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3056 status
= NT_STATUS(IVAL(inhdr
, SMB2_HDR_STATUS
));
3057 if ((flags
& SMB2_HDR_FLAG_ASYNC
) &&
3058 NT_STATUS_EQUAL(status
, STATUS_PENDING
)) {
3059 uint64_t async_id
= BVAL(inhdr
, SMB2_HDR_ASYNC_ID
);
3062 * async interim responses are not signed,
3063 * even if the SMB2_HDR_FLAG_SIGNED flag
3066 req_flags
|= SMB2_HDR_FLAG_ASYNC
;
3067 SBVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, req_flags
);
3068 SBVAL(state
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, async_id
);
3070 if (state
->smb2
.notify_async
) {
3071 state
->smb2
.got_async
= true;
3072 tevent_req_defer_callback(req
, state
->ev
);
3073 tevent_req_notify_callback(req
);
3078 session
= state
->session
;
3079 if (req_flags
& SMB2_HDR_FLAG_CHAINED
) {
3080 session
= last_session
;
3082 last_session
= session
;
3084 if (state
->smb2
.should_sign
) {
3085 if (!(flags
& SMB2_HDR_FLAG_SIGNED
)) {
3086 return NT_STATUS_ACCESS_DENIED
;
3090 if (flags
& SMB2_HDR_FLAG_SIGNED
) {
3091 uint64_t uid
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
3093 if (session
== NULL
) {
3094 struct smbXcli_session
*s
;
3096 s
= state
->conn
->sessions
;
3097 for (; s
; s
= s
->next
) {
3098 if (s
->smb2
.session_id
!= uid
) {
3107 if (session
== NULL
) {
3108 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3111 last_session
= session
;
3112 signing_key
= &session
->smb2
.channel_signing_key
;
3115 if (opcode
== SMB2_OP_SESSSETUP
) {
3117 * We prefer the channel signing key, if it is
3120 * If we do not have a channel signing key yet,
3121 * we try the main signing key, if it is not
3122 * the final response.
3124 if (signing_key
&& signing_key
->length
== 0 &&
3125 !NT_STATUS_IS_OK(status
)) {
3126 signing_key
= &session
->smb2
.signing_key
;
3129 if (signing_key
&& signing_key
->length
== 0) {
3131 * If we do not have a session key to
3132 * verify the signature, we defer the
3133 * signing check to the caller.
3135 * The caller gets NT_STATUS_OK, it
3137 * smb2cli_session_set_session_key()
3139 * smb2cli_session_set_channel_key()
3140 * which will check the signature
3141 * with the channel signing key.
3147 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_SESSION_DELETED
)) {
3149 * if the server returns NT_STATUS_USER_SESSION_DELETED
3150 * the response is not signed and we should
3151 * propagate the NT_STATUS_USER_SESSION_DELETED
3152 * status to the caller.
3155 } else if (state
->smb2
.should_encrypt
) {
3156 if (cur
[0].iov_len
!= SMB2_TF_HDR_SIZE
) {
3157 return NT_STATUS_ACCESS_DENIED
;
3161 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_NAME_DELETED
) ||
3162 NT_STATUS_EQUAL(status
, NT_STATUS_FILE_CLOSED
) ||
3163 NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
3165 * if the server returns
3166 * NT_STATUS_NETWORK_NAME_DELETED
3167 * NT_STATUS_FILE_CLOSED
3168 * NT_STATUS_INVALID_PARAMETER
3169 * the response might not be signed
3170 * as this happens before the signing checks.
3172 * If server echos the signature (or all zeros)
3173 * we should report the status from the server
3179 cmp
= memcmp(inhdr
+SMB2_HDR_SIGNATURE
,
3180 state
->smb2
.hdr
+SMB2_HDR_SIGNATURE
,
3183 state
->smb2
.signing_skipped
= true;
3189 static const uint8_t zeros
[16];
3191 cmp
= memcmp(inhdr
+SMB2_HDR_SIGNATURE
,
3195 state
->smb2
.signing_skipped
= true;
3202 status
= smb2_signing_check_pdu(*signing_key
,
3203 state
->conn
->protocol
,
3205 if (!NT_STATUS_IS_OK(status
)) {
3207 * If the signing check fails, we disconnect
3214 smbXcli_req_unset_pending(req
);
3217 * There might be more than one response
3218 * we need to defer the notifications
3220 if ((num_iov
== 5) && (talloc_array_length(conn
->pending
) == 0)) {
3225 tevent_req_defer_callback(req
, state
->ev
);
3229 * Note: here we use talloc_reference() in a way
3230 * that does not expose it to the caller.
3232 inbuf_ref
= talloc_reference(state
->smb2
.recv_iov
, inbuf
);
3233 if (tevent_req_nomem(inbuf_ref
, req
)) {
3237 /* copy the related buffers */
3238 state
->smb2
.recv_iov
[0] = cur
[1];
3239 state
->smb2
.recv_iov
[1] = cur
[2];
3240 state
->smb2
.recv_iov
[2] = cur
[3];
3242 tevent_req_done(req
);
3246 return NT_STATUS_RETRY
;
3249 return NT_STATUS_OK
;
3252 NTSTATUS
smb2cli_req_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
3253 struct iovec
**piov
,
3254 const struct smb2cli_req_expected_response
*expected
,
3255 size_t num_expected
)
3257 struct smbXcli_req_state
*state
=
3258 tevent_req_data(req
,
3259 struct smbXcli_req_state
);
3262 bool found_status
= false;
3263 bool found_size
= false;
3270 if (state
->smb2
.got_async
) {
3271 return STATUS_PENDING
;
3274 if (tevent_req_is_nterror(req
, &status
)) {
3275 for (i
=0; i
< num_expected
; i
++) {
3276 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
3277 found_status
= true;
3283 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
3289 if (num_expected
== 0) {
3290 found_status
= true;
3294 status
= NT_STATUS(IVAL(state
->smb2
.recv_iov
[0].iov_base
, SMB2_HDR_STATUS
));
3295 body_size
= SVAL(state
->smb2
.recv_iov
[1].iov_base
, 0);
3297 for (i
=0; i
< num_expected
; i
++) {
3298 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
3302 found_status
= true;
3303 if (expected
[i
].body_size
== 0) {
3308 if (expected
[i
].body_size
== body_size
) {
3314 if (!found_status
) {
3318 if (state
->smb2
.signing_skipped
) {
3319 if (num_expected
> 0) {
3320 return NT_STATUS_ACCESS_DENIED
;
3322 if (!NT_STATUS_IS_ERR(status
)) {
3323 return NT_STATUS_ACCESS_DENIED
;
3328 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3332 *piov
= talloc_move(mem_ctx
, &state
->smb2
.recv_iov
);
3338 static const struct {
3339 enum protocol_types proto
;
3340 const char *smb1_name
;
3341 } smb1cli_prots
[] = {
3342 {PROTOCOL_CORE
, "PC NETWORK PROGRAM 1.0"},
3343 {PROTOCOL_COREPLUS
, "MICROSOFT NETWORKS 1.03"},
3344 {PROTOCOL_LANMAN1
, "MICROSOFT NETWORKS 3.0"},
3345 {PROTOCOL_LANMAN1
, "LANMAN1.0"},
3346 {PROTOCOL_LANMAN2
, "LM1.2X002"},
3347 {PROTOCOL_LANMAN2
, "DOS LANMAN2.1"},
3348 {PROTOCOL_LANMAN2
, "LANMAN2.1"},
3349 {PROTOCOL_LANMAN2
, "Samba"},
3350 {PROTOCOL_NT1
, "NT LANMAN 1.0"},
3351 {PROTOCOL_NT1
, "NT LM 0.12"},
3352 {PROTOCOL_SMB2_02
, "SMB 2.002"},
3353 {PROTOCOL_SMB2_10
, "SMB 2.???"},
3356 static const struct {
3357 enum protocol_types proto
;
3358 uint16_t smb2_dialect
;
3359 } smb2cli_prots
[] = {
3360 {PROTOCOL_SMB2_02
, SMB2_DIALECT_REVISION_202
},
3361 {PROTOCOL_SMB2_10
, SMB2_DIALECT_REVISION_210
},
3362 {PROTOCOL_SMB2_22
, SMB2_DIALECT_REVISION_222
},
3363 {PROTOCOL_SMB2_24
, SMB2_DIALECT_REVISION_224
},
3364 {PROTOCOL_SMB3_00
, SMB3_DIALECT_REVISION_300
},
3367 struct smbXcli_negprot_state
{
3368 struct smbXcli_conn
*conn
;
3369 struct tevent_context
*ev
;
3370 uint32_t timeout_msec
;
3371 enum protocol_types min_protocol
;
3372 enum protocol_types max_protocol
;
3376 uint8_t dyn
[ARRAY_SIZE(smb2cli_prots
)*2];
3380 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
);
3381 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
);
3382 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
);
3383 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
);
3384 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
);
3385 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
3389 struct tevent_req
*smbXcli_negprot_send(TALLOC_CTX
*mem_ctx
,
3390 struct tevent_context
*ev
,
3391 struct smbXcli_conn
*conn
,
3392 uint32_t timeout_msec
,
3393 enum protocol_types min_protocol
,
3394 enum protocol_types max_protocol
)
3396 struct tevent_req
*req
, *subreq
;
3397 struct smbXcli_negprot_state
*state
;
3399 req
= tevent_req_create(mem_ctx
, &state
,
3400 struct smbXcli_negprot_state
);
3406 state
->timeout_msec
= timeout_msec
;
3407 state
->min_protocol
= min_protocol
;
3408 state
->max_protocol
= max_protocol
;
3410 if (min_protocol
== PROTOCOL_NONE
) {
3411 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3412 return tevent_req_post(req
, ev
);
3415 if (max_protocol
== PROTOCOL_NONE
) {
3416 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3417 return tevent_req_post(req
, ev
);
3420 if (min_protocol
> max_protocol
) {
3421 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3422 return tevent_req_post(req
, ev
);
3425 if ((min_protocol
< PROTOCOL_SMB2_02
) &&
3426 (max_protocol
< PROTOCOL_SMB2_02
)) {
3430 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
3432 subreq
= smbXcli_negprot_smb1_subreq(state
);
3433 if (tevent_req_nomem(subreq
, req
)) {
3434 return tevent_req_post(req
, ev
);
3436 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
3440 if ((min_protocol
>= PROTOCOL_SMB2_02
) &&
3441 (max_protocol
>= PROTOCOL_SMB2_02
)) {
3445 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
3447 subreq
= smbXcli_negprot_smb2_subreq(state
);
3448 if (tevent_req_nomem(subreq
, req
)) {
3449 return tevent_req_post(req
, ev
);
3451 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
3456 * We send an SMB1 negprot with the SMB2 dialects
3457 * and expect a SMB1 or a SMB2 response.
3459 * smbXcli_negprot_dispatch_incoming() will fix the
3460 * callback to match protocol of the response.
3462 conn
->dispatch_incoming
= smbXcli_negprot_dispatch_incoming
;
3464 subreq
= smbXcli_negprot_smb1_subreq(state
);
3465 if (tevent_req_nomem(subreq
, req
)) {
3466 return tevent_req_post(req
, ev
);
3468 tevent_req_set_callback(subreq
, smbXcli_negprot_invalid_done
, req
);
3472 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
)
3474 struct tevent_req
*req
=
3475 tevent_req_callback_data(subreq
,
3480 * we just want the low level error
3482 status
= tevent_req_simple_recv_ntstatus(subreq
);
3483 TALLOC_FREE(subreq
);
3484 if (tevent_req_nterror(req
, status
)) {
3488 /* this should never happen */
3489 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
3492 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
)
3495 DATA_BLOB bytes
= data_blob_null
;
3499 /* setup the protocol strings */
3500 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
3504 if (smb1cli_prots
[i
].proto
< state
->min_protocol
) {
3508 if (smb1cli_prots
[i
].proto
> state
->max_protocol
) {
3512 ok
= data_blob_append(state
, &bytes
, &c
, sizeof(c
));
3518 * We now it is already ascii and
3519 * we want NULL termination.
3521 ok
= data_blob_append(state
, &bytes
,
3522 smb1cli_prots
[i
].smb1_name
,
3523 strlen(smb1cli_prots
[i
].smb1_name
)+1);
3529 smb1cli_req_flags(state
->max_protocol
,
3530 state
->conn
->smb1
.client
.capabilities
,
3535 return smb1cli_req_send(state
, state
->ev
, state
->conn
,
3539 state
->timeout_msec
,
3540 0xFFFE, 0, NULL
, /* pid, tid, session */
3541 0, NULL
, /* wct, vwv */
3542 bytes
.length
, bytes
.data
);
3545 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
)
3547 struct tevent_req
*req
=
3548 tevent_req_callback_data(subreq
,
3550 struct smbXcli_negprot_state
*state
=
3551 tevent_req_data(req
,
3552 struct smbXcli_negprot_state
);
3553 struct smbXcli_conn
*conn
= state
->conn
;
3554 struct iovec
*recv_iov
= NULL
;
3563 size_t num_prots
= 0;
3565 uint32_t client_capabilities
= conn
->smb1
.client
.capabilities
;
3566 uint32_t both_capabilities
;
3567 uint32_t server_capabilities
= 0;
3568 uint32_t capabilities
;
3569 uint32_t client_max_xmit
= conn
->smb1
.client
.max_xmit
;
3570 uint32_t server_max_xmit
= 0;
3572 uint32_t server_max_mux
= 0;
3573 uint16_t server_security_mode
= 0;
3574 uint32_t server_session_key
= 0;
3575 bool server_readbraw
= false;
3576 bool server_writebraw
= false;
3577 bool server_lockread
= false;
3578 bool server_writeunlock
= false;
3579 struct GUID server_guid
= GUID_zero();
3580 DATA_BLOB server_gss_blob
= data_blob_null
;
3581 uint8_t server_challenge
[8];
3582 char *server_workgroup
= NULL
;
3583 char *server_name
= NULL
;
3584 int server_time_zone
= 0;
3585 NTTIME server_system_time
= 0;
3586 static const struct smb1cli_req_expected_response expected
[] = {
3588 .status
= NT_STATUS_OK
,
3589 .wct
= 0x11, /* NT1 */
3592 .status
= NT_STATUS_OK
,
3593 .wct
= 0x0D, /* LM */
3596 .status
= NT_STATUS_OK
,
3597 .wct
= 0x01, /* CORE */
3601 ZERO_STRUCT(server_challenge
);
3603 status
= smb1cli_req_recv(subreq
, state
,
3608 NULL
, /* pvwv_offset */
3611 NULL
, /* pbytes_offset */
3613 expected
, ARRAY_SIZE(expected
));
3614 TALLOC_FREE(subreq
);
3615 if (tevent_req_nterror(req
, status
)) {
3619 flags
= CVAL(inhdr
, HDR_FLG
);
3621 protnum
= SVAL(vwv
, 0);
3623 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
3624 if (smb1cli_prots
[i
].proto
< state
->min_protocol
) {
3628 if (smb1cli_prots
[i
].proto
> state
->max_protocol
) {
3632 if (protnum
!= num_prots
) {
3637 conn
->protocol
= smb1cli_prots
[i
].proto
;
3641 if (conn
->protocol
== PROTOCOL_NONE
) {
3642 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3646 if ((conn
->protocol
< PROTOCOL_NT1
) && conn
->mandatory_signing
) {
3647 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3648 "and the selected protocol level doesn't support it.\n"));
3649 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
3653 if (flags
& FLAG_SUPPORT_LOCKREAD
) {
3654 server_lockread
= true;
3655 server_writeunlock
= true;
3658 if (conn
->protocol
>= PROTOCOL_NT1
) {
3659 const char *client_signing
= NULL
;
3660 bool server_mandatory
= false;
3661 bool server_allowed
= false;
3662 const char *server_signing
= NULL
;
3667 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3672 server_security_mode
= CVAL(vwv
+ 1, 0);
3673 server_max_mux
= SVAL(vwv
+ 1, 1);
3674 server_max_xmit
= IVAL(vwv
+ 3, 1);
3675 server_session_key
= IVAL(vwv
+ 7, 1);
3676 server_time_zone
= SVALS(vwv
+ 15, 1);
3677 server_time_zone
*= 60;
3678 /* this time arrives in real GMT */
3679 server_system_time
= BVAL(vwv
+ 11, 1);
3680 server_capabilities
= IVAL(vwv
+ 9, 1);
3682 key_len
= CVAL(vwv
+ 16, 1);
3684 if (server_capabilities
& CAP_RAW_MODE
) {
3685 server_readbraw
= true;
3686 server_writebraw
= true;
3688 if (server_capabilities
& CAP_LOCK_AND_READ
) {
3689 server_lockread
= true;
3692 if (server_capabilities
& CAP_EXTENDED_SECURITY
) {
3693 DATA_BLOB blob1
, blob2
;
3695 if (num_bytes
< 16) {
3696 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3700 blob1
= data_blob_const(bytes
, 16);
3701 status
= GUID_from_data_blob(&blob1
, &server_guid
);
3702 if (tevent_req_nterror(req
, status
)) {
3706 blob1
= data_blob_const(bytes
+16, num_bytes
-16);
3707 blob2
= data_blob_dup_talloc(state
, blob1
);
3708 if (blob1
.length
> 0 &&
3709 tevent_req_nomem(blob2
.data
, req
)) {
3712 server_gss_blob
= blob2
;
3714 DATA_BLOB blob1
, blob2
;
3716 if (num_bytes
< key_len
) {
3717 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3721 if (key_len
!= 0 && key_len
!= 8) {
3722 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3727 memcpy(server_challenge
, bytes
, 8);
3730 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
3731 blob2
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
3732 if (blob1
.length
> 0) {
3735 len
= utf16_len_n(blob1
.data
,
3739 ok
= convert_string_talloc(state
,
3747 status
= map_nt_error_from_unix_common(errno
);
3748 tevent_req_nterror(req
, status
);
3753 blob2
.data
+= blob1
.length
;
3754 blob2
.length
-= blob1
.length
;
3755 if (blob2
.length
> 0) {
3758 len
= utf16_len_n(blob1
.data
,
3762 ok
= convert_string_talloc(state
,
3770 status
= map_nt_error_from_unix_common(errno
);
3771 tevent_req_nterror(req
, status
);
3777 client_signing
= "disabled";
3778 if (conn
->allow_signing
) {
3779 client_signing
= "allowed";
3781 if (conn
->mandatory_signing
) {
3782 client_signing
= "required";
3785 server_signing
= "not supported";
3786 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_ENABLED
) {
3787 server_signing
= "supported";
3788 server_allowed
= true;
3790 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_REQUIRED
) {
3791 server_signing
= "required";
3792 server_mandatory
= true;
3795 ok
= smb_signing_set_negotiated(conn
->smb1
.signing
,
3799 DEBUG(1,("cli_negprot: SMB signing is required, "
3800 "but client[%s] and server[%s] mismatch\n",
3801 client_signing
, server_signing
));
3802 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
3806 } else if (conn
->protocol
>= PROTOCOL_LANMAN1
) {
3812 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3816 server_security_mode
= SVAL(vwv
+ 1, 0);
3817 server_max_xmit
= SVAL(vwv
+ 2, 0);
3818 server_max_mux
= SVAL(vwv
+ 3, 0);
3819 server_readbraw
= ((SVAL(vwv
+ 5, 0) & 0x1) != 0);
3820 server_writebraw
= ((SVAL(vwv
+ 5, 0) & 0x2) != 0);
3821 server_session_key
= IVAL(vwv
+ 6, 0);
3822 server_time_zone
= SVALS(vwv
+ 10, 0);
3823 server_time_zone
*= 60;
3824 /* this time is converted to GMT by make_unix_date */
3825 t
= pull_dos_date((const uint8_t *)(vwv
+ 8), server_time_zone
);
3826 unix_to_nt_time(&server_system_time
, t
);
3827 key_len
= SVAL(vwv
+ 11, 0);
3829 if (num_bytes
< key_len
) {
3830 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3834 if (key_len
!= 0 && key_len
!= 8) {
3835 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3840 memcpy(server_challenge
, bytes
, 8);
3843 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
3844 if (blob1
.length
> 0) {
3848 len
= utf16_len_n(blob1
.data
,
3852 ok
= convert_string_talloc(state
,
3860 status
= map_nt_error_from_unix_common(errno
);
3861 tevent_req_nterror(req
, status
);
3867 /* the old core protocol */
3868 server_time_zone
= get_time_zone(time(NULL
));
3869 server_max_xmit
= 1024;
3873 if (server_max_xmit
< 1024) {
3874 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3878 if (server_max_mux
< 1) {
3879 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3884 * Now calculate the negotiated capabilities
3885 * based on the mask for:
3886 * - client only flags
3887 * - flags used in both directions
3888 * - server only flags
3890 both_capabilities
= client_capabilities
& server_capabilities
;
3891 capabilities
= client_capabilities
& SMB_CAP_CLIENT_MASK
;
3892 capabilities
|= both_capabilities
& SMB_CAP_BOTH_MASK
;
3893 capabilities
|= server_capabilities
& SMB_CAP_SERVER_MASK
;
3895 max_xmit
= MIN(client_max_xmit
, server_max_xmit
);
3897 conn
->smb1
.server
.capabilities
= server_capabilities
;
3898 conn
->smb1
.capabilities
= capabilities
;
3900 conn
->smb1
.server
.max_xmit
= server_max_xmit
;
3901 conn
->smb1
.max_xmit
= max_xmit
;
3903 conn
->smb1
.server
.max_mux
= server_max_mux
;
3905 conn
->smb1
.server
.security_mode
= server_security_mode
;
3907 conn
->smb1
.server
.readbraw
= server_readbraw
;
3908 conn
->smb1
.server
.writebraw
= server_writebraw
;
3909 conn
->smb1
.server
.lockread
= server_lockread
;
3910 conn
->smb1
.server
.writeunlock
= server_writeunlock
;
3912 conn
->smb1
.server
.session_key
= server_session_key
;
3914 talloc_steal(conn
, server_gss_blob
.data
);
3915 conn
->smb1
.server
.gss_blob
= server_gss_blob
;
3916 conn
->smb1
.server
.guid
= server_guid
;
3917 memcpy(conn
->smb1
.server
.challenge
, server_challenge
, 8);
3918 conn
->smb1
.server
.workgroup
= talloc_move(conn
, &server_workgroup
);
3919 conn
->smb1
.server
.name
= talloc_move(conn
, &server_name
);
3921 conn
->smb1
.server
.time_zone
= server_time_zone
;
3922 conn
->smb1
.server
.system_time
= server_system_time
;
3924 tevent_req_done(req
);
3927 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
)
3931 uint16_t dialect_count
= 0;
3933 buf
= state
->smb2
.dyn
;
3934 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
3935 if (smb2cli_prots
[i
].proto
< state
->min_protocol
) {
3939 if (smb2cli_prots
[i
].proto
> state
->max_protocol
) {
3943 SSVAL(buf
, dialect_count
*2, smb2cli_prots
[i
].smb2_dialect
);
3947 buf
= state
->smb2
.fixed
;
3949 SSVAL(buf
, 2, dialect_count
);
3950 SSVAL(buf
, 4, state
->conn
->smb2
.client
.security_mode
);
3951 SSVAL(buf
, 6, 0); /* Reserved */
3952 if (state
->max_protocol
>= PROTOCOL_SMB2_22
) {
3953 SIVAL(buf
, 8, state
->conn
->smb2
.client
.capabilities
);
3955 SIVAL(buf
, 8, 0); /* Capabilities */
3957 if (state
->max_protocol
>= PROTOCOL_SMB2_10
) {
3961 status
= GUID_to_ndr_blob(&state
->conn
->smb2
.client
.guid
,
3963 if (!NT_STATUS_IS_OK(status
)) {
3966 memcpy(buf
+12, blob
.data
, 16); /* ClientGuid */
3968 memset(buf
+12, 0, 16); /* ClientGuid */
3970 SBVAL(buf
, 28, 0); /* ClientStartTime */
3972 return smb2cli_req_send(state
, state
->ev
,
3973 state
->conn
, SMB2_OP_NEGPROT
,
3975 state
->timeout_msec
,
3976 0xFEFF, 0, NULL
, /* pid, tid, session */
3977 state
->smb2
.fixed
, sizeof(state
->smb2
.fixed
),
3978 state
->smb2
.dyn
, dialect_count
*2);
3981 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
)
3983 struct tevent_req
*req
=
3984 tevent_req_callback_data(subreq
,
3986 struct smbXcli_negprot_state
*state
=
3987 tevent_req_data(req
,
3988 struct smbXcli_negprot_state
);
3989 struct smbXcli_conn
*conn
= state
->conn
;
3990 size_t security_offset
, security_length
;
3996 uint16_t dialect_revision
;
3997 static const struct smb2cli_req_expected_response expected
[] = {
3999 .status
= NT_STATUS_OK
,
4004 status
= smb2cli_req_recv(subreq
, state
, &iov
,
4005 expected
, ARRAY_SIZE(expected
));
4006 TALLOC_FREE(subreq
);
4007 if (tevent_req_nterror(req
, status
)) {
4011 body
= (uint8_t *)iov
[1].iov_base
;
4013 dialect_revision
= SVAL(body
, 4);
4015 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4016 if (smb2cli_prots
[i
].proto
< state
->min_protocol
) {
4020 if (smb2cli_prots
[i
].proto
> state
->max_protocol
) {
4024 if (smb2cli_prots
[i
].smb2_dialect
!= dialect_revision
) {
4028 conn
->protocol
= smb2cli_prots
[i
].proto
;
4032 if (conn
->protocol
== PROTOCOL_NONE
) {
4033 if (state
->min_protocol
>= PROTOCOL_SMB2_02
) {
4034 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4038 if (dialect_revision
!= SMB2_DIALECT_REVISION_2FF
) {
4039 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4043 /* make sure we do not loop forever */
4044 state
->min_protocol
= PROTOCOL_SMB2_02
;
4047 * send a SMB2 negprot, in order to negotiate
4050 subreq
= smbXcli_negprot_smb2_subreq(state
);
4051 if (tevent_req_nomem(subreq
, req
)) {
4054 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4058 conn
->smb2
.server
.security_mode
= SVAL(body
, 2);
4060 blob
= data_blob_const(body
+ 8, 16);
4061 status
= GUID_from_data_blob(&blob
, &conn
->smb2
.server
.guid
);
4062 if (tevent_req_nterror(req
, status
)) {
4066 conn
->smb2
.server
.capabilities
= IVAL(body
, 24);
4067 conn
->smb2
.server
.max_trans_size
= IVAL(body
, 28);
4068 conn
->smb2
.server
.max_read_size
= IVAL(body
, 32);
4069 conn
->smb2
.server
.max_write_size
= IVAL(body
, 36);
4070 conn
->smb2
.server
.system_time
= BVAL(body
, 40);
4071 conn
->smb2
.server
.start_time
= BVAL(body
, 48);
4073 security_offset
= SVAL(body
, 56);
4074 security_length
= SVAL(body
, 58);
4076 if (security_offset
!= SMB2_HDR_BODY
+ iov
[1].iov_len
) {
4077 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4081 if (security_length
> iov
[2].iov_len
) {
4082 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4086 conn
->smb2
.server
.gss_blob
= data_blob_talloc(conn
,
4089 if (tevent_req_nomem(conn
->smb2
.server
.gss_blob
.data
, req
)) {
4093 tevent_req_done(req
);
4096 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
4097 TALLOC_CTX
*tmp_mem
,
4100 size_t num_pending
= talloc_array_length(conn
->pending
);
4101 struct tevent_req
*subreq
;
4102 struct smbXcli_req_state
*substate
;
4103 struct tevent_req
*req
;
4104 uint32_t protocol_magic
;
4105 size_t inbuf_len
= smb_len_nbt(inbuf
);
4107 if (num_pending
!= 1) {
4108 return NT_STATUS_INTERNAL_ERROR
;
4111 if (inbuf_len
< 4) {
4112 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4115 subreq
= conn
->pending
[0];
4116 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
4117 req
= tevent_req_callback_data(subreq
, struct tevent_req
);
4119 protocol_magic
= IVAL(inbuf
, 4);
4121 switch (protocol_magic
) {
4123 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
4124 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
4125 return smb1cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
4128 if (substate
->smb2
.recv_iov
== NULL
) {
4130 * For the SMB1 negprot we have move it.
4132 substate
->smb2
.recv_iov
= substate
->smb1
.recv_iov
;
4133 substate
->smb1
.recv_iov
= NULL
;
4137 * we got an SMB2 answer, which consumed sequence number 0
4138 * so we need to use 1 as the next one
4141 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4142 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
4143 return smb2cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
4146 DEBUG(10, ("Got non-SMB PDU\n"));
4147 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4150 NTSTATUS
smbXcli_negprot_recv(struct tevent_req
*req
)
4152 return tevent_req_simple_recv_ntstatus(req
);
4155 NTSTATUS
smbXcli_negprot(struct smbXcli_conn
*conn
,
4156 uint32_t timeout_msec
,
4157 enum protocol_types min_protocol
,
4158 enum protocol_types max_protocol
)
4160 TALLOC_CTX
*frame
= talloc_stackframe();
4161 struct tevent_context
*ev
;
4162 struct tevent_req
*req
;
4163 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4166 if (smbXcli_conn_has_async_calls(conn
)) {
4168 * Can't use sync call while an async call is in flight
4170 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
4173 ev
= tevent_context_init(frame
);
4177 req
= smbXcli_negprot_send(frame
, ev
, conn
, timeout_msec
,
4178 min_protocol
, max_protocol
);
4182 ok
= tevent_req_poll(req
, ev
);
4184 status
= map_nt_error_from_unix_common(errno
);
4187 status
= smbXcli_negprot_recv(req
);
4193 static int smbXcli_session_destructor(struct smbXcli_session
*session
)
4195 if (session
->conn
== NULL
) {
4199 DLIST_REMOVE(session
->conn
->sessions
, session
);
4203 struct smbXcli_session
*smbXcli_session_create(TALLOC_CTX
*mem_ctx
,
4204 struct smbXcli_conn
*conn
)
4206 struct smbXcli_session
*session
;
4208 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
4209 if (session
== NULL
) {
4212 talloc_set_destructor(session
, smbXcli_session_destructor
);
4214 DLIST_ADD_END(conn
->sessions
, session
, struct smbXcli_session
*);
4215 session
->conn
= conn
;
4220 uint16_t smb1cli_session_current_id(struct smbXcli_session
*session
)
4222 return session
->smb1
.session_id
;
4225 void smb1cli_session_set_id(struct smbXcli_session
*session
,
4226 uint16_t session_id
)
4228 session
->smb1
.session_id
= session_id
;
4231 uint8_t smb2cli_session_security_mode(struct smbXcli_session
*session
)
4233 struct smbXcli_conn
*conn
= session
->conn
;
4234 uint8_t security_mode
= 0;
4237 return security_mode
;
4240 security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
4241 if (conn
->mandatory_signing
) {
4242 security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
4245 return security_mode
;
4248 uint64_t smb2cli_session_current_id(struct smbXcli_session
*session
)
4250 return session
->smb2
.session_id
;
4253 uint16_t smb2cli_session_get_flags(struct smbXcli_session
*session
)
4255 return session
->smb2
.session_flags
;
4258 NTSTATUS
smb2cli_session_application_key(struct smbXcli_session
*session
,
4259 TALLOC_CTX
*mem_ctx
,
4262 *key
= data_blob_null
;
4264 if (session
->smb2
.application_key
.length
== 0) {
4265 return NT_STATUS_NO_USER_SESSION_KEY
;
4268 *key
= data_blob_dup_talloc(mem_ctx
, session
->smb2
.application_key
);
4269 if (key
->data
== NULL
) {
4270 return NT_STATUS_NO_MEMORY
;
4273 return NT_STATUS_OK
;
4276 void smb2cli_session_set_id_and_flags(struct smbXcli_session
*session
,
4277 uint64_t session_id
,
4278 uint16_t session_flags
)
4280 session
->smb2
.session_id
= session_id
;
4281 session
->smb2
.session_flags
= session_flags
;
4284 NTSTATUS
smb2cli_session_set_session_key(struct smbXcli_session
*session
,
4285 const DATA_BLOB _session_key
,
4286 const struct iovec
*recv_iov
)
4288 struct smbXcli_conn
*conn
= session
->conn
;
4289 uint16_t no_sign_flags
;
4290 uint8_t session_key
[16];
4294 return NT_STATUS_INVALID_PARAMETER_MIX
;
4297 no_sign_flags
= SMB2_SESSION_FLAG_IS_GUEST
| SMB2_SESSION_FLAG_IS_NULL
;
4299 if (session
->smb2
.session_flags
& no_sign_flags
) {
4300 session
->smb2
.should_sign
= false;
4301 return NT_STATUS_OK
;
4304 if (session
->smb2
.signing_key
.length
!= 0) {
4305 return NT_STATUS_INVALID_PARAMETER_MIX
;
4308 ZERO_STRUCT(session_key
);
4309 memcpy(session_key
, _session_key
.data
,
4310 MIN(_session_key
.length
, sizeof(session_key
)));
4312 session
->smb2
.signing_key
= data_blob_talloc(session
,
4314 sizeof(session_key
));
4315 if (session
->smb2
.signing_key
.data
== NULL
) {
4316 ZERO_STRUCT(session_key
);
4317 return NT_STATUS_NO_MEMORY
;
4320 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4321 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCMAC");
4322 const DATA_BLOB context
= data_blob_string_const_null("SmbSign");
4324 smb2_key_derivation(session_key
, sizeof(session_key
),
4325 label
.data
, label
.length
,
4326 context
.data
, context
.length
,
4327 session
->smb2
.signing_key
.data
);
4330 session
->smb2
.encryption_key
= data_blob_dup_talloc(session
,
4331 session
->smb2
.signing_key
);
4332 if (session
->smb2
.encryption_key
.data
== NULL
) {
4333 ZERO_STRUCT(session_key
);
4334 return NT_STATUS_NO_MEMORY
;
4337 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4338 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCCM");
4339 const DATA_BLOB context
= data_blob_string_const_null("ServerIn ");
4341 smb2_key_derivation(session_key
, sizeof(session_key
),
4342 label
.data
, label
.length
,
4343 context
.data
, context
.length
,
4344 session
->smb2
.encryption_key
.data
);
4347 session
->smb2
.decryption_key
= data_blob_dup_talloc(session
,
4348 session
->smb2
.signing_key
);
4349 if (session
->smb2
.decryption_key
.data
== NULL
) {
4350 ZERO_STRUCT(session_key
);
4351 return NT_STATUS_NO_MEMORY
;
4354 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4355 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCCM");
4356 const DATA_BLOB context
= data_blob_string_const_null("ServerOut");
4358 smb2_key_derivation(session_key
, sizeof(session_key
),
4359 label
.data
, label
.length
,
4360 context
.data
, context
.length
,
4361 session
->smb2
.decryption_key
.data
);
4364 session
->smb2
.application_key
= data_blob_dup_talloc(session
,
4365 session
->smb2
.signing_key
);
4366 if (session
->smb2
.application_key
.data
== NULL
) {
4367 ZERO_STRUCT(session_key
);
4368 return NT_STATUS_NO_MEMORY
;
4371 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4372 const DATA_BLOB label
= data_blob_string_const_null("SMB2APP");
4373 const DATA_BLOB context
= data_blob_string_const_null("SmbRpc");
4375 smb2_key_derivation(session_key
, sizeof(session_key
),
4376 label
.data
, label
.length
,
4377 context
.data
, context
.length
,
4378 session
->smb2
.application_key
.data
);
4380 ZERO_STRUCT(session_key
);
4382 session
->smb2
.channel_signing_key
= data_blob_dup_talloc(session
,
4383 session
->smb2
.signing_key
);
4384 if (session
->smb2
.channel_signing_key
.data
== NULL
) {
4385 return NT_STATUS_NO_MEMORY
;
4388 status
= smb2_signing_check_pdu(session
->smb2
.channel_signing_key
,
4389 session
->conn
->protocol
,
4391 if (!NT_STATUS_IS_OK(status
)) {
4395 session
->smb2
.should_sign
= false;
4396 session
->smb2
.should_encrypt
= false;
4398 if (conn
->desire_signing
) {
4399 session
->smb2
.should_sign
= true;
4402 if (conn
->smb2
.server
.security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) {
4403 session
->smb2
.should_sign
= true;
4406 if (session
->smb2
.session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
) {
4407 session
->smb2
.should_encrypt
= true;
4410 if (conn
->protocol
< PROTOCOL_SMB2_24
) {
4411 session
->smb2
.should_encrypt
= false;
4414 if (!(conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
4415 session
->smb2
.should_encrypt
= false;
4418 generate_random_buffer((uint8_t *)&session
->smb2
.channel_nonce
,
4419 sizeof(session
->smb2
.channel_nonce
));
4420 session
->smb2
.channel_next
= 1;
4422 return NT_STATUS_OK
;
4425 NTSTATUS
smb2cli_session_create_channel(TALLOC_CTX
*mem_ctx
,
4426 struct smbXcli_session
*session1
,
4427 struct smbXcli_conn
*conn
,
4428 struct smbXcli_session
**_session2
)
4430 struct smbXcli_session
*session2
;
4432 if (session1
->smb2
.signing_key
.length
== 0) {
4433 return NT_STATUS_INVALID_PARAMETER_MIX
;
4436 if (session1
->smb2
.channel_next
== 0) {
4437 return NT_STATUS_INVALID_PARAMETER_MIX
;
4441 return NT_STATUS_INVALID_PARAMETER_MIX
;
4444 session2
= talloc_zero(mem_ctx
, struct smbXcli_session
);
4445 if (session2
== NULL
) {
4446 return NT_STATUS_NO_MEMORY
;
4448 session2
->smb2
.session_id
= session1
->smb2
.session_id
;
4449 session2
->smb2
.session_flags
= session1
->smb2
.session_flags
;
4451 session2
->smb2
.signing_key
= data_blob_dup_talloc(session2
,
4452 session1
->smb2
.signing_key
);
4453 if (session2
->smb2
.signing_key
.data
== NULL
) {
4454 return NT_STATUS_NO_MEMORY
;
4457 session2
->smb2
.application_key
= data_blob_dup_talloc(session2
,
4458 session1
->smb2
.application_key
);
4459 if (session2
->smb2
.application_key
.data
== NULL
) {
4460 return NT_STATUS_NO_MEMORY
;
4463 session2
->smb2
.should_sign
= session1
->smb2
.should_sign
;
4464 session2
->smb2
.should_encrypt
= session1
->smb2
.should_encrypt
;
4466 session2
->smb2
.channel_nonce
= session1
->smb2
.channel_nonce
;
4467 session2
->smb2
.channel_nonce
+= session1
->smb2
.channel_next
;
4468 session1
->smb2
.channel_next
++;
4470 session2
->smb2
.encryption_key
= data_blob_dup_talloc(session2
,
4471 session1
->smb2
.encryption_key
);
4472 if (session2
->smb2
.encryption_key
.data
== NULL
) {
4473 return NT_STATUS_NO_MEMORY
;
4476 session2
->smb2
.decryption_key
= data_blob_dup_talloc(session2
,
4477 session1
->smb2
.decryption_key
);
4478 if (session2
->smb2
.decryption_key
.data
== NULL
) {
4479 return NT_STATUS_NO_MEMORY
;
4482 talloc_set_destructor(session2
, smbXcli_session_destructor
);
4483 DLIST_ADD_END(conn
->sessions
, session2
, struct smbXcli_session
*);
4484 session2
->conn
= conn
;
4486 *_session2
= session2
;
4487 return NT_STATUS_OK
;
4490 NTSTATUS
smb2cli_session_set_channel_key(struct smbXcli_session
*session
,
4491 const DATA_BLOB _channel_key
,
4492 const struct iovec
*recv_iov
)
4494 struct smbXcli_conn
*conn
= session
->conn
;
4495 uint8_t channel_key
[16];
4499 return NT_STATUS_INVALID_PARAMETER_MIX
;
4502 if (session
->smb2
.channel_signing_key
.length
!= 0) {
4503 return NT_STATUS_INVALID_PARAMETER_MIX
;
4506 ZERO_STRUCT(channel_key
);
4507 memcpy(channel_key
, _channel_key
.data
,
4508 MIN(_channel_key
.length
, sizeof(channel_key
)));
4510 session
->smb2
.channel_signing_key
= data_blob_talloc(session
,
4512 sizeof(channel_key
));
4513 if (session
->smb2
.channel_signing_key
.data
== NULL
) {
4514 ZERO_STRUCT(channel_key
);
4515 return NT_STATUS_NO_MEMORY
;
4518 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4519 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCMAC");
4520 const DATA_BLOB context
= data_blob_string_const_null("SmbSign");
4522 smb2_key_derivation(channel_key
, sizeof(channel_key
),
4523 label
.data
, label
.length
,
4524 context
.data
, context
.length
,
4525 session
->smb2
.channel_signing_key
.data
);
4527 ZERO_STRUCT(channel_key
);
4529 status
= smb2_signing_check_pdu(session
->smb2
.channel_signing_key
,
4530 session
->conn
->protocol
,
4532 if (!NT_STATUS_IS_OK(status
)) {
4536 return NT_STATUS_OK
;