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"
40 struct smbXcli_session
;
46 struct sockaddr_storage local_ss
;
47 struct sockaddr_storage remote_ss
;
48 const char *remote_name
;
50 struct tevent_queue
*outgoing
;
51 struct tevent_req
**pending
;
52 struct tevent_req
*read_smb_req
;
54 enum protocol_types min_protocol
;
55 enum protocol_types max_protocol
;
56 enum protocol_types protocol
;
59 bool mandatory_signing
;
62 * The incoming dispatch function should return:
63 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
64 * - NT_STATUS_OK, if no more processing is desired, e.g.
65 * the dispatch function called
67 * - All other return values disconnect the connection.
69 NTSTATUS (*dispatch_incoming
)(struct smbXcli_conn
*conn
,
75 uint32_t capabilities
;
80 uint32_t capabilities
;
83 uint16_t security_mode
;
92 const char *workgroup
;
98 uint32_t capabilities
;
103 struct smb_signing_state
*signing
;
104 struct smb_trans_enc_state
*trans_enc
;
106 struct tevent_req
*read_braw_req
;
111 uint32_t capabilities
;
112 uint16_t security_mode
;
117 uint32_t capabilities
;
118 uint16_t security_mode
;
120 uint32_t max_trans_size
;
121 uint32_t max_read_size
;
122 uint32_t max_write_size
;
130 uint16_t cur_credits
;
131 uint16_t max_credits
;
133 uint8_t preauth_sha512
[64];
136 struct smbXcli_session
*sessions
;
139 struct smb2cli_session
{
141 uint16_t session_flags
;
142 DATA_BLOB application_key
;
143 DATA_BLOB signing_key
;
146 DATA_BLOB encryption_key
;
147 DATA_BLOB decryption_key
;
150 uint16_t channel_sequence
;
154 struct smbXcli_session
{
155 struct smbXcli_session
*prev
, *next
;
156 struct smbXcli_conn
*conn
;
160 DATA_BLOB application_key
;
164 struct smb2cli_session
*smb2
;
167 DATA_BLOB signing_key
;
168 uint8_t preauth_sha512
[64];
172 * this should be a short term hack
173 * until the upper layers have implemented
176 bool disconnect_expired
;
179 struct smbXcli_tcon
{
181 uint32_t fs_attributes
;
185 uint16_t optional_support
;
186 uint32_t maximal_access
;
187 uint32_t guest_maximal_access
;
196 uint32_t capabilities
;
197 uint32_t maximal_access
;
203 struct smbXcli_req_state
{
204 struct tevent_context
*ev
;
205 struct smbXcli_conn
*conn
;
206 struct smbXcli_session
*session
; /* maybe NULL */
207 struct smbXcli_tcon
*tcon
; /* maybe NULL */
209 uint8_t length_hdr
[4];
216 /* Space for the header including the wct */
217 uint8_t hdr
[HDR_VWV
];
220 * For normal requests, smb1cli_req_send chooses a mid.
221 * SecondaryV trans requests need to use the mid of the primary
222 * request, so we need a place to store it.
223 * Assume it is set if != 0.
228 uint8_t bytecount_buf
[2];
230 #define MAX_SMB_IOV 10
231 /* length_hdr, hdr, words, byte_count, buffers */
232 struct iovec iov
[1 + 3 + MAX_SMB_IOV
];
237 struct tevent_req
**chained_requests
;
240 NTSTATUS recv_status
;
241 /* always an array of 3 talloc elements */
242 struct iovec
*recv_iov
;
246 const uint8_t *fixed
;
251 uint8_t transform
[SMB2_TF_HDR_SIZE
];
252 uint8_t hdr
[SMB2_HDR_BODY
];
253 uint8_t pad
[7]; /* padding space for compounding */
256 * always an array of 3 talloc elements
257 * (without a SMB2_TRANSFORM header!)
261 struct iovec
*recv_iov
;
264 * the expected max for the response dyn_len
266 uint32_t max_dyn_len
;
268 uint16_t credit_charge
;
272 uint64_t encryption_session_id
;
274 bool signing_skipped
;
280 static int smbXcli_conn_destructor(struct smbXcli_conn
*conn
)
283 * NT_STATUS_OK, means we do not notify the callers
285 smbXcli_conn_disconnect(conn
, NT_STATUS_OK
);
287 while (conn
->sessions
) {
288 conn
->sessions
->conn
= NULL
;
289 DLIST_REMOVE(conn
->sessions
, conn
->sessions
);
292 if (conn
->smb1
.trans_enc
) {
293 TALLOC_FREE(conn
->smb1
.trans_enc
);
299 struct smbXcli_conn
*smbXcli_conn_create(TALLOC_CTX
*mem_ctx
,
301 const char *remote_name
,
302 enum smb_signing_setting signing_state
,
303 uint32_t smb1_capabilities
,
304 struct GUID
*client_guid
,
305 uint32_t smb2_capabilities
)
307 struct smbXcli_conn
*conn
= NULL
;
309 struct sockaddr
*sa
= NULL
;
313 conn
= talloc_zero(mem_ctx
, struct smbXcli_conn
);
319 conn
->write_fd
= dup(fd
);
320 if (conn
->write_fd
== -1) {
324 conn
->remote_name
= talloc_strdup(conn
, remote_name
);
325 if (conn
->remote_name
== NULL
) {
330 ss
= (void *)&conn
->local_ss
;
331 sa
= (struct sockaddr
*)ss
;
332 sa_length
= sizeof(conn
->local_ss
);
333 ret
= getsockname(fd
, sa
, &sa_length
);
337 ss
= (void *)&conn
->remote_ss
;
338 sa
= (struct sockaddr
*)ss
;
339 sa_length
= sizeof(conn
->remote_ss
);
340 ret
= getpeername(fd
, sa
, &sa_length
);
345 conn
->outgoing
= tevent_queue_create(conn
, "smbXcli_outgoing");
346 if (conn
->outgoing
== NULL
) {
349 conn
->pending
= NULL
;
351 conn
->min_protocol
= PROTOCOL_NONE
;
352 conn
->max_protocol
= PROTOCOL_NONE
;
353 conn
->protocol
= PROTOCOL_NONE
;
355 switch (signing_state
) {
356 case SMB_SIGNING_OFF
:
358 conn
->allow_signing
= false;
359 conn
->desire_signing
= false;
360 conn
->mandatory_signing
= false;
362 case SMB_SIGNING_DEFAULT
:
363 case SMB_SIGNING_IF_REQUIRED
:
364 /* if the server requires it */
365 conn
->allow_signing
= true;
366 conn
->desire_signing
= false;
367 conn
->mandatory_signing
= false;
369 case SMB_SIGNING_REQUIRED
:
371 conn
->allow_signing
= true;
372 conn
->desire_signing
= true;
373 conn
->mandatory_signing
= true;
377 conn
->smb1
.client
.capabilities
= smb1_capabilities
;
378 conn
->smb1
.client
.max_xmit
= UINT16_MAX
;
380 conn
->smb1
.capabilities
= conn
->smb1
.client
.capabilities
;
381 conn
->smb1
.max_xmit
= 1024;
385 /* initialise signing */
386 conn
->smb1
.signing
= smb_signing_init(conn
,
388 conn
->desire_signing
,
389 conn
->mandatory_signing
);
390 if (!conn
->smb1
.signing
) {
394 conn
->smb2
.client
.security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
395 if (conn
->mandatory_signing
) {
396 conn
->smb2
.client
.security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
399 conn
->smb2
.client
.guid
= *client_guid
;
401 conn
->smb2
.client
.capabilities
= smb2_capabilities
;
403 conn
->smb2
.cur_credits
= 1;
404 conn
->smb2
.max_credits
= 0;
406 talloc_set_destructor(conn
, smbXcli_conn_destructor
);
410 if (conn
->write_fd
!= -1) {
411 close(conn
->write_fd
);
417 bool smbXcli_conn_is_connected(struct smbXcli_conn
*conn
)
423 if (conn
->read_fd
== -1) {
430 enum protocol_types
smbXcli_conn_protocol(struct smbXcli_conn
*conn
)
432 return conn
->protocol
;
435 bool smbXcli_conn_use_unicode(struct smbXcli_conn
*conn
)
437 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
441 if (conn
->smb1
.capabilities
& CAP_UNICODE
) {
448 void smbXcli_conn_set_sockopt(struct smbXcli_conn
*conn
, const char *options
)
450 set_socket_options(conn
->read_fd
, options
);
453 const struct sockaddr_storage
*smbXcli_conn_local_sockaddr(struct smbXcli_conn
*conn
)
455 return &conn
->local_ss
;
458 const struct sockaddr_storage
*smbXcli_conn_remote_sockaddr(struct smbXcli_conn
*conn
)
460 return &conn
->remote_ss
;
463 const char *smbXcli_conn_remote_name(struct smbXcli_conn
*conn
)
465 return conn
->remote_name
;
468 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn
*conn
)
470 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
477 return conn
->smb1
.server
.max_mux
;
480 NTTIME
smbXcli_conn_server_system_time(struct smbXcli_conn
*conn
)
482 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
483 return conn
->smb2
.server
.system_time
;
486 return conn
->smb1
.server
.system_time
;
489 const DATA_BLOB
*smbXcli_conn_server_gss_blob(struct smbXcli_conn
*conn
)
491 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
492 return &conn
->smb2
.server
.gss_blob
;
495 return &conn
->smb1
.server
.gss_blob
;
498 const struct GUID
*smbXcli_conn_server_guid(struct smbXcli_conn
*conn
)
500 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
501 return &conn
->smb2
.server
.guid
;
504 return &conn
->smb1
.server
.guid
;
507 struct smbXcli_conn_samba_suicide_state
{
508 struct smbXcli_conn
*conn
;
513 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
);
515 struct tevent_req
*smbXcli_conn_samba_suicide_send(TALLOC_CTX
*mem_ctx
,
516 struct tevent_context
*ev
,
517 struct smbXcli_conn
*conn
,
520 struct tevent_req
*req
, *subreq
;
521 struct smbXcli_conn_samba_suicide_state
*state
;
523 req
= tevent_req_create(mem_ctx
, &state
,
524 struct smbXcli_conn_samba_suicide_state
);
529 SIVAL(state
->buf
, 4, 0x74697865);
530 SCVAL(state
->buf
, 8, exitcode
);
531 _smb_setlen_nbt(state
->buf
, sizeof(state
->buf
)-4);
533 state
->iov
.iov_base
= state
->buf
;
534 state
->iov
.iov_len
= sizeof(state
->buf
);
536 subreq
= writev_send(state
, ev
, conn
->outgoing
, conn
->write_fd
,
537 false, &state
->iov
, 1);
538 if (tevent_req_nomem(subreq
, req
)) {
539 return tevent_req_post(req
, ev
);
541 tevent_req_set_callback(subreq
, smbXcli_conn_samba_suicide_done
, req
);
545 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
)
547 struct tevent_req
*req
= tevent_req_callback_data(
548 subreq
, struct tevent_req
);
549 struct smbXcli_conn_samba_suicide_state
*state
= tevent_req_data(
550 req
, struct smbXcli_conn_samba_suicide_state
);
554 nwritten
= writev_recv(subreq
, &err
);
556 if (nwritten
== -1) {
557 NTSTATUS status
= map_nt_error_from_unix_common(err
);
558 smbXcli_conn_disconnect(state
->conn
, status
);
561 tevent_req_done(req
);
564 NTSTATUS
smbXcli_conn_samba_suicide_recv(struct tevent_req
*req
)
566 return tevent_req_simple_recv_ntstatus(req
);
569 NTSTATUS
smbXcli_conn_samba_suicide(struct smbXcli_conn
*conn
,
572 TALLOC_CTX
*frame
= talloc_stackframe();
573 struct tevent_context
*ev
;
574 struct tevent_req
*req
;
575 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
578 if (smbXcli_conn_has_async_calls(conn
)) {
580 * Can't use sync call while an async call is in flight
582 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
585 ev
= samba_tevent_context_init(frame
);
589 req
= smbXcli_conn_samba_suicide_send(frame
, ev
, conn
, exitcode
);
593 ok
= tevent_req_poll_ntstatus(req
, ev
, &status
);
597 status
= smbXcli_conn_samba_suicide_recv(req
);
603 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn
*conn
)
605 return conn
->smb1
.capabilities
;
608 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn
*conn
)
610 return conn
->smb1
.max_xmit
;
613 bool smb1cli_conn_req_possible(struct smbXcli_conn
*conn
)
616 uint16_t possible
= conn
->smb1
.server
.max_mux
;
618 pending
= tevent_queue_length(conn
->outgoing
);
619 if (pending
>= possible
) {
622 pending
+= talloc_array_length(conn
->pending
);
623 if (pending
>= possible
) {
630 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn
*conn
)
632 return conn
->smb1
.server
.session_key
;
635 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn
*conn
)
637 return conn
->smb1
.server
.challenge
;
640 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
642 return conn
->smb1
.server
.security_mode
;
645 bool smb1cli_conn_server_readbraw(struct smbXcli_conn
*conn
)
647 return conn
->smb1
.server
.readbraw
;
650 bool smb1cli_conn_server_writebraw(struct smbXcli_conn
*conn
)
652 return conn
->smb1
.server
.writebraw
;
655 bool smb1cli_conn_server_lockread(struct smbXcli_conn
*conn
)
657 return conn
->smb1
.server
.lockread
;
660 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn
*conn
)
662 return conn
->smb1
.server
.writeunlock
;
665 int smb1cli_conn_server_time_zone(struct smbXcli_conn
*conn
)
667 return conn
->smb1
.server
.time_zone
;
670 bool smb1cli_conn_activate_signing(struct smbXcli_conn
*conn
,
671 const DATA_BLOB user_session_key
,
672 const DATA_BLOB response
)
674 return smb_signing_activate(conn
->smb1
.signing
,
679 bool smb1cli_conn_check_signing(struct smbXcli_conn
*conn
,
680 const uint8_t *buf
, uint32_t seqnum
)
682 const uint8_t *hdr
= buf
+ NBT_HDR_SIZE
;
683 size_t len
= smb_len_nbt(buf
);
685 return smb_signing_check_pdu(conn
->smb1
.signing
, hdr
, len
, seqnum
);
688 bool smb1cli_conn_signing_is_active(struct smbXcli_conn
*conn
)
690 return smb_signing_is_active(conn
->smb1
.signing
);
693 void smb1cli_conn_set_encryption(struct smbXcli_conn
*conn
,
694 struct smb_trans_enc_state
*es
)
696 /* Replace the old state, if any. */
697 if (conn
->smb1
.trans_enc
) {
698 TALLOC_FREE(conn
->smb1
.trans_enc
);
700 conn
->smb1
.trans_enc
= es
;
703 bool smb1cli_conn_encryption_on(struct smbXcli_conn
*conn
)
705 return common_encryption_on(conn
->smb1
.trans_enc
);
709 static NTSTATUS
smb1cli_pull_raw_error(const uint8_t *hdr
)
711 uint32_t flags2
= SVAL(hdr
, HDR_FLG2
);
712 NTSTATUS status
= NT_STATUS(IVAL(hdr
, HDR_RCLS
));
714 if (NT_STATUS_IS_OK(status
)) {
718 if (flags2
& FLAGS2_32_BIT_ERROR_CODES
) {
722 return NT_STATUS_DOS(CVAL(hdr
, HDR_RCLS
), SVAL(hdr
, HDR_ERR
));
726 * Is the SMB command able to hold an AND_X successor
727 * @param[in] cmd The SMB command in question
728 * @retval Can we add a chained request after "cmd"?
730 bool smb1cli_is_andx_req(uint8_t cmd
)
750 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn
*conn
)
752 size_t num_pending
= talloc_array_length(conn
->pending
);
755 if (conn
->protocol
== PROTOCOL_NONE
) {
757 * This is what windows sends on the SMB1 Negprot request
758 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
766 result
= conn
->smb1
.mid
++;
767 if ((result
== 0) || (result
== 0xffff)) {
771 for (i
=0; i
<num_pending
; i
++) {
772 if (result
== smb1cli_req_mid(conn
->pending
[i
])) {
777 if (i
== num_pending
) {
783 void smbXcli_req_unset_pending(struct tevent_req
*req
)
785 struct smbXcli_req_state
*state
=
787 struct smbXcli_req_state
);
788 struct smbXcli_conn
*conn
= state
->conn
;
789 size_t num_pending
= talloc_array_length(conn
->pending
);
792 if (state
->smb1
.mid
!= 0) {
794 * This is a [nt]trans[2] request which waits
795 * for more than one reply.
800 tevent_req_set_cleanup_fn(req
, NULL
);
802 if (num_pending
== 1) {
804 * The pending read_smb tevent_req is a child of
805 * conn->pending. So if nothing is pending anymore, we need to
806 * delete the socket read fde.
808 TALLOC_FREE(conn
->pending
);
809 conn
->read_smb_req
= NULL
;
813 for (i
=0; i
<num_pending
; i
++) {
814 if (req
== conn
->pending
[i
]) {
818 if (i
== num_pending
) {
820 * Something's seriously broken. Just returning here is the
821 * right thing nevertheless, the point of this routine is to
822 * remove ourselves from conn->pending.
828 * Remove ourselves from the conn->pending array
830 for (; i
< (num_pending
- 1); i
++) {
831 conn
->pending
[i
] = conn
->pending
[i
+1];
835 * No NULL check here, we're shrinking by sizeof(void *), and
836 * talloc_realloc just adjusts the size for this.
838 conn
->pending
= talloc_realloc(NULL
, conn
->pending
, struct tevent_req
*,
843 static void smbXcli_req_cleanup(struct tevent_req
*req
,
844 enum tevent_req_state req_state
)
846 struct smbXcli_req_state
*state
=
848 struct smbXcli_req_state
);
851 case TEVENT_REQ_RECEIVED
:
853 * Make sure we really remove it from
854 * the pending array on destruction.
857 smbXcli_req_unset_pending(req
);
864 static bool smb1cli_req_cancel(struct tevent_req
*req
);
865 static bool smb2cli_req_cancel(struct tevent_req
*req
);
867 static bool smbXcli_req_cancel(struct tevent_req
*req
)
869 struct smbXcli_req_state
*state
=
871 struct smbXcli_req_state
);
873 if (!smbXcli_conn_is_connected(state
->conn
)) {
877 if (state
->conn
->protocol
== PROTOCOL_NONE
) {
881 if (state
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
882 return smb2cli_req_cancel(req
);
885 return smb1cli_req_cancel(req
);
888 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
);
890 bool smbXcli_req_set_pending(struct tevent_req
*req
)
892 struct smbXcli_req_state
*state
=
894 struct smbXcli_req_state
);
895 struct smbXcli_conn
*conn
;
896 struct tevent_req
**pending
;
901 if (!smbXcli_conn_is_connected(conn
)) {
905 num_pending
= talloc_array_length(conn
->pending
);
907 pending
= talloc_realloc(conn
, conn
->pending
, struct tevent_req
*,
909 if (pending
== NULL
) {
912 pending
[num_pending
] = req
;
913 conn
->pending
= pending
;
914 tevent_req_set_cleanup_fn(req
, smbXcli_req_cleanup
);
915 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
917 if (!smbXcli_conn_receive_next(conn
)) {
919 * the caller should notify the current request
921 * And all other pending requests get notified
922 * by smbXcli_conn_disconnect().
924 smbXcli_req_unset_pending(req
);
925 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
932 static void smbXcli_conn_received(struct tevent_req
*subreq
);
934 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
)
936 size_t num_pending
= talloc_array_length(conn
->pending
);
937 struct tevent_req
*req
;
938 struct smbXcli_req_state
*state
;
940 if (conn
->read_smb_req
!= NULL
) {
944 if (num_pending
== 0) {
945 if (conn
->smb2
.mid
< UINT64_MAX
) {
946 /* no more pending requests, so we are done for now */
951 * If there are no more SMB2 requests possible,
952 * because we are out of message ids,
953 * we need to disconnect.
955 smbXcli_conn_disconnect(conn
, NT_STATUS_CONNECTION_ABORTED
);
959 req
= conn
->pending
[0];
960 state
= tevent_req_data(req
, struct smbXcli_req_state
);
963 * We're the first ones, add the read_smb request that waits for the
964 * answer from the server
966 conn
->read_smb_req
= read_smb_send(conn
->pending
,
969 if (conn
->read_smb_req
== NULL
) {
972 tevent_req_set_callback(conn
->read_smb_req
, smbXcli_conn_received
, conn
);
976 void smbXcli_conn_disconnect(struct smbXcli_conn
*conn
, NTSTATUS status
)
978 struct smbXcli_session
*session
;
980 tevent_queue_stop(conn
->outgoing
);
982 if (conn
->read_fd
!= -1) {
983 close(conn
->read_fd
);
985 if (conn
->write_fd
!= -1) {
986 close(conn
->write_fd
);
991 session
= conn
->sessions
;
992 if (talloc_array_length(conn
->pending
) == 0) {
994 * if we do not have pending requests
995 * there is no need to update the channel_sequence
999 for (; session
; session
= session
->next
) {
1000 smb2cli_session_increment_channel_sequence(session
);
1004 * Cancel all pending requests. We do not do a for-loop walking
1005 * conn->pending because that array changes in
1006 * smbXcli_req_unset_pending.
1008 while (talloc_array_length(conn
->pending
) > 0) {
1009 struct tevent_req
*req
;
1010 struct smbXcli_req_state
*state
;
1011 struct tevent_req
**chain
;
1015 req
= conn
->pending
[0];
1016 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1018 if (state
->smb1
.chained_requests
== NULL
) {
1020 * We're dead. No point waiting for trans2
1023 state
->smb1
.mid
= 0;
1025 smbXcli_req_unset_pending(req
);
1027 if (NT_STATUS_IS_OK(status
)) {
1028 /* do not notify the callers */
1033 * we need to defer the callback, because we may notify
1034 * more then one caller.
1036 tevent_req_defer_callback(req
, state
->ev
);
1037 tevent_req_nterror(req
, status
);
1041 chain
= talloc_move(conn
, &state
->smb1
.chained_requests
);
1042 num_chained
= talloc_array_length(chain
);
1044 for (i
=0; i
<num_chained
; i
++) {
1046 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1049 * We're dead. No point waiting for trans2
1052 state
->smb1
.mid
= 0;
1054 smbXcli_req_unset_pending(req
);
1056 if (NT_STATUS_IS_OK(status
)) {
1057 /* do not notify the callers */
1062 * we need to defer the callback, because we may notify
1063 * more than one caller.
1065 tevent_req_defer_callback(req
, state
->ev
);
1066 tevent_req_nterror(req
, status
);
1073 * Fetch a smb request's mid. Only valid after the request has been sent by
1074 * smb1cli_req_send().
1076 uint16_t smb1cli_req_mid(struct tevent_req
*req
)
1078 struct smbXcli_req_state
*state
=
1079 tevent_req_data(req
,
1080 struct smbXcli_req_state
);
1082 if (state
->smb1
.mid
!= 0) {
1083 return state
->smb1
.mid
;
1086 return SVAL(state
->smb1
.hdr
, HDR_MID
);
1089 void smb1cli_req_set_mid(struct tevent_req
*req
, uint16_t mid
)
1091 struct smbXcli_req_state
*state
=
1092 tevent_req_data(req
,
1093 struct smbXcli_req_state
);
1095 state
->smb1
.mid
= mid
;
1098 uint32_t smb1cli_req_seqnum(struct tevent_req
*req
)
1100 struct smbXcli_req_state
*state
=
1101 tevent_req_data(req
,
1102 struct smbXcli_req_state
);
1104 return state
->smb1
.seqnum
;
1107 void smb1cli_req_set_seqnum(struct tevent_req
*req
, uint32_t seqnum
)
1109 struct smbXcli_req_state
*state
=
1110 tevent_req_data(req
,
1111 struct smbXcli_req_state
);
1113 state
->smb1
.seqnum
= seqnum
;
1116 static size_t smbXcli_iov_len(const struct iovec
*iov
, int count
)
1118 ssize_t ret
= iov_buflen(iov
, count
);
1120 /* Ignore the overflow case for now ... */
1124 static uint8_t *smbXcli_iov_concat(TALLOC_CTX
*mem_ctx
,
1125 const struct iovec
*iov
,
1131 buflen
= iov_buflen(iov
, count
);
1136 buf
= talloc_array(mem_ctx
, uint8_t, buflen
);
1141 iov_buf(iov
, count
, buf
, buflen
);
1146 static void smb1cli_req_flags(enum protocol_types protocol
,
1147 uint32_t smb1_capabilities
,
1148 uint8_t smb_command
,
1149 uint8_t additional_flags
,
1150 uint8_t clear_flags
,
1152 uint16_t additional_flags2
,
1153 uint16_t clear_flags2
,
1157 uint16_t flags2
= 0;
1159 if (protocol
>= PROTOCOL_LANMAN1
) {
1160 flags
|= FLAG_CASELESS_PATHNAMES
;
1161 flags
|= FLAG_CANONICAL_PATHNAMES
;
1164 if (protocol
>= PROTOCOL_LANMAN2
) {
1165 flags2
|= FLAGS2_LONG_PATH_COMPONENTS
;
1166 flags2
|= FLAGS2_EXTENDED_ATTRIBUTES
;
1169 if (protocol
>= PROTOCOL_NT1
) {
1170 flags2
|= FLAGS2_IS_LONG_NAME
;
1172 if (smb1_capabilities
& CAP_UNICODE
) {
1173 flags2
|= FLAGS2_UNICODE_STRINGS
;
1175 if (smb1_capabilities
& CAP_STATUS32
) {
1176 flags2
|= FLAGS2_32_BIT_ERROR_CODES
;
1178 if (smb1_capabilities
& CAP_EXTENDED_SECURITY
) {
1179 flags2
|= FLAGS2_EXTENDED_SECURITY
;
1183 flags
|= additional_flags
;
1184 flags
&= ~clear_flags
;
1185 flags2
|= additional_flags2
;
1186 flags2
&= ~clear_flags2
;
1192 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
);
1194 static bool smb1cli_req_cancel(struct tevent_req
*req
)
1196 struct smbXcli_req_state
*state
=
1197 tevent_req_data(req
,
1198 struct smbXcli_req_state
);
1203 struct tevent_req
*subreq
;
1206 flags
= CVAL(state
->smb1
.hdr
, HDR_FLG
);
1207 flags2
= SVAL(state
->smb1
.hdr
, HDR_FLG2
);
1208 pid
= SVAL(state
->smb1
.hdr
, HDR_PID
);
1209 pid
|= SVAL(state
->smb1
.hdr
, HDR_PIDHIGH
)<<16;
1210 mid
= SVAL(state
->smb1
.hdr
, HDR_MID
);
1212 subreq
= smb1cli_req_create(state
, state
->ev
,
1222 0, NULL
); /* bytes */
1223 if (subreq
== NULL
) {
1226 smb1cli_req_set_mid(subreq
, mid
);
1228 status
= smb1cli_req_chain_submit(&subreq
, 1);
1229 if (!NT_STATUS_IS_OK(status
)) {
1230 TALLOC_FREE(subreq
);
1233 smb1cli_req_set_mid(subreq
, 0);
1235 tevent_req_set_callback(subreq
, smb1cli_req_cancel_done
, NULL
);
1240 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
)
1242 /* we do not care about the result */
1243 TALLOC_FREE(subreq
);
1246 struct tevent_req
*smb1cli_req_create(TALLOC_CTX
*mem_ctx
,
1247 struct tevent_context
*ev
,
1248 struct smbXcli_conn
*conn
,
1249 uint8_t smb_command
,
1250 uint8_t additional_flags
,
1251 uint8_t clear_flags
,
1252 uint16_t additional_flags2
,
1253 uint16_t clear_flags2
,
1254 uint32_t timeout_msec
,
1256 struct smbXcli_tcon
*tcon
,
1257 struct smbXcli_session
*session
,
1258 uint8_t wct
, uint16_t *vwv
,
1260 struct iovec
*bytes_iov
)
1262 struct tevent_req
*req
;
1263 struct smbXcli_req_state
*state
;
1265 uint16_t flags2
= 0;
1270 if (iov_count
> MAX_SMB_IOV
) {
1272 * Should not happen :-)
1277 req
= tevent_req_create(mem_ctx
, &state
,
1278 struct smbXcli_req_state
);
1284 state
->session
= session
;
1288 uid
= session
->smb1
.session_id
;
1292 tid
= tcon
->smb1
.tcon_id
;
1294 if (tcon
->fs_attributes
& FILE_CASE_SENSITIVE_SEARCH
) {
1295 clear_flags
|= FLAG_CASELESS_PATHNAMES
;
1297 /* Default setting, case insensitive. */
1298 additional_flags
|= FLAG_CASELESS_PATHNAMES
;
1301 if (smbXcli_conn_dfs_supported(conn
) &&
1302 smbXcli_tcon_is_dfs_share(tcon
))
1304 additional_flags2
|= FLAGS2_DFS_PATHNAMES
;
1308 state
->smb1
.recv_cmd
= 0xFF;
1309 state
->smb1
.recv_status
= NT_STATUS_INTERNAL_ERROR
;
1310 state
->smb1
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
1311 if (state
->smb1
.recv_iov
== NULL
) {
1316 smb1cli_req_flags(conn
->protocol
,
1317 conn
->smb1
.capabilities
,
1326 SIVAL(state
->smb1
.hdr
, 0, SMB_MAGIC
);
1327 SCVAL(state
->smb1
.hdr
, HDR_COM
, smb_command
);
1328 SIVAL(state
->smb1
.hdr
, HDR_RCLS
, NT_STATUS_V(NT_STATUS_OK
));
1329 SCVAL(state
->smb1
.hdr
, HDR_FLG
, flags
);
1330 SSVAL(state
->smb1
.hdr
, HDR_FLG2
, flags2
);
1331 SSVAL(state
->smb1
.hdr
, HDR_PIDHIGH
, pid
>> 16);
1332 SSVAL(state
->smb1
.hdr
, HDR_TID
, tid
);
1333 SSVAL(state
->smb1
.hdr
, HDR_PID
, pid
);
1334 SSVAL(state
->smb1
.hdr
, HDR_UID
, uid
);
1335 SSVAL(state
->smb1
.hdr
, HDR_MID
, 0); /* this comes later */
1336 SCVAL(state
->smb1
.hdr
, HDR_WCT
, wct
);
1338 state
->smb1
.vwv
= vwv
;
1340 num_bytes
= iov_buflen(bytes_iov
, iov_count
);
1341 if (num_bytes
== -1) {
1343 * I'd love to add a check for num_bytes<=UINT16_MAX here, but
1344 * the smbclient->samba connections can lie and transfer more.
1350 SSVAL(state
->smb1
.bytecount_buf
, 0, num_bytes
);
1352 state
->smb1
.iov
[0].iov_base
= (void *)state
->length_hdr
;
1353 state
->smb1
.iov
[0].iov_len
= sizeof(state
->length_hdr
);
1354 state
->smb1
.iov
[1].iov_base
= (void *)state
->smb1
.hdr
;
1355 state
->smb1
.iov
[1].iov_len
= sizeof(state
->smb1
.hdr
);
1356 state
->smb1
.iov
[2].iov_base
= (void *)state
->smb1
.vwv
;
1357 state
->smb1
.iov
[2].iov_len
= wct
* sizeof(uint16_t);
1358 state
->smb1
.iov
[3].iov_base
= (void *)state
->smb1
.bytecount_buf
;
1359 state
->smb1
.iov
[3].iov_len
= sizeof(uint16_t);
1361 if (iov_count
!= 0) {
1362 memcpy(&state
->smb1
.iov
[4], bytes_iov
,
1363 iov_count
* sizeof(*bytes_iov
));
1365 state
->smb1
.iov_count
= iov_count
+ 4;
1367 if (timeout_msec
> 0) {
1368 struct timeval endtime
;
1370 endtime
= timeval_current_ofs_msec(timeout_msec
);
1371 if (!tevent_req_set_endtime(req
, ev
, endtime
)) {
1376 switch (smb_command
) {
1380 state
->one_way
= true;
1383 state
->one_way
= true;
1384 state
->smb1
.one_way_seqnum
= true;
1388 (CVAL(vwv
+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE
)) {
1389 state
->one_way
= true;
1397 static NTSTATUS
smb1cli_conn_signv(struct smbXcli_conn
*conn
,
1398 struct iovec
*iov
, int iov_count
,
1400 bool one_way_seqnum
)
1402 TALLOC_CTX
*frame
= NULL
;
1406 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1407 * iovec directly. MD5Update would do that just fine.
1410 if (iov_count
< 4) {
1411 return NT_STATUS_INVALID_PARAMETER_MIX
;
1413 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1414 return NT_STATUS_INVALID_PARAMETER_MIX
;
1416 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1417 return NT_STATUS_INVALID_PARAMETER_MIX
;
1419 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1420 return NT_STATUS_INVALID_PARAMETER_MIX
;
1422 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1423 return NT_STATUS_INVALID_PARAMETER_MIX
;
1426 frame
= talloc_stackframe();
1428 buf
= smbXcli_iov_concat(frame
, &iov
[1], iov_count
- 1);
1430 return NT_STATUS_NO_MEMORY
;
1433 *seqnum
= smb_signing_next_seqnum(conn
->smb1
.signing
,
1435 smb_signing_sign_pdu(conn
->smb1
.signing
,
1436 buf
, talloc_get_size(buf
),
1438 memcpy(iov
[1].iov_base
, buf
, iov
[1].iov_len
);
1441 return NT_STATUS_OK
;
1444 static void smb1cli_req_writev_done(struct tevent_req
*subreq
);
1445 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1446 TALLOC_CTX
*tmp_mem
,
1449 static NTSTATUS
smb1cli_req_writev_submit(struct tevent_req
*req
,
1450 struct smbXcli_req_state
*state
,
1451 struct iovec
*iov
, int iov_count
)
1453 struct tevent_req
*subreq
;
1459 if (!smbXcli_conn_is_connected(state
->conn
)) {
1460 return NT_STATUS_CONNECTION_DISCONNECTED
;
1463 if (state
->conn
->protocol
> PROTOCOL_NT1
) {
1464 return NT_STATUS_REVISION_MISMATCH
;
1467 if (iov_count
< 4) {
1468 return NT_STATUS_INVALID_PARAMETER_MIX
;
1470 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1471 return NT_STATUS_INVALID_PARAMETER_MIX
;
1473 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1474 return NT_STATUS_INVALID_PARAMETER_MIX
;
1476 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1477 return NT_STATUS_INVALID_PARAMETER_MIX
;
1479 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1480 return NT_STATUS_INVALID_PARAMETER_MIX
;
1483 cmd
= CVAL(iov
[1].iov_base
, HDR_COM
);
1484 if (cmd
== SMBreadBraw
) {
1485 if (smbXcli_conn_has_async_calls(state
->conn
)) {
1486 return NT_STATUS_INVALID_PARAMETER_MIX
;
1488 state
->conn
->smb1
.read_braw_req
= req
;
1491 if (state
->smb1
.mid
!= 0) {
1492 mid
= state
->smb1
.mid
;
1494 mid
= smb1cli_alloc_mid(state
->conn
);
1496 SSVAL(iov
[1].iov_base
, HDR_MID
, mid
);
1498 nbtlen
= iov_buflen(&iov
[1], iov_count
-1);
1499 if ((nbtlen
== -1) || (nbtlen
> 0x1FFFF)) {
1500 return NT_STATUS_INVALID_PARAMETER_MIX
;
1503 _smb_setlen_nbt(iov
[0].iov_base
, nbtlen
);
1505 status
= smb1cli_conn_signv(state
->conn
, iov
, iov_count
,
1506 &state
->smb1
.seqnum
,
1507 state
->smb1
.one_way_seqnum
);
1509 if (!NT_STATUS_IS_OK(status
)) {
1514 * If we supported multiple encrytion contexts
1515 * here we'd look up based on tid.
1517 if (common_encryption_on(state
->conn
->smb1
.trans_enc
)) {
1518 char *buf
, *enc_buf
;
1520 buf
= (char *)smbXcli_iov_concat(talloc_tos(), iov
, iov_count
);
1522 return NT_STATUS_NO_MEMORY
;
1524 status
= common_encrypt_buffer(state
->conn
->smb1
.trans_enc
,
1525 (char *)buf
, &enc_buf
);
1527 if (!NT_STATUS_IS_OK(status
)) {
1528 DEBUG(0, ("Error in encrypting client message: %s\n",
1529 nt_errstr(status
)));
1532 buf
= (char *)talloc_memdup(state
, enc_buf
,
1533 smb_len_nbt(enc_buf
)+4);
1536 return NT_STATUS_NO_MEMORY
;
1538 iov
[0].iov_base
= (void *)buf
;
1539 iov
[0].iov_len
= talloc_get_size(buf
);
1543 if (state
->conn
->dispatch_incoming
== NULL
) {
1544 state
->conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
1547 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
1549 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
1550 state
->conn
->write_fd
, false, iov
, iov_count
);
1551 if (subreq
== NULL
) {
1552 return NT_STATUS_NO_MEMORY
;
1554 tevent_req_set_callback(subreq
, smb1cli_req_writev_done
, req
);
1555 return NT_STATUS_OK
;
1558 struct tevent_req
*smb1cli_req_send(TALLOC_CTX
*mem_ctx
,
1559 struct tevent_context
*ev
,
1560 struct smbXcli_conn
*conn
,
1561 uint8_t smb_command
,
1562 uint8_t additional_flags
,
1563 uint8_t clear_flags
,
1564 uint16_t additional_flags2
,
1565 uint16_t clear_flags2
,
1566 uint32_t timeout_msec
,
1568 struct smbXcli_tcon
*tcon
,
1569 struct smbXcli_session
*session
,
1570 uint8_t wct
, uint16_t *vwv
,
1572 const uint8_t *bytes
)
1574 struct tevent_req
*req
;
1578 iov
.iov_base
= discard_const_p(void, bytes
);
1579 iov
.iov_len
= num_bytes
;
1581 req
= smb1cli_req_create(mem_ctx
, ev
, conn
, smb_command
,
1582 additional_flags
, clear_flags
,
1583 additional_flags2
, clear_flags2
,
1590 if (!tevent_req_is_in_progress(req
)) {
1591 return tevent_req_post(req
, ev
);
1593 status
= smb1cli_req_chain_submit(&req
, 1);
1594 if (tevent_req_nterror(req
, status
)) {
1595 return tevent_req_post(req
, ev
);
1600 static void smb1cli_req_writev_done(struct tevent_req
*subreq
)
1602 struct tevent_req
*req
=
1603 tevent_req_callback_data(subreq
,
1605 struct smbXcli_req_state
*state
=
1606 tevent_req_data(req
,
1607 struct smbXcli_req_state
);
1611 nwritten
= writev_recv(subreq
, &err
);
1612 TALLOC_FREE(subreq
);
1613 if (nwritten
== -1) {
1614 NTSTATUS status
= map_nt_error_from_unix_common(err
);
1615 smbXcli_conn_disconnect(state
->conn
, status
);
1619 if (state
->one_way
) {
1620 state
->inbuf
= NULL
;
1621 tevent_req_done(req
);
1625 if (!smbXcli_req_set_pending(req
)) {
1626 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1631 static void smbXcli_conn_received(struct tevent_req
*subreq
)
1633 struct smbXcli_conn
*conn
=
1634 tevent_req_callback_data(subreq
,
1635 struct smbXcli_conn
);
1636 TALLOC_CTX
*frame
= talloc_stackframe();
1642 if (subreq
!= conn
->read_smb_req
) {
1643 DEBUG(1, ("Internal error: cli_smb_received called with "
1644 "unexpected subreq\n"));
1645 smbXcli_conn_disconnect(conn
, NT_STATUS_INTERNAL_ERROR
);
1649 conn
->read_smb_req
= NULL
;
1651 received
= read_smb_recv(subreq
, frame
, &inbuf
, &err
);
1652 TALLOC_FREE(subreq
);
1653 if (received
== -1) {
1654 status
= map_nt_error_from_unix_common(err
);
1655 smbXcli_conn_disconnect(conn
, status
);
1660 status
= conn
->dispatch_incoming(conn
, frame
, inbuf
);
1662 if (NT_STATUS_IS_OK(status
)) {
1664 * We should not do any more processing
1665 * as the dispatch function called
1666 * tevent_req_done().
1671 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1673 * We got an error, so notify all pending requests
1675 smbXcli_conn_disconnect(conn
, status
);
1680 * We got NT_STATUS_RETRY, so we may ask for a
1681 * next incoming pdu.
1683 if (!smbXcli_conn_receive_next(conn
)) {
1684 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
1688 static NTSTATUS
smb1cli_inbuf_parse_chain(uint8_t *buf
, TALLOC_CTX
*mem_ctx
,
1689 struct iovec
**piov
, int *pnum_iov
)
1700 size_t min_size
= MIN_SMB_SIZE
;
1702 buflen
= smb_len_tcp(buf
);
1705 hdr
= buf
+ NBT_HDR_SIZE
;
1707 status
= smb1cli_pull_raw_error(hdr
);
1708 if (NT_STATUS_IS_ERR(status
)) {
1710 * This is an ugly hack to support OS/2
1711 * which skips the byte_count in the DATA block
1712 * on some error responses.
1716 min_size
-= sizeof(uint16_t);
1719 if (buflen
< min_size
) {
1720 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1724 * This returns iovec elements in the following order:
1739 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
1741 return NT_STATUS_NO_MEMORY
;
1743 iov
[0].iov_base
= hdr
;
1744 iov
[0].iov_len
= HDR_WCT
;
1747 cmd
= CVAL(hdr
, HDR_COM
);
1751 size_t len
= buflen
- taken
;
1753 struct iovec
*iov_tmp
;
1760 * we need at least WCT
1762 needed
= sizeof(uint8_t);
1764 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1765 __location__
, (int)len
, (int)needed
));
1770 * Now we check if the specified words are there
1772 wct
= CVAL(hdr
, wct_ofs
);
1773 needed
+= wct
* sizeof(uint16_t);
1775 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1776 __location__
, (int)len
, (int)needed
));
1780 if ((num_iov
== 1) &&
1782 NT_STATUS_IS_ERR(status
))
1785 * This is an ugly hack to support OS/2
1786 * which skips the byte_count in the DATA block
1787 * on some error responses.
1791 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
1793 if (iov_tmp
== NULL
) {
1795 return NT_STATUS_NO_MEMORY
;
1798 cur
= &iov
[num_iov
];
1802 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
1804 cur
[1].iov_base
= cur
[0].iov_base
;
1811 * we need at least BCC
1813 needed
+= sizeof(uint16_t);
1815 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1816 __location__
, (int)len
, (int)needed
));
1821 * Now we check if the specified bytes are there
1823 bcc_ofs
= wct_ofs
+ sizeof(uint8_t) + wct
* sizeof(uint16_t);
1824 bcc
= SVAL(hdr
, bcc_ofs
);
1825 needed
+= bcc
* sizeof(uint8_t);
1827 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1828 __location__
, (int)len
, (int)needed
));
1833 * we allocate 2 iovec structures for words and bytes
1835 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
1837 if (iov_tmp
== NULL
) {
1839 return NT_STATUS_NO_MEMORY
;
1842 cur
= &iov
[num_iov
];
1845 cur
[0].iov_len
= wct
* sizeof(uint16_t);
1846 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
1847 cur
[1].iov_len
= bcc
* sizeof(uint8_t);
1848 cur
[1].iov_base
= hdr
+ (bcc_ofs
+ sizeof(uint16_t));
1852 if (!smb1cli_is_andx_req(cmd
)) {
1854 * If the current command does not have AndX chanining
1860 if (wct
== 0 && bcc
== 0) {
1862 * An empty response also ends the chain,
1863 * most likely with an error.
1869 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1870 __location__
, (int)wct
, (int)cmd
));
1873 cmd
= CVAL(cur
[0].iov_base
, 0);
1876 * If it is the end of the chain we are also done.
1880 wct_ofs
= SVAL(cur
[0].iov_base
, 2);
1882 if (wct_ofs
< taken
) {
1883 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1885 if (wct_ofs
> buflen
) {
1886 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1890 * we consumed everything up to the start of the next
1896 remaining
= buflen
- taken
;
1898 if (remaining
> 0 && num_iov
>= 3) {
1900 * The last DATA block gets the remaining
1901 * bytes, this is needed to support
1902 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1904 iov
[num_iov
-1].iov_len
+= remaining
;
1908 *pnum_iov
= num_iov
;
1909 return NT_STATUS_OK
;
1913 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1916 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1917 TALLOC_CTX
*tmp_mem
,
1920 struct tevent_req
*req
;
1921 struct smbXcli_req_state
*state
;
1928 uint8_t *inhdr
= inbuf
+ NBT_HDR_SIZE
;
1929 size_t len
= smb_len_tcp(inbuf
);
1930 struct iovec
*iov
= NULL
;
1932 struct tevent_req
**chain
= NULL
;
1933 size_t num_chained
= 0;
1934 size_t num_responses
= 0;
1936 if (conn
->smb1
.read_braw_req
!= NULL
) {
1937 req
= conn
->smb1
.read_braw_req
;
1938 conn
->smb1
.read_braw_req
= NULL
;
1939 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1941 smbXcli_req_unset_pending(req
);
1943 if (state
->smb1
.recv_iov
== NULL
) {
1945 * For requests with more than
1946 * one response, we have to readd the
1949 state
->smb1
.recv_iov
= talloc_zero_array(state
,
1952 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
1953 return NT_STATUS_OK
;
1957 state
->smb1
.recv_iov
[0].iov_base
= (void *)(inhdr
);
1958 state
->smb1
.recv_iov
[0].iov_len
= len
;
1959 ZERO_STRUCT(state
->smb1
.recv_iov
[1]);
1960 ZERO_STRUCT(state
->smb1
.recv_iov
[2]);
1962 state
->smb1
.recv_cmd
= SMBreadBraw
;
1963 state
->smb1
.recv_status
= NT_STATUS_OK
;
1964 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
1966 tevent_req_done(req
);
1967 return NT_STATUS_OK
;
1970 if ((IVAL(inhdr
, 0) != SMB_MAGIC
) /* 0xFF"SMB" */
1971 && (SVAL(inhdr
, 0) != 0x45ff)) /* 0xFF"E" */ {
1972 DEBUG(10, ("Got non-SMB PDU\n"));
1973 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1977 * If we supported multiple encrytion contexts
1978 * here we'd look up based on tid.
1980 if (common_encryption_on(conn
->smb1
.trans_enc
)
1981 && (CVAL(inbuf
, 0) == 0)) {
1982 uint16_t enc_ctx_num
;
1984 status
= get_enc_ctx_num(inbuf
, &enc_ctx_num
);
1985 if (!NT_STATUS_IS_OK(status
)) {
1986 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1987 nt_errstr(status
)));
1991 if (enc_ctx_num
!= conn
->smb1
.trans_enc
->enc_ctx_num
) {
1992 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1994 conn
->smb1
.trans_enc
->enc_ctx_num
));
1995 return NT_STATUS_INVALID_HANDLE
;
1998 status
= common_decrypt_buffer(conn
->smb1
.trans_enc
,
2000 if (!NT_STATUS_IS_OK(status
)) {
2001 DEBUG(10, ("common_decrypt_buffer returned %s\n",
2002 nt_errstr(status
)));
2005 inhdr
= inbuf
+ NBT_HDR_SIZE
;
2006 len
= smb_len_nbt(inbuf
);
2009 mid
= SVAL(inhdr
, HDR_MID
);
2010 num_pending
= talloc_array_length(conn
->pending
);
2012 for (i
=0; i
<num_pending
; i
++) {
2013 if (mid
== smb1cli_req_mid(conn
->pending
[i
])) {
2017 if (i
== num_pending
) {
2018 /* Dump unexpected reply */
2019 return NT_STATUS_RETRY
;
2022 oplock_break
= false;
2024 if (mid
== 0xffff) {
2026 * Paranoia checks that this is really an oplock break request.
2028 oplock_break
= (len
== 51); /* hdr + 8 words */
2029 oplock_break
&= ((CVAL(inhdr
, HDR_FLG
) & FLAG_REPLY
) == 0);
2030 oplock_break
&= (CVAL(inhdr
, HDR_COM
) == SMBlockingX
);
2031 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(6)) == 0);
2032 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(7)) == 0);
2034 if (!oplock_break
) {
2035 /* Dump unexpected reply */
2036 return NT_STATUS_RETRY
;
2040 req
= conn
->pending
[i
];
2041 state
= tevent_req_data(req
, struct smbXcli_req_state
);
2043 if (!oplock_break
/* oplock breaks are not signed */
2044 && !smb_signing_check_pdu(conn
->smb1
.signing
,
2045 inhdr
, len
, state
->smb1
.seqnum
+1)) {
2046 DEBUG(10, ("cli_check_sign_mac failed\n"));
2047 return NT_STATUS_ACCESS_DENIED
;
2050 status
= smb1cli_inbuf_parse_chain(inbuf
, tmp_mem
,
2052 if (!NT_STATUS_IS_OK(status
)) {
2053 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2054 nt_errstr(status
)));
2058 cmd
= CVAL(inhdr
, HDR_COM
);
2059 status
= smb1cli_pull_raw_error(inhdr
);
2061 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
) &&
2062 (state
->session
!= NULL
) && state
->session
->disconnect_expired
)
2065 * this should be a short term hack
2066 * until the upper layers have implemented
2067 * re-authentication.
2072 if (state
->smb1
.chained_requests
== NULL
) {
2074 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2077 smbXcli_req_unset_pending(req
);
2079 if (state
->smb1
.recv_iov
== NULL
) {
2081 * For requests with more than
2082 * one response, we have to readd the
2085 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2088 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2089 return NT_STATUS_OK
;
2093 state
->smb1
.recv_cmd
= cmd
;
2094 state
->smb1
.recv_status
= status
;
2095 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
2097 state
->smb1
.recv_iov
[0] = iov
[0];
2098 state
->smb1
.recv_iov
[1] = iov
[1];
2099 state
->smb1
.recv_iov
[2] = iov
[2];
2101 if (talloc_array_length(conn
->pending
) == 0) {
2102 tevent_req_done(req
);
2103 return NT_STATUS_OK
;
2106 tevent_req_defer_callback(req
, state
->ev
);
2107 tevent_req_done(req
);
2108 return NT_STATUS_RETRY
;
2111 chain
= talloc_move(tmp_mem
, &state
->smb1
.chained_requests
);
2112 num_chained
= talloc_array_length(chain
);
2113 num_responses
= (num_iov
- 1)/2;
2115 if (num_responses
> num_chained
) {
2116 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2119 for (i
=0; i
<num_chained
; i
++) {
2120 size_t iov_idx
= 1 + (i
*2);
2121 struct iovec
*cur
= &iov
[iov_idx
];
2125 state
= tevent_req_data(req
, struct smbXcli_req_state
);
2127 smbXcli_req_unset_pending(req
);
2130 * as we finish multiple requests here
2131 * we need to defer the callbacks as
2132 * they could destroy our current stack state.
2134 tevent_req_defer_callback(req
, state
->ev
);
2136 if (i
>= num_responses
) {
2137 tevent_req_nterror(req
, NT_STATUS_REQUEST_ABORTED
);
2141 if (state
->smb1
.recv_iov
== NULL
) {
2143 * For requests with more than
2144 * one response, we have to readd the
2147 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2150 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2155 state
->smb1
.recv_cmd
= cmd
;
2157 if (i
== (num_responses
- 1)) {
2159 * The last request in the chain gets the status
2161 state
->smb1
.recv_status
= status
;
2163 cmd
= CVAL(cur
[0].iov_base
, 0);
2164 state
->smb1
.recv_status
= NT_STATUS_OK
;
2167 state
->inbuf
= inbuf
;
2170 * Note: here we use talloc_reference() in a way
2171 * that does not expose it to the caller.
2173 inbuf_ref
= talloc_reference(state
->smb1
.recv_iov
, inbuf
);
2174 if (tevent_req_nomem(inbuf_ref
, req
)) {
2178 /* copy the related buffers */
2179 state
->smb1
.recv_iov
[0] = iov
[0];
2180 state
->smb1
.recv_iov
[1] = cur
[0];
2181 state
->smb1
.recv_iov
[2] = cur
[1];
2183 tevent_req_done(req
);
2186 return NT_STATUS_RETRY
;
2189 NTSTATUS
smb1cli_req_recv(struct tevent_req
*req
,
2190 TALLOC_CTX
*mem_ctx
,
2191 struct iovec
**piov
,
2195 uint32_t *pvwv_offset
,
2196 uint32_t *pnum_bytes
,
2198 uint32_t *pbytes_offset
,
2200 const struct smb1cli_req_expected_response
*expected
,
2201 size_t num_expected
)
2203 struct smbXcli_req_state
*state
=
2204 tevent_req_data(req
,
2205 struct smbXcli_req_state
);
2206 NTSTATUS status
= NT_STATUS_OK
;
2207 struct iovec
*recv_iov
= NULL
;
2208 uint8_t *hdr
= NULL
;
2210 uint32_t vwv_offset
= 0;
2211 uint16_t *vwv
= NULL
;
2212 uint32_t num_bytes
= 0;
2213 uint32_t bytes_offset
= 0;
2214 uint8_t *bytes
= NULL
;
2216 bool found_status
= false;
2217 bool found_size
= false;
2231 if (pvwv_offset
!= NULL
) {
2234 if (pnum_bytes
!= NULL
) {
2237 if (pbytes
!= NULL
) {
2240 if (pbytes_offset
!= NULL
) {
2243 if (pinbuf
!= NULL
) {
2247 if (state
->inbuf
!= NULL
) {
2248 recv_iov
= state
->smb1
.recv_iov
;
2249 state
->smb1
.recv_iov
= NULL
;
2250 if (state
->smb1
.recv_cmd
!= SMBreadBraw
) {
2251 hdr
= (uint8_t *)recv_iov
[0].iov_base
;
2252 wct
= recv_iov
[1].iov_len
/2;
2253 vwv
= (uint16_t *)recv_iov
[1].iov_base
;
2254 vwv_offset
= PTR_DIFF(vwv
, hdr
);
2255 num_bytes
= recv_iov
[2].iov_len
;
2256 bytes
= (uint8_t *)recv_iov
[2].iov_base
;
2257 bytes_offset
= PTR_DIFF(bytes
, hdr
);
2261 if (tevent_req_is_nterror(req
, &status
)) {
2262 for (i
=0; i
< num_expected
; i
++) {
2263 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2264 found_status
= true;
2270 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
2276 if (num_expected
== 0) {
2277 found_status
= true;
2281 status
= state
->smb1
.recv_status
;
2283 for (i
=0; i
< num_expected
; i
++) {
2284 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2288 found_status
= true;
2289 if (expected
[i
].wct
== 0) {
2294 if (expected
[i
].wct
== wct
) {
2300 if (!found_status
) {
2305 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2309 *piov
= talloc_move(mem_ctx
, &recv_iov
);
2321 if (pvwv_offset
!= NULL
) {
2322 *pvwv_offset
= vwv_offset
;
2324 if (pnum_bytes
!= NULL
) {
2325 *pnum_bytes
= num_bytes
;
2327 if (pbytes
!= NULL
) {
2330 if (pbytes_offset
!= NULL
) {
2331 *pbytes_offset
= bytes_offset
;
2333 if (pinbuf
!= NULL
) {
2334 *pinbuf
= state
->inbuf
;
2340 size_t smb1cli_req_wct_ofs(struct tevent_req
**reqs
, int num_reqs
)
2347 for (i
=0; i
<num_reqs
; i
++) {
2348 struct smbXcli_req_state
*state
;
2349 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2350 wct_ofs
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2351 state
->smb1
.iov_count
-2);
2352 wct_ofs
= (wct_ofs
+ 3) & ~3;
2357 NTSTATUS
smb1cli_req_chain_submit(struct tevent_req
**reqs
, int num_reqs
)
2359 struct smbXcli_req_state
*first_state
=
2360 tevent_req_data(reqs
[0],
2361 struct smbXcli_req_state
);
2362 struct smbXcli_req_state
*state
;
2364 size_t chain_padding
= 0;
2366 struct iovec
*iov
= NULL
;
2367 struct iovec
*this_iov
;
2371 if (num_reqs
== 1) {
2372 return smb1cli_req_writev_submit(reqs
[0], first_state
,
2373 first_state
->smb1
.iov
,
2374 first_state
->smb1
.iov_count
);
2378 for (i
=0; i
<num_reqs
; i
++) {
2379 if (!tevent_req_is_in_progress(reqs
[i
])) {
2380 return NT_STATUS_INTERNAL_ERROR
;
2383 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2385 if (state
->smb1
.iov_count
< 4) {
2386 return NT_STATUS_INVALID_PARAMETER_MIX
;
2391 * The NBT and SMB header
2404 iovlen
+= state
->smb1
.iov_count
- 2;
2407 iov
= talloc_zero_array(first_state
, struct iovec
, iovlen
);
2409 return NT_STATUS_NO_MEMORY
;
2412 first_state
->smb1
.chained_requests
= (struct tevent_req
**)talloc_memdup(
2413 first_state
, reqs
, sizeof(*reqs
) * num_reqs
);
2414 if (first_state
->smb1
.chained_requests
== NULL
) {
2416 return NT_STATUS_NO_MEMORY
;
2419 wct_offset
= HDR_WCT
;
2422 for (i
=0; i
<num_reqs
; i
++) {
2423 size_t next_padding
= 0;
2426 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2428 if (i
< num_reqs
-1) {
2429 if (!smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))
2430 || CVAL(state
->smb1
.hdr
, HDR_WCT
) < 2) {
2432 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2433 return NT_STATUS_INVALID_PARAMETER_MIX
;
2437 wct_offset
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2438 state
->smb1
.iov_count
-2) + 1;
2439 if ((wct_offset
% 4) != 0) {
2440 next_padding
= 4 - (wct_offset
% 4);
2442 wct_offset
+= next_padding
;
2443 vwv
= state
->smb1
.vwv
;
2445 if (i
< num_reqs
-1) {
2446 struct smbXcli_req_state
*next_state
=
2447 tevent_req_data(reqs
[i
+1],
2448 struct smbXcli_req_state
);
2449 SCVAL(vwv
+0, 0, CVAL(next_state
->smb1
.hdr
, HDR_COM
));
2451 SSVAL(vwv
+1, 0, wct_offset
);
2452 } else if (smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))) {
2453 /* properly end the chain */
2454 SCVAL(vwv
+0, 0, 0xff);
2455 SCVAL(vwv
+0, 1, 0xff);
2461 * The NBT and SMB header
2463 this_iov
[0] = state
->smb1
.iov
[0];
2464 this_iov
[1] = state
->smb1
.iov
[1];
2468 * This one is a bit subtle. We have to add
2469 * chain_padding bytes between the requests, and we
2470 * have to also include the wct field of the
2471 * subsequent requests. We use the subsequent header
2472 * for the padding, it contains the wct field in its
2475 this_iov
[0].iov_len
= chain_padding
+1;
2476 this_iov
[0].iov_base
= (void *)&state
->smb1
.hdr
[
2477 sizeof(state
->smb1
.hdr
) - this_iov
[0].iov_len
];
2478 memset(this_iov
[0].iov_base
, 0, this_iov
[0].iov_len
-1);
2483 * copy the words and bytes
2485 memcpy(this_iov
, state
->smb1
.iov
+2,
2486 sizeof(struct iovec
) * (state
->smb1
.iov_count
-2));
2487 this_iov
+= state
->smb1
.iov_count
- 2;
2488 chain_padding
= next_padding
;
2491 nbt_len
= iov_buflen(&iov
[1], iovlen
-1);
2492 if ((nbt_len
== -1) || (nbt_len
> first_state
->conn
->smb1
.max_xmit
)) {
2494 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2495 return NT_STATUS_INVALID_PARAMETER_MIX
;
2498 status
= smb1cli_req_writev_submit(reqs
[0], first_state
, iov
, iovlen
);
2499 if (!NT_STATUS_IS_OK(status
)) {
2501 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2505 return NT_STATUS_OK
;
2508 bool smbXcli_conn_has_async_calls(struct smbXcli_conn
*conn
)
2510 return ((tevent_queue_length(conn
->outgoing
) != 0)
2511 || (talloc_array_length(conn
->pending
) != 0));
2514 bool smbXcli_conn_dfs_supported(struct smbXcli_conn
*conn
)
2516 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
2517 return (smb2cli_conn_server_capabilities(conn
) & SMB2_CAP_DFS
);
2520 return (smb1cli_conn_capabilities(conn
) & CAP_DFS
);
2523 bool smb2cli_conn_req_possible(struct smbXcli_conn
*conn
, uint32_t *max_dyn_len
)
2525 uint16_t credits
= 1;
2527 if (conn
->smb2
.cur_credits
== 0) {
2528 if (max_dyn_len
!= NULL
) {
2534 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2535 credits
= conn
->smb2
.cur_credits
;
2538 if (max_dyn_len
!= NULL
) {
2539 *max_dyn_len
= credits
* 65536;
2545 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn
*conn
)
2547 return conn
->smb2
.server
.capabilities
;
2550 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
2552 return conn
->smb2
.server
.security_mode
;
2555 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn
*conn
)
2557 return conn
->smb2
.server
.max_trans_size
;
2560 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn
*conn
)
2562 return conn
->smb2
.server
.max_read_size
;
2565 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn
*conn
)
2567 return conn
->smb2
.server
.max_write_size
;
2570 void smb2cli_conn_set_max_credits(struct smbXcli_conn
*conn
,
2571 uint16_t max_credits
)
2573 conn
->smb2
.max_credits
= max_credits
;
2576 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
);
2578 static bool smb2cli_req_cancel(struct tevent_req
*req
)
2580 struct smbXcli_req_state
*state
=
2581 tevent_req_data(req
,
2582 struct smbXcli_req_state
);
2583 uint32_t flags
= IVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
2584 uint64_t mid
= BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
);
2585 uint64_t aid
= BVAL(state
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
);
2586 struct smbXcli_tcon
*tcon
= state
->tcon
;
2587 struct smbXcli_session
*session
= state
->session
;
2588 uint8_t *fixed
= state
->smb2
.pad
;
2589 uint16_t fixed_len
= 4;
2590 struct tevent_req
*subreq
;
2591 struct smbXcli_req_state
*substate
;
2594 SSVAL(fixed
, 0, 0x04);
2597 subreq
= smb2cli_req_create(state
, state
->ev
,
2605 if (subreq
== NULL
) {
2608 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
2611 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2612 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2614 flags
&= SMB2_HDR_FLAG_ASYNC
;
2616 if (flags
& SMB2_HDR_FLAG_ASYNC
) {
2620 SIVAL(substate
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
2621 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
2622 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, aid
);
2624 status
= smb2cli_req_compound_submit(&subreq
, 1);
2625 if (!NT_STATUS_IS_OK(status
)) {
2626 TALLOC_FREE(subreq
);
2630 tevent_req_set_callback(subreq
, smb2cli_req_cancel_done
, NULL
);
2635 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
)
2637 /* we do not care about the result */
2638 TALLOC_FREE(subreq
);
2641 struct tevent_req
*smb2cli_req_create(TALLOC_CTX
*mem_ctx
,
2642 struct tevent_context
*ev
,
2643 struct smbXcli_conn
*conn
,
2645 uint32_t additional_flags
,
2646 uint32_t clear_flags
,
2647 uint32_t timeout_msec
,
2648 struct smbXcli_tcon
*tcon
,
2649 struct smbXcli_session
*session
,
2650 const uint8_t *fixed
,
2654 uint32_t max_dyn_len
)
2656 struct tevent_req
*req
;
2657 struct smbXcli_req_state
*state
;
2661 bool use_channel_sequence
= false;
2662 uint16_t channel_sequence
= 0;
2663 bool use_replay_flag
= false;
2665 req
= tevent_req_create(mem_ctx
, &state
,
2666 struct smbXcli_req_state
);
2673 state
->session
= session
;
2676 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_PERSISTENT_HANDLES
) {
2677 use_channel_sequence
= true;
2678 } else if (conn
->smb2
.server
.capabilities
& SMB2_CAP_MULTI_CHANNEL
) {
2679 use_channel_sequence
= true;
2682 if (smbXcli_conn_protocol(conn
) >= PROTOCOL_SMB3_00
) {
2683 use_replay_flag
= true;
2687 uid
= session
->smb2
->session_id
;
2689 if (use_channel_sequence
) {
2690 channel_sequence
= session
->smb2
->channel_sequence
;
2693 if (use_replay_flag
&& session
->smb2
->replay_active
) {
2694 additional_flags
|= SMB2_HDR_FLAG_REPLAY_OPERATION
;
2697 state
->smb2
.should_sign
= session
->smb2
->should_sign
;
2698 state
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
2700 if (cmd
== SMB2_OP_SESSSETUP
&&
2701 session
->smb2_channel
.signing_key
.length
== 0 &&
2702 session
->smb2
->signing_key
.length
!= 0)
2705 * a session bind needs to be signed
2707 state
->smb2
.should_sign
= true;
2710 if (cmd
== SMB2_OP_SESSSETUP
&&
2711 session
->smb2_channel
.signing_key
.length
== 0) {
2712 state
->smb2
.should_encrypt
= false;
2715 if (additional_flags
& SMB2_HDR_FLAG_SIGNED
) {
2716 if (session
->smb2_channel
.signing_key
.length
== 0) {
2717 tevent_req_nterror(req
, NT_STATUS_NO_USER_SESSION_KEY
);
2721 additional_flags
&= ~SMB2_HDR_FLAG_SIGNED
;
2722 state
->smb2
.should_sign
= true;
2727 tid
= tcon
->smb2
.tcon_id
;
2729 if (tcon
->smb2
.should_sign
) {
2730 state
->smb2
.should_sign
= true;
2732 if (tcon
->smb2
.should_encrypt
) {
2733 state
->smb2
.should_encrypt
= true;
2737 if (state
->smb2
.should_encrypt
) {
2738 state
->smb2
.should_sign
= false;
2741 state
->smb2
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
2742 if (state
->smb2
.recv_iov
== NULL
) {
2747 flags
|= additional_flags
;
2748 flags
&= ~clear_flags
;
2750 state
->smb2
.fixed
= fixed
;
2751 state
->smb2
.fixed_len
= fixed_len
;
2752 state
->smb2
.dyn
= dyn
;
2753 state
->smb2
.dyn_len
= dyn_len
;
2754 state
->smb2
.max_dyn_len
= max_dyn_len
;
2756 if (state
->smb2
.should_encrypt
) {
2757 SIVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
2758 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
, uid
);
2761 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PROTOCOL_ID
, SMB2_MAGIC
);
2762 SSVAL(state
->smb2
.hdr
, SMB2_HDR_LENGTH
, SMB2_HDR_BODY
);
2763 SSVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
, cmd
);
2764 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CHANNEL_SEQUENCE
, channel_sequence
);
2765 SIVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
2766 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PID
, 0); /* reserved */
2767 SIVAL(state
->smb2
.hdr
, SMB2_HDR_TID
, tid
);
2768 SBVAL(state
->smb2
.hdr
, SMB2_HDR_SESSION_ID
, uid
);
2771 case SMB2_OP_CANCEL
:
2772 state
->one_way
= true;
2776 * If this is a dummy request, it will have
2777 * UINT64_MAX as message id.
2778 * If we send on break acknowledgement,
2779 * this gets overwritten later.
2781 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, UINT64_MAX
);
2785 if (timeout_msec
> 0) {
2786 struct timeval endtime
;
2788 endtime
= timeval_current_ofs_msec(timeout_msec
);
2789 if (!tevent_req_set_endtime(req
, ev
, endtime
)) {
2797 void smb2cli_req_set_notify_async(struct tevent_req
*req
)
2799 struct smbXcli_req_state
*state
=
2800 tevent_req_data(req
,
2801 struct smbXcli_req_state
);
2803 state
->smb2
.notify_async
= true;
2806 static void smb2cli_req_writev_done(struct tevent_req
*subreq
);
2807 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
2808 TALLOC_CTX
*tmp_mem
,
2811 NTSTATUS
smb2cli_req_compound_submit(struct tevent_req
**reqs
,
2814 struct smbXcli_req_state
*state
;
2815 struct tevent_req
*subreq
;
2817 int i
, num_iov
, nbt_len
;
2819 const DATA_BLOB
*encryption_key
= NULL
;
2820 uint64_t encryption_session_id
= 0;
2823 * 1 for the nbt length, optional TRANSFORM
2824 * per request: HDR, fixed, dyn, padding
2825 * -1 because the last one does not need padding
2828 iov
= talloc_array(reqs
[0], struct iovec
, 1 + 1 + 4*num_reqs
- 1);
2830 return NT_STATUS_NO_MEMORY
;
2837 * the session of the first request that requires encryption
2838 * specifies the encryption key.
2840 for (i
=0; i
<num_reqs
; i
++) {
2841 if (!tevent_req_is_in_progress(reqs
[i
])) {
2842 return NT_STATUS_INTERNAL_ERROR
;
2845 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2847 if (!smbXcli_conn_is_connected(state
->conn
)) {
2848 return NT_STATUS_CONNECTION_DISCONNECTED
;
2851 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
2852 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
2853 return NT_STATUS_REVISION_MISMATCH
;
2856 if (state
->session
== NULL
) {
2860 if (!state
->smb2
.should_encrypt
) {
2864 encryption_key
= &state
->session
->smb2
->encryption_key
;
2865 if (encryption_key
->length
== 0) {
2866 return NT_STATUS_INVALID_PARAMETER_MIX
;
2869 encryption_session_id
= state
->session
->smb2
->session_id
;
2872 iov
[num_iov
].iov_base
= state
->smb2
.transform
;
2873 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.transform
);
2876 SBVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
2877 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
,
2878 state
->session
->smb2
->nonce_low
);
2879 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
+8,
2880 state
->session
->smb2
->nonce_high
);
2881 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
,
2882 encryption_session_id
);
2884 state
->session
->smb2
->nonce_low
+= 1;
2885 if (state
->session
->smb2
->nonce_low
== 0) {
2886 state
->session
->smb2
->nonce_high
+= 1;
2887 state
->session
->smb2
->nonce_low
+= 1;
2890 nbt_len
+= SMB2_TF_HDR_SIZE
;
2894 for (i
=0; i
<num_reqs
; i
++) {
2903 const DATA_BLOB
*signing_key
= NULL
;
2905 if (!tevent_req_is_in_progress(reqs
[i
])) {
2906 return NT_STATUS_INTERNAL_ERROR
;
2909 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2911 if (!smbXcli_conn_is_connected(state
->conn
)) {
2912 return NT_STATUS_CONNECTION_DISCONNECTED
;
2915 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
2916 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
2917 return NT_STATUS_REVISION_MISMATCH
;
2920 opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
2921 if (opcode
== SMB2_OP_CANCEL
) {
2925 avail
= UINT64_MAX
- state
->conn
->smb2
.mid
;
2927 return NT_STATUS_CONNECTION_ABORTED
;
2930 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2931 uint32_t max_dyn_len
= 1;
2933 max_dyn_len
= MAX(max_dyn_len
, state
->smb2
.dyn_len
);
2934 max_dyn_len
= MAX(max_dyn_len
, state
->smb2
.max_dyn_len
);
2936 charge
= (max_dyn_len
- 1)/ 65536 + 1;
2941 charge
= MAX(state
->smb2
.credit_charge
, charge
);
2943 avail
= MIN(avail
, state
->conn
->smb2
.cur_credits
);
2944 if (avail
< charge
) {
2945 return NT_STATUS_INTERNAL_ERROR
;
2949 if (state
->conn
->smb2
.max_credits
> state
->conn
->smb2
.cur_credits
) {
2950 credits
= state
->conn
->smb2
.max_credits
-
2951 state
->conn
->smb2
.cur_credits
;
2953 if (state
->conn
->smb2
.max_credits
>= state
->conn
->smb2
.cur_credits
) {
2957 mid
= state
->conn
->smb2
.mid
;
2958 state
->conn
->smb2
.mid
+= charge
;
2959 state
->conn
->smb2
.cur_credits
-= charge
;
2961 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2962 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT_CHARGE
, charge
);
2964 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT
, credits
);
2965 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
2968 if (state
->session
&& encryption_key
== NULL
) {
2970 * We prefer the channel signing key if it is
2973 if (state
->smb2
.should_sign
) {
2974 signing_key
= &state
->session
->smb2_channel
.signing_key
;
2978 * If it is a channel binding, we already have the main
2979 * signing key and try that one.
2981 if (signing_key
&& signing_key
->length
== 0) {
2982 signing_key
= &state
->session
->smb2
->signing_key
;
2986 * If we do not have any session key yet, we skip the
2987 * signing of SMB2_OP_SESSSETUP requests.
2989 if (signing_key
&& signing_key
->length
== 0) {
2995 iov
[num_iov
].iov_base
= state
->smb2
.hdr
;
2996 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.hdr
);
2999 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.fixed
);
3000 iov
[num_iov
].iov_len
= state
->smb2
.fixed_len
;
3003 if (state
->smb2
.dyn
!= NULL
) {
3004 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.dyn
);
3005 iov
[num_iov
].iov_len
= state
->smb2
.dyn_len
;
3009 reqlen
= sizeof(state
->smb2
.hdr
);
3010 reqlen
+= state
->smb2
.fixed_len
;
3011 reqlen
+= state
->smb2
.dyn_len
;
3013 if (i
< num_reqs
-1) {
3014 if ((reqlen
% 8) > 0) {
3015 uint8_t pad
= 8 - (reqlen
% 8);
3016 iov
[num_iov
].iov_base
= state
->smb2
.pad
;
3017 iov
[num_iov
].iov_len
= pad
;
3021 SIVAL(state
->smb2
.hdr
, SMB2_HDR_NEXT_COMMAND
, reqlen
);
3024 state
->smb2
.encryption_session_id
= encryption_session_id
;
3026 if (signing_key
!= NULL
) {
3029 status
= smb2_signing_sign_pdu(*signing_key
,
3030 state
->session
->conn
->protocol
,
3031 &iov
[hdr_iov
], num_iov
- hdr_iov
);
3032 if (!NT_STATUS_IS_OK(status
)) {
3039 ret
= smbXcli_req_set_pending(reqs
[i
]);
3041 return NT_STATUS_NO_MEMORY
;
3045 state
= tevent_req_data(reqs
[0], struct smbXcli_req_state
);
3046 _smb_setlen_tcp(state
->length_hdr
, nbt_len
);
3047 iov
[0].iov_base
= state
->length_hdr
;
3048 iov
[0].iov_len
= sizeof(state
->length_hdr
);
3050 if (encryption_key
!= NULL
) {
3052 size_t buflen
= nbt_len
- SMB2_TF_HDR_SIZE
;
3056 buf
= talloc_array(iov
, uint8_t, buflen
);
3058 return NT_STATUS_NO_MEMORY
;
3062 * We copy the buffers before encrypting them,
3063 * this is at least currently needed for the
3064 * to keep state->smb2.hdr.
3066 * Also the callers may expect there buffers
3069 for (vi
= tf_iov
+ 1; vi
< num_iov
; vi
++) {
3070 struct iovec
*v
= &iov
[vi
];
3071 const uint8_t *o
= (const uint8_t *)v
->iov_base
;
3073 memcpy(buf
, o
, v
->iov_len
);
3074 v
->iov_base
= (void *)buf
;
3078 status
= smb2_signing_encrypt_pdu(*encryption_key
,
3079 state
->conn
->smb2
.server
.cipher
,
3080 &iov
[tf_iov
], num_iov
- tf_iov
);
3081 if (!NT_STATUS_IS_OK(status
)) {
3086 if (state
->conn
->dispatch_incoming
== NULL
) {
3087 state
->conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
3090 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
3091 state
->conn
->write_fd
, false, iov
, num_iov
);
3092 if (subreq
== NULL
) {
3093 return NT_STATUS_NO_MEMORY
;
3095 tevent_req_set_callback(subreq
, smb2cli_req_writev_done
, reqs
[0]);
3096 return NT_STATUS_OK
;
3099 void smb2cli_req_set_credit_charge(struct tevent_req
*req
, uint16_t charge
)
3101 struct smbXcli_req_state
*state
=
3102 tevent_req_data(req
,
3103 struct smbXcli_req_state
);
3105 state
->smb2
.credit_charge
= charge
;
3108 struct tevent_req
*smb2cli_req_send(TALLOC_CTX
*mem_ctx
,
3109 struct tevent_context
*ev
,
3110 struct smbXcli_conn
*conn
,
3112 uint32_t additional_flags
,
3113 uint32_t clear_flags
,
3114 uint32_t timeout_msec
,
3115 struct smbXcli_tcon
*tcon
,
3116 struct smbXcli_session
*session
,
3117 const uint8_t *fixed
,
3121 uint32_t max_dyn_len
)
3123 struct tevent_req
*req
;
3126 req
= smb2cli_req_create(mem_ctx
, ev
, conn
, cmd
,
3127 additional_flags
, clear_flags
,
3136 if (!tevent_req_is_in_progress(req
)) {
3137 return tevent_req_post(req
, ev
);
3139 status
= smb2cli_req_compound_submit(&req
, 1);
3140 if (tevent_req_nterror(req
, status
)) {
3141 return tevent_req_post(req
, ev
);
3146 static void smb2cli_req_writev_done(struct tevent_req
*subreq
)
3148 struct tevent_req
*req
=
3149 tevent_req_callback_data(subreq
,
3151 struct smbXcli_req_state
*state
=
3152 tevent_req_data(req
,
3153 struct smbXcli_req_state
);
3157 nwritten
= writev_recv(subreq
, &err
);
3158 TALLOC_FREE(subreq
);
3159 if (nwritten
== -1) {
3160 /* here, we need to notify all pending requests */
3161 NTSTATUS status
= map_nt_error_from_unix_common(err
);
3162 smbXcli_conn_disconnect(state
->conn
, status
);
3167 static NTSTATUS
smb2cli_inbuf_parse_compound(struct smbXcli_conn
*conn
,
3170 TALLOC_CTX
*mem_ctx
,
3171 struct iovec
**piov
, int *pnum_iov
)
3176 uint8_t *first_hdr
= buf
;
3177 size_t verified_buflen
= 0;
3181 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
3183 return NT_STATUS_NO_MEMORY
;
3186 while (taken
< buflen
) {
3187 size_t len
= buflen
- taken
;
3188 uint8_t *hdr
= first_hdr
+ taken
;
3191 size_t next_command_ofs
;
3193 struct iovec
*iov_tmp
;
3195 if (verified_buflen
> taken
) {
3196 len
= verified_buflen
- taken
;
3203 DEBUG(10, ("%d bytes left, expected at least %d\n",
3207 if (IVAL(hdr
, 0) == SMB2_TF_MAGIC
) {
3208 struct smbXcli_session
*s
;
3210 struct iovec tf_iov
[2];
3214 if (len
< SMB2_TF_HDR_SIZE
) {
3215 DEBUG(10, ("%d bytes left, expected at least %d\n",
3216 (int)len
, SMB2_TF_HDR_SIZE
));
3220 tf_len
= SMB2_TF_HDR_SIZE
;
3223 hdr
= first_hdr
+ taken
;
3224 enc_len
= IVAL(tf
, SMB2_TF_MSG_SIZE
);
3225 uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
3227 if (len
< SMB2_TF_HDR_SIZE
+ enc_len
) {
3228 DEBUG(10, ("%d bytes left, expected at least %d\n",
3230 (int)(SMB2_TF_HDR_SIZE
+ enc_len
)));
3235 for (; s
; s
= s
->next
) {
3236 if (s
->smb2
->session_id
!= uid
) {
3243 DEBUG(10, ("unknown session_id %llu\n",
3244 (unsigned long long)uid
));
3248 tf_iov
[0].iov_base
= (void *)tf
;
3249 tf_iov
[0].iov_len
= tf_len
;
3250 tf_iov
[1].iov_base
= (void *)hdr
;
3251 tf_iov
[1].iov_len
= enc_len
;
3253 status
= smb2_signing_decrypt_pdu(s
->smb2
->decryption_key
,
3254 conn
->smb2
.server
.cipher
,
3256 if (!NT_STATUS_IS_OK(status
)) {
3261 verified_buflen
= taken
+ enc_len
;
3266 * We need the header plus the body length field
3269 if (len
< SMB2_HDR_BODY
+ 2) {
3270 DEBUG(10, ("%d bytes left, expected at least %d\n",
3271 (int)len
, SMB2_HDR_BODY
));
3274 if (IVAL(hdr
, 0) != SMB2_MAGIC
) {
3275 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3279 if (SVAL(hdr
, 4) != SMB2_HDR_BODY
) {
3280 DEBUG(10, ("Got HDR len %d, expected %d\n",
3281 SVAL(hdr
, 4), SMB2_HDR_BODY
));
3286 next_command_ofs
= IVAL(hdr
, SMB2_HDR_NEXT_COMMAND
);
3287 body_size
= SVAL(hdr
, SMB2_HDR_BODY
);
3289 if (next_command_ofs
!= 0) {
3290 if (next_command_ofs
< (SMB2_HDR_BODY
+ 2)) {
3293 if (next_command_ofs
> full_size
) {
3296 full_size
= next_command_ofs
;
3298 if (body_size
< 2) {
3301 body_size
&= 0xfffe;
3303 if (body_size
> (full_size
- SMB2_HDR_BODY
)) {
3307 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
3309 if (iov_tmp
== NULL
) {
3311 return NT_STATUS_NO_MEMORY
;
3314 cur
= &iov
[num_iov
];
3317 cur
[0].iov_base
= tf
;
3318 cur
[0].iov_len
= tf_len
;
3319 cur
[1].iov_base
= hdr
;
3320 cur
[1].iov_len
= SMB2_HDR_BODY
;
3321 cur
[2].iov_base
= hdr
+ SMB2_HDR_BODY
;
3322 cur
[2].iov_len
= body_size
;
3323 cur
[3].iov_base
= hdr
+ SMB2_HDR_BODY
+ body_size
;
3324 cur
[3].iov_len
= full_size
- (SMB2_HDR_BODY
+ body_size
);
3330 *pnum_iov
= num_iov
;
3331 return NT_STATUS_OK
;
3335 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3338 static struct tevent_req
*smb2cli_conn_find_pending(struct smbXcli_conn
*conn
,
3341 size_t num_pending
= talloc_array_length(conn
->pending
);
3344 for (i
=0; i
<num_pending
; i
++) {
3345 struct tevent_req
*req
= conn
->pending
[i
];
3346 struct smbXcli_req_state
*state
=
3347 tevent_req_data(req
,
3348 struct smbXcli_req_state
);
3350 if (mid
== BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
)) {
3357 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
3358 TALLOC_CTX
*tmp_mem
,
3361 struct tevent_req
*req
;
3362 struct smbXcli_req_state
*state
= NULL
;
3367 struct smbXcli_session
*last_session
= NULL
;
3368 size_t inbuf_len
= smb_len_tcp(inbuf
);
3370 status
= smb2cli_inbuf_parse_compound(conn
,
3371 inbuf
+ NBT_HDR_SIZE
,
3375 if (!NT_STATUS_IS_OK(status
)) {
3379 for (i
=0; i
<num_iov
; i
+=4) {
3380 uint8_t *inbuf_ref
= NULL
;
3381 struct iovec
*cur
= &iov
[i
];
3382 uint8_t *inhdr
= (uint8_t *)cur
[1].iov_base
;
3383 uint16_t opcode
= SVAL(inhdr
, SMB2_HDR_OPCODE
);
3384 uint32_t flags
= IVAL(inhdr
, SMB2_HDR_FLAGS
);
3385 uint64_t mid
= BVAL(inhdr
, SMB2_HDR_MESSAGE_ID
);
3386 uint16_t req_opcode
;
3388 uint16_t credits
= SVAL(inhdr
, SMB2_HDR_CREDIT
);
3389 uint32_t new_credits
;
3390 struct smbXcli_session
*session
= NULL
;
3391 const DATA_BLOB
*signing_key
= NULL
;
3392 bool was_encrypted
= false;
3394 new_credits
= conn
->smb2
.cur_credits
;
3395 new_credits
+= credits
;
3396 if (new_credits
> UINT16_MAX
) {
3397 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3399 conn
->smb2
.cur_credits
+= credits
;
3401 req
= smb2cli_conn_find_pending(conn
, mid
);
3403 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3405 state
= tevent_req_data(req
, struct smbXcli_req_state
);
3407 req_opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
3408 if (opcode
!= req_opcode
) {
3409 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3411 req_flags
= SVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
3413 if (!(flags
& SMB2_HDR_FLAG_REDIRECT
)) {
3414 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3417 status
= NT_STATUS(IVAL(inhdr
, SMB2_HDR_STATUS
));
3418 if ((flags
& SMB2_HDR_FLAG_ASYNC
) &&
3419 NT_STATUS_EQUAL(status
, STATUS_PENDING
)) {
3420 uint64_t async_id
= BVAL(inhdr
, SMB2_HDR_ASYNC_ID
);
3422 if (state
->smb2
.got_async
) {
3423 /* We only expect one STATUS_PENDING response */
3424 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3426 state
->smb2
.got_async
= true;
3429 * async interim responses are not signed,
3430 * even if the SMB2_HDR_FLAG_SIGNED flag
3433 req_flags
|= SMB2_HDR_FLAG_ASYNC
;
3434 SBVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, req_flags
);
3435 SBVAL(state
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, async_id
);
3437 if (state
->smb2
.notify_async
) {
3438 tevent_req_defer_callback(req
, state
->ev
);
3439 tevent_req_notify_callback(req
);
3444 session
= state
->session
;
3445 if (req_flags
& SMB2_HDR_FLAG_CHAINED
) {
3446 session
= last_session
;
3448 last_session
= session
;
3450 if (state
->smb2
.should_sign
) {
3451 if (!(flags
& SMB2_HDR_FLAG_SIGNED
)) {
3452 return NT_STATUS_ACCESS_DENIED
;
3456 if (flags
& SMB2_HDR_FLAG_SIGNED
) {
3457 uint64_t uid
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
3459 if (session
== NULL
) {
3460 struct smbXcli_session
*s
;
3462 s
= state
->conn
->sessions
;
3463 for (; s
; s
= s
->next
) {
3464 if (s
->smb2
->session_id
!= uid
) {
3473 if (session
== NULL
) {
3474 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3477 last_session
= session
;
3478 signing_key
= &session
->smb2_channel
.signing_key
;
3481 if (opcode
== SMB2_OP_SESSSETUP
) {
3483 * We prefer the channel signing key, if it is
3486 * If we do not have a channel signing key yet,
3487 * we try the main signing key, if it is not
3488 * the final response.
3490 if (signing_key
&& signing_key
->length
== 0 &&
3491 !NT_STATUS_IS_OK(status
)) {
3492 signing_key
= &session
->smb2
->signing_key
;
3495 if (signing_key
&& signing_key
->length
== 0) {
3497 * If we do not have a session key to
3498 * verify the signature, we defer the
3499 * signing check to the caller.
3501 * The caller gets NT_STATUS_OK, it
3503 * smb2cli_session_set_session_key()
3505 * smb2cli_session_set_channel_key()
3506 * which will check the signature
3507 * with the channel signing key.
3513 if (cur
[0].iov_len
== SMB2_TF_HDR_SIZE
) {
3514 const uint8_t *tf
= (const uint8_t *)cur
[0].iov_base
;
3515 uint64_t uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
3518 * If the response was encrypted in a SMB2_TRANSFORM
3519 * pdu, which belongs to the correct session,
3520 * we do not need to do signing checks
3522 * It could be the session the response belongs to
3523 * or the session that was used to encrypt the
3524 * SMB2_TRANSFORM request.
3526 if ((session
&& session
->smb2
->session_id
== uid
) ||
3527 (state
->smb2
.encryption_session_id
== uid
)) {
3529 was_encrypted
= true;
3533 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_SESSION_DELETED
)) {
3535 * if the server returns NT_STATUS_USER_SESSION_DELETED
3536 * the response is not signed and we should
3537 * propagate the NT_STATUS_USER_SESSION_DELETED
3538 * status to the caller.
3540 state
->smb2
.signing_skipped
= true;
3544 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
3546 * if the server returns
3547 * NT_STATUS_INVALID_PARAMETER
3548 * the response might not be encrypted.
3550 if (state
->smb2
.should_encrypt
&& !was_encrypted
) {
3551 state
->smb2
.signing_skipped
= true;
3556 if (state
->smb2
.should_encrypt
&& !was_encrypted
) {
3557 if (!state
->smb2
.signing_skipped
) {
3558 return NT_STATUS_ACCESS_DENIED
;
3562 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_NAME_DELETED
) ||
3563 NT_STATUS_EQUAL(status
, NT_STATUS_FILE_CLOSED
) ||
3564 NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
3566 * if the server returns
3567 * NT_STATUS_NETWORK_NAME_DELETED
3568 * NT_STATUS_FILE_CLOSED
3569 * NT_STATUS_INVALID_PARAMETER
3570 * the response might not be signed
3571 * as this happens before the signing checks.
3573 * If server echos the signature (or all zeros)
3574 * we should report the status from the server
3580 cmp
= memcmp(inhdr
+SMB2_HDR_SIGNATURE
,
3581 state
->smb2
.hdr
+SMB2_HDR_SIGNATURE
,
3584 state
->smb2
.signing_skipped
= true;
3590 static const uint8_t zeros
[16];
3592 cmp
= memcmp(inhdr
+SMB2_HDR_SIGNATURE
,
3596 state
->smb2
.signing_skipped
= true;
3603 status
= smb2_signing_check_pdu(*signing_key
,
3604 state
->conn
->protocol
,
3606 if (!NT_STATUS_IS_OK(status
)) {
3608 * If the signing check fails, we disconnect
3615 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
) &&
3616 (session
!= NULL
) && session
->disconnect_expired
)
3619 * this should be a short term hack
3620 * until the upper layers have implemented
3621 * re-authentication.
3626 smbXcli_req_unset_pending(req
);
3629 * There might be more than one response
3630 * we need to defer the notifications
3632 if ((num_iov
== 5) && (talloc_array_length(conn
->pending
) == 0)) {
3637 tevent_req_defer_callback(req
, state
->ev
);
3641 * Note: here we use talloc_reference() in a way
3642 * that does not expose it to the caller.
3644 inbuf_ref
= talloc_reference(state
->smb2
.recv_iov
, inbuf
);
3645 if (tevent_req_nomem(inbuf_ref
, req
)) {
3649 /* copy the related buffers */
3650 state
->smb2
.recv_iov
[0] = cur
[1];
3651 state
->smb2
.recv_iov
[1] = cur
[2];
3652 state
->smb2
.recv_iov
[2] = cur
[3];
3654 tevent_req_done(req
);
3658 return NT_STATUS_RETRY
;
3661 return NT_STATUS_OK
;
3664 NTSTATUS
smb2cli_req_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
3665 struct iovec
**piov
,
3666 const struct smb2cli_req_expected_response
*expected
,
3667 size_t num_expected
)
3669 struct smbXcli_req_state
*state
=
3670 tevent_req_data(req
,
3671 struct smbXcli_req_state
);
3674 bool found_status
= false;
3675 bool found_size
= false;
3682 if (tevent_req_is_in_progress(req
) && state
->smb2
.got_async
) {
3683 return STATUS_PENDING
;
3686 if (tevent_req_is_nterror(req
, &status
)) {
3687 for (i
=0; i
< num_expected
; i
++) {
3688 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
3689 found_status
= true;
3695 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
3701 if (num_expected
== 0) {
3702 found_status
= true;
3706 status
= NT_STATUS(IVAL(state
->smb2
.recv_iov
[0].iov_base
, SMB2_HDR_STATUS
));
3707 body_size
= SVAL(state
->smb2
.recv_iov
[1].iov_base
, 0);
3709 for (i
=0; i
< num_expected
; i
++) {
3710 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
3714 found_status
= true;
3715 if (expected
[i
].body_size
== 0) {
3720 if (expected
[i
].body_size
== body_size
) {
3726 if (!found_status
) {
3730 if (state
->smb2
.signing_skipped
) {
3731 if (num_expected
> 0) {
3732 return NT_STATUS_ACCESS_DENIED
;
3734 if (!NT_STATUS_IS_ERR(status
)) {
3735 return NT_STATUS_ACCESS_DENIED
;
3740 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3744 *piov
= talloc_move(mem_ctx
, &state
->smb2
.recv_iov
);
3750 NTSTATUS
smb2cli_req_get_sent_iov(struct tevent_req
*req
,
3751 struct iovec
*sent_iov
)
3753 struct smbXcli_req_state
*state
=
3754 tevent_req_data(req
,
3755 struct smbXcli_req_state
);
3757 if (tevent_req_is_in_progress(req
)) {
3758 return STATUS_PENDING
;
3761 sent_iov
[0].iov_base
= state
->smb2
.hdr
;
3762 sent_iov
[0].iov_len
= sizeof(state
->smb2
.hdr
);
3764 sent_iov
[1].iov_base
= discard_const(state
->smb2
.fixed
);
3765 sent_iov
[1].iov_len
= state
->smb2
.fixed_len
;
3767 if (state
->smb2
.dyn
!= NULL
) {
3768 sent_iov
[2].iov_base
= discard_const(state
->smb2
.dyn
);
3769 sent_iov
[2].iov_len
= state
->smb2
.dyn_len
;
3771 sent_iov
[2].iov_base
= NULL
;
3772 sent_iov
[2].iov_len
= 0;
3775 return NT_STATUS_OK
;
3778 static const struct {
3779 enum protocol_types proto
;
3780 const char *smb1_name
;
3781 } smb1cli_prots
[] = {
3782 {PROTOCOL_CORE
, "PC NETWORK PROGRAM 1.0"},
3783 {PROTOCOL_COREPLUS
, "MICROSOFT NETWORKS 1.03"},
3784 {PROTOCOL_LANMAN1
, "MICROSOFT NETWORKS 3.0"},
3785 {PROTOCOL_LANMAN1
, "LANMAN1.0"},
3786 {PROTOCOL_LANMAN2
, "LM1.2X002"},
3787 {PROTOCOL_LANMAN2
, "DOS LANMAN2.1"},
3788 {PROTOCOL_LANMAN2
, "LANMAN2.1"},
3789 {PROTOCOL_LANMAN2
, "Samba"},
3790 {PROTOCOL_NT1
, "NT LANMAN 1.0"},
3791 {PROTOCOL_NT1
, "NT LM 0.12"},
3792 {PROTOCOL_SMB2_02
, "SMB 2.002"},
3793 {PROTOCOL_SMB2_10
, "SMB 2.???"},
3796 static const struct {
3797 enum protocol_types proto
;
3798 uint16_t smb2_dialect
;
3799 } smb2cli_prots
[] = {
3800 {PROTOCOL_SMB2_02
, SMB2_DIALECT_REVISION_202
},
3801 {PROTOCOL_SMB2_10
, SMB2_DIALECT_REVISION_210
},
3802 {PROTOCOL_SMB2_22
, SMB2_DIALECT_REVISION_222
},
3803 {PROTOCOL_SMB2_24
, SMB2_DIALECT_REVISION_224
},
3804 {PROTOCOL_SMB3_00
, SMB3_DIALECT_REVISION_300
},
3805 {PROTOCOL_SMB3_02
, SMB3_DIALECT_REVISION_302
},
3806 {PROTOCOL_SMB3_10
, SMB3_DIALECT_REVISION_310
},
3809 struct smbXcli_negprot_state
{
3810 struct smbXcli_conn
*conn
;
3811 struct tevent_context
*ev
;
3812 uint32_t timeout_msec
;
3819 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
);
3820 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
);
3821 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
);
3822 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
);
3823 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
);
3824 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
3828 struct tevent_req
*smbXcli_negprot_send(TALLOC_CTX
*mem_ctx
,
3829 struct tevent_context
*ev
,
3830 struct smbXcli_conn
*conn
,
3831 uint32_t timeout_msec
,
3832 enum protocol_types min_protocol
,
3833 enum protocol_types max_protocol
)
3835 struct tevent_req
*req
, *subreq
;
3836 struct smbXcli_negprot_state
*state
;
3838 req
= tevent_req_create(mem_ctx
, &state
,
3839 struct smbXcli_negprot_state
);
3845 state
->timeout_msec
= timeout_msec
;
3847 if (min_protocol
== PROTOCOL_NONE
) {
3848 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3849 return tevent_req_post(req
, ev
);
3852 if (max_protocol
== PROTOCOL_NONE
) {
3853 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3854 return tevent_req_post(req
, ev
);
3857 if (min_protocol
> max_protocol
) {
3858 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3859 return tevent_req_post(req
, ev
);
3862 conn
->min_protocol
= min_protocol
;
3863 conn
->max_protocol
= max_protocol
;
3864 conn
->protocol
= PROTOCOL_NONE
;
3866 if ((min_protocol
< PROTOCOL_SMB2_02
) &&
3867 (max_protocol
< PROTOCOL_SMB2_02
)) {
3871 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
3873 subreq
= smbXcli_negprot_smb1_subreq(state
);
3874 if (tevent_req_nomem(subreq
, req
)) {
3875 return tevent_req_post(req
, ev
);
3877 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
3881 if ((min_protocol
>= PROTOCOL_SMB2_02
) &&
3882 (max_protocol
>= PROTOCOL_SMB2_02
)) {
3886 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
3889 * As we're starting with an SMB2 negprot, emulate Windows
3890 * and ask for 31 credits in the initial SMB2 negprot.
3891 * If we don't and leave requested credits at
3892 * zero, MacOSX servers return zero credits on
3893 * the negprot reply and we fail to connect.
3895 smb2cli_conn_set_max_credits(conn
,
3896 WINDOWS_CLIENT_PURE_SMB2_NEGPROT_INITIAL_CREDIT_ASK
);
3898 subreq
= smbXcli_negprot_smb2_subreq(state
);
3899 if (tevent_req_nomem(subreq
, req
)) {
3900 return tevent_req_post(req
, ev
);
3902 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
3907 * We send an SMB1 negprot with the SMB2 dialects
3908 * and expect a SMB1 or a SMB2 response.
3910 * smbXcli_negprot_dispatch_incoming() will fix the
3911 * callback to match protocol of the response.
3913 conn
->dispatch_incoming
= smbXcli_negprot_dispatch_incoming
;
3915 subreq
= smbXcli_negprot_smb1_subreq(state
);
3916 if (tevent_req_nomem(subreq
, req
)) {
3917 return tevent_req_post(req
, ev
);
3919 tevent_req_set_callback(subreq
, smbXcli_negprot_invalid_done
, req
);
3923 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
)
3925 struct tevent_req
*req
=
3926 tevent_req_callback_data(subreq
,
3931 * we just want the low level error
3933 status
= tevent_req_simple_recv_ntstatus(subreq
);
3934 TALLOC_FREE(subreq
);
3935 if (tevent_req_nterror(req
, status
)) {
3939 /* this should never happen */
3940 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
3943 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
)
3946 DATA_BLOB bytes
= data_blob_null
;
3950 /* setup the protocol strings */
3951 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
3955 if (smb1cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
3959 if (smb1cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
3963 ok
= data_blob_append(state
, &bytes
, &c
, sizeof(c
));
3969 * We now it is already ascii and
3970 * we want NULL termination.
3972 ok
= data_blob_append(state
, &bytes
,
3973 smb1cli_prots
[i
].smb1_name
,
3974 strlen(smb1cli_prots
[i
].smb1_name
)+1);
3980 smb1cli_req_flags(state
->conn
->max_protocol
,
3981 state
->conn
->smb1
.client
.capabilities
,
3986 return smb1cli_req_send(state
, state
->ev
, state
->conn
,
3990 state
->timeout_msec
,
3991 0xFFFE, 0, NULL
, /* pid, tid, session */
3992 0, NULL
, /* wct, vwv */
3993 bytes
.length
, bytes
.data
);
3996 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
)
3998 struct tevent_req
*req
=
3999 tevent_req_callback_data(subreq
,
4001 struct smbXcli_negprot_state
*state
=
4002 tevent_req_data(req
,
4003 struct smbXcli_negprot_state
);
4004 struct smbXcli_conn
*conn
= state
->conn
;
4005 struct iovec
*recv_iov
= NULL
;
4014 size_t num_prots
= 0;
4016 uint32_t client_capabilities
= conn
->smb1
.client
.capabilities
;
4017 uint32_t both_capabilities
;
4018 uint32_t server_capabilities
= 0;
4019 uint32_t capabilities
;
4020 uint32_t client_max_xmit
= conn
->smb1
.client
.max_xmit
;
4021 uint32_t server_max_xmit
= 0;
4023 uint32_t server_max_mux
= 0;
4024 uint16_t server_security_mode
= 0;
4025 uint32_t server_session_key
= 0;
4026 bool server_readbraw
= false;
4027 bool server_writebraw
= false;
4028 bool server_lockread
= false;
4029 bool server_writeunlock
= false;
4030 struct GUID server_guid
= GUID_zero();
4031 DATA_BLOB server_gss_blob
= data_blob_null
;
4032 uint8_t server_challenge
[8];
4033 char *server_workgroup
= NULL
;
4034 char *server_name
= NULL
;
4035 int server_time_zone
= 0;
4036 NTTIME server_system_time
= 0;
4037 static const struct smb1cli_req_expected_response expected
[] = {
4039 .status
= NT_STATUS_OK
,
4040 .wct
= 0x11, /* NT1 */
4043 .status
= NT_STATUS_OK
,
4044 .wct
= 0x0D, /* LM */
4047 .status
= NT_STATUS_OK
,
4048 .wct
= 0x01, /* CORE */
4052 ZERO_STRUCT(server_challenge
);
4054 status
= smb1cli_req_recv(subreq
, state
,
4059 NULL
, /* pvwv_offset */
4062 NULL
, /* pbytes_offset */
4064 expected
, ARRAY_SIZE(expected
));
4065 TALLOC_FREE(subreq
);
4066 if (tevent_req_nterror(req
, status
)) {
4070 flags
= CVAL(inhdr
, HDR_FLG
);
4072 protnum
= SVAL(vwv
, 0);
4074 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
4075 if (smb1cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
4079 if (smb1cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
4083 if (protnum
!= num_prots
) {
4088 conn
->protocol
= smb1cli_prots
[i
].proto
;
4092 if (conn
->protocol
== PROTOCOL_NONE
) {
4093 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4097 if ((conn
->protocol
< PROTOCOL_NT1
) && conn
->mandatory_signing
) {
4098 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4099 "and the selected protocol level doesn't support it.\n"));
4100 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
4104 if (flags
& FLAG_SUPPORT_LOCKREAD
) {
4105 server_lockread
= true;
4106 server_writeunlock
= true;
4109 if (conn
->protocol
>= PROTOCOL_NT1
) {
4110 const char *client_signing
= NULL
;
4111 bool server_mandatory
= false;
4112 bool server_allowed
= false;
4113 const char *server_signing
= NULL
;
4118 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4123 server_security_mode
= CVAL(vwv
+ 1, 0);
4124 server_max_mux
= SVAL(vwv
+ 1, 1);
4125 server_max_xmit
= IVAL(vwv
+ 3, 1);
4126 server_session_key
= IVAL(vwv
+ 7, 1);
4127 server_time_zone
= SVALS(vwv
+ 15, 1);
4128 server_time_zone
*= 60;
4129 /* this time arrives in real GMT */
4130 server_system_time
= BVAL(vwv
+ 11, 1);
4131 server_capabilities
= IVAL(vwv
+ 9, 1);
4133 key_len
= CVAL(vwv
+ 16, 1);
4135 if (server_capabilities
& CAP_RAW_MODE
) {
4136 server_readbraw
= true;
4137 server_writebraw
= true;
4139 if (server_capabilities
& CAP_LOCK_AND_READ
) {
4140 server_lockread
= true;
4143 if (server_capabilities
& CAP_EXTENDED_SECURITY
) {
4144 DATA_BLOB blob1
, blob2
;
4146 if (num_bytes
< 16) {
4147 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4151 blob1
= data_blob_const(bytes
, 16);
4152 status
= GUID_from_data_blob(&blob1
, &server_guid
);
4153 if (tevent_req_nterror(req
, status
)) {
4157 blob1
= data_blob_const(bytes
+16, num_bytes
-16);
4158 blob2
= data_blob_dup_talloc(state
, blob1
);
4159 if (blob1
.length
> 0 &&
4160 tevent_req_nomem(blob2
.data
, req
)) {
4163 server_gss_blob
= blob2
;
4165 DATA_BLOB blob1
, blob2
;
4167 if (num_bytes
< key_len
) {
4168 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4172 if (key_len
!= 0 && key_len
!= 8) {
4173 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4178 memcpy(server_challenge
, bytes
, 8);
4181 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4182 blob2
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4183 if (blob1
.length
> 0) {
4186 len
= utf16_len_n(blob1
.data
,
4190 ok
= convert_string_talloc(state
,
4198 status
= map_nt_error_from_unix_common(errno
);
4199 tevent_req_nterror(req
, status
);
4204 blob2
.data
+= blob1
.length
;
4205 blob2
.length
-= blob1
.length
;
4206 if (blob2
.length
> 0) {
4209 len
= utf16_len_n(blob1
.data
,
4213 ok
= convert_string_talloc(state
,
4221 status
= map_nt_error_from_unix_common(errno
);
4222 tevent_req_nterror(req
, status
);
4228 client_signing
= "disabled";
4229 if (conn
->allow_signing
) {
4230 client_signing
= "allowed";
4232 if (conn
->mandatory_signing
) {
4233 client_signing
= "required";
4236 server_signing
= "not supported";
4237 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_ENABLED
) {
4238 server_signing
= "supported";
4239 server_allowed
= true;
4240 } else if (conn
->mandatory_signing
) {
4242 * We have mandatory signing as client
4243 * lets assume the server will look at our
4244 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4245 * flag in the session setup
4247 server_signing
= "not announced";
4248 server_allowed
= true;
4250 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_REQUIRED
) {
4251 server_signing
= "required";
4252 server_mandatory
= true;
4255 ok
= smb_signing_set_negotiated(conn
->smb1
.signing
,
4259 DEBUG(1,("cli_negprot: SMB signing is required, "
4260 "but client[%s] and server[%s] mismatch\n",
4261 client_signing
, server_signing
));
4262 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
4266 } else if (conn
->protocol
>= PROTOCOL_LANMAN1
) {
4272 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4276 server_security_mode
= SVAL(vwv
+ 1, 0);
4277 server_max_xmit
= SVAL(vwv
+ 2, 0);
4278 server_max_mux
= SVAL(vwv
+ 3, 0);
4279 server_readbraw
= ((SVAL(vwv
+ 5, 0) & 0x1) != 0);
4280 server_writebraw
= ((SVAL(vwv
+ 5, 0) & 0x2) != 0);
4281 server_session_key
= IVAL(vwv
+ 6, 0);
4282 server_time_zone
= SVALS(vwv
+ 10, 0);
4283 server_time_zone
*= 60;
4284 /* this time is converted to GMT by make_unix_date */
4285 t
= pull_dos_date((const uint8_t *)(vwv
+ 8), server_time_zone
);
4286 unix_to_nt_time(&server_system_time
, t
);
4287 key_len
= SVAL(vwv
+ 11, 0);
4289 if (num_bytes
< key_len
) {
4290 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4294 if (key_len
!= 0 && key_len
!= 8) {
4295 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4300 memcpy(server_challenge
, bytes
, 8);
4303 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4304 if (blob1
.length
> 0) {
4308 len
= utf16_len_n(blob1
.data
,
4312 ok
= convert_string_talloc(state
,
4320 status
= map_nt_error_from_unix_common(errno
);
4321 tevent_req_nterror(req
, status
);
4327 /* the old core protocol */
4328 server_time_zone
= get_time_zone(time(NULL
));
4329 server_max_xmit
= 1024;
4333 if (server_max_xmit
< 1024) {
4334 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4338 if (server_max_mux
< 1) {
4339 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4344 * Now calculate the negotiated capabilities
4345 * based on the mask for:
4346 * - client only flags
4347 * - flags used in both directions
4348 * - server only flags
4350 both_capabilities
= client_capabilities
& server_capabilities
;
4351 capabilities
= client_capabilities
& SMB_CAP_CLIENT_MASK
;
4352 capabilities
|= both_capabilities
& SMB_CAP_BOTH_MASK
;
4353 capabilities
|= server_capabilities
& SMB_CAP_SERVER_MASK
;
4355 max_xmit
= MIN(client_max_xmit
, server_max_xmit
);
4357 conn
->smb1
.server
.capabilities
= server_capabilities
;
4358 conn
->smb1
.capabilities
= capabilities
;
4360 conn
->smb1
.server
.max_xmit
= server_max_xmit
;
4361 conn
->smb1
.max_xmit
= max_xmit
;
4363 conn
->smb1
.server
.max_mux
= server_max_mux
;
4365 conn
->smb1
.server
.security_mode
= server_security_mode
;
4367 conn
->smb1
.server
.readbraw
= server_readbraw
;
4368 conn
->smb1
.server
.writebraw
= server_writebraw
;
4369 conn
->smb1
.server
.lockread
= server_lockread
;
4370 conn
->smb1
.server
.writeunlock
= server_writeunlock
;
4372 conn
->smb1
.server
.session_key
= server_session_key
;
4374 talloc_steal(conn
, server_gss_blob
.data
);
4375 conn
->smb1
.server
.gss_blob
= server_gss_blob
;
4376 conn
->smb1
.server
.guid
= server_guid
;
4377 memcpy(conn
->smb1
.server
.challenge
, server_challenge
, 8);
4378 conn
->smb1
.server
.workgroup
= talloc_move(conn
, &server_workgroup
);
4379 conn
->smb1
.server
.name
= talloc_move(conn
, &server_name
);
4381 conn
->smb1
.server
.time_zone
= server_time_zone
;
4382 conn
->smb1
.server
.system_time
= server_system_time
;
4384 tevent_req_done(req
);
4387 static size_t smbXcli_padding_helper(uint32_t offset
, size_t n
)
4389 if ((offset
& (n
-1)) == 0) return 0;
4390 return n
- (offset
& (n
-1));
4393 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
)
4397 uint16_t dialect_count
= 0;
4398 DATA_BLOB dyn
= data_blob_null
;
4400 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4404 if (smb2cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
4408 if (smb2cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
4412 SSVAL(val
, 0, smb2cli_prots
[i
].smb2_dialect
);
4414 ok
= data_blob_append(state
, &dyn
, val
, sizeof(val
));
4422 buf
= state
->smb2
.fixed
;
4424 SSVAL(buf
, 2, dialect_count
);
4425 SSVAL(buf
, 4, state
->conn
->smb2
.client
.security_mode
);
4426 SSVAL(buf
, 6, 0); /* Reserved */
4427 if (state
->conn
->max_protocol
>= PROTOCOL_SMB2_22
) {
4428 SIVAL(buf
, 8, state
->conn
->smb2
.client
.capabilities
);
4430 SIVAL(buf
, 8, 0); /* Capabilities */
4432 if (state
->conn
->max_protocol
>= PROTOCOL_SMB2_10
) {
4436 status
= GUID_to_ndr_blob(&state
->conn
->smb2
.client
.guid
,
4438 if (!NT_STATUS_IS_OK(status
)) {
4441 memcpy(buf
+12, blob
.data
, 16); /* ClientGuid */
4443 memset(buf
+12, 0, 16); /* ClientGuid */
4446 if (state
->conn
->max_protocol
>= PROTOCOL_SMB3_10
) {
4448 struct smb2_negotiate_contexts c
= { .num_contexts
= 0, };
4452 const uint8_t zeros
[8] = {0, };
4456 SSVAL(p
, 0, 1); /* HashAlgorithmCount */
4457 SSVAL(p
, 2, 32); /* SaltLength */
4458 SSVAL(p
, 4, SMB2_PREAUTH_INTEGRITY_SHA512
);
4459 generate_random_buffer(p
+ 6, 32);
4461 b
= data_blob_const(p
, 38);
4462 status
= smb2_negotiate_context_add(state
, &c
,
4463 SMB2_PREAUTH_INTEGRITY_CAPABILITIES
, b
);
4464 if (!NT_STATUS_IS_OK(status
)) {
4468 SSVAL(p
, 0, 2); /* ChiperCount */
4469 SSVAL(p
, 2, SMB2_ENCRYPTION_AES128_GCM
);
4470 SSVAL(p
, 4, SMB2_ENCRYPTION_AES128_CCM
);
4472 b
= data_blob_const(p
, 6);
4473 status
= smb2_negotiate_context_add(state
, &c
,
4474 SMB2_ENCRYPTION_CAPABILITIES
, b
);
4475 if (!NT_STATUS_IS_OK(status
)) {
4479 status
= smb2_negotiate_context_push(state
, &b
, c
);
4480 if (!NT_STATUS_IS_OK(status
)) {
4484 offset
= SMB2_HDR_BODY
+ sizeof(state
->smb2
.fixed
) + dyn
.length
;
4485 pad
= smbXcli_padding_helper(offset
, 8);
4487 ok
= data_blob_append(state
, &dyn
, zeros
, pad
);
4493 ok
= data_blob_append(state
, &dyn
, b
.data
, b
.length
);
4498 SIVAL(buf
, 28, offset
); /* NegotiateContextOffset */
4499 SSVAL(buf
, 32, c
.num_contexts
); /* NegotiateContextCount */
4500 SSVAL(buf
, 34, 0); /* Reserved */
4502 SBVAL(buf
, 28, 0); /* Reserved/ClientStartTime */
4505 return smb2cli_req_send(state
, state
->ev
,
4506 state
->conn
, SMB2_OP_NEGPROT
,
4508 state
->timeout_msec
,
4509 NULL
, NULL
, /* tcon, session */
4510 state
->smb2
.fixed
, sizeof(state
->smb2
.fixed
),
4511 dyn
.data
, dyn
.length
,
4512 UINT16_MAX
); /* max_dyn_len */
4515 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
)
4517 struct tevent_req
*req
=
4518 tevent_req_callback_data(subreq
,
4520 struct smbXcli_negprot_state
*state
=
4521 tevent_req_data(req
,
4522 struct smbXcli_negprot_state
);
4523 struct smbXcli_conn
*conn
= state
->conn
;
4524 size_t security_offset
, security_length
;
4530 uint16_t dialect_revision
;
4531 struct smb2_negotiate_contexts c
= { .num_contexts
= 0, };
4532 uint32_t negotiate_context_offset
= 0;
4533 uint16_t negotiate_context_count
= 0;
4534 DATA_BLOB negotiate_context_blob
= data_blob_null
;
4538 struct smb2_negotiate_context
*preauth
= NULL
;
4539 uint16_t hash_count
;
4540 uint16_t salt_length
;
4541 uint16_t hash_selected
;
4542 struct hc_sha512state sctx
;
4543 struct smb2_negotiate_context
*cipher
= NULL
;
4544 struct iovec sent_iov
[3];
4545 static const struct smb2cli_req_expected_response expected
[] = {
4547 .status
= NT_STATUS_OK
,
4552 status
= smb2cli_req_recv(subreq
, state
, &iov
,
4553 expected
, ARRAY_SIZE(expected
));
4554 if (tevent_req_nterror(req
, status
)) {
4558 body
= (uint8_t *)iov
[1].iov_base
;
4560 dialect_revision
= SVAL(body
, 4);
4562 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4563 if (smb2cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
4567 if (smb2cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
4571 if (smb2cli_prots
[i
].smb2_dialect
!= dialect_revision
) {
4575 conn
->protocol
= smb2cli_prots
[i
].proto
;
4579 if (conn
->protocol
== PROTOCOL_NONE
) {
4580 TALLOC_FREE(subreq
);
4582 if (state
->conn
->min_protocol
>= PROTOCOL_SMB2_02
) {
4583 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4587 if (dialect_revision
!= SMB2_DIALECT_REVISION_2FF
) {
4588 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4592 /* make sure we do not loop forever */
4593 state
->conn
->min_protocol
= PROTOCOL_SMB2_02
;
4596 * send a SMB2 negprot, in order to negotiate
4599 subreq
= smbXcli_negprot_smb2_subreq(state
);
4600 if (tevent_req_nomem(subreq
, req
)) {
4603 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4607 conn
->smb2
.server
.security_mode
= SVAL(body
, 2);
4608 if (conn
->protocol
>= PROTOCOL_SMB3_10
) {
4609 negotiate_context_count
= SVAL(body
, 6);
4612 blob
= data_blob_const(body
+ 8, 16);
4613 status
= GUID_from_data_blob(&blob
, &conn
->smb2
.server
.guid
);
4614 if (tevent_req_nterror(req
, status
)) {
4618 conn
->smb2
.server
.capabilities
= IVAL(body
, 24);
4619 conn
->smb2
.server
.max_trans_size
= IVAL(body
, 28);
4620 conn
->smb2
.server
.max_read_size
= IVAL(body
, 32);
4621 conn
->smb2
.server
.max_write_size
= IVAL(body
, 36);
4622 conn
->smb2
.server
.system_time
= BVAL(body
, 40);
4623 conn
->smb2
.server
.start_time
= BVAL(body
, 48);
4625 security_offset
= SVAL(body
, 56);
4626 security_length
= SVAL(body
, 58);
4628 if (security_offset
!= SMB2_HDR_BODY
+ iov
[1].iov_len
) {
4629 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4633 if (security_length
> iov
[2].iov_len
) {
4634 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4638 conn
->smb2
.server
.gss_blob
= data_blob_talloc(conn
,
4641 if (tevent_req_nomem(conn
->smb2
.server
.gss_blob
.data
, req
)) {
4645 if (conn
->protocol
< PROTOCOL_SMB3_10
) {
4646 TALLOC_FREE(subreq
);
4648 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
) {
4649 conn
->smb2
.server
.cipher
= SMB2_ENCRYPTION_AES128_CCM
;
4651 tevent_req_done(req
);
4655 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
) {
4656 tevent_req_nterror(req
,
4657 NT_STATUS_INVALID_NETWORK_RESPONSE
);
4661 negotiate_context_offset
= IVAL(body
, 60);
4662 if (negotiate_context_offset
< security_offset
) {
4663 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4667 ctx_ofs
= negotiate_context_offset
- security_offset
;
4668 if (ctx_ofs
> iov
[2].iov_len
) {
4669 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4672 avail
= iov
[2].iov_len
- security_length
;
4673 needed
= iov
[2].iov_len
- ctx_ofs
;
4674 if (needed
> avail
) {
4675 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4679 negotiate_context_blob
.data
= (uint8_t *)iov
[2].iov_base
;
4680 negotiate_context_blob
.length
= iov
[2].iov_len
;
4682 negotiate_context_blob
.data
+= ctx_ofs
;
4683 negotiate_context_blob
.length
-= ctx_ofs
;
4685 status
= smb2_negotiate_context_parse(state
, negotiate_context_blob
, &c
);
4686 if (tevent_req_nterror(req
, status
)) {
4690 if (negotiate_context_count
!= c
.num_contexts
) {
4691 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4695 preauth
= smb2_negotiate_context_find(&c
,
4696 SMB2_PREAUTH_INTEGRITY_CAPABILITIES
);
4697 if (preauth
== NULL
) {
4698 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4702 if (preauth
->data
.length
< 6) {
4703 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4707 hash_count
= SVAL(preauth
->data
.data
, 0);
4708 salt_length
= SVAL(preauth
->data
.data
, 2);
4709 hash_selected
= SVAL(preauth
->data
.data
, 4);
4711 if (hash_count
!= 1) {
4712 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4716 if (preauth
->data
.length
!= (6 + salt_length
)) {
4717 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4721 if (hash_selected
!= SMB2_PREAUTH_INTEGRITY_SHA512
) {
4722 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4726 cipher
= smb2_negotiate_context_find(&c
, SMB2_ENCRYPTION_CAPABILITIES
);
4727 if (cipher
!= NULL
) {
4728 uint16_t cipher_count
;
4730 if (cipher
->data
.length
< 2) {
4731 tevent_req_nterror(req
,
4732 NT_STATUS_INVALID_NETWORK_RESPONSE
);
4736 cipher_count
= SVAL(cipher
->data
.data
, 0);
4738 if (cipher_count
> 1) {
4739 tevent_req_nterror(req
,
4740 NT_STATUS_INVALID_NETWORK_RESPONSE
);
4744 if (cipher
->data
.length
!= (2 + 2 * cipher_count
)) {
4745 tevent_req_nterror(req
,
4746 NT_STATUS_INVALID_NETWORK_RESPONSE
);
4750 if (cipher_count
== 1) {
4751 uint16_t cipher_selected
;
4753 cipher_selected
= SVAL(cipher
->data
.data
, 2);
4755 switch (cipher_selected
) {
4756 case SMB2_ENCRYPTION_AES128_GCM
:
4757 case SMB2_ENCRYPTION_AES128_CCM
:
4758 conn
->smb2
.server
.cipher
= cipher_selected
;
4764 /* First we hash the request */
4765 smb2cli_req_get_sent_iov(subreq
, sent_iov
);
4766 samba_SHA512_Init(&sctx
);
4767 samba_SHA512_Update(&sctx
, conn
->smb2
.preauth_sha512
,
4768 sizeof(conn
->smb2
.preauth_sha512
));
4769 for (i
= 0; i
< 3; i
++) {
4770 samba_SHA512_Update(&sctx
, sent_iov
[i
].iov_base
, sent_iov
[i
].iov_len
);
4772 samba_SHA512_Final(conn
->smb2
.preauth_sha512
, &sctx
);
4773 TALLOC_FREE(subreq
);
4775 /* And now we hash the response */
4776 samba_SHA512_Init(&sctx
);
4777 samba_SHA512_Update(&sctx
, conn
->smb2
.preauth_sha512
,
4778 sizeof(conn
->smb2
.preauth_sha512
));
4779 for (i
= 0; i
< 3; i
++) {
4780 samba_SHA512_Update(&sctx
, iov
[i
].iov_base
, iov
[i
].iov_len
);
4782 samba_SHA512_Final(conn
->smb2
.preauth_sha512
, &sctx
);
4784 tevent_req_done(req
);
4787 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
4788 TALLOC_CTX
*tmp_mem
,
4791 size_t num_pending
= talloc_array_length(conn
->pending
);
4792 struct tevent_req
*subreq
;
4793 struct smbXcli_req_state
*substate
;
4794 struct tevent_req
*req
;
4795 uint32_t protocol_magic
;
4796 size_t inbuf_len
= smb_len_nbt(inbuf
);
4798 if (num_pending
!= 1) {
4799 return NT_STATUS_INTERNAL_ERROR
;
4802 if (inbuf_len
< 4) {
4803 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4806 subreq
= conn
->pending
[0];
4807 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
4808 req
= tevent_req_callback_data(subreq
, struct tevent_req
);
4810 protocol_magic
= IVAL(inbuf
, 4);
4812 switch (protocol_magic
) {
4814 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
4815 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
4816 return smb1cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
4819 if (substate
->smb2
.recv_iov
== NULL
) {
4821 * For the SMB1 negprot we have move it.
4823 substate
->smb2
.recv_iov
= substate
->smb1
.recv_iov
;
4824 substate
->smb1
.recv_iov
= NULL
;
4828 * we got an SMB2 answer, which consumed sequence number 0
4829 * so we need to use 1 as the next one.
4831 * we also need to set the current credits to 0
4832 * as we consumed the initial one. The SMB2 answer
4833 * hopefully grant us a new credit.
4836 conn
->smb2
.cur_credits
= 0;
4837 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4838 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
4839 return smb2cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
4842 DEBUG(10, ("Got non-SMB PDU\n"));
4843 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4846 NTSTATUS
smbXcli_negprot_recv(struct tevent_req
*req
)
4848 return tevent_req_simple_recv_ntstatus(req
);
4851 NTSTATUS
smbXcli_negprot(struct smbXcli_conn
*conn
,
4852 uint32_t timeout_msec
,
4853 enum protocol_types min_protocol
,
4854 enum protocol_types max_protocol
)
4856 TALLOC_CTX
*frame
= talloc_stackframe();
4857 struct tevent_context
*ev
;
4858 struct tevent_req
*req
;
4859 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4862 if (smbXcli_conn_has_async_calls(conn
)) {
4864 * Can't use sync call while an async call is in flight
4866 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
4869 ev
= samba_tevent_context_init(frame
);
4873 req
= smbXcli_negprot_send(frame
, ev
, conn
, timeout_msec
,
4874 min_protocol
, max_protocol
);
4878 ok
= tevent_req_poll_ntstatus(req
, ev
, &status
);
4882 status
= smbXcli_negprot_recv(req
);
4888 struct smb2cli_validate_negotiate_info_state
{
4889 struct smbXcli_conn
*conn
;
4890 DATA_BLOB in_input_buffer
;
4891 DATA_BLOB in_output_buffer
;
4892 DATA_BLOB out_input_buffer
;
4893 DATA_BLOB out_output_buffer
;
4897 static void smb2cli_validate_negotiate_info_done(struct tevent_req
*subreq
);
4899 struct tevent_req
*smb2cli_validate_negotiate_info_send(TALLOC_CTX
*mem_ctx
,
4900 struct tevent_context
*ev
,
4901 struct smbXcli_conn
*conn
,
4902 uint32_t timeout_msec
,
4903 struct smbXcli_session
*session
,
4904 struct smbXcli_tcon
*tcon
)
4906 struct tevent_req
*req
;
4907 struct smb2cli_validate_negotiate_info_state
*state
;
4909 uint16_t dialect_count
= 0;
4910 struct tevent_req
*subreq
;
4911 bool _save_should_sign
;
4914 req
= tevent_req_create(mem_ctx
, &state
,
4915 struct smb2cli_validate_negotiate_info_state
);
4921 state
->in_input_buffer
= data_blob_talloc_zero(state
,
4922 4 + 16 + 1 + 1 + 2);
4923 if (tevent_req_nomem(state
->in_input_buffer
.data
, req
)) {
4924 return tevent_req_post(req
, ev
);
4926 buf
= state
->in_input_buffer
.data
;
4928 if (state
->conn
->max_protocol
>= PROTOCOL_SMB2_22
) {
4929 SIVAL(buf
, 0, conn
->smb2
.client
.capabilities
);
4931 SIVAL(buf
, 0, 0); /* Capabilities */
4933 if (state
->conn
->max_protocol
>= PROTOCOL_SMB2_10
) {
4937 status
= GUID_to_ndr_blob(&conn
->smb2
.client
.guid
,
4939 if (!NT_STATUS_IS_OK(status
)) {
4942 memcpy(buf
+4, blob
.data
, 16); /* ClientGuid */
4944 memset(buf
+4, 0, 16); /* ClientGuid */
4946 if (state
->conn
->min_protocol
>= PROTOCOL_SMB2_02
) {
4947 SCVAL(buf
, 20, conn
->smb2
.client
.security_mode
);
4951 SCVAL(buf
, 21, 0); /* reserved */
4953 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4957 if (smb2cli_prots
[i
].proto
< state
->conn
->min_protocol
) {
4961 if (smb2cli_prots
[i
].proto
> state
->conn
->max_protocol
) {
4965 if (smb2cli_prots
[i
].proto
== state
->conn
->protocol
) {
4966 state
->dialect
= smb2cli_prots
[i
].smb2_dialect
;
4969 ofs
= state
->in_input_buffer
.length
;
4970 ok
= data_blob_realloc(state
, &state
->in_input_buffer
,
4973 tevent_req_oom(req
);
4974 return tevent_req_post(req
, ev
);
4977 buf
= state
->in_input_buffer
.data
;
4978 SSVAL(buf
, ofs
, smb2cli_prots
[i
].smb2_dialect
);
4982 buf
= state
->in_input_buffer
.data
;
4983 SSVAL(buf
, 22, dialect_count
);
4985 _save_should_sign
= smb2cli_tcon_is_signing_on(tcon
);
4986 smb2cli_tcon_should_sign(tcon
, true);
4987 subreq
= smb2cli_ioctl_send(state
, ev
, conn
,
4988 timeout_msec
, session
, tcon
,
4989 UINT64_MAX
, /* in_fid_persistent */
4990 UINT64_MAX
, /* in_fid_volatile */
4991 FSCTL_VALIDATE_NEGOTIATE_INFO
,
4992 0, /* in_max_input_length */
4993 &state
->in_input_buffer
,
4994 24, /* in_max_output_length */
4995 &state
->in_output_buffer
,
4996 SMB2_IOCTL_FLAG_IS_FSCTL
);
4997 smb2cli_tcon_should_sign(tcon
, _save_should_sign
);
4998 if (tevent_req_nomem(subreq
, req
)) {
4999 return tevent_req_post(req
, ev
);
5001 tevent_req_set_callback(subreq
,
5002 smb2cli_validate_negotiate_info_done
,
5008 static void smb2cli_validate_negotiate_info_done(struct tevent_req
*subreq
)
5010 struct tevent_req
*req
=
5011 tevent_req_callback_data(subreq
,
5013 struct smb2cli_validate_negotiate_info_state
*state
=
5014 tevent_req_data(req
,
5015 struct smb2cli_validate_negotiate_info_state
);
5018 uint32_t capabilities
;
5019 DATA_BLOB guid_blob
;
5020 struct GUID server_guid
;
5021 uint16_t security_mode
;
5024 status
= smb2cli_ioctl_recv(subreq
, state
,
5025 &state
->out_input_buffer
,
5026 &state
->out_output_buffer
);
5027 TALLOC_FREE(subreq
);
5028 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_CLOSED
)) {
5030 * The response was signed, but not supported
5032 * Older Windows and Samba releases return
5033 * NT_STATUS_FILE_CLOSED.
5035 tevent_req_done(req
);
5038 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_DEVICE_REQUEST
)) {
5040 * The response was signed, but not supported
5042 * This is returned by the NTVFS based Samba 4.x file server
5045 tevent_req_done(req
);
5048 if (NT_STATUS_EQUAL(status
, NT_STATUS_FS_DRIVER_REQUIRED
)) {
5050 * The response was signed, but not supported
5052 * This is returned by the NTVFS based Samba 4.x file server
5055 tevent_req_done(req
);
5058 if (tevent_req_nterror(req
, status
)) {
5062 if (state
->out_output_buffer
.length
!= 24) {
5063 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
5067 buf
= state
->out_output_buffer
.data
;
5069 capabilities
= IVAL(buf
, 0);
5070 guid_blob
= data_blob_const(buf
+ 4, 16);
5071 status
= GUID_from_data_blob(&guid_blob
, &server_guid
);
5072 if (tevent_req_nterror(req
, status
)) {
5075 security_mode
= CVAL(buf
, 20);
5076 dialect
= SVAL(buf
, 22);
5078 if (capabilities
!= state
->conn
->smb2
.server
.capabilities
) {
5079 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
5083 if (!GUID_equal(&server_guid
, &state
->conn
->smb2
.server
.guid
)) {
5084 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
5088 if (security_mode
!= state
->conn
->smb2
.server
.security_mode
) {
5089 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
5093 if (dialect
!= state
->dialect
) {
5094 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
5098 tevent_req_done(req
);
5101 NTSTATUS
smb2cli_validate_negotiate_info_recv(struct tevent_req
*req
)
5103 return tevent_req_simple_recv_ntstatus(req
);
5106 static int smbXcli_session_destructor(struct smbXcli_session
*session
)
5108 if (session
->conn
== NULL
) {
5112 DLIST_REMOVE(session
->conn
->sessions
, session
);
5116 struct smbXcli_session
*smbXcli_session_create(TALLOC_CTX
*mem_ctx
,
5117 struct smbXcli_conn
*conn
)
5119 struct smbXcli_session
*session
;
5121 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
5122 if (session
== NULL
) {
5125 session
->smb2
= talloc_zero(session
, struct smb2cli_session
);
5126 if (session
->smb2
== NULL
) {
5127 talloc_free(session
);
5130 talloc_set_destructor(session
, smbXcli_session_destructor
);
5132 DLIST_ADD_END(conn
->sessions
, session
, struct smbXcli_session
*);
5133 session
->conn
= conn
;
5135 memcpy(session
->smb2_channel
.preauth_sha512
,
5136 conn
->smb2
.preauth_sha512
,
5137 sizeof(session
->smb2_channel
.preauth_sha512
));
5142 struct smbXcli_session
*smbXcli_session_copy(TALLOC_CTX
*mem_ctx
,
5143 struct smbXcli_session
*src
)
5145 struct smbXcli_session
*session
;
5147 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
5148 if (session
== NULL
) {
5151 session
->smb2
= talloc_zero(session
, struct smb2cli_session
);
5152 if (session
->smb2
== NULL
) {
5153 talloc_free(session
);
5157 session
->conn
= src
->conn
;
5158 *session
->smb2
= *src
->smb2
;
5159 session
->smb2_channel
= src
->smb2_channel
;
5160 session
->disconnect_expired
= src
->disconnect_expired
;
5162 DLIST_ADD_END(src
->conn
->sessions
, session
, struct smbXcli_session
*);
5163 talloc_set_destructor(session
, smbXcli_session_destructor
);
5168 bool smbXcli_session_is_authenticated(struct smbXcli_session
*session
)
5170 const DATA_BLOB
*application_key
;
5172 if (session
->conn
== NULL
) {
5177 * If we have an application key we had a session key negotiated
5180 if (session
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
5181 application_key
= &session
->smb2
->application_key
;
5183 application_key
= &session
->smb1
.application_key
;
5186 if (application_key
->length
== 0) {
5193 NTSTATUS
smbXcli_session_application_key(struct smbXcli_session
*session
,
5194 TALLOC_CTX
*mem_ctx
,
5197 const DATA_BLOB
*application_key
;
5199 *key
= data_blob_null
;
5201 if (session
->conn
== NULL
) {
5202 return NT_STATUS_NO_USER_SESSION_KEY
;
5205 if (session
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
5206 application_key
= &session
->smb2
->application_key
;
5208 application_key
= &session
->smb1
.application_key
;
5211 if (application_key
->length
== 0) {
5212 return NT_STATUS_NO_USER_SESSION_KEY
;
5215 *key
= data_blob_dup_talloc(mem_ctx
, *application_key
);
5216 if (key
->data
== NULL
) {
5217 return NT_STATUS_NO_MEMORY
;
5220 return NT_STATUS_OK
;
5223 void smbXcli_session_set_disconnect_expired(struct smbXcli_session
*session
)
5225 session
->disconnect_expired
= true;
5228 uint16_t smb1cli_session_current_id(struct smbXcli_session
*session
)
5230 return session
->smb1
.session_id
;
5233 void smb1cli_session_set_id(struct smbXcli_session
*session
,
5234 uint16_t session_id
)
5236 session
->smb1
.session_id
= session_id
;
5239 NTSTATUS
smb1cli_session_set_session_key(struct smbXcli_session
*session
,
5240 const DATA_BLOB _session_key
)
5242 struct smbXcli_conn
*conn
= session
->conn
;
5243 uint8_t session_key
[16];
5246 return NT_STATUS_INVALID_PARAMETER_MIX
;
5249 if (session
->smb1
.application_key
.length
!= 0) {
5251 * TODO: do not allow this...
5253 * return NT_STATUS_INVALID_PARAMETER_MIX;
5255 data_blob_clear_free(&session
->smb1
.application_key
);
5256 session
->smb1
.protected_key
= false;
5259 if (_session_key
.length
== 0) {
5260 return NT_STATUS_OK
;
5263 ZERO_STRUCT(session_key
);
5264 memcpy(session_key
, _session_key
.data
,
5265 MIN(_session_key
.length
, sizeof(session_key
)));
5267 session
->smb1
.application_key
= data_blob_talloc(session
,
5269 sizeof(session_key
));
5270 ZERO_STRUCT(session_key
);
5271 if (session
->smb1
.application_key
.data
== NULL
) {
5272 return NT_STATUS_NO_MEMORY
;
5275 session
->smb1
.protected_key
= false;
5277 return NT_STATUS_OK
;
5280 NTSTATUS
smb1cli_session_protect_session_key(struct smbXcli_session
*session
)
5282 if (session
->smb1
.protected_key
) {
5283 /* already protected */
5284 return NT_STATUS_OK
;
5287 if (session
->smb1
.application_key
.length
!= 16) {
5288 return NT_STATUS_INVALID_PARAMETER_MIX
;
5291 smb_key_derivation(session
->smb1
.application_key
.data
,
5292 session
->smb1
.application_key
.length
,
5293 session
->smb1
.application_key
.data
);
5295 session
->smb1
.protected_key
= true;
5297 return NT_STATUS_OK
;
5300 uint8_t smb2cli_session_security_mode(struct smbXcli_session
*session
)
5302 struct smbXcli_conn
*conn
= session
->conn
;
5303 uint8_t security_mode
= 0;
5306 return security_mode
;
5309 security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
5310 if (conn
->mandatory_signing
) {
5311 security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
5314 return security_mode
;
5317 uint64_t smb2cli_session_current_id(struct smbXcli_session
*session
)
5319 return session
->smb2
->session_id
;
5322 uint16_t smb2cli_session_get_flags(struct smbXcli_session
*session
)
5324 return session
->smb2
->session_flags
;
5327 void smb2cli_session_set_id_and_flags(struct smbXcli_session
*session
,
5328 uint64_t session_id
,
5329 uint16_t session_flags
)
5331 session
->smb2
->session_id
= session_id
;
5332 session
->smb2
->session_flags
= session_flags
;
5335 void smb2cli_session_increment_channel_sequence(struct smbXcli_session
*session
)
5337 session
->smb2
->channel_sequence
+= 1;
5340 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session
*session
,
5341 uint16_t channel_sequence
)
5345 prev_cs
= session
->smb2
->channel_sequence
;
5346 session
->smb2
->channel_sequence
= channel_sequence
;
5351 void smb2cli_session_start_replay(struct smbXcli_session
*session
)
5353 session
->smb2
->replay_active
= true;
5356 void smb2cli_session_stop_replay(struct smbXcli_session
*session
)
5358 session
->smb2
->replay_active
= false;
5361 NTSTATUS
smb2cli_session_update_preauth(struct smbXcli_session
*session
,
5362 const struct iovec
*iov
)
5364 struct hc_sha512state sctx
;
5367 if (session
->conn
== NULL
) {
5368 return NT_STATUS_INTERNAL_ERROR
;
5371 if (session
->conn
->protocol
< PROTOCOL_SMB3_10
) {
5372 return NT_STATUS_OK
;
5375 if (session
->smb2_channel
.signing_key
.length
!= 0) {
5376 return NT_STATUS_OK
;
5379 samba_SHA512_Init(&sctx
);
5380 samba_SHA512_Update(&sctx
, session
->smb2_channel
.preauth_sha512
,
5381 sizeof(session
->smb2_channel
.preauth_sha512
));
5382 for (i
= 0; i
< 3; i
++) {
5383 samba_SHA512_Update(&sctx
, iov
[i
].iov_base
, iov
[i
].iov_len
);
5385 samba_SHA512_Final(session
->smb2_channel
.preauth_sha512
, &sctx
);
5387 return NT_STATUS_OK
;
5390 NTSTATUS
smb2cli_session_set_session_key(struct smbXcli_session
*session
,
5391 const DATA_BLOB _session_key
,
5392 const struct iovec
*recv_iov
)
5394 struct smbXcli_conn
*conn
= session
->conn
;
5395 uint16_t no_sign_flags
;
5396 uint8_t session_key
[16];
5397 bool check_signature
= true;
5400 struct _derivation
{
5405 struct _derivation signing
;
5406 struct _derivation encryption
;
5407 struct _derivation decryption
;
5408 struct _derivation application
;
5412 return NT_STATUS_INVALID_PARAMETER_MIX
;
5415 if (recv_iov
[0].iov_len
!= SMB2_HDR_BODY
) {
5416 return NT_STATUS_INVALID_PARAMETER_MIX
;
5419 no_sign_flags
= SMB2_SESSION_FLAG_IS_GUEST
| SMB2_SESSION_FLAG_IS_NULL
;
5421 if (session
->smb2
->session_flags
& no_sign_flags
) {
5422 session
->smb2
->should_sign
= false;
5423 return NT_STATUS_OK
;
5426 if (session
->smb2
->signing_key
.length
!= 0) {
5427 return NT_STATUS_INVALID_PARAMETER_MIX
;
5430 if (conn
->protocol
>= PROTOCOL_SMB3_10
) {
5431 struct _derivation
*d
;
5434 p
= data_blob_const(session
->smb2_channel
.preauth_sha512
,
5435 sizeof(session
->smb2_channel
.preauth_sha512
));
5437 d
= &derivation
.signing
;
5438 d
->label
= data_blob_string_const_null("SMBSigningKey");
5441 d
= &derivation
.encryption
;
5442 d
->label
= data_blob_string_const_null("SMBC2SCipherKey");
5445 d
= &derivation
.decryption
;
5446 d
->label
= data_blob_string_const_null("SMBS2CCipherKey");
5449 d
= &derivation
.application
;
5450 d
->label
= data_blob_string_const_null("SMBAppKey");
5453 } else if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
5454 struct _derivation
*d
;
5456 d
= &derivation
.signing
;
5457 d
->label
= data_blob_string_const_null("SMB2AESCMAC");
5458 d
->context
= data_blob_string_const_null("SmbSign");
5460 d
= &derivation
.encryption
;
5461 d
->label
= data_blob_string_const_null("SMB2AESCCM");
5462 d
->context
= data_blob_string_const_null("ServerIn ");
5464 d
= &derivation
.decryption
;
5465 d
->label
= data_blob_string_const_null("SMB2AESCCM");
5466 d
->context
= data_blob_string_const_null("ServerOut");
5468 d
= &derivation
.application
;
5469 d
->label
= data_blob_string_const_null("SMB2APP");
5470 d
->context
= data_blob_string_const_null("SmbRpc");
5473 ZERO_STRUCT(session_key
);
5474 memcpy(session_key
, _session_key
.data
,
5475 MIN(_session_key
.length
, sizeof(session_key
)));
5477 session
->smb2
->signing_key
= data_blob_talloc(session
,
5479 sizeof(session_key
));
5480 if (session
->smb2
->signing_key
.data
== NULL
) {
5481 ZERO_STRUCT(session_key
);
5482 return NT_STATUS_NO_MEMORY
;
5485 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
5486 struct _derivation
*d
= &derivation
.signing
;
5488 smb2_key_derivation(session_key
, sizeof(session_key
),
5489 d
->label
.data
, d
->label
.length
,
5490 d
->context
.data
, d
->context
.length
,
5491 session
->smb2
->signing_key
.data
);
5494 session
->smb2
->encryption_key
= data_blob_dup_talloc(session
,
5495 session
->smb2
->signing_key
);
5496 if (session
->smb2
->encryption_key
.data
== NULL
) {
5497 ZERO_STRUCT(session_key
);
5498 return NT_STATUS_NO_MEMORY
;
5501 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
5502 struct _derivation
*d
= &derivation
.encryption
;
5504 smb2_key_derivation(session_key
, sizeof(session_key
),
5505 d
->label
.data
, d
->label
.length
,
5506 d
->context
.data
, d
->context
.length
,
5507 session
->smb2
->encryption_key
.data
);
5510 session
->smb2
->decryption_key
= data_blob_dup_talloc(session
,
5511 session
->smb2
->signing_key
);
5512 if (session
->smb2
->decryption_key
.data
== NULL
) {
5513 ZERO_STRUCT(session_key
);
5514 return NT_STATUS_NO_MEMORY
;
5517 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
5518 struct _derivation
*d
= &derivation
.decryption
;
5520 smb2_key_derivation(session_key
, sizeof(session_key
),
5521 d
->label
.data
, d
->label
.length
,
5522 d
->context
.data
, d
->context
.length
,
5523 session
->smb2
->decryption_key
.data
);
5526 session
->smb2
->application_key
= data_blob_dup_talloc(session
,
5527 session
->smb2
->signing_key
);
5528 if (session
->smb2
->application_key
.data
== NULL
) {
5529 ZERO_STRUCT(session_key
);
5530 return NT_STATUS_NO_MEMORY
;
5533 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
5534 struct _derivation
*d
= &derivation
.application
;
5536 smb2_key_derivation(session_key
, sizeof(session_key
),
5537 d
->label
.data
, d
->label
.length
,
5538 d
->context
.data
, d
->context
.length
,
5539 session
->smb2
->application_key
.data
);
5541 ZERO_STRUCT(session_key
);
5543 session
->smb2_channel
.signing_key
= data_blob_dup_talloc(session
,
5544 session
->smb2
->signing_key
);
5545 if (session
->smb2_channel
.signing_key
.data
== NULL
) {
5546 return NT_STATUS_NO_MEMORY
;
5549 check_signature
= conn
->mandatory_signing
;
5551 hdr_flags
= IVAL(recv_iov
[0].iov_base
, SMB2_HDR_FLAGS
);
5552 if (hdr_flags
& SMB2_HDR_FLAG_SIGNED
) {
5554 * Sadly some vendors don't sign the
5555 * final SMB2 session setup response
5557 * At least Windows and Samba are always doing this
5558 * if there's a session key available.
5560 * We only check the signature if it's mandatory
5561 * or SMB2_HDR_FLAG_SIGNED is provided.
5563 check_signature
= true;
5566 if (conn
->protocol
>= PROTOCOL_SMB3_10
) {
5567 check_signature
= true;
5570 if (check_signature
) {
5571 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
5572 session
->conn
->protocol
,
5574 if (!NT_STATUS_IS_OK(status
)) {
5579 session
->smb2
->should_sign
= false;
5580 session
->smb2
->should_encrypt
= false;
5582 if (conn
->desire_signing
) {
5583 session
->smb2
->should_sign
= true;
5586 if (conn
->smb2
.server
.security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) {
5587 session
->smb2
->should_sign
= true;
5590 if (session
->smb2
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
) {
5591 session
->smb2
->should_encrypt
= true;
5594 if (conn
->protocol
< PROTOCOL_SMB2_24
) {
5595 session
->smb2
->should_encrypt
= false;
5598 if (conn
->smb2
.server
.cipher
== 0) {
5599 session
->smb2
->should_encrypt
= false;
5602 generate_random_buffer((uint8_t *)&session
->smb2
->nonce_high
,
5603 sizeof(session
->smb2
->nonce_high
));
5604 session
->smb2
->nonce_low
= 1;
5606 return NT_STATUS_OK
;
5609 NTSTATUS
smb2cli_session_create_channel(TALLOC_CTX
*mem_ctx
,
5610 struct smbXcli_session
*session1
,
5611 struct smbXcli_conn
*conn
,
5612 struct smbXcli_session
**_session2
)
5614 struct smbXcli_session
*session2
;
5616 if (session1
->smb2
->signing_key
.length
== 0) {
5617 return NT_STATUS_INVALID_PARAMETER_MIX
;
5621 return NT_STATUS_INVALID_PARAMETER_MIX
;
5624 session2
= talloc_zero(mem_ctx
, struct smbXcli_session
);
5625 if (session2
== NULL
) {
5626 return NT_STATUS_NO_MEMORY
;
5628 session2
->smb2
= talloc_reference(session2
, session1
->smb2
);
5629 if (session2
->smb2
== NULL
) {
5630 talloc_free(session2
);
5631 return NT_STATUS_NO_MEMORY
;
5634 talloc_set_destructor(session2
, smbXcli_session_destructor
);
5635 DLIST_ADD_END(conn
->sessions
, session2
, struct smbXcli_session
*);
5636 session2
->conn
= conn
;
5638 memcpy(session2
->smb2_channel
.preauth_sha512
,
5639 conn
->smb2
.preauth_sha512
,
5640 sizeof(session2
->smb2_channel
.preauth_sha512
));
5642 *_session2
= session2
;
5643 return NT_STATUS_OK
;
5646 NTSTATUS
smb2cli_session_set_channel_key(struct smbXcli_session
*session
,
5647 const DATA_BLOB _channel_key
,
5648 const struct iovec
*recv_iov
)
5650 struct smbXcli_conn
*conn
= session
->conn
;
5651 uint8_t channel_key
[16];
5653 struct _derivation
{
5658 struct _derivation signing
;
5662 return NT_STATUS_INVALID_PARAMETER_MIX
;
5665 if (session
->smb2_channel
.signing_key
.length
!= 0) {
5666 return NT_STATUS_INVALID_PARAMETER_MIX
;
5669 if (conn
->protocol
>= PROTOCOL_SMB3_10
) {
5670 struct _derivation
*d
;
5673 p
= data_blob_const(session
->smb2_channel
.preauth_sha512
,
5674 sizeof(session
->smb2_channel
.preauth_sha512
));
5676 d
= &derivation
.signing
;
5677 d
->label
= data_blob_string_const_null("SMBSigningKey");
5679 } else if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
5680 struct _derivation
*d
;
5682 d
= &derivation
.signing
;
5683 d
->label
= data_blob_string_const_null("SMB2AESCMAC");
5684 d
->context
= data_blob_string_const_null("SmbSign");
5687 ZERO_STRUCT(channel_key
);
5688 memcpy(channel_key
, _channel_key
.data
,
5689 MIN(_channel_key
.length
, sizeof(channel_key
)));
5691 session
->smb2_channel
.signing_key
= data_blob_talloc(session
,
5693 sizeof(channel_key
));
5694 if (session
->smb2_channel
.signing_key
.data
== NULL
) {
5695 ZERO_STRUCT(channel_key
);
5696 return NT_STATUS_NO_MEMORY
;
5699 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
5700 struct _derivation
*d
= &derivation
.signing
;
5702 smb2_key_derivation(channel_key
, sizeof(channel_key
),
5703 d
->label
.data
, d
->label
.length
,
5704 d
->context
.data
, d
->context
.length
,
5705 session
->smb2_channel
.signing_key
.data
);
5707 ZERO_STRUCT(channel_key
);
5709 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
5710 session
->conn
->protocol
,
5712 if (!NT_STATUS_IS_OK(status
)) {
5716 return NT_STATUS_OK
;
5719 NTSTATUS
smb2cli_session_encryption_on(struct smbXcli_session
*session
)
5721 if (session
->smb2
->should_encrypt
) {
5722 return NT_STATUS_OK
;
5725 if (session
->conn
->protocol
< PROTOCOL_SMB2_24
) {
5726 return NT_STATUS_NOT_SUPPORTED
;
5729 if (session
->conn
->smb2
.server
.cipher
== 0) {
5730 return NT_STATUS_NOT_SUPPORTED
;
5733 if (session
->smb2
->signing_key
.data
== NULL
) {
5734 return NT_STATUS_NOT_SUPPORTED
;
5736 session
->smb2
->should_encrypt
= true;
5737 return NT_STATUS_OK
;
5740 struct smbXcli_tcon
*smbXcli_tcon_create(TALLOC_CTX
*mem_ctx
)
5742 struct smbXcli_tcon
*tcon
;
5744 tcon
= talloc_zero(mem_ctx
, struct smbXcli_tcon
);
5752 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon
*tcon
,
5753 uint32_t fs_attributes
)
5755 tcon
->fs_attributes
= fs_attributes
;
5758 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon
*tcon
)
5760 return tcon
->fs_attributes
;
5763 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon
*tcon
)
5769 if (tcon
->is_smb1
) {
5770 if (tcon
->smb1
.optional_support
& SMB_SHARE_IN_DFS
) {
5777 if (tcon
->smb2
.capabilities
& SMB2_SHARE_CAP_DFS
) {
5784 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
5786 return tcon
->smb1
.tcon_id
;
5789 void smb1cli_tcon_set_id(struct smbXcli_tcon
*tcon
, uint16_t tcon_id
)
5791 tcon
->is_smb1
= true;
5792 tcon
->smb1
.tcon_id
= tcon_id
;
5795 bool smb1cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
5797 uint16_t optional_support
,
5798 uint32_t maximal_access
,
5799 uint32_t guest_maximal_access
,
5800 const char *service
,
5801 const char *fs_type
)
5803 tcon
->is_smb1
= true;
5804 tcon
->fs_attributes
= 0;
5805 tcon
->smb1
.tcon_id
= tcon_id
;
5806 tcon
->smb1
.optional_support
= optional_support
;
5807 tcon
->smb1
.maximal_access
= maximal_access
;
5808 tcon
->smb1
.guest_maximal_access
= guest_maximal_access
;
5810 TALLOC_FREE(tcon
->smb1
.service
);
5811 tcon
->smb1
.service
= talloc_strdup(tcon
, service
);
5812 if (service
!= NULL
&& tcon
->smb1
.service
== NULL
) {
5816 TALLOC_FREE(tcon
->smb1
.fs_type
);
5817 tcon
->smb1
.fs_type
= talloc_strdup(tcon
, fs_type
);
5818 if (fs_type
!= NULL
&& tcon
->smb1
.fs_type
== NULL
) {
5825 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
5827 return tcon
->smb2
.tcon_id
;
5830 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon
*tcon
)
5832 return tcon
->smb2
.capabilities
;
5835 void smb2cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
5836 struct smbXcli_session
*session
,
5840 uint32_t capabilities
,
5841 uint32_t maximal_access
)
5843 tcon
->is_smb1
= false;
5844 tcon
->fs_attributes
= 0;
5845 tcon
->smb2
.tcon_id
= tcon_id
;
5846 tcon
->smb2
.type
= type
;
5847 tcon
->smb2
.flags
= flags
;
5848 tcon
->smb2
.capabilities
= capabilities
;
5849 tcon
->smb2
.maximal_access
= maximal_access
;
5851 tcon
->smb2
.should_sign
= false;
5852 tcon
->smb2
.should_encrypt
= false;
5854 if (session
== NULL
) {
5858 tcon
->smb2
.should_sign
= session
->smb2
->should_sign
;
5859 tcon
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
5861 if (flags
& SMB2_SHAREFLAG_ENCRYPT_DATA
) {
5862 tcon
->smb2
.should_encrypt
= true;
5866 void smb2cli_tcon_should_sign(struct smbXcli_tcon
*tcon
,
5869 tcon
->smb2
.should_sign
= should_sign
;
5872 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon
*tcon
)
5874 if (tcon
->smb2
.should_encrypt
) {
5878 return tcon
->smb2
.should_sign
;
5881 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon
*tcon
,
5882 bool should_encrypt
)
5884 tcon
->smb2
.should_encrypt
= should_encrypt
;
5887 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon
*tcon
)
5889 return tcon
->smb2
.should_encrypt
;