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
{
172 uint16_t optional_support
;
173 uint32_t maximal_access
;
174 uint32_t guest_maximal_access
;
183 uint32_t capabilities
;
184 uint32_t maximal_access
;
189 struct smbXcli_req_state
{
190 struct tevent_context
*ev
;
191 struct smbXcli_conn
*conn
;
192 struct smbXcli_session
*session
; /* maybe NULL */
193 struct smbXcli_tcon
*tcon
; /* maybe NULL */
195 uint8_t length_hdr
[4];
202 /* Space for the header including the wct */
203 uint8_t hdr
[HDR_VWV
];
206 * For normal requests, smb1cli_req_send chooses a mid.
207 * SecondaryV trans requests need to use the mid of the primary
208 * request, so we need a place to store it.
209 * Assume it is set if != 0.
214 uint8_t bytecount_buf
[2];
216 #define MAX_SMB_IOV 10
217 /* length_hdr, hdr, words, byte_count, buffers */
218 struct iovec iov
[1 + 3 + MAX_SMB_IOV
];
223 struct tevent_req
**chained_requests
;
226 NTSTATUS recv_status
;
227 /* always an array of 3 talloc elements */
228 struct iovec
*recv_iov
;
232 const uint8_t *fixed
;
237 uint8_t transform
[SMB2_TF_HDR_SIZE
];
238 uint8_t hdr
[SMB2_HDR_BODY
];
239 uint8_t pad
[7]; /* padding space for compounding */
242 * always an array of 3 talloc elements
243 * (without a SMB2_TRANSFORM header!)
247 struct iovec
*recv_iov
;
250 * the expected max for the response dyn_len
252 uint32_t max_dyn_len
;
254 uint16_t credit_charge
;
258 uint64_t encryption_session_id
;
260 bool signing_skipped
;
266 static int smbXcli_conn_destructor(struct smbXcli_conn
*conn
)
269 * NT_STATUS_OK, means we do not notify the callers
271 smbXcli_conn_disconnect(conn
, NT_STATUS_OK
);
273 while (conn
->sessions
) {
274 conn
->sessions
->conn
= NULL
;
275 DLIST_REMOVE(conn
->sessions
, conn
->sessions
);
278 if (conn
->smb1
.trans_enc
) {
279 TALLOC_FREE(conn
->smb1
.trans_enc
);
285 struct smbXcli_conn
*smbXcli_conn_create(TALLOC_CTX
*mem_ctx
,
287 const char *remote_name
,
288 enum smb_signing_setting signing_state
,
289 uint32_t smb1_capabilities
,
290 struct GUID
*client_guid
,
291 uint32_t smb2_capabilities
)
293 struct smbXcli_conn
*conn
= NULL
;
295 struct sockaddr
*sa
= NULL
;
299 conn
= talloc_zero(mem_ctx
, struct smbXcli_conn
);
305 conn
->write_fd
= dup(fd
);
306 if (conn
->write_fd
== -1) {
310 conn
->remote_name
= talloc_strdup(conn
, remote_name
);
311 if (conn
->remote_name
== NULL
) {
316 ss
= (void *)&conn
->local_ss
;
317 sa
= (struct sockaddr
*)ss
;
318 sa_length
= sizeof(conn
->local_ss
);
319 ret
= getsockname(fd
, sa
, &sa_length
);
323 ss
= (void *)&conn
->remote_ss
;
324 sa
= (struct sockaddr
*)ss
;
325 sa_length
= sizeof(conn
->remote_ss
);
326 ret
= getpeername(fd
, sa
, &sa_length
);
331 conn
->outgoing
= tevent_queue_create(conn
, "smbXcli_outgoing");
332 if (conn
->outgoing
== NULL
) {
335 conn
->pending
= NULL
;
337 conn
->protocol
= PROTOCOL_NONE
;
339 switch (signing_state
) {
340 case SMB_SIGNING_OFF
:
342 conn
->allow_signing
= false;
343 conn
->desire_signing
= false;
344 conn
->mandatory_signing
= false;
346 case SMB_SIGNING_DEFAULT
:
347 case SMB_SIGNING_IF_REQUIRED
:
348 /* if the server requires it */
349 conn
->allow_signing
= true;
350 conn
->desire_signing
= false;
351 conn
->mandatory_signing
= false;
353 case SMB_SIGNING_REQUIRED
:
355 conn
->allow_signing
= true;
356 conn
->desire_signing
= true;
357 conn
->mandatory_signing
= true;
361 conn
->smb1
.client
.capabilities
= smb1_capabilities
;
362 conn
->smb1
.client
.max_xmit
= UINT16_MAX
;
364 conn
->smb1
.capabilities
= conn
->smb1
.client
.capabilities
;
365 conn
->smb1
.max_xmit
= 1024;
369 /* initialise signing */
370 conn
->smb1
.signing
= smb_signing_init(conn
,
372 conn
->desire_signing
,
373 conn
->mandatory_signing
);
374 if (!conn
->smb1
.signing
) {
378 conn
->smb2
.client
.security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
379 if (conn
->mandatory_signing
) {
380 conn
->smb2
.client
.security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
383 conn
->smb2
.client
.guid
= *client_guid
;
385 conn
->smb2
.client
.capabilities
= smb2_capabilities
;
387 conn
->smb2
.cur_credits
= 1;
388 conn
->smb2
.max_credits
= 0;
390 talloc_set_destructor(conn
, smbXcli_conn_destructor
);
394 if (conn
->write_fd
!= -1) {
395 close(conn
->write_fd
);
401 bool smbXcli_conn_is_connected(struct smbXcli_conn
*conn
)
407 if (conn
->read_fd
== -1) {
414 enum protocol_types
smbXcli_conn_protocol(struct smbXcli_conn
*conn
)
416 return conn
->protocol
;
419 bool smbXcli_conn_use_unicode(struct smbXcli_conn
*conn
)
421 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
425 if (conn
->smb1
.capabilities
& CAP_UNICODE
) {
432 void smbXcli_conn_set_sockopt(struct smbXcli_conn
*conn
, const char *options
)
434 set_socket_options(conn
->read_fd
, options
);
437 const struct sockaddr_storage
*smbXcli_conn_local_sockaddr(struct smbXcli_conn
*conn
)
439 return &conn
->local_ss
;
442 const struct sockaddr_storage
*smbXcli_conn_remote_sockaddr(struct smbXcli_conn
*conn
)
444 return &conn
->remote_ss
;
447 const char *smbXcli_conn_remote_name(struct smbXcli_conn
*conn
)
449 return conn
->remote_name
;
452 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn
*conn
)
454 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
461 return conn
->smb1
.server
.max_mux
;
464 NTTIME
smbXcli_conn_server_system_time(struct smbXcli_conn
*conn
)
466 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
467 return conn
->smb2
.server
.system_time
;
470 return conn
->smb1
.server
.system_time
;
473 const DATA_BLOB
*smbXcli_conn_server_gss_blob(struct smbXcli_conn
*conn
)
475 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
476 return &conn
->smb2
.server
.gss_blob
;
479 return &conn
->smb1
.server
.gss_blob
;
482 const struct GUID
*smbXcli_conn_server_guid(struct smbXcli_conn
*conn
)
484 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
485 return &conn
->smb2
.server
.guid
;
488 return &conn
->smb1
.server
.guid
;
491 struct smbXcli_conn_samba_suicide_state
{
492 struct smbXcli_conn
*conn
;
497 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
);
499 struct tevent_req
*smbXcli_conn_samba_suicide_send(TALLOC_CTX
*mem_ctx
,
500 struct tevent_context
*ev
,
501 struct smbXcli_conn
*conn
,
504 struct tevent_req
*req
, *subreq
;
505 struct smbXcli_conn_samba_suicide_state
*state
;
507 req
= tevent_req_create(mem_ctx
, &state
,
508 struct smbXcli_conn_samba_suicide_state
);
513 SIVAL(state
->buf
, 4, 0x74697865);
514 SCVAL(state
->buf
, 8, exitcode
);
515 _smb_setlen_nbt(state
->buf
, sizeof(state
->buf
)-4);
517 state
->iov
.iov_base
= state
->buf
;
518 state
->iov
.iov_len
= sizeof(state
->buf
);
520 subreq
= writev_send(state
, ev
, conn
->outgoing
, conn
->write_fd
,
521 false, &state
->iov
, 1);
522 if (tevent_req_nomem(subreq
, req
)) {
523 return tevent_req_post(req
, ev
);
525 tevent_req_set_callback(subreq
, smbXcli_conn_samba_suicide_done
, req
);
529 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
)
531 struct tevent_req
*req
= tevent_req_callback_data(
532 subreq
, struct tevent_req
);
533 struct smbXcli_conn_samba_suicide_state
*state
= tevent_req_data(
534 req
, struct smbXcli_conn_samba_suicide_state
);
538 nwritten
= writev_recv(subreq
, &err
);
540 if (nwritten
== -1) {
541 NTSTATUS status
= map_nt_error_from_unix_common(err
);
542 smbXcli_conn_disconnect(state
->conn
, status
);
545 tevent_req_done(req
);
548 NTSTATUS
smbXcli_conn_samba_suicide_recv(struct tevent_req
*req
)
550 return tevent_req_simple_recv_ntstatus(req
);
553 NTSTATUS
smbXcli_conn_samba_suicide(struct smbXcli_conn
*conn
,
556 TALLOC_CTX
*frame
= talloc_stackframe();
557 struct tevent_context
*ev
;
558 struct tevent_req
*req
;
559 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
562 if (smbXcli_conn_has_async_calls(conn
)) {
564 * Can't use sync call while an async call is in flight
566 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
569 ev
= samba_tevent_context_init(frame
);
573 req
= smbXcli_conn_samba_suicide_send(frame
, ev
, conn
, exitcode
);
577 ok
= tevent_req_poll(req
, ev
);
579 status
= map_nt_error_from_unix_common(errno
);
582 status
= smbXcli_conn_samba_suicide_recv(req
);
588 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn
*conn
)
590 return conn
->smb1
.capabilities
;
593 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn
*conn
)
595 return conn
->smb1
.max_xmit
;
598 bool smb1cli_conn_req_possible(struct smbXcli_conn
*conn
)
601 uint16_t possible
= conn
->smb1
.server
.max_mux
;
603 pending
= tevent_queue_length(conn
->outgoing
);
604 if (pending
>= possible
) {
607 pending
+= talloc_array_length(conn
->pending
);
608 if (pending
>= possible
) {
615 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn
*conn
)
617 return conn
->smb1
.server
.session_key
;
620 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn
*conn
)
622 return conn
->smb1
.server
.challenge
;
625 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
627 return conn
->smb1
.server
.security_mode
;
630 bool smb1cli_conn_server_readbraw(struct smbXcli_conn
*conn
)
632 return conn
->smb1
.server
.readbraw
;
635 bool smb1cli_conn_server_writebraw(struct smbXcli_conn
*conn
)
637 return conn
->smb1
.server
.writebraw
;
640 bool smb1cli_conn_server_lockread(struct smbXcli_conn
*conn
)
642 return conn
->smb1
.server
.lockread
;
645 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn
*conn
)
647 return conn
->smb1
.server
.writeunlock
;
650 int smb1cli_conn_server_time_zone(struct smbXcli_conn
*conn
)
652 return conn
->smb1
.server
.time_zone
;
655 bool smb1cli_conn_activate_signing(struct smbXcli_conn
*conn
,
656 const DATA_BLOB user_session_key
,
657 const DATA_BLOB response
)
659 return smb_signing_activate(conn
->smb1
.signing
,
664 bool smb1cli_conn_check_signing(struct smbXcli_conn
*conn
,
665 const uint8_t *buf
, uint32_t seqnum
)
667 const uint8_t *hdr
= buf
+ NBT_HDR_SIZE
;
668 size_t len
= smb_len_nbt(buf
);
670 return smb_signing_check_pdu(conn
->smb1
.signing
, hdr
, len
, seqnum
);
673 bool smb1cli_conn_signing_is_active(struct smbXcli_conn
*conn
)
675 return smb_signing_is_active(conn
->smb1
.signing
);
678 void smb1cli_conn_set_encryption(struct smbXcli_conn
*conn
,
679 struct smb_trans_enc_state
*es
)
681 /* Replace the old state, if any. */
682 if (conn
->smb1
.trans_enc
) {
683 TALLOC_FREE(conn
->smb1
.trans_enc
);
685 conn
->smb1
.trans_enc
= es
;
688 bool smb1cli_conn_encryption_on(struct smbXcli_conn
*conn
)
690 return common_encryption_on(conn
->smb1
.trans_enc
);
694 static NTSTATUS
smb1cli_pull_raw_error(const uint8_t *hdr
)
696 uint32_t flags2
= SVAL(hdr
, HDR_FLG2
);
697 NTSTATUS status
= NT_STATUS(IVAL(hdr
, HDR_RCLS
));
699 if (NT_STATUS_IS_OK(status
)) {
703 if (flags2
& FLAGS2_32_BIT_ERROR_CODES
) {
707 return NT_STATUS_DOS(CVAL(hdr
, HDR_RCLS
), SVAL(hdr
, HDR_ERR
));
711 * Is the SMB command able to hold an AND_X successor
712 * @param[in] cmd The SMB command in question
713 * @retval Can we add a chained request after "cmd"?
715 bool smb1cli_is_andx_req(uint8_t cmd
)
735 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn
*conn
)
737 size_t num_pending
= talloc_array_length(conn
->pending
);
740 if (conn
->protocol
== PROTOCOL_NONE
) {
742 * This is what windows sends on the SMB1 Negprot request
743 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
751 result
= conn
->smb1
.mid
++;
752 if ((result
== 0) || (result
== 0xffff)) {
756 for (i
=0; i
<num_pending
; i
++) {
757 if (result
== smb1cli_req_mid(conn
->pending
[i
])) {
762 if (i
== num_pending
) {
768 void smbXcli_req_unset_pending(struct tevent_req
*req
)
770 struct smbXcli_req_state
*state
=
772 struct smbXcli_req_state
);
773 struct smbXcli_conn
*conn
= state
->conn
;
774 size_t num_pending
= talloc_array_length(conn
->pending
);
777 if (state
->smb1
.mid
!= 0) {
779 * This is a [nt]trans[2] request which waits
780 * for more than one reply.
785 talloc_set_destructor(req
, NULL
);
787 if (num_pending
== 1) {
789 * The pending read_smb tevent_req is a child of
790 * conn->pending. So if nothing is pending anymore, we need to
791 * delete the socket read fde.
793 TALLOC_FREE(conn
->pending
);
794 conn
->read_smb_req
= NULL
;
798 for (i
=0; i
<num_pending
; i
++) {
799 if (req
== conn
->pending
[i
]) {
803 if (i
== num_pending
) {
805 * Something's seriously broken. Just returning here is the
806 * right thing nevertheless, the point of this routine is to
807 * remove ourselves from conn->pending.
813 * Remove ourselves from the conn->pending array
815 for (; i
< (num_pending
- 1); i
++) {
816 conn
->pending
[i
] = conn
->pending
[i
+1];
820 * No NULL check here, we're shrinking by sizeof(void *), and
821 * talloc_realloc just adjusts the size for this.
823 conn
->pending
= talloc_realloc(NULL
, conn
->pending
, struct tevent_req
*,
828 static int smbXcli_req_destructor(struct tevent_req
*req
)
830 struct smbXcli_req_state
*state
=
832 struct smbXcli_req_state
);
835 * Make sure we really remove it from
836 * the pending array on destruction.
839 smbXcli_req_unset_pending(req
);
843 static bool smb1cli_req_cancel(struct tevent_req
*req
);
844 static bool smb2cli_req_cancel(struct tevent_req
*req
);
846 static bool smbXcli_req_cancel(struct tevent_req
*req
)
848 struct smbXcli_req_state
*state
=
850 struct smbXcli_req_state
);
852 if (!smbXcli_conn_is_connected(state
->conn
)) {
856 if (state
->conn
->protocol
== PROTOCOL_NONE
) {
860 if (state
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
861 return smb2cli_req_cancel(req
);
864 return smb1cli_req_cancel(req
);
867 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
);
869 bool smbXcli_req_set_pending(struct tevent_req
*req
)
871 struct smbXcli_req_state
*state
=
873 struct smbXcli_req_state
);
874 struct smbXcli_conn
*conn
;
875 struct tevent_req
**pending
;
880 if (!smbXcli_conn_is_connected(conn
)) {
884 num_pending
= talloc_array_length(conn
->pending
);
886 pending
= talloc_realloc(conn
, conn
->pending
, struct tevent_req
*,
888 if (pending
== NULL
) {
891 pending
[num_pending
] = req
;
892 conn
->pending
= pending
;
893 talloc_set_destructor(req
, smbXcli_req_destructor
);
894 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
896 if (!smbXcli_conn_receive_next(conn
)) {
898 * the caller should notify the current request
900 * And all other pending requests get notified
901 * by smbXcli_conn_disconnect().
903 smbXcli_req_unset_pending(req
);
904 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
911 static void smbXcli_conn_received(struct tevent_req
*subreq
);
913 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
)
915 size_t num_pending
= talloc_array_length(conn
->pending
);
916 struct tevent_req
*req
;
917 struct smbXcli_req_state
*state
;
919 if (conn
->read_smb_req
!= NULL
) {
923 if (num_pending
== 0) {
924 if (conn
->smb2
.mid
< UINT64_MAX
) {
925 /* no more pending requests, so we are done for now */
930 * If there are no more SMB2 requests possible,
931 * because we are out of message ids,
932 * we need to disconnect.
934 smbXcli_conn_disconnect(conn
, NT_STATUS_CONNECTION_ABORTED
);
938 req
= conn
->pending
[0];
939 state
= tevent_req_data(req
, struct smbXcli_req_state
);
942 * We're the first ones, add the read_smb request that waits for the
943 * answer from the server
945 conn
->read_smb_req
= read_smb_send(conn
->pending
,
948 if (conn
->read_smb_req
== NULL
) {
951 tevent_req_set_callback(conn
->read_smb_req
, smbXcli_conn_received
, conn
);
955 void smbXcli_conn_disconnect(struct smbXcli_conn
*conn
, NTSTATUS status
)
957 struct smbXcli_session
*session
;
959 tevent_queue_stop(conn
->outgoing
);
961 if (conn
->read_fd
!= -1) {
962 close(conn
->read_fd
);
964 if (conn
->write_fd
!= -1) {
965 close(conn
->write_fd
);
970 session
= conn
->sessions
;
971 if (talloc_array_length(conn
->pending
) == 0) {
973 * if we do not have pending requests
974 * there is no need to update the channel_sequence
978 for (; session
; session
= session
->next
) {
979 smb2cli_session_increment_channel_sequence(session
);
983 * Cancel all pending requests. We do not do a for-loop walking
984 * conn->pending because that array changes in
985 * smbXcli_req_unset_pending.
987 while (talloc_array_length(conn
->pending
) > 0) {
988 struct tevent_req
*req
;
989 struct smbXcli_req_state
*state
;
990 struct tevent_req
**chain
;
994 req
= conn
->pending
[0];
995 state
= tevent_req_data(req
, struct smbXcli_req_state
);
997 if (state
->smb1
.chained_requests
== NULL
) {
999 * We're dead. No point waiting for trans2
1002 state
->smb1
.mid
= 0;
1004 smbXcli_req_unset_pending(req
);
1006 if (NT_STATUS_IS_OK(status
)) {
1007 /* do not notify the callers */
1012 * we need to defer the callback, because we may notify
1013 * more then one caller.
1015 tevent_req_defer_callback(req
, state
->ev
);
1016 tevent_req_nterror(req
, status
);
1020 chain
= talloc_move(conn
, &state
->smb1
.chained_requests
);
1021 num_chained
= talloc_array_length(chain
);
1023 for (i
=0; i
<num_chained
; i
++) {
1025 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1028 * We're dead. No point waiting for trans2
1031 state
->smb1
.mid
= 0;
1033 smbXcli_req_unset_pending(req
);
1035 if (NT_STATUS_IS_OK(status
)) {
1036 /* do not notify the callers */
1041 * we need to defer the callback, because we may notify
1042 * more than one caller.
1044 tevent_req_defer_callback(req
, state
->ev
);
1045 tevent_req_nterror(req
, status
);
1052 * Fetch a smb request's mid. Only valid after the request has been sent by
1053 * smb1cli_req_send().
1055 uint16_t smb1cli_req_mid(struct tevent_req
*req
)
1057 struct smbXcli_req_state
*state
=
1058 tevent_req_data(req
,
1059 struct smbXcli_req_state
);
1061 if (state
->smb1
.mid
!= 0) {
1062 return state
->smb1
.mid
;
1065 return SVAL(state
->smb1
.hdr
, HDR_MID
);
1068 void smb1cli_req_set_mid(struct tevent_req
*req
, uint16_t mid
)
1070 struct smbXcli_req_state
*state
=
1071 tevent_req_data(req
,
1072 struct smbXcli_req_state
);
1074 state
->smb1
.mid
= mid
;
1077 uint32_t smb1cli_req_seqnum(struct tevent_req
*req
)
1079 struct smbXcli_req_state
*state
=
1080 tevent_req_data(req
,
1081 struct smbXcli_req_state
);
1083 return state
->smb1
.seqnum
;
1086 void smb1cli_req_set_seqnum(struct tevent_req
*req
, uint32_t seqnum
)
1088 struct smbXcli_req_state
*state
=
1089 tevent_req_data(req
,
1090 struct smbXcli_req_state
);
1092 state
->smb1
.seqnum
= seqnum
;
1095 static size_t smbXcli_iov_len(const struct iovec
*iov
, int count
)
1099 for (i
=0; i
<count
; i
++) {
1100 result
+= iov
[i
].iov_len
;
1105 static uint8_t *smbXcli_iov_concat(TALLOC_CTX
*mem_ctx
,
1106 const struct iovec
*iov
,
1109 size_t len
= smbXcli_iov_len(iov
, count
);
1114 buf
= talloc_array(mem_ctx
, uint8_t, len
);
1119 for (i
=0; i
<count
; i
++) {
1120 memcpy(buf
+copied
, iov
[i
].iov_base
, iov
[i
].iov_len
);
1121 copied
+= iov
[i
].iov_len
;
1126 static void smb1cli_req_flags(enum protocol_types protocol
,
1127 uint32_t smb1_capabilities
,
1128 uint8_t smb_command
,
1129 uint8_t additional_flags
,
1130 uint8_t clear_flags
,
1132 uint16_t additional_flags2
,
1133 uint16_t clear_flags2
,
1137 uint16_t flags2
= 0;
1139 if (protocol
>= PROTOCOL_LANMAN1
) {
1140 flags
|= FLAG_CASELESS_PATHNAMES
;
1141 flags
|= FLAG_CANONICAL_PATHNAMES
;
1144 if (protocol
>= PROTOCOL_LANMAN2
) {
1145 flags2
|= FLAGS2_LONG_PATH_COMPONENTS
;
1146 flags2
|= FLAGS2_EXTENDED_ATTRIBUTES
;
1149 if (protocol
>= PROTOCOL_NT1
) {
1150 flags2
|= FLAGS2_IS_LONG_NAME
;
1152 if (smb1_capabilities
& CAP_UNICODE
) {
1153 flags2
|= FLAGS2_UNICODE_STRINGS
;
1155 if (smb1_capabilities
& CAP_STATUS32
) {
1156 flags2
|= FLAGS2_32_BIT_ERROR_CODES
;
1158 if (smb1_capabilities
& CAP_EXTENDED_SECURITY
) {
1159 flags2
|= FLAGS2_EXTENDED_SECURITY
;
1163 flags
|= additional_flags
;
1164 flags
&= ~clear_flags
;
1165 flags2
|= additional_flags2
;
1166 flags2
&= ~clear_flags2
;
1172 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
);
1174 static bool smb1cli_req_cancel(struct tevent_req
*req
)
1176 struct smbXcli_req_state
*state
=
1177 tevent_req_data(req
,
1178 struct smbXcli_req_state
);
1183 struct tevent_req
*subreq
;
1186 flags
= CVAL(state
->smb1
.hdr
, HDR_FLG
);
1187 flags2
= SVAL(state
->smb1
.hdr
, HDR_FLG2
);
1188 pid
= SVAL(state
->smb1
.hdr
, HDR_PID
);
1189 pid
|= SVAL(state
->smb1
.hdr
, HDR_PIDHIGH
)<<16;
1190 mid
= SVAL(state
->smb1
.hdr
, HDR_MID
);
1192 subreq
= smb1cli_req_create(state
, state
->ev
,
1202 0, NULL
); /* bytes */
1203 if (subreq
== NULL
) {
1206 smb1cli_req_set_mid(subreq
, mid
);
1208 status
= smb1cli_req_chain_submit(&subreq
, 1);
1209 if (!NT_STATUS_IS_OK(status
)) {
1210 TALLOC_FREE(subreq
);
1213 smb1cli_req_set_mid(subreq
, 0);
1215 tevent_req_set_callback(subreq
, smb1cli_req_cancel_done
, NULL
);
1220 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
)
1222 /* we do not care about the result */
1223 TALLOC_FREE(subreq
);
1226 struct tevent_req
*smb1cli_req_create(TALLOC_CTX
*mem_ctx
,
1227 struct tevent_context
*ev
,
1228 struct smbXcli_conn
*conn
,
1229 uint8_t smb_command
,
1230 uint8_t additional_flags
,
1231 uint8_t clear_flags
,
1232 uint16_t additional_flags2
,
1233 uint16_t clear_flags2
,
1234 uint32_t timeout_msec
,
1236 struct smbXcli_tcon
*tcon
,
1237 struct smbXcli_session
*session
,
1238 uint8_t wct
, uint16_t *vwv
,
1240 struct iovec
*bytes_iov
)
1242 struct tevent_req
*req
;
1243 struct smbXcli_req_state
*state
;
1245 uint16_t flags2
= 0;
1249 if (iov_count
> MAX_SMB_IOV
) {
1251 * Should not happen :-)
1256 req
= tevent_req_create(mem_ctx
, &state
,
1257 struct smbXcli_req_state
);
1263 state
->session
= session
;
1267 uid
= session
->smb1
.session_id
;
1271 tid
= tcon
->smb1
.tcon_id
;
1274 state
->smb1
.recv_cmd
= 0xFF;
1275 state
->smb1
.recv_status
= NT_STATUS_INTERNAL_ERROR
;
1276 state
->smb1
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
1277 if (state
->smb1
.recv_iov
== NULL
) {
1282 smb1cli_req_flags(conn
->protocol
,
1283 conn
->smb1
.capabilities
,
1292 SIVAL(state
->smb1
.hdr
, 0, SMB_MAGIC
);
1293 SCVAL(state
->smb1
.hdr
, HDR_COM
, smb_command
);
1294 SIVAL(state
->smb1
.hdr
, HDR_RCLS
, NT_STATUS_V(NT_STATUS_OK
));
1295 SCVAL(state
->smb1
.hdr
, HDR_FLG
, flags
);
1296 SSVAL(state
->smb1
.hdr
, HDR_FLG2
, flags2
);
1297 SSVAL(state
->smb1
.hdr
, HDR_PIDHIGH
, pid
>> 16);
1298 SSVAL(state
->smb1
.hdr
, HDR_TID
, tid
);
1299 SSVAL(state
->smb1
.hdr
, HDR_PID
, pid
);
1300 SSVAL(state
->smb1
.hdr
, HDR_UID
, uid
);
1301 SSVAL(state
->smb1
.hdr
, HDR_MID
, 0); /* this comes later */
1302 SCVAL(state
->smb1
.hdr
, HDR_WCT
, wct
);
1304 state
->smb1
.vwv
= vwv
;
1306 SSVAL(state
->smb1
.bytecount_buf
, 0, smbXcli_iov_len(bytes_iov
, iov_count
));
1308 state
->smb1
.iov
[0].iov_base
= (void *)state
->length_hdr
;
1309 state
->smb1
.iov
[0].iov_len
= sizeof(state
->length_hdr
);
1310 state
->smb1
.iov
[1].iov_base
= (void *)state
->smb1
.hdr
;
1311 state
->smb1
.iov
[1].iov_len
= sizeof(state
->smb1
.hdr
);
1312 state
->smb1
.iov
[2].iov_base
= (void *)state
->smb1
.vwv
;
1313 state
->smb1
.iov
[2].iov_len
= wct
* sizeof(uint16_t);
1314 state
->smb1
.iov
[3].iov_base
= (void *)state
->smb1
.bytecount_buf
;
1315 state
->smb1
.iov
[3].iov_len
= sizeof(uint16_t);
1317 if (iov_count
!= 0) {
1318 memcpy(&state
->smb1
.iov
[4], bytes_iov
,
1319 iov_count
* sizeof(*bytes_iov
));
1321 state
->smb1
.iov_count
= iov_count
+ 4;
1323 if (timeout_msec
> 0) {
1324 struct timeval endtime
;
1326 endtime
= timeval_current_ofs_msec(timeout_msec
);
1327 if (!tevent_req_set_endtime(req
, ev
, endtime
)) {
1332 switch (smb_command
) {
1336 state
->one_way
= true;
1339 state
->one_way
= true;
1340 state
->smb1
.one_way_seqnum
= true;
1344 (CVAL(vwv
+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE
)) {
1345 state
->one_way
= true;
1353 static NTSTATUS
smb1cli_conn_signv(struct smbXcli_conn
*conn
,
1354 struct iovec
*iov
, int iov_count
,
1356 bool one_way_seqnum
)
1358 TALLOC_CTX
*frame
= NULL
;
1362 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1363 * iovec directly. MD5Update would do that just fine.
1366 if (iov_count
< 4) {
1367 return NT_STATUS_INVALID_PARAMETER_MIX
;
1369 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1370 return NT_STATUS_INVALID_PARAMETER_MIX
;
1372 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1373 return NT_STATUS_INVALID_PARAMETER_MIX
;
1375 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1376 return NT_STATUS_INVALID_PARAMETER_MIX
;
1378 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1379 return NT_STATUS_INVALID_PARAMETER_MIX
;
1382 frame
= talloc_stackframe();
1384 buf
= smbXcli_iov_concat(frame
, &iov
[1], iov_count
- 1);
1386 return NT_STATUS_NO_MEMORY
;
1389 *seqnum
= smb_signing_next_seqnum(conn
->smb1
.signing
,
1391 smb_signing_sign_pdu(conn
->smb1
.signing
,
1392 buf
, talloc_get_size(buf
),
1394 memcpy(iov
[1].iov_base
, buf
, iov
[1].iov_len
);
1397 return NT_STATUS_OK
;
1400 static void smb1cli_req_writev_done(struct tevent_req
*subreq
);
1401 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1402 TALLOC_CTX
*tmp_mem
,
1405 static NTSTATUS
smb1cli_req_writev_submit(struct tevent_req
*req
,
1406 struct smbXcli_req_state
*state
,
1407 struct iovec
*iov
, int iov_count
)
1409 struct tevent_req
*subreq
;
1414 if (!smbXcli_conn_is_connected(state
->conn
)) {
1415 return NT_STATUS_CONNECTION_DISCONNECTED
;
1418 if (state
->conn
->protocol
> PROTOCOL_NT1
) {
1419 return NT_STATUS_REVISION_MISMATCH
;
1422 if (iov_count
< 4) {
1423 return NT_STATUS_INVALID_PARAMETER_MIX
;
1425 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1426 return NT_STATUS_INVALID_PARAMETER_MIX
;
1428 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1429 return NT_STATUS_INVALID_PARAMETER_MIX
;
1431 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1432 return NT_STATUS_INVALID_PARAMETER_MIX
;
1434 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1435 return NT_STATUS_INVALID_PARAMETER_MIX
;
1438 cmd
= CVAL(iov
[1].iov_base
, HDR_COM
);
1439 if (cmd
== SMBreadBraw
) {
1440 if (smbXcli_conn_has_async_calls(state
->conn
)) {
1441 return NT_STATUS_INVALID_PARAMETER_MIX
;
1443 state
->conn
->smb1
.read_braw_req
= req
;
1446 if (state
->smb1
.mid
!= 0) {
1447 mid
= state
->smb1
.mid
;
1449 mid
= smb1cli_alloc_mid(state
->conn
);
1451 SSVAL(iov
[1].iov_base
, HDR_MID
, mid
);
1453 _smb_setlen_nbt(iov
[0].iov_base
, smbXcli_iov_len(&iov
[1], iov_count
-1));
1455 status
= smb1cli_conn_signv(state
->conn
, iov
, iov_count
,
1456 &state
->smb1
.seqnum
,
1457 state
->smb1
.one_way_seqnum
);
1459 if (!NT_STATUS_IS_OK(status
)) {
1464 * If we supported multiple encrytion contexts
1465 * here we'd look up based on tid.
1467 if (common_encryption_on(state
->conn
->smb1
.trans_enc
)) {
1468 char *buf
, *enc_buf
;
1470 buf
= (char *)smbXcli_iov_concat(talloc_tos(), iov
, iov_count
);
1472 return NT_STATUS_NO_MEMORY
;
1474 status
= common_encrypt_buffer(state
->conn
->smb1
.trans_enc
,
1475 (char *)buf
, &enc_buf
);
1477 if (!NT_STATUS_IS_OK(status
)) {
1478 DEBUG(0, ("Error in encrypting client message: %s\n",
1479 nt_errstr(status
)));
1482 buf
= (char *)talloc_memdup(state
, enc_buf
,
1483 smb_len_nbt(enc_buf
)+4);
1486 return NT_STATUS_NO_MEMORY
;
1488 iov
[0].iov_base
= (void *)buf
;
1489 iov
[0].iov_len
= talloc_get_size(buf
);
1493 if (state
->conn
->dispatch_incoming
== NULL
) {
1494 state
->conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
1497 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
1499 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
1500 state
->conn
->write_fd
, false, iov
, iov_count
);
1501 if (subreq
== NULL
) {
1502 return NT_STATUS_NO_MEMORY
;
1504 tevent_req_set_callback(subreq
, smb1cli_req_writev_done
, req
);
1505 return NT_STATUS_OK
;
1508 struct tevent_req
*smb1cli_req_send(TALLOC_CTX
*mem_ctx
,
1509 struct tevent_context
*ev
,
1510 struct smbXcli_conn
*conn
,
1511 uint8_t smb_command
,
1512 uint8_t additional_flags
,
1513 uint8_t clear_flags
,
1514 uint16_t additional_flags2
,
1515 uint16_t clear_flags2
,
1516 uint32_t timeout_msec
,
1518 struct smbXcli_tcon
*tcon
,
1519 struct smbXcli_session
*session
,
1520 uint8_t wct
, uint16_t *vwv
,
1522 const uint8_t *bytes
)
1524 struct tevent_req
*req
;
1528 iov
.iov_base
= discard_const_p(void, bytes
);
1529 iov
.iov_len
= num_bytes
;
1531 req
= smb1cli_req_create(mem_ctx
, ev
, conn
, smb_command
,
1532 additional_flags
, clear_flags
,
1533 additional_flags2
, clear_flags2
,
1540 if (!tevent_req_is_in_progress(req
)) {
1541 return tevent_req_post(req
, ev
);
1543 status
= smb1cli_req_chain_submit(&req
, 1);
1544 if (tevent_req_nterror(req
, status
)) {
1545 return tevent_req_post(req
, ev
);
1550 static void smb1cli_req_writev_done(struct tevent_req
*subreq
)
1552 struct tevent_req
*req
=
1553 tevent_req_callback_data(subreq
,
1555 struct smbXcli_req_state
*state
=
1556 tevent_req_data(req
,
1557 struct smbXcli_req_state
);
1561 nwritten
= writev_recv(subreq
, &err
);
1562 TALLOC_FREE(subreq
);
1563 if (nwritten
== -1) {
1564 NTSTATUS status
= map_nt_error_from_unix_common(err
);
1565 smbXcli_conn_disconnect(state
->conn
, status
);
1569 if (state
->one_way
) {
1570 state
->inbuf
= NULL
;
1571 tevent_req_done(req
);
1575 if (!smbXcli_req_set_pending(req
)) {
1576 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1581 static void smbXcli_conn_received(struct tevent_req
*subreq
)
1583 struct smbXcli_conn
*conn
=
1584 tevent_req_callback_data(subreq
,
1585 struct smbXcli_conn
);
1586 TALLOC_CTX
*frame
= talloc_stackframe();
1592 if (subreq
!= conn
->read_smb_req
) {
1593 DEBUG(1, ("Internal error: cli_smb_received called with "
1594 "unexpected subreq\n"));
1595 smbXcli_conn_disconnect(conn
, NT_STATUS_INTERNAL_ERROR
);
1599 conn
->read_smb_req
= NULL
;
1601 received
= read_smb_recv(subreq
, frame
, &inbuf
, &err
);
1602 TALLOC_FREE(subreq
);
1603 if (received
== -1) {
1604 status
= map_nt_error_from_unix_common(err
);
1605 smbXcli_conn_disconnect(conn
, status
);
1610 status
= conn
->dispatch_incoming(conn
, frame
, inbuf
);
1612 if (NT_STATUS_IS_OK(status
)) {
1614 * We should not do any more processing
1615 * as the dispatch function called
1616 * tevent_req_done().
1621 if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1623 * We got an error, so notify all pending requests
1625 smbXcli_conn_disconnect(conn
, status
);
1630 * We got NT_STATUS_RETRY, so we may ask for a
1631 * next incoming pdu.
1633 if (!smbXcli_conn_receive_next(conn
)) {
1634 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
1638 static NTSTATUS
smb1cli_inbuf_parse_chain(uint8_t *buf
, TALLOC_CTX
*mem_ctx
,
1639 struct iovec
**piov
, int *pnum_iov
)
1650 size_t min_size
= MIN_SMB_SIZE
;
1652 buflen
= smb_len_tcp(buf
);
1655 hdr
= buf
+ NBT_HDR_SIZE
;
1657 status
= smb1cli_pull_raw_error(hdr
);
1658 if (NT_STATUS_IS_ERR(status
)) {
1660 * This is an ugly hack to support OS/2
1661 * which skips the byte_count in the DATA block
1662 * on some error responses.
1666 min_size
-= sizeof(uint16_t);
1669 if (buflen
< min_size
) {
1670 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1674 * This returns iovec elements in the following order:
1689 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
1691 return NT_STATUS_NO_MEMORY
;
1693 iov
[0].iov_base
= hdr
;
1694 iov
[0].iov_len
= HDR_WCT
;
1697 cmd
= CVAL(hdr
, HDR_COM
);
1701 size_t len
= buflen
- taken
;
1703 struct iovec
*iov_tmp
;
1710 * we need at least WCT
1712 needed
= sizeof(uint8_t);
1714 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1715 __location__
, (int)len
, (int)needed
));
1720 * Now we check if the specified words are there
1722 wct
= CVAL(hdr
, wct_ofs
);
1723 needed
+= wct
* sizeof(uint16_t);
1725 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1726 __location__
, (int)len
, (int)needed
));
1730 if ((num_iov
== 1) &&
1732 NT_STATUS_IS_ERR(status
))
1735 * This is an ugly hack to support OS/2
1736 * which skips the byte_count in the DATA block
1737 * on some error responses.
1741 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
1743 if (iov_tmp
== NULL
) {
1745 return NT_STATUS_NO_MEMORY
;
1748 cur
= &iov
[num_iov
];
1752 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
1754 cur
[1].iov_base
= cur
[0].iov_base
;
1761 * we need at least BCC
1763 needed
+= sizeof(uint16_t);
1765 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1766 __location__
, (int)len
, (int)needed
));
1771 * Now we check if the specified bytes are there
1773 bcc_ofs
= wct_ofs
+ sizeof(uint8_t) + wct
* sizeof(uint16_t);
1774 bcc
= SVAL(hdr
, bcc_ofs
);
1775 needed
+= bcc
* sizeof(uint8_t);
1777 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1778 __location__
, (int)len
, (int)needed
));
1783 * we allocate 2 iovec structures for words and bytes
1785 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
1787 if (iov_tmp
== NULL
) {
1789 return NT_STATUS_NO_MEMORY
;
1792 cur
= &iov
[num_iov
];
1795 cur
[0].iov_len
= wct
* sizeof(uint16_t);
1796 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
1797 cur
[1].iov_len
= bcc
* sizeof(uint8_t);
1798 cur
[1].iov_base
= hdr
+ (bcc_ofs
+ sizeof(uint16_t));
1802 if (!smb1cli_is_andx_req(cmd
)) {
1804 * If the current command does not have AndX chanining
1810 if (wct
== 0 && bcc
== 0) {
1812 * An empty response also ends the chain,
1813 * most likely with an error.
1819 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1820 __location__
, (int)wct
, (int)cmd
));
1823 cmd
= CVAL(cur
[0].iov_base
, 0);
1826 * If it is the end of the chain we are also done.
1830 wct_ofs
= SVAL(cur
[0].iov_base
, 2);
1832 if (wct_ofs
< taken
) {
1833 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1835 if (wct_ofs
> buflen
) {
1836 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1840 * we consumed everything up to the start of the next
1846 remaining
= buflen
- taken
;
1848 if (remaining
> 0 && num_iov
>= 3) {
1850 * The last DATA block gets the remaining
1851 * bytes, this is needed to support
1852 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1854 iov
[num_iov
-1].iov_len
+= remaining
;
1858 *pnum_iov
= num_iov
;
1859 return NT_STATUS_OK
;
1863 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1866 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1867 TALLOC_CTX
*tmp_mem
,
1870 struct tevent_req
*req
;
1871 struct smbXcli_req_state
*state
;
1878 uint8_t *inhdr
= inbuf
+ NBT_HDR_SIZE
;
1879 size_t len
= smb_len_tcp(inbuf
);
1880 struct iovec
*iov
= NULL
;
1882 struct tevent_req
**chain
= NULL
;
1883 size_t num_chained
= 0;
1884 size_t num_responses
= 0;
1886 if (conn
->smb1
.read_braw_req
!= NULL
) {
1887 req
= conn
->smb1
.read_braw_req
;
1888 conn
->smb1
.read_braw_req
= NULL
;
1889 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1891 smbXcli_req_unset_pending(req
);
1893 if (state
->smb1
.recv_iov
== NULL
) {
1895 * For requests with more than
1896 * one response, we have to readd the
1899 state
->smb1
.recv_iov
= talloc_zero_array(state
,
1902 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
1903 return NT_STATUS_OK
;
1907 state
->smb1
.recv_iov
[0].iov_base
= (void *)(inhdr
);
1908 state
->smb1
.recv_iov
[0].iov_len
= len
;
1909 ZERO_STRUCT(state
->smb1
.recv_iov
[1]);
1910 ZERO_STRUCT(state
->smb1
.recv_iov
[2]);
1912 state
->smb1
.recv_cmd
= SMBreadBraw
;
1913 state
->smb1
.recv_status
= NT_STATUS_OK
;
1914 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
1916 tevent_req_done(req
);
1917 return NT_STATUS_OK
;
1920 if ((IVAL(inhdr
, 0) != SMB_MAGIC
) /* 0xFF"SMB" */
1921 && (SVAL(inhdr
, 0) != 0x45ff)) /* 0xFF"E" */ {
1922 DEBUG(10, ("Got non-SMB PDU\n"));
1923 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1927 * If we supported multiple encrytion contexts
1928 * here we'd look up based on tid.
1930 if (common_encryption_on(conn
->smb1
.trans_enc
)
1931 && (CVAL(inbuf
, 0) == 0)) {
1932 uint16_t enc_ctx_num
;
1934 status
= get_enc_ctx_num(inbuf
, &enc_ctx_num
);
1935 if (!NT_STATUS_IS_OK(status
)) {
1936 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1937 nt_errstr(status
)));
1941 if (enc_ctx_num
!= conn
->smb1
.trans_enc
->enc_ctx_num
) {
1942 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1944 conn
->smb1
.trans_enc
->enc_ctx_num
));
1945 return NT_STATUS_INVALID_HANDLE
;
1948 status
= common_decrypt_buffer(conn
->smb1
.trans_enc
,
1950 if (!NT_STATUS_IS_OK(status
)) {
1951 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1952 nt_errstr(status
)));
1955 inhdr
= inbuf
+ NBT_HDR_SIZE
;
1956 len
= smb_len_nbt(inbuf
);
1959 mid
= SVAL(inhdr
, HDR_MID
);
1960 num_pending
= talloc_array_length(conn
->pending
);
1962 for (i
=0; i
<num_pending
; i
++) {
1963 if (mid
== smb1cli_req_mid(conn
->pending
[i
])) {
1967 if (i
== num_pending
) {
1968 /* Dump unexpected reply */
1969 return NT_STATUS_RETRY
;
1972 oplock_break
= false;
1974 if (mid
== 0xffff) {
1976 * Paranoia checks that this is really an oplock break request.
1978 oplock_break
= (len
== 51); /* hdr + 8 words */
1979 oplock_break
&= ((CVAL(inhdr
, HDR_FLG
) & FLAG_REPLY
) == 0);
1980 oplock_break
&= (CVAL(inhdr
, HDR_COM
) == SMBlockingX
);
1981 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(6)) == 0);
1982 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(7)) == 0);
1984 if (!oplock_break
) {
1985 /* Dump unexpected reply */
1986 return NT_STATUS_RETRY
;
1990 req
= conn
->pending
[i
];
1991 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1993 if (!oplock_break
/* oplock breaks are not signed */
1994 && !smb_signing_check_pdu(conn
->smb1
.signing
,
1995 inhdr
, len
, state
->smb1
.seqnum
+1)) {
1996 DEBUG(10, ("cli_check_sign_mac failed\n"));
1997 return NT_STATUS_ACCESS_DENIED
;
2000 status
= smb1cli_inbuf_parse_chain(inbuf
, tmp_mem
,
2002 if (!NT_STATUS_IS_OK(status
)) {
2003 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2004 nt_errstr(status
)));
2008 cmd
= CVAL(inhdr
, HDR_COM
);
2009 status
= smb1cli_pull_raw_error(inhdr
);
2011 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
) &&
2012 (state
->session
!= NULL
) && state
->session
->disconnect_expired
)
2015 * this should be a short term hack
2016 * until the upper layers have implemented
2017 * re-authentication.
2022 if (state
->smb1
.chained_requests
== NULL
) {
2024 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2027 smbXcli_req_unset_pending(req
);
2029 if (state
->smb1
.recv_iov
== NULL
) {
2031 * For requests with more than
2032 * one response, we have to readd the
2035 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2038 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2039 return NT_STATUS_OK
;
2043 state
->smb1
.recv_cmd
= cmd
;
2044 state
->smb1
.recv_status
= status
;
2045 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
2047 state
->smb1
.recv_iov
[0] = iov
[0];
2048 state
->smb1
.recv_iov
[1] = iov
[1];
2049 state
->smb1
.recv_iov
[2] = iov
[2];
2051 if (talloc_array_length(conn
->pending
) == 0) {
2052 tevent_req_done(req
);
2053 return NT_STATUS_OK
;
2056 tevent_req_defer_callback(req
, state
->ev
);
2057 tevent_req_done(req
);
2058 return NT_STATUS_RETRY
;
2061 chain
= talloc_move(tmp_mem
, &state
->smb1
.chained_requests
);
2062 num_chained
= talloc_array_length(chain
);
2063 num_responses
= (num_iov
- 1)/2;
2065 if (num_responses
> num_chained
) {
2066 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2069 for (i
=0; i
<num_chained
; i
++) {
2070 size_t iov_idx
= 1 + (i
*2);
2071 struct iovec
*cur
= &iov
[iov_idx
];
2075 state
= tevent_req_data(req
, struct smbXcli_req_state
);
2077 smbXcli_req_unset_pending(req
);
2080 * as we finish multiple requests here
2081 * we need to defer the callbacks as
2082 * they could destroy our current stack state.
2084 tevent_req_defer_callback(req
, state
->ev
);
2086 if (i
>= num_responses
) {
2087 tevent_req_nterror(req
, NT_STATUS_REQUEST_ABORTED
);
2091 if (state
->smb1
.recv_iov
== NULL
) {
2093 * For requests with more than
2094 * one response, we have to readd the
2097 state
->smb1
.recv_iov
= talloc_zero_array(state
,
2100 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
2105 state
->smb1
.recv_cmd
= cmd
;
2107 if (i
== (num_responses
- 1)) {
2109 * The last request in the chain gets the status
2111 state
->smb1
.recv_status
= status
;
2113 cmd
= CVAL(cur
[0].iov_base
, 0);
2114 state
->smb1
.recv_status
= NT_STATUS_OK
;
2117 state
->inbuf
= inbuf
;
2120 * Note: here we use talloc_reference() in a way
2121 * that does not expose it to the caller.
2123 inbuf_ref
= talloc_reference(state
->smb1
.recv_iov
, inbuf
);
2124 if (tevent_req_nomem(inbuf_ref
, req
)) {
2128 /* copy the related buffers */
2129 state
->smb1
.recv_iov
[0] = iov
[0];
2130 state
->smb1
.recv_iov
[1] = cur
[0];
2131 state
->smb1
.recv_iov
[2] = cur
[1];
2133 tevent_req_done(req
);
2136 return NT_STATUS_RETRY
;
2139 NTSTATUS
smb1cli_req_recv(struct tevent_req
*req
,
2140 TALLOC_CTX
*mem_ctx
,
2141 struct iovec
**piov
,
2145 uint32_t *pvwv_offset
,
2146 uint32_t *pnum_bytes
,
2148 uint32_t *pbytes_offset
,
2150 const struct smb1cli_req_expected_response
*expected
,
2151 size_t num_expected
)
2153 struct smbXcli_req_state
*state
=
2154 tevent_req_data(req
,
2155 struct smbXcli_req_state
);
2156 NTSTATUS status
= NT_STATUS_OK
;
2157 struct iovec
*recv_iov
= NULL
;
2158 uint8_t *hdr
= NULL
;
2160 uint32_t vwv_offset
= 0;
2161 uint16_t *vwv
= NULL
;
2162 uint32_t num_bytes
= 0;
2163 uint32_t bytes_offset
= 0;
2164 uint8_t *bytes
= NULL
;
2166 bool found_status
= false;
2167 bool found_size
= false;
2181 if (pvwv_offset
!= NULL
) {
2184 if (pnum_bytes
!= NULL
) {
2187 if (pbytes
!= NULL
) {
2190 if (pbytes_offset
!= NULL
) {
2193 if (pinbuf
!= NULL
) {
2197 if (state
->inbuf
!= NULL
) {
2198 recv_iov
= state
->smb1
.recv_iov
;
2199 state
->smb1
.recv_iov
= NULL
;
2200 if (state
->smb1
.recv_cmd
!= SMBreadBraw
) {
2201 hdr
= (uint8_t *)recv_iov
[0].iov_base
;
2202 wct
= recv_iov
[1].iov_len
/2;
2203 vwv
= (uint16_t *)recv_iov
[1].iov_base
;
2204 vwv_offset
= PTR_DIFF(vwv
, hdr
);
2205 num_bytes
= recv_iov
[2].iov_len
;
2206 bytes
= (uint8_t *)recv_iov
[2].iov_base
;
2207 bytes_offset
= PTR_DIFF(bytes
, hdr
);
2211 if (tevent_req_is_nterror(req
, &status
)) {
2212 for (i
=0; i
< num_expected
; i
++) {
2213 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2214 found_status
= true;
2220 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
2226 if (num_expected
== 0) {
2227 found_status
= true;
2231 status
= state
->smb1
.recv_status
;
2233 for (i
=0; i
< num_expected
; i
++) {
2234 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2238 found_status
= true;
2239 if (expected
[i
].wct
== 0) {
2244 if (expected
[i
].wct
== wct
) {
2250 if (!found_status
) {
2255 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2259 *piov
= talloc_move(mem_ctx
, &recv_iov
);
2271 if (pvwv_offset
!= NULL
) {
2272 *pvwv_offset
= vwv_offset
;
2274 if (pnum_bytes
!= NULL
) {
2275 *pnum_bytes
= num_bytes
;
2277 if (pbytes
!= NULL
) {
2280 if (pbytes_offset
!= NULL
) {
2281 *pbytes_offset
= bytes_offset
;
2283 if (pinbuf
!= NULL
) {
2284 *pinbuf
= state
->inbuf
;
2290 size_t smb1cli_req_wct_ofs(struct tevent_req
**reqs
, int num_reqs
)
2297 for (i
=0; i
<num_reqs
; i
++) {
2298 struct smbXcli_req_state
*state
;
2299 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2300 wct_ofs
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2301 state
->smb1
.iov_count
-2);
2302 wct_ofs
= (wct_ofs
+ 3) & ~3;
2307 NTSTATUS
smb1cli_req_chain_submit(struct tevent_req
**reqs
, int num_reqs
)
2309 struct smbXcli_req_state
*first_state
=
2310 tevent_req_data(reqs
[0],
2311 struct smbXcli_req_state
);
2312 struct smbXcli_req_state
*state
;
2314 size_t chain_padding
= 0;
2316 struct iovec
*iov
= NULL
;
2317 struct iovec
*this_iov
;
2321 if (num_reqs
== 1) {
2322 return smb1cli_req_writev_submit(reqs
[0], first_state
,
2323 first_state
->smb1
.iov
,
2324 first_state
->smb1
.iov_count
);
2328 for (i
=0; i
<num_reqs
; i
++) {
2329 if (!tevent_req_is_in_progress(reqs
[i
])) {
2330 return NT_STATUS_INTERNAL_ERROR
;
2333 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2335 if (state
->smb1
.iov_count
< 4) {
2336 return NT_STATUS_INVALID_PARAMETER_MIX
;
2341 * The NBT and SMB header
2354 iovlen
+= state
->smb1
.iov_count
- 2;
2357 iov
= talloc_zero_array(first_state
, struct iovec
, iovlen
);
2359 return NT_STATUS_NO_MEMORY
;
2362 first_state
->smb1
.chained_requests
= (struct tevent_req
**)talloc_memdup(
2363 first_state
, reqs
, sizeof(*reqs
) * num_reqs
);
2364 if (first_state
->smb1
.chained_requests
== NULL
) {
2366 return NT_STATUS_NO_MEMORY
;
2369 wct_offset
= HDR_WCT
;
2372 for (i
=0; i
<num_reqs
; i
++) {
2373 size_t next_padding
= 0;
2376 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2378 if (i
< num_reqs
-1) {
2379 if (!smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))
2380 || CVAL(state
->smb1
.hdr
, HDR_WCT
) < 2) {
2382 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2383 return NT_STATUS_INVALID_PARAMETER_MIX
;
2387 wct_offset
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2388 state
->smb1
.iov_count
-2) + 1;
2389 if ((wct_offset
% 4) != 0) {
2390 next_padding
= 4 - (wct_offset
% 4);
2392 wct_offset
+= next_padding
;
2393 vwv
= state
->smb1
.vwv
;
2395 if (i
< num_reqs
-1) {
2396 struct smbXcli_req_state
*next_state
=
2397 tevent_req_data(reqs
[i
+1],
2398 struct smbXcli_req_state
);
2399 SCVAL(vwv
+0, 0, CVAL(next_state
->smb1
.hdr
, HDR_COM
));
2401 SSVAL(vwv
+1, 0, wct_offset
);
2402 } else if (smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))) {
2403 /* properly end the chain */
2404 SCVAL(vwv
+0, 0, 0xff);
2405 SCVAL(vwv
+0, 1, 0xff);
2411 * The NBT and SMB header
2413 this_iov
[0] = state
->smb1
.iov
[0];
2414 this_iov
[1] = state
->smb1
.iov
[1];
2418 * This one is a bit subtle. We have to add
2419 * chain_padding bytes between the requests, and we
2420 * have to also include the wct field of the
2421 * subsequent requests. We use the subsequent header
2422 * for the padding, it contains the wct field in its
2425 this_iov
[0].iov_len
= chain_padding
+1;
2426 this_iov
[0].iov_base
= (void *)&state
->smb1
.hdr
[
2427 sizeof(state
->smb1
.hdr
) - this_iov
[0].iov_len
];
2428 memset(this_iov
[0].iov_base
, 0, this_iov
[0].iov_len
-1);
2433 * copy the words and bytes
2435 memcpy(this_iov
, state
->smb1
.iov
+2,
2436 sizeof(struct iovec
) * (state
->smb1
.iov_count
-2));
2437 this_iov
+= state
->smb1
.iov_count
- 2;
2438 chain_padding
= next_padding
;
2441 nbt_len
= smbXcli_iov_len(&iov
[1], iovlen
-1);
2442 if (nbt_len
> first_state
->conn
->smb1
.max_xmit
) {
2444 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2445 return NT_STATUS_INVALID_PARAMETER_MIX
;
2448 status
= smb1cli_req_writev_submit(reqs
[0], first_state
, iov
, iovlen
);
2449 if (!NT_STATUS_IS_OK(status
)) {
2451 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2455 return NT_STATUS_OK
;
2458 bool smbXcli_conn_has_async_calls(struct smbXcli_conn
*conn
)
2460 return ((tevent_queue_length(conn
->outgoing
) != 0)
2461 || (talloc_array_length(conn
->pending
) != 0));
2464 bool smbXcli_conn_dfs_supported(struct smbXcli_conn
*conn
)
2466 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
2467 return (smb2cli_conn_server_capabilities(conn
) & SMB2_CAP_DFS
);
2470 return (smb1cli_conn_capabilities(conn
) & CAP_DFS
);
2473 bool smb2cli_conn_req_possible(struct smbXcli_conn
*conn
, uint32_t *max_dyn_len
)
2475 uint16_t credits
= 1;
2477 if (conn
->smb2
.cur_credits
== 0) {
2478 if (max_dyn_len
!= NULL
) {
2484 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2485 credits
= conn
->smb2
.cur_credits
;
2488 if (max_dyn_len
!= NULL
) {
2489 *max_dyn_len
= credits
* 65536;
2495 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn
*conn
)
2497 return conn
->smb2
.server
.capabilities
;
2500 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
2502 return conn
->smb2
.server
.security_mode
;
2505 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn
*conn
)
2507 return conn
->smb2
.server
.max_trans_size
;
2510 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn
*conn
)
2512 return conn
->smb2
.server
.max_read_size
;
2515 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn
*conn
)
2517 return conn
->smb2
.server
.max_write_size
;
2520 void smb2cli_conn_set_max_credits(struct smbXcli_conn
*conn
,
2521 uint16_t max_credits
)
2523 conn
->smb2
.max_credits
= max_credits
;
2526 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
);
2528 static bool smb2cli_req_cancel(struct tevent_req
*req
)
2530 struct smbXcli_req_state
*state
=
2531 tevent_req_data(req
,
2532 struct smbXcli_req_state
);
2533 uint32_t flags
= IVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
2534 uint64_t mid
= BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
);
2535 uint64_t aid
= BVAL(state
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
);
2536 struct smbXcli_tcon
*tcon
= state
->tcon
;
2537 struct smbXcli_session
*session
= state
->session
;
2538 uint8_t *fixed
= state
->smb2
.pad
;
2539 uint16_t fixed_len
= 4;
2540 struct tevent_req
*subreq
;
2541 struct smbXcli_req_state
*substate
;
2544 SSVAL(fixed
, 0, 0x04);
2547 subreq
= smb2cli_req_create(state
, state
->ev
,
2555 if (subreq
== NULL
) {
2558 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
2561 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2562 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2564 flags
&= SMB2_HDR_FLAG_ASYNC
;
2566 if (flags
& SMB2_HDR_FLAG_ASYNC
) {
2570 SIVAL(substate
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
2571 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
2572 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, aid
);
2574 status
= smb2cli_req_compound_submit(&subreq
, 1);
2575 if (!NT_STATUS_IS_OK(status
)) {
2576 TALLOC_FREE(subreq
);
2580 tevent_req_set_callback(subreq
, smb2cli_req_cancel_done
, NULL
);
2585 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
)
2587 /* we do not care about the result */
2588 TALLOC_FREE(subreq
);
2591 struct tevent_req
*smb2cli_req_create(TALLOC_CTX
*mem_ctx
,
2592 struct tevent_context
*ev
,
2593 struct smbXcli_conn
*conn
,
2595 uint32_t additional_flags
,
2596 uint32_t clear_flags
,
2597 uint32_t timeout_msec
,
2598 struct smbXcli_tcon
*tcon
,
2599 struct smbXcli_session
*session
,
2600 const uint8_t *fixed
,
2604 uint32_t max_dyn_len
)
2606 struct tevent_req
*req
;
2607 struct smbXcli_req_state
*state
;
2611 bool use_channel_sequence
= false;
2612 uint16_t channel_sequence
= 0;
2614 req
= tevent_req_create(mem_ctx
, &state
,
2615 struct smbXcli_req_state
);
2622 state
->session
= session
;
2625 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_PERSISTENT_HANDLES
) {
2626 use_channel_sequence
= true;
2627 } else if (conn
->smb2
.server
.capabilities
& SMB2_CAP_MULTI_CHANNEL
) {
2628 use_channel_sequence
= true;
2632 uid
= session
->smb2
->session_id
;
2634 if (use_channel_sequence
) {
2635 channel_sequence
= session
->smb2
->channel_sequence
;
2638 state
->smb2
.should_sign
= session
->smb2
->should_sign
;
2639 state
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
2641 if (cmd
== SMB2_OP_SESSSETUP
&&
2642 session
->smb2
->signing_key
.length
!= 0) {
2643 state
->smb2
.should_sign
= true;
2646 if (cmd
== SMB2_OP_SESSSETUP
&&
2647 session
->smb2_channel
.signing_key
.length
== 0) {
2648 state
->smb2
.should_encrypt
= false;
2653 tid
= tcon
->smb2
.tcon_id
;
2655 if (tcon
->smb2
.should_encrypt
) {
2656 state
->smb2
.should_encrypt
= true;
2660 if (state
->smb2
.should_encrypt
) {
2661 state
->smb2
.should_sign
= false;
2664 state
->smb2
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
2665 if (state
->smb2
.recv_iov
== NULL
) {
2670 flags
|= additional_flags
;
2671 flags
&= ~clear_flags
;
2673 state
->smb2
.fixed
= fixed
;
2674 state
->smb2
.fixed_len
= fixed_len
;
2675 state
->smb2
.dyn
= dyn
;
2676 state
->smb2
.dyn_len
= dyn_len
;
2677 state
->smb2
.max_dyn_len
= max_dyn_len
;
2679 if (state
->smb2
.should_encrypt
) {
2680 SIVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
2681 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
, uid
);
2684 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PROTOCOL_ID
, SMB2_MAGIC
);
2685 SSVAL(state
->smb2
.hdr
, SMB2_HDR_LENGTH
, SMB2_HDR_BODY
);
2686 SSVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
, cmd
);
2687 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CHANNEL_SEQUENCE
, channel_sequence
);
2688 SIVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
2689 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PID
, 0); /* reserved */
2690 SIVAL(state
->smb2
.hdr
, SMB2_HDR_TID
, tid
);
2691 SBVAL(state
->smb2
.hdr
, SMB2_HDR_SESSION_ID
, uid
);
2694 case SMB2_OP_CANCEL
:
2695 state
->one_way
= true;
2699 * If this is a dummy request, it will have
2700 * UINT64_MAX as message id.
2701 * If we send on break acknowledgement,
2702 * this gets overwritten later.
2704 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, UINT64_MAX
);
2708 if (timeout_msec
> 0) {
2709 struct timeval endtime
;
2711 endtime
= timeval_current_ofs_msec(timeout_msec
);
2712 if (!tevent_req_set_endtime(req
, ev
, endtime
)) {
2720 void smb2cli_req_set_notify_async(struct tevent_req
*req
)
2722 struct smbXcli_req_state
*state
=
2723 tevent_req_data(req
,
2724 struct smbXcli_req_state
);
2726 state
->smb2
.notify_async
= true;
2729 static void smb2cli_req_writev_done(struct tevent_req
*subreq
);
2730 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
2731 TALLOC_CTX
*tmp_mem
,
2734 NTSTATUS
smb2cli_req_compound_submit(struct tevent_req
**reqs
,
2737 struct smbXcli_req_state
*state
;
2738 struct tevent_req
*subreq
;
2740 int i
, num_iov
, nbt_len
;
2742 const DATA_BLOB
*encryption_key
= NULL
;
2743 uint64_t encryption_session_id
= 0;
2746 * 1 for the nbt length, optional TRANSFORM
2747 * per request: HDR, fixed, dyn, padding
2748 * -1 because the last one does not need padding
2751 iov
= talloc_array(reqs
[0], struct iovec
, 1 + 1 + 4*num_reqs
- 1);
2753 return NT_STATUS_NO_MEMORY
;
2760 * the session of the first request that requires encryption
2761 * specifies the encryption key.
2763 for (i
=0; i
<num_reqs
; i
++) {
2764 if (!tevent_req_is_in_progress(reqs
[i
])) {
2765 return NT_STATUS_INTERNAL_ERROR
;
2768 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2770 if (!smbXcli_conn_is_connected(state
->conn
)) {
2771 return NT_STATUS_CONNECTION_DISCONNECTED
;
2774 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
2775 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
2776 return NT_STATUS_REVISION_MISMATCH
;
2779 if (state
->session
== NULL
) {
2783 if (!state
->smb2
.should_encrypt
) {
2787 encryption_key
= &state
->session
->smb2
->encryption_key
;
2788 if (encryption_key
->length
== 0) {
2789 return NT_STATUS_INVALID_PARAMETER_MIX
;
2792 encryption_session_id
= state
->session
->smb2
->session_id
;
2795 iov
[num_iov
].iov_base
= state
->smb2
.transform
;
2796 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.transform
);
2799 SBVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
2800 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
,
2801 state
->session
->smb2
->nonce_low
);
2802 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
+8,
2803 state
->session
->smb2
->nonce_high
);
2804 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
,
2805 encryption_session_id
);
2807 state
->session
->smb2
->nonce_low
+= 1;
2808 if (state
->session
->smb2
->nonce_low
== 0) {
2809 state
->session
->smb2
->nonce_high
+= 1;
2810 state
->session
->smb2
->nonce_low
+= 1;
2813 nbt_len
+= SMB2_TF_HDR_SIZE
;
2817 for (i
=0; i
<num_reqs
; i
++) {
2826 const DATA_BLOB
*signing_key
= NULL
;
2828 if (!tevent_req_is_in_progress(reqs
[i
])) {
2829 return NT_STATUS_INTERNAL_ERROR
;
2832 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2834 if (!smbXcli_conn_is_connected(state
->conn
)) {
2835 return NT_STATUS_CONNECTION_DISCONNECTED
;
2838 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
2839 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
2840 return NT_STATUS_REVISION_MISMATCH
;
2843 opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
2844 if (opcode
== SMB2_OP_CANCEL
) {
2848 avail
= UINT64_MAX
- state
->conn
->smb2
.mid
;
2850 return NT_STATUS_CONNECTION_ABORTED
;
2853 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2854 uint32_t max_dyn_len
= 1;
2856 max_dyn_len
= MAX(max_dyn_len
, state
->smb2
.dyn_len
);
2857 max_dyn_len
= MAX(max_dyn_len
, state
->smb2
.max_dyn_len
);
2859 charge
= (max_dyn_len
- 1)/ 65536 + 1;
2864 charge
= MAX(state
->smb2
.credit_charge
, charge
);
2866 avail
= MIN(avail
, state
->conn
->smb2
.cur_credits
);
2867 if (avail
< charge
) {
2868 return NT_STATUS_INTERNAL_ERROR
;
2872 if (state
->conn
->smb2
.max_credits
> state
->conn
->smb2
.cur_credits
) {
2873 credits
= state
->conn
->smb2
.max_credits
-
2874 state
->conn
->smb2
.cur_credits
;
2876 if (state
->conn
->smb2
.max_credits
>= state
->conn
->smb2
.cur_credits
) {
2880 mid
= state
->conn
->smb2
.mid
;
2881 state
->conn
->smb2
.mid
+= charge
;
2882 state
->conn
->smb2
.cur_credits
-= charge
;
2884 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2885 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT_CHARGE
, charge
);
2887 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT
, credits
);
2888 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
2891 if (state
->session
&& encryption_key
== NULL
) {
2893 * We prefer the channel signing key if it is
2896 if (state
->smb2
.should_sign
) {
2897 signing_key
= &state
->session
->smb2_channel
.signing_key
;
2901 * If it is a channel binding, we already have the main
2902 * signing key and try that one.
2904 if (signing_key
&& signing_key
->length
== 0) {
2905 signing_key
= &state
->session
->smb2
->signing_key
;
2909 * If we do not have any session key yet, we skip the
2910 * signing of SMB2_OP_SESSSETUP requests.
2912 if (signing_key
&& signing_key
->length
== 0) {
2918 iov
[num_iov
].iov_base
= state
->smb2
.hdr
;
2919 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.hdr
);
2922 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.fixed
);
2923 iov
[num_iov
].iov_len
= state
->smb2
.fixed_len
;
2926 if (state
->smb2
.dyn
!= NULL
) {
2927 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.dyn
);
2928 iov
[num_iov
].iov_len
= state
->smb2
.dyn_len
;
2932 reqlen
= sizeof(state
->smb2
.hdr
);
2933 reqlen
+= state
->smb2
.fixed_len
;
2934 reqlen
+= state
->smb2
.dyn_len
;
2936 if (i
< num_reqs
-1) {
2937 if ((reqlen
% 8) > 0) {
2938 uint8_t pad
= 8 - (reqlen
% 8);
2939 iov
[num_iov
].iov_base
= state
->smb2
.pad
;
2940 iov
[num_iov
].iov_len
= pad
;
2944 SIVAL(state
->smb2
.hdr
, SMB2_HDR_NEXT_COMMAND
, reqlen
);
2947 state
->smb2
.encryption_session_id
= encryption_session_id
;
2949 if (signing_key
!= NULL
) {
2952 status
= smb2_signing_sign_pdu(*signing_key
,
2953 state
->session
->conn
->protocol
,
2954 &iov
[hdr_iov
], num_iov
- hdr_iov
);
2955 if (!NT_STATUS_IS_OK(status
)) {
2962 ret
= smbXcli_req_set_pending(reqs
[i
]);
2964 return NT_STATUS_NO_MEMORY
;
2968 state
= tevent_req_data(reqs
[0], struct smbXcli_req_state
);
2969 _smb_setlen_tcp(state
->length_hdr
, nbt_len
);
2970 iov
[0].iov_base
= state
->length_hdr
;
2971 iov
[0].iov_len
= sizeof(state
->length_hdr
);
2973 if (encryption_key
!= NULL
) {
2975 size_t buflen
= nbt_len
- SMB2_TF_HDR_SIZE
;
2979 buf
= talloc_array(iov
, uint8_t, buflen
);
2981 return NT_STATUS_NO_MEMORY
;
2985 * We copy the buffers before encrypting them,
2986 * this is at least currently needed for the
2987 * to keep state->smb2.hdr.
2989 * Also the callers may expect there buffers
2992 for (vi
= tf_iov
+ 1; vi
< num_iov
; vi
++) {
2993 struct iovec
*v
= &iov
[vi
];
2994 const uint8_t *o
= (const uint8_t *)v
->iov_base
;
2996 memcpy(buf
, o
, v
->iov_len
);
2997 v
->iov_base
= (void *)buf
;
3001 status
= smb2_signing_encrypt_pdu(*encryption_key
,
3002 state
->conn
->protocol
,
3003 &iov
[tf_iov
], num_iov
- tf_iov
);
3004 if (!NT_STATUS_IS_OK(status
)) {
3009 if (state
->conn
->dispatch_incoming
== NULL
) {
3010 state
->conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
3013 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
3014 state
->conn
->write_fd
, false, iov
, num_iov
);
3015 if (subreq
== NULL
) {
3016 return NT_STATUS_NO_MEMORY
;
3018 tevent_req_set_callback(subreq
, smb2cli_req_writev_done
, reqs
[0]);
3019 return NT_STATUS_OK
;
3022 void smb2cli_req_set_credit_charge(struct tevent_req
*req
, uint16_t charge
)
3024 struct smbXcli_req_state
*state
=
3025 tevent_req_data(req
,
3026 struct smbXcli_req_state
);
3028 state
->smb2
.credit_charge
= charge
;
3031 struct tevent_req
*smb2cli_req_send(TALLOC_CTX
*mem_ctx
,
3032 struct tevent_context
*ev
,
3033 struct smbXcli_conn
*conn
,
3035 uint32_t additional_flags
,
3036 uint32_t clear_flags
,
3037 uint32_t timeout_msec
,
3038 struct smbXcli_tcon
*tcon
,
3039 struct smbXcli_session
*session
,
3040 const uint8_t *fixed
,
3044 uint32_t max_dyn_len
)
3046 struct tevent_req
*req
;
3049 req
= smb2cli_req_create(mem_ctx
, ev
, conn
, cmd
,
3050 additional_flags
, clear_flags
,
3059 if (!tevent_req_is_in_progress(req
)) {
3060 return tevent_req_post(req
, ev
);
3062 status
= smb2cli_req_compound_submit(&req
, 1);
3063 if (tevent_req_nterror(req
, status
)) {
3064 return tevent_req_post(req
, ev
);
3069 static void smb2cli_req_writev_done(struct tevent_req
*subreq
)
3071 struct tevent_req
*req
=
3072 tevent_req_callback_data(subreq
,
3074 struct smbXcli_req_state
*state
=
3075 tevent_req_data(req
,
3076 struct smbXcli_req_state
);
3080 nwritten
= writev_recv(subreq
, &err
);
3081 TALLOC_FREE(subreq
);
3082 if (nwritten
== -1) {
3083 /* here, we need to notify all pending requests */
3084 NTSTATUS status
= map_nt_error_from_unix_common(err
);
3085 smbXcli_conn_disconnect(state
->conn
, status
);
3090 static NTSTATUS
smb2cli_inbuf_parse_compound(struct smbXcli_conn
*conn
,
3093 TALLOC_CTX
*mem_ctx
,
3094 struct iovec
**piov
, int *pnum_iov
)
3099 uint8_t *first_hdr
= buf
;
3100 size_t verified_buflen
= 0;
3104 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
3106 return NT_STATUS_NO_MEMORY
;
3109 while (taken
< buflen
) {
3110 size_t len
= buflen
- taken
;
3111 uint8_t *hdr
= first_hdr
+ taken
;
3114 size_t next_command_ofs
;
3116 struct iovec
*iov_tmp
;
3118 if (verified_buflen
> taken
) {
3119 len
= verified_buflen
- taken
;
3126 DEBUG(10, ("%d bytes left, expected at least %d\n",
3130 if (IVAL(hdr
, 0) == SMB2_TF_MAGIC
) {
3131 struct smbXcli_session
*s
;
3133 struct iovec tf_iov
[2];
3137 if (len
< SMB2_TF_HDR_SIZE
) {
3138 DEBUG(10, ("%d bytes left, expected at least %d\n",
3139 (int)len
, SMB2_TF_HDR_SIZE
));
3143 tf_len
= SMB2_TF_HDR_SIZE
;
3146 hdr
= first_hdr
+ taken
;
3147 enc_len
= IVAL(tf
, SMB2_TF_MSG_SIZE
);
3148 uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
3150 if (len
< SMB2_TF_HDR_SIZE
+ enc_len
) {
3151 DEBUG(10, ("%d bytes left, expected at least %d\n",
3153 (int)(SMB2_TF_HDR_SIZE
+ enc_len
)));
3158 for (; s
; s
= s
->next
) {
3159 if (s
->smb2
->session_id
!= uid
) {
3166 DEBUG(10, ("unknown session_id %llu\n",
3167 (unsigned long long)uid
));
3171 tf_iov
[0].iov_base
= (void *)tf
;
3172 tf_iov
[0].iov_len
= tf_len
;
3173 tf_iov
[1].iov_base
= (void *)hdr
;
3174 tf_iov
[1].iov_len
= enc_len
;
3176 status
= smb2_signing_decrypt_pdu(s
->smb2
->decryption_key
,
3179 if (!NT_STATUS_IS_OK(status
)) {
3184 verified_buflen
= taken
+ enc_len
;
3189 * We need the header plus the body length field
3192 if (len
< SMB2_HDR_BODY
+ 2) {
3193 DEBUG(10, ("%d bytes left, expected at least %d\n",
3194 (int)len
, SMB2_HDR_BODY
));
3197 if (IVAL(hdr
, 0) != SMB2_MAGIC
) {
3198 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3202 if (SVAL(hdr
, 4) != SMB2_HDR_BODY
) {
3203 DEBUG(10, ("Got HDR len %d, expected %d\n",
3204 SVAL(hdr
, 4), SMB2_HDR_BODY
));
3209 next_command_ofs
= IVAL(hdr
, SMB2_HDR_NEXT_COMMAND
);
3210 body_size
= SVAL(hdr
, SMB2_HDR_BODY
);
3212 if (next_command_ofs
!= 0) {
3213 if (next_command_ofs
< (SMB2_HDR_BODY
+ 2)) {
3216 if (next_command_ofs
> full_size
) {
3219 full_size
= next_command_ofs
;
3221 if (body_size
< 2) {
3224 body_size
&= 0xfffe;
3226 if (body_size
> (full_size
- SMB2_HDR_BODY
)) {
3230 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
3232 if (iov_tmp
== NULL
) {
3234 return NT_STATUS_NO_MEMORY
;
3237 cur
= &iov
[num_iov
];
3240 cur
[0].iov_base
= tf
;
3241 cur
[0].iov_len
= tf_len
;
3242 cur
[1].iov_base
= hdr
;
3243 cur
[1].iov_len
= SMB2_HDR_BODY
;
3244 cur
[2].iov_base
= hdr
+ SMB2_HDR_BODY
;
3245 cur
[2].iov_len
= body_size
;
3246 cur
[3].iov_base
= hdr
+ SMB2_HDR_BODY
+ body_size
;
3247 cur
[3].iov_len
= full_size
- (SMB2_HDR_BODY
+ body_size
);
3253 *pnum_iov
= num_iov
;
3254 return NT_STATUS_OK
;
3258 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3261 static struct tevent_req
*smb2cli_conn_find_pending(struct smbXcli_conn
*conn
,
3264 size_t num_pending
= talloc_array_length(conn
->pending
);
3267 for (i
=0; i
<num_pending
; i
++) {
3268 struct tevent_req
*req
= conn
->pending
[i
];
3269 struct smbXcli_req_state
*state
=
3270 tevent_req_data(req
,
3271 struct smbXcli_req_state
);
3273 if (mid
== BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
)) {
3280 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
3281 TALLOC_CTX
*tmp_mem
,
3284 struct tevent_req
*req
;
3285 struct smbXcli_req_state
*state
= NULL
;
3290 struct smbXcli_session
*last_session
= NULL
;
3291 size_t inbuf_len
= smb_len_tcp(inbuf
);
3293 status
= smb2cli_inbuf_parse_compound(conn
,
3294 inbuf
+ NBT_HDR_SIZE
,
3298 if (!NT_STATUS_IS_OK(status
)) {
3302 for (i
=0; i
<num_iov
; i
+=4) {
3303 uint8_t *inbuf_ref
= NULL
;
3304 struct iovec
*cur
= &iov
[i
];
3305 uint8_t *inhdr
= (uint8_t *)cur
[1].iov_base
;
3306 uint16_t opcode
= SVAL(inhdr
, SMB2_HDR_OPCODE
);
3307 uint32_t flags
= IVAL(inhdr
, SMB2_HDR_FLAGS
);
3308 uint64_t mid
= BVAL(inhdr
, SMB2_HDR_MESSAGE_ID
);
3309 uint16_t req_opcode
;
3311 uint16_t credits
= SVAL(inhdr
, SMB2_HDR_CREDIT
);
3312 uint32_t new_credits
;
3313 struct smbXcli_session
*session
= NULL
;
3314 const DATA_BLOB
*signing_key
= NULL
;
3315 bool was_encrypted
= false;
3317 new_credits
= conn
->smb2
.cur_credits
;
3318 new_credits
+= credits
;
3319 if (new_credits
> UINT16_MAX
) {
3320 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3322 conn
->smb2
.cur_credits
+= credits
;
3324 req
= smb2cli_conn_find_pending(conn
, mid
);
3326 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3328 state
= tevent_req_data(req
, struct smbXcli_req_state
);
3330 state
->smb2
.got_async
= false;
3332 req_opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
3333 if (opcode
!= req_opcode
) {
3334 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3336 req_flags
= SVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
3338 if (!(flags
& SMB2_HDR_FLAG_REDIRECT
)) {
3339 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3342 status
= NT_STATUS(IVAL(inhdr
, SMB2_HDR_STATUS
));
3343 if ((flags
& SMB2_HDR_FLAG_ASYNC
) &&
3344 NT_STATUS_EQUAL(status
, STATUS_PENDING
)) {
3345 uint64_t async_id
= BVAL(inhdr
, SMB2_HDR_ASYNC_ID
);
3348 * async interim responses are not signed,
3349 * even if the SMB2_HDR_FLAG_SIGNED flag
3352 req_flags
|= SMB2_HDR_FLAG_ASYNC
;
3353 SBVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, req_flags
);
3354 SBVAL(state
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, async_id
);
3356 if (state
->smb2
.notify_async
) {
3357 state
->smb2
.got_async
= true;
3358 tevent_req_defer_callback(req
, state
->ev
);
3359 tevent_req_notify_callback(req
);
3364 session
= state
->session
;
3365 if (req_flags
& SMB2_HDR_FLAG_CHAINED
) {
3366 session
= last_session
;
3368 last_session
= session
;
3370 if (state
->smb2
.should_sign
) {
3371 if (!(flags
& SMB2_HDR_FLAG_SIGNED
)) {
3372 return NT_STATUS_ACCESS_DENIED
;
3376 if (flags
& SMB2_HDR_FLAG_SIGNED
) {
3377 uint64_t uid
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
3379 if (session
== NULL
) {
3380 struct smbXcli_session
*s
;
3382 s
= state
->conn
->sessions
;
3383 for (; s
; s
= s
->next
) {
3384 if (s
->smb2
->session_id
!= uid
) {
3393 if (session
== NULL
) {
3394 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3397 last_session
= session
;
3398 signing_key
= &session
->smb2_channel
.signing_key
;
3401 if (opcode
== SMB2_OP_SESSSETUP
) {
3403 * We prefer the channel signing key, if it is
3406 * If we do not have a channel signing key yet,
3407 * we try the main signing key, if it is not
3408 * the final response.
3410 if (signing_key
&& signing_key
->length
== 0 &&
3411 !NT_STATUS_IS_OK(status
)) {
3412 signing_key
= &session
->smb2
->signing_key
;
3415 if (signing_key
&& signing_key
->length
== 0) {
3417 * If we do not have a session key to
3418 * verify the signature, we defer the
3419 * signing check to the caller.
3421 * The caller gets NT_STATUS_OK, it
3423 * smb2cli_session_set_session_key()
3425 * smb2cli_session_set_channel_key()
3426 * which will check the signature
3427 * with the channel signing key.
3433 if (cur
[0].iov_len
== SMB2_TF_HDR_SIZE
) {
3434 const uint8_t *tf
= (const uint8_t *)cur
[0].iov_base
;
3435 uint64_t uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
3438 * If the response was encrypted in a SMB2_TRANSFORM
3439 * pdu, which belongs to the correct session,
3440 * we do not need to do signing checks
3442 * It could be the session the response belongs to
3443 * or the session that was used to encrypt the
3444 * SMB2_TRANSFORM request.
3446 if ((session
&& session
->smb2
->session_id
== uid
) ||
3447 (state
->smb2
.encryption_session_id
== uid
)) {
3449 was_encrypted
= true;
3453 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_SESSION_DELETED
)) {
3455 * if the server returns NT_STATUS_USER_SESSION_DELETED
3456 * the response is not signed and we should
3457 * propagate the NT_STATUS_USER_SESSION_DELETED
3458 * status to the caller.
3460 state
->smb2
.signing_skipped
= true;
3464 if (NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
3466 * if the server returns
3467 * NT_STATUS_INVALID_PARAMETER
3468 * the response might not be encrypted.
3470 if (state
->smb2
.should_encrypt
&& !was_encrypted
) {
3471 state
->smb2
.signing_skipped
= true;
3476 if (state
->smb2
.should_encrypt
&& !was_encrypted
) {
3477 if (!state
->smb2
.signing_skipped
) {
3478 return NT_STATUS_ACCESS_DENIED
;
3482 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_NAME_DELETED
) ||
3483 NT_STATUS_EQUAL(status
, NT_STATUS_FILE_CLOSED
) ||
3484 NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
3486 * if the server returns
3487 * NT_STATUS_NETWORK_NAME_DELETED
3488 * NT_STATUS_FILE_CLOSED
3489 * NT_STATUS_INVALID_PARAMETER
3490 * the response might not be signed
3491 * as this happens before the signing checks.
3493 * If server echos the signature (or all zeros)
3494 * we should report the status from the server
3500 cmp
= memcmp(inhdr
+SMB2_HDR_SIGNATURE
,
3501 state
->smb2
.hdr
+SMB2_HDR_SIGNATURE
,
3504 state
->smb2
.signing_skipped
= true;
3510 static const uint8_t zeros
[16];
3512 cmp
= memcmp(inhdr
+SMB2_HDR_SIGNATURE
,
3516 state
->smb2
.signing_skipped
= true;
3523 status
= smb2_signing_check_pdu(*signing_key
,
3524 state
->conn
->protocol
,
3526 if (!NT_STATUS_IS_OK(status
)) {
3528 * If the signing check fails, we disconnect
3535 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_SESSION_EXPIRED
) &&
3536 (session
!= NULL
) && session
->disconnect_expired
)
3539 * this should be a short term hack
3540 * until the upper layers have implemented
3541 * re-authentication.
3546 smbXcli_req_unset_pending(req
);
3549 * There might be more than one response
3550 * we need to defer the notifications
3552 if ((num_iov
== 5) && (talloc_array_length(conn
->pending
) == 0)) {
3557 tevent_req_defer_callback(req
, state
->ev
);
3561 * Note: here we use talloc_reference() in a way
3562 * that does not expose it to the caller.
3564 inbuf_ref
= talloc_reference(state
->smb2
.recv_iov
, inbuf
);
3565 if (tevent_req_nomem(inbuf_ref
, req
)) {
3569 /* copy the related buffers */
3570 state
->smb2
.recv_iov
[0] = cur
[1];
3571 state
->smb2
.recv_iov
[1] = cur
[2];
3572 state
->smb2
.recv_iov
[2] = cur
[3];
3574 tevent_req_done(req
);
3578 return NT_STATUS_RETRY
;
3581 return NT_STATUS_OK
;
3584 NTSTATUS
smb2cli_req_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
3585 struct iovec
**piov
,
3586 const struct smb2cli_req_expected_response
*expected
,
3587 size_t num_expected
)
3589 struct smbXcli_req_state
*state
=
3590 tevent_req_data(req
,
3591 struct smbXcli_req_state
);
3594 bool found_status
= false;
3595 bool found_size
= false;
3602 if (state
->smb2
.got_async
) {
3603 return STATUS_PENDING
;
3606 if (tevent_req_is_nterror(req
, &status
)) {
3607 for (i
=0; i
< num_expected
; i
++) {
3608 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
3609 found_status
= true;
3615 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
3621 if (num_expected
== 0) {
3622 found_status
= true;
3626 status
= NT_STATUS(IVAL(state
->smb2
.recv_iov
[0].iov_base
, SMB2_HDR_STATUS
));
3627 body_size
= SVAL(state
->smb2
.recv_iov
[1].iov_base
, 0);
3629 for (i
=0; i
< num_expected
; i
++) {
3630 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
3634 found_status
= true;
3635 if (expected
[i
].body_size
== 0) {
3640 if (expected
[i
].body_size
== body_size
) {
3646 if (!found_status
) {
3650 if (state
->smb2
.signing_skipped
) {
3651 if (num_expected
> 0) {
3652 return NT_STATUS_ACCESS_DENIED
;
3654 if (!NT_STATUS_IS_ERR(status
)) {
3655 return NT_STATUS_ACCESS_DENIED
;
3660 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3664 *piov
= talloc_move(mem_ctx
, &state
->smb2
.recv_iov
);
3670 static const struct {
3671 enum protocol_types proto
;
3672 const char *smb1_name
;
3673 } smb1cli_prots
[] = {
3674 {PROTOCOL_CORE
, "PC NETWORK PROGRAM 1.0"},
3675 {PROTOCOL_COREPLUS
, "MICROSOFT NETWORKS 1.03"},
3676 {PROTOCOL_LANMAN1
, "MICROSOFT NETWORKS 3.0"},
3677 {PROTOCOL_LANMAN1
, "LANMAN1.0"},
3678 {PROTOCOL_LANMAN2
, "LM1.2X002"},
3679 {PROTOCOL_LANMAN2
, "DOS LANMAN2.1"},
3680 {PROTOCOL_LANMAN2
, "LANMAN2.1"},
3681 {PROTOCOL_LANMAN2
, "Samba"},
3682 {PROTOCOL_NT1
, "NT LANMAN 1.0"},
3683 {PROTOCOL_NT1
, "NT LM 0.12"},
3684 {PROTOCOL_SMB2_02
, "SMB 2.002"},
3685 {PROTOCOL_SMB2_10
, "SMB 2.???"},
3688 static const struct {
3689 enum protocol_types proto
;
3690 uint16_t smb2_dialect
;
3691 } smb2cli_prots
[] = {
3692 {PROTOCOL_SMB2_02
, SMB2_DIALECT_REVISION_202
},
3693 {PROTOCOL_SMB2_10
, SMB2_DIALECT_REVISION_210
},
3694 {PROTOCOL_SMB2_22
, SMB2_DIALECT_REVISION_222
},
3695 {PROTOCOL_SMB2_24
, SMB2_DIALECT_REVISION_224
},
3696 {PROTOCOL_SMB3_00
, SMB3_DIALECT_REVISION_300
},
3697 {PROTOCOL_SMB3_02
, SMB3_DIALECT_REVISION_302
},
3700 struct smbXcli_negprot_state
{
3701 struct smbXcli_conn
*conn
;
3702 struct tevent_context
*ev
;
3703 uint32_t timeout_msec
;
3704 enum protocol_types min_protocol
;
3705 enum protocol_types max_protocol
;
3709 uint8_t dyn
[ARRAY_SIZE(smb2cli_prots
)*2];
3713 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
);
3714 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
);
3715 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
);
3716 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
);
3717 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
);
3718 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
3722 struct tevent_req
*smbXcli_negprot_send(TALLOC_CTX
*mem_ctx
,
3723 struct tevent_context
*ev
,
3724 struct smbXcli_conn
*conn
,
3725 uint32_t timeout_msec
,
3726 enum protocol_types min_protocol
,
3727 enum protocol_types max_protocol
)
3729 struct tevent_req
*req
, *subreq
;
3730 struct smbXcli_negprot_state
*state
;
3732 req
= tevent_req_create(mem_ctx
, &state
,
3733 struct smbXcli_negprot_state
);
3739 state
->timeout_msec
= timeout_msec
;
3740 state
->min_protocol
= min_protocol
;
3741 state
->max_protocol
= max_protocol
;
3743 if (min_protocol
== PROTOCOL_NONE
) {
3744 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3745 return tevent_req_post(req
, ev
);
3748 if (max_protocol
== PROTOCOL_NONE
) {
3749 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3750 return tevent_req_post(req
, ev
);
3753 if (min_protocol
> max_protocol
) {
3754 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3755 return tevent_req_post(req
, ev
);
3758 if ((min_protocol
< PROTOCOL_SMB2_02
) &&
3759 (max_protocol
< PROTOCOL_SMB2_02
)) {
3763 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
3765 subreq
= smbXcli_negprot_smb1_subreq(state
);
3766 if (tevent_req_nomem(subreq
, req
)) {
3767 return tevent_req_post(req
, ev
);
3769 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
3773 if ((min_protocol
>= PROTOCOL_SMB2_02
) &&
3774 (max_protocol
>= PROTOCOL_SMB2_02
)) {
3778 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
3780 subreq
= smbXcli_negprot_smb2_subreq(state
);
3781 if (tevent_req_nomem(subreq
, req
)) {
3782 return tevent_req_post(req
, ev
);
3784 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
3789 * We send an SMB1 negprot with the SMB2 dialects
3790 * and expect a SMB1 or a SMB2 response.
3792 * smbXcli_negprot_dispatch_incoming() will fix the
3793 * callback to match protocol of the response.
3795 conn
->dispatch_incoming
= smbXcli_negprot_dispatch_incoming
;
3797 subreq
= smbXcli_negprot_smb1_subreq(state
);
3798 if (tevent_req_nomem(subreq
, req
)) {
3799 return tevent_req_post(req
, ev
);
3801 tevent_req_set_callback(subreq
, smbXcli_negprot_invalid_done
, req
);
3805 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
)
3807 struct tevent_req
*req
=
3808 tevent_req_callback_data(subreq
,
3813 * we just want the low level error
3815 status
= tevent_req_simple_recv_ntstatus(subreq
);
3816 TALLOC_FREE(subreq
);
3817 if (tevent_req_nterror(req
, status
)) {
3821 /* this should never happen */
3822 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
3825 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
)
3828 DATA_BLOB bytes
= data_blob_null
;
3832 /* setup the protocol strings */
3833 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
3837 if (smb1cli_prots
[i
].proto
< state
->min_protocol
) {
3841 if (smb1cli_prots
[i
].proto
> state
->max_protocol
) {
3845 ok
= data_blob_append(state
, &bytes
, &c
, sizeof(c
));
3851 * We now it is already ascii and
3852 * we want NULL termination.
3854 ok
= data_blob_append(state
, &bytes
,
3855 smb1cli_prots
[i
].smb1_name
,
3856 strlen(smb1cli_prots
[i
].smb1_name
)+1);
3862 smb1cli_req_flags(state
->max_protocol
,
3863 state
->conn
->smb1
.client
.capabilities
,
3868 return smb1cli_req_send(state
, state
->ev
, state
->conn
,
3872 state
->timeout_msec
,
3873 0xFFFE, 0, NULL
, /* pid, tid, session */
3874 0, NULL
, /* wct, vwv */
3875 bytes
.length
, bytes
.data
);
3878 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
)
3880 struct tevent_req
*req
=
3881 tevent_req_callback_data(subreq
,
3883 struct smbXcli_negprot_state
*state
=
3884 tevent_req_data(req
,
3885 struct smbXcli_negprot_state
);
3886 struct smbXcli_conn
*conn
= state
->conn
;
3887 struct iovec
*recv_iov
= NULL
;
3896 size_t num_prots
= 0;
3898 uint32_t client_capabilities
= conn
->smb1
.client
.capabilities
;
3899 uint32_t both_capabilities
;
3900 uint32_t server_capabilities
= 0;
3901 uint32_t capabilities
;
3902 uint32_t client_max_xmit
= conn
->smb1
.client
.max_xmit
;
3903 uint32_t server_max_xmit
= 0;
3905 uint32_t server_max_mux
= 0;
3906 uint16_t server_security_mode
= 0;
3907 uint32_t server_session_key
= 0;
3908 bool server_readbraw
= false;
3909 bool server_writebraw
= false;
3910 bool server_lockread
= false;
3911 bool server_writeunlock
= false;
3912 struct GUID server_guid
= GUID_zero();
3913 DATA_BLOB server_gss_blob
= data_blob_null
;
3914 uint8_t server_challenge
[8];
3915 char *server_workgroup
= NULL
;
3916 char *server_name
= NULL
;
3917 int server_time_zone
= 0;
3918 NTTIME server_system_time
= 0;
3919 static const struct smb1cli_req_expected_response expected
[] = {
3921 .status
= NT_STATUS_OK
,
3922 .wct
= 0x11, /* NT1 */
3925 .status
= NT_STATUS_OK
,
3926 .wct
= 0x0D, /* LM */
3929 .status
= NT_STATUS_OK
,
3930 .wct
= 0x01, /* CORE */
3934 ZERO_STRUCT(server_challenge
);
3936 status
= smb1cli_req_recv(subreq
, state
,
3941 NULL
, /* pvwv_offset */
3944 NULL
, /* pbytes_offset */
3946 expected
, ARRAY_SIZE(expected
));
3947 TALLOC_FREE(subreq
);
3948 if (tevent_req_nterror(req
, status
)) {
3952 flags
= CVAL(inhdr
, HDR_FLG
);
3954 protnum
= SVAL(vwv
, 0);
3956 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
3957 if (smb1cli_prots
[i
].proto
< state
->min_protocol
) {
3961 if (smb1cli_prots
[i
].proto
> state
->max_protocol
) {
3965 if (protnum
!= num_prots
) {
3970 conn
->protocol
= smb1cli_prots
[i
].proto
;
3974 if (conn
->protocol
== PROTOCOL_NONE
) {
3975 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3979 if ((conn
->protocol
< PROTOCOL_NT1
) && conn
->mandatory_signing
) {
3980 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3981 "and the selected protocol level doesn't support it.\n"));
3982 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
3986 if (flags
& FLAG_SUPPORT_LOCKREAD
) {
3987 server_lockread
= true;
3988 server_writeunlock
= true;
3991 if (conn
->protocol
>= PROTOCOL_NT1
) {
3992 const char *client_signing
= NULL
;
3993 bool server_mandatory
= false;
3994 bool server_allowed
= false;
3995 const char *server_signing
= NULL
;
4000 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4005 server_security_mode
= CVAL(vwv
+ 1, 0);
4006 server_max_mux
= SVAL(vwv
+ 1, 1);
4007 server_max_xmit
= IVAL(vwv
+ 3, 1);
4008 server_session_key
= IVAL(vwv
+ 7, 1);
4009 server_time_zone
= SVALS(vwv
+ 15, 1);
4010 server_time_zone
*= 60;
4011 /* this time arrives in real GMT */
4012 server_system_time
= BVAL(vwv
+ 11, 1);
4013 server_capabilities
= IVAL(vwv
+ 9, 1);
4015 key_len
= CVAL(vwv
+ 16, 1);
4017 if (server_capabilities
& CAP_RAW_MODE
) {
4018 server_readbraw
= true;
4019 server_writebraw
= true;
4021 if (server_capabilities
& CAP_LOCK_AND_READ
) {
4022 server_lockread
= true;
4025 if (server_capabilities
& CAP_EXTENDED_SECURITY
) {
4026 DATA_BLOB blob1
, blob2
;
4028 if (num_bytes
< 16) {
4029 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4033 blob1
= data_blob_const(bytes
, 16);
4034 status
= GUID_from_data_blob(&blob1
, &server_guid
);
4035 if (tevent_req_nterror(req
, status
)) {
4039 blob1
= data_blob_const(bytes
+16, num_bytes
-16);
4040 blob2
= data_blob_dup_talloc(state
, blob1
);
4041 if (blob1
.length
> 0 &&
4042 tevent_req_nomem(blob2
.data
, req
)) {
4045 server_gss_blob
= blob2
;
4047 DATA_BLOB blob1
, blob2
;
4049 if (num_bytes
< key_len
) {
4050 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4054 if (key_len
!= 0 && key_len
!= 8) {
4055 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4060 memcpy(server_challenge
, bytes
, 8);
4063 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4064 blob2
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4065 if (blob1
.length
> 0) {
4068 len
= utf16_len_n(blob1
.data
,
4072 ok
= convert_string_talloc(state
,
4080 status
= map_nt_error_from_unix_common(errno
);
4081 tevent_req_nterror(req
, status
);
4086 blob2
.data
+= blob1
.length
;
4087 blob2
.length
-= blob1
.length
;
4088 if (blob2
.length
> 0) {
4091 len
= utf16_len_n(blob1
.data
,
4095 ok
= convert_string_talloc(state
,
4103 status
= map_nt_error_from_unix_common(errno
);
4104 tevent_req_nterror(req
, status
);
4110 client_signing
= "disabled";
4111 if (conn
->allow_signing
) {
4112 client_signing
= "allowed";
4114 if (conn
->mandatory_signing
) {
4115 client_signing
= "required";
4118 server_signing
= "not supported";
4119 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_ENABLED
) {
4120 server_signing
= "supported";
4121 server_allowed
= true;
4122 } else if (conn
->mandatory_signing
) {
4124 * We have mandatory signing as client
4125 * lets assume the server will look at our
4126 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4127 * flag in the session setup
4129 server_signing
= "not announced";
4130 server_allowed
= true;
4132 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_REQUIRED
) {
4133 server_signing
= "required";
4134 server_mandatory
= true;
4137 ok
= smb_signing_set_negotiated(conn
->smb1
.signing
,
4141 DEBUG(1,("cli_negprot: SMB signing is required, "
4142 "but client[%s] and server[%s] mismatch\n",
4143 client_signing
, server_signing
));
4144 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
4148 } else if (conn
->protocol
>= PROTOCOL_LANMAN1
) {
4154 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4158 server_security_mode
= SVAL(vwv
+ 1, 0);
4159 server_max_xmit
= SVAL(vwv
+ 2, 0);
4160 server_max_mux
= SVAL(vwv
+ 3, 0);
4161 server_readbraw
= ((SVAL(vwv
+ 5, 0) & 0x1) != 0);
4162 server_writebraw
= ((SVAL(vwv
+ 5, 0) & 0x2) != 0);
4163 server_session_key
= IVAL(vwv
+ 6, 0);
4164 server_time_zone
= SVALS(vwv
+ 10, 0);
4165 server_time_zone
*= 60;
4166 /* this time is converted to GMT by make_unix_date */
4167 t
= pull_dos_date((const uint8_t *)(vwv
+ 8), server_time_zone
);
4168 unix_to_nt_time(&server_system_time
, t
);
4169 key_len
= SVAL(vwv
+ 11, 0);
4171 if (num_bytes
< key_len
) {
4172 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4176 if (key_len
!= 0 && key_len
!= 8) {
4177 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4182 memcpy(server_challenge
, bytes
, 8);
4185 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
4186 if (blob1
.length
> 0) {
4190 len
= utf16_len_n(blob1
.data
,
4194 ok
= convert_string_talloc(state
,
4202 status
= map_nt_error_from_unix_common(errno
);
4203 tevent_req_nterror(req
, status
);
4209 /* the old core protocol */
4210 server_time_zone
= get_time_zone(time(NULL
));
4211 server_max_xmit
= 1024;
4215 if (server_max_xmit
< 1024) {
4216 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4220 if (server_max_mux
< 1) {
4221 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4226 * Now calculate the negotiated capabilities
4227 * based on the mask for:
4228 * - client only flags
4229 * - flags used in both directions
4230 * - server only flags
4232 both_capabilities
= client_capabilities
& server_capabilities
;
4233 capabilities
= client_capabilities
& SMB_CAP_CLIENT_MASK
;
4234 capabilities
|= both_capabilities
& SMB_CAP_BOTH_MASK
;
4235 capabilities
|= server_capabilities
& SMB_CAP_SERVER_MASK
;
4237 max_xmit
= MIN(client_max_xmit
, server_max_xmit
);
4239 conn
->smb1
.server
.capabilities
= server_capabilities
;
4240 conn
->smb1
.capabilities
= capabilities
;
4242 conn
->smb1
.server
.max_xmit
= server_max_xmit
;
4243 conn
->smb1
.max_xmit
= max_xmit
;
4245 conn
->smb1
.server
.max_mux
= server_max_mux
;
4247 conn
->smb1
.server
.security_mode
= server_security_mode
;
4249 conn
->smb1
.server
.readbraw
= server_readbraw
;
4250 conn
->smb1
.server
.writebraw
= server_writebraw
;
4251 conn
->smb1
.server
.lockread
= server_lockread
;
4252 conn
->smb1
.server
.writeunlock
= server_writeunlock
;
4254 conn
->smb1
.server
.session_key
= server_session_key
;
4256 talloc_steal(conn
, server_gss_blob
.data
);
4257 conn
->smb1
.server
.gss_blob
= server_gss_blob
;
4258 conn
->smb1
.server
.guid
= server_guid
;
4259 memcpy(conn
->smb1
.server
.challenge
, server_challenge
, 8);
4260 conn
->smb1
.server
.workgroup
= talloc_move(conn
, &server_workgroup
);
4261 conn
->smb1
.server
.name
= talloc_move(conn
, &server_name
);
4263 conn
->smb1
.server
.time_zone
= server_time_zone
;
4264 conn
->smb1
.server
.system_time
= server_system_time
;
4266 tevent_req_done(req
);
4269 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
)
4273 uint16_t dialect_count
= 0;
4275 buf
= state
->smb2
.dyn
;
4276 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4277 if (smb2cli_prots
[i
].proto
< state
->min_protocol
) {
4281 if (smb2cli_prots
[i
].proto
> state
->max_protocol
) {
4285 SSVAL(buf
, dialect_count
*2, smb2cli_prots
[i
].smb2_dialect
);
4289 buf
= state
->smb2
.fixed
;
4291 SSVAL(buf
, 2, dialect_count
);
4292 SSVAL(buf
, 4, state
->conn
->smb2
.client
.security_mode
);
4293 SSVAL(buf
, 6, 0); /* Reserved */
4294 if (state
->max_protocol
>= PROTOCOL_SMB2_22
) {
4295 SIVAL(buf
, 8, state
->conn
->smb2
.client
.capabilities
);
4297 SIVAL(buf
, 8, 0); /* Capabilities */
4299 if (state
->max_protocol
>= PROTOCOL_SMB2_10
) {
4303 status
= GUID_to_ndr_blob(&state
->conn
->smb2
.client
.guid
,
4305 if (!NT_STATUS_IS_OK(status
)) {
4308 memcpy(buf
+12, blob
.data
, 16); /* ClientGuid */
4310 memset(buf
+12, 0, 16); /* ClientGuid */
4312 SBVAL(buf
, 28, 0); /* ClientStartTime */
4314 return smb2cli_req_send(state
, state
->ev
,
4315 state
->conn
, SMB2_OP_NEGPROT
,
4317 state
->timeout_msec
,
4318 NULL
, NULL
, /* tcon, session */
4319 state
->smb2
.fixed
, sizeof(state
->smb2
.fixed
),
4320 state
->smb2
.dyn
, dialect_count
*2,
4321 UINT16_MAX
); /* max_dyn_len */
4324 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
)
4326 struct tevent_req
*req
=
4327 tevent_req_callback_data(subreq
,
4329 struct smbXcli_negprot_state
*state
=
4330 tevent_req_data(req
,
4331 struct smbXcli_negprot_state
);
4332 struct smbXcli_conn
*conn
= state
->conn
;
4333 size_t security_offset
, security_length
;
4339 uint16_t dialect_revision
;
4340 static const struct smb2cli_req_expected_response expected
[] = {
4342 .status
= NT_STATUS_OK
,
4347 status
= smb2cli_req_recv(subreq
, state
, &iov
,
4348 expected
, ARRAY_SIZE(expected
));
4349 TALLOC_FREE(subreq
);
4350 if (tevent_req_nterror(req
, status
)) {
4354 body
= (uint8_t *)iov
[1].iov_base
;
4356 dialect_revision
= SVAL(body
, 4);
4358 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4359 if (smb2cli_prots
[i
].proto
< state
->min_protocol
) {
4363 if (smb2cli_prots
[i
].proto
> state
->max_protocol
) {
4367 if (smb2cli_prots
[i
].smb2_dialect
!= dialect_revision
) {
4371 conn
->protocol
= smb2cli_prots
[i
].proto
;
4375 if (conn
->protocol
== PROTOCOL_NONE
) {
4376 if (state
->min_protocol
>= PROTOCOL_SMB2_02
) {
4377 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4381 if (dialect_revision
!= SMB2_DIALECT_REVISION_2FF
) {
4382 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4386 /* make sure we do not loop forever */
4387 state
->min_protocol
= PROTOCOL_SMB2_02
;
4390 * send a SMB2 negprot, in order to negotiate
4393 subreq
= smbXcli_negprot_smb2_subreq(state
);
4394 if (tevent_req_nomem(subreq
, req
)) {
4397 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4401 conn
->smb2
.server
.security_mode
= SVAL(body
, 2);
4403 blob
= data_blob_const(body
+ 8, 16);
4404 status
= GUID_from_data_blob(&blob
, &conn
->smb2
.server
.guid
);
4405 if (tevent_req_nterror(req
, status
)) {
4409 conn
->smb2
.server
.capabilities
= IVAL(body
, 24);
4410 conn
->smb2
.server
.max_trans_size
= IVAL(body
, 28);
4411 conn
->smb2
.server
.max_read_size
= IVAL(body
, 32);
4412 conn
->smb2
.server
.max_write_size
= IVAL(body
, 36);
4413 conn
->smb2
.server
.system_time
= BVAL(body
, 40);
4414 conn
->smb2
.server
.start_time
= BVAL(body
, 48);
4416 security_offset
= SVAL(body
, 56);
4417 security_length
= SVAL(body
, 58);
4419 if (security_offset
!= SMB2_HDR_BODY
+ iov
[1].iov_len
) {
4420 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4424 if (security_length
> iov
[2].iov_len
) {
4425 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4429 conn
->smb2
.server
.gss_blob
= data_blob_talloc(conn
,
4432 if (tevent_req_nomem(conn
->smb2
.server
.gss_blob
.data
, req
)) {
4436 tevent_req_done(req
);
4439 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
4440 TALLOC_CTX
*tmp_mem
,
4443 size_t num_pending
= talloc_array_length(conn
->pending
);
4444 struct tevent_req
*subreq
;
4445 struct smbXcli_req_state
*substate
;
4446 struct tevent_req
*req
;
4447 uint32_t protocol_magic
;
4448 size_t inbuf_len
= smb_len_nbt(inbuf
);
4450 if (num_pending
!= 1) {
4451 return NT_STATUS_INTERNAL_ERROR
;
4454 if (inbuf_len
< 4) {
4455 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4458 subreq
= conn
->pending
[0];
4459 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
4460 req
= tevent_req_callback_data(subreq
, struct tevent_req
);
4462 protocol_magic
= IVAL(inbuf
, 4);
4464 switch (protocol_magic
) {
4466 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
4467 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
4468 return smb1cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
4471 if (substate
->smb2
.recv_iov
== NULL
) {
4473 * For the SMB1 negprot we have move it.
4475 substate
->smb2
.recv_iov
= substate
->smb1
.recv_iov
;
4476 substate
->smb1
.recv_iov
= NULL
;
4480 * we got an SMB2 answer, which consumed sequence number 0
4481 * so we need to use 1 as the next one.
4483 * we also need to set the current credits to 0
4484 * as we consumed the initial one. The SMB2 answer
4485 * hopefully grant us a new credit.
4488 conn
->smb2
.cur_credits
= 0;
4489 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4490 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
4491 return smb2cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
4494 DEBUG(10, ("Got non-SMB PDU\n"));
4495 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4498 NTSTATUS
smbXcli_negprot_recv(struct tevent_req
*req
)
4500 return tevent_req_simple_recv_ntstatus(req
);
4503 NTSTATUS
smbXcli_negprot(struct smbXcli_conn
*conn
,
4504 uint32_t timeout_msec
,
4505 enum protocol_types min_protocol
,
4506 enum protocol_types max_protocol
)
4508 TALLOC_CTX
*frame
= talloc_stackframe();
4509 struct tevent_context
*ev
;
4510 struct tevent_req
*req
;
4511 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4514 if (smbXcli_conn_has_async_calls(conn
)) {
4516 * Can't use sync call while an async call is in flight
4518 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
4521 ev
= samba_tevent_context_init(frame
);
4525 req
= smbXcli_negprot_send(frame
, ev
, conn
, timeout_msec
,
4526 min_protocol
, max_protocol
);
4530 ok
= tevent_req_poll(req
, ev
);
4532 status
= map_nt_error_from_unix_common(errno
);
4535 status
= smbXcli_negprot_recv(req
);
4541 static int smbXcli_session_destructor(struct smbXcli_session
*session
)
4543 if (session
->conn
== NULL
) {
4547 DLIST_REMOVE(session
->conn
->sessions
, session
);
4551 struct smbXcli_session
*smbXcli_session_create(TALLOC_CTX
*mem_ctx
,
4552 struct smbXcli_conn
*conn
)
4554 struct smbXcli_session
*session
;
4556 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
4557 if (session
== NULL
) {
4560 session
->smb2
= talloc_zero(session
, struct smb2cli_session
);
4561 if (session
->smb2
== NULL
) {
4562 talloc_free(session
);
4565 talloc_set_destructor(session
, smbXcli_session_destructor
);
4567 DLIST_ADD_END(conn
->sessions
, session
, struct smbXcli_session
*);
4568 session
->conn
= conn
;
4573 struct smbXcli_session
*smbXcli_session_copy(TALLOC_CTX
*mem_ctx
,
4574 struct smbXcli_session
*src
)
4576 struct smbXcli_session
*session
;
4578 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
4579 if (session
== NULL
) {
4582 session
->smb2
= talloc_zero(session
, struct smb2cli_session
);
4583 if (session
->smb2
== NULL
) {
4584 talloc_free(session
);
4588 session
->conn
= src
->conn
;
4589 *session
->smb2
= *src
->smb2
;
4590 session
->smb2_channel
= src
->smb2_channel
;
4591 session
->disconnect_expired
= src
->disconnect_expired
;
4593 DLIST_ADD_END(src
->conn
->sessions
, session
, struct smbXcli_session
*);
4594 talloc_set_destructor(session
, smbXcli_session_destructor
);
4600 NTSTATUS
smbXcli_session_application_key(struct smbXcli_session
*session
,
4601 TALLOC_CTX
*mem_ctx
,
4604 const DATA_BLOB
*application_key
;
4606 *key
= data_blob_null
;
4608 if (session
->conn
== NULL
) {
4609 return NT_STATUS_NO_USER_SESSION_KEY
;
4612 if (session
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
4613 application_key
= &session
->smb2
->application_key
;
4615 application_key
= &session
->smb1
.application_key
;
4618 if (application_key
->length
== 0) {
4619 return NT_STATUS_NO_USER_SESSION_KEY
;
4622 *key
= data_blob_dup_talloc(mem_ctx
, *application_key
);
4623 if (key
->data
== NULL
) {
4624 return NT_STATUS_NO_MEMORY
;
4627 return NT_STATUS_OK
;
4630 void smbXcli_session_set_disconnect_expired(struct smbXcli_session
*session
)
4632 session
->disconnect_expired
= true;
4635 uint16_t smb1cli_session_current_id(struct smbXcli_session
*session
)
4637 return session
->smb1
.session_id
;
4640 void smb1cli_session_set_id(struct smbXcli_session
*session
,
4641 uint16_t session_id
)
4643 session
->smb1
.session_id
= session_id
;
4646 NTSTATUS
smb1cli_session_set_session_key(struct smbXcli_session
*session
,
4647 const DATA_BLOB _session_key
)
4649 struct smbXcli_conn
*conn
= session
->conn
;
4650 uint8_t session_key
[16];
4653 return NT_STATUS_INVALID_PARAMETER_MIX
;
4656 if (session
->smb1
.application_key
.length
!= 0) {
4658 * TODO: do not allow this...
4660 * return NT_STATUS_INVALID_PARAMETER_MIX;
4662 data_blob_clear_free(&session
->smb1
.application_key
);
4663 session
->smb1
.protected_key
= false;
4666 if (_session_key
.length
== 0) {
4667 return NT_STATUS_OK
;
4670 ZERO_STRUCT(session_key
);
4671 memcpy(session_key
, _session_key
.data
,
4672 MIN(_session_key
.length
, sizeof(session_key
)));
4674 session
->smb1
.application_key
= data_blob_talloc(session
,
4676 sizeof(session_key
));
4677 ZERO_STRUCT(session_key
);
4678 if (session
->smb1
.application_key
.data
== NULL
) {
4679 return NT_STATUS_NO_MEMORY
;
4682 session
->smb1
.protected_key
= false;
4684 return NT_STATUS_OK
;
4687 NTSTATUS
smb1cli_session_protect_session_key(struct smbXcli_session
*session
)
4689 if (session
->smb1
.protected_key
) {
4690 /* already protected */
4691 return NT_STATUS_OK
;
4694 if (session
->smb1
.application_key
.length
!= 16) {
4695 return NT_STATUS_INVALID_PARAMETER_MIX
;
4698 smb_key_derivation(session
->smb1
.application_key
.data
,
4699 session
->smb1
.application_key
.length
,
4700 session
->smb1
.application_key
.data
);
4702 session
->smb1
.protected_key
= true;
4704 return NT_STATUS_OK
;
4707 uint8_t smb2cli_session_security_mode(struct smbXcli_session
*session
)
4709 struct smbXcli_conn
*conn
= session
->conn
;
4710 uint8_t security_mode
= 0;
4713 return security_mode
;
4716 security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
4717 if (conn
->mandatory_signing
) {
4718 security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
4721 return security_mode
;
4724 uint64_t smb2cli_session_current_id(struct smbXcli_session
*session
)
4726 return session
->smb2
->session_id
;
4729 uint16_t smb2cli_session_get_flags(struct smbXcli_session
*session
)
4731 return session
->smb2
->session_flags
;
4734 void smb2cli_session_set_id_and_flags(struct smbXcli_session
*session
,
4735 uint64_t session_id
,
4736 uint16_t session_flags
)
4738 session
->smb2
->session_id
= session_id
;
4739 session
->smb2
->session_flags
= session_flags
;
4742 void smb2cli_session_increment_channel_sequence(struct smbXcli_session
*session
)
4744 session
->smb2
->channel_sequence
+= 1;
4747 NTSTATUS
smb2cli_session_set_session_key(struct smbXcli_session
*session
,
4748 const DATA_BLOB _session_key
,
4749 const struct iovec
*recv_iov
)
4751 struct smbXcli_conn
*conn
= session
->conn
;
4752 uint16_t no_sign_flags
;
4753 uint8_t session_key
[16];
4754 bool check_signature
= true;
4759 return NT_STATUS_INVALID_PARAMETER_MIX
;
4762 if (recv_iov
[0].iov_len
!= SMB2_HDR_BODY
) {
4763 return NT_STATUS_INVALID_PARAMETER_MIX
;
4766 no_sign_flags
= SMB2_SESSION_FLAG_IS_GUEST
| SMB2_SESSION_FLAG_IS_NULL
;
4768 if (session
->smb2
->session_flags
& no_sign_flags
) {
4769 session
->smb2
->should_sign
= false;
4770 return NT_STATUS_OK
;
4773 if (session
->smb2
->signing_key
.length
!= 0) {
4774 return NT_STATUS_INVALID_PARAMETER_MIX
;
4777 ZERO_STRUCT(session_key
);
4778 memcpy(session_key
, _session_key
.data
,
4779 MIN(_session_key
.length
, sizeof(session_key
)));
4781 session
->smb2
->signing_key
= data_blob_talloc(session
,
4783 sizeof(session_key
));
4784 if (session
->smb2
->signing_key
.data
== NULL
) {
4785 ZERO_STRUCT(session_key
);
4786 return NT_STATUS_NO_MEMORY
;
4789 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4790 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCMAC");
4791 const DATA_BLOB context
= data_blob_string_const_null("SmbSign");
4793 smb2_key_derivation(session_key
, sizeof(session_key
),
4794 label
.data
, label
.length
,
4795 context
.data
, context
.length
,
4796 session
->smb2
->signing_key
.data
);
4799 session
->smb2
->encryption_key
= data_blob_dup_talloc(session
,
4800 session
->smb2
->signing_key
);
4801 if (session
->smb2
->encryption_key
.data
== NULL
) {
4802 ZERO_STRUCT(session_key
);
4803 return NT_STATUS_NO_MEMORY
;
4806 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4807 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCCM");
4808 const DATA_BLOB context
= data_blob_string_const_null("ServerIn ");
4810 smb2_key_derivation(session_key
, sizeof(session_key
),
4811 label
.data
, label
.length
,
4812 context
.data
, context
.length
,
4813 session
->smb2
->encryption_key
.data
);
4816 session
->smb2
->decryption_key
= data_blob_dup_talloc(session
,
4817 session
->smb2
->signing_key
);
4818 if (session
->smb2
->decryption_key
.data
== NULL
) {
4819 ZERO_STRUCT(session_key
);
4820 return NT_STATUS_NO_MEMORY
;
4823 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4824 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCCM");
4825 const DATA_BLOB context
= data_blob_string_const_null("ServerOut");
4827 smb2_key_derivation(session_key
, sizeof(session_key
),
4828 label
.data
, label
.length
,
4829 context
.data
, context
.length
,
4830 session
->smb2
->decryption_key
.data
);
4833 session
->smb2
->application_key
= data_blob_dup_talloc(session
,
4834 session
->smb2
->signing_key
);
4835 if (session
->smb2
->application_key
.data
== NULL
) {
4836 ZERO_STRUCT(session_key
);
4837 return NT_STATUS_NO_MEMORY
;
4840 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4841 const DATA_BLOB label
= data_blob_string_const_null("SMB2APP");
4842 const DATA_BLOB context
= data_blob_string_const_null("SmbRpc");
4844 smb2_key_derivation(session_key
, sizeof(session_key
),
4845 label
.data
, label
.length
,
4846 context
.data
, context
.length
,
4847 session
->smb2
->application_key
.data
);
4849 ZERO_STRUCT(session_key
);
4851 session
->smb2_channel
.signing_key
= data_blob_dup_talloc(session
,
4852 session
->smb2
->signing_key
);
4853 if (session
->smb2_channel
.signing_key
.data
== NULL
) {
4854 return NT_STATUS_NO_MEMORY
;
4857 check_signature
= conn
->mandatory_signing
;
4859 hdr_flags
= IVAL(recv_iov
[0].iov_base
, SMB2_HDR_FLAGS
);
4860 if (hdr_flags
& SMB2_HDR_FLAG_SIGNED
) {
4862 * Sadly some vendors don't sign the
4863 * final SMB2 session setup response
4865 * At least Windows and Samba are always doing this
4866 * if there's a session key available.
4868 * We only check the signature if it's mandatory
4869 * or SMB2_HDR_FLAG_SIGNED is provided.
4871 check_signature
= true;
4874 if (check_signature
) {
4875 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
4876 session
->conn
->protocol
,
4878 if (!NT_STATUS_IS_OK(status
)) {
4883 session
->smb2
->should_sign
= false;
4884 session
->smb2
->should_encrypt
= false;
4886 if (conn
->desire_signing
) {
4887 session
->smb2
->should_sign
= true;
4890 if (conn
->smb2
.server
.security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) {
4891 session
->smb2
->should_sign
= true;
4894 if (session
->smb2
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
) {
4895 session
->smb2
->should_encrypt
= true;
4898 if (conn
->protocol
< PROTOCOL_SMB2_24
) {
4899 session
->smb2
->should_encrypt
= false;
4902 if (!(conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
4903 session
->smb2
->should_encrypt
= false;
4906 generate_random_buffer((uint8_t *)&session
->smb2
->nonce_high
,
4907 sizeof(session
->smb2
->nonce_high
));
4908 session
->smb2
->nonce_low
= 1;
4910 return NT_STATUS_OK
;
4913 NTSTATUS
smb2cli_session_create_channel(TALLOC_CTX
*mem_ctx
,
4914 struct smbXcli_session
*session1
,
4915 struct smbXcli_conn
*conn
,
4916 struct smbXcli_session
**_session2
)
4918 struct smbXcli_session
*session2
;
4920 if (session1
->smb2
->signing_key
.length
== 0) {
4921 return NT_STATUS_INVALID_PARAMETER_MIX
;
4925 return NT_STATUS_INVALID_PARAMETER_MIX
;
4928 session2
= talloc_zero(mem_ctx
, struct smbXcli_session
);
4929 if (session2
== NULL
) {
4930 return NT_STATUS_NO_MEMORY
;
4932 session2
->smb2
= talloc_reference(session2
, session1
->smb2
);
4933 if (session2
->smb2
== NULL
) {
4934 talloc_free(session2
);
4935 return NT_STATUS_NO_MEMORY
;
4938 talloc_set_destructor(session2
, smbXcli_session_destructor
);
4939 DLIST_ADD_END(conn
->sessions
, session2
, struct smbXcli_session
*);
4940 session2
->conn
= conn
;
4942 *_session2
= session2
;
4943 return NT_STATUS_OK
;
4946 NTSTATUS
smb2cli_session_set_channel_key(struct smbXcli_session
*session
,
4947 const DATA_BLOB _channel_key
,
4948 const struct iovec
*recv_iov
)
4950 struct smbXcli_conn
*conn
= session
->conn
;
4951 uint8_t channel_key
[16];
4955 return NT_STATUS_INVALID_PARAMETER_MIX
;
4958 if (session
->smb2_channel
.signing_key
.length
!= 0) {
4959 return NT_STATUS_INVALID_PARAMETER_MIX
;
4962 ZERO_STRUCT(channel_key
);
4963 memcpy(channel_key
, _channel_key
.data
,
4964 MIN(_channel_key
.length
, sizeof(channel_key
)));
4966 session
->smb2_channel
.signing_key
= data_blob_talloc(session
,
4968 sizeof(channel_key
));
4969 if (session
->smb2_channel
.signing_key
.data
== NULL
) {
4970 ZERO_STRUCT(channel_key
);
4971 return NT_STATUS_NO_MEMORY
;
4974 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4975 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCMAC");
4976 const DATA_BLOB context
= data_blob_string_const_null("SmbSign");
4978 smb2_key_derivation(channel_key
, sizeof(channel_key
),
4979 label
.data
, label
.length
,
4980 context
.data
, context
.length
,
4981 session
->smb2_channel
.signing_key
.data
);
4983 ZERO_STRUCT(channel_key
);
4985 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
4986 session
->conn
->protocol
,
4988 if (!NT_STATUS_IS_OK(status
)) {
4992 return NT_STATUS_OK
;
4995 NTSTATUS
smb2cli_session_encryption_on(struct smbXcli_session
*session
)
4997 if (session
->smb2
->should_encrypt
) {
4998 return NT_STATUS_OK
;
5001 if (session
->conn
->protocol
< PROTOCOL_SMB2_24
) {
5002 return NT_STATUS_NOT_SUPPORTED
;
5005 if (!(session
->conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
5006 return NT_STATUS_NOT_SUPPORTED
;
5009 if (session
->smb2
->signing_key
.data
== NULL
) {
5010 return NT_STATUS_NOT_SUPPORTED
;
5012 session
->smb2
->should_encrypt
= true;
5013 return NT_STATUS_OK
;
5016 struct smbXcli_tcon
*smbXcli_tcon_create(TALLOC_CTX
*mem_ctx
)
5018 struct smbXcli_tcon
*tcon
;
5020 tcon
= talloc_zero(mem_ctx
, struct smbXcli_tcon
);
5028 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
5030 return tcon
->smb1
.tcon_id
;
5033 void smb1cli_tcon_set_id(struct smbXcli_tcon
*tcon
, uint16_t tcon_id
)
5035 tcon
->smb1
.tcon_id
= tcon_id
;
5038 bool smb1cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
5040 uint16_t optional_support
,
5041 uint32_t maximal_access
,
5042 uint32_t guest_maximal_access
,
5043 const char *service
,
5044 const char *fs_type
)
5046 tcon
->smb1
.tcon_id
= tcon_id
;
5047 tcon
->smb1
.optional_support
= optional_support
;
5048 tcon
->smb1
.maximal_access
= maximal_access
;
5049 tcon
->smb1
.guest_maximal_access
= guest_maximal_access
;
5051 TALLOC_FREE(tcon
->smb1
.service
);
5052 tcon
->smb1
.service
= talloc_strdup(tcon
, service
);
5053 if (service
!= NULL
&& tcon
->smb1
.service
== NULL
) {
5057 TALLOC_FREE(tcon
->smb1
.fs_type
);
5058 tcon
->smb1
.fs_type
= talloc_strdup(tcon
, fs_type
);
5059 if (fs_type
!= NULL
&& tcon
->smb1
.fs_type
== NULL
) {
5066 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
5068 return tcon
->smb2
.tcon_id
;
5071 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon
*tcon
)
5073 return tcon
->smb2
.capabilities
;
5076 void smb2cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
5077 struct smbXcli_session
*session
,
5081 uint32_t capabilities
,
5082 uint32_t maximal_access
)
5084 tcon
->smb2
.tcon_id
= tcon_id
;
5085 tcon
->smb2
.type
= type
;
5086 tcon
->smb2
.flags
= flags
;
5087 tcon
->smb2
.capabilities
= capabilities
;
5088 tcon
->smb2
.maximal_access
= maximal_access
;
5090 tcon
->smb2
.should_encrypt
= false;
5092 if (session
== NULL
) {
5096 tcon
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
5098 if (flags
& SMB2_SHAREFLAG_ENCRYPT_DATA
) {
5099 tcon
->smb2
.should_encrypt
= true;
5103 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon
*tcon
)
5105 return tcon
->smb2
.should_encrypt
;