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 "lib/util/iov_buf.h"
29 #include "../libcli/smb/smb_common.h"
30 #include "../libcli/smb/smb_seal.h"
31 #include "../libcli/smb/smb_signing.h"
32 #include "../libcli/smb/read_smb.h"
33 #include "smbXcli_base.h"
34 #include "librpc/ndr/libndr.h"
35 #include "libcli/smb/smb2_negotiate_context.h"
36 #include "libcli/smb/smb2_signing.h"
38 #include "lib/crypto/gnutls_helpers.h"
39 #include <gnutls/gnutls.h>
40 #include <gnutls/crypto.h>
44 struct smbXcli_session
;
49 struct sockaddr_storage local_ss
;
50 struct sockaddr_storage remote_ss
;
51 const char *remote_name
;
53 struct tevent_queue
*outgoing
;
54 struct tevent_req
**pending
;
55 struct tevent_req
*read_smb_req
;
56 struct tevent_req
*suicide_req
;
58 enum protocol_types min_protocol
;
59 enum protocol_types max_protocol
;
60 enum protocol_types protocol
;
63 bool mandatory_signing
;
66 * The incoming dispatch function should return:
67 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
68 * - NT_STATUS_OK, if no more processing is desired, e.g.
69 * the dispatch function called
71 * - All other return values disconnect the connection.
73 NTSTATUS (*dispatch_incoming
)(struct smbXcli_conn
*conn
,
79 uint32_t capabilities
;
84 uint32_t capabilities
;
87 uint16_t security_mode
;
96 const char *workgroup
;
102 uint32_t capabilities
;
107 struct smb1_signing_state
*signing
;
108 struct smb_trans_enc_state
*trans_enc
;
110 struct tevent_req
*read_braw_req
;
115 uint32_t capabilities
;
116 uint16_t security_mode
;
118 struct smb311_capabilities smb3_capabilities
;
122 uint32_t capabilities
;
123 uint16_t security_mode
;
125 uint32_t max_trans_size
;
126 uint32_t max_read_size
;
127 uint32_t max_write_size
;
137 uint16_t cur_credits
;
138 uint16_t max_credits
;
140 uint32_t cc_chunk_len
;
141 uint32_t cc_max_chunks
;
145 bool force_channel_sequence
;
147 uint8_t preauth_sha512
[64];
150 struct smbXcli_session
*sessions
;
153 struct smb2cli_session
{
155 uint16_t session_flags
;
156 struct smb2_signing_key
*application_key
;
157 struct smb2_signing_key
*signing_key
;
160 struct smb2_signing_key
*encryption_key
;
161 struct smb2_signing_key
*decryption_key
;
162 uint64_t nonce_high_random
;
163 uint64_t nonce_high_max
;
166 uint16_t channel_sequence
;
168 bool require_signed_response
;
171 * The following are just for torture tests
173 bool anonymous_signing
;
174 bool anonymous_encryption
;
175 bool no_signing_disconnect
;
178 struct smbXcli_session
{
179 struct smbXcli_session
*prev
, *next
;
180 struct smbXcli_conn
*conn
;
185 DATA_BLOB application_key
;
189 struct smb2cli_session
*smb2
;
192 struct smb2_signing_key
*signing_key
;
193 uint8_t preauth_sha512
[64];
197 * this should be a short term hack
198 * until the upper layers have implemented
201 bool disconnect_expired
;
204 struct smbXcli_tcon
{
206 uint32_t fs_attributes
;
210 uint16_t optional_support
;
211 uint32_t maximal_access
;
212 uint32_t guest_maximal_access
;
221 uint32_t capabilities
;
222 uint32_t maximal_access
;
228 struct smbXcli_req_state
{
229 struct tevent_context
*ev
;
230 struct smbXcli_conn
*conn
;
231 struct smbXcli_session
*session
; /* maybe NULL */
232 struct smbXcli_tcon
*tcon
; /* maybe NULL */
234 uint8_t length_hdr
[4];
240 struct tevent_req
*write_req
;
242 struct timeval endtime
;
245 /* Space for the header including the wct */
246 uint8_t hdr
[HDR_VWV
];
249 * For normal requests, smb1cli_req_send chooses a mid.
250 * SecondaryV trans requests need to use the mid of the primary
251 * request, so we need a place to store it.
252 * Assume it is set if != 0.
257 uint8_t bytecount_buf
[2];
259 #define MAX_SMB_IOV 10
260 /* length_hdr, hdr, words, byte_count, buffers */
261 struct iovec iov
[1 + 3 + MAX_SMB_IOV
];
266 struct tevent_req
**chained_requests
;
269 NTSTATUS recv_status
;
270 /* always an array of 3 talloc elements */
271 struct iovec
*recv_iov
;
275 const uint8_t *fixed
;
280 uint8_t transform
[SMB2_TF_HDR_SIZE
];
281 uint8_t hdr
[SMB2_HDR_BODY
];
282 uint8_t pad
[7]; /* padding space for compounding */
285 * always an array of 3 talloc elements
286 * (without a SMB2_TRANSFORM header!)
290 struct iovec
*recv_iov
;
293 * the expected max for the response dyn_len
295 uint32_t max_dyn_len
;
297 uint16_t credit_charge
;
301 uint64_t encryption_session_id
;
303 bool signing_skipped
;
304 bool require_signed_response
;
307 uint16_t cancel_flags
;
313 static int smbXcli_conn_destructor(struct smbXcli_conn
*conn
)
316 * NT_STATUS_OK, means we do not notify the callers
318 smbXcli_conn_disconnect(conn
, NT_STATUS_OK
);
320 while (conn
->sessions
) {
321 conn
->sessions
->conn
= NULL
;
322 DLIST_REMOVE(conn
->sessions
, conn
->sessions
);
325 if (conn
->smb1
.trans_enc
) {
326 TALLOC_FREE(conn
->smb1
.trans_enc
);
332 struct smbXcli_conn
*smbXcli_conn_create(TALLOC_CTX
*mem_ctx
,
334 const char *remote_name
,
335 enum smb_signing_setting signing_state
,
336 uint32_t smb1_capabilities
,
337 struct GUID
*client_guid
,
338 uint32_t smb2_capabilities
,
339 const struct smb311_capabilities
*smb3_capabilities
)
341 struct smbXcli_conn
*conn
= NULL
;
343 struct sockaddr
*sa
= NULL
;
347 if (smb3_capabilities
!= NULL
) {
348 const struct smb3_signing_capabilities
*sign_algos
=
349 &smb3_capabilities
->signing
;
350 const struct smb3_encryption_capabilities
*ciphers
=
351 &smb3_capabilities
->encryption
;
353 SMB_ASSERT(sign_algos
->num_algos
<= SMB3_SIGNING_CAPABILITIES_MAX_ALGOS
);
354 SMB_ASSERT(ciphers
->num_algos
<= SMB3_ENCRYTION_CAPABILITIES_MAX_ALGOS
);
357 conn
= talloc_zero(mem_ctx
, struct smbXcli_conn
);
362 ret
= set_blocking(fd
, false);
368 conn
->remote_name
= talloc_strdup(conn
, remote_name
);
369 if (conn
->remote_name
== NULL
) {
373 ss
= (void *)&conn
->local_ss
;
374 sa
= (struct sockaddr
*)ss
;
375 sa_length
= sizeof(conn
->local_ss
);
376 ret
= getsockname(fd
, sa
, &sa_length
);
380 ss
= (void *)&conn
->remote_ss
;
381 sa
= (struct sockaddr
*)ss
;
382 sa_length
= sizeof(conn
->remote_ss
);
383 ret
= getpeername(fd
, sa
, &sa_length
);
388 conn
->outgoing
= tevent_queue_create(conn
, "smbXcli_outgoing");
389 if (conn
->outgoing
== NULL
) {
392 conn
->pending
= NULL
;
394 conn
->min_protocol
= PROTOCOL_NONE
;
395 conn
->max_protocol
= PROTOCOL_NONE
;
396 conn
->protocol
= PROTOCOL_NONE
;
398 switch (signing_state
) {
399 case SMB_SIGNING_OFF
:
401 conn
->allow_signing
= false;
402 conn
->desire_signing
= false;
403 conn
->mandatory_signing
= false;
405 case SMB_SIGNING_DEFAULT
:
406 case SMB_SIGNING_IF_REQUIRED
:
407 /* if the server requires it */
408 conn
->allow_signing
= true;
409 conn
->desire_signing
= false;
410 conn
->mandatory_signing
= false;
412 case SMB_SIGNING_DESIRED
:
413 /* if the server desires it */
414 conn
->allow_signing
= true;
415 conn
->desire_signing
= true;
416 conn
->mandatory_signing
= false;
418 case SMB_SIGNING_IPC_DEFAULT
:
419 case SMB_SIGNING_REQUIRED
:
421 conn
->allow_signing
= true;
422 conn
->desire_signing
= true;
423 conn
->mandatory_signing
= true;
427 conn
->smb1
.client
.capabilities
= smb1_capabilities
;
428 conn
->smb1
.client
.max_xmit
= UINT16_MAX
;
430 conn
->smb1
.capabilities
= conn
->smb1
.client
.capabilities
;
431 conn
->smb1
.max_xmit
= 1024;
435 /* initialise signing */
436 conn
->smb1
.signing
= smb1_signing_init(conn
,
438 conn
->desire_signing
,
439 conn
->mandatory_signing
);
440 if (!conn
->smb1
.signing
) {
444 conn
->smb2
.client
.security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
445 if (conn
->mandatory_signing
) {
446 conn
->smb2
.client
.security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
449 conn
->smb2
.client
.guid
= *client_guid
;
451 conn
->smb2
.client
.capabilities
= smb2_capabilities
;
452 if (smb3_capabilities
!= NULL
) {
453 conn
->smb2
.client
.smb3_capabilities
= *smb3_capabilities
;
456 conn
->smb2
.cur_credits
= 1;
457 conn
->smb2
.max_credits
= 0;
458 conn
->smb2
.io_priority
= 1;
461 * Samba and Windows servers accept a maximum of 16 MiB with a maximum
462 * chunk length of 1 MiB.
464 conn
->smb2
.cc_chunk_len
= 1024 * 1024;
465 conn
->smb2
.cc_max_chunks
= 16;
467 talloc_set_destructor(conn
, smbXcli_conn_destructor
);
475 bool smbXcli_conn_is_connected(struct smbXcli_conn
*conn
)
481 if (conn
->sock_fd
== -1) {
488 enum protocol_types
smbXcli_conn_protocol(struct smbXcli_conn
*conn
)
490 return conn
->protocol
;
493 bool smbXcli_conn_use_unicode(struct smbXcli_conn
*conn
)
495 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
499 if (conn
->smb1
.capabilities
& CAP_UNICODE
) {
506 bool smbXcli_conn_signing_mandatory(struct smbXcli_conn
*conn
)
508 return conn
->mandatory_signing
;
511 bool smbXcli_conn_have_posix(struct smbXcli_conn
*conn
)
513 if (conn
->protocol
>= PROTOCOL_SMB3_11
) {
514 return conn
->smb2
.server
.smb311_posix
;
516 if (conn
->protocol
<= PROTOCOL_NT1
) {
517 return (conn
->smb1
.capabilities
& CAP_UNIX
);
523 * [MS-SMB] 2.2.2.3.5 - SMB1 support for passing through
524 * query/set commands to the file system
526 bool smbXcli_conn_support_passthrough(struct smbXcli_conn
*conn
)
528 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
532 if (conn
->smb1
.capabilities
& CAP_W2K_SMBS
) {
539 void smbXcli_conn_set_sockopt(struct smbXcli_conn
*conn
, const char *options
)
541 set_socket_options(conn
->sock_fd
, options
);
544 const struct sockaddr_storage
*smbXcli_conn_local_sockaddr(struct smbXcli_conn
*conn
)
546 return &conn
->local_ss
;
549 const struct sockaddr_storage
*smbXcli_conn_remote_sockaddr(struct smbXcli_conn
*conn
)
551 return &conn
->remote_ss
;
554 const char *smbXcli_conn_remote_name(struct smbXcli_conn
*conn
)
556 return conn
->remote_name
;
559 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn
*conn
)
561 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
568 return conn
->smb1
.server
.max_mux
;
571 NTTIME
smbXcli_conn_server_system_time(struct smbXcli_conn
*conn
)
573 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
574 return conn
->smb2
.server
.system_time
;
577 return conn
->smb1
.server
.system_time
;
580 const DATA_BLOB
*smbXcli_conn_server_gss_blob(struct smbXcli_conn
*conn
)
582 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
583 return &conn
->smb2
.server
.gss_blob
;
586 return &conn
->smb1
.server
.gss_blob
;
589 const struct GUID
*smbXcli_conn_server_guid(struct smbXcli_conn
*conn
)
591 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
592 return &conn
->smb2
.server
.guid
;
595 return &conn
->smb1
.server
.guid
;
598 bool smbXcli_conn_get_force_channel_sequence(struct smbXcli_conn
*conn
)
600 return conn
->smb2
.force_channel_sequence
;
603 void smbXcli_conn_set_force_channel_sequence(struct smbXcli_conn
*conn
,
606 conn
->smb2
.force_channel_sequence
= v
;
609 struct smbXcli_conn_samba_suicide_state
{
610 struct smbXcli_conn
*conn
;
613 struct tevent_req
*write_req
;
616 static void smbXcli_conn_samba_suicide_cleanup(struct tevent_req
*req
,
617 enum tevent_req_state req_state
);
618 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
);
620 struct tevent_req
*smbXcli_conn_samba_suicide_send(TALLOC_CTX
*mem_ctx
,
621 struct tevent_context
*ev
,
622 struct smbXcli_conn
*conn
,
625 struct tevent_req
*req
, *subreq
;
626 struct smbXcli_conn_samba_suicide_state
*state
;
628 req
= tevent_req_create(mem_ctx
, &state
,
629 struct smbXcli_conn_samba_suicide_state
);
634 SIVAL(state
->buf
, 4, SMB_SUICIDE_PACKET
);
635 SCVAL(state
->buf
, 8, exitcode
);
636 _smb_setlen_nbt(state
->buf
, sizeof(state
->buf
)-4);
638 if (conn
->suicide_req
!= NULL
) {
639 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
640 return tevent_req_post(req
, ev
);
643 state
->iov
.iov_base
= state
->buf
;
644 state
->iov
.iov_len
= sizeof(state
->buf
);
646 subreq
= writev_send(state
, ev
, conn
->outgoing
, conn
->sock_fd
,
647 false, &state
->iov
, 1);
648 if (tevent_req_nomem(subreq
, req
)) {
649 return tevent_req_post(req
, ev
);
651 tevent_req_set_callback(subreq
, smbXcli_conn_samba_suicide_done
, req
);
652 state
->write_req
= subreq
;
654 tevent_req_set_cleanup_fn(req
, smbXcli_conn_samba_suicide_cleanup
);
657 * We need to use tevent_req_defer_callback()
658 * in order to allow smbXcli_conn_disconnect()
659 * to do a safe cleanup.
661 tevent_req_defer_callback(req
, ev
);
662 conn
->suicide_req
= req
;
667 static void smbXcli_conn_samba_suicide_cleanup(struct tevent_req
*req
,
668 enum tevent_req_state req_state
)
670 struct smbXcli_conn_samba_suicide_state
*state
= tevent_req_data(
671 req
, struct smbXcli_conn_samba_suicide_state
);
673 TALLOC_FREE(state
->write_req
);
675 if (state
->conn
== NULL
) {
679 if (state
->conn
->suicide_req
== req
) {
680 state
->conn
->suicide_req
= NULL
;
685 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
)
687 struct tevent_req
*req
= tevent_req_callback_data(
688 subreq
, struct tevent_req
);
689 struct smbXcli_conn_samba_suicide_state
*state
= tevent_req_data(
690 req
, struct smbXcli_conn_samba_suicide_state
);
694 state
->write_req
= NULL
;
696 nwritten
= writev_recv(subreq
, &err
);
698 if (nwritten
== -1) {
699 /* here, we need to notify all pending requests */
700 NTSTATUS status
= map_nt_error_from_unix_common(err
);
701 smbXcli_conn_disconnect(state
->conn
, status
);
704 tevent_req_done(req
);
707 NTSTATUS
smbXcli_conn_samba_suicide_recv(struct tevent_req
*req
)
709 return tevent_req_simple_recv_ntstatus(req
);
712 NTSTATUS
smbXcli_conn_samba_suicide(struct smbXcli_conn
*conn
,
715 TALLOC_CTX
*frame
= talloc_stackframe();
716 struct tevent_context
*ev
;
717 struct tevent_req
*req
;
718 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
721 if (smbXcli_conn_has_async_calls(conn
)) {
723 * Can't use sync call while an async call is in flight
725 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
728 ev
= samba_tevent_context_init(frame
);
732 req
= smbXcli_conn_samba_suicide_send(frame
, ev
, conn
, exitcode
);
736 ok
= tevent_req_poll_ntstatus(req
, ev
, &status
);
740 status
= smbXcli_conn_samba_suicide_recv(req
);
746 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn
*conn
)
748 return conn
->smb1
.capabilities
;
751 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn
*conn
)
753 return conn
->smb1
.max_xmit
;
756 bool smb1cli_conn_req_possible(struct smbXcli_conn
*conn
)
758 size_t pending
= talloc_array_length(conn
->pending
);
759 uint16_t possible
= conn
->smb1
.server
.max_mux
;
761 if (pending
>= possible
) {
768 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn
*conn
)
770 return conn
->smb1
.server
.session_key
;
773 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn
*conn
)
775 return conn
->smb1
.server
.challenge
;
778 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
780 return conn
->smb1
.server
.security_mode
;
783 bool smb1cli_conn_server_readbraw(struct smbXcli_conn
*conn
)
785 return conn
->smb1
.server
.readbraw
;
788 bool smb1cli_conn_server_writebraw(struct smbXcli_conn
*conn
)
790 return conn
->smb1
.server
.writebraw
;
793 bool smb1cli_conn_server_lockread(struct smbXcli_conn
*conn
)
795 return conn
->smb1
.server
.lockread
;
798 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn
*conn
)
800 return conn
->smb1
.server
.writeunlock
;
803 int smb1cli_conn_server_time_zone(struct smbXcli_conn
*conn
)
805 return conn
->smb1
.server
.time_zone
;
808 bool smb1cli_conn_activate_signing(struct smbXcli_conn
*conn
,
809 const DATA_BLOB user_session_key
,
810 const DATA_BLOB response
)
812 return smb1_signing_activate(conn
->smb1
.signing
,
817 bool smb1cli_conn_check_signing(struct smbXcli_conn
*conn
,
818 const uint8_t *buf
, uint32_t seqnum
)
820 const uint8_t *hdr
= buf
+ NBT_HDR_SIZE
;
821 size_t len
= smb_len_nbt(buf
);
823 return smb1_signing_check_pdu(conn
->smb1
.signing
, hdr
, len
, seqnum
);
826 bool smb1cli_conn_signing_is_active(struct smbXcli_conn
*conn
)
828 return smb1_signing_is_active(conn
->smb1
.signing
);
831 void smb1cli_conn_set_encryption(struct smbXcli_conn
*conn
,
832 struct smb_trans_enc_state
*es
)
834 /* Replace the old state, if any. */
835 if (conn
->smb1
.trans_enc
) {
836 TALLOC_FREE(conn
->smb1
.trans_enc
);
838 conn
->smb1
.trans_enc
= es
;
841 bool smb1cli_conn_encryption_on(struct smbXcli_conn
*conn
)
843 return common_encryption_on(conn
->smb1
.trans_enc
);
847 static NTSTATUS
smb1cli_pull_raw_error(const uint8_t *hdr
)
849 uint32_t flags2
= SVAL(hdr
, HDR_FLG2
);
850 NTSTATUS status
= NT_STATUS(IVAL(hdr
, HDR_RCLS
));
852 if (NT_STATUS_IS_OK(status
)) {
856 if (flags2
& FLAGS2_32_BIT_ERROR_CODES
) {
860 return NT_STATUS_DOS(CVAL(hdr
, HDR_RCLS
), SVAL(hdr
, HDR_ERR
));
864 * Is the SMB command able to hold an AND_X successor
865 * @param[in] cmd The SMB command in question
866 * @retval Can we add a chained request after "cmd"?
868 bool smb1cli_is_andx_req(uint8_t cmd
)
888 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn
*conn
)
890 size_t num_pending
= talloc_array_length(conn
->pending
);
893 if (conn
->protocol
== PROTOCOL_NONE
) {
895 * This is what windows sends on the SMB1 Negprot request
896 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
904 result
= conn
->smb1
.mid
++;
905 if ((result
== 0) || (result
== 0xffff)) {
909 for (i
=0; i
<num_pending
; i
++) {
910 if (result
== smb1cli_req_mid(conn
->pending
[i
])) {
915 if (i
== num_pending
) {
921 static NTSTATUS
smbXcli_req_cancel_write_req(struct tevent_req
*req
)
923 struct smbXcli_req_state
*state
=
925 struct smbXcli_req_state
);
926 struct smbXcli_conn
*conn
= state
->conn
;
927 size_t num_pending
= talloc_array_length(conn
->pending
);
932 if (state
->write_req
== NULL
) {
937 * Check if it's possible to cancel the request.
938 * If the result is true it's not too late.
939 * See writev_cancel().
941 ok
= tevent_req_cancel(state
->write_req
);
943 TALLOC_FREE(state
->write_req
);
945 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
947 * SMB2 has a sane signing state.
952 if (num_pending
> 1) {
954 * We have more pending requests following us. This
955 * means the signing state will be broken for them.
957 * As a solution we could add the requests directly to
958 * our outgoing queue and do the signing in the trigger
959 * function and then use writev_send() without passing a
960 * queue. That way we'll only sign packets we're most
961 * likely send to the wire.
963 return NT_STATUS_REQUEST_OUT_OF_SEQUENCE
;
967 * If we're the only request that's
968 * pending, we're able to recover the signing
971 smb1_signing_cancel_reply(conn
->smb1
.signing
,
972 state
->smb1
.one_way_seqnum
);
976 ret
= writev_recv(state
->write_req
, &err
);
977 TALLOC_FREE(state
->write_req
);
979 return map_nt_error_from_unix_common(err
);
985 void smbXcli_req_unset_pending(struct tevent_req
*req
)
987 struct smbXcli_req_state
*state
=
989 struct smbXcli_req_state
);
990 struct smbXcli_conn
*conn
= state
->conn
;
991 size_t num_pending
= talloc_array_length(conn
->pending
);
993 NTSTATUS cancel_status
;
995 cancel_status
= smbXcli_req_cancel_write_req(req
);
997 if (state
->smb1
.mid
!= 0) {
999 * This is a [nt]trans[2] request which waits
1000 * for more than one reply.
1002 if (!NT_STATUS_IS_OK(cancel_status
)) {
1004 * If the write_req cancel didn't work
1005 * we can't use the connection anymore.
1007 smbXcli_conn_disconnect(conn
, cancel_status
);
1013 tevent_req_set_cleanup_fn(req
, NULL
);
1015 if (num_pending
== 1) {
1017 * The pending read_smb tevent_req is a child of
1018 * conn->pending. So if nothing is pending anymore, we need to
1019 * delete the socket read fde.
1021 /* TODO: smbXcli_conn_cancel_read_req */
1022 TALLOC_FREE(conn
->pending
);
1023 conn
->read_smb_req
= NULL
;
1025 if (!NT_STATUS_IS_OK(cancel_status
)) {
1027 * If the write_req cancel didn't work
1028 * we can't use the connection anymore.
1030 smbXcli_conn_disconnect(conn
, cancel_status
);
1036 for (i
=0; i
<num_pending
; i
++) {
1037 if (req
== conn
->pending
[i
]) {
1041 if (i
== num_pending
) {
1043 * Something's seriously broken. Just returning here is the
1044 * right thing nevertheless, the point of this routine is to
1045 * remove ourselves from conn->pending.
1048 if (!NT_STATUS_IS_OK(cancel_status
)) {
1050 * If the write_req cancel didn't work
1051 * we can't use the connection anymore.
1053 smbXcli_conn_disconnect(conn
, cancel_status
);
1060 * Remove ourselves from the conn->pending array
1062 for (; i
< (num_pending
- 1); i
++) {
1063 conn
->pending
[i
] = conn
->pending
[i
+1];
1067 * No NULL check here, we're shrinking by sizeof(void *), and
1068 * talloc_realloc just adjusts the size for this.
1070 conn
->pending
= talloc_realloc(NULL
, conn
->pending
, struct tevent_req
*,
1073 if (!NT_STATUS_IS_OK(cancel_status
)) {
1075 * If the write_req cancel didn't work
1076 * we can't use the connection anymore.
1078 smbXcli_conn_disconnect(conn
, cancel_status
);
1084 static void smbXcli_req_cleanup(struct tevent_req
*req
,
1085 enum tevent_req_state req_state
)
1087 struct smbXcli_req_state
*state
=
1088 tevent_req_data(req
,
1089 struct smbXcli_req_state
);
1090 struct smbXcli_conn
*conn
= state
->conn
;
1091 NTSTATUS cancel_status
;
1093 switch (req_state
) {
1094 case TEVENT_REQ_RECEIVED
:
1096 * Make sure we really remove it from
1097 * the pending array on destruction.
1099 * smbXcli_req_unset_pending() calls
1100 * smbXcli_req_cancel_write_req() internal
1102 state
->smb1
.mid
= 0;
1103 smbXcli_req_unset_pending(req
);
1106 cancel_status
= smbXcli_req_cancel_write_req(req
);
1107 if (!NT_STATUS_IS_OK(cancel_status
)) {
1109 * If the write_req cancel didn't work
1110 * we can't use the connection anymore.
1112 smbXcli_conn_disconnect(conn
, cancel_status
);
1119 static bool smb1cli_req_cancel(struct tevent_req
*req
);
1120 static bool smb2cli_req_cancel(struct tevent_req
*req
);
1122 static bool smbXcli_req_cancel(struct tevent_req
*req
)
1124 struct smbXcli_req_state
*state
=
1125 tevent_req_data(req
,
1126 struct smbXcli_req_state
);
1128 if (!smbXcli_conn_is_connected(state
->conn
)) {
1132 if (state
->conn
->protocol
== PROTOCOL_NONE
) {
1136 if (state
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
1137 return smb2cli_req_cancel(req
);
1140 return smb1cli_req_cancel(req
);
1143 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
);
1145 bool smbXcli_req_set_pending(struct tevent_req
*req
)
1147 struct smbXcli_req_state
*state
=
1148 tevent_req_data(req
,
1149 struct smbXcli_req_state
);
1150 struct smbXcli_conn
*conn
;
1151 struct tevent_req
**pending
;
1156 if (!smbXcli_conn_is_connected(conn
)) {
1160 num_pending
= talloc_array_length(conn
->pending
);
1162 pending
= talloc_realloc(conn
, conn
->pending
, struct tevent_req
*,
1164 if (pending
== NULL
) {
1167 pending
[num_pending
] = req
;
1168 conn
->pending
= pending
;
1169 tevent_req_set_cleanup_fn(req
, smbXcli_req_cleanup
);
1170 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
1172 if (!smbXcli_conn_receive_next(conn
)) {
1174 * the caller should notify the current request
1176 * And all other pending requests get notified
1177 * by smbXcli_conn_disconnect().
1179 smbXcli_req_unset_pending(req
);
1180 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
1187 static void smbXcli_conn_received(struct tevent_req
*subreq
);
1189 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
)
1191 size_t num_pending
= talloc_array_length(conn
->pending
);
1192 struct tevent_req
*req
;
1193 struct smbXcli_req_state
*state
;
1195 if (conn
->read_smb_req
!= NULL
) {
1199 if (num_pending
== 0) {
1200 if (conn
->smb2
.mid
< UINT64_MAX
) {
1201 /* no more pending requests, so we are done for now */
1206 * If there are no more SMB2 requests possible,
1207 * because we are out of message ids,
1208 * we need to disconnect.
1210 smbXcli_conn_disconnect(conn
, NT_STATUS_CONNECTION_ABORTED
);
1214 req
= conn
->pending
[0];
1215 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1218 * We're the first ones, add the read_smb request that waits for the
1219 * answer from the server
1221 conn
->read_smb_req
= read_smb_send(conn
->pending
,
1224 if (conn
->read_smb_req
== NULL
) {
1227 tevent_req_set_callback(conn
->read_smb_req
, smbXcli_conn_received
, conn
);
1231 void smbXcli_conn_disconnect(struct smbXcli_conn
*conn
, NTSTATUS status
)
1233 struct smbXcli_session
*session
;
1234 int sock_fd
= conn
->sock_fd
;
1236 tevent_queue_stop(conn
->outgoing
);
1240 session
= conn
->sessions
;
1241 if (talloc_array_length(conn
->pending
) == 0) {
1243 * if we do not have pending requests
1244 * there is no need to update the channel_sequence
1248 for (; session
; session
= session
->next
) {
1249 smb2cli_session_increment_channel_sequence(session
);
1252 if (conn
->suicide_req
!= NULL
) {
1254 * smbXcli_conn_samba_suicide_send()
1255 * used tevent_req_defer_callback() already.
1257 if (!NT_STATUS_IS_OK(status
)) {
1258 tevent_req_nterror(conn
->suicide_req
, status
);
1260 conn
->suicide_req
= NULL
;
1264 * Cancel all pending requests. We do not do a for-loop walking
1265 * conn->pending because that array changes in
1266 * smbXcli_req_unset_pending.
1268 while (conn
->pending
!= NULL
&&
1269 talloc_array_length(conn
->pending
) > 0) {
1270 struct tevent_req
*req
;
1271 struct smbXcli_req_state
*state
;
1272 struct tevent_req
**chain
;
1276 req
= conn
->pending
[0];
1277 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1279 if (state
->smb1
.chained_requests
== NULL
) {
1283 * We're dead. No point waiting for trans2
1286 state
->smb1
.mid
= 0;
1288 smbXcli_req_unset_pending(req
);
1290 if (NT_STATUS_IS_OK(status
)) {
1291 /* do not notify the callers */
1295 in_progress
= tevent_req_is_in_progress(req
);
1304 * we need to defer the callback, because we may notify
1305 * more then one caller.
1307 tevent_req_defer_callback(req
, state
->ev
);
1308 tevent_req_nterror(req
, status
);
1312 chain
= talloc_move(conn
, &state
->smb1
.chained_requests
);
1313 num_chained
= talloc_array_length(chain
);
1315 for (i
=0; i
<num_chained
; i
++) {
1319 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1322 * We're dead. No point waiting for trans2
1325 state
->smb1
.mid
= 0;
1327 smbXcli_req_unset_pending(req
);
1329 if (NT_STATUS_IS_OK(status
)) {
1330 /* do not notify the callers */
1334 in_progress
= tevent_req_is_in_progress(req
);
1343 * we need to defer the callback, because we may notify
1344 * more than one caller.
1346 tevent_req_defer_callback(req
, state
->ev
);
1347 tevent_req_nterror(req
, status
);
1352 if (sock_fd
!= -1) {
1358 * Fetch a smb request's mid. Only valid after the request has been sent by
1359 * smb1cli_req_send().
1361 uint16_t smb1cli_req_mid(struct tevent_req
*req
)
1363 struct smbXcli_req_state
*state
=
1364 tevent_req_data(req
,
1365 struct smbXcli_req_state
);
1367 if (state
->smb1
.mid
!= 0) {
1368 return state
->smb1
.mid
;
1371 return SVAL(state
->smb1
.hdr
, HDR_MID
);
1374 void smb1cli_req_set_mid(struct tevent_req
*req
, uint16_t mid
)
1376 struct smbXcli_req_state
*state
=
1377 tevent_req_data(req
,
1378 struct smbXcli_req_state
);
1380 state
->smb1
.mid
= mid
;
1383 uint32_t smb1cli_req_seqnum(struct tevent_req
*req
)
1385 struct smbXcli_req_state
*state
=
1386 tevent_req_data(req
,
1387 struct smbXcli_req_state
);
1389 return state
->smb1
.seqnum
;
1392 void smb1cli_req_set_seqnum(struct tevent_req
*req
, uint32_t seqnum
)
1394 struct smbXcli_req_state
*state
=
1395 tevent_req_data(req
,
1396 struct smbXcli_req_state
);
1398 state
->smb1
.seqnum
= seqnum
;
1401 static size_t smbXcli_iov_len(const struct iovec
*iov
, int count
)
1403 ssize_t ret
= iov_buflen(iov
, count
);
1405 /* Ignore the overflow case for now ... */
1409 static void smb1cli_req_flags(enum protocol_types protocol
,
1410 uint32_t smb1_capabilities
,
1411 uint8_t smb_command
,
1412 uint8_t additional_flags
,
1413 uint8_t clear_flags
,
1415 uint16_t additional_flags2
,
1416 uint16_t clear_flags2
,
1420 uint16_t flags2
= 0;
1422 if (protocol
>= PROTOCOL_LANMAN1
) {
1423 flags
|= FLAG_CASELESS_PATHNAMES
;
1424 flags
|= FLAG_CANONICAL_PATHNAMES
;
1427 if (protocol
>= PROTOCOL_LANMAN2
) {
1428 flags2
|= FLAGS2_LONG_PATH_COMPONENTS
;
1429 flags2
|= FLAGS2_EXTENDED_ATTRIBUTES
;
1432 if (protocol
>= PROTOCOL_NT1
) {
1433 flags2
|= FLAGS2_IS_LONG_NAME
;
1435 if (smb1_capabilities
& CAP_UNICODE
) {
1436 flags2
|= FLAGS2_UNICODE_STRINGS
;
1438 if (smb1_capabilities
& CAP_STATUS32
) {
1439 flags2
|= FLAGS2_32_BIT_ERROR_CODES
;
1441 if (smb1_capabilities
& CAP_EXTENDED_SECURITY
) {
1442 flags2
|= FLAGS2_EXTENDED_SECURITY
;
1446 flags
|= additional_flags
;
1447 flags
&= ~clear_flags
;
1448 flags2
|= additional_flags2
;
1449 flags2
&= ~clear_flags2
;
1455 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
);
1457 static bool smb1cli_req_cancel(struct tevent_req
*req
)
1459 struct smbXcli_req_state
*state
=
1460 tevent_req_data(req
,
1461 struct smbXcli_req_state
);
1466 struct tevent_req
*subreq
;
1469 flags
= CVAL(state
->smb1
.hdr
, HDR_FLG
);
1470 flags2
= SVAL(state
->smb1
.hdr
, HDR_FLG2
);
1471 pid
= SVAL(state
->smb1
.hdr
, HDR_PID
);
1472 pid
|= SVAL(state
->smb1
.hdr
, HDR_PIDHIGH
)<<16;
1473 mid
= SVAL(state
->smb1
.hdr
, HDR_MID
);
1475 subreq
= smb1cli_req_create(state
, state
->ev
,
1485 0, NULL
); /* bytes */
1486 if (subreq
== NULL
) {
1489 smb1cli_req_set_mid(subreq
, mid
);
1491 status
= smb1cli_req_chain_submit(&subreq
, 1);
1492 if (!NT_STATUS_IS_OK(status
)) {
1493 TALLOC_FREE(subreq
);
1496 smb1cli_req_set_mid(subreq
, 0);
1498 tevent_req_set_callback(subreq
, smb1cli_req_cancel_done
, NULL
);
1503 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
)
1505 /* we do not care about the result */
1506 TALLOC_FREE(subreq
);
1509 struct tevent_req
*smb1cli_req_create(TALLOC_CTX
*mem_ctx
,
1510 struct tevent_context
*ev
,
1511 struct smbXcli_conn
*conn
,
1512 uint8_t smb_command
,
1513 uint8_t additional_flags
,
1514 uint8_t clear_flags
,
1515 uint16_t additional_flags2
,
1516 uint16_t clear_flags2
,
1517 uint32_t timeout_msec
,
1519 struct smbXcli_tcon
*tcon
,
1520 struct smbXcli_session
*session
,
1521 uint8_t wct
, uint16_t *vwv
,
1523 struct iovec
*bytes_iov
)
1525 struct tevent_req
*req
;
1526 struct smbXcli_req_state
*state
;
1528 uint16_t flags2
= 0;
1533 if (iov_count
> MAX_SMB_IOV
) {
1535 * Should not happen :-)
1540 req
= tevent_req_create(mem_ctx
, &state
,
1541 struct smbXcli_req_state
);
1547 state
->session
= session
;
1551 uid
= session
->smb1
.session_id
;
1555 tid
= tcon
->smb1
.tcon_id
;
1557 if (tcon
->fs_attributes
& FILE_CASE_SENSITIVE_SEARCH
) {
1558 clear_flags
|= FLAG_CASELESS_PATHNAMES
;
1560 /* Default setting, case insensitive. */
1561 additional_flags
|= FLAG_CASELESS_PATHNAMES
;
1564 if (smbXcli_conn_dfs_supported(conn
) &&
1565 smbXcli_tcon_is_dfs_share(tcon
))
1567 additional_flags2
|= FLAGS2_DFS_PATHNAMES
;
1571 state
->smb1
.recv_cmd
= 0xFF;
1572 state
->smb1
.recv_status
= NT_STATUS_INTERNAL_ERROR
;
1573 state
->smb1
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
1574 if (state
->smb1
.recv_iov
== NULL
) {
1579 smb1cli_req_flags(conn
->protocol
,
1580 conn
->smb1
.capabilities
,
1589 SIVAL(state
->smb1
.hdr
, 0, SMB_MAGIC
);
1590 SCVAL(state
->smb1
.hdr
, HDR_COM
, smb_command
);
1591 SIVAL(state
->smb1
.hdr
, HDR_RCLS
, NT_STATUS_V(NT_STATUS_OK
));
1592 SCVAL(state
->smb1
.hdr
, HDR_FLG
, flags
);
1593 SSVAL(state
->smb1
.hdr
, HDR_FLG2
, flags2
);
1594 SSVAL(state
->smb1
.hdr
, HDR_PIDHIGH
, pid
>> 16);
1595 SSVAL(state
->smb1
.hdr
, HDR_TID
, tid
);
1596 SSVAL(state
->smb1
.hdr
, HDR_PID
, pid
);
1597 SSVAL(state
->smb1
.hdr
, HDR_UID
, uid
);
1598 SSVAL(state
->smb1
.hdr
, HDR_MID
, 0); /* this comes later */
1599 SCVAL(state
->smb1
.hdr
, HDR_WCT
, wct
);
1601 state
->smb1
.vwv
= vwv
;
1603 num_bytes
= iov_buflen(bytes_iov
, iov_count
);
1604 if (num_bytes
== -1) {
1606 * I'd love to add a check for num_bytes<=UINT16_MAX here, but
1607 * the smbclient->samba connections can lie and transfer more.
1613 SSVAL(state
->smb1
.bytecount_buf
, 0, num_bytes
);
1615 state
->smb1
.iov
[0].iov_base
= (void *)state
->length_hdr
;
1616 state
->smb1
.iov
[0].iov_len
= sizeof(state
->length_hdr
);
1617 state
->smb1
.iov
[1].iov_base
= (void *)state
->smb1
.hdr
;
1618 state
->smb1
.iov
[1].iov_len
= sizeof(state
->smb1
.hdr
);
1619 state
->smb1
.iov
[2].iov_base
= (void *)state
->smb1
.vwv
;
1620 state
->smb1
.iov
[2].iov_len
= wct
* sizeof(uint16_t);
1621 state
->smb1
.iov
[3].iov_base
= (void *)state
->smb1
.bytecount_buf
;
1622 state
->smb1
.iov
[3].iov_len
= sizeof(uint16_t);
1624 if (iov_count
!= 0) {
1625 memcpy(&state
->smb1
.iov
[4], bytes_iov
,
1626 iov_count
* sizeof(*bytes_iov
));
1628 state
->smb1
.iov_count
= iov_count
+ 4;
1630 if (timeout_msec
> 0) {
1631 state
->endtime
= timeval_current_ofs_msec(timeout_msec
);
1632 if (!tevent_req_set_endtime(req
, ev
, state
->endtime
)) {
1637 switch (smb_command
) {
1641 state
->one_way
= true;
1644 state
->one_way
= true;
1645 state
->smb1
.one_way_seqnum
= true;
1649 (CVAL(vwv
+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE
)) {
1650 state
->one_way
= true;
1658 static NTSTATUS
smb1cli_conn_signv(struct smbXcli_conn
*conn
,
1659 struct iovec
*iov
, int iov_count
,
1661 bool one_way_seqnum
)
1663 TALLOC_CTX
*frame
= NULL
;
1668 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1669 * iovec directly. MD5Update would do that just fine.
1672 if (iov_count
< 4) {
1673 return NT_STATUS_INVALID_PARAMETER_MIX
;
1675 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1676 return NT_STATUS_INVALID_PARAMETER_MIX
;
1678 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1679 return NT_STATUS_INVALID_PARAMETER_MIX
;
1681 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1682 return NT_STATUS_INVALID_PARAMETER_MIX
;
1684 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1685 return NT_STATUS_INVALID_PARAMETER_MIX
;
1688 frame
= talloc_stackframe();
1690 buf
= iov_concat(frame
, &iov
[1], iov_count
- 1);
1692 return NT_STATUS_NO_MEMORY
;
1695 *seqnum
= smb1_signing_next_seqnum(conn
->smb1
.signing
,
1697 status
= smb1_signing_sign_pdu(conn
->smb1
.signing
,
1699 talloc_get_size(buf
),
1701 if (!NT_STATUS_IS_OK(status
)) {
1704 memcpy(iov
[1].iov_base
, buf
, iov
[1].iov_len
);
1707 return NT_STATUS_OK
;
1710 static void smb1cli_req_writev_done(struct tevent_req
*subreq
);
1711 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1712 TALLOC_CTX
*tmp_mem
,
1715 static NTSTATUS
smb1cli_req_writev_submit(struct tevent_req
*req
,
1716 struct smbXcli_req_state
*state
,
1717 struct iovec
*iov
, int iov_count
)
1719 struct tevent_req
*subreq
;
1725 if (!smbXcli_conn_is_connected(state
->conn
)) {
1726 return NT_STATUS_CONNECTION_DISCONNECTED
;
1729 if (state
->conn
->protocol
> PROTOCOL_NT1
) {
1730 DBG_ERR("called for dialect[%s] server[%s]\n",
1731 smb_protocol_types_string(state
->conn
->protocol
),
1732 smbXcli_conn_remote_name(state
->conn
));
1733 return NT_STATUS_REVISION_MISMATCH
;
1736 if (iov_count
< 4) {
1737 return NT_STATUS_INVALID_PARAMETER_MIX
;
1739 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1740 return NT_STATUS_INVALID_PARAMETER_MIX
;
1742 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1743 return NT_STATUS_INVALID_PARAMETER_MIX
;
1745 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1746 return NT_STATUS_INVALID_PARAMETER_MIX
;
1748 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1749 return NT_STATUS_INVALID_PARAMETER_MIX
;
1752 cmd
= CVAL(iov
[1].iov_base
, HDR_COM
);
1753 if (cmd
== SMBreadBraw
) {
1754 if (smbXcli_conn_has_async_calls(state
->conn
)) {
1755 return NT_STATUS_INVALID_PARAMETER_MIX
;
1757 state
->conn
->smb1
.read_braw_req
= req
;
1760 if (state
->smb1
.mid
!= 0) {
1761 mid
= state
->smb1
.mid
;
1763 mid
= smb1cli_alloc_mid(state
->conn
);
1765 SSVAL(iov
[1].iov_base
, HDR_MID
, mid
);
1767 nbtlen
= iov_buflen(&iov
[1], iov_count
-1);
1768 if ((nbtlen
== -1) || (nbtlen
> 0x1FFFF)) {
1769 return NT_STATUS_INVALID_PARAMETER_MIX
;
1772 _smb_setlen_nbt(iov
[0].iov_base
, nbtlen
);
1774 status
= smb1cli_conn_signv(state
->conn
, iov
, iov_count
,
1775 &state
->smb1
.seqnum
,
1776 state
->smb1
.one_way_seqnum
);
1778 if (!NT_STATUS_IS_OK(status
)) {
1783 * If we supported multiple encryption contexts
1784 * here we'd look up based on tid.
1786 if (common_encryption_on(state
->conn
->smb1
.trans_enc
)) {
1787 char *buf
, *enc_buf
;
1789 buf
= (char *)iov_concat(talloc_tos(), iov
, iov_count
);
1791 return NT_STATUS_NO_MEMORY
;
1793 status
= common_encrypt_buffer(state
->conn
->smb1
.trans_enc
,
1794 (char *)buf
, &enc_buf
);
1796 if (!NT_STATUS_IS_OK(status
)) {
1797 DEBUG(0, ("Error in encrypting client message: %s\n",
1798 nt_errstr(status
)));
1801 buf
= (char *)talloc_memdup(state
, enc_buf
,
1802 smb_len_nbt(enc_buf
)+4);
1805 return NT_STATUS_NO_MEMORY
;
1807 iov
[0].iov_base
= (void *)buf
;
1808 iov
[0].iov_len
= talloc_get_size(buf
);
1812 if (state
->conn
->dispatch_incoming
== NULL
) {
1813 state
->conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
1816 if (!smbXcli_req_set_pending(req
)) {
1817 return NT_STATUS_NO_MEMORY
;
1820 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
1822 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
1823 state
->conn
->sock_fd
, false, iov
, iov_count
);
1824 if (subreq
== NULL
) {
1825 return NT_STATUS_NO_MEMORY
;
1827 tevent_req_set_callback(subreq
, smb1cli_req_writev_done
, req
);
1828 state
->write_req
= subreq
;
1830 return NT_STATUS_OK
;
1833 struct tevent_req
*smb1cli_req_send(TALLOC_CTX
*mem_ctx
,
1834 struct tevent_context
*ev
,
1835 struct smbXcli_conn
*conn
,
1836 uint8_t smb_command
,
1837 uint8_t additional_flags
,
1838 uint8_t clear_flags
,
1839 uint16_t additional_flags2
,
1840 uint16_t clear_flags2
,
1841 uint32_t timeout_msec
,
1843 struct smbXcli_tcon
*tcon
,
1844 struct smbXcli_session
*session
,
1845 uint8_t wct
, uint16_t *vwv
,
1847 const uint8_t *bytes
)
1849 struct tevent_req
*req
;
1853 iov
.iov_base
= discard_const_p(void, bytes
);
1854 iov
.iov_len
= num_bytes
;
1856 req
= smb1cli_req_create(mem_ctx
, ev
, conn
, smb_command
,
1857 additional_flags
, clear_flags
,
1858 additional_flags2
, clear_flags2
,
1865 if (!tevent_req_is_in_progress(req
)) {
1866 return tevent_req_post(req
, ev
);
1868 status
= smb1cli_req_chain_submit(&req
, 1);
1869 if (tevent_req_nterror(req
, status
)) {
1870 return tevent_req_post(req
, ev
);
1875 static void smb1cli_req_writev_done(struct tevent_req
*subreq
)
1877 struct tevent_req
*req
=
1878 tevent_req_callback_data(subreq
,
1880 struct smbXcli_req_state
*state
=
1881 tevent_req_data(req
,
1882 struct smbXcli_req_state
);
1886 state
->write_req
= NULL
;
1888 nwritten
= writev_recv(subreq
, &err
);
1889 TALLOC_FREE(subreq
);
1890 if (nwritten
== -1) {
1891 /* here, we need to notify all pending requests */
1892 NTSTATUS status
= map_nt_error_from_unix_common(err
);
1893 smbXcli_conn_disconnect(state
->conn
, status
);
1897 if (state
->one_way
) {
1898 state
->inbuf
= NULL
;
1899 tevent_req_done(req
);
1904 static void smbXcli_conn_received(struct tevent_req
*subreq
)
1906 struct smbXcli_conn
*conn
=
1907 tevent_req_callback_data(subreq
,
1908 struct smbXcli_conn
);
1909 TALLOC_CTX
*frame
= talloc_stackframe();
1915 if (subreq
!= conn
->read_smb_req
) {
1916 DEBUG(1, ("Internal error: cli_smb_received called with "
1917 "unexpected subreq\n"));
1918 smbXcli_conn_disconnect(conn
, NT_STATUS_INTERNAL_ERROR
);
1922 conn
->read_smb_req
= NULL
;
1924 received
= read_smb_recv(subreq
, frame
, &inbuf
, &err
);
1925 TALLOC_FREE(subreq
);
1926 if (received
== -1) {
1927 status
= map_nt_error_from_unix_common(err
);
1928 smbXcli_conn_disconnect(conn
, status
);
1933 status
= conn
->dispatch_incoming(conn
, frame
, inbuf
);
1935 if (NT_STATUS_IS_OK(status
)) {
1937 * We should not do any more processing
1938 * as the dispatch function called
1939 * tevent_req_done().
1944 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1946 * We got an error, so notify all pending requests
1948 smbXcli_conn_disconnect(conn
, status
);
1953 * We got NT_STATUS_RETRY, so we may ask for a
1954 * next incoming pdu.
1956 if (!smbXcli_conn_receive_next(conn
)) {
1957 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
1961 static NTSTATUS
smb1cli_inbuf_parse_chain(uint8_t *buf
, TALLOC_CTX
*mem_ctx
,
1962 struct iovec
**piov
, int *pnum_iov
)
1973 size_t min_size
= MIN_SMB_SIZE
;
1975 buflen
= smb_len_tcp(buf
);
1978 hdr
= buf
+ NBT_HDR_SIZE
;
1980 status
= smb1cli_pull_raw_error(hdr
);
1981 if (NT_STATUS_IS_ERR(status
)) {
1983 * This is an ugly hack to support OS/2
1984 * which skips the byte_count in the DATA block
1985 * on some error responses.
1989 min_size
-= sizeof(uint16_t);
1992 if (buflen
< min_size
) {
1993 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1997 * This returns iovec elements in the following order:
2012 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
2014 return NT_STATUS_NO_MEMORY
;
2016 iov
[0].iov_base
= hdr
;
2017 iov
[0].iov_len
= HDR_WCT
;
2020 cmd
= CVAL(hdr
, HDR_COM
);
2024 size_t len
= buflen
- taken
;
2026 struct iovec
*iov_tmp
;
2033 * we need at least WCT
2035 needed
= sizeof(uint8_t);
2037 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2038 __location__
, (int)len
, (int)needed
));
2043 * Now we check if the specified words are there
2045 wct
= CVAL(hdr
, wct_ofs
);
2046 needed
+= wct
* sizeof(uint16_t);
2048 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2049 __location__
, (int)len
, (int)needed
));
2053 if ((num_iov
== 1) &&
2055 NT_STATUS_IS_ERR(status
))
2058 * This is an ugly hack to support OS/2
2059 * which skips the byte_count in the DATA block
2060 * on some error responses.
2064 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
2066 if (iov_tmp
== NULL
) {
2068 return NT_STATUS_NO_MEMORY
;
2071 cur
= &iov
[num_iov
];
2075 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
2077 cur
[1].iov_base
= cur
[0].iov_base
;
2084 * we need at least BCC
2086 needed
+= sizeof(uint16_t);
2088 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2089 __location__
, (int)len
, (int)needed
));
2094 * Now we check if the specified bytes are there
2096 bcc_ofs
= wct_ofs
+ sizeof(uint8_t) + wct
* sizeof(uint16_t);
2097 bcc
= SVAL(hdr
, bcc_ofs
);
2098 needed
+= bcc
* sizeof(uint8_t);
2100 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2101 __location__
, (int)len
, (int)needed
));
2106 * we allocate 2 iovec structures for words and bytes
2108 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
2110 if (iov_tmp
== NULL
) {
2112 return NT_STATUS_NO_MEMORY
;
2115 cur
= &iov
[num_iov
];
2118 cur
[0].iov_len
= wct
* sizeof(uint16_t);
2119 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
2120 cur
[1].iov_len
= bcc
* sizeof(uint8_t);
2121 cur
[1].iov_base
= hdr
+ (bcc_ofs
+ sizeof(uint16_t));
2125 if (!smb1cli_is_andx_req(cmd
)) {
2127 * If the current command does not have AndX chanining
2133 if (wct
== 0 && bcc
== 0) {
2135 * An empty response also ends the chain,
2136 * most likely with an error.
2142 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
2143 __location__
, (int)wct
, (int)cmd
));
2146 cmd
= CVAL(cur
[0].iov_base
, 0);
2149 * If it is the end of the chain we are also done.
2153 wct_ofs
= SVAL(cur
[0].iov_base
, 2);
2155 if (wct_ofs
< taken
) {
2158 if (wct_ofs
> buflen
) {
2163 * we consumed everything up to the start of the next
2169 remaining
= buflen
- taken
;
2171 if (remaining
> 0 && num_iov
>= 3) {
2173 * The last DATA block gets the remaining
2174 * bytes, this is needed to support
2175 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
2177 iov
[num_iov
-1].iov_len
+= remaining
;
2181 *pnum_iov
= num_iov
;
2182 return NT_STATUS_OK
;
2186 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2189 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
2190 TALLOC_CTX
*tmp_mem
,
2193 struct tevent_req
*req
;
2194 struct smbXcli_req_state
*state
;
2201 uint8_t *inhdr
= inbuf
+ NBT_HDR_SIZE
;
2202 size_t len
= smb_len_tcp(inbuf
);
2203 struct iovec
*iov
= NULL
;
2205 struct tevent_req
**chain
= NULL
;
2206 size_t num_chained
= 0;
2207 size_t num_responses
= 0;
2209 if (conn
->smb1
.read_braw_req
!= NULL
) {
2210 req
= conn
->smb1
.read_braw_req
;
2211 conn
->smb1
.read_braw_req
= NULL
;
2212 state
= tevent_req_data(req
, struct smbXcli_req_state
);
2214 smbXcli_req_unset_pending(req
);
2216 if (state
->smb1
.recv_iov
== NULL
) {
2218 * For requests with more than
2219 * one response, we have to readd the
2222 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2225 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2226 return NT_STATUS_OK
;
2230 state
->smb1
.recv_iov
[0].iov_base
= (void *)(inhdr
);
2231 state
->smb1
.recv_iov
[0].iov_len
= len
;
2232 ZERO_STRUCT(state
->smb1
.recv_iov
[1]);
2233 ZERO_STRUCT(state
->smb1
.recv_iov
[2]);
2235 state
->smb1
.recv_cmd
= SMBreadBraw
;
2236 state
->smb1
.recv_status
= NT_STATUS_OK
;
2237 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
2239 tevent_req_done(req
);
2240 return NT_STATUS_OK
;
2243 if ((IVAL(inhdr
, 0) != SMB_MAGIC
) /* 0xFF"SMB" */
2244 && (SVAL(inhdr
, 0) != 0x45ff)) /* 0xFF"E" */ {
2245 DEBUG(10, ("Got non-SMB PDU\n"));
2246 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2250 * If we supported multiple encryption contexts
2251 * here we'd look up based on tid.
2253 if (common_encryption_on(conn
->smb1
.trans_enc
)
2254 && (CVAL(inbuf
, 0) == 0)) {
2255 uint16_t enc_ctx_num
;
2257 status
= get_enc_ctx_num(inbuf
, &enc_ctx_num
);
2258 if (!NT_STATUS_IS_OK(status
)) {
2259 DEBUG(10, ("get_enc_ctx_num returned %s\n",
2260 nt_errstr(status
)));
2264 if (enc_ctx_num
!= conn
->smb1
.trans_enc
->enc_ctx_num
) {
2265 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
2267 conn
->smb1
.trans_enc
->enc_ctx_num
));
2268 return NT_STATUS_INVALID_HANDLE
;
2271 status
= common_decrypt_buffer(conn
->smb1
.trans_enc
,
2273 if (!NT_STATUS_IS_OK(status
)) {
2274 DEBUG(10, ("common_decrypt_buffer returned %s\n",
2275 nt_errstr(status
)));
2278 inhdr
= inbuf
+ NBT_HDR_SIZE
;
2279 len
= smb_len_nbt(inbuf
);
2282 mid
= SVAL(inhdr
, HDR_MID
);
2283 num_pending
= talloc_array_length(conn
->pending
);
2285 for (i
=0; i
<num_pending
; i
++) {
2286 if (mid
== smb1cli_req_mid(conn
->pending
[i
])) {
2290 if (i
== num_pending
) {
2291 /* Dump unexpected reply */
2292 return NT_STATUS_RETRY
;
2295 oplock_break
= false;
2297 if (mid
== 0xffff) {
2299 * Paranoia checks that this is really an oplock break request.
2301 oplock_break
= (len
== 51); /* hdr + 8 words */
2302 oplock_break
&= ((CVAL(inhdr
, HDR_FLG
) & FLAG_REPLY
) == 0);
2303 oplock_break
&= (CVAL(inhdr
, HDR_COM
) == SMBlockingX
);
2304 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(6)) == 0);
2305 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(7)) == 0);
2307 if (!oplock_break
) {
2308 /* Dump unexpected reply */
2309 return NT_STATUS_RETRY
;
2313 req
= conn
->pending
[i
];
2314 state
= tevent_req_data(req
, struct smbXcli_req_state
);
2316 if (!oplock_break
/* oplock breaks are not signed */
2317 && !smb1_signing_check_pdu(conn
->smb1
.signing
,
2318 inhdr
, len
, state
->smb1
.seqnum
+1)) {
2319 DEBUG(10, ("cli_check_sign_mac failed\n"));
2320 return NT_STATUS_ACCESS_DENIED
;
2323 status
= smb1cli_inbuf_parse_chain(inbuf
, tmp_mem
,
2325 if (!NT_STATUS_IS_OK(status
)) {
2326 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2327 nt_errstr(status
)));
2331 cmd
= CVAL(inhdr
, HDR_COM
);
2332 status
= smb1cli_pull_raw_error(inhdr
);
2334 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
) &&
2335 (state
->session
!= NULL
) && state
->session
->disconnect_expired
)
2338 * this should be a short term hack
2339 * until the upper layers have implemented
2340 * re-authentication.
2345 if (state
->smb1
.chained_requests
== NULL
) {
2347 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2350 smbXcli_req_unset_pending(req
);
2352 if (state
->smb1
.recv_iov
== NULL
) {
2354 * For requests with more than
2355 * one response, we have to readd the
2358 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2361 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2362 return NT_STATUS_OK
;
2366 state
->smb1
.recv_cmd
= cmd
;
2367 state
->smb1
.recv_status
= status
;
2368 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
2370 state
->smb1
.recv_iov
[0] = iov
[0];
2371 state
->smb1
.recv_iov
[1] = iov
[1];
2372 state
->smb1
.recv_iov
[2] = iov
[2];
2374 if (talloc_array_length(conn
->pending
) == 0) {
2375 tevent_req_done(req
);
2376 return NT_STATUS_OK
;
2379 tevent_req_defer_callback(req
, state
->ev
);
2380 tevent_req_done(req
);
2381 return NT_STATUS_RETRY
;
2384 chain
= talloc_move(tmp_mem
, &state
->smb1
.chained_requests
);
2385 num_chained
= talloc_array_length(chain
);
2386 num_responses
= (num_iov
- 1)/2;
2388 if (num_responses
> num_chained
) {
2389 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2392 for (i
=0; i
<num_chained
; i
++) {
2393 size_t iov_idx
= 1 + (i
*2);
2394 struct iovec
*cur
= &iov
[iov_idx
];
2398 state
= tevent_req_data(req
, struct smbXcli_req_state
);
2400 smbXcli_req_unset_pending(req
);
2403 * as we finish multiple requests here
2404 * we need to defer the callbacks as
2405 * they could destroy our current stack state.
2407 tevent_req_defer_callback(req
, state
->ev
);
2409 if (i
>= num_responses
) {
2410 tevent_req_nterror(req
, NT_STATUS_REQUEST_ABORTED
);
2414 if (state
->smb1
.recv_iov
== NULL
) {
2416 * For requests with more than
2417 * one response, we have to readd the
2420 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2423 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2428 state
->smb1
.recv_cmd
= cmd
;
2430 if (i
== (num_responses
- 1)) {
2432 * The last request in the chain gets the status
2434 state
->smb1
.recv_status
= status
;
2436 cmd
= CVAL(cur
[0].iov_base
, 0);
2437 state
->smb1
.recv_status
= NT_STATUS_OK
;
2440 state
->inbuf
= inbuf
;
2443 * Note: here we use talloc_reference() in a way
2444 * that does not expose it to the caller.
2446 inbuf_ref
= talloc_reference(state
->smb1
.recv_iov
, inbuf
);
2447 if (tevent_req_nomem(inbuf_ref
, req
)) {
2451 /* copy the related buffers */
2452 state
->smb1
.recv_iov
[0] = iov
[0];
2453 state
->smb1
.recv_iov
[1] = cur
[0];
2454 state
->smb1
.recv_iov
[2] = cur
[1];
2456 tevent_req_done(req
);
2459 return NT_STATUS_RETRY
;
2462 NTSTATUS
smb1cli_req_recv(struct tevent_req
*req
,
2463 TALLOC_CTX
*mem_ctx
,
2464 struct iovec
**piov
,
2468 uint32_t *pvwv_offset
,
2469 uint32_t *pnum_bytes
,
2471 uint32_t *pbytes_offset
,
2473 const struct smb1cli_req_expected_response
*expected
,
2474 size_t num_expected
)
2476 struct smbXcli_req_state
*state
=
2477 tevent_req_data(req
,
2478 struct smbXcli_req_state
);
2479 NTSTATUS status
= NT_STATUS_OK
;
2480 struct iovec
*recv_iov
= NULL
;
2481 uint8_t *hdr
= NULL
;
2483 uint32_t vwv_offset
= 0;
2484 uint16_t *vwv
= NULL
;
2485 uint32_t num_bytes
= 0;
2486 uint32_t bytes_offset
= 0;
2487 uint8_t *bytes
= NULL
;
2489 bool found_status
= false;
2490 bool found_size
= false;
2504 if (pvwv_offset
!= NULL
) {
2507 if (pnum_bytes
!= NULL
) {
2510 if (pbytes
!= NULL
) {
2513 if (pbytes_offset
!= NULL
) {
2516 if (pinbuf
!= NULL
) {
2520 if (state
->inbuf
!= NULL
) {
2521 recv_iov
= state
->smb1
.recv_iov
;
2522 state
->smb1
.recv_iov
= NULL
;
2523 if (state
->smb1
.recv_cmd
!= SMBreadBraw
) {
2524 hdr
= (uint8_t *)recv_iov
[0].iov_base
;
2525 wct
= recv_iov
[1].iov_len
/2;
2526 vwv
= (uint16_t *)recv_iov
[1].iov_base
;
2527 vwv_offset
= PTR_DIFF(vwv
, hdr
);
2528 num_bytes
= recv_iov
[2].iov_len
;
2529 bytes
= (uint8_t *)recv_iov
[2].iov_base
;
2530 bytes_offset
= PTR_DIFF(bytes
, hdr
);
2534 if (tevent_req_is_nterror(req
, &status
)) {
2535 for (i
=0; i
< num_expected
; i
++) {
2536 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2537 found_status
= true;
2543 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
2549 if (num_expected
== 0) {
2550 found_status
= true;
2554 status
= state
->smb1
.recv_status
;
2556 for (i
=0; i
< num_expected
; i
++) {
2557 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2561 found_status
= true;
2562 if (expected
[i
].wct
== 0) {
2567 if (expected
[i
].wct
== wct
) {
2573 if (!found_status
) {
2578 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2582 *piov
= talloc_move(mem_ctx
, &recv_iov
);
2594 if (pvwv_offset
!= NULL
) {
2595 *pvwv_offset
= vwv_offset
;
2597 if (pnum_bytes
!= NULL
) {
2598 *pnum_bytes
= num_bytes
;
2600 if (pbytes
!= NULL
) {
2603 if (pbytes_offset
!= NULL
) {
2604 *pbytes_offset
= bytes_offset
;
2606 if (pinbuf
!= NULL
) {
2607 *pinbuf
= state
->inbuf
;
2613 size_t smb1cli_req_wct_ofs(struct tevent_req
**reqs
, int num_reqs
)
2620 for (i
=0; i
<num_reqs
; i
++) {
2621 struct smbXcli_req_state
*state
;
2622 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2623 wct_ofs
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2624 state
->smb1
.iov_count
-2);
2625 wct_ofs
= (wct_ofs
+ 3) & ~3;
2630 NTSTATUS
smb1cli_req_chain_submit(struct tevent_req
**reqs
, int num_reqs
)
2632 struct smbXcli_req_state
*first_state
=
2633 tevent_req_data(reqs
[0],
2634 struct smbXcli_req_state
);
2635 struct smbXcli_req_state
*state
;
2637 size_t chain_padding
= 0;
2639 struct iovec
*iov
= NULL
;
2640 struct iovec
*this_iov
;
2644 if (num_reqs
== 1) {
2645 return smb1cli_req_writev_submit(reqs
[0], first_state
,
2646 first_state
->smb1
.iov
,
2647 first_state
->smb1
.iov_count
);
2651 for (i
=0; i
<num_reqs
; i
++) {
2652 if (!tevent_req_is_in_progress(reqs
[i
])) {
2653 return NT_STATUS_INTERNAL_ERROR
;
2656 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2658 if (state
->smb1
.iov_count
< 4) {
2659 return NT_STATUS_INVALID_PARAMETER_MIX
;
2664 * The NBT and SMB header
2677 iovlen
+= state
->smb1
.iov_count
- 2;
2680 iov
= talloc_zero_array(first_state
, struct iovec
, iovlen
);
2682 return NT_STATUS_NO_MEMORY
;
2685 first_state
->smb1
.chained_requests
= (struct tevent_req
**)talloc_memdup(
2686 first_state
, reqs
, sizeof(*reqs
) * num_reqs
);
2687 if (first_state
->smb1
.chained_requests
== NULL
) {
2689 return NT_STATUS_NO_MEMORY
;
2692 wct_offset
= HDR_WCT
;
2695 for (i
=0; i
<num_reqs
; i
++) {
2696 size_t next_padding
= 0;
2699 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2701 if (i
< num_reqs
-1) {
2702 if (!smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))
2703 || CVAL(state
->smb1
.hdr
, HDR_WCT
) < 2) {
2705 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2706 return NT_STATUS_INVALID_PARAMETER_MIX
;
2710 wct_offset
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2711 state
->smb1
.iov_count
-2) + 1;
2712 if ((wct_offset
% 4) != 0) {
2713 next_padding
= 4 - (wct_offset
% 4);
2715 wct_offset
+= next_padding
;
2716 vwv
= state
->smb1
.vwv
;
2718 if (i
< num_reqs
-1) {
2719 struct smbXcli_req_state
*next_state
=
2720 tevent_req_data(reqs
[i
+1],
2721 struct smbXcli_req_state
);
2722 SCVAL(vwv
+0, 0, CVAL(next_state
->smb1
.hdr
, HDR_COM
));
2724 SSVAL(vwv
+1, 0, wct_offset
);
2725 } else if (smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))) {
2726 /* properly end the chain */
2727 SCVAL(vwv
+0, 0, 0xff);
2728 SCVAL(vwv
+0, 1, 0xff);
2734 * The NBT and SMB header
2736 this_iov
[0] = state
->smb1
.iov
[0];
2737 this_iov
[1] = state
->smb1
.iov
[1];
2741 * This one is a bit subtle. We have to add
2742 * chain_padding bytes between the requests, and we
2743 * have to also include the wct field of the
2744 * subsequent requests. We use the subsequent header
2745 * for the padding, it contains the wct field in its
2748 this_iov
[0].iov_len
= chain_padding
+1;
2749 this_iov
[0].iov_base
= (void *)&state
->smb1
.hdr
[
2750 sizeof(state
->smb1
.hdr
) - this_iov
[0].iov_len
];
2751 memset(this_iov
[0].iov_base
, 0, this_iov
[0].iov_len
-1);
2756 * copy the words and bytes
2758 memcpy(this_iov
, state
->smb1
.iov
+2,
2759 sizeof(struct iovec
) * (state
->smb1
.iov_count
-2));
2760 this_iov
+= state
->smb1
.iov_count
- 2;
2761 chain_padding
= next_padding
;
2764 nbt_len
= iov_buflen(&iov
[1], iovlen
-1);
2765 if ((nbt_len
== -1) || (nbt_len
> first_state
->conn
->smb1
.max_xmit
)) {
2767 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2768 return NT_STATUS_INVALID_PARAMETER_MIX
;
2771 status
= smb1cli_req_writev_submit(reqs
[0], first_state
, iov
, iovlen
);
2772 if (!NT_STATUS_IS_OK(status
)) {
2774 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2778 return NT_STATUS_OK
;
2781 struct tevent_queue
*smbXcli_conn_send_queue(struct smbXcli_conn
*conn
)
2783 return conn
->outgoing
;
2786 bool smbXcli_conn_has_async_calls(struct smbXcli_conn
*conn
)
2788 return ((tevent_queue_length(conn
->outgoing
) != 0)
2789 || (talloc_array_length(conn
->pending
) != 0));
2792 bool smbXcli_conn_dfs_supported(struct smbXcli_conn
*conn
)
2794 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
2795 return (smb2cli_conn_server_capabilities(conn
) & SMB2_CAP_DFS
);
2798 return (smb1cli_conn_capabilities(conn
) & CAP_DFS
);
2801 bool smb2cli_conn_req_possible(struct smbXcli_conn
*conn
, uint32_t *max_dyn_len
)
2803 uint16_t credits
= 1;
2805 if (conn
->smb2
.cur_credits
== 0) {
2806 if (max_dyn_len
!= NULL
) {
2812 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2813 credits
= conn
->smb2
.cur_credits
;
2816 if (max_dyn_len
!= NULL
) {
2817 *max_dyn_len
= credits
* 65536;
2823 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn
*conn
)
2825 return conn
->smb2
.server
.capabilities
;
2828 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
2830 return conn
->smb2
.server
.security_mode
;
2833 uint16_t smb2cli_conn_server_signing_algo(struct smbXcli_conn
*conn
)
2835 return conn
->smb2
.server
.sign_algo
;
2838 uint16_t smb2cli_conn_server_encryption_algo(struct smbXcli_conn
*conn
)
2840 return conn
->smb2
.server
.cipher
;
2843 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn
*conn
)
2845 return conn
->smb2
.server
.max_trans_size
;
2848 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn
*conn
)
2850 return conn
->smb2
.server
.max_read_size
;
2853 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn
*conn
)
2855 return conn
->smb2
.server
.max_write_size
;
2858 void smb2cli_conn_set_max_credits(struct smbXcli_conn
*conn
,
2859 uint16_t max_credits
)
2861 conn
->smb2
.max_credits
= max_credits
;
2864 uint16_t smb2cli_conn_get_cur_credits(struct smbXcli_conn
*conn
)
2866 return conn
->smb2
.cur_credits
;
2869 uint8_t smb2cli_conn_get_io_priority(struct smbXcli_conn
*conn
)
2871 if (conn
->protocol
< PROTOCOL_SMB3_11
) {
2875 return conn
->smb2
.io_priority
;
2878 void smb2cli_conn_set_io_priority(struct smbXcli_conn
*conn
,
2879 uint8_t io_priority
)
2881 conn
->smb2
.io_priority
= io_priority
;
2884 uint32_t smb2cli_conn_cc_chunk_len(struct smbXcli_conn
*conn
)
2886 return conn
->smb2
.cc_chunk_len
;
2889 void smb2cli_conn_set_cc_chunk_len(struct smbXcli_conn
*conn
,
2892 conn
->smb2
.cc_chunk_len
= chunk_len
;
2895 uint32_t smb2cli_conn_cc_max_chunks(struct smbXcli_conn
*conn
)
2897 return conn
->smb2
.cc_max_chunks
;
2900 void smb2cli_conn_set_cc_max_chunks(struct smbXcli_conn
*conn
,
2901 uint32_t max_chunks
)
2903 conn
->smb2
.cc_max_chunks
= max_chunks
;
2906 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
);
2908 static bool smb2cli_req_cancel(struct tevent_req
*req
)
2910 struct smbXcli_req_state
*state
=
2911 tevent_req_data(req
,
2912 struct smbXcli_req_state
);
2913 struct smbXcli_tcon
*tcon
= state
->tcon
;
2914 struct smbXcli_session
*session
= state
->session
;
2915 uint8_t *fixed
= state
->smb2
.pad
;
2916 uint16_t fixed_len
= 4;
2917 struct tevent_req
*subreq
;
2918 struct smbXcli_req_state
*substate
;
2921 if (state
->smb2
.cancel_mid
== UINT64_MAX
) {
2923 * We already send a cancel,
2924 * make sure we don't do it
2925 * twice, otherwise we may
2926 * expose the same NONCE for
2927 * AES-128-GMAC signing
2932 SSVAL(fixed
, 0, 0x04);
2935 subreq
= smb2cli_req_create(state
, state
->ev
,
2943 if (subreq
== NULL
) {
2946 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
2948 substate
->smb2
.cancel_mid
= BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
);
2950 SIVAL(substate
->smb2
.hdr
, SMB2_HDR_FLAGS
, state
->smb2
.cancel_flags
);
2951 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, state
->smb2
.cancel_mid
);
2952 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, state
->smb2
.cancel_aid
);
2955 * remember that we don't send a cancel again
2957 state
->smb2
.cancel_mid
= UINT64_MAX
;
2959 status
= smb2cli_req_compound_submit(&subreq
, 1);
2960 if (!NT_STATUS_IS_OK(status
)) {
2961 TALLOC_FREE(subreq
);
2965 tevent_req_set_callback(subreq
, smb2cli_req_cancel_done
, NULL
);
2970 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
)
2972 /* we do not care about the result */
2973 TALLOC_FREE(subreq
);
2976 struct timeval
smbXcli_req_endtime(struct tevent_req
*req
)
2978 struct smbXcli_req_state
*state
= tevent_req_data(
2979 req
, struct smbXcli_req_state
);
2981 return state
->endtime
;
2984 struct tevent_req
*smb2cli_req_create(TALLOC_CTX
*mem_ctx
,
2985 struct tevent_context
*ev
,
2986 struct smbXcli_conn
*conn
,
2988 uint32_t additional_flags
,
2989 uint32_t clear_flags
,
2990 uint32_t timeout_msec
,
2991 struct smbXcli_tcon
*tcon
,
2992 struct smbXcli_session
*session
,
2993 const uint8_t *fixed
,
2997 uint32_t max_dyn_len
)
2999 struct tevent_req
*req
;
3000 struct smbXcli_req_state
*state
;
3004 bool use_channel_sequence
= conn
->smb2
.force_channel_sequence
;
3005 uint16_t channel_sequence
= 0;
3006 bool use_replay_flag
= false;
3007 enum protocol_types proto
= smbXcli_conn_protocol(conn
);
3009 req
= tevent_req_create(mem_ctx
, &state
,
3010 struct smbXcli_req_state
);
3015 if ((proto
> PROTOCOL_NONE
) && (proto
< PROTOCOL_SMB2_02
)) {
3016 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3022 state
->session
= session
;
3025 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_PERSISTENT_HANDLES
) {
3026 use_channel_sequence
= true;
3027 } else if (conn
->smb2
.server
.capabilities
& SMB2_CAP_MULTI_CHANNEL
) {
3028 use_channel_sequence
= true;
3031 if (smbXcli_conn_protocol(conn
) >= PROTOCOL_SMB3_00
) {
3032 use_replay_flag
= true;
3035 if (smbXcli_conn_protocol(conn
) >= PROTOCOL_SMB3_11
) {
3036 flags
|= SMB2_PRIORITY_VALUE_TO_MASK(conn
->smb2
.io_priority
);
3040 uid
= session
->smb2
->session_id
;
3042 if (use_channel_sequence
) {
3043 channel_sequence
= session
->smb2
->channel_sequence
;
3046 if (use_replay_flag
&& session
->smb2
->replay_active
) {
3047 additional_flags
|= SMB2_HDR_FLAG_REPLAY_OPERATION
;
3050 state
->smb2
.should_sign
= session
->smb2
->should_sign
;
3051 state
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
3052 state
->smb2
.require_signed_response
=
3053 session
->smb2
->require_signed_response
;
3055 if (cmd
== SMB2_OP_SESSSETUP
&&
3056 !smb2_signing_key_valid(session
->smb2_channel
.signing_key
) &&
3057 smb2_signing_key_valid(session
->smb2
->signing_key
))
3060 * a session bind needs to be signed
3062 state
->smb2
.should_sign
= true;
3065 if (cmd
== SMB2_OP_SESSSETUP
&&
3066 !smb2_signing_key_valid(session
->smb2_channel
.signing_key
)) {
3067 state
->smb2
.should_encrypt
= false;
3070 if (additional_flags
& SMB2_HDR_FLAG_SIGNED
) {
3071 if (!smb2_signing_key_valid(session
->smb2_channel
.signing_key
)) {
3072 tevent_req_nterror(req
, NT_STATUS_NO_USER_SESSION_KEY
);
3076 additional_flags
&= ~SMB2_HDR_FLAG_SIGNED
;
3077 state
->smb2
.should_sign
= true;
3082 tid
= tcon
->smb2
.tcon_id
;
3084 if (tcon
->smb2
.should_sign
) {
3085 state
->smb2
.should_sign
= true;
3087 if (tcon
->smb2
.should_encrypt
) {
3088 state
->smb2
.should_encrypt
= true;
3092 if (state
->smb2
.should_encrypt
) {
3093 state
->smb2
.should_sign
= false;
3096 state
->smb2
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
3097 if (tevent_req_nomem(state
->smb2
.recv_iov
, req
)) {
3101 flags
|= additional_flags
;
3102 flags
&= ~clear_flags
;
3104 state
->smb2
.fixed
= fixed
;
3105 state
->smb2
.fixed_len
= fixed_len
;
3106 state
->smb2
.dyn
= dyn
;
3107 state
->smb2
.dyn_len
= dyn_len
;
3108 state
->smb2
.max_dyn_len
= max_dyn_len
;
3110 if (state
->smb2
.should_encrypt
) {
3111 SIVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
3112 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
, uid
);
3115 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PROTOCOL_ID
, SMB2_MAGIC
);
3116 SSVAL(state
->smb2
.hdr
, SMB2_HDR_LENGTH
, SMB2_HDR_BODY
);
3117 SSVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
, cmd
);
3118 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CHANNEL_SEQUENCE
, channel_sequence
);
3119 SIVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
3120 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PID
, 0); /* reserved */
3121 SIVAL(state
->smb2
.hdr
, SMB2_HDR_TID
, tid
);
3122 SBVAL(state
->smb2
.hdr
, SMB2_HDR_SESSION_ID
, uid
);
3125 case SMB2_OP_CANCEL
:
3126 state
->one_way
= true;
3130 * If this is a dummy request, it will have
3131 * UINT64_MAX as message id.
3132 * If we send on break acknowledgement,
3133 * this gets overwritten later.
3135 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, UINT64_MAX
);
3139 if (timeout_msec
> 0) {
3140 state
->endtime
= timeval_current_ofs_msec(timeout_msec
);
3141 if (!tevent_req_set_endtime(req
, ev
, state
->endtime
)) {
3149 void smb2cli_req_set_notify_async(struct tevent_req
*req
)
3151 struct smbXcli_req_state
*state
=
3152 tevent_req_data(req
,
3153 struct smbXcli_req_state
);
3155 state
->smb2
.notify_async
= true;
3158 static void smb2cli_req_writev_done(struct tevent_req
*subreq
);
3159 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
3160 TALLOC_CTX
*tmp_mem
,
3163 NTSTATUS
smb2cli_req_compound_submit(struct tevent_req
**reqs
,
3166 struct smbXcli_req_state
*state
;
3167 struct tevent_req
*subreq
;
3169 int i
, num_iov
, nbt_len
;
3171 struct smb2_signing_key
*encryption_key
= NULL
;
3172 uint64_t encryption_session_id
= 0;
3173 uint64_t nonce_high
= UINT64_MAX
;
3174 uint64_t nonce_low
= UINT64_MAX
;
3177 * 1 for the nbt length, optional TRANSFORM
3178 * per request: HDR, fixed, dyn, padding
3179 * -1 because the last one does not need padding
3182 iov
= talloc_array(reqs
[0], struct iovec
, 1 + 1 + 4*num_reqs
- 1);
3184 return NT_STATUS_NO_MEMORY
;
3191 * the session of the first request that requires encryption
3192 * specifies the encryption key.
3194 for (i
=0; i
<num_reqs
; i
++) {
3195 if (!tevent_req_is_in_progress(reqs
[i
])) {
3196 return NT_STATUS_INTERNAL_ERROR
;
3199 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
3201 if (!smbXcli_conn_is_connected(state
->conn
)) {
3202 return NT_STATUS_CONNECTION_DISCONNECTED
;
3205 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
3206 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
3207 return NT_STATUS_REVISION_MISMATCH
;
3210 if (state
->session
== NULL
) {
3214 if (!state
->smb2
.should_encrypt
) {
3218 encryption_key
= state
->session
->smb2
->encryption_key
;
3219 if (!smb2_signing_key_valid(encryption_key
)) {
3220 return NT_STATUS_INVALID_PARAMETER_MIX
;
3223 encryption_session_id
= state
->session
->smb2
->session_id
;
3225 state
->session
->smb2
->nonce_low
+= 1;
3226 if (state
->session
->smb2
->nonce_low
== 0) {
3227 state
->session
->smb2
->nonce_high
+= 1;
3228 state
->session
->smb2
->nonce_low
+= 1;
3232 * CCM and GCM algorithms must never have their
3233 * nonce wrap, or the security of the whole
3234 * communication and the keys is destroyed.
3235 * We must drop the connection once we have
3236 * transferred too much data.
3238 * NOTE: We assume nonces greater than 8 bytes.
3240 if (state
->session
->smb2
->nonce_high
>=
3241 state
->session
->smb2
->nonce_high_max
)
3243 return NT_STATUS_ENCRYPTION_FAILED
;
3246 nonce_high
= state
->session
->smb2
->nonce_high_random
;
3247 nonce_high
+= state
->session
->smb2
->nonce_high
;
3248 nonce_low
= state
->session
->smb2
->nonce_low
;
3251 iov
[num_iov
].iov_base
= state
->smb2
.transform
;
3252 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.transform
);
3255 SBVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
3256 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
,
3258 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
+8,
3260 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
,
3261 encryption_session_id
);
3263 nbt_len
+= SMB2_TF_HDR_SIZE
;
3267 for (i
=0; i
<num_reqs
; i
++) {
3276 struct smb2_signing_key
*signing_key
= NULL
;
3278 if (!tevent_req_is_in_progress(reqs
[i
])) {
3279 return NT_STATUS_INTERNAL_ERROR
;
3282 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
3284 if (!smbXcli_conn_is_connected(state
->conn
)) {
3285 return NT_STATUS_CONNECTION_DISCONNECTED
;
3288 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
3289 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
3290 return NT_STATUS_REVISION_MISMATCH
;
3293 opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
3294 if (opcode
== SMB2_OP_CANCEL
) {
3298 avail
= UINT64_MAX
- state
->conn
->smb2
.mid
;
3300 return NT_STATUS_CONNECTION_ABORTED
;
3303 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
3304 uint32_t max_dyn_len
= 1;
3306 max_dyn_len
= MAX(max_dyn_len
, state
->smb2
.dyn_len
);
3307 max_dyn_len
= MAX(max_dyn_len
, state
->smb2
.max_dyn_len
);
3309 charge
= (max_dyn_len
- 1)/ 65536 + 1;
3314 charge
= MAX(state
->smb2
.credit_charge
, charge
);
3316 avail
= MIN(avail
, state
->conn
->smb2
.cur_credits
);
3317 if (avail
< charge
) {
3318 DBG_ERR("Insufficient credits. "
3319 "%"PRIu64
" available, %"PRIu16
" needed\n",
3321 return NT_STATUS_INTERNAL_ERROR
;
3325 if (state
->conn
->smb2
.max_credits
> state
->conn
->smb2
.cur_credits
) {
3326 credits
= state
->conn
->smb2
.max_credits
-
3327 state
->conn
->smb2
.cur_credits
;
3329 if (state
->conn
->smb2
.max_credits
>= state
->conn
->smb2
.cur_credits
) {
3333 mid
= state
->conn
->smb2
.mid
;
3334 state
->conn
->smb2
.mid
+= charge
;
3335 state
->conn
->smb2
.cur_credits
-= charge
;
3337 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
3338 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT_CHARGE
, charge
);
3340 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT
, credits
);
3341 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
3343 state
->smb2
.cancel_flags
= SVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
3344 state
->smb2
.cancel_flags
&= ~SMB2_HDR_FLAG_CHAINED
;
3345 if (state
->conn
->smb2
.server
.sign_algo
>= SMB2_SIGNING_AES128_GMAC
) {
3346 state
->smb2
.cancel_mid
= mid
;
3348 state
->smb2
.cancel_mid
= 0;
3350 state
->smb2
.cancel_aid
= 0;
3353 if (state
->session
&& encryption_key
== NULL
) {
3355 * We prefer the channel signing key if it is
3358 if (state
->smb2
.should_sign
) {
3359 signing_key
= state
->session
->smb2_channel
.signing_key
;
3363 * If it is a channel binding, we already have the main
3364 * signing key and try that one.
3366 if (signing_key
!= NULL
&&
3367 !smb2_signing_key_valid(signing_key
)) {
3368 signing_key
= state
->session
->smb2
->signing_key
;
3372 * If we do not have any session key yet, we skip the
3373 * signing of SMB2_OP_SESSSETUP requests.
3375 if (signing_key
!= NULL
&&
3376 !smb2_signing_key_valid(signing_key
)) {
3382 iov
[num_iov
].iov_base
= state
->smb2
.hdr
;
3383 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.hdr
);
3386 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.fixed
);
3387 iov
[num_iov
].iov_len
= state
->smb2
.fixed_len
;
3390 if (state
->smb2
.dyn
!= NULL
) {
3391 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.dyn
);
3392 iov
[num_iov
].iov_len
= state
->smb2
.dyn_len
;
3396 reqlen
= sizeof(state
->smb2
.hdr
);
3397 reqlen
+= state
->smb2
.fixed_len
;
3398 reqlen
+= state
->smb2
.dyn_len
;
3400 if (i
< num_reqs
-1) {
3401 if ((reqlen
% 8) > 0) {
3402 uint8_t pad
= 8 - (reqlen
% 8);
3403 iov
[num_iov
].iov_base
= state
->smb2
.pad
;
3404 iov
[num_iov
].iov_len
= pad
;
3408 SIVAL(state
->smb2
.hdr
, SMB2_HDR_NEXT_COMMAND
, reqlen
);
3411 state
->smb2
.encryption_session_id
= encryption_session_id
;
3413 if (signing_key
!= NULL
) {
3416 status
= smb2_signing_sign_pdu(signing_key
,
3417 &iov
[hdr_iov
], num_iov
- hdr_iov
);
3418 if (!NT_STATUS_IS_OK(status
)) {
3425 ret
= smbXcli_req_set_pending(reqs
[i
]);
3427 return NT_STATUS_NO_MEMORY
;
3431 state
= tevent_req_data(reqs
[0], struct smbXcli_req_state
);
3432 _smb_setlen_tcp(state
->length_hdr
, nbt_len
);
3433 iov
[0].iov_base
= state
->length_hdr
;
3434 iov
[0].iov_len
= sizeof(state
->length_hdr
);
3436 if (encryption_key
!= NULL
) {
3438 size_t buflen
= nbt_len
- SMB2_TF_HDR_SIZE
;
3442 buf
= talloc_array(iov
, uint8_t, buflen
);
3444 return NT_STATUS_NO_MEMORY
;
3448 * We copy the buffers before encrypting them,
3449 * this is at least currently needed for the
3450 * to keep state->smb2.hdr.
3452 * Also the callers may expect there buffers
3455 for (vi
= tf_iov
+ 1; vi
< num_iov
; vi
++) {
3456 struct iovec
*v
= &iov
[vi
];
3457 const uint8_t *o
= (const uint8_t *)v
->iov_base
;
3459 memcpy(buf
, o
, v
->iov_len
);
3460 v
->iov_base
= (void *)buf
;
3464 status
= smb2_signing_encrypt_pdu(encryption_key
,
3465 &iov
[tf_iov
], num_iov
- tf_iov
);
3466 if (!NT_STATUS_IS_OK(status
)) {
3471 if (state
->conn
->dispatch_incoming
== NULL
) {
3472 state
->conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
3475 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
3476 state
->conn
->sock_fd
, false, iov
, num_iov
);
3477 if (subreq
== NULL
) {
3478 return NT_STATUS_NO_MEMORY
;
3480 tevent_req_set_callback(subreq
, smb2cli_req_writev_done
, reqs
[0]);
3481 state
->write_req
= subreq
;
3483 return NT_STATUS_OK
;
3486 void smb2cli_req_set_credit_charge(struct tevent_req
*req
, uint16_t charge
)
3488 struct smbXcli_req_state
*state
=
3489 tevent_req_data(req
,
3490 struct smbXcli_req_state
);
3492 state
->smb2
.credit_charge
= charge
;
3495 struct tevent_req
*smb2cli_req_send(TALLOC_CTX
*mem_ctx
,
3496 struct tevent_context
*ev
,
3497 struct smbXcli_conn
*conn
,
3499 uint32_t additional_flags
,
3500 uint32_t clear_flags
,
3501 uint32_t timeout_msec
,
3502 struct smbXcli_tcon
*tcon
,
3503 struct smbXcli_session
*session
,
3504 const uint8_t *fixed
,
3508 uint32_t max_dyn_len
)
3510 struct tevent_req
*req
;
3513 req
= smb2cli_req_create(mem_ctx
, ev
, conn
, cmd
,
3514 additional_flags
, clear_flags
,
3523 if (!tevent_req_is_in_progress(req
)) {
3524 return tevent_req_post(req
, ev
);
3526 status
= smb2cli_req_compound_submit(&req
, 1);
3527 if (tevent_req_nterror(req
, status
)) {
3528 return tevent_req_post(req
, ev
);
3533 static void smb2cli_req_writev_done(struct tevent_req
*subreq
)
3535 struct tevent_req
*req
=
3536 tevent_req_callback_data(subreq
,
3538 struct smbXcli_req_state
*state
=
3539 tevent_req_data(req
,
3540 struct smbXcli_req_state
);
3544 state
->write_req
= NULL
;
3546 nwritten
= writev_recv(subreq
, &err
);
3547 TALLOC_FREE(subreq
);
3548 if (nwritten
== -1) {
3549 /* here, we need to notify all pending requests */
3550 NTSTATUS status
= map_nt_error_from_unix_common(err
);
3551 smbXcli_conn_disconnect(state
->conn
, status
);
3556 static struct smbXcli_session
* smbXcli_session_by_uid(struct smbXcli_conn
*conn
,
3559 struct smbXcli_session
*s
= conn
->sessions
;
3561 for (; s
; s
= s
->next
) {
3562 if (s
->smb2
->session_id
!= uid
) {
3571 static NTSTATUS
smb2cli_inbuf_parse_compound(struct smbXcli_conn
*conn
,
3574 TALLOC_CTX
*mem_ctx
,
3575 struct iovec
**piov
,
3581 uint8_t *first_hdr
= buf
;
3582 size_t verified_buflen
= 0;
3586 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
3588 return NT_STATUS_NO_MEMORY
;
3591 while (taken
< buflen
) {
3592 size_t len
= buflen
- taken
;
3593 uint8_t *hdr
= first_hdr
+ taken
;
3596 size_t next_command_ofs
;
3598 struct iovec
*iov_tmp
;
3600 if (verified_buflen
> taken
) {
3601 len
= verified_buflen
- taken
;
3608 DEBUG(10, ("%d bytes left, expected at least %d\n",
3612 if (IVAL(hdr
, 0) == SMB2_TF_MAGIC
) {
3613 struct smbXcli_session
*s
;
3615 struct iovec tf_iov
[2];
3619 if (len
< SMB2_TF_HDR_SIZE
) {
3620 DEBUG(10, ("%d bytes left, expected at least %d\n",
3621 (int)len
, SMB2_TF_HDR_SIZE
));
3625 tf_len
= SMB2_TF_HDR_SIZE
;
3628 hdr
= first_hdr
+ taken
;
3629 enc_len
= IVAL(tf
, SMB2_TF_MSG_SIZE
);
3630 uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
3632 if (len
< SMB2_TF_HDR_SIZE
+ enc_len
) {
3633 DEBUG(10, ("%d bytes left, expected at least %d\n",
3635 (int)(SMB2_TF_HDR_SIZE
+ enc_len
)));
3639 s
= smbXcli_session_by_uid(conn
, uid
);
3641 DEBUG(10, ("unknown session_id %llu\n",
3642 (unsigned long long)uid
));
3646 tf_iov
[0].iov_base
= (void *)tf
;
3647 tf_iov
[0].iov_len
= tf_len
;
3648 tf_iov
[1].iov_base
= (void *)hdr
;
3649 tf_iov
[1].iov_len
= enc_len
;
3651 status
= smb2_signing_decrypt_pdu(s
->smb2
->decryption_key
,
3653 if (!NT_STATUS_IS_OK(status
)) {
3658 verified_buflen
= taken
+ enc_len
;
3663 * We need the header plus the body length field
3666 if (len
< SMB2_HDR_BODY
+ 2) {
3667 DEBUG(10, ("%d bytes left, expected at least %d\n",
3668 (int)len
, SMB2_HDR_BODY
));
3671 if (IVAL(hdr
, 0) != SMB2_MAGIC
) {
3672 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3676 if (SVAL(hdr
, 4) != SMB2_HDR_BODY
) {
3677 DEBUG(10, ("Got HDR len %d, expected %d\n",
3678 SVAL(hdr
, 4), SMB2_HDR_BODY
));
3683 next_command_ofs
= IVAL(hdr
, SMB2_HDR_NEXT_COMMAND
);
3684 body_size
= SVAL(hdr
, SMB2_HDR_BODY
);
3686 if (next_command_ofs
!= 0) {
3687 if (next_command_ofs
< (SMB2_HDR_BODY
+ 2)) {
3690 if (next_command_ofs
> full_size
) {
3693 full_size
= next_command_ofs
;
3695 if (body_size
< 2) {
3698 body_size
&= 0xfffe;
3700 if (body_size
> (full_size
- SMB2_HDR_BODY
)) {
3704 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
3706 if (iov_tmp
== NULL
) {
3708 return NT_STATUS_NO_MEMORY
;
3711 cur
= &iov
[num_iov
];
3714 cur
[0].iov_base
= tf
;
3715 cur
[0].iov_len
= tf_len
;
3716 cur
[1].iov_base
= hdr
;
3717 cur
[1].iov_len
= SMB2_HDR_BODY
;
3718 cur
[2].iov_base
= hdr
+ SMB2_HDR_BODY
;
3719 cur
[2].iov_len
= body_size
;
3720 cur
[3].iov_base
= hdr
+ SMB2_HDR_BODY
+ body_size
;
3721 cur
[3].iov_len
= full_size
- (SMB2_HDR_BODY
+ body_size
);
3727 *pnum_iov
= num_iov
;
3728 return NT_STATUS_OK
;
3732 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3735 static struct tevent_req
*smb2cli_conn_find_pending(struct smbXcli_conn
*conn
,
3738 size_t num_pending
= talloc_array_length(conn
->pending
);
3741 for (i
=0; i
<num_pending
; i
++) {
3742 struct tevent_req
*req
= conn
->pending
[i
];
3743 struct smbXcli_req_state
*state
=
3744 tevent_req_data(req
,
3745 struct smbXcli_req_state
);
3747 if (mid
== BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
)) {
3754 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
3755 TALLOC_CTX
*tmp_mem
,
3758 struct tevent_req
*req
;
3759 struct smbXcli_req_state
*state
= NULL
;
3760 struct iovec
*iov
= NULL
;
3761 size_t i
, num_iov
= 0;
3764 struct smbXcli_session
*last_session
= NULL
;
3765 size_t inbuf_len
= smb_len_tcp(inbuf
);
3767 status
= smb2cli_inbuf_parse_compound(conn
,
3768 inbuf
+ NBT_HDR_SIZE
,
3772 if (!NT_STATUS_IS_OK(status
)) {
3776 for (i
=0; i
<num_iov
; i
+=4) {
3777 uint8_t *inbuf_ref
= NULL
;
3778 struct iovec
*cur
= &iov
[i
];
3779 uint8_t *inhdr
= (uint8_t *)cur
[1].iov_base
;
3780 uint16_t opcode
= SVAL(inhdr
, SMB2_HDR_OPCODE
);
3781 uint32_t flags
= IVAL(inhdr
, SMB2_HDR_FLAGS
);
3782 uint64_t mid
= BVAL(inhdr
, SMB2_HDR_MESSAGE_ID
);
3783 uint16_t req_opcode
;
3785 uint16_t credits
= SVAL(inhdr
, SMB2_HDR_CREDIT
);
3786 uint32_t new_credits
;
3787 struct smbXcli_session
*session
= NULL
;
3788 struct smb2_signing_key
*signing_key
= NULL
;
3789 bool was_encrypted
= false;
3791 new_credits
= conn
->smb2
.cur_credits
;
3792 new_credits
+= credits
;
3793 if (new_credits
> UINT16_MAX
) {
3794 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3796 conn
->smb2
.cur_credits
+= credits
;
3798 req
= smb2cli_conn_find_pending(conn
, mid
);
3800 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3802 state
= tevent_req_data(req
, struct smbXcli_req_state
);
3804 req_opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
3805 if (opcode
!= req_opcode
) {
3806 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3808 req_flags
= SVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
3810 if (!(flags
& SMB2_HDR_FLAG_REDIRECT
)) {
3811 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3814 status
= NT_STATUS(IVAL(inhdr
, SMB2_HDR_STATUS
));
3815 if ((flags
& SMB2_HDR_FLAG_ASYNC
) &&
3816 NT_STATUS_EQUAL(status
, NT_STATUS_PENDING
)) {
3817 uint64_t async_id
= BVAL(inhdr
, SMB2_HDR_ASYNC_ID
);
3819 if (state
->smb2
.got_async
) {
3820 /* We only expect one STATUS_PENDING response */
3821 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3823 state
->smb2
.got_async
= true;
3826 * async interim responses are not signed,
3827 * even if the SMB2_HDR_FLAG_SIGNED flag
3830 state
->smb2
.cancel_flags
|= SMB2_HDR_FLAG_ASYNC
;
3831 state
->smb2
.cancel_aid
= async_id
;
3833 if (state
->smb2
.notify_async
) {
3834 tevent_req_defer_callback(req
, state
->ev
);
3835 tevent_req_notify_callback(req
);
3840 session
= state
->session
;
3841 if (req_flags
& SMB2_HDR_FLAG_CHAINED
) {
3842 session
= last_session
;
3844 last_session
= session
;
3846 if (flags
& SMB2_HDR_FLAG_SIGNED
) {
3847 uint64_t uid
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
3849 if (session
== NULL
) {
3850 session
= smbXcli_session_by_uid(state
->conn
,
3854 if (session
== NULL
) {
3855 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3858 last_session
= session
;
3859 signing_key
= session
->smb2_channel
.signing_key
;
3862 if (opcode
== SMB2_OP_SESSSETUP
) {
3864 * We prefer the channel signing key, if it is
3867 * If we do not have a channel signing key yet,
3868 * we try the main signing key, if it is not
3869 * the final response.
3871 if (signing_key
!= NULL
&&
3872 !smb2_signing_key_valid(signing_key
) &&
3873 !NT_STATUS_IS_OK(status
)) {
3874 signing_key
= session
->smb2
->signing_key
;
3877 if (signing_key
!= NULL
&&
3878 !smb2_signing_key_valid(signing_key
)) {
3880 * If we do not have a session key to
3881 * verify the signature, we defer the
3882 * signing check to the caller.
3884 * The caller gets NT_STATUS_OK, it
3886 * smb2cli_session_set_session_key()
3888 * smb2cli_session_set_channel_key()
3889 * which will check the signature
3890 * with the channel signing key.
3895 if (!NT_STATUS_IS_OK(status
)) {
3897 * Only check the signature of the last response
3898 * of a successful session auth. This matches
3899 * Windows behaviour for NTLM auth and reauth.
3901 state
->smb2
.require_signed_response
= false;
3905 if (state
->smb2
.should_sign
||
3906 state
->smb2
.require_signed_response
)
3908 if (!(flags
& SMB2_HDR_FLAG_SIGNED
)) {
3909 return NT_STATUS_ACCESS_DENIED
;
3913 if (!smb2_signing_key_valid(signing_key
) &&
3914 state
->smb2
.require_signed_response
) {
3915 signing_key
= session
->smb2_channel
.signing_key
;
3918 if (cur
[0].iov_len
== SMB2_TF_HDR_SIZE
) {
3919 const uint8_t *tf
= (const uint8_t *)cur
[0].iov_base
;
3920 uint64_t uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
3923 * If the response was encrypted in a SMB2_TRANSFORM
3924 * pdu, which belongs to the correct session,
3925 * we do not need to do signing checks
3927 * It could be the session the response belongs to
3928 * or the session that was used to encrypt the
3929 * SMB2_TRANSFORM request.
3931 if ((session
&& session
->smb2
->session_id
== uid
) ||
3932 (state
->smb2
.encryption_session_id
== uid
)) {
3934 was_encrypted
= true;
3938 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_SESSION_DELETED
)) {
3940 * if the server returns NT_STATUS_USER_SESSION_DELETED
3941 * the response is not signed and we should
3942 * propagate the NT_STATUS_USER_SESSION_DELETED
3943 * status to the caller.
3945 state
->smb2
.signing_skipped
= true;
3948 if (NT_STATUS_EQUAL(status
, NT_STATUS_REQUEST_OUT_OF_SEQUENCE
)) {
3950 * if the server returns
3951 * NT_STATUS_REQUEST_OUT_OF_SEQUENCE for a session setup
3952 * request, the response is not signed and we should
3953 * propagate the NT_STATUS_REQUEST_OUT_OF_SEQUENCE
3954 * status to the caller
3956 if (opcode
== SMB2_OP_SESSSETUP
) {
3957 state
->smb2
.signing_skipped
= true;
3961 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_SUPPORTED
)) {
3963 * if the server returns NT_STATUS_NOT_SUPPORTED
3964 * for a session setup request, the response is not
3965 * signed and we should propagate the NT_STATUS_NOT_SUPPORTED
3966 * status to the caller.
3968 if (opcode
== SMB2_OP_SESSSETUP
) {
3969 state
->smb2
.signing_skipped
= true;
3973 if (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
3975 * if the server returns
3976 * NT_STATUS_ACCESS_DENIED for a session setup
3977 * request, the response is not signed and we should
3978 * propagate the NT_STATUS_ACCESS_DENIED
3979 * status to the caller without disconnecting
3980 * the connection because we where not able to
3981 * verify the response signature.
3983 if (opcode
== SMB2_OP_SESSSETUP
) {
3984 state
->smb2
.signing_skipped
= true;
3989 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
3991 * if the server returns
3992 * NT_STATUS_INVALID_PARAMETER
3993 * the response might not be encrypted.
3995 if (state
->smb2
.should_encrypt
&& !was_encrypted
) {
3996 state
->smb2
.signing_skipped
= true;
4001 if (state
->smb2
.should_encrypt
&& !was_encrypted
) {
4002 if (!state
->smb2
.signing_skipped
) {
4003 return NT_STATUS_ACCESS_DENIED
;
4007 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_NAME_DELETED
) ||
4008 NT_STATUS_EQUAL(status
, NT_STATUS_FILE_CLOSED
) ||
4009 (NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
) &&
4011 session
->smb2
->no_signing_disconnect
) ||
4012 NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
4014 * if the server returns
4015 * NT_STATUS_NETWORK_NAME_DELETED
4016 * NT_STATUS_FILE_CLOSED
4017 * NT_STATUS_INVALID_PARAMETER
4018 * the response might not be signed
4019 * as this happens before the signing checks.
4021 * If server echos the signature (or all zeros)
4022 * we should report the status from the server
4028 cmp
= mem_equal_const_time(inhdr
+SMB2_HDR_SIGNATURE
,
4029 state
->smb2
.hdr
+SMB2_HDR_SIGNATURE
,
4032 state
->smb2
.signing_skipped
= true;
4038 zero
= all_zero(inhdr
+SMB2_HDR_SIGNATURE
, 16);
4040 state
->smb2
.signing_skipped
= true;
4047 NTSTATUS signing_status
;
4049 signing_status
= smb2_signing_check_pdu(signing_key
,
4051 if (!NT_STATUS_IS_OK(signing_status
)) {
4053 * If the signing check fails, we disconnect
4057 * smb2cli_session_torture_no_signing_disconnect
4058 * was called in torture tests
4061 if (!NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
4062 return signing_status
;
4065 if (!NT_STATUS_EQUAL(status
, signing_status
)) {
4066 return signing_status
;
4069 if (session
== NULL
) {
4070 return signing_status
;
4073 if (!session
->smb2
->no_signing_disconnect
) {
4074 return signing_status
;
4077 state
->smb2
.signing_skipped
= true;
4081 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
) &&
4082 (session
!= NULL
) && session
->disconnect_expired
)
4085 * this should be a short term hack
4086 * until the upper layers have implemented
4087 * re-authentication.
4092 smbXcli_req_unset_pending(req
);
4095 * There might be more than one response
4096 * we need to defer the notifications
4098 if ((num_iov
== 5) && (talloc_array_length(conn
->pending
) == 0)) {
4103 tevent_req_defer_callback(req
, state
->ev
);
4107 * Note: here we use talloc_reference() in a way
4108 * that does not expose it to the caller.
4110 inbuf_ref
= talloc_reference(state
->smb2
.recv_iov
, inbuf
);
4111 if (tevent_req_nomem(inbuf_ref
, req
)) {
4115 /* copy the related buffers */
4116 state
->smb2
.recv_iov
[0] = cur
[1];
4117 state
->smb2
.recv_iov
[1] = cur
[2];
4118 state
->smb2
.recv_iov
[2] = cur
[3];
4120 tevent_req_done(req
);
4124 return NT_STATUS_RETRY
;
4127 return NT_STATUS_OK
;
4130 NTSTATUS
smb2cli_req_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
4131 struct iovec
**piov
,
4132 const struct smb2cli_req_expected_response
*expected
,
4133 size_t num_expected
)
4135 struct smbXcli_req_state
*state
=
4136 tevent_req_data(req
,
4137 struct smbXcli_req_state
);
4140 bool found_status
= false;
4141 bool found_size
= false;
4148 if (tevent_req_is_in_progress(req
) && state
->smb2
.got_async
) {
4149 return NT_STATUS_PENDING
;
4152 if (tevent_req_is_nterror(req
, &status
)) {
4153 for (i
=0; i
< num_expected
; i
++) {
4154 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
4155 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
4162 if (num_expected
== 0) {
4163 found_status
= true;
4167 status
= NT_STATUS(IVAL(state
->smb2
.recv_iov
[0].iov_base
, SMB2_HDR_STATUS
));
4168 body_size
= SVAL(state
->smb2
.recv_iov
[1].iov_base
, 0);
4170 for (i
=0; i
< num_expected
; i
++) {
4171 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
4175 found_status
= true;
4176 if (expected
[i
].body_size
== 0) {
4181 if (expected
[i
].body_size
== body_size
) {
4187 if (!found_status
) {
4191 if (state
->smb2
.signing_skipped
) {
4192 if (num_expected
> 0) {
4193 return NT_STATUS_ACCESS_DENIED
;
4195 if (!NT_STATUS_IS_ERR(status
)) {
4196 return NT_STATUS_ACCESS_DENIED
;
4201 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4205 *piov
= talloc_move(mem_ctx
, &state
->smb2
.recv_iov
);
4211 NTSTATUS
smb2cli_req_get_sent_iov(struct tevent_req
*req
,
4212 struct iovec
*sent_iov
)
4214 struct smbXcli_req_state
*state
=
4215 tevent_req_data(req
,
4216 struct smbXcli_req_state
);
4218 if (tevent_req_is_in_progress(req
)) {
4219 return NT_STATUS_PENDING
;
4222 sent_iov
[0].iov_base
= state
->smb2
.hdr
;
4223 sent_iov
[0].iov_len
= sizeof(state
->smb2
.hdr
);
4225 sent_iov
[1].iov_base
= discard_const(state
->smb2
.fixed
);
4226 sent_iov
[1].iov_len
= state
->smb2
.fixed_len
;
4228 if (state
->smb2
.dyn
!= NULL
) {
4229 sent_iov
[2].iov_base
= discard_const(state
->smb2
.dyn
);
4230 sent_iov
[2].iov_len
= state
->smb2
.dyn_len
;
4232 sent_iov
[2].iov_base
= NULL
;
4233 sent_iov
[2].iov_len
= 0;
4236 return NT_STATUS_OK
;
4239 static const struct {
4240 enum protocol_types proto
;
4241 const char *smb1_name
;
4242 } smb1cli_prots
[] = {
4243 {PROTOCOL_CORE
, "PC NETWORK PROGRAM 1.0"},
4244 {PROTOCOL_COREPLUS
, "MICROSOFT NETWORKS 1.03"},
4245 {PROTOCOL_LANMAN1
, "MICROSOFT NETWORKS 3.0"},
4246 {PROTOCOL_LANMAN1
, "LANMAN1.0"},
4247 {PROTOCOL_LANMAN2
, "LM1.2X002"},
4248 {PROTOCOL_LANMAN2
, "DOS LANMAN2.1"},
4249 {PROTOCOL_LANMAN2
, "LANMAN2.1"},
4250 {PROTOCOL_LANMAN2
, "Samba"},
4251 {PROTOCOL_NT1
, "NT LANMAN 1.0"},
4252 {PROTOCOL_NT1
, "NT LM 0.12"},
4253 {PROTOCOL_SMB2_02
, "SMB 2.002"},
4254 {PROTOCOL_SMB2_10
, "SMB 2.???"},
4257 static const struct {
4258 enum protocol_types proto
;
4259 uint16_t smb2_dialect
;
4260 } smb2cli_prots
[] = {
4261 {PROTOCOL_SMB2_02
, SMB2_DIALECT_REVISION_202
},
4262 {PROTOCOL_SMB2_10
, SMB2_DIALECT_REVISION_210
},
4263 {PROTOCOL_SMB3_00
, SMB3_DIALECT_REVISION_300
},
4264 {PROTOCOL_SMB3_02
, SMB3_DIALECT_REVISION_302
},
4265 {PROTOCOL_SMB3_11
, SMB3_DIALECT_REVISION_311
},
4268 struct smbXcli_negprot_state
{
4269 struct smbXcli_conn
*conn
;
4270 struct tevent_context
*ev
;
4271 struct smb2_negotiate_contexts
*in_ctx
;
4272 struct smb2_negotiate_contexts
*out_ctx
;
4273 uint32_t timeout_msec
;
4280 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
);
4281 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
);
4282 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
);
4283 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
);
4284 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
);
4285 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
4289 struct tevent_req
*smbXcli_negprot_send(TALLOC_CTX
*mem_ctx
,
4290 struct tevent_context
*ev
,
4291 struct smbXcli_conn
*conn
,
4292 uint32_t timeout_msec
,
4293 enum protocol_types min_protocol
,
4294 enum protocol_types max_protocol
,
4295 uint16_t max_credits
,
4296 struct smb2_negotiate_contexts
*in_ctx
)
4298 struct tevent_req
*req
, *subreq
;
4299 struct smbXcli_negprot_state
*state
;
4301 req
= tevent_req_create(mem_ctx
, &state
,
4302 struct smbXcli_negprot_state
);
4308 state
->in_ctx
= in_ctx
;
4309 state
->timeout_msec
= timeout_msec
;
4311 if (min_protocol
== PROTOCOL_NONE
) {
4312 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
4313 return tevent_req_post(req
, ev
);
4316 if (max_protocol
== PROTOCOL_NONE
) {
4317 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
4318 return tevent_req_post(req
, ev
);
4321 if (min_protocol
> max_protocol
) {
4322 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
4323 return tevent_req_post(req
, ev
);
4326 conn
->min_protocol
= min_protocol
;
4327 conn
->max_protocol
= max_protocol
;
4328 conn
->protocol
= PROTOCOL_NONE
;
4330 if (max_protocol
>= PROTOCOL_SMB2_02
) {
4331 conn
->smb2
.max_credits
= max_credits
;
4334 if ((min_protocol
< PROTOCOL_SMB2_02
) &&
4335 (max_protocol
< PROTOCOL_SMB2_02
)) {
4339 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
4341 subreq
= smbXcli_negprot_smb1_subreq(state
);
4342 if (tevent_req_nomem(subreq
, req
)) {
4343 return tevent_req_post(req
, ev
);
4345 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
4349 if ((min_protocol
>= PROTOCOL_SMB2_02
) &&
4350 (max_protocol
>= PROTOCOL_SMB2_02
)) {
4354 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
4356 subreq
= smbXcli_negprot_smb2_subreq(state
);
4357 if (tevent_req_nomem(subreq
, req
)) {
4358 return tevent_req_post(req
, ev
);
4360 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4365 * We send an SMB1 negprot with the SMB2 dialects
4366 * and expect a SMB1 or a SMB2 response.
4368 * smbXcli_negprot_dispatch_incoming() will fix the
4369 * callback to match protocol of the response.
4371 conn
->dispatch_incoming
= smbXcli_negprot_dispatch_incoming
;
4373 subreq
= smbXcli_negprot_smb1_subreq(state
);
4374 if (tevent_req_nomem(subreq
, req
)) {
4375 return tevent_req_post(req
, ev
);
4377 tevent_req_set_callback(subreq
, smbXcli_negprot_invalid_done
, req
);
4381 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
)
4383 struct tevent_req
*req
=
4384 tevent_req_callback_data(subreq
,
4389 * we just want the low level error
4391 status
= tevent_req_simple_recv_ntstatus(subreq
);
4392 TALLOC_FREE(subreq
);
4393 if (tevent_req_nterror(req
, status
)) {
4397 /* this should never happen */
4398 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
4401 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
)
4404 DATA_BLOB bytes
= data_blob_null
;
4408 /* setup the protocol strings */
4409 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
4413 if (smb1cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
4417 if (smb1cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
4421 ok
= data_blob_append(state
, &bytes
, &c
, sizeof(c
));
4427 * We know it is already ascii and
4428 * we want NULL termination.
4430 ok
= data_blob_append(state
, &bytes
,
4431 smb1cli_prots
[i
].smb1_name
,
4432 strlen(smb1cli_prots
[i
].smb1_name
)+1);
4438 smb1cli_req_flags(state
->conn
->max_protocol
,
4439 state
->conn
->smb1
.client
.capabilities
,
4444 return smb1cli_req_send(state
, state
->ev
, state
->conn
,
4448 state
->timeout_msec
,
4449 0xFFFE, 0, NULL
, /* pid, tid, session */
4450 0, NULL
, /* wct, vwv */
4451 bytes
.length
, bytes
.data
);
4454 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
)
4456 struct tevent_req
*req
=
4457 tevent_req_callback_data(subreq
,
4459 struct smbXcli_negprot_state
*state
=
4460 tevent_req_data(req
,
4461 struct smbXcli_negprot_state
);
4462 struct smbXcli_conn
*conn
= state
->conn
;
4463 struct iovec
*recv_iov
= NULL
;
4464 uint8_t *inhdr
= NULL
;
4472 size_t num_prots
= 0;
4474 uint32_t client_capabilities
= conn
->smb1
.client
.capabilities
;
4475 uint32_t both_capabilities
;
4476 uint32_t server_capabilities
= 0;
4477 uint32_t capabilities
;
4478 uint32_t client_max_xmit
= conn
->smb1
.client
.max_xmit
;
4479 uint32_t server_max_xmit
= 0;
4481 uint32_t server_max_mux
= 0;
4482 uint16_t server_security_mode
= 0;
4483 uint32_t server_session_key
= 0;
4484 bool server_readbraw
= false;
4485 bool server_writebraw
= false;
4486 bool server_lockread
= false;
4487 bool server_writeunlock
= false;
4488 struct GUID server_guid
= GUID_zero();
4489 DATA_BLOB server_gss_blob
= data_blob_null
;
4490 uint8_t server_challenge
[8];
4491 char *server_workgroup
= NULL
;
4492 char *server_name
= NULL
;
4493 int server_time_zone
= 0;
4494 NTTIME server_system_time
= 0;
4495 static const struct smb1cli_req_expected_response expected
[] = {
4497 .status
= NT_STATUS_OK
,
4498 .wct
= 0x11, /* NT1 */
4501 .status
= NT_STATUS_OK
,
4502 .wct
= 0x0D, /* LM */
4505 .status
= NT_STATUS_OK
,
4506 .wct
= 0x01, /* CORE */
4510 ZERO_STRUCT(server_challenge
);
4512 status
= smb1cli_req_recv(subreq
, state
,
4517 NULL
, /* pvwv_offset */
4520 NULL
, /* pbytes_offset */
4522 expected
, ARRAY_SIZE(expected
));
4523 TALLOC_FREE(subreq
);
4524 if (tevent_req_nterror(req
, status
)) {
4527 if (inhdr
== NULL
) {
4528 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
4532 flags
= CVAL(inhdr
, HDR_FLG
);
4534 protnum
= SVAL(vwv
, 0);
4536 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
4537 if (smb1cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
4541 if (smb1cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
4545 if (protnum
!= num_prots
) {
4550 conn
->protocol
= smb1cli_prots
[i
].proto
;
4554 if (conn
->protocol
== PROTOCOL_NONE
) {
4555 DBG_ERR("No compatible protocol selected by server.\n");
4556 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4560 if ((conn
->protocol
< PROTOCOL_NT1
) && conn
->mandatory_signing
) {
4561 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4562 "and the selected protocol level doesn't support it.\n"));
4563 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
4567 if (flags
& FLAG_SUPPORT_LOCKREAD
) {
4568 server_lockread
= true;
4569 server_writeunlock
= true;
4572 if (conn
->protocol
>= PROTOCOL_NT1
) {
4573 const char *client_signing
= NULL
;
4574 bool server_mandatory
= false;
4575 bool server_allowed
= false;
4576 const char *server_signing
= NULL
;
4581 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4586 server_security_mode
= CVAL(vwv
+ 1, 0);
4587 server_max_mux
= SVAL(vwv
+ 1, 1);
4588 server_max_xmit
= IVAL(vwv
+ 3, 1);
4589 server_session_key
= IVAL(vwv
+ 7, 1);
4590 server_time_zone
= SVALS(vwv
+ 15, 1);
4591 server_time_zone
*= 60;
4592 /* this time arrives in real GMT */
4593 server_system_time
= BVAL(vwv
+ 11, 1);
4594 server_capabilities
= IVAL(vwv
+ 9, 1);
4596 key_len
= CVAL(vwv
+ 16, 1);
4598 if (server_capabilities
& CAP_RAW_MODE
) {
4599 server_readbraw
= true;
4600 server_writebraw
= true;
4602 if (server_capabilities
& CAP_LOCK_AND_READ
) {
4603 server_lockread
= true;
4606 if (server_capabilities
& CAP_EXTENDED_SECURITY
) {
4607 DATA_BLOB blob1
, blob2
;
4609 if (num_bytes
< 16) {
4610 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4614 blob1
= data_blob_const(bytes
, 16);
4615 status
= GUID_from_data_blob(&blob1
, &server_guid
);
4616 if (tevent_req_nterror(req
, status
)) {
4620 blob1
= data_blob_const(bytes
+16, num_bytes
-16);
4621 blob2
= data_blob_dup_talloc(state
, blob1
);
4622 if (blob1
.length
> 0 &&
4623 tevent_req_nomem(blob2
.data
, req
)) {
4626 server_gss_blob
= blob2
;
4628 DATA_BLOB blob1
, blob2
;
4630 if (num_bytes
< key_len
) {
4631 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4635 if (key_len
!= 0 && key_len
!= 8) {
4636 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4641 memcpy(server_challenge
, bytes
, 8);
4644 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4645 blob2
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4646 if (blob1
.length
> 0) {
4649 len
= utf16_null_terminated_len_n(blob1
.data
,
4653 ok
= convert_string_talloc(state
,
4661 status
= map_nt_error_from_unix_common(errno
);
4662 tevent_req_nterror(req
, status
);
4667 blob2
.data
+= blob1
.length
;
4668 blob2
.length
-= blob1
.length
;
4669 if (blob2
.length
> 0) {
4672 ok
= convert_string_talloc(state
,
4680 status
= map_nt_error_from_unix_common(errno
);
4681 tevent_req_nterror(req
, status
);
4687 client_signing
= "disabled";
4688 if (conn
->allow_signing
) {
4689 client_signing
= "allowed";
4691 if (conn
->mandatory_signing
) {
4692 client_signing
= "required";
4695 server_signing
= "not supported";
4696 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_ENABLED
) {
4697 server_signing
= "supported";
4698 server_allowed
= true;
4699 } else if (conn
->mandatory_signing
) {
4701 * We have mandatory signing as client
4702 * lets assume the server will look at our
4703 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4704 * flag in the session setup
4706 server_signing
= "not announced";
4707 server_allowed
= true;
4709 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_REQUIRED
) {
4710 server_signing
= "required";
4711 server_mandatory
= true;
4714 ok
= smb1_signing_set_negotiated(conn
->smb1
.signing
,
4718 DEBUG(1,("cli_negprot: SMB signing is required, "
4719 "but client[%s] and server[%s] mismatch\n",
4720 client_signing
, server_signing
));
4721 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
4725 } else if (conn
->protocol
>= PROTOCOL_LANMAN1
) {
4731 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4735 server_security_mode
= SVAL(vwv
+ 1, 0);
4736 server_max_xmit
= SVAL(vwv
+ 2, 0);
4737 server_max_mux
= SVAL(vwv
+ 3, 0);
4738 server_readbraw
= ((SVAL(vwv
+ 5, 0) & 0x1) != 0);
4739 server_writebraw
= ((SVAL(vwv
+ 5, 0) & 0x2) != 0);
4740 server_session_key
= IVAL(vwv
+ 6, 0);
4741 server_time_zone
= SVALS(vwv
+ 10, 0);
4742 server_time_zone
*= 60;
4743 /* this time is converted to GMT by make_unix_date */
4744 t
= pull_dos_date((const uint8_t *)(vwv
+ 8), server_time_zone
);
4745 unix_to_nt_time(&server_system_time
, t
);
4746 key_len
= SVAL(vwv
+ 11, 0);
4748 if (num_bytes
< key_len
) {
4749 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4753 if (key_len
!= 0 && key_len
!= 8) {
4754 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4759 memcpy(server_challenge
, bytes
, 8);
4762 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4763 if (blob1
.length
> 0) {
4767 len
= utf16_null_terminated_len_n(blob1
.data
,
4771 ok
= convert_string_talloc(state
,
4779 status
= map_nt_error_from_unix_common(errno
);
4780 tevent_req_nterror(req
, status
);
4786 /* the old core protocol */
4787 server_time_zone
= get_time_zone(time(NULL
));
4788 server_max_xmit
= 1024;
4792 if (server_max_xmit
< 1024) {
4793 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4797 if (server_max_mux
< 1) {
4798 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4803 * Now calculate the negotiated capabilities
4804 * based on the mask for:
4805 * - client only flags
4806 * - flags used in both directions
4807 * - server only flags
4809 both_capabilities
= client_capabilities
& server_capabilities
;
4810 capabilities
= client_capabilities
& SMB_CAP_CLIENT_MASK
;
4811 capabilities
|= both_capabilities
& SMB_CAP_BOTH_MASK
;
4812 capabilities
|= server_capabilities
& SMB_CAP_SERVER_MASK
;
4814 max_xmit
= MIN(client_max_xmit
, server_max_xmit
);
4816 conn
->smb1
.server
.capabilities
= server_capabilities
;
4817 conn
->smb1
.capabilities
= capabilities
;
4819 conn
->smb1
.server
.max_xmit
= server_max_xmit
;
4820 conn
->smb1
.max_xmit
= max_xmit
;
4822 conn
->smb1
.server
.max_mux
= server_max_mux
;
4824 conn
->smb1
.server
.security_mode
= server_security_mode
;
4826 conn
->smb1
.server
.readbraw
= server_readbraw
;
4827 conn
->smb1
.server
.writebraw
= server_writebraw
;
4828 conn
->smb1
.server
.lockread
= server_lockread
;
4829 conn
->smb1
.server
.writeunlock
= server_writeunlock
;
4831 conn
->smb1
.server
.session_key
= server_session_key
;
4833 talloc_steal(conn
, server_gss_blob
.data
);
4834 conn
->smb1
.server
.gss_blob
= server_gss_blob
;
4835 conn
->smb1
.server
.guid
= server_guid
;
4836 memcpy(conn
->smb1
.server
.challenge
, server_challenge
, 8);
4837 conn
->smb1
.server
.workgroup
= talloc_move(conn
, &server_workgroup
);
4838 conn
->smb1
.server
.name
= talloc_move(conn
, &server_name
);
4840 conn
->smb1
.server
.time_zone
= server_time_zone
;
4841 conn
->smb1
.server
.system_time
= server_system_time
;
4843 tevent_req_done(req
);
4846 static size_t smbXcli_padding_helper(uint32_t offset
, size_t n
)
4848 if ((offset
& (n
-1)) == 0) return 0;
4849 return n
- (offset
& (n
-1));
4852 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
)
4856 uint16_t dialect_count
= 0;
4857 DATA_BLOB dyn
= data_blob_null
;
4859 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4863 if (smb2cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
4867 if (smb2cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
4871 SSVAL(val
, 0, smb2cli_prots
[i
].smb2_dialect
);
4873 ok
= data_blob_append(state
, &dyn
, val
, sizeof(val
));
4881 buf
= state
->smb2
.fixed
;
4883 SSVAL(buf
, 2, dialect_count
);
4884 SSVAL(buf
, 4, state
->conn
->smb2
.client
.security_mode
);
4885 SSVAL(buf
, 6, 0); /* Reserved */
4886 if (state
->conn
->max_protocol
>= PROTOCOL_SMB3_00
) {
4887 SIVAL(buf
, 8, state
->conn
->smb2
.client
.capabilities
);
4889 SIVAL(buf
, 8, 0); /* Capabilities */
4891 if (state
->conn
->max_protocol
>= PROTOCOL_SMB2_10
) {
4892 struct GUID_ndr_buf guid_buf
= { .buf
= {0}, };
4894 GUID_to_ndr_buf(&state
->conn
->smb2
.client
.guid
, &guid_buf
);
4895 memcpy(buf
+12, guid_buf
.buf
, 16); /* ClientGuid */
4897 memset(buf
+12, 0, 16); /* ClientGuid */
4900 if (state
->conn
->max_protocol
>= PROTOCOL_SMB3_11
) {
4901 const struct smb3_signing_capabilities
*client_sign_algos
=
4902 &state
->conn
->smb2
.client
.smb3_capabilities
.signing
;
4903 const struct smb3_encryption_capabilities
*client_ciphers
=
4904 &state
->conn
->smb2
.client
.smb3_capabilities
.encryption
;
4906 struct smb2_negotiate_contexts c
= { .num_contexts
= 0, };
4907 uint8_t *netname_utf16
= NULL
;
4908 size_t netname_utf16_len
= 0;
4912 const uint8_t zeros
[8] = {0, };
4916 SSVAL(p
, 0, 1); /* HashAlgorithmCount */
4917 SSVAL(p
, 2, 32); /* SaltLength */
4918 SSVAL(p
, 4, SMB2_PREAUTH_INTEGRITY_SHA512
);
4919 generate_random_buffer(p
+ 6, 32);
4921 status
= smb2_negotiate_context_add(
4922 state
, &c
, SMB2_PREAUTH_INTEGRITY_CAPABILITIES
, p
, 38);
4923 if (!NT_STATUS_IS_OK(status
)) {
4927 if (client_ciphers
->num_algos
> 0) {
4929 SSVAL(p
, ofs
, client_ciphers
->num_algos
);
4932 for (i
= 0; i
< client_ciphers
->num_algos
; i
++) {
4933 size_t next_ofs
= ofs
+ 2;
4934 SMB_ASSERT(next_ofs
< ARRAY_SIZE(p
));
4935 SSVAL(p
, ofs
, client_ciphers
->algos
[i
]);
4939 status
= smb2_negotiate_context_add(
4940 state
, &c
, SMB2_ENCRYPTION_CAPABILITIES
, p
, ofs
);
4941 if (!NT_STATUS_IS_OK(status
)) {
4946 if (client_sign_algos
->num_algos
> 0) {
4948 SSVAL(p
, ofs
, client_sign_algos
->num_algos
);
4951 for (i
= 0; i
< client_sign_algos
->num_algos
; i
++) {
4952 size_t next_ofs
= ofs
+ 2;
4953 SMB_ASSERT(next_ofs
< ARRAY_SIZE(p
));
4954 SSVAL(p
, ofs
, client_sign_algos
->algos
[i
]);
4958 status
= smb2_negotiate_context_add(
4959 state
, &c
, SMB2_SIGNING_CAPABILITIES
, p
, ofs
);
4960 if (!NT_STATUS_IS_OK(status
)) {
4965 ok
= convert_string_talloc(state
, CH_UNIX
, CH_UTF16
,
4966 state
->conn
->remote_name
,
4967 strlen(state
->conn
->remote_name
),
4968 &netname_utf16
, &netname_utf16_len
);
4973 status
= smb2_negotiate_context_add(state
, &c
,
4974 SMB2_NETNAME_NEGOTIATE_CONTEXT_ID
,
4975 netname_utf16
, netname_utf16_len
);
4976 if (!NT_STATUS_IS_OK(status
)) {
4980 if (state
->in_ctx
!= NULL
) {
4981 struct smb2_negotiate_contexts
*ctxs
= state
->in_ctx
;
4983 for (i
=0; i
<ctxs
->num_contexts
; i
++) {
4984 struct smb2_negotiate_context
*ctx
=
4987 status
= smb2_negotiate_context_add(
4993 if (!NT_STATUS_IS_OK(status
)) {
4999 status
= smb2_negotiate_context_push(state
, &b
, c
);
5000 if (!NT_STATUS_IS_OK(status
)) {
5004 offset
= SMB2_HDR_BODY
+ sizeof(state
->smb2
.fixed
) + dyn
.length
;
5005 pad
= smbXcli_padding_helper(offset
, 8);
5007 ok
= data_blob_append(state
, &dyn
, zeros
, pad
);
5013 ok
= data_blob_append(state
, &dyn
, b
.data
, b
.length
);
5018 SIVAL(buf
, 28, offset
); /* NegotiateContextOffset */
5019 SSVAL(buf
, 32, c
.num_contexts
); /* NegotiateContextCount */
5020 SSVAL(buf
, 34, 0); /* Reserved */
5022 SBVAL(buf
, 28, 0); /* Reserved/ClientStartTime */
5025 return smb2cli_req_send(state
, state
->ev
,
5026 state
->conn
, SMB2_OP_NEGPROT
,
5028 state
->timeout_msec
,
5029 NULL
, NULL
, /* tcon, session */
5030 state
->smb2
.fixed
, sizeof(state
->smb2
.fixed
),
5031 dyn
.data
, dyn
.length
,
5032 UINT16_MAX
); /* max_dyn_len */
5035 static NTSTATUS
smbXcli_negprot_smb3_check_capabilities(struct tevent_req
*req
);
5037 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
)
5039 struct tevent_req
*req
=
5040 tevent_req_callback_data(subreq
,
5042 struct smbXcli_negprot_state
*state
=
5043 tevent_req_data(req
,
5044 struct smbXcli_negprot_state
);
5045 struct smbXcli_conn
*conn
= state
->conn
;
5046 size_t security_offset
, security_length
;
5049 struct iovec
*iov
= NULL
;
5052 uint16_t dialect_revision
;
5053 uint32_t negotiate_context_offset
= 0;
5054 uint16_t negotiate_context_count
= 0;
5055 DATA_BLOB negotiate_context_blob
= data_blob_null
;
5059 struct smb2_negotiate_context
*preauth
= NULL
;
5060 uint16_t hash_count
;
5061 uint16_t salt_length
;
5062 uint16_t hash_selected
;
5063 gnutls_hash_hd_t hash_hnd
= NULL
;
5064 struct smb2_negotiate_context
*sign_algo
= NULL
;
5065 struct smb2_negotiate_context
*cipher
= NULL
;
5066 struct smb2_negotiate_context
*posix
= NULL
;
5067 struct iovec sent_iov
[3] = {{0}, {0}, {0}};
5068 static const struct smb2cli_req_expected_response expected
[] = {
5070 .status
= NT_STATUS_OK
,
5076 status
= smb2cli_req_recv(subreq
, state
, &iov
,
5077 expected
, ARRAY_SIZE(expected
));
5078 if (tevent_req_nterror(req
, status
)) {
5082 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
5086 body
= (uint8_t *)iov
[1].iov_base
;
5088 dialect_revision
= SVAL(body
, 4);
5090 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
5091 if (smb2cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
5095 if (smb2cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
5099 if (smb2cli_prots
[i
].smb2_dialect
!= dialect_revision
) {
5103 conn
->protocol
= smb2cli_prots
[i
].proto
;
5107 if (conn
->protocol
== PROTOCOL_NONE
) {
5108 TALLOC_FREE(subreq
);
5110 if (state
->conn
->min_protocol
>= PROTOCOL_SMB2_02
) {
5111 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5115 if (dialect_revision
!= SMB2_DIALECT_REVISION_2FF
) {
5116 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5120 /* make sure we do not loop forever */
5121 state
->conn
->min_protocol
= PROTOCOL_SMB2_02
;
5124 * send a SMB2 negprot, in order to negotiate
5127 subreq
= smbXcli_negprot_smb2_subreq(state
);
5128 if (tevent_req_nomem(subreq
, req
)) {
5131 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
5135 conn
->smb2
.server
.security_mode
= SVAL(body
, 2);
5136 if (conn
->protocol
>= PROTOCOL_SMB3_11
) {
5137 negotiate_context_count
= SVAL(body
, 6);
5140 blob
= data_blob_const(body
+ 8, 16);
5141 status
= GUID_from_data_blob(&blob
, &conn
->smb2
.server
.guid
);
5142 if (tevent_req_nterror(req
, status
)) {
5146 conn
->smb2
.server
.capabilities
= IVAL(body
, 24);
5147 conn
->smb2
.server
.max_trans_size
= IVAL(body
, 28);
5148 conn
->smb2
.server
.max_read_size
= IVAL(body
, 32);
5149 conn
->smb2
.server
.max_write_size
= IVAL(body
, 36);
5150 conn
->smb2
.server
.system_time
= BVAL(body
, 40);
5151 conn
->smb2
.server
.start_time
= BVAL(body
, 48);
5153 if (conn
->smb2
.server
.max_trans_size
== 0 ||
5154 conn
->smb2
.server
.max_read_size
== 0 ||
5155 conn
->smb2
.server
.max_write_size
== 0) {
5157 * We can't connect to servers we can't
5158 * do any operations on.
5160 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5164 security_offset
= SVAL(body
, 56);
5165 security_length
= SVAL(body
, 58);
5167 if (security_offset
== 0) {
5169 * Azure sends security_offset = 0 and security_length = 0
5171 * We just set security_offset to the expected value
5172 * in order to allow the further logic to work
5175 if (security_length
!= 0) {
5176 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5179 security_offset
= SMB2_HDR_BODY
+ iov
[1].iov_len
;
5182 if (security_offset
!= SMB2_HDR_BODY
+ iov
[1].iov_len
) {
5183 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5187 if (security_length
> iov
[2].iov_len
) {
5188 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5192 conn
->smb2
.server
.gss_blob
= data_blob_talloc(conn
,
5195 if (tevent_req_nomem(conn
->smb2
.server
.gss_blob
.data
, req
)) {
5199 if (conn
->protocol
>= PROTOCOL_SMB3_00
) {
5200 conn
->smb2
.server
.sign_algo
= SMB2_SIGNING_AES128_CMAC
;
5202 conn
->smb2
.server
.sign_algo
= SMB2_SIGNING_HMAC_SHA256
;
5205 if (conn
->protocol
< PROTOCOL_SMB3_11
) {
5206 TALLOC_FREE(subreq
);
5208 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
) {
5209 conn
->smb2
.server
.cipher
= SMB2_ENCRYPTION_AES128_CCM
;
5212 status
= smbXcli_negprot_smb3_check_capabilities(req
);
5213 if (tevent_req_nterror(req
, status
)) {
5217 tevent_req_done(req
);
5222 * Here we are now at SMB3_11, so encryption should be
5223 * negotiated via context, not capabilities.
5226 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
) {
5228 * Server set SMB2_CAP_ENCRYPTION capability,
5229 * but *SHOULD* not, not *MUST* not. Just mask it off.
5230 * NetApp seems to do this:
5231 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13009
5233 conn
->smb2
.server
.capabilities
&= ~SMB2_CAP_ENCRYPTION
;
5236 negotiate_context_offset
= IVAL(body
, 60);
5237 if (negotiate_context_offset
< security_offset
) {
5238 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5242 ctx_ofs
= negotiate_context_offset
- security_offset
;
5243 if (ctx_ofs
> iov
[2].iov_len
) {
5244 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5247 avail
= iov
[2].iov_len
- security_length
;
5248 needed
= iov
[2].iov_len
- ctx_ofs
;
5249 if (needed
> avail
) {
5250 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5254 negotiate_context_blob
.data
= (uint8_t *)iov
[2].iov_base
;
5255 negotiate_context_blob
.length
= iov
[2].iov_len
;
5257 negotiate_context_blob
.data
+= ctx_ofs
;
5258 negotiate_context_blob
.length
-= ctx_ofs
;
5260 state
->out_ctx
= talloc_zero(state
, struct smb2_negotiate_contexts
);
5261 if (tevent_req_nomem(state
->out_ctx
, req
)) {
5265 status
= smb2_negotiate_context_parse(state
->out_ctx
,
5266 negotiate_context_blob
,
5267 negotiate_context_count
,
5269 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
5270 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
5272 if (tevent_req_nterror(req
, status
)) {
5276 preauth
= smb2_negotiate_context_find(
5277 state
->out_ctx
, SMB2_PREAUTH_INTEGRITY_CAPABILITIES
);
5278 if (preauth
== NULL
) {
5279 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5283 if (preauth
->data
.length
< 6) {
5284 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5288 hash_count
= SVAL(preauth
->data
.data
, 0);
5289 salt_length
= SVAL(preauth
->data
.data
, 2);
5290 hash_selected
= SVAL(preauth
->data
.data
, 4);
5292 if (hash_count
!= 1) {
5293 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5297 if (preauth
->data
.length
!= (6 + salt_length
)) {
5298 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5302 if (hash_selected
!= SMB2_PREAUTH_INTEGRITY_SHA512
) {
5303 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5307 sign_algo
= smb2_negotiate_context_find(
5308 state
->out_ctx
, SMB2_SIGNING_CAPABILITIES
);
5309 if (sign_algo
!= NULL
) {
5310 const struct smb3_signing_capabilities
*client_sign_algos
=
5311 &state
->conn
->smb2
.client
.smb3_capabilities
.signing
;
5312 bool found_selected
= false;
5313 uint16_t sign_algo_count
;
5314 uint16_t sign_algo_selected
;
5316 if (client_sign_algos
->num_algos
== 0) {
5318 * We didn't ask for SMB2_ENCRYPTION_CAPABILITIES
5320 tevent_req_nterror(req
,
5321 NT_STATUS_INVALID_NETWORK_RESPONSE
);
5325 if (sign_algo
->data
.length
< 2) {
5326 tevent_req_nterror(req
,
5327 NT_STATUS_INVALID_NETWORK_RESPONSE
);
5331 sign_algo_count
= SVAL(sign_algo
->data
.data
, 0);
5332 if (sign_algo_count
!= 1) {
5333 tevent_req_nterror(req
,
5334 NT_STATUS_INVALID_NETWORK_RESPONSE
);
5338 if (sign_algo
->data
.length
< (2 + 2 * sign_algo_count
)) {
5339 tevent_req_nterror(req
,
5340 NT_STATUS_INVALID_NETWORK_RESPONSE
);
5343 sign_algo_selected
= SVAL(sign_algo
->data
.data
, 2);
5345 for (i
= 0; i
< client_sign_algos
->num_algos
; i
++) {
5346 if (client_sign_algos
->algos
[i
] == sign_algo_selected
) {
5350 found_selected
= true;
5355 if (!found_selected
) {
5357 * The server send a sign_algo we didn't offer.
5359 tevent_req_nterror(req
,
5360 NT_STATUS_INVALID_NETWORK_RESPONSE
);
5364 conn
->smb2
.server
.sign_algo
= sign_algo_selected
;
5367 cipher
= smb2_negotiate_context_find(
5368 state
->out_ctx
, SMB2_ENCRYPTION_CAPABILITIES
);
5369 if (cipher
!= NULL
) {
5370 const struct smb3_encryption_capabilities
*client_ciphers
=
5371 &state
->conn
->smb2
.client
.smb3_capabilities
.encryption
;
5372 bool found_selected
= false;
5373 uint16_t cipher_count
;
5374 uint16_t cipher_selected
;
5376 if (client_ciphers
->num_algos
== 0) {
5378 * We didn't ask for SMB2_ENCRYPTION_CAPABILITIES
5380 tevent_req_nterror(req
,
5381 NT_STATUS_INVALID_NETWORK_RESPONSE
);
5385 if (cipher
->data
.length
< 2) {
5386 tevent_req_nterror(req
,
5387 NT_STATUS_INVALID_NETWORK_RESPONSE
);
5391 cipher_count
= SVAL(cipher
->data
.data
, 0);
5392 if (cipher_count
!= 1) {
5393 tevent_req_nterror(req
,
5394 NT_STATUS_INVALID_NETWORK_RESPONSE
);
5398 if (cipher
->data
.length
< (2 + 2 * cipher_count
)) {
5399 tevent_req_nterror(req
,
5400 NT_STATUS_INVALID_NETWORK_RESPONSE
);
5403 cipher_selected
= SVAL(cipher
->data
.data
, 2);
5405 for (i
= 0; i
< client_ciphers
->num_algos
; i
++) {
5406 if (cipher_selected
== SMB2_ENCRYPTION_NONE
) {
5408 * encryption not supported
5410 found_selected
= true;
5413 if (client_ciphers
->algos
[i
] == cipher_selected
) {
5417 found_selected
= true;
5422 if (!found_selected
) {
5424 * The server send a cipher we didn't offer.
5426 tevent_req_nterror(req
,
5427 NT_STATUS_INVALID_NETWORK_RESPONSE
);
5431 conn
->smb2
.server
.cipher
= cipher_selected
;
5434 posix
= smb2_negotiate_context_find(
5435 state
->out_ctx
, SMB2_POSIX_EXTENSIONS_AVAILABLE
);
5436 if (posix
!= NULL
) {
5437 DATA_BLOB posix_blob
= data_blob_const(
5438 SMB2_CREATE_TAG_POSIX
, strlen(SMB2_CREATE_TAG_POSIX
));
5439 int cmp
= data_blob_cmp(&posix
->data
, &posix_blob
);
5441 conn
->smb2
.server
.smb311_posix
= (cmp
== 0);
5445 /* First we hash the request */
5446 smb2cli_req_get_sent_iov(subreq
, sent_iov
);
5448 rc
= gnutls_hash_init(&hash_hnd
, GNUTLS_DIG_SHA512
);
5450 tevent_req_nterror(req
,
5451 gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
));
5455 rc
= gnutls_hash(hash_hnd
,
5456 conn
->smb2
.preauth_sha512
,
5457 sizeof(conn
->smb2
.preauth_sha512
));
5459 gnutls_hash_deinit(hash_hnd
, NULL
);
5460 tevent_req_nterror(req
,
5461 gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
));
5464 for (i
= 0; i
< 3; i
++) {
5465 rc
= gnutls_hash(hash_hnd
,
5466 sent_iov
[i
].iov_base
,
5467 sent_iov
[i
].iov_len
);
5469 gnutls_hash_deinit(hash_hnd
, NULL
);
5470 tevent_req_nterror(req
,
5471 gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
));
5476 /* This resets the hash state */
5477 gnutls_hash_output(hash_hnd
, conn
->smb2
.preauth_sha512
);
5478 TALLOC_FREE(subreq
);
5480 /* And now we hash the response */
5481 rc
= gnutls_hash(hash_hnd
,
5482 conn
->smb2
.preauth_sha512
,
5483 sizeof(conn
->smb2
.preauth_sha512
));
5485 gnutls_hash_deinit(hash_hnd
, NULL
);
5486 tevent_req_nterror(req
,
5487 gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
));
5490 for (i
= 0; i
< 3; i
++) {
5491 rc
= gnutls_hash(hash_hnd
,
5495 gnutls_hash_deinit(hash_hnd
, NULL
);
5496 tevent_req_nterror(req
,
5497 gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
));
5501 gnutls_hash_deinit(hash_hnd
, conn
->smb2
.preauth_sha512
);
5503 tevent_req_nterror(req
,
5504 NT_STATUS_UNSUCCESSFUL
);
5508 status
= smbXcli_negprot_smb3_check_capabilities(req
);
5509 if (tevent_req_nterror(req
, status
)) {
5513 tevent_req_done(req
);
5516 static NTSTATUS
smbXcli_negprot_smb3_check_capabilities(struct tevent_req
*req
)
5518 struct smbXcli_negprot_state
*state
=
5519 tevent_req_data(req
,
5520 struct smbXcli_negprot_state
);
5521 struct smbXcli_conn
*conn
= state
->conn
;
5523 return smb311_capabilities_check(&conn
->smb2
.client
.smb3_capabilities
,
5526 NT_STATUS_ACCESS_DENIED
,
5529 conn
->smb2
.server
.sign_algo
,
5530 conn
->smb2
.server
.cipher
);
5533 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
5534 TALLOC_CTX
*tmp_mem
,
5537 size_t num_pending
= talloc_array_length(conn
->pending
);
5538 struct tevent_req
*subreq
;
5539 struct smbXcli_req_state
*substate
;
5540 struct tevent_req
*req
;
5541 uint32_t protocol_magic
;
5542 size_t inbuf_len
= smb_len_nbt(inbuf
);
5544 if (num_pending
!= 1) {
5545 return NT_STATUS_INTERNAL_ERROR
;
5548 if (inbuf_len
< 4) {
5549 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5552 subreq
= conn
->pending
[0];
5553 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
5554 req
= tevent_req_callback_data(subreq
, struct tevent_req
);
5556 protocol_magic
= IVAL(inbuf
, 4);
5558 switch (protocol_magic
) {
5560 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
5561 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
5562 return smb1cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
5565 if (substate
->smb2
.recv_iov
== NULL
) {
5567 * For the SMB1 negprot we have move it.
5569 substate
->smb2
.recv_iov
= substate
->smb1
.recv_iov
;
5570 substate
->smb1
.recv_iov
= NULL
;
5574 * we got an SMB2 answer, which consumed sequence number 0
5575 * so we need to use 1 as the next one.
5577 * we also need to set the current credits to 0
5578 * as we consumed the initial one. The SMB2 answer
5579 * hopefully grant us a new credit.
5582 conn
->smb2
.cur_credits
= 0;
5583 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
5584 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
5585 return smb2cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
5588 DEBUG(10, ("Got non-SMB PDU\n"));
5589 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5592 NTSTATUS
smbXcli_negprot_recv(
5593 struct tevent_req
*req
,
5594 TALLOC_CTX
*mem_ctx
,
5595 struct smb2_negotiate_contexts
**out_ctx
)
5597 struct smbXcli_negprot_state
*state
= tevent_req_data(
5598 req
, struct smbXcli_negprot_state
);
5601 if (tevent_req_is_nterror(req
, &status
)) {
5602 tevent_req_received(req
);
5606 if (out_ctx
!= NULL
) {
5607 *out_ctx
= talloc_move(mem_ctx
, &state
->out_ctx
);
5610 tevent_req_received(req
);
5611 return NT_STATUS_OK
;
5614 NTSTATUS
smbXcli_negprot(struct smbXcli_conn
*conn
,
5615 uint32_t timeout_msec
,
5616 enum protocol_types min_protocol
,
5617 enum protocol_types max_protocol
,
5618 struct smb2_negotiate_contexts
*in_ctx
,
5619 TALLOC_CTX
*mem_ctx
,
5620 struct smb2_negotiate_contexts
**out_ctx
)
5622 TALLOC_CTX
*frame
= talloc_stackframe();
5623 struct tevent_context
*ev
;
5624 struct tevent_req
*req
;
5625 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5628 if (smbXcli_conn_has_async_calls(conn
)) {
5630 * Can't use sync call while an async call is in flight
5632 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
5635 ev
= samba_tevent_context_init(frame
);
5639 req
= smbXcli_negprot_send(
5646 WINDOWS_CLIENT_PURE_SMB2_NEGPROT_INITIAL_CREDIT_ASK
,
5651 ok
= tevent_req_poll_ntstatus(req
, ev
, &status
);
5655 status
= smbXcli_negprot_recv(req
, mem_ctx
, out_ctx
);
5661 struct smb2cli_validate_negotiate_info_state
{
5662 struct smbXcli_conn
*conn
;
5663 DATA_BLOB in_input_buffer
;
5664 DATA_BLOB in_output_buffer
;
5665 DATA_BLOB out_input_buffer
;
5666 DATA_BLOB out_output_buffer
;
5670 static void smb2cli_validate_negotiate_info_done(struct tevent_req
*subreq
);
5672 struct tevent_req
*smb2cli_validate_negotiate_info_send(TALLOC_CTX
*mem_ctx
,
5673 struct tevent_context
*ev
,
5674 struct smbXcli_conn
*conn
,
5675 uint32_t timeout_msec
,
5676 struct smbXcli_session
*session
,
5677 struct smbXcli_tcon
*tcon
)
5679 struct tevent_req
*req
;
5680 struct smb2cli_validate_negotiate_info_state
*state
;
5682 uint16_t dialect_count
= 0;
5683 struct tevent_req
*subreq
;
5684 bool _save_should_sign
;
5687 req
= tevent_req_create(mem_ctx
, &state
,
5688 struct smb2cli_validate_negotiate_info_state
);
5694 state
->in_input_buffer
= data_blob_talloc_zero(state
,
5695 4 + 16 + 1 + 1 + 2);
5696 if (tevent_req_nomem(state
->in_input_buffer
.data
, req
)) {
5697 return tevent_req_post(req
, ev
);
5699 buf
= state
->in_input_buffer
.data
;
5701 if (state
->conn
->max_protocol
>= PROTOCOL_SMB3_00
) {
5702 SIVAL(buf
, 0, conn
->smb2
.client
.capabilities
);
5704 SIVAL(buf
, 0, 0); /* Capabilities */
5706 if (state
->conn
->max_protocol
>= PROTOCOL_SMB2_10
) {
5707 struct GUID_ndr_buf guid_buf
= { .buf
= {0}, };
5709 GUID_to_ndr_buf(&conn
->smb2
.client
.guid
, &guid_buf
);
5710 memcpy(buf
+4, guid_buf
.buf
, 16); /* ClientGuid */
5712 memset(buf
+4, 0, 16); /* ClientGuid */
5714 if (state
->conn
->min_protocol
>= PROTOCOL_SMB2_02
) {
5715 SCVAL(buf
, 20, conn
->smb2
.client
.security_mode
);
5719 SCVAL(buf
, 21, 0); /* reserved */
5721 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
5725 if (smb2cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
5729 if (smb2cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
5733 if (smb2cli_prots
[i
].proto
== state
->conn
->protocol
) {
5734 state
->dialect
= smb2cli_prots
[i
].smb2_dialect
;
5737 ofs
= state
->in_input_buffer
.length
;
5738 ok
= data_blob_realloc(state
, &state
->in_input_buffer
,
5741 tevent_req_oom(req
);
5742 return tevent_req_post(req
, ev
);
5745 buf
= state
->in_input_buffer
.data
;
5746 SSVAL(buf
, ofs
, smb2cli_prots
[i
].smb2_dialect
);
5750 buf
= state
->in_input_buffer
.data
;
5751 SSVAL(buf
, 22, dialect_count
);
5753 _save_should_sign
= smb2cli_tcon_is_signing_on(tcon
);
5754 smb2cli_tcon_should_sign(tcon
, true);
5755 subreq
= smb2cli_ioctl_send(state
, ev
, conn
,
5756 timeout_msec
, session
, tcon
,
5757 UINT64_MAX
, /* in_fid_persistent */
5758 UINT64_MAX
, /* in_fid_volatile */
5759 FSCTL_VALIDATE_NEGOTIATE_INFO
,
5760 0, /* in_max_input_length */
5761 &state
->in_input_buffer
,
5762 24, /* in_max_output_length */
5763 &state
->in_output_buffer
,
5764 SMB2_IOCTL_FLAG_IS_FSCTL
);
5765 smb2cli_tcon_should_sign(tcon
, _save_should_sign
);
5766 if (tevent_req_nomem(subreq
, req
)) {
5767 return tevent_req_post(req
, ev
);
5769 tevent_req_set_callback(subreq
,
5770 smb2cli_validate_negotiate_info_done
,
5776 static void smb2cli_validate_negotiate_info_done(struct tevent_req
*subreq
)
5778 struct tevent_req
*req
=
5779 tevent_req_callback_data(subreq
,
5781 struct smb2cli_validate_negotiate_info_state
*state
=
5782 tevent_req_data(req
,
5783 struct smb2cli_validate_negotiate_info_state
);
5786 uint32_t capabilities
;
5787 DATA_BLOB guid_blob
;
5788 struct GUID server_guid
;
5789 uint16_t security_mode
;
5792 status
= smb2cli_ioctl_recv(subreq
, state
,
5793 &state
->out_input_buffer
,
5794 &state
->out_output_buffer
);
5795 TALLOC_FREE(subreq
);
5798 * This response must be signed correctly for
5799 * these "normal" error codes to be processed.
5800 * If the packet wasn't signed correctly we will get
5801 * NT_STATUS_ACCESS_DENIED or NT_STATUS_HMAC_NOT_SUPPORTED,
5802 * or NT_STATUS_INVALID_NETWORK_RESPONSE
5803 * from smb2_signing_check_pdu().
5805 * We must never ignore the above errors here.
5808 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_CLOSED
)) {
5810 * The response was signed, but not supported
5812 * Older Windows and Samba releases return
5813 * NT_STATUS_FILE_CLOSED.
5815 tevent_req_done(req
);
5818 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_DEVICE_REQUEST
)) {
5820 * The response was signed, but not supported
5822 * This is returned by the NTVFS based Samba 4.x file server
5825 tevent_req_done(req
);
5828 if (NT_STATUS_EQUAL(status
, NT_STATUS_FS_DRIVER_REQUIRED
)) {
5830 * The response was signed, but not supported
5832 * This is returned by the NTVFS based Samba 4.x file server
5835 tevent_req_done(req
);
5838 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_SUPPORTED
)) {
5840 * The response was signed, but not supported
5842 * This might be returned by older Windows versions or by
5843 * NetApp SMB server implementations.
5847 * https://blogs.msdn.microsoft.com/openspecification/2012/06/28/smb3-secure-dialect-negotiation/
5850 tevent_req_done(req
);
5853 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
5855 * The response was signed, but not supported
5857 * This might be returned by NetApp Ontap 7.3.7 SMB server
5860 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=14607
5863 tevent_req_done(req
);
5866 if (tevent_req_nterror(req
, status
)) {
5870 if (state
->out_output_buffer
.length
!= 24) {
5871 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5875 buf
= state
->out_output_buffer
.data
;
5877 capabilities
= IVAL(buf
, 0);
5878 guid_blob
= data_blob_const(buf
+ 4, 16);
5879 status
= GUID_from_data_blob(&guid_blob
, &server_guid
);
5880 if (tevent_req_nterror(req
, status
)) {
5883 security_mode
= CVAL(buf
, 20);
5884 dialect
= SVAL(buf
, 22);
5886 if (capabilities
!= state
->conn
->smb2
.server
.capabilities
) {
5887 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
5891 if (!GUID_equal(&server_guid
, &state
->conn
->smb2
.server
.guid
)) {
5892 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
5896 if (security_mode
!= state
->conn
->smb2
.server
.security_mode
) {
5897 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
5901 if (dialect
!= state
->dialect
) {
5902 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
5906 tevent_req_done(req
);
5909 NTSTATUS
smb2cli_validate_negotiate_info_recv(struct tevent_req
*req
)
5911 return tevent_req_simple_recv_ntstatus(req
);
5914 static int smbXcli_session_destructor(struct smbXcli_session
*session
)
5916 if (session
->conn
== NULL
) {
5920 DLIST_REMOVE(session
->conn
->sessions
, session
);
5924 struct smbXcli_session
*smbXcli_session_create(TALLOC_CTX
*mem_ctx
,
5925 struct smbXcli_conn
*conn
)
5927 struct smbXcli_session
*session
;
5930 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
5931 if (session
== NULL
) {
5934 session
->smb2
= talloc_zero(session
, struct smb2cli_session
);
5935 if (session
->smb2
== NULL
) {
5936 talloc_free(session
);
5939 talloc_set_destructor(session
, smbXcli_session_destructor
);
5941 status
= smb2_signing_key_sign_create(session
->smb2
,
5942 conn
->smb2
.server
.sign_algo
,
5943 NULL
, /* no master key */
5944 NULL
, /* derivations */
5945 &session
->smb2
->signing_key
);
5946 if (!NT_STATUS_IS_OK(status
)) {
5947 talloc_free(session
);
5951 DLIST_ADD_END(conn
->sessions
, session
);
5952 session
->conn
= conn
;
5954 status
= smb2_signing_key_sign_create(session
,
5955 conn
->smb2
.server
.sign_algo
,
5956 NULL
, /* no master key */
5957 NULL
, /* derivations */
5958 &session
->smb2_channel
.signing_key
);
5959 if (!NT_STATUS_IS_OK(status
)) {
5960 talloc_free(session
);
5964 memcpy(session
->smb2_channel
.preauth_sha512
,
5965 conn
->smb2
.preauth_sha512
,
5966 sizeof(session
->smb2_channel
.preauth_sha512
));
5971 struct smbXcli_session
*smbXcli_session_shallow_copy(TALLOC_CTX
*mem_ctx
,
5972 struct smbXcli_session
*src
)
5974 struct smbXcli_session
*session
;
5978 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
5979 if (session
== NULL
) {
5982 session
->smb2
= talloc_zero(session
, struct smb2cli_session
);
5983 if (session
->smb2
== NULL
) {
5984 talloc_free(session
);
5989 * Note we keep a pointer to the session keys of the
5990 * main session and rely on the caller to free the
5991 * shallow copy first!
5993 session
->conn
= src
->conn
;
5994 *session
->smb2
= *src
->smb2
;
5995 session
->smb2_channel
= src
->smb2_channel
;
5996 session
->disconnect_expired
= src
->disconnect_expired
;
5999 * This is only supposed to be called in test code
6000 * but we should not reuse nonces!
6002 * Add the current timestamp as NTTIME to nonce_high
6003 * and set nonce_low to a value we can recognize in captures.
6005 clock_gettime_mono(&ts
);
6006 nt
= unix_timespec_to_nt_time(ts
);
6007 nt
&= session
->smb2
->nonce_high_max
;
6008 if (nt
== session
->smb2
->nonce_high_max
|| nt
< UINT8_MAX
) {
6009 talloc_free(session
);
6012 session
->smb2
->nonce_high
+= nt
;
6013 session
->smb2
->nonce_low
= UINT32_MAX
;
6015 DLIST_ADD_END(src
->conn
->sessions
, session
);
6016 talloc_set_destructor(session
, smbXcli_session_destructor
);
6021 bool smbXcli_session_is_guest(struct smbXcli_session
*session
)
6023 if (session
== NULL
) {
6027 if (session
->conn
== NULL
) {
6031 if (session
->conn
->mandatory_signing
) {
6035 if (session
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
6036 if (session
->smb2
->session_flags
& SMB2_SESSION_FLAG_IS_GUEST
) {
6042 if (session
->smb1
.action
& SMB_SETUP_GUEST
) {
6049 bool smbXcli_session_is_authenticated(struct smbXcli_session
*session
)
6051 const DATA_BLOB
*application_key
;
6053 if (session
== NULL
) {
6057 if (session
->conn
== NULL
) {
6062 * If we have an application key we had a session key negotiated
6065 if (session
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
6066 if (!smb2_signing_key_valid(session
->smb2
->application_key
)) {
6069 application_key
= &session
->smb2
->application_key
->blob
;
6071 application_key
= &session
->smb1
.application_key
;
6074 if (application_key
->length
== 0) {
6081 NTSTATUS
smb2cli_session_signing_key(struct smbXcli_session
*session
,
6082 TALLOC_CTX
*mem_ctx
,
6085 const struct smb2_signing_key
*sig
= NULL
;
6087 if (session
->conn
== NULL
) {
6088 return NT_STATUS_NO_USER_SESSION_KEY
;
6092 * Use channel signing key if there is one, otherwise fallback
6096 if (smb2_signing_key_valid(session
->smb2_channel
.signing_key
)) {
6097 sig
= session
->smb2_channel
.signing_key
;
6098 } else if (smb2_signing_key_valid(session
->smb2
->signing_key
)) {
6099 sig
= session
->smb2
->signing_key
;
6101 return NT_STATUS_NO_USER_SESSION_KEY
;
6104 *key
= data_blob_dup_talloc(mem_ctx
, sig
->blob
);
6105 if (key
->data
== NULL
) {
6106 return NT_STATUS_NO_MEMORY
;
6109 return NT_STATUS_OK
;
6112 NTSTATUS
smb2cli_session_encryption_key(struct smbXcli_session
*session
,
6113 TALLOC_CTX
*mem_ctx
,
6116 if (session
->conn
== NULL
) {
6117 return NT_STATUS_NO_USER_SESSION_KEY
;
6120 if (session
->conn
->protocol
< PROTOCOL_SMB3_00
) {
6121 return NT_STATUS_NO_USER_SESSION_KEY
;
6124 if (!smb2_signing_key_valid(session
->smb2
->encryption_key
)) {
6125 return NT_STATUS_NO_USER_SESSION_KEY
;
6128 *key
= data_blob_dup_talloc(mem_ctx
, session
->smb2
->encryption_key
->blob
);
6129 if (key
->data
== NULL
) {
6130 return NT_STATUS_NO_MEMORY
;
6133 return NT_STATUS_OK
;
6136 NTSTATUS
smb2cli_session_decryption_key(struct smbXcli_session
*session
,
6137 TALLOC_CTX
*mem_ctx
,
6140 if (session
->conn
== NULL
) {
6141 return NT_STATUS_NO_USER_SESSION_KEY
;
6144 if (session
->conn
->protocol
< PROTOCOL_SMB3_00
) {
6145 return NT_STATUS_NO_USER_SESSION_KEY
;
6148 if (!smb2_signing_key_valid(session
->smb2
->decryption_key
)) {
6149 return NT_STATUS_NO_USER_SESSION_KEY
;
6152 *key
= data_blob_dup_talloc(mem_ctx
, session
->smb2
->decryption_key
->blob
);
6153 if (key
->data
== NULL
) {
6154 return NT_STATUS_NO_MEMORY
;
6157 return NT_STATUS_OK
;
6160 NTSTATUS
smbXcli_session_application_key(struct smbXcli_session
*session
,
6161 TALLOC_CTX
*mem_ctx
,
6164 const DATA_BLOB
*application_key
;
6166 *key
= data_blob_null
;
6168 if (session
->conn
== NULL
) {
6169 return NT_STATUS_NO_USER_SESSION_KEY
;
6172 if (session
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
6173 if (!smb2_signing_key_valid(session
->smb2
->application_key
)) {
6174 return NT_STATUS_NO_USER_SESSION_KEY
;
6176 application_key
= &session
->smb2
->application_key
->blob
;
6178 application_key
= &session
->smb1
.application_key
;
6181 if (application_key
->length
== 0) {
6182 return NT_STATUS_NO_USER_SESSION_KEY
;
6185 *key
= data_blob_dup_talloc(mem_ctx
, *application_key
);
6186 if (key
->data
== NULL
) {
6187 return NT_STATUS_NO_MEMORY
;
6190 return NT_STATUS_OK
;
6193 void smbXcli_session_set_disconnect_expired(struct smbXcli_session
*session
)
6195 session
->disconnect_expired
= true;
6198 uint16_t smb1cli_session_current_id(struct smbXcli_session
*session
)
6200 return session
->smb1
.session_id
;
6203 void smb1cli_session_set_id(struct smbXcli_session
*session
,
6204 uint16_t session_id
)
6206 session
->smb1
.session_id
= session_id
;
6209 void smb1cli_session_set_action(struct smbXcli_session
*session
,
6212 session
->smb1
.action
= action
;
6215 NTSTATUS
smb1cli_session_set_session_key(struct smbXcli_session
*session
,
6216 const DATA_BLOB _session_key
)
6218 struct smbXcli_conn
*conn
= session
->conn
;
6219 uint8_t session_key
[16];
6222 return NT_STATUS_INVALID_PARAMETER_MIX
;
6225 if (session
->smb1
.application_key
.length
!= 0) {
6227 * TODO: do not allow this...
6229 * return NT_STATUS_INVALID_PARAMETER_MIX;
6231 data_blob_clear_free(&session
->smb1
.application_key
);
6232 session
->smb1
.protected_key
= false;
6235 if (_session_key
.length
== 0) {
6236 return NT_STATUS_OK
;
6239 ZERO_STRUCT(session_key
);
6240 memcpy(session_key
, _session_key
.data
,
6241 MIN(_session_key
.length
, sizeof(session_key
)));
6243 session
->smb1
.application_key
= data_blob_talloc(session
,
6245 sizeof(session_key
));
6246 ZERO_STRUCT(session_key
);
6247 if (session
->smb1
.application_key
.data
== NULL
) {
6248 return NT_STATUS_NO_MEMORY
;
6251 session
->smb1
.protected_key
= false;
6253 return NT_STATUS_OK
;
6256 NTSTATUS
smb1cli_session_protect_session_key(struct smbXcli_session
*session
)
6260 if (session
->smb1
.protected_key
) {
6261 /* already protected */
6262 return NT_STATUS_OK
;
6265 if (session
->smb1
.application_key
.length
!= 16) {
6266 return NT_STATUS_INVALID_PARAMETER_MIX
;
6269 status
= smb1_key_derivation(session
->smb1
.application_key
.data
,
6270 session
->smb1
.application_key
.length
,
6271 session
->smb1
.application_key
.data
);
6272 if (!NT_STATUS_IS_OK(status
)) {
6276 session
->smb1
.protected_key
= true;
6278 return NT_STATUS_OK
;
6281 uint8_t smb2cli_session_security_mode(struct smbXcli_session
*session
)
6283 struct smbXcli_conn
*conn
= session
->conn
;
6284 uint8_t security_mode
= 0;
6287 return security_mode
;
6290 security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
6291 if (conn
->mandatory_signing
) {
6292 security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
6294 if (session
->smb2
->should_sign
) {
6295 security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
6298 return security_mode
;
6301 uint64_t smb2cli_session_current_id(struct smbXcli_session
*session
)
6303 return session
->smb2
->session_id
;
6306 uint16_t smb2cli_session_get_flags(struct smbXcli_session
*session
)
6308 return session
->smb2
->session_flags
;
6311 void smb2cli_session_set_id_and_flags(struct smbXcli_session
*session
,
6312 uint64_t session_id
,
6313 uint16_t session_flags
)
6315 session
->smb2
->session_id
= session_id
;
6316 session
->smb2
->session_flags
= session_flags
;
6319 void smb2cli_session_increment_channel_sequence(struct smbXcli_session
*session
)
6321 session
->smb2
->channel_sequence
+= 1;
6324 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session
*session
,
6325 uint16_t channel_sequence
)
6329 prev_cs
= session
->smb2
->channel_sequence
;
6330 session
->smb2
->channel_sequence
= channel_sequence
;
6335 uint16_t smb2cli_session_current_channel_sequence(struct smbXcli_session
*session
)
6337 return session
->smb2
->channel_sequence
;
6340 void smb2cli_session_start_replay(struct smbXcli_session
*session
)
6342 session
->smb2
->replay_active
= true;
6345 void smb2cli_session_stop_replay(struct smbXcli_session
*session
)
6347 session
->smb2
->replay_active
= false;
6350 void smb2cli_session_require_signed_response(struct smbXcli_session
*session
,
6351 bool require_signed_response
)
6353 session
->smb2
->require_signed_response
= require_signed_response
;
6356 void smb2cli_session_torture_anonymous_signing(struct smbXcli_session
*session
,
6357 bool anonymous_signing
)
6359 session
->smb2
->anonymous_signing
= anonymous_signing
;
6362 void smb2cli_session_torture_anonymous_encryption(struct smbXcli_session
*session
,
6363 bool anonymous_encryption
)
6365 session
->smb2
->anonymous_encryption
= anonymous_encryption
;
6368 void smb2cli_session_torture_no_signing_disconnect(struct smbXcli_session
*session
)
6370 session
->smb2
->no_signing_disconnect
= true;
6373 NTSTATUS
smb2cli_session_update_preauth(struct smbXcli_session
*session
,
6374 const struct iovec
*iov
)
6376 gnutls_hash_hd_t hash_hnd
= NULL
;
6380 if (session
->conn
== NULL
) {
6381 return NT_STATUS_INTERNAL_ERROR
;
6384 if (session
->conn
->protocol
< PROTOCOL_SMB3_11
) {
6385 return NT_STATUS_OK
;
6388 if (smb2_signing_key_valid(session
->smb2_channel
.signing_key
)) {
6389 return NT_STATUS_OK
;
6392 rc
= gnutls_hash_init(&hash_hnd
,
6395 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
);
6398 rc
= gnutls_hash(hash_hnd
,
6399 session
->smb2_channel
.preauth_sha512
,
6400 sizeof(session
->smb2_channel
.preauth_sha512
));
6402 gnutls_hash_deinit(hash_hnd
, NULL
);
6403 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
);
6405 for (i
= 0; i
< 3; i
++) {
6406 rc
= gnutls_hash(hash_hnd
,
6410 gnutls_hash_deinit(hash_hnd
, NULL
);
6411 return gnutls_error_to_ntstatus(rc
, NT_STATUS_HASH_NOT_SUPPORTED
);
6414 gnutls_hash_deinit(hash_hnd
, session
->smb2_channel
.preauth_sha512
);
6416 return NT_STATUS_OK
;
6419 NTSTATUS
smb2cli_session_set_session_key(struct smbXcli_session
*session
,
6420 const DATA_BLOB _session_key
,
6421 const struct iovec
*recv_iov
)
6423 struct smbXcli_conn
*conn
= session
->conn
;
6424 uint16_t no_sign_flags
= 0;
6425 bool check_signature
= true;
6428 struct smb2_signing_derivations derivations
= {
6431 DATA_BLOB preauth_hash
= data_blob_null
;
6432 size_t nonce_size
= 0;
6435 return NT_STATUS_INVALID_PARAMETER_MIX
;
6438 if (recv_iov
[0].iov_len
!= SMB2_HDR_BODY
) {
6439 return NT_STATUS_INVALID_PARAMETER_MIX
;
6442 if (!conn
->mandatory_signing
) {
6444 * only allow guest sessions without
6445 * mandatory signing.
6447 * If we try an authentication with username != ""
6448 * and the server let us in without verifying the
6449 * password we don't have a negotiated session key
6452 no_sign_flags
= SMB2_SESSION_FLAG_IS_GUEST
;
6455 if (session
->smb2
->session_flags
& no_sign_flags
) {
6456 session
->smb2
->should_sign
= false;
6457 return NT_STATUS_OK
;
6460 if (smb2_signing_key_valid(session
->smb2
->signing_key
)) {
6461 return NT_STATUS_INVALID_PARAMETER_MIX
;
6464 if (conn
->protocol
>= PROTOCOL_SMB3_11
) {
6465 preauth_hash
= data_blob_const(session
->smb2_channel
.preauth_sha512
,
6466 sizeof(session
->smb2_channel
.preauth_sha512
));
6469 smb2_signing_derivations_fill_const_stack(&derivations
,
6473 if (session
->smb2
->anonymous_encryption
) {
6474 goto skip_signing_key
;
6477 status
= smb2_signing_key_sign_create(session
->smb2
,
6478 conn
->smb2
.server
.sign_algo
,
6480 derivations
.signing
,
6481 &session
->smb2
->signing_key
);
6482 if (!NT_STATUS_IS_OK(status
)) {
6486 if (session
->smb2
->anonymous_signing
) {
6488 * skip encryption and application keys
6490 goto skip_application_key
;
6495 status
= smb2_signing_key_cipher_create(session
->smb2
,
6496 conn
->smb2
.server
.cipher
,
6498 derivations
.cipher_c2s
,
6499 &session
->smb2
->encryption_key
);
6500 if (!NT_STATUS_IS_OK(status
)) {
6504 status
= smb2_signing_key_cipher_create(session
->smb2
,
6505 conn
->smb2
.server
.cipher
,
6507 derivations
.cipher_s2c
,
6508 &session
->smb2
->decryption_key
);
6509 if (!NT_STATUS_IS_OK(status
)) {
6513 if (session
->smb2
->anonymous_encryption
) {
6514 goto skip_application_key
;
6517 status
= smb2_signing_key_sign_create(session
->smb2
,
6518 conn
->smb2
.server
.sign_algo
,
6520 derivations
.application
,
6521 &session
->smb2
->application_key
);
6522 if (!NT_STATUS_IS_OK(status
)) {
6526 skip_application_key
:
6528 status
= smb2_signing_key_copy(session
,
6529 session
->smb2
->signing_key
,
6530 &session
->smb2_channel
.signing_key
);
6531 if (!NT_STATUS_IS_OK(status
)) {
6535 check_signature
= conn
->mandatory_signing
;
6537 if (conn
->protocol
>= PROTOCOL_SMB3_11
) {
6538 check_signature
= true;
6541 if (session
->smb2
->anonymous_signing
) {
6542 check_signature
= false;
6545 if (session
->smb2
->anonymous_encryption
) {
6546 check_signature
= false;
6549 hdr_flags
= IVAL(recv_iov
[0].iov_base
, SMB2_HDR_FLAGS
);
6550 if (hdr_flags
& SMB2_HDR_FLAG_SIGNED
) {
6552 * Sadly some vendors don't sign the
6553 * final SMB2 session setup response
6555 * At least Windows and Samba are always doing this
6556 * if there's a session key available.
6558 * We only check the signature if it's mandatory
6559 * or SMB2_HDR_FLAG_SIGNED is provided.
6561 check_signature
= true;
6564 if (check_signature
) {
6565 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
6567 if (!NT_STATUS_IS_OK(status
)) {
6572 session
->smb2
->should_sign
= false;
6573 session
->smb2
->should_encrypt
= false;
6575 if (conn
->desire_signing
) {
6576 session
->smb2
->should_sign
= true;
6579 if (conn
->smb2
.server
.security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) {
6580 session
->smb2
->should_sign
= true;
6583 if (session
->smb2
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
) {
6584 session
->smb2
->should_encrypt
= true;
6587 if (conn
->protocol
< PROTOCOL_SMB3_00
) {
6588 session
->smb2
->should_encrypt
= false;
6591 if (conn
->smb2
.server
.cipher
== 0) {
6592 session
->smb2
->should_encrypt
= false;
6595 if (session
->smb2
->anonymous_signing
) {
6596 session
->smb2
->should_sign
= true;
6599 if (session
->smb2
->anonymous_encryption
) {
6600 session
->smb2
->should_encrypt
= true;
6601 session
->smb2
->should_sign
= false;
6605 * CCM and GCM algorithms must never have their
6606 * nonce wrap, or the security of the whole
6607 * communication and the keys is destroyed.
6608 * We must drop the connection once we have
6609 * transferred too much data.
6611 * NOTE: We assume nonces greater than 8 bytes.
6613 generate_nonce_buffer((uint8_t *)&session
->smb2
->nonce_high_random
,
6614 sizeof(session
->smb2
->nonce_high_random
));
6615 switch (conn
->smb2
.server
.cipher
) {
6616 case SMB2_ENCRYPTION_AES128_CCM
:
6617 nonce_size
= SMB2_AES_128_CCM_NONCE_SIZE
;
6619 case SMB2_ENCRYPTION_AES128_GCM
:
6620 nonce_size
= gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_128_GCM
);
6622 case SMB2_ENCRYPTION_AES256_CCM
:
6623 nonce_size
= SMB2_AES_128_CCM_NONCE_SIZE
;
6625 case SMB2_ENCRYPTION_AES256_GCM
:
6626 nonce_size
= gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_256_GCM
);
6632 session
->smb2
->nonce_high_max
= SMB2_NONCE_HIGH_MAX(nonce_size
);
6633 session
->smb2
->nonce_high
= 0;
6634 session
->smb2
->nonce_low
= 0;
6636 return NT_STATUS_OK
;
6639 NTSTATUS
smb2cli_session_create_channel(TALLOC_CTX
*mem_ctx
,
6640 struct smbXcli_session
*session1
,
6641 struct smbXcli_conn
*conn
,
6642 struct smbXcli_session
**_session2
)
6644 struct smbXcli_session
*session2
;
6647 if (!smb2_signing_key_valid(session1
->smb2
->signing_key
)) {
6648 return NT_STATUS_INVALID_PARAMETER_MIX
;
6652 return NT_STATUS_INVALID_PARAMETER_MIX
;
6655 session2
= talloc_zero(mem_ctx
, struct smbXcli_session
);
6656 if (session2
== NULL
) {
6657 return NT_STATUS_NO_MEMORY
;
6659 session2
->smb2
= talloc_reference(session2
, session1
->smb2
);
6660 if (session2
->smb2
== NULL
) {
6661 talloc_free(session2
);
6662 return NT_STATUS_NO_MEMORY
;
6665 talloc_set_destructor(session2
, smbXcli_session_destructor
);
6666 DLIST_ADD_END(conn
->sessions
, session2
);
6667 session2
->conn
= conn
;
6669 status
= smb2_signing_key_sign_create(session2
,
6670 conn
->smb2
.server
.sign_algo
,
6671 NULL
, /* no master key */
6672 NULL
, /* derivations */
6673 &session2
->smb2_channel
.signing_key
);
6674 if (!NT_STATUS_IS_OK(status
)) {
6675 talloc_free(session2
);
6676 return NT_STATUS_NO_MEMORY
;
6679 memcpy(session2
->smb2_channel
.preauth_sha512
,
6680 conn
->smb2
.preauth_sha512
,
6681 sizeof(session2
->smb2_channel
.preauth_sha512
));
6683 *_session2
= session2
;
6684 return NT_STATUS_OK
;
6687 NTSTATUS
smb2cli_session_set_channel_key(struct smbXcli_session
*session
,
6688 const DATA_BLOB _channel_key
,
6689 const struct iovec
*recv_iov
)
6691 struct smbXcli_conn
*conn
= session
->conn
;
6692 uint8_t channel_key
[16];
6694 struct _derivation
{
6699 struct _derivation signing
;
6701 .signing
.label
.length
= 0,
6705 return NT_STATUS_INVALID_PARAMETER_MIX
;
6708 if (smb2_signing_key_valid(session
->smb2_channel
.signing_key
)) {
6709 return NT_STATUS_INVALID_PARAMETER_MIX
;
6712 if (conn
->protocol
>= PROTOCOL_SMB3_11
) {
6713 struct _derivation
*d
;
6716 p
= data_blob_const(session
->smb2_channel
.preauth_sha512
,
6717 sizeof(session
->smb2_channel
.preauth_sha512
));
6719 d
= &derivation
.signing
;
6720 d
->label
= data_blob_string_const_null("SMBSigningKey");
6722 } else if (conn
->protocol
>= PROTOCOL_SMB3_00
) {
6723 struct _derivation
*d
;
6725 d
= &derivation
.signing
;
6726 d
->label
= data_blob_string_const_null("SMB2AESCMAC");
6727 d
->context
= data_blob_string_const_null("SmbSign");
6730 ZERO_STRUCT(channel_key
);
6731 memcpy(channel_key
, _channel_key
.data
,
6732 MIN(_channel_key
.length
, sizeof(channel_key
)));
6734 session
->smb2_channel
.signing_key
->blob
=
6735 data_blob_talloc(session
->smb2_channel
.signing_key
,
6737 sizeof(channel_key
));
6738 if (!smb2_signing_key_valid(session
->smb2_channel
.signing_key
)) {
6739 ZERO_STRUCT(channel_key
);
6740 return NT_STATUS_NO_MEMORY
;
6743 if (conn
->protocol
>= PROTOCOL_SMB3_00
) {
6744 struct _derivation
*d
= &derivation
.signing
;
6746 status
= samba_gnutls_sp800_108_derive_key(
6748 sizeof(channel_key
),
6756 session
->smb2_channel
.signing_key
->blob
.data
,
6757 session
->smb2_channel
.signing_key
->blob
.length
);
6758 if (!NT_STATUS_IS_OK(status
)) {
6762 ZERO_STRUCT(channel_key
);
6764 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
6766 if (!NT_STATUS_IS_OK(status
)) {
6770 return NT_STATUS_OK
;
6773 NTSTATUS
smb2cli_session_encryption_on(struct smbXcli_session
*session
)
6775 if (session
->smb2
->anonymous_signing
) {
6776 return NT_STATUS_INVALID_PARAMETER_MIX
;
6779 if (session
->smb2
->anonymous_encryption
) {
6780 SMB_ASSERT(session
->smb2
->should_encrypt
);
6781 SMB_ASSERT(!session
->smb2
->should_sign
);
6782 return NT_STATUS_OK
;
6785 if (!session
->smb2
->should_sign
) {
6787 * We need required signing on the session
6788 * in order to prevent man in the middle attacks.
6790 return NT_STATUS_INVALID_PARAMETER_MIX
;
6793 if (session
->smb2
->should_encrypt
) {
6794 return NT_STATUS_OK
;
6797 if (session
->conn
->protocol
< PROTOCOL_SMB3_00
) {
6798 return NT_STATUS_NOT_SUPPORTED
;
6801 if (session
->conn
->smb2
.server
.cipher
== 0) {
6802 return NT_STATUS_NOT_SUPPORTED
;
6805 if (!smb2_signing_key_valid(session
->smb2
->signing_key
)) {
6806 return NT_STATUS_NOT_SUPPORTED
;
6808 session
->smb2
->should_encrypt
= true;
6809 return NT_STATUS_OK
;
6812 uint16_t smb2cli_session_get_encryption_cipher(struct smbXcli_session
*session
)
6814 if (session
->conn
->protocol
< PROTOCOL_SMB3_00
) {
6818 if (!session
->smb2
->should_encrypt
) {
6822 return session
->conn
->smb2
.server
.cipher
;
6825 struct smbXcli_tcon
*smbXcli_tcon_create(TALLOC_CTX
*mem_ctx
)
6827 struct smbXcli_tcon
*tcon
;
6829 tcon
= talloc_zero(mem_ctx
, struct smbXcli_tcon
);
6838 * Return a deep structure copy of a struct smbXcli_tcon *
6841 struct smbXcli_tcon
*smbXcli_tcon_copy(TALLOC_CTX
*mem_ctx
,
6842 const struct smbXcli_tcon
*tcon_in
)
6844 struct smbXcli_tcon
*tcon
;
6846 tcon
= talloc_memdup(mem_ctx
, tcon_in
, sizeof(struct smbXcli_tcon
));
6851 /* Deal with the SMB1 strings. */
6852 if (tcon_in
->smb1
.service
!= NULL
) {
6853 tcon
->smb1
.service
= talloc_strdup(tcon
, tcon_in
->smb1
.service
);
6854 if (tcon
->smb1
.service
== NULL
) {
6859 if (tcon
->smb1
.fs_type
!= NULL
) {
6860 tcon
->smb1
.fs_type
= talloc_strdup(tcon
, tcon_in
->smb1
.fs_type
);
6861 if (tcon
->smb1
.fs_type
== NULL
) {
6869 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon
*tcon
,
6870 uint32_t fs_attributes
)
6872 tcon
->fs_attributes
= fs_attributes
;
6875 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon
*tcon
)
6877 return tcon
->fs_attributes
;
6880 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon
*tcon
)
6886 if (tcon
->is_smb1
) {
6887 if (tcon
->smb1
.optional_support
& SMB_SHARE_IN_DFS
) {
6894 if (tcon
->smb2
.capabilities
& SMB2_SHARE_CAP_DFS
) {
6901 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
6903 return tcon
->smb1
.tcon_id
;
6906 void smb1cli_tcon_set_id(struct smbXcli_tcon
*tcon
, uint16_t tcon_id
)
6908 tcon
->is_smb1
= true;
6909 tcon
->smb1
.tcon_id
= tcon_id
;
6912 bool smb1cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
6914 uint16_t optional_support
,
6915 uint32_t maximal_access
,
6916 uint32_t guest_maximal_access
,
6917 const char *service
,
6918 const char *fs_type
)
6920 tcon
->is_smb1
= true;
6921 tcon
->fs_attributes
= 0;
6922 tcon
->smb1
.tcon_id
= tcon_id
;
6923 tcon
->smb1
.optional_support
= optional_support
;
6924 tcon
->smb1
.maximal_access
= maximal_access
;
6925 tcon
->smb1
.guest_maximal_access
= guest_maximal_access
;
6927 TALLOC_FREE(tcon
->smb1
.service
);
6928 tcon
->smb1
.service
= talloc_strdup(tcon
, service
);
6929 if (service
!= NULL
&& tcon
->smb1
.service
== NULL
) {
6933 TALLOC_FREE(tcon
->smb1
.fs_type
);
6934 tcon
->smb1
.fs_type
= talloc_strdup(tcon
, fs_type
);
6935 if (fs_type
!= NULL
&& tcon
->smb1
.fs_type
== NULL
) {
6942 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
6944 return tcon
->smb2
.tcon_id
;
6947 void smb2cli_tcon_set_id(struct smbXcli_tcon
*tcon
, uint32_t tcon_id
)
6949 tcon
->smb2
.tcon_id
= tcon_id
;
6952 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon
*tcon
)
6954 return tcon
->smb2
.capabilities
;
6957 uint32_t smb2cli_tcon_flags(struct smbXcli_tcon
*tcon
)
6959 return tcon
->smb2
.flags
;
6962 void smb2cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
6963 struct smbXcli_session
*session
,
6967 uint32_t capabilities
,
6968 uint32_t maximal_access
)
6970 tcon
->is_smb1
= false;
6971 tcon
->fs_attributes
= 0;
6972 tcon
->smb2
.tcon_id
= tcon_id
;
6973 tcon
->smb2
.type
= type
;
6974 tcon
->smb2
.flags
= flags
;
6975 tcon
->smb2
.capabilities
= capabilities
;
6976 tcon
->smb2
.maximal_access
= maximal_access
;
6978 tcon
->smb2
.should_sign
= false;
6979 tcon
->smb2
.should_encrypt
= false;
6981 if (session
== NULL
) {
6985 tcon
->smb2
.should_sign
= session
->smb2
->should_sign
;
6986 tcon
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
6988 if (flags
& SMB2_SHAREFLAG_ENCRYPT_DATA
) {
6989 tcon
->smb2
.should_encrypt
= true;
6993 void smb2cli_tcon_should_sign(struct smbXcli_tcon
*tcon
,
6996 tcon
->smb2
.should_sign
= should_sign
;
6999 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon
*tcon
)
7001 if (tcon
->smb2
.should_encrypt
) {
7005 return tcon
->smb2
.should_sign
;
7008 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon
*tcon
,
7009 bool should_encrypt
)
7011 tcon
->smb2
.should_encrypt
= should_encrypt
;
7014 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon
*tcon
)
7016 return tcon
->smb2
.should_encrypt
;
7019 void smb2cli_conn_set_mid(struct smbXcli_conn
*conn
, uint64_t mid
)
7021 conn
->smb2
.mid
= mid
;
7024 uint64_t smb2cli_conn_get_mid(struct smbXcli_conn
*conn
)
7026 return conn
->smb2
.mid
;
7029 NTSTATUS
smb2cli_parse_dyn_buffer(uint32_t dyn_offset
,
7030 const DATA_BLOB dyn_buffer
,
7031 uint32_t min_offset
,
7032 uint32_t buffer_offset
,
7033 uint32_t buffer_length
,
7034 uint32_t max_length
,
7035 uint32_t *next_offset
,
7041 *buffer
= data_blob_null
;
7042 *next_offset
= dyn_offset
;
7044 if (buffer_offset
== 0) {
7046 * If the offset is 0, we better ignore
7047 * the buffer_length field.
7049 return NT_STATUS_OK
;
7052 if (buffer_length
== 0) {
7054 * If the length is 0, we better ignore
7055 * the buffer_offset field.
7057 return NT_STATUS_OK
;
7060 if ((buffer_offset
% 8) != 0) {
7062 * The offset needs to be 8 byte aligned.
7064 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
7068 * We used to enforce buffer_offset to be
7069 * an exact match of the expected minimum,
7070 * but the NetApp Ontap 7.3.7 SMB server
7071 * gets the padding wrong and aligns the
7072 * input_buffer_offset by a value of 8.
7074 * So we just enforce that the offset is
7075 * not lower than the expected value.
7077 SMB_ASSERT(min_offset
>= dyn_offset
);
7078 if (buffer_offset
< min_offset
) {
7079 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
7083 * Make [input|output]_buffer_offset relative to "dyn_buffer"
7085 offset
= buffer_offset
- dyn_offset
;
7086 oob
= smb_buffer_oob(dyn_buffer
.length
, offset
, buffer_length
);
7088 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
7092 * Give the caller a hint what we consumed,
7093 * the caller may need to add possible padding.
7095 *next_offset
= buffer_offset
+ buffer_length
;
7097 if (max_length
== 0) {
7099 * If max_input_length is 0 we ignore the
7100 * input_buffer_length, because Windows 2008 echos the
7101 * DCERPC request from the requested input_buffer to
7102 * the response input_buffer.
7104 * We just use the same logic also for max_output_length...
7109 if (buffer_length
> max_length
) {
7110 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
7113 *buffer
= (DATA_BLOB
) {
7114 .data
= dyn_buffer
.data
+ offset
,
7115 .length
= buffer_length
,
7117 return NT_STATUS_OK
;