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 "lib/crypto/sha512.h"
37 #include "lib/crypto/aes.h"
38 #include "lib/crypto/aes_ccm_128.h"
39 #include "lib/crypto/aes_gcm_128.h"
43 struct smbXcli_session
;
48 struct sockaddr_storage local_ss
;
49 struct sockaddr_storage remote_ss
;
50 const char *remote_name
;
52 struct tevent_queue
*outgoing
;
53 struct tevent_req
**pending
;
54 struct tevent_req
*read_smb_req
;
55 struct tevent_req
*suicide_req
;
57 enum protocol_types min_protocol
;
58 enum protocol_types max_protocol
;
59 enum protocol_types protocol
;
62 bool mandatory_signing
;
65 * The incoming dispatch function should return:
66 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
67 * - NT_STATUS_OK, if no more processing is desired, e.g.
68 * the dispatch function called
70 * - All other return values disconnect the connection.
72 NTSTATUS (*dispatch_incoming
)(struct smbXcli_conn
*conn
,
78 uint32_t capabilities
;
83 uint32_t capabilities
;
86 uint16_t security_mode
;
95 const char *workgroup
;
101 uint32_t capabilities
;
106 struct smb_signing_state
*signing
;
107 struct smb_trans_enc_state
*trans_enc
;
109 struct tevent_req
*read_braw_req
;
114 uint32_t capabilities
;
115 uint16_t security_mode
;
120 uint32_t capabilities
;
121 uint16_t security_mode
;
123 uint32_t max_trans_size
;
124 uint32_t max_read_size
;
125 uint32_t max_write_size
;
133 uint16_t cur_credits
;
134 uint16_t max_credits
;
136 uint32_t cc_chunk_len
;
137 uint32_t cc_max_chunks
;
141 bool force_channel_sequence
;
143 uint8_t preauth_sha512
[64];
146 struct smbXcli_session
*sessions
;
149 struct smb2cli_session
{
151 uint16_t session_flags
;
152 DATA_BLOB application_key
;
153 DATA_BLOB signing_key
;
156 DATA_BLOB encryption_key
;
157 DATA_BLOB decryption_key
;
158 uint64_t nonce_high_random
;
159 uint64_t nonce_high_max
;
162 uint16_t channel_sequence
;
164 bool require_signed_response
;
167 struct smbXcli_session
{
168 struct smbXcli_session
*prev
, *next
;
169 struct smbXcli_conn
*conn
;
174 DATA_BLOB application_key
;
178 struct smb2cli_session
*smb2
;
181 DATA_BLOB signing_key
;
182 uint8_t preauth_sha512
[64];
186 * this should be a short term hack
187 * until the upper layers have implemented
190 bool disconnect_expired
;
193 struct smbXcli_tcon
{
195 uint32_t fs_attributes
;
199 uint16_t optional_support
;
200 uint32_t maximal_access
;
201 uint32_t guest_maximal_access
;
210 uint32_t capabilities
;
211 uint32_t maximal_access
;
217 struct smbXcli_req_state
{
218 struct tevent_context
*ev
;
219 struct smbXcli_conn
*conn
;
220 struct smbXcli_session
*session
; /* maybe NULL */
221 struct smbXcli_tcon
*tcon
; /* maybe NULL */
223 uint8_t length_hdr
[4];
229 struct tevent_req
*write_req
;
231 struct timeval endtime
;
234 /* Space for the header including the wct */
235 uint8_t hdr
[HDR_VWV
];
238 * For normal requests, smb1cli_req_send chooses a mid.
239 * SecondaryV trans requests need to use the mid of the primary
240 * request, so we need a place to store it.
241 * Assume it is set if != 0.
246 uint8_t bytecount_buf
[2];
248 #define MAX_SMB_IOV 10
249 /* length_hdr, hdr, words, byte_count, buffers */
250 struct iovec iov
[1 + 3 + MAX_SMB_IOV
];
255 struct tevent_req
**chained_requests
;
258 NTSTATUS recv_status
;
259 /* always an array of 3 talloc elements */
260 struct iovec
*recv_iov
;
264 const uint8_t *fixed
;
269 uint8_t transform
[SMB2_TF_HDR_SIZE
];
270 uint8_t hdr
[SMB2_HDR_BODY
];
271 uint8_t pad
[7]; /* padding space for compounding */
274 * always an array of 3 talloc elements
275 * (without a SMB2_TRANSFORM header!)
279 struct iovec
*recv_iov
;
282 * the expected max for the response dyn_len
284 uint32_t max_dyn_len
;
286 uint16_t credit_charge
;
290 uint64_t encryption_session_id
;
292 bool signing_skipped
;
293 bool require_signed_response
;
296 uint16_t cancel_flags
;
302 static int smbXcli_conn_destructor(struct smbXcli_conn
*conn
)
305 * NT_STATUS_OK, means we do not notify the callers
307 smbXcli_conn_disconnect(conn
, NT_STATUS_OK
);
309 while (conn
->sessions
) {
310 conn
->sessions
->conn
= NULL
;
311 DLIST_REMOVE(conn
->sessions
, conn
->sessions
);
314 if (conn
->smb1
.trans_enc
) {
315 TALLOC_FREE(conn
->smb1
.trans_enc
);
321 struct smbXcli_conn
*smbXcli_conn_create(TALLOC_CTX
*mem_ctx
,
323 const char *remote_name
,
324 enum smb_signing_setting signing_state
,
325 uint32_t smb1_capabilities
,
326 struct GUID
*client_guid
,
327 uint32_t smb2_capabilities
)
329 struct smbXcli_conn
*conn
= NULL
;
331 struct sockaddr
*sa
= NULL
;
335 conn
= talloc_zero(mem_ctx
, struct smbXcli_conn
);
342 conn
->remote_name
= talloc_strdup(conn
, remote_name
);
343 if (conn
->remote_name
== NULL
) {
347 ss
= (void *)&conn
->local_ss
;
348 sa
= (struct sockaddr
*)ss
;
349 sa_length
= sizeof(conn
->local_ss
);
350 ret
= getsockname(fd
, sa
, &sa_length
);
354 ss
= (void *)&conn
->remote_ss
;
355 sa
= (struct sockaddr
*)ss
;
356 sa_length
= sizeof(conn
->remote_ss
);
357 ret
= getpeername(fd
, sa
, &sa_length
);
362 conn
->outgoing
= tevent_queue_create(conn
, "smbXcli_outgoing");
363 if (conn
->outgoing
== NULL
) {
366 conn
->pending
= NULL
;
368 conn
->min_protocol
= PROTOCOL_NONE
;
369 conn
->max_protocol
= PROTOCOL_NONE
;
370 conn
->protocol
= PROTOCOL_NONE
;
372 switch (signing_state
) {
373 case SMB_SIGNING_OFF
:
375 conn
->allow_signing
= false;
376 conn
->desire_signing
= false;
377 conn
->mandatory_signing
= false;
379 case SMB_SIGNING_DEFAULT
:
380 case SMB_SIGNING_IF_REQUIRED
:
381 /* if the server requires it */
382 conn
->allow_signing
= true;
383 conn
->desire_signing
= false;
384 conn
->mandatory_signing
= false;
386 case SMB_SIGNING_DESIRED
:
387 /* if the server desires it */
388 conn
->allow_signing
= true;
389 conn
->desire_signing
= true;
390 conn
->mandatory_signing
= false;
392 case SMB_SIGNING_IPC_DEFAULT
:
393 case SMB_SIGNING_REQUIRED
:
395 conn
->allow_signing
= true;
396 conn
->desire_signing
= true;
397 conn
->mandatory_signing
= true;
401 conn
->smb1
.client
.capabilities
= smb1_capabilities
;
402 conn
->smb1
.client
.max_xmit
= UINT16_MAX
;
404 conn
->smb1
.capabilities
= conn
->smb1
.client
.capabilities
;
405 conn
->smb1
.max_xmit
= 1024;
409 /* initialise signing */
410 conn
->smb1
.signing
= smb_signing_init(conn
,
412 conn
->desire_signing
,
413 conn
->mandatory_signing
);
414 if (!conn
->smb1
.signing
) {
418 conn
->smb2
.client
.security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
419 if (conn
->mandatory_signing
) {
420 conn
->smb2
.client
.security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
423 conn
->smb2
.client
.guid
= *client_guid
;
425 conn
->smb2
.client
.capabilities
= smb2_capabilities
;
427 conn
->smb2
.cur_credits
= 1;
428 conn
->smb2
.max_credits
= 0;
429 conn
->smb2
.io_priority
= 1;
432 * Samba and Windows servers accept a maximum of 16 MiB with a maximum
433 * chunk length of 1 MiB.
435 conn
->smb2
.cc_chunk_len
= 1024 * 1024;
436 conn
->smb2
.cc_max_chunks
= 16;
438 talloc_set_destructor(conn
, smbXcli_conn_destructor
);
446 bool smbXcli_conn_is_connected(struct smbXcli_conn
*conn
)
452 if (conn
->sock_fd
== -1) {
459 enum protocol_types
smbXcli_conn_protocol(struct smbXcli_conn
*conn
)
461 return conn
->protocol
;
464 bool smbXcli_conn_use_unicode(struct smbXcli_conn
*conn
)
466 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
470 if (conn
->smb1
.capabilities
& CAP_UNICODE
) {
477 bool smbXcli_conn_signing_mandatory(struct smbXcli_conn
*conn
)
479 return conn
->mandatory_signing
;
483 * [MS-SMB] 2.2.2.3.5 - SMB1 support for passing through
484 * query/set commands to the file system
486 bool smbXcli_conn_support_passthrough(struct smbXcli_conn
*conn
)
488 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
492 if (conn
->smb1
.capabilities
& CAP_W2K_SMBS
) {
499 void smbXcli_conn_set_sockopt(struct smbXcli_conn
*conn
, const char *options
)
501 set_socket_options(conn
->sock_fd
, options
);
504 const struct sockaddr_storage
*smbXcli_conn_local_sockaddr(struct smbXcli_conn
*conn
)
506 return &conn
->local_ss
;
509 const struct sockaddr_storage
*smbXcli_conn_remote_sockaddr(struct smbXcli_conn
*conn
)
511 return &conn
->remote_ss
;
514 const char *smbXcli_conn_remote_name(struct smbXcli_conn
*conn
)
516 return conn
->remote_name
;
519 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn
*conn
)
521 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
528 return conn
->smb1
.server
.max_mux
;
531 NTTIME
smbXcli_conn_server_system_time(struct smbXcli_conn
*conn
)
533 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
534 return conn
->smb2
.server
.system_time
;
537 return conn
->smb1
.server
.system_time
;
540 const DATA_BLOB
*smbXcli_conn_server_gss_blob(struct smbXcli_conn
*conn
)
542 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
543 return &conn
->smb2
.server
.gss_blob
;
546 return &conn
->smb1
.server
.gss_blob
;
549 const struct GUID
*smbXcli_conn_server_guid(struct smbXcli_conn
*conn
)
551 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
552 return &conn
->smb2
.server
.guid
;
555 return &conn
->smb1
.server
.guid
;
558 bool smbXcli_conn_get_force_channel_sequence(struct smbXcli_conn
*conn
)
560 return conn
->smb2
.force_channel_sequence
;
563 void smbXcli_conn_set_force_channel_sequence(struct smbXcli_conn
*conn
,
566 conn
->smb2
.force_channel_sequence
= v
;
569 struct smbXcli_conn_samba_suicide_state
{
570 struct smbXcli_conn
*conn
;
573 struct tevent_req
*write_req
;
576 static void smbXcli_conn_samba_suicide_cleanup(struct tevent_req
*req
,
577 enum tevent_req_state req_state
);
578 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
);
580 struct tevent_req
*smbXcli_conn_samba_suicide_send(TALLOC_CTX
*mem_ctx
,
581 struct tevent_context
*ev
,
582 struct smbXcli_conn
*conn
,
585 struct tevent_req
*req
, *subreq
;
586 struct smbXcli_conn_samba_suicide_state
*state
;
588 req
= tevent_req_create(mem_ctx
, &state
,
589 struct smbXcli_conn_samba_suicide_state
);
594 SIVAL(state
->buf
, 4, 0x74697865);
595 SCVAL(state
->buf
, 8, exitcode
);
596 _smb_setlen_nbt(state
->buf
, sizeof(state
->buf
)-4);
598 if (conn
->suicide_req
!= NULL
) {
599 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
600 return tevent_req_post(req
, ev
);
603 state
->iov
.iov_base
= state
->buf
;
604 state
->iov
.iov_len
= sizeof(state
->buf
);
606 subreq
= writev_send(state
, ev
, conn
->outgoing
, conn
->sock_fd
,
607 false, &state
->iov
, 1);
608 if (tevent_req_nomem(subreq
, req
)) {
609 return tevent_req_post(req
, ev
);
611 tevent_req_set_callback(subreq
, smbXcli_conn_samba_suicide_done
, req
);
612 state
->write_req
= subreq
;
614 tevent_req_set_cleanup_fn(req
, smbXcli_conn_samba_suicide_cleanup
);
617 * We need to use tevent_req_defer_callback()
618 * in order to allow smbXcli_conn_disconnect()
619 * to do a safe cleanup.
621 tevent_req_defer_callback(req
, ev
);
622 conn
->suicide_req
= req
;
627 static void smbXcli_conn_samba_suicide_cleanup(struct tevent_req
*req
,
628 enum tevent_req_state req_state
)
630 struct smbXcli_conn_samba_suicide_state
*state
= tevent_req_data(
631 req
, struct smbXcli_conn_samba_suicide_state
);
633 TALLOC_FREE(state
->write_req
);
635 if (state
->conn
== NULL
) {
639 if (state
->conn
->suicide_req
== req
) {
640 state
->conn
->suicide_req
= NULL
;
645 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
)
647 struct tevent_req
*req
= tevent_req_callback_data(
648 subreq
, struct tevent_req
);
649 struct smbXcli_conn_samba_suicide_state
*state
= tevent_req_data(
650 req
, struct smbXcli_conn_samba_suicide_state
);
654 state
->write_req
= NULL
;
656 nwritten
= writev_recv(subreq
, &err
);
658 if (nwritten
== -1) {
659 /* here, we need to notify all pending requests */
660 NTSTATUS status
= map_nt_error_from_unix_common(err
);
661 smbXcli_conn_disconnect(state
->conn
, status
);
664 tevent_req_done(req
);
667 NTSTATUS
smbXcli_conn_samba_suicide_recv(struct tevent_req
*req
)
669 return tevent_req_simple_recv_ntstatus(req
);
672 NTSTATUS
smbXcli_conn_samba_suicide(struct smbXcli_conn
*conn
,
675 TALLOC_CTX
*frame
= talloc_stackframe();
676 struct tevent_context
*ev
;
677 struct tevent_req
*req
;
678 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
681 if (smbXcli_conn_has_async_calls(conn
)) {
683 * Can't use sync call while an async call is in flight
685 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
688 ev
= samba_tevent_context_init(frame
);
692 req
= smbXcli_conn_samba_suicide_send(frame
, ev
, conn
, exitcode
);
696 ok
= tevent_req_poll_ntstatus(req
, ev
, &status
);
700 status
= smbXcli_conn_samba_suicide_recv(req
);
706 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn
*conn
)
708 return conn
->smb1
.capabilities
;
711 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn
*conn
)
713 return conn
->smb1
.max_xmit
;
716 bool smb1cli_conn_req_possible(struct smbXcli_conn
*conn
)
718 size_t pending
= talloc_array_length(conn
->pending
);
719 uint16_t possible
= conn
->smb1
.server
.max_mux
;
721 if (pending
>= possible
) {
728 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn
*conn
)
730 return conn
->smb1
.server
.session_key
;
733 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn
*conn
)
735 return conn
->smb1
.server
.challenge
;
738 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
740 return conn
->smb1
.server
.security_mode
;
743 bool smb1cli_conn_server_readbraw(struct smbXcli_conn
*conn
)
745 return conn
->smb1
.server
.readbraw
;
748 bool smb1cli_conn_server_writebraw(struct smbXcli_conn
*conn
)
750 return conn
->smb1
.server
.writebraw
;
753 bool smb1cli_conn_server_lockread(struct smbXcli_conn
*conn
)
755 return conn
->smb1
.server
.lockread
;
758 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn
*conn
)
760 return conn
->smb1
.server
.writeunlock
;
763 int smb1cli_conn_server_time_zone(struct smbXcli_conn
*conn
)
765 return conn
->smb1
.server
.time_zone
;
768 bool smb1cli_conn_activate_signing(struct smbXcli_conn
*conn
,
769 const DATA_BLOB user_session_key
,
770 const DATA_BLOB response
)
772 return smb_signing_activate(conn
->smb1
.signing
,
777 bool smb1cli_conn_check_signing(struct smbXcli_conn
*conn
,
778 const uint8_t *buf
, uint32_t seqnum
)
780 const uint8_t *hdr
= buf
+ NBT_HDR_SIZE
;
781 size_t len
= smb_len_nbt(buf
);
783 return smb_signing_check_pdu(conn
->smb1
.signing
, hdr
, len
, seqnum
);
786 bool smb1cli_conn_signing_is_active(struct smbXcli_conn
*conn
)
788 return smb_signing_is_active(conn
->smb1
.signing
);
791 void smb1cli_conn_set_encryption(struct smbXcli_conn
*conn
,
792 struct smb_trans_enc_state
*es
)
794 /* Replace the old state, if any. */
795 if (conn
->smb1
.trans_enc
) {
796 TALLOC_FREE(conn
->smb1
.trans_enc
);
798 conn
->smb1
.trans_enc
= es
;
801 bool smb1cli_conn_encryption_on(struct smbXcli_conn
*conn
)
803 return common_encryption_on(conn
->smb1
.trans_enc
);
807 static NTSTATUS
smb1cli_pull_raw_error(const uint8_t *hdr
)
809 uint32_t flags2
= SVAL(hdr
, HDR_FLG2
);
810 NTSTATUS status
= NT_STATUS(IVAL(hdr
, HDR_RCLS
));
812 if (NT_STATUS_IS_OK(status
)) {
816 if (flags2
& FLAGS2_32_BIT_ERROR_CODES
) {
820 return NT_STATUS_DOS(CVAL(hdr
, HDR_RCLS
), SVAL(hdr
, HDR_ERR
));
824 * Is the SMB command able to hold an AND_X successor
825 * @param[in] cmd The SMB command in question
826 * @retval Can we add a chained request after "cmd"?
828 bool smb1cli_is_andx_req(uint8_t cmd
)
848 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn
*conn
)
850 size_t num_pending
= talloc_array_length(conn
->pending
);
853 if (conn
->protocol
== PROTOCOL_NONE
) {
855 * This is what windows sends on the SMB1 Negprot request
856 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
864 result
= conn
->smb1
.mid
++;
865 if ((result
== 0) || (result
== 0xffff)) {
869 for (i
=0; i
<num_pending
; i
++) {
870 if (result
== smb1cli_req_mid(conn
->pending
[i
])) {
875 if (i
== num_pending
) {
881 static NTSTATUS
smbXcli_req_cancel_write_req(struct tevent_req
*req
)
883 struct smbXcli_req_state
*state
=
885 struct smbXcli_req_state
);
886 struct smbXcli_conn
*conn
= state
->conn
;
887 size_t num_pending
= talloc_array_length(conn
->pending
);
892 if (state
->write_req
== NULL
) {
897 * Check if it's possible to cancel the request.
898 * If the result is true it's not too late.
899 * See writev_cancel().
901 ok
= tevent_req_cancel(state
->write_req
);
903 TALLOC_FREE(state
->write_req
);
905 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
907 * SMB2 has a sane signing state.
912 if (num_pending
> 1) {
914 * We have more pending requests following us. This
915 * means the signing state will be broken for them.
917 * As a solution we could add the requests directly to
918 * our outgoing queue and do the signing in the trigger
919 * function and then use writev_send() without passing a
920 * queue. That way we'll only sign packets we're most
921 * likely send to the wire.
923 return NT_STATUS_REQUEST_OUT_OF_SEQUENCE
;
927 * If we're the only request that's
928 * pending, we're able to recover the signing
931 smb_signing_cancel_reply(conn
->smb1
.signing
,
932 state
->smb1
.one_way_seqnum
);
936 ret
= writev_recv(state
->write_req
, &err
);
937 TALLOC_FREE(state
->write_req
);
939 return map_nt_error_from_unix_common(err
);
945 void smbXcli_req_unset_pending(struct tevent_req
*req
)
947 struct smbXcli_req_state
*state
=
949 struct smbXcli_req_state
);
950 struct smbXcli_conn
*conn
= state
->conn
;
951 size_t num_pending
= talloc_array_length(conn
->pending
);
953 NTSTATUS cancel_status
;
955 cancel_status
= smbXcli_req_cancel_write_req(req
);
957 if (state
->smb1
.mid
!= 0) {
959 * This is a [nt]trans[2] request which waits
960 * for more than one reply.
962 if (!NT_STATUS_IS_OK(cancel_status
)) {
964 * If the write_req cancel didn't work
965 * we can't use the connection anymore.
967 smbXcli_conn_disconnect(conn
, cancel_status
);
973 tevent_req_set_cleanup_fn(req
, NULL
);
975 if (num_pending
== 1) {
977 * The pending read_smb tevent_req is a child of
978 * conn->pending. So if nothing is pending anymore, we need to
979 * delete the socket read fde.
981 /* TODO: smbXcli_conn_cancel_read_req */
982 TALLOC_FREE(conn
->pending
);
983 conn
->read_smb_req
= NULL
;
985 if (!NT_STATUS_IS_OK(cancel_status
)) {
987 * If the write_req cancel didn't work
988 * we can't use the connection anymore.
990 smbXcli_conn_disconnect(conn
, cancel_status
);
996 for (i
=0; i
<num_pending
; i
++) {
997 if (req
== conn
->pending
[i
]) {
1001 if (i
== num_pending
) {
1003 * Something's seriously broken. Just returning here is the
1004 * right thing nevertheless, the point of this routine is to
1005 * remove ourselves from conn->pending.
1008 if (!NT_STATUS_IS_OK(cancel_status
)) {
1010 * If the write_req cancel didn't work
1011 * we can't use the connection anymore.
1013 smbXcli_conn_disconnect(conn
, cancel_status
);
1020 * Remove ourselves from the conn->pending array
1022 for (; i
< (num_pending
- 1); i
++) {
1023 conn
->pending
[i
] = conn
->pending
[i
+1];
1027 * No NULL check here, we're shrinking by sizeof(void *), and
1028 * talloc_realloc just adjusts the size for this.
1030 conn
->pending
= talloc_realloc(NULL
, conn
->pending
, struct tevent_req
*,
1033 if (!NT_STATUS_IS_OK(cancel_status
)) {
1035 * If the write_req cancel didn't work
1036 * we can't use the connection anymore.
1038 smbXcli_conn_disconnect(conn
, cancel_status
);
1044 static void smbXcli_req_cleanup(struct tevent_req
*req
,
1045 enum tevent_req_state req_state
)
1047 struct smbXcli_req_state
*state
=
1048 tevent_req_data(req
,
1049 struct smbXcli_req_state
);
1050 struct smbXcli_conn
*conn
= state
->conn
;
1051 NTSTATUS cancel_status
;
1053 switch (req_state
) {
1054 case TEVENT_REQ_RECEIVED
:
1056 * Make sure we really remove it from
1057 * the pending array on destruction.
1059 * smbXcli_req_unset_pending() calls
1060 * smbXcli_req_cancel_write_req() internal
1062 state
->smb1
.mid
= 0;
1063 smbXcli_req_unset_pending(req
);
1066 cancel_status
= smbXcli_req_cancel_write_req(req
);
1067 if (!NT_STATUS_IS_OK(cancel_status
)) {
1069 * If the write_req cancel didn't work
1070 * we can't use the connection anymore.
1072 smbXcli_conn_disconnect(conn
, cancel_status
);
1079 static bool smb1cli_req_cancel(struct tevent_req
*req
);
1080 static bool smb2cli_req_cancel(struct tevent_req
*req
);
1082 static bool smbXcli_req_cancel(struct tevent_req
*req
)
1084 struct smbXcli_req_state
*state
=
1085 tevent_req_data(req
,
1086 struct smbXcli_req_state
);
1088 if (!smbXcli_conn_is_connected(state
->conn
)) {
1092 if (state
->conn
->protocol
== PROTOCOL_NONE
) {
1096 if (state
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
1097 return smb2cli_req_cancel(req
);
1100 return smb1cli_req_cancel(req
);
1103 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
);
1105 bool smbXcli_req_set_pending(struct tevent_req
*req
)
1107 struct smbXcli_req_state
*state
=
1108 tevent_req_data(req
,
1109 struct smbXcli_req_state
);
1110 struct smbXcli_conn
*conn
;
1111 struct tevent_req
**pending
;
1116 if (!smbXcli_conn_is_connected(conn
)) {
1120 num_pending
= talloc_array_length(conn
->pending
);
1122 pending
= talloc_realloc(conn
, conn
->pending
, struct tevent_req
*,
1124 if (pending
== NULL
) {
1127 pending
[num_pending
] = req
;
1128 conn
->pending
= pending
;
1129 tevent_req_set_cleanup_fn(req
, smbXcli_req_cleanup
);
1130 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
1132 if (!smbXcli_conn_receive_next(conn
)) {
1134 * the caller should notify the current request
1136 * And all other pending requests get notified
1137 * by smbXcli_conn_disconnect().
1139 smbXcli_req_unset_pending(req
);
1140 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
1147 static void smbXcli_conn_received(struct tevent_req
*subreq
);
1149 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
)
1151 size_t num_pending
= talloc_array_length(conn
->pending
);
1152 struct tevent_req
*req
;
1153 struct smbXcli_req_state
*state
;
1155 if (conn
->read_smb_req
!= NULL
) {
1159 if (num_pending
== 0) {
1160 if (conn
->smb2
.mid
< UINT64_MAX
) {
1161 /* no more pending requests, so we are done for now */
1166 * If there are no more SMB2 requests possible,
1167 * because we are out of message ids,
1168 * we need to disconnect.
1170 smbXcli_conn_disconnect(conn
, NT_STATUS_CONNECTION_ABORTED
);
1174 req
= conn
->pending
[0];
1175 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1178 * We're the first ones, add the read_smb request that waits for the
1179 * answer from the server
1181 conn
->read_smb_req
= read_smb_send(conn
->pending
,
1184 if (conn
->read_smb_req
== NULL
) {
1187 tevent_req_set_callback(conn
->read_smb_req
, smbXcli_conn_received
, conn
);
1191 void smbXcli_conn_disconnect(struct smbXcli_conn
*conn
, NTSTATUS status
)
1193 struct smbXcli_session
*session
;
1194 int sock_fd
= conn
->sock_fd
;
1196 tevent_queue_stop(conn
->outgoing
);
1200 session
= conn
->sessions
;
1201 if (talloc_array_length(conn
->pending
) == 0) {
1203 * if we do not have pending requests
1204 * there is no need to update the channel_sequence
1208 for (; session
; session
= session
->next
) {
1209 smb2cli_session_increment_channel_sequence(session
);
1212 if (conn
->suicide_req
!= NULL
) {
1214 * smbXcli_conn_samba_suicide_send()
1215 * used tevent_req_defer_callback() already.
1217 if (!NT_STATUS_IS_OK(status
)) {
1218 tevent_req_nterror(conn
->suicide_req
, status
);
1220 conn
->suicide_req
= NULL
;
1224 * Cancel all pending requests. We do not do a for-loop walking
1225 * conn->pending because that array changes in
1226 * smbXcli_req_unset_pending.
1228 while (talloc_array_length(conn
->pending
) > 0) {
1229 struct tevent_req
*req
;
1230 struct smbXcli_req_state
*state
;
1231 struct tevent_req
**chain
;
1235 req
= conn
->pending
[0];
1236 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1238 if (state
->smb1
.chained_requests
== NULL
) {
1242 * We're dead. No point waiting for trans2
1245 state
->smb1
.mid
= 0;
1247 smbXcli_req_unset_pending(req
);
1249 if (NT_STATUS_IS_OK(status
)) {
1250 /* do not notify the callers */
1254 in_progress
= tevent_req_is_in_progress(req
);
1263 * we need to defer the callback, because we may notify
1264 * more then one caller.
1266 tevent_req_defer_callback(req
, state
->ev
);
1267 tevent_req_nterror(req
, status
);
1271 chain
= talloc_move(conn
, &state
->smb1
.chained_requests
);
1272 num_chained
= talloc_array_length(chain
);
1274 for (i
=0; i
<num_chained
; i
++) {
1278 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1281 * We're dead. No point waiting for trans2
1284 state
->smb1
.mid
= 0;
1286 smbXcli_req_unset_pending(req
);
1288 if (NT_STATUS_IS_OK(status
)) {
1289 /* do not notify the callers */
1293 in_progress
= tevent_req_is_in_progress(req
);
1302 * we need to defer the callback, because we may notify
1303 * more than one caller.
1305 tevent_req_defer_callback(req
, state
->ev
);
1306 tevent_req_nterror(req
, status
);
1311 if (sock_fd
!= -1) {
1317 * Fetch a smb request's mid. Only valid after the request has been sent by
1318 * smb1cli_req_send().
1320 uint16_t smb1cli_req_mid(struct tevent_req
*req
)
1322 struct smbXcli_req_state
*state
=
1323 tevent_req_data(req
,
1324 struct smbXcli_req_state
);
1326 if (state
->smb1
.mid
!= 0) {
1327 return state
->smb1
.mid
;
1330 return SVAL(state
->smb1
.hdr
, HDR_MID
);
1333 void smb1cli_req_set_mid(struct tevent_req
*req
, uint16_t mid
)
1335 struct smbXcli_req_state
*state
=
1336 tevent_req_data(req
,
1337 struct smbXcli_req_state
);
1339 state
->smb1
.mid
= mid
;
1342 uint32_t smb1cli_req_seqnum(struct tevent_req
*req
)
1344 struct smbXcli_req_state
*state
=
1345 tevent_req_data(req
,
1346 struct smbXcli_req_state
);
1348 return state
->smb1
.seqnum
;
1351 void smb1cli_req_set_seqnum(struct tevent_req
*req
, uint32_t seqnum
)
1353 struct smbXcli_req_state
*state
=
1354 tevent_req_data(req
,
1355 struct smbXcli_req_state
);
1357 state
->smb1
.seqnum
= seqnum
;
1360 static size_t smbXcli_iov_len(const struct iovec
*iov
, int count
)
1362 ssize_t ret
= iov_buflen(iov
, count
);
1364 /* Ignore the overflow case for now ... */
1368 static void smb1cli_req_flags(enum protocol_types protocol
,
1369 uint32_t smb1_capabilities
,
1370 uint8_t smb_command
,
1371 uint8_t additional_flags
,
1372 uint8_t clear_flags
,
1374 uint16_t additional_flags2
,
1375 uint16_t clear_flags2
,
1379 uint16_t flags2
= 0;
1381 if (protocol
>= PROTOCOL_LANMAN1
) {
1382 flags
|= FLAG_CASELESS_PATHNAMES
;
1383 flags
|= FLAG_CANONICAL_PATHNAMES
;
1386 if (protocol
>= PROTOCOL_LANMAN2
) {
1387 flags2
|= FLAGS2_LONG_PATH_COMPONENTS
;
1388 flags2
|= FLAGS2_EXTENDED_ATTRIBUTES
;
1391 if (protocol
>= PROTOCOL_NT1
) {
1392 flags2
|= FLAGS2_IS_LONG_NAME
;
1394 if (smb1_capabilities
& CAP_UNICODE
) {
1395 flags2
|= FLAGS2_UNICODE_STRINGS
;
1397 if (smb1_capabilities
& CAP_STATUS32
) {
1398 flags2
|= FLAGS2_32_BIT_ERROR_CODES
;
1400 if (smb1_capabilities
& CAP_EXTENDED_SECURITY
) {
1401 flags2
|= FLAGS2_EXTENDED_SECURITY
;
1405 flags
|= additional_flags
;
1406 flags
&= ~clear_flags
;
1407 flags2
|= additional_flags2
;
1408 flags2
&= ~clear_flags2
;
1414 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
);
1416 static bool smb1cli_req_cancel(struct tevent_req
*req
)
1418 struct smbXcli_req_state
*state
=
1419 tevent_req_data(req
,
1420 struct smbXcli_req_state
);
1425 struct tevent_req
*subreq
;
1428 flags
= CVAL(state
->smb1
.hdr
, HDR_FLG
);
1429 flags2
= SVAL(state
->smb1
.hdr
, HDR_FLG2
);
1430 pid
= SVAL(state
->smb1
.hdr
, HDR_PID
);
1431 pid
|= SVAL(state
->smb1
.hdr
, HDR_PIDHIGH
)<<16;
1432 mid
= SVAL(state
->smb1
.hdr
, HDR_MID
);
1434 subreq
= smb1cli_req_create(state
, state
->ev
,
1444 0, NULL
); /* bytes */
1445 if (subreq
== NULL
) {
1448 smb1cli_req_set_mid(subreq
, mid
);
1450 status
= smb1cli_req_chain_submit(&subreq
, 1);
1451 if (!NT_STATUS_IS_OK(status
)) {
1452 TALLOC_FREE(subreq
);
1455 smb1cli_req_set_mid(subreq
, 0);
1457 tevent_req_set_callback(subreq
, smb1cli_req_cancel_done
, NULL
);
1462 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
)
1464 /* we do not care about the result */
1465 TALLOC_FREE(subreq
);
1468 struct tevent_req
*smb1cli_req_create(TALLOC_CTX
*mem_ctx
,
1469 struct tevent_context
*ev
,
1470 struct smbXcli_conn
*conn
,
1471 uint8_t smb_command
,
1472 uint8_t additional_flags
,
1473 uint8_t clear_flags
,
1474 uint16_t additional_flags2
,
1475 uint16_t clear_flags2
,
1476 uint32_t timeout_msec
,
1478 struct smbXcli_tcon
*tcon
,
1479 struct smbXcli_session
*session
,
1480 uint8_t wct
, uint16_t *vwv
,
1482 struct iovec
*bytes_iov
)
1484 struct tevent_req
*req
;
1485 struct smbXcli_req_state
*state
;
1487 uint16_t flags2
= 0;
1492 if (iov_count
> MAX_SMB_IOV
) {
1494 * Should not happen :-)
1499 req
= tevent_req_create(mem_ctx
, &state
,
1500 struct smbXcli_req_state
);
1506 state
->session
= session
;
1510 uid
= session
->smb1
.session_id
;
1514 tid
= tcon
->smb1
.tcon_id
;
1516 if (tcon
->fs_attributes
& FILE_CASE_SENSITIVE_SEARCH
) {
1517 clear_flags
|= FLAG_CASELESS_PATHNAMES
;
1519 /* Default setting, case insensitive. */
1520 additional_flags
|= FLAG_CASELESS_PATHNAMES
;
1523 if (smbXcli_conn_dfs_supported(conn
) &&
1524 smbXcli_tcon_is_dfs_share(tcon
))
1526 additional_flags2
|= FLAGS2_DFS_PATHNAMES
;
1530 state
->smb1
.recv_cmd
= 0xFF;
1531 state
->smb1
.recv_status
= NT_STATUS_INTERNAL_ERROR
;
1532 state
->smb1
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
1533 if (state
->smb1
.recv_iov
== NULL
) {
1538 smb1cli_req_flags(conn
->protocol
,
1539 conn
->smb1
.capabilities
,
1548 SIVAL(state
->smb1
.hdr
, 0, SMB_MAGIC
);
1549 SCVAL(state
->smb1
.hdr
, HDR_COM
, smb_command
);
1550 SIVAL(state
->smb1
.hdr
, HDR_RCLS
, NT_STATUS_V(NT_STATUS_OK
));
1551 SCVAL(state
->smb1
.hdr
, HDR_FLG
, flags
);
1552 SSVAL(state
->smb1
.hdr
, HDR_FLG2
, flags2
);
1553 SSVAL(state
->smb1
.hdr
, HDR_PIDHIGH
, pid
>> 16);
1554 SSVAL(state
->smb1
.hdr
, HDR_TID
, tid
);
1555 SSVAL(state
->smb1
.hdr
, HDR_PID
, pid
);
1556 SSVAL(state
->smb1
.hdr
, HDR_UID
, uid
);
1557 SSVAL(state
->smb1
.hdr
, HDR_MID
, 0); /* this comes later */
1558 SCVAL(state
->smb1
.hdr
, HDR_WCT
, wct
);
1560 state
->smb1
.vwv
= vwv
;
1562 num_bytes
= iov_buflen(bytes_iov
, iov_count
);
1563 if (num_bytes
== -1) {
1565 * I'd love to add a check for num_bytes<=UINT16_MAX here, but
1566 * the smbclient->samba connections can lie and transfer more.
1572 SSVAL(state
->smb1
.bytecount_buf
, 0, num_bytes
);
1574 state
->smb1
.iov
[0].iov_base
= (void *)state
->length_hdr
;
1575 state
->smb1
.iov
[0].iov_len
= sizeof(state
->length_hdr
);
1576 state
->smb1
.iov
[1].iov_base
= (void *)state
->smb1
.hdr
;
1577 state
->smb1
.iov
[1].iov_len
= sizeof(state
->smb1
.hdr
);
1578 state
->smb1
.iov
[2].iov_base
= (void *)state
->smb1
.vwv
;
1579 state
->smb1
.iov
[2].iov_len
= wct
* sizeof(uint16_t);
1580 state
->smb1
.iov
[3].iov_base
= (void *)state
->smb1
.bytecount_buf
;
1581 state
->smb1
.iov
[3].iov_len
= sizeof(uint16_t);
1583 if (iov_count
!= 0) {
1584 memcpy(&state
->smb1
.iov
[4], bytes_iov
,
1585 iov_count
* sizeof(*bytes_iov
));
1587 state
->smb1
.iov_count
= iov_count
+ 4;
1589 if (timeout_msec
> 0) {
1590 state
->endtime
= timeval_current_ofs_msec(timeout_msec
);
1591 if (!tevent_req_set_endtime(req
, ev
, state
->endtime
)) {
1596 switch (smb_command
) {
1600 state
->one_way
= true;
1603 state
->one_way
= true;
1604 state
->smb1
.one_way_seqnum
= true;
1608 (CVAL(vwv
+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE
)) {
1609 state
->one_way
= true;
1617 static NTSTATUS
smb1cli_conn_signv(struct smbXcli_conn
*conn
,
1618 struct iovec
*iov
, int iov_count
,
1620 bool one_way_seqnum
)
1622 TALLOC_CTX
*frame
= NULL
;
1626 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1627 * iovec directly. MD5Update would do that just fine.
1630 if (iov_count
< 4) {
1631 return NT_STATUS_INVALID_PARAMETER_MIX
;
1633 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1634 return NT_STATUS_INVALID_PARAMETER_MIX
;
1636 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1637 return NT_STATUS_INVALID_PARAMETER_MIX
;
1639 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1640 return NT_STATUS_INVALID_PARAMETER_MIX
;
1642 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1643 return NT_STATUS_INVALID_PARAMETER_MIX
;
1646 frame
= talloc_stackframe();
1648 buf
= iov_concat(frame
, &iov
[1], iov_count
- 1);
1650 return NT_STATUS_NO_MEMORY
;
1653 *seqnum
= smb_signing_next_seqnum(conn
->smb1
.signing
,
1655 smb_signing_sign_pdu(conn
->smb1
.signing
,
1656 buf
, talloc_get_size(buf
),
1658 memcpy(iov
[1].iov_base
, buf
, iov
[1].iov_len
);
1661 return NT_STATUS_OK
;
1664 static void smb1cli_req_writev_done(struct tevent_req
*subreq
);
1665 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1666 TALLOC_CTX
*tmp_mem
,
1669 static NTSTATUS
smb1cli_req_writev_submit(struct tevent_req
*req
,
1670 struct smbXcli_req_state
*state
,
1671 struct iovec
*iov
, int iov_count
)
1673 struct tevent_req
*subreq
;
1679 if (!smbXcli_conn_is_connected(state
->conn
)) {
1680 return NT_STATUS_CONNECTION_DISCONNECTED
;
1683 if (state
->conn
->protocol
> PROTOCOL_NT1
) {
1684 DBG_ERR("called for dialect[%s] server[%s]\n",
1685 smb_protocol_types_string(state
->conn
->protocol
),
1686 smbXcli_conn_remote_name(state
->conn
));
1687 return NT_STATUS_REVISION_MISMATCH
;
1690 if (iov_count
< 4) {
1691 return NT_STATUS_INVALID_PARAMETER_MIX
;
1693 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1694 return NT_STATUS_INVALID_PARAMETER_MIX
;
1696 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1697 return NT_STATUS_INVALID_PARAMETER_MIX
;
1699 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1700 return NT_STATUS_INVALID_PARAMETER_MIX
;
1702 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1703 return NT_STATUS_INVALID_PARAMETER_MIX
;
1706 cmd
= CVAL(iov
[1].iov_base
, HDR_COM
);
1707 if (cmd
== SMBreadBraw
) {
1708 if (smbXcli_conn_has_async_calls(state
->conn
)) {
1709 return NT_STATUS_INVALID_PARAMETER_MIX
;
1711 state
->conn
->smb1
.read_braw_req
= req
;
1714 if (state
->smb1
.mid
!= 0) {
1715 mid
= state
->smb1
.mid
;
1717 mid
= smb1cli_alloc_mid(state
->conn
);
1719 SSVAL(iov
[1].iov_base
, HDR_MID
, mid
);
1721 nbtlen
= iov_buflen(&iov
[1], iov_count
-1);
1722 if ((nbtlen
== -1) || (nbtlen
> 0x1FFFF)) {
1723 return NT_STATUS_INVALID_PARAMETER_MIX
;
1726 _smb_setlen_nbt(iov
[0].iov_base
, nbtlen
);
1728 status
= smb1cli_conn_signv(state
->conn
, iov
, iov_count
,
1729 &state
->smb1
.seqnum
,
1730 state
->smb1
.one_way_seqnum
);
1732 if (!NT_STATUS_IS_OK(status
)) {
1737 * If we supported multiple encrytion contexts
1738 * here we'd look up based on tid.
1740 if (common_encryption_on(state
->conn
->smb1
.trans_enc
)) {
1741 char *buf
, *enc_buf
;
1743 buf
= (char *)iov_concat(talloc_tos(), iov
, iov_count
);
1745 return NT_STATUS_NO_MEMORY
;
1747 status
= common_encrypt_buffer(state
->conn
->smb1
.trans_enc
,
1748 (char *)buf
, &enc_buf
);
1750 if (!NT_STATUS_IS_OK(status
)) {
1751 DEBUG(0, ("Error in encrypting client message: %s\n",
1752 nt_errstr(status
)));
1755 buf
= (char *)talloc_memdup(state
, enc_buf
,
1756 smb_len_nbt(enc_buf
)+4);
1759 return NT_STATUS_NO_MEMORY
;
1761 iov
[0].iov_base
= (void *)buf
;
1762 iov
[0].iov_len
= talloc_get_size(buf
);
1766 if (state
->conn
->dispatch_incoming
== NULL
) {
1767 state
->conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
1770 if (!smbXcli_req_set_pending(req
)) {
1771 return NT_STATUS_NO_MEMORY
;
1774 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
1776 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
1777 state
->conn
->sock_fd
, false, iov
, iov_count
);
1778 if (subreq
== NULL
) {
1779 return NT_STATUS_NO_MEMORY
;
1781 tevent_req_set_callback(subreq
, smb1cli_req_writev_done
, req
);
1782 state
->write_req
= subreq
;
1784 return NT_STATUS_OK
;
1787 struct tevent_req
*smb1cli_req_send(TALLOC_CTX
*mem_ctx
,
1788 struct tevent_context
*ev
,
1789 struct smbXcli_conn
*conn
,
1790 uint8_t smb_command
,
1791 uint8_t additional_flags
,
1792 uint8_t clear_flags
,
1793 uint16_t additional_flags2
,
1794 uint16_t clear_flags2
,
1795 uint32_t timeout_msec
,
1797 struct smbXcli_tcon
*tcon
,
1798 struct smbXcli_session
*session
,
1799 uint8_t wct
, uint16_t *vwv
,
1801 const uint8_t *bytes
)
1803 struct tevent_req
*req
;
1807 iov
.iov_base
= discard_const_p(void, bytes
);
1808 iov
.iov_len
= num_bytes
;
1810 req
= smb1cli_req_create(mem_ctx
, ev
, conn
, smb_command
,
1811 additional_flags
, clear_flags
,
1812 additional_flags2
, clear_flags2
,
1819 if (!tevent_req_is_in_progress(req
)) {
1820 return tevent_req_post(req
, ev
);
1822 status
= smb1cli_req_chain_submit(&req
, 1);
1823 if (tevent_req_nterror(req
, status
)) {
1824 return tevent_req_post(req
, ev
);
1829 static void smb1cli_req_writev_done(struct tevent_req
*subreq
)
1831 struct tevent_req
*req
=
1832 tevent_req_callback_data(subreq
,
1834 struct smbXcli_req_state
*state
=
1835 tevent_req_data(req
,
1836 struct smbXcli_req_state
);
1840 state
->write_req
= NULL
;
1842 nwritten
= writev_recv(subreq
, &err
);
1843 TALLOC_FREE(subreq
);
1844 if (nwritten
== -1) {
1845 /* here, we need to notify all pending requests */
1846 NTSTATUS status
= map_nt_error_from_unix_common(err
);
1847 smbXcli_conn_disconnect(state
->conn
, status
);
1851 if (state
->one_way
) {
1852 state
->inbuf
= NULL
;
1853 tevent_req_done(req
);
1858 static void smbXcli_conn_received(struct tevent_req
*subreq
)
1860 struct smbXcli_conn
*conn
=
1861 tevent_req_callback_data(subreq
,
1862 struct smbXcli_conn
);
1863 TALLOC_CTX
*frame
= talloc_stackframe();
1869 if (subreq
!= conn
->read_smb_req
) {
1870 DEBUG(1, ("Internal error: cli_smb_received called with "
1871 "unexpected subreq\n"));
1872 smbXcli_conn_disconnect(conn
, NT_STATUS_INTERNAL_ERROR
);
1876 conn
->read_smb_req
= NULL
;
1878 received
= read_smb_recv(subreq
, frame
, &inbuf
, &err
);
1879 TALLOC_FREE(subreq
);
1880 if (received
== -1) {
1881 status
= map_nt_error_from_unix_common(err
);
1882 smbXcli_conn_disconnect(conn
, status
);
1887 status
= conn
->dispatch_incoming(conn
, frame
, inbuf
);
1889 if (NT_STATUS_IS_OK(status
)) {
1891 * We should not do any more processing
1892 * as the dispatch function called
1893 * tevent_req_done().
1898 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1900 * We got an error, so notify all pending requests
1902 smbXcli_conn_disconnect(conn
, status
);
1907 * We got NT_STATUS_RETRY, so we may ask for a
1908 * next incoming pdu.
1910 if (!smbXcli_conn_receive_next(conn
)) {
1911 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
1915 static NTSTATUS
smb1cli_inbuf_parse_chain(uint8_t *buf
, TALLOC_CTX
*mem_ctx
,
1916 struct iovec
**piov
, int *pnum_iov
)
1927 size_t min_size
= MIN_SMB_SIZE
;
1929 buflen
= smb_len_tcp(buf
);
1932 hdr
= buf
+ NBT_HDR_SIZE
;
1934 status
= smb1cli_pull_raw_error(hdr
);
1935 if (NT_STATUS_IS_ERR(status
)) {
1937 * This is an ugly hack to support OS/2
1938 * which skips the byte_count in the DATA block
1939 * on some error responses.
1943 min_size
-= sizeof(uint16_t);
1946 if (buflen
< min_size
) {
1947 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1951 * This returns iovec elements in the following order:
1966 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
1968 return NT_STATUS_NO_MEMORY
;
1970 iov
[0].iov_base
= hdr
;
1971 iov
[0].iov_len
= HDR_WCT
;
1974 cmd
= CVAL(hdr
, HDR_COM
);
1978 size_t len
= buflen
- taken
;
1980 struct iovec
*iov_tmp
;
1987 * we need at least WCT
1989 needed
= sizeof(uint8_t);
1991 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1992 __location__
, (int)len
, (int)needed
));
1997 * Now we check if the specified words are there
1999 wct
= CVAL(hdr
, wct_ofs
);
2000 needed
+= wct
* sizeof(uint16_t);
2002 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2003 __location__
, (int)len
, (int)needed
));
2007 if ((num_iov
== 1) &&
2009 NT_STATUS_IS_ERR(status
))
2012 * This is an ugly hack to support OS/2
2013 * which skips the byte_count in the DATA block
2014 * on some error responses.
2018 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
2020 if (iov_tmp
== NULL
) {
2022 return NT_STATUS_NO_MEMORY
;
2025 cur
= &iov
[num_iov
];
2029 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
2031 cur
[1].iov_base
= cur
[0].iov_base
;
2038 * we need at least BCC
2040 needed
+= sizeof(uint16_t);
2042 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2043 __location__
, (int)len
, (int)needed
));
2048 * Now we check if the specified bytes are there
2050 bcc_ofs
= wct_ofs
+ sizeof(uint8_t) + wct
* sizeof(uint16_t);
2051 bcc
= SVAL(hdr
, bcc_ofs
);
2052 needed
+= bcc
* sizeof(uint8_t);
2054 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2055 __location__
, (int)len
, (int)needed
));
2060 * we allocate 2 iovec structures for words and bytes
2062 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
2064 if (iov_tmp
== NULL
) {
2066 return NT_STATUS_NO_MEMORY
;
2069 cur
= &iov
[num_iov
];
2072 cur
[0].iov_len
= wct
* sizeof(uint16_t);
2073 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
2074 cur
[1].iov_len
= bcc
* sizeof(uint8_t);
2075 cur
[1].iov_base
= hdr
+ (bcc_ofs
+ sizeof(uint16_t));
2079 if (!smb1cli_is_andx_req(cmd
)) {
2081 * If the current command does not have AndX chanining
2087 if (wct
== 0 && bcc
== 0) {
2089 * An empty response also ends the chain,
2090 * most likely with an error.
2096 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
2097 __location__
, (int)wct
, (int)cmd
));
2100 cmd
= CVAL(cur
[0].iov_base
, 0);
2103 * If it is the end of the chain we are also done.
2107 wct_ofs
= SVAL(cur
[0].iov_base
, 2);
2109 if (wct_ofs
< taken
) {
2112 if (wct_ofs
> buflen
) {
2117 * we consumed everything up to the start of the next
2123 remaining
= buflen
- taken
;
2125 if (remaining
> 0 && num_iov
>= 3) {
2127 * The last DATA block gets the remaining
2128 * bytes, this is needed to support
2129 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
2131 iov
[num_iov
-1].iov_len
+= remaining
;
2135 *pnum_iov
= num_iov
;
2136 return NT_STATUS_OK
;
2140 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2143 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
2144 TALLOC_CTX
*tmp_mem
,
2147 struct tevent_req
*req
;
2148 struct smbXcli_req_state
*state
;
2155 uint8_t *inhdr
= inbuf
+ NBT_HDR_SIZE
;
2156 size_t len
= smb_len_tcp(inbuf
);
2157 struct iovec
*iov
= NULL
;
2159 struct tevent_req
**chain
= NULL
;
2160 size_t num_chained
= 0;
2161 size_t num_responses
= 0;
2163 if (conn
->smb1
.read_braw_req
!= NULL
) {
2164 req
= conn
->smb1
.read_braw_req
;
2165 conn
->smb1
.read_braw_req
= NULL
;
2166 state
= tevent_req_data(req
, struct smbXcli_req_state
);
2168 smbXcli_req_unset_pending(req
);
2170 if (state
->smb1
.recv_iov
== NULL
) {
2172 * For requests with more than
2173 * one response, we have to readd the
2176 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2179 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2180 return NT_STATUS_OK
;
2184 state
->smb1
.recv_iov
[0].iov_base
= (void *)(inhdr
);
2185 state
->smb1
.recv_iov
[0].iov_len
= len
;
2186 ZERO_STRUCT(state
->smb1
.recv_iov
[1]);
2187 ZERO_STRUCT(state
->smb1
.recv_iov
[2]);
2189 state
->smb1
.recv_cmd
= SMBreadBraw
;
2190 state
->smb1
.recv_status
= NT_STATUS_OK
;
2191 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
2193 tevent_req_done(req
);
2194 return NT_STATUS_OK
;
2197 if ((IVAL(inhdr
, 0) != SMB_MAGIC
) /* 0xFF"SMB" */
2198 && (SVAL(inhdr
, 0) != 0x45ff)) /* 0xFF"E" */ {
2199 DEBUG(10, ("Got non-SMB PDU\n"));
2200 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2204 * If we supported multiple encrytion contexts
2205 * here we'd look up based on tid.
2207 if (common_encryption_on(conn
->smb1
.trans_enc
)
2208 && (CVAL(inbuf
, 0) == 0)) {
2209 uint16_t enc_ctx_num
;
2211 status
= get_enc_ctx_num(inbuf
, &enc_ctx_num
);
2212 if (!NT_STATUS_IS_OK(status
)) {
2213 DEBUG(10, ("get_enc_ctx_num returned %s\n",
2214 nt_errstr(status
)));
2218 if (enc_ctx_num
!= conn
->smb1
.trans_enc
->enc_ctx_num
) {
2219 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
2221 conn
->smb1
.trans_enc
->enc_ctx_num
));
2222 return NT_STATUS_INVALID_HANDLE
;
2225 status
= common_decrypt_buffer(conn
->smb1
.trans_enc
,
2227 if (!NT_STATUS_IS_OK(status
)) {
2228 DEBUG(10, ("common_decrypt_buffer returned %s\n",
2229 nt_errstr(status
)));
2232 inhdr
= inbuf
+ NBT_HDR_SIZE
;
2233 len
= smb_len_nbt(inbuf
);
2236 mid
= SVAL(inhdr
, HDR_MID
);
2237 num_pending
= talloc_array_length(conn
->pending
);
2239 for (i
=0; i
<num_pending
; i
++) {
2240 if (mid
== smb1cli_req_mid(conn
->pending
[i
])) {
2244 if (i
== num_pending
) {
2245 /* Dump unexpected reply */
2246 return NT_STATUS_RETRY
;
2249 oplock_break
= false;
2251 if (mid
== 0xffff) {
2253 * Paranoia checks that this is really an oplock break request.
2255 oplock_break
= (len
== 51); /* hdr + 8 words */
2256 oplock_break
&= ((CVAL(inhdr
, HDR_FLG
) & FLAG_REPLY
) == 0);
2257 oplock_break
&= (CVAL(inhdr
, HDR_COM
) == SMBlockingX
);
2258 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(6)) == 0);
2259 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(7)) == 0);
2261 if (!oplock_break
) {
2262 /* Dump unexpected reply */
2263 return NT_STATUS_RETRY
;
2267 req
= conn
->pending
[i
];
2268 state
= tevent_req_data(req
, struct smbXcli_req_state
);
2270 if (!oplock_break
/* oplock breaks are not signed */
2271 && !smb_signing_check_pdu(conn
->smb1
.signing
,
2272 inhdr
, len
, state
->smb1
.seqnum
+1)) {
2273 DEBUG(10, ("cli_check_sign_mac failed\n"));
2274 return NT_STATUS_ACCESS_DENIED
;
2277 status
= smb1cli_inbuf_parse_chain(inbuf
, tmp_mem
,
2279 if (!NT_STATUS_IS_OK(status
)) {
2280 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2281 nt_errstr(status
)));
2285 cmd
= CVAL(inhdr
, HDR_COM
);
2286 status
= smb1cli_pull_raw_error(inhdr
);
2288 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
) &&
2289 (state
->session
!= NULL
) && state
->session
->disconnect_expired
)
2292 * this should be a short term hack
2293 * until the upper layers have implemented
2294 * re-authentication.
2299 if (state
->smb1
.chained_requests
== NULL
) {
2301 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2304 smbXcli_req_unset_pending(req
);
2306 if (state
->smb1
.recv_iov
== NULL
) {
2308 * For requests with more than
2309 * one response, we have to readd the
2312 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2315 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2316 return NT_STATUS_OK
;
2320 state
->smb1
.recv_cmd
= cmd
;
2321 state
->smb1
.recv_status
= status
;
2322 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
2324 state
->smb1
.recv_iov
[0] = iov
[0];
2325 state
->smb1
.recv_iov
[1] = iov
[1];
2326 state
->smb1
.recv_iov
[2] = iov
[2];
2328 if (talloc_array_length(conn
->pending
) == 0) {
2329 tevent_req_done(req
);
2330 return NT_STATUS_OK
;
2333 tevent_req_defer_callback(req
, state
->ev
);
2334 tevent_req_done(req
);
2335 return NT_STATUS_RETRY
;
2338 chain
= talloc_move(tmp_mem
, &state
->smb1
.chained_requests
);
2339 num_chained
= talloc_array_length(chain
);
2340 num_responses
= (num_iov
- 1)/2;
2342 if (num_responses
> num_chained
) {
2343 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2346 for (i
=0; i
<num_chained
; i
++) {
2347 size_t iov_idx
= 1 + (i
*2);
2348 struct iovec
*cur
= &iov
[iov_idx
];
2352 state
= tevent_req_data(req
, struct smbXcli_req_state
);
2354 smbXcli_req_unset_pending(req
);
2357 * as we finish multiple requests here
2358 * we need to defer the callbacks as
2359 * they could destroy our current stack state.
2361 tevent_req_defer_callback(req
, state
->ev
);
2363 if (i
>= num_responses
) {
2364 tevent_req_nterror(req
, NT_STATUS_REQUEST_ABORTED
);
2368 if (state
->smb1
.recv_iov
== NULL
) {
2370 * For requests with more than
2371 * one response, we have to readd the
2374 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2377 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2382 state
->smb1
.recv_cmd
= cmd
;
2384 if (i
== (num_responses
- 1)) {
2386 * The last request in the chain gets the status
2388 state
->smb1
.recv_status
= status
;
2390 cmd
= CVAL(cur
[0].iov_base
, 0);
2391 state
->smb1
.recv_status
= NT_STATUS_OK
;
2394 state
->inbuf
= inbuf
;
2397 * Note: here we use talloc_reference() in a way
2398 * that does not expose it to the caller.
2400 inbuf_ref
= talloc_reference(state
->smb1
.recv_iov
, inbuf
);
2401 if (tevent_req_nomem(inbuf_ref
, req
)) {
2405 /* copy the related buffers */
2406 state
->smb1
.recv_iov
[0] = iov
[0];
2407 state
->smb1
.recv_iov
[1] = cur
[0];
2408 state
->smb1
.recv_iov
[2] = cur
[1];
2410 tevent_req_done(req
);
2413 return NT_STATUS_RETRY
;
2416 NTSTATUS
smb1cli_req_recv(struct tevent_req
*req
,
2417 TALLOC_CTX
*mem_ctx
,
2418 struct iovec
**piov
,
2422 uint32_t *pvwv_offset
,
2423 uint32_t *pnum_bytes
,
2425 uint32_t *pbytes_offset
,
2427 const struct smb1cli_req_expected_response
*expected
,
2428 size_t num_expected
)
2430 struct smbXcli_req_state
*state
=
2431 tevent_req_data(req
,
2432 struct smbXcli_req_state
);
2433 NTSTATUS status
= NT_STATUS_OK
;
2434 struct iovec
*recv_iov
= NULL
;
2435 uint8_t *hdr
= NULL
;
2437 uint32_t vwv_offset
= 0;
2438 uint16_t *vwv
= NULL
;
2439 uint32_t num_bytes
= 0;
2440 uint32_t bytes_offset
= 0;
2441 uint8_t *bytes
= NULL
;
2443 bool found_status
= false;
2444 bool found_size
= false;
2458 if (pvwv_offset
!= NULL
) {
2461 if (pnum_bytes
!= NULL
) {
2464 if (pbytes
!= NULL
) {
2467 if (pbytes_offset
!= NULL
) {
2470 if (pinbuf
!= NULL
) {
2474 if (state
->inbuf
!= NULL
) {
2475 recv_iov
= state
->smb1
.recv_iov
;
2476 state
->smb1
.recv_iov
= NULL
;
2477 if (state
->smb1
.recv_cmd
!= SMBreadBraw
) {
2478 hdr
= (uint8_t *)recv_iov
[0].iov_base
;
2479 wct
= recv_iov
[1].iov_len
/2;
2480 vwv
= (uint16_t *)recv_iov
[1].iov_base
;
2481 vwv_offset
= PTR_DIFF(vwv
, hdr
);
2482 num_bytes
= recv_iov
[2].iov_len
;
2483 bytes
= (uint8_t *)recv_iov
[2].iov_base
;
2484 bytes_offset
= PTR_DIFF(bytes
, hdr
);
2488 if (tevent_req_is_nterror(req
, &status
)) {
2489 for (i
=0; i
< num_expected
; i
++) {
2490 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2491 found_status
= true;
2497 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
2503 if (num_expected
== 0) {
2504 found_status
= true;
2508 status
= state
->smb1
.recv_status
;
2510 for (i
=0; i
< num_expected
; i
++) {
2511 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2515 found_status
= true;
2516 if (expected
[i
].wct
== 0) {
2521 if (expected
[i
].wct
== wct
) {
2527 if (!found_status
) {
2532 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2536 *piov
= talloc_move(mem_ctx
, &recv_iov
);
2548 if (pvwv_offset
!= NULL
) {
2549 *pvwv_offset
= vwv_offset
;
2551 if (pnum_bytes
!= NULL
) {
2552 *pnum_bytes
= num_bytes
;
2554 if (pbytes
!= NULL
) {
2557 if (pbytes_offset
!= NULL
) {
2558 *pbytes_offset
= bytes_offset
;
2560 if (pinbuf
!= NULL
) {
2561 *pinbuf
= state
->inbuf
;
2567 size_t smb1cli_req_wct_ofs(struct tevent_req
**reqs
, int num_reqs
)
2574 for (i
=0; i
<num_reqs
; i
++) {
2575 struct smbXcli_req_state
*state
;
2576 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2577 wct_ofs
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2578 state
->smb1
.iov_count
-2);
2579 wct_ofs
= (wct_ofs
+ 3) & ~3;
2584 NTSTATUS
smb1cli_req_chain_submit(struct tevent_req
**reqs
, int num_reqs
)
2586 struct smbXcli_req_state
*first_state
=
2587 tevent_req_data(reqs
[0],
2588 struct smbXcli_req_state
);
2589 struct smbXcli_req_state
*state
;
2591 size_t chain_padding
= 0;
2593 struct iovec
*iov
= NULL
;
2594 struct iovec
*this_iov
;
2598 if (num_reqs
== 1) {
2599 return smb1cli_req_writev_submit(reqs
[0], first_state
,
2600 first_state
->smb1
.iov
,
2601 first_state
->smb1
.iov_count
);
2605 for (i
=0; i
<num_reqs
; i
++) {
2606 if (!tevent_req_is_in_progress(reqs
[i
])) {
2607 return NT_STATUS_INTERNAL_ERROR
;
2610 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2612 if (state
->smb1
.iov_count
< 4) {
2613 return NT_STATUS_INVALID_PARAMETER_MIX
;
2618 * The NBT and SMB header
2631 iovlen
+= state
->smb1
.iov_count
- 2;
2634 iov
= talloc_zero_array(first_state
, struct iovec
, iovlen
);
2636 return NT_STATUS_NO_MEMORY
;
2639 first_state
->smb1
.chained_requests
= (struct tevent_req
**)talloc_memdup(
2640 first_state
, reqs
, sizeof(*reqs
) * num_reqs
);
2641 if (first_state
->smb1
.chained_requests
== NULL
) {
2643 return NT_STATUS_NO_MEMORY
;
2646 wct_offset
= HDR_WCT
;
2649 for (i
=0; i
<num_reqs
; i
++) {
2650 size_t next_padding
= 0;
2653 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2655 if (i
< num_reqs
-1) {
2656 if (!smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))
2657 || CVAL(state
->smb1
.hdr
, HDR_WCT
) < 2) {
2659 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2660 return NT_STATUS_INVALID_PARAMETER_MIX
;
2664 wct_offset
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2665 state
->smb1
.iov_count
-2) + 1;
2666 if ((wct_offset
% 4) != 0) {
2667 next_padding
= 4 - (wct_offset
% 4);
2669 wct_offset
+= next_padding
;
2670 vwv
= state
->smb1
.vwv
;
2672 if (i
< num_reqs
-1) {
2673 struct smbXcli_req_state
*next_state
=
2674 tevent_req_data(reqs
[i
+1],
2675 struct smbXcli_req_state
);
2676 SCVAL(vwv
+0, 0, CVAL(next_state
->smb1
.hdr
, HDR_COM
));
2678 SSVAL(vwv
+1, 0, wct_offset
);
2679 } else if (smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))) {
2680 /* properly end the chain */
2681 SCVAL(vwv
+0, 0, 0xff);
2682 SCVAL(vwv
+0, 1, 0xff);
2688 * The NBT and SMB header
2690 this_iov
[0] = state
->smb1
.iov
[0];
2691 this_iov
[1] = state
->smb1
.iov
[1];
2695 * This one is a bit subtle. We have to add
2696 * chain_padding bytes between the requests, and we
2697 * have to also include the wct field of the
2698 * subsequent requests. We use the subsequent header
2699 * for the padding, it contains the wct field in its
2702 this_iov
[0].iov_len
= chain_padding
+1;
2703 this_iov
[0].iov_base
= (void *)&state
->smb1
.hdr
[
2704 sizeof(state
->smb1
.hdr
) - this_iov
[0].iov_len
];
2705 memset(this_iov
[0].iov_base
, 0, this_iov
[0].iov_len
-1);
2710 * copy the words and bytes
2712 memcpy(this_iov
, state
->smb1
.iov
+2,
2713 sizeof(struct iovec
) * (state
->smb1
.iov_count
-2));
2714 this_iov
+= state
->smb1
.iov_count
- 2;
2715 chain_padding
= next_padding
;
2718 nbt_len
= iov_buflen(&iov
[1], iovlen
-1);
2719 if ((nbt_len
== -1) || (nbt_len
> first_state
->conn
->smb1
.max_xmit
)) {
2721 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2722 return NT_STATUS_INVALID_PARAMETER_MIX
;
2725 status
= smb1cli_req_writev_submit(reqs
[0], first_state
, iov
, iovlen
);
2726 if (!NT_STATUS_IS_OK(status
)) {
2728 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2732 return NT_STATUS_OK
;
2735 bool smbXcli_conn_has_async_calls(struct smbXcli_conn
*conn
)
2737 return ((tevent_queue_length(conn
->outgoing
) != 0)
2738 || (talloc_array_length(conn
->pending
) != 0));
2741 bool smbXcli_conn_dfs_supported(struct smbXcli_conn
*conn
)
2743 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
2744 return (smb2cli_conn_server_capabilities(conn
) & SMB2_CAP_DFS
);
2747 return (smb1cli_conn_capabilities(conn
) & CAP_DFS
);
2750 bool smb2cli_conn_req_possible(struct smbXcli_conn
*conn
, uint32_t *max_dyn_len
)
2752 uint16_t credits
= 1;
2754 if (conn
->smb2
.cur_credits
== 0) {
2755 if (max_dyn_len
!= NULL
) {
2761 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2762 credits
= conn
->smb2
.cur_credits
;
2765 if (max_dyn_len
!= NULL
) {
2766 *max_dyn_len
= credits
* 65536;
2772 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn
*conn
)
2774 return conn
->smb2
.server
.capabilities
;
2777 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
2779 return conn
->smb2
.server
.security_mode
;
2782 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn
*conn
)
2784 return conn
->smb2
.server
.max_trans_size
;
2787 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn
*conn
)
2789 return conn
->smb2
.server
.max_read_size
;
2792 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn
*conn
)
2794 return conn
->smb2
.server
.max_write_size
;
2797 void smb2cli_conn_set_max_credits(struct smbXcli_conn
*conn
,
2798 uint16_t max_credits
)
2800 conn
->smb2
.max_credits
= max_credits
;
2803 uint16_t smb2cli_conn_get_cur_credits(struct smbXcli_conn
*conn
)
2805 return conn
->smb2
.cur_credits
;
2808 uint8_t smb2cli_conn_get_io_priority(struct smbXcli_conn
*conn
)
2810 if (conn
->protocol
< PROTOCOL_SMB3_11
) {
2814 return conn
->smb2
.io_priority
;
2817 void smb2cli_conn_set_io_priority(struct smbXcli_conn
*conn
,
2818 uint8_t io_priority
)
2820 conn
->smb2
.io_priority
= io_priority
;
2823 uint32_t smb2cli_conn_cc_chunk_len(struct smbXcli_conn
*conn
)
2825 return conn
->smb2
.cc_chunk_len
;
2828 void smb2cli_conn_set_cc_chunk_len(struct smbXcli_conn
*conn
,
2831 conn
->smb2
.cc_chunk_len
= chunk_len
;
2834 uint32_t smb2cli_conn_cc_max_chunks(struct smbXcli_conn
*conn
)
2836 return conn
->smb2
.cc_max_chunks
;
2839 void smb2cli_conn_set_cc_max_chunks(struct smbXcli_conn
*conn
,
2840 uint32_t max_chunks
)
2842 conn
->smb2
.cc_max_chunks
= max_chunks
;
2845 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
);
2847 static bool smb2cli_req_cancel(struct tevent_req
*req
)
2849 struct smbXcli_req_state
*state
=
2850 tevent_req_data(req
,
2851 struct smbXcli_req_state
);
2852 struct smbXcli_tcon
*tcon
= state
->tcon
;
2853 struct smbXcli_session
*session
= state
->session
;
2854 uint8_t *fixed
= state
->smb2
.pad
;
2855 uint16_t fixed_len
= 4;
2856 struct tevent_req
*subreq
;
2857 struct smbXcli_req_state
*substate
;
2860 SSVAL(fixed
, 0, 0x04);
2863 subreq
= smb2cli_req_create(state
, state
->ev
,
2871 if (subreq
== NULL
) {
2874 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
2876 SIVAL(substate
->smb2
.hdr
, SMB2_HDR_FLAGS
, state
->smb2
.cancel_flags
);
2877 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, state
->smb2
.cancel_mid
);
2878 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, state
->smb2
.cancel_aid
);
2880 status
= smb2cli_req_compound_submit(&subreq
, 1);
2881 if (!NT_STATUS_IS_OK(status
)) {
2882 TALLOC_FREE(subreq
);
2886 tevent_req_set_callback(subreq
, smb2cli_req_cancel_done
, NULL
);
2891 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
)
2893 /* we do not care about the result */
2894 TALLOC_FREE(subreq
);
2897 struct timeval
smbXcli_req_endtime(struct tevent_req
*req
)
2899 struct smbXcli_req_state
*state
= tevent_req_data(
2900 req
, struct smbXcli_req_state
);
2902 return state
->endtime
;
2905 struct tevent_req
*smb2cli_req_create(TALLOC_CTX
*mem_ctx
,
2906 struct tevent_context
*ev
,
2907 struct smbXcli_conn
*conn
,
2909 uint32_t additional_flags
,
2910 uint32_t clear_flags
,
2911 uint32_t timeout_msec
,
2912 struct smbXcli_tcon
*tcon
,
2913 struct smbXcli_session
*session
,
2914 const uint8_t *fixed
,
2918 uint32_t max_dyn_len
)
2920 struct tevent_req
*req
;
2921 struct smbXcli_req_state
*state
;
2925 bool use_channel_sequence
= conn
->smb2
.force_channel_sequence
;
2926 uint16_t channel_sequence
= 0;
2927 bool use_replay_flag
= false;
2929 req
= tevent_req_create(mem_ctx
, &state
,
2930 struct smbXcli_req_state
);
2937 state
->session
= session
;
2940 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_PERSISTENT_HANDLES
) {
2941 use_channel_sequence
= true;
2942 } else if (conn
->smb2
.server
.capabilities
& SMB2_CAP_MULTI_CHANNEL
) {
2943 use_channel_sequence
= true;
2946 if (smbXcli_conn_protocol(conn
) >= PROTOCOL_SMB3_00
) {
2947 use_replay_flag
= true;
2950 if (smbXcli_conn_protocol(conn
) >= PROTOCOL_SMB3_11
) {
2951 flags
|= SMB2_PRIORITY_VALUE_TO_MASK(conn
->smb2
.io_priority
);
2955 uid
= session
->smb2
->session_id
;
2957 if (use_channel_sequence
) {
2958 channel_sequence
= session
->smb2
->channel_sequence
;
2961 if (use_replay_flag
&& session
->smb2
->replay_active
) {
2962 additional_flags
|= SMB2_HDR_FLAG_REPLAY_OPERATION
;
2965 state
->smb2
.should_sign
= session
->smb2
->should_sign
;
2966 state
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
2967 state
->smb2
.require_signed_response
=
2968 session
->smb2
->require_signed_response
;
2970 if (cmd
== SMB2_OP_SESSSETUP
&&
2971 session
->smb2_channel
.signing_key
.length
== 0 &&
2972 session
->smb2
->signing_key
.length
!= 0)
2975 * a session bind needs to be signed
2977 state
->smb2
.should_sign
= true;
2980 if (cmd
== SMB2_OP_SESSSETUP
&&
2981 session
->smb2_channel
.signing_key
.length
== 0) {
2982 state
->smb2
.should_encrypt
= false;
2985 if (additional_flags
& SMB2_HDR_FLAG_SIGNED
) {
2986 if (session
->smb2_channel
.signing_key
.length
== 0) {
2987 tevent_req_nterror(req
, NT_STATUS_NO_USER_SESSION_KEY
);
2991 additional_flags
&= ~SMB2_HDR_FLAG_SIGNED
;
2992 state
->smb2
.should_sign
= true;
2997 tid
= tcon
->smb2
.tcon_id
;
2999 if (tcon
->smb2
.should_sign
) {
3000 state
->smb2
.should_sign
= true;
3002 if (tcon
->smb2
.should_encrypt
) {
3003 state
->smb2
.should_encrypt
= true;
3007 if (state
->smb2
.should_encrypt
) {
3008 state
->smb2
.should_sign
= false;
3011 state
->smb2
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
3012 if (state
->smb2
.recv_iov
== NULL
) {
3017 flags
|= additional_flags
;
3018 flags
&= ~clear_flags
;
3020 state
->smb2
.fixed
= fixed
;
3021 state
->smb2
.fixed_len
= fixed_len
;
3022 state
->smb2
.dyn
= dyn
;
3023 state
->smb2
.dyn_len
= dyn_len
;
3024 state
->smb2
.max_dyn_len
= max_dyn_len
;
3026 if (state
->smb2
.should_encrypt
) {
3027 SIVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
3028 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
, uid
);
3031 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PROTOCOL_ID
, SMB2_MAGIC
);
3032 SSVAL(state
->smb2
.hdr
, SMB2_HDR_LENGTH
, SMB2_HDR_BODY
);
3033 SSVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
, cmd
);
3034 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CHANNEL_SEQUENCE
, channel_sequence
);
3035 SIVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
3036 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PID
, 0); /* reserved */
3037 SIVAL(state
->smb2
.hdr
, SMB2_HDR_TID
, tid
);
3038 SBVAL(state
->smb2
.hdr
, SMB2_HDR_SESSION_ID
, uid
);
3041 case SMB2_OP_CANCEL
:
3042 state
->one_way
= true;
3046 * If this is a dummy request, it will have
3047 * UINT64_MAX as message id.
3048 * If we send on break acknowledgement,
3049 * this gets overwritten later.
3051 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, UINT64_MAX
);
3055 if (timeout_msec
> 0) {
3056 state
->endtime
= timeval_current_ofs_msec(timeout_msec
);
3057 if (!tevent_req_set_endtime(req
, ev
, state
->endtime
)) {
3065 void smb2cli_req_set_notify_async(struct tevent_req
*req
)
3067 struct smbXcli_req_state
*state
=
3068 tevent_req_data(req
,
3069 struct smbXcli_req_state
);
3071 state
->smb2
.notify_async
= true;
3074 static void smb2cli_req_writev_done(struct tevent_req
*subreq
);
3075 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
3076 TALLOC_CTX
*tmp_mem
,
3079 NTSTATUS
smb2cli_req_compound_submit(struct tevent_req
**reqs
,
3082 struct smbXcli_req_state
*state
;
3083 struct tevent_req
*subreq
;
3085 int i
, num_iov
, nbt_len
;
3087 const DATA_BLOB
*encryption_key
= NULL
;
3088 uint64_t encryption_session_id
= 0;
3089 uint64_t nonce_high
= UINT64_MAX
;
3090 uint64_t nonce_low
= UINT64_MAX
;
3093 * 1 for the nbt length, optional TRANSFORM
3094 * per request: HDR, fixed, dyn, padding
3095 * -1 because the last one does not need padding
3098 iov
= talloc_array(reqs
[0], struct iovec
, 1 + 1 + 4*num_reqs
- 1);
3100 return NT_STATUS_NO_MEMORY
;
3107 * the session of the first request that requires encryption
3108 * specifies the encryption key.
3110 for (i
=0; i
<num_reqs
; i
++) {
3111 if (!tevent_req_is_in_progress(reqs
[i
])) {
3112 return NT_STATUS_INTERNAL_ERROR
;
3115 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
3117 if (!smbXcli_conn_is_connected(state
->conn
)) {
3118 return NT_STATUS_CONNECTION_DISCONNECTED
;
3121 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
3122 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
3123 return NT_STATUS_REVISION_MISMATCH
;
3126 if (state
->session
== NULL
) {
3130 if (!state
->smb2
.should_encrypt
) {
3134 encryption_key
= &state
->session
->smb2
->encryption_key
;
3135 if (encryption_key
->length
== 0) {
3136 return NT_STATUS_INVALID_PARAMETER_MIX
;
3139 encryption_session_id
= state
->session
->smb2
->session_id
;
3141 state
->session
->smb2
->nonce_low
+= 1;
3142 if (state
->session
->smb2
->nonce_low
== 0) {
3143 state
->session
->smb2
->nonce_high
+= 1;
3144 state
->session
->smb2
->nonce_low
+= 1;
3148 * CCM and GCM algorithms must never have their
3149 * nonce wrap, or the security of the whole
3150 * communication and the keys is destroyed.
3151 * We must drop the connection once we have
3152 * transfered too much data.
3154 * NOTE: We assume nonces greater than 8 bytes.
3156 if (state
->session
->smb2
->nonce_high
>=
3157 state
->session
->smb2
->nonce_high_max
)
3159 return NT_STATUS_ENCRYPTION_FAILED
;
3162 nonce_high
= state
->session
->smb2
->nonce_high_random
;
3163 nonce_high
+= state
->session
->smb2
->nonce_high
;
3164 nonce_low
= state
->session
->smb2
->nonce_low
;
3167 iov
[num_iov
].iov_base
= state
->smb2
.transform
;
3168 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.transform
);
3171 SBVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
3172 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
,
3174 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
+8,
3176 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
,
3177 encryption_session_id
);
3179 nbt_len
+= SMB2_TF_HDR_SIZE
;
3183 for (i
=0; i
<num_reqs
; i
++) {
3192 const DATA_BLOB
*signing_key
= NULL
;
3194 if (!tevent_req_is_in_progress(reqs
[i
])) {
3195 return NT_STATUS_INTERNAL_ERROR
;
3198 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
3200 if (!smbXcli_conn_is_connected(state
->conn
)) {
3201 return NT_STATUS_CONNECTION_DISCONNECTED
;
3204 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
3205 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
3206 return NT_STATUS_REVISION_MISMATCH
;
3209 opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
3210 if (opcode
== SMB2_OP_CANCEL
) {
3214 avail
= UINT64_MAX
- state
->conn
->smb2
.mid
;
3216 return NT_STATUS_CONNECTION_ABORTED
;
3219 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
3220 uint32_t max_dyn_len
= 1;
3222 max_dyn_len
= MAX(max_dyn_len
, state
->smb2
.dyn_len
);
3223 max_dyn_len
= MAX(max_dyn_len
, state
->smb2
.max_dyn_len
);
3225 charge
= (max_dyn_len
- 1)/ 65536 + 1;
3230 charge
= MAX(state
->smb2
.credit_charge
, charge
);
3232 avail
= MIN(avail
, state
->conn
->smb2
.cur_credits
);
3233 if (avail
< charge
) {
3234 DBG_ERR("Insufficient credits. "
3235 "%"PRIu64
" available, %"PRIu16
" needed\n",
3237 return NT_STATUS_INTERNAL_ERROR
;
3241 if (state
->conn
->smb2
.max_credits
> state
->conn
->smb2
.cur_credits
) {
3242 credits
= state
->conn
->smb2
.max_credits
-
3243 state
->conn
->smb2
.cur_credits
;
3245 if (state
->conn
->smb2
.max_credits
>= state
->conn
->smb2
.cur_credits
) {
3249 mid
= state
->conn
->smb2
.mid
;
3250 state
->conn
->smb2
.mid
+= charge
;
3251 state
->conn
->smb2
.cur_credits
-= charge
;
3253 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
3254 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT_CHARGE
, charge
);
3256 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT
, credits
);
3257 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
3259 state
->smb2
.cancel_flags
= 0;
3260 state
->smb2
.cancel_mid
= mid
;
3261 state
->smb2
.cancel_aid
= 0;
3264 if (state
->session
&& encryption_key
== NULL
) {
3266 * We prefer the channel signing key if it is
3269 if (state
->smb2
.should_sign
) {
3270 signing_key
= &state
->session
->smb2_channel
.signing_key
;
3274 * If it is a channel binding, we already have the main
3275 * signing key and try that one.
3277 if (signing_key
&& signing_key
->length
== 0) {
3278 signing_key
= &state
->session
->smb2
->signing_key
;
3282 * If we do not have any session key yet, we skip the
3283 * signing of SMB2_OP_SESSSETUP requests.
3285 if (signing_key
&& signing_key
->length
== 0) {
3291 iov
[num_iov
].iov_base
= state
->smb2
.hdr
;
3292 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.hdr
);
3295 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.fixed
);
3296 iov
[num_iov
].iov_len
= state
->smb2
.fixed_len
;
3299 if (state
->smb2
.dyn
!= NULL
) {
3300 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.dyn
);
3301 iov
[num_iov
].iov_len
= state
->smb2
.dyn_len
;
3305 reqlen
= sizeof(state
->smb2
.hdr
);
3306 reqlen
+= state
->smb2
.fixed_len
;
3307 reqlen
+= state
->smb2
.dyn_len
;
3309 if (i
< num_reqs
-1) {
3310 if ((reqlen
% 8) > 0) {
3311 uint8_t pad
= 8 - (reqlen
% 8);
3312 iov
[num_iov
].iov_base
= state
->smb2
.pad
;
3313 iov
[num_iov
].iov_len
= pad
;
3317 SIVAL(state
->smb2
.hdr
, SMB2_HDR_NEXT_COMMAND
, reqlen
);
3320 state
->smb2
.encryption_session_id
= encryption_session_id
;
3322 if (signing_key
!= NULL
) {
3325 status
= smb2_signing_sign_pdu(*signing_key
,
3326 state
->session
->conn
->protocol
,
3327 &iov
[hdr_iov
], num_iov
- hdr_iov
);
3328 if (!NT_STATUS_IS_OK(status
)) {
3335 ret
= smbXcli_req_set_pending(reqs
[i
]);
3337 return NT_STATUS_NO_MEMORY
;
3341 state
= tevent_req_data(reqs
[0], struct smbXcli_req_state
);
3342 _smb_setlen_tcp(state
->length_hdr
, nbt_len
);
3343 iov
[0].iov_base
= state
->length_hdr
;
3344 iov
[0].iov_len
= sizeof(state
->length_hdr
);
3346 if (encryption_key
!= NULL
) {
3348 size_t buflen
= nbt_len
- SMB2_TF_HDR_SIZE
;
3352 buf
= talloc_array(iov
, uint8_t, buflen
);
3354 return NT_STATUS_NO_MEMORY
;
3358 * We copy the buffers before encrypting them,
3359 * this is at least currently needed for the
3360 * to keep state->smb2.hdr.
3362 * Also the callers may expect there buffers
3365 for (vi
= tf_iov
+ 1; vi
< num_iov
; vi
++) {
3366 struct iovec
*v
= &iov
[vi
];
3367 const uint8_t *o
= (const uint8_t *)v
->iov_base
;
3369 memcpy(buf
, o
, v
->iov_len
);
3370 v
->iov_base
= (void *)buf
;
3374 status
= smb2_signing_encrypt_pdu(*encryption_key
,
3375 state
->conn
->smb2
.server
.cipher
,
3376 &iov
[tf_iov
], num_iov
- tf_iov
);
3377 if (!NT_STATUS_IS_OK(status
)) {
3382 if (state
->conn
->dispatch_incoming
== NULL
) {
3383 state
->conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
3386 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
3387 state
->conn
->sock_fd
, false, iov
, num_iov
);
3388 if (subreq
== NULL
) {
3389 return NT_STATUS_NO_MEMORY
;
3391 tevent_req_set_callback(subreq
, smb2cli_req_writev_done
, reqs
[0]);
3392 state
->write_req
= subreq
;
3394 return NT_STATUS_OK
;
3397 void smb2cli_req_set_credit_charge(struct tevent_req
*req
, uint16_t charge
)
3399 struct smbXcli_req_state
*state
=
3400 tevent_req_data(req
,
3401 struct smbXcli_req_state
);
3403 state
->smb2
.credit_charge
= charge
;
3406 struct tevent_req
*smb2cli_req_send(TALLOC_CTX
*mem_ctx
,
3407 struct tevent_context
*ev
,
3408 struct smbXcli_conn
*conn
,
3410 uint32_t additional_flags
,
3411 uint32_t clear_flags
,
3412 uint32_t timeout_msec
,
3413 struct smbXcli_tcon
*tcon
,
3414 struct smbXcli_session
*session
,
3415 const uint8_t *fixed
,
3419 uint32_t max_dyn_len
)
3421 struct tevent_req
*req
;
3424 req
= smb2cli_req_create(mem_ctx
, ev
, conn
, cmd
,
3425 additional_flags
, clear_flags
,
3434 if (!tevent_req_is_in_progress(req
)) {
3435 return tevent_req_post(req
, ev
);
3437 status
= smb2cli_req_compound_submit(&req
, 1);
3438 if (tevent_req_nterror(req
, status
)) {
3439 return tevent_req_post(req
, ev
);
3444 static void smb2cli_req_writev_done(struct tevent_req
*subreq
)
3446 struct tevent_req
*req
=
3447 tevent_req_callback_data(subreq
,
3449 struct smbXcli_req_state
*state
=
3450 tevent_req_data(req
,
3451 struct smbXcli_req_state
);
3455 state
->write_req
= NULL
;
3457 nwritten
= writev_recv(subreq
, &err
);
3458 TALLOC_FREE(subreq
);
3459 if (nwritten
== -1) {
3460 /* here, we need to notify all pending requests */
3461 NTSTATUS status
= map_nt_error_from_unix_common(err
);
3462 smbXcli_conn_disconnect(state
->conn
, status
);
3467 static struct smbXcli_session
* smbXcli_session_by_uid(struct smbXcli_conn
*conn
,
3470 struct smbXcli_session
*s
= conn
->sessions
;
3472 for (; s
; s
= s
->next
) {
3473 if (s
->smb2
->session_id
!= uid
) {
3482 static NTSTATUS
smb2cli_inbuf_parse_compound(struct smbXcli_conn
*conn
,
3485 TALLOC_CTX
*mem_ctx
,
3486 struct iovec
**piov
,
3492 uint8_t *first_hdr
= buf
;
3493 size_t verified_buflen
= 0;
3497 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
3499 return NT_STATUS_NO_MEMORY
;
3502 while (taken
< buflen
) {
3503 size_t len
= buflen
- taken
;
3504 uint8_t *hdr
= first_hdr
+ taken
;
3507 size_t next_command_ofs
;
3509 struct iovec
*iov_tmp
;
3511 if (verified_buflen
> taken
) {
3512 len
= verified_buflen
- taken
;
3519 DEBUG(10, ("%d bytes left, expected at least %d\n",
3523 if (IVAL(hdr
, 0) == SMB2_TF_MAGIC
) {
3524 struct smbXcli_session
*s
;
3526 struct iovec tf_iov
[2];
3530 if (len
< SMB2_TF_HDR_SIZE
) {
3531 DEBUG(10, ("%d bytes left, expected at least %d\n",
3532 (int)len
, SMB2_TF_HDR_SIZE
));
3536 tf_len
= SMB2_TF_HDR_SIZE
;
3539 hdr
= first_hdr
+ taken
;
3540 enc_len
= IVAL(tf
, SMB2_TF_MSG_SIZE
);
3541 uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
3543 if (len
< SMB2_TF_HDR_SIZE
+ enc_len
) {
3544 DEBUG(10, ("%d bytes left, expected at least %d\n",
3546 (int)(SMB2_TF_HDR_SIZE
+ enc_len
)));
3550 s
= smbXcli_session_by_uid(conn
, uid
);
3552 DEBUG(10, ("unknown session_id %llu\n",
3553 (unsigned long long)uid
));
3557 tf_iov
[0].iov_base
= (void *)tf
;
3558 tf_iov
[0].iov_len
= tf_len
;
3559 tf_iov
[1].iov_base
= (void *)hdr
;
3560 tf_iov
[1].iov_len
= enc_len
;
3562 status
= smb2_signing_decrypt_pdu(s
->smb2
->decryption_key
,
3563 conn
->smb2
.server
.cipher
,
3565 if (!NT_STATUS_IS_OK(status
)) {
3570 verified_buflen
= taken
+ enc_len
;
3575 * We need the header plus the body length field
3578 if (len
< SMB2_HDR_BODY
+ 2) {
3579 DEBUG(10, ("%d bytes left, expected at least %d\n",
3580 (int)len
, SMB2_HDR_BODY
));
3583 if (IVAL(hdr
, 0) != SMB2_MAGIC
) {
3584 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3588 if (SVAL(hdr
, 4) != SMB2_HDR_BODY
) {
3589 DEBUG(10, ("Got HDR len %d, expected %d\n",
3590 SVAL(hdr
, 4), SMB2_HDR_BODY
));
3595 next_command_ofs
= IVAL(hdr
, SMB2_HDR_NEXT_COMMAND
);
3596 body_size
= SVAL(hdr
, SMB2_HDR_BODY
);
3598 if (next_command_ofs
!= 0) {
3599 if (next_command_ofs
< (SMB2_HDR_BODY
+ 2)) {
3602 if (next_command_ofs
> full_size
) {
3605 full_size
= next_command_ofs
;
3607 if (body_size
< 2) {
3610 body_size
&= 0xfffe;
3612 if (body_size
> (full_size
- SMB2_HDR_BODY
)) {
3616 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
3618 if (iov_tmp
== NULL
) {
3620 return NT_STATUS_NO_MEMORY
;
3623 cur
= &iov
[num_iov
];
3626 cur
[0].iov_base
= tf
;
3627 cur
[0].iov_len
= tf_len
;
3628 cur
[1].iov_base
= hdr
;
3629 cur
[1].iov_len
= SMB2_HDR_BODY
;
3630 cur
[2].iov_base
= hdr
+ SMB2_HDR_BODY
;
3631 cur
[2].iov_len
= body_size
;
3632 cur
[3].iov_base
= hdr
+ SMB2_HDR_BODY
+ body_size
;
3633 cur
[3].iov_len
= full_size
- (SMB2_HDR_BODY
+ body_size
);
3639 *pnum_iov
= num_iov
;
3640 return NT_STATUS_OK
;
3644 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3647 static struct tevent_req
*smb2cli_conn_find_pending(struct smbXcli_conn
*conn
,
3650 size_t num_pending
= talloc_array_length(conn
->pending
);
3653 for (i
=0; i
<num_pending
; i
++) {
3654 struct tevent_req
*req
= conn
->pending
[i
];
3655 struct smbXcli_req_state
*state
=
3656 tevent_req_data(req
,
3657 struct smbXcli_req_state
);
3659 if (mid
== BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
)) {
3666 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
3667 TALLOC_CTX
*tmp_mem
,
3670 struct tevent_req
*req
;
3671 struct smbXcli_req_state
*state
= NULL
;
3672 struct iovec
*iov
= NULL
;
3673 size_t i
, num_iov
= 0;
3676 struct smbXcli_session
*last_session
= NULL
;
3677 size_t inbuf_len
= smb_len_tcp(inbuf
);
3679 status
= smb2cli_inbuf_parse_compound(conn
,
3680 inbuf
+ NBT_HDR_SIZE
,
3684 if (!NT_STATUS_IS_OK(status
)) {
3688 for (i
=0; i
<num_iov
; i
+=4) {
3689 uint8_t *inbuf_ref
= NULL
;
3690 struct iovec
*cur
= &iov
[i
];
3691 uint8_t *inhdr
= (uint8_t *)cur
[1].iov_base
;
3692 uint16_t opcode
= SVAL(inhdr
, SMB2_HDR_OPCODE
);
3693 uint32_t flags
= IVAL(inhdr
, SMB2_HDR_FLAGS
);
3694 uint64_t mid
= BVAL(inhdr
, SMB2_HDR_MESSAGE_ID
);
3695 uint16_t req_opcode
;
3697 uint16_t credits
= SVAL(inhdr
, SMB2_HDR_CREDIT
);
3698 uint32_t new_credits
;
3699 struct smbXcli_session
*session
= NULL
;
3700 const DATA_BLOB
*signing_key
= NULL
;
3701 bool was_encrypted
= false;
3703 new_credits
= conn
->smb2
.cur_credits
;
3704 new_credits
+= credits
;
3705 if (new_credits
> UINT16_MAX
) {
3706 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3708 conn
->smb2
.cur_credits
+= credits
;
3710 req
= smb2cli_conn_find_pending(conn
, mid
);
3712 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3714 state
= tevent_req_data(req
, struct smbXcli_req_state
);
3716 req_opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
3717 if (opcode
!= req_opcode
) {
3718 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3720 req_flags
= SVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
3722 if (!(flags
& SMB2_HDR_FLAG_REDIRECT
)) {
3723 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3726 status
= NT_STATUS(IVAL(inhdr
, SMB2_HDR_STATUS
));
3727 if ((flags
& SMB2_HDR_FLAG_ASYNC
) &&
3728 NT_STATUS_EQUAL(status
, STATUS_PENDING
)) {
3729 uint64_t async_id
= BVAL(inhdr
, SMB2_HDR_ASYNC_ID
);
3731 if (state
->smb2
.got_async
) {
3732 /* We only expect one STATUS_PENDING response */
3733 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3735 state
->smb2
.got_async
= true;
3738 * async interim responses are not signed,
3739 * even if the SMB2_HDR_FLAG_SIGNED flag
3742 state
->smb2
.cancel_flags
= SMB2_HDR_FLAG_ASYNC
;
3743 state
->smb2
.cancel_mid
= 0;
3744 state
->smb2
.cancel_aid
= async_id
;
3746 if (state
->smb2
.notify_async
) {
3747 tevent_req_defer_callback(req
, state
->ev
);
3748 tevent_req_notify_callback(req
);
3753 session
= state
->session
;
3754 if (req_flags
& SMB2_HDR_FLAG_CHAINED
) {
3755 session
= last_session
;
3757 last_session
= session
;
3759 if (flags
& SMB2_HDR_FLAG_SIGNED
) {
3760 uint64_t uid
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
3762 if (session
== NULL
) {
3763 session
= smbXcli_session_by_uid(state
->conn
,
3767 if (session
== NULL
) {
3768 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3771 last_session
= session
;
3772 signing_key
= &session
->smb2_channel
.signing_key
;
3775 if (opcode
== SMB2_OP_SESSSETUP
) {
3777 * We prefer the channel signing key, if it is
3780 * If we do not have a channel signing key yet,
3781 * we try the main signing key, if it is not
3782 * the final response.
3784 if (signing_key
&& signing_key
->length
== 0 &&
3785 !NT_STATUS_IS_OK(status
)) {
3786 signing_key
= &session
->smb2
->signing_key
;
3789 if (signing_key
&& signing_key
->length
== 0) {
3791 * If we do not have a session key to
3792 * verify the signature, we defer the
3793 * signing check to the caller.
3795 * The caller gets NT_STATUS_OK, it
3797 * smb2cli_session_set_session_key()
3799 * smb2cli_session_set_channel_key()
3800 * which will check the signature
3801 * with the channel signing key.
3806 if (!NT_STATUS_IS_OK(status
)) {
3808 * Only check the signature of the last response
3809 * of a successfull session auth. This matches
3810 * Windows behaviour for NTLM auth and reauth.
3812 state
->smb2
.require_signed_response
= false;
3816 if (state
->smb2
.should_sign
||
3817 state
->smb2
.require_signed_response
)
3819 if (!(flags
& SMB2_HDR_FLAG_SIGNED
)) {
3820 return NT_STATUS_ACCESS_DENIED
;
3824 if (signing_key
== NULL
&& state
->smb2
.require_signed_response
) {
3825 signing_key
= &session
->smb2_channel
.signing_key
;
3828 if (cur
[0].iov_len
== SMB2_TF_HDR_SIZE
) {
3829 const uint8_t *tf
= (const uint8_t *)cur
[0].iov_base
;
3830 uint64_t uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
3833 * If the response was encrypted in a SMB2_TRANSFORM
3834 * pdu, which belongs to the correct session,
3835 * we do not need to do signing checks
3837 * It could be the session the response belongs to
3838 * or the session that was used to encrypt the
3839 * SMB2_TRANSFORM request.
3841 if ((session
&& session
->smb2
->session_id
== uid
) ||
3842 (state
->smb2
.encryption_session_id
== uid
)) {
3844 was_encrypted
= true;
3848 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_SESSION_DELETED
)) {
3850 * if the server returns NT_STATUS_USER_SESSION_DELETED
3851 * the response is not signed and we should
3852 * propagate the NT_STATUS_USER_SESSION_DELETED
3853 * status to the caller.
3855 state
->smb2
.signing_skipped
= true;
3859 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
3861 * if the server returns
3862 * NT_STATUS_INVALID_PARAMETER
3863 * the response might not be encrypted.
3865 if (state
->smb2
.should_encrypt
&& !was_encrypted
) {
3866 state
->smb2
.signing_skipped
= true;
3871 if (state
->smb2
.should_encrypt
&& !was_encrypted
) {
3872 if (!state
->smb2
.signing_skipped
) {
3873 return NT_STATUS_ACCESS_DENIED
;
3877 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_NAME_DELETED
) ||
3878 NT_STATUS_EQUAL(status
, NT_STATUS_FILE_CLOSED
) ||
3879 NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
3881 * if the server returns
3882 * NT_STATUS_NETWORK_NAME_DELETED
3883 * NT_STATUS_FILE_CLOSED
3884 * NT_STATUS_INVALID_PARAMETER
3885 * the response might not be signed
3886 * as this happens before the signing checks.
3888 * If server echos the signature (or all zeros)
3889 * we should report the status from the server
3895 cmp
= memcmp(inhdr
+SMB2_HDR_SIGNATURE
,
3896 state
->smb2
.hdr
+SMB2_HDR_SIGNATURE
,
3899 state
->smb2
.signing_skipped
= true;
3905 zero
= all_zero(inhdr
+SMB2_HDR_SIGNATURE
, 16);
3907 state
->smb2
.signing_skipped
= true;
3914 NTSTATUS signing_status
;
3916 signing_status
= smb2_signing_check_pdu(*signing_key
,
3917 state
->conn
->protocol
,
3919 if (!NT_STATUS_IS_OK(signing_status
)) {
3921 * If the signing check fails, we disconnect
3924 return signing_status
;
3928 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
) &&
3929 (session
!= NULL
) && session
->disconnect_expired
)
3932 * this should be a short term hack
3933 * until the upper layers have implemented
3934 * re-authentication.
3939 smbXcli_req_unset_pending(req
);
3942 * There might be more than one response
3943 * we need to defer the notifications
3945 if ((num_iov
== 5) && (talloc_array_length(conn
->pending
) == 0)) {
3950 tevent_req_defer_callback(req
, state
->ev
);
3954 * Note: here we use talloc_reference() in a way
3955 * that does not expose it to the caller.
3957 inbuf_ref
= talloc_reference(state
->smb2
.recv_iov
, inbuf
);
3958 if (tevent_req_nomem(inbuf_ref
, req
)) {
3962 /* copy the related buffers */
3963 state
->smb2
.recv_iov
[0] = cur
[1];
3964 state
->smb2
.recv_iov
[1] = cur
[2];
3965 state
->smb2
.recv_iov
[2] = cur
[3];
3967 tevent_req_done(req
);
3971 return NT_STATUS_RETRY
;
3974 return NT_STATUS_OK
;
3977 NTSTATUS
smb2cli_req_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
3978 struct iovec
**piov
,
3979 const struct smb2cli_req_expected_response
*expected
,
3980 size_t num_expected
)
3982 struct smbXcli_req_state
*state
=
3983 tevent_req_data(req
,
3984 struct smbXcli_req_state
);
3987 bool found_status
= false;
3988 bool found_size
= false;
3995 if (tevent_req_is_in_progress(req
) && state
->smb2
.got_async
) {
3996 return STATUS_PENDING
;
3999 if (tevent_req_is_nterror(req
, &status
)) {
4000 for (i
=0; i
< num_expected
; i
++) {
4001 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
4002 found_status
= true;
4008 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
4014 if (num_expected
== 0) {
4015 found_status
= true;
4019 status
= NT_STATUS(IVAL(state
->smb2
.recv_iov
[0].iov_base
, SMB2_HDR_STATUS
));
4020 body_size
= SVAL(state
->smb2
.recv_iov
[1].iov_base
, 0);
4022 for (i
=0; i
< num_expected
; i
++) {
4023 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
4027 found_status
= true;
4028 if (expected
[i
].body_size
== 0) {
4033 if (expected
[i
].body_size
== body_size
) {
4039 if (!found_status
) {
4043 if (state
->smb2
.signing_skipped
) {
4044 if (num_expected
> 0) {
4045 return NT_STATUS_ACCESS_DENIED
;
4047 if (!NT_STATUS_IS_ERR(status
)) {
4048 return NT_STATUS_ACCESS_DENIED
;
4053 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4057 *piov
= talloc_move(mem_ctx
, &state
->smb2
.recv_iov
);
4063 NTSTATUS
smb2cli_req_get_sent_iov(struct tevent_req
*req
,
4064 struct iovec
*sent_iov
)
4066 struct smbXcli_req_state
*state
=
4067 tevent_req_data(req
,
4068 struct smbXcli_req_state
);
4070 if (tevent_req_is_in_progress(req
)) {
4071 return STATUS_PENDING
;
4074 sent_iov
[0].iov_base
= state
->smb2
.hdr
;
4075 sent_iov
[0].iov_len
= sizeof(state
->smb2
.hdr
);
4077 sent_iov
[1].iov_base
= discard_const(state
->smb2
.fixed
);
4078 sent_iov
[1].iov_len
= state
->smb2
.fixed_len
;
4080 if (state
->smb2
.dyn
!= NULL
) {
4081 sent_iov
[2].iov_base
= discard_const(state
->smb2
.dyn
);
4082 sent_iov
[2].iov_len
= state
->smb2
.dyn_len
;
4084 sent_iov
[2].iov_base
= NULL
;
4085 sent_iov
[2].iov_len
= 0;
4088 return NT_STATUS_OK
;
4091 static const struct {
4092 enum protocol_types proto
;
4093 const char *smb1_name
;
4094 } smb1cli_prots
[] = {
4095 {PROTOCOL_CORE
, "PC NETWORK PROGRAM 1.0"},
4096 {PROTOCOL_COREPLUS
, "MICROSOFT NETWORKS 1.03"},
4097 {PROTOCOL_LANMAN1
, "MICROSOFT NETWORKS 3.0"},
4098 {PROTOCOL_LANMAN1
, "LANMAN1.0"},
4099 {PROTOCOL_LANMAN2
, "LM1.2X002"},
4100 {PROTOCOL_LANMAN2
, "DOS LANMAN2.1"},
4101 {PROTOCOL_LANMAN2
, "LANMAN2.1"},
4102 {PROTOCOL_LANMAN2
, "Samba"},
4103 {PROTOCOL_NT1
, "NT LANMAN 1.0"},
4104 {PROTOCOL_NT1
, "NT LM 0.12"},
4105 {PROTOCOL_SMB2_02
, "SMB 2.002"},
4106 {PROTOCOL_SMB2_10
, "SMB 2.???"},
4109 static const struct {
4110 enum protocol_types proto
;
4111 uint16_t smb2_dialect
;
4112 } smb2cli_prots
[] = {
4113 {PROTOCOL_SMB2_02
, SMB2_DIALECT_REVISION_202
},
4114 {PROTOCOL_SMB2_10
, SMB2_DIALECT_REVISION_210
},
4115 {PROTOCOL_SMB2_22
, SMB2_DIALECT_REVISION_222
},
4116 {PROTOCOL_SMB2_24
, SMB2_DIALECT_REVISION_224
},
4117 {PROTOCOL_SMB3_00
, SMB3_DIALECT_REVISION_300
},
4118 {PROTOCOL_SMB3_02
, SMB3_DIALECT_REVISION_302
},
4119 {PROTOCOL_SMB3_10
, SMB3_DIALECT_REVISION_310
},
4120 {PROTOCOL_SMB3_11
, SMB3_DIALECT_REVISION_311
},
4123 struct smbXcli_negprot_state
{
4124 struct smbXcli_conn
*conn
;
4125 struct tevent_context
*ev
;
4126 uint32_t timeout_msec
;
4133 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
);
4134 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
);
4135 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
);
4136 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
);
4137 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
);
4138 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
4142 struct tevent_req
*smbXcli_negprot_send(TALLOC_CTX
*mem_ctx
,
4143 struct tevent_context
*ev
,
4144 struct smbXcli_conn
*conn
,
4145 uint32_t timeout_msec
,
4146 enum protocol_types min_protocol
,
4147 enum protocol_types max_protocol
,
4148 uint16_t max_credits
)
4150 struct tevent_req
*req
, *subreq
;
4151 struct smbXcli_negprot_state
*state
;
4153 req
= tevent_req_create(mem_ctx
, &state
,
4154 struct smbXcli_negprot_state
);
4160 state
->timeout_msec
= timeout_msec
;
4162 if (min_protocol
== PROTOCOL_NONE
) {
4163 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
4164 return tevent_req_post(req
, ev
);
4167 if (max_protocol
== PROTOCOL_NONE
) {
4168 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
4169 return tevent_req_post(req
, ev
);
4172 if (min_protocol
> max_protocol
) {
4173 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
4174 return tevent_req_post(req
, ev
);
4177 conn
->min_protocol
= min_protocol
;
4178 conn
->max_protocol
= max_protocol
;
4179 conn
->protocol
= PROTOCOL_NONE
;
4181 if (max_protocol
>= PROTOCOL_SMB2_02
) {
4182 conn
->smb2
.max_credits
= max_credits
;
4185 if ((min_protocol
< PROTOCOL_SMB2_02
) &&
4186 (max_protocol
< PROTOCOL_SMB2_02
)) {
4190 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
4192 subreq
= smbXcli_negprot_smb1_subreq(state
);
4193 if (tevent_req_nomem(subreq
, req
)) {
4194 return tevent_req_post(req
, ev
);
4196 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
4200 if ((min_protocol
>= PROTOCOL_SMB2_02
) &&
4201 (max_protocol
>= PROTOCOL_SMB2_02
)) {
4205 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
4207 subreq
= smbXcli_negprot_smb2_subreq(state
);
4208 if (tevent_req_nomem(subreq
, req
)) {
4209 return tevent_req_post(req
, ev
);
4211 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4216 * We send an SMB1 negprot with the SMB2 dialects
4217 * and expect a SMB1 or a SMB2 response.
4219 * smbXcli_negprot_dispatch_incoming() will fix the
4220 * callback to match protocol of the response.
4222 conn
->dispatch_incoming
= smbXcli_negprot_dispatch_incoming
;
4224 subreq
= smbXcli_negprot_smb1_subreq(state
);
4225 if (tevent_req_nomem(subreq
, req
)) {
4226 return tevent_req_post(req
, ev
);
4228 tevent_req_set_callback(subreq
, smbXcli_negprot_invalid_done
, req
);
4232 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
)
4234 struct tevent_req
*req
=
4235 tevent_req_callback_data(subreq
,
4240 * we just want the low level error
4242 status
= tevent_req_simple_recv_ntstatus(subreq
);
4243 TALLOC_FREE(subreq
);
4244 if (tevent_req_nterror(req
, status
)) {
4248 /* this should never happen */
4249 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
4252 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
)
4255 DATA_BLOB bytes
= data_blob_null
;
4259 /* setup the protocol strings */
4260 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
4264 if (smb1cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
4268 if (smb1cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
4272 ok
= data_blob_append(state
, &bytes
, &c
, sizeof(c
));
4278 * We now it is already ascii and
4279 * we want NULL termination.
4281 ok
= data_blob_append(state
, &bytes
,
4282 smb1cli_prots
[i
].smb1_name
,
4283 strlen(smb1cli_prots
[i
].smb1_name
)+1);
4289 smb1cli_req_flags(state
->conn
->max_protocol
,
4290 state
->conn
->smb1
.client
.capabilities
,
4295 return smb1cli_req_send(state
, state
->ev
, state
->conn
,
4299 state
->timeout_msec
,
4300 0xFFFE, 0, NULL
, /* pid, tid, session */
4301 0, NULL
, /* wct, vwv */
4302 bytes
.length
, bytes
.data
);
4305 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
)
4307 struct tevent_req
*req
=
4308 tevent_req_callback_data(subreq
,
4310 struct smbXcli_negprot_state
*state
=
4311 tevent_req_data(req
,
4312 struct smbXcli_negprot_state
);
4313 struct smbXcli_conn
*conn
= state
->conn
;
4314 struct iovec
*recv_iov
= NULL
;
4323 size_t num_prots
= 0;
4325 uint32_t client_capabilities
= conn
->smb1
.client
.capabilities
;
4326 uint32_t both_capabilities
;
4327 uint32_t server_capabilities
= 0;
4328 uint32_t capabilities
;
4329 uint32_t client_max_xmit
= conn
->smb1
.client
.max_xmit
;
4330 uint32_t server_max_xmit
= 0;
4332 uint32_t server_max_mux
= 0;
4333 uint16_t server_security_mode
= 0;
4334 uint32_t server_session_key
= 0;
4335 bool server_readbraw
= false;
4336 bool server_writebraw
= false;
4337 bool server_lockread
= false;
4338 bool server_writeunlock
= false;
4339 struct GUID server_guid
= GUID_zero();
4340 DATA_BLOB server_gss_blob
= data_blob_null
;
4341 uint8_t server_challenge
[8];
4342 char *server_workgroup
= NULL
;
4343 char *server_name
= NULL
;
4344 int server_time_zone
= 0;
4345 NTTIME server_system_time
= 0;
4346 static const struct smb1cli_req_expected_response expected
[] = {
4348 .status
= NT_STATUS_OK
,
4349 .wct
= 0x11, /* NT1 */
4352 .status
= NT_STATUS_OK
,
4353 .wct
= 0x0D, /* LM */
4356 .status
= NT_STATUS_OK
,
4357 .wct
= 0x01, /* CORE */
4361 ZERO_STRUCT(server_challenge
);
4363 status
= smb1cli_req_recv(subreq
, state
,
4368 NULL
, /* pvwv_offset */
4371 NULL
, /* pbytes_offset */
4373 expected
, ARRAY_SIZE(expected
));
4374 TALLOC_FREE(subreq
);
4375 if (tevent_req_nterror(req
, status
)) {
4379 flags
= CVAL(inhdr
, HDR_FLG
);
4381 protnum
= SVAL(vwv
, 0);
4383 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
4384 if (smb1cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
4388 if (smb1cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
4392 if (protnum
!= num_prots
) {
4397 conn
->protocol
= smb1cli_prots
[i
].proto
;
4401 if (conn
->protocol
== PROTOCOL_NONE
) {
4402 DBG_ERR("No compatible protocol selected by server.\n");
4403 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4407 if ((conn
->protocol
< PROTOCOL_NT1
) && conn
->mandatory_signing
) {
4408 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4409 "and the selected protocol level doesn't support it.\n"));
4410 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
4414 if (flags
& FLAG_SUPPORT_LOCKREAD
) {
4415 server_lockread
= true;
4416 server_writeunlock
= true;
4419 if (conn
->protocol
>= PROTOCOL_NT1
) {
4420 const char *client_signing
= NULL
;
4421 bool server_mandatory
= false;
4422 bool server_allowed
= false;
4423 const char *server_signing
= NULL
;
4428 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4433 server_security_mode
= CVAL(vwv
+ 1, 0);
4434 server_max_mux
= SVAL(vwv
+ 1, 1);
4435 server_max_xmit
= IVAL(vwv
+ 3, 1);
4436 server_session_key
= IVAL(vwv
+ 7, 1);
4437 server_time_zone
= SVALS(vwv
+ 15, 1);
4438 server_time_zone
*= 60;
4439 /* this time arrives in real GMT */
4440 server_system_time
= BVAL(vwv
+ 11, 1);
4441 server_capabilities
= IVAL(vwv
+ 9, 1);
4443 key_len
= CVAL(vwv
+ 16, 1);
4445 if (server_capabilities
& CAP_RAW_MODE
) {
4446 server_readbraw
= true;
4447 server_writebraw
= true;
4449 if (server_capabilities
& CAP_LOCK_AND_READ
) {
4450 server_lockread
= true;
4453 if (server_capabilities
& CAP_EXTENDED_SECURITY
) {
4454 DATA_BLOB blob1
, blob2
;
4456 if (num_bytes
< 16) {
4457 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4461 blob1
= data_blob_const(bytes
, 16);
4462 status
= GUID_from_data_blob(&blob1
, &server_guid
);
4463 if (tevent_req_nterror(req
, status
)) {
4467 blob1
= data_blob_const(bytes
+16, num_bytes
-16);
4468 blob2
= data_blob_dup_talloc(state
, blob1
);
4469 if (blob1
.length
> 0 &&
4470 tevent_req_nomem(blob2
.data
, req
)) {
4473 server_gss_blob
= blob2
;
4475 DATA_BLOB blob1
, blob2
;
4477 if (num_bytes
< key_len
) {
4478 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4482 if (key_len
!= 0 && key_len
!= 8) {
4483 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4488 memcpy(server_challenge
, bytes
, 8);
4491 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4492 blob2
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4493 if (blob1
.length
> 0) {
4496 len
= utf16_len_n(blob1
.data
,
4500 ok
= convert_string_talloc(state
,
4508 status
= map_nt_error_from_unix_common(errno
);
4509 tevent_req_nterror(req
, status
);
4514 blob2
.data
+= blob1
.length
;
4515 blob2
.length
-= blob1
.length
;
4516 if (blob2
.length
> 0) {
4519 len
= utf16_len_n(blob1
.data
,
4523 ok
= convert_string_talloc(state
,
4531 status
= map_nt_error_from_unix_common(errno
);
4532 tevent_req_nterror(req
, status
);
4538 client_signing
= "disabled";
4539 if (conn
->allow_signing
) {
4540 client_signing
= "allowed";
4542 if (conn
->mandatory_signing
) {
4543 client_signing
= "required";
4546 server_signing
= "not supported";
4547 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_ENABLED
) {
4548 server_signing
= "supported";
4549 server_allowed
= true;
4550 } else if (conn
->mandatory_signing
) {
4552 * We have mandatory signing as client
4553 * lets assume the server will look at our
4554 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4555 * flag in the session setup
4557 server_signing
= "not announced";
4558 server_allowed
= true;
4560 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_REQUIRED
) {
4561 server_signing
= "required";
4562 server_mandatory
= true;
4565 ok
= smb_signing_set_negotiated(conn
->smb1
.signing
,
4569 DEBUG(1,("cli_negprot: SMB signing is required, "
4570 "but client[%s] and server[%s] mismatch\n",
4571 client_signing
, server_signing
));
4572 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
4576 } else if (conn
->protocol
>= PROTOCOL_LANMAN1
) {
4582 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4586 server_security_mode
= SVAL(vwv
+ 1, 0);
4587 server_max_xmit
= SVAL(vwv
+ 2, 0);
4588 server_max_mux
= SVAL(vwv
+ 3, 0);
4589 server_readbraw
= ((SVAL(vwv
+ 5, 0) & 0x1) != 0);
4590 server_writebraw
= ((SVAL(vwv
+ 5, 0) & 0x2) != 0);
4591 server_session_key
= IVAL(vwv
+ 6, 0);
4592 server_time_zone
= SVALS(vwv
+ 10, 0);
4593 server_time_zone
*= 60;
4594 /* this time is converted to GMT by make_unix_date */
4595 t
= pull_dos_date((const uint8_t *)(vwv
+ 8), server_time_zone
);
4596 unix_to_nt_time(&server_system_time
, t
);
4597 key_len
= SVAL(vwv
+ 11, 0);
4599 if (num_bytes
< key_len
) {
4600 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4604 if (key_len
!= 0 && key_len
!= 8) {
4605 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4610 memcpy(server_challenge
, bytes
, 8);
4613 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4614 if (blob1
.length
> 0) {
4618 len
= utf16_len_n(blob1
.data
,
4622 ok
= convert_string_talloc(state
,
4630 status
= map_nt_error_from_unix_common(errno
);
4631 tevent_req_nterror(req
, status
);
4637 /* the old core protocol */
4638 server_time_zone
= get_time_zone(time(NULL
));
4639 server_max_xmit
= 1024;
4643 if (server_max_xmit
< 1024) {
4644 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4648 if (server_max_mux
< 1) {
4649 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4654 * Now calculate the negotiated capabilities
4655 * based on the mask for:
4656 * - client only flags
4657 * - flags used in both directions
4658 * - server only flags
4660 both_capabilities
= client_capabilities
& server_capabilities
;
4661 capabilities
= client_capabilities
& SMB_CAP_CLIENT_MASK
;
4662 capabilities
|= both_capabilities
& SMB_CAP_BOTH_MASK
;
4663 capabilities
|= server_capabilities
& SMB_CAP_SERVER_MASK
;
4665 max_xmit
= MIN(client_max_xmit
, server_max_xmit
);
4667 conn
->smb1
.server
.capabilities
= server_capabilities
;
4668 conn
->smb1
.capabilities
= capabilities
;
4670 conn
->smb1
.server
.max_xmit
= server_max_xmit
;
4671 conn
->smb1
.max_xmit
= max_xmit
;
4673 conn
->smb1
.server
.max_mux
= server_max_mux
;
4675 conn
->smb1
.server
.security_mode
= server_security_mode
;
4677 conn
->smb1
.server
.readbraw
= server_readbraw
;
4678 conn
->smb1
.server
.writebraw
= server_writebraw
;
4679 conn
->smb1
.server
.lockread
= server_lockread
;
4680 conn
->smb1
.server
.writeunlock
= server_writeunlock
;
4682 conn
->smb1
.server
.session_key
= server_session_key
;
4684 talloc_steal(conn
, server_gss_blob
.data
);
4685 conn
->smb1
.server
.gss_blob
= server_gss_blob
;
4686 conn
->smb1
.server
.guid
= server_guid
;
4687 memcpy(conn
->smb1
.server
.challenge
, server_challenge
, 8);
4688 conn
->smb1
.server
.workgroup
= talloc_move(conn
, &server_workgroup
);
4689 conn
->smb1
.server
.name
= talloc_move(conn
, &server_name
);
4691 conn
->smb1
.server
.time_zone
= server_time_zone
;
4692 conn
->smb1
.server
.system_time
= server_system_time
;
4694 tevent_req_done(req
);
4697 static size_t smbXcli_padding_helper(uint32_t offset
, size_t n
)
4699 if ((offset
& (n
-1)) == 0) return 0;
4700 return n
- (offset
& (n
-1));
4703 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
)
4707 uint16_t dialect_count
= 0;
4708 DATA_BLOB dyn
= data_blob_null
;
4710 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4714 if (smb2cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
4718 if (smb2cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
4722 SSVAL(val
, 0, smb2cli_prots
[i
].smb2_dialect
);
4724 ok
= data_blob_append(state
, &dyn
, val
, sizeof(val
));
4732 buf
= state
->smb2
.fixed
;
4734 SSVAL(buf
, 2, dialect_count
);
4735 SSVAL(buf
, 4, state
->conn
->smb2
.client
.security_mode
);
4736 SSVAL(buf
, 6, 0); /* Reserved */
4737 if (state
->conn
->max_protocol
>= PROTOCOL_SMB2_22
) {
4738 SIVAL(buf
, 8, state
->conn
->smb2
.client
.capabilities
);
4740 SIVAL(buf
, 8, 0); /* Capabilities */
4742 if (state
->conn
->max_protocol
>= PROTOCOL_SMB2_10
) {
4746 status
= GUID_to_ndr_blob(&state
->conn
->smb2
.client
.guid
,
4748 if (!NT_STATUS_IS_OK(status
)) {
4751 memcpy(buf
+12, blob
.data
, 16); /* ClientGuid */
4753 memset(buf
+12, 0, 16); /* ClientGuid */
4756 if (state
->conn
->max_protocol
>= PROTOCOL_SMB3_10
) {
4758 struct smb2_negotiate_contexts c
= { .num_contexts
= 0, };
4762 const uint8_t zeros
[8] = {0, };
4766 SSVAL(p
, 0, 1); /* HashAlgorithmCount */
4767 SSVAL(p
, 2, 32); /* SaltLength */
4768 SSVAL(p
, 4, SMB2_PREAUTH_INTEGRITY_SHA512
);
4769 generate_random_buffer(p
+ 6, 32);
4771 b
= data_blob_const(p
, 38);
4772 status
= smb2_negotiate_context_add(state
, &c
,
4773 SMB2_PREAUTH_INTEGRITY_CAPABILITIES
, b
);
4774 if (!NT_STATUS_IS_OK(status
)) {
4778 SSVAL(p
, 0, 2); /* ChiperCount */
4780 * For now we preferr CCM because our implementation
4781 * is faster than GCM, see bug #11451.
4783 SSVAL(p
, 2, SMB2_ENCRYPTION_AES128_CCM
);
4784 SSVAL(p
, 4, SMB2_ENCRYPTION_AES128_GCM
);
4786 b
= data_blob_const(p
, 6);
4787 status
= smb2_negotiate_context_add(state
, &c
,
4788 SMB2_ENCRYPTION_CAPABILITIES
, b
);
4789 if (!NT_STATUS_IS_OK(status
)) {
4793 status
= smb2_negotiate_context_push(state
, &b
, c
);
4794 if (!NT_STATUS_IS_OK(status
)) {
4798 offset
= SMB2_HDR_BODY
+ sizeof(state
->smb2
.fixed
) + dyn
.length
;
4799 pad
= smbXcli_padding_helper(offset
, 8);
4801 ok
= data_blob_append(state
, &dyn
, zeros
, pad
);
4807 ok
= data_blob_append(state
, &dyn
, b
.data
, b
.length
);
4812 SIVAL(buf
, 28, offset
); /* NegotiateContextOffset */
4813 SSVAL(buf
, 32, c
.num_contexts
); /* NegotiateContextCount */
4814 SSVAL(buf
, 34, 0); /* Reserved */
4816 SBVAL(buf
, 28, 0); /* Reserved/ClientStartTime */
4819 return smb2cli_req_send(state
, state
->ev
,
4820 state
->conn
, SMB2_OP_NEGPROT
,
4822 state
->timeout_msec
,
4823 NULL
, NULL
, /* tcon, session */
4824 state
->smb2
.fixed
, sizeof(state
->smb2
.fixed
),
4825 dyn
.data
, dyn
.length
,
4826 UINT16_MAX
); /* max_dyn_len */
4829 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
)
4831 struct tevent_req
*req
=
4832 tevent_req_callback_data(subreq
,
4834 struct smbXcli_negprot_state
*state
=
4835 tevent_req_data(req
,
4836 struct smbXcli_negprot_state
);
4837 struct smbXcli_conn
*conn
= state
->conn
;
4838 size_t security_offset
, security_length
;
4844 uint16_t dialect_revision
;
4845 struct smb2_negotiate_contexts c
= { .num_contexts
= 0, };
4846 uint32_t negotiate_context_offset
= 0;
4847 uint16_t negotiate_context_count
= 0;
4848 DATA_BLOB negotiate_context_blob
= data_blob_null
;
4852 struct smb2_negotiate_context
*preauth
= NULL
;
4853 uint16_t hash_count
;
4854 uint16_t salt_length
;
4855 uint16_t hash_selected
;
4856 struct hc_sha512state sctx
;
4857 struct smb2_negotiate_context
*cipher
= NULL
;
4858 struct iovec sent_iov
[3];
4859 static const struct smb2cli_req_expected_response expected
[] = {
4861 .status
= NT_STATUS_OK
,
4866 status
= smb2cli_req_recv(subreq
, state
, &iov
,
4867 expected
, ARRAY_SIZE(expected
));
4868 if (tevent_req_nterror(req
, status
)) {
4872 body
= (uint8_t *)iov
[1].iov_base
;
4874 dialect_revision
= SVAL(body
, 4);
4876 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4877 if (smb2cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
4881 if (smb2cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
4885 if (smb2cli_prots
[i
].smb2_dialect
!= dialect_revision
) {
4889 conn
->protocol
= smb2cli_prots
[i
].proto
;
4893 if (conn
->protocol
== PROTOCOL_NONE
) {
4894 TALLOC_FREE(subreq
);
4896 if (state
->conn
->min_protocol
>= PROTOCOL_SMB2_02
) {
4897 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4901 if (dialect_revision
!= SMB2_DIALECT_REVISION_2FF
) {
4902 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4906 /* make sure we do not loop forever */
4907 state
->conn
->min_protocol
= PROTOCOL_SMB2_02
;
4910 * send a SMB2 negprot, in order to negotiate
4913 subreq
= smbXcli_negprot_smb2_subreq(state
);
4914 if (tevent_req_nomem(subreq
, req
)) {
4917 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4921 conn
->smb2
.server
.security_mode
= SVAL(body
, 2);
4922 if (conn
->protocol
>= PROTOCOL_SMB3_10
) {
4923 negotiate_context_count
= SVAL(body
, 6);
4926 blob
= data_blob_const(body
+ 8, 16);
4927 status
= GUID_from_data_blob(&blob
, &conn
->smb2
.server
.guid
);
4928 if (tevent_req_nterror(req
, status
)) {
4932 conn
->smb2
.server
.capabilities
= IVAL(body
, 24);
4933 conn
->smb2
.server
.max_trans_size
= IVAL(body
, 28);
4934 conn
->smb2
.server
.max_read_size
= IVAL(body
, 32);
4935 conn
->smb2
.server
.max_write_size
= IVAL(body
, 36);
4936 conn
->smb2
.server
.system_time
= BVAL(body
, 40);
4937 conn
->smb2
.server
.start_time
= BVAL(body
, 48);
4939 security_offset
= SVAL(body
, 56);
4940 security_length
= SVAL(body
, 58);
4942 if (security_offset
!= SMB2_HDR_BODY
+ iov
[1].iov_len
) {
4943 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4947 if (security_length
> iov
[2].iov_len
) {
4948 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4952 conn
->smb2
.server
.gss_blob
= data_blob_talloc(conn
,
4955 if (tevent_req_nomem(conn
->smb2
.server
.gss_blob
.data
, req
)) {
4959 if (conn
->protocol
< PROTOCOL_SMB3_10
) {
4960 TALLOC_FREE(subreq
);
4962 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
) {
4963 conn
->smb2
.server
.cipher
= SMB2_ENCRYPTION_AES128_CCM
;
4965 tevent_req_done(req
);
4970 * Here we are now at SMB3_11, so encryption should be
4971 * negotiated via context, not capabilities.
4974 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
) {
4976 * Server set SMB2_CAP_ENCRYPTION capability,
4977 * but *SHOULD* not, not *MUST* not. Just mask it off.
4978 * NetApp seems to do this:
4979 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13009
4981 conn
->smb2
.server
.capabilities
&= ~SMB2_CAP_ENCRYPTION
;
4984 negotiate_context_offset
= IVAL(body
, 60);
4985 if (negotiate_context_offset
< security_offset
) {
4986 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4990 ctx_ofs
= negotiate_context_offset
- security_offset
;
4991 if (ctx_ofs
> iov
[2].iov_len
) {
4992 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4995 avail
= iov
[2].iov_len
- security_length
;
4996 needed
= iov
[2].iov_len
- ctx_ofs
;
4997 if (needed
> avail
) {
4998 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5002 negotiate_context_blob
.data
= (uint8_t *)iov
[2].iov_base
;
5003 negotiate_context_blob
.length
= iov
[2].iov_len
;
5005 negotiate_context_blob
.data
+= ctx_ofs
;
5006 negotiate_context_blob
.length
-= ctx_ofs
;
5008 status
= smb2_negotiate_context_parse(state
, negotiate_context_blob
, &c
);
5009 if (tevent_req_nterror(req
, status
)) {
5013 if (negotiate_context_count
!= c
.num_contexts
) {
5014 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5018 preauth
= smb2_negotiate_context_find(&c
,
5019 SMB2_PREAUTH_INTEGRITY_CAPABILITIES
);
5020 if (preauth
== NULL
) {
5021 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5025 if (preauth
->data
.length
< 6) {
5026 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5030 hash_count
= SVAL(preauth
->data
.data
, 0);
5031 salt_length
= SVAL(preauth
->data
.data
, 2);
5032 hash_selected
= SVAL(preauth
->data
.data
, 4);
5034 if (hash_count
!= 1) {
5035 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5039 if (preauth
->data
.length
!= (6 + salt_length
)) {
5040 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5044 if (hash_selected
!= SMB2_PREAUTH_INTEGRITY_SHA512
) {
5045 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5049 cipher
= smb2_negotiate_context_find(&c
, SMB2_ENCRYPTION_CAPABILITIES
);
5050 if (cipher
!= NULL
) {
5051 uint16_t cipher_count
;
5053 if (cipher
->data
.length
< 2) {
5054 tevent_req_nterror(req
,
5055 NT_STATUS_INVALID_NETWORK_RESPONSE
);
5059 cipher_count
= SVAL(cipher
->data
.data
, 0);
5061 if (cipher_count
> 1) {
5062 tevent_req_nterror(req
,
5063 NT_STATUS_INVALID_NETWORK_RESPONSE
);
5067 if (cipher
->data
.length
< (2 + 2 * cipher_count
)) {
5068 tevent_req_nterror(req
,
5069 NT_STATUS_INVALID_NETWORK_RESPONSE
);
5073 if (cipher_count
== 1) {
5074 uint16_t cipher_selected
;
5076 cipher_selected
= SVAL(cipher
->data
.data
, 2);
5078 switch (cipher_selected
) {
5079 case SMB2_ENCRYPTION_AES128_GCM
:
5080 case SMB2_ENCRYPTION_AES128_CCM
:
5081 conn
->smb2
.server
.cipher
= cipher_selected
;
5087 /* First we hash the request */
5088 smb2cli_req_get_sent_iov(subreq
, sent_iov
);
5089 samba_SHA512_Init(&sctx
);
5090 samba_SHA512_Update(&sctx
, conn
->smb2
.preauth_sha512
,
5091 sizeof(conn
->smb2
.preauth_sha512
));
5092 for (i
= 0; i
< 3; i
++) {
5093 samba_SHA512_Update(&sctx
, sent_iov
[i
].iov_base
, sent_iov
[i
].iov_len
);
5095 samba_SHA512_Final(conn
->smb2
.preauth_sha512
, &sctx
);
5096 TALLOC_FREE(subreq
);
5098 /* And now we hash the response */
5099 samba_SHA512_Init(&sctx
);
5100 samba_SHA512_Update(&sctx
, conn
->smb2
.preauth_sha512
,
5101 sizeof(conn
->smb2
.preauth_sha512
));
5102 for (i
= 0; i
< 3; i
++) {
5103 samba_SHA512_Update(&sctx
, iov
[i
].iov_base
, iov
[i
].iov_len
);
5105 samba_SHA512_Final(conn
->smb2
.preauth_sha512
, &sctx
);
5107 tevent_req_done(req
);
5110 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
5111 TALLOC_CTX
*tmp_mem
,
5114 size_t num_pending
= talloc_array_length(conn
->pending
);
5115 struct tevent_req
*subreq
;
5116 struct smbXcli_req_state
*substate
;
5117 struct tevent_req
*req
;
5118 uint32_t protocol_magic
;
5119 size_t inbuf_len
= smb_len_nbt(inbuf
);
5121 if (num_pending
!= 1) {
5122 return NT_STATUS_INTERNAL_ERROR
;
5125 if (inbuf_len
< 4) {
5126 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5129 subreq
= conn
->pending
[0];
5130 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
5131 req
= tevent_req_callback_data(subreq
, struct tevent_req
);
5133 protocol_magic
= IVAL(inbuf
, 4);
5135 switch (protocol_magic
) {
5137 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
5138 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
5139 return smb1cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
5142 if (substate
->smb2
.recv_iov
== NULL
) {
5144 * For the SMB1 negprot we have move it.
5146 substate
->smb2
.recv_iov
= substate
->smb1
.recv_iov
;
5147 substate
->smb1
.recv_iov
= NULL
;
5151 * we got an SMB2 answer, which consumed sequence number 0
5152 * so we need to use 1 as the next one.
5154 * we also need to set the current credits to 0
5155 * as we consumed the initial one. The SMB2 answer
5156 * hopefully grant us a new credit.
5159 conn
->smb2
.cur_credits
= 0;
5160 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
5161 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
5162 return smb2cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
5165 DEBUG(10, ("Got non-SMB PDU\n"));
5166 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
5169 NTSTATUS
smbXcli_negprot_recv(struct tevent_req
*req
)
5171 return tevent_req_simple_recv_ntstatus(req
);
5174 NTSTATUS
smbXcli_negprot(struct smbXcli_conn
*conn
,
5175 uint32_t timeout_msec
,
5176 enum protocol_types min_protocol
,
5177 enum protocol_types max_protocol
)
5179 TALLOC_CTX
*frame
= talloc_stackframe();
5180 struct tevent_context
*ev
;
5181 struct tevent_req
*req
;
5182 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
5185 if (smbXcli_conn_has_async_calls(conn
)) {
5187 * Can't use sync call while an async call is in flight
5189 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
5192 ev
= samba_tevent_context_init(frame
);
5196 req
= smbXcli_negprot_send(frame
, ev
, conn
, timeout_msec
,
5197 min_protocol
, max_protocol
,
5198 WINDOWS_CLIENT_PURE_SMB2_NEGPROT_INITIAL_CREDIT_ASK
);
5202 ok
= tevent_req_poll_ntstatus(req
, ev
, &status
);
5206 status
= smbXcli_negprot_recv(req
);
5212 struct smb2cli_validate_negotiate_info_state
{
5213 struct smbXcli_conn
*conn
;
5214 DATA_BLOB in_input_buffer
;
5215 DATA_BLOB in_output_buffer
;
5216 DATA_BLOB out_input_buffer
;
5217 DATA_BLOB out_output_buffer
;
5221 static void smb2cli_validate_negotiate_info_done(struct tevent_req
*subreq
);
5223 struct tevent_req
*smb2cli_validate_negotiate_info_send(TALLOC_CTX
*mem_ctx
,
5224 struct tevent_context
*ev
,
5225 struct smbXcli_conn
*conn
,
5226 uint32_t timeout_msec
,
5227 struct smbXcli_session
*session
,
5228 struct smbXcli_tcon
*tcon
)
5230 struct tevent_req
*req
;
5231 struct smb2cli_validate_negotiate_info_state
*state
;
5233 uint16_t dialect_count
= 0;
5234 struct tevent_req
*subreq
;
5235 bool _save_should_sign
;
5238 req
= tevent_req_create(mem_ctx
, &state
,
5239 struct smb2cli_validate_negotiate_info_state
);
5245 state
->in_input_buffer
= data_blob_talloc_zero(state
,
5246 4 + 16 + 1 + 1 + 2);
5247 if (tevent_req_nomem(state
->in_input_buffer
.data
, req
)) {
5248 return tevent_req_post(req
, ev
);
5250 buf
= state
->in_input_buffer
.data
;
5252 if (state
->conn
->max_protocol
>= PROTOCOL_SMB2_22
) {
5253 SIVAL(buf
, 0, conn
->smb2
.client
.capabilities
);
5255 SIVAL(buf
, 0, 0); /* Capabilities */
5257 if (state
->conn
->max_protocol
>= PROTOCOL_SMB2_10
) {
5261 status
= GUID_to_ndr_blob(&conn
->smb2
.client
.guid
,
5263 if (!NT_STATUS_IS_OK(status
)) {
5266 memcpy(buf
+4, blob
.data
, 16); /* ClientGuid */
5268 memset(buf
+4, 0, 16); /* ClientGuid */
5270 if (state
->conn
->min_protocol
>= PROTOCOL_SMB2_02
) {
5271 SCVAL(buf
, 20, conn
->smb2
.client
.security_mode
);
5275 SCVAL(buf
, 21, 0); /* reserved */
5277 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
5281 if (smb2cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
5285 if (smb2cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
5289 if (smb2cli_prots
[i
].proto
== state
->conn
->protocol
) {
5290 state
->dialect
= smb2cli_prots
[i
].smb2_dialect
;
5293 ofs
= state
->in_input_buffer
.length
;
5294 ok
= data_blob_realloc(state
, &state
->in_input_buffer
,
5297 tevent_req_oom(req
);
5298 return tevent_req_post(req
, ev
);
5301 buf
= state
->in_input_buffer
.data
;
5302 SSVAL(buf
, ofs
, smb2cli_prots
[i
].smb2_dialect
);
5306 buf
= state
->in_input_buffer
.data
;
5307 SSVAL(buf
, 22, dialect_count
);
5309 _save_should_sign
= smb2cli_tcon_is_signing_on(tcon
);
5310 smb2cli_tcon_should_sign(tcon
, true);
5311 subreq
= smb2cli_ioctl_send(state
, ev
, conn
,
5312 timeout_msec
, session
, tcon
,
5313 UINT64_MAX
, /* in_fid_persistent */
5314 UINT64_MAX
, /* in_fid_volatile */
5315 FSCTL_VALIDATE_NEGOTIATE_INFO
,
5316 0, /* in_max_input_length */
5317 &state
->in_input_buffer
,
5318 24, /* in_max_output_length */
5319 &state
->in_output_buffer
,
5320 SMB2_IOCTL_FLAG_IS_FSCTL
);
5321 smb2cli_tcon_should_sign(tcon
, _save_should_sign
);
5322 if (tevent_req_nomem(subreq
, req
)) {
5323 return tevent_req_post(req
, ev
);
5325 tevent_req_set_callback(subreq
,
5326 smb2cli_validate_negotiate_info_done
,
5332 static void smb2cli_validate_negotiate_info_done(struct tevent_req
*subreq
)
5334 struct tevent_req
*req
=
5335 tevent_req_callback_data(subreq
,
5337 struct smb2cli_validate_negotiate_info_state
*state
=
5338 tevent_req_data(req
,
5339 struct smb2cli_validate_negotiate_info_state
);
5342 uint32_t capabilities
;
5343 DATA_BLOB guid_blob
;
5344 struct GUID server_guid
;
5345 uint16_t security_mode
;
5348 status
= smb2cli_ioctl_recv(subreq
, state
,
5349 &state
->out_input_buffer
,
5350 &state
->out_output_buffer
);
5351 TALLOC_FREE(subreq
);
5352 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_CLOSED
)) {
5354 * The response was signed, but not supported
5356 * Older Windows and Samba releases return
5357 * NT_STATUS_FILE_CLOSED.
5359 tevent_req_done(req
);
5362 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_DEVICE_REQUEST
)) {
5364 * The response was signed, but not supported
5366 * This is returned by the NTVFS based Samba 4.x file server
5369 tevent_req_done(req
);
5372 if (NT_STATUS_EQUAL(status
, NT_STATUS_FS_DRIVER_REQUIRED
)) {
5374 * The response was signed, but not supported
5376 * This is returned by the NTVFS based Samba 4.x file server
5379 tevent_req_done(req
);
5382 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_SUPPORTED
)) {
5384 * The response was signed, but not supported
5386 * This might be returned by older Windows versions or by
5387 * NetApp SMB server implementations.
5391 * https://blogs.msdn.microsoft.com/openspecification/2012/06/28/smb3-secure-dialect-negotiation/
5394 tevent_req_done(req
);
5397 if (tevent_req_nterror(req
, status
)) {
5401 if (state
->out_output_buffer
.length
!= 24) {
5402 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5406 buf
= state
->out_output_buffer
.data
;
5408 capabilities
= IVAL(buf
, 0);
5409 guid_blob
= data_blob_const(buf
+ 4, 16);
5410 status
= GUID_from_data_blob(&guid_blob
, &server_guid
);
5411 if (tevent_req_nterror(req
, status
)) {
5414 security_mode
= CVAL(buf
, 20);
5415 dialect
= SVAL(buf
, 22);
5417 if (capabilities
!= state
->conn
->smb2
.server
.capabilities
) {
5418 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
5422 if (!GUID_equal(&server_guid
, &state
->conn
->smb2
.server
.guid
)) {
5423 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
5427 if (security_mode
!= state
->conn
->smb2
.server
.security_mode
) {
5428 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
5432 if (dialect
!= state
->dialect
) {
5433 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
5437 tevent_req_done(req
);
5440 NTSTATUS
smb2cli_validate_negotiate_info_recv(struct tevent_req
*req
)
5442 return tevent_req_simple_recv_ntstatus(req
);
5445 static int smbXcli_session_destructor(struct smbXcli_session
*session
)
5447 if (session
->conn
== NULL
) {
5451 DLIST_REMOVE(session
->conn
->sessions
, session
);
5455 struct smbXcli_session
*smbXcli_session_create(TALLOC_CTX
*mem_ctx
,
5456 struct smbXcli_conn
*conn
)
5458 struct smbXcli_session
*session
;
5460 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
5461 if (session
== NULL
) {
5464 session
->smb2
= talloc_zero(session
, struct smb2cli_session
);
5465 if (session
->smb2
== NULL
) {
5466 talloc_free(session
);
5469 talloc_set_destructor(session
, smbXcli_session_destructor
);
5471 DLIST_ADD_END(conn
->sessions
, session
);
5472 session
->conn
= conn
;
5474 memcpy(session
->smb2_channel
.preauth_sha512
,
5475 conn
->smb2
.preauth_sha512
,
5476 sizeof(session
->smb2_channel
.preauth_sha512
));
5481 struct smbXcli_session
*smbXcli_session_copy(TALLOC_CTX
*mem_ctx
,
5482 struct smbXcli_session
*src
)
5484 struct smbXcli_session
*session
;
5486 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
5487 if (session
== NULL
) {
5490 session
->smb2
= talloc_zero(session
, struct smb2cli_session
);
5491 if (session
->smb2
== NULL
) {
5492 talloc_free(session
);
5496 session
->conn
= src
->conn
;
5497 *session
->smb2
= *src
->smb2
;
5498 session
->smb2_channel
= src
->smb2_channel
;
5499 session
->disconnect_expired
= src
->disconnect_expired
;
5501 DLIST_ADD_END(src
->conn
->sessions
, session
);
5502 talloc_set_destructor(session
, smbXcli_session_destructor
);
5507 bool smbXcli_session_is_guest(struct smbXcli_session
*session
)
5509 if (session
== NULL
) {
5513 if (session
->conn
== NULL
) {
5517 if (session
->conn
->mandatory_signing
) {
5521 if (session
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
5522 if (session
->smb2
->session_flags
& SMB2_SESSION_FLAG_IS_GUEST
) {
5528 if (session
->smb1
.action
& SMB_SETUP_GUEST
) {
5535 bool smbXcli_session_is_authenticated(struct smbXcli_session
*session
)
5537 const DATA_BLOB
*application_key
;
5539 if (session
== NULL
) {
5543 if (session
->conn
== NULL
) {
5548 * If we have an application key we had a session key negotiated
5551 if (session
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
5552 application_key
= &session
->smb2
->application_key
;
5554 application_key
= &session
->smb1
.application_key
;
5557 if (application_key
->length
== 0) {
5564 NTSTATUS
smbXcli_session_application_key(struct smbXcli_session
*session
,
5565 TALLOC_CTX
*mem_ctx
,
5568 const DATA_BLOB
*application_key
;
5570 *key
= data_blob_null
;
5572 if (session
->conn
== NULL
) {
5573 return NT_STATUS_NO_USER_SESSION_KEY
;
5576 if (session
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
5577 application_key
= &session
->smb2
->application_key
;
5579 application_key
= &session
->smb1
.application_key
;
5582 if (application_key
->length
== 0) {
5583 return NT_STATUS_NO_USER_SESSION_KEY
;
5586 *key
= data_blob_dup_talloc(mem_ctx
, *application_key
);
5587 if (key
->data
== NULL
) {
5588 return NT_STATUS_NO_MEMORY
;
5591 return NT_STATUS_OK
;
5594 void smbXcli_session_set_disconnect_expired(struct smbXcli_session
*session
)
5596 session
->disconnect_expired
= true;
5599 uint16_t smb1cli_session_current_id(struct smbXcli_session
*session
)
5601 return session
->smb1
.session_id
;
5604 void smb1cli_session_set_id(struct smbXcli_session
*session
,
5605 uint16_t session_id
)
5607 session
->smb1
.session_id
= session_id
;
5610 void smb1cli_session_set_action(struct smbXcli_session
*session
,
5613 session
->smb1
.action
= action
;
5616 NTSTATUS
smb1cli_session_set_session_key(struct smbXcli_session
*session
,
5617 const DATA_BLOB _session_key
)
5619 struct smbXcli_conn
*conn
= session
->conn
;
5620 uint8_t session_key
[16];
5623 return NT_STATUS_INVALID_PARAMETER_MIX
;
5626 if (session
->smb1
.application_key
.length
!= 0) {
5628 * TODO: do not allow this...
5630 * return NT_STATUS_INVALID_PARAMETER_MIX;
5632 data_blob_clear_free(&session
->smb1
.application_key
);
5633 session
->smb1
.protected_key
= false;
5636 if (_session_key
.length
== 0) {
5637 return NT_STATUS_OK
;
5640 ZERO_STRUCT(session_key
);
5641 memcpy(session_key
, _session_key
.data
,
5642 MIN(_session_key
.length
, sizeof(session_key
)));
5644 session
->smb1
.application_key
= data_blob_talloc(session
,
5646 sizeof(session_key
));
5647 ZERO_STRUCT(session_key
);
5648 if (session
->smb1
.application_key
.data
== NULL
) {
5649 return NT_STATUS_NO_MEMORY
;
5652 session
->smb1
.protected_key
= false;
5654 return NT_STATUS_OK
;
5657 NTSTATUS
smb1cli_session_protect_session_key(struct smbXcli_session
*session
)
5659 if (session
->smb1
.protected_key
) {
5660 /* already protected */
5661 return NT_STATUS_OK
;
5664 if (session
->smb1
.application_key
.length
!= 16) {
5665 return NT_STATUS_INVALID_PARAMETER_MIX
;
5668 smb_key_derivation(session
->smb1
.application_key
.data
,
5669 session
->smb1
.application_key
.length
,
5670 session
->smb1
.application_key
.data
);
5672 session
->smb1
.protected_key
= true;
5674 return NT_STATUS_OK
;
5677 uint8_t smb2cli_session_security_mode(struct smbXcli_session
*session
)
5679 struct smbXcli_conn
*conn
= session
->conn
;
5680 uint8_t security_mode
= 0;
5683 return security_mode
;
5686 security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
5687 if (conn
->mandatory_signing
) {
5688 security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
5690 if (session
->smb2
->should_sign
) {
5691 security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
5694 return security_mode
;
5697 uint64_t smb2cli_session_current_id(struct smbXcli_session
*session
)
5699 return session
->smb2
->session_id
;
5702 uint16_t smb2cli_session_get_flags(struct smbXcli_session
*session
)
5704 return session
->smb2
->session_flags
;
5707 void smb2cli_session_set_id_and_flags(struct smbXcli_session
*session
,
5708 uint64_t session_id
,
5709 uint16_t session_flags
)
5711 session
->smb2
->session_id
= session_id
;
5712 session
->smb2
->session_flags
= session_flags
;
5715 void smb2cli_session_increment_channel_sequence(struct smbXcli_session
*session
)
5717 session
->smb2
->channel_sequence
+= 1;
5720 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session
*session
,
5721 uint16_t channel_sequence
)
5725 prev_cs
= session
->smb2
->channel_sequence
;
5726 session
->smb2
->channel_sequence
= channel_sequence
;
5731 uint16_t smb2cli_session_current_channel_sequence(struct smbXcli_session
*session
)
5733 return session
->smb2
->channel_sequence
;
5736 void smb2cli_session_start_replay(struct smbXcli_session
*session
)
5738 session
->smb2
->replay_active
= true;
5741 void smb2cli_session_stop_replay(struct smbXcli_session
*session
)
5743 session
->smb2
->replay_active
= false;
5746 void smb2cli_session_require_signed_response(struct smbXcli_session
*session
,
5747 bool require_signed_response
)
5749 session
->smb2
->require_signed_response
= require_signed_response
;
5752 NTSTATUS
smb2cli_session_update_preauth(struct smbXcli_session
*session
,
5753 const struct iovec
*iov
)
5755 struct hc_sha512state sctx
;
5758 if (session
->conn
== NULL
) {
5759 return NT_STATUS_INTERNAL_ERROR
;
5762 if (session
->conn
->protocol
< PROTOCOL_SMB3_10
) {
5763 return NT_STATUS_OK
;
5766 if (session
->smb2_channel
.signing_key
.length
!= 0) {
5767 return NT_STATUS_OK
;
5770 samba_SHA512_Init(&sctx
);
5771 samba_SHA512_Update(&sctx
, session
->smb2_channel
.preauth_sha512
,
5772 sizeof(session
->smb2_channel
.preauth_sha512
));
5773 for (i
= 0; i
< 3; i
++) {
5774 samba_SHA512_Update(&sctx
, iov
[i
].iov_base
, iov
[i
].iov_len
);
5776 samba_SHA512_Final(session
->smb2_channel
.preauth_sha512
, &sctx
);
5778 return NT_STATUS_OK
;
5781 NTSTATUS
smb2cli_session_set_session_key(struct smbXcli_session
*session
,
5782 const DATA_BLOB _session_key
,
5783 const struct iovec
*recv_iov
)
5785 struct smbXcli_conn
*conn
= session
->conn
;
5786 uint16_t no_sign_flags
= 0;
5787 uint8_t session_key
[16];
5788 bool check_signature
= true;
5791 struct _derivation
{
5796 struct _derivation signing
;
5797 struct _derivation encryption
;
5798 struct _derivation decryption
;
5799 struct _derivation application
;
5801 size_t nonce_size
= 0;
5804 return NT_STATUS_INVALID_PARAMETER_MIX
;
5807 if (recv_iov
[0].iov_len
!= SMB2_HDR_BODY
) {
5808 return NT_STATUS_INVALID_PARAMETER_MIX
;
5811 if (!conn
->mandatory_signing
) {
5813 * only allow guest sessions without
5814 * mandatory signing.
5816 * If we try an authentication with username != ""
5817 * and the server let us in without verifying the
5818 * password we don't have a negotiated session key
5821 no_sign_flags
= SMB2_SESSION_FLAG_IS_GUEST
;
5824 if (session
->smb2
->session_flags
& no_sign_flags
) {
5825 session
->smb2
->should_sign
= false;
5826 return NT_STATUS_OK
;
5829 if (session
->smb2
->signing_key
.length
!= 0) {
5830 return NT_STATUS_INVALID_PARAMETER_MIX
;
5833 if (conn
->protocol
>= PROTOCOL_SMB3_10
) {
5834 struct _derivation
*d
;
5837 p
= data_blob_const(session
->smb2_channel
.preauth_sha512
,
5838 sizeof(session
->smb2_channel
.preauth_sha512
));
5840 d
= &derivation
.signing
;
5841 d
->label
= data_blob_string_const_null("SMBSigningKey");
5844 d
= &derivation
.encryption
;
5845 d
->label
= data_blob_string_const_null("SMBC2SCipherKey");
5848 d
= &derivation
.decryption
;
5849 d
->label
= data_blob_string_const_null("SMBS2CCipherKey");
5852 d
= &derivation
.application
;
5853 d
->label
= data_blob_string_const_null("SMBAppKey");
5856 } else if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
5857 struct _derivation
*d
;
5859 d
= &derivation
.signing
;
5860 d
->label
= data_blob_string_const_null("SMB2AESCMAC");
5861 d
->context
= data_blob_string_const_null("SmbSign");
5863 d
= &derivation
.encryption
;
5864 d
->label
= data_blob_string_const_null("SMB2AESCCM");
5865 d
->context
= data_blob_string_const_null("ServerIn ");
5867 d
= &derivation
.decryption
;
5868 d
->label
= data_blob_string_const_null("SMB2AESCCM");
5869 d
->context
= data_blob_string_const_null("ServerOut");
5871 d
= &derivation
.application
;
5872 d
->label
= data_blob_string_const_null("SMB2APP");
5873 d
->context
= data_blob_string_const_null("SmbRpc");
5876 ZERO_STRUCT(session_key
);
5877 memcpy(session_key
, _session_key
.data
,
5878 MIN(_session_key
.length
, sizeof(session_key
)));
5880 session
->smb2
->signing_key
= data_blob_talloc(session
,
5882 sizeof(session_key
));
5883 if (session
->smb2
->signing_key
.data
== NULL
) {
5884 ZERO_STRUCT(session_key
);
5885 return NT_STATUS_NO_MEMORY
;
5888 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
5889 struct _derivation
*d
= &derivation
.signing
;
5891 smb2_key_derivation(session_key
, sizeof(session_key
),
5892 d
->label
.data
, d
->label
.length
,
5893 d
->context
.data
, d
->context
.length
,
5894 session
->smb2
->signing_key
.data
);
5897 session
->smb2
->encryption_key
= data_blob_dup_talloc(session
,
5898 session
->smb2
->signing_key
);
5899 if (session
->smb2
->encryption_key
.data
== NULL
) {
5900 ZERO_STRUCT(session_key
);
5901 return NT_STATUS_NO_MEMORY
;
5904 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
5905 struct _derivation
*d
= &derivation
.encryption
;
5907 smb2_key_derivation(session_key
, sizeof(session_key
),
5908 d
->label
.data
, d
->label
.length
,
5909 d
->context
.data
, d
->context
.length
,
5910 session
->smb2
->encryption_key
.data
);
5913 session
->smb2
->decryption_key
= data_blob_dup_talloc(session
,
5914 session
->smb2
->signing_key
);
5915 if (session
->smb2
->decryption_key
.data
== NULL
) {
5916 ZERO_STRUCT(session_key
);
5917 return NT_STATUS_NO_MEMORY
;
5920 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
5921 struct _derivation
*d
= &derivation
.decryption
;
5923 smb2_key_derivation(session_key
, sizeof(session_key
),
5924 d
->label
.data
, d
->label
.length
,
5925 d
->context
.data
, d
->context
.length
,
5926 session
->smb2
->decryption_key
.data
);
5929 session
->smb2
->application_key
= data_blob_dup_talloc(session
,
5930 session
->smb2
->signing_key
);
5931 if (session
->smb2
->application_key
.data
== NULL
) {
5932 ZERO_STRUCT(session_key
);
5933 return NT_STATUS_NO_MEMORY
;
5936 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
5937 struct _derivation
*d
= &derivation
.application
;
5939 smb2_key_derivation(session_key
, sizeof(session_key
),
5940 d
->label
.data
, d
->label
.length
,
5941 d
->context
.data
, d
->context
.length
,
5942 session
->smb2
->application_key
.data
);
5944 ZERO_STRUCT(session_key
);
5946 session
->smb2_channel
.signing_key
= data_blob_dup_talloc(session
,
5947 session
->smb2
->signing_key
);
5948 if (session
->smb2_channel
.signing_key
.data
== NULL
) {
5949 return NT_STATUS_NO_MEMORY
;
5952 check_signature
= conn
->mandatory_signing
;
5954 hdr_flags
= IVAL(recv_iov
[0].iov_base
, SMB2_HDR_FLAGS
);
5955 if (hdr_flags
& SMB2_HDR_FLAG_SIGNED
) {
5957 * Sadly some vendors don't sign the
5958 * final SMB2 session setup response
5960 * At least Windows and Samba are always doing this
5961 * if there's a session key available.
5963 * We only check the signature if it's mandatory
5964 * or SMB2_HDR_FLAG_SIGNED is provided.
5966 check_signature
= true;
5969 if (conn
->protocol
>= PROTOCOL_SMB3_10
) {
5970 check_signature
= true;
5973 if (check_signature
) {
5974 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
5975 session
->conn
->protocol
,
5977 if (!NT_STATUS_IS_OK(status
)) {
5982 session
->smb2
->should_sign
= false;
5983 session
->smb2
->should_encrypt
= false;
5985 if (conn
->desire_signing
) {
5986 session
->smb2
->should_sign
= true;
5989 if (conn
->smb2
.server
.security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) {
5990 session
->smb2
->should_sign
= true;
5993 if (session
->smb2
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
) {
5994 session
->smb2
->should_encrypt
= true;
5997 if (conn
->protocol
< PROTOCOL_SMB2_24
) {
5998 session
->smb2
->should_encrypt
= false;
6001 if (conn
->smb2
.server
.cipher
== 0) {
6002 session
->smb2
->should_encrypt
= false;
6006 * CCM and GCM algorithms must never have their
6007 * nonce wrap, or the security of the whole
6008 * communication and the keys is destroyed.
6009 * We must drop the connection once we have
6010 * transfered too much data.
6012 * NOTE: We assume nonces greater than 8 bytes.
6014 generate_random_buffer((uint8_t *)&session
->smb2
->nonce_high_random
,
6015 sizeof(session
->smb2
->nonce_high_random
));
6016 switch (conn
->smb2
.server
.cipher
) {
6017 case SMB2_ENCRYPTION_AES128_CCM
:
6018 nonce_size
= AES_CCM_128_NONCE_SIZE
;
6020 case SMB2_ENCRYPTION_AES128_GCM
:
6021 nonce_size
= AES_GCM_128_IV_SIZE
;
6027 session
->smb2
->nonce_high_max
= SMB2_NONCE_HIGH_MAX(nonce_size
);
6028 session
->smb2
->nonce_high
= 0;
6029 session
->smb2
->nonce_low
= 0;
6031 return NT_STATUS_OK
;
6034 NTSTATUS
smb2cli_session_create_channel(TALLOC_CTX
*mem_ctx
,
6035 struct smbXcli_session
*session1
,
6036 struct smbXcli_conn
*conn
,
6037 struct smbXcli_session
**_session2
)
6039 struct smbXcli_session
*session2
;
6041 if (session1
->smb2
->signing_key
.length
== 0) {
6042 return NT_STATUS_INVALID_PARAMETER_MIX
;
6046 return NT_STATUS_INVALID_PARAMETER_MIX
;
6049 session2
= talloc_zero(mem_ctx
, struct smbXcli_session
);
6050 if (session2
== NULL
) {
6051 return NT_STATUS_NO_MEMORY
;
6053 session2
->smb2
= talloc_reference(session2
, session1
->smb2
);
6054 if (session2
->smb2
== NULL
) {
6055 talloc_free(session2
);
6056 return NT_STATUS_NO_MEMORY
;
6059 talloc_set_destructor(session2
, smbXcli_session_destructor
);
6060 DLIST_ADD_END(conn
->sessions
, session2
);
6061 session2
->conn
= conn
;
6063 memcpy(session2
->smb2_channel
.preauth_sha512
,
6064 conn
->smb2
.preauth_sha512
,
6065 sizeof(session2
->smb2_channel
.preauth_sha512
));
6067 *_session2
= session2
;
6068 return NT_STATUS_OK
;
6071 NTSTATUS
smb2cli_session_set_channel_key(struct smbXcli_session
*session
,
6072 const DATA_BLOB _channel_key
,
6073 const struct iovec
*recv_iov
)
6075 struct smbXcli_conn
*conn
= session
->conn
;
6076 uint8_t channel_key
[16];
6078 struct _derivation
{
6083 struct _derivation signing
;
6087 return NT_STATUS_INVALID_PARAMETER_MIX
;
6090 if (session
->smb2_channel
.signing_key
.length
!= 0) {
6091 return NT_STATUS_INVALID_PARAMETER_MIX
;
6094 if (conn
->protocol
>= PROTOCOL_SMB3_10
) {
6095 struct _derivation
*d
;
6098 p
= data_blob_const(session
->smb2_channel
.preauth_sha512
,
6099 sizeof(session
->smb2_channel
.preauth_sha512
));
6101 d
= &derivation
.signing
;
6102 d
->label
= data_blob_string_const_null("SMBSigningKey");
6104 } else if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
6105 struct _derivation
*d
;
6107 d
= &derivation
.signing
;
6108 d
->label
= data_blob_string_const_null("SMB2AESCMAC");
6109 d
->context
= data_blob_string_const_null("SmbSign");
6112 ZERO_STRUCT(channel_key
);
6113 memcpy(channel_key
, _channel_key
.data
,
6114 MIN(_channel_key
.length
, sizeof(channel_key
)));
6116 session
->smb2_channel
.signing_key
= data_blob_talloc(session
,
6118 sizeof(channel_key
));
6119 if (session
->smb2_channel
.signing_key
.data
== NULL
) {
6120 ZERO_STRUCT(channel_key
);
6121 return NT_STATUS_NO_MEMORY
;
6124 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
6125 struct _derivation
*d
= &derivation
.signing
;
6127 smb2_key_derivation(channel_key
, sizeof(channel_key
),
6128 d
->label
.data
, d
->label
.length
,
6129 d
->context
.data
, d
->context
.length
,
6130 session
->smb2_channel
.signing_key
.data
);
6132 ZERO_STRUCT(channel_key
);
6134 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
6135 session
->conn
->protocol
,
6137 if (!NT_STATUS_IS_OK(status
)) {
6141 return NT_STATUS_OK
;
6144 NTSTATUS
smb2cli_session_encryption_on(struct smbXcli_session
*session
)
6146 if (!session
->smb2
->should_sign
) {
6148 * We need required signing on the session
6149 * in order to prevent man in the middle attacks.
6151 return NT_STATUS_INVALID_PARAMETER_MIX
;
6154 if (session
->smb2
->should_encrypt
) {
6155 return NT_STATUS_OK
;
6158 if (session
->conn
->protocol
< PROTOCOL_SMB2_24
) {
6159 return NT_STATUS_NOT_SUPPORTED
;
6162 if (session
->conn
->smb2
.server
.cipher
== 0) {
6163 return NT_STATUS_NOT_SUPPORTED
;
6166 if (session
->smb2
->signing_key
.data
== NULL
) {
6167 return NT_STATUS_NOT_SUPPORTED
;
6169 session
->smb2
->should_encrypt
= true;
6170 return NT_STATUS_OK
;
6173 struct smbXcli_tcon
*smbXcli_tcon_create(TALLOC_CTX
*mem_ctx
)
6175 struct smbXcli_tcon
*tcon
;
6177 tcon
= talloc_zero(mem_ctx
, struct smbXcli_tcon
);
6186 * Return a deep structure copy of a struct smbXcli_tcon *
6189 struct smbXcli_tcon
*smbXcli_tcon_copy(TALLOC_CTX
*mem_ctx
,
6190 const struct smbXcli_tcon
*tcon_in
)
6192 struct smbXcli_tcon
*tcon
;
6194 tcon
= talloc_memdup(mem_ctx
, tcon_in
, sizeof(struct smbXcli_tcon
));
6199 /* Deal with the SMB1 strings. */
6200 if (tcon_in
->smb1
.service
!= NULL
) {
6201 tcon
->smb1
.service
= talloc_strdup(tcon
, tcon_in
->smb1
.service
);
6202 if (tcon
->smb1
.service
== NULL
) {
6207 if (tcon
->smb1
.fs_type
!= NULL
) {
6208 tcon
->smb1
.fs_type
= talloc_strdup(tcon
, tcon_in
->smb1
.fs_type
);
6209 if (tcon
->smb1
.fs_type
== NULL
) {
6217 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon
*tcon
,
6218 uint32_t fs_attributes
)
6220 tcon
->fs_attributes
= fs_attributes
;
6223 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon
*tcon
)
6225 return tcon
->fs_attributes
;
6228 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon
*tcon
)
6234 if (tcon
->is_smb1
) {
6235 if (tcon
->smb1
.optional_support
& SMB_SHARE_IN_DFS
) {
6242 if (tcon
->smb2
.capabilities
& SMB2_SHARE_CAP_DFS
) {
6249 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
6251 return tcon
->smb1
.tcon_id
;
6254 void smb1cli_tcon_set_id(struct smbXcli_tcon
*tcon
, uint16_t tcon_id
)
6256 tcon
->is_smb1
= true;
6257 tcon
->smb1
.tcon_id
= tcon_id
;
6260 bool smb1cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
6262 uint16_t optional_support
,
6263 uint32_t maximal_access
,
6264 uint32_t guest_maximal_access
,
6265 const char *service
,
6266 const char *fs_type
)
6268 tcon
->is_smb1
= true;
6269 tcon
->fs_attributes
= 0;
6270 tcon
->smb1
.tcon_id
= tcon_id
;
6271 tcon
->smb1
.optional_support
= optional_support
;
6272 tcon
->smb1
.maximal_access
= maximal_access
;
6273 tcon
->smb1
.guest_maximal_access
= guest_maximal_access
;
6275 TALLOC_FREE(tcon
->smb1
.service
);
6276 tcon
->smb1
.service
= talloc_strdup(tcon
, service
);
6277 if (service
!= NULL
&& tcon
->smb1
.service
== NULL
) {
6281 TALLOC_FREE(tcon
->smb1
.fs_type
);
6282 tcon
->smb1
.fs_type
= talloc_strdup(tcon
, fs_type
);
6283 if (fs_type
!= NULL
&& tcon
->smb1
.fs_type
== NULL
) {
6290 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
6292 return tcon
->smb2
.tcon_id
;
6295 void smb2cli_tcon_set_id(struct smbXcli_tcon
*tcon
, uint32_t tcon_id
)
6297 tcon
->smb2
.tcon_id
= tcon_id
;
6300 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon
*tcon
)
6302 return tcon
->smb2
.capabilities
;
6305 uint32_t smb2cli_tcon_flags(struct smbXcli_tcon
*tcon
)
6307 return tcon
->smb2
.flags
;
6310 void smb2cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
6311 struct smbXcli_session
*session
,
6315 uint32_t capabilities
,
6316 uint32_t maximal_access
)
6318 tcon
->is_smb1
= false;
6319 tcon
->fs_attributes
= 0;
6320 tcon
->smb2
.tcon_id
= tcon_id
;
6321 tcon
->smb2
.type
= type
;
6322 tcon
->smb2
.flags
= flags
;
6323 tcon
->smb2
.capabilities
= capabilities
;
6324 tcon
->smb2
.maximal_access
= maximal_access
;
6326 tcon
->smb2
.should_sign
= false;
6327 tcon
->smb2
.should_encrypt
= false;
6329 if (session
== NULL
) {
6333 tcon
->smb2
.should_sign
= session
->smb2
->should_sign
;
6334 tcon
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
6336 if (flags
& SMB2_SHAREFLAG_ENCRYPT_DATA
) {
6337 tcon
->smb2
.should_encrypt
= true;
6341 void smb2cli_tcon_should_sign(struct smbXcli_tcon
*tcon
,
6344 tcon
->smb2
.should_sign
= should_sign
;
6347 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon
*tcon
)
6349 if (tcon
->smb2
.should_encrypt
) {
6353 return tcon
->smb2
.should_sign
;
6356 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon
*tcon
,
6357 bool should_encrypt
)
6359 tcon
->smb2
.should_encrypt
= should_encrypt
;
6362 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon
*tcon
)
6364 return tcon
->smb2
.should_encrypt
;
6367 void smb2cli_conn_set_mid(struct smbXcli_conn
*conn
, uint64_t mid
)
6369 conn
->smb2
.mid
= mid
;
6372 uint64_t smb2cli_conn_get_mid(struct smbXcli_conn
*conn
)
6374 return conn
->smb2
.mid
;