2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Stefan Metzmacher 2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/network.h"
23 #include "../lib/async_req/async_sock.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
26 #include "lib/util/util_net.h"
27 #include "lib/util/dlinklist.h"
28 #include "../libcli/smb/smb_common.h"
29 #include "../libcli/smb/smb_seal.h"
30 #include "../libcli/smb/smb_signing.h"
31 #include "../libcli/smb/read_smb.h"
32 #include "smbXcli_base.h"
33 #include "librpc/ndr/libndr.h"
37 struct smbXcli_session
;
43 struct sockaddr_storage local_ss
;
44 struct sockaddr_storage remote_ss
;
45 const char *remote_name
;
47 struct tevent_queue
*outgoing
;
48 struct tevent_req
**pending
;
49 struct tevent_req
*read_smb_req
;
51 enum protocol_types protocol
;
54 bool mandatory_signing
;
57 * The incoming dispatch function should return:
58 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
59 * - NT_STATUS_OK, if no more processing is desired, e.g.
60 * the dispatch function called
62 * - All other return values disconnect the connection.
64 NTSTATUS (*dispatch_incoming
)(struct smbXcli_conn
*conn
,
70 uint32_t capabilities
;
75 uint32_t capabilities
;
78 uint16_t security_mode
;
87 const char *workgroup
;
93 uint32_t capabilities
;
98 struct smb_signing_state
*signing
;
99 struct smb_trans_enc_state
*trans_enc
;
101 struct tevent_req
*read_braw_req
;
106 uint32_t capabilities
;
107 uint16_t security_mode
;
112 uint32_t capabilities
;
113 uint16_t security_mode
;
115 uint32_t max_trans_size
;
116 uint32_t max_read_size
;
117 uint32_t max_write_size
;
124 uint16_t cur_credits
;
125 uint16_t max_credits
;
128 struct smbXcli_session
*sessions
;
131 struct smb2cli_session
{
133 uint16_t session_flags
;
134 DATA_BLOB application_key
;
135 DATA_BLOB signing_key
;
138 DATA_BLOB encryption_key
;
139 DATA_BLOB decryption_key
;
142 uint16_t channel_sequence
;
145 struct smbXcli_session
{
146 struct smbXcli_session
*prev
, *next
;
147 struct smbXcli_conn
*conn
;
151 DATA_BLOB application_key
;
155 struct smb2cli_session
*smb2
;
158 DATA_BLOB signing_key
;
162 * this should be a short term hack
163 * until the upper layers have implemented
166 bool disconnect_expired
;
169 struct smbXcli_tcon
{
171 uint32_t fs_attributes
;
175 uint16_t optional_support
;
176 uint32_t maximal_access
;
177 uint32_t guest_maximal_access
;
186 uint32_t capabilities
;
187 uint32_t maximal_access
;
192 struct smbXcli_req_state
{
193 struct tevent_context
*ev
;
194 struct smbXcli_conn
*conn
;
195 struct smbXcli_session
*session
; /* maybe NULL */
196 struct smbXcli_tcon
*tcon
; /* maybe NULL */
198 uint8_t length_hdr
[4];
205 /* Space for the header including the wct */
206 uint8_t hdr
[HDR_VWV
];
209 * For normal requests, smb1cli_req_send chooses a mid.
210 * SecondaryV trans requests need to use the mid of the primary
211 * request, so we need a place to store it.
212 * Assume it is set if != 0.
217 uint8_t bytecount_buf
[2];
219 #define MAX_SMB_IOV 10
220 /* length_hdr, hdr, words, byte_count, buffers */
221 struct iovec iov
[1 + 3 + MAX_SMB_IOV
];
226 struct tevent_req
**chained_requests
;
229 NTSTATUS recv_status
;
230 /* always an array of 3 talloc elements */
231 struct iovec
*recv_iov
;
235 const uint8_t *fixed
;
240 uint8_t transform
[SMB2_TF_HDR_SIZE
];
241 uint8_t hdr
[SMB2_HDR_BODY
];
242 uint8_t pad
[7]; /* padding space for compounding */
245 * always an array of 3 talloc elements
246 * (without a SMB2_TRANSFORM header!)
250 struct iovec
*recv_iov
;
253 * the expected max for the response dyn_len
255 uint32_t max_dyn_len
;
257 uint16_t credit_charge
;
261 uint64_t encryption_session_id
;
263 bool signing_skipped
;
269 static int smbXcli_conn_destructor(struct smbXcli_conn
*conn
)
272 * NT_STATUS_OK, means we do not notify the callers
274 smbXcli_conn_disconnect(conn
, NT_STATUS_OK
);
276 while (conn
->sessions
) {
277 conn
->sessions
->conn
= NULL
;
278 DLIST_REMOVE(conn
->sessions
, conn
->sessions
);
281 if (conn
->smb1
.trans_enc
) {
282 TALLOC_FREE(conn
->smb1
.trans_enc
);
288 struct smbXcli_conn
*smbXcli_conn_create(TALLOC_CTX
*mem_ctx
,
290 const char *remote_name
,
291 enum smb_signing_setting signing_state
,
292 uint32_t smb1_capabilities
,
293 struct GUID
*client_guid
,
294 uint32_t smb2_capabilities
)
296 struct smbXcli_conn
*conn
= NULL
;
298 struct sockaddr
*sa
= NULL
;
302 conn
= talloc_zero(mem_ctx
, struct smbXcli_conn
);
308 conn
->write_fd
= dup(fd
);
309 if (conn
->write_fd
== -1) {
313 conn
->remote_name
= talloc_strdup(conn
, remote_name
);
314 if (conn
->remote_name
== NULL
) {
319 ss
= (void *)&conn
->local_ss
;
320 sa
= (struct sockaddr
*)ss
;
321 sa_length
= sizeof(conn
->local_ss
);
322 ret
= getsockname(fd
, sa
, &sa_length
);
326 ss
= (void *)&conn
->remote_ss
;
327 sa
= (struct sockaddr
*)ss
;
328 sa_length
= sizeof(conn
->remote_ss
);
329 ret
= getpeername(fd
, sa
, &sa_length
);
334 conn
->outgoing
= tevent_queue_create(conn
, "smbXcli_outgoing");
335 if (conn
->outgoing
== NULL
) {
338 conn
->pending
= NULL
;
340 conn
->protocol
= PROTOCOL_NONE
;
342 switch (signing_state
) {
343 case SMB_SIGNING_OFF
:
345 conn
->allow_signing
= false;
346 conn
->desire_signing
= false;
347 conn
->mandatory_signing
= false;
349 case SMB_SIGNING_DEFAULT
:
350 case SMB_SIGNING_IF_REQUIRED
:
351 /* if the server requires it */
352 conn
->allow_signing
= true;
353 conn
->desire_signing
= false;
354 conn
->mandatory_signing
= false;
356 case SMB_SIGNING_REQUIRED
:
358 conn
->allow_signing
= true;
359 conn
->desire_signing
= true;
360 conn
->mandatory_signing
= true;
364 conn
->smb1
.client
.capabilities
= smb1_capabilities
;
365 conn
->smb1
.client
.max_xmit
= UINT16_MAX
;
367 conn
->smb1
.capabilities
= conn
->smb1
.client
.capabilities
;
368 conn
->smb1
.max_xmit
= 1024;
372 /* initialise signing */
373 conn
->smb1
.signing
= smb_signing_init(conn
,
375 conn
->desire_signing
,
376 conn
->mandatory_signing
);
377 if (!conn
->smb1
.signing
) {
381 conn
->smb2
.client
.security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
382 if (conn
->mandatory_signing
) {
383 conn
->smb2
.client
.security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
386 conn
->smb2
.client
.guid
= *client_guid
;
388 conn
->smb2
.client
.capabilities
= smb2_capabilities
;
390 conn
->smb2
.cur_credits
= 1;
391 conn
->smb2
.max_credits
= 0;
393 talloc_set_destructor(conn
, smbXcli_conn_destructor
);
397 if (conn
->write_fd
!= -1) {
398 close(conn
->write_fd
);
404 bool smbXcli_conn_is_connected(struct smbXcli_conn
*conn
)
410 if (conn
->read_fd
== -1) {
417 enum protocol_types
smbXcli_conn_protocol(struct smbXcli_conn
*conn
)
419 return conn
->protocol
;
422 bool smbXcli_conn_use_unicode(struct smbXcli_conn
*conn
)
424 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
428 if (conn
->smb1
.capabilities
& CAP_UNICODE
) {
435 void smbXcli_conn_set_sockopt(struct smbXcli_conn
*conn
, const char *options
)
437 set_socket_options(conn
->read_fd
, options
);
440 const struct sockaddr_storage
*smbXcli_conn_local_sockaddr(struct smbXcli_conn
*conn
)
442 return &conn
->local_ss
;
445 const struct sockaddr_storage
*smbXcli_conn_remote_sockaddr(struct smbXcli_conn
*conn
)
447 return &conn
->remote_ss
;
450 const char *smbXcli_conn_remote_name(struct smbXcli_conn
*conn
)
452 return conn
->remote_name
;
455 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn
*conn
)
457 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
464 return conn
->smb1
.server
.max_mux
;
467 NTTIME
smbXcli_conn_server_system_time(struct smbXcli_conn
*conn
)
469 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
470 return conn
->smb2
.server
.system_time
;
473 return conn
->smb1
.server
.system_time
;
476 const DATA_BLOB
*smbXcli_conn_server_gss_blob(struct smbXcli_conn
*conn
)
478 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
479 return &conn
->smb2
.server
.gss_blob
;
482 return &conn
->smb1
.server
.gss_blob
;
485 const struct GUID
*smbXcli_conn_server_guid(struct smbXcli_conn
*conn
)
487 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
488 return &conn
->smb2
.server
.guid
;
491 return &conn
->smb1
.server
.guid
;
494 struct smbXcli_conn_samba_suicide_state
{
495 struct smbXcli_conn
*conn
;
500 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
);
502 struct tevent_req
*smbXcli_conn_samba_suicide_send(TALLOC_CTX
*mem_ctx
,
503 struct tevent_context
*ev
,
504 struct smbXcli_conn
*conn
,
507 struct tevent_req
*req
, *subreq
;
508 struct smbXcli_conn_samba_suicide_state
*state
;
510 req
= tevent_req_create(mem_ctx
, &state
,
511 struct smbXcli_conn_samba_suicide_state
);
516 SIVAL(state
->buf
, 4, 0x74697865);
517 SCVAL(state
->buf
, 8, exitcode
);
518 _smb_setlen_nbt(state
->buf
, sizeof(state
->buf
)-4);
520 state
->iov
.iov_base
= state
->buf
;
521 state
->iov
.iov_len
= sizeof(state
->buf
);
523 subreq
= writev_send(state
, ev
, conn
->outgoing
, conn
->write_fd
,
524 false, &state
->iov
, 1);
525 if (tevent_req_nomem(subreq
, req
)) {
526 return tevent_req_post(req
, ev
);
528 tevent_req_set_callback(subreq
, smbXcli_conn_samba_suicide_done
, req
);
532 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
)
534 struct tevent_req
*req
= tevent_req_callback_data(
535 subreq
, struct tevent_req
);
536 struct smbXcli_conn_samba_suicide_state
*state
= tevent_req_data(
537 req
, struct smbXcli_conn_samba_suicide_state
);
541 nwritten
= writev_recv(subreq
, &err
);
543 if (nwritten
== -1) {
544 NTSTATUS status
= map_nt_error_from_unix_common(err
);
545 smbXcli_conn_disconnect(state
->conn
, status
);
548 tevent_req_done(req
);
551 NTSTATUS
smbXcli_conn_samba_suicide_recv(struct tevent_req
*req
)
553 return tevent_req_simple_recv_ntstatus(req
);
556 NTSTATUS
smbXcli_conn_samba_suicide(struct smbXcli_conn
*conn
,
559 TALLOC_CTX
*frame
= talloc_stackframe();
560 struct tevent_context
*ev
;
561 struct tevent_req
*req
;
562 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
565 if (smbXcli_conn_has_async_calls(conn
)) {
567 * Can't use sync call while an async call is in flight
569 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
572 ev
= samba_tevent_context_init(frame
);
576 req
= smbXcli_conn_samba_suicide_send(frame
, ev
, conn
, exitcode
);
580 ok
= tevent_req_poll(req
, ev
);
582 status
= map_nt_error_from_unix_common(errno
);
585 status
= smbXcli_conn_samba_suicide_recv(req
);
591 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn
*conn
)
593 return conn
->smb1
.capabilities
;
596 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn
*conn
)
598 return conn
->smb1
.max_xmit
;
601 bool smb1cli_conn_req_possible(struct smbXcli_conn
*conn
)
604 uint16_t possible
= conn
->smb1
.server
.max_mux
;
606 pending
= tevent_queue_length(conn
->outgoing
);
607 if (pending
>= possible
) {
610 pending
+= talloc_array_length(conn
->pending
);
611 if (pending
>= possible
) {
618 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn
*conn
)
620 return conn
->smb1
.server
.session_key
;
623 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn
*conn
)
625 return conn
->smb1
.server
.challenge
;
628 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
630 return conn
->smb1
.server
.security_mode
;
633 bool smb1cli_conn_server_readbraw(struct smbXcli_conn
*conn
)
635 return conn
->smb1
.server
.readbraw
;
638 bool smb1cli_conn_server_writebraw(struct smbXcli_conn
*conn
)
640 return conn
->smb1
.server
.writebraw
;
643 bool smb1cli_conn_server_lockread(struct smbXcli_conn
*conn
)
645 return conn
->smb1
.server
.lockread
;
648 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn
*conn
)
650 return conn
->smb1
.server
.writeunlock
;
653 int smb1cli_conn_server_time_zone(struct smbXcli_conn
*conn
)
655 return conn
->smb1
.server
.time_zone
;
658 bool smb1cli_conn_activate_signing(struct smbXcli_conn
*conn
,
659 const DATA_BLOB user_session_key
,
660 const DATA_BLOB response
)
662 return smb_signing_activate(conn
->smb1
.signing
,
667 bool smb1cli_conn_check_signing(struct smbXcli_conn
*conn
,
668 const uint8_t *buf
, uint32_t seqnum
)
670 const uint8_t *hdr
= buf
+ NBT_HDR_SIZE
;
671 size_t len
= smb_len_nbt(buf
);
673 return smb_signing_check_pdu(conn
->smb1
.signing
, hdr
, len
, seqnum
);
676 bool smb1cli_conn_signing_is_active(struct smbXcli_conn
*conn
)
678 return smb_signing_is_active(conn
->smb1
.signing
);
681 void smb1cli_conn_set_encryption(struct smbXcli_conn
*conn
,
682 struct smb_trans_enc_state
*es
)
684 /* Replace the old state, if any. */
685 if (conn
->smb1
.trans_enc
) {
686 TALLOC_FREE(conn
->smb1
.trans_enc
);
688 conn
->smb1
.trans_enc
= es
;
691 bool smb1cli_conn_encryption_on(struct smbXcli_conn
*conn
)
693 return common_encryption_on(conn
->smb1
.trans_enc
);
697 static NTSTATUS
smb1cli_pull_raw_error(const uint8_t *hdr
)
699 uint32_t flags2
= SVAL(hdr
, HDR_FLG2
);
700 NTSTATUS status
= NT_STATUS(IVAL(hdr
, HDR_RCLS
));
702 if (NT_STATUS_IS_OK(status
)) {
706 if (flags2
& FLAGS2_32_BIT_ERROR_CODES
) {
710 return NT_STATUS_DOS(CVAL(hdr
, HDR_RCLS
), SVAL(hdr
, HDR_ERR
));
714 * Is the SMB command able to hold an AND_X successor
715 * @param[in] cmd The SMB command in question
716 * @retval Can we add a chained request after "cmd"?
718 bool smb1cli_is_andx_req(uint8_t cmd
)
738 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn
*conn
)
740 size_t num_pending
= talloc_array_length(conn
->pending
);
743 if (conn
->protocol
== PROTOCOL_NONE
) {
745 * This is what windows sends on the SMB1 Negprot request
746 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
754 result
= conn
->smb1
.mid
++;
755 if ((result
== 0) || (result
== 0xffff)) {
759 for (i
=0; i
<num_pending
; i
++) {
760 if (result
== smb1cli_req_mid(conn
->pending
[i
])) {
765 if (i
== num_pending
) {
771 void smbXcli_req_unset_pending(struct tevent_req
*req
)
773 struct smbXcli_req_state
*state
=
775 struct smbXcli_req_state
);
776 struct smbXcli_conn
*conn
= state
->conn
;
777 size_t num_pending
= talloc_array_length(conn
->pending
);
780 if (state
->smb1
.mid
!= 0) {
782 * This is a [nt]trans[2] request which waits
783 * for more than one reply.
788 talloc_set_destructor(req
, NULL
);
790 if (num_pending
== 1) {
792 * The pending read_smb tevent_req is a child of
793 * conn->pending. So if nothing is pending anymore, we need to
794 * delete the socket read fde.
796 TALLOC_FREE(conn
->pending
);
797 conn
->read_smb_req
= NULL
;
801 for (i
=0; i
<num_pending
; i
++) {
802 if (req
== conn
->pending
[i
]) {
806 if (i
== num_pending
) {
808 * Something's seriously broken. Just returning here is the
809 * right thing nevertheless, the point of this routine is to
810 * remove ourselves from conn->pending.
816 * Remove ourselves from the conn->pending array
818 for (; i
< (num_pending
- 1); i
++) {
819 conn
->pending
[i
] = conn
->pending
[i
+1];
823 * No NULL check here, we're shrinking by sizeof(void *), and
824 * talloc_realloc just adjusts the size for this.
826 conn
->pending
= talloc_realloc(NULL
, conn
->pending
, struct tevent_req
*,
831 static int smbXcli_req_destructor(struct tevent_req
*req
)
833 struct smbXcli_req_state
*state
=
835 struct smbXcli_req_state
);
838 * Make sure we really remove it from
839 * the pending array on destruction.
842 smbXcli_req_unset_pending(req
);
846 static bool smb1cli_req_cancel(struct tevent_req
*req
);
847 static bool smb2cli_req_cancel(struct tevent_req
*req
);
849 static bool smbXcli_req_cancel(struct tevent_req
*req
)
851 struct smbXcli_req_state
*state
=
853 struct smbXcli_req_state
);
855 if (!smbXcli_conn_is_connected(state
->conn
)) {
859 if (state
->conn
->protocol
== PROTOCOL_NONE
) {
863 if (state
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
864 return smb2cli_req_cancel(req
);
867 return smb1cli_req_cancel(req
);
870 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
);
872 bool smbXcli_req_set_pending(struct tevent_req
*req
)
874 struct smbXcli_req_state
*state
=
876 struct smbXcli_req_state
);
877 struct smbXcli_conn
*conn
;
878 struct tevent_req
**pending
;
883 if (!smbXcli_conn_is_connected(conn
)) {
887 num_pending
= talloc_array_length(conn
->pending
);
889 pending
= talloc_realloc(conn
, conn
->pending
, struct tevent_req
*,
891 if (pending
== NULL
) {
894 pending
[num_pending
] = req
;
895 conn
->pending
= pending
;
896 talloc_set_destructor(req
, smbXcli_req_destructor
);
897 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
899 if (!smbXcli_conn_receive_next(conn
)) {
901 * the caller should notify the current request
903 * And all other pending requests get notified
904 * by smbXcli_conn_disconnect().
906 smbXcli_req_unset_pending(req
);
907 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
914 static void smbXcli_conn_received(struct tevent_req
*subreq
);
916 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
)
918 size_t num_pending
= talloc_array_length(conn
->pending
);
919 struct tevent_req
*req
;
920 struct smbXcli_req_state
*state
;
922 if (conn
->read_smb_req
!= NULL
) {
926 if (num_pending
== 0) {
927 if (conn
->smb2
.mid
< UINT64_MAX
) {
928 /* no more pending requests, so we are done for now */
933 * If there are no more SMB2 requests possible,
934 * because we are out of message ids,
935 * we need to disconnect.
937 smbXcli_conn_disconnect(conn
, NT_STATUS_CONNECTION_ABORTED
);
941 req
= conn
->pending
[0];
942 state
= tevent_req_data(req
, struct smbXcli_req_state
);
945 * We're the first ones, add the read_smb request that waits for the
946 * answer from the server
948 conn
->read_smb_req
= read_smb_send(conn
->pending
,
951 if (conn
->read_smb_req
== NULL
) {
954 tevent_req_set_callback(conn
->read_smb_req
, smbXcli_conn_received
, conn
);
958 void smbXcli_conn_disconnect(struct smbXcli_conn
*conn
, NTSTATUS status
)
960 struct smbXcli_session
*session
;
962 tevent_queue_stop(conn
->outgoing
);
964 if (conn
->read_fd
!= -1) {
965 close(conn
->read_fd
);
967 if (conn
->write_fd
!= -1) {
968 close(conn
->write_fd
);
973 session
= conn
->sessions
;
974 if (talloc_array_length(conn
->pending
) == 0) {
976 * if we do not have pending requests
977 * there is no need to update the channel_sequence
981 for (; session
; session
= session
->next
) {
982 smb2cli_session_increment_channel_sequence(session
);
986 * Cancel all pending requests. We do not do a for-loop walking
987 * conn->pending because that array changes in
988 * smbXcli_req_unset_pending.
990 while (talloc_array_length(conn
->pending
) > 0) {
991 struct tevent_req
*req
;
992 struct smbXcli_req_state
*state
;
993 struct tevent_req
**chain
;
997 req
= conn
->pending
[0];
998 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1000 if (state
->smb1
.chained_requests
== NULL
) {
1002 * We're dead. No point waiting for trans2
1005 state
->smb1
.mid
= 0;
1007 smbXcli_req_unset_pending(req
);
1009 if (NT_STATUS_IS_OK(status
)) {
1010 /* do not notify the callers */
1015 * we need to defer the callback, because we may notify
1016 * more then one caller.
1018 tevent_req_defer_callback(req
, state
->ev
);
1019 tevent_req_nterror(req
, status
);
1023 chain
= talloc_move(conn
, &state
->smb1
.chained_requests
);
1024 num_chained
= talloc_array_length(chain
);
1026 for (i
=0; i
<num_chained
; i
++) {
1028 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1031 * We're dead. No point waiting for trans2
1034 state
->smb1
.mid
= 0;
1036 smbXcli_req_unset_pending(req
);
1038 if (NT_STATUS_IS_OK(status
)) {
1039 /* do not notify the callers */
1044 * we need to defer the callback, because we may notify
1045 * more than one caller.
1047 tevent_req_defer_callback(req
, state
->ev
);
1048 tevent_req_nterror(req
, status
);
1055 * Fetch a smb request's mid. Only valid after the request has been sent by
1056 * smb1cli_req_send().
1058 uint16_t smb1cli_req_mid(struct tevent_req
*req
)
1060 struct smbXcli_req_state
*state
=
1061 tevent_req_data(req
,
1062 struct smbXcli_req_state
);
1064 if (state
->smb1
.mid
!= 0) {
1065 return state
->smb1
.mid
;
1068 return SVAL(state
->smb1
.hdr
, HDR_MID
);
1071 void smb1cli_req_set_mid(struct tevent_req
*req
, uint16_t mid
)
1073 struct smbXcli_req_state
*state
=
1074 tevent_req_data(req
,
1075 struct smbXcli_req_state
);
1077 state
->smb1
.mid
= mid
;
1080 uint32_t smb1cli_req_seqnum(struct tevent_req
*req
)
1082 struct smbXcli_req_state
*state
=
1083 tevent_req_data(req
,
1084 struct smbXcli_req_state
);
1086 return state
->smb1
.seqnum
;
1089 void smb1cli_req_set_seqnum(struct tevent_req
*req
, uint32_t seqnum
)
1091 struct smbXcli_req_state
*state
=
1092 tevent_req_data(req
,
1093 struct smbXcli_req_state
);
1095 state
->smb1
.seqnum
= seqnum
;
1098 static size_t smbXcli_iov_len(const struct iovec
*iov
, int count
)
1102 for (i
=0; i
<count
; i
++) {
1103 result
+= iov
[i
].iov_len
;
1108 static uint8_t *smbXcli_iov_concat(TALLOC_CTX
*mem_ctx
,
1109 const struct iovec
*iov
,
1112 size_t len
= smbXcli_iov_len(iov
, count
);
1117 buf
= talloc_array(mem_ctx
, uint8_t, len
);
1122 for (i
=0; i
<count
; i
++) {
1123 memcpy(buf
+copied
, iov
[i
].iov_base
, iov
[i
].iov_len
);
1124 copied
+= iov
[i
].iov_len
;
1129 static void smb1cli_req_flags(enum protocol_types protocol
,
1130 uint32_t smb1_capabilities
,
1131 uint8_t smb_command
,
1132 uint8_t additional_flags
,
1133 uint8_t clear_flags
,
1135 uint16_t additional_flags2
,
1136 uint16_t clear_flags2
,
1140 uint16_t flags2
= 0;
1142 if (protocol
>= PROTOCOL_LANMAN1
) {
1143 flags
|= FLAG_CASELESS_PATHNAMES
;
1144 flags
|= FLAG_CANONICAL_PATHNAMES
;
1147 if (protocol
>= PROTOCOL_LANMAN2
) {
1148 flags2
|= FLAGS2_LONG_PATH_COMPONENTS
;
1149 flags2
|= FLAGS2_EXTENDED_ATTRIBUTES
;
1152 if (protocol
>= PROTOCOL_NT1
) {
1153 flags2
|= FLAGS2_IS_LONG_NAME
;
1155 if (smb1_capabilities
& CAP_UNICODE
) {
1156 flags2
|= FLAGS2_UNICODE_STRINGS
;
1158 if (smb1_capabilities
& CAP_STATUS32
) {
1159 flags2
|= FLAGS2_32_BIT_ERROR_CODES
;
1161 if (smb1_capabilities
& CAP_EXTENDED_SECURITY
) {
1162 flags2
|= FLAGS2_EXTENDED_SECURITY
;
1166 flags
|= additional_flags
;
1167 flags
&= ~clear_flags
;
1168 flags2
|= additional_flags2
;
1169 flags2
&= ~clear_flags2
;
1175 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
);
1177 static bool smb1cli_req_cancel(struct tevent_req
*req
)
1179 struct smbXcli_req_state
*state
=
1180 tevent_req_data(req
,
1181 struct smbXcli_req_state
);
1186 struct tevent_req
*subreq
;
1189 flags
= CVAL(state
->smb1
.hdr
, HDR_FLG
);
1190 flags2
= SVAL(state
->smb1
.hdr
, HDR_FLG2
);
1191 pid
= SVAL(state
->smb1
.hdr
, HDR_PID
);
1192 pid
|= SVAL(state
->smb1
.hdr
, HDR_PIDHIGH
)<<16;
1193 mid
= SVAL(state
->smb1
.hdr
, HDR_MID
);
1195 subreq
= smb1cli_req_create(state
, state
->ev
,
1205 0, NULL
); /* bytes */
1206 if (subreq
== NULL
) {
1209 smb1cli_req_set_mid(subreq
, mid
);
1211 status
= smb1cli_req_chain_submit(&subreq
, 1);
1212 if (!NT_STATUS_IS_OK(status
)) {
1213 TALLOC_FREE(subreq
);
1216 smb1cli_req_set_mid(subreq
, 0);
1218 tevent_req_set_callback(subreq
, smb1cli_req_cancel_done
, NULL
);
1223 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
)
1225 /* we do not care about the result */
1226 TALLOC_FREE(subreq
);
1229 struct tevent_req
*smb1cli_req_create(TALLOC_CTX
*mem_ctx
,
1230 struct tevent_context
*ev
,
1231 struct smbXcli_conn
*conn
,
1232 uint8_t smb_command
,
1233 uint8_t additional_flags
,
1234 uint8_t clear_flags
,
1235 uint16_t additional_flags2
,
1236 uint16_t clear_flags2
,
1237 uint32_t timeout_msec
,
1239 struct smbXcli_tcon
*tcon
,
1240 struct smbXcli_session
*session
,
1241 uint8_t wct
, uint16_t *vwv
,
1243 struct iovec
*bytes_iov
)
1245 struct tevent_req
*req
;
1246 struct smbXcli_req_state
*state
;
1248 uint16_t flags2
= 0;
1252 if (iov_count
> MAX_SMB_IOV
) {
1254 * Should not happen :-)
1259 req
= tevent_req_create(mem_ctx
, &state
,
1260 struct smbXcli_req_state
);
1266 state
->session
= session
;
1270 uid
= session
->smb1
.session_id
;
1274 tid
= tcon
->smb1
.tcon_id
;
1276 if (tcon
->fs_attributes
& FILE_CASE_SENSITIVE_SEARCH
) {
1277 clear_flags
|= FLAG_CASELESS_PATHNAMES
;
1279 /* Default setting, case insensitive. */
1280 additional_flags
|= FLAG_CASELESS_PATHNAMES
;
1283 if (smbXcli_conn_dfs_supported(conn
) &&
1284 smbXcli_tcon_is_dfs_share(tcon
))
1286 additional_flags2
|= FLAGS2_DFS_PATHNAMES
;
1290 state
->smb1
.recv_cmd
= 0xFF;
1291 state
->smb1
.recv_status
= NT_STATUS_INTERNAL_ERROR
;
1292 state
->smb1
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
1293 if (state
->smb1
.recv_iov
== NULL
) {
1298 smb1cli_req_flags(conn
->protocol
,
1299 conn
->smb1
.capabilities
,
1308 SIVAL(state
->smb1
.hdr
, 0, SMB_MAGIC
);
1309 SCVAL(state
->smb1
.hdr
, HDR_COM
, smb_command
);
1310 SIVAL(state
->smb1
.hdr
, HDR_RCLS
, NT_STATUS_V(NT_STATUS_OK
));
1311 SCVAL(state
->smb1
.hdr
, HDR_FLG
, flags
);
1312 SSVAL(state
->smb1
.hdr
, HDR_FLG2
, flags2
);
1313 SSVAL(state
->smb1
.hdr
, HDR_PIDHIGH
, pid
>> 16);
1314 SSVAL(state
->smb1
.hdr
, HDR_TID
, tid
);
1315 SSVAL(state
->smb1
.hdr
, HDR_PID
, pid
);
1316 SSVAL(state
->smb1
.hdr
, HDR_UID
, uid
);
1317 SSVAL(state
->smb1
.hdr
, HDR_MID
, 0); /* this comes later */
1318 SCVAL(state
->smb1
.hdr
, HDR_WCT
, wct
);
1320 state
->smb1
.vwv
= vwv
;
1322 SSVAL(state
->smb1
.bytecount_buf
, 0, smbXcli_iov_len(bytes_iov
, iov_count
));
1324 state
->smb1
.iov
[0].iov_base
= (void *)state
->length_hdr
;
1325 state
->smb1
.iov
[0].iov_len
= sizeof(state
->length_hdr
);
1326 state
->smb1
.iov
[1].iov_base
= (void *)state
->smb1
.hdr
;
1327 state
->smb1
.iov
[1].iov_len
= sizeof(state
->smb1
.hdr
);
1328 state
->smb1
.iov
[2].iov_base
= (void *)state
->smb1
.vwv
;
1329 state
->smb1
.iov
[2].iov_len
= wct
* sizeof(uint16_t);
1330 state
->smb1
.iov
[3].iov_base
= (void *)state
->smb1
.bytecount_buf
;
1331 state
->smb1
.iov
[3].iov_len
= sizeof(uint16_t);
1333 if (iov_count
!= 0) {
1334 memcpy(&state
->smb1
.iov
[4], bytes_iov
,
1335 iov_count
* sizeof(*bytes_iov
));
1337 state
->smb1
.iov_count
= iov_count
+ 4;
1339 if (timeout_msec
> 0) {
1340 struct timeval endtime
;
1342 endtime
= timeval_current_ofs_msec(timeout_msec
);
1343 if (!tevent_req_set_endtime(req
, ev
, endtime
)) {
1348 switch (smb_command
) {
1352 state
->one_way
= true;
1355 state
->one_way
= true;
1356 state
->smb1
.one_way_seqnum
= true;
1360 (CVAL(vwv
+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE
)) {
1361 state
->one_way
= true;
1369 static NTSTATUS
smb1cli_conn_signv(struct smbXcli_conn
*conn
,
1370 struct iovec
*iov
, int iov_count
,
1372 bool one_way_seqnum
)
1374 TALLOC_CTX
*frame
= NULL
;
1378 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1379 * iovec directly. MD5Update would do that just fine.
1382 if (iov_count
< 4) {
1383 return NT_STATUS_INVALID_PARAMETER_MIX
;
1385 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1386 return NT_STATUS_INVALID_PARAMETER_MIX
;
1388 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1389 return NT_STATUS_INVALID_PARAMETER_MIX
;
1391 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1392 return NT_STATUS_INVALID_PARAMETER_MIX
;
1394 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1395 return NT_STATUS_INVALID_PARAMETER_MIX
;
1398 frame
= talloc_stackframe();
1400 buf
= smbXcli_iov_concat(frame
, &iov
[1], iov_count
- 1);
1402 return NT_STATUS_NO_MEMORY
;
1405 *seqnum
= smb_signing_next_seqnum(conn
->smb1
.signing
,
1407 smb_signing_sign_pdu(conn
->smb1
.signing
,
1408 buf
, talloc_get_size(buf
),
1410 memcpy(iov
[1].iov_base
, buf
, iov
[1].iov_len
);
1413 return NT_STATUS_OK
;
1416 static void smb1cli_req_writev_done(struct tevent_req
*subreq
);
1417 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1418 TALLOC_CTX
*tmp_mem
,
1421 static NTSTATUS
smb1cli_req_writev_submit(struct tevent_req
*req
,
1422 struct smbXcli_req_state
*state
,
1423 struct iovec
*iov
, int iov_count
)
1425 struct tevent_req
*subreq
;
1430 if (!smbXcli_conn_is_connected(state
->conn
)) {
1431 return NT_STATUS_CONNECTION_DISCONNECTED
;
1434 if (state
->conn
->protocol
> PROTOCOL_NT1
) {
1435 return NT_STATUS_REVISION_MISMATCH
;
1438 if (iov_count
< 4) {
1439 return NT_STATUS_INVALID_PARAMETER_MIX
;
1441 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1442 return NT_STATUS_INVALID_PARAMETER_MIX
;
1444 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1445 return NT_STATUS_INVALID_PARAMETER_MIX
;
1447 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1448 return NT_STATUS_INVALID_PARAMETER_MIX
;
1450 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1451 return NT_STATUS_INVALID_PARAMETER_MIX
;
1454 cmd
= CVAL(iov
[1].iov_base
, HDR_COM
);
1455 if (cmd
== SMBreadBraw
) {
1456 if (smbXcli_conn_has_async_calls(state
->conn
)) {
1457 return NT_STATUS_INVALID_PARAMETER_MIX
;
1459 state
->conn
->smb1
.read_braw_req
= req
;
1462 if (state
->smb1
.mid
!= 0) {
1463 mid
= state
->smb1
.mid
;
1465 mid
= smb1cli_alloc_mid(state
->conn
);
1467 SSVAL(iov
[1].iov_base
, HDR_MID
, mid
);
1469 _smb_setlen_nbt(iov
[0].iov_base
, smbXcli_iov_len(&iov
[1], iov_count
-1));
1471 status
= smb1cli_conn_signv(state
->conn
, iov
, iov_count
,
1472 &state
->smb1
.seqnum
,
1473 state
->smb1
.one_way_seqnum
);
1475 if (!NT_STATUS_IS_OK(status
)) {
1480 * If we supported multiple encrytion contexts
1481 * here we'd look up based on tid.
1483 if (common_encryption_on(state
->conn
->smb1
.trans_enc
)) {
1484 char *buf
, *enc_buf
;
1486 buf
= (char *)smbXcli_iov_concat(talloc_tos(), iov
, iov_count
);
1488 return NT_STATUS_NO_MEMORY
;
1490 status
= common_encrypt_buffer(state
->conn
->smb1
.trans_enc
,
1491 (char *)buf
, &enc_buf
);
1493 if (!NT_STATUS_IS_OK(status
)) {
1494 DEBUG(0, ("Error in encrypting client message: %s\n",
1495 nt_errstr(status
)));
1498 buf
= (char *)talloc_memdup(state
, enc_buf
,
1499 smb_len_nbt(enc_buf
)+4);
1502 return NT_STATUS_NO_MEMORY
;
1504 iov
[0].iov_base
= (void *)buf
;
1505 iov
[0].iov_len
= talloc_get_size(buf
);
1509 if (state
->conn
->dispatch_incoming
== NULL
) {
1510 state
->conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
1513 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
1515 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
1516 state
->conn
->write_fd
, false, iov
, iov_count
);
1517 if (subreq
== NULL
) {
1518 return NT_STATUS_NO_MEMORY
;
1520 tevent_req_set_callback(subreq
, smb1cli_req_writev_done
, req
);
1521 return NT_STATUS_OK
;
1524 struct tevent_req
*smb1cli_req_send(TALLOC_CTX
*mem_ctx
,
1525 struct tevent_context
*ev
,
1526 struct smbXcli_conn
*conn
,
1527 uint8_t smb_command
,
1528 uint8_t additional_flags
,
1529 uint8_t clear_flags
,
1530 uint16_t additional_flags2
,
1531 uint16_t clear_flags2
,
1532 uint32_t timeout_msec
,
1534 struct smbXcli_tcon
*tcon
,
1535 struct smbXcli_session
*session
,
1536 uint8_t wct
, uint16_t *vwv
,
1538 const uint8_t *bytes
)
1540 struct tevent_req
*req
;
1544 iov
.iov_base
= discard_const_p(void, bytes
);
1545 iov
.iov_len
= num_bytes
;
1547 req
= smb1cli_req_create(mem_ctx
, ev
, conn
, smb_command
,
1548 additional_flags
, clear_flags
,
1549 additional_flags2
, clear_flags2
,
1556 if (!tevent_req_is_in_progress(req
)) {
1557 return tevent_req_post(req
, ev
);
1559 status
= smb1cli_req_chain_submit(&req
, 1);
1560 if (tevent_req_nterror(req
, status
)) {
1561 return tevent_req_post(req
, ev
);
1566 static void smb1cli_req_writev_done(struct tevent_req
*subreq
)
1568 struct tevent_req
*req
=
1569 tevent_req_callback_data(subreq
,
1571 struct smbXcli_req_state
*state
=
1572 tevent_req_data(req
,
1573 struct smbXcli_req_state
);
1577 nwritten
= writev_recv(subreq
, &err
);
1578 TALLOC_FREE(subreq
);
1579 if (nwritten
== -1) {
1580 NTSTATUS status
= map_nt_error_from_unix_common(err
);
1581 smbXcli_conn_disconnect(state
->conn
, status
);
1585 if (state
->one_way
) {
1586 state
->inbuf
= NULL
;
1587 tevent_req_done(req
);
1591 if (!smbXcli_req_set_pending(req
)) {
1592 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1597 static void smbXcli_conn_received(struct tevent_req
*subreq
)
1599 struct smbXcli_conn
*conn
=
1600 tevent_req_callback_data(subreq
,
1601 struct smbXcli_conn
);
1602 TALLOC_CTX
*frame
= talloc_stackframe();
1608 if (subreq
!= conn
->read_smb_req
) {
1609 DEBUG(1, ("Internal error: cli_smb_received called with "
1610 "unexpected subreq\n"));
1611 status
= NT_STATUS_INTERNAL_ERROR
;
1612 smbXcli_conn_disconnect(conn
, status
);
1616 conn
->read_smb_req
= NULL
;
1618 received
= read_smb_recv(subreq
, frame
, &inbuf
, &err
);
1619 TALLOC_FREE(subreq
);
1620 if (received
== -1) {
1621 status
= map_nt_error_from_unix_common(err
);
1622 smbXcli_conn_disconnect(conn
, status
);
1627 status
= conn
->dispatch_incoming(conn
, frame
, inbuf
);
1629 if (NT_STATUS_IS_OK(status
)) {
1631 * We should not do any more processing
1632 * as the dispatch function called
1633 * tevent_req_done().
1636 } else if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1638 * We got an error, so notify all pending requests
1640 smbXcli_conn_disconnect(conn
, status
);
1645 * We got NT_STATUS_RETRY, so we may ask for a
1646 * next incoming pdu.
1648 if (!smbXcli_conn_receive_next(conn
)) {
1649 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
1653 static NTSTATUS
smb1cli_inbuf_parse_chain(uint8_t *buf
, TALLOC_CTX
*mem_ctx
,
1654 struct iovec
**piov
, int *pnum_iov
)
1665 size_t min_size
= MIN_SMB_SIZE
;
1667 buflen
= smb_len_tcp(buf
);
1670 hdr
= buf
+ NBT_HDR_SIZE
;
1672 status
= smb1cli_pull_raw_error(hdr
);
1673 if (NT_STATUS_IS_ERR(status
)) {
1675 * This is an ugly hack to support OS/2
1676 * which skips the byte_count in the DATA block
1677 * on some error responses.
1681 min_size
-= sizeof(uint16_t);
1684 if (buflen
< min_size
) {
1685 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1689 * This returns iovec elements in the following order:
1704 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
1706 return NT_STATUS_NO_MEMORY
;
1708 iov
[0].iov_base
= hdr
;
1709 iov
[0].iov_len
= HDR_WCT
;
1712 cmd
= CVAL(hdr
, HDR_COM
);
1716 size_t len
= buflen
- taken
;
1718 struct iovec
*iov_tmp
;
1725 * we need at least WCT
1727 needed
= sizeof(uint8_t);
1729 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1730 __location__
, (int)len
, (int)needed
));
1735 * Now we check if the specified words are there
1737 wct
= CVAL(hdr
, wct_ofs
);
1738 needed
+= wct
* sizeof(uint16_t);
1740 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1741 __location__
, (int)len
, (int)needed
));
1745 if ((num_iov
== 1) &&
1747 NT_STATUS_IS_ERR(status
))
1750 * This is an ugly hack to support OS/2
1751 * which skips the byte_count in the DATA block
1752 * on some error responses.
1756 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
1758 if (iov_tmp
== NULL
) {
1760 return NT_STATUS_NO_MEMORY
;
1763 cur
= &iov
[num_iov
];
1767 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
1769 cur
[1].iov_base
= cur
[0].iov_base
;
1776 * we need at least BCC
1778 needed
+= sizeof(uint16_t);
1780 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1781 __location__
, (int)len
, (int)needed
));
1786 * Now we check if the specified bytes are there
1788 bcc_ofs
= wct_ofs
+ sizeof(uint8_t) + wct
* sizeof(uint16_t);
1789 bcc
= SVAL(hdr
, bcc_ofs
);
1790 needed
+= bcc
* sizeof(uint8_t);
1792 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1793 __location__
, (int)len
, (int)needed
));
1798 * we allocate 2 iovec structures for words and bytes
1800 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
1802 if (iov_tmp
== NULL
) {
1804 return NT_STATUS_NO_MEMORY
;
1807 cur
= &iov
[num_iov
];
1810 cur
[0].iov_len
= wct
* sizeof(uint16_t);
1811 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
1812 cur
[1].iov_len
= bcc
* sizeof(uint8_t);
1813 cur
[1].iov_base
= hdr
+ (bcc_ofs
+ sizeof(uint16_t));
1817 if (!smb1cli_is_andx_req(cmd
)) {
1819 * If the current command does not have AndX chanining
1825 if (wct
== 0 && bcc
== 0) {
1827 * An empty response also ends the chain,
1828 * most likely with an error.
1834 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1835 __location__
, (int)wct
, (int)cmd
));
1838 cmd
= CVAL(cur
[0].iov_base
, 0);
1841 * If it is the end of the chain we are also done.
1845 wct_ofs
= SVAL(cur
[0].iov_base
, 2);
1847 if (wct_ofs
< taken
) {
1848 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1850 if (wct_ofs
> buflen
) {
1851 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1855 * we consumed everything up to the start of the next
1861 remaining
= buflen
- taken
;
1863 if (remaining
> 0 && num_iov
>= 3) {
1865 * The last DATA block gets the remaining
1866 * bytes, this is needed to support
1867 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1869 iov
[num_iov
-1].iov_len
+= remaining
;
1873 *pnum_iov
= num_iov
;
1874 return NT_STATUS_OK
;
1878 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1881 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1882 TALLOC_CTX
*tmp_mem
,
1885 struct tevent_req
*req
;
1886 struct smbXcli_req_state
*state
;
1893 uint8_t *inhdr
= inbuf
+ NBT_HDR_SIZE
;
1894 size_t len
= smb_len_tcp(inbuf
);
1895 struct iovec
*iov
= NULL
;
1897 struct tevent_req
**chain
= NULL
;
1898 size_t num_chained
= 0;
1899 size_t num_responses
= 0;
1901 if (conn
->smb1
.read_braw_req
!= NULL
) {
1902 req
= conn
->smb1
.read_braw_req
;
1903 conn
->smb1
.read_braw_req
= NULL
;
1904 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1906 smbXcli_req_unset_pending(req
);
1908 if (state
->smb1
.recv_iov
== NULL
) {
1910 * For requests with more than
1911 * one response, we have to readd the
1914 state
->smb1
.recv_iov
= talloc_zero_array(state
,
1917 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
1918 return NT_STATUS_OK
;
1922 state
->smb1
.recv_iov
[0].iov_base
= (void *)(inhdr
);
1923 state
->smb1
.recv_iov
[0].iov_len
= len
;
1924 ZERO_STRUCT(state
->smb1
.recv_iov
[1]);
1925 ZERO_STRUCT(state
->smb1
.recv_iov
[2]);
1927 state
->smb1
.recv_cmd
= SMBreadBraw
;
1928 state
->smb1
.recv_status
= NT_STATUS_OK
;
1929 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
1931 tevent_req_done(req
);
1932 return NT_STATUS_OK
;
1935 if ((IVAL(inhdr
, 0) != SMB_MAGIC
) /* 0xFF"SMB" */
1936 && (SVAL(inhdr
, 0) != 0x45ff)) /* 0xFF"E" */ {
1937 DEBUG(10, ("Got non-SMB PDU\n"));
1938 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1942 * If we supported multiple encrytion contexts
1943 * here we'd look up based on tid.
1945 if (common_encryption_on(conn
->smb1
.trans_enc
)
1946 && (CVAL(inbuf
, 0) == 0)) {
1947 uint16_t enc_ctx_num
;
1949 status
= get_enc_ctx_num(inbuf
, &enc_ctx_num
);
1950 if (!NT_STATUS_IS_OK(status
)) {
1951 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1952 nt_errstr(status
)));
1956 if (enc_ctx_num
!= conn
->smb1
.trans_enc
->enc_ctx_num
) {
1957 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1959 conn
->smb1
.trans_enc
->enc_ctx_num
));
1960 return NT_STATUS_INVALID_HANDLE
;
1963 status
= common_decrypt_buffer(conn
->smb1
.trans_enc
,
1965 if (!NT_STATUS_IS_OK(status
)) {
1966 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1967 nt_errstr(status
)));
1970 inhdr
= inbuf
+ NBT_HDR_SIZE
;
1971 len
= smb_len_nbt(inbuf
);
1974 mid
= SVAL(inhdr
, HDR_MID
);
1975 num_pending
= talloc_array_length(conn
->pending
);
1977 for (i
=0; i
<num_pending
; i
++) {
1978 if (mid
== smb1cli_req_mid(conn
->pending
[i
])) {
1982 if (i
== num_pending
) {
1983 /* Dump unexpected reply */
1984 return NT_STATUS_RETRY
;
1987 oplock_break
= false;
1989 if (mid
== 0xffff) {
1991 * Paranoia checks that this is really an oplock break request.
1993 oplock_break
= (len
== 51); /* hdr + 8 words */
1994 oplock_break
&= ((CVAL(inhdr
, HDR_FLG
) & FLAG_REPLY
) == 0);
1995 oplock_break
&= (CVAL(inhdr
, HDR_COM
) == SMBlockingX
);
1996 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(6)) == 0);
1997 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(7)) == 0);
1999 if (!oplock_break
) {
2000 /* Dump unexpected reply */
2001 return NT_STATUS_RETRY
;
2005 req
= conn
->pending
[i
];
2006 state
= tevent_req_data(req
, struct smbXcli_req_state
);
2008 if (!oplock_break
/* oplock breaks are not signed */
2009 && !smb_signing_check_pdu(conn
->smb1
.signing
,
2010 inhdr
, len
, state
->smb1
.seqnum
+1)) {
2011 DEBUG(10, ("cli_check_sign_mac failed\n"));
2012 return NT_STATUS_ACCESS_DENIED
;
2015 status
= smb1cli_inbuf_parse_chain(inbuf
, tmp_mem
,
2017 if (!NT_STATUS_IS_OK(status
)) {
2018 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2019 nt_errstr(status
)));
2023 cmd
= CVAL(inhdr
, HDR_COM
);
2024 status
= smb1cli_pull_raw_error(inhdr
);
2026 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
) &&
2027 (state
->session
!= NULL
) && state
->session
->disconnect_expired
)
2030 * this should be a short term hack
2031 * until the upper layers have implemented
2032 * re-authentication.
2037 if (state
->smb1
.chained_requests
== NULL
) {
2039 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2042 smbXcli_req_unset_pending(req
);
2044 if (state
->smb1
.recv_iov
== NULL
) {
2046 * For requests with more than
2047 * one response, we have to readd the
2050 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2053 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2054 return NT_STATUS_OK
;
2058 state
->smb1
.recv_cmd
= cmd
;
2059 state
->smb1
.recv_status
= status
;
2060 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
2062 state
->smb1
.recv_iov
[0] = iov
[0];
2063 state
->smb1
.recv_iov
[1] = iov
[1];
2064 state
->smb1
.recv_iov
[2] = iov
[2];
2066 if (talloc_array_length(conn
->pending
) == 0) {
2067 tevent_req_done(req
);
2068 return NT_STATUS_OK
;
2071 tevent_req_defer_callback(req
, state
->ev
);
2072 tevent_req_done(req
);
2073 return NT_STATUS_RETRY
;
2076 chain
= talloc_move(tmp_mem
, &state
->smb1
.chained_requests
);
2077 num_chained
= talloc_array_length(chain
);
2078 num_responses
= (num_iov
- 1)/2;
2080 if (num_responses
> num_chained
) {
2081 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2084 for (i
=0; i
<num_chained
; i
++) {
2085 size_t iov_idx
= 1 + (i
*2);
2086 struct iovec
*cur
= &iov
[iov_idx
];
2090 state
= tevent_req_data(req
, struct smbXcli_req_state
);
2092 smbXcli_req_unset_pending(req
);
2095 * as we finish multiple requests here
2096 * we need to defer the callbacks as
2097 * they could destroy our current stack state.
2099 tevent_req_defer_callback(req
, state
->ev
);
2101 if (i
>= num_responses
) {
2102 tevent_req_nterror(req
, NT_STATUS_REQUEST_ABORTED
);
2106 if (state
->smb1
.recv_iov
== NULL
) {
2108 * For requests with more than
2109 * one response, we have to readd the
2112 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2115 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2120 state
->smb1
.recv_cmd
= cmd
;
2122 if (i
== (num_responses
- 1)) {
2124 * The last request in the chain gets the status
2126 state
->smb1
.recv_status
= status
;
2128 cmd
= CVAL(cur
[0].iov_base
, 0);
2129 state
->smb1
.recv_status
= NT_STATUS_OK
;
2132 state
->inbuf
= inbuf
;
2135 * Note: here we use talloc_reference() in a way
2136 * that does not expose it to the caller.
2138 inbuf_ref
= talloc_reference(state
->smb1
.recv_iov
, inbuf
);
2139 if (tevent_req_nomem(inbuf_ref
, req
)) {
2143 /* copy the related buffers */
2144 state
->smb1
.recv_iov
[0] = iov
[0];
2145 state
->smb1
.recv_iov
[1] = cur
[0];
2146 state
->smb1
.recv_iov
[2] = cur
[1];
2148 tevent_req_done(req
);
2151 return NT_STATUS_RETRY
;
2154 NTSTATUS
smb1cli_req_recv(struct tevent_req
*req
,
2155 TALLOC_CTX
*mem_ctx
,
2156 struct iovec
**piov
,
2160 uint32_t *pvwv_offset
,
2161 uint32_t *pnum_bytes
,
2163 uint32_t *pbytes_offset
,
2165 const struct smb1cli_req_expected_response
*expected
,
2166 size_t num_expected
)
2168 struct smbXcli_req_state
*state
=
2169 tevent_req_data(req
,
2170 struct smbXcli_req_state
);
2171 NTSTATUS status
= NT_STATUS_OK
;
2172 struct iovec
*recv_iov
= NULL
;
2173 uint8_t *hdr
= NULL
;
2175 uint32_t vwv_offset
= 0;
2176 uint16_t *vwv
= NULL
;
2177 uint32_t num_bytes
= 0;
2178 uint32_t bytes_offset
= 0;
2179 uint8_t *bytes
= NULL
;
2181 bool found_status
= false;
2182 bool found_size
= false;
2196 if (pvwv_offset
!= NULL
) {
2199 if (pnum_bytes
!= NULL
) {
2202 if (pbytes
!= NULL
) {
2205 if (pbytes_offset
!= NULL
) {
2208 if (pinbuf
!= NULL
) {
2212 if (state
->inbuf
!= NULL
) {
2213 recv_iov
= state
->smb1
.recv_iov
;
2214 state
->smb1
.recv_iov
= NULL
;
2215 if (state
->smb1
.recv_cmd
!= SMBreadBraw
) {
2216 hdr
= (uint8_t *)recv_iov
[0].iov_base
;
2217 wct
= recv_iov
[1].iov_len
/2;
2218 vwv
= (uint16_t *)recv_iov
[1].iov_base
;
2219 vwv_offset
= PTR_DIFF(vwv
, hdr
);
2220 num_bytes
= recv_iov
[2].iov_len
;
2221 bytes
= (uint8_t *)recv_iov
[2].iov_base
;
2222 bytes_offset
= PTR_DIFF(bytes
, hdr
);
2226 if (tevent_req_is_nterror(req
, &status
)) {
2227 for (i
=0; i
< num_expected
; i
++) {
2228 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2229 found_status
= true;
2235 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
2241 if (num_expected
== 0) {
2242 found_status
= true;
2246 status
= state
->smb1
.recv_status
;
2248 for (i
=0; i
< num_expected
; i
++) {
2249 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2253 found_status
= true;
2254 if (expected
[i
].wct
== 0) {
2259 if (expected
[i
].wct
== wct
) {
2265 if (!found_status
) {
2270 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2274 *piov
= talloc_move(mem_ctx
, &recv_iov
);
2286 if (pvwv_offset
!= NULL
) {
2287 *pvwv_offset
= vwv_offset
;
2289 if (pnum_bytes
!= NULL
) {
2290 *pnum_bytes
= num_bytes
;
2292 if (pbytes
!= NULL
) {
2295 if (pbytes_offset
!= NULL
) {
2296 *pbytes_offset
= bytes_offset
;
2298 if (pinbuf
!= NULL
) {
2299 *pinbuf
= state
->inbuf
;
2305 size_t smb1cli_req_wct_ofs(struct tevent_req
**reqs
, int num_reqs
)
2312 for (i
=0; i
<num_reqs
; i
++) {
2313 struct smbXcli_req_state
*state
;
2314 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2315 wct_ofs
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2316 state
->smb1
.iov_count
-2);
2317 wct_ofs
= (wct_ofs
+ 3) & ~3;
2322 NTSTATUS
smb1cli_req_chain_submit(struct tevent_req
**reqs
, int num_reqs
)
2324 struct smbXcli_req_state
*first_state
=
2325 tevent_req_data(reqs
[0],
2326 struct smbXcli_req_state
);
2327 struct smbXcli_req_state
*state
;
2329 size_t chain_padding
= 0;
2331 struct iovec
*iov
= NULL
;
2332 struct iovec
*this_iov
;
2336 if (num_reqs
== 1) {
2337 return smb1cli_req_writev_submit(reqs
[0], first_state
,
2338 first_state
->smb1
.iov
,
2339 first_state
->smb1
.iov_count
);
2343 for (i
=0; i
<num_reqs
; i
++) {
2344 if (!tevent_req_is_in_progress(reqs
[i
])) {
2345 return NT_STATUS_INTERNAL_ERROR
;
2348 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2350 if (state
->smb1
.iov_count
< 4) {
2351 return NT_STATUS_INVALID_PARAMETER_MIX
;
2356 * The NBT and SMB header
2369 iovlen
+= state
->smb1
.iov_count
- 2;
2372 iov
= talloc_zero_array(first_state
, struct iovec
, iovlen
);
2374 return NT_STATUS_NO_MEMORY
;
2377 first_state
->smb1
.chained_requests
= (struct tevent_req
**)talloc_memdup(
2378 first_state
, reqs
, sizeof(*reqs
) * num_reqs
);
2379 if (first_state
->smb1
.chained_requests
== NULL
) {
2381 return NT_STATUS_NO_MEMORY
;
2384 wct_offset
= HDR_WCT
;
2387 for (i
=0; i
<num_reqs
; i
++) {
2388 size_t next_padding
= 0;
2391 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2393 if (i
< num_reqs
-1) {
2394 if (!smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))
2395 || CVAL(state
->smb1
.hdr
, HDR_WCT
) < 2) {
2397 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2398 return NT_STATUS_INVALID_PARAMETER_MIX
;
2402 wct_offset
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2403 state
->smb1
.iov_count
-2) + 1;
2404 if ((wct_offset
% 4) != 0) {
2405 next_padding
= 4 - (wct_offset
% 4);
2407 wct_offset
+= next_padding
;
2408 vwv
= state
->smb1
.vwv
;
2410 if (i
< num_reqs
-1) {
2411 struct smbXcli_req_state
*next_state
=
2412 tevent_req_data(reqs
[i
+1],
2413 struct smbXcli_req_state
);
2414 SCVAL(vwv
+0, 0, CVAL(next_state
->smb1
.hdr
, HDR_COM
));
2416 SSVAL(vwv
+1, 0, wct_offset
);
2417 } else if (smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))) {
2418 /* properly end the chain */
2419 SCVAL(vwv
+0, 0, 0xff);
2420 SCVAL(vwv
+0, 1, 0xff);
2426 * The NBT and SMB header
2428 this_iov
[0] = state
->smb1
.iov
[0];
2429 this_iov
[1] = state
->smb1
.iov
[1];
2433 * This one is a bit subtle. We have to add
2434 * chain_padding bytes between the requests, and we
2435 * have to also include the wct field of the
2436 * subsequent requests. We use the subsequent header
2437 * for the padding, it contains the wct field in its
2440 this_iov
[0].iov_len
= chain_padding
+1;
2441 this_iov
[0].iov_base
= (void *)&state
->smb1
.hdr
[
2442 sizeof(state
->smb1
.hdr
) - this_iov
[0].iov_len
];
2443 memset(this_iov
[0].iov_base
, 0, this_iov
[0].iov_len
-1);
2448 * copy the words and bytes
2450 memcpy(this_iov
, state
->smb1
.iov
+2,
2451 sizeof(struct iovec
) * (state
->smb1
.iov_count
-2));
2452 this_iov
+= state
->smb1
.iov_count
- 2;
2453 chain_padding
= next_padding
;
2456 nbt_len
= smbXcli_iov_len(&iov
[1], iovlen
-1);
2457 if (nbt_len
> first_state
->conn
->smb1
.max_xmit
) {
2459 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2460 return NT_STATUS_INVALID_PARAMETER_MIX
;
2463 status
= smb1cli_req_writev_submit(reqs
[0], first_state
, iov
, iovlen
);
2464 if (!NT_STATUS_IS_OK(status
)) {
2466 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2470 return NT_STATUS_OK
;
2473 bool smbXcli_conn_has_async_calls(struct smbXcli_conn
*conn
)
2475 return ((tevent_queue_length(conn
->outgoing
) != 0)
2476 || (talloc_array_length(conn
->pending
) != 0));
2479 bool smbXcli_conn_dfs_supported(struct smbXcli_conn
*conn
)
2481 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
2482 return (smb2cli_conn_server_capabilities(conn
) & SMB2_CAP_DFS
);
2485 return (smb1cli_conn_capabilities(conn
) & CAP_DFS
);
2488 bool smb2cli_conn_req_possible(struct smbXcli_conn
*conn
, uint32_t *max_dyn_len
)
2490 uint16_t credits
= 1;
2492 if (conn
->smb2
.cur_credits
== 0) {
2493 if (max_dyn_len
!= NULL
) {
2499 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2500 credits
= conn
->smb2
.cur_credits
;
2503 if (max_dyn_len
!= NULL
) {
2504 *max_dyn_len
= credits
* 65536;
2510 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn
*conn
)
2512 return conn
->smb2
.server
.capabilities
;
2515 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
2517 return conn
->smb2
.server
.security_mode
;
2520 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn
*conn
)
2522 return conn
->smb2
.server
.max_trans_size
;
2525 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn
*conn
)
2527 return conn
->smb2
.server
.max_read_size
;
2530 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn
*conn
)
2532 return conn
->smb2
.server
.max_write_size
;
2535 void smb2cli_conn_set_max_credits(struct smbXcli_conn
*conn
,
2536 uint16_t max_credits
)
2538 conn
->smb2
.max_credits
= max_credits
;
2541 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
);
2543 static bool smb2cli_req_cancel(struct tevent_req
*req
)
2545 struct smbXcli_req_state
*state
=
2546 tevent_req_data(req
,
2547 struct smbXcli_req_state
);
2548 uint32_t flags
= IVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
2549 uint64_t mid
= BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
);
2550 uint64_t aid
= BVAL(state
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
);
2551 struct smbXcli_tcon
*tcon
= state
->tcon
;
2552 struct smbXcli_session
*session
= state
->session
;
2553 uint8_t *fixed
= state
->smb2
.pad
;
2554 uint16_t fixed_len
= 4;
2555 struct tevent_req
*subreq
;
2556 struct smbXcli_req_state
*substate
;
2559 SSVAL(fixed
, 0, 0x04);
2562 subreq
= smb2cli_req_create(state
, state
->ev
,
2570 if (subreq
== NULL
) {
2573 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
2576 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2577 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2579 flags
&= SMB2_HDR_FLAG_ASYNC
;
2581 if (flags
& SMB2_HDR_FLAG_ASYNC
) {
2585 SIVAL(substate
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
2586 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
2587 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, aid
);
2589 status
= smb2cli_req_compound_submit(&subreq
, 1);
2590 if (!NT_STATUS_IS_OK(status
)) {
2591 TALLOC_FREE(subreq
);
2595 tevent_req_set_callback(subreq
, smb2cli_req_cancel_done
, NULL
);
2600 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
)
2602 /* we do not care about the result */
2603 TALLOC_FREE(subreq
);
2606 struct tevent_req
*smb2cli_req_create(TALLOC_CTX
*mem_ctx
,
2607 struct tevent_context
*ev
,
2608 struct smbXcli_conn
*conn
,
2610 uint32_t additional_flags
,
2611 uint32_t clear_flags
,
2612 uint32_t timeout_msec
,
2613 struct smbXcli_tcon
*tcon
,
2614 struct smbXcli_session
*session
,
2615 const uint8_t *fixed
,
2619 uint32_t max_dyn_len
)
2621 struct tevent_req
*req
;
2622 struct smbXcli_req_state
*state
;
2626 bool use_channel_sequence
= false;
2627 uint16_t channel_sequence
= 0;
2629 req
= tevent_req_create(mem_ctx
, &state
,
2630 struct smbXcli_req_state
);
2637 state
->session
= session
;
2640 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_PERSISTENT_HANDLES
) {
2641 use_channel_sequence
= true;
2642 } else if (conn
->smb2
.server
.capabilities
& SMB2_CAP_MULTI_CHANNEL
) {
2643 use_channel_sequence
= true;
2647 uid
= session
->smb2
->session_id
;
2649 if (use_channel_sequence
) {
2650 channel_sequence
= session
->smb2
->channel_sequence
;
2653 state
->smb2
.should_sign
= session
->smb2
->should_sign
;
2654 state
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
2656 if (cmd
== SMB2_OP_SESSSETUP
&&
2657 session
->smb2
->signing_key
.length
!= 0) {
2658 state
->smb2
.should_sign
= true;
2661 if (cmd
== SMB2_OP_SESSSETUP
&&
2662 session
->smb2_channel
.signing_key
.length
== 0) {
2663 state
->smb2
.should_encrypt
= false;
2668 tid
= tcon
->smb2
.tcon_id
;
2670 if (tcon
->smb2
.should_encrypt
) {
2671 state
->smb2
.should_encrypt
= true;
2675 if (state
->smb2
.should_encrypt
) {
2676 state
->smb2
.should_sign
= false;
2679 state
->smb2
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
2680 if (state
->smb2
.recv_iov
== NULL
) {
2685 flags
|= additional_flags
;
2686 flags
&= ~clear_flags
;
2688 state
->smb2
.fixed
= fixed
;
2689 state
->smb2
.fixed_len
= fixed_len
;
2690 state
->smb2
.dyn
= dyn
;
2691 state
->smb2
.dyn_len
= dyn_len
;
2692 state
->smb2
.max_dyn_len
= max_dyn_len
;
2694 if (state
->smb2
.should_encrypt
) {
2695 SIVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
2696 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
, uid
);
2699 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PROTOCOL_ID
, SMB2_MAGIC
);
2700 SSVAL(state
->smb2
.hdr
, SMB2_HDR_LENGTH
, SMB2_HDR_BODY
);
2701 SSVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
, cmd
);
2702 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CHANNEL_SEQUENCE
, channel_sequence
);
2703 SIVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
2704 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PID
, 0); /* reserved */
2705 SIVAL(state
->smb2
.hdr
, SMB2_HDR_TID
, tid
);
2706 SBVAL(state
->smb2
.hdr
, SMB2_HDR_SESSION_ID
, uid
);
2709 case SMB2_OP_CANCEL
:
2710 state
->one_way
= true;
2714 * If this is a dummy request, it will have
2715 * UINT64_MAX as message id.
2716 * If we send on break acknowledgement,
2717 * this gets overwritten later.
2719 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, UINT64_MAX
);
2723 if (timeout_msec
> 0) {
2724 struct timeval endtime
;
2726 endtime
= timeval_current_ofs_msec(timeout_msec
);
2727 if (!tevent_req_set_endtime(req
, ev
, endtime
)) {
2735 void smb2cli_req_set_notify_async(struct tevent_req
*req
)
2737 struct smbXcli_req_state
*state
=
2738 tevent_req_data(req
,
2739 struct smbXcli_req_state
);
2741 state
->smb2
.notify_async
= true;
2744 static void smb2cli_req_writev_done(struct tevent_req
*subreq
);
2745 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
2746 TALLOC_CTX
*tmp_mem
,
2749 NTSTATUS
smb2cli_req_compound_submit(struct tevent_req
**reqs
,
2752 struct smbXcli_req_state
*state
;
2753 struct tevent_req
*subreq
;
2755 int i
, num_iov
, nbt_len
;
2757 const DATA_BLOB
*encryption_key
= NULL
;
2758 uint64_t encryption_session_id
= 0;
2761 * 1 for the nbt length, optional TRANSFORM
2762 * per request: HDR, fixed, dyn, padding
2763 * -1 because the last one does not need padding
2766 iov
= talloc_array(reqs
[0], struct iovec
, 1 + 1 + 4*num_reqs
- 1);
2768 return NT_STATUS_NO_MEMORY
;
2775 * the session of the first request that requires encryption
2776 * specifies the encryption key.
2778 for (i
=0; i
<num_reqs
; i
++) {
2779 if (!tevent_req_is_in_progress(reqs
[i
])) {
2780 return NT_STATUS_INTERNAL_ERROR
;
2783 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2785 if (!smbXcli_conn_is_connected(state
->conn
)) {
2786 return NT_STATUS_CONNECTION_DISCONNECTED
;
2789 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
2790 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
2791 return NT_STATUS_REVISION_MISMATCH
;
2794 if (state
->session
== NULL
) {
2798 if (!state
->smb2
.should_encrypt
) {
2802 encryption_key
= &state
->session
->smb2
->encryption_key
;
2803 if (encryption_key
->length
== 0) {
2804 return NT_STATUS_INVALID_PARAMETER_MIX
;
2807 encryption_session_id
= state
->session
->smb2
->session_id
;
2810 iov
[num_iov
].iov_base
= state
->smb2
.transform
;
2811 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.transform
);
2814 SBVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
2815 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
,
2816 state
->session
->smb2
->nonce_low
);
2817 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
+8,
2818 state
->session
->smb2
->nonce_high
);
2819 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
,
2820 encryption_session_id
);
2822 state
->session
->smb2
->nonce_low
+= 1;
2823 if (state
->session
->smb2
->nonce_low
== 0) {
2824 state
->session
->smb2
->nonce_high
+= 1;
2825 state
->session
->smb2
->nonce_low
+= 1;
2828 nbt_len
+= SMB2_TF_HDR_SIZE
;
2832 for (i
=0; i
<num_reqs
; i
++) {
2841 const DATA_BLOB
*signing_key
= NULL
;
2843 if (!tevent_req_is_in_progress(reqs
[i
])) {
2844 return NT_STATUS_INTERNAL_ERROR
;
2847 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2849 if (!smbXcli_conn_is_connected(state
->conn
)) {
2850 return NT_STATUS_CONNECTION_DISCONNECTED
;
2853 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
2854 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
2855 return NT_STATUS_REVISION_MISMATCH
;
2858 opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
2859 if (opcode
== SMB2_OP_CANCEL
) {
2863 avail
= UINT64_MAX
- state
->conn
->smb2
.mid
;
2865 return NT_STATUS_CONNECTION_ABORTED
;
2868 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2869 uint32_t max_dyn_len
= 1;
2871 max_dyn_len
= MAX(max_dyn_len
, state
->smb2
.dyn_len
);
2872 max_dyn_len
= MAX(max_dyn_len
, state
->smb2
.max_dyn_len
);
2874 charge
= (max_dyn_len
- 1)/ 65536 + 1;
2879 charge
= MAX(state
->smb2
.credit_charge
, charge
);
2881 avail
= MIN(avail
, state
->conn
->smb2
.cur_credits
);
2882 if (avail
< charge
) {
2883 return NT_STATUS_INTERNAL_ERROR
;
2887 if (state
->conn
->smb2
.max_credits
> state
->conn
->smb2
.cur_credits
) {
2888 credits
= state
->conn
->smb2
.max_credits
-
2889 state
->conn
->smb2
.cur_credits
;
2891 if (state
->conn
->smb2
.max_credits
>= state
->conn
->smb2
.cur_credits
) {
2895 mid
= state
->conn
->smb2
.mid
;
2896 state
->conn
->smb2
.mid
+= charge
;
2897 state
->conn
->smb2
.cur_credits
-= charge
;
2899 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2900 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT_CHARGE
, charge
);
2902 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT
, credits
);
2903 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
2906 if (state
->session
&& encryption_key
== NULL
) {
2908 * We prefer the channel signing key if it is
2911 if (state
->smb2
.should_sign
) {
2912 signing_key
= &state
->session
->smb2_channel
.signing_key
;
2916 * If it is a channel binding, we already have the main
2917 * signing key and try that one.
2919 if (signing_key
&& signing_key
->length
== 0) {
2920 signing_key
= &state
->session
->smb2
->signing_key
;
2924 * If we do not have any session key yet, we skip the
2925 * signing of SMB2_OP_SESSSETUP requests.
2927 if (signing_key
&& signing_key
->length
== 0) {
2933 iov
[num_iov
].iov_base
= state
->smb2
.hdr
;
2934 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.hdr
);
2937 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.fixed
);
2938 iov
[num_iov
].iov_len
= state
->smb2
.fixed_len
;
2941 if (state
->smb2
.dyn
!= NULL
) {
2942 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.dyn
);
2943 iov
[num_iov
].iov_len
= state
->smb2
.dyn_len
;
2947 reqlen
= sizeof(state
->smb2
.hdr
);
2948 reqlen
+= state
->smb2
.fixed_len
;
2949 reqlen
+= state
->smb2
.dyn_len
;
2951 if (i
< num_reqs
-1) {
2952 if ((reqlen
% 8) > 0) {
2953 uint8_t pad
= 8 - (reqlen
% 8);
2954 iov
[num_iov
].iov_base
= state
->smb2
.pad
;
2955 iov
[num_iov
].iov_len
= pad
;
2959 SIVAL(state
->smb2
.hdr
, SMB2_HDR_NEXT_COMMAND
, reqlen
);
2962 state
->smb2
.encryption_session_id
= encryption_session_id
;
2964 if (signing_key
!= NULL
) {
2967 status
= smb2_signing_sign_pdu(*signing_key
,
2968 state
->session
->conn
->protocol
,
2969 &iov
[hdr_iov
], num_iov
- hdr_iov
);
2970 if (!NT_STATUS_IS_OK(status
)) {
2977 ret
= smbXcli_req_set_pending(reqs
[i
]);
2979 return NT_STATUS_NO_MEMORY
;
2983 state
= tevent_req_data(reqs
[0], struct smbXcli_req_state
);
2984 _smb_setlen_tcp(state
->length_hdr
, nbt_len
);
2985 iov
[0].iov_base
= state
->length_hdr
;
2986 iov
[0].iov_len
= sizeof(state
->length_hdr
);
2988 if (encryption_key
!= NULL
) {
2990 size_t buflen
= nbt_len
- SMB2_TF_HDR_SIZE
;
2994 buf
= talloc_array(iov
, uint8_t, buflen
);
2996 return NT_STATUS_NO_MEMORY
;
3000 * We copy the buffers before encrypting them,
3001 * this is at least currently needed for the
3002 * to keep state->smb2.hdr.
3004 * Also the callers may expect there buffers
3007 for (vi
= tf_iov
+ 1; vi
< num_iov
; vi
++) {
3008 struct iovec
*v
= &iov
[vi
];
3009 const uint8_t *o
= (const uint8_t *)v
->iov_base
;
3011 memcpy(buf
, o
, v
->iov_len
);
3012 v
->iov_base
= (void *)buf
;
3016 status
= smb2_signing_encrypt_pdu(*encryption_key
,
3017 state
->conn
->protocol
,
3018 &iov
[tf_iov
], num_iov
- tf_iov
);
3019 if (!NT_STATUS_IS_OK(status
)) {
3024 if (state
->conn
->dispatch_incoming
== NULL
) {
3025 state
->conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
3028 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
3029 state
->conn
->write_fd
, false, iov
, num_iov
);
3030 if (subreq
== NULL
) {
3031 return NT_STATUS_NO_MEMORY
;
3033 tevent_req_set_callback(subreq
, smb2cli_req_writev_done
, reqs
[0]);
3034 return NT_STATUS_OK
;
3037 void smb2cli_req_set_credit_charge(struct tevent_req
*req
, uint16_t charge
)
3039 struct smbXcli_req_state
*state
=
3040 tevent_req_data(req
,
3041 struct smbXcli_req_state
);
3043 state
->smb2
.credit_charge
= charge
;
3046 struct tevent_req
*smb2cli_req_send(TALLOC_CTX
*mem_ctx
,
3047 struct tevent_context
*ev
,
3048 struct smbXcli_conn
*conn
,
3050 uint32_t additional_flags
,
3051 uint32_t clear_flags
,
3052 uint32_t timeout_msec
,
3053 struct smbXcli_tcon
*tcon
,
3054 struct smbXcli_session
*session
,
3055 const uint8_t *fixed
,
3059 uint32_t max_dyn_len
)
3061 struct tevent_req
*req
;
3064 req
= smb2cli_req_create(mem_ctx
, ev
, conn
, cmd
,
3065 additional_flags
, clear_flags
,
3074 if (!tevent_req_is_in_progress(req
)) {
3075 return tevent_req_post(req
, ev
);
3077 status
= smb2cli_req_compound_submit(&req
, 1);
3078 if (tevent_req_nterror(req
, status
)) {
3079 return tevent_req_post(req
, ev
);
3084 static void smb2cli_req_writev_done(struct tevent_req
*subreq
)
3086 struct tevent_req
*req
=
3087 tevent_req_callback_data(subreq
,
3089 struct smbXcli_req_state
*state
=
3090 tevent_req_data(req
,
3091 struct smbXcli_req_state
);
3095 nwritten
= writev_recv(subreq
, &err
);
3096 TALLOC_FREE(subreq
);
3097 if (nwritten
== -1) {
3098 /* here, we need to notify all pending requests */
3099 NTSTATUS status
= map_nt_error_from_unix_common(err
);
3100 smbXcli_conn_disconnect(state
->conn
, status
);
3105 static NTSTATUS
smb2cli_inbuf_parse_compound(struct smbXcli_conn
*conn
,
3108 TALLOC_CTX
*mem_ctx
,
3109 struct iovec
**piov
, int *pnum_iov
)
3114 uint8_t *first_hdr
= buf
;
3115 size_t verified_buflen
= 0;
3119 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
3121 return NT_STATUS_NO_MEMORY
;
3124 while (taken
< buflen
) {
3125 size_t len
= buflen
- taken
;
3126 uint8_t *hdr
= first_hdr
+ taken
;
3129 size_t next_command_ofs
;
3131 struct iovec
*iov_tmp
;
3133 if (verified_buflen
> taken
) {
3134 len
= verified_buflen
- taken
;
3141 DEBUG(10, ("%d bytes left, expected at least %d\n",
3145 if (IVAL(hdr
, 0) == SMB2_TF_MAGIC
) {
3146 struct smbXcli_session
*s
;
3148 struct iovec tf_iov
[2];
3152 if (len
< SMB2_TF_HDR_SIZE
) {
3153 DEBUG(10, ("%d bytes left, expected at least %d\n",
3154 (int)len
, SMB2_TF_HDR_SIZE
));
3158 tf_len
= SMB2_TF_HDR_SIZE
;
3161 hdr
= first_hdr
+ taken
;
3162 enc_len
= IVAL(tf
, SMB2_TF_MSG_SIZE
);
3163 uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
3165 if (len
< SMB2_TF_HDR_SIZE
+ enc_len
) {
3166 DEBUG(10, ("%d bytes left, expected at least %d\n",
3168 (int)(SMB2_TF_HDR_SIZE
+ enc_len
)));
3173 for (; s
; s
= s
->next
) {
3174 if (s
->smb2
->session_id
!= uid
) {
3181 DEBUG(10, ("unknown session_id %llu\n",
3182 (unsigned long long)uid
));
3186 tf_iov
[0].iov_base
= (void *)tf
;
3187 tf_iov
[0].iov_len
= tf_len
;
3188 tf_iov
[1].iov_base
= (void *)hdr
;
3189 tf_iov
[1].iov_len
= enc_len
;
3191 status
= smb2_signing_decrypt_pdu(s
->smb2
->decryption_key
,
3194 if (!NT_STATUS_IS_OK(status
)) {
3199 verified_buflen
= taken
+ enc_len
;
3204 * We need the header plus the body length field
3207 if (len
< SMB2_HDR_BODY
+ 2) {
3208 DEBUG(10, ("%d bytes left, expected at least %d\n",
3209 (int)len
, SMB2_HDR_BODY
));
3212 if (IVAL(hdr
, 0) != SMB2_MAGIC
) {
3213 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3217 if (SVAL(hdr
, 4) != SMB2_HDR_BODY
) {
3218 DEBUG(10, ("Got HDR len %d, expected %d\n",
3219 SVAL(hdr
, 4), SMB2_HDR_BODY
));
3224 next_command_ofs
= IVAL(hdr
, SMB2_HDR_NEXT_COMMAND
);
3225 body_size
= SVAL(hdr
, SMB2_HDR_BODY
);
3227 if (next_command_ofs
!= 0) {
3228 if (next_command_ofs
< (SMB2_HDR_BODY
+ 2)) {
3231 if (next_command_ofs
> full_size
) {
3234 full_size
= next_command_ofs
;
3236 if (body_size
< 2) {
3239 body_size
&= 0xfffe;
3241 if (body_size
> (full_size
- SMB2_HDR_BODY
)) {
3245 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
3247 if (iov_tmp
== NULL
) {
3249 return NT_STATUS_NO_MEMORY
;
3252 cur
= &iov
[num_iov
];
3255 cur
[0].iov_base
= tf
;
3256 cur
[0].iov_len
= tf_len
;
3257 cur
[1].iov_base
= hdr
;
3258 cur
[1].iov_len
= SMB2_HDR_BODY
;
3259 cur
[2].iov_base
= hdr
+ SMB2_HDR_BODY
;
3260 cur
[2].iov_len
= body_size
;
3261 cur
[3].iov_base
= hdr
+ SMB2_HDR_BODY
+ body_size
;
3262 cur
[3].iov_len
= full_size
- (SMB2_HDR_BODY
+ body_size
);
3268 *pnum_iov
= num_iov
;
3269 return NT_STATUS_OK
;
3273 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3276 static struct tevent_req
*smb2cli_conn_find_pending(struct smbXcli_conn
*conn
,
3279 size_t num_pending
= talloc_array_length(conn
->pending
);
3282 for (i
=0; i
<num_pending
; i
++) {
3283 struct tevent_req
*req
= conn
->pending
[i
];
3284 struct smbXcli_req_state
*state
=
3285 tevent_req_data(req
,
3286 struct smbXcli_req_state
);
3288 if (mid
== BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
)) {
3295 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
3296 TALLOC_CTX
*tmp_mem
,
3299 struct tevent_req
*req
;
3300 struct smbXcli_req_state
*state
= NULL
;
3305 struct smbXcli_session
*last_session
= NULL
;
3306 size_t inbuf_len
= smb_len_tcp(inbuf
);
3308 status
= smb2cli_inbuf_parse_compound(conn
,
3309 inbuf
+ NBT_HDR_SIZE
,
3313 if (!NT_STATUS_IS_OK(status
)) {
3317 for (i
=0; i
<num_iov
; i
+=4) {
3318 uint8_t *inbuf_ref
= NULL
;
3319 struct iovec
*cur
= &iov
[i
];
3320 uint8_t *inhdr
= (uint8_t *)cur
[1].iov_base
;
3321 uint16_t opcode
= SVAL(inhdr
, SMB2_HDR_OPCODE
);
3322 uint32_t flags
= IVAL(inhdr
, SMB2_HDR_FLAGS
);
3323 uint64_t mid
= BVAL(inhdr
, SMB2_HDR_MESSAGE_ID
);
3324 uint16_t req_opcode
;
3326 uint16_t credits
= SVAL(inhdr
, SMB2_HDR_CREDIT
);
3327 uint32_t new_credits
;
3328 struct smbXcli_session
*session
= NULL
;
3329 const DATA_BLOB
*signing_key
= NULL
;
3330 bool was_encrypted
= false;
3332 new_credits
= conn
->smb2
.cur_credits
;
3333 new_credits
+= credits
;
3334 if (new_credits
> UINT16_MAX
) {
3335 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3337 conn
->smb2
.cur_credits
+= credits
;
3339 req
= smb2cli_conn_find_pending(conn
, mid
);
3341 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3343 state
= tevent_req_data(req
, struct smbXcli_req_state
);
3345 state
->smb2
.got_async
= false;
3347 req_opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
3348 if (opcode
!= req_opcode
) {
3349 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3351 req_flags
= SVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
3353 if (!(flags
& SMB2_HDR_FLAG_REDIRECT
)) {
3354 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3357 status
= NT_STATUS(IVAL(inhdr
, SMB2_HDR_STATUS
));
3358 if ((flags
& SMB2_HDR_FLAG_ASYNC
) &&
3359 NT_STATUS_EQUAL(status
, STATUS_PENDING
)) {
3360 uint64_t async_id
= BVAL(inhdr
, SMB2_HDR_ASYNC_ID
);
3363 * async interim responses are not signed,
3364 * even if the SMB2_HDR_FLAG_SIGNED flag
3367 req_flags
|= SMB2_HDR_FLAG_ASYNC
;
3368 SBVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, req_flags
);
3369 SBVAL(state
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, async_id
);
3371 if (state
->smb2
.notify_async
) {
3372 state
->smb2
.got_async
= true;
3373 tevent_req_defer_callback(req
, state
->ev
);
3374 tevent_req_notify_callback(req
);
3379 session
= state
->session
;
3380 if (req_flags
& SMB2_HDR_FLAG_CHAINED
) {
3381 session
= last_session
;
3383 last_session
= session
;
3385 if (state
->smb2
.should_sign
) {
3386 if (!(flags
& SMB2_HDR_FLAG_SIGNED
)) {
3387 return NT_STATUS_ACCESS_DENIED
;
3391 if (flags
& SMB2_HDR_FLAG_SIGNED
) {
3392 uint64_t uid
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
3394 if (session
== NULL
) {
3395 struct smbXcli_session
*s
;
3397 s
= state
->conn
->sessions
;
3398 for (; s
; s
= s
->next
) {
3399 if (s
->smb2
->session_id
!= uid
) {
3408 if (session
== NULL
) {
3409 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3412 last_session
= session
;
3413 signing_key
= &session
->smb2_channel
.signing_key
;
3416 if (opcode
== SMB2_OP_SESSSETUP
) {
3418 * We prefer the channel signing key, if it is
3421 * If we do not have a channel signing key yet,
3422 * we try the main signing key, if it is not
3423 * the final response.
3425 if (signing_key
&& signing_key
->length
== 0 &&
3426 !NT_STATUS_IS_OK(status
)) {
3427 signing_key
= &session
->smb2
->signing_key
;
3430 if (signing_key
&& signing_key
->length
== 0) {
3432 * If we do not have a session key to
3433 * verify the signature, we defer the
3434 * signing check to the caller.
3436 * The caller gets NT_STATUS_OK, it
3438 * smb2cli_session_set_session_key()
3440 * smb2cli_session_set_channel_key()
3441 * which will check the signature
3442 * with the channel signing key.
3448 if (cur
[0].iov_len
== SMB2_TF_HDR_SIZE
) {
3449 const uint8_t *tf
= (const uint8_t *)cur
[0].iov_base
;
3450 uint64_t uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
3453 * If the response was encrypted in a SMB2_TRANSFORM
3454 * pdu, which belongs to the correct session,
3455 * we do not need to do signing checks
3457 * It could be the session the response belongs to
3458 * or the session that was used to encrypt the
3459 * SMB2_TRANSFORM request.
3461 if ((session
&& session
->smb2
->session_id
== uid
) ||
3462 (state
->smb2
.encryption_session_id
== uid
)) {
3464 was_encrypted
= true;
3468 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_SESSION_DELETED
)) {
3470 * if the server returns NT_STATUS_USER_SESSION_DELETED
3471 * the response is not signed and we should
3472 * propagate the NT_STATUS_USER_SESSION_DELETED
3473 * status to the caller.
3475 state
->smb2
.signing_skipped
= true;
3479 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
3481 * if the server returns
3482 * NT_STATUS_INVALID_PARAMETER
3483 * the response might not be encrypted.
3485 if (state
->smb2
.should_encrypt
&& !was_encrypted
) {
3486 state
->smb2
.signing_skipped
= true;
3491 if (state
->smb2
.should_encrypt
&& !was_encrypted
) {
3492 if (!state
->smb2
.signing_skipped
) {
3493 return NT_STATUS_ACCESS_DENIED
;
3497 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_NAME_DELETED
) ||
3498 NT_STATUS_EQUAL(status
, NT_STATUS_FILE_CLOSED
) ||
3499 NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
3501 * if the server returns
3502 * NT_STATUS_NETWORK_NAME_DELETED
3503 * NT_STATUS_FILE_CLOSED
3504 * NT_STATUS_INVALID_PARAMETER
3505 * the response might not be signed
3506 * as this happens before the signing checks.
3508 * If server echos the signature (or all zeros)
3509 * we should report the status from the server
3515 cmp
= memcmp(inhdr
+SMB2_HDR_SIGNATURE
,
3516 state
->smb2
.hdr
+SMB2_HDR_SIGNATURE
,
3519 state
->smb2
.signing_skipped
= true;
3525 static const uint8_t zeros
[16];
3527 cmp
= memcmp(inhdr
+SMB2_HDR_SIGNATURE
,
3531 state
->smb2
.signing_skipped
= true;
3538 status
= smb2_signing_check_pdu(*signing_key
,
3539 state
->conn
->protocol
,
3541 if (!NT_STATUS_IS_OK(status
)) {
3543 * If the signing check fails, we disconnect
3550 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
) &&
3551 (session
!= NULL
) && session
->disconnect_expired
)
3554 * this should be a short term hack
3555 * until the upper layers have implemented
3556 * re-authentication.
3561 smbXcli_req_unset_pending(req
);
3564 * There might be more than one response
3565 * we need to defer the notifications
3567 if ((num_iov
== 5) && (talloc_array_length(conn
->pending
) == 0)) {
3572 tevent_req_defer_callback(req
, state
->ev
);
3576 * Note: here we use talloc_reference() in a way
3577 * that does not expose it to the caller.
3579 inbuf_ref
= talloc_reference(state
->smb2
.recv_iov
, inbuf
);
3580 if (tevent_req_nomem(inbuf_ref
, req
)) {
3584 /* copy the related buffers */
3585 state
->smb2
.recv_iov
[0] = cur
[1];
3586 state
->smb2
.recv_iov
[1] = cur
[2];
3587 state
->smb2
.recv_iov
[2] = cur
[3];
3589 tevent_req_done(req
);
3593 return NT_STATUS_RETRY
;
3596 return NT_STATUS_OK
;
3599 NTSTATUS
smb2cli_req_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
3600 struct iovec
**piov
,
3601 const struct smb2cli_req_expected_response
*expected
,
3602 size_t num_expected
)
3604 struct smbXcli_req_state
*state
=
3605 tevent_req_data(req
,
3606 struct smbXcli_req_state
);
3609 bool found_status
= false;
3610 bool found_size
= false;
3617 if (state
->smb2
.got_async
) {
3618 return STATUS_PENDING
;
3621 if (tevent_req_is_nterror(req
, &status
)) {
3622 for (i
=0; i
< num_expected
; i
++) {
3623 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
3624 found_status
= true;
3630 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
3636 if (num_expected
== 0) {
3637 found_status
= true;
3641 status
= NT_STATUS(IVAL(state
->smb2
.recv_iov
[0].iov_base
, SMB2_HDR_STATUS
));
3642 body_size
= SVAL(state
->smb2
.recv_iov
[1].iov_base
, 0);
3644 for (i
=0; i
< num_expected
; i
++) {
3645 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
3649 found_status
= true;
3650 if (expected
[i
].body_size
== 0) {
3655 if (expected
[i
].body_size
== body_size
) {
3661 if (!found_status
) {
3665 if (state
->smb2
.signing_skipped
) {
3666 if (num_expected
> 0) {
3667 return NT_STATUS_ACCESS_DENIED
;
3669 if (!NT_STATUS_IS_ERR(status
)) {
3670 return NT_STATUS_ACCESS_DENIED
;
3675 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3679 *piov
= talloc_move(mem_ctx
, &state
->smb2
.recv_iov
);
3685 static const struct {
3686 enum protocol_types proto
;
3687 const char *smb1_name
;
3688 } smb1cli_prots
[] = {
3689 {PROTOCOL_CORE
, "PC NETWORK PROGRAM 1.0"},
3690 {PROTOCOL_COREPLUS
, "MICROSOFT NETWORKS 1.03"},
3691 {PROTOCOL_LANMAN1
, "MICROSOFT NETWORKS 3.0"},
3692 {PROTOCOL_LANMAN1
, "LANMAN1.0"},
3693 {PROTOCOL_LANMAN2
, "LM1.2X002"},
3694 {PROTOCOL_LANMAN2
, "DOS LANMAN2.1"},
3695 {PROTOCOL_LANMAN2
, "LANMAN2.1"},
3696 {PROTOCOL_LANMAN2
, "Samba"},
3697 {PROTOCOL_NT1
, "NT LANMAN 1.0"},
3698 {PROTOCOL_NT1
, "NT LM 0.12"},
3699 {PROTOCOL_SMB2_02
, "SMB 2.002"},
3700 {PROTOCOL_SMB2_10
, "SMB 2.???"},
3703 static const struct {
3704 enum protocol_types proto
;
3705 uint16_t smb2_dialect
;
3706 } smb2cli_prots
[] = {
3707 {PROTOCOL_SMB2_02
, SMB2_DIALECT_REVISION_202
},
3708 {PROTOCOL_SMB2_10
, SMB2_DIALECT_REVISION_210
},
3709 {PROTOCOL_SMB2_22
, SMB2_DIALECT_REVISION_222
},
3710 {PROTOCOL_SMB2_24
, SMB2_DIALECT_REVISION_224
},
3711 {PROTOCOL_SMB3_00
, SMB3_DIALECT_REVISION_300
},
3714 struct smbXcli_negprot_state
{
3715 struct smbXcli_conn
*conn
;
3716 struct tevent_context
*ev
;
3717 uint32_t timeout_msec
;
3718 enum protocol_types min_protocol
;
3719 enum protocol_types max_protocol
;
3723 uint8_t dyn
[ARRAY_SIZE(smb2cli_prots
)*2];
3727 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
);
3728 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
);
3729 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
);
3730 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
);
3731 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
);
3732 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
3736 struct tevent_req
*smbXcli_negprot_send(TALLOC_CTX
*mem_ctx
,
3737 struct tevent_context
*ev
,
3738 struct smbXcli_conn
*conn
,
3739 uint32_t timeout_msec
,
3740 enum protocol_types min_protocol
,
3741 enum protocol_types max_protocol
)
3743 struct tevent_req
*req
, *subreq
;
3744 struct smbXcli_negprot_state
*state
;
3746 req
= tevent_req_create(mem_ctx
, &state
,
3747 struct smbXcli_negprot_state
);
3753 state
->timeout_msec
= timeout_msec
;
3754 state
->min_protocol
= min_protocol
;
3755 state
->max_protocol
= max_protocol
;
3757 if (min_protocol
== PROTOCOL_NONE
) {
3758 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3759 return tevent_req_post(req
, ev
);
3762 if (max_protocol
== PROTOCOL_NONE
) {
3763 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3764 return tevent_req_post(req
, ev
);
3767 if (min_protocol
> max_protocol
) {
3768 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3769 return tevent_req_post(req
, ev
);
3772 if ((min_protocol
< PROTOCOL_SMB2_02
) &&
3773 (max_protocol
< PROTOCOL_SMB2_02
)) {
3777 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
3779 subreq
= smbXcli_negprot_smb1_subreq(state
);
3780 if (tevent_req_nomem(subreq
, req
)) {
3781 return tevent_req_post(req
, ev
);
3783 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
3787 if ((min_protocol
>= PROTOCOL_SMB2_02
) &&
3788 (max_protocol
>= PROTOCOL_SMB2_02
)) {
3792 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
3794 subreq
= smbXcli_negprot_smb2_subreq(state
);
3795 if (tevent_req_nomem(subreq
, req
)) {
3796 return tevent_req_post(req
, ev
);
3798 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
3803 * We send an SMB1 negprot with the SMB2 dialects
3804 * and expect a SMB1 or a SMB2 response.
3806 * smbXcli_negprot_dispatch_incoming() will fix the
3807 * callback to match protocol of the response.
3809 conn
->dispatch_incoming
= smbXcli_negprot_dispatch_incoming
;
3811 subreq
= smbXcli_negprot_smb1_subreq(state
);
3812 if (tevent_req_nomem(subreq
, req
)) {
3813 return tevent_req_post(req
, ev
);
3815 tevent_req_set_callback(subreq
, smbXcli_negprot_invalid_done
, req
);
3819 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
)
3821 struct tevent_req
*req
=
3822 tevent_req_callback_data(subreq
,
3827 * we just want the low level error
3829 status
= tevent_req_simple_recv_ntstatus(subreq
);
3830 TALLOC_FREE(subreq
);
3831 if (tevent_req_nterror(req
, status
)) {
3835 /* this should never happen */
3836 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
3839 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
)
3842 DATA_BLOB bytes
= data_blob_null
;
3846 /* setup the protocol strings */
3847 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
3851 if (smb1cli_prots
[i
].proto
< state
->min_protocol
) {
3855 if (smb1cli_prots
[i
].proto
> state
->max_protocol
) {
3859 ok
= data_blob_append(state
, &bytes
, &c
, sizeof(c
));
3865 * We now it is already ascii and
3866 * we want NULL termination.
3868 ok
= data_blob_append(state
, &bytes
,
3869 smb1cli_prots
[i
].smb1_name
,
3870 strlen(smb1cli_prots
[i
].smb1_name
)+1);
3876 smb1cli_req_flags(state
->max_protocol
,
3877 state
->conn
->smb1
.client
.capabilities
,
3882 return smb1cli_req_send(state
, state
->ev
, state
->conn
,
3886 state
->timeout_msec
,
3887 0xFFFE, 0, NULL
, /* pid, tid, session */
3888 0, NULL
, /* wct, vwv */
3889 bytes
.length
, bytes
.data
);
3892 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
)
3894 struct tevent_req
*req
=
3895 tevent_req_callback_data(subreq
,
3897 struct smbXcli_negprot_state
*state
=
3898 tevent_req_data(req
,
3899 struct smbXcli_negprot_state
);
3900 struct smbXcli_conn
*conn
= state
->conn
;
3901 struct iovec
*recv_iov
= NULL
;
3910 size_t num_prots
= 0;
3912 uint32_t client_capabilities
= conn
->smb1
.client
.capabilities
;
3913 uint32_t both_capabilities
;
3914 uint32_t server_capabilities
= 0;
3915 uint32_t capabilities
;
3916 uint32_t client_max_xmit
= conn
->smb1
.client
.max_xmit
;
3917 uint32_t server_max_xmit
= 0;
3919 uint32_t server_max_mux
= 0;
3920 uint16_t server_security_mode
= 0;
3921 uint32_t server_session_key
= 0;
3922 bool server_readbraw
= false;
3923 bool server_writebraw
= false;
3924 bool server_lockread
= false;
3925 bool server_writeunlock
= false;
3926 struct GUID server_guid
= GUID_zero();
3927 DATA_BLOB server_gss_blob
= data_blob_null
;
3928 uint8_t server_challenge
[8];
3929 char *server_workgroup
= NULL
;
3930 char *server_name
= NULL
;
3931 int server_time_zone
= 0;
3932 NTTIME server_system_time
= 0;
3933 static const struct smb1cli_req_expected_response expected
[] = {
3935 .status
= NT_STATUS_OK
,
3936 .wct
= 0x11, /* NT1 */
3939 .status
= NT_STATUS_OK
,
3940 .wct
= 0x0D, /* LM */
3943 .status
= NT_STATUS_OK
,
3944 .wct
= 0x01, /* CORE */
3948 ZERO_STRUCT(server_challenge
);
3950 status
= smb1cli_req_recv(subreq
, state
,
3955 NULL
, /* pvwv_offset */
3958 NULL
, /* pbytes_offset */
3960 expected
, ARRAY_SIZE(expected
));
3961 TALLOC_FREE(subreq
);
3962 if (tevent_req_nterror(req
, status
)) {
3966 flags
= CVAL(inhdr
, HDR_FLG
);
3968 protnum
= SVAL(vwv
, 0);
3970 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
3971 if (smb1cli_prots
[i
].proto
< state
->min_protocol
) {
3975 if (smb1cli_prots
[i
].proto
> state
->max_protocol
) {
3979 if (protnum
!= num_prots
) {
3984 conn
->protocol
= smb1cli_prots
[i
].proto
;
3988 if (conn
->protocol
== PROTOCOL_NONE
) {
3989 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3993 if ((conn
->protocol
< PROTOCOL_NT1
) && conn
->mandatory_signing
) {
3994 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3995 "and the selected protocol level doesn't support it.\n"));
3996 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
4000 if (flags
& FLAG_SUPPORT_LOCKREAD
) {
4001 server_lockread
= true;
4002 server_writeunlock
= true;
4005 if (conn
->protocol
>= PROTOCOL_NT1
) {
4006 const char *client_signing
= NULL
;
4007 bool server_mandatory
= false;
4008 bool server_allowed
= false;
4009 const char *server_signing
= NULL
;
4014 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4019 server_security_mode
= CVAL(vwv
+ 1, 0);
4020 server_max_mux
= SVAL(vwv
+ 1, 1);
4021 server_max_xmit
= IVAL(vwv
+ 3, 1);
4022 server_session_key
= IVAL(vwv
+ 7, 1);
4023 server_time_zone
= SVALS(vwv
+ 15, 1);
4024 server_time_zone
*= 60;
4025 /* this time arrives in real GMT */
4026 server_system_time
= BVAL(vwv
+ 11, 1);
4027 server_capabilities
= IVAL(vwv
+ 9, 1);
4029 key_len
= CVAL(vwv
+ 16, 1);
4031 if (server_capabilities
& CAP_RAW_MODE
) {
4032 server_readbraw
= true;
4033 server_writebraw
= true;
4035 if (server_capabilities
& CAP_LOCK_AND_READ
) {
4036 server_lockread
= true;
4039 if (server_capabilities
& CAP_EXTENDED_SECURITY
) {
4040 DATA_BLOB blob1
, blob2
;
4042 if (num_bytes
< 16) {
4043 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4047 blob1
= data_blob_const(bytes
, 16);
4048 status
= GUID_from_data_blob(&blob1
, &server_guid
);
4049 if (tevent_req_nterror(req
, status
)) {
4053 blob1
= data_blob_const(bytes
+16, num_bytes
-16);
4054 blob2
= data_blob_dup_talloc(state
, blob1
);
4055 if (blob1
.length
> 0 &&
4056 tevent_req_nomem(blob2
.data
, req
)) {
4059 server_gss_blob
= blob2
;
4061 DATA_BLOB blob1
, blob2
;
4063 if (num_bytes
< key_len
) {
4064 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4068 if (key_len
!= 0 && key_len
!= 8) {
4069 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4074 memcpy(server_challenge
, bytes
, 8);
4077 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4078 blob2
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4079 if (blob1
.length
> 0) {
4082 len
= utf16_len_n(blob1
.data
,
4086 ok
= convert_string_talloc(state
,
4094 status
= map_nt_error_from_unix_common(errno
);
4095 tevent_req_nterror(req
, status
);
4100 blob2
.data
+= blob1
.length
;
4101 blob2
.length
-= blob1
.length
;
4102 if (blob2
.length
> 0) {
4105 len
= utf16_len_n(blob1
.data
,
4109 ok
= convert_string_talloc(state
,
4117 status
= map_nt_error_from_unix_common(errno
);
4118 tevent_req_nterror(req
, status
);
4124 client_signing
= "disabled";
4125 if (conn
->allow_signing
) {
4126 client_signing
= "allowed";
4128 if (conn
->mandatory_signing
) {
4129 client_signing
= "required";
4132 server_signing
= "not supported";
4133 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_ENABLED
) {
4134 server_signing
= "supported";
4135 server_allowed
= true;
4136 } else if (conn
->mandatory_signing
) {
4138 * We have mandatory signing as client
4139 * lets assume the server will look at our
4140 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4141 * flag in the session setup
4143 server_signing
= "not announced";
4144 server_allowed
= true;
4146 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_REQUIRED
) {
4147 server_signing
= "required";
4148 server_mandatory
= true;
4151 ok
= smb_signing_set_negotiated(conn
->smb1
.signing
,
4155 DEBUG(1,("cli_negprot: SMB signing is required, "
4156 "but client[%s] and server[%s] mismatch\n",
4157 client_signing
, server_signing
));
4158 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
4162 } else if (conn
->protocol
>= PROTOCOL_LANMAN1
) {
4168 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4172 server_security_mode
= SVAL(vwv
+ 1, 0);
4173 server_max_xmit
= SVAL(vwv
+ 2, 0);
4174 server_max_mux
= SVAL(vwv
+ 3, 0);
4175 server_readbraw
= ((SVAL(vwv
+ 5, 0) & 0x1) != 0);
4176 server_writebraw
= ((SVAL(vwv
+ 5, 0) & 0x2) != 0);
4177 server_session_key
= IVAL(vwv
+ 6, 0);
4178 server_time_zone
= SVALS(vwv
+ 10, 0);
4179 server_time_zone
*= 60;
4180 /* this time is converted to GMT by make_unix_date */
4181 t
= pull_dos_date((const uint8_t *)(vwv
+ 8), server_time_zone
);
4182 unix_to_nt_time(&server_system_time
, t
);
4183 key_len
= SVAL(vwv
+ 11, 0);
4185 if (num_bytes
< key_len
) {
4186 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4190 if (key_len
!= 0 && key_len
!= 8) {
4191 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4196 memcpy(server_challenge
, bytes
, 8);
4199 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4200 if (blob1
.length
> 0) {
4204 len
= utf16_len_n(blob1
.data
,
4208 ok
= convert_string_talloc(state
,
4216 status
= map_nt_error_from_unix_common(errno
);
4217 tevent_req_nterror(req
, status
);
4223 /* the old core protocol */
4224 server_time_zone
= get_time_zone(time(NULL
));
4225 server_max_xmit
= 1024;
4229 if (server_max_xmit
< 1024) {
4230 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4234 if (server_max_mux
< 1) {
4235 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4240 * Now calculate the negotiated capabilities
4241 * based on the mask for:
4242 * - client only flags
4243 * - flags used in both directions
4244 * - server only flags
4246 both_capabilities
= client_capabilities
& server_capabilities
;
4247 capabilities
= client_capabilities
& SMB_CAP_CLIENT_MASK
;
4248 capabilities
|= both_capabilities
& SMB_CAP_BOTH_MASK
;
4249 capabilities
|= server_capabilities
& SMB_CAP_SERVER_MASK
;
4251 max_xmit
= MIN(client_max_xmit
, server_max_xmit
);
4253 conn
->smb1
.server
.capabilities
= server_capabilities
;
4254 conn
->smb1
.capabilities
= capabilities
;
4256 conn
->smb1
.server
.max_xmit
= server_max_xmit
;
4257 conn
->smb1
.max_xmit
= max_xmit
;
4259 conn
->smb1
.server
.max_mux
= server_max_mux
;
4261 conn
->smb1
.server
.security_mode
= server_security_mode
;
4263 conn
->smb1
.server
.readbraw
= server_readbraw
;
4264 conn
->smb1
.server
.writebraw
= server_writebraw
;
4265 conn
->smb1
.server
.lockread
= server_lockread
;
4266 conn
->smb1
.server
.writeunlock
= server_writeunlock
;
4268 conn
->smb1
.server
.session_key
= server_session_key
;
4270 talloc_steal(conn
, server_gss_blob
.data
);
4271 conn
->smb1
.server
.gss_blob
= server_gss_blob
;
4272 conn
->smb1
.server
.guid
= server_guid
;
4273 memcpy(conn
->smb1
.server
.challenge
, server_challenge
, 8);
4274 conn
->smb1
.server
.workgroup
= talloc_move(conn
, &server_workgroup
);
4275 conn
->smb1
.server
.name
= talloc_move(conn
, &server_name
);
4277 conn
->smb1
.server
.time_zone
= server_time_zone
;
4278 conn
->smb1
.server
.system_time
= server_system_time
;
4280 tevent_req_done(req
);
4283 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
)
4287 uint16_t dialect_count
= 0;
4289 buf
= state
->smb2
.dyn
;
4290 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4291 if (smb2cli_prots
[i
].proto
< state
->min_protocol
) {
4295 if (smb2cli_prots
[i
].proto
> state
->max_protocol
) {
4299 SSVAL(buf
, dialect_count
*2, smb2cli_prots
[i
].smb2_dialect
);
4303 buf
= state
->smb2
.fixed
;
4305 SSVAL(buf
, 2, dialect_count
);
4306 SSVAL(buf
, 4, state
->conn
->smb2
.client
.security_mode
);
4307 SSVAL(buf
, 6, 0); /* Reserved */
4308 if (state
->max_protocol
>= PROTOCOL_SMB2_22
) {
4309 SIVAL(buf
, 8, state
->conn
->smb2
.client
.capabilities
);
4311 SIVAL(buf
, 8, 0); /* Capabilities */
4313 if (state
->max_protocol
>= PROTOCOL_SMB2_10
) {
4317 status
= GUID_to_ndr_blob(&state
->conn
->smb2
.client
.guid
,
4319 if (!NT_STATUS_IS_OK(status
)) {
4322 memcpy(buf
+12, blob
.data
, 16); /* ClientGuid */
4324 memset(buf
+12, 0, 16); /* ClientGuid */
4326 SBVAL(buf
, 28, 0); /* ClientStartTime */
4328 return smb2cli_req_send(state
, state
->ev
,
4329 state
->conn
, SMB2_OP_NEGPROT
,
4331 state
->timeout_msec
,
4332 NULL
, NULL
, /* tcon, session */
4333 state
->smb2
.fixed
, sizeof(state
->smb2
.fixed
),
4334 state
->smb2
.dyn
, dialect_count
*2,
4335 UINT16_MAX
); /* max_dyn_len */
4338 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
)
4340 struct tevent_req
*req
=
4341 tevent_req_callback_data(subreq
,
4343 struct smbXcli_negprot_state
*state
=
4344 tevent_req_data(req
,
4345 struct smbXcli_negprot_state
);
4346 struct smbXcli_conn
*conn
= state
->conn
;
4347 size_t security_offset
, security_length
;
4353 uint16_t dialect_revision
;
4354 static const struct smb2cli_req_expected_response expected
[] = {
4356 .status
= NT_STATUS_OK
,
4361 status
= smb2cli_req_recv(subreq
, state
, &iov
,
4362 expected
, ARRAY_SIZE(expected
));
4363 TALLOC_FREE(subreq
);
4364 if (tevent_req_nterror(req
, status
)) {
4368 body
= (uint8_t *)iov
[1].iov_base
;
4370 dialect_revision
= SVAL(body
, 4);
4372 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4373 if (smb2cli_prots
[i
].proto
< state
->min_protocol
) {
4377 if (smb2cli_prots
[i
].proto
> state
->max_protocol
) {
4381 if (smb2cli_prots
[i
].smb2_dialect
!= dialect_revision
) {
4385 conn
->protocol
= smb2cli_prots
[i
].proto
;
4389 if (conn
->protocol
== PROTOCOL_NONE
) {
4390 if (state
->min_protocol
>= PROTOCOL_SMB2_02
) {
4391 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4395 if (dialect_revision
!= SMB2_DIALECT_REVISION_2FF
) {
4396 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4400 /* make sure we do not loop forever */
4401 state
->min_protocol
= PROTOCOL_SMB2_02
;
4404 * send a SMB2 negprot, in order to negotiate
4407 subreq
= smbXcli_negprot_smb2_subreq(state
);
4408 if (tevent_req_nomem(subreq
, req
)) {
4411 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4415 conn
->smb2
.server
.security_mode
= SVAL(body
, 2);
4417 blob
= data_blob_const(body
+ 8, 16);
4418 status
= GUID_from_data_blob(&blob
, &conn
->smb2
.server
.guid
);
4419 if (tevent_req_nterror(req
, status
)) {
4423 conn
->smb2
.server
.capabilities
= IVAL(body
, 24);
4424 conn
->smb2
.server
.max_trans_size
= IVAL(body
, 28);
4425 conn
->smb2
.server
.max_read_size
= IVAL(body
, 32);
4426 conn
->smb2
.server
.max_write_size
= IVAL(body
, 36);
4427 conn
->smb2
.server
.system_time
= BVAL(body
, 40);
4428 conn
->smb2
.server
.start_time
= BVAL(body
, 48);
4430 security_offset
= SVAL(body
, 56);
4431 security_length
= SVAL(body
, 58);
4433 if (security_offset
!= SMB2_HDR_BODY
+ iov
[1].iov_len
) {
4434 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4438 if (security_length
> iov
[2].iov_len
) {
4439 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4443 conn
->smb2
.server
.gss_blob
= data_blob_talloc(conn
,
4446 if (tevent_req_nomem(conn
->smb2
.server
.gss_blob
.data
, req
)) {
4450 tevent_req_done(req
);
4453 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
4454 TALLOC_CTX
*tmp_mem
,
4457 size_t num_pending
= talloc_array_length(conn
->pending
);
4458 struct tevent_req
*subreq
;
4459 struct smbXcli_req_state
*substate
;
4460 struct tevent_req
*req
;
4461 uint32_t protocol_magic
;
4462 size_t inbuf_len
= smb_len_nbt(inbuf
);
4464 if (num_pending
!= 1) {
4465 return NT_STATUS_INTERNAL_ERROR
;
4468 if (inbuf_len
< 4) {
4469 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4472 subreq
= conn
->pending
[0];
4473 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
4474 req
= tevent_req_callback_data(subreq
, struct tevent_req
);
4476 protocol_magic
= IVAL(inbuf
, 4);
4478 switch (protocol_magic
) {
4480 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
4481 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
4482 return smb1cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
4485 if (substate
->smb2
.recv_iov
== NULL
) {
4487 * For the SMB1 negprot we have move it.
4489 substate
->smb2
.recv_iov
= substate
->smb1
.recv_iov
;
4490 substate
->smb1
.recv_iov
= NULL
;
4494 * we got an SMB2 answer, which consumed sequence number 0
4495 * so we need to use 1 as the next one.
4497 * we also need to set the current credits to 0
4498 * as we consumed the initial one. The SMB2 answer
4499 * hopefully grant us a new credit.
4502 conn
->smb2
.cur_credits
= 0;
4503 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4504 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
4505 return smb2cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
4508 DEBUG(10, ("Got non-SMB PDU\n"));
4509 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4512 NTSTATUS
smbXcli_negprot_recv(struct tevent_req
*req
)
4514 return tevent_req_simple_recv_ntstatus(req
);
4517 NTSTATUS
smbXcli_negprot(struct smbXcli_conn
*conn
,
4518 uint32_t timeout_msec
,
4519 enum protocol_types min_protocol
,
4520 enum protocol_types max_protocol
)
4522 TALLOC_CTX
*frame
= talloc_stackframe();
4523 struct tevent_context
*ev
;
4524 struct tevent_req
*req
;
4525 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4528 if (smbXcli_conn_has_async_calls(conn
)) {
4530 * Can't use sync call while an async call is in flight
4532 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
4535 ev
= samba_tevent_context_init(frame
);
4539 req
= smbXcli_negprot_send(frame
, ev
, conn
, timeout_msec
,
4540 min_protocol
, max_protocol
);
4544 ok
= tevent_req_poll(req
, ev
);
4546 status
= map_nt_error_from_unix_common(errno
);
4549 status
= smbXcli_negprot_recv(req
);
4555 static int smbXcli_session_destructor(struct smbXcli_session
*session
)
4557 if (session
->conn
== NULL
) {
4561 DLIST_REMOVE(session
->conn
->sessions
, session
);
4565 struct smbXcli_session
*smbXcli_session_create(TALLOC_CTX
*mem_ctx
,
4566 struct smbXcli_conn
*conn
)
4568 struct smbXcli_session
*session
;
4570 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
4571 if (session
== NULL
) {
4574 session
->smb2
= talloc_zero(session
, struct smb2cli_session
);
4575 if (session
->smb2
== NULL
) {
4576 talloc_free(session
);
4579 talloc_set_destructor(session
, smbXcli_session_destructor
);
4581 DLIST_ADD_END(conn
->sessions
, session
, struct smbXcli_session
*);
4582 session
->conn
= conn
;
4587 struct smbXcli_session
*smbXcli_session_copy(TALLOC_CTX
*mem_ctx
,
4588 struct smbXcli_session
*src
)
4590 struct smbXcli_session
*session
;
4592 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
4593 if (session
== NULL
) {
4596 session
->smb2
= talloc_zero(session
, struct smb2cli_session
);
4597 if (session
->smb2
== NULL
) {
4598 talloc_free(session
);
4602 session
->conn
= src
->conn
;
4603 *session
->smb2
= *src
->smb2
;
4604 session
->smb2_channel
= src
->smb2_channel
;
4605 session
->disconnect_expired
= src
->disconnect_expired
;
4607 DLIST_ADD_END(src
->conn
->sessions
, session
, struct smbXcli_session
*);
4608 talloc_set_destructor(session
, smbXcli_session_destructor
);
4614 NTSTATUS
smbXcli_session_application_key(struct smbXcli_session
*session
,
4615 TALLOC_CTX
*mem_ctx
,
4618 const DATA_BLOB
*application_key
;
4620 *key
= data_blob_null
;
4622 if (session
->conn
== NULL
) {
4623 return NT_STATUS_NO_USER_SESSION_KEY
;
4626 if (session
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
4627 application_key
= &session
->smb2
->application_key
;
4629 application_key
= &session
->smb1
.application_key
;
4632 if (application_key
->length
== 0) {
4633 return NT_STATUS_NO_USER_SESSION_KEY
;
4636 *key
= data_blob_dup_talloc(mem_ctx
, *application_key
);
4637 if (key
->data
== NULL
) {
4638 return NT_STATUS_NO_MEMORY
;
4641 return NT_STATUS_OK
;
4644 void smbXcli_session_set_disconnect_expired(struct smbXcli_session
*session
)
4646 session
->disconnect_expired
= true;
4649 uint16_t smb1cli_session_current_id(struct smbXcli_session
*session
)
4651 return session
->smb1
.session_id
;
4654 void smb1cli_session_set_id(struct smbXcli_session
*session
,
4655 uint16_t session_id
)
4657 session
->smb1
.session_id
= session_id
;
4660 NTSTATUS
smb1cli_session_set_session_key(struct smbXcli_session
*session
,
4661 const DATA_BLOB _session_key
)
4663 struct smbXcli_conn
*conn
= session
->conn
;
4664 uint8_t session_key
[16];
4667 return NT_STATUS_INVALID_PARAMETER_MIX
;
4670 if (session
->smb1
.application_key
.length
!= 0) {
4672 * TODO: do not allow this...
4674 * return NT_STATUS_INVALID_PARAMETER_MIX;
4676 data_blob_clear_free(&session
->smb1
.application_key
);
4677 session
->smb1
.protected_key
= false;
4680 if (_session_key
.length
== 0) {
4681 return NT_STATUS_OK
;
4684 ZERO_STRUCT(session_key
);
4685 memcpy(session_key
, _session_key
.data
,
4686 MIN(_session_key
.length
, sizeof(session_key
)));
4688 session
->smb1
.application_key
= data_blob_talloc(session
,
4690 sizeof(session_key
));
4691 ZERO_STRUCT(session_key
);
4692 if (session
->smb1
.application_key
.data
== NULL
) {
4693 return NT_STATUS_NO_MEMORY
;
4696 session
->smb1
.protected_key
= false;
4698 return NT_STATUS_OK
;
4701 NTSTATUS
smb1cli_session_protect_session_key(struct smbXcli_session
*session
)
4703 if (session
->smb1
.protected_key
) {
4704 /* already protected */
4705 return NT_STATUS_OK
;
4708 if (session
->smb1
.application_key
.length
!= 16) {
4709 return NT_STATUS_INVALID_PARAMETER_MIX
;
4712 smb_key_derivation(session
->smb1
.application_key
.data
,
4713 session
->smb1
.application_key
.length
,
4714 session
->smb1
.application_key
.data
);
4716 session
->smb1
.protected_key
= true;
4718 return NT_STATUS_OK
;
4721 uint8_t smb2cli_session_security_mode(struct smbXcli_session
*session
)
4723 struct smbXcli_conn
*conn
= session
->conn
;
4724 uint8_t security_mode
= 0;
4727 return security_mode
;
4730 security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
4731 if (conn
->mandatory_signing
) {
4732 security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
4735 return security_mode
;
4738 uint64_t smb2cli_session_current_id(struct smbXcli_session
*session
)
4740 return session
->smb2
->session_id
;
4743 uint16_t smb2cli_session_get_flags(struct smbXcli_session
*session
)
4745 return session
->smb2
->session_flags
;
4748 void smb2cli_session_set_id_and_flags(struct smbXcli_session
*session
,
4749 uint64_t session_id
,
4750 uint16_t session_flags
)
4752 session
->smb2
->session_id
= session_id
;
4753 session
->smb2
->session_flags
= session_flags
;
4756 void smb2cli_session_increment_channel_sequence(struct smbXcli_session
*session
)
4758 session
->smb2
->channel_sequence
+= 1;
4761 NTSTATUS
smb2cli_session_set_session_key(struct smbXcli_session
*session
,
4762 const DATA_BLOB _session_key
,
4763 const struct iovec
*recv_iov
)
4765 struct smbXcli_conn
*conn
= session
->conn
;
4766 uint16_t no_sign_flags
;
4767 uint8_t session_key
[16];
4768 bool check_signature
= true;
4773 return NT_STATUS_INVALID_PARAMETER_MIX
;
4776 if (recv_iov
[0].iov_len
!= SMB2_HDR_BODY
) {
4777 return NT_STATUS_INVALID_PARAMETER_MIX
;
4780 no_sign_flags
= SMB2_SESSION_FLAG_IS_GUEST
| SMB2_SESSION_FLAG_IS_NULL
;
4782 if (session
->smb2
->session_flags
& no_sign_flags
) {
4783 session
->smb2
->should_sign
= false;
4784 return NT_STATUS_OK
;
4787 if (session
->smb2
->signing_key
.length
!= 0) {
4788 return NT_STATUS_INVALID_PARAMETER_MIX
;
4791 ZERO_STRUCT(session_key
);
4792 memcpy(session_key
, _session_key
.data
,
4793 MIN(_session_key
.length
, sizeof(session_key
)));
4795 session
->smb2
->signing_key
= data_blob_talloc(session
,
4797 sizeof(session_key
));
4798 if (session
->smb2
->signing_key
.data
== NULL
) {
4799 ZERO_STRUCT(session_key
);
4800 return NT_STATUS_NO_MEMORY
;
4803 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4804 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCMAC");
4805 const DATA_BLOB context
= data_blob_string_const_null("SmbSign");
4807 smb2_key_derivation(session_key
, sizeof(session_key
),
4808 label
.data
, label
.length
,
4809 context
.data
, context
.length
,
4810 session
->smb2
->signing_key
.data
);
4813 session
->smb2
->encryption_key
= data_blob_dup_talloc(session
,
4814 session
->smb2
->signing_key
);
4815 if (session
->smb2
->encryption_key
.data
== NULL
) {
4816 ZERO_STRUCT(session_key
);
4817 return NT_STATUS_NO_MEMORY
;
4820 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4821 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCCM");
4822 const DATA_BLOB context
= data_blob_string_const_null("ServerIn ");
4824 smb2_key_derivation(session_key
, sizeof(session_key
),
4825 label
.data
, label
.length
,
4826 context
.data
, context
.length
,
4827 session
->smb2
->encryption_key
.data
);
4830 session
->smb2
->decryption_key
= data_blob_dup_talloc(session
,
4831 session
->smb2
->signing_key
);
4832 if (session
->smb2
->decryption_key
.data
== NULL
) {
4833 ZERO_STRUCT(session_key
);
4834 return NT_STATUS_NO_MEMORY
;
4837 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4838 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCCM");
4839 const DATA_BLOB context
= data_blob_string_const_null("ServerOut");
4841 smb2_key_derivation(session_key
, sizeof(session_key
),
4842 label
.data
, label
.length
,
4843 context
.data
, context
.length
,
4844 session
->smb2
->decryption_key
.data
);
4847 session
->smb2
->application_key
= data_blob_dup_talloc(session
,
4848 session
->smb2
->signing_key
);
4849 if (session
->smb2
->application_key
.data
== NULL
) {
4850 ZERO_STRUCT(session_key
);
4851 return NT_STATUS_NO_MEMORY
;
4854 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4855 const DATA_BLOB label
= data_blob_string_const_null("SMB2APP");
4856 const DATA_BLOB context
= data_blob_string_const_null("SmbRpc");
4858 smb2_key_derivation(session_key
, sizeof(session_key
),
4859 label
.data
, label
.length
,
4860 context
.data
, context
.length
,
4861 session
->smb2
->application_key
.data
);
4863 ZERO_STRUCT(session_key
);
4865 session
->smb2_channel
.signing_key
= data_blob_dup_talloc(session
,
4866 session
->smb2
->signing_key
);
4867 if (session
->smb2_channel
.signing_key
.data
== NULL
) {
4868 return NT_STATUS_NO_MEMORY
;
4871 check_signature
= conn
->mandatory_signing
;
4873 hdr_flags
= IVAL(recv_iov
[0].iov_base
, SMB2_HDR_FLAGS
);
4874 if (hdr_flags
& SMB2_HDR_FLAG_SIGNED
) {
4876 * Sadly some vendors don't sign the
4877 * final SMB2 session setup response
4879 * At least Windows and Samba are always doing this
4880 * if there's a session key available.
4882 * We only check the signature if it's mandatory
4883 * or SMB2_HDR_FLAG_SIGNED is provided.
4885 check_signature
= true;
4888 if (check_signature
) {
4889 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
4890 session
->conn
->protocol
,
4892 if (!NT_STATUS_IS_OK(status
)) {
4897 session
->smb2
->should_sign
= false;
4898 session
->smb2
->should_encrypt
= false;
4900 if (conn
->desire_signing
) {
4901 session
->smb2
->should_sign
= true;
4904 if (conn
->smb2
.server
.security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) {
4905 session
->smb2
->should_sign
= true;
4908 if (session
->smb2
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
) {
4909 session
->smb2
->should_encrypt
= true;
4912 if (conn
->protocol
< PROTOCOL_SMB2_24
) {
4913 session
->smb2
->should_encrypt
= false;
4916 if (!(conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
4917 session
->smb2
->should_encrypt
= false;
4920 generate_random_buffer((uint8_t *)&session
->smb2
->nonce_high
,
4921 sizeof(session
->smb2
->nonce_high
));
4922 session
->smb2
->nonce_low
= 1;
4924 return NT_STATUS_OK
;
4927 NTSTATUS
smb2cli_session_create_channel(TALLOC_CTX
*mem_ctx
,
4928 struct smbXcli_session
*session1
,
4929 struct smbXcli_conn
*conn
,
4930 struct smbXcli_session
**_session2
)
4932 struct smbXcli_session
*session2
;
4934 if (session1
->smb2
->signing_key
.length
== 0) {
4935 return NT_STATUS_INVALID_PARAMETER_MIX
;
4939 return NT_STATUS_INVALID_PARAMETER_MIX
;
4942 session2
= talloc_zero(mem_ctx
, struct smbXcli_session
);
4943 if (session2
== NULL
) {
4944 return NT_STATUS_NO_MEMORY
;
4946 session2
->smb2
= talloc_reference(session2
, session1
->smb2
);
4947 if (session2
->smb2
== NULL
) {
4948 talloc_free(session2
);
4949 return NT_STATUS_NO_MEMORY
;
4952 talloc_set_destructor(session2
, smbXcli_session_destructor
);
4953 DLIST_ADD_END(conn
->sessions
, session2
, struct smbXcli_session
*);
4954 session2
->conn
= conn
;
4956 *_session2
= session2
;
4957 return NT_STATUS_OK
;
4960 NTSTATUS
smb2cli_session_set_channel_key(struct smbXcli_session
*session
,
4961 const DATA_BLOB _channel_key
,
4962 const struct iovec
*recv_iov
)
4964 struct smbXcli_conn
*conn
= session
->conn
;
4965 uint8_t channel_key
[16];
4969 return NT_STATUS_INVALID_PARAMETER_MIX
;
4972 if (session
->smb2_channel
.signing_key
.length
!= 0) {
4973 return NT_STATUS_INVALID_PARAMETER_MIX
;
4976 ZERO_STRUCT(channel_key
);
4977 memcpy(channel_key
, _channel_key
.data
,
4978 MIN(_channel_key
.length
, sizeof(channel_key
)));
4980 session
->smb2_channel
.signing_key
= data_blob_talloc(session
,
4982 sizeof(channel_key
));
4983 if (session
->smb2_channel
.signing_key
.data
== NULL
) {
4984 ZERO_STRUCT(channel_key
);
4985 return NT_STATUS_NO_MEMORY
;
4988 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4989 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCMAC");
4990 const DATA_BLOB context
= data_blob_string_const_null("SmbSign");
4992 smb2_key_derivation(channel_key
, sizeof(channel_key
),
4993 label
.data
, label
.length
,
4994 context
.data
, context
.length
,
4995 session
->smb2_channel
.signing_key
.data
);
4997 ZERO_STRUCT(channel_key
);
4999 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
5000 session
->conn
->protocol
,
5002 if (!NT_STATUS_IS_OK(status
)) {
5006 return NT_STATUS_OK
;
5009 NTSTATUS
smb2cli_session_encryption_on(struct smbXcli_session
*session
)
5011 if (session
->smb2
->should_encrypt
) {
5012 return NT_STATUS_OK
;
5015 if (session
->conn
->protocol
< PROTOCOL_SMB2_24
) {
5016 return NT_STATUS_NOT_SUPPORTED
;
5019 if (!(session
->conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
5020 return NT_STATUS_NOT_SUPPORTED
;
5023 if (session
->smb2
->signing_key
.data
== NULL
) {
5024 return NT_STATUS_NOT_SUPPORTED
;
5026 session
->smb2
->should_encrypt
= true;
5027 return NT_STATUS_OK
;
5030 struct smbXcli_tcon
*smbXcli_tcon_create(TALLOC_CTX
*mem_ctx
)
5032 struct smbXcli_tcon
*tcon
;
5034 tcon
= talloc_zero(mem_ctx
, struct smbXcli_tcon
);
5042 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon
*tcon
,
5043 uint32_t fs_attributes
)
5045 tcon
->fs_attributes
= fs_attributes
;
5048 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon
*tcon
)
5050 return tcon
->fs_attributes
;
5053 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon
*tcon
)
5059 if (tcon
->is_smb1
) {
5060 if (tcon
->smb1
.optional_support
& SMB_SHARE_IN_DFS
) {
5067 if (tcon
->smb2
.capabilities
& SMB2_SHARE_CAP_DFS
) {
5074 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
5076 return tcon
->smb1
.tcon_id
;
5079 void smb1cli_tcon_set_id(struct smbXcli_tcon
*tcon
, uint16_t tcon_id
)
5081 tcon
->is_smb1
= true;
5082 tcon
->smb1
.tcon_id
= tcon_id
;
5085 bool smb1cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
5087 uint16_t optional_support
,
5088 uint32_t maximal_access
,
5089 uint32_t guest_maximal_access
,
5090 const char *service
,
5091 const char *fs_type
)
5093 tcon
->is_smb1
= true;
5094 tcon
->fs_attributes
= 0;
5095 tcon
->smb1
.tcon_id
= tcon_id
;
5096 tcon
->smb1
.optional_support
= optional_support
;
5097 tcon
->smb1
.maximal_access
= maximal_access
;
5098 tcon
->smb1
.guest_maximal_access
= guest_maximal_access
;
5100 TALLOC_FREE(tcon
->smb1
.service
);
5101 tcon
->smb1
.service
= talloc_strdup(tcon
, service
);
5102 if (service
!= NULL
&& tcon
->smb1
.service
== NULL
) {
5106 TALLOC_FREE(tcon
->smb1
.fs_type
);
5107 tcon
->smb1
.fs_type
= talloc_strdup(tcon
, fs_type
);
5108 if (fs_type
!= NULL
&& tcon
->smb1
.fs_type
== NULL
) {
5115 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
5117 return tcon
->smb2
.tcon_id
;
5120 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon
*tcon
)
5122 return tcon
->smb2
.capabilities
;
5125 void smb2cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
5126 struct smbXcli_session
*session
,
5130 uint32_t capabilities
,
5131 uint32_t maximal_access
)
5133 tcon
->is_smb1
= false;
5134 tcon
->fs_attributes
= 0;
5135 tcon
->smb2
.tcon_id
= tcon_id
;
5136 tcon
->smb2
.type
= type
;
5137 tcon
->smb2
.flags
= flags
;
5138 tcon
->smb2
.capabilities
= capabilities
;
5139 tcon
->smb2
.maximal_access
= maximal_access
;
5141 tcon
->smb2
.should_encrypt
= false;
5143 if (session
== NULL
) {
5147 tcon
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
5149 if (flags
& SMB2_SHAREFLAG_ENCRYPT_DATA
) {
5150 tcon
->smb2
.should_encrypt
= true;