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
;
1277 state
->smb1
.recv_cmd
= 0xFF;
1278 state
->smb1
.recv_status
= NT_STATUS_INTERNAL_ERROR
;
1279 state
->smb1
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
1280 if (state
->smb1
.recv_iov
== NULL
) {
1285 smb1cli_req_flags(conn
->protocol
,
1286 conn
->smb1
.capabilities
,
1295 SIVAL(state
->smb1
.hdr
, 0, SMB_MAGIC
);
1296 SCVAL(state
->smb1
.hdr
, HDR_COM
, smb_command
);
1297 SIVAL(state
->smb1
.hdr
, HDR_RCLS
, NT_STATUS_V(NT_STATUS_OK
));
1298 SCVAL(state
->smb1
.hdr
, HDR_FLG
, flags
);
1299 SSVAL(state
->smb1
.hdr
, HDR_FLG2
, flags2
);
1300 SSVAL(state
->smb1
.hdr
, HDR_PIDHIGH
, pid
>> 16);
1301 SSVAL(state
->smb1
.hdr
, HDR_TID
, tid
);
1302 SSVAL(state
->smb1
.hdr
, HDR_PID
, pid
);
1303 SSVAL(state
->smb1
.hdr
, HDR_UID
, uid
);
1304 SSVAL(state
->smb1
.hdr
, HDR_MID
, 0); /* this comes later */
1305 SCVAL(state
->smb1
.hdr
, HDR_WCT
, wct
);
1307 state
->smb1
.vwv
= vwv
;
1309 SSVAL(state
->smb1
.bytecount_buf
, 0, smbXcli_iov_len(bytes_iov
, iov_count
));
1311 state
->smb1
.iov
[0].iov_base
= (void *)state
->length_hdr
;
1312 state
->smb1
.iov
[0].iov_len
= sizeof(state
->length_hdr
);
1313 state
->smb1
.iov
[1].iov_base
= (void *)state
->smb1
.hdr
;
1314 state
->smb1
.iov
[1].iov_len
= sizeof(state
->smb1
.hdr
);
1315 state
->smb1
.iov
[2].iov_base
= (void *)state
->smb1
.vwv
;
1316 state
->smb1
.iov
[2].iov_len
= wct
* sizeof(uint16_t);
1317 state
->smb1
.iov
[3].iov_base
= (void *)state
->smb1
.bytecount_buf
;
1318 state
->smb1
.iov
[3].iov_len
= sizeof(uint16_t);
1320 if (iov_count
!= 0) {
1321 memcpy(&state
->smb1
.iov
[4], bytes_iov
,
1322 iov_count
* sizeof(*bytes_iov
));
1324 state
->smb1
.iov_count
= iov_count
+ 4;
1326 if (timeout_msec
> 0) {
1327 struct timeval endtime
;
1329 endtime
= timeval_current_ofs_msec(timeout_msec
);
1330 if (!tevent_req_set_endtime(req
, ev
, endtime
)) {
1335 switch (smb_command
) {
1339 state
->one_way
= true;
1342 state
->one_way
= true;
1343 state
->smb1
.one_way_seqnum
= true;
1347 (CVAL(vwv
+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE
)) {
1348 state
->one_way
= true;
1356 static NTSTATUS
smb1cli_conn_signv(struct smbXcli_conn
*conn
,
1357 struct iovec
*iov
, int iov_count
,
1359 bool one_way_seqnum
)
1361 TALLOC_CTX
*frame
= NULL
;
1365 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1366 * iovec directly. MD5Update would do that just fine.
1369 if (iov_count
< 4) {
1370 return NT_STATUS_INVALID_PARAMETER_MIX
;
1372 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1373 return NT_STATUS_INVALID_PARAMETER_MIX
;
1375 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1376 return NT_STATUS_INVALID_PARAMETER_MIX
;
1378 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1379 return NT_STATUS_INVALID_PARAMETER_MIX
;
1381 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1382 return NT_STATUS_INVALID_PARAMETER_MIX
;
1385 frame
= talloc_stackframe();
1387 buf
= smbXcli_iov_concat(frame
, &iov
[1], iov_count
- 1);
1389 return NT_STATUS_NO_MEMORY
;
1392 *seqnum
= smb_signing_next_seqnum(conn
->smb1
.signing
,
1394 smb_signing_sign_pdu(conn
->smb1
.signing
,
1395 buf
, talloc_get_size(buf
),
1397 memcpy(iov
[1].iov_base
, buf
, iov
[1].iov_len
);
1400 return NT_STATUS_OK
;
1403 static void smb1cli_req_writev_done(struct tevent_req
*subreq
);
1404 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1405 TALLOC_CTX
*tmp_mem
,
1408 static NTSTATUS
smb1cli_req_writev_submit(struct tevent_req
*req
,
1409 struct smbXcli_req_state
*state
,
1410 struct iovec
*iov
, int iov_count
)
1412 struct tevent_req
*subreq
;
1417 if (!smbXcli_conn_is_connected(state
->conn
)) {
1418 return NT_STATUS_CONNECTION_DISCONNECTED
;
1421 if (state
->conn
->protocol
> PROTOCOL_NT1
) {
1422 return NT_STATUS_REVISION_MISMATCH
;
1425 if (iov_count
< 4) {
1426 return NT_STATUS_INVALID_PARAMETER_MIX
;
1428 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1429 return NT_STATUS_INVALID_PARAMETER_MIX
;
1431 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1432 return NT_STATUS_INVALID_PARAMETER_MIX
;
1434 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1435 return NT_STATUS_INVALID_PARAMETER_MIX
;
1437 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1438 return NT_STATUS_INVALID_PARAMETER_MIX
;
1441 cmd
= CVAL(iov
[1].iov_base
, HDR_COM
);
1442 if (cmd
== SMBreadBraw
) {
1443 if (smbXcli_conn_has_async_calls(state
->conn
)) {
1444 return NT_STATUS_INVALID_PARAMETER_MIX
;
1446 state
->conn
->smb1
.read_braw_req
= req
;
1449 if (state
->smb1
.mid
!= 0) {
1450 mid
= state
->smb1
.mid
;
1452 mid
= smb1cli_alloc_mid(state
->conn
);
1454 SSVAL(iov
[1].iov_base
, HDR_MID
, mid
);
1456 _smb_setlen_nbt(iov
[0].iov_base
, smbXcli_iov_len(&iov
[1], iov_count
-1));
1458 status
= smb1cli_conn_signv(state
->conn
, iov
, iov_count
,
1459 &state
->smb1
.seqnum
,
1460 state
->smb1
.one_way_seqnum
);
1462 if (!NT_STATUS_IS_OK(status
)) {
1467 * If we supported multiple encrytion contexts
1468 * here we'd look up based on tid.
1470 if (common_encryption_on(state
->conn
->smb1
.trans_enc
)) {
1471 char *buf
, *enc_buf
;
1473 buf
= (char *)smbXcli_iov_concat(talloc_tos(), iov
, iov_count
);
1475 return NT_STATUS_NO_MEMORY
;
1477 status
= common_encrypt_buffer(state
->conn
->smb1
.trans_enc
,
1478 (char *)buf
, &enc_buf
);
1480 if (!NT_STATUS_IS_OK(status
)) {
1481 DEBUG(0, ("Error in encrypting client message: %s\n",
1482 nt_errstr(status
)));
1485 buf
= (char *)talloc_memdup(state
, enc_buf
,
1486 smb_len_nbt(enc_buf
)+4);
1489 return NT_STATUS_NO_MEMORY
;
1491 iov
[0].iov_base
= (void *)buf
;
1492 iov
[0].iov_len
= talloc_get_size(buf
);
1496 if (state
->conn
->dispatch_incoming
== NULL
) {
1497 state
->conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
1500 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
1502 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
1503 state
->conn
->write_fd
, false, iov
, iov_count
);
1504 if (subreq
== NULL
) {
1505 return NT_STATUS_NO_MEMORY
;
1507 tevent_req_set_callback(subreq
, smb1cli_req_writev_done
, req
);
1508 return NT_STATUS_OK
;
1511 struct tevent_req
*smb1cli_req_send(TALLOC_CTX
*mem_ctx
,
1512 struct tevent_context
*ev
,
1513 struct smbXcli_conn
*conn
,
1514 uint8_t smb_command
,
1515 uint8_t additional_flags
,
1516 uint8_t clear_flags
,
1517 uint16_t additional_flags2
,
1518 uint16_t clear_flags2
,
1519 uint32_t timeout_msec
,
1521 struct smbXcli_tcon
*tcon
,
1522 struct smbXcli_session
*session
,
1523 uint8_t wct
, uint16_t *vwv
,
1525 const uint8_t *bytes
)
1527 struct tevent_req
*req
;
1531 iov
.iov_base
= discard_const_p(void, bytes
);
1532 iov
.iov_len
= num_bytes
;
1534 req
= smb1cli_req_create(mem_ctx
, ev
, conn
, smb_command
,
1535 additional_flags
, clear_flags
,
1536 additional_flags2
, clear_flags2
,
1543 if (!tevent_req_is_in_progress(req
)) {
1544 return tevent_req_post(req
, ev
);
1546 status
= smb1cli_req_chain_submit(&req
, 1);
1547 if (tevent_req_nterror(req
, status
)) {
1548 return tevent_req_post(req
, ev
);
1553 static void smb1cli_req_writev_done(struct tevent_req
*subreq
)
1555 struct tevent_req
*req
=
1556 tevent_req_callback_data(subreq
,
1558 struct smbXcli_req_state
*state
=
1559 tevent_req_data(req
,
1560 struct smbXcli_req_state
);
1564 nwritten
= writev_recv(subreq
, &err
);
1565 TALLOC_FREE(subreq
);
1566 if (nwritten
== -1) {
1567 NTSTATUS status
= map_nt_error_from_unix_common(err
);
1568 smbXcli_conn_disconnect(state
->conn
, status
);
1572 if (state
->one_way
) {
1573 state
->inbuf
= NULL
;
1574 tevent_req_done(req
);
1578 if (!smbXcli_req_set_pending(req
)) {
1579 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1584 static void smbXcli_conn_received(struct tevent_req
*subreq
)
1586 struct smbXcli_conn
*conn
=
1587 tevent_req_callback_data(subreq
,
1588 struct smbXcli_conn
);
1589 TALLOC_CTX
*frame
= talloc_stackframe();
1595 if (subreq
!= conn
->read_smb_req
) {
1596 DEBUG(1, ("Internal error: cli_smb_received called with "
1597 "unexpected subreq\n"));
1598 status
= NT_STATUS_INTERNAL_ERROR
;
1599 smbXcli_conn_disconnect(conn
, status
);
1603 conn
->read_smb_req
= NULL
;
1605 received
= read_smb_recv(subreq
, frame
, &inbuf
, &err
);
1606 TALLOC_FREE(subreq
);
1607 if (received
== -1) {
1608 status
= map_nt_error_from_unix_common(err
);
1609 smbXcli_conn_disconnect(conn
, status
);
1614 status
= conn
->dispatch_incoming(conn
, frame
, inbuf
);
1616 if (NT_STATUS_IS_OK(status
)) {
1618 * We should not do any more processing
1619 * as the dispatch function called
1620 * tevent_req_done().
1623 } else if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1625 * We got an error, so notify all pending requests
1627 smbXcli_conn_disconnect(conn
, status
);
1632 * We got NT_STATUS_RETRY, so we may ask for a
1633 * next incoming pdu.
1635 if (!smbXcli_conn_receive_next(conn
)) {
1636 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
1640 static NTSTATUS
smb1cli_inbuf_parse_chain(uint8_t *buf
, TALLOC_CTX
*mem_ctx
,
1641 struct iovec
**piov
, int *pnum_iov
)
1652 size_t min_size
= MIN_SMB_SIZE
;
1654 buflen
= smb_len_tcp(buf
);
1657 hdr
= buf
+ NBT_HDR_SIZE
;
1659 status
= smb1cli_pull_raw_error(hdr
);
1660 if (NT_STATUS_IS_ERR(status
)) {
1662 * This is an ugly hack to support OS/2
1663 * which skips the byte_count in the DATA block
1664 * on some error responses.
1668 min_size
-= sizeof(uint16_t);
1671 if (buflen
< min_size
) {
1672 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1676 * This returns iovec elements in the following order:
1691 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
1693 return NT_STATUS_NO_MEMORY
;
1695 iov
[0].iov_base
= hdr
;
1696 iov
[0].iov_len
= HDR_WCT
;
1699 cmd
= CVAL(hdr
, HDR_COM
);
1703 size_t len
= buflen
- taken
;
1705 struct iovec
*iov_tmp
;
1712 * we need at least WCT
1714 needed
= sizeof(uint8_t);
1716 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1717 __location__
, (int)len
, (int)needed
));
1722 * Now we check if the specified words are there
1724 wct
= CVAL(hdr
, wct_ofs
);
1725 needed
+= wct
* sizeof(uint16_t);
1727 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1728 __location__
, (int)len
, (int)needed
));
1732 if ((num_iov
== 1) &&
1734 NT_STATUS_IS_ERR(status
))
1737 * This is an ugly hack to support OS/2
1738 * which skips the byte_count in the DATA block
1739 * on some error responses.
1743 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
1745 if (iov_tmp
== NULL
) {
1747 return NT_STATUS_NO_MEMORY
;
1750 cur
= &iov
[num_iov
];
1754 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
1756 cur
[1].iov_base
= cur
[0].iov_base
;
1763 * we need at least BCC
1765 needed
+= sizeof(uint16_t);
1767 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1768 __location__
, (int)len
, (int)needed
));
1773 * Now we check if the specified bytes are there
1775 bcc_ofs
= wct_ofs
+ sizeof(uint8_t) + wct
* sizeof(uint16_t);
1776 bcc
= SVAL(hdr
, bcc_ofs
);
1777 needed
+= bcc
* sizeof(uint8_t);
1779 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1780 __location__
, (int)len
, (int)needed
));
1785 * we allocate 2 iovec structures for words and bytes
1787 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
1789 if (iov_tmp
== NULL
) {
1791 return NT_STATUS_NO_MEMORY
;
1794 cur
= &iov
[num_iov
];
1797 cur
[0].iov_len
= wct
* sizeof(uint16_t);
1798 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
1799 cur
[1].iov_len
= bcc
* sizeof(uint8_t);
1800 cur
[1].iov_base
= hdr
+ (bcc_ofs
+ sizeof(uint16_t));
1804 if (!smb1cli_is_andx_req(cmd
)) {
1806 * If the current command does not have AndX chanining
1812 if (wct
== 0 && bcc
== 0) {
1814 * An empty response also ends the chain,
1815 * most likely with an error.
1821 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1822 __location__
, (int)wct
, (int)cmd
));
1825 cmd
= CVAL(cur
[0].iov_base
, 0);
1828 * If it is the end of the chain we are also done.
1832 wct_ofs
= SVAL(cur
[0].iov_base
, 2);
1834 if (wct_ofs
< taken
) {
1835 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1837 if (wct_ofs
> buflen
) {
1838 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1842 * we consumed everything up to the start of the next
1848 remaining
= buflen
- taken
;
1850 if (remaining
> 0 && num_iov
>= 3) {
1852 * The last DATA block gets the remaining
1853 * bytes, this is needed to support
1854 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1856 iov
[num_iov
-1].iov_len
+= remaining
;
1860 *pnum_iov
= num_iov
;
1861 return NT_STATUS_OK
;
1865 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1868 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1869 TALLOC_CTX
*tmp_mem
,
1872 struct tevent_req
*req
;
1873 struct smbXcli_req_state
*state
;
1880 uint8_t *inhdr
= inbuf
+ NBT_HDR_SIZE
;
1881 size_t len
= smb_len_tcp(inbuf
);
1882 struct iovec
*iov
= NULL
;
1884 struct tevent_req
**chain
= NULL
;
1885 size_t num_chained
= 0;
1886 size_t num_responses
= 0;
1888 if (conn
->smb1
.read_braw_req
!= NULL
) {
1889 req
= conn
->smb1
.read_braw_req
;
1890 conn
->smb1
.read_braw_req
= NULL
;
1891 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1893 smbXcli_req_unset_pending(req
);
1895 if (state
->smb1
.recv_iov
== NULL
) {
1897 * For requests with more than
1898 * one response, we have to readd the
1901 state
->smb1
.recv_iov
= talloc_zero_array(state
,
1904 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
1905 return NT_STATUS_OK
;
1909 state
->smb1
.recv_iov
[0].iov_base
= (void *)(inhdr
);
1910 state
->smb1
.recv_iov
[0].iov_len
= len
;
1911 ZERO_STRUCT(state
->smb1
.recv_iov
[1]);
1912 ZERO_STRUCT(state
->smb1
.recv_iov
[2]);
1914 state
->smb1
.recv_cmd
= SMBreadBraw
;
1915 state
->smb1
.recv_status
= NT_STATUS_OK
;
1916 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
1918 tevent_req_done(req
);
1919 return NT_STATUS_OK
;
1922 if ((IVAL(inhdr
, 0) != SMB_MAGIC
) /* 0xFF"SMB" */
1923 && (SVAL(inhdr
, 0) != 0x45ff)) /* 0xFF"E" */ {
1924 DEBUG(10, ("Got non-SMB PDU\n"));
1925 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1929 * If we supported multiple encrytion contexts
1930 * here we'd look up based on tid.
1932 if (common_encryption_on(conn
->smb1
.trans_enc
)
1933 && (CVAL(inbuf
, 0) == 0)) {
1934 uint16_t enc_ctx_num
;
1936 status
= get_enc_ctx_num(inbuf
, &enc_ctx_num
);
1937 if (!NT_STATUS_IS_OK(status
)) {
1938 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1939 nt_errstr(status
)));
1943 if (enc_ctx_num
!= conn
->smb1
.trans_enc
->enc_ctx_num
) {
1944 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1946 conn
->smb1
.trans_enc
->enc_ctx_num
));
1947 return NT_STATUS_INVALID_HANDLE
;
1950 status
= common_decrypt_buffer(conn
->smb1
.trans_enc
,
1952 if (!NT_STATUS_IS_OK(status
)) {
1953 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1954 nt_errstr(status
)));
1957 inhdr
= inbuf
+ NBT_HDR_SIZE
;
1958 len
= smb_len_nbt(inbuf
);
1961 mid
= SVAL(inhdr
, HDR_MID
);
1962 num_pending
= talloc_array_length(conn
->pending
);
1964 for (i
=0; i
<num_pending
; i
++) {
1965 if (mid
== smb1cli_req_mid(conn
->pending
[i
])) {
1969 if (i
== num_pending
) {
1970 /* Dump unexpected reply */
1971 return NT_STATUS_RETRY
;
1974 oplock_break
= false;
1976 if (mid
== 0xffff) {
1978 * Paranoia checks that this is really an oplock break request.
1980 oplock_break
= (len
== 51); /* hdr + 8 words */
1981 oplock_break
&= ((CVAL(inhdr
, HDR_FLG
) & FLAG_REPLY
) == 0);
1982 oplock_break
&= (CVAL(inhdr
, HDR_COM
) == SMBlockingX
);
1983 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(6)) == 0);
1984 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(7)) == 0);
1986 if (!oplock_break
) {
1987 /* Dump unexpected reply */
1988 return NT_STATUS_RETRY
;
1992 req
= conn
->pending
[i
];
1993 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1995 if (!oplock_break
/* oplock breaks are not signed */
1996 && !smb_signing_check_pdu(conn
->smb1
.signing
,
1997 inhdr
, len
, state
->smb1
.seqnum
+1)) {
1998 DEBUG(10, ("cli_check_sign_mac failed\n"));
1999 return NT_STATUS_ACCESS_DENIED
;
2002 status
= smb1cli_inbuf_parse_chain(inbuf
, tmp_mem
,
2004 if (!NT_STATUS_IS_OK(status
)) {
2005 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2006 nt_errstr(status
)));
2010 cmd
= CVAL(inhdr
, HDR_COM
);
2011 status
= smb1cli_pull_raw_error(inhdr
);
2013 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
) &&
2014 (state
->session
!= NULL
) && state
->session
->disconnect_expired
)
2017 * this should be a short term hack
2018 * until the upper layers have implemented
2019 * re-authentication.
2024 if (state
->smb1
.chained_requests
== NULL
) {
2026 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2029 smbXcli_req_unset_pending(req
);
2031 if (state
->smb1
.recv_iov
== NULL
) {
2033 * For requests with more than
2034 * one response, we have to readd the
2037 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2040 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2041 return NT_STATUS_OK
;
2045 state
->smb1
.recv_cmd
= cmd
;
2046 state
->smb1
.recv_status
= status
;
2047 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
2049 state
->smb1
.recv_iov
[0] = iov
[0];
2050 state
->smb1
.recv_iov
[1] = iov
[1];
2051 state
->smb1
.recv_iov
[2] = iov
[2];
2053 if (talloc_array_length(conn
->pending
) == 0) {
2054 tevent_req_done(req
);
2055 return NT_STATUS_OK
;
2058 tevent_req_defer_callback(req
, state
->ev
);
2059 tevent_req_done(req
);
2060 return NT_STATUS_RETRY
;
2063 chain
= talloc_move(tmp_mem
, &state
->smb1
.chained_requests
);
2064 num_chained
= talloc_array_length(chain
);
2065 num_responses
= (num_iov
- 1)/2;
2067 if (num_responses
> num_chained
) {
2068 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2071 for (i
=0; i
<num_chained
; i
++) {
2072 size_t iov_idx
= 1 + (i
*2);
2073 struct iovec
*cur
= &iov
[iov_idx
];
2077 state
= tevent_req_data(req
, struct smbXcli_req_state
);
2079 smbXcli_req_unset_pending(req
);
2082 * as we finish multiple requests here
2083 * we need to defer the callbacks as
2084 * they could destroy our current stack state.
2086 tevent_req_defer_callback(req
, state
->ev
);
2088 if (i
>= num_responses
) {
2089 tevent_req_nterror(req
, NT_STATUS_REQUEST_ABORTED
);
2093 if (state
->smb1
.recv_iov
== NULL
) {
2095 * For requests with more than
2096 * one response, we have to readd the
2099 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2102 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2107 state
->smb1
.recv_cmd
= cmd
;
2109 if (i
== (num_responses
- 1)) {
2111 * The last request in the chain gets the status
2113 state
->smb1
.recv_status
= status
;
2115 cmd
= CVAL(cur
[0].iov_base
, 0);
2116 state
->smb1
.recv_status
= NT_STATUS_OK
;
2119 state
->inbuf
= inbuf
;
2122 * Note: here we use talloc_reference() in a way
2123 * that does not expose it to the caller.
2125 inbuf_ref
= talloc_reference(state
->smb1
.recv_iov
, inbuf
);
2126 if (tevent_req_nomem(inbuf_ref
, req
)) {
2130 /* copy the related buffers */
2131 state
->smb1
.recv_iov
[0] = iov
[0];
2132 state
->smb1
.recv_iov
[1] = cur
[0];
2133 state
->smb1
.recv_iov
[2] = cur
[1];
2135 tevent_req_done(req
);
2138 return NT_STATUS_RETRY
;
2141 NTSTATUS
smb1cli_req_recv(struct tevent_req
*req
,
2142 TALLOC_CTX
*mem_ctx
,
2143 struct iovec
**piov
,
2147 uint32_t *pvwv_offset
,
2148 uint32_t *pnum_bytes
,
2150 uint32_t *pbytes_offset
,
2152 const struct smb1cli_req_expected_response
*expected
,
2153 size_t num_expected
)
2155 struct smbXcli_req_state
*state
=
2156 tevent_req_data(req
,
2157 struct smbXcli_req_state
);
2158 NTSTATUS status
= NT_STATUS_OK
;
2159 struct iovec
*recv_iov
= NULL
;
2160 uint8_t *hdr
= NULL
;
2162 uint32_t vwv_offset
= 0;
2163 uint16_t *vwv
= NULL
;
2164 uint32_t num_bytes
= 0;
2165 uint32_t bytes_offset
= 0;
2166 uint8_t *bytes
= NULL
;
2168 bool found_status
= false;
2169 bool found_size
= false;
2183 if (pvwv_offset
!= NULL
) {
2186 if (pnum_bytes
!= NULL
) {
2189 if (pbytes
!= NULL
) {
2192 if (pbytes_offset
!= NULL
) {
2195 if (pinbuf
!= NULL
) {
2199 if (state
->inbuf
!= NULL
) {
2200 recv_iov
= state
->smb1
.recv_iov
;
2201 state
->smb1
.recv_iov
= NULL
;
2202 if (state
->smb1
.recv_cmd
!= SMBreadBraw
) {
2203 hdr
= (uint8_t *)recv_iov
[0].iov_base
;
2204 wct
= recv_iov
[1].iov_len
/2;
2205 vwv
= (uint16_t *)recv_iov
[1].iov_base
;
2206 vwv_offset
= PTR_DIFF(vwv
, hdr
);
2207 num_bytes
= recv_iov
[2].iov_len
;
2208 bytes
= (uint8_t *)recv_iov
[2].iov_base
;
2209 bytes_offset
= PTR_DIFF(bytes
, hdr
);
2213 if (tevent_req_is_nterror(req
, &status
)) {
2214 for (i
=0; i
< num_expected
; i
++) {
2215 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2216 found_status
= true;
2222 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
2228 if (num_expected
== 0) {
2229 found_status
= true;
2233 status
= state
->smb1
.recv_status
;
2235 for (i
=0; i
< num_expected
; i
++) {
2236 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2240 found_status
= true;
2241 if (expected
[i
].wct
== 0) {
2246 if (expected
[i
].wct
== wct
) {
2252 if (!found_status
) {
2257 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2261 *piov
= talloc_move(mem_ctx
, &recv_iov
);
2273 if (pvwv_offset
!= NULL
) {
2274 *pvwv_offset
= vwv_offset
;
2276 if (pnum_bytes
!= NULL
) {
2277 *pnum_bytes
= num_bytes
;
2279 if (pbytes
!= NULL
) {
2282 if (pbytes_offset
!= NULL
) {
2283 *pbytes_offset
= bytes_offset
;
2285 if (pinbuf
!= NULL
) {
2286 *pinbuf
= state
->inbuf
;
2292 size_t smb1cli_req_wct_ofs(struct tevent_req
**reqs
, int num_reqs
)
2299 for (i
=0; i
<num_reqs
; i
++) {
2300 struct smbXcli_req_state
*state
;
2301 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2302 wct_ofs
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2303 state
->smb1
.iov_count
-2);
2304 wct_ofs
= (wct_ofs
+ 3) & ~3;
2309 NTSTATUS
smb1cli_req_chain_submit(struct tevent_req
**reqs
, int num_reqs
)
2311 struct smbXcli_req_state
*first_state
=
2312 tevent_req_data(reqs
[0],
2313 struct smbXcli_req_state
);
2314 struct smbXcli_req_state
*state
;
2316 size_t chain_padding
= 0;
2318 struct iovec
*iov
= NULL
;
2319 struct iovec
*this_iov
;
2323 if (num_reqs
== 1) {
2324 return smb1cli_req_writev_submit(reqs
[0], first_state
,
2325 first_state
->smb1
.iov
,
2326 first_state
->smb1
.iov_count
);
2330 for (i
=0; i
<num_reqs
; i
++) {
2331 if (!tevent_req_is_in_progress(reqs
[i
])) {
2332 return NT_STATUS_INTERNAL_ERROR
;
2335 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2337 if (state
->smb1
.iov_count
< 4) {
2338 return NT_STATUS_INVALID_PARAMETER_MIX
;
2343 * The NBT and SMB header
2356 iovlen
+= state
->smb1
.iov_count
- 2;
2359 iov
= talloc_zero_array(first_state
, struct iovec
, iovlen
);
2361 return NT_STATUS_NO_MEMORY
;
2364 first_state
->smb1
.chained_requests
= (struct tevent_req
**)talloc_memdup(
2365 first_state
, reqs
, sizeof(*reqs
) * num_reqs
);
2366 if (first_state
->smb1
.chained_requests
== NULL
) {
2368 return NT_STATUS_NO_MEMORY
;
2371 wct_offset
= HDR_WCT
;
2374 for (i
=0; i
<num_reqs
; i
++) {
2375 size_t next_padding
= 0;
2378 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2380 if (i
< num_reqs
-1) {
2381 if (!smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))
2382 || CVAL(state
->smb1
.hdr
, HDR_WCT
) < 2) {
2384 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2385 return NT_STATUS_INVALID_PARAMETER_MIX
;
2389 wct_offset
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2390 state
->smb1
.iov_count
-2) + 1;
2391 if ((wct_offset
% 4) != 0) {
2392 next_padding
= 4 - (wct_offset
% 4);
2394 wct_offset
+= next_padding
;
2395 vwv
= state
->smb1
.vwv
;
2397 if (i
< num_reqs
-1) {
2398 struct smbXcli_req_state
*next_state
=
2399 tevent_req_data(reqs
[i
+1],
2400 struct smbXcli_req_state
);
2401 SCVAL(vwv
+0, 0, CVAL(next_state
->smb1
.hdr
, HDR_COM
));
2403 SSVAL(vwv
+1, 0, wct_offset
);
2404 } else if (smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))) {
2405 /* properly end the chain */
2406 SCVAL(vwv
+0, 0, 0xff);
2407 SCVAL(vwv
+0, 1, 0xff);
2413 * The NBT and SMB header
2415 this_iov
[0] = state
->smb1
.iov
[0];
2416 this_iov
[1] = state
->smb1
.iov
[1];
2420 * This one is a bit subtle. We have to add
2421 * chain_padding bytes between the requests, and we
2422 * have to also include the wct field of the
2423 * subsequent requests. We use the subsequent header
2424 * for the padding, it contains the wct field in its
2427 this_iov
[0].iov_len
= chain_padding
+1;
2428 this_iov
[0].iov_base
= (void *)&state
->smb1
.hdr
[
2429 sizeof(state
->smb1
.hdr
) - this_iov
[0].iov_len
];
2430 memset(this_iov
[0].iov_base
, 0, this_iov
[0].iov_len
-1);
2435 * copy the words and bytes
2437 memcpy(this_iov
, state
->smb1
.iov
+2,
2438 sizeof(struct iovec
) * (state
->smb1
.iov_count
-2));
2439 this_iov
+= state
->smb1
.iov_count
- 2;
2440 chain_padding
= next_padding
;
2443 nbt_len
= smbXcli_iov_len(&iov
[1], iovlen
-1);
2444 if (nbt_len
> first_state
->conn
->smb1
.max_xmit
) {
2446 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2447 return NT_STATUS_INVALID_PARAMETER_MIX
;
2450 status
= smb1cli_req_writev_submit(reqs
[0], first_state
, iov
, iovlen
);
2451 if (!NT_STATUS_IS_OK(status
)) {
2453 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2457 return NT_STATUS_OK
;
2460 bool smbXcli_conn_has_async_calls(struct smbXcli_conn
*conn
)
2462 return ((tevent_queue_length(conn
->outgoing
) != 0)
2463 || (talloc_array_length(conn
->pending
) != 0));
2466 bool smbXcli_conn_dfs_supported(struct smbXcli_conn
*conn
)
2468 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
2469 return (smb2cli_conn_server_capabilities(conn
) & SMB2_CAP_DFS
);
2472 return (smb1cli_conn_capabilities(conn
) & CAP_DFS
);
2475 bool smb2cli_conn_req_possible(struct smbXcli_conn
*conn
, uint32_t *max_dyn_len
)
2477 uint16_t credits
= 1;
2479 if (conn
->smb2
.cur_credits
== 0) {
2480 if (max_dyn_len
!= NULL
) {
2486 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2487 credits
= conn
->smb2
.cur_credits
;
2490 if (max_dyn_len
!= NULL
) {
2491 *max_dyn_len
= credits
* 65536;
2497 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn
*conn
)
2499 return conn
->smb2
.server
.capabilities
;
2502 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
2504 return conn
->smb2
.server
.security_mode
;
2507 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn
*conn
)
2509 return conn
->smb2
.server
.max_trans_size
;
2512 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn
*conn
)
2514 return conn
->smb2
.server
.max_read_size
;
2517 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn
*conn
)
2519 return conn
->smb2
.server
.max_write_size
;
2522 void smb2cli_conn_set_max_credits(struct smbXcli_conn
*conn
,
2523 uint16_t max_credits
)
2525 conn
->smb2
.max_credits
= max_credits
;
2528 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
);
2530 static bool smb2cli_req_cancel(struct tevent_req
*req
)
2532 struct smbXcli_req_state
*state
=
2533 tevent_req_data(req
,
2534 struct smbXcli_req_state
);
2535 uint32_t flags
= IVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
2536 uint64_t mid
= BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
);
2537 uint64_t aid
= BVAL(state
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
);
2538 struct smbXcli_tcon
*tcon
= state
->tcon
;
2539 struct smbXcli_session
*session
= state
->session
;
2540 uint8_t *fixed
= state
->smb2
.pad
;
2541 uint16_t fixed_len
= 4;
2542 struct tevent_req
*subreq
;
2543 struct smbXcli_req_state
*substate
;
2546 SSVAL(fixed
, 0, 0x04);
2549 subreq
= smb2cli_req_create(state
, state
->ev
,
2557 if (subreq
== NULL
) {
2560 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
2563 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2564 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2566 flags
&= SMB2_HDR_FLAG_ASYNC
;
2568 if (flags
& SMB2_HDR_FLAG_ASYNC
) {
2572 SIVAL(substate
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
2573 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
2574 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, aid
);
2576 status
= smb2cli_req_compound_submit(&subreq
, 1);
2577 if (!NT_STATUS_IS_OK(status
)) {
2578 TALLOC_FREE(subreq
);
2582 tevent_req_set_callback(subreq
, smb2cli_req_cancel_done
, NULL
);
2587 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
)
2589 /* we do not care about the result */
2590 TALLOC_FREE(subreq
);
2593 struct tevent_req
*smb2cli_req_create(TALLOC_CTX
*mem_ctx
,
2594 struct tevent_context
*ev
,
2595 struct smbXcli_conn
*conn
,
2597 uint32_t additional_flags
,
2598 uint32_t clear_flags
,
2599 uint32_t timeout_msec
,
2600 struct smbXcli_tcon
*tcon
,
2601 struct smbXcli_session
*session
,
2602 const uint8_t *fixed
,
2606 uint32_t max_dyn_len
)
2608 struct tevent_req
*req
;
2609 struct smbXcli_req_state
*state
;
2613 bool use_channel_sequence
= false;
2614 uint16_t channel_sequence
= 0;
2616 req
= tevent_req_create(mem_ctx
, &state
,
2617 struct smbXcli_req_state
);
2624 state
->session
= session
;
2627 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_PERSISTENT_HANDLES
) {
2628 use_channel_sequence
= true;
2629 } else if (conn
->smb2
.server
.capabilities
& SMB2_CAP_MULTI_CHANNEL
) {
2630 use_channel_sequence
= true;
2634 uid
= session
->smb2
->session_id
;
2636 if (use_channel_sequence
) {
2637 channel_sequence
= session
->smb2
->channel_sequence
;
2640 state
->smb2
.should_sign
= session
->smb2
->should_sign
;
2641 state
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
2643 if (cmd
== SMB2_OP_SESSSETUP
&&
2644 session
->smb2
->signing_key
.length
!= 0) {
2645 state
->smb2
.should_sign
= true;
2648 if (cmd
== SMB2_OP_SESSSETUP
&&
2649 session
->smb2_channel
.signing_key
.length
== 0) {
2650 state
->smb2
.should_encrypt
= false;
2655 tid
= tcon
->smb2
.tcon_id
;
2657 if (tcon
->smb2
.should_encrypt
) {
2658 state
->smb2
.should_encrypt
= true;
2662 if (state
->smb2
.should_encrypt
) {
2663 state
->smb2
.should_sign
= false;
2666 state
->smb2
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
2667 if (state
->smb2
.recv_iov
== NULL
) {
2672 flags
|= additional_flags
;
2673 flags
&= ~clear_flags
;
2675 state
->smb2
.fixed
= fixed
;
2676 state
->smb2
.fixed_len
= fixed_len
;
2677 state
->smb2
.dyn
= dyn
;
2678 state
->smb2
.dyn_len
= dyn_len
;
2679 state
->smb2
.max_dyn_len
= max_dyn_len
;
2681 if (state
->smb2
.should_encrypt
) {
2682 SIVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
2683 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
, uid
);
2686 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PROTOCOL_ID
, SMB2_MAGIC
);
2687 SSVAL(state
->smb2
.hdr
, SMB2_HDR_LENGTH
, SMB2_HDR_BODY
);
2688 SSVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
, cmd
);
2689 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CHANNEL_SEQUENCE
, channel_sequence
);
2690 SIVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
2691 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PID
, 0); /* reserved */
2692 SIVAL(state
->smb2
.hdr
, SMB2_HDR_TID
, tid
);
2693 SBVAL(state
->smb2
.hdr
, SMB2_HDR_SESSION_ID
, uid
);
2696 case SMB2_OP_CANCEL
:
2697 state
->one_way
= true;
2701 * If this is a dummy request, it will have
2702 * UINT64_MAX as message id.
2703 * If we send on break acknowledgement,
2704 * this gets overwritten later.
2706 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, UINT64_MAX
);
2710 if (timeout_msec
> 0) {
2711 struct timeval endtime
;
2713 endtime
= timeval_current_ofs_msec(timeout_msec
);
2714 if (!tevent_req_set_endtime(req
, ev
, endtime
)) {
2722 void smb2cli_req_set_notify_async(struct tevent_req
*req
)
2724 struct smbXcli_req_state
*state
=
2725 tevent_req_data(req
,
2726 struct smbXcli_req_state
);
2728 state
->smb2
.notify_async
= true;
2731 static void smb2cli_req_writev_done(struct tevent_req
*subreq
);
2732 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
2733 TALLOC_CTX
*tmp_mem
,
2736 NTSTATUS
smb2cli_req_compound_submit(struct tevent_req
**reqs
,
2739 struct smbXcli_req_state
*state
;
2740 struct tevent_req
*subreq
;
2742 int i
, num_iov
, nbt_len
;
2744 const DATA_BLOB
*encryption_key
= NULL
;
2745 uint64_t encryption_session_id
= 0;
2748 * 1 for the nbt length, optional TRANSFORM
2749 * per request: HDR, fixed, dyn, padding
2750 * -1 because the last one does not need padding
2753 iov
= talloc_array(reqs
[0], struct iovec
, 1 + 1 + 4*num_reqs
- 1);
2755 return NT_STATUS_NO_MEMORY
;
2762 * the session of the first request that requires encryption
2763 * specifies the encryption key.
2765 for (i
=0; i
<num_reqs
; i
++) {
2766 if (!tevent_req_is_in_progress(reqs
[i
])) {
2767 return NT_STATUS_INTERNAL_ERROR
;
2770 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2772 if (!smbXcli_conn_is_connected(state
->conn
)) {
2773 return NT_STATUS_CONNECTION_DISCONNECTED
;
2776 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
2777 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
2778 return NT_STATUS_REVISION_MISMATCH
;
2781 if (state
->session
== NULL
) {
2785 if (!state
->smb2
.should_encrypt
) {
2789 encryption_key
= &state
->session
->smb2
->encryption_key
;
2790 if (encryption_key
->length
== 0) {
2791 return NT_STATUS_INVALID_PARAMETER_MIX
;
2794 encryption_session_id
= state
->session
->smb2
->session_id
;
2797 iov
[num_iov
].iov_base
= state
->smb2
.transform
;
2798 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.transform
);
2801 SBVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
2802 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
,
2803 state
->session
->smb2
->nonce_low
);
2804 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
+8,
2805 state
->session
->smb2
->nonce_high
);
2806 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
,
2807 encryption_session_id
);
2809 state
->session
->smb2
->nonce_low
+= 1;
2810 if (state
->session
->smb2
->nonce_low
== 0) {
2811 state
->session
->smb2
->nonce_high
+= 1;
2812 state
->session
->smb2
->nonce_low
+= 1;
2815 nbt_len
+= SMB2_TF_HDR_SIZE
;
2819 for (i
=0; i
<num_reqs
; i
++) {
2828 const DATA_BLOB
*signing_key
= NULL
;
2830 if (!tevent_req_is_in_progress(reqs
[i
])) {
2831 return NT_STATUS_INTERNAL_ERROR
;
2834 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2836 if (!smbXcli_conn_is_connected(state
->conn
)) {
2837 return NT_STATUS_CONNECTION_DISCONNECTED
;
2840 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
2841 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
2842 return NT_STATUS_REVISION_MISMATCH
;
2845 opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
2846 if (opcode
== SMB2_OP_CANCEL
) {
2850 avail
= UINT64_MAX
- state
->conn
->smb2
.mid
;
2852 return NT_STATUS_CONNECTION_ABORTED
;
2855 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2856 uint32_t max_dyn_len
= 1;
2858 max_dyn_len
= MAX(max_dyn_len
, state
->smb2
.dyn_len
);
2859 max_dyn_len
= MAX(max_dyn_len
, state
->smb2
.max_dyn_len
);
2861 charge
= (max_dyn_len
- 1)/ 65536 + 1;
2866 charge
= MAX(state
->smb2
.credit_charge
, charge
);
2868 avail
= MIN(avail
, state
->conn
->smb2
.cur_credits
);
2869 if (avail
< charge
) {
2870 return NT_STATUS_INTERNAL_ERROR
;
2874 if (state
->conn
->smb2
.max_credits
> state
->conn
->smb2
.cur_credits
) {
2875 credits
= state
->conn
->smb2
.max_credits
-
2876 state
->conn
->smb2
.cur_credits
;
2878 if (state
->conn
->smb2
.max_credits
>= state
->conn
->smb2
.cur_credits
) {
2882 mid
= state
->conn
->smb2
.mid
;
2883 state
->conn
->smb2
.mid
+= charge
;
2884 state
->conn
->smb2
.cur_credits
-= charge
;
2886 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2887 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT_CHARGE
, charge
);
2889 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT
, credits
);
2890 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
2893 if (state
->session
&& encryption_key
== NULL
) {
2895 * We prefer the channel signing key if it is
2898 if (state
->smb2
.should_sign
) {
2899 signing_key
= &state
->session
->smb2_channel
.signing_key
;
2903 * If it is a channel binding, we already have the main
2904 * signing key and try that one.
2906 if (signing_key
&& signing_key
->length
== 0) {
2907 signing_key
= &state
->session
->smb2
->signing_key
;
2911 * If we do not have any session key yet, we skip the
2912 * signing of SMB2_OP_SESSSETUP requests.
2914 if (signing_key
&& signing_key
->length
== 0) {
2920 iov
[num_iov
].iov_base
= state
->smb2
.hdr
;
2921 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.hdr
);
2924 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.fixed
);
2925 iov
[num_iov
].iov_len
= state
->smb2
.fixed_len
;
2928 if (state
->smb2
.dyn
!= NULL
) {
2929 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.dyn
);
2930 iov
[num_iov
].iov_len
= state
->smb2
.dyn_len
;
2934 reqlen
= sizeof(state
->smb2
.hdr
);
2935 reqlen
+= state
->smb2
.fixed_len
;
2936 reqlen
+= state
->smb2
.dyn_len
;
2938 if (i
< num_reqs
-1) {
2939 if ((reqlen
% 8) > 0) {
2940 uint8_t pad
= 8 - (reqlen
% 8);
2941 iov
[num_iov
].iov_base
= state
->smb2
.pad
;
2942 iov
[num_iov
].iov_len
= pad
;
2946 SIVAL(state
->smb2
.hdr
, SMB2_HDR_NEXT_COMMAND
, reqlen
);
2949 state
->smb2
.encryption_session_id
= encryption_session_id
;
2951 if (signing_key
!= NULL
) {
2954 status
= smb2_signing_sign_pdu(*signing_key
,
2955 state
->session
->conn
->protocol
,
2956 &iov
[hdr_iov
], num_iov
- hdr_iov
);
2957 if (!NT_STATUS_IS_OK(status
)) {
2964 ret
= smbXcli_req_set_pending(reqs
[i
]);
2966 return NT_STATUS_NO_MEMORY
;
2970 state
= tevent_req_data(reqs
[0], struct smbXcli_req_state
);
2971 _smb_setlen_tcp(state
->length_hdr
, nbt_len
);
2972 iov
[0].iov_base
= state
->length_hdr
;
2973 iov
[0].iov_len
= sizeof(state
->length_hdr
);
2975 if (encryption_key
!= NULL
) {
2977 size_t buflen
= nbt_len
- SMB2_TF_HDR_SIZE
;
2981 buf
= talloc_array(iov
, uint8_t, buflen
);
2983 return NT_STATUS_NO_MEMORY
;
2987 * We copy the buffers before encrypting them,
2988 * this is at least currently needed for the
2989 * to keep state->smb2.hdr.
2991 * Also the callers may expect there buffers
2994 for (vi
= tf_iov
+ 1; vi
< num_iov
; vi
++) {
2995 struct iovec
*v
= &iov
[vi
];
2996 const uint8_t *o
= (const uint8_t *)v
->iov_base
;
2998 memcpy(buf
, o
, v
->iov_len
);
2999 v
->iov_base
= (void *)buf
;
3003 status
= smb2_signing_encrypt_pdu(*encryption_key
,
3004 state
->conn
->protocol
,
3005 &iov
[tf_iov
], num_iov
- tf_iov
);
3006 if (!NT_STATUS_IS_OK(status
)) {
3011 if (state
->conn
->dispatch_incoming
== NULL
) {
3012 state
->conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
3015 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
3016 state
->conn
->write_fd
, false, iov
, num_iov
);
3017 if (subreq
== NULL
) {
3018 return NT_STATUS_NO_MEMORY
;
3020 tevent_req_set_callback(subreq
, smb2cli_req_writev_done
, reqs
[0]);
3021 return NT_STATUS_OK
;
3024 void smb2cli_req_set_credit_charge(struct tevent_req
*req
, uint16_t charge
)
3026 struct smbXcli_req_state
*state
=
3027 tevent_req_data(req
,
3028 struct smbXcli_req_state
);
3030 state
->smb2
.credit_charge
= charge
;
3033 struct tevent_req
*smb2cli_req_send(TALLOC_CTX
*mem_ctx
,
3034 struct tevent_context
*ev
,
3035 struct smbXcli_conn
*conn
,
3037 uint32_t additional_flags
,
3038 uint32_t clear_flags
,
3039 uint32_t timeout_msec
,
3040 struct smbXcli_tcon
*tcon
,
3041 struct smbXcli_session
*session
,
3042 const uint8_t *fixed
,
3046 uint32_t max_dyn_len
)
3048 struct tevent_req
*req
;
3051 req
= smb2cli_req_create(mem_ctx
, ev
, conn
, cmd
,
3052 additional_flags
, clear_flags
,
3061 if (!tevent_req_is_in_progress(req
)) {
3062 return tevent_req_post(req
, ev
);
3064 status
= smb2cli_req_compound_submit(&req
, 1);
3065 if (tevent_req_nterror(req
, status
)) {
3066 return tevent_req_post(req
, ev
);
3071 static void smb2cli_req_writev_done(struct tevent_req
*subreq
)
3073 struct tevent_req
*req
=
3074 tevent_req_callback_data(subreq
,
3076 struct smbXcli_req_state
*state
=
3077 tevent_req_data(req
,
3078 struct smbXcli_req_state
);
3082 nwritten
= writev_recv(subreq
, &err
);
3083 TALLOC_FREE(subreq
);
3084 if (nwritten
== -1) {
3085 /* here, we need to notify all pending requests */
3086 NTSTATUS status
= map_nt_error_from_unix_common(err
);
3087 smbXcli_conn_disconnect(state
->conn
, status
);
3092 static NTSTATUS
smb2cli_inbuf_parse_compound(struct smbXcli_conn
*conn
,
3095 TALLOC_CTX
*mem_ctx
,
3096 struct iovec
**piov
, int *pnum_iov
)
3101 uint8_t *first_hdr
= buf
;
3102 size_t verified_buflen
= 0;
3106 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
3108 return NT_STATUS_NO_MEMORY
;
3111 while (taken
< buflen
) {
3112 size_t len
= buflen
- taken
;
3113 uint8_t *hdr
= first_hdr
+ taken
;
3116 size_t next_command_ofs
;
3118 struct iovec
*iov_tmp
;
3120 if (verified_buflen
> taken
) {
3121 len
= verified_buflen
- taken
;
3128 DEBUG(10, ("%d bytes left, expected at least %d\n",
3132 if (IVAL(hdr
, 0) == SMB2_TF_MAGIC
) {
3133 struct smbXcli_session
*s
;
3135 struct iovec tf_iov
[2];
3139 if (len
< SMB2_TF_HDR_SIZE
) {
3140 DEBUG(10, ("%d bytes left, expected at least %d\n",
3141 (int)len
, SMB2_TF_HDR_SIZE
));
3145 tf_len
= SMB2_TF_HDR_SIZE
;
3148 hdr
= first_hdr
+ taken
;
3149 enc_len
= IVAL(tf
, SMB2_TF_MSG_SIZE
);
3150 uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
3152 if (len
< SMB2_TF_HDR_SIZE
+ enc_len
) {
3153 DEBUG(10, ("%d bytes left, expected at least %d\n",
3155 (int)(SMB2_TF_HDR_SIZE
+ enc_len
)));
3160 for (; s
; s
= s
->next
) {
3161 if (s
->smb2
->session_id
!= uid
) {
3168 DEBUG(10, ("unknown session_id %llu\n",
3169 (unsigned long long)uid
));
3173 tf_iov
[0].iov_base
= (void *)tf
;
3174 tf_iov
[0].iov_len
= tf_len
;
3175 tf_iov
[1].iov_base
= (void *)hdr
;
3176 tf_iov
[1].iov_len
= enc_len
;
3178 status
= smb2_signing_decrypt_pdu(s
->smb2
->decryption_key
,
3181 if (!NT_STATUS_IS_OK(status
)) {
3186 verified_buflen
= taken
+ enc_len
;
3191 * We need the header plus the body length field
3194 if (len
< SMB2_HDR_BODY
+ 2) {
3195 DEBUG(10, ("%d bytes left, expected at least %d\n",
3196 (int)len
, SMB2_HDR_BODY
));
3199 if (IVAL(hdr
, 0) != SMB2_MAGIC
) {
3200 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3204 if (SVAL(hdr
, 4) != SMB2_HDR_BODY
) {
3205 DEBUG(10, ("Got HDR len %d, expected %d\n",
3206 SVAL(hdr
, 4), SMB2_HDR_BODY
));
3211 next_command_ofs
= IVAL(hdr
, SMB2_HDR_NEXT_COMMAND
);
3212 body_size
= SVAL(hdr
, SMB2_HDR_BODY
);
3214 if (next_command_ofs
!= 0) {
3215 if (next_command_ofs
< (SMB2_HDR_BODY
+ 2)) {
3218 if (next_command_ofs
> full_size
) {
3221 full_size
= next_command_ofs
;
3223 if (body_size
< 2) {
3226 body_size
&= 0xfffe;
3228 if (body_size
> (full_size
- SMB2_HDR_BODY
)) {
3232 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
3234 if (iov_tmp
== NULL
) {
3236 return NT_STATUS_NO_MEMORY
;
3239 cur
= &iov
[num_iov
];
3242 cur
[0].iov_base
= tf
;
3243 cur
[0].iov_len
= tf_len
;
3244 cur
[1].iov_base
= hdr
;
3245 cur
[1].iov_len
= SMB2_HDR_BODY
;
3246 cur
[2].iov_base
= hdr
+ SMB2_HDR_BODY
;
3247 cur
[2].iov_len
= body_size
;
3248 cur
[3].iov_base
= hdr
+ SMB2_HDR_BODY
+ body_size
;
3249 cur
[3].iov_len
= full_size
- (SMB2_HDR_BODY
+ body_size
);
3255 *pnum_iov
= num_iov
;
3256 return NT_STATUS_OK
;
3260 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3263 static struct tevent_req
*smb2cli_conn_find_pending(struct smbXcli_conn
*conn
,
3266 size_t num_pending
= talloc_array_length(conn
->pending
);
3269 for (i
=0; i
<num_pending
; i
++) {
3270 struct tevent_req
*req
= conn
->pending
[i
];
3271 struct smbXcli_req_state
*state
=
3272 tevent_req_data(req
,
3273 struct smbXcli_req_state
);
3275 if (mid
== BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
)) {
3282 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
3283 TALLOC_CTX
*tmp_mem
,
3286 struct tevent_req
*req
;
3287 struct smbXcli_req_state
*state
= NULL
;
3292 struct smbXcli_session
*last_session
= NULL
;
3293 size_t inbuf_len
= smb_len_tcp(inbuf
);
3295 status
= smb2cli_inbuf_parse_compound(conn
,
3296 inbuf
+ NBT_HDR_SIZE
,
3300 if (!NT_STATUS_IS_OK(status
)) {
3304 for (i
=0; i
<num_iov
; i
+=4) {
3305 uint8_t *inbuf_ref
= NULL
;
3306 struct iovec
*cur
= &iov
[i
];
3307 uint8_t *inhdr
= (uint8_t *)cur
[1].iov_base
;
3308 uint16_t opcode
= SVAL(inhdr
, SMB2_HDR_OPCODE
);
3309 uint32_t flags
= IVAL(inhdr
, SMB2_HDR_FLAGS
);
3310 uint64_t mid
= BVAL(inhdr
, SMB2_HDR_MESSAGE_ID
);
3311 uint16_t req_opcode
;
3313 uint16_t credits
= SVAL(inhdr
, SMB2_HDR_CREDIT
);
3314 uint32_t new_credits
;
3315 struct smbXcli_session
*session
= NULL
;
3316 const DATA_BLOB
*signing_key
= NULL
;
3317 bool was_encrypted
= false;
3319 new_credits
= conn
->smb2
.cur_credits
;
3320 new_credits
+= credits
;
3321 if (new_credits
> UINT16_MAX
) {
3322 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3324 conn
->smb2
.cur_credits
+= credits
;
3326 req
= smb2cli_conn_find_pending(conn
, mid
);
3328 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3330 state
= tevent_req_data(req
, struct smbXcli_req_state
);
3332 state
->smb2
.got_async
= false;
3334 req_opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
3335 if (opcode
!= req_opcode
) {
3336 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3338 req_flags
= SVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
3340 if (!(flags
& SMB2_HDR_FLAG_REDIRECT
)) {
3341 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3344 status
= NT_STATUS(IVAL(inhdr
, SMB2_HDR_STATUS
));
3345 if ((flags
& SMB2_HDR_FLAG_ASYNC
) &&
3346 NT_STATUS_EQUAL(status
, STATUS_PENDING
)) {
3347 uint64_t async_id
= BVAL(inhdr
, SMB2_HDR_ASYNC_ID
);
3350 * async interim responses are not signed,
3351 * even if the SMB2_HDR_FLAG_SIGNED flag
3354 req_flags
|= SMB2_HDR_FLAG_ASYNC
;
3355 SBVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, req_flags
);
3356 SBVAL(state
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, async_id
);
3358 if (state
->smb2
.notify_async
) {
3359 state
->smb2
.got_async
= true;
3360 tevent_req_defer_callback(req
, state
->ev
);
3361 tevent_req_notify_callback(req
);
3366 session
= state
->session
;
3367 if (req_flags
& SMB2_HDR_FLAG_CHAINED
) {
3368 session
= last_session
;
3370 last_session
= session
;
3372 if (state
->smb2
.should_sign
) {
3373 if (!(flags
& SMB2_HDR_FLAG_SIGNED
)) {
3374 return NT_STATUS_ACCESS_DENIED
;
3378 if (flags
& SMB2_HDR_FLAG_SIGNED
) {
3379 uint64_t uid
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
3381 if (session
== NULL
) {
3382 struct smbXcli_session
*s
;
3384 s
= state
->conn
->sessions
;
3385 for (; s
; s
= s
->next
) {
3386 if (s
->smb2
->session_id
!= uid
) {
3395 if (session
== NULL
) {
3396 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3399 last_session
= session
;
3400 signing_key
= &session
->smb2_channel
.signing_key
;
3403 if (opcode
== SMB2_OP_SESSSETUP
) {
3405 * We prefer the channel signing key, if it is
3408 * If we do not have a channel signing key yet,
3409 * we try the main signing key, if it is not
3410 * the final response.
3412 if (signing_key
&& signing_key
->length
== 0 &&
3413 !NT_STATUS_IS_OK(status
)) {
3414 signing_key
= &session
->smb2
->signing_key
;
3417 if (signing_key
&& signing_key
->length
== 0) {
3419 * If we do not have a session key to
3420 * verify the signature, we defer the
3421 * signing check to the caller.
3423 * The caller gets NT_STATUS_OK, it
3425 * smb2cli_session_set_session_key()
3427 * smb2cli_session_set_channel_key()
3428 * which will check the signature
3429 * with the channel signing key.
3435 if (cur
[0].iov_len
== SMB2_TF_HDR_SIZE
) {
3436 const uint8_t *tf
= (const uint8_t *)cur
[0].iov_base
;
3437 uint64_t uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
3440 * If the response was encrypted in a SMB2_TRANSFORM
3441 * pdu, which belongs to the correct session,
3442 * we do not need to do signing checks
3444 * It could be the session the response belongs to
3445 * or the session that was used to encrypt the
3446 * SMB2_TRANSFORM request.
3448 if ((session
&& session
->smb2
->session_id
== uid
) ||
3449 (state
->smb2
.encryption_session_id
== uid
)) {
3451 was_encrypted
= true;
3455 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_SESSION_DELETED
)) {
3457 * if the server returns NT_STATUS_USER_SESSION_DELETED
3458 * the response is not signed and we should
3459 * propagate the NT_STATUS_USER_SESSION_DELETED
3460 * status to the caller.
3462 state
->smb2
.signing_skipped
= true;
3466 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
3468 * if the server returns
3469 * NT_STATUS_INVALID_PARAMETER
3470 * the response might not be encrypted.
3472 if (state
->smb2
.should_encrypt
&& !was_encrypted
) {
3473 state
->smb2
.signing_skipped
= true;
3478 if (state
->smb2
.should_encrypt
&& !was_encrypted
) {
3479 if (!state
->smb2
.signing_skipped
) {
3480 return NT_STATUS_ACCESS_DENIED
;
3484 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_NAME_DELETED
) ||
3485 NT_STATUS_EQUAL(status
, NT_STATUS_FILE_CLOSED
) ||
3486 NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
3488 * if the server returns
3489 * NT_STATUS_NETWORK_NAME_DELETED
3490 * NT_STATUS_FILE_CLOSED
3491 * NT_STATUS_INVALID_PARAMETER
3492 * the response might not be signed
3493 * as this happens before the signing checks.
3495 * If server echos the signature (or all zeros)
3496 * we should report the status from the server
3502 cmp
= memcmp(inhdr
+SMB2_HDR_SIGNATURE
,
3503 state
->smb2
.hdr
+SMB2_HDR_SIGNATURE
,
3506 state
->smb2
.signing_skipped
= true;
3512 static const uint8_t zeros
[16];
3514 cmp
= memcmp(inhdr
+SMB2_HDR_SIGNATURE
,
3518 state
->smb2
.signing_skipped
= true;
3525 status
= smb2_signing_check_pdu(*signing_key
,
3526 state
->conn
->protocol
,
3528 if (!NT_STATUS_IS_OK(status
)) {
3530 * If the signing check fails, we disconnect
3537 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
) &&
3538 (session
!= NULL
) && session
->disconnect_expired
)
3541 * this should be a short term hack
3542 * until the upper layers have implemented
3543 * re-authentication.
3548 smbXcli_req_unset_pending(req
);
3551 * There might be more than one response
3552 * we need to defer the notifications
3554 if ((num_iov
== 5) && (talloc_array_length(conn
->pending
) == 0)) {
3559 tevent_req_defer_callback(req
, state
->ev
);
3563 * Note: here we use talloc_reference() in a way
3564 * that does not expose it to the caller.
3566 inbuf_ref
= talloc_reference(state
->smb2
.recv_iov
, inbuf
);
3567 if (tevent_req_nomem(inbuf_ref
, req
)) {
3571 /* copy the related buffers */
3572 state
->smb2
.recv_iov
[0] = cur
[1];
3573 state
->smb2
.recv_iov
[1] = cur
[2];
3574 state
->smb2
.recv_iov
[2] = cur
[3];
3576 tevent_req_done(req
);
3580 return NT_STATUS_RETRY
;
3583 return NT_STATUS_OK
;
3586 NTSTATUS
smb2cli_req_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
3587 struct iovec
**piov
,
3588 const struct smb2cli_req_expected_response
*expected
,
3589 size_t num_expected
)
3591 struct smbXcli_req_state
*state
=
3592 tevent_req_data(req
,
3593 struct smbXcli_req_state
);
3596 bool found_status
= false;
3597 bool found_size
= false;
3604 if (state
->smb2
.got_async
) {
3605 return STATUS_PENDING
;
3608 if (tevent_req_is_nterror(req
, &status
)) {
3609 for (i
=0; i
< num_expected
; i
++) {
3610 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
3611 found_status
= true;
3617 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
3623 if (num_expected
== 0) {
3624 found_status
= true;
3628 status
= NT_STATUS(IVAL(state
->smb2
.recv_iov
[0].iov_base
, SMB2_HDR_STATUS
));
3629 body_size
= SVAL(state
->smb2
.recv_iov
[1].iov_base
, 0);
3631 for (i
=0; i
< num_expected
; i
++) {
3632 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
3636 found_status
= true;
3637 if (expected
[i
].body_size
== 0) {
3642 if (expected
[i
].body_size
== body_size
) {
3648 if (!found_status
) {
3652 if (state
->smb2
.signing_skipped
) {
3653 if (num_expected
> 0) {
3654 return NT_STATUS_ACCESS_DENIED
;
3656 if (!NT_STATUS_IS_ERR(status
)) {
3657 return NT_STATUS_ACCESS_DENIED
;
3662 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3666 *piov
= talloc_move(mem_ctx
, &state
->smb2
.recv_iov
);
3672 static const struct {
3673 enum protocol_types proto
;
3674 const char *smb1_name
;
3675 } smb1cli_prots
[] = {
3676 {PROTOCOL_CORE
, "PC NETWORK PROGRAM 1.0"},
3677 {PROTOCOL_COREPLUS
, "MICROSOFT NETWORKS 1.03"},
3678 {PROTOCOL_LANMAN1
, "MICROSOFT NETWORKS 3.0"},
3679 {PROTOCOL_LANMAN1
, "LANMAN1.0"},
3680 {PROTOCOL_LANMAN2
, "LM1.2X002"},
3681 {PROTOCOL_LANMAN2
, "DOS LANMAN2.1"},
3682 {PROTOCOL_LANMAN2
, "LANMAN2.1"},
3683 {PROTOCOL_LANMAN2
, "Samba"},
3684 {PROTOCOL_NT1
, "NT LANMAN 1.0"},
3685 {PROTOCOL_NT1
, "NT LM 0.12"},
3686 {PROTOCOL_SMB2_02
, "SMB 2.002"},
3687 {PROTOCOL_SMB2_10
, "SMB 2.???"},
3690 static const struct {
3691 enum protocol_types proto
;
3692 uint16_t smb2_dialect
;
3693 } smb2cli_prots
[] = {
3694 {PROTOCOL_SMB2_02
, SMB2_DIALECT_REVISION_202
},
3695 {PROTOCOL_SMB2_10
, SMB2_DIALECT_REVISION_210
},
3696 {PROTOCOL_SMB2_22
, SMB2_DIALECT_REVISION_222
},
3697 {PROTOCOL_SMB2_24
, SMB2_DIALECT_REVISION_224
},
3698 {PROTOCOL_SMB3_00
, SMB3_DIALECT_REVISION_300
},
3701 struct smbXcli_negprot_state
{
3702 struct smbXcli_conn
*conn
;
3703 struct tevent_context
*ev
;
3704 uint32_t timeout_msec
;
3705 enum protocol_types min_protocol
;
3706 enum protocol_types max_protocol
;
3710 uint8_t dyn
[ARRAY_SIZE(smb2cli_prots
)*2];
3714 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
);
3715 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
);
3716 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
);
3717 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
);
3718 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
);
3719 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
3723 struct tevent_req
*smbXcli_negprot_send(TALLOC_CTX
*mem_ctx
,
3724 struct tevent_context
*ev
,
3725 struct smbXcli_conn
*conn
,
3726 uint32_t timeout_msec
,
3727 enum protocol_types min_protocol
,
3728 enum protocol_types max_protocol
)
3730 struct tevent_req
*req
, *subreq
;
3731 struct smbXcli_negprot_state
*state
;
3733 req
= tevent_req_create(mem_ctx
, &state
,
3734 struct smbXcli_negprot_state
);
3740 state
->timeout_msec
= timeout_msec
;
3741 state
->min_protocol
= min_protocol
;
3742 state
->max_protocol
= max_protocol
;
3744 if (min_protocol
== PROTOCOL_NONE
) {
3745 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3746 return tevent_req_post(req
, ev
);
3749 if (max_protocol
== PROTOCOL_NONE
) {
3750 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3751 return tevent_req_post(req
, ev
);
3754 if (min_protocol
> max_protocol
) {
3755 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3756 return tevent_req_post(req
, ev
);
3759 if ((min_protocol
< PROTOCOL_SMB2_02
) &&
3760 (max_protocol
< PROTOCOL_SMB2_02
)) {
3764 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
3766 subreq
= smbXcli_negprot_smb1_subreq(state
);
3767 if (tevent_req_nomem(subreq
, req
)) {
3768 return tevent_req_post(req
, ev
);
3770 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
3774 if ((min_protocol
>= PROTOCOL_SMB2_02
) &&
3775 (max_protocol
>= PROTOCOL_SMB2_02
)) {
3779 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
3781 subreq
= smbXcli_negprot_smb2_subreq(state
);
3782 if (tevent_req_nomem(subreq
, req
)) {
3783 return tevent_req_post(req
, ev
);
3785 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
3790 * We send an SMB1 negprot with the SMB2 dialects
3791 * and expect a SMB1 or a SMB2 response.
3793 * smbXcli_negprot_dispatch_incoming() will fix the
3794 * callback to match protocol of the response.
3796 conn
->dispatch_incoming
= smbXcli_negprot_dispatch_incoming
;
3798 subreq
= smbXcli_negprot_smb1_subreq(state
);
3799 if (tevent_req_nomem(subreq
, req
)) {
3800 return tevent_req_post(req
, ev
);
3802 tevent_req_set_callback(subreq
, smbXcli_negprot_invalid_done
, req
);
3806 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
)
3808 struct tevent_req
*req
=
3809 tevent_req_callback_data(subreq
,
3814 * we just want the low level error
3816 status
= tevent_req_simple_recv_ntstatus(subreq
);
3817 TALLOC_FREE(subreq
);
3818 if (tevent_req_nterror(req
, status
)) {
3822 /* this should never happen */
3823 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
3826 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
)
3829 DATA_BLOB bytes
= data_blob_null
;
3833 /* setup the protocol strings */
3834 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
3838 if (smb1cli_prots
[i
].proto
< state
->min_protocol
) {
3842 if (smb1cli_prots
[i
].proto
> state
->max_protocol
) {
3846 ok
= data_blob_append(state
, &bytes
, &c
, sizeof(c
));
3852 * We now it is already ascii and
3853 * we want NULL termination.
3855 ok
= data_blob_append(state
, &bytes
,
3856 smb1cli_prots
[i
].smb1_name
,
3857 strlen(smb1cli_prots
[i
].smb1_name
)+1);
3863 smb1cli_req_flags(state
->max_protocol
,
3864 state
->conn
->smb1
.client
.capabilities
,
3869 return smb1cli_req_send(state
, state
->ev
, state
->conn
,
3873 state
->timeout_msec
,
3874 0xFFFE, 0, NULL
, /* pid, tid, session */
3875 0, NULL
, /* wct, vwv */
3876 bytes
.length
, bytes
.data
);
3879 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
)
3881 struct tevent_req
*req
=
3882 tevent_req_callback_data(subreq
,
3884 struct smbXcli_negprot_state
*state
=
3885 tevent_req_data(req
,
3886 struct smbXcli_negprot_state
);
3887 struct smbXcli_conn
*conn
= state
->conn
;
3888 struct iovec
*recv_iov
= NULL
;
3897 size_t num_prots
= 0;
3899 uint32_t client_capabilities
= conn
->smb1
.client
.capabilities
;
3900 uint32_t both_capabilities
;
3901 uint32_t server_capabilities
= 0;
3902 uint32_t capabilities
;
3903 uint32_t client_max_xmit
= conn
->smb1
.client
.max_xmit
;
3904 uint32_t server_max_xmit
= 0;
3906 uint32_t server_max_mux
= 0;
3907 uint16_t server_security_mode
= 0;
3908 uint32_t server_session_key
= 0;
3909 bool server_readbraw
= false;
3910 bool server_writebraw
= false;
3911 bool server_lockread
= false;
3912 bool server_writeunlock
= false;
3913 struct GUID server_guid
= GUID_zero();
3914 DATA_BLOB server_gss_blob
= data_blob_null
;
3915 uint8_t server_challenge
[8];
3916 char *server_workgroup
= NULL
;
3917 char *server_name
= NULL
;
3918 int server_time_zone
= 0;
3919 NTTIME server_system_time
= 0;
3920 static const struct smb1cli_req_expected_response expected
[] = {
3922 .status
= NT_STATUS_OK
,
3923 .wct
= 0x11, /* NT1 */
3926 .status
= NT_STATUS_OK
,
3927 .wct
= 0x0D, /* LM */
3930 .status
= NT_STATUS_OK
,
3931 .wct
= 0x01, /* CORE */
3935 ZERO_STRUCT(server_challenge
);
3937 status
= smb1cli_req_recv(subreq
, state
,
3942 NULL
, /* pvwv_offset */
3945 NULL
, /* pbytes_offset */
3947 expected
, ARRAY_SIZE(expected
));
3948 TALLOC_FREE(subreq
);
3949 if (tevent_req_nterror(req
, status
)) {
3953 flags
= CVAL(inhdr
, HDR_FLG
);
3955 protnum
= SVAL(vwv
, 0);
3957 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
3958 if (smb1cli_prots
[i
].proto
< state
->min_protocol
) {
3962 if (smb1cli_prots
[i
].proto
> state
->max_protocol
) {
3966 if (protnum
!= num_prots
) {
3971 conn
->protocol
= smb1cli_prots
[i
].proto
;
3975 if (conn
->protocol
== PROTOCOL_NONE
) {
3976 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3980 if ((conn
->protocol
< PROTOCOL_NT1
) && conn
->mandatory_signing
) {
3981 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3982 "and the selected protocol level doesn't support it.\n"));
3983 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
3987 if (flags
& FLAG_SUPPORT_LOCKREAD
) {
3988 server_lockread
= true;
3989 server_writeunlock
= true;
3992 if (conn
->protocol
>= PROTOCOL_NT1
) {
3993 const char *client_signing
= NULL
;
3994 bool server_mandatory
= false;
3995 bool server_allowed
= false;
3996 const char *server_signing
= NULL
;
4001 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4006 server_security_mode
= CVAL(vwv
+ 1, 0);
4007 server_max_mux
= SVAL(vwv
+ 1, 1);
4008 server_max_xmit
= IVAL(vwv
+ 3, 1);
4009 server_session_key
= IVAL(vwv
+ 7, 1);
4010 server_time_zone
= SVALS(vwv
+ 15, 1);
4011 server_time_zone
*= 60;
4012 /* this time arrives in real GMT */
4013 server_system_time
= BVAL(vwv
+ 11, 1);
4014 server_capabilities
= IVAL(vwv
+ 9, 1);
4016 key_len
= CVAL(vwv
+ 16, 1);
4018 if (server_capabilities
& CAP_RAW_MODE
) {
4019 server_readbraw
= true;
4020 server_writebraw
= true;
4022 if (server_capabilities
& CAP_LOCK_AND_READ
) {
4023 server_lockread
= true;
4026 if (server_capabilities
& CAP_EXTENDED_SECURITY
) {
4027 DATA_BLOB blob1
, blob2
;
4029 if (num_bytes
< 16) {
4030 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4034 blob1
= data_blob_const(bytes
, 16);
4035 status
= GUID_from_data_blob(&blob1
, &server_guid
);
4036 if (tevent_req_nterror(req
, status
)) {
4040 blob1
= data_blob_const(bytes
+16, num_bytes
-16);
4041 blob2
= data_blob_dup_talloc(state
, blob1
);
4042 if (blob1
.length
> 0 &&
4043 tevent_req_nomem(blob2
.data
, req
)) {
4046 server_gss_blob
= blob2
;
4048 DATA_BLOB blob1
, blob2
;
4050 if (num_bytes
< key_len
) {
4051 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4055 if (key_len
!= 0 && key_len
!= 8) {
4056 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4061 memcpy(server_challenge
, bytes
, 8);
4064 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4065 blob2
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4066 if (blob1
.length
> 0) {
4069 len
= utf16_len_n(blob1
.data
,
4073 ok
= convert_string_talloc(state
,
4081 status
= map_nt_error_from_unix_common(errno
);
4082 tevent_req_nterror(req
, status
);
4087 blob2
.data
+= blob1
.length
;
4088 blob2
.length
-= blob1
.length
;
4089 if (blob2
.length
> 0) {
4092 len
= utf16_len_n(blob1
.data
,
4096 ok
= convert_string_talloc(state
,
4104 status
= map_nt_error_from_unix_common(errno
);
4105 tevent_req_nterror(req
, status
);
4111 client_signing
= "disabled";
4112 if (conn
->allow_signing
) {
4113 client_signing
= "allowed";
4115 if (conn
->mandatory_signing
) {
4116 client_signing
= "required";
4119 server_signing
= "not supported";
4120 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_ENABLED
) {
4121 server_signing
= "supported";
4122 server_allowed
= true;
4123 } else if (conn
->mandatory_signing
) {
4125 * We have mandatory signing as client
4126 * lets assume the server will look at our
4127 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4128 * flag in the session setup
4130 server_signing
= "not announced";
4131 server_allowed
= true;
4133 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_REQUIRED
) {
4134 server_signing
= "required";
4135 server_mandatory
= true;
4138 ok
= smb_signing_set_negotiated(conn
->smb1
.signing
,
4142 DEBUG(1,("cli_negprot: SMB signing is required, "
4143 "but client[%s] and server[%s] mismatch\n",
4144 client_signing
, server_signing
));
4145 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
4149 } else if (conn
->protocol
>= PROTOCOL_LANMAN1
) {
4155 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4159 server_security_mode
= SVAL(vwv
+ 1, 0);
4160 server_max_xmit
= SVAL(vwv
+ 2, 0);
4161 server_max_mux
= SVAL(vwv
+ 3, 0);
4162 server_readbraw
= ((SVAL(vwv
+ 5, 0) & 0x1) != 0);
4163 server_writebraw
= ((SVAL(vwv
+ 5, 0) & 0x2) != 0);
4164 server_session_key
= IVAL(vwv
+ 6, 0);
4165 server_time_zone
= SVALS(vwv
+ 10, 0);
4166 server_time_zone
*= 60;
4167 /* this time is converted to GMT by make_unix_date */
4168 t
= pull_dos_date((const uint8_t *)(vwv
+ 8), server_time_zone
);
4169 unix_to_nt_time(&server_system_time
, t
);
4170 key_len
= SVAL(vwv
+ 11, 0);
4172 if (num_bytes
< key_len
) {
4173 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4177 if (key_len
!= 0 && key_len
!= 8) {
4178 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4183 memcpy(server_challenge
, bytes
, 8);
4186 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4187 if (blob1
.length
> 0) {
4191 len
= utf16_len_n(blob1
.data
,
4195 ok
= convert_string_talloc(state
,
4203 status
= map_nt_error_from_unix_common(errno
);
4204 tevent_req_nterror(req
, status
);
4210 /* the old core protocol */
4211 server_time_zone
= get_time_zone(time(NULL
));
4212 server_max_xmit
= 1024;
4216 if (server_max_xmit
< 1024) {
4217 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4221 if (server_max_mux
< 1) {
4222 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4227 * Now calculate the negotiated capabilities
4228 * based on the mask for:
4229 * - client only flags
4230 * - flags used in both directions
4231 * - server only flags
4233 both_capabilities
= client_capabilities
& server_capabilities
;
4234 capabilities
= client_capabilities
& SMB_CAP_CLIENT_MASK
;
4235 capabilities
|= both_capabilities
& SMB_CAP_BOTH_MASK
;
4236 capabilities
|= server_capabilities
& SMB_CAP_SERVER_MASK
;
4238 max_xmit
= MIN(client_max_xmit
, server_max_xmit
);
4240 conn
->smb1
.server
.capabilities
= server_capabilities
;
4241 conn
->smb1
.capabilities
= capabilities
;
4243 conn
->smb1
.server
.max_xmit
= server_max_xmit
;
4244 conn
->smb1
.max_xmit
= max_xmit
;
4246 conn
->smb1
.server
.max_mux
= server_max_mux
;
4248 conn
->smb1
.server
.security_mode
= server_security_mode
;
4250 conn
->smb1
.server
.readbraw
= server_readbraw
;
4251 conn
->smb1
.server
.writebraw
= server_writebraw
;
4252 conn
->smb1
.server
.lockread
= server_lockread
;
4253 conn
->smb1
.server
.writeunlock
= server_writeunlock
;
4255 conn
->smb1
.server
.session_key
= server_session_key
;
4257 talloc_steal(conn
, server_gss_blob
.data
);
4258 conn
->smb1
.server
.gss_blob
= server_gss_blob
;
4259 conn
->smb1
.server
.guid
= server_guid
;
4260 memcpy(conn
->smb1
.server
.challenge
, server_challenge
, 8);
4261 conn
->smb1
.server
.workgroup
= talloc_move(conn
, &server_workgroup
);
4262 conn
->smb1
.server
.name
= talloc_move(conn
, &server_name
);
4264 conn
->smb1
.server
.time_zone
= server_time_zone
;
4265 conn
->smb1
.server
.system_time
= server_system_time
;
4267 tevent_req_done(req
);
4270 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
)
4274 uint16_t dialect_count
= 0;
4276 buf
= state
->smb2
.dyn
;
4277 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4278 if (smb2cli_prots
[i
].proto
< state
->min_protocol
) {
4282 if (smb2cli_prots
[i
].proto
> state
->max_protocol
) {
4286 SSVAL(buf
, dialect_count
*2, smb2cli_prots
[i
].smb2_dialect
);
4290 buf
= state
->smb2
.fixed
;
4292 SSVAL(buf
, 2, dialect_count
);
4293 SSVAL(buf
, 4, state
->conn
->smb2
.client
.security_mode
);
4294 SSVAL(buf
, 6, 0); /* Reserved */
4295 if (state
->max_protocol
>= PROTOCOL_SMB2_22
) {
4296 SIVAL(buf
, 8, state
->conn
->smb2
.client
.capabilities
);
4298 SIVAL(buf
, 8, 0); /* Capabilities */
4300 if (state
->max_protocol
>= PROTOCOL_SMB2_10
) {
4304 status
= GUID_to_ndr_blob(&state
->conn
->smb2
.client
.guid
,
4306 if (!NT_STATUS_IS_OK(status
)) {
4309 memcpy(buf
+12, blob
.data
, 16); /* ClientGuid */
4311 memset(buf
+12, 0, 16); /* ClientGuid */
4313 SBVAL(buf
, 28, 0); /* ClientStartTime */
4315 return smb2cli_req_send(state
, state
->ev
,
4316 state
->conn
, SMB2_OP_NEGPROT
,
4318 state
->timeout_msec
,
4319 NULL
, NULL
, /* tcon, session */
4320 state
->smb2
.fixed
, sizeof(state
->smb2
.fixed
),
4321 state
->smb2
.dyn
, dialect_count
*2,
4322 UINT16_MAX
); /* max_dyn_len */
4325 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
)
4327 struct tevent_req
*req
=
4328 tevent_req_callback_data(subreq
,
4330 struct smbXcli_negprot_state
*state
=
4331 tevent_req_data(req
,
4332 struct smbXcli_negprot_state
);
4333 struct smbXcli_conn
*conn
= state
->conn
;
4334 size_t security_offset
, security_length
;
4340 uint16_t dialect_revision
;
4341 static const struct smb2cli_req_expected_response expected
[] = {
4343 .status
= NT_STATUS_OK
,
4348 status
= smb2cli_req_recv(subreq
, state
, &iov
,
4349 expected
, ARRAY_SIZE(expected
));
4350 TALLOC_FREE(subreq
);
4351 if (tevent_req_nterror(req
, status
)) {
4355 body
= (uint8_t *)iov
[1].iov_base
;
4357 dialect_revision
= SVAL(body
, 4);
4359 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4360 if (smb2cli_prots
[i
].proto
< state
->min_protocol
) {
4364 if (smb2cli_prots
[i
].proto
> state
->max_protocol
) {
4368 if (smb2cli_prots
[i
].smb2_dialect
!= dialect_revision
) {
4372 conn
->protocol
= smb2cli_prots
[i
].proto
;
4376 if (conn
->protocol
== PROTOCOL_NONE
) {
4377 if (state
->min_protocol
>= PROTOCOL_SMB2_02
) {
4378 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4382 if (dialect_revision
!= SMB2_DIALECT_REVISION_2FF
) {
4383 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4387 /* make sure we do not loop forever */
4388 state
->min_protocol
= PROTOCOL_SMB2_02
;
4391 * send a SMB2 negprot, in order to negotiate
4394 subreq
= smbXcli_negprot_smb2_subreq(state
);
4395 if (tevent_req_nomem(subreq
, req
)) {
4398 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4402 conn
->smb2
.server
.security_mode
= SVAL(body
, 2);
4404 blob
= data_blob_const(body
+ 8, 16);
4405 status
= GUID_from_data_blob(&blob
, &conn
->smb2
.server
.guid
);
4406 if (tevent_req_nterror(req
, status
)) {
4410 conn
->smb2
.server
.capabilities
= IVAL(body
, 24);
4411 conn
->smb2
.server
.max_trans_size
= IVAL(body
, 28);
4412 conn
->smb2
.server
.max_read_size
= IVAL(body
, 32);
4413 conn
->smb2
.server
.max_write_size
= IVAL(body
, 36);
4414 conn
->smb2
.server
.system_time
= BVAL(body
, 40);
4415 conn
->smb2
.server
.start_time
= BVAL(body
, 48);
4417 security_offset
= SVAL(body
, 56);
4418 security_length
= SVAL(body
, 58);
4420 if (security_offset
!= SMB2_HDR_BODY
+ iov
[1].iov_len
) {
4421 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4425 if (security_length
> iov
[2].iov_len
) {
4426 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4430 conn
->smb2
.server
.gss_blob
= data_blob_talloc(conn
,
4433 if (tevent_req_nomem(conn
->smb2
.server
.gss_blob
.data
, req
)) {
4437 tevent_req_done(req
);
4440 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
4441 TALLOC_CTX
*tmp_mem
,
4444 size_t num_pending
= talloc_array_length(conn
->pending
);
4445 struct tevent_req
*subreq
;
4446 struct smbXcli_req_state
*substate
;
4447 struct tevent_req
*req
;
4448 uint32_t protocol_magic
;
4449 size_t inbuf_len
= smb_len_nbt(inbuf
);
4451 if (num_pending
!= 1) {
4452 return NT_STATUS_INTERNAL_ERROR
;
4455 if (inbuf_len
< 4) {
4456 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4459 subreq
= conn
->pending
[0];
4460 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
4461 req
= tevent_req_callback_data(subreq
, struct tevent_req
);
4463 protocol_magic
= IVAL(inbuf
, 4);
4465 switch (protocol_magic
) {
4467 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
4468 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
4469 return smb1cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
4472 if (substate
->smb2
.recv_iov
== NULL
) {
4474 * For the SMB1 negprot we have move it.
4476 substate
->smb2
.recv_iov
= substate
->smb1
.recv_iov
;
4477 substate
->smb1
.recv_iov
= NULL
;
4481 * we got an SMB2 answer, which consumed sequence number 0
4482 * so we need to use 1 as the next one.
4484 * we also need to set the current credits to 0
4485 * as we consumed the initial one. The SMB2 answer
4486 * hopefully grant us a new credit.
4489 conn
->smb2
.cur_credits
= 0;
4490 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4491 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
4492 return smb2cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
4495 DEBUG(10, ("Got non-SMB PDU\n"));
4496 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4499 NTSTATUS
smbXcli_negprot_recv(struct tevent_req
*req
)
4501 return tevent_req_simple_recv_ntstatus(req
);
4504 NTSTATUS
smbXcli_negprot(struct smbXcli_conn
*conn
,
4505 uint32_t timeout_msec
,
4506 enum protocol_types min_protocol
,
4507 enum protocol_types max_protocol
)
4509 TALLOC_CTX
*frame
= talloc_stackframe();
4510 struct tevent_context
*ev
;
4511 struct tevent_req
*req
;
4512 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4515 if (smbXcli_conn_has_async_calls(conn
)) {
4517 * Can't use sync call while an async call is in flight
4519 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
4522 ev
= samba_tevent_context_init(frame
);
4526 req
= smbXcli_negprot_send(frame
, ev
, conn
, timeout_msec
,
4527 min_protocol
, max_protocol
);
4531 ok
= tevent_req_poll(req
, ev
);
4533 status
= map_nt_error_from_unix_common(errno
);
4536 status
= smbXcli_negprot_recv(req
);
4542 static int smbXcli_session_destructor(struct smbXcli_session
*session
)
4544 if (session
->conn
== NULL
) {
4548 DLIST_REMOVE(session
->conn
->sessions
, session
);
4552 struct smbXcli_session
*smbXcli_session_create(TALLOC_CTX
*mem_ctx
,
4553 struct smbXcli_conn
*conn
)
4555 struct smbXcli_session
*session
;
4557 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
4558 if (session
== NULL
) {
4561 session
->smb2
= talloc_zero(session
, struct smb2cli_session
);
4562 if (session
->smb2
== NULL
) {
4563 talloc_free(session
);
4566 talloc_set_destructor(session
, smbXcli_session_destructor
);
4568 DLIST_ADD_END(conn
->sessions
, session
, struct smbXcli_session
*);
4569 session
->conn
= conn
;
4574 struct smbXcli_session
*smbXcli_session_copy(TALLOC_CTX
*mem_ctx
,
4575 struct smbXcli_session
*src
)
4577 struct smbXcli_session
*session
;
4579 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
4580 if (session
== NULL
) {
4583 session
->smb2
= talloc_zero(session
, struct smb2cli_session
);
4584 if (session
->smb2
== NULL
) {
4585 talloc_free(session
);
4589 session
->conn
= src
->conn
;
4590 *session
->smb2
= *src
->smb2
;
4591 session
->smb2_channel
= src
->smb2_channel
;
4592 session
->disconnect_expired
= src
->disconnect_expired
;
4594 DLIST_ADD_END(src
->conn
->sessions
, session
, struct smbXcli_session
*);
4595 talloc_set_destructor(session
, smbXcli_session_destructor
);
4601 NTSTATUS
smbXcli_session_application_key(struct smbXcli_session
*session
,
4602 TALLOC_CTX
*mem_ctx
,
4605 const DATA_BLOB
*application_key
;
4607 *key
= data_blob_null
;
4609 if (session
->conn
== NULL
) {
4610 return NT_STATUS_NO_USER_SESSION_KEY
;
4613 if (session
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
4614 application_key
= &session
->smb2
->application_key
;
4616 application_key
= &session
->smb1
.application_key
;
4619 if (application_key
->length
== 0) {
4620 return NT_STATUS_NO_USER_SESSION_KEY
;
4623 *key
= data_blob_dup_talloc(mem_ctx
, *application_key
);
4624 if (key
->data
== NULL
) {
4625 return NT_STATUS_NO_MEMORY
;
4628 return NT_STATUS_OK
;
4631 void smbXcli_session_set_disconnect_expired(struct smbXcli_session
*session
)
4633 session
->disconnect_expired
= true;
4636 uint16_t smb1cli_session_current_id(struct smbXcli_session
*session
)
4638 return session
->smb1
.session_id
;
4641 void smb1cli_session_set_id(struct smbXcli_session
*session
,
4642 uint16_t session_id
)
4644 session
->smb1
.session_id
= session_id
;
4647 NTSTATUS
smb1cli_session_set_session_key(struct smbXcli_session
*session
,
4648 const DATA_BLOB _session_key
)
4650 struct smbXcli_conn
*conn
= session
->conn
;
4651 uint8_t session_key
[16];
4654 return NT_STATUS_INVALID_PARAMETER_MIX
;
4657 if (session
->smb1
.application_key
.length
!= 0) {
4659 * TODO: do not allow this...
4661 * return NT_STATUS_INVALID_PARAMETER_MIX;
4663 data_blob_clear_free(&session
->smb1
.application_key
);
4664 session
->smb1
.protected_key
= false;
4667 if (_session_key
.length
== 0) {
4668 return NT_STATUS_OK
;
4671 ZERO_STRUCT(session_key
);
4672 memcpy(session_key
, _session_key
.data
,
4673 MIN(_session_key
.length
, sizeof(session_key
)));
4675 session
->smb1
.application_key
= data_blob_talloc(session
,
4677 sizeof(session_key
));
4678 ZERO_STRUCT(session_key
);
4679 if (session
->smb1
.application_key
.data
== NULL
) {
4680 return NT_STATUS_NO_MEMORY
;
4683 session
->smb1
.protected_key
= false;
4685 return NT_STATUS_OK
;
4688 NTSTATUS
smb1cli_session_protect_session_key(struct smbXcli_session
*session
)
4690 if (session
->smb1
.protected_key
) {
4691 /* already protected */
4692 return NT_STATUS_OK
;
4695 if (session
->smb1
.application_key
.length
!= 16) {
4696 return NT_STATUS_INVALID_PARAMETER_MIX
;
4699 smb_key_derivation(session
->smb1
.application_key
.data
,
4700 session
->smb1
.application_key
.length
,
4701 session
->smb1
.application_key
.data
);
4703 session
->smb1
.protected_key
= true;
4705 return NT_STATUS_OK
;
4708 uint8_t smb2cli_session_security_mode(struct smbXcli_session
*session
)
4710 struct smbXcli_conn
*conn
= session
->conn
;
4711 uint8_t security_mode
= 0;
4714 return security_mode
;
4717 security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
4718 if (conn
->mandatory_signing
) {
4719 security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
4722 return security_mode
;
4725 uint64_t smb2cli_session_current_id(struct smbXcli_session
*session
)
4727 return session
->smb2
->session_id
;
4730 uint16_t smb2cli_session_get_flags(struct smbXcli_session
*session
)
4732 return session
->smb2
->session_flags
;
4735 void smb2cli_session_set_id_and_flags(struct smbXcli_session
*session
,
4736 uint64_t session_id
,
4737 uint16_t session_flags
)
4739 session
->smb2
->session_id
= session_id
;
4740 session
->smb2
->session_flags
= session_flags
;
4743 void smb2cli_session_increment_channel_sequence(struct smbXcli_session
*session
)
4745 session
->smb2
->channel_sequence
+= 1;
4748 NTSTATUS
smb2cli_session_set_session_key(struct smbXcli_session
*session
,
4749 const DATA_BLOB _session_key
,
4750 const struct iovec
*recv_iov
)
4752 struct smbXcli_conn
*conn
= session
->conn
;
4753 uint16_t no_sign_flags
;
4754 uint8_t session_key
[16];
4755 bool check_signature
= true;
4760 return NT_STATUS_INVALID_PARAMETER_MIX
;
4763 if (recv_iov
[0].iov_len
!= SMB2_HDR_BODY
) {
4764 return NT_STATUS_INVALID_PARAMETER_MIX
;
4767 no_sign_flags
= SMB2_SESSION_FLAG_IS_GUEST
| SMB2_SESSION_FLAG_IS_NULL
;
4769 if (session
->smb2
->session_flags
& no_sign_flags
) {
4770 session
->smb2
->should_sign
= false;
4771 return NT_STATUS_OK
;
4774 if (session
->smb2
->signing_key
.length
!= 0) {
4775 return NT_STATUS_INVALID_PARAMETER_MIX
;
4778 ZERO_STRUCT(session_key
);
4779 memcpy(session_key
, _session_key
.data
,
4780 MIN(_session_key
.length
, sizeof(session_key
)));
4782 session
->smb2
->signing_key
= data_blob_talloc(session
,
4784 sizeof(session_key
));
4785 if (session
->smb2
->signing_key
.data
== NULL
) {
4786 ZERO_STRUCT(session_key
);
4787 return NT_STATUS_NO_MEMORY
;
4790 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4791 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCMAC");
4792 const DATA_BLOB context
= data_blob_string_const_null("SmbSign");
4794 smb2_key_derivation(session_key
, sizeof(session_key
),
4795 label
.data
, label
.length
,
4796 context
.data
, context
.length
,
4797 session
->smb2
->signing_key
.data
);
4800 session
->smb2
->encryption_key
= data_blob_dup_talloc(session
,
4801 session
->smb2
->signing_key
);
4802 if (session
->smb2
->encryption_key
.data
== NULL
) {
4803 ZERO_STRUCT(session_key
);
4804 return NT_STATUS_NO_MEMORY
;
4807 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4808 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCCM");
4809 const DATA_BLOB context
= data_blob_string_const_null("ServerIn ");
4811 smb2_key_derivation(session_key
, sizeof(session_key
),
4812 label
.data
, label
.length
,
4813 context
.data
, context
.length
,
4814 session
->smb2
->encryption_key
.data
);
4817 session
->smb2
->decryption_key
= data_blob_dup_talloc(session
,
4818 session
->smb2
->signing_key
);
4819 if (session
->smb2
->decryption_key
.data
== NULL
) {
4820 ZERO_STRUCT(session_key
);
4821 return NT_STATUS_NO_MEMORY
;
4824 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4825 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCCM");
4826 const DATA_BLOB context
= data_blob_string_const_null("ServerOut");
4828 smb2_key_derivation(session_key
, sizeof(session_key
),
4829 label
.data
, label
.length
,
4830 context
.data
, context
.length
,
4831 session
->smb2
->decryption_key
.data
);
4834 session
->smb2
->application_key
= data_blob_dup_talloc(session
,
4835 session
->smb2
->signing_key
);
4836 if (session
->smb2
->application_key
.data
== NULL
) {
4837 ZERO_STRUCT(session_key
);
4838 return NT_STATUS_NO_MEMORY
;
4841 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4842 const DATA_BLOB label
= data_blob_string_const_null("SMB2APP");
4843 const DATA_BLOB context
= data_blob_string_const_null("SmbRpc");
4845 smb2_key_derivation(session_key
, sizeof(session_key
),
4846 label
.data
, label
.length
,
4847 context
.data
, context
.length
,
4848 session
->smb2
->application_key
.data
);
4850 ZERO_STRUCT(session_key
);
4852 session
->smb2_channel
.signing_key
= data_blob_dup_talloc(session
,
4853 session
->smb2
->signing_key
);
4854 if (session
->smb2_channel
.signing_key
.data
== NULL
) {
4855 return NT_STATUS_NO_MEMORY
;
4858 check_signature
= conn
->mandatory_signing
;
4860 hdr_flags
= IVAL(recv_iov
[0].iov_base
, SMB2_HDR_FLAGS
);
4861 if (hdr_flags
& SMB2_HDR_FLAG_SIGNED
) {
4863 * Sadly some vendors don't sign the
4864 * final SMB2 session setup response
4866 * At least Windows and Samba are always doing this
4867 * if there's a session key available.
4869 * We only check the signature if it's mandatory
4870 * or SMB2_HDR_FLAG_SIGNED is provided.
4872 check_signature
= true;
4875 if (check_signature
) {
4876 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
4877 session
->conn
->protocol
,
4879 if (!NT_STATUS_IS_OK(status
)) {
4884 session
->smb2
->should_sign
= false;
4885 session
->smb2
->should_encrypt
= false;
4887 if (conn
->desire_signing
) {
4888 session
->smb2
->should_sign
= true;
4891 if (conn
->smb2
.server
.security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) {
4892 session
->smb2
->should_sign
= true;
4895 if (session
->smb2
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
) {
4896 session
->smb2
->should_encrypt
= true;
4899 if (conn
->protocol
< PROTOCOL_SMB2_24
) {
4900 session
->smb2
->should_encrypt
= false;
4903 if (!(conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
4904 session
->smb2
->should_encrypt
= false;
4907 generate_random_buffer((uint8_t *)&session
->smb2
->nonce_high
,
4908 sizeof(session
->smb2
->nonce_high
));
4909 session
->smb2
->nonce_low
= 1;
4911 return NT_STATUS_OK
;
4914 NTSTATUS
smb2cli_session_create_channel(TALLOC_CTX
*mem_ctx
,
4915 struct smbXcli_session
*session1
,
4916 struct smbXcli_conn
*conn
,
4917 struct smbXcli_session
**_session2
)
4919 struct smbXcli_session
*session2
;
4921 if (session1
->smb2
->signing_key
.length
== 0) {
4922 return NT_STATUS_INVALID_PARAMETER_MIX
;
4926 return NT_STATUS_INVALID_PARAMETER_MIX
;
4929 session2
= talloc_zero(mem_ctx
, struct smbXcli_session
);
4930 if (session2
== NULL
) {
4931 return NT_STATUS_NO_MEMORY
;
4933 session2
->smb2
= talloc_reference(session2
, session1
->smb2
);
4934 if (session2
->smb2
== NULL
) {
4935 talloc_free(session2
);
4936 return NT_STATUS_NO_MEMORY
;
4939 talloc_set_destructor(session2
, smbXcli_session_destructor
);
4940 DLIST_ADD_END(conn
->sessions
, session2
, struct smbXcli_session
*);
4941 session2
->conn
= conn
;
4943 *_session2
= session2
;
4944 return NT_STATUS_OK
;
4947 NTSTATUS
smb2cli_session_set_channel_key(struct smbXcli_session
*session
,
4948 const DATA_BLOB _channel_key
,
4949 const struct iovec
*recv_iov
)
4951 struct smbXcli_conn
*conn
= session
->conn
;
4952 uint8_t channel_key
[16];
4956 return NT_STATUS_INVALID_PARAMETER_MIX
;
4959 if (session
->smb2_channel
.signing_key
.length
!= 0) {
4960 return NT_STATUS_INVALID_PARAMETER_MIX
;
4963 ZERO_STRUCT(channel_key
);
4964 memcpy(channel_key
, _channel_key
.data
,
4965 MIN(_channel_key
.length
, sizeof(channel_key
)));
4967 session
->smb2_channel
.signing_key
= data_blob_talloc(session
,
4969 sizeof(channel_key
));
4970 if (session
->smb2_channel
.signing_key
.data
== NULL
) {
4971 ZERO_STRUCT(channel_key
);
4972 return NT_STATUS_NO_MEMORY
;
4975 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4976 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCMAC");
4977 const DATA_BLOB context
= data_blob_string_const_null("SmbSign");
4979 smb2_key_derivation(channel_key
, sizeof(channel_key
),
4980 label
.data
, label
.length
,
4981 context
.data
, context
.length
,
4982 session
->smb2_channel
.signing_key
.data
);
4984 ZERO_STRUCT(channel_key
);
4986 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
4987 session
->conn
->protocol
,
4989 if (!NT_STATUS_IS_OK(status
)) {
4993 return NT_STATUS_OK
;
4996 NTSTATUS
smb2cli_session_encryption_on(struct smbXcli_session
*session
)
4998 if (session
->smb2
->should_encrypt
) {
4999 return NT_STATUS_OK
;
5002 if (session
->conn
->protocol
< PROTOCOL_SMB2_24
) {
5003 return NT_STATUS_NOT_SUPPORTED
;
5006 if (!(session
->conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
5007 return NT_STATUS_NOT_SUPPORTED
;
5010 if (session
->smb2
->signing_key
.data
== NULL
) {
5011 return NT_STATUS_NOT_SUPPORTED
;
5013 session
->smb2
->should_encrypt
= true;
5014 return NT_STATUS_OK
;
5017 struct smbXcli_tcon
*smbXcli_tcon_create(TALLOC_CTX
*mem_ctx
)
5019 struct smbXcli_tcon
*tcon
;
5021 tcon
= talloc_zero(mem_ctx
, struct smbXcli_tcon
);
5029 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon
*tcon
,
5030 uint32_t fs_attributes
)
5032 tcon
->fs_attributes
= fs_attributes
;
5035 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon
*tcon
)
5037 return tcon
->fs_attributes
;
5040 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon
*tcon
)
5046 if (tcon
->is_smb1
) {
5047 if (tcon
->smb1
.optional_support
& SMB_SHARE_IN_DFS
) {
5054 if (tcon
->smb2
.capabilities
& SMB2_SHARE_CAP_DFS
) {
5061 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
5063 return tcon
->smb1
.tcon_id
;
5066 void smb1cli_tcon_set_id(struct smbXcli_tcon
*tcon
, uint16_t tcon_id
)
5068 tcon
->is_smb1
= true;
5069 tcon
->smb1
.tcon_id
= tcon_id
;
5072 bool smb1cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
5074 uint16_t optional_support
,
5075 uint32_t maximal_access
,
5076 uint32_t guest_maximal_access
,
5077 const char *service
,
5078 const char *fs_type
)
5080 tcon
->is_smb1
= true;
5081 tcon
->fs_attributes
= 0;
5082 tcon
->smb1
.tcon_id
= tcon_id
;
5083 tcon
->smb1
.optional_support
= optional_support
;
5084 tcon
->smb1
.maximal_access
= maximal_access
;
5085 tcon
->smb1
.guest_maximal_access
= guest_maximal_access
;
5087 TALLOC_FREE(tcon
->smb1
.service
);
5088 tcon
->smb1
.service
= talloc_strdup(tcon
, service
);
5089 if (service
!= NULL
&& tcon
->smb1
.service
== NULL
) {
5093 TALLOC_FREE(tcon
->smb1
.fs_type
);
5094 tcon
->smb1
.fs_type
= talloc_strdup(tcon
, fs_type
);
5095 if (fs_type
!= NULL
&& tcon
->smb1
.fs_type
== NULL
) {
5102 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
5104 return tcon
->smb2
.tcon_id
;
5107 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon
*tcon
)
5109 return tcon
->smb2
.capabilities
;
5112 void smb2cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
5113 struct smbXcli_session
*session
,
5117 uint32_t capabilities
,
5118 uint32_t maximal_access
)
5120 tcon
->is_smb1
= false;
5121 tcon
->fs_attributes
= 0;
5122 tcon
->smb2
.tcon_id
= tcon_id
;
5123 tcon
->smb2
.type
= type
;
5124 tcon
->smb2
.flags
= flags
;
5125 tcon
->smb2
.capabilities
= capabilities
;
5126 tcon
->smb2
.maximal_access
= maximal_access
;
5128 tcon
->smb2
.should_encrypt
= false;
5130 if (session
== NULL
) {
5134 tcon
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
5136 if (flags
& SMB2_SHAREFLAG_ENCRYPT_DATA
) {
5137 tcon
->smb2
.should_encrypt
= true;