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
;
153 struct smb2cli_session
*smb2
;
156 DATA_BLOB signing_key
;
160 struct smbXcli_tcon
{
163 uint16_t optional_support
;
164 uint32_t maximal_access
;
165 uint32_t guest_maximal_access
;
174 uint32_t capabilities
;
175 uint32_t maximal_access
;
180 struct smbXcli_req_state
{
181 struct tevent_context
*ev
;
182 struct smbXcli_conn
*conn
;
183 struct smbXcli_session
*session
; /* maybe NULL */
184 struct smbXcli_tcon
*tcon
; /* maybe NULL */
186 uint8_t length_hdr
[4];
193 /* Space for the header including the wct */
194 uint8_t hdr
[HDR_VWV
];
197 * For normal requests, smb1cli_req_send chooses a mid.
198 * SecondaryV trans requests need to use the mid of the primary
199 * request, so we need a place to store it.
200 * Assume it is set if != 0.
205 uint8_t bytecount_buf
[2];
207 #define MAX_SMB_IOV 10
208 /* length_hdr, hdr, words, byte_count, buffers */
209 struct iovec iov
[1 + 3 + MAX_SMB_IOV
];
214 struct tevent_req
**chained_requests
;
217 NTSTATUS recv_status
;
218 /* always an array of 3 talloc elements */
219 struct iovec
*recv_iov
;
223 const uint8_t *fixed
;
228 uint8_t transform
[SMB2_TF_HDR_SIZE
];
229 uint8_t hdr
[SMB2_HDR_BODY
];
230 uint8_t pad
[7]; /* padding space for compounding */
233 * always an array of 3 talloc elements
234 * (without a SMB2_TRANSFORM header!)
238 struct iovec
*recv_iov
;
240 uint16_t credit_charge
;
245 bool signing_skipped
;
251 static int smbXcli_conn_destructor(struct smbXcli_conn
*conn
)
254 * NT_STATUS_OK, means we do not notify the callers
256 smbXcli_conn_disconnect(conn
, NT_STATUS_OK
);
258 while (conn
->sessions
) {
259 conn
->sessions
->conn
= NULL
;
260 DLIST_REMOVE(conn
->sessions
, conn
->sessions
);
263 if (conn
->smb1
.trans_enc
) {
264 TALLOC_FREE(conn
->smb1
.trans_enc
);
270 struct smbXcli_conn
*smbXcli_conn_create(TALLOC_CTX
*mem_ctx
,
272 const char *remote_name
,
273 enum smb_signing_setting signing_state
,
274 uint32_t smb1_capabilities
,
275 struct GUID
*client_guid
,
276 uint32_t smb2_capabilities
)
278 struct smbXcli_conn
*conn
= NULL
;
280 struct sockaddr
*sa
= NULL
;
284 conn
= talloc_zero(mem_ctx
, struct smbXcli_conn
);
290 conn
->write_fd
= dup(fd
);
291 if (conn
->write_fd
== -1) {
295 conn
->remote_name
= talloc_strdup(conn
, remote_name
);
296 if (conn
->remote_name
== NULL
) {
301 ss
= (void *)&conn
->local_ss
;
302 sa
= (struct sockaddr
*)ss
;
303 sa_length
= sizeof(conn
->local_ss
);
304 ret
= getsockname(fd
, sa
, &sa_length
);
308 ss
= (void *)&conn
->remote_ss
;
309 sa
= (struct sockaddr
*)ss
;
310 sa_length
= sizeof(conn
->remote_ss
);
311 ret
= getpeername(fd
, sa
, &sa_length
);
316 conn
->outgoing
= tevent_queue_create(conn
, "smbXcli_outgoing");
317 if (conn
->outgoing
== NULL
) {
320 conn
->pending
= NULL
;
322 conn
->protocol
= PROTOCOL_NONE
;
324 switch (signing_state
) {
325 case SMB_SIGNING_OFF
:
327 conn
->allow_signing
= false;
328 conn
->desire_signing
= false;
329 conn
->mandatory_signing
= false;
331 case SMB_SIGNING_DEFAULT
:
332 case SMB_SIGNING_IF_REQUIRED
:
333 /* if the server requires it */
334 conn
->allow_signing
= true;
335 conn
->desire_signing
= false;
336 conn
->mandatory_signing
= false;
338 case SMB_SIGNING_REQUIRED
:
340 conn
->allow_signing
= true;
341 conn
->desire_signing
= true;
342 conn
->mandatory_signing
= true;
346 conn
->smb1
.client
.capabilities
= smb1_capabilities
;
347 conn
->smb1
.client
.max_xmit
= UINT16_MAX
;
349 conn
->smb1
.capabilities
= conn
->smb1
.client
.capabilities
;
350 conn
->smb1
.max_xmit
= 1024;
354 /* initialise signing */
355 conn
->smb1
.signing
= smb_signing_init(conn
,
357 conn
->desire_signing
,
358 conn
->mandatory_signing
);
359 if (!conn
->smb1
.signing
) {
363 conn
->smb2
.client
.security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
364 if (conn
->mandatory_signing
) {
365 conn
->smb2
.client
.security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
368 conn
->smb2
.client
.guid
= *client_guid
;
370 conn
->smb2
.client
.capabilities
= smb2_capabilities
;
372 conn
->smb2
.cur_credits
= 1;
373 conn
->smb2
.max_credits
= 0;
375 talloc_set_destructor(conn
, smbXcli_conn_destructor
);
379 if (conn
->write_fd
!= -1) {
380 close(conn
->write_fd
);
386 bool smbXcli_conn_is_connected(struct smbXcli_conn
*conn
)
392 if (conn
->read_fd
== -1) {
399 enum protocol_types
smbXcli_conn_protocol(struct smbXcli_conn
*conn
)
401 return conn
->protocol
;
404 bool smbXcli_conn_use_unicode(struct smbXcli_conn
*conn
)
406 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
410 if (conn
->smb1
.capabilities
& CAP_UNICODE
) {
417 void smbXcli_conn_set_sockopt(struct smbXcli_conn
*conn
, const char *options
)
419 set_socket_options(conn
->read_fd
, options
);
422 const struct sockaddr_storage
*smbXcli_conn_local_sockaddr(struct smbXcli_conn
*conn
)
424 return &conn
->local_ss
;
427 const struct sockaddr_storage
*smbXcli_conn_remote_sockaddr(struct smbXcli_conn
*conn
)
429 return &conn
->remote_ss
;
432 const char *smbXcli_conn_remote_name(struct smbXcli_conn
*conn
)
434 return conn
->remote_name
;
437 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn
*conn
)
439 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
446 return conn
->smb1
.server
.max_mux
;
449 NTTIME
smbXcli_conn_server_system_time(struct smbXcli_conn
*conn
)
451 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
452 return conn
->smb2
.server
.system_time
;
455 return conn
->smb1
.server
.system_time
;
458 const DATA_BLOB
*smbXcli_conn_server_gss_blob(struct smbXcli_conn
*conn
)
460 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
461 return &conn
->smb2
.server
.gss_blob
;
464 return &conn
->smb1
.server
.gss_blob
;
467 const struct GUID
*smbXcli_conn_server_guid(struct smbXcli_conn
*conn
)
469 if (conn
->protocol
>= PROTOCOL_SMB2_02
) {
470 return &conn
->smb2
.server
.guid
;
473 return &conn
->smb1
.server
.guid
;
476 struct smbXcli_conn_samba_suicide_state
{
477 struct smbXcli_conn
*conn
;
482 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
);
484 struct tevent_req
*smbXcli_conn_samba_suicide_send(TALLOC_CTX
*mem_ctx
,
485 struct tevent_context
*ev
,
486 struct smbXcli_conn
*conn
,
489 struct tevent_req
*req
, *subreq
;
490 struct smbXcli_conn_samba_suicide_state
*state
;
492 req
= tevent_req_create(mem_ctx
, &state
,
493 struct smbXcli_conn_samba_suicide_state
);
498 SIVAL(state
->buf
, 4, 0x74697865);
499 SCVAL(state
->buf
, 8, exitcode
);
500 _smb_setlen_nbt(state
->buf
, sizeof(state
->buf
)-4);
502 state
->iov
.iov_base
= state
->buf
;
503 state
->iov
.iov_len
= sizeof(state
->buf
);
505 subreq
= writev_send(state
, ev
, conn
->outgoing
, conn
->write_fd
,
506 false, &state
->iov
, 1);
507 if (tevent_req_nomem(subreq
, req
)) {
508 return tevent_req_post(req
, ev
);
510 tevent_req_set_callback(subreq
, smbXcli_conn_samba_suicide_done
, req
);
514 static void smbXcli_conn_samba_suicide_done(struct tevent_req
*subreq
)
516 struct tevent_req
*req
= tevent_req_callback_data(
517 subreq
, struct tevent_req
);
518 struct smbXcli_conn_samba_suicide_state
*state
= tevent_req_data(
519 req
, struct smbXcli_conn_samba_suicide_state
);
523 nwritten
= writev_recv(subreq
, &err
);
525 if (nwritten
== -1) {
526 NTSTATUS status
= map_nt_error_from_unix_common(err
);
527 smbXcli_conn_disconnect(state
->conn
, status
);
530 tevent_req_done(req
);
533 NTSTATUS
smbXcli_conn_samba_suicide_recv(struct tevent_req
*req
)
535 return tevent_req_simple_recv_ntstatus(req
);
538 NTSTATUS
smbXcli_conn_samba_suicide(struct smbXcli_conn
*conn
,
541 TALLOC_CTX
*frame
= talloc_stackframe();
542 struct tevent_context
*ev
;
543 struct tevent_req
*req
;
544 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
547 if (smbXcli_conn_has_async_calls(conn
)) {
549 * Can't use sync call while an async call is in flight
551 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
554 ev
= tevent_context_init(frame
);
558 req
= smbXcli_conn_samba_suicide_send(frame
, ev
, conn
, exitcode
);
562 ok
= tevent_req_poll(req
, ev
);
564 status
= map_nt_error_from_unix_common(errno
);
567 status
= smbXcli_conn_samba_suicide_recv(req
);
573 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn
*conn
)
575 return conn
->smb1
.capabilities
;
578 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn
*conn
)
580 return conn
->smb1
.max_xmit
;
583 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn
*conn
)
585 return conn
->smb1
.server
.session_key
;
588 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn
*conn
)
590 return conn
->smb1
.server
.challenge
;
593 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
595 return conn
->smb1
.server
.security_mode
;
598 bool smb1cli_conn_server_readbraw(struct smbXcli_conn
*conn
)
600 return conn
->smb1
.server
.readbraw
;
603 bool smb1cli_conn_server_writebraw(struct smbXcli_conn
*conn
)
605 return conn
->smb1
.server
.writebraw
;
608 bool smb1cli_conn_server_lockread(struct smbXcli_conn
*conn
)
610 return conn
->smb1
.server
.lockread
;
613 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn
*conn
)
615 return conn
->smb1
.server
.writeunlock
;
618 int smb1cli_conn_server_time_zone(struct smbXcli_conn
*conn
)
620 return conn
->smb1
.server
.time_zone
;
623 bool smb1cli_conn_activate_signing(struct smbXcli_conn
*conn
,
624 const DATA_BLOB user_session_key
,
625 const DATA_BLOB response
)
627 return smb_signing_activate(conn
->smb1
.signing
,
632 bool smb1cli_conn_check_signing(struct smbXcli_conn
*conn
,
633 const uint8_t *buf
, uint32_t seqnum
)
635 return smb_signing_check_pdu(conn
->smb1
.signing
, buf
, seqnum
);
638 bool smb1cli_conn_signing_is_active(struct smbXcli_conn
*conn
)
640 return smb_signing_is_active(conn
->smb1
.signing
);
643 void smb1cli_conn_set_encryption(struct smbXcli_conn
*conn
,
644 struct smb_trans_enc_state
*es
)
646 /* Replace the old state, if any. */
647 if (conn
->smb1
.trans_enc
) {
648 TALLOC_FREE(conn
->smb1
.trans_enc
);
650 conn
->smb1
.trans_enc
= es
;
653 bool smb1cli_conn_encryption_on(struct smbXcli_conn
*conn
)
655 return common_encryption_on(conn
->smb1
.trans_enc
);
659 static NTSTATUS
smb1cli_pull_raw_error(const uint8_t *hdr
)
661 uint32_t flags2
= SVAL(hdr
, HDR_FLG2
);
662 NTSTATUS status
= NT_STATUS(IVAL(hdr
, HDR_RCLS
));
664 if (NT_STATUS_IS_OK(status
)) {
668 if (flags2
& FLAGS2_32_BIT_ERROR_CODES
) {
672 return NT_STATUS_DOS(CVAL(hdr
, HDR_RCLS
), SVAL(hdr
, HDR_ERR
));
676 * Is the SMB command able to hold an AND_X successor
677 * @param[in] cmd The SMB command in question
678 * @retval Can we add a chained request after "cmd"?
680 bool smb1cli_is_andx_req(uint8_t cmd
)
700 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn
*conn
)
702 size_t num_pending
= talloc_array_length(conn
->pending
);
708 result
= conn
->smb1
.mid
++;
709 if ((result
== 0) || (result
== 0xffff)) {
713 for (i
=0; i
<num_pending
; i
++) {
714 if (result
== smb1cli_req_mid(conn
->pending
[i
])) {
719 if (i
== num_pending
) {
725 void smbXcli_req_unset_pending(struct tevent_req
*req
)
727 struct smbXcli_req_state
*state
=
729 struct smbXcli_req_state
);
730 struct smbXcli_conn
*conn
= state
->conn
;
731 size_t num_pending
= talloc_array_length(conn
->pending
);
734 if (state
->smb1
.mid
!= 0) {
736 * This is a [nt]trans[2] request which waits
737 * for more than one reply.
742 talloc_set_destructor(req
, NULL
);
744 if (num_pending
== 1) {
746 * The pending read_smb tevent_req is a child of
747 * conn->pending. So if nothing is pending anymore, we need to
748 * delete the socket read fde.
750 TALLOC_FREE(conn
->pending
);
751 conn
->read_smb_req
= NULL
;
755 for (i
=0; i
<num_pending
; i
++) {
756 if (req
== conn
->pending
[i
]) {
760 if (i
== num_pending
) {
762 * Something's seriously broken. Just returning here is the
763 * right thing nevertheless, the point of this routine is to
764 * remove ourselves from conn->pending.
770 * Remove ourselves from the conn->pending array
772 for (; i
< (num_pending
- 1); i
++) {
773 conn
->pending
[i
] = conn
->pending
[i
+1];
777 * No NULL check here, we're shrinking by sizeof(void *), and
778 * talloc_realloc just adjusts the size for this.
780 conn
->pending
= talloc_realloc(NULL
, conn
->pending
, struct tevent_req
*,
785 static int smbXcli_req_destructor(struct tevent_req
*req
)
787 struct smbXcli_req_state
*state
=
789 struct smbXcli_req_state
);
792 * Make sure we really remove it from
793 * the pending array on destruction.
796 smbXcli_req_unset_pending(req
);
800 static bool smb1cli_req_cancel(struct tevent_req
*req
);
801 static bool smb2cli_req_cancel(struct tevent_req
*req
);
803 static bool smbXcli_req_cancel(struct tevent_req
*req
)
805 struct smbXcli_req_state
*state
=
807 struct smbXcli_req_state
);
809 if (!smbXcli_conn_is_connected(state
->conn
)) {
813 if (state
->conn
->protocol
== PROTOCOL_NONE
) {
817 if (state
->conn
->protocol
>= PROTOCOL_SMB2_02
) {
818 return smb2cli_req_cancel(req
);
821 return smb1cli_req_cancel(req
);
824 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
);
826 bool smbXcli_req_set_pending(struct tevent_req
*req
)
828 struct smbXcli_req_state
*state
=
830 struct smbXcli_req_state
);
831 struct smbXcli_conn
*conn
;
832 struct tevent_req
**pending
;
837 if (!smbXcli_conn_is_connected(conn
)) {
841 num_pending
= talloc_array_length(conn
->pending
);
843 pending
= talloc_realloc(conn
, conn
->pending
, struct tevent_req
*,
845 if (pending
== NULL
) {
848 pending
[num_pending
] = req
;
849 conn
->pending
= pending
;
850 talloc_set_destructor(req
, smbXcli_req_destructor
);
851 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
853 if (!smbXcli_conn_receive_next(conn
)) {
855 * the caller should notify the current request
857 * And all other pending requests get notified
858 * by smbXcli_conn_disconnect().
860 smbXcli_req_unset_pending(req
);
861 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
868 static void smbXcli_conn_received(struct tevent_req
*subreq
);
870 static bool smbXcli_conn_receive_next(struct smbXcli_conn
*conn
)
872 size_t num_pending
= talloc_array_length(conn
->pending
);
873 struct tevent_req
*req
;
874 struct smbXcli_req_state
*state
;
876 if (conn
->read_smb_req
!= NULL
) {
880 if (num_pending
== 0) {
881 if (conn
->smb2
.mid
< UINT64_MAX
) {
882 /* no more pending requests, so we are done for now */
887 * If there are no more SMB2 requests possible,
888 * because we are out of message ids,
889 * we need to disconnect.
891 smbXcli_conn_disconnect(conn
, NT_STATUS_CONNECTION_ABORTED
);
895 req
= conn
->pending
[0];
896 state
= tevent_req_data(req
, struct smbXcli_req_state
);
899 * We're the first ones, add the read_smb request that waits for the
900 * answer from the server
902 conn
->read_smb_req
= read_smb_send(conn
->pending
,
905 if (conn
->read_smb_req
== NULL
) {
908 tevent_req_set_callback(conn
->read_smb_req
, smbXcli_conn_received
, conn
);
912 void smbXcli_conn_disconnect(struct smbXcli_conn
*conn
, NTSTATUS status
)
914 struct smbXcli_session
*session
;
916 tevent_queue_stop(conn
->outgoing
);
918 if (conn
->read_fd
!= -1) {
919 close(conn
->read_fd
);
921 if (conn
->write_fd
!= -1) {
922 close(conn
->write_fd
);
927 session
= conn
->sessions
;
928 if (talloc_array_length(conn
->pending
) == 0) {
930 * if we do not have pending requests
931 * there is no need to update the channel_sequence
935 for (; session
; session
= session
->next
) {
936 smb2cli_session_increment_channel_sequence(session
);
940 * Cancel all pending requests. We do not do a for-loop walking
941 * conn->pending because that array changes in
942 * smbXcli_req_unset_pending.
944 while (talloc_array_length(conn
->pending
) > 0) {
945 struct tevent_req
*req
;
946 struct smbXcli_req_state
*state
;
947 struct tevent_req
**chain
;
951 req
= conn
->pending
[0];
952 state
= tevent_req_data(req
, struct smbXcli_req_state
);
954 if (state
->smb1
.chained_requests
== NULL
) {
956 * We're dead. No point waiting for trans2
961 smbXcli_req_unset_pending(req
);
963 if (NT_STATUS_IS_OK(status
)) {
964 /* do not notify the callers */
969 * we need to defer the callback, because we may notify
970 * more then one caller.
972 tevent_req_defer_callback(req
, state
->ev
);
973 tevent_req_nterror(req
, status
);
977 chain
= talloc_move(conn
, &state
->smb1
.chained_requests
);
978 num_chained
= talloc_array_length(chain
);
980 for (i
=0; i
<num_chained
; i
++) {
982 state
= tevent_req_data(req
, struct smbXcli_req_state
);
985 * We're dead. No point waiting for trans2
990 smbXcli_req_unset_pending(req
);
992 if (NT_STATUS_IS_OK(status
)) {
993 /* do not notify the callers */
998 * we need to defer the callback, because we may notify
999 * more than one caller.
1001 tevent_req_defer_callback(req
, state
->ev
);
1002 tevent_req_nterror(req
, status
);
1009 * Fetch a smb request's mid. Only valid after the request has been sent by
1010 * smb1cli_req_send().
1012 uint16_t smb1cli_req_mid(struct tevent_req
*req
)
1014 struct smbXcli_req_state
*state
=
1015 tevent_req_data(req
,
1016 struct smbXcli_req_state
);
1018 if (state
->smb1
.mid
!= 0) {
1019 return state
->smb1
.mid
;
1022 return SVAL(state
->smb1
.hdr
, HDR_MID
);
1025 void smb1cli_req_set_mid(struct tevent_req
*req
, uint16_t mid
)
1027 struct smbXcli_req_state
*state
=
1028 tevent_req_data(req
,
1029 struct smbXcli_req_state
);
1031 state
->smb1
.mid
= mid
;
1034 uint32_t smb1cli_req_seqnum(struct tevent_req
*req
)
1036 struct smbXcli_req_state
*state
=
1037 tevent_req_data(req
,
1038 struct smbXcli_req_state
);
1040 return state
->smb1
.seqnum
;
1043 void smb1cli_req_set_seqnum(struct tevent_req
*req
, uint32_t seqnum
)
1045 struct smbXcli_req_state
*state
=
1046 tevent_req_data(req
,
1047 struct smbXcli_req_state
);
1049 state
->smb1
.seqnum
= seqnum
;
1052 static size_t smbXcli_iov_len(const struct iovec
*iov
, int count
)
1056 for (i
=0; i
<count
; i
++) {
1057 result
+= iov
[i
].iov_len
;
1062 static uint8_t *smbXcli_iov_concat(TALLOC_CTX
*mem_ctx
,
1063 const struct iovec
*iov
,
1066 size_t len
= smbXcli_iov_len(iov
, count
);
1071 buf
= talloc_array(mem_ctx
, uint8_t, len
);
1076 for (i
=0; i
<count
; i
++) {
1077 memcpy(buf
+copied
, iov
[i
].iov_base
, iov
[i
].iov_len
);
1078 copied
+= iov
[i
].iov_len
;
1083 static void smb1cli_req_flags(enum protocol_types protocol
,
1084 uint32_t smb1_capabilities
,
1085 uint8_t smb_command
,
1086 uint8_t additional_flags
,
1087 uint8_t clear_flags
,
1089 uint16_t additional_flags2
,
1090 uint16_t clear_flags2
,
1094 uint16_t flags2
= 0;
1096 if (protocol
>= PROTOCOL_LANMAN1
) {
1097 flags
|= FLAG_CASELESS_PATHNAMES
;
1098 flags
|= FLAG_CANONICAL_PATHNAMES
;
1101 if (protocol
>= PROTOCOL_LANMAN2
) {
1102 flags2
|= FLAGS2_LONG_PATH_COMPONENTS
;
1103 flags2
|= FLAGS2_EXTENDED_ATTRIBUTES
;
1106 if (protocol
>= PROTOCOL_NT1
) {
1107 flags2
|= FLAGS2_IS_LONG_NAME
;
1109 if (smb1_capabilities
& CAP_UNICODE
) {
1110 flags2
|= FLAGS2_UNICODE_STRINGS
;
1112 if (smb1_capabilities
& CAP_STATUS32
) {
1113 flags2
|= FLAGS2_32_BIT_ERROR_CODES
;
1115 if (smb1_capabilities
& CAP_EXTENDED_SECURITY
) {
1116 flags2
|= FLAGS2_EXTENDED_SECURITY
;
1120 flags
|= additional_flags
;
1121 flags
&= ~clear_flags
;
1122 flags2
|= additional_flags2
;
1123 flags2
&= ~clear_flags2
;
1129 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
);
1131 static bool smb1cli_req_cancel(struct tevent_req
*req
)
1133 struct smbXcli_req_state
*state
=
1134 tevent_req_data(req
,
1135 struct smbXcli_req_state
);
1140 struct tevent_req
*subreq
;
1143 flags
= CVAL(state
->smb1
.hdr
, HDR_FLG
);
1144 flags2
= SVAL(state
->smb1
.hdr
, HDR_FLG2
);
1145 pid
= SVAL(state
->smb1
.hdr
, HDR_PID
);
1146 pid
|= SVAL(state
->smb1
.hdr
, HDR_PIDHIGH
)<<16;
1147 mid
= SVAL(state
->smb1
.hdr
, HDR_MID
);
1149 subreq
= smb1cli_req_create(state
, state
->ev
,
1159 0, NULL
); /* bytes */
1160 if (subreq
== NULL
) {
1163 smb1cli_req_set_mid(subreq
, mid
);
1165 status
= smb1cli_req_chain_submit(&subreq
, 1);
1166 if (!NT_STATUS_IS_OK(status
)) {
1167 TALLOC_FREE(subreq
);
1170 smb1cli_req_set_mid(subreq
, 0);
1172 tevent_req_set_callback(subreq
, smb1cli_req_cancel_done
, NULL
);
1177 static void smb1cli_req_cancel_done(struct tevent_req
*subreq
)
1179 /* we do not care about the result */
1180 TALLOC_FREE(subreq
);
1183 struct tevent_req
*smb1cli_req_create(TALLOC_CTX
*mem_ctx
,
1184 struct tevent_context
*ev
,
1185 struct smbXcli_conn
*conn
,
1186 uint8_t smb_command
,
1187 uint8_t additional_flags
,
1188 uint8_t clear_flags
,
1189 uint16_t additional_flags2
,
1190 uint16_t clear_flags2
,
1191 uint32_t timeout_msec
,
1193 struct smbXcli_tcon
*tcon
,
1194 struct smbXcli_session
*session
,
1195 uint8_t wct
, uint16_t *vwv
,
1197 struct iovec
*bytes_iov
)
1199 struct tevent_req
*req
;
1200 struct smbXcli_req_state
*state
;
1202 uint16_t flags2
= 0;
1206 if (iov_count
> MAX_SMB_IOV
) {
1208 * Should not happen :-)
1213 req
= tevent_req_create(mem_ctx
, &state
,
1214 struct smbXcli_req_state
);
1220 state
->session
= session
;
1224 uid
= session
->smb1
.session_id
;
1228 tid
= tcon
->smb1
.tcon_id
;
1231 state
->smb1
.recv_cmd
= 0xFF;
1232 state
->smb1
.recv_status
= NT_STATUS_INTERNAL_ERROR
;
1233 state
->smb1
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
1234 if (state
->smb1
.recv_iov
== NULL
) {
1239 smb1cli_req_flags(conn
->protocol
,
1240 conn
->smb1
.capabilities
,
1249 SIVAL(state
->smb1
.hdr
, 0, SMB_MAGIC
);
1250 SCVAL(state
->smb1
.hdr
, HDR_COM
, smb_command
);
1251 SIVAL(state
->smb1
.hdr
, HDR_RCLS
, NT_STATUS_V(NT_STATUS_OK
));
1252 SCVAL(state
->smb1
.hdr
, HDR_FLG
, flags
);
1253 SSVAL(state
->smb1
.hdr
, HDR_FLG2
, flags2
);
1254 SSVAL(state
->smb1
.hdr
, HDR_PIDHIGH
, pid
>> 16);
1255 SSVAL(state
->smb1
.hdr
, HDR_TID
, tid
);
1256 SSVAL(state
->smb1
.hdr
, HDR_PID
, pid
);
1257 SSVAL(state
->smb1
.hdr
, HDR_UID
, uid
);
1258 SSVAL(state
->smb1
.hdr
, HDR_MID
, 0); /* this comes later */
1259 SCVAL(state
->smb1
.hdr
, HDR_WCT
, wct
);
1261 state
->smb1
.vwv
= vwv
;
1263 SSVAL(state
->smb1
.bytecount_buf
, 0, smbXcli_iov_len(bytes_iov
, iov_count
));
1265 state
->smb1
.iov
[0].iov_base
= (void *)state
->length_hdr
;
1266 state
->smb1
.iov
[0].iov_len
= sizeof(state
->length_hdr
);
1267 state
->smb1
.iov
[1].iov_base
= (void *)state
->smb1
.hdr
;
1268 state
->smb1
.iov
[1].iov_len
= sizeof(state
->smb1
.hdr
);
1269 state
->smb1
.iov
[2].iov_base
= (void *)state
->smb1
.vwv
;
1270 state
->smb1
.iov
[2].iov_len
= wct
* sizeof(uint16_t);
1271 state
->smb1
.iov
[3].iov_base
= (void *)state
->smb1
.bytecount_buf
;
1272 state
->smb1
.iov
[3].iov_len
= sizeof(uint16_t);
1274 if (iov_count
!= 0) {
1275 memcpy(&state
->smb1
.iov
[4], bytes_iov
,
1276 iov_count
* sizeof(*bytes_iov
));
1278 state
->smb1
.iov_count
= iov_count
+ 4;
1280 if (timeout_msec
> 0) {
1281 struct timeval endtime
;
1283 endtime
= timeval_current_ofs_msec(timeout_msec
);
1284 if (!tevent_req_set_endtime(req
, ev
, endtime
)) {
1289 switch (smb_command
) {
1293 state
->one_way
= true;
1296 state
->one_way
= true;
1297 state
->smb1
.one_way_seqnum
= true;
1301 (CVAL(vwv
+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE
)) {
1302 state
->one_way
= true;
1310 static NTSTATUS
smb1cli_conn_signv(struct smbXcli_conn
*conn
,
1311 struct iovec
*iov
, int iov_count
,
1313 bool one_way_seqnum
)
1315 TALLOC_CTX
*frame
= NULL
;
1319 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1320 * iovec directly. MD5Update would do that just fine.
1323 if (iov_count
< 4) {
1324 return NT_STATUS_INVALID_PARAMETER_MIX
;
1326 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1327 return NT_STATUS_INVALID_PARAMETER_MIX
;
1329 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1330 return NT_STATUS_INVALID_PARAMETER_MIX
;
1332 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1333 return NT_STATUS_INVALID_PARAMETER_MIX
;
1335 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1336 return NT_STATUS_INVALID_PARAMETER_MIX
;
1339 frame
= talloc_stackframe();
1341 buf
= smbXcli_iov_concat(frame
, iov
, iov_count
);
1343 return NT_STATUS_NO_MEMORY
;
1346 *seqnum
= smb_signing_next_seqnum(conn
->smb1
.signing
,
1348 smb_signing_sign_pdu(conn
->smb1
.signing
, buf
, *seqnum
);
1349 memcpy(iov
[1].iov_base
, buf
+4, iov
[1].iov_len
);
1352 return NT_STATUS_OK
;
1355 static void smb1cli_req_writev_done(struct tevent_req
*subreq
);
1356 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1357 TALLOC_CTX
*tmp_mem
,
1360 static NTSTATUS
smb1cli_req_writev_submit(struct tevent_req
*req
,
1361 struct smbXcli_req_state
*state
,
1362 struct iovec
*iov
, int iov_count
)
1364 struct tevent_req
*subreq
;
1369 if (!smbXcli_conn_is_connected(state
->conn
)) {
1370 return NT_STATUS_CONNECTION_DISCONNECTED
;
1373 if (state
->conn
->protocol
> PROTOCOL_NT1
) {
1374 return NT_STATUS_REVISION_MISMATCH
;
1377 if (iov_count
< 4) {
1378 return NT_STATUS_INVALID_PARAMETER_MIX
;
1380 if (iov
[0].iov_len
!= NBT_HDR_SIZE
) {
1381 return NT_STATUS_INVALID_PARAMETER_MIX
;
1383 if (iov
[1].iov_len
!= (MIN_SMB_SIZE
-sizeof(uint16_t))) {
1384 return NT_STATUS_INVALID_PARAMETER_MIX
;
1386 if (iov
[2].iov_len
> (0xFF * sizeof(uint16_t))) {
1387 return NT_STATUS_INVALID_PARAMETER_MIX
;
1389 if (iov
[3].iov_len
!= sizeof(uint16_t)) {
1390 return NT_STATUS_INVALID_PARAMETER_MIX
;
1393 cmd
= CVAL(iov
[1].iov_base
, HDR_COM
);
1394 if (cmd
== SMBreadBraw
) {
1395 if (smbXcli_conn_has_async_calls(state
->conn
)) {
1396 return NT_STATUS_INVALID_PARAMETER_MIX
;
1398 state
->conn
->smb1
.read_braw_req
= req
;
1401 if (state
->smb1
.mid
!= 0) {
1402 mid
= state
->smb1
.mid
;
1404 mid
= smb1cli_alloc_mid(state
->conn
);
1406 SSVAL(iov
[1].iov_base
, HDR_MID
, mid
);
1408 _smb_setlen_nbt(iov
[0].iov_base
, smbXcli_iov_len(&iov
[1], iov_count
-1));
1410 status
= smb1cli_conn_signv(state
->conn
, iov
, iov_count
,
1411 &state
->smb1
.seqnum
,
1412 state
->smb1
.one_way_seqnum
);
1414 if (!NT_STATUS_IS_OK(status
)) {
1419 * If we supported multiple encrytion contexts
1420 * here we'd look up based on tid.
1422 if (common_encryption_on(state
->conn
->smb1
.trans_enc
)) {
1423 char *buf
, *enc_buf
;
1425 buf
= (char *)smbXcli_iov_concat(talloc_tos(), iov
, iov_count
);
1427 return NT_STATUS_NO_MEMORY
;
1429 status
= common_encrypt_buffer(state
->conn
->smb1
.trans_enc
,
1430 (char *)buf
, &enc_buf
);
1432 if (!NT_STATUS_IS_OK(status
)) {
1433 DEBUG(0, ("Error in encrypting client message: %s\n",
1434 nt_errstr(status
)));
1437 buf
= (char *)talloc_memdup(state
, enc_buf
,
1438 smb_len_nbt(enc_buf
)+4);
1441 return NT_STATUS_NO_MEMORY
;
1443 iov
[0].iov_base
= (void *)buf
;
1444 iov
[0].iov_len
= talloc_get_size(buf
);
1448 if (state
->conn
->dispatch_incoming
== NULL
) {
1449 state
->conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
1452 tevent_req_set_cancel_fn(req
, smbXcli_req_cancel
);
1454 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
1455 state
->conn
->write_fd
, false, iov
, iov_count
);
1456 if (subreq
== NULL
) {
1457 return NT_STATUS_NO_MEMORY
;
1459 tevent_req_set_callback(subreq
, smb1cli_req_writev_done
, req
);
1460 return NT_STATUS_OK
;
1463 struct tevent_req
*smb1cli_req_send(TALLOC_CTX
*mem_ctx
,
1464 struct tevent_context
*ev
,
1465 struct smbXcli_conn
*conn
,
1466 uint8_t smb_command
,
1467 uint8_t additional_flags
,
1468 uint8_t clear_flags
,
1469 uint16_t additional_flags2
,
1470 uint16_t clear_flags2
,
1471 uint32_t timeout_msec
,
1473 struct smbXcli_tcon
*tcon
,
1474 struct smbXcli_session
*session
,
1475 uint8_t wct
, uint16_t *vwv
,
1477 const uint8_t *bytes
)
1479 struct tevent_req
*req
;
1483 iov
.iov_base
= discard_const_p(void, bytes
);
1484 iov
.iov_len
= num_bytes
;
1486 req
= smb1cli_req_create(mem_ctx
, ev
, conn
, smb_command
,
1487 additional_flags
, clear_flags
,
1488 additional_flags2
, clear_flags2
,
1495 if (!tevent_req_is_in_progress(req
)) {
1496 return tevent_req_post(req
, ev
);
1498 status
= smb1cli_req_chain_submit(&req
, 1);
1499 if (tevent_req_nterror(req
, status
)) {
1500 return tevent_req_post(req
, ev
);
1505 static void smb1cli_req_writev_done(struct tevent_req
*subreq
)
1507 struct tevent_req
*req
=
1508 tevent_req_callback_data(subreq
,
1510 struct smbXcli_req_state
*state
=
1511 tevent_req_data(req
,
1512 struct smbXcli_req_state
);
1516 nwritten
= writev_recv(subreq
, &err
);
1517 TALLOC_FREE(subreq
);
1518 if (nwritten
== -1) {
1519 NTSTATUS status
= map_nt_error_from_unix_common(err
);
1520 smbXcli_conn_disconnect(state
->conn
, status
);
1524 if (state
->one_way
) {
1525 state
->inbuf
= NULL
;
1526 tevent_req_done(req
);
1530 if (!smbXcli_req_set_pending(req
)) {
1531 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1536 static void smbXcli_conn_received(struct tevent_req
*subreq
)
1538 struct smbXcli_conn
*conn
=
1539 tevent_req_callback_data(subreq
,
1540 struct smbXcli_conn
);
1541 TALLOC_CTX
*frame
= talloc_stackframe();
1547 if (subreq
!= conn
->read_smb_req
) {
1548 DEBUG(1, ("Internal error: cli_smb_received called with "
1549 "unexpected subreq\n"));
1550 status
= NT_STATUS_INTERNAL_ERROR
;
1551 smbXcli_conn_disconnect(conn
, status
);
1555 conn
->read_smb_req
= NULL
;
1557 received
= read_smb_recv(subreq
, frame
, &inbuf
, &err
);
1558 TALLOC_FREE(subreq
);
1559 if (received
== -1) {
1560 status
= map_nt_error_from_unix_common(err
);
1561 smbXcli_conn_disconnect(conn
, status
);
1566 status
= conn
->dispatch_incoming(conn
, frame
, inbuf
);
1568 if (NT_STATUS_IS_OK(status
)) {
1570 * We should not do any more processing
1571 * as the dispatch function called
1572 * tevent_req_done().
1575 } else if (!NT_STATUS_EQUAL(status
, NT_STATUS_RETRY
)) {
1577 * We got an error, so notify all pending requests
1579 smbXcli_conn_disconnect(conn
, status
);
1584 * We got NT_STATUS_RETRY, so we may ask for a
1585 * next incoming pdu.
1587 if (!smbXcli_conn_receive_next(conn
)) {
1588 smbXcli_conn_disconnect(conn
, NT_STATUS_NO_MEMORY
);
1592 static NTSTATUS
smb1cli_inbuf_parse_chain(uint8_t *buf
, TALLOC_CTX
*mem_ctx
,
1593 struct iovec
**piov
, int *pnum_iov
)
1604 buflen
= smb_len_nbt(buf
);
1607 hdr
= buf
+ NBT_HDR_SIZE
;
1609 if (buflen
< MIN_SMB_SIZE
) {
1610 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1614 * This returns iovec elements in the following order:
1629 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
1631 return NT_STATUS_NO_MEMORY
;
1633 iov
[0].iov_base
= hdr
;
1634 iov
[0].iov_len
= HDR_WCT
;
1637 cmd
= CVAL(hdr
, HDR_COM
);
1641 size_t len
= buflen
- taken
;
1643 struct iovec
*iov_tmp
;
1650 * we need at least WCT and BCC
1652 needed
= sizeof(uint8_t) + sizeof(uint16_t);
1654 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1655 __location__
, (int)len
, (int)needed
));
1660 * Now we check if the specified words are there
1662 wct
= CVAL(hdr
, wct_ofs
);
1663 needed
+= wct
* sizeof(uint16_t);
1665 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1666 __location__
, (int)len
, (int)needed
));
1671 * Now we check if the specified bytes are there
1673 bcc_ofs
= wct_ofs
+ sizeof(uint8_t) + wct
* sizeof(uint16_t);
1674 bcc
= SVAL(hdr
, bcc_ofs
);
1675 needed
+= bcc
* sizeof(uint8_t);
1677 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1678 __location__
, (int)len
, (int)needed
));
1683 * we allocate 2 iovec structures for words and bytes
1685 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
1687 if (iov_tmp
== NULL
) {
1689 return NT_STATUS_NO_MEMORY
;
1692 cur
= &iov
[num_iov
];
1695 cur
[0].iov_len
= wct
* sizeof(uint16_t);
1696 cur
[0].iov_base
= hdr
+ (wct_ofs
+ sizeof(uint8_t));
1697 cur
[1].iov_len
= bcc
* sizeof(uint8_t);
1698 cur
[1].iov_base
= hdr
+ (bcc_ofs
+ sizeof(uint16_t));
1702 if (!smb1cli_is_andx_req(cmd
)) {
1704 * If the current command does not have AndX chanining
1710 if (wct
== 0 && bcc
== 0) {
1712 * An empty response also ends the chain,
1713 * most likely with an error.
1719 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1720 __location__
, (int)wct
, (int)cmd
));
1723 cmd
= CVAL(cur
[0].iov_base
, 0);
1726 * If it is the end of the chain we are also done.
1730 wct_ofs
= SVAL(cur
[0].iov_base
, 2);
1732 if (wct_ofs
< taken
) {
1733 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1735 if (wct_ofs
> buflen
) {
1736 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1740 * we consumed everything up to the start of the next
1746 remaining
= buflen
- taken
;
1748 if (remaining
> 0 && num_iov
>= 3) {
1750 * The last DATA block gets the remaining
1751 * bytes, this is needed to support
1752 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1754 iov
[num_iov
-1].iov_len
+= remaining
;
1758 *pnum_iov
= num_iov
;
1759 return NT_STATUS_OK
;
1763 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1766 static NTSTATUS
smb1cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
1767 TALLOC_CTX
*tmp_mem
,
1770 struct tevent_req
*req
;
1771 struct smbXcli_req_state
*state
;
1778 const uint8_t *inhdr
= inbuf
+ NBT_HDR_SIZE
;
1779 struct iovec
*iov
= NULL
;
1781 struct tevent_req
**chain
= NULL
;
1782 size_t num_chained
= 0;
1783 size_t num_responses
= 0;
1785 if (conn
->smb1
.read_braw_req
!= NULL
) {
1786 req
= conn
->smb1
.read_braw_req
;
1787 conn
->smb1
.read_braw_req
= NULL
;
1788 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1790 smbXcli_req_unset_pending(req
);
1792 if (state
->smb1
.recv_iov
== NULL
) {
1794 * For requests with more than
1795 * one response, we have to readd the
1798 state
->smb1
.recv_iov
= talloc_zero_array(state
,
1801 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
1802 return NT_STATUS_OK
;
1806 state
->smb1
.recv_iov
[0].iov_base
= (void *)(inbuf
+ NBT_HDR_SIZE
);
1807 state
->smb1
.recv_iov
[0].iov_len
= smb_len_nbt(inbuf
);
1808 ZERO_STRUCT(state
->smb1
.recv_iov
[1]);
1809 ZERO_STRUCT(state
->smb1
.recv_iov
[2]);
1811 state
->smb1
.recv_cmd
= SMBreadBraw
;
1812 state
->smb1
.recv_status
= NT_STATUS_OK
;
1813 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
1815 tevent_req_done(req
);
1816 return NT_STATUS_OK
;
1819 if ((IVAL(inhdr
, 0) != SMB_MAGIC
) /* 0xFF"SMB" */
1820 && (SVAL(inhdr
, 0) != 0x45ff)) /* 0xFF"E" */ {
1821 DEBUG(10, ("Got non-SMB PDU\n"));
1822 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1826 * If we supported multiple encrytion contexts
1827 * here we'd look up based on tid.
1829 if (common_encryption_on(conn
->smb1
.trans_enc
)
1830 && (CVAL(inbuf
, 0) == 0)) {
1831 uint16_t enc_ctx_num
;
1833 status
= get_enc_ctx_num(inbuf
, &enc_ctx_num
);
1834 if (!NT_STATUS_IS_OK(status
)) {
1835 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1836 nt_errstr(status
)));
1840 if (enc_ctx_num
!= conn
->smb1
.trans_enc
->enc_ctx_num
) {
1841 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1843 conn
->smb1
.trans_enc
->enc_ctx_num
));
1844 return NT_STATUS_INVALID_HANDLE
;
1847 status
= common_decrypt_buffer(conn
->smb1
.trans_enc
,
1849 if (!NT_STATUS_IS_OK(status
)) {
1850 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1851 nt_errstr(status
)));
1856 mid
= SVAL(inhdr
, HDR_MID
);
1857 num_pending
= talloc_array_length(conn
->pending
);
1859 for (i
=0; i
<num_pending
; i
++) {
1860 if (mid
== smb1cli_req_mid(conn
->pending
[i
])) {
1864 if (i
== num_pending
) {
1865 /* Dump unexpected reply */
1866 return NT_STATUS_RETRY
;
1869 oplock_break
= false;
1871 if (mid
== 0xffff) {
1873 * Paranoia checks that this is really an oplock break request.
1875 oplock_break
= (smb_len_nbt(inbuf
) == 51); /* hdr + 8 words */
1876 oplock_break
&= ((CVAL(inhdr
, HDR_FLG
) & FLAG_REPLY
) == 0);
1877 oplock_break
&= (CVAL(inhdr
, HDR_COM
) == SMBlockingX
);
1878 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(6)) == 0);
1879 oplock_break
&= (SVAL(inhdr
, HDR_VWV
+VWV(7)) == 0);
1881 if (!oplock_break
) {
1882 /* Dump unexpected reply */
1883 return NT_STATUS_RETRY
;
1887 req
= conn
->pending
[i
];
1888 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1890 if (!oplock_break
/* oplock breaks are not signed */
1891 && !smb_signing_check_pdu(conn
->smb1
.signing
,
1892 inbuf
, state
->smb1
.seqnum
+1)) {
1893 DEBUG(10, ("cli_check_sign_mac failed\n"));
1894 return NT_STATUS_ACCESS_DENIED
;
1897 status
= smb1cli_inbuf_parse_chain(inbuf
, tmp_mem
,
1899 if (!NT_STATUS_IS_OK(status
)) {
1900 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1901 nt_errstr(status
)));
1905 cmd
= CVAL(inhdr
, HDR_COM
);
1906 status
= smb1cli_pull_raw_error(inhdr
);
1908 if (state
->smb1
.chained_requests
== NULL
) {
1910 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1913 smbXcli_req_unset_pending(req
);
1915 if (state
->smb1
.recv_iov
== NULL
) {
1917 * For requests with more than
1918 * one response, we have to readd the
1921 state
->smb1
.recv_iov
= talloc_zero_array(state
,
1924 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
1925 return NT_STATUS_OK
;
1929 state
->smb1
.recv_cmd
= cmd
;
1930 state
->smb1
.recv_status
= status
;
1931 state
->inbuf
= talloc_move(state
->smb1
.recv_iov
, &inbuf
);
1933 state
->smb1
.recv_iov
[0] = iov
[0];
1934 state
->smb1
.recv_iov
[1] = iov
[1];
1935 state
->smb1
.recv_iov
[2] = iov
[2];
1937 if (talloc_array_length(conn
->pending
) == 0) {
1938 tevent_req_done(req
);
1939 return NT_STATUS_OK
;
1942 tevent_req_defer_callback(req
, state
->ev
);
1943 tevent_req_done(req
);
1944 return NT_STATUS_RETRY
;
1947 chain
= talloc_move(tmp_mem
, &state
->smb1
.chained_requests
);
1948 num_chained
= talloc_array_length(chain
);
1949 num_responses
= (num_iov
- 1)/2;
1951 if (num_responses
> num_chained
) {
1952 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1955 for (i
=0; i
<num_chained
; i
++) {
1956 size_t iov_idx
= 1 + (i
*2);
1957 struct iovec
*cur
= &iov
[iov_idx
];
1961 state
= tevent_req_data(req
, struct smbXcli_req_state
);
1963 smbXcli_req_unset_pending(req
);
1966 * as we finish multiple requests here
1967 * we need to defer the callbacks as
1968 * they could destroy our current stack state.
1970 tevent_req_defer_callback(req
, state
->ev
);
1972 if (i
>= num_responses
) {
1973 tevent_req_nterror(req
, NT_STATUS_REQUEST_ABORTED
);
1977 if (state
->smb1
.recv_iov
== NULL
) {
1979 * For requests with more than
1980 * one response, we have to readd the
1983 state
->smb1
.recv_iov
= talloc_zero_array(state
,
1986 if (tevent_req_nomem(state
->smb1
.recv_iov
, req
)) {
1991 state
->smb1
.recv_cmd
= cmd
;
1993 if (i
== (num_responses
- 1)) {
1995 * The last request in the chain gets the status
1997 state
->smb1
.recv_status
= status
;
1999 cmd
= CVAL(cur
[0].iov_base
, 0);
2000 state
->smb1
.recv_status
= NT_STATUS_OK
;
2003 state
->inbuf
= inbuf
;
2006 * Note: here we use talloc_reference() in a way
2007 * that does not expose it to the caller.
2009 inbuf_ref
= talloc_reference(state
->smb1
.recv_iov
, inbuf
);
2010 if (tevent_req_nomem(inbuf_ref
, req
)) {
2014 /* copy the related buffers */
2015 state
->smb1
.recv_iov
[0] = iov
[0];
2016 state
->smb1
.recv_iov
[1] = cur
[0];
2017 state
->smb1
.recv_iov
[2] = cur
[1];
2019 tevent_req_done(req
);
2022 return NT_STATUS_RETRY
;
2025 NTSTATUS
smb1cli_req_recv(struct tevent_req
*req
,
2026 TALLOC_CTX
*mem_ctx
,
2027 struct iovec
**piov
,
2031 uint32_t *pvwv_offset
,
2032 uint32_t *pnum_bytes
,
2034 uint32_t *pbytes_offset
,
2036 const struct smb1cli_req_expected_response
*expected
,
2037 size_t num_expected
)
2039 struct smbXcli_req_state
*state
=
2040 tevent_req_data(req
,
2041 struct smbXcli_req_state
);
2042 NTSTATUS status
= NT_STATUS_OK
;
2043 struct iovec
*recv_iov
= NULL
;
2044 uint8_t *hdr
= NULL
;
2046 uint32_t vwv_offset
= 0;
2047 uint16_t *vwv
= NULL
;
2048 uint32_t num_bytes
= 0;
2049 uint32_t bytes_offset
= 0;
2050 uint8_t *bytes
= NULL
;
2052 bool found_status
= false;
2053 bool found_size
= false;
2067 if (pvwv_offset
!= NULL
) {
2070 if (pnum_bytes
!= NULL
) {
2073 if (pbytes
!= NULL
) {
2076 if (pbytes_offset
!= NULL
) {
2079 if (pinbuf
!= NULL
) {
2083 if (state
->inbuf
!= NULL
) {
2084 recv_iov
= state
->smb1
.recv_iov
;
2085 state
->smb1
.recv_iov
= NULL
;
2086 if (state
->smb1
.recv_cmd
!= SMBreadBraw
) {
2087 hdr
= (uint8_t *)recv_iov
[0].iov_base
;
2088 wct
= recv_iov
[1].iov_len
/2;
2089 vwv
= (uint16_t *)recv_iov
[1].iov_base
;
2090 vwv_offset
= PTR_DIFF(vwv
, hdr
);
2091 num_bytes
= recv_iov
[2].iov_len
;
2092 bytes
= (uint8_t *)recv_iov
[2].iov_base
;
2093 bytes_offset
= PTR_DIFF(bytes
, hdr
);
2097 if (tevent_req_is_nterror(req
, &status
)) {
2098 for (i
=0; i
< num_expected
; i
++) {
2099 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2100 found_status
= true;
2106 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
2112 if (num_expected
== 0) {
2113 found_status
= true;
2117 status
= state
->smb1
.recv_status
;
2119 for (i
=0; i
< num_expected
; i
++) {
2120 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
2124 found_status
= true;
2125 if (expected
[i
].wct
== 0) {
2130 if (expected
[i
].wct
== wct
) {
2136 if (!found_status
) {
2141 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
2145 *piov
= talloc_move(mem_ctx
, &recv_iov
);
2157 if (pvwv_offset
!= NULL
) {
2158 *pvwv_offset
= vwv_offset
;
2160 if (pnum_bytes
!= NULL
) {
2161 *pnum_bytes
= num_bytes
;
2163 if (pbytes
!= NULL
) {
2166 if (pbytes_offset
!= NULL
) {
2167 *pbytes_offset
= bytes_offset
;
2169 if (pinbuf
!= NULL
) {
2170 *pinbuf
= state
->inbuf
;
2176 size_t smb1cli_req_wct_ofs(struct tevent_req
**reqs
, int num_reqs
)
2183 for (i
=0; i
<num_reqs
; i
++) {
2184 struct smbXcli_req_state
*state
;
2185 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2186 wct_ofs
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2187 state
->smb1
.iov_count
-2);
2188 wct_ofs
= (wct_ofs
+ 3) & ~3;
2193 NTSTATUS
smb1cli_req_chain_submit(struct tevent_req
**reqs
, int num_reqs
)
2195 struct smbXcli_req_state
*first_state
=
2196 tevent_req_data(reqs
[0],
2197 struct smbXcli_req_state
);
2198 struct smbXcli_req_state
*state
;
2200 size_t chain_padding
= 0;
2202 struct iovec
*iov
= NULL
;
2203 struct iovec
*this_iov
;
2207 if (num_reqs
== 1) {
2208 return smb1cli_req_writev_submit(reqs
[0], first_state
,
2209 first_state
->smb1
.iov
,
2210 first_state
->smb1
.iov_count
);
2214 for (i
=0; i
<num_reqs
; i
++) {
2215 if (!tevent_req_is_in_progress(reqs
[i
])) {
2216 return NT_STATUS_INTERNAL_ERROR
;
2219 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2221 if (state
->smb1
.iov_count
< 4) {
2222 return NT_STATUS_INVALID_PARAMETER_MIX
;
2227 * The NBT and SMB header
2240 iovlen
+= state
->smb1
.iov_count
- 2;
2243 iov
= talloc_zero_array(first_state
, struct iovec
, iovlen
);
2245 return NT_STATUS_NO_MEMORY
;
2248 first_state
->smb1
.chained_requests
= (struct tevent_req
**)talloc_memdup(
2249 first_state
, reqs
, sizeof(*reqs
) * num_reqs
);
2250 if (first_state
->smb1
.chained_requests
== NULL
) {
2252 return NT_STATUS_NO_MEMORY
;
2255 wct_offset
= HDR_WCT
;
2258 for (i
=0; i
<num_reqs
; i
++) {
2259 size_t next_padding
= 0;
2262 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2264 if (i
< num_reqs
-1) {
2265 if (!smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))
2266 || CVAL(state
->smb1
.hdr
, HDR_WCT
) < 2) {
2268 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2269 return NT_STATUS_INVALID_PARAMETER_MIX
;
2273 wct_offset
+= smbXcli_iov_len(state
->smb1
.iov
+2,
2274 state
->smb1
.iov_count
-2) + 1;
2275 if ((wct_offset
% 4) != 0) {
2276 next_padding
= 4 - (wct_offset
% 4);
2278 wct_offset
+= next_padding
;
2279 vwv
= state
->smb1
.vwv
;
2281 if (i
< num_reqs
-1) {
2282 struct smbXcli_req_state
*next_state
=
2283 tevent_req_data(reqs
[i
+1],
2284 struct smbXcli_req_state
);
2285 SCVAL(vwv
+0, 0, CVAL(next_state
->smb1
.hdr
, HDR_COM
));
2287 SSVAL(vwv
+1, 0, wct_offset
);
2288 } else if (smb1cli_is_andx_req(CVAL(state
->smb1
.hdr
, HDR_COM
))) {
2289 /* properly end the chain */
2290 SCVAL(vwv
+0, 0, 0xff);
2291 SCVAL(vwv
+0, 1, 0xff);
2297 * The NBT and SMB header
2299 this_iov
[0] = state
->smb1
.iov
[0];
2300 this_iov
[1] = state
->smb1
.iov
[1];
2304 * This one is a bit subtle. We have to add
2305 * chain_padding bytes between the requests, and we
2306 * have to also include the wct field of the
2307 * subsequent requests. We use the subsequent header
2308 * for the padding, it contains the wct field in its
2311 this_iov
[0].iov_len
= chain_padding
+1;
2312 this_iov
[0].iov_base
= (void *)&state
->smb1
.hdr
[
2313 sizeof(state
->smb1
.hdr
) - this_iov
[0].iov_len
];
2314 memset(this_iov
[0].iov_base
, 0, this_iov
[0].iov_len
-1);
2319 * copy the words and bytes
2321 memcpy(this_iov
, state
->smb1
.iov
+2,
2322 sizeof(struct iovec
) * (state
->smb1
.iov_count
-2));
2323 this_iov
+= state
->smb1
.iov_count
- 2;
2324 chain_padding
= next_padding
;
2327 nbt_len
= smbXcli_iov_len(&iov
[1], iovlen
-1);
2328 if (nbt_len
> first_state
->conn
->smb1
.max_xmit
) {
2330 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2331 return NT_STATUS_INVALID_PARAMETER_MIX
;
2334 status
= smb1cli_req_writev_submit(reqs
[0], first_state
, iov
, iovlen
);
2335 if (!NT_STATUS_IS_OK(status
)) {
2337 TALLOC_FREE(first_state
->smb1
.chained_requests
);
2341 return NT_STATUS_OK
;
2344 bool smbXcli_conn_has_async_calls(struct smbXcli_conn
*conn
)
2346 return ((tevent_queue_length(conn
->outgoing
) != 0)
2347 || (talloc_array_length(conn
->pending
) != 0));
2350 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn
*conn
)
2352 return conn
->smb2
.server
.capabilities
;
2355 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn
*conn
)
2357 return conn
->smb2
.server
.security_mode
;
2360 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn
*conn
)
2362 return conn
->smb2
.server
.max_trans_size
;
2365 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn
*conn
)
2367 return conn
->smb2
.server
.max_read_size
;
2370 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn
*conn
)
2372 return conn
->smb2
.server
.max_write_size
;
2375 void smb2cli_conn_set_max_credits(struct smbXcli_conn
*conn
,
2376 uint16_t max_credits
)
2378 conn
->smb2
.max_credits
= max_credits
;
2381 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
);
2383 static bool smb2cli_req_cancel(struct tevent_req
*req
)
2385 struct smbXcli_req_state
*state
=
2386 tevent_req_data(req
,
2387 struct smbXcli_req_state
);
2388 uint32_t flags
= IVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
2389 uint64_t mid
= BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
);
2390 uint64_t aid
= BVAL(state
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
);
2391 struct smbXcli_tcon
*tcon
= state
->tcon
;
2392 struct smbXcli_session
*session
= state
->session
;
2393 uint8_t *fixed
= state
->smb2
.pad
;
2394 uint16_t fixed_len
= 4;
2395 struct tevent_req
*subreq
;
2396 struct smbXcli_req_state
*substate
;
2399 SSVAL(fixed
, 0, 0x04);
2402 subreq
= smb2cli_req_create(state
, state
->ev
,
2410 if (subreq
== NULL
) {
2413 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
2415 if (flags
& SMB2_HDR_FLAG_ASYNC
) {
2419 SIVAL(substate
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
2420 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
2421 SBVAL(substate
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, aid
);
2423 status
= smb2cli_req_compound_submit(&subreq
, 1);
2424 if (!NT_STATUS_IS_OK(status
)) {
2425 TALLOC_FREE(subreq
);
2429 tevent_req_set_callback(subreq
, smb2cli_req_cancel_done
, NULL
);
2434 static void smb2cli_req_cancel_done(struct tevent_req
*subreq
)
2436 /* we do not care about the result */
2437 TALLOC_FREE(subreq
);
2440 struct tevent_req
*smb2cli_req_create(TALLOC_CTX
*mem_ctx
,
2441 struct tevent_context
*ev
,
2442 struct smbXcli_conn
*conn
,
2444 uint32_t additional_flags
,
2445 uint32_t clear_flags
,
2446 uint32_t timeout_msec
,
2447 struct smbXcli_tcon
*tcon
,
2448 struct smbXcli_session
*session
,
2449 const uint8_t *fixed
,
2454 struct tevent_req
*req
;
2455 struct smbXcli_req_state
*state
;
2459 bool use_channel_sequence
= false;
2460 uint16_t channel_sequence
= 0;
2462 req
= tevent_req_create(mem_ctx
, &state
,
2463 struct smbXcli_req_state
);
2470 state
->session
= session
;
2473 if (conn
->smb2
.server
.capabilities
& SMB2_CAP_PERSISTENT_HANDLES
) {
2474 use_channel_sequence
= true;
2475 } else if (conn
->smb2
.server
.capabilities
& SMB2_CAP_MULTI_CHANNEL
) {
2476 use_channel_sequence
= true;
2480 uid
= session
->smb2
->session_id
;
2482 if (use_channel_sequence
) {
2483 channel_sequence
= session
->smb2
->channel_sequence
;
2486 state
->smb2
.should_sign
= session
->smb2
->should_sign
;
2487 state
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
2489 if (cmd
== SMB2_OP_SESSSETUP
&&
2490 session
->smb2
->signing_key
.length
!= 0) {
2491 state
->smb2
.should_sign
= true;
2494 if (cmd
== SMB2_OP_SESSSETUP
&&
2495 session
->smb2_channel
.signing_key
.length
== 0) {
2496 state
->smb2
.should_encrypt
= false;
2501 tid
= tcon
->smb2
.tcon_id
;
2503 if (tcon
->smb2
.should_encrypt
) {
2504 state
->smb2
.should_encrypt
= true;
2508 if (state
->smb2
.should_encrypt
) {
2509 state
->smb2
.should_sign
= false;
2512 state
->smb2
.recv_iov
= talloc_zero_array(state
, struct iovec
, 3);
2513 if (state
->smb2
.recv_iov
== NULL
) {
2518 flags
|= additional_flags
;
2519 flags
&= ~clear_flags
;
2521 state
->smb2
.fixed
= fixed
;
2522 state
->smb2
.fixed_len
= fixed_len
;
2523 state
->smb2
.dyn
= dyn
;
2524 state
->smb2
.dyn_len
= dyn_len
;
2526 if (state
->smb2
.should_encrypt
) {
2527 SIVAL(state
->smb2
.transform
, SMB2_TF_PROTOCOL_ID
, SMB2_TF_MAGIC
);
2528 SBVAL(state
->smb2
.transform
, SMB2_TF_SESSION_ID
, uid
);
2531 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PROTOCOL_ID
, SMB2_MAGIC
);
2532 SSVAL(state
->smb2
.hdr
, SMB2_HDR_LENGTH
, SMB2_HDR_BODY
);
2533 SSVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
, cmd
);
2534 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CHANNEL_SEQUENCE
, channel_sequence
);
2535 SIVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, flags
);
2536 SIVAL(state
->smb2
.hdr
, SMB2_HDR_PID
, 0); /* reserved */
2537 SIVAL(state
->smb2
.hdr
, SMB2_HDR_TID
, tid
);
2538 SBVAL(state
->smb2
.hdr
, SMB2_HDR_SESSION_ID
, uid
);
2541 case SMB2_OP_CANCEL
:
2542 state
->one_way
= true;
2546 * If this is a dummy request, it will have
2547 * UINT64_MAX as message id.
2548 * If we send on break acknowledgement,
2549 * this gets overwritten later.
2551 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, UINT64_MAX
);
2555 if (timeout_msec
> 0) {
2556 struct timeval endtime
;
2558 endtime
= timeval_current_ofs_msec(timeout_msec
);
2559 if (!tevent_req_set_endtime(req
, ev
, endtime
)) {
2567 void smb2cli_req_set_notify_async(struct tevent_req
*req
)
2569 struct smbXcli_req_state
*state
=
2570 tevent_req_data(req
,
2571 struct smbXcli_req_state
);
2573 state
->smb2
.notify_async
= true;
2576 static void smb2cli_req_writev_done(struct tevent_req
*subreq
);
2577 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
2578 TALLOC_CTX
*tmp_mem
,
2581 NTSTATUS
smb2cli_req_compound_submit(struct tevent_req
**reqs
,
2584 struct smbXcli_req_state
*state
;
2585 struct tevent_req
*subreq
;
2587 int i
, num_iov
, nbt_len
;
2590 * 1 for the nbt length
2591 * per request: TRANSFORM, HDR, fixed, dyn, padding
2592 * -1 because the last one does not need padding
2595 iov
= talloc_array(reqs
[0], struct iovec
, 1 + 5*num_reqs
- 1);
2597 return NT_STATUS_NO_MEMORY
;
2603 for (i
=0; i
<num_reqs
; i
++) {
2613 const DATA_BLOB
*signing_key
= NULL
;
2614 const DATA_BLOB
*encryption_key
= NULL
;
2616 if (!tevent_req_is_in_progress(reqs
[i
])) {
2617 return NT_STATUS_INTERNAL_ERROR
;
2620 state
= tevent_req_data(reqs
[i
], struct smbXcli_req_state
);
2622 if (!smbXcli_conn_is_connected(state
->conn
)) {
2623 return NT_STATUS_CONNECTION_DISCONNECTED
;
2626 if ((state
->conn
->protocol
!= PROTOCOL_NONE
) &&
2627 (state
->conn
->protocol
< PROTOCOL_SMB2_02
)) {
2628 return NT_STATUS_REVISION_MISMATCH
;
2631 opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
2632 if (opcode
== SMB2_OP_CANCEL
) {
2636 avail
= UINT64_MAX
- state
->conn
->smb2
.mid
;
2638 return NT_STATUS_CONNECTION_ABORTED
;
2641 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2642 charge
= (MAX(state
->smb2
.dyn_len
, 1) - 1)/ 65536 + 1;
2647 charge
= MAX(state
->smb2
.credit_charge
, charge
);
2649 avail
= MIN(avail
, state
->conn
->smb2
.cur_credits
);
2650 if (avail
< charge
) {
2651 return NT_STATUS_INTERNAL_ERROR
;
2655 if (state
->conn
->smb2
.max_credits
> state
->conn
->smb2
.cur_credits
) {
2656 credits
= state
->conn
->smb2
.max_credits
-
2657 state
->conn
->smb2
.cur_credits
;
2659 if (state
->conn
->smb2
.max_credits
>= state
->conn
->smb2
.cur_credits
) {
2663 mid
= state
->conn
->smb2
.mid
;
2664 state
->conn
->smb2
.mid
+= charge
;
2665 state
->conn
->smb2
.cur_credits
-= charge
;
2667 if (state
->conn
->smb2
.server
.capabilities
& SMB2_CAP_LARGE_MTU
) {
2668 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT_CHARGE
, charge
);
2670 SSVAL(state
->smb2
.hdr
, SMB2_HDR_CREDIT
, credits
);
2671 SBVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
, mid
);
2674 if (state
->session
) {
2676 * We prefer the channel signing key if it is
2679 if (state
->smb2
.should_sign
) {
2680 signing_key
= &state
->session
->smb2_channel
.signing_key
;
2684 * If it is a channel binding, we already have the main
2685 * signing key and try that one.
2687 if (signing_key
&& signing_key
->length
== 0) {
2688 signing_key
= &state
->session
->smb2
->signing_key
;
2692 * If we do not have any session key yet, we skip the
2693 * signing of SMB2_OP_SESSSETUP requests.
2695 if (signing_key
&& signing_key
->length
== 0) {
2699 if (state
->smb2
.should_encrypt
) {
2700 encryption_key
= &state
->session
->smb2
->encryption_key
;
2704 if (encryption_key
) {
2706 iov
[num_iov
].iov_base
= state
->smb2
.transform
;
2707 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.transform
);
2712 iov
[num_iov
].iov_base
= state
->smb2
.hdr
;
2713 iov
[num_iov
].iov_len
= sizeof(state
->smb2
.hdr
);
2716 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.fixed
);
2717 iov
[num_iov
].iov_len
= state
->smb2
.fixed_len
;
2720 if (state
->smb2
.dyn
!= NULL
) {
2721 iov
[num_iov
].iov_base
= discard_const(state
->smb2
.dyn
);
2722 iov
[num_iov
].iov_len
= state
->smb2
.dyn_len
;
2726 reqlen
= sizeof(state
->smb2
.hdr
);
2727 reqlen
+= state
->smb2
.fixed_len
;
2728 reqlen
+= state
->smb2
.dyn_len
;
2730 if (i
< num_reqs
-1) {
2731 if ((reqlen
% 8) > 0) {
2732 uint8_t pad
= 8 - (reqlen
% 8);
2733 iov
[num_iov
].iov_base
= state
->smb2
.pad
;
2734 iov
[num_iov
].iov_len
= pad
;
2738 SIVAL(state
->smb2
.hdr
, SMB2_HDR_NEXT_COMMAND
, reqlen
);
2741 if (encryption_key
) {
2746 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
,
2747 state
->session
->smb2
->nonce_low
);
2748 SBVAL(state
->smb2
.transform
, SMB2_TF_NONCE
+8,
2749 state
->session
->smb2
->nonce_high
);
2751 state
->session
->smb2
->nonce_low
+= 1;
2752 if (state
->session
->smb2
->nonce_low
== 0) {
2753 state
->session
->smb2
->nonce_high
+= 1;
2754 state
->session
->smb2
->nonce_low
+= 1;
2757 SBVAL(state
->smb2
.transform
, SMB2_TF_MSG_SIZE
,
2760 buf
= talloc_array(iov
, uint8_t, reqlen
);
2762 return NT_STATUS_NO_MEMORY
;
2765 reqlen
+= SMB2_TF_HDR_SIZE
;
2768 * We copy the buffers before encrypting them,
2769 * this is at least currently needed for the
2770 * to keep state->smb2.hdr.
2772 * Also the callers may expect there buffers
2775 for (vi
= hdr_iov
; vi
< num_iov
; vi
++) {
2776 struct iovec
*v
= &iov
[vi
];
2777 const uint8_t *o
= (const uint8_t *)v
->iov_base
;
2779 memcpy(buf
, o
, v
->iov_len
);
2780 v
->iov_base
= (void *)buf
;
2784 status
= smb2_signing_encrypt_pdu(*encryption_key
,
2785 state
->session
->conn
->protocol
,
2786 &iov
[tf_iov
], num_iov
- tf_iov
);
2787 if (!NT_STATUS_IS_OK(status
)) {
2790 } else if (signing_key
) {
2793 status
= smb2_signing_sign_pdu(*signing_key
,
2794 state
->session
->conn
->protocol
,
2795 &iov
[hdr_iov
], num_iov
- hdr_iov
);
2796 if (!NT_STATUS_IS_OK(status
)) {
2803 ret
= smbXcli_req_set_pending(reqs
[i
]);
2805 return NT_STATUS_NO_MEMORY
;
2809 state
= tevent_req_data(reqs
[0], struct smbXcli_req_state
);
2810 _smb_setlen_tcp(state
->length_hdr
, nbt_len
);
2811 iov
[0].iov_base
= state
->length_hdr
;
2812 iov
[0].iov_len
= sizeof(state
->length_hdr
);
2814 if (state
->conn
->dispatch_incoming
== NULL
) {
2815 state
->conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
2818 subreq
= writev_send(state
, state
->ev
, state
->conn
->outgoing
,
2819 state
->conn
->write_fd
, false, iov
, num_iov
);
2820 if (subreq
== NULL
) {
2821 return NT_STATUS_NO_MEMORY
;
2823 tevent_req_set_callback(subreq
, smb2cli_req_writev_done
, reqs
[0]);
2824 return NT_STATUS_OK
;
2827 void smb2cli_req_set_credit_charge(struct tevent_req
*req
, uint16_t charge
)
2829 struct smbXcli_req_state
*state
=
2830 tevent_req_data(req
,
2831 struct smbXcli_req_state
);
2833 state
->smb2
.credit_charge
= charge
;
2836 struct tevent_req
*smb2cli_req_send(TALLOC_CTX
*mem_ctx
,
2837 struct tevent_context
*ev
,
2838 struct smbXcli_conn
*conn
,
2840 uint32_t additional_flags
,
2841 uint32_t clear_flags
,
2842 uint32_t timeout_msec
,
2843 struct smbXcli_tcon
*tcon
,
2844 struct smbXcli_session
*session
,
2845 const uint8_t *fixed
,
2850 struct tevent_req
*req
;
2853 req
= smb2cli_req_create(mem_ctx
, ev
, conn
, cmd
,
2854 additional_flags
, clear_flags
,
2857 fixed
, fixed_len
, dyn
, dyn_len
);
2861 if (!tevent_req_is_in_progress(req
)) {
2862 return tevent_req_post(req
, ev
);
2864 status
= smb2cli_req_compound_submit(&req
, 1);
2865 if (tevent_req_nterror(req
, status
)) {
2866 return tevent_req_post(req
, ev
);
2871 static void smb2cli_req_writev_done(struct tevent_req
*subreq
)
2873 struct tevent_req
*req
=
2874 tevent_req_callback_data(subreq
,
2876 struct smbXcli_req_state
*state
=
2877 tevent_req_data(req
,
2878 struct smbXcli_req_state
);
2882 nwritten
= writev_recv(subreq
, &err
);
2883 TALLOC_FREE(subreq
);
2884 if (nwritten
== -1) {
2885 /* here, we need to notify all pending requests */
2886 NTSTATUS status
= map_nt_error_from_unix_common(err
);
2887 smbXcli_conn_disconnect(state
->conn
, status
);
2892 static NTSTATUS
smb2cli_inbuf_parse_compound(struct smbXcli_conn
*conn
,
2895 TALLOC_CTX
*mem_ctx
,
2896 struct iovec
**piov
, int *pnum_iov
)
2901 uint8_t *first_hdr
= buf
;
2903 iov
= talloc_array(mem_ctx
, struct iovec
, num_iov
);
2905 return NT_STATUS_NO_MEMORY
;
2908 while (taken
< buflen
) {
2911 size_t len
= buflen
- taken
;
2912 uint8_t *hdr
= first_hdr
+ taken
;
2915 size_t next_command_ofs
;
2917 struct iovec
*iov_tmp
;
2920 DEBUG(10, ("%d bytes left, expected at least %d\n",
2924 if (IVAL(hdr
, 0) == SMB2_TF_MAGIC
) {
2925 struct smbXcli_session
*s
;
2927 struct iovec tf_iov
[2];
2930 if (len
< SMB2_TF_HDR_SIZE
) {
2931 DEBUG(10, ("%d bytes left, expected at least %d\n",
2932 (int)len
, SMB2_TF_HDR_SIZE
));
2936 tf_len
= SMB2_TF_HDR_SIZE
;
2939 hdr
= first_hdr
+ taken
;
2940 len
= IVAL(tf
, SMB2_TF_MSG_SIZE
);
2941 uid
= BVAL(tf
, SMB2_TF_SESSION_ID
);
2944 for (; s
; s
= s
->next
) {
2945 if (s
->smb2
->session_id
!= uid
) {
2952 DEBUG(10, ("unknown session_id %llu\n",
2953 (unsigned long long)uid
));
2957 tf_iov
[0].iov_base
= (void *)tf
;
2958 tf_iov
[0].iov_len
= tf_len
;
2959 tf_iov
[1].iov_base
= (void *)hdr
;
2960 tf_iov
[1].iov_len
= len
;
2962 status
= smb2_signing_decrypt_pdu(s
->smb2
->decryption_key
,
2965 if (!NT_STATUS_IS_OK(status
)) {
2972 * We need the header plus the body length field
2975 if (len
< SMB2_HDR_BODY
+ 2) {
2976 DEBUG(10, ("%d bytes left, expected at least %d\n",
2977 (int)len
, SMB2_HDR_BODY
));
2980 if (IVAL(hdr
, 0) != SMB2_MAGIC
) {
2981 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2985 if (SVAL(hdr
, 4) != SMB2_HDR_BODY
) {
2986 DEBUG(10, ("Got HDR len %d, expected %d\n",
2987 SVAL(hdr
, 4), SMB2_HDR_BODY
));
2992 next_command_ofs
= IVAL(hdr
, SMB2_HDR_NEXT_COMMAND
);
2993 body_size
= SVAL(hdr
, SMB2_HDR_BODY
);
2995 if (next_command_ofs
!= 0) {
2996 if (next_command_ofs
< (SMB2_HDR_BODY
+ 2)) {
2999 if (next_command_ofs
> full_size
) {
3002 if (tf
&& next_command_ofs
< len
) {
3005 full_size
= next_command_ofs
;
3007 if (body_size
< 2) {
3010 body_size
&= 0xfffe;
3012 if (body_size
> (full_size
- SMB2_HDR_BODY
)) {
3016 iov_tmp
= talloc_realloc(mem_ctx
, iov
, struct iovec
,
3018 if (iov_tmp
== NULL
) {
3020 return NT_STATUS_NO_MEMORY
;
3023 cur
= &iov
[num_iov
];
3026 cur
[0].iov_base
= tf
;
3027 cur
[0].iov_len
= tf_len
;
3028 cur
[1].iov_base
= hdr
;
3029 cur
[1].iov_len
= SMB2_HDR_BODY
;
3030 cur
[2].iov_base
= hdr
+ SMB2_HDR_BODY
;
3031 cur
[2].iov_len
= body_size
;
3032 cur
[3].iov_base
= hdr
+ SMB2_HDR_BODY
+ body_size
;
3033 cur
[3].iov_len
= full_size
- (SMB2_HDR_BODY
+ body_size
);
3039 *pnum_iov
= num_iov
;
3040 return NT_STATUS_OK
;
3044 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3047 static struct tevent_req
*smb2cli_conn_find_pending(struct smbXcli_conn
*conn
,
3050 size_t num_pending
= talloc_array_length(conn
->pending
);
3053 for (i
=0; i
<num_pending
; i
++) {
3054 struct tevent_req
*req
= conn
->pending
[i
];
3055 struct smbXcli_req_state
*state
=
3056 tevent_req_data(req
,
3057 struct smbXcli_req_state
);
3059 if (mid
== BVAL(state
->smb2
.hdr
, SMB2_HDR_MESSAGE_ID
)) {
3066 static NTSTATUS
smb2cli_conn_dispatch_incoming(struct smbXcli_conn
*conn
,
3067 TALLOC_CTX
*tmp_mem
,
3070 struct tevent_req
*req
;
3071 struct smbXcli_req_state
*state
= NULL
;
3076 struct smbXcli_session
*last_session
= NULL
;
3077 size_t inbuf_len
= smb_len_tcp(inbuf
);
3079 status
= smb2cli_inbuf_parse_compound(conn
,
3080 inbuf
+ NBT_HDR_SIZE
,
3084 if (!NT_STATUS_IS_OK(status
)) {
3088 for (i
=0; i
<num_iov
; i
+=4) {
3089 uint8_t *inbuf_ref
= NULL
;
3090 struct iovec
*cur
= &iov
[i
];
3091 uint8_t *inhdr
= (uint8_t *)cur
[1].iov_base
;
3092 uint16_t opcode
= SVAL(inhdr
, SMB2_HDR_OPCODE
);
3093 uint32_t flags
= IVAL(inhdr
, SMB2_HDR_FLAGS
);
3094 uint64_t mid
= BVAL(inhdr
, SMB2_HDR_MESSAGE_ID
);
3095 uint16_t req_opcode
;
3097 uint16_t credits
= SVAL(inhdr
, SMB2_HDR_CREDIT
);
3098 uint32_t new_credits
;
3099 struct smbXcli_session
*session
= NULL
;
3100 const DATA_BLOB
*signing_key
= NULL
;
3102 new_credits
= conn
->smb2
.cur_credits
;
3103 new_credits
+= credits
;
3104 if (new_credits
> UINT16_MAX
) {
3105 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3107 conn
->smb2
.cur_credits
+= credits
;
3109 req
= smb2cli_conn_find_pending(conn
, mid
);
3111 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3113 state
= tevent_req_data(req
, struct smbXcli_req_state
);
3115 state
->smb2
.got_async
= false;
3117 req_opcode
= SVAL(state
->smb2
.hdr
, SMB2_HDR_OPCODE
);
3118 if (opcode
!= req_opcode
) {
3119 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3121 req_flags
= SVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
);
3123 if (!(flags
& SMB2_HDR_FLAG_REDIRECT
)) {
3124 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3127 status
= NT_STATUS(IVAL(inhdr
, SMB2_HDR_STATUS
));
3128 if ((flags
& SMB2_HDR_FLAG_ASYNC
) &&
3129 NT_STATUS_EQUAL(status
, STATUS_PENDING
)) {
3130 uint64_t async_id
= BVAL(inhdr
, SMB2_HDR_ASYNC_ID
);
3133 * async interim responses are not signed,
3134 * even if the SMB2_HDR_FLAG_SIGNED flag
3137 req_flags
|= SMB2_HDR_FLAG_ASYNC
;
3138 SBVAL(state
->smb2
.hdr
, SMB2_HDR_FLAGS
, req_flags
);
3139 SBVAL(state
->smb2
.hdr
, SMB2_HDR_ASYNC_ID
, async_id
);
3141 if (state
->smb2
.notify_async
) {
3142 state
->smb2
.got_async
= true;
3143 tevent_req_defer_callback(req
, state
->ev
);
3144 tevent_req_notify_callback(req
);
3149 session
= state
->session
;
3150 if (req_flags
& SMB2_HDR_FLAG_CHAINED
) {
3151 session
= last_session
;
3153 last_session
= session
;
3155 if (state
->smb2
.should_sign
) {
3156 if (!(flags
& SMB2_HDR_FLAG_SIGNED
)) {
3157 return NT_STATUS_ACCESS_DENIED
;
3161 if (flags
& SMB2_HDR_FLAG_SIGNED
) {
3162 uint64_t uid
= BVAL(inhdr
, SMB2_HDR_SESSION_ID
);
3164 if (session
== NULL
) {
3165 struct smbXcli_session
*s
;
3167 s
= state
->conn
->sessions
;
3168 for (; s
; s
= s
->next
) {
3169 if (s
->smb2
->session_id
!= uid
) {
3178 if (session
== NULL
) {
3179 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3182 last_session
= session
;
3183 signing_key
= &session
->smb2_channel
.signing_key
;
3186 if (opcode
== SMB2_OP_SESSSETUP
) {
3188 * We prefer the channel signing key, if it is
3191 * If we do not have a channel signing key yet,
3192 * we try the main signing key, if it is not
3193 * the final response.
3195 if (signing_key
&& signing_key
->length
== 0 &&
3196 !NT_STATUS_IS_OK(status
)) {
3197 signing_key
= &session
->smb2
->signing_key
;
3200 if (signing_key
&& signing_key
->length
== 0) {
3202 * If we do not have a session key to
3203 * verify the signature, we defer the
3204 * signing check to the caller.
3206 * The caller gets NT_STATUS_OK, it
3208 * smb2cli_session_set_session_key()
3210 * smb2cli_session_set_channel_key()
3211 * which will check the signature
3212 * with the channel signing key.
3218 if (NT_STATUS_EQUAL(status
, NT_STATUS_USER_SESSION_DELETED
)) {
3220 * if the server returns NT_STATUS_USER_SESSION_DELETED
3221 * the response is not signed and we should
3222 * propagate the NT_STATUS_USER_SESSION_DELETED
3223 * status to the caller.
3226 } else if (state
->smb2
.should_encrypt
) {
3227 if (cur
[0].iov_len
!= SMB2_TF_HDR_SIZE
) {
3228 return NT_STATUS_ACCESS_DENIED
;
3232 if (NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_NAME_DELETED
) ||
3233 NT_STATUS_EQUAL(status
, NT_STATUS_FILE_CLOSED
) ||
3234 NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
3236 * if the server returns
3237 * NT_STATUS_NETWORK_NAME_DELETED
3238 * NT_STATUS_FILE_CLOSED
3239 * NT_STATUS_INVALID_PARAMETER
3240 * the response might not be signed
3241 * as this happens before the signing checks.
3243 * If server echos the signature (or all zeros)
3244 * we should report the status from the server
3250 cmp
= memcmp(inhdr
+SMB2_HDR_SIGNATURE
,
3251 state
->smb2
.hdr
+SMB2_HDR_SIGNATURE
,
3254 state
->smb2
.signing_skipped
= true;
3260 static const uint8_t zeros
[16];
3262 cmp
= memcmp(inhdr
+SMB2_HDR_SIGNATURE
,
3266 state
->smb2
.signing_skipped
= true;
3273 status
= smb2_signing_check_pdu(*signing_key
,
3274 state
->conn
->protocol
,
3276 if (!NT_STATUS_IS_OK(status
)) {
3278 * If the signing check fails, we disconnect
3285 smbXcli_req_unset_pending(req
);
3288 * There might be more than one response
3289 * we need to defer the notifications
3291 if ((num_iov
== 5) && (talloc_array_length(conn
->pending
) == 0)) {
3296 tevent_req_defer_callback(req
, state
->ev
);
3300 * Note: here we use talloc_reference() in a way
3301 * that does not expose it to the caller.
3303 inbuf_ref
= talloc_reference(state
->smb2
.recv_iov
, inbuf
);
3304 if (tevent_req_nomem(inbuf_ref
, req
)) {
3308 /* copy the related buffers */
3309 state
->smb2
.recv_iov
[0] = cur
[1];
3310 state
->smb2
.recv_iov
[1] = cur
[2];
3311 state
->smb2
.recv_iov
[2] = cur
[3];
3313 tevent_req_done(req
);
3317 return NT_STATUS_RETRY
;
3320 return NT_STATUS_OK
;
3323 NTSTATUS
smb2cli_req_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
3324 struct iovec
**piov
,
3325 const struct smb2cli_req_expected_response
*expected
,
3326 size_t num_expected
)
3328 struct smbXcli_req_state
*state
=
3329 tevent_req_data(req
,
3330 struct smbXcli_req_state
);
3333 bool found_status
= false;
3334 bool found_size
= false;
3341 if (state
->smb2
.got_async
) {
3342 return STATUS_PENDING
;
3345 if (tevent_req_is_nterror(req
, &status
)) {
3346 for (i
=0; i
< num_expected
; i
++) {
3347 if (NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
3348 found_status
= true;
3354 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
3360 if (num_expected
== 0) {
3361 found_status
= true;
3365 status
= NT_STATUS(IVAL(state
->smb2
.recv_iov
[0].iov_base
, SMB2_HDR_STATUS
));
3366 body_size
= SVAL(state
->smb2
.recv_iov
[1].iov_base
, 0);
3368 for (i
=0; i
< num_expected
; i
++) {
3369 if (!NT_STATUS_EQUAL(status
, expected
[i
].status
)) {
3373 found_status
= true;
3374 if (expected
[i
].body_size
== 0) {
3379 if (expected
[i
].body_size
== body_size
) {
3385 if (!found_status
) {
3389 if (state
->smb2
.signing_skipped
) {
3390 if (num_expected
> 0) {
3391 return NT_STATUS_ACCESS_DENIED
;
3393 if (!NT_STATUS_IS_ERR(status
)) {
3394 return NT_STATUS_ACCESS_DENIED
;
3399 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
3403 *piov
= talloc_move(mem_ctx
, &state
->smb2
.recv_iov
);
3409 static const struct {
3410 enum protocol_types proto
;
3411 const char *smb1_name
;
3412 } smb1cli_prots
[] = {
3413 {PROTOCOL_CORE
, "PC NETWORK PROGRAM 1.0"},
3414 {PROTOCOL_COREPLUS
, "MICROSOFT NETWORKS 1.03"},
3415 {PROTOCOL_LANMAN1
, "MICROSOFT NETWORKS 3.0"},
3416 {PROTOCOL_LANMAN1
, "LANMAN1.0"},
3417 {PROTOCOL_LANMAN2
, "LM1.2X002"},
3418 {PROTOCOL_LANMAN2
, "DOS LANMAN2.1"},
3419 {PROTOCOL_LANMAN2
, "LANMAN2.1"},
3420 {PROTOCOL_LANMAN2
, "Samba"},
3421 {PROTOCOL_NT1
, "NT LANMAN 1.0"},
3422 {PROTOCOL_NT1
, "NT LM 0.12"},
3423 {PROTOCOL_SMB2_02
, "SMB 2.002"},
3424 {PROTOCOL_SMB2_10
, "SMB 2.???"},
3427 static const struct {
3428 enum protocol_types proto
;
3429 uint16_t smb2_dialect
;
3430 } smb2cli_prots
[] = {
3431 {PROTOCOL_SMB2_02
, SMB2_DIALECT_REVISION_202
},
3432 {PROTOCOL_SMB2_10
, SMB2_DIALECT_REVISION_210
},
3433 {PROTOCOL_SMB2_22
, SMB2_DIALECT_REVISION_222
},
3434 {PROTOCOL_SMB2_24
, SMB2_DIALECT_REVISION_224
},
3435 {PROTOCOL_SMB3_00
, SMB3_DIALECT_REVISION_300
},
3438 struct smbXcli_negprot_state
{
3439 struct smbXcli_conn
*conn
;
3440 struct tevent_context
*ev
;
3441 uint32_t timeout_msec
;
3442 enum protocol_types min_protocol
;
3443 enum protocol_types max_protocol
;
3447 uint8_t dyn
[ARRAY_SIZE(smb2cli_prots
)*2];
3451 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
);
3452 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
);
3453 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
);
3454 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
);
3455 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
);
3456 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
3460 struct tevent_req
*smbXcli_negprot_send(TALLOC_CTX
*mem_ctx
,
3461 struct tevent_context
*ev
,
3462 struct smbXcli_conn
*conn
,
3463 uint32_t timeout_msec
,
3464 enum protocol_types min_protocol
,
3465 enum protocol_types max_protocol
)
3467 struct tevent_req
*req
, *subreq
;
3468 struct smbXcli_negprot_state
*state
;
3470 req
= tevent_req_create(mem_ctx
, &state
,
3471 struct smbXcli_negprot_state
);
3477 state
->timeout_msec
= timeout_msec
;
3478 state
->min_protocol
= min_protocol
;
3479 state
->max_protocol
= max_protocol
;
3481 if (min_protocol
== PROTOCOL_NONE
) {
3482 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3483 return tevent_req_post(req
, ev
);
3486 if (max_protocol
== PROTOCOL_NONE
) {
3487 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3488 return tevent_req_post(req
, ev
);
3491 if (min_protocol
> max_protocol
) {
3492 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER_MIX
);
3493 return tevent_req_post(req
, ev
);
3496 if ((min_protocol
< PROTOCOL_SMB2_02
) &&
3497 (max_protocol
< PROTOCOL_SMB2_02
)) {
3501 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
3503 subreq
= smbXcli_negprot_smb1_subreq(state
);
3504 if (tevent_req_nomem(subreq
, req
)) {
3505 return tevent_req_post(req
, ev
);
3507 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
3511 if ((min_protocol
>= PROTOCOL_SMB2_02
) &&
3512 (max_protocol
>= PROTOCOL_SMB2_02
)) {
3516 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
3518 subreq
= smbXcli_negprot_smb2_subreq(state
);
3519 if (tevent_req_nomem(subreq
, req
)) {
3520 return tevent_req_post(req
, ev
);
3522 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
3527 * We send an SMB1 negprot with the SMB2 dialects
3528 * and expect a SMB1 or a SMB2 response.
3530 * smbXcli_negprot_dispatch_incoming() will fix the
3531 * callback to match protocol of the response.
3533 conn
->dispatch_incoming
= smbXcli_negprot_dispatch_incoming
;
3535 subreq
= smbXcli_negprot_smb1_subreq(state
);
3536 if (tevent_req_nomem(subreq
, req
)) {
3537 return tevent_req_post(req
, ev
);
3539 tevent_req_set_callback(subreq
, smbXcli_negprot_invalid_done
, req
);
3543 static void smbXcli_negprot_invalid_done(struct tevent_req
*subreq
)
3545 struct tevent_req
*req
=
3546 tevent_req_callback_data(subreq
,
3551 * we just want the low level error
3553 status
= tevent_req_simple_recv_ntstatus(subreq
);
3554 TALLOC_FREE(subreq
);
3555 if (tevent_req_nterror(req
, status
)) {
3559 /* this should never happen */
3560 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
3563 static struct tevent_req
*smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state
*state
)
3566 DATA_BLOB bytes
= data_blob_null
;
3570 /* setup the protocol strings */
3571 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
3575 if (smb1cli_prots
[i
].proto
< state
->min_protocol
) {
3579 if (smb1cli_prots
[i
].proto
> state
->max_protocol
) {
3583 ok
= data_blob_append(state
, &bytes
, &c
, sizeof(c
));
3589 * We now it is already ascii and
3590 * we want NULL termination.
3592 ok
= data_blob_append(state
, &bytes
,
3593 smb1cli_prots
[i
].smb1_name
,
3594 strlen(smb1cli_prots
[i
].smb1_name
)+1);
3600 smb1cli_req_flags(state
->max_protocol
,
3601 state
->conn
->smb1
.client
.capabilities
,
3606 return smb1cli_req_send(state
, state
->ev
, state
->conn
,
3610 state
->timeout_msec
,
3611 0xFFFE, 0, NULL
, /* pid, tid, session */
3612 0, NULL
, /* wct, vwv */
3613 bytes
.length
, bytes
.data
);
3616 static void smbXcli_negprot_smb1_done(struct tevent_req
*subreq
)
3618 struct tevent_req
*req
=
3619 tevent_req_callback_data(subreq
,
3621 struct smbXcli_negprot_state
*state
=
3622 tevent_req_data(req
,
3623 struct smbXcli_negprot_state
);
3624 struct smbXcli_conn
*conn
= state
->conn
;
3625 struct iovec
*recv_iov
= NULL
;
3634 size_t num_prots
= 0;
3636 uint32_t client_capabilities
= conn
->smb1
.client
.capabilities
;
3637 uint32_t both_capabilities
;
3638 uint32_t server_capabilities
= 0;
3639 uint32_t capabilities
;
3640 uint32_t client_max_xmit
= conn
->smb1
.client
.max_xmit
;
3641 uint32_t server_max_xmit
= 0;
3643 uint32_t server_max_mux
= 0;
3644 uint16_t server_security_mode
= 0;
3645 uint32_t server_session_key
= 0;
3646 bool server_readbraw
= false;
3647 bool server_writebraw
= false;
3648 bool server_lockread
= false;
3649 bool server_writeunlock
= false;
3650 struct GUID server_guid
= GUID_zero();
3651 DATA_BLOB server_gss_blob
= data_blob_null
;
3652 uint8_t server_challenge
[8];
3653 char *server_workgroup
= NULL
;
3654 char *server_name
= NULL
;
3655 int server_time_zone
= 0;
3656 NTTIME server_system_time
= 0;
3657 static const struct smb1cli_req_expected_response expected
[] = {
3659 .status
= NT_STATUS_OK
,
3660 .wct
= 0x11, /* NT1 */
3663 .status
= NT_STATUS_OK
,
3664 .wct
= 0x0D, /* LM */
3667 .status
= NT_STATUS_OK
,
3668 .wct
= 0x01, /* CORE */
3672 ZERO_STRUCT(server_challenge
);
3674 status
= smb1cli_req_recv(subreq
, state
,
3679 NULL
, /* pvwv_offset */
3682 NULL
, /* pbytes_offset */
3684 expected
, ARRAY_SIZE(expected
));
3685 TALLOC_FREE(subreq
);
3686 if (tevent_req_nterror(req
, status
)) {
3690 flags
= CVAL(inhdr
, HDR_FLG
);
3692 protnum
= SVAL(vwv
, 0);
3694 for (i
=0; i
< ARRAY_SIZE(smb1cli_prots
); i
++) {
3695 if (smb1cli_prots
[i
].proto
< state
->min_protocol
) {
3699 if (smb1cli_prots
[i
].proto
> state
->max_protocol
) {
3703 if (protnum
!= num_prots
) {
3708 conn
->protocol
= smb1cli_prots
[i
].proto
;
3712 if (conn
->protocol
== PROTOCOL_NONE
) {
3713 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3717 if ((conn
->protocol
< PROTOCOL_NT1
) && conn
->mandatory_signing
) {
3718 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3719 "and the selected protocol level doesn't support it.\n"));
3720 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
3724 if (flags
& FLAG_SUPPORT_LOCKREAD
) {
3725 server_lockread
= true;
3726 server_writeunlock
= true;
3729 if (conn
->protocol
>= PROTOCOL_NT1
) {
3730 const char *client_signing
= NULL
;
3731 bool server_mandatory
= false;
3732 bool server_allowed
= false;
3733 const char *server_signing
= NULL
;
3738 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3743 server_security_mode
= CVAL(vwv
+ 1, 0);
3744 server_max_mux
= SVAL(vwv
+ 1, 1);
3745 server_max_xmit
= IVAL(vwv
+ 3, 1);
3746 server_session_key
= IVAL(vwv
+ 7, 1);
3747 server_time_zone
= SVALS(vwv
+ 15, 1);
3748 server_time_zone
*= 60;
3749 /* this time arrives in real GMT */
3750 server_system_time
= BVAL(vwv
+ 11, 1);
3751 server_capabilities
= IVAL(vwv
+ 9, 1);
3753 key_len
= CVAL(vwv
+ 16, 1);
3755 if (server_capabilities
& CAP_RAW_MODE
) {
3756 server_readbraw
= true;
3757 server_writebraw
= true;
3759 if (server_capabilities
& CAP_LOCK_AND_READ
) {
3760 server_lockread
= true;
3763 if (server_capabilities
& CAP_EXTENDED_SECURITY
) {
3764 DATA_BLOB blob1
, blob2
;
3766 if (num_bytes
< 16) {
3767 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3771 blob1
= data_blob_const(bytes
, 16);
3772 status
= GUID_from_data_blob(&blob1
, &server_guid
);
3773 if (tevent_req_nterror(req
, status
)) {
3777 blob1
= data_blob_const(bytes
+16, num_bytes
-16);
3778 blob2
= data_blob_dup_talloc(state
, blob1
);
3779 if (blob1
.length
> 0 &&
3780 tevent_req_nomem(blob2
.data
, req
)) {
3783 server_gss_blob
= blob2
;
3785 DATA_BLOB blob1
, blob2
;
3787 if (num_bytes
< key_len
) {
3788 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3792 if (key_len
!= 0 && key_len
!= 8) {
3793 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3798 memcpy(server_challenge
, bytes
, 8);
3801 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
3802 blob2
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
3803 if (blob1
.length
> 0) {
3806 len
= utf16_len_n(blob1
.data
,
3810 ok
= convert_string_talloc(state
,
3818 status
= map_nt_error_from_unix_common(errno
);
3819 tevent_req_nterror(req
, status
);
3824 blob2
.data
+= blob1
.length
;
3825 blob2
.length
-= blob1
.length
;
3826 if (blob2
.length
> 0) {
3829 len
= utf16_len_n(blob1
.data
,
3833 ok
= convert_string_talloc(state
,
3841 status
= map_nt_error_from_unix_common(errno
);
3842 tevent_req_nterror(req
, status
);
3848 client_signing
= "disabled";
3849 if (conn
->allow_signing
) {
3850 client_signing
= "allowed";
3852 if (conn
->mandatory_signing
) {
3853 client_signing
= "required";
3856 server_signing
= "not supported";
3857 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_ENABLED
) {
3858 server_signing
= "supported";
3859 server_allowed
= true;
3861 if (server_security_mode
& NEGOTIATE_SECURITY_SIGNATURES_REQUIRED
) {
3862 server_signing
= "required";
3863 server_mandatory
= true;
3866 ok
= smb_signing_set_negotiated(conn
->smb1
.signing
,
3870 DEBUG(1,("cli_negprot: SMB signing is required, "
3871 "but client[%s] and server[%s] mismatch\n",
3872 client_signing
, server_signing
));
3873 tevent_req_nterror(req
, NT_STATUS_ACCESS_DENIED
);
3877 } else if (conn
->protocol
>= PROTOCOL_LANMAN1
) {
3883 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3887 server_security_mode
= SVAL(vwv
+ 1, 0);
3888 server_max_xmit
= SVAL(vwv
+ 2, 0);
3889 server_max_mux
= SVAL(vwv
+ 3, 0);
3890 server_readbraw
= ((SVAL(vwv
+ 5, 0) & 0x1) != 0);
3891 server_writebraw
= ((SVAL(vwv
+ 5, 0) & 0x2) != 0);
3892 server_session_key
= IVAL(vwv
+ 6, 0);
3893 server_time_zone
= SVALS(vwv
+ 10, 0);
3894 server_time_zone
*= 60;
3895 /* this time is converted to GMT by make_unix_date */
3896 t
= pull_dos_date((const uint8_t *)(vwv
+ 8), server_time_zone
);
3897 unix_to_nt_time(&server_system_time
, t
);
3898 key_len
= SVAL(vwv
+ 11, 0);
3900 if (num_bytes
< key_len
) {
3901 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3905 if (key_len
!= 0 && key_len
!= 8) {
3906 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3911 memcpy(server_challenge
, bytes
, 8);
3914 blob1
= data_blob_const(bytes
+key_len
, num_bytes
-key_len
);
3915 if (blob1
.length
> 0) {
3919 len
= utf16_len_n(blob1
.data
,
3923 ok
= convert_string_talloc(state
,
3931 status
= map_nt_error_from_unix_common(errno
);
3932 tevent_req_nterror(req
, status
);
3938 /* the old core protocol */
3939 server_time_zone
= get_time_zone(time(NULL
));
3940 server_max_xmit
= 1024;
3944 if (server_max_xmit
< 1024) {
3945 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3949 if (server_max_mux
< 1) {
3950 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
3955 * Now calculate the negotiated capabilities
3956 * based on the mask for:
3957 * - client only flags
3958 * - flags used in both directions
3959 * - server only flags
3961 both_capabilities
= client_capabilities
& server_capabilities
;
3962 capabilities
= client_capabilities
& SMB_CAP_CLIENT_MASK
;
3963 capabilities
|= both_capabilities
& SMB_CAP_BOTH_MASK
;
3964 capabilities
|= server_capabilities
& SMB_CAP_SERVER_MASK
;
3966 max_xmit
= MIN(client_max_xmit
, server_max_xmit
);
3968 conn
->smb1
.server
.capabilities
= server_capabilities
;
3969 conn
->smb1
.capabilities
= capabilities
;
3971 conn
->smb1
.server
.max_xmit
= server_max_xmit
;
3972 conn
->smb1
.max_xmit
= max_xmit
;
3974 conn
->smb1
.server
.max_mux
= server_max_mux
;
3976 conn
->smb1
.server
.security_mode
= server_security_mode
;
3978 conn
->smb1
.server
.readbraw
= server_readbraw
;
3979 conn
->smb1
.server
.writebraw
= server_writebraw
;
3980 conn
->smb1
.server
.lockread
= server_lockread
;
3981 conn
->smb1
.server
.writeunlock
= server_writeunlock
;
3983 conn
->smb1
.server
.session_key
= server_session_key
;
3985 talloc_steal(conn
, server_gss_blob
.data
);
3986 conn
->smb1
.server
.gss_blob
= server_gss_blob
;
3987 conn
->smb1
.server
.guid
= server_guid
;
3988 memcpy(conn
->smb1
.server
.challenge
, server_challenge
, 8);
3989 conn
->smb1
.server
.workgroup
= talloc_move(conn
, &server_workgroup
);
3990 conn
->smb1
.server
.name
= talloc_move(conn
, &server_name
);
3992 conn
->smb1
.server
.time_zone
= server_time_zone
;
3993 conn
->smb1
.server
.system_time
= server_system_time
;
3995 tevent_req_done(req
);
3998 static struct tevent_req
*smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state
*state
)
4002 uint16_t dialect_count
= 0;
4004 buf
= state
->smb2
.dyn
;
4005 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4006 if (smb2cli_prots
[i
].proto
< state
->min_protocol
) {
4010 if (smb2cli_prots
[i
].proto
> state
->max_protocol
) {
4014 SSVAL(buf
, dialect_count
*2, smb2cli_prots
[i
].smb2_dialect
);
4018 buf
= state
->smb2
.fixed
;
4020 SSVAL(buf
, 2, dialect_count
);
4021 SSVAL(buf
, 4, state
->conn
->smb2
.client
.security_mode
);
4022 SSVAL(buf
, 6, 0); /* Reserved */
4023 if (state
->max_protocol
>= PROTOCOL_SMB2_22
) {
4024 SIVAL(buf
, 8, state
->conn
->smb2
.client
.capabilities
);
4026 SIVAL(buf
, 8, 0); /* Capabilities */
4028 if (state
->max_protocol
>= PROTOCOL_SMB2_10
) {
4032 status
= GUID_to_ndr_blob(&state
->conn
->smb2
.client
.guid
,
4034 if (!NT_STATUS_IS_OK(status
)) {
4037 memcpy(buf
+12, blob
.data
, 16); /* ClientGuid */
4039 memset(buf
+12, 0, 16); /* ClientGuid */
4041 SBVAL(buf
, 28, 0); /* ClientStartTime */
4043 return smb2cli_req_send(state
, state
->ev
,
4044 state
->conn
, SMB2_OP_NEGPROT
,
4046 state
->timeout_msec
,
4047 NULL
, NULL
, /* tcon, session */
4048 state
->smb2
.fixed
, sizeof(state
->smb2
.fixed
),
4049 state
->smb2
.dyn
, dialect_count
*2);
4052 static void smbXcli_negprot_smb2_done(struct tevent_req
*subreq
)
4054 struct tevent_req
*req
=
4055 tevent_req_callback_data(subreq
,
4057 struct smbXcli_negprot_state
*state
=
4058 tevent_req_data(req
,
4059 struct smbXcli_negprot_state
);
4060 struct smbXcli_conn
*conn
= state
->conn
;
4061 size_t security_offset
, security_length
;
4067 uint16_t dialect_revision
;
4068 static const struct smb2cli_req_expected_response expected
[] = {
4070 .status
= NT_STATUS_OK
,
4075 status
= smb2cli_req_recv(subreq
, state
, &iov
,
4076 expected
, ARRAY_SIZE(expected
));
4077 TALLOC_FREE(subreq
);
4078 if (tevent_req_nterror(req
, status
)) {
4082 body
= (uint8_t *)iov
[1].iov_base
;
4084 dialect_revision
= SVAL(body
, 4);
4086 for (i
=0; i
< ARRAY_SIZE(smb2cli_prots
); i
++) {
4087 if (smb2cli_prots
[i
].proto
< state
->min_protocol
) {
4091 if (smb2cli_prots
[i
].proto
> state
->max_protocol
) {
4095 if (smb2cli_prots
[i
].smb2_dialect
!= dialect_revision
) {
4099 conn
->protocol
= smb2cli_prots
[i
].proto
;
4103 if (conn
->protocol
== PROTOCOL_NONE
) {
4104 if (state
->min_protocol
>= PROTOCOL_SMB2_02
) {
4105 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4109 if (dialect_revision
!= SMB2_DIALECT_REVISION_2FF
) {
4110 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4114 /* make sure we do not loop forever */
4115 state
->min_protocol
= PROTOCOL_SMB2_02
;
4118 * send a SMB2 negprot, in order to negotiate
4121 subreq
= smbXcli_negprot_smb2_subreq(state
);
4122 if (tevent_req_nomem(subreq
, req
)) {
4125 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4129 conn
->smb2
.server
.security_mode
= SVAL(body
, 2);
4131 blob
= data_blob_const(body
+ 8, 16);
4132 status
= GUID_from_data_blob(&blob
, &conn
->smb2
.server
.guid
);
4133 if (tevent_req_nterror(req
, status
)) {
4137 conn
->smb2
.server
.capabilities
= IVAL(body
, 24);
4138 conn
->smb2
.server
.max_trans_size
= IVAL(body
, 28);
4139 conn
->smb2
.server
.max_read_size
= IVAL(body
, 32);
4140 conn
->smb2
.server
.max_write_size
= IVAL(body
, 36);
4141 conn
->smb2
.server
.system_time
= BVAL(body
, 40);
4142 conn
->smb2
.server
.start_time
= BVAL(body
, 48);
4144 security_offset
= SVAL(body
, 56);
4145 security_length
= SVAL(body
, 58);
4147 if (security_offset
!= SMB2_HDR_BODY
+ iov
[1].iov_len
) {
4148 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4152 if (security_length
> iov
[2].iov_len
) {
4153 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
4157 conn
->smb2
.server
.gss_blob
= data_blob_talloc(conn
,
4160 if (tevent_req_nomem(conn
->smb2
.server
.gss_blob
.data
, req
)) {
4164 tevent_req_done(req
);
4167 static NTSTATUS
smbXcli_negprot_dispatch_incoming(struct smbXcli_conn
*conn
,
4168 TALLOC_CTX
*tmp_mem
,
4171 size_t num_pending
= talloc_array_length(conn
->pending
);
4172 struct tevent_req
*subreq
;
4173 struct smbXcli_req_state
*substate
;
4174 struct tevent_req
*req
;
4175 uint32_t protocol_magic
;
4176 size_t inbuf_len
= smb_len_nbt(inbuf
);
4178 if (num_pending
!= 1) {
4179 return NT_STATUS_INTERNAL_ERROR
;
4182 if (inbuf_len
< 4) {
4183 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4186 subreq
= conn
->pending
[0];
4187 substate
= tevent_req_data(subreq
, struct smbXcli_req_state
);
4188 req
= tevent_req_callback_data(subreq
, struct tevent_req
);
4190 protocol_magic
= IVAL(inbuf
, 4);
4192 switch (protocol_magic
) {
4194 tevent_req_set_callback(subreq
, smbXcli_negprot_smb1_done
, req
);
4195 conn
->dispatch_incoming
= smb1cli_conn_dispatch_incoming
;
4196 return smb1cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
4199 if (substate
->smb2
.recv_iov
== NULL
) {
4201 * For the SMB1 negprot we have move it.
4203 substate
->smb2
.recv_iov
= substate
->smb1
.recv_iov
;
4204 substate
->smb1
.recv_iov
= NULL
;
4208 * we got an SMB2 answer, which consumed sequence number 0
4209 * so we need to use 1 as the next one
4212 tevent_req_set_callback(subreq
, smbXcli_negprot_smb2_done
, req
);
4213 conn
->dispatch_incoming
= smb2cli_conn_dispatch_incoming
;
4214 return smb2cli_conn_dispatch_incoming(conn
, tmp_mem
, inbuf
);
4217 DEBUG(10, ("Got non-SMB PDU\n"));
4218 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
4221 NTSTATUS
smbXcli_negprot_recv(struct tevent_req
*req
)
4223 return tevent_req_simple_recv_ntstatus(req
);
4226 NTSTATUS
smbXcli_negprot(struct smbXcli_conn
*conn
,
4227 uint32_t timeout_msec
,
4228 enum protocol_types min_protocol
,
4229 enum protocol_types max_protocol
)
4231 TALLOC_CTX
*frame
= talloc_stackframe();
4232 struct tevent_context
*ev
;
4233 struct tevent_req
*req
;
4234 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
4237 if (smbXcli_conn_has_async_calls(conn
)) {
4239 * Can't use sync call while an async call is in flight
4241 status
= NT_STATUS_INVALID_PARAMETER_MIX
;
4244 ev
= tevent_context_init(frame
);
4248 req
= smbXcli_negprot_send(frame
, ev
, conn
, timeout_msec
,
4249 min_protocol
, max_protocol
);
4253 ok
= tevent_req_poll(req
, ev
);
4255 status
= map_nt_error_from_unix_common(errno
);
4258 status
= smbXcli_negprot_recv(req
);
4264 static int smbXcli_session_destructor(struct smbXcli_session
*session
)
4266 if (session
->conn
== NULL
) {
4270 DLIST_REMOVE(session
->conn
->sessions
, session
);
4274 struct smbXcli_session
*smbXcli_session_create(TALLOC_CTX
*mem_ctx
,
4275 struct smbXcli_conn
*conn
)
4277 struct smbXcli_session
*session
;
4279 session
= talloc_zero(mem_ctx
, struct smbXcli_session
);
4280 if (session
== NULL
) {
4283 session
->smb2
= talloc_zero(session
, struct smb2cli_session
);
4284 if (session
->smb2
== NULL
) {
4285 talloc_free(session
);
4288 talloc_set_destructor(session
, smbXcli_session_destructor
);
4290 DLIST_ADD_END(conn
->sessions
, session
, struct smbXcli_session
*);
4291 session
->conn
= conn
;
4296 uint16_t smb1cli_session_current_id(struct smbXcli_session
*session
)
4298 return session
->smb1
.session_id
;
4301 void smb1cli_session_set_id(struct smbXcli_session
*session
,
4302 uint16_t session_id
)
4304 session
->smb1
.session_id
= session_id
;
4307 uint8_t smb2cli_session_security_mode(struct smbXcli_session
*session
)
4309 struct smbXcli_conn
*conn
= session
->conn
;
4310 uint8_t security_mode
= 0;
4313 return security_mode
;
4316 security_mode
= SMB2_NEGOTIATE_SIGNING_ENABLED
;
4317 if (conn
->mandatory_signing
) {
4318 security_mode
|= SMB2_NEGOTIATE_SIGNING_REQUIRED
;
4321 return security_mode
;
4324 uint64_t smb2cli_session_current_id(struct smbXcli_session
*session
)
4326 return session
->smb2
->session_id
;
4329 uint16_t smb2cli_session_get_flags(struct smbXcli_session
*session
)
4331 return session
->smb2
->session_flags
;
4334 NTSTATUS
smb2cli_session_application_key(struct smbXcli_session
*session
,
4335 TALLOC_CTX
*mem_ctx
,
4338 *key
= data_blob_null
;
4340 if (session
->smb2
->application_key
.length
== 0) {
4341 return NT_STATUS_NO_USER_SESSION_KEY
;
4344 *key
= data_blob_dup_talloc(mem_ctx
, session
->smb2
->application_key
);
4345 if (key
->data
== NULL
) {
4346 return NT_STATUS_NO_MEMORY
;
4349 return NT_STATUS_OK
;
4352 void smb2cli_session_set_id_and_flags(struct smbXcli_session
*session
,
4353 uint64_t session_id
,
4354 uint16_t session_flags
)
4356 session
->smb2
->session_id
= session_id
;
4357 session
->smb2
->session_flags
= session_flags
;
4360 void smb2cli_session_increment_channel_sequence(struct smbXcli_session
*session
)
4362 session
->smb2
->channel_sequence
+= 1;
4365 NTSTATUS
smb2cli_session_set_session_key(struct smbXcli_session
*session
,
4366 const DATA_BLOB _session_key
,
4367 const struct iovec
*recv_iov
)
4369 struct smbXcli_conn
*conn
= session
->conn
;
4370 uint16_t no_sign_flags
;
4371 uint8_t session_key
[16];
4375 return NT_STATUS_INVALID_PARAMETER_MIX
;
4378 no_sign_flags
= SMB2_SESSION_FLAG_IS_GUEST
| SMB2_SESSION_FLAG_IS_NULL
;
4380 if (session
->smb2
->session_flags
& no_sign_flags
) {
4381 session
->smb2
->should_sign
= false;
4382 return NT_STATUS_OK
;
4385 if (session
->smb2
->signing_key
.length
!= 0) {
4386 return NT_STATUS_INVALID_PARAMETER_MIX
;
4389 ZERO_STRUCT(session_key
);
4390 memcpy(session_key
, _session_key
.data
,
4391 MIN(_session_key
.length
, sizeof(session_key
)));
4393 session
->smb2
->signing_key
= data_blob_talloc(session
,
4395 sizeof(session_key
));
4396 if (session
->smb2
->signing_key
.data
== NULL
) {
4397 ZERO_STRUCT(session_key
);
4398 return NT_STATUS_NO_MEMORY
;
4401 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4402 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCMAC");
4403 const DATA_BLOB context
= data_blob_string_const_null("SmbSign");
4405 smb2_key_derivation(session_key
, sizeof(session_key
),
4406 label
.data
, label
.length
,
4407 context
.data
, context
.length
,
4408 session
->smb2
->signing_key
.data
);
4411 session
->smb2
->encryption_key
= data_blob_dup_talloc(session
,
4412 session
->smb2
->signing_key
);
4413 if (session
->smb2
->encryption_key
.data
== NULL
) {
4414 ZERO_STRUCT(session_key
);
4415 return NT_STATUS_NO_MEMORY
;
4418 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4419 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCCM");
4420 const DATA_BLOB context
= data_blob_string_const_null("ServerIn ");
4422 smb2_key_derivation(session_key
, sizeof(session_key
),
4423 label
.data
, label
.length
,
4424 context
.data
, context
.length
,
4425 session
->smb2
->encryption_key
.data
);
4428 session
->smb2
->decryption_key
= data_blob_dup_talloc(session
,
4429 session
->smb2
->signing_key
);
4430 if (session
->smb2
->decryption_key
.data
== NULL
) {
4431 ZERO_STRUCT(session_key
);
4432 return NT_STATUS_NO_MEMORY
;
4435 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4436 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCCM");
4437 const DATA_BLOB context
= data_blob_string_const_null("ServerOut");
4439 smb2_key_derivation(session_key
, sizeof(session_key
),
4440 label
.data
, label
.length
,
4441 context
.data
, context
.length
,
4442 session
->smb2
->decryption_key
.data
);
4445 session
->smb2
->application_key
= data_blob_dup_talloc(session
,
4446 session
->smb2
->signing_key
);
4447 if (session
->smb2
->application_key
.data
== NULL
) {
4448 ZERO_STRUCT(session_key
);
4449 return NT_STATUS_NO_MEMORY
;
4452 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4453 const DATA_BLOB label
= data_blob_string_const_null("SMB2APP");
4454 const DATA_BLOB context
= data_blob_string_const_null("SmbRpc");
4456 smb2_key_derivation(session_key
, sizeof(session_key
),
4457 label
.data
, label
.length
,
4458 context
.data
, context
.length
,
4459 session
->smb2
->application_key
.data
);
4461 ZERO_STRUCT(session_key
);
4463 session
->smb2_channel
.signing_key
= data_blob_dup_talloc(session
,
4464 session
->smb2
->signing_key
);
4465 if (session
->smb2_channel
.signing_key
.data
== NULL
) {
4466 return NT_STATUS_NO_MEMORY
;
4469 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
4470 session
->conn
->protocol
,
4472 if (!NT_STATUS_IS_OK(status
)) {
4476 session
->smb2
->should_sign
= false;
4477 session
->smb2
->should_encrypt
= false;
4479 if (conn
->desire_signing
) {
4480 session
->smb2
->should_sign
= true;
4483 if (conn
->smb2
.server
.security_mode
& SMB2_NEGOTIATE_SIGNING_REQUIRED
) {
4484 session
->smb2
->should_sign
= true;
4487 if (session
->smb2
->session_flags
& SMB2_SESSION_FLAG_ENCRYPT_DATA
) {
4488 session
->smb2
->should_encrypt
= true;
4491 if (conn
->protocol
< PROTOCOL_SMB2_24
) {
4492 session
->smb2
->should_encrypt
= false;
4495 if (!(conn
->smb2
.server
.capabilities
& SMB2_CAP_ENCRYPTION
)) {
4496 session
->smb2
->should_encrypt
= false;
4499 generate_random_buffer((uint8_t *)&session
->smb2
->nonce_high
,
4500 sizeof(session
->smb2
->nonce_high
));
4501 session
->smb2
->nonce_low
= 1;
4503 return NT_STATUS_OK
;
4506 NTSTATUS
smb2cli_session_create_channel(TALLOC_CTX
*mem_ctx
,
4507 struct smbXcli_session
*session1
,
4508 struct smbXcli_conn
*conn
,
4509 struct smbXcli_session
**_session2
)
4511 struct smbXcli_session
*session2
;
4513 if (session1
->smb2
->signing_key
.length
== 0) {
4514 return NT_STATUS_INVALID_PARAMETER_MIX
;
4518 return NT_STATUS_INVALID_PARAMETER_MIX
;
4521 session2
= talloc_zero(mem_ctx
, struct smbXcli_session
);
4522 if (session2
== NULL
) {
4523 return NT_STATUS_NO_MEMORY
;
4525 session2
->smb2
= talloc_reference(session2
, session1
->smb2
);
4526 if (session2
->smb2
== NULL
) {
4527 talloc_free(session2
);
4528 return NT_STATUS_NO_MEMORY
;
4531 talloc_set_destructor(session2
, smbXcli_session_destructor
);
4532 DLIST_ADD_END(conn
->sessions
, session2
, struct smbXcli_session
*);
4533 session2
->conn
= conn
;
4535 *_session2
= session2
;
4536 return NT_STATUS_OK
;
4539 NTSTATUS
smb2cli_session_set_channel_key(struct smbXcli_session
*session
,
4540 const DATA_BLOB _channel_key
,
4541 const struct iovec
*recv_iov
)
4543 struct smbXcli_conn
*conn
= session
->conn
;
4544 uint8_t channel_key
[16];
4548 return NT_STATUS_INVALID_PARAMETER_MIX
;
4551 if (session
->smb2_channel
.signing_key
.length
!= 0) {
4552 return NT_STATUS_INVALID_PARAMETER_MIX
;
4555 ZERO_STRUCT(channel_key
);
4556 memcpy(channel_key
, _channel_key
.data
,
4557 MIN(_channel_key
.length
, sizeof(channel_key
)));
4559 session
->smb2_channel
.signing_key
= data_blob_talloc(session
,
4561 sizeof(channel_key
));
4562 if (session
->smb2_channel
.signing_key
.data
== NULL
) {
4563 ZERO_STRUCT(channel_key
);
4564 return NT_STATUS_NO_MEMORY
;
4567 if (conn
->protocol
>= PROTOCOL_SMB2_24
) {
4568 const DATA_BLOB label
= data_blob_string_const_null("SMB2AESCMAC");
4569 const DATA_BLOB context
= data_blob_string_const_null("SmbSign");
4571 smb2_key_derivation(channel_key
, sizeof(channel_key
),
4572 label
.data
, label
.length
,
4573 context
.data
, context
.length
,
4574 session
->smb2_channel
.signing_key
.data
);
4576 ZERO_STRUCT(channel_key
);
4578 status
= smb2_signing_check_pdu(session
->smb2_channel
.signing_key
,
4579 session
->conn
->protocol
,
4581 if (!NT_STATUS_IS_OK(status
)) {
4585 return NT_STATUS_OK
;
4588 struct smbXcli_tcon
*smbXcli_tcon_create(TALLOC_CTX
*mem_ctx
)
4590 struct smbXcli_tcon
*tcon
;
4592 tcon
= talloc_zero(mem_ctx
, struct smbXcli_tcon
);
4600 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
4602 return tcon
->smb1
.tcon_id
;
4605 void smb1cli_tcon_set_id(struct smbXcli_tcon
*tcon
, uint16_t tcon_id
)
4607 tcon
->smb1
.tcon_id
= tcon_id
;
4610 bool smb1cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
4612 uint16_t optional_support
,
4613 uint32_t maximal_access
,
4614 uint32_t guest_maximal_access
,
4615 const char *service
,
4616 const char *fs_type
)
4618 tcon
->smb1
.tcon_id
= tcon_id
;
4619 tcon
->smb1
.optional_support
= optional_support
;
4620 tcon
->smb1
.maximal_access
= maximal_access
;
4621 tcon
->smb1
.guest_maximal_access
= guest_maximal_access
;
4623 TALLOC_FREE(tcon
->smb1
.service
);
4624 tcon
->smb1
.service
= talloc_strdup(tcon
, service
);
4625 if (service
!= NULL
&& tcon
->smb1
.service
== NULL
) {
4629 TALLOC_FREE(tcon
->smb1
.fs_type
);
4630 tcon
->smb1
.fs_type
= talloc_strdup(tcon
, fs_type
);
4631 if (fs_type
!= NULL
&& tcon
->smb1
.fs_type
== NULL
) {
4638 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon
*tcon
)
4640 return tcon
->smb2
.tcon_id
;
4643 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon
*tcon
)
4645 return tcon
->smb2
.capabilities
;
4648 void smb2cli_tcon_set_values(struct smbXcli_tcon
*tcon
,
4649 struct smbXcli_session
*session
,
4653 uint32_t capabilities
,
4654 uint32_t maximal_access
)
4656 tcon
->smb2
.tcon_id
= tcon_id
;
4657 tcon
->smb2
.type
= type
;
4658 tcon
->smb2
.flags
= flags
;
4659 tcon
->smb2
.capabilities
= capabilities
;
4660 tcon
->smb2
.maximal_access
= maximal_access
;
4662 tcon
->smb2
.should_encrypt
= false;
4664 if (session
== NULL
) {
4668 tcon
->smb2
.should_encrypt
= session
->smb2
->should_encrypt
;
4670 if (flags
& SMB2_SHAREFLAG_ENCRYPT_DATA
) {
4671 tcon
->smb2
.should_encrypt
= true;