2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Stefan Metzmacher 2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/network.h"
23 #include "../lib/async_req/async_sock.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
26 #include "lib/util/util_net.h"
27 #include "lib/util/dlinklist.h"
28 #include "lib/util/iov_buf.h"
29 #include "../libcli/smb/smb_common.h"
30 #include "../libcli/smb/smb_seal.h"
31 #include "../libcli/smb/smb_signing.h"
32 #include "../libcli/smb/read_smb.h"
33 #include "smbXcli_base.h"
34 #include "librpc/ndr/libndr.h"
35 #include "libcli/smb/smb2_negotiate_context.h"
36 #include "libcli/smb/smb2_signing.h"
38 #include "lib/crypto/gnutls_helpers.h"
39 #include <gnutls/gnutls.h>
40 #include <gnutls/crypto.h>
44 struct smbXcli_session;
49 struct sockaddr_storage local_ss;
50 struct sockaddr_storage remote_ss;
51 const char *remote_name;
53 struct tevent_queue *outgoing;
54 struct tevent_req **pending;
55 struct tevent_req *read_smb_req;
56 struct tevent_req *suicide_req;
58 enum protocol_types min_protocol;
59 enum protocol_types max_protocol;
60 enum protocol_types protocol;
63 bool mandatory_signing;
66 * The incoming dispatch function should return:
67 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
68 * - NT_STATUS_OK, if no more processing is desired, e.g.
69 * the dispatch function called
71 * - All other return values disconnect the connection.
73 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
79 uint32_t capabilities;
84 uint32_t capabilities;
87 uint16_t security_mode;
96 const char *workgroup;
102 uint32_t capabilities;
107 struct smb_signing_state *signing;
108 struct smb_trans_enc_state *trans_enc;
110 struct tevent_req *read_braw_req;
115 uint32_t capabilities;
116 uint16_t security_mode;
118 struct smb311_capabilities smb3_capabilities;
122 uint32_t capabilities;
123 uint16_t security_mode;
125 uint32_t max_trans_size;
126 uint32_t max_read_size;
127 uint32_t max_write_size;
136 uint16_t cur_credits;
137 uint16_t max_credits;
139 uint32_t cc_chunk_len;
140 uint32_t cc_max_chunks;
144 bool force_channel_sequence;
146 uint8_t preauth_sha512[64];
149 struct smbXcli_session *sessions;
152 struct smb2cli_session {
154 uint16_t session_flags;
155 struct smb2_signing_key *application_key;
156 struct smb2_signing_key *signing_key;
159 struct smb2_signing_key *encryption_key;
160 struct smb2_signing_key *decryption_key;
161 uint64_t nonce_high_random;
162 uint64_t nonce_high_max;
165 uint16_t channel_sequence;
167 bool require_signed_response;
170 struct smbXcli_session {
171 struct smbXcli_session *prev, *next;
172 struct smbXcli_conn *conn;
177 DATA_BLOB application_key;
181 struct smb2cli_session *smb2;
184 struct smb2_signing_key *signing_key;
185 uint8_t preauth_sha512[64];
189 * this should be a short term hack
190 * until the upper layers have implemented
193 bool disconnect_expired;
196 struct smbXcli_tcon {
198 uint32_t fs_attributes;
202 uint16_t optional_support;
203 uint32_t maximal_access;
204 uint32_t guest_maximal_access;
213 uint32_t capabilities;
214 uint32_t maximal_access;
220 struct smbXcli_req_state {
221 struct tevent_context *ev;
222 struct smbXcli_conn *conn;
223 struct smbXcli_session *session; /* maybe NULL */
224 struct smbXcli_tcon *tcon; /* maybe NULL */
226 uint8_t length_hdr[4];
232 struct tevent_req *write_req;
234 struct timeval endtime;
237 /* Space for the header including the wct */
238 uint8_t hdr[HDR_VWV];
241 * For normal requests, smb1cli_req_send chooses a mid.
242 * SecondaryV trans requests need to use the mid of the primary
243 * request, so we need a place to store it.
244 * Assume it is set if != 0.
249 uint8_t bytecount_buf[2];
251 #define MAX_SMB_IOV 10
252 /* length_hdr, hdr, words, byte_count, buffers */
253 struct iovec iov[1 + 3 + MAX_SMB_IOV];
258 struct tevent_req **chained_requests;
261 NTSTATUS recv_status;
262 /* always an array of 3 talloc elements */
263 struct iovec *recv_iov;
267 const uint8_t *fixed;
272 uint8_t transform[SMB2_TF_HDR_SIZE];
273 uint8_t hdr[SMB2_HDR_BODY];
274 uint8_t pad[7]; /* padding space for compounding */
277 * always an array of 3 talloc elements
278 * (without a SMB2_TRANSFORM header!)
282 struct iovec *recv_iov;
285 * the expected max for the response dyn_len
287 uint32_t max_dyn_len;
289 uint16_t credit_charge;
293 uint64_t encryption_session_id;
295 bool signing_skipped;
296 bool require_signed_response;
299 uint16_t cancel_flags;
305 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
308 * NT_STATUS_OK, means we do not notify the callers
310 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
312 while (conn->sessions) {
313 conn->sessions->conn = NULL;
314 DLIST_REMOVE(conn->sessions, conn->sessions);
317 if (conn->smb1.trans_enc) {
318 TALLOC_FREE(conn->smb1.trans_enc);
324 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
326 const char *remote_name,
327 enum smb_signing_setting signing_state,
328 uint32_t smb1_capabilities,
329 struct GUID *client_guid,
330 uint32_t smb2_capabilities,
331 const struct smb311_capabilities *smb3_capabilities)
333 struct smbXcli_conn *conn = NULL;
335 struct sockaddr *sa = NULL;
339 if (smb3_capabilities != NULL) {
340 const struct smb3_encryption_capabilities *ciphers =
341 &smb3_capabilities->encryption;
343 SMB_ASSERT(ciphers->num_algos <= SMB3_ENCRYTION_CAPABILITIES_MAX_ALGOS);
346 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
351 ret = set_blocking(fd, false);
357 conn->remote_name = talloc_strdup(conn, remote_name);
358 if (conn->remote_name == NULL) {
362 ss = (void *)&conn->local_ss;
363 sa = (struct sockaddr *)ss;
364 sa_length = sizeof(conn->local_ss);
365 ret = getsockname(fd, sa, &sa_length);
369 ss = (void *)&conn->remote_ss;
370 sa = (struct sockaddr *)ss;
371 sa_length = sizeof(conn->remote_ss);
372 ret = getpeername(fd, sa, &sa_length);
377 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
378 if (conn->outgoing == NULL) {
381 conn->pending = NULL;
383 conn->min_protocol = PROTOCOL_NONE;
384 conn->max_protocol = PROTOCOL_NONE;
385 conn->protocol = PROTOCOL_NONE;
387 switch (signing_state) {
388 case SMB_SIGNING_OFF:
390 conn->allow_signing = false;
391 conn->desire_signing = false;
392 conn->mandatory_signing = false;
394 case SMB_SIGNING_DEFAULT:
395 case SMB_SIGNING_IF_REQUIRED:
396 /* if the server requires it */
397 conn->allow_signing = true;
398 conn->desire_signing = false;
399 conn->mandatory_signing = false;
401 case SMB_SIGNING_DESIRED:
402 /* if the server desires it */
403 conn->allow_signing = true;
404 conn->desire_signing = true;
405 conn->mandatory_signing = false;
407 case SMB_SIGNING_IPC_DEFAULT:
408 case SMB_SIGNING_REQUIRED:
410 conn->allow_signing = true;
411 conn->desire_signing = true;
412 conn->mandatory_signing = true;
416 conn->smb1.client.capabilities = smb1_capabilities;
417 conn->smb1.client.max_xmit = UINT16_MAX;
419 conn->smb1.capabilities = conn->smb1.client.capabilities;
420 conn->smb1.max_xmit = 1024;
424 /* initialise signing */
425 conn->smb1.signing = smb_signing_init(conn,
427 conn->desire_signing,
428 conn->mandatory_signing);
429 if (!conn->smb1.signing) {
433 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
434 if (conn->mandatory_signing) {
435 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
438 conn->smb2.client.guid = *client_guid;
440 conn->smb2.client.capabilities = smb2_capabilities;
441 if (smb3_capabilities != NULL) {
442 conn->smb2.client.smb3_capabilities = *smb3_capabilities;
445 conn->smb2.cur_credits = 1;
446 conn->smb2.max_credits = 0;
447 conn->smb2.io_priority = 1;
450 * Samba and Windows servers accept a maximum of 16 MiB with a maximum
451 * chunk length of 1 MiB.
453 conn->smb2.cc_chunk_len = 1024 * 1024;
454 conn->smb2.cc_max_chunks = 16;
456 talloc_set_destructor(conn, smbXcli_conn_destructor);
464 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
470 if (conn->sock_fd == -1) {
477 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
479 return conn->protocol;
482 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
484 if (conn->protocol >= PROTOCOL_SMB2_02) {
488 if (conn->smb1.capabilities & CAP_UNICODE) {
495 bool smbXcli_conn_signing_mandatory(struct smbXcli_conn *conn)
497 return conn->mandatory_signing;
501 * [MS-SMB] 2.2.2.3.5 - SMB1 support for passing through
502 * query/set commands to the file system
504 bool smbXcli_conn_support_passthrough(struct smbXcli_conn *conn)
506 if (conn->protocol >= PROTOCOL_SMB2_02) {
510 if (conn->smb1.capabilities & CAP_W2K_SMBS) {
517 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
519 set_socket_options(conn->sock_fd, options);
522 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
524 return &conn->local_ss;
527 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
529 return &conn->remote_ss;
532 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
534 return conn->remote_name;
537 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
539 if (conn->protocol >= PROTOCOL_SMB2_02) {
546 return conn->smb1.server.max_mux;
549 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
551 if (conn->protocol >= PROTOCOL_SMB2_02) {
552 return conn->smb2.server.system_time;
555 return conn->smb1.server.system_time;
558 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
560 if (conn->protocol >= PROTOCOL_SMB2_02) {
561 return &conn->smb2.server.gss_blob;
564 return &conn->smb1.server.gss_blob;
567 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
569 if (conn->protocol >= PROTOCOL_SMB2_02) {
570 return &conn->smb2.server.guid;
573 return &conn->smb1.server.guid;
576 bool smbXcli_conn_get_force_channel_sequence(struct smbXcli_conn *conn)
578 return conn->smb2.force_channel_sequence;
581 void smbXcli_conn_set_force_channel_sequence(struct smbXcli_conn *conn,
584 conn->smb2.force_channel_sequence = v;
587 struct smbXcli_conn_samba_suicide_state {
588 struct smbXcli_conn *conn;
591 struct tevent_req *write_req;
594 static void smbXcli_conn_samba_suicide_cleanup(struct tevent_req *req,
595 enum tevent_req_state req_state);
596 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
598 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
599 struct tevent_context *ev,
600 struct smbXcli_conn *conn,
603 struct tevent_req *req, *subreq;
604 struct smbXcli_conn_samba_suicide_state *state;
606 req = tevent_req_create(mem_ctx, &state,
607 struct smbXcli_conn_samba_suicide_state);
612 SIVAL(state->buf, 4, SMB_SUICIDE_PACKET);
613 SCVAL(state->buf, 8, exitcode);
614 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
616 if (conn->suicide_req != NULL) {
617 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
618 return tevent_req_post(req, ev);
621 state->iov.iov_base = state->buf;
622 state->iov.iov_len = sizeof(state->buf);
624 subreq = writev_send(state, ev, conn->outgoing, conn->sock_fd,
625 false, &state->iov, 1);
626 if (tevent_req_nomem(subreq, req)) {
627 return tevent_req_post(req, ev);
629 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
630 state->write_req = subreq;
632 tevent_req_set_cleanup_fn(req, smbXcli_conn_samba_suicide_cleanup);
635 * We need to use tevent_req_defer_callback()
636 * in order to allow smbXcli_conn_disconnect()
637 * to do a safe cleanup.
639 tevent_req_defer_callback(req, ev);
640 conn->suicide_req = req;
645 static void smbXcli_conn_samba_suicide_cleanup(struct tevent_req *req,
646 enum tevent_req_state req_state)
648 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
649 req, struct smbXcli_conn_samba_suicide_state);
651 TALLOC_FREE(state->write_req);
653 if (state->conn == NULL) {
657 if (state->conn->suicide_req == req) {
658 state->conn->suicide_req = NULL;
663 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
665 struct tevent_req *req = tevent_req_callback_data(
666 subreq, struct tevent_req);
667 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
668 req, struct smbXcli_conn_samba_suicide_state);
672 state->write_req = NULL;
674 nwritten = writev_recv(subreq, &err);
676 if (nwritten == -1) {
677 /* here, we need to notify all pending requests */
678 NTSTATUS status = map_nt_error_from_unix_common(err);
679 smbXcli_conn_disconnect(state->conn, status);
682 tevent_req_done(req);
685 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
687 return tevent_req_simple_recv_ntstatus(req);
690 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
693 TALLOC_CTX *frame = talloc_stackframe();
694 struct tevent_context *ev;
695 struct tevent_req *req;
696 NTSTATUS status = NT_STATUS_NO_MEMORY;
699 if (smbXcli_conn_has_async_calls(conn)) {
701 * Can't use sync call while an async call is in flight
703 status = NT_STATUS_INVALID_PARAMETER_MIX;
706 ev = samba_tevent_context_init(frame);
710 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
714 ok = tevent_req_poll_ntstatus(req, ev, &status);
718 status = smbXcli_conn_samba_suicide_recv(req);
724 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
726 return conn->smb1.capabilities;
729 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
731 return conn->smb1.max_xmit;
734 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
736 size_t pending = talloc_array_length(conn->pending);
737 uint16_t possible = conn->smb1.server.max_mux;
739 if (pending >= possible) {
746 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
748 return conn->smb1.server.session_key;
751 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
753 return conn->smb1.server.challenge;
756 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
758 return conn->smb1.server.security_mode;
761 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
763 return conn->smb1.server.readbraw;
766 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
768 return conn->smb1.server.writebraw;
771 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
773 return conn->smb1.server.lockread;
776 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
778 return conn->smb1.server.writeunlock;
781 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
783 return conn->smb1.server.time_zone;
786 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
787 const DATA_BLOB user_session_key,
788 const DATA_BLOB response)
790 return smb_signing_activate(conn->smb1.signing,
795 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
796 const uint8_t *buf, uint32_t seqnum)
798 const uint8_t *hdr = buf + NBT_HDR_SIZE;
799 size_t len = smb_len_nbt(buf);
801 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
804 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
806 return smb_signing_is_active(conn->smb1.signing);
809 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
810 struct smb_trans_enc_state *es)
812 /* Replace the old state, if any. */
813 if (conn->smb1.trans_enc) {
814 TALLOC_FREE(conn->smb1.trans_enc);
816 conn->smb1.trans_enc = es;
819 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
821 return common_encryption_on(conn->smb1.trans_enc);
825 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
827 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
828 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
830 if (NT_STATUS_IS_OK(status)) {
834 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
838 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
842 * Is the SMB command able to hold an AND_X successor
843 * @param[in] cmd The SMB command in question
844 * @retval Can we add a chained request after "cmd"?
846 bool smb1cli_is_andx_req(uint8_t cmd)
866 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
868 size_t num_pending = talloc_array_length(conn->pending);
871 if (conn->protocol == PROTOCOL_NONE) {
873 * This is what windows sends on the SMB1 Negprot request
874 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
882 result = conn->smb1.mid++;
883 if ((result == 0) || (result == 0xffff)) {
887 for (i=0; i<num_pending; i++) {
888 if (result == smb1cli_req_mid(conn->pending[i])) {
893 if (i == num_pending) {
899 static NTSTATUS smbXcli_req_cancel_write_req(struct tevent_req *req)
901 struct smbXcli_req_state *state =
903 struct smbXcli_req_state);
904 struct smbXcli_conn *conn = state->conn;
905 size_t num_pending = talloc_array_length(conn->pending);
910 if (state->write_req == NULL) {
915 * Check if it's possible to cancel the request.
916 * If the result is true it's not too late.
917 * See writev_cancel().
919 ok = tevent_req_cancel(state->write_req);
921 TALLOC_FREE(state->write_req);
923 if (conn->protocol >= PROTOCOL_SMB2_02) {
925 * SMB2 has a sane signing state.
930 if (num_pending > 1) {
932 * We have more pending requests following us. This
933 * means the signing state will be broken for them.
935 * As a solution we could add the requests directly to
936 * our outgoing queue and do the signing in the trigger
937 * function and then use writev_send() without passing a
938 * queue. That way we'll only sign packets we're most
939 * likely send to the wire.
941 return NT_STATUS_REQUEST_OUT_OF_SEQUENCE;
945 * If we're the only request that's
946 * pending, we're able to recover the signing
949 smb_signing_cancel_reply(conn->smb1.signing,
950 state->smb1.one_way_seqnum);
954 ret = writev_recv(state->write_req, &err);
955 TALLOC_FREE(state->write_req);
957 return map_nt_error_from_unix_common(err);
963 void smbXcli_req_unset_pending(struct tevent_req *req)
965 struct smbXcli_req_state *state =
967 struct smbXcli_req_state);
968 struct smbXcli_conn *conn = state->conn;
969 size_t num_pending = talloc_array_length(conn->pending);
971 NTSTATUS cancel_status;
973 cancel_status = smbXcli_req_cancel_write_req(req);
975 if (state->smb1.mid != 0) {
977 * This is a [nt]trans[2] request which waits
978 * for more than one reply.
980 if (!NT_STATUS_IS_OK(cancel_status)) {
982 * If the write_req cancel didn't work
983 * we can't use the connection anymore.
985 smbXcli_conn_disconnect(conn, cancel_status);
991 tevent_req_set_cleanup_fn(req, NULL);
993 if (num_pending == 1) {
995 * The pending read_smb tevent_req is a child of
996 * conn->pending. So if nothing is pending anymore, we need to
997 * delete the socket read fde.
999 /* TODO: smbXcli_conn_cancel_read_req */
1000 TALLOC_FREE(conn->pending);
1001 conn->read_smb_req = NULL;
1003 if (!NT_STATUS_IS_OK(cancel_status)) {
1005 * If the write_req cancel didn't work
1006 * we can't use the connection anymore.
1008 smbXcli_conn_disconnect(conn, cancel_status);
1014 for (i=0; i<num_pending; i++) {
1015 if (req == conn->pending[i]) {
1019 if (i == num_pending) {
1021 * Something's seriously broken. Just returning here is the
1022 * right thing nevertheless, the point of this routine is to
1023 * remove ourselves from conn->pending.
1026 if (!NT_STATUS_IS_OK(cancel_status)) {
1028 * If the write_req cancel didn't work
1029 * we can't use the connection anymore.
1031 smbXcli_conn_disconnect(conn, cancel_status);
1038 * Remove ourselves from the conn->pending array
1040 for (; i < (num_pending - 1); i++) {
1041 conn->pending[i] = conn->pending[i+1];
1045 * No NULL check here, we're shrinking by sizeof(void *), and
1046 * talloc_realloc just adjusts the size for this.
1048 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
1051 if (!NT_STATUS_IS_OK(cancel_status)) {
1053 * If the write_req cancel didn't work
1054 * we can't use the connection anymore.
1056 smbXcli_conn_disconnect(conn, cancel_status);
1062 static void smbXcli_req_cleanup(struct tevent_req *req,
1063 enum tevent_req_state req_state)
1065 struct smbXcli_req_state *state =
1066 tevent_req_data(req,
1067 struct smbXcli_req_state);
1068 struct smbXcli_conn *conn = state->conn;
1069 NTSTATUS cancel_status;
1071 switch (req_state) {
1072 case TEVENT_REQ_RECEIVED:
1074 * Make sure we really remove it from
1075 * the pending array on destruction.
1077 * smbXcli_req_unset_pending() calls
1078 * smbXcli_req_cancel_write_req() internal
1080 state->smb1.mid = 0;
1081 smbXcli_req_unset_pending(req);
1084 cancel_status = smbXcli_req_cancel_write_req(req);
1085 if (!NT_STATUS_IS_OK(cancel_status)) {
1087 * If the write_req cancel didn't work
1088 * we can't use the connection anymore.
1090 smbXcli_conn_disconnect(conn, cancel_status);
1097 static bool smb1cli_req_cancel(struct tevent_req *req);
1098 static bool smb2cli_req_cancel(struct tevent_req *req);
1100 static bool smbXcli_req_cancel(struct tevent_req *req)
1102 struct smbXcli_req_state *state =
1103 tevent_req_data(req,
1104 struct smbXcli_req_state);
1106 if (!smbXcli_conn_is_connected(state->conn)) {
1110 if (state->conn->protocol == PROTOCOL_NONE) {
1114 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
1115 return smb2cli_req_cancel(req);
1118 return smb1cli_req_cancel(req);
1121 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
1123 bool smbXcli_req_set_pending(struct tevent_req *req)
1125 struct smbXcli_req_state *state =
1126 tevent_req_data(req,
1127 struct smbXcli_req_state);
1128 struct smbXcli_conn *conn;
1129 struct tevent_req **pending;
1134 if (!smbXcli_conn_is_connected(conn)) {
1138 num_pending = talloc_array_length(conn->pending);
1140 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
1142 if (pending == NULL) {
1145 pending[num_pending] = req;
1146 conn->pending = pending;
1147 tevent_req_set_cleanup_fn(req, smbXcli_req_cleanup);
1148 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1150 if (!smbXcli_conn_receive_next(conn)) {
1152 * the caller should notify the current request
1154 * And all other pending requests get notified
1155 * by smbXcli_conn_disconnect().
1157 smbXcli_req_unset_pending(req);
1158 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1165 static void smbXcli_conn_received(struct tevent_req *subreq);
1167 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
1169 size_t num_pending = talloc_array_length(conn->pending);
1170 struct tevent_req *req;
1171 struct smbXcli_req_state *state;
1173 if (conn->read_smb_req != NULL) {
1177 if (num_pending == 0) {
1178 if (conn->smb2.mid < UINT64_MAX) {
1179 /* no more pending requests, so we are done for now */
1184 * If there are no more SMB2 requests possible,
1185 * because we are out of message ids,
1186 * we need to disconnect.
1188 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
1192 req = conn->pending[0];
1193 state = tevent_req_data(req, struct smbXcli_req_state);
1196 * We're the first ones, add the read_smb request that waits for the
1197 * answer from the server
1199 conn->read_smb_req = read_smb_send(conn->pending,
1202 if (conn->read_smb_req == NULL) {
1205 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
1209 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
1211 struct smbXcli_session *session;
1212 int sock_fd = conn->sock_fd;
1214 tevent_queue_stop(conn->outgoing);
1218 session = conn->sessions;
1219 if (talloc_array_length(conn->pending) == 0) {
1221 * if we do not have pending requests
1222 * there is no need to update the channel_sequence
1226 for (; session; session = session->next) {
1227 smb2cli_session_increment_channel_sequence(session);
1230 if (conn->suicide_req != NULL) {
1232 * smbXcli_conn_samba_suicide_send()
1233 * used tevent_req_defer_callback() already.
1235 if (!NT_STATUS_IS_OK(status)) {
1236 tevent_req_nterror(conn->suicide_req, status);
1238 conn->suicide_req = NULL;
1242 * Cancel all pending requests. We do not do a for-loop walking
1243 * conn->pending because that array changes in
1244 * smbXcli_req_unset_pending.
1246 while (conn->pending != NULL &&
1247 talloc_array_length(conn->pending) > 0) {
1248 struct tevent_req *req;
1249 struct smbXcli_req_state *state;
1250 struct tevent_req **chain;
1254 req = conn->pending[0];
1255 state = tevent_req_data(req, struct smbXcli_req_state);
1257 if (state->smb1.chained_requests == NULL) {
1261 * We're dead. No point waiting for trans2
1264 state->smb1.mid = 0;
1266 smbXcli_req_unset_pending(req);
1268 if (NT_STATUS_IS_OK(status)) {
1269 /* do not notify the callers */
1273 in_progress = tevent_req_is_in_progress(req);
1282 * we need to defer the callback, because we may notify
1283 * more then one caller.
1285 tevent_req_defer_callback(req, state->ev);
1286 tevent_req_nterror(req, status);
1290 chain = talloc_move(conn, &state->smb1.chained_requests);
1291 num_chained = talloc_array_length(chain);
1293 for (i=0; i<num_chained; i++) {
1297 state = tevent_req_data(req, struct smbXcli_req_state);
1300 * We're dead. No point waiting for trans2
1303 state->smb1.mid = 0;
1305 smbXcli_req_unset_pending(req);
1307 if (NT_STATUS_IS_OK(status)) {
1308 /* do not notify the callers */
1312 in_progress = tevent_req_is_in_progress(req);
1321 * we need to defer the callback, because we may notify
1322 * more than one caller.
1324 tevent_req_defer_callback(req, state->ev);
1325 tevent_req_nterror(req, status);
1330 if (sock_fd != -1) {
1336 * Fetch a smb request's mid. Only valid after the request has been sent by
1337 * smb1cli_req_send().
1339 uint16_t smb1cli_req_mid(struct tevent_req *req)
1341 struct smbXcli_req_state *state =
1342 tevent_req_data(req,
1343 struct smbXcli_req_state);
1345 if (state->smb1.mid != 0) {
1346 return state->smb1.mid;
1349 return SVAL(state->smb1.hdr, HDR_MID);
1352 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1354 struct smbXcli_req_state *state =
1355 tevent_req_data(req,
1356 struct smbXcli_req_state);
1358 state->smb1.mid = mid;
1361 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1363 struct smbXcli_req_state *state =
1364 tevent_req_data(req,
1365 struct smbXcli_req_state);
1367 return state->smb1.seqnum;
1370 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1372 struct smbXcli_req_state *state =
1373 tevent_req_data(req,
1374 struct smbXcli_req_state);
1376 state->smb1.seqnum = seqnum;
1379 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1381 ssize_t ret = iov_buflen(iov, count);
1383 /* Ignore the overflow case for now ... */
1387 static void smb1cli_req_flags(enum protocol_types protocol,
1388 uint32_t smb1_capabilities,
1389 uint8_t smb_command,
1390 uint8_t additional_flags,
1391 uint8_t clear_flags,
1393 uint16_t additional_flags2,
1394 uint16_t clear_flags2,
1398 uint16_t flags2 = 0;
1400 if (protocol >= PROTOCOL_LANMAN1) {
1401 flags |= FLAG_CASELESS_PATHNAMES;
1402 flags |= FLAG_CANONICAL_PATHNAMES;
1405 if (protocol >= PROTOCOL_LANMAN2) {
1406 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1407 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1410 if (protocol >= PROTOCOL_NT1) {
1411 flags2 |= FLAGS2_IS_LONG_NAME;
1413 if (smb1_capabilities & CAP_UNICODE) {
1414 flags2 |= FLAGS2_UNICODE_STRINGS;
1416 if (smb1_capabilities & CAP_STATUS32) {
1417 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1419 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1420 flags2 |= FLAGS2_EXTENDED_SECURITY;
1424 flags |= additional_flags;
1425 flags &= ~clear_flags;
1426 flags2 |= additional_flags2;
1427 flags2 &= ~clear_flags2;
1433 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1435 static bool smb1cli_req_cancel(struct tevent_req *req)
1437 struct smbXcli_req_state *state =
1438 tevent_req_data(req,
1439 struct smbXcli_req_state);
1444 struct tevent_req *subreq;
1447 flags = CVAL(state->smb1.hdr, HDR_FLG);
1448 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1449 pid = SVAL(state->smb1.hdr, HDR_PID);
1450 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1451 mid = SVAL(state->smb1.hdr, HDR_MID);
1453 subreq = smb1cli_req_create(state, state->ev,
1463 0, NULL); /* bytes */
1464 if (subreq == NULL) {
1467 smb1cli_req_set_mid(subreq, mid);
1469 status = smb1cli_req_chain_submit(&subreq, 1);
1470 if (!NT_STATUS_IS_OK(status)) {
1471 TALLOC_FREE(subreq);
1474 smb1cli_req_set_mid(subreq, 0);
1476 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1481 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1483 /* we do not care about the result */
1484 TALLOC_FREE(subreq);
1487 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1488 struct tevent_context *ev,
1489 struct smbXcli_conn *conn,
1490 uint8_t smb_command,
1491 uint8_t additional_flags,
1492 uint8_t clear_flags,
1493 uint16_t additional_flags2,
1494 uint16_t clear_flags2,
1495 uint32_t timeout_msec,
1497 struct smbXcli_tcon *tcon,
1498 struct smbXcli_session *session,
1499 uint8_t wct, uint16_t *vwv,
1501 struct iovec *bytes_iov)
1503 struct tevent_req *req;
1504 struct smbXcli_req_state *state;
1506 uint16_t flags2 = 0;
1511 if (iov_count > MAX_SMB_IOV) {
1513 * Should not happen :-)
1518 req = tevent_req_create(mem_ctx, &state,
1519 struct smbXcli_req_state);
1525 state->session = session;
1529 uid = session->smb1.session_id;
1533 tid = tcon->smb1.tcon_id;
1535 if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
1536 clear_flags |= FLAG_CASELESS_PATHNAMES;
1538 /* Default setting, case insensitive. */
1539 additional_flags |= FLAG_CASELESS_PATHNAMES;
1542 if (smbXcli_conn_dfs_supported(conn) &&
1543 smbXcli_tcon_is_dfs_share(tcon))
1545 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
1549 state->smb1.recv_cmd = 0xFF;
1550 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1551 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1552 if (state->smb1.recv_iov == NULL) {
1557 smb1cli_req_flags(conn->protocol,
1558 conn->smb1.capabilities,
1567 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1568 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1569 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1570 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1571 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1572 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1573 SSVAL(state->smb1.hdr, HDR_TID, tid);
1574 SSVAL(state->smb1.hdr, HDR_PID, pid);
1575 SSVAL(state->smb1.hdr, HDR_UID, uid);
1576 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1577 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1579 state->smb1.vwv = vwv;
1581 num_bytes = iov_buflen(bytes_iov, iov_count);
1582 if (num_bytes == -1) {
1584 * I'd love to add a check for num_bytes<=UINT16_MAX here, but
1585 * the smbclient->samba connections can lie and transfer more.
1591 SSVAL(state->smb1.bytecount_buf, 0, num_bytes);
1593 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1594 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1595 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1596 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1597 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1598 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1599 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1600 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1602 if (iov_count != 0) {
1603 memcpy(&state->smb1.iov[4], bytes_iov,
1604 iov_count * sizeof(*bytes_iov));
1606 state->smb1.iov_count = iov_count + 4;
1608 if (timeout_msec > 0) {
1609 state->endtime = timeval_current_ofs_msec(timeout_msec);
1610 if (!tevent_req_set_endtime(req, ev, state->endtime)) {
1615 switch (smb_command) {
1619 state->one_way = true;
1622 state->one_way = true;
1623 state->smb1.one_way_seqnum = true;
1627 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1628 state->one_way = true;
1636 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1637 struct iovec *iov, int iov_count,
1639 bool one_way_seqnum)
1641 TALLOC_CTX *frame = NULL;
1646 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1647 * iovec directly. MD5Update would do that just fine.
1650 if (iov_count < 4) {
1651 return NT_STATUS_INVALID_PARAMETER_MIX;
1653 if (iov[0].iov_len != NBT_HDR_SIZE) {
1654 return NT_STATUS_INVALID_PARAMETER_MIX;
1656 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1657 return NT_STATUS_INVALID_PARAMETER_MIX;
1659 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1660 return NT_STATUS_INVALID_PARAMETER_MIX;
1662 if (iov[3].iov_len != sizeof(uint16_t)) {
1663 return NT_STATUS_INVALID_PARAMETER_MIX;
1666 frame = talloc_stackframe();
1668 buf = iov_concat(frame, &iov[1], iov_count - 1);
1670 return NT_STATUS_NO_MEMORY;
1673 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1675 status = smb_signing_sign_pdu(conn->smb1.signing,
1677 talloc_get_size(buf),
1679 if (!NT_STATUS_IS_OK(status)) {
1682 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1685 return NT_STATUS_OK;
1688 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1689 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1690 TALLOC_CTX *tmp_mem,
1693 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1694 struct smbXcli_req_state *state,
1695 struct iovec *iov, int iov_count)
1697 struct tevent_req *subreq;
1703 if (!smbXcli_conn_is_connected(state->conn)) {
1704 return NT_STATUS_CONNECTION_DISCONNECTED;
1707 if (state->conn->protocol > PROTOCOL_NT1) {
1708 DBG_ERR("called for dialect[%s] server[%s]\n",
1709 smb_protocol_types_string(state->conn->protocol),
1710 smbXcli_conn_remote_name(state->conn));
1711 return NT_STATUS_REVISION_MISMATCH;
1714 if (iov_count < 4) {
1715 return NT_STATUS_INVALID_PARAMETER_MIX;
1717 if (iov[0].iov_len != NBT_HDR_SIZE) {
1718 return NT_STATUS_INVALID_PARAMETER_MIX;
1720 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1721 return NT_STATUS_INVALID_PARAMETER_MIX;
1723 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1724 return NT_STATUS_INVALID_PARAMETER_MIX;
1726 if (iov[3].iov_len != sizeof(uint16_t)) {
1727 return NT_STATUS_INVALID_PARAMETER_MIX;
1730 cmd = CVAL(iov[1].iov_base, HDR_COM);
1731 if (cmd == SMBreadBraw) {
1732 if (smbXcli_conn_has_async_calls(state->conn)) {
1733 return NT_STATUS_INVALID_PARAMETER_MIX;
1735 state->conn->smb1.read_braw_req = req;
1738 if (state->smb1.mid != 0) {
1739 mid = state->smb1.mid;
1741 mid = smb1cli_alloc_mid(state->conn);
1743 SSVAL(iov[1].iov_base, HDR_MID, mid);
1745 nbtlen = iov_buflen(&iov[1], iov_count-1);
1746 if ((nbtlen == -1) || (nbtlen > 0x1FFFF)) {
1747 return NT_STATUS_INVALID_PARAMETER_MIX;
1750 _smb_setlen_nbt(iov[0].iov_base, nbtlen);
1752 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1753 &state->smb1.seqnum,
1754 state->smb1.one_way_seqnum);
1756 if (!NT_STATUS_IS_OK(status)) {
1761 * If we supported multiple encrytion contexts
1762 * here we'd look up based on tid.
1764 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1765 char *buf, *enc_buf;
1767 buf = (char *)iov_concat(talloc_tos(), iov, iov_count);
1769 return NT_STATUS_NO_MEMORY;
1771 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1772 (char *)buf, &enc_buf);
1774 if (!NT_STATUS_IS_OK(status)) {
1775 DEBUG(0, ("Error in encrypting client message: %s\n",
1776 nt_errstr(status)));
1779 buf = (char *)talloc_memdup(state, enc_buf,
1780 smb_len_nbt(enc_buf)+4);
1783 return NT_STATUS_NO_MEMORY;
1785 iov[0].iov_base = (void *)buf;
1786 iov[0].iov_len = talloc_get_size(buf);
1790 if (state->conn->dispatch_incoming == NULL) {
1791 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1794 if (!smbXcli_req_set_pending(req)) {
1795 return NT_STATUS_NO_MEMORY;
1798 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1800 subreq = writev_send(state, state->ev, state->conn->outgoing,
1801 state->conn->sock_fd, false, iov, iov_count);
1802 if (subreq == NULL) {
1803 return NT_STATUS_NO_MEMORY;
1805 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1806 state->write_req = subreq;
1808 return NT_STATUS_OK;
1811 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1812 struct tevent_context *ev,
1813 struct smbXcli_conn *conn,
1814 uint8_t smb_command,
1815 uint8_t additional_flags,
1816 uint8_t clear_flags,
1817 uint16_t additional_flags2,
1818 uint16_t clear_flags2,
1819 uint32_t timeout_msec,
1821 struct smbXcli_tcon *tcon,
1822 struct smbXcli_session *session,
1823 uint8_t wct, uint16_t *vwv,
1825 const uint8_t *bytes)
1827 struct tevent_req *req;
1831 iov.iov_base = discard_const_p(void, bytes);
1832 iov.iov_len = num_bytes;
1834 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1835 additional_flags, clear_flags,
1836 additional_flags2, clear_flags2,
1843 if (!tevent_req_is_in_progress(req)) {
1844 return tevent_req_post(req, ev);
1846 status = smb1cli_req_chain_submit(&req, 1);
1847 if (tevent_req_nterror(req, status)) {
1848 return tevent_req_post(req, ev);
1853 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1855 struct tevent_req *req =
1856 tevent_req_callback_data(subreq,
1858 struct smbXcli_req_state *state =
1859 tevent_req_data(req,
1860 struct smbXcli_req_state);
1864 state->write_req = NULL;
1866 nwritten = writev_recv(subreq, &err);
1867 TALLOC_FREE(subreq);
1868 if (nwritten == -1) {
1869 /* here, we need to notify all pending requests */
1870 NTSTATUS status = map_nt_error_from_unix_common(err);
1871 smbXcli_conn_disconnect(state->conn, status);
1875 if (state->one_way) {
1876 state->inbuf = NULL;
1877 tevent_req_done(req);
1882 static void smbXcli_conn_received(struct tevent_req *subreq)
1884 struct smbXcli_conn *conn =
1885 tevent_req_callback_data(subreq,
1886 struct smbXcli_conn);
1887 TALLOC_CTX *frame = talloc_stackframe();
1893 if (subreq != conn->read_smb_req) {
1894 DEBUG(1, ("Internal error: cli_smb_received called with "
1895 "unexpected subreq\n"));
1896 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1900 conn->read_smb_req = NULL;
1902 received = read_smb_recv(subreq, frame, &inbuf, &err);
1903 TALLOC_FREE(subreq);
1904 if (received == -1) {
1905 status = map_nt_error_from_unix_common(err);
1906 smbXcli_conn_disconnect(conn, status);
1911 status = conn->dispatch_incoming(conn, frame, inbuf);
1913 if (NT_STATUS_IS_OK(status)) {
1915 * We should not do any more processing
1916 * as the dispatch function called
1917 * tevent_req_done().
1922 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1924 * We got an error, so notify all pending requests
1926 smbXcli_conn_disconnect(conn, status);
1931 * We got NT_STATUS_RETRY, so we may ask for a
1932 * next incoming pdu.
1934 if (!smbXcli_conn_receive_next(conn)) {
1935 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1939 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1940 struct iovec **piov, int *pnum_iov)
1951 size_t min_size = MIN_SMB_SIZE;
1953 buflen = smb_len_tcp(buf);
1956 hdr = buf + NBT_HDR_SIZE;
1958 status = smb1cli_pull_raw_error(hdr);
1959 if (NT_STATUS_IS_ERR(status)) {
1961 * This is an ugly hack to support OS/2
1962 * which skips the byte_count in the DATA block
1963 * on some error responses.
1967 min_size -= sizeof(uint16_t);
1970 if (buflen < min_size) {
1971 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1975 * This returns iovec elements in the following order:
1990 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1992 return NT_STATUS_NO_MEMORY;
1994 iov[0].iov_base = hdr;
1995 iov[0].iov_len = HDR_WCT;
1998 cmd = CVAL(hdr, HDR_COM);
2002 size_t len = buflen - taken;
2004 struct iovec *iov_tmp;
2011 * we need at least WCT
2013 needed = sizeof(uint8_t);
2015 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2016 __location__, (int)len, (int)needed));
2021 * Now we check if the specified words are there
2023 wct = CVAL(hdr, wct_ofs);
2024 needed += wct * sizeof(uint16_t);
2026 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2027 __location__, (int)len, (int)needed));
2031 if ((num_iov == 1) &&
2033 NT_STATUS_IS_ERR(status))
2036 * This is an ugly hack to support OS/2
2037 * which skips the byte_count in the DATA block
2038 * on some error responses.
2042 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2044 if (iov_tmp == NULL) {
2046 return NT_STATUS_NO_MEMORY;
2049 cur = &iov[num_iov];
2053 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
2055 cur[1].iov_base = cur[0].iov_base;
2062 * we need at least BCC
2064 needed += sizeof(uint16_t);
2066 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2067 __location__, (int)len, (int)needed));
2072 * Now we check if the specified bytes are there
2074 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
2075 bcc = SVAL(hdr, bcc_ofs);
2076 needed += bcc * sizeof(uint8_t);
2078 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2079 __location__, (int)len, (int)needed));
2084 * we allocate 2 iovec structures for words and bytes
2086 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2088 if (iov_tmp == NULL) {
2090 return NT_STATUS_NO_MEMORY;
2093 cur = &iov[num_iov];
2096 cur[0].iov_len = wct * sizeof(uint16_t);
2097 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
2098 cur[1].iov_len = bcc * sizeof(uint8_t);
2099 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
2103 if (!smb1cli_is_andx_req(cmd)) {
2105 * If the current command does not have AndX chanining
2111 if (wct == 0 && bcc == 0) {
2113 * An empty response also ends the chain,
2114 * most likely with an error.
2120 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
2121 __location__, (int)wct, (int)cmd));
2124 cmd = CVAL(cur[0].iov_base, 0);
2127 * If it is the end of the chain we are also done.
2131 wct_ofs = SVAL(cur[0].iov_base, 2);
2133 if (wct_ofs < taken) {
2136 if (wct_ofs > buflen) {
2141 * we consumed everything up to the start of the next
2147 remaining = buflen - taken;
2149 if (remaining > 0 && num_iov >= 3) {
2151 * The last DATA block gets the remaining
2152 * bytes, this is needed to support
2153 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
2155 iov[num_iov-1].iov_len += remaining;
2159 *pnum_iov = num_iov;
2160 return NT_STATUS_OK;
2164 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2167 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2168 TALLOC_CTX *tmp_mem,
2171 struct tevent_req *req;
2172 struct smbXcli_req_state *state;
2179 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
2180 size_t len = smb_len_tcp(inbuf);
2181 struct iovec *iov = NULL;
2183 struct tevent_req **chain = NULL;
2184 size_t num_chained = 0;
2185 size_t num_responses = 0;
2187 if (conn->smb1.read_braw_req != NULL) {
2188 req = conn->smb1.read_braw_req;
2189 conn->smb1.read_braw_req = NULL;
2190 state = tevent_req_data(req, struct smbXcli_req_state);
2192 smbXcli_req_unset_pending(req);
2194 if (state->smb1.recv_iov == NULL) {
2196 * For requests with more than
2197 * one response, we have to readd the
2200 state->smb1.recv_iov = talloc_zero_array(state,
2203 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2204 return NT_STATUS_OK;
2208 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
2209 state->smb1.recv_iov[0].iov_len = len;
2210 ZERO_STRUCT(state->smb1.recv_iov[1]);
2211 ZERO_STRUCT(state->smb1.recv_iov[2]);
2213 state->smb1.recv_cmd = SMBreadBraw;
2214 state->smb1.recv_status = NT_STATUS_OK;
2215 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2217 tevent_req_done(req);
2218 return NT_STATUS_OK;
2221 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
2222 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
2223 DEBUG(10, ("Got non-SMB PDU\n"));
2224 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2228 * If we supported multiple encrytion contexts
2229 * here we'd look up based on tid.
2231 if (common_encryption_on(conn->smb1.trans_enc)
2232 && (CVAL(inbuf, 0) == 0)) {
2233 uint16_t enc_ctx_num;
2235 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
2236 if (!NT_STATUS_IS_OK(status)) {
2237 DEBUG(10, ("get_enc_ctx_num returned %s\n",
2238 nt_errstr(status)));
2242 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
2243 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
2245 conn->smb1.trans_enc->enc_ctx_num));
2246 return NT_STATUS_INVALID_HANDLE;
2249 status = common_decrypt_buffer(conn->smb1.trans_enc,
2251 if (!NT_STATUS_IS_OK(status)) {
2252 DEBUG(10, ("common_decrypt_buffer returned %s\n",
2253 nt_errstr(status)));
2256 inhdr = inbuf + NBT_HDR_SIZE;
2257 len = smb_len_nbt(inbuf);
2260 mid = SVAL(inhdr, HDR_MID);
2261 num_pending = talloc_array_length(conn->pending);
2263 for (i=0; i<num_pending; i++) {
2264 if (mid == smb1cli_req_mid(conn->pending[i])) {
2268 if (i == num_pending) {
2269 /* Dump unexpected reply */
2270 return NT_STATUS_RETRY;
2273 oplock_break = false;
2275 if (mid == 0xffff) {
2277 * Paranoia checks that this is really an oplock break request.
2279 oplock_break = (len == 51); /* hdr + 8 words */
2280 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
2281 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
2282 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
2283 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
2285 if (!oplock_break) {
2286 /* Dump unexpected reply */
2287 return NT_STATUS_RETRY;
2291 req = conn->pending[i];
2292 state = tevent_req_data(req, struct smbXcli_req_state);
2294 if (!oplock_break /* oplock breaks are not signed */
2295 && !smb_signing_check_pdu(conn->smb1.signing,
2296 inhdr, len, state->smb1.seqnum+1)) {
2297 DEBUG(10, ("cli_check_sign_mac failed\n"));
2298 return NT_STATUS_ACCESS_DENIED;
2301 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2303 if (!NT_STATUS_IS_OK(status)) {
2304 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2305 nt_errstr(status)));
2309 cmd = CVAL(inhdr, HDR_COM);
2310 status = smb1cli_pull_raw_error(inhdr);
2312 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2313 (state->session != NULL) && state->session->disconnect_expired)
2316 * this should be a short term hack
2317 * until the upper layers have implemented
2318 * re-authentication.
2323 if (state->smb1.chained_requests == NULL) {
2325 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2328 smbXcli_req_unset_pending(req);
2330 if (state->smb1.recv_iov == NULL) {
2332 * For requests with more than
2333 * one response, we have to readd the
2336 state->smb1.recv_iov = talloc_zero_array(state,
2339 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2340 return NT_STATUS_OK;
2344 state->smb1.recv_cmd = cmd;
2345 state->smb1.recv_status = status;
2346 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2348 state->smb1.recv_iov[0] = iov[0];
2349 state->smb1.recv_iov[1] = iov[1];
2350 state->smb1.recv_iov[2] = iov[2];
2352 if (talloc_array_length(conn->pending) == 0) {
2353 tevent_req_done(req);
2354 return NT_STATUS_OK;
2357 tevent_req_defer_callback(req, state->ev);
2358 tevent_req_done(req);
2359 return NT_STATUS_RETRY;
2362 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2363 num_chained = talloc_array_length(chain);
2364 num_responses = (num_iov - 1)/2;
2366 if (num_responses > num_chained) {
2367 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2370 for (i=0; i<num_chained; i++) {
2371 size_t iov_idx = 1 + (i*2);
2372 struct iovec *cur = &iov[iov_idx];
2376 state = tevent_req_data(req, struct smbXcli_req_state);
2378 smbXcli_req_unset_pending(req);
2381 * as we finish multiple requests here
2382 * we need to defer the callbacks as
2383 * they could destroy our current stack state.
2385 tevent_req_defer_callback(req, state->ev);
2387 if (i >= num_responses) {
2388 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2392 if (state->smb1.recv_iov == NULL) {
2394 * For requests with more than
2395 * one response, we have to readd the
2398 state->smb1.recv_iov = talloc_zero_array(state,
2401 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2406 state->smb1.recv_cmd = cmd;
2408 if (i == (num_responses - 1)) {
2410 * The last request in the chain gets the status
2412 state->smb1.recv_status = status;
2414 cmd = CVAL(cur[0].iov_base, 0);
2415 state->smb1.recv_status = NT_STATUS_OK;
2418 state->inbuf = inbuf;
2421 * Note: here we use talloc_reference() in a way
2422 * that does not expose it to the caller.
2424 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2425 if (tevent_req_nomem(inbuf_ref, req)) {
2429 /* copy the related buffers */
2430 state->smb1.recv_iov[0] = iov[0];
2431 state->smb1.recv_iov[1] = cur[0];
2432 state->smb1.recv_iov[2] = cur[1];
2434 tevent_req_done(req);
2437 return NT_STATUS_RETRY;
2440 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2441 TALLOC_CTX *mem_ctx,
2442 struct iovec **piov,
2446 uint32_t *pvwv_offset,
2447 uint32_t *pnum_bytes,
2449 uint32_t *pbytes_offset,
2451 const struct smb1cli_req_expected_response *expected,
2452 size_t num_expected)
2454 struct smbXcli_req_state *state =
2455 tevent_req_data(req,
2456 struct smbXcli_req_state);
2457 NTSTATUS status = NT_STATUS_OK;
2458 struct iovec *recv_iov = NULL;
2459 uint8_t *hdr = NULL;
2461 uint32_t vwv_offset = 0;
2462 uint16_t *vwv = NULL;
2463 uint32_t num_bytes = 0;
2464 uint32_t bytes_offset = 0;
2465 uint8_t *bytes = NULL;
2467 bool found_status = false;
2468 bool found_size = false;
2482 if (pvwv_offset != NULL) {
2485 if (pnum_bytes != NULL) {
2488 if (pbytes != NULL) {
2491 if (pbytes_offset != NULL) {
2494 if (pinbuf != NULL) {
2498 if (state->inbuf != NULL) {
2499 recv_iov = state->smb1.recv_iov;
2500 state->smb1.recv_iov = NULL;
2501 if (state->smb1.recv_cmd != SMBreadBraw) {
2502 hdr = (uint8_t *)recv_iov[0].iov_base;
2503 wct = recv_iov[1].iov_len/2;
2504 vwv = (uint16_t *)recv_iov[1].iov_base;
2505 vwv_offset = PTR_DIFF(vwv, hdr);
2506 num_bytes = recv_iov[2].iov_len;
2507 bytes = (uint8_t *)recv_iov[2].iov_base;
2508 bytes_offset = PTR_DIFF(bytes, hdr);
2512 if (tevent_req_is_nterror(req, &status)) {
2513 for (i=0; i < num_expected; i++) {
2514 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2515 found_status = true;
2521 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2527 if (num_expected == 0) {
2528 found_status = true;
2532 status = state->smb1.recv_status;
2534 for (i=0; i < num_expected; i++) {
2535 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2539 found_status = true;
2540 if (expected[i].wct == 0) {
2545 if (expected[i].wct == wct) {
2551 if (!found_status) {
2556 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2560 *piov = talloc_move(mem_ctx, &recv_iov);
2572 if (pvwv_offset != NULL) {
2573 *pvwv_offset = vwv_offset;
2575 if (pnum_bytes != NULL) {
2576 *pnum_bytes = num_bytes;
2578 if (pbytes != NULL) {
2581 if (pbytes_offset != NULL) {
2582 *pbytes_offset = bytes_offset;
2584 if (pinbuf != NULL) {
2585 *pinbuf = state->inbuf;
2591 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2598 for (i=0; i<num_reqs; i++) {
2599 struct smbXcli_req_state *state;
2600 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2601 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2602 state->smb1.iov_count-2);
2603 wct_ofs = (wct_ofs + 3) & ~3;
2608 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2610 struct smbXcli_req_state *first_state =
2611 tevent_req_data(reqs[0],
2612 struct smbXcli_req_state);
2613 struct smbXcli_req_state *state;
2615 size_t chain_padding = 0;
2617 struct iovec *iov = NULL;
2618 struct iovec *this_iov;
2622 if (num_reqs == 1) {
2623 return smb1cli_req_writev_submit(reqs[0], first_state,
2624 first_state->smb1.iov,
2625 first_state->smb1.iov_count);
2629 for (i=0; i<num_reqs; i++) {
2630 if (!tevent_req_is_in_progress(reqs[i])) {
2631 return NT_STATUS_INTERNAL_ERROR;
2634 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2636 if (state->smb1.iov_count < 4) {
2637 return NT_STATUS_INVALID_PARAMETER_MIX;
2642 * The NBT and SMB header
2655 iovlen += state->smb1.iov_count - 2;
2658 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2660 return NT_STATUS_NO_MEMORY;
2663 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2664 first_state, reqs, sizeof(*reqs) * num_reqs);
2665 if (first_state->smb1.chained_requests == NULL) {
2667 return NT_STATUS_NO_MEMORY;
2670 wct_offset = HDR_WCT;
2673 for (i=0; i<num_reqs; i++) {
2674 size_t next_padding = 0;
2677 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2679 if (i < num_reqs-1) {
2680 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2681 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2683 TALLOC_FREE(first_state->smb1.chained_requests);
2684 return NT_STATUS_INVALID_PARAMETER_MIX;
2688 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2689 state->smb1.iov_count-2) + 1;
2690 if ((wct_offset % 4) != 0) {
2691 next_padding = 4 - (wct_offset % 4);
2693 wct_offset += next_padding;
2694 vwv = state->smb1.vwv;
2696 if (i < num_reqs-1) {
2697 struct smbXcli_req_state *next_state =
2698 tevent_req_data(reqs[i+1],
2699 struct smbXcli_req_state);
2700 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2702 SSVAL(vwv+1, 0, wct_offset);
2703 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2704 /* properly end the chain */
2705 SCVAL(vwv+0, 0, 0xff);
2706 SCVAL(vwv+0, 1, 0xff);
2712 * The NBT and SMB header
2714 this_iov[0] = state->smb1.iov[0];
2715 this_iov[1] = state->smb1.iov[1];
2719 * This one is a bit subtle. We have to add
2720 * chain_padding bytes between the requests, and we
2721 * have to also include the wct field of the
2722 * subsequent requests. We use the subsequent header
2723 * for the padding, it contains the wct field in its
2726 this_iov[0].iov_len = chain_padding+1;
2727 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2728 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2729 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2734 * copy the words and bytes
2736 memcpy(this_iov, state->smb1.iov+2,
2737 sizeof(struct iovec) * (state->smb1.iov_count-2));
2738 this_iov += state->smb1.iov_count - 2;
2739 chain_padding = next_padding;
2742 nbt_len = iov_buflen(&iov[1], iovlen-1);
2743 if ((nbt_len == -1) || (nbt_len > first_state->conn->smb1.max_xmit)) {
2745 TALLOC_FREE(first_state->smb1.chained_requests);
2746 return NT_STATUS_INVALID_PARAMETER_MIX;
2749 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2750 if (!NT_STATUS_IS_OK(status)) {
2752 TALLOC_FREE(first_state->smb1.chained_requests);
2756 return NT_STATUS_OK;
2759 struct tevent_queue *smbXcli_conn_send_queue(struct smbXcli_conn *conn)
2761 return conn->outgoing;
2764 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2766 return ((tevent_queue_length(conn->outgoing) != 0)
2767 || (talloc_array_length(conn->pending) != 0));
2770 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2772 if (conn->protocol >= PROTOCOL_SMB2_02) {
2773 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2776 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2779 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2781 uint16_t credits = 1;
2783 if (conn->smb2.cur_credits == 0) {
2784 if (max_dyn_len != NULL) {
2790 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2791 credits = conn->smb2.cur_credits;
2794 if (max_dyn_len != NULL) {
2795 *max_dyn_len = credits * 65536;
2801 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2803 return conn->smb2.server.capabilities;
2806 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2808 return conn->smb2.server.security_mode;
2811 uint16_t smb2cli_conn_server_signing_algo(struct smbXcli_conn *conn)
2813 return conn->smb2.server.sign_algo;
2816 uint16_t smb2cli_conn_server_encryption_algo(struct smbXcli_conn *conn)
2818 return conn->smb2.server.cipher;
2821 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2823 return conn->smb2.server.max_trans_size;
2826 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2828 return conn->smb2.server.max_read_size;
2831 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2833 return conn->smb2.server.max_write_size;
2836 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2837 uint16_t max_credits)
2839 conn->smb2.max_credits = max_credits;
2842 uint16_t smb2cli_conn_get_cur_credits(struct smbXcli_conn *conn)
2844 return conn->smb2.cur_credits;
2847 uint8_t smb2cli_conn_get_io_priority(struct smbXcli_conn *conn)
2849 if (conn->protocol < PROTOCOL_SMB3_11) {
2853 return conn->smb2.io_priority;
2856 void smb2cli_conn_set_io_priority(struct smbXcli_conn *conn,
2857 uint8_t io_priority)
2859 conn->smb2.io_priority = io_priority;
2862 uint32_t smb2cli_conn_cc_chunk_len(struct smbXcli_conn *conn)
2864 return conn->smb2.cc_chunk_len;
2867 void smb2cli_conn_set_cc_chunk_len(struct smbXcli_conn *conn,
2870 conn->smb2.cc_chunk_len = chunk_len;
2873 uint32_t smb2cli_conn_cc_max_chunks(struct smbXcli_conn *conn)
2875 return conn->smb2.cc_max_chunks;
2878 void smb2cli_conn_set_cc_max_chunks(struct smbXcli_conn *conn,
2879 uint32_t max_chunks)
2881 conn->smb2.cc_max_chunks = max_chunks;
2884 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2886 static bool smb2cli_req_cancel(struct tevent_req *req)
2888 struct smbXcli_req_state *state =
2889 tevent_req_data(req,
2890 struct smbXcli_req_state);
2891 struct smbXcli_tcon *tcon = state->tcon;
2892 struct smbXcli_session *session = state->session;
2893 uint8_t *fixed = state->smb2.pad;
2894 uint16_t fixed_len = 4;
2895 struct tevent_req *subreq;
2896 struct smbXcli_req_state *substate;
2899 SSVAL(fixed, 0, 0x04);
2902 subreq = smb2cli_req_create(state, state->ev,
2910 if (subreq == NULL) {
2913 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2915 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, state->smb2.cancel_flags);
2916 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, state->smb2.cancel_mid);
2917 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, state->smb2.cancel_aid);
2919 status = smb2cli_req_compound_submit(&subreq, 1);
2920 if (!NT_STATUS_IS_OK(status)) {
2921 TALLOC_FREE(subreq);
2925 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2930 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2932 /* we do not care about the result */
2933 TALLOC_FREE(subreq);
2936 struct timeval smbXcli_req_endtime(struct tevent_req *req)
2938 struct smbXcli_req_state *state = tevent_req_data(
2939 req, struct smbXcli_req_state);
2941 return state->endtime;
2944 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2945 struct tevent_context *ev,
2946 struct smbXcli_conn *conn,
2948 uint32_t additional_flags,
2949 uint32_t clear_flags,
2950 uint32_t timeout_msec,
2951 struct smbXcli_tcon *tcon,
2952 struct smbXcli_session *session,
2953 const uint8_t *fixed,
2957 uint32_t max_dyn_len)
2959 struct tevent_req *req;
2960 struct smbXcli_req_state *state;
2964 bool use_channel_sequence = conn->smb2.force_channel_sequence;
2965 uint16_t channel_sequence = 0;
2966 bool use_replay_flag = false;
2968 req = tevent_req_create(mem_ctx, &state,
2969 struct smbXcli_req_state);
2976 state->session = session;
2979 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2980 use_channel_sequence = true;
2981 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2982 use_channel_sequence = true;
2985 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_00) {
2986 use_replay_flag = true;
2989 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_11) {
2990 flags |= SMB2_PRIORITY_VALUE_TO_MASK(conn->smb2.io_priority);
2994 uid = session->smb2->session_id;
2996 if (use_channel_sequence) {
2997 channel_sequence = session->smb2->channel_sequence;
3000 if (use_replay_flag && session->smb2->replay_active) {
3001 additional_flags |= SMB2_HDR_FLAG_REPLAY_OPERATION;
3004 state->smb2.should_sign = session->smb2->should_sign;
3005 state->smb2.should_encrypt = session->smb2->should_encrypt;
3006 state->smb2.require_signed_response =
3007 session->smb2->require_signed_response;
3009 if (cmd == SMB2_OP_SESSSETUP &&
3010 !smb2_signing_key_valid(session->smb2_channel.signing_key) &&
3011 smb2_signing_key_valid(session->smb2->signing_key))
3014 * a session bind needs to be signed
3016 state->smb2.should_sign = true;
3019 if (cmd == SMB2_OP_SESSSETUP &&
3020 !smb2_signing_key_valid(session->smb2_channel.signing_key)) {
3021 state->smb2.should_encrypt = false;
3024 if (additional_flags & SMB2_HDR_FLAG_SIGNED) {
3025 if (!smb2_signing_key_valid(session->smb2_channel.signing_key)) {
3026 tevent_req_nterror(req, NT_STATUS_NO_USER_SESSION_KEY);
3030 additional_flags &= ~SMB2_HDR_FLAG_SIGNED;
3031 state->smb2.should_sign = true;
3036 tid = tcon->smb2.tcon_id;
3038 if (tcon->smb2.should_sign) {
3039 state->smb2.should_sign = true;
3041 if (tcon->smb2.should_encrypt) {
3042 state->smb2.should_encrypt = true;
3046 if (state->smb2.should_encrypt) {
3047 state->smb2.should_sign = false;
3050 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
3051 if (state->smb2.recv_iov == NULL) {
3056 flags |= additional_flags;
3057 flags &= ~clear_flags;
3059 state->smb2.fixed = fixed;
3060 state->smb2.fixed_len = fixed_len;
3061 state->smb2.dyn = dyn;
3062 state->smb2.dyn_len = dyn_len;
3063 state->smb2.max_dyn_len = max_dyn_len;
3065 if (state->smb2.should_encrypt) {
3066 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
3067 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
3070 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
3071 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
3072 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
3073 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
3074 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
3075 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
3076 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
3077 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
3080 case SMB2_OP_CANCEL:
3081 state->one_way = true;
3085 * If this is a dummy request, it will have
3086 * UINT64_MAX as message id.
3087 * If we send on break acknowledgement,
3088 * this gets overwritten later.
3090 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
3094 if (timeout_msec > 0) {
3095 state->endtime = timeval_current_ofs_msec(timeout_msec);
3096 if (!tevent_req_set_endtime(req, ev, state->endtime)) {
3104 void smb2cli_req_set_notify_async(struct tevent_req *req)
3106 struct smbXcli_req_state *state =
3107 tevent_req_data(req,
3108 struct smbXcli_req_state);
3110 state->smb2.notify_async = true;
3113 static void smb2cli_req_writev_done(struct tevent_req *subreq);
3114 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3115 TALLOC_CTX *tmp_mem,
3118 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
3121 struct smbXcli_req_state *state;
3122 struct tevent_req *subreq;
3124 int i, num_iov, nbt_len;
3126 struct smb2_signing_key *encryption_key = NULL;
3127 uint64_t encryption_session_id = 0;
3128 uint64_t nonce_high = UINT64_MAX;
3129 uint64_t nonce_low = UINT64_MAX;
3132 * 1 for the nbt length, optional TRANSFORM
3133 * per request: HDR, fixed, dyn, padding
3134 * -1 because the last one does not need padding
3137 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
3139 return NT_STATUS_NO_MEMORY;
3146 * the session of the first request that requires encryption
3147 * specifies the encryption key.
3149 for (i=0; i<num_reqs; i++) {
3150 if (!tevent_req_is_in_progress(reqs[i])) {
3151 return NT_STATUS_INTERNAL_ERROR;
3154 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
3156 if (!smbXcli_conn_is_connected(state->conn)) {
3157 return NT_STATUS_CONNECTION_DISCONNECTED;
3160 if ((state->conn->protocol != PROTOCOL_NONE) &&
3161 (state->conn->protocol < PROTOCOL_SMB2_02)) {
3162 return NT_STATUS_REVISION_MISMATCH;
3165 if (state->session == NULL) {
3169 if (!state->smb2.should_encrypt) {
3173 encryption_key = state->session->smb2->encryption_key;
3174 if (!smb2_signing_key_valid(encryption_key)) {
3175 return NT_STATUS_INVALID_PARAMETER_MIX;
3178 encryption_session_id = state->session->smb2->session_id;
3180 state->session->smb2->nonce_low += 1;
3181 if (state->session->smb2->nonce_low == 0) {
3182 state->session->smb2->nonce_high += 1;
3183 state->session->smb2->nonce_low += 1;
3187 * CCM and GCM algorithms must never have their
3188 * nonce wrap, or the security of the whole
3189 * communication and the keys is destroyed.
3190 * We must drop the connection once we have
3191 * transfered too much data.
3193 * NOTE: We assume nonces greater than 8 bytes.
3195 if (state->session->smb2->nonce_high >=
3196 state->session->smb2->nonce_high_max)
3198 return NT_STATUS_ENCRYPTION_FAILED;
3201 nonce_high = state->session->smb2->nonce_high_random;
3202 nonce_high += state->session->smb2->nonce_high;
3203 nonce_low = state->session->smb2->nonce_low;
3206 iov[num_iov].iov_base = state->smb2.transform;
3207 iov[num_iov].iov_len = sizeof(state->smb2.transform);
3210 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
3211 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
3213 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
3215 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
3216 encryption_session_id);
3218 nbt_len += SMB2_TF_HDR_SIZE;
3222 for (i=0; i<num_reqs; i++) {
3231 struct smb2_signing_key *signing_key = NULL;
3233 if (!tevent_req_is_in_progress(reqs[i])) {
3234 return NT_STATUS_INTERNAL_ERROR;
3237 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
3239 if (!smbXcli_conn_is_connected(state->conn)) {
3240 return NT_STATUS_CONNECTION_DISCONNECTED;
3243 if ((state->conn->protocol != PROTOCOL_NONE) &&
3244 (state->conn->protocol < PROTOCOL_SMB2_02)) {
3245 return NT_STATUS_REVISION_MISMATCH;
3248 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3249 if (opcode == SMB2_OP_CANCEL) {
3253 avail = UINT64_MAX - state->conn->smb2.mid;
3255 return NT_STATUS_CONNECTION_ABORTED;
3258 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
3259 uint32_t max_dyn_len = 1;
3261 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
3262 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
3264 charge = (max_dyn_len - 1)/ 65536 + 1;
3269 charge = MAX(state->smb2.credit_charge, charge);
3271 avail = MIN(avail, state->conn->smb2.cur_credits);
3272 if (avail < charge) {
3273 DBG_ERR("Insufficient credits. "
3274 "%"PRIu64" available, %"PRIu16" needed\n",
3276 return NT_STATUS_INTERNAL_ERROR;
3280 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
3281 credits = state->conn->smb2.max_credits -
3282 state->conn->smb2.cur_credits;
3284 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
3288 mid = state->conn->smb2.mid;
3289 state->conn->smb2.mid += charge;
3290 state->conn->smb2.cur_credits -= charge;
3292 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
3293 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
3295 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
3296 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
3298 state->smb2.cancel_flags = 0;
3299 state->smb2.cancel_mid = mid;
3300 state->smb2.cancel_aid = 0;
3303 if (state->session && encryption_key == NULL) {
3305 * We prefer the channel signing key if it is
3308 if (state->smb2.should_sign) {
3309 signing_key = state->session->smb2_channel.signing_key;
3313 * If it is a channel binding, we already have the main
3314 * signing key and try that one.
3316 if (signing_key != NULL &&
3317 !smb2_signing_key_valid(signing_key)) {
3318 signing_key = state->session->smb2->signing_key;
3322 * If we do not have any session key yet, we skip the
3323 * signing of SMB2_OP_SESSSETUP requests.
3325 if (signing_key != NULL &&
3326 !smb2_signing_key_valid(signing_key)) {
3332 iov[num_iov].iov_base = state->smb2.hdr;
3333 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
3336 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
3337 iov[num_iov].iov_len = state->smb2.fixed_len;
3340 if (state->smb2.dyn != NULL) {
3341 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
3342 iov[num_iov].iov_len = state->smb2.dyn_len;
3346 reqlen = sizeof(state->smb2.hdr);
3347 reqlen += state->smb2.fixed_len;
3348 reqlen += state->smb2.dyn_len;
3350 if (i < num_reqs-1) {
3351 if ((reqlen % 8) > 0) {
3352 uint8_t pad = 8 - (reqlen % 8);
3353 iov[num_iov].iov_base = state->smb2.pad;
3354 iov[num_iov].iov_len = pad;
3358 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
3361 state->smb2.encryption_session_id = encryption_session_id;
3363 if (signing_key != NULL) {
3366 status = smb2_signing_sign_pdu(signing_key,
3367 &iov[hdr_iov], num_iov - hdr_iov);
3368 if (!NT_STATUS_IS_OK(status)) {
3375 ret = smbXcli_req_set_pending(reqs[i]);
3377 return NT_STATUS_NO_MEMORY;
3381 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
3382 _smb_setlen_tcp(state->length_hdr, nbt_len);
3383 iov[0].iov_base = state->length_hdr;
3384 iov[0].iov_len = sizeof(state->length_hdr);
3386 if (encryption_key != NULL) {
3388 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
3392 buf = talloc_array(iov, uint8_t, buflen);
3394 return NT_STATUS_NO_MEMORY;
3398 * We copy the buffers before encrypting them,
3399 * this is at least currently needed for the
3400 * to keep state->smb2.hdr.
3402 * Also the callers may expect there buffers
3405 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3406 struct iovec *v = &iov[vi];
3407 const uint8_t *o = (const uint8_t *)v->iov_base;
3409 memcpy(buf, o, v->iov_len);
3410 v->iov_base = (void *)buf;
3414 status = smb2_signing_encrypt_pdu(encryption_key,
3415 &iov[tf_iov], num_iov - tf_iov);
3416 if (!NT_STATUS_IS_OK(status)) {
3421 if (state->conn->dispatch_incoming == NULL) {
3422 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3425 subreq = writev_send(state, state->ev, state->conn->outgoing,
3426 state->conn->sock_fd, false, iov, num_iov);
3427 if (subreq == NULL) {
3428 return NT_STATUS_NO_MEMORY;
3430 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3431 state->write_req = subreq;
3433 return NT_STATUS_OK;
3436 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3438 struct smbXcli_req_state *state =
3439 tevent_req_data(req,
3440 struct smbXcli_req_state);
3442 state->smb2.credit_charge = charge;
3445 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3446 struct tevent_context *ev,
3447 struct smbXcli_conn *conn,
3449 uint32_t additional_flags,
3450 uint32_t clear_flags,
3451 uint32_t timeout_msec,
3452 struct smbXcli_tcon *tcon,
3453 struct smbXcli_session *session,
3454 const uint8_t *fixed,
3458 uint32_t max_dyn_len)
3460 struct tevent_req *req;
3463 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3464 additional_flags, clear_flags,
3473 if (!tevent_req_is_in_progress(req)) {
3474 return tevent_req_post(req, ev);
3476 status = smb2cli_req_compound_submit(&req, 1);
3477 if (tevent_req_nterror(req, status)) {
3478 return tevent_req_post(req, ev);
3483 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3485 struct tevent_req *req =
3486 tevent_req_callback_data(subreq,
3488 struct smbXcli_req_state *state =
3489 tevent_req_data(req,
3490 struct smbXcli_req_state);
3494 state->write_req = NULL;
3496 nwritten = writev_recv(subreq, &err);
3497 TALLOC_FREE(subreq);
3498 if (nwritten == -1) {
3499 /* here, we need to notify all pending requests */
3500 NTSTATUS status = map_nt_error_from_unix_common(err);
3501 smbXcli_conn_disconnect(state->conn, status);
3506 static struct smbXcli_session* smbXcli_session_by_uid(struct smbXcli_conn *conn,
3509 struct smbXcli_session *s = conn->sessions;
3511 for (; s; s = s->next) {
3512 if (s->smb2->session_id != uid) {
3521 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3524 TALLOC_CTX *mem_ctx,
3525 struct iovec **piov,
3531 uint8_t *first_hdr = buf;
3532 size_t verified_buflen = 0;
3536 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3538 return NT_STATUS_NO_MEMORY;
3541 while (taken < buflen) {
3542 size_t len = buflen - taken;
3543 uint8_t *hdr = first_hdr + taken;
3546 size_t next_command_ofs;
3548 struct iovec *iov_tmp;
3550 if (verified_buflen > taken) {
3551 len = verified_buflen - taken;
3558 DEBUG(10, ("%d bytes left, expected at least %d\n",
3562 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3563 struct smbXcli_session *s;
3565 struct iovec tf_iov[2];
3569 if (len < SMB2_TF_HDR_SIZE) {
3570 DEBUG(10, ("%d bytes left, expected at least %d\n",
3571 (int)len, SMB2_TF_HDR_SIZE));
3575 tf_len = SMB2_TF_HDR_SIZE;
3578 hdr = first_hdr + taken;
3579 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3580 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3582 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3583 DEBUG(10, ("%d bytes left, expected at least %d\n",
3585 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3589 s = smbXcli_session_by_uid(conn, uid);
3591 DEBUG(10, ("unknown session_id %llu\n",
3592 (unsigned long long)uid));
3596 tf_iov[0].iov_base = (void *)tf;
3597 tf_iov[0].iov_len = tf_len;
3598 tf_iov[1].iov_base = (void *)hdr;
3599 tf_iov[1].iov_len = enc_len;
3601 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3603 if (!NT_STATUS_IS_OK(status)) {
3608 verified_buflen = taken + enc_len;
3613 * We need the header plus the body length field
3616 if (len < SMB2_HDR_BODY + 2) {
3617 DEBUG(10, ("%d bytes left, expected at least %d\n",
3618 (int)len, SMB2_HDR_BODY));
3621 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3622 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3626 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3627 DEBUG(10, ("Got HDR len %d, expected %d\n",
3628 SVAL(hdr, 4), SMB2_HDR_BODY));
3633 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3634 body_size = SVAL(hdr, SMB2_HDR_BODY);
3636 if (next_command_ofs != 0) {
3637 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3640 if (next_command_ofs > full_size) {
3643 full_size = next_command_ofs;
3645 if (body_size < 2) {
3648 body_size &= 0xfffe;
3650 if (body_size > (full_size - SMB2_HDR_BODY)) {
3654 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3656 if (iov_tmp == NULL) {
3658 return NT_STATUS_NO_MEMORY;
3661 cur = &iov[num_iov];
3664 cur[0].iov_base = tf;
3665 cur[0].iov_len = tf_len;
3666 cur[1].iov_base = hdr;
3667 cur[1].iov_len = SMB2_HDR_BODY;
3668 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3669 cur[2].iov_len = body_size;
3670 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3671 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3677 *pnum_iov = num_iov;
3678 return NT_STATUS_OK;
3682 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3685 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3688 size_t num_pending = talloc_array_length(conn->pending);
3691 for (i=0; i<num_pending; i++) {
3692 struct tevent_req *req = conn->pending[i];
3693 struct smbXcli_req_state *state =
3694 tevent_req_data(req,
3695 struct smbXcli_req_state);
3697 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3704 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3705 TALLOC_CTX *tmp_mem,
3708 struct tevent_req *req;
3709 struct smbXcli_req_state *state = NULL;
3710 struct iovec *iov = NULL;
3711 size_t i, num_iov = 0;
3714 struct smbXcli_session *last_session = NULL;
3715 size_t inbuf_len = smb_len_tcp(inbuf);
3717 status = smb2cli_inbuf_parse_compound(conn,
3718 inbuf + NBT_HDR_SIZE,
3722 if (!NT_STATUS_IS_OK(status)) {
3726 for (i=0; i<num_iov; i+=4) {
3727 uint8_t *inbuf_ref = NULL;
3728 struct iovec *cur = &iov[i];
3729 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3730 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3731 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3732 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3733 uint16_t req_opcode;
3735 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3736 uint32_t new_credits;
3737 struct smbXcli_session *session = NULL;
3738 struct smb2_signing_key *signing_key = NULL;
3739 bool was_encrypted = false;
3741 new_credits = conn->smb2.cur_credits;
3742 new_credits += credits;
3743 if (new_credits > UINT16_MAX) {
3744 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3746 conn->smb2.cur_credits += credits;
3748 req = smb2cli_conn_find_pending(conn, mid);
3750 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3752 state = tevent_req_data(req, struct smbXcli_req_state);
3754 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3755 if (opcode != req_opcode) {
3756 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3758 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3760 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3761 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3764 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3765 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3766 NT_STATUS_EQUAL(status, NT_STATUS_PENDING)) {
3767 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3769 if (state->smb2.got_async) {
3770 /* We only expect one STATUS_PENDING response */
3771 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3773 state->smb2.got_async = true;
3776 * async interim responses are not signed,
3777 * even if the SMB2_HDR_FLAG_SIGNED flag
3780 state->smb2.cancel_flags = SMB2_HDR_FLAG_ASYNC;
3781 state->smb2.cancel_mid = 0;
3782 state->smb2.cancel_aid = async_id;
3784 if (state->smb2.notify_async) {
3785 tevent_req_defer_callback(req, state->ev);
3786 tevent_req_notify_callback(req);
3791 session = state->session;
3792 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3793 session = last_session;
3795 last_session = session;
3797 if (flags & SMB2_HDR_FLAG_SIGNED) {
3798 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3800 if (session == NULL) {
3801 session = smbXcli_session_by_uid(state->conn,
3805 if (session == NULL) {
3806 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3809 last_session = session;
3810 signing_key = session->smb2_channel.signing_key;
3813 if (opcode == SMB2_OP_SESSSETUP) {
3815 * We prefer the channel signing key, if it is
3818 * If we do not have a channel signing key yet,
3819 * we try the main signing key, if it is not
3820 * the final response.
3822 if (signing_key != NULL &&
3823 !smb2_signing_key_valid(signing_key) &&
3824 !NT_STATUS_IS_OK(status)) {
3825 signing_key = session->smb2->signing_key;
3828 if (signing_key != NULL &&
3829 !smb2_signing_key_valid(signing_key)) {
3831 * If we do not have a session key to
3832 * verify the signature, we defer the
3833 * signing check to the caller.
3835 * The caller gets NT_STATUS_OK, it
3837 * smb2cli_session_set_session_key()
3839 * smb2cli_session_set_channel_key()
3840 * which will check the signature
3841 * with the channel signing key.
3846 if (!NT_STATUS_IS_OK(status)) {
3848 * Only check the signature of the last response
3849 * of a successfull session auth. This matches
3850 * Windows behaviour for NTLM auth and reauth.
3852 state->smb2.require_signed_response = false;
3856 if (state->smb2.should_sign ||
3857 state->smb2.require_signed_response)
3859 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3860 return NT_STATUS_ACCESS_DENIED;
3864 if (!smb2_signing_key_valid(signing_key) &&
3865 state->smb2.require_signed_response) {
3866 signing_key = session->smb2_channel.signing_key;
3869 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3870 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3871 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3874 * If the response was encrypted in a SMB2_TRANSFORM
3875 * pdu, which belongs to the correct session,
3876 * we do not need to do signing checks
3878 * It could be the session the response belongs to
3879 * or the session that was used to encrypt the
3880 * SMB2_TRANSFORM request.
3882 if ((session && session->smb2->session_id == uid) ||
3883 (state->smb2.encryption_session_id == uid)) {
3885 was_encrypted = true;
3889 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3891 * if the server returns NT_STATUS_USER_SESSION_DELETED
3892 * the response is not signed and we should
3893 * propagate the NT_STATUS_USER_SESSION_DELETED
3894 * status to the caller.
3896 state->smb2.signing_skipped = true;
3899 if (NT_STATUS_EQUAL(status, NT_STATUS_REQUEST_OUT_OF_SEQUENCE)) {
3901 * if the server returns
3902 * NT_STATUS_REQUEST_OUT_OF_SEQUENCE for a session setup
3903 * request, the response is not signed and we should
3904 * propagate the NT_STATUS_REQUEST_OUT_OF_SEQUENCE
3905 * status to the caller
3907 if (opcode == SMB2_OP_SESSSETUP) {
3908 state->smb2.signing_skipped = true;
3912 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
3914 * if the server returns NT_STATUS_NOT_SUPPORTED
3915 * for a session setup request, the response is not
3916 * signed and we should propagate the NT_STATUS_NOT_SUPPORTED
3917 * status to the caller.
3919 if (opcode == SMB2_OP_SESSSETUP) {
3920 state->smb2.signing_skipped = true;
3924 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
3926 * if the server returns
3927 * NT_STATUS_ACCESS_DENIED for a session setup
3928 * request, the response is not signed and we should
3929 * propagate the NT_STATUS_ACCESS_DENIED
3930 * status to the caller without disconnecting
3931 * the connection because we where not able to
3932 * verify the response signature.
3934 if (opcode == SMB2_OP_SESSSETUP) {
3935 state->smb2.signing_skipped = true;
3940 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3942 * if the server returns
3943 * NT_STATUS_INVALID_PARAMETER
3944 * the response might not be encrypted.
3946 if (state->smb2.should_encrypt && !was_encrypted) {
3947 state->smb2.signing_skipped = true;
3952 if (state->smb2.should_encrypt && !was_encrypted) {
3953 if (!state->smb2.signing_skipped) {
3954 return NT_STATUS_ACCESS_DENIED;
3958 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3959 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3960 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3962 * if the server returns
3963 * NT_STATUS_NETWORK_NAME_DELETED
3964 * NT_STATUS_FILE_CLOSED
3965 * NT_STATUS_INVALID_PARAMETER
3966 * the response might not be signed
3967 * as this happens before the signing checks.
3969 * If server echos the signature (or all zeros)
3970 * we should report the status from the server
3976 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3977 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3980 state->smb2.signing_skipped = true;
3986 zero = all_zero(inhdr+SMB2_HDR_SIGNATURE, 16);
3988 state->smb2.signing_skipped = true;
3995 NTSTATUS signing_status;
3997 signing_status = smb2_signing_check_pdu(signing_key,
3999 if (!NT_STATUS_IS_OK(signing_status)) {
4001 * If the signing check fails, we disconnect
4004 return signing_status;
4008 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
4009 (session != NULL) && session->disconnect_expired)
4012 * this should be a short term hack
4013 * until the upper layers have implemented
4014 * re-authentication.
4019 smbXcli_req_unset_pending(req);
4022 * There might be more than one response
4023 * we need to defer the notifications
4025 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
4030 tevent_req_defer_callback(req, state->ev);
4034 * Note: here we use talloc_reference() in a way
4035 * that does not expose it to the caller.
4037 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
4038 if (tevent_req_nomem(inbuf_ref, req)) {
4042 /* copy the related buffers */
4043 state->smb2.recv_iov[0] = cur[1];
4044 state->smb2.recv_iov[1] = cur[2];
4045 state->smb2.recv_iov[2] = cur[3];
4047 tevent_req_done(req);
4051 return NT_STATUS_RETRY;
4054 return NT_STATUS_OK;
4057 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4058 struct iovec **piov,
4059 const struct smb2cli_req_expected_response *expected,
4060 size_t num_expected)
4062 struct smbXcli_req_state *state =
4063 tevent_req_data(req,
4064 struct smbXcli_req_state);
4067 bool found_status = false;
4068 bool found_size = false;
4075 if (tevent_req_is_in_progress(req) && state->smb2.got_async) {
4076 return NT_STATUS_PENDING;
4079 if (tevent_req_is_nterror(req, &status)) {
4080 for (i=0; i < num_expected; i++) {
4081 if (NT_STATUS_EQUAL(status, expected[i].status)) {
4082 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
4089 if (num_expected == 0) {
4090 found_status = true;
4094 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
4095 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
4097 for (i=0; i < num_expected; i++) {
4098 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
4102 found_status = true;
4103 if (expected[i].body_size == 0) {
4108 if (expected[i].body_size == body_size) {
4114 if (!found_status) {
4118 if (state->smb2.signing_skipped) {
4119 if (num_expected > 0) {
4120 return NT_STATUS_ACCESS_DENIED;
4122 if (!NT_STATUS_IS_ERR(status)) {
4123 return NT_STATUS_ACCESS_DENIED;
4128 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4132 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
4138 NTSTATUS smb2cli_req_get_sent_iov(struct tevent_req *req,
4139 struct iovec *sent_iov)
4141 struct smbXcli_req_state *state =
4142 tevent_req_data(req,
4143 struct smbXcli_req_state);
4145 if (tevent_req_is_in_progress(req)) {
4146 return NT_STATUS_PENDING;
4149 sent_iov[0].iov_base = state->smb2.hdr;
4150 sent_iov[0].iov_len = sizeof(state->smb2.hdr);
4152 sent_iov[1].iov_base = discard_const(state->smb2.fixed);
4153 sent_iov[1].iov_len = state->smb2.fixed_len;
4155 if (state->smb2.dyn != NULL) {
4156 sent_iov[2].iov_base = discard_const(state->smb2.dyn);
4157 sent_iov[2].iov_len = state->smb2.dyn_len;
4159 sent_iov[2].iov_base = NULL;
4160 sent_iov[2].iov_len = 0;
4163 return NT_STATUS_OK;
4166 static const struct {
4167 enum protocol_types proto;
4168 const char *smb1_name;
4169 } smb1cli_prots[] = {
4170 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
4171 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
4172 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
4173 {PROTOCOL_LANMAN1, "LANMAN1.0"},
4174 {PROTOCOL_LANMAN2, "LM1.2X002"},
4175 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
4176 {PROTOCOL_LANMAN2, "LANMAN2.1"},
4177 {PROTOCOL_LANMAN2, "Samba"},
4178 {PROTOCOL_NT1, "NT LANMAN 1.0"},
4179 {PROTOCOL_NT1, "NT LM 0.12"},
4180 {PROTOCOL_SMB2_02, "SMB 2.002"},
4181 {PROTOCOL_SMB2_10, "SMB 2.???"},
4184 static const struct {
4185 enum protocol_types proto;
4186 uint16_t smb2_dialect;
4187 } smb2cli_prots[] = {
4188 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
4189 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
4190 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
4191 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
4192 {PROTOCOL_SMB3_11, SMB3_DIALECT_REVISION_311},
4195 struct smbXcli_negprot_state {
4196 struct smbXcli_conn *conn;
4197 struct tevent_context *ev;
4198 uint32_t timeout_msec;
4205 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
4206 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
4207 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
4208 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
4209 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
4210 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4214 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
4215 struct tevent_context *ev,
4216 struct smbXcli_conn *conn,
4217 uint32_t timeout_msec,
4218 enum protocol_types min_protocol,
4219 enum protocol_types max_protocol,
4220 uint16_t max_credits)
4222 struct tevent_req *req, *subreq;
4223 struct smbXcli_negprot_state *state;
4225 req = tevent_req_create(mem_ctx, &state,
4226 struct smbXcli_negprot_state);
4232 state->timeout_msec = timeout_msec;
4234 if (min_protocol == PROTOCOL_NONE) {
4235 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
4236 return tevent_req_post(req, ev);
4239 if (max_protocol == PROTOCOL_NONE) {
4240 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
4241 return tevent_req_post(req, ev);
4244 if (min_protocol > max_protocol) {
4245 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
4246 return tevent_req_post(req, ev);
4249 conn->min_protocol = min_protocol;
4250 conn->max_protocol = max_protocol;
4251 conn->protocol = PROTOCOL_NONE;
4253 if (max_protocol >= PROTOCOL_SMB2_02) {
4254 conn->smb2.max_credits = max_credits;
4257 if ((min_protocol < PROTOCOL_SMB2_02) &&
4258 (max_protocol < PROTOCOL_SMB2_02)) {
4262 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4264 subreq = smbXcli_negprot_smb1_subreq(state);
4265 if (tevent_req_nomem(subreq, req)) {
4266 return tevent_req_post(req, ev);
4268 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4272 if ((min_protocol >= PROTOCOL_SMB2_02) &&
4273 (max_protocol >= PROTOCOL_SMB2_02)) {
4277 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4279 subreq = smbXcli_negprot_smb2_subreq(state);
4280 if (tevent_req_nomem(subreq, req)) {
4281 return tevent_req_post(req, ev);
4283 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4288 * We send an SMB1 negprot with the SMB2 dialects
4289 * and expect a SMB1 or a SMB2 response.
4291 * smbXcli_negprot_dispatch_incoming() will fix the
4292 * callback to match protocol of the response.
4294 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
4296 subreq = smbXcli_negprot_smb1_subreq(state);
4297 if (tevent_req_nomem(subreq, req)) {
4298 return tevent_req_post(req, ev);
4300 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
4304 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
4306 struct tevent_req *req =
4307 tevent_req_callback_data(subreq,
4312 * we just want the low level error
4314 status = tevent_req_simple_recv_ntstatus(subreq);
4315 TALLOC_FREE(subreq);
4316 if (tevent_req_nterror(req, status)) {
4320 /* this should never happen */
4321 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4324 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
4327 DATA_BLOB bytes = data_blob_null;
4331 /* setup the protocol strings */
4332 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4336 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4340 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4344 ok = data_blob_append(state, &bytes, &c, sizeof(c));
4350 * We now it is already ascii and
4351 * we want NULL termination.
4353 ok = data_blob_append(state, &bytes,
4354 smb1cli_prots[i].smb1_name,
4355 strlen(smb1cli_prots[i].smb1_name)+1);
4361 smb1cli_req_flags(state->conn->max_protocol,
4362 state->conn->smb1.client.capabilities,
4367 return smb1cli_req_send(state, state->ev, state->conn,
4371 state->timeout_msec,
4372 0xFFFE, 0, NULL, /* pid, tid, session */
4373 0, NULL, /* wct, vwv */
4374 bytes.length, bytes.data);
4377 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
4379 struct tevent_req *req =
4380 tevent_req_callback_data(subreq,
4382 struct smbXcli_negprot_state *state =
4383 tevent_req_data(req,
4384 struct smbXcli_negprot_state);
4385 struct smbXcli_conn *conn = state->conn;
4386 struct iovec *recv_iov = NULL;
4387 uint8_t *inhdr = NULL;
4395 size_t num_prots = 0;
4397 uint32_t client_capabilities = conn->smb1.client.capabilities;
4398 uint32_t both_capabilities;
4399 uint32_t server_capabilities = 0;
4400 uint32_t capabilities;
4401 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
4402 uint32_t server_max_xmit = 0;
4404 uint32_t server_max_mux = 0;
4405 uint16_t server_security_mode = 0;
4406 uint32_t server_session_key = 0;
4407 bool server_readbraw = false;
4408 bool server_writebraw = false;
4409 bool server_lockread = false;
4410 bool server_writeunlock = false;
4411 struct GUID server_guid = GUID_zero();
4412 DATA_BLOB server_gss_blob = data_blob_null;
4413 uint8_t server_challenge[8];
4414 char *server_workgroup = NULL;
4415 char *server_name = NULL;
4416 int server_time_zone = 0;
4417 NTTIME server_system_time = 0;
4418 static const struct smb1cli_req_expected_response expected[] = {
4420 .status = NT_STATUS_OK,
4421 .wct = 0x11, /* NT1 */
4424 .status = NT_STATUS_OK,
4425 .wct = 0x0D, /* LM */
4428 .status = NT_STATUS_OK,
4429 .wct = 0x01, /* CORE */
4433 ZERO_STRUCT(server_challenge);
4435 status = smb1cli_req_recv(subreq, state,
4440 NULL, /* pvwv_offset */
4443 NULL, /* pbytes_offset */
4445 expected, ARRAY_SIZE(expected));
4446 TALLOC_FREE(subreq);
4447 if (inhdr == NULL || tevent_req_nterror(req, status)) {
4451 flags = CVAL(inhdr, HDR_FLG);
4453 protnum = SVAL(vwv, 0);
4455 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4456 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4460 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4464 if (protnum != num_prots) {
4469 conn->protocol = smb1cli_prots[i].proto;
4473 if (conn->protocol == PROTOCOL_NONE) {
4474 DBG_ERR("No compatible protocol selected by server.\n");
4475 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4479 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
4480 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4481 "and the selected protocol level doesn't support it.\n"));
4482 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4486 if (flags & FLAG_SUPPORT_LOCKREAD) {
4487 server_lockread = true;
4488 server_writeunlock = true;
4491 if (conn->protocol >= PROTOCOL_NT1) {
4492 const char *client_signing = NULL;
4493 bool server_mandatory = false;
4494 bool server_allowed = false;
4495 const char *server_signing = NULL;
4500 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4505 server_security_mode = CVAL(vwv + 1, 0);
4506 server_max_mux = SVAL(vwv + 1, 1);
4507 server_max_xmit = IVAL(vwv + 3, 1);
4508 server_session_key = IVAL(vwv + 7, 1);
4509 server_time_zone = SVALS(vwv + 15, 1);
4510 server_time_zone *= 60;
4511 /* this time arrives in real GMT */
4512 server_system_time = BVAL(vwv + 11, 1);
4513 server_capabilities = IVAL(vwv + 9, 1);
4515 key_len = CVAL(vwv + 16, 1);
4517 if (server_capabilities & CAP_RAW_MODE) {
4518 server_readbraw = true;
4519 server_writebraw = true;
4521 if (server_capabilities & CAP_LOCK_AND_READ) {
4522 server_lockread = true;
4525 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4526 DATA_BLOB blob1, blob2;
4528 if (num_bytes < 16) {
4529 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4533 blob1 = data_blob_const(bytes, 16);
4534 status = GUID_from_data_blob(&blob1, &server_guid);
4535 if (tevent_req_nterror(req, status)) {
4539 blob1 = data_blob_const(bytes+16, num_bytes-16);
4540 blob2 = data_blob_dup_talloc(state, blob1);
4541 if (blob1.length > 0 &&
4542 tevent_req_nomem(blob2.data, req)) {
4545 server_gss_blob = blob2;
4547 DATA_BLOB blob1, blob2;
4549 if (num_bytes < key_len) {
4550 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4554 if (key_len != 0 && key_len != 8) {
4555 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4560 memcpy(server_challenge, bytes, 8);
4563 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4564 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4565 if (blob1.length > 0) {
4568 len = utf16_len_n(blob1.data,
4572 ok = convert_string_talloc(state,
4580 status = map_nt_error_from_unix_common(errno);
4581 tevent_req_nterror(req, status);
4586 blob2.data += blob1.length;
4587 blob2.length -= blob1.length;
4588 if (blob2.length > 0) {
4591 len = utf16_len_n(blob1.data,
4595 ok = convert_string_talloc(state,
4603 status = map_nt_error_from_unix_common(errno);
4604 tevent_req_nterror(req, status);
4610 client_signing = "disabled";
4611 if (conn->allow_signing) {
4612 client_signing = "allowed";
4614 if (conn->mandatory_signing) {
4615 client_signing = "required";
4618 server_signing = "not supported";
4619 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4620 server_signing = "supported";
4621 server_allowed = true;
4622 } else if (conn->mandatory_signing) {
4624 * We have mandatory signing as client
4625 * lets assume the server will look at our
4626 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4627 * flag in the session setup
4629 server_signing = "not announced";
4630 server_allowed = true;
4632 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4633 server_signing = "required";
4634 server_mandatory = true;
4637 ok = smb_signing_set_negotiated(conn->smb1.signing,
4641 DEBUG(1,("cli_negprot: SMB signing is required, "
4642 "but client[%s] and server[%s] mismatch\n",
4643 client_signing, server_signing));
4644 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4648 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4654 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4658 server_security_mode = SVAL(vwv + 1, 0);
4659 server_max_xmit = SVAL(vwv + 2, 0);
4660 server_max_mux = SVAL(vwv + 3, 0);
4661 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4662 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4663 server_session_key = IVAL(vwv + 6, 0);
4664 server_time_zone = SVALS(vwv + 10, 0);
4665 server_time_zone *= 60;
4666 /* this time is converted to GMT by make_unix_date */
4667 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4668 unix_to_nt_time(&server_system_time, t);
4669 key_len = SVAL(vwv + 11, 0);
4671 if (num_bytes < key_len) {
4672 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4676 if (key_len != 0 && key_len != 8) {
4677 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4682 memcpy(server_challenge, bytes, 8);
4685 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4686 if (blob1.length > 0) {
4690 len = utf16_len_n(blob1.data,
4694 ok = convert_string_talloc(state,
4702 status = map_nt_error_from_unix_common(errno);
4703 tevent_req_nterror(req, status);
4709 /* the old core protocol */
4710 server_time_zone = get_time_zone(time(NULL));
4711 server_max_xmit = 1024;
4715 if (server_max_xmit < 1024) {
4716 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4720 if (server_max_mux < 1) {
4721 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4726 * Now calculate the negotiated capabilities
4727 * based on the mask for:
4728 * - client only flags
4729 * - flags used in both directions
4730 * - server only flags
4732 both_capabilities = client_capabilities & server_capabilities;
4733 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4734 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4735 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4737 max_xmit = MIN(client_max_xmit, server_max_xmit);
4739 conn->smb1.server.capabilities = server_capabilities;
4740 conn->smb1.capabilities = capabilities;
4742 conn->smb1.server.max_xmit = server_max_xmit;
4743 conn->smb1.max_xmit = max_xmit;
4745 conn->smb1.server.max_mux = server_max_mux;
4747 conn->smb1.server.security_mode = server_security_mode;
4749 conn->smb1.server.readbraw = server_readbraw;
4750 conn->smb1.server.writebraw = server_writebraw;
4751 conn->smb1.server.lockread = server_lockread;
4752 conn->smb1.server.writeunlock = server_writeunlock;
4754 conn->smb1.server.session_key = server_session_key;
4756 talloc_steal(conn, server_gss_blob.data);
4757 conn->smb1.server.gss_blob = server_gss_blob;
4758 conn->smb1.server.guid = server_guid;
4759 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4760 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4761 conn->smb1.server.name = talloc_move(conn, &server_name);
4763 conn->smb1.server.time_zone = server_time_zone;
4764 conn->smb1.server.system_time = server_system_time;
4766 tevent_req_done(req);
4769 static size_t smbXcli_padding_helper(uint32_t offset, size_t n)
4771 if ((offset & (n-1)) == 0) return 0;
4772 return n - (offset & (n-1));
4775 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4779 uint16_t dialect_count = 0;
4780 DATA_BLOB dyn = data_blob_null;
4782 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4786 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4790 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4794 SSVAL(val, 0, smb2cli_prots[i].smb2_dialect);
4796 ok = data_blob_append(state, &dyn, val, sizeof(val));
4804 buf = state->smb2.fixed;
4806 SSVAL(buf, 2, dialect_count);
4807 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4808 SSVAL(buf, 6, 0); /* Reserved */
4809 if (state->conn->max_protocol >= PROTOCOL_SMB3_00) {
4810 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4812 SIVAL(buf, 8, 0); /* Capabilities */
4814 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4816 struct GUID_ndr_buf guid_buf = { .buf = {0}, };
4818 status = GUID_to_ndr_buf(&state->conn->smb2.client.guid,
4820 if (!NT_STATUS_IS_OK(status)) {
4823 memcpy(buf+12, guid_buf.buf, 16); /* ClientGuid */
4825 memset(buf+12, 0, 16); /* ClientGuid */
4828 if (state->conn->max_protocol >= PROTOCOL_SMB3_11) {
4829 const struct smb3_encryption_capabilities *client_ciphers =
4830 &state->conn->smb2.client.smb3_capabilities.encryption;
4832 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4833 uint8_t *netname_utf16 = NULL;
4834 size_t netname_utf16_len = 0;
4838 const uint8_t zeros[8] = {0, };
4842 SSVAL(p, 0, 1); /* HashAlgorithmCount */
4843 SSVAL(p, 2, 32); /* SaltLength */
4844 SSVAL(p, 4, SMB2_PREAUTH_INTEGRITY_SHA512);
4845 generate_random_buffer(p + 6, 32);
4847 status = smb2_negotiate_context_add(
4848 state, &c, SMB2_PREAUTH_INTEGRITY_CAPABILITIES, p, 38);
4849 if (!NT_STATUS_IS_OK(status)) {
4853 if (client_ciphers->num_algos > 0) {
4855 SSVAL(p, ofs, client_ciphers->num_algos);
4858 for (i = 0; i < client_ciphers->num_algos; i++) {
4859 size_t next_ofs = ofs + 2;
4860 SMB_ASSERT(next_ofs < ARRAY_SIZE(p));
4861 SSVAL(p, ofs, client_ciphers->algos[i]);
4865 status = smb2_negotiate_context_add(
4866 state, &c, SMB2_ENCRYPTION_CAPABILITIES, p, ofs);
4867 if (!NT_STATUS_IS_OK(status)) {
4872 ok = convert_string_talloc(state, CH_UNIX, CH_UTF16,
4873 state->conn->remote_name,
4874 strlen(state->conn->remote_name),
4875 &netname_utf16, &netname_utf16_len);
4880 status = smb2_negotiate_context_add(state, &c,
4881 SMB2_NETNAME_NEGOTIATE_CONTEXT_ID,
4882 netname_utf16, netname_utf16_len);
4883 if (!NT_STATUS_IS_OK(status)) {
4887 status = smb2_negotiate_context_push(state, &b, c);
4888 if (!NT_STATUS_IS_OK(status)) {
4892 offset = SMB2_HDR_BODY + sizeof(state->smb2.fixed) + dyn.length;
4893 pad = smbXcli_padding_helper(offset, 8);
4895 ok = data_blob_append(state, &dyn, zeros, pad);
4901 ok = data_blob_append(state, &dyn, b.data, b.length);
4906 SIVAL(buf, 28, offset); /* NegotiateContextOffset */
4907 SSVAL(buf, 32, c.num_contexts); /* NegotiateContextCount */
4908 SSVAL(buf, 34, 0); /* Reserved */
4910 SBVAL(buf, 28, 0); /* Reserved/ClientStartTime */
4913 return smb2cli_req_send(state, state->ev,
4914 state->conn, SMB2_OP_NEGPROT,
4916 state->timeout_msec,
4917 NULL, NULL, /* tcon, session */
4918 state->smb2.fixed, sizeof(state->smb2.fixed),
4919 dyn.data, dyn.length,
4920 UINT16_MAX); /* max_dyn_len */
4923 static NTSTATUS smbXcli_negprot_smb3_check_capabilities(struct tevent_req *req);
4925 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4927 struct tevent_req *req =
4928 tevent_req_callback_data(subreq,
4930 struct smbXcli_negprot_state *state =
4931 tevent_req_data(req,
4932 struct smbXcli_negprot_state);
4933 struct smbXcli_conn *conn = state->conn;
4934 size_t security_offset, security_length;
4937 struct iovec *iov = NULL;
4940 uint16_t dialect_revision;
4941 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4942 uint32_t negotiate_context_offset = 0;
4943 uint16_t negotiate_context_count = 0;
4944 DATA_BLOB negotiate_context_blob = data_blob_null;
4948 struct smb2_negotiate_context *preauth = NULL;
4949 uint16_t hash_count;
4950 uint16_t salt_length;
4951 uint16_t hash_selected;
4952 gnutls_hash_hd_t hash_hnd = NULL;
4953 struct smb2_negotiate_context *cipher = NULL;
4954 struct iovec sent_iov[3] = {{0}, {0}, {0}};
4955 static const struct smb2cli_req_expected_response expected[] = {
4957 .status = NT_STATUS_OK,
4963 status = smb2cli_req_recv(subreq, state, &iov,
4964 expected, ARRAY_SIZE(expected));
4965 if (tevent_req_nterror(req, status) || iov == NULL) {
4969 body = (uint8_t *)iov[1].iov_base;
4971 dialect_revision = SVAL(body, 4);
4973 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4974 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4978 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4982 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4986 conn->protocol = smb2cli_prots[i].proto;
4990 if (conn->protocol == PROTOCOL_NONE) {
4991 TALLOC_FREE(subreq);
4993 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
4994 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4998 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4999 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5003 /* make sure we do not loop forever */
5004 state->conn->min_protocol = PROTOCOL_SMB2_02;
5007 * send a SMB2 negprot, in order to negotiate
5010 subreq = smbXcli_negprot_smb2_subreq(state);
5011 if (tevent_req_nomem(subreq, req)) {
5014 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
5018 conn->smb2.server.security_mode = SVAL(body, 2);
5019 if (conn->protocol >= PROTOCOL_SMB3_11) {
5020 negotiate_context_count = SVAL(body, 6);
5023 blob = data_blob_const(body + 8, 16);
5024 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
5025 if (tevent_req_nterror(req, status)) {
5029 conn->smb2.server.capabilities = IVAL(body, 24);
5030 conn->smb2.server.max_trans_size= IVAL(body, 28);
5031 conn->smb2.server.max_read_size = IVAL(body, 32);
5032 conn->smb2.server.max_write_size= IVAL(body, 36);
5033 conn->smb2.server.system_time = BVAL(body, 40);
5034 conn->smb2.server.start_time = BVAL(body, 48);
5036 security_offset = SVAL(body, 56);
5037 security_length = SVAL(body, 58);
5039 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
5040 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5044 if (security_length > iov[2].iov_len) {
5045 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5049 conn->smb2.server.gss_blob = data_blob_talloc(conn,
5052 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
5056 if (conn->protocol >= PROTOCOL_SMB3_00) {
5057 conn->smb2.server.sign_algo = SMB2_SIGNING_AES128_CMAC;
5059 conn->smb2.server.sign_algo = SMB2_SIGNING_HMAC_SHA256;
5062 if (conn->protocol < PROTOCOL_SMB3_11) {
5063 TALLOC_FREE(subreq);
5065 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
5066 conn->smb2.server.cipher = SMB2_ENCRYPTION_AES128_CCM;
5069 status = smbXcli_negprot_smb3_check_capabilities(req);
5070 if (tevent_req_nterror(req, status)) {
5074 tevent_req_done(req);
5079 * Here we are now at SMB3_11, so encryption should be
5080 * negotiated via context, not capabilities.
5083 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
5085 * Server set SMB2_CAP_ENCRYPTION capability,
5086 * but *SHOULD* not, not *MUST* not. Just mask it off.
5087 * NetApp seems to do this:
5088 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13009
5090 conn->smb2.server.capabilities &= ~SMB2_CAP_ENCRYPTION;
5093 negotiate_context_offset = IVAL(body, 60);
5094 if (negotiate_context_offset < security_offset) {
5095 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5099 ctx_ofs = negotiate_context_offset - security_offset;
5100 if (ctx_ofs > iov[2].iov_len) {
5101 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5104 avail = iov[2].iov_len - security_length;
5105 needed = iov[2].iov_len - ctx_ofs;
5106 if (needed > avail) {
5107 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5111 negotiate_context_blob.data = (uint8_t *)iov[2].iov_base;
5112 negotiate_context_blob.length = iov[2].iov_len;
5114 negotiate_context_blob.data += ctx_ofs;
5115 negotiate_context_blob.length -= ctx_ofs;
5117 status = smb2_negotiate_context_parse(state,
5118 negotiate_context_blob,
5119 negotiate_context_count,
5121 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
5122 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
5124 if (tevent_req_nterror(req, status)) {
5128 preauth = smb2_negotiate_context_find(&c,
5129 SMB2_PREAUTH_INTEGRITY_CAPABILITIES);
5130 if (preauth == NULL) {
5131 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5135 if (preauth->data.length < 6) {
5136 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5140 hash_count = SVAL(preauth->data.data, 0);
5141 salt_length = SVAL(preauth->data.data, 2);
5142 hash_selected = SVAL(preauth->data.data, 4);
5144 if (hash_count != 1) {
5145 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5149 if (preauth->data.length != (6 + salt_length)) {
5150 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5154 if (hash_selected != SMB2_PREAUTH_INTEGRITY_SHA512) {
5155 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5159 cipher = smb2_negotiate_context_find(&c, SMB2_ENCRYPTION_CAPABILITIES);
5160 if (cipher != NULL) {
5161 const struct smb3_encryption_capabilities *client_ciphers =
5162 &state->conn->smb2.client.smb3_capabilities.encryption;
5163 bool found_selected = false;
5164 uint16_t cipher_count;
5165 uint16_t cipher_selected;
5167 if (client_ciphers->num_algos == 0) {
5169 * We didn't ask for SMB2_ENCRYPTION_CAPABILITIES
5171 tevent_req_nterror(req,
5172 NT_STATUS_INVALID_NETWORK_RESPONSE);
5176 if (cipher->data.length < 2) {
5177 tevent_req_nterror(req,
5178 NT_STATUS_INVALID_NETWORK_RESPONSE);
5182 cipher_count = SVAL(cipher->data.data, 0);
5183 if (cipher_count != 1) {
5184 tevent_req_nterror(req,
5185 NT_STATUS_INVALID_NETWORK_RESPONSE);
5189 if (cipher->data.length < (2 + 2 * cipher_count)) {
5190 tevent_req_nterror(req,
5191 NT_STATUS_INVALID_NETWORK_RESPONSE);
5194 cipher_selected = SVAL(cipher->data.data, 2);
5196 for (i = 0; i < client_ciphers->num_algos; i++) {
5197 if (cipher_selected == SMB2_ENCRYPTION_NONE) {
5199 * encryption not supported
5201 found_selected = true;
5204 if (client_ciphers->algos[i] == cipher_selected) {
5208 found_selected = true;
5213 if (!found_selected) {
5215 * The server send a cipher we didn't offer.
5217 tevent_req_nterror(req,
5218 NT_STATUS_INVALID_NETWORK_RESPONSE);
5222 conn->smb2.server.cipher = cipher_selected;
5225 /* First we hash the request */
5226 smb2cli_req_get_sent_iov(subreq, sent_iov);
5228 rc = gnutls_hash_init(&hash_hnd, GNUTLS_DIG_SHA512);
5230 tevent_req_nterror(req,
5231 gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED));
5235 rc = gnutls_hash(hash_hnd,
5236 conn->smb2.preauth_sha512,
5237 sizeof(conn->smb2.preauth_sha512));
5239 gnutls_hash_deinit(hash_hnd, NULL);
5240 tevent_req_nterror(req,
5241 gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED));
5244 for (i = 0; i < 3; i++) {
5245 rc = gnutls_hash(hash_hnd,
5246 sent_iov[i].iov_base,
5247 sent_iov[i].iov_len);
5249 gnutls_hash_deinit(hash_hnd, NULL);
5250 tevent_req_nterror(req,
5251 gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED));
5257 gnutls_hash_deinit(hash_hnd, NULL);
5258 tevent_req_nterror(req,
5259 gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED));
5263 /* This resets the hash state */
5264 gnutls_hash_output(hash_hnd, conn->smb2.preauth_sha512);
5265 TALLOC_FREE(subreq);
5267 /* And now we hash the response */
5268 rc = gnutls_hash(hash_hnd,
5269 conn->smb2.preauth_sha512,
5270 sizeof(conn->smb2.preauth_sha512));
5272 gnutls_hash_deinit(hash_hnd, NULL);
5273 tevent_req_nterror(req,
5274 gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED));
5277 for (i = 0; i < 3; i++) {
5278 rc = gnutls_hash(hash_hnd,
5282 gnutls_hash_deinit(hash_hnd, NULL);
5283 tevent_req_nterror(req,
5284 gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED));
5288 gnutls_hash_deinit(hash_hnd, conn->smb2.preauth_sha512);
5290 tevent_req_nterror(req,
5291 NT_STATUS_UNSUCCESSFUL);
5295 status = smbXcli_negprot_smb3_check_capabilities(req);
5296 if (tevent_req_nterror(req, status)) {
5300 tevent_req_done(req);
5303 static NTSTATUS smbXcli_negprot_smb3_check_capabilities(struct tevent_req *req)
5305 struct smbXcli_negprot_state *state =
5306 tevent_req_data(req,
5307 struct smbXcli_negprot_state);
5308 struct smbXcli_conn *conn = state->conn;
5310 return smb311_capabilities_check(&conn->smb2.client.smb3_capabilities,
5313 NT_STATUS_ACCESS_DENIED,
5316 conn->smb2.server.cipher);
5319 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
5320 TALLOC_CTX *tmp_mem,
5323 size_t num_pending = talloc_array_length(conn->pending);
5324 struct tevent_req *subreq;
5325 struct smbXcli_req_state *substate;
5326 struct tevent_req *req;
5327 uint32_t protocol_magic;
5328 size_t inbuf_len = smb_len_nbt(inbuf);
5330 if (num_pending != 1) {
5331 return NT_STATUS_INTERNAL_ERROR;
5334 if (inbuf_len < 4) {
5335 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5338 subreq = conn->pending[0];
5339 substate = tevent_req_data(subreq, struct smbXcli_req_state);
5340 req = tevent_req_callback_data(subreq, struct tevent_req);
5342 protocol_magic = IVAL(inbuf, 4);
5344 switch (protocol_magic) {
5346 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
5347 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
5348 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
5351 if (substate->smb2.recv_iov == NULL) {
5353 * For the SMB1 negprot we have move it.
5355 substate->smb2.recv_iov = substate->smb1.recv_iov;
5356 substate->smb1.recv_iov = NULL;
5360 * we got an SMB2 answer, which consumed sequence number 0
5361 * so we need to use 1 as the next one.
5363 * we also need to set the current credits to 0
5364 * as we consumed the initial one. The SMB2 answer
5365 * hopefully grant us a new credit.
5368 conn->smb2.cur_credits = 0;
5369 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
5370 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
5371 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
5374 DEBUG(10, ("Got non-SMB PDU\n"));
5375 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5378 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
5380 return tevent_req_simple_recv_ntstatus(req);
5383 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
5384 uint32_t timeout_msec,
5385 enum protocol_types min_protocol,
5386 enum protocol_types max_protocol)
5388 TALLOC_CTX *frame = talloc_stackframe();
5389 struct tevent_context *ev;
5390 struct tevent_req *req;
5391 NTSTATUS status = NT_STATUS_NO_MEMORY;
5394 if (smbXcli_conn_has_async_calls(conn)) {
5396 * Can't use sync call while an async call is in flight
5398 status = NT_STATUS_INVALID_PARAMETER_MIX;
5401 ev = samba_tevent_context_init(frame);
5405 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
5406 min_protocol, max_protocol,
5407 WINDOWS_CLIENT_PURE_SMB2_NEGPROT_INITIAL_CREDIT_ASK);
5411 ok = tevent_req_poll_ntstatus(req, ev, &status);
5415 status = smbXcli_negprot_recv(req);
5421 struct smb2cli_validate_negotiate_info_state {
5422 struct smbXcli_conn *conn;
5423 DATA_BLOB in_input_buffer;
5424 DATA_BLOB in_output_buffer;
5425 DATA_BLOB out_input_buffer;
5426 DATA_BLOB out_output_buffer;
5430 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq);
5432 struct tevent_req *smb2cli_validate_negotiate_info_send(TALLOC_CTX *mem_ctx,
5433 struct tevent_context *ev,
5434 struct smbXcli_conn *conn,
5435 uint32_t timeout_msec,
5436 struct smbXcli_session *session,
5437 struct smbXcli_tcon *tcon)
5439 struct tevent_req *req;
5440 struct smb2cli_validate_negotiate_info_state *state;
5442 uint16_t dialect_count = 0;
5443 struct tevent_req *subreq;
5444 bool _save_should_sign;
5447 req = tevent_req_create(mem_ctx, &state,
5448 struct smb2cli_validate_negotiate_info_state);
5454 state->in_input_buffer = data_blob_talloc_zero(state,
5455 4 + 16 + 1 + 1 + 2);
5456 if (tevent_req_nomem(state->in_input_buffer.data, req)) {
5457 return tevent_req_post(req, ev);
5459 buf = state->in_input_buffer.data;
5461 if (state->conn->max_protocol >= PROTOCOL_SMB3_00) {
5462 SIVAL(buf, 0, conn->smb2.client.capabilities);
5464 SIVAL(buf, 0, 0); /* Capabilities */
5466 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
5468 struct GUID_ndr_buf guid_buf = { .buf = {0}, };
5470 status = GUID_to_ndr_buf(&conn->smb2.client.guid,
5472 if (!NT_STATUS_IS_OK(status)) {
5475 memcpy(buf+4, guid_buf.buf, 16); /* ClientGuid */
5477 memset(buf+4, 0, 16); /* ClientGuid */
5479 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
5480 SCVAL(buf, 20, conn->smb2.client.security_mode);
5484 SCVAL(buf, 21, 0); /* reserved */
5486 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
5490 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
5494 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
5498 if (smb2cli_prots[i].proto == state->conn->protocol) {
5499 state->dialect = smb2cli_prots[i].smb2_dialect;
5502 ofs = state->in_input_buffer.length;
5503 ok = data_blob_realloc(state, &state->in_input_buffer,
5506 tevent_req_oom(req);
5507 return tevent_req_post(req, ev);
5510 buf = state->in_input_buffer.data;
5511 SSVAL(buf, ofs, smb2cli_prots[i].smb2_dialect);
5515 buf = state->in_input_buffer.data;
5516 SSVAL(buf, 22, dialect_count);
5518 _save_should_sign = smb2cli_tcon_is_signing_on(tcon);
5519 smb2cli_tcon_should_sign(tcon, true);
5520 subreq = smb2cli_ioctl_send(state, ev, conn,
5521 timeout_msec, session, tcon,
5522 UINT64_MAX, /* in_fid_persistent */
5523 UINT64_MAX, /* in_fid_volatile */
5524 FSCTL_VALIDATE_NEGOTIATE_INFO,
5525 0, /* in_max_input_length */
5526 &state->in_input_buffer,
5527 24, /* in_max_output_length */
5528 &state->in_output_buffer,
5529 SMB2_IOCTL_FLAG_IS_FSCTL);
5530 smb2cli_tcon_should_sign(tcon, _save_should_sign);
5531 if (tevent_req_nomem(subreq, req)) {
5532 return tevent_req_post(req, ev);
5534 tevent_req_set_callback(subreq,
5535 smb2cli_validate_negotiate_info_done,
5541 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq)
5543 struct tevent_req *req =
5544 tevent_req_callback_data(subreq,
5546 struct smb2cli_validate_negotiate_info_state *state =
5547 tevent_req_data(req,
5548 struct smb2cli_validate_negotiate_info_state);
5551 uint32_t capabilities;
5552 DATA_BLOB guid_blob;
5553 struct GUID server_guid;
5554 uint16_t security_mode;
5557 status = smb2cli_ioctl_recv(subreq, state,
5558 &state->out_input_buffer,
5559 &state->out_output_buffer);
5560 TALLOC_FREE(subreq);
5563 * This response must be signed correctly for
5564 * these "normal" error codes to be processed.
5565 * If the packet wasn't signed correctly we will get
5566 * NT_STATUS_ACCESS_DENIED or NT_STATUS_HMAC_NOT_SUPPORTED,
5567 * or NT_STATUS_INVALID_NETWORK_RESPONSE
5568 * from smb2_signing_check_pdu().
5570 * We must never ignore the above errors here.
5573 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
5575 * The response was signed, but not supported
5577 * Older Windows and Samba releases return
5578 * NT_STATUS_FILE_CLOSED.
5580 tevent_req_done(req);
5583 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_DEVICE_REQUEST)) {
5585 * The response was signed, but not supported
5587 * This is returned by the NTVFS based Samba 4.x file server
5590 tevent_req_done(req);
5593 if (NT_STATUS_EQUAL(status, NT_STATUS_FS_DRIVER_REQUIRED)) {
5595 * The response was signed, but not supported
5597 * This is returned by the NTVFS based Samba 4.x file server
5600 tevent_req_done(req);
5603 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
5605 * The response was signed, but not supported
5607 * This might be returned by older Windows versions or by
5608 * NetApp SMB server implementations.
5612 * https://blogs.msdn.microsoft.com/openspecification/2012/06/28/smb3-secure-dialect-negotiation/
5615 tevent_req_done(req);
5618 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
5620 * The response was signed, but not supported
5622 * This might be returned by NetApp Ontap 7.3.7 SMB server
5625 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=14607
5628 tevent_req_done(req);
5631 if (tevent_req_nterror(req, status)) {
5635 if (state->out_output_buffer.length != 24) {
5636 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5640 buf = state->out_output_buffer.data;
5642 capabilities = IVAL(buf, 0);
5643 guid_blob = data_blob_const(buf + 4, 16);
5644 status = GUID_from_data_blob(&guid_blob, &server_guid);
5645 if (tevent_req_nterror(req, status)) {
5648 security_mode = CVAL(buf, 20);
5649 dialect = SVAL(buf, 22);
5651 if (capabilities != state->conn->smb2.server.capabilities) {
5652 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5656 if (!GUID_equal(&server_guid, &state->conn->smb2.server.guid)) {
5657 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5661 if (security_mode != state->conn->smb2.server.security_mode) {
5662 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5666 if (dialect != state->dialect) {
5667 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5671 tevent_req_done(req);
5674 NTSTATUS smb2cli_validate_negotiate_info_recv(struct tevent_req *req)
5676 return tevent_req_simple_recv_ntstatus(req);
5679 static int smbXcli_session_destructor(struct smbXcli_session *session)
5681 if (session->conn == NULL) {
5685 DLIST_REMOVE(session->conn->sessions, session);
5689 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
5690 struct smbXcli_conn *conn)
5692 struct smbXcli_session *session;
5695 session = talloc_zero(mem_ctx, struct smbXcli_session);
5696 if (session == NULL) {
5699 session->smb2 = talloc_zero(session, struct smb2cli_session);
5700 if (session->smb2 == NULL) {
5701 talloc_free(session);
5704 talloc_set_destructor(session, smbXcli_session_destructor);
5706 status = smb2_signing_key_sign_create(session->smb2,
5707 conn->smb2.server.sign_algo,
5708 NULL, /* no master key */
5709 NULL, /* derivations */
5710 &session->smb2->signing_key);
5711 if (!NT_STATUS_IS_OK(status)) {
5712 talloc_free(session);
5716 DLIST_ADD_END(conn->sessions, session);
5717 session->conn = conn;
5719 status = smb2_signing_key_sign_create(session,
5720 conn->smb2.server.sign_algo,
5721 NULL, /* no master key */
5722 NULL, /* derivations */
5723 &session->smb2_channel.signing_key);
5724 if (!NT_STATUS_IS_OK(status)) {
5725 talloc_free(session);
5729 memcpy(session->smb2_channel.preauth_sha512,
5730 conn->smb2.preauth_sha512,
5731 sizeof(session->smb2_channel.preauth_sha512));
5736 struct smbXcli_session *smbXcli_session_shallow_copy(TALLOC_CTX *mem_ctx,
5737 struct smbXcli_session *src)
5739 struct smbXcli_session *session;
5743 session = talloc_zero(mem_ctx, struct smbXcli_session);
5744 if (session == NULL) {
5747 session->smb2 = talloc_zero(session, struct smb2cli_session);
5748 if (session->smb2 == NULL) {
5749 talloc_free(session);
5754 * Note we keep a pointer to the session keys of the
5755 * main session and rely on the caller to free the
5756 * shallow copy first!
5758 session->conn = src->conn;
5759 *session->smb2 = *src->smb2;
5760 session->smb2_channel = src->smb2_channel;
5761 session->disconnect_expired = src->disconnect_expired;
5764 * This is only supposed to be called in test code
5765 * but we should not reuse nonces!
5767 * Add the current timestamp as NTTIME to nonce_high
5768 * and set nonce_low to a value we can recognize in captures.
5770 clock_gettime_mono(&ts);
5771 nt = unix_timespec_to_nt_time(ts);
5772 nt &= session->smb2->nonce_high_max;
5773 if (nt == session->smb2->nonce_high_max || nt < UINT8_MAX) {
5774 talloc_free(session);
5777 session->smb2->nonce_high += nt;
5778 session->smb2->nonce_low = UINT32_MAX;
5780 DLIST_ADD_END(src->conn->sessions, session);
5781 talloc_set_destructor(session, smbXcli_session_destructor);
5786 bool smbXcli_session_is_guest(struct smbXcli_session *session)
5788 if (session == NULL) {
5792 if (session->conn == NULL) {
5796 if (session->conn->mandatory_signing) {
5800 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5801 if (session->smb2->session_flags & SMB2_SESSION_FLAG_IS_GUEST) {
5807 if (session->smb1.action & SMB_SETUP_GUEST) {
5814 bool smbXcli_session_is_authenticated(struct smbXcli_session *session)
5816 const DATA_BLOB *application_key;
5818 if (session == NULL) {
5822 if (session->conn == NULL) {
5827 * If we have an application key we had a session key negotiated
5830 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5831 if (!smb2_signing_key_valid(session->smb2->application_key)) {
5834 application_key = &session->smb2->application_key->blob;
5836 application_key = &session->smb1.application_key;
5839 if (application_key->length == 0) {
5846 NTSTATUS smb2cli_session_signing_key(struct smbXcli_session *session,
5847 TALLOC_CTX *mem_ctx,
5850 const struct smb2_signing_key *sig = NULL;
5852 if (session->conn == NULL) {
5853 return NT_STATUS_NO_USER_SESSION_KEY;
5857 * Use channel signing key if there is one, otherwise fallback
5861 if (smb2_signing_key_valid(session->smb2_channel.signing_key)) {
5862 sig = session->smb2_channel.signing_key;
5863 } else if (smb2_signing_key_valid(session->smb2->signing_key)) {
5864 sig = session->smb2->signing_key;
5866 return NT_STATUS_NO_USER_SESSION_KEY;
5869 *key = data_blob_dup_talloc(mem_ctx, sig->blob);
5870 if (key->data == NULL) {
5871 return NT_STATUS_NO_MEMORY;
5874 return NT_STATUS_OK;
5877 NTSTATUS smb2cli_session_encryption_key(struct smbXcli_session *session,
5878 TALLOC_CTX *mem_ctx,
5881 if (session->conn == NULL) {
5882 return NT_STATUS_NO_USER_SESSION_KEY;
5885 if (session->conn->protocol < PROTOCOL_SMB3_00) {
5886 return NT_STATUS_NO_USER_SESSION_KEY;
5889 if (!smb2_signing_key_valid(session->smb2->encryption_key)) {
5890 return NT_STATUS_NO_USER_SESSION_KEY;
5893 *key = data_blob_dup_talloc(mem_ctx, session->smb2->encryption_key->blob);
5894 if (key->data == NULL) {
5895 return NT_STATUS_NO_MEMORY;
5898 return NT_STATUS_OK;
5901 NTSTATUS smb2cli_session_decryption_key(struct smbXcli_session *session,
5902 TALLOC_CTX *mem_ctx,
5905 if (session->conn == NULL) {
5906 return NT_STATUS_NO_USER_SESSION_KEY;
5909 if (session->conn->protocol < PROTOCOL_SMB3_00) {
5910 return NT_STATUS_NO_USER_SESSION_KEY;
5913 if (!smb2_signing_key_valid(session->smb2->decryption_key)) {
5914 return NT_STATUS_NO_USER_SESSION_KEY;
5917 *key = data_blob_dup_talloc(mem_ctx, session->smb2->decryption_key->blob);
5918 if (key->data == NULL) {
5919 return NT_STATUS_NO_MEMORY;
5922 return NT_STATUS_OK;
5925 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
5926 TALLOC_CTX *mem_ctx,
5929 const DATA_BLOB *application_key;
5931 *key = data_blob_null;
5933 if (session->conn == NULL) {
5934 return NT_STATUS_NO_USER_SESSION_KEY;
5937 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5938 if (!smb2_signing_key_valid(session->smb2->application_key)) {
5939 return NT_STATUS_NO_USER_SESSION_KEY;
5941 application_key = &session->smb2->application_key->blob;
5943 application_key = &session->smb1.application_key;
5946 if (application_key->length == 0) {
5947 return NT_STATUS_NO_USER_SESSION_KEY;
5950 *key = data_blob_dup_talloc(mem_ctx, *application_key);
5951 if (key->data == NULL) {
5952 return NT_STATUS_NO_MEMORY;
5955 return NT_STATUS_OK;
5958 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
5960 session->disconnect_expired = true;
5963 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
5965 return session->smb1.session_id;
5968 void smb1cli_session_set_id(struct smbXcli_session *session,
5969 uint16_t session_id)
5971 session->smb1.session_id = session_id;
5974 void smb1cli_session_set_action(struct smbXcli_session *session,
5977 session->smb1.action = action;
5980 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
5981 const DATA_BLOB _session_key)
5983 struct smbXcli_conn *conn = session->conn;
5984 uint8_t session_key[16];
5987 return NT_STATUS_INVALID_PARAMETER_MIX;
5990 if (session->smb1.application_key.length != 0) {
5992 * TODO: do not allow this...
5994 * return NT_STATUS_INVALID_PARAMETER_MIX;
5996 data_blob_clear_free(&session->smb1.application_key);
5997 session->smb1.protected_key = false;
6000 if (_session_key.length == 0) {
6001 return NT_STATUS_OK;
6004 ZERO_STRUCT(session_key);
6005 memcpy(session_key, _session_key.data,
6006 MIN(_session_key.length, sizeof(session_key)));
6008 session->smb1.application_key = data_blob_talloc(session,
6010 sizeof(session_key));
6011 ZERO_STRUCT(session_key);
6012 if (session->smb1.application_key.data == NULL) {
6013 return NT_STATUS_NO_MEMORY;
6016 session->smb1.protected_key = false;
6018 return NT_STATUS_OK;
6021 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
6025 if (session->smb1.protected_key) {
6026 /* already protected */
6027 return NT_STATUS_OK;
6030 if (session->smb1.application_key.length != 16) {
6031 return NT_STATUS_INVALID_PARAMETER_MIX;
6034 status = smb_key_derivation(session->smb1.application_key.data,
6035 session->smb1.application_key.length,
6036 session->smb1.application_key.data);
6037 if (!NT_STATUS_IS_OK(status)) {
6041 session->smb1.protected_key = true;
6043 return NT_STATUS_OK;
6046 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
6048 struct smbXcli_conn *conn = session->conn;
6049 uint8_t security_mode = 0;
6052 return security_mode;
6055 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
6056 if (conn->mandatory_signing) {
6057 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
6059 if (session->smb2->should_sign) {
6060 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
6063 return security_mode;
6066 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
6068 return session->smb2->session_id;
6071 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
6073 return session->smb2->session_flags;
6076 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
6077 uint64_t session_id,
6078 uint16_t session_flags)
6080 session->smb2->session_id = session_id;
6081 session->smb2->session_flags = session_flags;
6084 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
6086 session->smb2->channel_sequence += 1;
6089 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session *session,
6090 uint16_t channel_sequence)
6094 prev_cs = session->smb2->channel_sequence;
6095 session->smb2->channel_sequence = channel_sequence;
6100 uint16_t smb2cli_session_current_channel_sequence(struct smbXcli_session *session)
6102 return session->smb2->channel_sequence;
6105 void smb2cli_session_start_replay(struct smbXcli_session *session)
6107 session->smb2->replay_active = true;
6110 void smb2cli_session_stop_replay(struct smbXcli_session *session)
6112 session->smb2->replay_active = false;
6115 void smb2cli_session_require_signed_response(struct smbXcli_session *session,
6116 bool require_signed_response)
6118 session->smb2->require_signed_response = require_signed_response;
6121 NTSTATUS smb2cli_session_update_preauth(struct smbXcli_session *session,
6122 const struct iovec *iov)
6124 gnutls_hash_hd_t hash_hnd = NULL;
6128 if (session->conn == NULL) {
6129 return NT_STATUS_INTERNAL_ERROR;
6132 if (session->conn->protocol < PROTOCOL_SMB3_11) {
6133 return NT_STATUS_OK;
6136 if (smb2_signing_key_valid(session->smb2_channel.signing_key)) {
6137 return NT_STATUS_OK;
6140 rc = gnutls_hash_init(&hash_hnd,
6143 return gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
6146 rc = gnutls_hash(hash_hnd,
6147 session->smb2_channel.preauth_sha512,
6148 sizeof(session->smb2_channel.preauth_sha512));
6150 gnutls_hash_deinit(hash_hnd, NULL);
6151 return gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
6153 for (i = 0; i < 3; i++) {
6154 rc = gnutls_hash(hash_hnd,
6158 gnutls_hash_deinit(hash_hnd, NULL);
6159 return gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
6162 gnutls_hash_deinit(hash_hnd, session->smb2_channel.preauth_sha512);
6164 return NT_STATUS_OK;
6167 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
6168 const DATA_BLOB _session_key,
6169 const struct iovec *recv_iov)
6171 struct smbXcli_conn *conn = session->conn;
6172 uint16_t no_sign_flags = 0;
6173 bool check_signature = true;
6176 struct smb2_signing_derivations derivations = {
6179 DATA_BLOB preauth_hash = data_blob_null;
6180 size_t nonce_size = 0;
6183 return NT_STATUS_INVALID_PARAMETER_MIX;
6186 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
6187 return NT_STATUS_INVALID_PARAMETER_MIX;
6190 if (!conn->mandatory_signing) {
6192 * only allow guest sessions without
6193 * mandatory signing.
6195 * If we try an authentication with username != ""
6196 * and the server let us in without verifying the
6197 * password we don't have a negotiated session key
6200 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST;
6203 if (session->smb2->session_flags & no_sign_flags) {
6204 session->smb2->should_sign = false;
6205 return NT_STATUS_OK;
6208 if (smb2_signing_key_valid(session->smb2->signing_key)) {
6209 return NT_STATUS_INVALID_PARAMETER_MIX;
6212 if (conn->protocol >= PROTOCOL_SMB3_11) {
6213 preauth_hash = data_blob_const(session->smb2_channel.preauth_sha512,
6214 sizeof(session->smb2_channel.preauth_sha512));
6217 smb2_signing_derivations_fill_const_stack(&derivations,
6221 status = smb2_signing_key_sign_create(session->smb2,
6222 conn->smb2.server.sign_algo,
6224 derivations.signing,
6225 &session->smb2->signing_key);
6226 if (!NT_STATUS_IS_OK(status)) {
6230 status = smb2_signing_key_cipher_create(session->smb2,
6231 conn->smb2.server.cipher,
6233 derivations.cipher_c2s,
6234 &session->smb2->encryption_key);
6235 if (!NT_STATUS_IS_OK(status)) {
6239 status = smb2_signing_key_cipher_create(session->smb2,
6240 conn->smb2.server.cipher,
6242 derivations.cipher_s2c,
6243 &session->smb2->decryption_key);
6244 if (!NT_STATUS_IS_OK(status)) {
6248 status = smb2_signing_key_sign_create(session->smb2,
6249 conn->smb2.server.sign_algo,
6251 derivations.application,
6252 &session->smb2->application_key);
6253 if (!NT_STATUS_IS_OK(status)) {
6257 status = smb2_signing_key_copy(session,
6258 session->smb2->signing_key,
6259 &session->smb2_channel.signing_key);
6260 if (!NT_STATUS_IS_OK(status)) {
6264 check_signature = conn->mandatory_signing;
6266 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
6267 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
6269 * Sadly some vendors don't sign the
6270 * final SMB2 session setup response
6272 * At least Windows and Samba are always doing this
6273 * if there's a session key available.
6275 * We only check the signature if it's mandatory
6276 * or SMB2_HDR_FLAG_SIGNED is provided.
6278 check_signature = true;
6281 if (conn->protocol >= PROTOCOL_SMB3_11) {
6282 check_signature = true;
6285 if (check_signature) {
6286 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
6288 if (!NT_STATUS_IS_OK(status)) {
6293 session->smb2->should_sign = false;
6294 session->smb2->should_encrypt = false;
6296 if (conn->desire_signing) {
6297 session->smb2->should_sign = true;
6300 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
6301 session->smb2->should_sign = true;
6304 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
6305 session->smb2->should_encrypt = true;
6308 if (conn->protocol < PROTOCOL_SMB3_00) {
6309 session->smb2->should_encrypt = false;
6312 if (conn->smb2.server.cipher == 0) {
6313 session->smb2->should_encrypt = false;
6317 * CCM and GCM algorithms must never have their
6318 * nonce wrap, or the security of the whole
6319 * communication and the keys is destroyed.
6320 * We must drop the connection once we have
6321 * transfered too much data.
6323 * NOTE: We assume nonces greater than 8 bytes.
6325 generate_nonce_buffer((uint8_t *)&session->smb2->nonce_high_random,
6326 sizeof(session->smb2->nonce_high_random));
6327 switch (conn->smb2.server.cipher) {
6328 case SMB2_ENCRYPTION_AES128_CCM:
6329 nonce_size = SMB2_AES_128_CCM_NONCE_SIZE;
6331 case SMB2_ENCRYPTION_AES128_GCM:
6332 nonce_size = gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_128_GCM);
6334 case SMB2_ENCRYPTION_AES256_CCM:
6335 nonce_size = SMB2_AES_128_CCM_NONCE_SIZE;
6337 case SMB2_ENCRYPTION_AES256_GCM:
6338 nonce_size = gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_256_GCM);
6344 session->smb2->nonce_high_max = SMB2_NONCE_HIGH_MAX(nonce_size);
6345 session->smb2->nonce_high = 0;
6346 session->smb2->nonce_low = 0;
6348 return NT_STATUS_OK;
6351 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
6352 struct smbXcli_session *session1,
6353 struct smbXcli_conn *conn,
6354 struct smbXcli_session **_session2)
6356 struct smbXcli_session *session2;
6359 if (!smb2_signing_key_valid(session1->smb2->signing_key)) {
6360 return NT_STATUS_INVALID_PARAMETER_MIX;
6364 return NT_STATUS_INVALID_PARAMETER_MIX;
6367 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
6368 if (session2 == NULL) {
6369 return NT_STATUS_NO_MEMORY;
6371 session2->smb2 = talloc_reference(session2, session1->smb2);
6372 if (session2->smb2 == NULL) {
6373 talloc_free(session2);
6374 return NT_STATUS_NO_MEMORY;
6377 talloc_set_destructor(session2, smbXcli_session_destructor);
6378 DLIST_ADD_END(conn->sessions, session2);
6379 session2->conn = conn;
6381 status = smb2_signing_key_sign_create(session2,
6382 conn->smb2.server.sign_algo,
6383 NULL, /* no master key */
6384 NULL, /* derivations */
6385 &session2->smb2_channel.signing_key);
6386 if (!NT_STATUS_IS_OK(status)) {
6387 talloc_free(session2);
6388 return NT_STATUS_NO_MEMORY;
6391 memcpy(session2->smb2_channel.preauth_sha512,
6392 conn->smb2.preauth_sha512,
6393 sizeof(session2->smb2_channel.preauth_sha512));
6395 *_session2 = session2;
6396 return NT_STATUS_OK;
6399 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
6400 const DATA_BLOB _channel_key,
6401 const struct iovec *recv_iov)
6403 struct smbXcli_conn *conn = session->conn;
6404 uint8_t channel_key[16];
6406 struct _derivation {
6411 struct _derivation signing;
6413 .signing.label.length = 0,
6417 return NT_STATUS_INVALID_PARAMETER_MIX;
6420 if (smb2_signing_key_valid(session->smb2_channel.signing_key)) {
6421 return NT_STATUS_INVALID_PARAMETER_MIX;
6424 if (conn->protocol >= PROTOCOL_SMB3_11) {
6425 struct _derivation *d;
6428 p = data_blob_const(session->smb2_channel.preauth_sha512,
6429 sizeof(session->smb2_channel.preauth_sha512));
6431 d = &derivation.signing;
6432 d->label = data_blob_string_const_null("SMBSigningKey");
6434 } else if (conn->protocol >= PROTOCOL_SMB3_00) {
6435 struct _derivation *d;
6437 d = &derivation.signing;
6438 d->label = data_blob_string_const_null("SMB2AESCMAC");
6439 d->context = data_blob_string_const_null("SmbSign");
6442 ZERO_STRUCT(channel_key);
6443 memcpy(channel_key, _channel_key.data,
6444 MIN(_channel_key.length, sizeof(channel_key)));
6446 session->smb2_channel.signing_key->blob =
6447 data_blob_talloc(session->smb2_channel.signing_key,
6449 sizeof(channel_key));
6450 if (!smb2_signing_key_valid(session->smb2_channel.signing_key)) {
6451 ZERO_STRUCT(channel_key);
6452 return NT_STATUS_NO_MEMORY;
6455 if (conn->protocol >= PROTOCOL_SMB3_00) {
6456 struct _derivation *d = &derivation.signing;
6458 status = smb2_key_derivation(channel_key, sizeof(channel_key),
6459 d->label.data, d->label.length,
6460 d->context.data, d->context.length,
6461 session->smb2_channel.signing_key->blob.data,
6462 session->smb2_channel.signing_key->blob.length);
6463 if (!NT_STATUS_IS_OK(status)) {
6467 ZERO_STRUCT(channel_key);
6469 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
6471 if (!NT_STATUS_IS_OK(status)) {
6475 return NT_STATUS_OK;
6478 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
6480 if (!session->smb2->should_sign) {
6482 * We need required signing on the session
6483 * in order to prevent man in the middle attacks.
6485 return NT_STATUS_INVALID_PARAMETER_MIX;
6488 if (session->smb2->should_encrypt) {
6489 return NT_STATUS_OK;
6492 if (session->conn->protocol < PROTOCOL_SMB3_00) {
6493 return NT_STATUS_NOT_SUPPORTED;
6496 if (session->conn->smb2.server.cipher == 0) {
6497 return NT_STATUS_NOT_SUPPORTED;
6500 if (!smb2_signing_key_valid(session->smb2->signing_key)) {
6501 return NT_STATUS_NOT_SUPPORTED;
6503 session->smb2->should_encrypt = true;
6504 return NT_STATUS_OK;
6507 uint16_t smb2cli_session_get_encryption_cipher(struct smbXcli_session *session)
6509 if (session->conn->protocol < PROTOCOL_SMB3_00) {
6513 if (!session->smb2->should_encrypt) {
6517 return session->conn->smb2.server.cipher;
6520 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
6522 struct smbXcli_tcon *tcon;
6524 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
6533 * Return a deep structure copy of a struct smbXcli_tcon *
6536 struct smbXcli_tcon *smbXcli_tcon_copy(TALLOC_CTX *mem_ctx,
6537 const struct smbXcli_tcon *tcon_in)
6539 struct smbXcli_tcon *tcon;
6541 tcon = talloc_memdup(mem_ctx, tcon_in, sizeof(struct smbXcli_tcon));
6546 /* Deal with the SMB1 strings. */
6547 if (tcon_in->smb1.service != NULL) {
6548 tcon->smb1.service = talloc_strdup(tcon, tcon_in->smb1.service);
6549 if (tcon->smb1.service == NULL) {
6554 if (tcon->smb1.fs_type != NULL) {
6555 tcon->smb1.fs_type = talloc_strdup(tcon, tcon_in->smb1.fs_type);
6556 if (tcon->smb1.fs_type == NULL) {
6564 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
6565 uint32_t fs_attributes)
6567 tcon->fs_attributes = fs_attributes;
6570 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
6572 return tcon->fs_attributes;
6575 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
6581 if (tcon->is_smb1) {
6582 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
6589 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
6596 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
6598 return tcon->smb1.tcon_id;
6601 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
6603 tcon->is_smb1 = true;
6604 tcon->smb1.tcon_id = tcon_id;
6607 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
6609 uint16_t optional_support,
6610 uint32_t maximal_access,
6611 uint32_t guest_maximal_access,
6612 const char *service,
6613 const char *fs_type)
6615 tcon->is_smb1 = true;
6616 tcon->fs_attributes = 0;
6617 tcon->smb1.tcon_id = tcon_id;
6618 tcon->smb1.optional_support = optional_support;
6619 tcon->smb1.maximal_access = maximal_access;
6620 tcon->smb1.guest_maximal_access = guest_maximal_access;
6622 TALLOC_FREE(tcon->smb1.service);
6623 tcon->smb1.service = talloc_strdup(tcon, service);
6624 if (service != NULL && tcon->smb1.service == NULL) {
6628 TALLOC_FREE(tcon->smb1.fs_type);
6629 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
6630 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
6637 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
6639 return tcon->smb2.tcon_id;
6642 void smb2cli_tcon_set_id(struct smbXcli_tcon *tcon, uint32_t tcon_id)
6644 tcon->smb2.tcon_id = tcon_id;
6647 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
6649 return tcon->smb2.capabilities;
6652 uint32_t smb2cli_tcon_flags(struct smbXcli_tcon *tcon)
6654 return tcon->smb2.flags;
6657 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
6658 struct smbXcli_session *session,
6662 uint32_t capabilities,
6663 uint32_t maximal_access)
6665 tcon->is_smb1 = false;
6666 tcon->fs_attributes = 0;
6667 tcon->smb2.tcon_id = tcon_id;
6668 tcon->smb2.type = type;
6669 tcon->smb2.flags = flags;
6670 tcon->smb2.capabilities = capabilities;
6671 tcon->smb2.maximal_access = maximal_access;
6673 tcon->smb2.should_sign = false;
6674 tcon->smb2.should_encrypt = false;
6676 if (session == NULL) {
6680 tcon->smb2.should_sign = session->smb2->should_sign;
6681 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
6683 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
6684 tcon->smb2.should_encrypt = true;
6688 void smb2cli_tcon_should_sign(struct smbXcli_tcon *tcon,
6691 tcon->smb2.should_sign = should_sign;
6694 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon *tcon)
6696 if (tcon->smb2.should_encrypt) {
6700 return tcon->smb2.should_sign;
6703 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon *tcon,
6704 bool should_encrypt)
6706 tcon->smb2.should_encrypt = should_encrypt;
6709 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
6711 return tcon->smb2.should_encrypt;
6714 void smb2cli_conn_set_mid(struct smbXcli_conn *conn, uint64_t mid)
6716 conn->smb2.mid = mid;
6719 uint64_t smb2cli_conn_get_mid(struct smbXcli_conn *conn)
6721 return conn->smb2.mid;