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 "lib/crypto/sha512.h"
37 #include "lib/crypto/aes.h"
38 #include "lib/crypto/aes_ccm_128.h"
39 #include "lib/crypto/aes_gcm_128.h"
43 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;
57 enum protocol_types min_protocol;
58 enum protocol_types max_protocol;
59 enum protocol_types protocol;
62 bool mandatory_signing;
65 * The incoming dispatch function should return:
66 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
67 * - NT_STATUS_OK, if no more processing is desired, e.g.
68 * the dispatch function called
70 * - All other return values disconnect the connection.
72 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
78 uint32_t capabilities;
83 uint32_t capabilities;
86 uint16_t security_mode;
95 const char *workgroup;
101 uint32_t capabilities;
106 struct smb_signing_state *signing;
107 struct smb_trans_enc_state *trans_enc;
109 struct tevent_req *read_braw_req;
114 uint32_t capabilities;
115 uint16_t security_mode;
120 uint32_t capabilities;
121 uint16_t security_mode;
123 uint32_t max_trans_size;
124 uint32_t max_read_size;
125 uint32_t max_write_size;
133 uint16_t cur_credits;
134 uint16_t max_credits;
136 uint32_t cc_chunk_len;
137 uint32_t cc_max_chunks;
141 uint8_t preauth_sha512[64];
144 struct smbXcli_session *sessions;
147 struct smb2cli_session {
149 uint16_t session_flags;
150 DATA_BLOB application_key;
151 DATA_BLOB signing_key;
154 DATA_BLOB encryption_key;
155 DATA_BLOB decryption_key;
156 uint64_t nonce_high_random;
157 uint64_t nonce_high_max;
160 uint16_t channel_sequence;
164 struct smbXcli_session {
165 struct smbXcli_session *prev, *next;
166 struct smbXcli_conn *conn;
170 DATA_BLOB application_key;
174 struct smb2cli_session *smb2;
177 DATA_BLOB signing_key;
178 uint8_t preauth_sha512[64];
182 * this should be a short term hack
183 * until the upper layers have implemented
186 bool disconnect_expired;
189 struct smbXcli_tcon {
191 uint32_t fs_attributes;
195 uint16_t optional_support;
196 uint32_t maximal_access;
197 uint32_t guest_maximal_access;
206 uint32_t capabilities;
207 uint32_t maximal_access;
213 struct smbXcli_req_state {
214 struct tevent_context *ev;
215 struct smbXcli_conn *conn;
216 struct smbXcli_session *session; /* maybe NULL */
217 struct smbXcli_tcon *tcon; /* maybe NULL */
219 uint8_t length_hdr[4];
226 /* Space for the header including the wct */
227 uint8_t hdr[HDR_VWV];
230 * For normal requests, smb1cli_req_send chooses a mid.
231 * SecondaryV trans requests need to use the mid of the primary
232 * request, so we need a place to store it.
233 * Assume it is set if != 0.
238 uint8_t bytecount_buf[2];
240 #define MAX_SMB_IOV 10
241 /* length_hdr, hdr, words, byte_count, buffers */
242 struct iovec iov[1 + 3 + MAX_SMB_IOV];
247 struct tevent_req **chained_requests;
250 NTSTATUS recv_status;
251 /* always an array of 3 talloc elements */
252 struct iovec *recv_iov;
256 const uint8_t *fixed;
261 uint8_t transform[SMB2_TF_HDR_SIZE];
262 uint8_t hdr[SMB2_HDR_BODY];
263 uint8_t pad[7]; /* padding space for compounding */
266 * always an array of 3 talloc elements
267 * (without a SMB2_TRANSFORM header!)
271 struct iovec *recv_iov;
274 * the expected max for the response dyn_len
276 uint32_t max_dyn_len;
278 uint16_t credit_charge;
282 uint64_t encryption_session_id;
284 bool signing_skipped;
287 uint16_t cancel_flags;
293 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
296 * NT_STATUS_OK, means we do not notify the callers
298 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
300 while (conn->sessions) {
301 conn->sessions->conn = NULL;
302 DLIST_REMOVE(conn->sessions, conn->sessions);
305 if (conn->smb1.trans_enc) {
306 TALLOC_FREE(conn->smb1.trans_enc);
312 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
314 const char *remote_name,
315 enum smb_signing_setting signing_state,
316 uint32_t smb1_capabilities,
317 struct GUID *client_guid,
318 uint32_t smb2_capabilities)
320 struct smbXcli_conn *conn = NULL;
322 struct sockaddr *sa = NULL;
326 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
332 conn->write_fd = dup(fd);
333 if (conn->write_fd == -1) {
337 conn->remote_name = talloc_strdup(conn, remote_name);
338 if (conn->remote_name == NULL) {
343 ss = (void *)&conn->local_ss;
344 sa = (struct sockaddr *)ss;
345 sa_length = sizeof(conn->local_ss);
346 ret = getsockname(fd, sa, &sa_length);
350 ss = (void *)&conn->remote_ss;
351 sa = (struct sockaddr *)ss;
352 sa_length = sizeof(conn->remote_ss);
353 ret = getpeername(fd, sa, &sa_length);
358 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
359 if (conn->outgoing == NULL) {
362 conn->pending = NULL;
364 conn->min_protocol = PROTOCOL_NONE;
365 conn->max_protocol = PROTOCOL_NONE;
366 conn->protocol = PROTOCOL_NONE;
368 switch (signing_state) {
369 case SMB_SIGNING_OFF:
371 conn->allow_signing = false;
372 conn->desire_signing = false;
373 conn->mandatory_signing = false;
375 case SMB_SIGNING_DEFAULT:
376 case SMB_SIGNING_IF_REQUIRED:
377 /* if the server requires it */
378 conn->allow_signing = true;
379 conn->desire_signing = false;
380 conn->mandatory_signing = false;
382 case SMB_SIGNING_REQUIRED:
384 conn->allow_signing = true;
385 conn->desire_signing = true;
386 conn->mandatory_signing = true;
390 conn->smb1.client.capabilities = smb1_capabilities;
391 conn->smb1.client.max_xmit = UINT16_MAX;
393 conn->smb1.capabilities = conn->smb1.client.capabilities;
394 conn->smb1.max_xmit = 1024;
398 /* initialise signing */
399 conn->smb1.signing = smb_signing_init(conn,
401 conn->desire_signing,
402 conn->mandatory_signing);
403 if (!conn->smb1.signing) {
407 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
408 if (conn->mandatory_signing) {
409 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
412 conn->smb2.client.guid = *client_guid;
414 conn->smb2.client.capabilities = smb2_capabilities;
416 conn->smb2.cur_credits = 1;
417 conn->smb2.max_credits = 0;
418 conn->smb2.io_priority = 1;
421 * Samba and Windows servers accept a maximum of 16 MiB with a maximum
422 * chunk length of 1 MiB.
424 conn->smb2.cc_chunk_len = 1024 * 1024;
425 conn->smb2.cc_max_chunks = 16;
427 talloc_set_destructor(conn, smbXcli_conn_destructor);
431 if (conn->write_fd != -1) {
432 close(conn->write_fd);
438 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
444 if (conn->read_fd == -1) {
451 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
453 return conn->protocol;
456 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
458 if (conn->protocol >= PROTOCOL_SMB2_02) {
462 if (conn->smb1.capabilities & CAP_UNICODE) {
469 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
471 set_socket_options(conn->read_fd, options);
474 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
476 return &conn->local_ss;
479 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
481 return &conn->remote_ss;
484 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
486 return conn->remote_name;
489 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
491 if (conn->protocol >= PROTOCOL_SMB2_02) {
498 return conn->smb1.server.max_mux;
501 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
503 if (conn->protocol >= PROTOCOL_SMB2_02) {
504 return conn->smb2.server.system_time;
507 return conn->smb1.server.system_time;
510 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
512 if (conn->protocol >= PROTOCOL_SMB2_02) {
513 return &conn->smb2.server.gss_blob;
516 return &conn->smb1.server.gss_blob;
519 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
521 if (conn->protocol >= PROTOCOL_SMB2_02) {
522 return &conn->smb2.server.guid;
525 return &conn->smb1.server.guid;
528 struct smbXcli_conn_samba_suicide_state {
529 struct smbXcli_conn *conn;
534 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
536 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
537 struct tevent_context *ev,
538 struct smbXcli_conn *conn,
541 struct tevent_req *req, *subreq;
542 struct smbXcli_conn_samba_suicide_state *state;
544 req = tevent_req_create(mem_ctx, &state,
545 struct smbXcli_conn_samba_suicide_state);
550 SIVAL(state->buf, 4, 0x74697865);
551 SCVAL(state->buf, 8, exitcode);
552 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
554 state->iov.iov_base = state->buf;
555 state->iov.iov_len = sizeof(state->buf);
557 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
558 false, &state->iov, 1);
559 if (tevent_req_nomem(subreq, req)) {
560 return tevent_req_post(req, ev);
562 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
566 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
568 struct tevent_req *req = tevent_req_callback_data(
569 subreq, struct tevent_req);
570 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
571 req, struct smbXcli_conn_samba_suicide_state);
575 nwritten = writev_recv(subreq, &err);
577 if (nwritten == -1) {
578 NTSTATUS status = map_nt_error_from_unix_common(err);
579 smbXcli_conn_disconnect(state->conn, status);
582 tevent_req_done(req);
585 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
587 return tevent_req_simple_recv_ntstatus(req);
590 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
593 TALLOC_CTX *frame = talloc_stackframe();
594 struct tevent_context *ev;
595 struct tevent_req *req;
596 NTSTATUS status = NT_STATUS_NO_MEMORY;
599 if (smbXcli_conn_has_async_calls(conn)) {
601 * Can't use sync call while an async call is in flight
603 status = NT_STATUS_INVALID_PARAMETER_MIX;
606 ev = samba_tevent_context_init(frame);
610 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
614 ok = tevent_req_poll_ntstatus(req, ev, &status);
618 status = smbXcli_conn_samba_suicide_recv(req);
624 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
626 return conn->smb1.capabilities;
629 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
631 return conn->smb1.max_xmit;
634 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
637 uint16_t possible = conn->smb1.server.max_mux;
639 pending = tevent_queue_length(conn->outgoing);
640 if (pending >= possible) {
643 pending += talloc_array_length(conn->pending);
644 if (pending >= possible) {
651 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
653 return conn->smb1.server.session_key;
656 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
658 return conn->smb1.server.challenge;
661 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
663 return conn->smb1.server.security_mode;
666 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
668 return conn->smb1.server.readbraw;
671 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
673 return conn->smb1.server.writebraw;
676 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
678 return conn->smb1.server.lockread;
681 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
683 return conn->smb1.server.writeunlock;
686 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
688 return conn->smb1.server.time_zone;
691 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
692 const DATA_BLOB user_session_key,
693 const DATA_BLOB response)
695 return smb_signing_activate(conn->smb1.signing,
700 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
701 const uint8_t *buf, uint32_t seqnum)
703 const uint8_t *hdr = buf + NBT_HDR_SIZE;
704 size_t len = smb_len_nbt(buf);
706 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
709 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
711 return smb_signing_is_active(conn->smb1.signing);
714 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
715 struct smb_trans_enc_state *es)
717 /* Replace the old state, if any. */
718 if (conn->smb1.trans_enc) {
719 TALLOC_FREE(conn->smb1.trans_enc);
721 conn->smb1.trans_enc = es;
724 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
726 return common_encryption_on(conn->smb1.trans_enc);
730 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
732 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
733 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
735 if (NT_STATUS_IS_OK(status)) {
739 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
743 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
747 * Is the SMB command able to hold an AND_X successor
748 * @param[in] cmd The SMB command in question
749 * @retval Can we add a chained request after "cmd"?
751 bool smb1cli_is_andx_req(uint8_t cmd)
771 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
773 size_t num_pending = talloc_array_length(conn->pending);
776 if (conn->protocol == PROTOCOL_NONE) {
778 * This is what windows sends on the SMB1 Negprot request
779 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
787 result = conn->smb1.mid++;
788 if ((result == 0) || (result == 0xffff)) {
792 for (i=0; i<num_pending; i++) {
793 if (result == smb1cli_req_mid(conn->pending[i])) {
798 if (i == num_pending) {
804 void smbXcli_req_unset_pending(struct tevent_req *req)
806 struct smbXcli_req_state *state =
808 struct smbXcli_req_state);
809 struct smbXcli_conn *conn = state->conn;
810 size_t num_pending = talloc_array_length(conn->pending);
813 if (state->smb1.mid != 0) {
815 * This is a [nt]trans[2] request which waits
816 * for more than one reply.
821 tevent_req_set_cleanup_fn(req, NULL);
823 if (num_pending == 1) {
825 * The pending read_smb tevent_req is a child of
826 * conn->pending. So if nothing is pending anymore, we need to
827 * delete the socket read fde.
829 TALLOC_FREE(conn->pending);
830 conn->read_smb_req = NULL;
834 for (i=0; i<num_pending; i++) {
835 if (req == conn->pending[i]) {
839 if (i == num_pending) {
841 * Something's seriously broken. Just returning here is the
842 * right thing nevertheless, the point of this routine is to
843 * remove ourselves from conn->pending.
849 * Remove ourselves from the conn->pending array
851 for (; i < (num_pending - 1); i++) {
852 conn->pending[i] = conn->pending[i+1];
856 * No NULL check here, we're shrinking by sizeof(void *), and
857 * talloc_realloc just adjusts the size for this.
859 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
864 static void smbXcli_req_cleanup(struct tevent_req *req,
865 enum tevent_req_state req_state)
867 struct smbXcli_req_state *state =
869 struct smbXcli_req_state);
872 case TEVENT_REQ_RECEIVED:
874 * Make sure we really remove it from
875 * the pending array on destruction.
878 smbXcli_req_unset_pending(req);
885 static bool smb1cli_req_cancel(struct tevent_req *req);
886 static bool smb2cli_req_cancel(struct tevent_req *req);
888 static bool smbXcli_req_cancel(struct tevent_req *req)
890 struct smbXcli_req_state *state =
892 struct smbXcli_req_state);
894 if (!smbXcli_conn_is_connected(state->conn)) {
898 if (state->conn->protocol == PROTOCOL_NONE) {
902 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
903 return smb2cli_req_cancel(req);
906 return smb1cli_req_cancel(req);
909 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
911 bool smbXcli_req_set_pending(struct tevent_req *req)
913 struct smbXcli_req_state *state =
915 struct smbXcli_req_state);
916 struct smbXcli_conn *conn;
917 struct tevent_req **pending;
922 if (!smbXcli_conn_is_connected(conn)) {
926 num_pending = talloc_array_length(conn->pending);
928 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
930 if (pending == NULL) {
933 pending[num_pending] = req;
934 conn->pending = pending;
935 tevent_req_set_cleanup_fn(req, smbXcli_req_cleanup);
936 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
938 if (!smbXcli_conn_receive_next(conn)) {
940 * the caller should notify the current request
942 * And all other pending requests get notified
943 * by smbXcli_conn_disconnect().
945 smbXcli_req_unset_pending(req);
946 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
953 static void smbXcli_conn_received(struct tevent_req *subreq);
955 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
957 size_t num_pending = talloc_array_length(conn->pending);
958 struct tevent_req *req;
959 struct smbXcli_req_state *state;
961 if (conn->read_smb_req != NULL) {
965 if (num_pending == 0) {
966 if (conn->smb2.mid < UINT64_MAX) {
967 /* no more pending requests, so we are done for now */
972 * If there are no more SMB2 requests possible,
973 * because we are out of message ids,
974 * we need to disconnect.
976 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
980 req = conn->pending[0];
981 state = tevent_req_data(req, struct smbXcli_req_state);
984 * We're the first ones, add the read_smb request that waits for the
985 * answer from the server
987 conn->read_smb_req = read_smb_send(conn->pending,
990 if (conn->read_smb_req == NULL) {
993 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
997 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
999 struct smbXcli_session *session;
1000 int read_fd = conn->read_fd;
1001 int write_fd = conn->write_fd;
1003 tevent_queue_stop(conn->outgoing);
1006 conn->write_fd = -1;
1008 session = conn->sessions;
1009 if (talloc_array_length(conn->pending) == 0) {
1011 * if we do not have pending requests
1012 * there is no need to update the channel_sequence
1016 for (; session; session = session->next) {
1017 smb2cli_session_increment_channel_sequence(session);
1021 * Cancel all pending requests. We do not do a for-loop walking
1022 * conn->pending because that array changes in
1023 * smbXcli_req_unset_pending.
1025 while (talloc_array_length(conn->pending) > 0) {
1026 struct tevent_req *req;
1027 struct smbXcli_req_state *state;
1028 struct tevent_req **chain;
1032 req = conn->pending[0];
1033 state = tevent_req_data(req, struct smbXcli_req_state);
1035 if (state->smb1.chained_requests == NULL) {
1037 * We're dead. No point waiting for trans2
1040 state->smb1.mid = 0;
1042 smbXcli_req_unset_pending(req);
1044 if (NT_STATUS_IS_OK(status)) {
1045 /* do not notify the callers */
1050 * we need to defer the callback, because we may notify
1051 * more then one caller.
1053 tevent_req_defer_callback(req, state->ev);
1054 tevent_req_nterror(req, status);
1058 chain = talloc_move(conn, &state->smb1.chained_requests);
1059 num_chained = talloc_array_length(chain);
1061 for (i=0; i<num_chained; i++) {
1063 state = tevent_req_data(req, struct smbXcli_req_state);
1066 * We're dead. No point waiting for trans2
1069 state->smb1.mid = 0;
1071 smbXcli_req_unset_pending(req);
1073 if (NT_STATUS_IS_OK(status)) {
1074 /* do not notify the callers */
1079 * we need to defer the callback, because we may notify
1080 * more than one caller.
1082 tevent_req_defer_callback(req, state->ev);
1083 tevent_req_nterror(req, status);
1088 if (read_fd != -1) {
1091 if (write_fd != -1) {
1097 * Fetch a smb request's mid. Only valid after the request has been sent by
1098 * smb1cli_req_send().
1100 uint16_t smb1cli_req_mid(struct tevent_req *req)
1102 struct smbXcli_req_state *state =
1103 tevent_req_data(req,
1104 struct smbXcli_req_state);
1106 if (state->smb1.mid != 0) {
1107 return state->smb1.mid;
1110 return SVAL(state->smb1.hdr, HDR_MID);
1113 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1115 struct smbXcli_req_state *state =
1116 tevent_req_data(req,
1117 struct smbXcli_req_state);
1119 state->smb1.mid = mid;
1122 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1124 struct smbXcli_req_state *state =
1125 tevent_req_data(req,
1126 struct smbXcli_req_state);
1128 return state->smb1.seqnum;
1131 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1133 struct smbXcli_req_state *state =
1134 tevent_req_data(req,
1135 struct smbXcli_req_state);
1137 state->smb1.seqnum = seqnum;
1140 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1142 ssize_t ret = iov_buflen(iov, count);
1144 /* Ignore the overflow case for now ... */
1148 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1149 const struct iovec *iov,
1155 buflen = iov_buflen(iov, count);
1160 buf = talloc_array(mem_ctx, uint8_t, buflen);
1165 iov_buf(iov, count, buf, buflen);
1170 static void smb1cli_req_flags(enum protocol_types protocol,
1171 uint32_t smb1_capabilities,
1172 uint8_t smb_command,
1173 uint8_t additional_flags,
1174 uint8_t clear_flags,
1176 uint16_t additional_flags2,
1177 uint16_t clear_flags2,
1181 uint16_t flags2 = 0;
1183 if (protocol >= PROTOCOL_LANMAN1) {
1184 flags |= FLAG_CASELESS_PATHNAMES;
1185 flags |= FLAG_CANONICAL_PATHNAMES;
1188 if (protocol >= PROTOCOL_LANMAN2) {
1189 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1190 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1193 if (protocol >= PROTOCOL_NT1) {
1194 flags2 |= FLAGS2_IS_LONG_NAME;
1196 if (smb1_capabilities & CAP_UNICODE) {
1197 flags2 |= FLAGS2_UNICODE_STRINGS;
1199 if (smb1_capabilities & CAP_STATUS32) {
1200 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1202 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1203 flags2 |= FLAGS2_EXTENDED_SECURITY;
1207 flags |= additional_flags;
1208 flags &= ~clear_flags;
1209 flags2 |= additional_flags2;
1210 flags2 &= ~clear_flags2;
1216 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1218 static bool smb1cli_req_cancel(struct tevent_req *req)
1220 struct smbXcli_req_state *state =
1221 tevent_req_data(req,
1222 struct smbXcli_req_state);
1227 struct tevent_req *subreq;
1230 flags = CVAL(state->smb1.hdr, HDR_FLG);
1231 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1232 pid = SVAL(state->smb1.hdr, HDR_PID);
1233 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1234 mid = SVAL(state->smb1.hdr, HDR_MID);
1236 subreq = smb1cli_req_create(state, state->ev,
1246 0, NULL); /* bytes */
1247 if (subreq == NULL) {
1250 smb1cli_req_set_mid(subreq, mid);
1252 status = smb1cli_req_chain_submit(&subreq, 1);
1253 if (!NT_STATUS_IS_OK(status)) {
1254 TALLOC_FREE(subreq);
1257 smb1cli_req_set_mid(subreq, 0);
1259 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1264 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1266 /* we do not care about the result */
1267 TALLOC_FREE(subreq);
1270 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1271 struct tevent_context *ev,
1272 struct smbXcli_conn *conn,
1273 uint8_t smb_command,
1274 uint8_t additional_flags,
1275 uint8_t clear_flags,
1276 uint16_t additional_flags2,
1277 uint16_t clear_flags2,
1278 uint32_t timeout_msec,
1280 struct smbXcli_tcon *tcon,
1281 struct smbXcli_session *session,
1282 uint8_t wct, uint16_t *vwv,
1284 struct iovec *bytes_iov)
1286 struct tevent_req *req;
1287 struct smbXcli_req_state *state;
1289 uint16_t flags2 = 0;
1294 if (iov_count > MAX_SMB_IOV) {
1296 * Should not happen :-)
1301 req = tevent_req_create(mem_ctx, &state,
1302 struct smbXcli_req_state);
1308 state->session = session;
1312 uid = session->smb1.session_id;
1316 tid = tcon->smb1.tcon_id;
1318 if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
1319 clear_flags |= FLAG_CASELESS_PATHNAMES;
1321 /* Default setting, case insensitive. */
1322 additional_flags |= FLAG_CASELESS_PATHNAMES;
1325 if (smbXcli_conn_dfs_supported(conn) &&
1326 smbXcli_tcon_is_dfs_share(tcon))
1328 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
1332 state->smb1.recv_cmd = 0xFF;
1333 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1334 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1335 if (state->smb1.recv_iov == NULL) {
1340 smb1cli_req_flags(conn->protocol,
1341 conn->smb1.capabilities,
1350 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1351 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1352 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1353 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1354 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1355 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1356 SSVAL(state->smb1.hdr, HDR_TID, tid);
1357 SSVAL(state->smb1.hdr, HDR_PID, pid);
1358 SSVAL(state->smb1.hdr, HDR_UID, uid);
1359 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1360 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1362 state->smb1.vwv = vwv;
1364 num_bytes = iov_buflen(bytes_iov, iov_count);
1365 if (num_bytes == -1) {
1367 * I'd love to add a check for num_bytes<=UINT16_MAX here, but
1368 * the smbclient->samba connections can lie and transfer more.
1374 SSVAL(state->smb1.bytecount_buf, 0, num_bytes);
1376 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1377 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1378 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1379 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1380 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1381 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1382 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1383 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1385 if (iov_count != 0) {
1386 memcpy(&state->smb1.iov[4], bytes_iov,
1387 iov_count * sizeof(*bytes_iov));
1389 state->smb1.iov_count = iov_count + 4;
1391 if (timeout_msec > 0) {
1392 struct timeval endtime;
1394 endtime = timeval_current_ofs_msec(timeout_msec);
1395 if (!tevent_req_set_endtime(req, ev, endtime)) {
1400 switch (smb_command) {
1404 state->one_way = true;
1407 state->one_way = true;
1408 state->smb1.one_way_seqnum = true;
1412 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1413 state->one_way = true;
1421 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1422 struct iovec *iov, int iov_count,
1424 bool one_way_seqnum)
1426 TALLOC_CTX *frame = NULL;
1430 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1431 * iovec directly. MD5Update would do that just fine.
1434 if (iov_count < 4) {
1435 return NT_STATUS_INVALID_PARAMETER_MIX;
1437 if (iov[0].iov_len != NBT_HDR_SIZE) {
1438 return NT_STATUS_INVALID_PARAMETER_MIX;
1440 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1441 return NT_STATUS_INVALID_PARAMETER_MIX;
1443 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1444 return NT_STATUS_INVALID_PARAMETER_MIX;
1446 if (iov[3].iov_len != sizeof(uint16_t)) {
1447 return NT_STATUS_INVALID_PARAMETER_MIX;
1450 frame = talloc_stackframe();
1452 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1454 return NT_STATUS_NO_MEMORY;
1457 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1459 smb_signing_sign_pdu(conn->smb1.signing,
1460 buf, talloc_get_size(buf),
1462 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1465 return NT_STATUS_OK;
1468 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1469 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1470 TALLOC_CTX *tmp_mem,
1473 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1474 struct smbXcli_req_state *state,
1475 struct iovec *iov, int iov_count)
1477 struct tevent_req *subreq;
1483 if (!smbXcli_conn_is_connected(state->conn)) {
1484 return NT_STATUS_CONNECTION_DISCONNECTED;
1487 if (state->conn->protocol > PROTOCOL_NT1) {
1488 return NT_STATUS_REVISION_MISMATCH;
1491 if (iov_count < 4) {
1492 return NT_STATUS_INVALID_PARAMETER_MIX;
1494 if (iov[0].iov_len != NBT_HDR_SIZE) {
1495 return NT_STATUS_INVALID_PARAMETER_MIX;
1497 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1498 return NT_STATUS_INVALID_PARAMETER_MIX;
1500 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1501 return NT_STATUS_INVALID_PARAMETER_MIX;
1503 if (iov[3].iov_len != sizeof(uint16_t)) {
1504 return NT_STATUS_INVALID_PARAMETER_MIX;
1507 cmd = CVAL(iov[1].iov_base, HDR_COM);
1508 if (cmd == SMBreadBraw) {
1509 if (smbXcli_conn_has_async_calls(state->conn)) {
1510 return NT_STATUS_INVALID_PARAMETER_MIX;
1512 state->conn->smb1.read_braw_req = req;
1515 if (state->smb1.mid != 0) {
1516 mid = state->smb1.mid;
1518 mid = smb1cli_alloc_mid(state->conn);
1520 SSVAL(iov[1].iov_base, HDR_MID, mid);
1522 nbtlen = iov_buflen(&iov[1], iov_count-1);
1523 if ((nbtlen == -1) || (nbtlen > 0x1FFFF)) {
1524 return NT_STATUS_INVALID_PARAMETER_MIX;
1527 _smb_setlen_nbt(iov[0].iov_base, nbtlen);
1529 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1530 &state->smb1.seqnum,
1531 state->smb1.one_way_seqnum);
1533 if (!NT_STATUS_IS_OK(status)) {
1538 * If we supported multiple encrytion contexts
1539 * here we'd look up based on tid.
1541 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1542 char *buf, *enc_buf;
1544 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1546 return NT_STATUS_NO_MEMORY;
1548 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1549 (char *)buf, &enc_buf);
1551 if (!NT_STATUS_IS_OK(status)) {
1552 DEBUG(0, ("Error in encrypting client message: %s\n",
1553 nt_errstr(status)));
1556 buf = (char *)talloc_memdup(state, enc_buf,
1557 smb_len_nbt(enc_buf)+4);
1560 return NT_STATUS_NO_MEMORY;
1562 iov[0].iov_base = (void *)buf;
1563 iov[0].iov_len = talloc_get_size(buf);
1567 if (state->conn->dispatch_incoming == NULL) {
1568 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1571 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1573 subreq = writev_send(state, state->ev, state->conn->outgoing,
1574 state->conn->write_fd, false, iov, iov_count);
1575 if (subreq == NULL) {
1576 return NT_STATUS_NO_MEMORY;
1578 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1579 return NT_STATUS_OK;
1582 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1583 struct tevent_context *ev,
1584 struct smbXcli_conn *conn,
1585 uint8_t smb_command,
1586 uint8_t additional_flags,
1587 uint8_t clear_flags,
1588 uint16_t additional_flags2,
1589 uint16_t clear_flags2,
1590 uint32_t timeout_msec,
1592 struct smbXcli_tcon *tcon,
1593 struct smbXcli_session *session,
1594 uint8_t wct, uint16_t *vwv,
1596 const uint8_t *bytes)
1598 struct tevent_req *req;
1602 iov.iov_base = discard_const_p(void, bytes);
1603 iov.iov_len = num_bytes;
1605 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1606 additional_flags, clear_flags,
1607 additional_flags2, clear_flags2,
1614 if (!tevent_req_is_in_progress(req)) {
1615 return tevent_req_post(req, ev);
1617 status = smb1cli_req_chain_submit(&req, 1);
1618 if (tevent_req_nterror(req, status)) {
1619 return tevent_req_post(req, ev);
1624 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1626 struct tevent_req *req =
1627 tevent_req_callback_data(subreq,
1629 struct smbXcli_req_state *state =
1630 tevent_req_data(req,
1631 struct smbXcli_req_state);
1635 nwritten = writev_recv(subreq, &err);
1636 TALLOC_FREE(subreq);
1637 if (nwritten == -1) {
1638 NTSTATUS status = map_nt_error_from_unix_common(err);
1639 smbXcli_conn_disconnect(state->conn, status);
1640 tevent_req_nterror(req, status);
1644 if (state->one_way) {
1645 state->inbuf = NULL;
1646 tevent_req_done(req);
1650 if (!smbXcli_req_set_pending(req)) {
1651 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1656 static void smbXcli_conn_received(struct tevent_req *subreq)
1658 struct smbXcli_conn *conn =
1659 tevent_req_callback_data(subreq,
1660 struct smbXcli_conn);
1661 TALLOC_CTX *frame = talloc_stackframe();
1667 if (subreq != conn->read_smb_req) {
1668 DEBUG(1, ("Internal error: cli_smb_received called with "
1669 "unexpected subreq\n"));
1670 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1674 conn->read_smb_req = NULL;
1676 received = read_smb_recv(subreq, frame, &inbuf, &err);
1677 TALLOC_FREE(subreq);
1678 if (received == -1) {
1679 status = map_nt_error_from_unix_common(err);
1680 smbXcli_conn_disconnect(conn, status);
1685 status = conn->dispatch_incoming(conn, frame, inbuf);
1687 if (NT_STATUS_IS_OK(status)) {
1689 * We should not do any more processing
1690 * as the dispatch function called
1691 * tevent_req_done().
1696 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1698 * We got an error, so notify all pending requests
1700 smbXcli_conn_disconnect(conn, status);
1705 * We got NT_STATUS_RETRY, so we may ask for a
1706 * next incoming pdu.
1708 if (!smbXcli_conn_receive_next(conn)) {
1709 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1713 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1714 struct iovec **piov, int *pnum_iov)
1725 size_t min_size = MIN_SMB_SIZE;
1727 buflen = smb_len_tcp(buf);
1730 hdr = buf + NBT_HDR_SIZE;
1732 status = smb1cli_pull_raw_error(hdr);
1733 if (NT_STATUS_IS_ERR(status)) {
1735 * This is an ugly hack to support OS/2
1736 * which skips the byte_count in the DATA block
1737 * on some error responses.
1741 min_size -= sizeof(uint16_t);
1744 if (buflen < min_size) {
1745 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1749 * This returns iovec elements in the following order:
1764 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1766 return NT_STATUS_NO_MEMORY;
1768 iov[0].iov_base = hdr;
1769 iov[0].iov_len = HDR_WCT;
1772 cmd = CVAL(hdr, HDR_COM);
1776 size_t len = buflen - taken;
1778 struct iovec *iov_tmp;
1785 * we need at least WCT
1787 needed = sizeof(uint8_t);
1789 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1790 __location__, (int)len, (int)needed));
1795 * Now we check if the specified words are there
1797 wct = CVAL(hdr, wct_ofs);
1798 needed += wct * sizeof(uint16_t);
1800 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1801 __location__, (int)len, (int)needed));
1805 if ((num_iov == 1) &&
1807 NT_STATUS_IS_ERR(status))
1810 * This is an ugly hack to support OS/2
1811 * which skips the byte_count in the DATA block
1812 * on some error responses.
1816 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1818 if (iov_tmp == NULL) {
1820 return NT_STATUS_NO_MEMORY;
1823 cur = &iov[num_iov];
1827 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1829 cur[1].iov_base = cur[0].iov_base;
1836 * we need at least BCC
1838 needed += sizeof(uint16_t);
1840 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1841 __location__, (int)len, (int)needed));
1846 * Now we check if the specified bytes are there
1848 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1849 bcc = SVAL(hdr, bcc_ofs);
1850 needed += bcc * sizeof(uint8_t);
1852 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1853 __location__, (int)len, (int)needed));
1858 * we allocate 2 iovec structures for words and bytes
1860 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1862 if (iov_tmp == NULL) {
1864 return NT_STATUS_NO_MEMORY;
1867 cur = &iov[num_iov];
1870 cur[0].iov_len = wct * sizeof(uint16_t);
1871 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1872 cur[1].iov_len = bcc * sizeof(uint8_t);
1873 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1877 if (!smb1cli_is_andx_req(cmd)) {
1879 * If the current command does not have AndX chanining
1885 if (wct == 0 && bcc == 0) {
1887 * An empty response also ends the chain,
1888 * most likely with an error.
1894 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1895 __location__, (int)wct, (int)cmd));
1898 cmd = CVAL(cur[0].iov_base, 0);
1901 * If it is the end of the chain we are also done.
1905 wct_ofs = SVAL(cur[0].iov_base, 2);
1907 if (wct_ofs < taken) {
1908 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1910 if (wct_ofs > buflen) {
1911 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1915 * we consumed everything up to the start of the next
1921 remaining = buflen - taken;
1923 if (remaining > 0 && num_iov >= 3) {
1925 * The last DATA block gets the remaining
1926 * bytes, this is needed to support
1927 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1929 iov[num_iov-1].iov_len += remaining;
1933 *pnum_iov = num_iov;
1934 return NT_STATUS_OK;
1938 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1941 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1942 TALLOC_CTX *tmp_mem,
1945 struct tevent_req *req;
1946 struct smbXcli_req_state *state;
1953 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1954 size_t len = smb_len_tcp(inbuf);
1955 struct iovec *iov = NULL;
1957 struct tevent_req **chain = NULL;
1958 size_t num_chained = 0;
1959 size_t num_responses = 0;
1961 if (conn->smb1.read_braw_req != NULL) {
1962 req = conn->smb1.read_braw_req;
1963 conn->smb1.read_braw_req = NULL;
1964 state = tevent_req_data(req, struct smbXcli_req_state);
1966 smbXcli_req_unset_pending(req);
1968 if (state->smb1.recv_iov == NULL) {
1970 * For requests with more than
1971 * one response, we have to readd the
1974 state->smb1.recv_iov = talloc_zero_array(state,
1977 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1978 return NT_STATUS_OK;
1982 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1983 state->smb1.recv_iov[0].iov_len = len;
1984 ZERO_STRUCT(state->smb1.recv_iov[1]);
1985 ZERO_STRUCT(state->smb1.recv_iov[2]);
1987 state->smb1.recv_cmd = SMBreadBraw;
1988 state->smb1.recv_status = NT_STATUS_OK;
1989 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1991 tevent_req_done(req);
1992 return NT_STATUS_OK;
1995 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1996 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1997 DEBUG(10, ("Got non-SMB PDU\n"));
1998 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2002 * If we supported multiple encrytion contexts
2003 * here we'd look up based on tid.
2005 if (common_encryption_on(conn->smb1.trans_enc)
2006 && (CVAL(inbuf, 0) == 0)) {
2007 uint16_t enc_ctx_num;
2009 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
2010 if (!NT_STATUS_IS_OK(status)) {
2011 DEBUG(10, ("get_enc_ctx_num returned %s\n",
2012 nt_errstr(status)));
2016 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
2017 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
2019 conn->smb1.trans_enc->enc_ctx_num));
2020 return NT_STATUS_INVALID_HANDLE;
2023 status = common_decrypt_buffer(conn->smb1.trans_enc,
2025 if (!NT_STATUS_IS_OK(status)) {
2026 DEBUG(10, ("common_decrypt_buffer returned %s\n",
2027 nt_errstr(status)));
2030 inhdr = inbuf + NBT_HDR_SIZE;
2031 len = smb_len_nbt(inbuf);
2034 mid = SVAL(inhdr, HDR_MID);
2035 num_pending = talloc_array_length(conn->pending);
2037 for (i=0; i<num_pending; i++) {
2038 if (mid == smb1cli_req_mid(conn->pending[i])) {
2042 if (i == num_pending) {
2043 /* Dump unexpected reply */
2044 return NT_STATUS_RETRY;
2047 oplock_break = false;
2049 if (mid == 0xffff) {
2051 * Paranoia checks that this is really an oplock break request.
2053 oplock_break = (len == 51); /* hdr + 8 words */
2054 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
2055 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
2056 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
2057 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
2059 if (!oplock_break) {
2060 /* Dump unexpected reply */
2061 return NT_STATUS_RETRY;
2065 req = conn->pending[i];
2066 state = tevent_req_data(req, struct smbXcli_req_state);
2068 if (!oplock_break /* oplock breaks are not signed */
2069 && !smb_signing_check_pdu(conn->smb1.signing,
2070 inhdr, len, state->smb1.seqnum+1)) {
2071 DEBUG(10, ("cli_check_sign_mac failed\n"));
2072 return NT_STATUS_ACCESS_DENIED;
2075 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2077 if (!NT_STATUS_IS_OK(status)) {
2078 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2079 nt_errstr(status)));
2083 cmd = CVAL(inhdr, HDR_COM);
2084 status = smb1cli_pull_raw_error(inhdr);
2086 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2087 (state->session != NULL) && state->session->disconnect_expired)
2090 * this should be a short term hack
2091 * until the upper layers have implemented
2092 * re-authentication.
2097 if (state->smb1.chained_requests == NULL) {
2099 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2102 smbXcli_req_unset_pending(req);
2104 if (state->smb1.recv_iov == NULL) {
2106 * For requests with more than
2107 * one response, we have to readd the
2110 state->smb1.recv_iov = talloc_zero_array(state,
2113 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2114 return NT_STATUS_OK;
2118 state->smb1.recv_cmd = cmd;
2119 state->smb1.recv_status = status;
2120 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2122 state->smb1.recv_iov[0] = iov[0];
2123 state->smb1.recv_iov[1] = iov[1];
2124 state->smb1.recv_iov[2] = iov[2];
2126 if (talloc_array_length(conn->pending) == 0) {
2127 tevent_req_done(req);
2128 return NT_STATUS_OK;
2131 tevent_req_defer_callback(req, state->ev);
2132 tevent_req_done(req);
2133 return NT_STATUS_RETRY;
2136 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2137 num_chained = talloc_array_length(chain);
2138 num_responses = (num_iov - 1)/2;
2140 if (num_responses > num_chained) {
2141 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2144 for (i=0; i<num_chained; i++) {
2145 size_t iov_idx = 1 + (i*2);
2146 struct iovec *cur = &iov[iov_idx];
2150 state = tevent_req_data(req, struct smbXcli_req_state);
2152 smbXcli_req_unset_pending(req);
2155 * as we finish multiple requests here
2156 * we need to defer the callbacks as
2157 * they could destroy our current stack state.
2159 tevent_req_defer_callback(req, state->ev);
2161 if (i >= num_responses) {
2162 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2166 if (state->smb1.recv_iov == NULL) {
2168 * For requests with more than
2169 * one response, we have to readd the
2172 state->smb1.recv_iov = talloc_zero_array(state,
2175 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2180 state->smb1.recv_cmd = cmd;
2182 if (i == (num_responses - 1)) {
2184 * The last request in the chain gets the status
2186 state->smb1.recv_status = status;
2188 cmd = CVAL(cur[0].iov_base, 0);
2189 state->smb1.recv_status = NT_STATUS_OK;
2192 state->inbuf = inbuf;
2195 * Note: here we use talloc_reference() in a way
2196 * that does not expose it to the caller.
2198 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2199 if (tevent_req_nomem(inbuf_ref, req)) {
2203 /* copy the related buffers */
2204 state->smb1.recv_iov[0] = iov[0];
2205 state->smb1.recv_iov[1] = cur[0];
2206 state->smb1.recv_iov[2] = cur[1];
2208 tevent_req_done(req);
2211 return NT_STATUS_RETRY;
2214 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2215 TALLOC_CTX *mem_ctx,
2216 struct iovec **piov,
2220 uint32_t *pvwv_offset,
2221 uint32_t *pnum_bytes,
2223 uint32_t *pbytes_offset,
2225 const struct smb1cli_req_expected_response *expected,
2226 size_t num_expected)
2228 struct smbXcli_req_state *state =
2229 tevent_req_data(req,
2230 struct smbXcli_req_state);
2231 NTSTATUS status = NT_STATUS_OK;
2232 struct iovec *recv_iov = NULL;
2233 uint8_t *hdr = NULL;
2235 uint32_t vwv_offset = 0;
2236 uint16_t *vwv = NULL;
2237 uint32_t num_bytes = 0;
2238 uint32_t bytes_offset = 0;
2239 uint8_t *bytes = NULL;
2241 bool found_status = false;
2242 bool found_size = false;
2256 if (pvwv_offset != NULL) {
2259 if (pnum_bytes != NULL) {
2262 if (pbytes != NULL) {
2265 if (pbytes_offset != NULL) {
2268 if (pinbuf != NULL) {
2272 if (state->inbuf != NULL) {
2273 recv_iov = state->smb1.recv_iov;
2274 state->smb1.recv_iov = NULL;
2275 if (state->smb1.recv_cmd != SMBreadBraw) {
2276 hdr = (uint8_t *)recv_iov[0].iov_base;
2277 wct = recv_iov[1].iov_len/2;
2278 vwv = (uint16_t *)recv_iov[1].iov_base;
2279 vwv_offset = PTR_DIFF(vwv, hdr);
2280 num_bytes = recv_iov[2].iov_len;
2281 bytes = (uint8_t *)recv_iov[2].iov_base;
2282 bytes_offset = PTR_DIFF(bytes, hdr);
2286 if (tevent_req_is_nterror(req, &status)) {
2287 for (i=0; i < num_expected; i++) {
2288 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2289 found_status = true;
2295 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2301 if (num_expected == 0) {
2302 found_status = true;
2306 status = state->smb1.recv_status;
2308 for (i=0; i < num_expected; i++) {
2309 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2313 found_status = true;
2314 if (expected[i].wct == 0) {
2319 if (expected[i].wct == wct) {
2325 if (!found_status) {
2330 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2334 *piov = talloc_move(mem_ctx, &recv_iov);
2346 if (pvwv_offset != NULL) {
2347 *pvwv_offset = vwv_offset;
2349 if (pnum_bytes != NULL) {
2350 *pnum_bytes = num_bytes;
2352 if (pbytes != NULL) {
2355 if (pbytes_offset != NULL) {
2356 *pbytes_offset = bytes_offset;
2358 if (pinbuf != NULL) {
2359 *pinbuf = state->inbuf;
2365 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2372 for (i=0; i<num_reqs; i++) {
2373 struct smbXcli_req_state *state;
2374 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2375 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2376 state->smb1.iov_count-2);
2377 wct_ofs = (wct_ofs + 3) & ~3;
2382 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2384 struct smbXcli_req_state *first_state =
2385 tevent_req_data(reqs[0],
2386 struct smbXcli_req_state);
2387 struct smbXcli_req_state *state;
2389 size_t chain_padding = 0;
2391 struct iovec *iov = NULL;
2392 struct iovec *this_iov;
2396 if (num_reqs == 1) {
2397 return smb1cli_req_writev_submit(reqs[0], first_state,
2398 first_state->smb1.iov,
2399 first_state->smb1.iov_count);
2403 for (i=0; i<num_reqs; i++) {
2404 if (!tevent_req_is_in_progress(reqs[i])) {
2405 return NT_STATUS_INTERNAL_ERROR;
2408 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2410 if (state->smb1.iov_count < 4) {
2411 return NT_STATUS_INVALID_PARAMETER_MIX;
2416 * The NBT and SMB header
2429 iovlen += state->smb1.iov_count - 2;
2432 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2434 return NT_STATUS_NO_MEMORY;
2437 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2438 first_state, reqs, sizeof(*reqs) * num_reqs);
2439 if (first_state->smb1.chained_requests == NULL) {
2441 return NT_STATUS_NO_MEMORY;
2444 wct_offset = HDR_WCT;
2447 for (i=0; i<num_reqs; i++) {
2448 size_t next_padding = 0;
2451 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2453 if (i < num_reqs-1) {
2454 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2455 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2457 TALLOC_FREE(first_state->smb1.chained_requests);
2458 return NT_STATUS_INVALID_PARAMETER_MIX;
2462 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2463 state->smb1.iov_count-2) + 1;
2464 if ((wct_offset % 4) != 0) {
2465 next_padding = 4 - (wct_offset % 4);
2467 wct_offset += next_padding;
2468 vwv = state->smb1.vwv;
2470 if (i < num_reqs-1) {
2471 struct smbXcli_req_state *next_state =
2472 tevent_req_data(reqs[i+1],
2473 struct smbXcli_req_state);
2474 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2476 SSVAL(vwv+1, 0, wct_offset);
2477 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2478 /* properly end the chain */
2479 SCVAL(vwv+0, 0, 0xff);
2480 SCVAL(vwv+0, 1, 0xff);
2486 * The NBT and SMB header
2488 this_iov[0] = state->smb1.iov[0];
2489 this_iov[1] = state->smb1.iov[1];
2493 * This one is a bit subtle. We have to add
2494 * chain_padding bytes between the requests, and we
2495 * have to also include the wct field of the
2496 * subsequent requests. We use the subsequent header
2497 * for the padding, it contains the wct field in its
2500 this_iov[0].iov_len = chain_padding+1;
2501 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2502 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2503 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2508 * copy the words and bytes
2510 memcpy(this_iov, state->smb1.iov+2,
2511 sizeof(struct iovec) * (state->smb1.iov_count-2));
2512 this_iov += state->smb1.iov_count - 2;
2513 chain_padding = next_padding;
2516 nbt_len = iov_buflen(&iov[1], iovlen-1);
2517 if ((nbt_len == -1) || (nbt_len > first_state->conn->smb1.max_xmit)) {
2519 TALLOC_FREE(first_state->smb1.chained_requests);
2520 return NT_STATUS_INVALID_PARAMETER_MIX;
2523 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2524 if (!NT_STATUS_IS_OK(status)) {
2526 TALLOC_FREE(first_state->smb1.chained_requests);
2530 return NT_STATUS_OK;
2533 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2535 return ((tevent_queue_length(conn->outgoing) != 0)
2536 || (talloc_array_length(conn->pending) != 0));
2539 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2541 if (conn->protocol >= PROTOCOL_SMB2_02) {
2542 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2545 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2548 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2550 uint16_t credits = 1;
2552 if (conn->smb2.cur_credits == 0) {
2553 if (max_dyn_len != NULL) {
2559 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2560 credits = conn->smb2.cur_credits;
2563 if (max_dyn_len != NULL) {
2564 *max_dyn_len = credits * 65536;
2570 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2572 return conn->smb2.server.capabilities;
2575 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2577 return conn->smb2.server.security_mode;
2580 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2582 return conn->smb2.server.max_trans_size;
2585 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2587 return conn->smb2.server.max_read_size;
2590 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2592 return conn->smb2.server.max_write_size;
2595 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2596 uint16_t max_credits)
2598 conn->smb2.max_credits = max_credits;
2601 uint8_t smb2cli_conn_get_io_priority(struct smbXcli_conn *conn)
2603 if (conn->protocol < PROTOCOL_SMB3_11) {
2607 return conn->smb2.io_priority;
2610 void smb2cli_conn_set_io_priority(struct smbXcli_conn *conn,
2611 uint8_t io_priority)
2613 conn->smb2.io_priority = io_priority;
2616 uint32_t smb2cli_conn_cc_chunk_len(struct smbXcli_conn *conn)
2618 return conn->smb2.cc_chunk_len;
2621 void smb2cli_conn_set_cc_chunk_len(struct smbXcli_conn *conn,
2624 conn->smb2.cc_chunk_len = chunk_len;
2627 uint32_t smb2cli_conn_cc_max_chunks(struct smbXcli_conn *conn)
2629 return conn->smb2.cc_max_chunks;
2632 void smb2cli_conn_set_cc_max_chunks(struct smbXcli_conn *conn,
2633 uint32_t max_chunks)
2635 conn->smb2.cc_max_chunks = max_chunks;
2638 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2640 static bool smb2cli_req_cancel(struct tevent_req *req)
2642 struct smbXcli_req_state *state =
2643 tevent_req_data(req,
2644 struct smbXcli_req_state);
2645 struct smbXcli_tcon *tcon = state->tcon;
2646 struct smbXcli_session *session = state->session;
2647 uint8_t *fixed = state->smb2.pad;
2648 uint16_t fixed_len = 4;
2649 struct tevent_req *subreq;
2650 struct smbXcli_req_state *substate;
2653 SSVAL(fixed, 0, 0x04);
2656 subreq = smb2cli_req_create(state, state->ev,
2664 if (subreq == NULL) {
2667 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2669 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, state->smb2.cancel_flags);
2670 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, state->smb2.cancel_mid);
2671 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, state->smb2.cancel_aid);
2673 status = smb2cli_req_compound_submit(&subreq, 1);
2674 if (!NT_STATUS_IS_OK(status)) {
2675 TALLOC_FREE(subreq);
2679 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2684 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2686 /* we do not care about the result */
2687 TALLOC_FREE(subreq);
2690 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2691 struct tevent_context *ev,
2692 struct smbXcli_conn *conn,
2694 uint32_t additional_flags,
2695 uint32_t clear_flags,
2696 uint32_t timeout_msec,
2697 struct smbXcli_tcon *tcon,
2698 struct smbXcli_session *session,
2699 const uint8_t *fixed,
2703 uint32_t max_dyn_len)
2705 struct tevent_req *req;
2706 struct smbXcli_req_state *state;
2710 bool use_channel_sequence = false;
2711 uint16_t channel_sequence = 0;
2712 bool use_replay_flag = false;
2714 req = tevent_req_create(mem_ctx, &state,
2715 struct smbXcli_req_state);
2722 state->session = session;
2725 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2726 use_channel_sequence = true;
2727 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2728 use_channel_sequence = true;
2731 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_00) {
2732 use_replay_flag = true;
2735 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_11) {
2736 flags |= SMB2_PRIORITY_VALUE_TO_MASK(conn->smb2.io_priority);
2740 uid = session->smb2->session_id;
2742 if (use_channel_sequence) {
2743 channel_sequence = session->smb2->channel_sequence;
2746 if (use_replay_flag && session->smb2->replay_active) {
2747 additional_flags |= SMB2_HDR_FLAG_REPLAY_OPERATION;
2750 state->smb2.should_sign = session->smb2->should_sign;
2751 state->smb2.should_encrypt = session->smb2->should_encrypt;
2753 if (cmd == SMB2_OP_SESSSETUP &&
2754 session->smb2_channel.signing_key.length == 0 &&
2755 session->smb2->signing_key.length != 0)
2758 * a session bind needs to be signed
2760 state->smb2.should_sign = true;
2763 if (cmd == SMB2_OP_SESSSETUP &&
2764 session->smb2_channel.signing_key.length == 0) {
2765 state->smb2.should_encrypt = false;
2768 if (additional_flags & SMB2_HDR_FLAG_SIGNED) {
2769 if (session->smb2_channel.signing_key.length == 0) {
2770 tevent_req_nterror(req, NT_STATUS_NO_USER_SESSION_KEY);
2774 additional_flags &= ~SMB2_HDR_FLAG_SIGNED;
2775 state->smb2.should_sign = true;
2780 tid = tcon->smb2.tcon_id;
2782 if (tcon->smb2.should_sign) {
2783 state->smb2.should_sign = true;
2785 if (tcon->smb2.should_encrypt) {
2786 state->smb2.should_encrypt = true;
2790 if (state->smb2.should_encrypt) {
2791 state->smb2.should_sign = false;
2794 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2795 if (state->smb2.recv_iov == NULL) {
2800 flags |= additional_flags;
2801 flags &= ~clear_flags;
2803 state->smb2.fixed = fixed;
2804 state->smb2.fixed_len = fixed_len;
2805 state->smb2.dyn = dyn;
2806 state->smb2.dyn_len = dyn_len;
2807 state->smb2.max_dyn_len = max_dyn_len;
2809 if (state->smb2.should_encrypt) {
2810 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2811 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2814 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2815 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2816 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2817 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2818 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2819 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2820 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2821 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2824 case SMB2_OP_CANCEL:
2825 state->one_way = true;
2829 * If this is a dummy request, it will have
2830 * UINT64_MAX as message id.
2831 * If we send on break acknowledgement,
2832 * this gets overwritten later.
2834 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2838 if (timeout_msec > 0) {
2839 struct timeval endtime;
2841 endtime = timeval_current_ofs_msec(timeout_msec);
2842 if (!tevent_req_set_endtime(req, ev, endtime)) {
2850 void smb2cli_req_set_notify_async(struct tevent_req *req)
2852 struct smbXcli_req_state *state =
2853 tevent_req_data(req,
2854 struct smbXcli_req_state);
2856 state->smb2.notify_async = true;
2859 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2860 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2861 TALLOC_CTX *tmp_mem,
2864 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2867 struct smbXcli_req_state *state;
2868 struct tevent_req *subreq;
2870 int i, num_iov, nbt_len;
2872 const DATA_BLOB *encryption_key = NULL;
2873 uint64_t encryption_session_id = 0;
2874 uint64_t nonce_high = UINT64_MAX;
2875 uint64_t nonce_low = UINT64_MAX;
2878 * 1 for the nbt length, optional TRANSFORM
2879 * per request: HDR, fixed, dyn, padding
2880 * -1 because the last one does not need padding
2883 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2885 return NT_STATUS_NO_MEMORY;
2892 * the session of the first request that requires encryption
2893 * specifies the encryption key.
2895 for (i=0; i<num_reqs; i++) {
2896 if (!tevent_req_is_in_progress(reqs[i])) {
2897 return NT_STATUS_INTERNAL_ERROR;
2900 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2902 if (!smbXcli_conn_is_connected(state->conn)) {
2903 return NT_STATUS_CONNECTION_DISCONNECTED;
2906 if ((state->conn->protocol != PROTOCOL_NONE) &&
2907 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2908 return NT_STATUS_REVISION_MISMATCH;
2911 if (state->session == NULL) {
2915 if (!state->smb2.should_encrypt) {
2919 encryption_key = &state->session->smb2->encryption_key;
2920 if (encryption_key->length == 0) {
2921 return NT_STATUS_INVALID_PARAMETER_MIX;
2924 encryption_session_id = state->session->smb2->session_id;
2926 state->session->smb2->nonce_low += 1;
2927 if (state->session->smb2->nonce_low == 0) {
2928 state->session->smb2->nonce_high += 1;
2929 state->session->smb2->nonce_low += 1;
2933 * CCM and GCM algorithms must never have their
2934 * nonce wrap, or the security of the whole
2935 * communication and the keys is destroyed.
2936 * We must drop the connection once we have
2937 * transfered too much data.
2939 * NOTE: We assume nonces greater than 8 bytes.
2941 if (state->session->smb2->nonce_high >=
2942 state->session->smb2->nonce_high_max)
2944 return NT_STATUS_ENCRYPTION_FAILED;
2947 nonce_high = state->session->smb2->nonce_high_random;
2948 nonce_high += state->session->smb2->nonce_high;
2949 nonce_low = state->session->smb2->nonce_low;
2952 iov[num_iov].iov_base = state->smb2.transform;
2953 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2956 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2957 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2959 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2961 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2962 encryption_session_id);
2964 nbt_len += SMB2_TF_HDR_SIZE;
2968 for (i=0; i<num_reqs; i++) {
2977 const DATA_BLOB *signing_key = NULL;
2979 if (!tevent_req_is_in_progress(reqs[i])) {
2980 return NT_STATUS_INTERNAL_ERROR;
2983 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2985 if (!smbXcli_conn_is_connected(state->conn)) {
2986 return NT_STATUS_CONNECTION_DISCONNECTED;
2989 if ((state->conn->protocol != PROTOCOL_NONE) &&
2990 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2991 return NT_STATUS_REVISION_MISMATCH;
2994 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2995 if (opcode == SMB2_OP_CANCEL) {
2999 avail = UINT64_MAX - state->conn->smb2.mid;
3001 return NT_STATUS_CONNECTION_ABORTED;
3004 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
3005 uint32_t max_dyn_len = 1;
3007 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
3008 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
3010 charge = (max_dyn_len - 1)/ 65536 + 1;
3015 charge = MAX(state->smb2.credit_charge, charge);
3017 avail = MIN(avail, state->conn->smb2.cur_credits);
3018 if (avail < charge) {
3019 return NT_STATUS_INTERNAL_ERROR;
3023 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
3024 credits = state->conn->smb2.max_credits -
3025 state->conn->smb2.cur_credits;
3027 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
3031 mid = state->conn->smb2.mid;
3032 state->conn->smb2.mid += charge;
3033 state->conn->smb2.cur_credits -= charge;
3035 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
3036 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
3038 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
3039 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
3041 state->smb2.cancel_flags = 0;
3042 state->smb2.cancel_mid = mid;
3043 state->smb2.cancel_aid = 0;
3046 if (state->session && encryption_key == NULL) {
3048 * We prefer the channel signing key if it is
3051 if (state->smb2.should_sign) {
3052 signing_key = &state->session->smb2_channel.signing_key;
3056 * If it is a channel binding, we already have the main
3057 * signing key and try that one.
3059 if (signing_key && signing_key->length == 0) {
3060 signing_key = &state->session->smb2->signing_key;
3064 * If we do not have any session key yet, we skip the
3065 * signing of SMB2_OP_SESSSETUP requests.
3067 if (signing_key && signing_key->length == 0) {
3073 iov[num_iov].iov_base = state->smb2.hdr;
3074 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
3077 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
3078 iov[num_iov].iov_len = state->smb2.fixed_len;
3081 if (state->smb2.dyn != NULL) {
3082 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
3083 iov[num_iov].iov_len = state->smb2.dyn_len;
3087 reqlen = sizeof(state->smb2.hdr);
3088 reqlen += state->smb2.fixed_len;
3089 reqlen += state->smb2.dyn_len;
3091 if (i < num_reqs-1) {
3092 if ((reqlen % 8) > 0) {
3093 uint8_t pad = 8 - (reqlen % 8);
3094 iov[num_iov].iov_base = state->smb2.pad;
3095 iov[num_iov].iov_len = pad;
3099 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
3102 state->smb2.encryption_session_id = encryption_session_id;
3104 if (signing_key != NULL) {
3107 status = smb2_signing_sign_pdu(*signing_key,
3108 state->session->conn->protocol,
3109 &iov[hdr_iov], num_iov - hdr_iov);
3110 if (!NT_STATUS_IS_OK(status)) {
3117 ret = smbXcli_req_set_pending(reqs[i]);
3119 return NT_STATUS_NO_MEMORY;
3123 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
3124 _smb_setlen_tcp(state->length_hdr, nbt_len);
3125 iov[0].iov_base = state->length_hdr;
3126 iov[0].iov_len = sizeof(state->length_hdr);
3128 if (encryption_key != NULL) {
3130 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
3134 buf = talloc_array(iov, uint8_t, buflen);
3136 return NT_STATUS_NO_MEMORY;
3140 * We copy the buffers before encrypting them,
3141 * this is at least currently needed for the
3142 * to keep state->smb2.hdr.
3144 * Also the callers may expect there buffers
3147 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3148 struct iovec *v = &iov[vi];
3149 const uint8_t *o = (const uint8_t *)v->iov_base;
3151 memcpy(buf, o, v->iov_len);
3152 v->iov_base = (void *)buf;
3156 status = smb2_signing_encrypt_pdu(*encryption_key,
3157 state->conn->smb2.server.cipher,
3158 &iov[tf_iov], num_iov - tf_iov);
3159 if (!NT_STATUS_IS_OK(status)) {
3164 if (state->conn->dispatch_incoming == NULL) {
3165 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3168 subreq = writev_send(state, state->ev, state->conn->outgoing,
3169 state->conn->write_fd, false, iov, num_iov);
3170 if (subreq == NULL) {
3171 return NT_STATUS_NO_MEMORY;
3173 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3174 return NT_STATUS_OK;
3177 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3179 struct smbXcli_req_state *state =
3180 tevent_req_data(req,
3181 struct smbXcli_req_state);
3183 state->smb2.credit_charge = charge;
3186 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3187 struct tevent_context *ev,
3188 struct smbXcli_conn *conn,
3190 uint32_t additional_flags,
3191 uint32_t clear_flags,
3192 uint32_t timeout_msec,
3193 struct smbXcli_tcon *tcon,
3194 struct smbXcli_session *session,
3195 const uint8_t *fixed,
3199 uint32_t max_dyn_len)
3201 struct tevent_req *req;
3204 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3205 additional_flags, clear_flags,
3214 if (!tevent_req_is_in_progress(req)) {
3215 return tevent_req_post(req, ev);
3217 status = smb2cli_req_compound_submit(&req, 1);
3218 if (tevent_req_nterror(req, status)) {
3219 return tevent_req_post(req, ev);
3224 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3226 struct tevent_req *req =
3227 tevent_req_callback_data(subreq,
3229 struct smbXcli_req_state *state =
3230 tevent_req_data(req,
3231 struct smbXcli_req_state);
3235 nwritten = writev_recv(subreq, &err);
3236 TALLOC_FREE(subreq);
3237 if (nwritten == -1) {
3238 /* here, we need to notify all pending requests */
3239 NTSTATUS status = map_nt_error_from_unix_common(err);
3240 smbXcli_conn_disconnect(state->conn, status);
3245 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3248 TALLOC_CTX *mem_ctx,
3249 struct iovec **piov, int *pnum_iov)
3254 uint8_t *first_hdr = buf;
3255 size_t verified_buflen = 0;
3259 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3261 return NT_STATUS_NO_MEMORY;
3264 while (taken < buflen) {
3265 size_t len = buflen - taken;
3266 uint8_t *hdr = first_hdr + taken;
3269 size_t next_command_ofs;
3271 struct iovec *iov_tmp;
3273 if (verified_buflen > taken) {
3274 len = verified_buflen - taken;
3281 DEBUG(10, ("%d bytes left, expected at least %d\n",
3285 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3286 struct smbXcli_session *s;
3288 struct iovec tf_iov[2];
3292 if (len < SMB2_TF_HDR_SIZE) {
3293 DEBUG(10, ("%d bytes left, expected at least %d\n",
3294 (int)len, SMB2_TF_HDR_SIZE));
3298 tf_len = SMB2_TF_HDR_SIZE;
3301 hdr = first_hdr + taken;
3302 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3303 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3305 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3306 DEBUG(10, ("%d bytes left, expected at least %d\n",
3308 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3313 for (; s; s = s->next) {
3314 if (s->smb2->session_id != uid) {
3321 DEBUG(10, ("unknown session_id %llu\n",
3322 (unsigned long long)uid));
3326 tf_iov[0].iov_base = (void *)tf;
3327 tf_iov[0].iov_len = tf_len;
3328 tf_iov[1].iov_base = (void *)hdr;
3329 tf_iov[1].iov_len = enc_len;
3331 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3332 conn->smb2.server.cipher,
3334 if (!NT_STATUS_IS_OK(status)) {
3339 verified_buflen = taken + enc_len;
3344 * We need the header plus the body length field
3347 if (len < SMB2_HDR_BODY + 2) {
3348 DEBUG(10, ("%d bytes left, expected at least %d\n",
3349 (int)len, SMB2_HDR_BODY));
3352 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3353 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3357 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3358 DEBUG(10, ("Got HDR len %d, expected %d\n",
3359 SVAL(hdr, 4), SMB2_HDR_BODY));
3364 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3365 body_size = SVAL(hdr, SMB2_HDR_BODY);
3367 if (next_command_ofs != 0) {
3368 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3371 if (next_command_ofs > full_size) {
3374 full_size = next_command_ofs;
3376 if (body_size < 2) {
3379 body_size &= 0xfffe;
3381 if (body_size > (full_size - SMB2_HDR_BODY)) {
3385 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3387 if (iov_tmp == NULL) {
3389 return NT_STATUS_NO_MEMORY;
3392 cur = &iov[num_iov];
3395 cur[0].iov_base = tf;
3396 cur[0].iov_len = tf_len;
3397 cur[1].iov_base = hdr;
3398 cur[1].iov_len = SMB2_HDR_BODY;
3399 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3400 cur[2].iov_len = body_size;
3401 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3402 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3408 *pnum_iov = num_iov;
3409 return NT_STATUS_OK;
3413 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3416 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3419 size_t num_pending = talloc_array_length(conn->pending);
3422 for (i=0; i<num_pending; i++) {
3423 struct tevent_req *req = conn->pending[i];
3424 struct smbXcli_req_state *state =
3425 tevent_req_data(req,
3426 struct smbXcli_req_state);
3428 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3435 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3436 TALLOC_CTX *tmp_mem,
3439 struct tevent_req *req;
3440 struct smbXcli_req_state *state = NULL;
3445 struct smbXcli_session *last_session = NULL;
3446 size_t inbuf_len = smb_len_tcp(inbuf);
3448 status = smb2cli_inbuf_parse_compound(conn,
3449 inbuf + NBT_HDR_SIZE,
3453 if (!NT_STATUS_IS_OK(status)) {
3457 for (i=0; i<num_iov; i+=4) {
3458 uint8_t *inbuf_ref = NULL;
3459 struct iovec *cur = &iov[i];
3460 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3461 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3462 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3463 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3464 uint16_t req_opcode;
3466 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3467 uint32_t new_credits;
3468 struct smbXcli_session *session = NULL;
3469 const DATA_BLOB *signing_key = NULL;
3470 bool was_encrypted = false;
3472 new_credits = conn->smb2.cur_credits;
3473 new_credits += credits;
3474 if (new_credits > UINT16_MAX) {
3475 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3477 conn->smb2.cur_credits += credits;
3479 req = smb2cli_conn_find_pending(conn, mid);
3481 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3483 state = tevent_req_data(req, struct smbXcli_req_state);
3485 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3486 if (opcode != req_opcode) {
3487 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3489 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3491 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3492 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3495 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3496 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3497 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3498 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3500 if (state->smb2.got_async) {
3501 /* We only expect one STATUS_PENDING response */
3502 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3504 state->smb2.got_async = true;
3507 * async interim responses are not signed,
3508 * even if the SMB2_HDR_FLAG_SIGNED flag
3511 state->smb2.cancel_flags = SMB2_HDR_FLAG_ASYNC;
3512 state->smb2.cancel_mid = 0;
3513 state->smb2.cancel_aid = async_id;
3515 if (state->smb2.notify_async) {
3516 tevent_req_defer_callback(req, state->ev);
3517 tevent_req_notify_callback(req);
3522 session = state->session;
3523 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3524 session = last_session;
3526 last_session = session;
3528 if (state->smb2.should_sign) {
3529 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3530 return NT_STATUS_ACCESS_DENIED;
3534 if (flags & SMB2_HDR_FLAG_SIGNED) {
3535 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3537 if (session == NULL) {
3538 struct smbXcli_session *s;
3540 s = state->conn->sessions;
3541 for (; s; s = s->next) {
3542 if (s->smb2->session_id != uid) {
3551 if (session == NULL) {
3552 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3555 last_session = session;
3556 signing_key = &session->smb2_channel.signing_key;
3559 if (opcode == SMB2_OP_SESSSETUP) {
3561 * We prefer the channel signing key, if it is
3564 * If we do not have a channel signing key yet,
3565 * we try the main signing key, if it is not
3566 * the final response.
3568 if (signing_key && signing_key->length == 0 &&
3569 !NT_STATUS_IS_OK(status)) {
3570 signing_key = &session->smb2->signing_key;
3573 if (signing_key && signing_key->length == 0) {
3575 * If we do not have a session key to
3576 * verify the signature, we defer the
3577 * signing check to the caller.
3579 * The caller gets NT_STATUS_OK, it
3581 * smb2cli_session_set_session_key()
3583 * smb2cli_session_set_channel_key()
3584 * which will check the signature
3585 * with the channel signing key.
3591 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3592 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3593 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3596 * If the response was encrypted in a SMB2_TRANSFORM
3597 * pdu, which belongs to the correct session,
3598 * we do not need to do signing checks
3600 * It could be the session the response belongs to
3601 * or the session that was used to encrypt the
3602 * SMB2_TRANSFORM request.
3604 if ((session && session->smb2->session_id == uid) ||
3605 (state->smb2.encryption_session_id == uid)) {
3607 was_encrypted = true;
3611 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3613 * if the server returns NT_STATUS_USER_SESSION_DELETED
3614 * the response is not signed and we should
3615 * propagate the NT_STATUS_USER_SESSION_DELETED
3616 * status to the caller.
3618 state->smb2.signing_skipped = true;
3622 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3624 * if the server returns
3625 * NT_STATUS_INVALID_PARAMETER
3626 * the response might not be encrypted.
3628 if (state->smb2.should_encrypt && !was_encrypted) {
3629 state->smb2.signing_skipped = true;
3634 if (state->smb2.should_encrypt && !was_encrypted) {
3635 if (!state->smb2.signing_skipped) {
3636 return NT_STATUS_ACCESS_DENIED;
3640 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3641 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3642 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3644 * if the server returns
3645 * NT_STATUS_NETWORK_NAME_DELETED
3646 * NT_STATUS_FILE_CLOSED
3647 * NT_STATUS_INVALID_PARAMETER
3648 * the response might not be signed
3649 * as this happens before the signing checks.
3651 * If server echos the signature (or all zeros)
3652 * we should report the status from the server
3658 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3659 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3662 state->smb2.signing_skipped = true;
3668 static const uint8_t zeros[16];
3670 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3674 state->smb2.signing_skipped = true;
3681 status = smb2_signing_check_pdu(*signing_key,
3682 state->conn->protocol,
3684 if (!NT_STATUS_IS_OK(status)) {
3686 * If the signing check fails, we disconnect
3693 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3694 (session != NULL) && session->disconnect_expired)
3697 * this should be a short term hack
3698 * until the upper layers have implemented
3699 * re-authentication.
3704 smbXcli_req_unset_pending(req);
3707 * There might be more than one response
3708 * we need to defer the notifications
3710 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3715 tevent_req_defer_callback(req, state->ev);
3719 * Note: here we use talloc_reference() in a way
3720 * that does not expose it to the caller.
3722 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3723 if (tevent_req_nomem(inbuf_ref, req)) {
3727 /* copy the related buffers */
3728 state->smb2.recv_iov[0] = cur[1];
3729 state->smb2.recv_iov[1] = cur[2];
3730 state->smb2.recv_iov[2] = cur[3];
3732 tevent_req_done(req);
3736 return NT_STATUS_RETRY;
3739 return NT_STATUS_OK;
3742 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3743 struct iovec **piov,
3744 const struct smb2cli_req_expected_response *expected,
3745 size_t num_expected)
3747 struct smbXcli_req_state *state =
3748 tevent_req_data(req,
3749 struct smbXcli_req_state);
3752 bool found_status = false;
3753 bool found_size = false;
3760 if (tevent_req_is_in_progress(req) && state->smb2.got_async) {
3761 return STATUS_PENDING;
3764 if (tevent_req_is_nterror(req, &status)) {
3765 for (i=0; i < num_expected; i++) {
3766 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3767 found_status = true;
3773 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3779 if (num_expected == 0) {
3780 found_status = true;
3784 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3785 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3787 for (i=0; i < num_expected; i++) {
3788 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3792 found_status = true;
3793 if (expected[i].body_size == 0) {
3798 if (expected[i].body_size == body_size) {
3804 if (!found_status) {
3808 if (state->smb2.signing_skipped) {
3809 if (num_expected > 0) {
3810 return NT_STATUS_ACCESS_DENIED;
3812 if (!NT_STATUS_IS_ERR(status)) {
3813 return NT_STATUS_ACCESS_DENIED;
3818 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3822 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3828 NTSTATUS smb2cli_req_get_sent_iov(struct tevent_req *req,
3829 struct iovec *sent_iov)
3831 struct smbXcli_req_state *state =
3832 tevent_req_data(req,
3833 struct smbXcli_req_state);
3835 if (tevent_req_is_in_progress(req)) {
3836 return STATUS_PENDING;
3839 sent_iov[0].iov_base = state->smb2.hdr;
3840 sent_iov[0].iov_len = sizeof(state->smb2.hdr);
3842 sent_iov[1].iov_base = discard_const(state->smb2.fixed);
3843 sent_iov[1].iov_len = state->smb2.fixed_len;
3845 if (state->smb2.dyn != NULL) {
3846 sent_iov[2].iov_base = discard_const(state->smb2.dyn);
3847 sent_iov[2].iov_len = state->smb2.dyn_len;
3849 sent_iov[2].iov_base = NULL;
3850 sent_iov[2].iov_len = 0;
3853 return NT_STATUS_OK;
3856 static const struct {
3857 enum protocol_types proto;
3858 const char *smb1_name;
3859 } smb1cli_prots[] = {
3860 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3861 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3862 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3863 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3864 {PROTOCOL_LANMAN2, "LM1.2X002"},
3865 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3866 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3867 {PROTOCOL_LANMAN2, "Samba"},
3868 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3869 {PROTOCOL_NT1, "NT LM 0.12"},
3870 {PROTOCOL_SMB2_02, "SMB 2.002"},
3871 {PROTOCOL_SMB2_10, "SMB 2.???"},
3874 static const struct {
3875 enum protocol_types proto;
3876 uint16_t smb2_dialect;
3877 } smb2cli_prots[] = {
3878 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3879 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3880 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3881 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3882 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3883 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
3884 {PROTOCOL_SMB3_10, SMB3_DIALECT_REVISION_310},
3885 {PROTOCOL_SMB3_11, SMB3_DIALECT_REVISION_311},
3888 struct smbXcli_negprot_state {
3889 struct smbXcli_conn *conn;
3890 struct tevent_context *ev;
3891 uint32_t timeout_msec;
3898 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3899 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3900 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3901 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3902 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3903 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3907 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3908 struct tevent_context *ev,
3909 struct smbXcli_conn *conn,
3910 uint32_t timeout_msec,
3911 enum protocol_types min_protocol,
3912 enum protocol_types max_protocol)
3914 struct tevent_req *req, *subreq;
3915 struct smbXcli_negprot_state *state;
3917 req = tevent_req_create(mem_ctx, &state,
3918 struct smbXcli_negprot_state);
3924 state->timeout_msec = timeout_msec;
3926 if (min_protocol == PROTOCOL_NONE) {
3927 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3928 return tevent_req_post(req, ev);
3931 if (max_protocol == PROTOCOL_NONE) {
3932 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3933 return tevent_req_post(req, ev);
3936 if (min_protocol > max_protocol) {
3937 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3938 return tevent_req_post(req, ev);
3941 conn->min_protocol = min_protocol;
3942 conn->max_protocol = max_protocol;
3943 conn->protocol = PROTOCOL_NONE;
3945 if ((min_protocol < PROTOCOL_SMB2_02) &&
3946 (max_protocol < PROTOCOL_SMB2_02)) {
3950 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3952 subreq = smbXcli_negprot_smb1_subreq(state);
3953 if (tevent_req_nomem(subreq, req)) {
3954 return tevent_req_post(req, ev);
3956 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3960 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3961 (max_protocol >= PROTOCOL_SMB2_02)) {
3965 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3968 * As we're starting with an SMB2 negprot, emulate Windows
3969 * and ask for 31 credits in the initial SMB2 negprot.
3970 * If we don't and leave requested credits at
3971 * zero, MacOSX servers return zero credits on
3972 * the negprot reply and we fail to connect.
3974 smb2cli_conn_set_max_credits(conn,
3975 WINDOWS_CLIENT_PURE_SMB2_NEGPROT_INITIAL_CREDIT_ASK);
3977 subreq = smbXcli_negprot_smb2_subreq(state);
3978 if (tevent_req_nomem(subreq, req)) {
3979 return tevent_req_post(req, ev);
3981 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3986 * We send an SMB1 negprot with the SMB2 dialects
3987 * and expect a SMB1 or a SMB2 response.
3989 * smbXcli_negprot_dispatch_incoming() will fix the
3990 * callback to match protocol of the response.
3992 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3994 subreq = smbXcli_negprot_smb1_subreq(state);
3995 if (tevent_req_nomem(subreq, req)) {
3996 return tevent_req_post(req, ev);
3998 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
4002 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
4004 struct tevent_req *req =
4005 tevent_req_callback_data(subreq,
4010 * we just want the low level error
4012 status = tevent_req_simple_recv_ntstatus(subreq);
4013 TALLOC_FREE(subreq);
4014 if (tevent_req_nterror(req, status)) {
4018 /* this should never happen */
4019 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4022 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
4025 DATA_BLOB bytes = data_blob_null;
4029 /* setup the protocol strings */
4030 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4034 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4038 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4042 ok = data_blob_append(state, &bytes, &c, sizeof(c));
4048 * We now it is already ascii and
4049 * we want NULL termination.
4051 ok = data_blob_append(state, &bytes,
4052 smb1cli_prots[i].smb1_name,
4053 strlen(smb1cli_prots[i].smb1_name)+1);
4059 smb1cli_req_flags(state->conn->max_protocol,
4060 state->conn->smb1.client.capabilities,
4065 return smb1cli_req_send(state, state->ev, state->conn,
4069 state->timeout_msec,
4070 0xFFFE, 0, NULL, /* pid, tid, session */
4071 0, NULL, /* wct, vwv */
4072 bytes.length, bytes.data);
4075 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
4077 struct tevent_req *req =
4078 tevent_req_callback_data(subreq,
4080 struct smbXcli_negprot_state *state =
4081 tevent_req_data(req,
4082 struct smbXcli_negprot_state);
4083 struct smbXcli_conn *conn = state->conn;
4084 struct iovec *recv_iov = NULL;
4093 size_t num_prots = 0;
4095 uint32_t client_capabilities = conn->smb1.client.capabilities;
4096 uint32_t both_capabilities;
4097 uint32_t server_capabilities = 0;
4098 uint32_t capabilities;
4099 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
4100 uint32_t server_max_xmit = 0;
4102 uint32_t server_max_mux = 0;
4103 uint16_t server_security_mode = 0;
4104 uint32_t server_session_key = 0;
4105 bool server_readbraw = false;
4106 bool server_writebraw = false;
4107 bool server_lockread = false;
4108 bool server_writeunlock = false;
4109 struct GUID server_guid = GUID_zero();
4110 DATA_BLOB server_gss_blob = data_blob_null;
4111 uint8_t server_challenge[8];
4112 char *server_workgroup = NULL;
4113 char *server_name = NULL;
4114 int server_time_zone = 0;
4115 NTTIME server_system_time = 0;
4116 static const struct smb1cli_req_expected_response expected[] = {
4118 .status = NT_STATUS_OK,
4119 .wct = 0x11, /* NT1 */
4122 .status = NT_STATUS_OK,
4123 .wct = 0x0D, /* LM */
4126 .status = NT_STATUS_OK,
4127 .wct = 0x01, /* CORE */
4131 ZERO_STRUCT(server_challenge);
4133 status = smb1cli_req_recv(subreq, state,
4138 NULL, /* pvwv_offset */
4141 NULL, /* pbytes_offset */
4143 expected, ARRAY_SIZE(expected));
4144 TALLOC_FREE(subreq);
4145 if (tevent_req_nterror(req, status)) {
4149 flags = CVAL(inhdr, HDR_FLG);
4151 protnum = SVAL(vwv, 0);
4153 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4154 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4158 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4162 if (protnum != num_prots) {
4167 conn->protocol = smb1cli_prots[i].proto;
4171 if (conn->protocol == PROTOCOL_NONE) {
4172 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4176 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
4177 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4178 "and the selected protocol level doesn't support it.\n"));
4179 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4183 if (flags & FLAG_SUPPORT_LOCKREAD) {
4184 server_lockread = true;
4185 server_writeunlock = true;
4188 if (conn->protocol >= PROTOCOL_NT1) {
4189 const char *client_signing = NULL;
4190 bool server_mandatory = false;
4191 bool server_allowed = false;
4192 const char *server_signing = NULL;
4197 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4202 server_security_mode = CVAL(vwv + 1, 0);
4203 server_max_mux = SVAL(vwv + 1, 1);
4204 server_max_xmit = IVAL(vwv + 3, 1);
4205 server_session_key = IVAL(vwv + 7, 1);
4206 server_time_zone = SVALS(vwv + 15, 1);
4207 server_time_zone *= 60;
4208 /* this time arrives in real GMT */
4209 server_system_time = BVAL(vwv + 11, 1);
4210 server_capabilities = IVAL(vwv + 9, 1);
4212 key_len = CVAL(vwv + 16, 1);
4214 if (server_capabilities & CAP_RAW_MODE) {
4215 server_readbraw = true;
4216 server_writebraw = true;
4218 if (server_capabilities & CAP_LOCK_AND_READ) {
4219 server_lockread = true;
4222 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4223 DATA_BLOB blob1, blob2;
4225 if (num_bytes < 16) {
4226 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4230 blob1 = data_blob_const(bytes, 16);
4231 status = GUID_from_data_blob(&blob1, &server_guid);
4232 if (tevent_req_nterror(req, status)) {
4236 blob1 = data_blob_const(bytes+16, num_bytes-16);
4237 blob2 = data_blob_dup_talloc(state, blob1);
4238 if (blob1.length > 0 &&
4239 tevent_req_nomem(blob2.data, req)) {
4242 server_gss_blob = blob2;
4244 DATA_BLOB blob1, blob2;
4246 if (num_bytes < key_len) {
4247 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4251 if (key_len != 0 && key_len != 8) {
4252 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4257 memcpy(server_challenge, bytes, 8);
4260 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4261 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4262 if (blob1.length > 0) {
4265 len = utf16_len_n(blob1.data,
4269 ok = convert_string_talloc(state,
4277 status = map_nt_error_from_unix_common(errno);
4278 tevent_req_nterror(req, status);
4283 blob2.data += blob1.length;
4284 blob2.length -= blob1.length;
4285 if (blob2.length > 0) {
4288 len = utf16_len_n(blob1.data,
4292 ok = convert_string_talloc(state,
4300 status = map_nt_error_from_unix_common(errno);
4301 tevent_req_nterror(req, status);
4307 client_signing = "disabled";
4308 if (conn->allow_signing) {
4309 client_signing = "allowed";
4311 if (conn->mandatory_signing) {
4312 client_signing = "required";
4315 server_signing = "not supported";
4316 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4317 server_signing = "supported";
4318 server_allowed = true;
4319 } else if (conn->mandatory_signing) {
4321 * We have mandatory signing as client
4322 * lets assume the server will look at our
4323 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4324 * flag in the session setup
4326 server_signing = "not announced";
4327 server_allowed = true;
4329 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4330 server_signing = "required";
4331 server_mandatory = true;
4334 ok = smb_signing_set_negotiated(conn->smb1.signing,
4338 DEBUG(1,("cli_negprot: SMB signing is required, "
4339 "but client[%s] and server[%s] mismatch\n",
4340 client_signing, server_signing));
4341 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4345 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4351 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4355 server_security_mode = SVAL(vwv + 1, 0);
4356 server_max_xmit = SVAL(vwv + 2, 0);
4357 server_max_mux = SVAL(vwv + 3, 0);
4358 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4359 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4360 server_session_key = IVAL(vwv + 6, 0);
4361 server_time_zone = SVALS(vwv + 10, 0);
4362 server_time_zone *= 60;
4363 /* this time is converted to GMT by make_unix_date */
4364 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4365 unix_to_nt_time(&server_system_time, t);
4366 key_len = SVAL(vwv + 11, 0);
4368 if (num_bytes < key_len) {
4369 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4373 if (key_len != 0 && key_len != 8) {
4374 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4379 memcpy(server_challenge, bytes, 8);
4382 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4383 if (blob1.length > 0) {
4387 len = utf16_len_n(blob1.data,
4391 ok = convert_string_talloc(state,
4399 status = map_nt_error_from_unix_common(errno);
4400 tevent_req_nterror(req, status);
4406 /* the old core protocol */
4407 server_time_zone = get_time_zone(time(NULL));
4408 server_max_xmit = 1024;
4412 if (server_max_xmit < 1024) {
4413 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4417 if (server_max_mux < 1) {
4418 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4423 * Now calculate the negotiated capabilities
4424 * based on the mask for:
4425 * - client only flags
4426 * - flags used in both directions
4427 * - server only flags
4429 both_capabilities = client_capabilities & server_capabilities;
4430 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4431 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4432 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4434 max_xmit = MIN(client_max_xmit, server_max_xmit);
4436 conn->smb1.server.capabilities = server_capabilities;
4437 conn->smb1.capabilities = capabilities;
4439 conn->smb1.server.max_xmit = server_max_xmit;
4440 conn->smb1.max_xmit = max_xmit;
4442 conn->smb1.server.max_mux = server_max_mux;
4444 conn->smb1.server.security_mode = server_security_mode;
4446 conn->smb1.server.readbraw = server_readbraw;
4447 conn->smb1.server.writebraw = server_writebraw;
4448 conn->smb1.server.lockread = server_lockread;
4449 conn->smb1.server.writeunlock = server_writeunlock;
4451 conn->smb1.server.session_key = server_session_key;
4453 talloc_steal(conn, server_gss_blob.data);
4454 conn->smb1.server.gss_blob = server_gss_blob;
4455 conn->smb1.server.guid = server_guid;
4456 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4457 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4458 conn->smb1.server.name = talloc_move(conn, &server_name);
4460 conn->smb1.server.time_zone = server_time_zone;
4461 conn->smb1.server.system_time = server_system_time;
4463 tevent_req_done(req);
4466 static size_t smbXcli_padding_helper(uint32_t offset, size_t n)
4468 if ((offset & (n-1)) == 0) return 0;
4469 return n - (offset & (n-1));
4472 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4476 uint16_t dialect_count = 0;
4477 DATA_BLOB dyn = data_blob_null;
4479 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4483 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4487 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4491 SSVAL(val, 0, smb2cli_prots[i].smb2_dialect);
4493 ok = data_blob_append(state, &dyn, val, sizeof(val));
4501 buf = state->smb2.fixed;
4503 SSVAL(buf, 2, dialect_count);
4504 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4505 SSVAL(buf, 6, 0); /* Reserved */
4506 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
4507 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4509 SIVAL(buf, 8, 0); /* Capabilities */
4511 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4515 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4517 if (!NT_STATUS_IS_OK(status)) {
4520 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4522 memset(buf+12, 0, 16); /* ClientGuid */
4525 if (state->conn->max_protocol >= PROTOCOL_SMB3_10) {
4527 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4531 const uint8_t zeros[8] = {0, };
4535 SSVAL(p, 0, 1); /* HashAlgorithmCount */
4536 SSVAL(p, 2, 32); /* SaltLength */
4537 SSVAL(p, 4, SMB2_PREAUTH_INTEGRITY_SHA512);
4538 generate_random_buffer(p + 6, 32);
4540 b = data_blob_const(p, 38);
4541 status = smb2_negotiate_context_add(state, &c,
4542 SMB2_PREAUTH_INTEGRITY_CAPABILITIES, b);
4543 if (!NT_STATUS_IS_OK(status)) {
4547 SSVAL(p, 0, 2); /* ChiperCount */
4548 SSVAL(p, 2, SMB2_ENCRYPTION_AES128_GCM);
4549 SSVAL(p, 4, SMB2_ENCRYPTION_AES128_CCM);
4551 b = data_blob_const(p, 6);
4552 status = smb2_negotiate_context_add(state, &c,
4553 SMB2_ENCRYPTION_CAPABILITIES, b);
4554 if (!NT_STATUS_IS_OK(status)) {
4558 status = smb2_negotiate_context_push(state, &b, c);
4559 if (!NT_STATUS_IS_OK(status)) {
4563 offset = SMB2_HDR_BODY + sizeof(state->smb2.fixed) + dyn.length;
4564 pad = smbXcli_padding_helper(offset, 8);
4566 ok = data_blob_append(state, &dyn, zeros, pad);
4572 ok = data_blob_append(state, &dyn, b.data, b.length);
4577 SIVAL(buf, 28, offset); /* NegotiateContextOffset */
4578 SSVAL(buf, 32, c.num_contexts); /* NegotiateContextCount */
4579 SSVAL(buf, 34, 0); /* Reserved */
4581 SBVAL(buf, 28, 0); /* Reserved/ClientStartTime */
4584 return smb2cli_req_send(state, state->ev,
4585 state->conn, SMB2_OP_NEGPROT,
4587 state->timeout_msec,
4588 NULL, NULL, /* tcon, session */
4589 state->smb2.fixed, sizeof(state->smb2.fixed),
4590 dyn.data, dyn.length,
4591 UINT16_MAX); /* max_dyn_len */
4594 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4596 struct tevent_req *req =
4597 tevent_req_callback_data(subreq,
4599 struct smbXcli_negprot_state *state =
4600 tevent_req_data(req,
4601 struct smbXcli_negprot_state);
4602 struct smbXcli_conn *conn = state->conn;
4603 size_t security_offset, security_length;
4609 uint16_t dialect_revision;
4610 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4611 uint32_t negotiate_context_offset = 0;
4612 uint16_t negotiate_context_count = 0;
4613 DATA_BLOB negotiate_context_blob = data_blob_null;
4617 struct smb2_negotiate_context *preauth = NULL;
4618 uint16_t hash_count;
4619 uint16_t salt_length;
4620 uint16_t hash_selected;
4621 struct hc_sha512state sctx;
4622 struct smb2_negotiate_context *cipher = NULL;
4623 struct iovec sent_iov[3];
4624 static const struct smb2cli_req_expected_response expected[] = {
4626 .status = NT_STATUS_OK,
4631 status = smb2cli_req_recv(subreq, state, &iov,
4632 expected, ARRAY_SIZE(expected));
4633 if (tevent_req_nterror(req, status)) {
4637 body = (uint8_t *)iov[1].iov_base;
4639 dialect_revision = SVAL(body, 4);
4641 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4642 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4646 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4650 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4654 conn->protocol = smb2cli_prots[i].proto;
4658 if (conn->protocol == PROTOCOL_NONE) {
4659 TALLOC_FREE(subreq);
4661 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
4662 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4666 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4667 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4671 /* make sure we do not loop forever */
4672 state->conn->min_protocol = PROTOCOL_SMB2_02;
4675 * send a SMB2 negprot, in order to negotiate
4678 subreq = smbXcli_negprot_smb2_subreq(state);
4679 if (tevent_req_nomem(subreq, req)) {
4682 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4686 conn->smb2.server.security_mode = SVAL(body, 2);
4687 if (conn->protocol >= PROTOCOL_SMB3_10) {
4688 negotiate_context_count = SVAL(body, 6);
4691 blob = data_blob_const(body + 8, 16);
4692 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4693 if (tevent_req_nterror(req, status)) {
4697 conn->smb2.server.capabilities = IVAL(body, 24);
4698 conn->smb2.server.max_trans_size= IVAL(body, 28);
4699 conn->smb2.server.max_read_size = IVAL(body, 32);
4700 conn->smb2.server.max_write_size= IVAL(body, 36);
4701 conn->smb2.server.system_time = BVAL(body, 40);
4702 conn->smb2.server.start_time = BVAL(body, 48);
4704 security_offset = SVAL(body, 56);
4705 security_length = SVAL(body, 58);
4707 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4708 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4712 if (security_length > iov[2].iov_len) {
4713 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4717 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4720 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4724 if (conn->protocol < PROTOCOL_SMB3_10) {
4725 TALLOC_FREE(subreq);
4727 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4728 conn->smb2.server.cipher = SMB2_ENCRYPTION_AES128_CCM;
4730 tevent_req_done(req);
4734 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4735 tevent_req_nterror(req,
4736 NT_STATUS_INVALID_NETWORK_RESPONSE);
4740 negotiate_context_offset = IVAL(body, 60);
4741 if (negotiate_context_offset < security_offset) {
4742 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4746 ctx_ofs = negotiate_context_offset - security_offset;
4747 if (ctx_ofs > iov[2].iov_len) {
4748 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4751 avail = iov[2].iov_len - security_length;
4752 needed = iov[2].iov_len - ctx_ofs;
4753 if (needed > avail) {
4754 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4758 negotiate_context_blob.data = (uint8_t *)iov[2].iov_base;
4759 negotiate_context_blob.length = iov[2].iov_len;
4761 negotiate_context_blob.data += ctx_ofs;
4762 negotiate_context_blob.length -= ctx_ofs;
4764 status = smb2_negotiate_context_parse(state, negotiate_context_blob, &c);
4765 if (tevent_req_nterror(req, status)) {
4769 if (negotiate_context_count != c.num_contexts) {
4770 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4774 preauth = smb2_negotiate_context_find(&c,
4775 SMB2_PREAUTH_INTEGRITY_CAPABILITIES);
4776 if (preauth == NULL) {
4777 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4781 if (preauth->data.length < 6) {
4782 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4786 hash_count = SVAL(preauth->data.data, 0);
4787 salt_length = SVAL(preauth->data.data, 2);
4788 hash_selected = SVAL(preauth->data.data, 4);
4790 if (hash_count != 1) {
4791 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4795 if (preauth->data.length != (6 + salt_length)) {
4796 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4800 if (hash_selected != SMB2_PREAUTH_INTEGRITY_SHA512) {
4801 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4805 cipher = smb2_negotiate_context_find(&c, SMB2_ENCRYPTION_CAPABILITIES);
4806 if (cipher != NULL) {
4807 uint16_t cipher_count;
4809 if (cipher->data.length < 2) {
4810 tevent_req_nterror(req,
4811 NT_STATUS_INVALID_NETWORK_RESPONSE);
4815 cipher_count = SVAL(cipher->data.data, 0);
4817 if (cipher_count > 1) {
4818 tevent_req_nterror(req,
4819 NT_STATUS_INVALID_NETWORK_RESPONSE);
4823 if (cipher->data.length != (2 + 2 * cipher_count)) {
4824 tevent_req_nterror(req,
4825 NT_STATUS_INVALID_NETWORK_RESPONSE);
4829 if (cipher_count == 1) {
4830 uint16_t cipher_selected;
4832 cipher_selected = SVAL(cipher->data.data, 2);
4834 switch (cipher_selected) {
4835 case SMB2_ENCRYPTION_AES128_GCM:
4836 case SMB2_ENCRYPTION_AES128_CCM:
4837 conn->smb2.server.cipher = cipher_selected;
4843 /* First we hash the request */
4844 smb2cli_req_get_sent_iov(subreq, sent_iov);
4845 samba_SHA512_Init(&sctx);
4846 samba_SHA512_Update(&sctx, conn->smb2.preauth_sha512,
4847 sizeof(conn->smb2.preauth_sha512));
4848 for (i = 0; i < 3; i++) {
4849 samba_SHA512_Update(&sctx, sent_iov[i].iov_base, sent_iov[i].iov_len);
4851 samba_SHA512_Final(conn->smb2.preauth_sha512, &sctx);
4852 TALLOC_FREE(subreq);
4854 /* And now we hash the response */
4855 samba_SHA512_Init(&sctx);
4856 samba_SHA512_Update(&sctx, conn->smb2.preauth_sha512,
4857 sizeof(conn->smb2.preauth_sha512));
4858 for (i = 0; i < 3; i++) {
4859 samba_SHA512_Update(&sctx, iov[i].iov_base, iov[i].iov_len);
4861 samba_SHA512_Final(conn->smb2.preauth_sha512, &sctx);
4863 tevent_req_done(req);
4866 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4867 TALLOC_CTX *tmp_mem,
4870 size_t num_pending = talloc_array_length(conn->pending);
4871 struct tevent_req *subreq;
4872 struct smbXcli_req_state *substate;
4873 struct tevent_req *req;
4874 uint32_t protocol_magic;
4875 size_t inbuf_len = smb_len_nbt(inbuf);
4877 if (num_pending != 1) {
4878 return NT_STATUS_INTERNAL_ERROR;
4881 if (inbuf_len < 4) {
4882 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4885 subreq = conn->pending[0];
4886 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4887 req = tevent_req_callback_data(subreq, struct tevent_req);
4889 protocol_magic = IVAL(inbuf, 4);
4891 switch (protocol_magic) {
4893 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4894 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4895 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4898 if (substate->smb2.recv_iov == NULL) {
4900 * For the SMB1 negprot we have move it.
4902 substate->smb2.recv_iov = substate->smb1.recv_iov;
4903 substate->smb1.recv_iov = NULL;
4907 * we got an SMB2 answer, which consumed sequence number 0
4908 * so we need to use 1 as the next one.
4910 * we also need to set the current credits to 0
4911 * as we consumed the initial one. The SMB2 answer
4912 * hopefully grant us a new credit.
4915 conn->smb2.cur_credits = 0;
4916 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4917 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4918 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4921 DEBUG(10, ("Got non-SMB PDU\n"));
4922 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4925 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4927 return tevent_req_simple_recv_ntstatus(req);
4930 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4931 uint32_t timeout_msec,
4932 enum protocol_types min_protocol,
4933 enum protocol_types max_protocol)
4935 TALLOC_CTX *frame = talloc_stackframe();
4936 struct tevent_context *ev;
4937 struct tevent_req *req;
4938 NTSTATUS status = NT_STATUS_NO_MEMORY;
4941 if (smbXcli_conn_has_async_calls(conn)) {
4943 * Can't use sync call while an async call is in flight
4945 status = NT_STATUS_INVALID_PARAMETER_MIX;
4948 ev = samba_tevent_context_init(frame);
4952 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4953 min_protocol, max_protocol);
4957 ok = tevent_req_poll_ntstatus(req, ev, &status);
4961 status = smbXcli_negprot_recv(req);
4967 struct smb2cli_validate_negotiate_info_state {
4968 struct smbXcli_conn *conn;
4969 DATA_BLOB in_input_buffer;
4970 DATA_BLOB in_output_buffer;
4971 DATA_BLOB out_input_buffer;
4972 DATA_BLOB out_output_buffer;
4976 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq);
4978 struct tevent_req *smb2cli_validate_negotiate_info_send(TALLOC_CTX *mem_ctx,
4979 struct tevent_context *ev,
4980 struct smbXcli_conn *conn,
4981 uint32_t timeout_msec,
4982 struct smbXcli_session *session,
4983 struct smbXcli_tcon *tcon)
4985 struct tevent_req *req;
4986 struct smb2cli_validate_negotiate_info_state *state;
4988 uint16_t dialect_count = 0;
4989 struct tevent_req *subreq;
4990 bool _save_should_sign;
4993 req = tevent_req_create(mem_ctx, &state,
4994 struct smb2cli_validate_negotiate_info_state);
5000 state->in_input_buffer = data_blob_talloc_zero(state,
5001 4 + 16 + 1 + 1 + 2);
5002 if (tevent_req_nomem(state->in_input_buffer.data, req)) {
5003 return tevent_req_post(req, ev);
5005 buf = state->in_input_buffer.data;
5007 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
5008 SIVAL(buf, 0, conn->smb2.client.capabilities);
5010 SIVAL(buf, 0, 0); /* Capabilities */
5012 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
5016 status = GUID_to_ndr_blob(&conn->smb2.client.guid,
5018 if (!NT_STATUS_IS_OK(status)) {
5021 memcpy(buf+4, blob.data, 16); /* ClientGuid */
5023 memset(buf+4, 0, 16); /* ClientGuid */
5025 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
5026 SCVAL(buf, 20, conn->smb2.client.security_mode);
5030 SCVAL(buf, 21, 0); /* reserved */
5032 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
5036 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
5040 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
5044 if (smb2cli_prots[i].proto == state->conn->protocol) {
5045 state->dialect = smb2cli_prots[i].smb2_dialect;
5048 ofs = state->in_input_buffer.length;
5049 ok = data_blob_realloc(state, &state->in_input_buffer,
5052 tevent_req_oom(req);
5053 return tevent_req_post(req, ev);
5056 buf = state->in_input_buffer.data;
5057 SSVAL(buf, ofs, smb2cli_prots[i].smb2_dialect);
5061 buf = state->in_input_buffer.data;
5062 SSVAL(buf, 22, dialect_count);
5064 _save_should_sign = smb2cli_tcon_is_signing_on(tcon);
5065 smb2cli_tcon_should_sign(tcon, true);
5066 subreq = smb2cli_ioctl_send(state, ev, conn,
5067 timeout_msec, session, tcon,
5068 UINT64_MAX, /* in_fid_persistent */
5069 UINT64_MAX, /* in_fid_volatile */
5070 FSCTL_VALIDATE_NEGOTIATE_INFO,
5071 0, /* in_max_input_length */
5072 &state->in_input_buffer,
5073 24, /* in_max_output_length */
5074 &state->in_output_buffer,
5075 SMB2_IOCTL_FLAG_IS_FSCTL);
5076 smb2cli_tcon_should_sign(tcon, _save_should_sign);
5077 if (tevent_req_nomem(subreq, req)) {
5078 return tevent_req_post(req, ev);
5080 tevent_req_set_callback(subreq,
5081 smb2cli_validate_negotiate_info_done,
5087 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq)
5089 struct tevent_req *req =
5090 tevent_req_callback_data(subreq,
5092 struct smb2cli_validate_negotiate_info_state *state =
5093 tevent_req_data(req,
5094 struct smb2cli_validate_negotiate_info_state);
5097 uint32_t capabilities;
5098 DATA_BLOB guid_blob;
5099 struct GUID server_guid;
5100 uint16_t security_mode;
5103 status = smb2cli_ioctl_recv(subreq, state,
5104 &state->out_input_buffer,
5105 &state->out_output_buffer);
5106 TALLOC_FREE(subreq);
5107 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
5109 * The response was signed, but not supported
5111 * Older Windows and Samba releases return
5112 * NT_STATUS_FILE_CLOSED.
5114 tevent_req_done(req);
5117 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_DEVICE_REQUEST)) {
5119 * The response was signed, but not supported
5121 * This is returned by the NTVFS based Samba 4.x file server
5124 tevent_req_done(req);
5127 if (NT_STATUS_EQUAL(status, NT_STATUS_FS_DRIVER_REQUIRED)) {
5129 * The response was signed, but not supported
5131 * This is returned by the NTVFS based Samba 4.x file server
5134 tevent_req_done(req);
5137 if (tevent_req_nterror(req, status)) {
5141 if (state->out_output_buffer.length != 24) {
5142 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5146 buf = state->out_output_buffer.data;
5148 capabilities = IVAL(buf, 0);
5149 guid_blob = data_blob_const(buf + 4, 16);
5150 status = GUID_from_data_blob(&guid_blob, &server_guid);
5151 if (tevent_req_nterror(req, status)) {
5154 security_mode = CVAL(buf, 20);
5155 dialect = SVAL(buf, 22);
5157 if (capabilities != state->conn->smb2.server.capabilities) {
5158 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5162 if (!GUID_equal(&server_guid, &state->conn->smb2.server.guid)) {
5163 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5167 if (security_mode != state->conn->smb2.server.security_mode) {
5168 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5172 if (dialect != state->dialect) {
5173 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5177 tevent_req_done(req);
5180 NTSTATUS smb2cli_validate_negotiate_info_recv(struct tevent_req *req)
5182 return tevent_req_simple_recv_ntstatus(req);
5185 static int smbXcli_session_destructor(struct smbXcli_session *session)
5187 if (session->conn == NULL) {
5191 DLIST_REMOVE(session->conn->sessions, session);
5195 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
5196 struct smbXcli_conn *conn)
5198 struct smbXcli_session *session;
5200 session = talloc_zero(mem_ctx, struct smbXcli_session);
5201 if (session == NULL) {
5204 session->smb2 = talloc_zero(session, struct smb2cli_session);
5205 if (session->smb2 == NULL) {
5206 talloc_free(session);
5209 talloc_set_destructor(session, smbXcli_session_destructor);
5211 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
5212 session->conn = conn;
5214 memcpy(session->smb2_channel.preauth_sha512,
5215 conn->smb2.preauth_sha512,
5216 sizeof(session->smb2_channel.preauth_sha512));
5221 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
5222 struct smbXcli_session *src)
5224 struct smbXcli_session *session;
5226 session = talloc_zero(mem_ctx, struct smbXcli_session);
5227 if (session == NULL) {
5230 session->smb2 = talloc_zero(session, struct smb2cli_session);
5231 if (session->smb2 == NULL) {
5232 talloc_free(session);
5236 session->conn = src->conn;
5237 *session->smb2 = *src->smb2;
5238 session->smb2_channel = src->smb2_channel;
5239 session->disconnect_expired = src->disconnect_expired;
5241 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
5242 talloc_set_destructor(session, smbXcli_session_destructor);
5247 bool smbXcli_session_is_authenticated(struct smbXcli_session *session)
5249 const DATA_BLOB *application_key;
5251 if (session->conn == NULL) {
5256 * If we have an application key we had a session key negotiated
5259 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5260 application_key = &session->smb2->application_key;
5262 application_key = &session->smb1.application_key;
5265 if (application_key->length == 0) {
5272 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
5273 TALLOC_CTX *mem_ctx,
5276 const DATA_BLOB *application_key;
5278 *key = data_blob_null;
5280 if (session->conn == NULL) {
5281 return NT_STATUS_NO_USER_SESSION_KEY;
5284 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5285 application_key = &session->smb2->application_key;
5287 application_key = &session->smb1.application_key;
5290 if (application_key->length == 0) {
5291 return NT_STATUS_NO_USER_SESSION_KEY;
5294 *key = data_blob_dup_talloc(mem_ctx, *application_key);
5295 if (key->data == NULL) {
5296 return NT_STATUS_NO_MEMORY;
5299 return NT_STATUS_OK;
5302 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
5304 session->disconnect_expired = true;
5307 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
5309 return session->smb1.session_id;
5312 void smb1cli_session_set_id(struct smbXcli_session *session,
5313 uint16_t session_id)
5315 session->smb1.session_id = session_id;
5318 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
5319 const DATA_BLOB _session_key)
5321 struct smbXcli_conn *conn = session->conn;
5322 uint8_t session_key[16];
5325 return NT_STATUS_INVALID_PARAMETER_MIX;
5328 if (session->smb1.application_key.length != 0) {
5330 * TODO: do not allow this...
5332 * return NT_STATUS_INVALID_PARAMETER_MIX;
5334 data_blob_clear_free(&session->smb1.application_key);
5335 session->smb1.protected_key = false;
5338 if (_session_key.length == 0) {
5339 return NT_STATUS_OK;
5342 ZERO_STRUCT(session_key);
5343 memcpy(session_key, _session_key.data,
5344 MIN(_session_key.length, sizeof(session_key)));
5346 session->smb1.application_key = data_blob_talloc(session,
5348 sizeof(session_key));
5349 ZERO_STRUCT(session_key);
5350 if (session->smb1.application_key.data == NULL) {
5351 return NT_STATUS_NO_MEMORY;
5354 session->smb1.protected_key = false;
5356 return NT_STATUS_OK;
5359 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
5361 if (session->smb1.protected_key) {
5362 /* already protected */
5363 return NT_STATUS_OK;
5366 if (session->smb1.application_key.length != 16) {
5367 return NT_STATUS_INVALID_PARAMETER_MIX;
5370 smb_key_derivation(session->smb1.application_key.data,
5371 session->smb1.application_key.length,
5372 session->smb1.application_key.data);
5374 session->smb1.protected_key = true;
5376 return NT_STATUS_OK;
5379 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
5381 struct smbXcli_conn *conn = session->conn;
5382 uint8_t security_mode = 0;
5385 return security_mode;
5388 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
5389 if (conn->mandatory_signing) {
5390 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
5393 return security_mode;
5396 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
5398 return session->smb2->session_id;
5401 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
5403 return session->smb2->session_flags;
5406 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
5407 uint64_t session_id,
5408 uint16_t session_flags)
5410 session->smb2->session_id = session_id;
5411 session->smb2->session_flags = session_flags;
5414 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
5416 session->smb2->channel_sequence += 1;
5419 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session *session,
5420 uint16_t channel_sequence)
5424 prev_cs = session->smb2->channel_sequence;
5425 session->smb2->channel_sequence = channel_sequence;
5430 void smb2cli_session_start_replay(struct smbXcli_session *session)
5432 session->smb2->replay_active = true;
5435 void smb2cli_session_stop_replay(struct smbXcli_session *session)
5437 session->smb2->replay_active = false;
5440 NTSTATUS smb2cli_session_update_preauth(struct smbXcli_session *session,
5441 const struct iovec *iov)
5443 struct hc_sha512state sctx;
5446 if (session->conn == NULL) {
5447 return NT_STATUS_INTERNAL_ERROR;
5450 if (session->conn->protocol < PROTOCOL_SMB3_10) {
5451 return NT_STATUS_OK;
5454 if (session->smb2_channel.signing_key.length != 0) {
5455 return NT_STATUS_OK;
5458 samba_SHA512_Init(&sctx);
5459 samba_SHA512_Update(&sctx, session->smb2_channel.preauth_sha512,
5460 sizeof(session->smb2_channel.preauth_sha512));
5461 for (i = 0; i < 3; i++) {
5462 samba_SHA512_Update(&sctx, iov[i].iov_base, iov[i].iov_len);
5464 samba_SHA512_Final(session->smb2_channel.preauth_sha512, &sctx);
5466 return NT_STATUS_OK;
5469 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
5470 const DATA_BLOB _session_key,
5471 const struct iovec *recv_iov)
5473 struct smbXcli_conn *conn = session->conn;
5474 uint16_t no_sign_flags;
5475 uint8_t session_key[16];
5476 bool check_signature = true;
5479 struct _derivation {
5484 struct _derivation signing;
5485 struct _derivation encryption;
5486 struct _derivation decryption;
5487 struct _derivation application;
5489 size_t nonce_size = 0;
5492 return NT_STATUS_INVALID_PARAMETER_MIX;
5495 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
5496 return NT_STATUS_INVALID_PARAMETER_MIX;
5499 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
5501 if (session->smb2->session_flags & no_sign_flags) {
5502 session->smb2->should_sign = false;
5503 return NT_STATUS_OK;
5506 if (session->smb2->signing_key.length != 0) {
5507 return NT_STATUS_INVALID_PARAMETER_MIX;
5510 if (conn->protocol >= PROTOCOL_SMB3_10) {
5511 struct _derivation *d;
5514 p = data_blob_const(session->smb2_channel.preauth_sha512,
5515 sizeof(session->smb2_channel.preauth_sha512));
5517 d = &derivation.signing;
5518 d->label = data_blob_string_const_null("SMBSigningKey");
5521 d = &derivation.encryption;
5522 d->label = data_blob_string_const_null("SMBC2SCipherKey");
5525 d = &derivation.decryption;
5526 d->label = data_blob_string_const_null("SMBS2CCipherKey");
5529 d = &derivation.application;
5530 d->label = data_blob_string_const_null("SMBAppKey");
5533 } else if (conn->protocol >= PROTOCOL_SMB2_24) {
5534 struct _derivation *d;
5536 d = &derivation.signing;
5537 d->label = data_blob_string_const_null("SMB2AESCMAC");
5538 d->context = data_blob_string_const_null("SmbSign");
5540 d = &derivation.encryption;
5541 d->label = data_blob_string_const_null("SMB2AESCCM");
5542 d->context = data_blob_string_const_null("ServerIn ");
5544 d = &derivation.decryption;
5545 d->label = data_blob_string_const_null("SMB2AESCCM");
5546 d->context = data_blob_string_const_null("ServerOut");
5548 d = &derivation.application;
5549 d->label = data_blob_string_const_null("SMB2APP");
5550 d->context = data_blob_string_const_null("SmbRpc");
5553 ZERO_STRUCT(session_key);
5554 memcpy(session_key, _session_key.data,
5555 MIN(_session_key.length, sizeof(session_key)));
5557 session->smb2->signing_key = data_blob_talloc(session,
5559 sizeof(session_key));
5560 if (session->smb2->signing_key.data == NULL) {
5561 ZERO_STRUCT(session_key);
5562 return NT_STATUS_NO_MEMORY;
5565 if (conn->protocol >= PROTOCOL_SMB2_24) {
5566 struct _derivation *d = &derivation.signing;
5568 smb2_key_derivation(session_key, sizeof(session_key),
5569 d->label.data, d->label.length,
5570 d->context.data, d->context.length,
5571 session->smb2->signing_key.data);
5574 session->smb2->encryption_key = data_blob_dup_talloc(session,
5575 session->smb2->signing_key);
5576 if (session->smb2->encryption_key.data == NULL) {
5577 ZERO_STRUCT(session_key);
5578 return NT_STATUS_NO_MEMORY;
5581 if (conn->protocol >= PROTOCOL_SMB2_24) {
5582 struct _derivation *d = &derivation.encryption;
5584 smb2_key_derivation(session_key, sizeof(session_key),
5585 d->label.data, d->label.length,
5586 d->context.data, d->context.length,
5587 session->smb2->encryption_key.data);
5590 session->smb2->decryption_key = data_blob_dup_talloc(session,
5591 session->smb2->signing_key);
5592 if (session->smb2->decryption_key.data == NULL) {
5593 ZERO_STRUCT(session_key);
5594 return NT_STATUS_NO_MEMORY;
5597 if (conn->protocol >= PROTOCOL_SMB2_24) {
5598 struct _derivation *d = &derivation.decryption;
5600 smb2_key_derivation(session_key, sizeof(session_key),
5601 d->label.data, d->label.length,
5602 d->context.data, d->context.length,
5603 session->smb2->decryption_key.data);
5606 session->smb2->application_key = data_blob_dup_talloc(session,
5607 session->smb2->signing_key);
5608 if (session->smb2->application_key.data == NULL) {
5609 ZERO_STRUCT(session_key);
5610 return NT_STATUS_NO_MEMORY;
5613 if (conn->protocol >= PROTOCOL_SMB2_24) {
5614 struct _derivation *d = &derivation.application;
5616 smb2_key_derivation(session_key, sizeof(session_key),
5617 d->label.data, d->label.length,
5618 d->context.data, d->context.length,
5619 session->smb2->application_key.data);
5621 ZERO_STRUCT(session_key);
5623 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
5624 session->smb2->signing_key);
5625 if (session->smb2_channel.signing_key.data == NULL) {
5626 return NT_STATUS_NO_MEMORY;
5629 check_signature = conn->mandatory_signing;
5631 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
5632 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
5634 * Sadly some vendors don't sign the
5635 * final SMB2 session setup response
5637 * At least Windows and Samba are always doing this
5638 * if there's a session key available.
5640 * We only check the signature if it's mandatory
5641 * or SMB2_HDR_FLAG_SIGNED is provided.
5643 check_signature = true;
5646 if (conn->protocol >= PROTOCOL_SMB3_10) {
5647 check_signature = true;
5650 if (check_signature) {
5651 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5652 session->conn->protocol,
5654 if (!NT_STATUS_IS_OK(status)) {
5659 session->smb2->should_sign = false;
5660 session->smb2->should_encrypt = false;
5662 if (conn->desire_signing) {
5663 session->smb2->should_sign = true;
5666 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
5667 session->smb2->should_sign = true;
5670 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
5671 session->smb2->should_encrypt = true;
5674 if (conn->protocol < PROTOCOL_SMB2_24) {
5675 session->smb2->should_encrypt = false;
5678 if (conn->smb2.server.cipher == 0) {
5679 session->smb2->should_encrypt = false;
5683 * CCM and GCM algorithms must never have their
5684 * nonce wrap, or the security of the whole
5685 * communication and the keys is destroyed.
5686 * We must drop the connection once we have
5687 * transfered too much data.
5689 * NOTE: We assume nonces greater than 8 bytes.
5691 generate_random_buffer((uint8_t *)&session->smb2->nonce_high_random,
5692 sizeof(session->smb2->nonce_high_random));
5693 switch (conn->smb2.server.cipher) {
5694 case SMB2_ENCRYPTION_AES128_CCM:
5695 nonce_size = AES_CCM_128_NONCE_SIZE;
5697 case SMB2_ENCRYPTION_AES128_GCM:
5698 nonce_size = AES_GCM_128_IV_SIZE;
5704 session->smb2->nonce_high_max = SMB2_NONCE_HIGH_MAX(nonce_size);
5705 session->smb2->nonce_high = 0;
5706 session->smb2->nonce_low = 0;
5708 return NT_STATUS_OK;
5711 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
5712 struct smbXcli_session *session1,
5713 struct smbXcli_conn *conn,
5714 struct smbXcli_session **_session2)
5716 struct smbXcli_session *session2;
5718 if (session1->smb2->signing_key.length == 0) {
5719 return NT_STATUS_INVALID_PARAMETER_MIX;
5723 return NT_STATUS_INVALID_PARAMETER_MIX;
5726 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
5727 if (session2 == NULL) {
5728 return NT_STATUS_NO_MEMORY;
5730 session2->smb2 = talloc_reference(session2, session1->smb2);
5731 if (session2->smb2 == NULL) {
5732 talloc_free(session2);
5733 return NT_STATUS_NO_MEMORY;
5736 talloc_set_destructor(session2, smbXcli_session_destructor);
5737 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
5738 session2->conn = conn;
5740 memcpy(session2->smb2_channel.preauth_sha512,
5741 conn->smb2.preauth_sha512,
5742 sizeof(session2->smb2_channel.preauth_sha512));
5744 *_session2 = session2;
5745 return NT_STATUS_OK;
5748 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
5749 const DATA_BLOB _channel_key,
5750 const struct iovec *recv_iov)
5752 struct smbXcli_conn *conn = session->conn;
5753 uint8_t channel_key[16];
5755 struct _derivation {
5760 struct _derivation signing;
5764 return NT_STATUS_INVALID_PARAMETER_MIX;
5767 if (session->smb2_channel.signing_key.length != 0) {
5768 return NT_STATUS_INVALID_PARAMETER_MIX;
5771 if (conn->protocol >= PROTOCOL_SMB3_10) {
5772 struct _derivation *d;
5775 p = data_blob_const(session->smb2_channel.preauth_sha512,
5776 sizeof(session->smb2_channel.preauth_sha512));
5778 d = &derivation.signing;
5779 d->label = data_blob_string_const_null("SMBSigningKey");
5781 } else if (conn->protocol >= PROTOCOL_SMB2_24) {
5782 struct _derivation *d;
5784 d = &derivation.signing;
5785 d->label = data_blob_string_const_null("SMB2AESCMAC");
5786 d->context = data_blob_string_const_null("SmbSign");
5789 ZERO_STRUCT(channel_key);
5790 memcpy(channel_key, _channel_key.data,
5791 MIN(_channel_key.length, sizeof(channel_key)));
5793 session->smb2_channel.signing_key = data_blob_talloc(session,
5795 sizeof(channel_key));
5796 if (session->smb2_channel.signing_key.data == NULL) {
5797 ZERO_STRUCT(channel_key);
5798 return NT_STATUS_NO_MEMORY;
5801 if (conn->protocol >= PROTOCOL_SMB2_24) {
5802 struct _derivation *d = &derivation.signing;
5804 smb2_key_derivation(channel_key, sizeof(channel_key),
5805 d->label.data, d->label.length,
5806 d->context.data, d->context.length,
5807 session->smb2_channel.signing_key.data);
5809 ZERO_STRUCT(channel_key);
5811 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5812 session->conn->protocol,
5814 if (!NT_STATUS_IS_OK(status)) {
5818 return NT_STATUS_OK;
5821 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
5823 if (session->smb2->should_encrypt) {
5824 return NT_STATUS_OK;
5827 if (session->conn->protocol < PROTOCOL_SMB2_24) {
5828 return NT_STATUS_NOT_SUPPORTED;
5831 if (session->conn->smb2.server.cipher == 0) {
5832 return NT_STATUS_NOT_SUPPORTED;
5835 if (session->smb2->signing_key.data == NULL) {
5836 return NT_STATUS_NOT_SUPPORTED;
5838 session->smb2->should_encrypt = true;
5839 return NT_STATUS_OK;
5842 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
5844 struct smbXcli_tcon *tcon;
5846 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
5854 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
5855 uint32_t fs_attributes)
5857 tcon->fs_attributes = fs_attributes;
5860 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
5862 return tcon->fs_attributes;
5865 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
5871 if (tcon->is_smb1) {
5872 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
5879 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
5886 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
5888 return tcon->smb1.tcon_id;
5891 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
5893 tcon->is_smb1 = true;
5894 tcon->smb1.tcon_id = tcon_id;
5897 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
5899 uint16_t optional_support,
5900 uint32_t maximal_access,
5901 uint32_t guest_maximal_access,
5902 const char *service,
5903 const char *fs_type)
5905 tcon->is_smb1 = true;
5906 tcon->fs_attributes = 0;
5907 tcon->smb1.tcon_id = tcon_id;
5908 tcon->smb1.optional_support = optional_support;
5909 tcon->smb1.maximal_access = maximal_access;
5910 tcon->smb1.guest_maximal_access = guest_maximal_access;
5912 TALLOC_FREE(tcon->smb1.service);
5913 tcon->smb1.service = talloc_strdup(tcon, service);
5914 if (service != NULL && tcon->smb1.service == NULL) {
5918 TALLOC_FREE(tcon->smb1.fs_type);
5919 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5920 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5927 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5929 return tcon->smb2.tcon_id;
5932 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5934 return tcon->smb2.capabilities;
5937 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5938 struct smbXcli_session *session,
5942 uint32_t capabilities,
5943 uint32_t maximal_access)
5945 tcon->is_smb1 = false;
5946 tcon->fs_attributes = 0;
5947 tcon->smb2.tcon_id = tcon_id;
5948 tcon->smb2.type = type;
5949 tcon->smb2.flags = flags;
5950 tcon->smb2.capabilities = capabilities;
5951 tcon->smb2.maximal_access = maximal_access;
5953 tcon->smb2.should_sign = false;
5954 tcon->smb2.should_encrypt = false;
5956 if (session == NULL) {
5960 tcon->smb2.should_sign = session->smb2->should_sign;
5961 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5963 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5964 tcon->smb2.should_encrypt = true;
5968 void smb2cli_tcon_should_sign(struct smbXcli_tcon *tcon,
5971 tcon->smb2.should_sign = should_sign;
5974 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon *tcon)
5976 if (tcon->smb2.should_encrypt) {
5980 return tcon->smb2.should_sign;
5983 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon *tcon,
5984 bool should_encrypt)
5986 tcon->smb2.should_encrypt = should_encrypt;
5989 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
5991 return tcon->smb2.should_encrypt;