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;
1001 tevent_queue_stop(conn->outgoing);
1003 if (conn->read_fd != -1) {
1004 close(conn->read_fd);
1006 if (conn->write_fd != -1) {
1007 close(conn->write_fd);
1010 conn->write_fd = -1;
1012 session = conn->sessions;
1013 if (talloc_array_length(conn->pending) == 0) {
1015 * if we do not have pending requests
1016 * there is no need to update the channel_sequence
1020 for (; session; session = session->next) {
1021 smb2cli_session_increment_channel_sequence(session);
1025 * Cancel all pending requests. We do not do a for-loop walking
1026 * conn->pending because that array changes in
1027 * smbXcli_req_unset_pending.
1029 while (talloc_array_length(conn->pending) > 0) {
1030 struct tevent_req *req;
1031 struct smbXcli_req_state *state;
1032 struct tevent_req **chain;
1036 req = conn->pending[0];
1037 state = tevent_req_data(req, struct smbXcli_req_state);
1039 if (state->smb1.chained_requests == NULL) {
1041 * We're dead. No point waiting for trans2
1044 state->smb1.mid = 0;
1046 smbXcli_req_unset_pending(req);
1048 if (NT_STATUS_IS_OK(status)) {
1049 /* do not notify the callers */
1054 * we need to defer the callback, because we may notify
1055 * more then one caller.
1057 tevent_req_defer_callback(req, state->ev);
1058 tevent_req_nterror(req, status);
1062 chain = talloc_move(conn, &state->smb1.chained_requests);
1063 num_chained = talloc_array_length(chain);
1065 for (i=0; i<num_chained; i++) {
1067 state = tevent_req_data(req, struct smbXcli_req_state);
1070 * We're dead. No point waiting for trans2
1073 state->smb1.mid = 0;
1075 smbXcli_req_unset_pending(req);
1077 if (NT_STATUS_IS_OK(status)) {
1078 /* do not notify the callers */
1083 * we need to defer the callback, because we may notify
1084 * more than one caller.
1086 tevent_req_defer_callback(req, state->ev);
1087 tevent_req_nterror(req, status);
1094 * Fetch a smb request's mid. Only valid after the request has been sent by
1095 * smb1cli_req_send().
1097 uint16_t smb1cli_req_mid(struct tevent_req *req)
1099 struct smbXcli_req_state *state =
1100 tevent_req_data(req,
1101 struct smbXcli_req_state);
1103 if (state->smb1.mid != 0) {
1104 return state->smb1.mid;
1107 return SVAL(state->smb1.hdr, HDR_MID);
1110 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1112 struct smbXcli_req_state *state =
1113 tevent_req_data(req,
1114 struct smbXcli_req_state);
1116 state->smb1.mid = mid;
1119 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1121 struct smbXcli_req_state *state =
1122 tevent_req_data(req,
1123 struct smbXcli_req_state);
1125 return state->smb1.seqnum;
1128 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1130 struct smbXcli_req_state *state =
1131 tevent_req_data(req,
1132 struct smbXcli_req_state);
1134 state->smb1.seqnum = seqnum;
1137 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1139 ssize_t ret = iov_buflen(iov, count);
1141 /* Ignore the overflow case for now ... */
1145 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1146 const struct iovec *iov,
1152 buflen = iov_buflen(iov, count);
1157 buf = talloc_array(mem_ctx, uint8_t, buflen);
1162 iov_buf(iov, count, buf, buflen);
1167 static void smb1cli_req_flags(enum protocol_types protocol,
1168 uint32_t smb1_capabilities,
1169 uint8_t smb_command,
1170 uint8_t additional_flags,
1171 uint8_t clear_flags,
1173 uint16_t additional_flags2,
1174 uint16_t clear_flags2,
1178 uint16_t flags2 = 0;
1180 if (protocol >= PROTOCOL_LANMAN1) {
1181 flags |= FLAG_CASELESS_PATHNAMES;
1182 flags |= FLAG_CANONICAL_PATHNAMES;
1185 if (protocol >= PROTOCOL_LANMAN2) {
1186 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1187 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1190 if (protocol >= PROTOCOL_NT1) {
1191 flags2 |= FLAGS2_IS_LONG_NAME;
1193 if (smb1_capabilities & CAP_UNICODE) {
1194 flags2 |= FLAGS2_UNICODE_STRINGS;
1196 if (smb1_capabilities & CAP_STATUS32) {
1197 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1199 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1200 flags2 |= FLAGS2_EXTENDED_SECURITY;
1204 flags |= additional_flags;
1205 flags &= ~clear_flags;
1206 flags2 |= additional_flags2;
1207 flags2 &= ~clear_flags2;
1213 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1215 static bool smb1cli_req_cancel(struct tevent_req *req)
1217 struct smbXcli_req_state *state =
1218 tevent_req_data(req,
1219 struct smbXcli_req_state);
1224 struct tevent_req *subreq;
1227 flags = CVAL(state->smb1.hdr, HDR_FLG);
1228 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1229 pid = SVAL(state->smb1.hdr, HDR_PID);
1230 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1231 mid = SVAL(state->smb1.hdr, HDR_MID);
1233 subreq = smb1cli_req_create(state, state->ev,
1243 0, NULL); /* bytes */
1244 if (subreq == NULL) {
1247 smb1cli_req_set_mid(subreq, mid);
1249 status = smb1cli_req_chain_submit(&subreq, 1);
1250 if (!NT_STATUS_IS_OK(status)) {
1251 TALLOC_FREE(subreq);
1254 smb1cli_req_set_mid(subreq, 0);
1256 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1261 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1263 /* we do not care about the result */
1264 TALLOC_FREE(subreq);
1267 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1268 struct tevent_context *ev,
1269 struct smbXcli_conn *conn,
1270 uint8_t smb_command,
1271 uint8_t additional_flags,
1272 uint8_t clear_flags,
1273 uint16_t additional_flags2,
1274 uint16_t clear_flags2,
1275 uint32_t timeout_msec,
1277 struct smbXcli_tcon *tcon,
1278 struct smbXcli_session *session,
1279 uint8_t wct, uint16_t *vwv,
1281 struct iovec *bytes_iov)
1283 struct tevent_req *req;
1284 struct smbXcli_req_state *state;
1286 uint16_t flags2 = 0;
1291 if (iov_count > MAX_SMB_IOV) {
1293 * Should not happen :-)
1298 req = tevent_req_create(mem_ctx, &state,
1299 struct smbXcli_req_state);
1305 state->session = session;
1309 uid = session->smb1.session_id;
1313 tid = tcon->smb1.tcon_id;
1315 if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
1316 clear_flags |= FLAG_CASELESS_PATHNAMES;
1318 /* Default setting, case insensitive. */
1319 additional_flags |= FLAG_CASELESS_PATHNAMES;
1322 if (smbXcli_conn_dfs_supported(conn) &&
1323 smbXcli_tcon_is_dfs_share(tcon))
1325 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
1329 state->smb1.recv_cmd = 0xFF;
1330 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1331 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1332 if (state->smb1.recv_iov == NULL) {
1337 smb1cli_req_flags(conn->protocol,
1338 conn->smb1.capabilities,
1347 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1348 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1349 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1350 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1351 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1352 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1353 SSVAL(state->smb1.hdr, HDR_TID, tid);
1354 SSVAL(state->smb1.hdr, HDR_PID, pid);
1355 SSVAL(state->smb1.hdr, HDR_UID, uid);
1356 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1357 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1359 state->smb1.vwv = vwv;
1361 num_bytes = iov_buflen(bytes_iov, iov_count);
1362 if (num_bytes == -1) {
1364 * I'd love to add a check for num_bytes<=UINT16_MAX here, but
1365 * the smbclient->samba connections can lie and transfer more.
1371 SSVAL(state->smb1.bytecount_buf, 0, num_bytes);
1373 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1374 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1375 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1376 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1377 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1378 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1379 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1380 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1382 if (iov_count != 0) {
1383 memcpy(&state->smb1.iov[4], bytes_iov,
1384 iov_count * sizeof(*bytes_iov));
1386 state->smb1.iov_count = iov_count + 4;
1388 if (timeout_msec > 0) {
1389 struct timeval endtime;
1391 endtime = timeval_current_ofs_msec(timeout_msec);
1392 if (!tevent_req_set_endtime(req, ev, endtime)) {
1397 switch (smb_command) {
1401 state->one_way = true;
1404 state->one_way = true;
1405 state->smb1.one_way_seqnum = true;
1409 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1410 state->one_way = true;
1418 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1419 struct iovec *iov, int iov_count,
1421 bool one_way_seqnum)
1423 TALLOC_CTX *frame = NULL;
1427 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1428 * iovec directly. MD5Update would do that just fine.
1431 if (iov_count < 4) {
1432 return NT_STATUS_INVALID_PARAMETER_MIX;
1434 if (iov[0].iov_len != NBT_HDR_SIZE) {
1435 return NT_STATUS_INVALID_PARAMETER_MIX;
1437 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1438 return NT_STATUS_INVALID_PARAMETER_MIX;
1440 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1441 return NT_STATUS_INVALID_PARAMETER_MIX;
1443 if (iov[3].iov_len != sizeof(uint16_t)) {
1444 return NT_STATUS_INVALID_PARAMETER_MIX;
1447 frame = talloc_stackframe();
1449 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1451 return NT_STATUS_NO_MEMORY;
1454 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1456 smb_signing_sign_pdu(conn->smb1.signing,
1457 buf, talloc_get_size(buf),
1459 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1462 return NT_STATUS_OK;
1465 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1466 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1467 TALLOC_CTX *tmp_mem,
1470 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1471 struct smbXcli_req_state *state,
1472 struct iovec *iov, int iov_count)
1474 struct tevent_req *subreq;
1480 if (!smbXcli_conn_is_connected(state->conn)) {
1481 return NT_STATUS_CONNECTION_DISCONNECTED;
1484 if (state->conn->protocol > PROTOCOL_NT1) {
1485 return NT_STATUS_REVISION_MISMATCH;
1488 if (iov_count < 4) {
1489 return NT_STATUS_INVALID_PARAMETER_MIX;
1491 if (iov[0].iov_len != NBT_HDR_SIZE) {
1492 return NT_STATUS_INVALID_PARAMETER_MIX;
1494 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1495 return NT_STATUS_INVALID_PARAMETER_MIX;
1497 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1498 return NT_STATUS_INVALID_PARAMETER_MIX;
1500 if (iov[3].iov_len != sizeof(uint16_t)) {
1501 return NT_STATUS_INVALID_PARAMETER_MIX;
1504 cmd = CVAL(iov[1].iov_base, HDR_COM);
1505 if (cmd == SMBreadBraw) {
1506 if (smbXcli_conn_has_async_calls(state->conn)) {
1507 return NT_STATUS_INVALID_PARAMETER_MIX;
1509 state->conn->smb1.read_braw_req = req;
1512 if (state->smb1.mid != 0) {
1513 mid = state->smb1.mid;
1515 mid = smb1cli_alloc_mid(state->conn);
1517 SSVAL(iov[1].iov_base, HDR_MID, mid);
1519 nbtlen = iov_buflen(&iov[1], iov_count-1);
1520 if ((nbtlen == -1) || (nbtlen > 0x1FFFF)) {
1521 return NT_STATUS_INVALID_PARAMETER_MIX;
1524 _smb_setlen_nbt(iov[0].iov_base, nbtlen);
1526 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1527 &state->smb1.seqnum,
1528 state->smb1.one_way_seqnum);
1530 if (!NT_STATUS_IS_OK(status)) {
1535 * If we supported multiple encrytion contexts
1536 * here we'd look up based on tid.
1538 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1539 char *buf, *enc_buf;
1541 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1543 return NT_STATUS_NO_MEMORY;
1545 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1546 (char *)buf, &enc_buf);
1548 if (!NT_STATUS_IS_OK(status)) {
1549 DEBUG(0, ("Error in encrypting client message: %s\n",
1550 nt_errstr(status)));
1553 buf = (char *)talloc_memdup(state, enc_buf,
1554 smb_len_nbt(enc_buf)+4);
1557 return NT_STATUS_NO_MEMORY;
1559 iov[0].iov_base = (void *)buf;
1560 iov[0].iov_len = talloc_get_size(buf);
1564 if (state->conn->dispatch_incoming == NULL) {
1565 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1568 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1570 subreq = writev_send(state, state->ev, state->conn->outgoing,
1571 state->conn->write_fd, false, iov, iov_count);
1572 if (subreq == NULL) {
1573 return NT_STATUS_NO_MEMORY;
1575 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1576 return NT_STATUS_OK;
1579 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1580 struct tevent_context *ev,
1581 struct smbXcli_conn *conn,
1582 uint8_t smb_command,
1583 uint8_t additional_flags,
1584 uint8_t clear_flags,
1585 uint16_t additional_flags2,
1586 uint16_t clear_flags2,
1587 uint32_t timeout_msec,
1589 struct smbXcli_tcon *tcon,
1590 struct smbXcli_session *session,
1591 uint8_t wct, uint16_t *vwv,
1593 const uint8_t *bytes)
1595 struct tevent_req *req;
1599 iov.iov_base = discard_const_p(void, bytes);
1600 iov.iov_len = num_bytes;
1602 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1603 additional_flags, clear_flags,
1604 additional_flags2, clear_flags2,
1611 if (!tevent_req_is_in_progress(req)) {
1612 return tevent_req_post(req, ev);
1614 status = smb1cli_req_chain_submit(&req, 1);
1615 if (tevent_req_nterror(req, status)) {
1616 return tevent_req_post(req, ev);
1621 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1623 struct tevent_req *req =
1624 tevent_req_callback_data(subreq,
1626 struct smbXcli_req_state *state =
1627 tevent_req_data(req,
1628 struct smbXcli_req_state);
1632 nwritten = writev_recv(subreq, &err);
1633 TALLOC_FREE(subreq);
1634 if (nwritten == -1) {
1635 NTSTATUS status = map_nt_error_from_unix_common(err);
1636 smbXcli_conn_disconnect(state->conn, status);
1637 tevent_req_nterror(req, status);
1641 if (state->one_way) {
1642 state->inbuf = NULL;
1643 tevent_req_done(req);
1647 if (!smbXcli_req_set_pending(req)) {
1648 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1653 static void smbXcli_conn_received(struct tevent_req *subreq)
1655 struct smbXcli_conn *conn =
1656 tevent_req_callback_data(subreq,
1657 struct smbXcli_conn);
1658 TALLOC_CTX *frame = talloc_stackframe();
1664 if (subreq != conn->read_smb_req) {
1665 DEBUG(1, ("Internal error: cli_smb_received called with "
1666 "unexpected subreq\n"));
1667 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1671 conn->read_smb_req = NULL;
1673 received = read_smb_recv(subreq, frame, &inbuf, &err);
1674 TALLOC_FREE(subreq);
1675 if (received == -1) {
1676 status = map_nt_error_from_unix_common(err);
1677 smbXcli_conn_disconnect(conn, status);
1682 status = conn->dispatch_incoming(conn, frame, inbuf);
1684 if (NT_STATUS_IS_OK(status)) {
1686 * We should not do any more processing
1687 * as the dispatch function called
1688 * tevent_req_done().
1693 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1695 * We got an error, so notify all pending requests
1697 smbXcli_conn_disconnect(conn, status);
1702 * We got NT_STATUS_RETRY, so we may ask for a
1703 * next incoming pdu.
1705 if (!smbXcli_conn_receive_next(conn)) {
1706 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1710 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1711 struct iovec **piov, int *pnum_iov)
1722 size_t min_size = MIN_SMB_SIZE;
1724 buflen = smb_len_tcp(buf);
1727 hdr = buf + NBT_HDR_SIZE;
1729 status = smb1cli_pull_raw_error(hdr);
1730 if (NT_STATUS_IS_ERR(status)) {
1732 * This is an ugly hack to support OS/2
1733 * which skips the byte_count in the DATA block
1734 * on some error responses.
1738 min_size -= sizeof(uint16_t);
1741 if (buflen < min_size) {
1742 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1746 * This returns iovec elements in the following order:
1761 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1763 return NT_STATUS_NO_MEMORY;
1765 iov[0].iov_base = hdr;
1766 iov[0].iov_len = HDR_WCT;
1769 cmd = CVAL(hdr, HDR_COM);
1773 size_t len = buflen - taken;
1775 struct iovec *iov_tmp;
1782 * we need at least WCT
1784 needed = sizeof(uint8_t);
1786 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1787 __location__, (int)len, (int)needed));
1792 * Now we check if the specified words are there
1794 wct = CVAL(hdr, wct_ofs);
1795 needed += wct * sizeof(uint16_t);
1797 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1798 __location__, (int)len, (int)needed));
1802 if ((num_iov == 1) &&
1804 NT_STATUS_IS_ERR(status))
1807 * This is an ugly hack to support OS/2
1808 * which skips the byte_count in the DATA block
1809 * on some error responses.
1813 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1815 if (iov_tmp == NULL) {
1817 return NT_STATUS_NO_MEMORY;
1820 cur = &iov[num_iov];
1824 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1826 cur[1].iov_base = cur[0].iov_base;
1833 * we need at least BCC
1835 needed += sizeof(uint16_t);
1837 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1838 __location__, (int)len, (int)needed));
1843 * Now we check if the specified bytes are there
1845 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1846 bcc = SVAL(hdr, bcc_ofs);
1847 needed += bcc * sizeof(uint8_t);
1849 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1850 __location__, (int)len, (int)needed));
1855 * we allocate 2 iovec structures for words and bytes
1857 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1859 if (iov_tmp == NULL) {
1861 return NT_STATUS_NO_MEMORY;
1864 cur = &iov[num_iov];
1867 cur[0].iov_len = wct * sizeof(uint16_t);
1868 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1869 cur[1].iov_len = bcc * sizeof(uint8_t);
1870 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1874 if (!smb1cli_is_andx_req(cmd)) {
1876 * If the current command does not have AndX chanining
1882 if (wct == 0 && bcc == 0) {
1884 * An empty response also ends the chain,
1885 * most likely with an error.
1891 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1892 __location__, (int)wct, (int)cmd));
1895 cmd = CVAL(cur[0].iov_base, 0);
1898 * If it is the end of the chain we are also done.
1902 wct_ofs = SVAL(cur[0].iov_base, 2);
1904 if (wct_ofs < taken) {
1905 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1907 if (wct_ofs > buflen) {
1908 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1912 * we consumed everything up to the start of the next
1918 remaining = buflen - taken;
1920 if (remaining > 0 && num_iov >= 3) {
1922 * The last DATA block gets the remaining
1923 * bytes, this is needed to support
1924 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1926 iov[num_iov-1].iov_len += remaining;
1930 *pnum_iov = num_iov;
1931 return NT_STATUS_OK;
1935 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1938 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1939 TALLOC_CTX *tmp_mem,
1942 struct tevent_req *req;
1943 struct smbXcli_req_state *state;
1950 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1951 size_t len = smb_len_tcp(inbuf);
1952 struct iovec *iov = NULL;
1954 struct tevent_req **chain = NULL;
1955 size_t num_chained = 0;
1956 size_t num_responses = 0;
1958 if (conn->smb1.read_braw_req != NULL) {
1959 req = conn->smb1.read_braw_req;
1960 conn->smb1.read_braw_req = NULL;
1961 state = tevent_req_data(req, struct smbXcli_req_state);
1963 smbXcli_req_unset_pending(req);
1965 if (state->smb1.recv_iov == NULL) {
1967 * For requests with more than
1968 * one response, we have to readd the
1971 state->smb1.recv_iov = talloc_zero_array(state,
1974 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1975 return NT_STATUS_OK;
1979 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1980 state->smb1.recv_iov[0].iov_len = len;
1981 ZERO_STRUCT(state->smb1.recv_iov[1]);
1982 ZERO_STRUCT(state->smb1.recv_iov[2]);
1984 state->smb1.recv_cmd = SMBreadBraw;
1985 state->smb1.recv_status = NT_STATUS_OK;
1986 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1988 tevent_req_done(req);
1989 return NT_STATUS_OK;
1992 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1993 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1994 DEBUG(10, ("Got non-SMB PDU\n"));
1995 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1999 * If we supported multiple encrytion contexts
2000 * here we'd look up based on tid.
2002 if (common_encryption_on(conn->smb1.trans_enc)
2003 && (CVAL(inbuf, 0) == 0)) {
2004 uint16_t enc_ctx_num;
2006 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
2007 if (!NT_STATUS_IS_OK(status)) {
2008 DEBUG(10, ("get_enc_ctx_num returned %s\n",
2009 nt_errstr(status)));
2013 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
2014 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
2016 conn->smb1.trans_enc->enc_ctx_num));
2017 return NT_STATUS_INVALID_HANDLE;
2020 status = common_decrypt_buffer(conn->smb1.trans_enc,
2022 if (!NT_STATUS_IS_OK(status)) {
2023 DEBUG(10, ("common_decrypt_buffer returned %s\n",
2024 nt_errstr(status)));
2027 inhdr = inbuf + NBT_HDR_SIZE;
2028 len = smb_len_nbt(inbuf);
2031 mid = SVAL(inhdr, HDR_MID);
2032 num_pending = talloc_array_length(conn->pending);
2034 for (i=0; i<num_pending; i++) {
2035 if (mid == smb1cli_req_mid(conn->pending[i])) {
2039 if (i == num_pending) {
2040 /* Dump unexpected reply */
2041 return NT_STATUS_RETRY;
2044 oplock_break = false;
2046 if (mid == 0xffff) {
2048 * Paranoia checks that this is really an oplock break request.
2050 oplock_break = (len == 51); /* hdr + 8 words */
2051 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
2052 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
2053 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
2054 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
2056 if (!oplock_break) {
2057 /* Dump unexpected reply */
2058 return NT_STATUS_RETRY;
2062 req = conn->pending[i];
2063 state = tevent_req_data(req, struct smbXcli_req_state);
2065 if (!oplock_break /* oplock breaks are not signed */
2066 && !smb_signing_check_pdu(conn->smb1.signing,
2067 inhdr, len, state->smb1.seqnum+1)) {
2068 DEBUG(10, ("cli_check_sign_mac failed\n"));
2069 return NT_STATUS_ACCESS_DENIED;
2072 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2074 if (!NT_STATUS_IS_OK(status)) {
2075 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2076 nt_errstr(status)));
2080 cmd = CVAL(inhdr, HDR_COM);
2081 status = smb1cli_pull_raw_error(inhdr);
2083 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2084 (state->session != NULL) && state->session->disconnect_expired)
2087 * this should be a short term hack
2088 * until the upper layers have implemented
2089 * re-authentication.
2094 if (state->smb1.chained_requests == NULL) {
2096 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2099 smbXcli_req_unset_pending(req);
2101 if (state->smb1.recv_iov == NULL) {
2103 * For requests with more than
2104 * one response, we have to readd the
2107 state->smb1.recv_iov = talloc_zero_array(state,
2110 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2111 return NT_STATUS_OK;
2115 state->smb1.recv_cmd = cmd;
2116 state->smb1.recv_status = status;
2117 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2119 state->smb1.recv_iov[0] = iov[0];
2120 state->smb1.recv_iov[1] = iov[1];
2121 state->smb1.recv_iov[2] = iov[2];
2123 if (talloc_array_length(conn->pending) == 0) {
2124 tevent_req_done(req);
2125 return NT_STATUS_OK;
2128 tevent_req_defer_callback(req, state->ev);
2129 tevent_req_done(req);
2130 return NT_STATUS_RETRY;
2133 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2134 num_chained = talloc_array_length(chain);
2135 num_responses = (num_iov - 1)/2;
2137 if (num_responses > num_chained) {
2138 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2141 for (i=0; i<num_chained; i++) {
2142 size_t iov_idx = 1 + (i*2);
2143 struct iovec *cur = &iov[iov_idx];
2147 state = tevent_req_data(req, struct smbXcli_req_state);
2149 smbXcli_req_unset_pending(req);
2152 * as we finish multiple requests here
2153 * we need to defer the callbacks as
2154 * they could destroy our current stack state.
2156 tevent_req_defer_callback(req, state->ev);
2158 if (i >= num_responses) {
2159 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2163 if (state->smb1.recv_iov == NULL) {
2165 * For requests with more than
2166 * one response, we have to readd the
2169 state->smb1.recv_iov = talloc_zero_array(state,
2172 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2177 state->smb1.recv_cmd = cmd;
2179 if (i == (num_responses - 1)) {
2181 * The last request in the chain gets the status
2183 state->smb1.recv_status = status;
2185 cmd = CVAL(cur[0].iov_base, 0);
2186 state->smb1.recv_status = NT_STATUS_OK;
2189 state->inbuf = inbuf;
2192 * Note: here we use talloc_reference() in a way
2193 * that does not expose it to the caller.
2195 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2196 if (tevent_req_nomem(inbuf_ref, req)) {
2200 /* copy the related buffers */
2201 state->smb1.recv_iov[0] = iov[0];
2202 state->smb1.recv_iov[1] = cur[0];
2203 state->smb1.recv_iov[2] = cur[1];
2205 tevent_req_done(req);
2208 return NT_STATUS_RETRY;
2211 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2212 TALLOC_CTX *mem_ctx,
2213 struct iovec **piov,
2217 uint32_t *pvwv_offset,
2218 uint32_t *pnum_bytes,
2220 uint32_t *pbytes_offset,
2222 const struct smb1cli_req_expected_response *expected,
2223 size_t num_expected)
2225 struct smbXcli_req_state *state =
2226 tevent_req_data(req,
2227 struct smbXcli_req_state);
2228 NTSTATUS status = NT_STATUS_OK;
2229 struct iovec *recv_iov = NULL;
2230 uint8_t *hdr = NULL;
2232 uint32_t vwv_offset = 0;
2233 uint16_t *vwv = NULL;
2234 uint32_t num_bytes = 0;
2235 uint32_t bytes_offset = 0;
2236 uint8_t *bytes = NULL;
2238 bool found_status = false;
2239 bool found_size = false;
2253 if (pvwv_offset != NULL) {
2256 if (pnum_bytes != NULL) {
2259 if (pbytes != NULL) {
2262 if (pbytes_offset != NULL) {
2265 if (pinbuf != NULL) {
2269 if (state->inbuf != NULL) {
2270 recv_iov = state->smb1.recv_iov;
2271 state->smb1.recv_iov = NULL;
2272 if (state->smb1.recv_cmd != SMBreadBraw) {
2273 hdr = (uint8_t *)recv_iov[0].iov_base;
2274 wct = recv_iov[1].iov_len/2;
2275 vwv = (uint16_t *)recv_iov[1].iov_base;
2276 vwv_offset = PTR_DIFF(vwv, hdr);
2277 num_bytes = recv_iov[2].iov_len;
2278 bytes = (uint8_t *)recv_iov[2].iov_base;
2279 bytes_offset = PTR_DIFF(bytes, hdr);
2283 if (tevent_req_is_nterror(req, &status)) {
2284 for (i=0; i < num_expected; i++) {
2285 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2286 found_status = true;
2292 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2298 if (num_expected == 0) {
2299 found_status = true;
2303 status = state->smb1.recv_status;
2305 for (i=0; i < num_expected; i++) {
2306 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2310 found_status = true;
2311 if (expected[i].wct == 0) {
2316 if (expected[i].wct == wct) {
2322 if (!found_status) {
2327 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2331 *piov = talloc_move(mem_ctx, &recv_iov);
2343 if (pvwv_offset != NULL) {
2344 *pvwv_offset = vwv_offset;
2346 if (pnum_bytes != NULL) {
2347 *pnum_bytes = num_bytes;
2349 if (pbytes != NULL) {
2352 if (pbytes_offset != NULL) {
2353 *pbytes_offset = bytes_offset;
2355 if (pinbuf != NULL) {
2356 *pinbuf = state->inbuf;
2362 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2369 for (i=0; i<num_reqs; i++) {
2370 struct smbXcli_req_state *state;
2371 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2372 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2373 state->smb1.iov_count-2);
2374 wct_ofs = (wct_ofs + 3) & ~3;
2379 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2381 struct smbXcli_req_state *first_state =
2382 tevent_req_data(reqs[0],
2383 struct smbXcli_req_state);
2384 struct smbXcli_req_state *state;
2386 size_t chain_padding = 0;
2388 struct iovec *iov = NULL;
2389 struct iovec *this_iov;
2393 if (num_reqs == 1) {
2394 return smb1cli_req_writev_submit(reqs[0], first_state,
2395 first_state->smb1.iov,
2396 first_state->smb1.iov_count);
2400 for (i=0; i<num_reqs; i++) {
2401 if (!tevent_req_is_in_progress(reqs[i])) {
2402 return NT_STATUS_INTERNAL_ERROR;
2405 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2407 if (state->smb1.iov_count < 4) {
2408 return NT_STATUS_INVALID_PARAMETER_MIX;
2413 * The NBT and SMB header
2426 iovlen += state->smb1.iov_count - 2;
2429 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2431 return NT_STATUS_NO_MEMORY;
2434 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2435 first_state, reqs, sizeof(*reqs) * num_reqs);
2436 if (first_state->smb1.chained_requests == NULL) {
2438 return NT_STATUS_NO_MEMORY;
2441 wct_offset = HDR_WCT;
2444 for (i=0; i<num_reqs; i++) {
2445 size_t next_padding = 0;
2448 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2450 if (i < num_reqs-1) {
2451 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2452 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2454 TALLOC_FREE(first_state->smb1.chained_requests);
2455 return NT_STATUS_INVALID_PARAMETER_MIX;
2459 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2460 state->smb1.iov_count-2) + 1;
2461 if ((wct_offset % 4) != 0) {
2462 next_padding = 4 - (wct_offset % 4);
2464 wct_offset += next_padding;
2465 vwv = state->smb1.vwv;
2467 if (i < num_reqs-1) {
2468 struct smbXcli_req_state *next_state =
2469 tevent_req_data(reqs[i+1],
2470 struct smbXcli_req_state);
2471 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2473 SSVAL(vwv+1, 0, wct_offset);
2474 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2475 /* properly end the chain */
2476 SCVAL(vwv+0, 0, 0xff);
2477 SCVAL(vwv+0, 1, 0xff);
2483 * The NBT and SMB header
2485 this_iov[0] = state->smb1.iov[0];
2486 this_iov[1] = state->smb1.iov[1];
2490 * This one is a bit subtle. We have to add
2491 * chain_padding bytes between the requests, and we
2492 * have to also include the wct field of the
2493 * subsequent requests. We use the subsequent header
2494 * for the padding, it contains the wct field in its
2497 this_iov[0].iov_len = chain_padding+1;
2498 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2499 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2500 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2505 * copy the words and bytes
2507 memcpy(this_iov, state->smb1.iov+2,
2508 sizeof(struct iovec) * (state->smb1.iov_count-2));
2509 this_iov += state->smb1.iov_count - 2;
2510 chain_padding = next_padding;
2513 nbt_len = iov_buflen(&iov[1], iovlen-1);
2514 if ((nbt_len == -1) || (nbt_len > first_state->conn->smb1.max_xmit)) {
2516 TALLOC_FREE(first_state->smb1.chained_requests);
2517 return NT_STATUS_INVALID_PARAMETER_MIX;
2520 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2521 if (!NT_STATUS_IS_OK(status)) {
2523 TALLOC_FREE(first_state->smb1.chained_requests);
2527 return NT_STATUS_OK;
2530 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2532 return ((tevent_queue_length(conn->outgoing) != 0)
2533 || (talloc_array_length(conn->pending) != 0));
2536 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2538 if (conn->protocol >= PROTOCOL_SMB2_02) {
2539 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2542 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2545 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2547 uint16_t credits = 1;
2549 if (conn->smb2.cur_credits == 0) {
2550 if (max_dyn_len != NULL) {
2556 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2557 credits = conn->smb2.cur_credits;
2560 if (max_dyn_len != NULL) {
2561 *max_dyn_len = credits * 65536;
2567 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2569 return conn->smb2.server.capabilities;
2572 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2574 return conn->smb2.server.security_mode;
2577 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2579 return conn->smb2.server.max_trans_size;
2582 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2584 return conn->smb2.server.max_read_size;
2587 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2589 return conn->smb2.server.max_write_size;
2592 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2593 uint16_t max_credits)
2595 conn->smb2.max_credits = max_credits;
2598 uint8_t smb2cli_conn_get_io_priority(struct smbXcli_conn *conn)
2600 if (conn->protocol < PROTOCOL_SMB3_11) {
2604 return conn->smb2.io_priority;
2607 void smb2cli_conn_set_io_priority(struct smbXcli_conn *conn,
2608 uint8_t io_priority)
2610 conn->smb2.io_priority = io_priority;
2613 uint32_t smb2cli_conn_cc_chunk_len(struct smbXcli_conn *conn)
2615 return conn->smb2.cc_chunk_len;
2618 void smb2cli_conn_set_cc_chunk_len(struct smbXcli_conn *conn,
2621 conn->smb2.cc_chunk_len = chunk_len;
2624 uint32_t smb2cli_conn_cc_max_chunks(struct smbXcli_conn *conn)
2626 return conn->smb2.cc_max_chunks;
2629 void smb2cli_conn_set_cc_max_chunks(struct smbXcli_conn *conn,
2630 uint32_t max_chunks)
2632 conn->smb2.cc_max_chunks = max_chunks;
2635 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2637 static bool smb2cli_req_cancel(struct tevent_req *req)
2639 struct smbXcli_req_state *state =
2640 tevent_req_data(req,
2641 struct smbXcli_req_state);
2642 struct smbXcli_tcon *tcon = state->tcon;
2643 struct smbXcli_session *session = state->session;
2644 uint8_t *fixed = state->smb2.pad;
2645 uint16_t fixed_len = 4;
2646 struct tevent_req *subreq;
2647 struct smbXcli_req_state *substate;
2650 SSVAL(fixed, 0, 0x04);
2653 subreq = smb2cli_req_create(state, state->ev,
2661 if (subreq == NULL) {
2664 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2666 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, state->smb2.cancel_flags);
2667 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, state->smb2.cancel_mid);
2668 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, state->smb2.cancel_aid);
2670 status = smb2cli_req_compound_submit(&subreq, 1);
2671 if (!NT_STATUS_IS_OK(status)) {
2672 TALLOC_FREE(subreq);
2676 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2681 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2683 /* we do not care about the result */
2684 TALLOC_FREE(subreq);
2687 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2688 struct tevent_context *ev,
2689 struct smbXcli_conn *conn,
2691 uint32_t additional_flags,
2692 uint32_t clear_flags,
2693 uint32_t timeout_msec,
2694 struct smbXcli_tcon *tcon,
2695 struct smbXcli_session *session,
2696 const uint8_t *fixed,
2700 uint32_t max_dyn_len)
2702 struct tevent_req *req;
2703 struct smbXcli_req_state *state;
2707 bool use_channel_sequence = false;
2708 uint16_t channel_sequence = 0;
2709 bool use_replay_flag = false;
2711 req = tevent_req_create(mem_ctx, &state,
2712 struct smbXcli_req_state);
2719 state->session = session;
2722 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2723 use_channel_sequence = true;
2724 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2725 use_channel_sequence = true;
2728 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_00) {
2729 use_replay_flag = true;
2732 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_11) {
2733 flags |= SMB2_PRIORITY_VALUE_TO_MASK(conn->smb2.io_priority);
2737 uid = session->smb2->session_id;
2739 if (use_channel_sequence) {
2740 channel_sequence = session->smb2->channel_sequence;
2743 if (use_replay_flag && session->smb2->replay_active) {
2744 additional_flags |= SMB2_HDR_FLAG_REPLAY_OPERATION;
2747 state->smb2.should_sign = session->smb2->should_sign;
2748 state->smb2.should_encrypt = session->smb2->should_encrypt;
2750 if (cmd == SMB2_OP_SESSSETUP &&
2751 session->smb2_channel.signing_key.length == 0 &&
2752 session->smb2->signing_key.length != 0)
2755 * a session bind needs to be signed
2757 state->smb2.should_sign = true;
2760 if (cmd == SMB2_OP_SESSSETUP &&
2761 session->smb2_channel.signing_key.length == 0) {
2762 state->smb2.should_encrypt = false;
2765 if (additional_flags & SMB2_HDR_FLAG_SIGNED) {
2766 if (session->smb2_channel.signing_key.length == 0) {
2767 tevent_req_nterror(req, NT_STATUS_NO_USER_SESSION_KEY);
2771 additional_flags &= ~SMB2_HDR_FLAG_SIGNED;
2772 state->smb2.should_sign = true;
2777 tid = tcon->smb2.tcon_id;
2779 if (tcon->smb2.should_sign) {
2780 state->smb2.should_sign = true;
2782 if (tcon->smb2.should_encrypt) {
2783 state->smb2.should_encrypt = true;
2787 if (state->smb2.should_encrypt) {
2788 state->smb2.should_sign = false;
2791 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2792 if (state->smb2.recv_iov == NULL) {
2797 flags |= additional_flags;
2798 flags &= ~clear_flags;
2800 state->smb2.fixed = fixed;
2801 state->smb2.fixed_len = fixed_len;
2802 state->smb2.dyn = dyn;
2803 state->smb2.dyn_len = dyn_len;
2804 state->smb2.max_dyn_len = max_dyn_len;
2806 if (state->smb2.should_encrypt) {
2807 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2808 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2811 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2812 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2813 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2814 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2815 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2816 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2817 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2818 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2821 case SMB2_OP_CANCEL:
2822 state->one_way = true;
2826 * If this is a dummy request, it will have
2827 * UINT64_MAX as message id.
2828 * If we send on break acknowledgement,
2829 * this gets overwritten later.
2831 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2835 if (timeout_msec > 0) {
2836 struct timeval endtime;
2838 endtime = timeval_current_ofs_msec(timeout_msec);
2839 if (!tevent_req_set_endtime(req, ev, endtime)) {
2847 void smb2cli_req_set_notify_async(struct tevent_req *req)
2849 struct smbXcli_req_state *state =
2850 tevent_req_data(req,
2851 struct smbXcli_req_state);
2853 state->smb2.notify_async = true;
2856 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2857 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2858 TALLOC_CTX *tmp_mem,
2861 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2864 struct smbXcli_req_state *state;
2865 struct tevent_req *subreq;
2867 int i, num_iov, nbt_len;
2869 const DATA_BLOB *encryption_key = NULL;
2870 uint64_t encryption_session_id = 0;
2871 uint64_t nonce_high = UINT64_MAX;
2872 uint64_t nonce_low = UINT64_MAX;
2875 * 1 for the nbt length, optional TRANSFORM
2876 * per request: HDR, fixed, dyn, padding
2877 * -1 because the last one does not need padding
2880 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2882 return NT_STATUS_NO_MEMORY;
2889 * the session of the first request that requires encryption
2890 * specifies the encryption key.
2892 for (i=0; i<num_reqs; i++) {
2893 if (!tevent_req_is_in_progress(reqs[i])) {
2894 return NT_STATUS_INTERNAL_ERROR;
2897 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2899 if (!smbXcli_conn_is_connected(state->conn)) {
2900 return NT_STATUS_CONNECTION_DISCONNECTED;
2903 if ((state->conn->protocol != PROTOCOL_NONE) &&
2904 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2905 return NT_STATUS_REVISION_MISMATCH;
2908 if (state->session == NULL) {
2912 if (!state->smb2.should_encrypt) {
2916 encryption_key = &state->session->smb2->encryption_key;
2917 if (encryption_key->length == 0) {
2918 return NT_STATUS_INVALID_PARAMETER_MIX;
2921 encryption_session_id = state->session->smb2->session_id;
2923 state->session->smb2->nonce_low += 1;
2924 if (state->session->smb2->nonce_low == 0) {
2925 state->session->smb2->nonce_high += 1;
2926 state->session->smb2->nonce_low += 1;
2930 * CCM and GCM algorithms must never have their
2931 * nonce wrap, or the security of the whole
2932 * communication and the keys is destroyed.
2933 * We must drop the connection once we have
2934 * transfered too much data.
2936 * NOTE: We assume nonces greater than 8 bytes.
2938 if (state->session->smb2->nonce_high >=
2939 state->session->smb2->nonce_high_max)
2941 return NT_STATUS_ENCRYPTION_FAILED;
2944 nonce_high = state->session->smb2->nonce_high_random;
2945 nonce_high += state->session->smb2->nonce_high;
2946 nonce_low = state->session->smb2->nonce_low;
2949 iov[num_iov].iov_base = state->smb2.transform;
2950 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2953 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2954 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2956 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2958 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2959 encryption_session_id);
2961 nbt_len += SMB2_TF_HDR_SIZE;
2965 for (i=0; i<num_reqs; i++) {
2974 const DATA_BLOB *signing_key = NULL;
2976 if (!tevent_req_is_in_progress(reqs[i])) {
2977 return NT_STATUS_INTERNAL_ERROR;
2980 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2982 if (!smbXcli_conn_is_connected(state->conn)) {
2983 return NT_STATUS_CONNECTION_DISCONNECTED;
2986 if ((state->conn->protocol != PROTOCOL_NONE) &&
2987 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2988 return NT_STATUS_REVISION_MISMATCH;
2991 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2992 if (opcode == SMB2_OP_CANCEL) {
2996 avail = UINT64_MAX - state->conn->smb2.mid;
2998 return NT_STATUS_CONNECTION_ABORTED;
3001 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
3002 uint32_t max_dyn_len = 1;
3004 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
3005 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
3007 charge = (max_dyn_len - 1)/ 65536 + 1;
3012 charge = MAX(state->smb2.credit_charge, charge);
3014 avail = MIN(avail, state->conn->smb2.cur_credits);
3015 if (avail < charge) {
3016 return NT_STATUS_INTERNAL_ERROR;
3020 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
3021 credits = state->conn->smb2.max_credits -
3022 state->conn->smb2.cur_credits;
3024 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
3028 mid = state->conn->smb2.mid;
3029 state->conn->smb2.mid += charge;
3030 state->conn->smb2.cur_credits -= charge;
3032 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
3033 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
3035 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
3036 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
3038 state->smb2.cancel_flags = 0;
3039 state->smb2.cancel_mid = mid;
3040 state->smb2.cancel_aid = 0;
3043 if (state->session && encryption_key == NULL) {
3045 * We prefer the channel signing key if it is
3048 if (state->smb2.should_sign) {
3049 signing_key = &state->session->smb2_channel.signing_key;
3053 * If it is a channel binding, we already have the main
3054 * signing key and try that one.
3056 if (signing_key && signing_key->length == 0) {
3057 signing_key = &state->session->smb2->signing_key;
3061 * If we do not have any session key yet, we skip the
3062 * signing of SMB2_OP_SESSSETUP requests.
3064 if (signing_key && signing_key->length == 0) {
3070 iov[num_iov].iov_base = state->smb2.hdr;
3071 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
3074 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
3075 iov[num_iov].iov_len = state->smb2.fixed_len;
3078 if (state->smb2.dyn != NULL) {
3079 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
3080 iov[num_iov].iov_len = state->smb2.dyn_len;
3084 reqlen = sizeof(state->smb2.hdr);
3085 reqlen += state->smb2.fixed_len;
3086 reqlen += state->smb2.dyn_len;
3088 if (i < num_reqs-1) {
3089 if ((reqlen % 8) > 0) {
3090 uint8_t pad = 8 - (reqlen % 8);
3091 iov[num_iov].iov_base = state->smb2.pad;
3092 iov[num_iov].iov_len = pad;
3096 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
3099 state->smb2.encryption_session_id = encryption_session_id;
3101 if (signing_key != NULL) {
3104 status = smb2_signing_sign_pdu(*signing_key,
3105 state->session->conn->protocol,
3106 &iov[hdr_iov], num_iov - hdr_iov);
3107 if (!NT_STATUS_IS_OK(status)) {
3114 ret = smbXcli_req_set_pending(reqs[i]);
3116 return NT_STATUS_NO_MEMORY;
3120 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
3121 _smb_setlen_tcp(state->length_hdr, nbt_len);
3122 iov[0].iov_base = state->length_hdr;
3123 iov[0].iov_len = sizeof(state->length_hdr);
3125 if (encryption_key != NULL) {
3127 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
3131 buf = talloc_array(iov, uint8_t, buflen);
3133 return NT_STATUS_NO_MEMORY;
3137 * We copy the buffers before encrypting them,
3138 * this is at least currently needed for the
3139 * to keep state->smb2.hdr.
3141 * Also the callers may expect there buffers
3144 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3145 struct iovec *v = &iov[vi];
3146 const uint8_t *o = (const uint8_t *)v->iov_base;
3148 memcpy(buf, o, v->iov_len);
3149 v->iov_base = (void *)buf;
3153 status = smb2_signing_encrypt_pdu(*encryption_key,
3154 state->conn->smb2.server.cipher,
3155 &iov[tf_iov], num_iov - tf_iov);
3156 if (!NT_STATUS_IS_OK(status)) {
3161 if (state->conn->dispatch_incoming == NULL) {
3162 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3165 subreq = writev_send(state, state->ev, state->conn->outgoing,
3166 state->conn->write_fd, false, iov, num_iov);
3167 if (subreq == NULL) {
3168 return NT_STATUS_NO_MEMORY;
3170 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3171 return NT_STATUS_OK;
3174 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3176 struct smbXcli_req_state *state =
3177 tevent_req_data(req,
3178 struct smbXcli_req_state);
3180 state->smb2.credit_charge = charge;
3183 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3184 struct tevent_context *ev,
3185 struct smbXcli_conn *conn,
3187 uint32_t additional_flags,
3188 uint32_t clear_flags,
3189 uint32_t timeout_msec,
3190 struct smbXcli_tcon *tcon,
3191 struct smbXcli_session *session,
3192 const uint8_t *fixed,
3196 uint32_t max_dyn_len)
3198 struct tevent_req *req;
3201 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3202 additional_flags, clear_flags,
3211 if (!tevent_req_is_in_progress(req)) {
3212 return tevent_req_post(req, ev);
3214 status = smb2cli_req_compound_submit(&req, 1);
3215 if (tevent_req_nterror(req, status)) {
3216 return tevent_req_post(req, ev);
3221 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3223 struct tevent_req *req =
3224 tevent_req_callback_data(subreq,
3226 struct smbXcli_req_state *state =
3227 tevent_req_data(req,
3228 struct smbXcli_req_state);
3232 nwritten = writev_recv(subreq, &err);
3233 TALLOC_FREE(subreq);
3234 if (nwritten == -1) {
3235 /* here, we need to notify all pending requests */
3236 NTSTATUS status = map_nt_error_from_unix_common(err);
3237 smbXcli_conn_disconnect(state->conn, status);
3242 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3245 TALLOC_CTX *mem_ctx,
3246 struct iovec **piov, int *pnum_iov)
3251 uint8_t *first_hdr = buf;
3252 size_t verified_buflen = 0;
3256 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3258 return NT_STATUS_NO_MEMORY;
3261 while (taken < buflen) {
3262 size_t len = buflen - taken;
3263 uint8_t *hdr = first_hdr + taken;
3266 size_t next_command_ofs;
3268 struct iovec *iov_tmp;
3270 if (verified_buflen > taken) {
3271 len = verified_buflen - taken;
3278 DEBUG(10, ("%d bytes left, expected at least %d\n",
3282 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3283 struct smbXcli_session *s;
3285 struct iovec tf_iov[2];
3289 if (len < SMB2_TF_HDR_SIZE) {
3290 DEBUG(10, ("%d bytes left, expected at least %d\n",
3291 (int)len, SMB2_TF_HDR_SIZE));
3295 tf_len = SMB2_TF_HDR_SIZE;
3298 hdr = first_hdr + taken;
3299 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3300 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3302 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3303 DEBUG(10, ("%d bytes left, expected at least %d\n",
3305 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3310 for (; s; s = s->next) {
3311 if (s->smb2->session_id != uid) {
3318 DEBUG(10, ("unknown session_id %llu\n",
3319 (unsigned long long)uid));
3323 tf_iov[0].iov_base = (void *)tf;
3324 tf_iov[0].iov_len = tf_len;
3325 tf_iov[1].iov_base = (void *)hdr;
3326 tf_iov[1].iov_len = enc_len;
3328 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3329 conn->smb2.server.cipher,
3331 if (!NT_STATUS_IS_OK(status)) {
3336 verified_buflen = taken + enc_len;
3341 * We need the header plus the body length field
3344 if (len < SMB2_HDR_BODY + 2) {
3345 DEBUG(10, ("%d bytes left, expected at least %d\n",
3346 (int)len, SMB2_HDR_BODY));
3349 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3350 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3354 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3355 DEBUG(10, ("Got HDR len %d, expected %d\n",
3356 SVAL(hdr, 4), SMB2_HDR_BODY));
3361 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3362 body_size = SVAL(hdr, SMB2_HDR_BODY);
3364 if (next_command_ofs != 0) {
3365 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3368 if (next_command_ofs > full_size) {
3371 full_size = next_command_ofs;
3373 if (body_size < 2) {
3376 body_size &= 0xfffe;
3378 if (body_size > (full_size - SMB2_HDR_BODY)) {
3382 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3384 if (iov_tmp == NULL) {
3386 return NT_STATUS_NO_MEMORY;
3389 cur = &iov[num_iov];
3392 cur[0].iov_base = tf;
3393 cur[0].iov_len = tf_len;
3394 cur[1].iov_base = hdr;
3395 cur[1].iov_len = SMB2_HDR_BODY;
3396 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3397 cur[2].iov_len = body_size;
3398 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3399 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3405 *pnum_iov = num_iov;
3406 return NT_STATUS_OK;
3410 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3413 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3416 size_t num_pending = talloc_array_length(conn->pending);
3419 for (i=0; i<num_pending; i++) {
3420 struct tevent_req *req = conn->pending[i];
3421 struct smbXcli_req_state *state =
3422 tevent_req_data(req,
3423 struct smbXcli_req_state);
3425 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3432 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3433 TALLOC_CTX *tmp_mem,
3436 struct tevent_req *req;
3437 struct smbXcli_req_state *state = NULL;
3442 struct smbXcli_session *last_session = NULL;
3443 size_t inbuf_len = smb_len_tcp(inbuf);
3445 status = smb2cli_inbuf_parse_compound(conn,
3446 inbuf + NBT_HDR_SIZE,
3450 if (!NT_STATUS_IS_OK(status)) {
3454 for (i=0; i<num_iov; i+=4) {
3455 uint8_t *inbuf_ref = NULL;
3456 struct iovec *cur = &iov[i];
3457 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3458 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3459 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3460 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3461 uint16_t req_opcode;
3463 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3464 uint32_t new_credits;
3465 struct smbXcli_session *session = NULL;
3466 const DATA_BLOB *signing_key = NULL;
3467 bool was_encrypted = false;
3469 new_credits = conn->smb2.cur_credits;
3470 new_credits += credits;
3471 if (new_credits > UINT16_MAX) {
3472 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3474 conn->smb2.cur_credits += credits;
3476 req = smb2cli_conn_find_pending(conn, mid);
3478 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3480 state = tevent_req_data(req, struct smbXcli_req_state);
3482 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3483 if (opcode != req_opcode) {
3484 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3486 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3488 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3489 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3492 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3493 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3494 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3495 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3497 if (state->smb2.got_async) {
3498 /* We only expect one STATUS_PENDING response */
3499 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3501 state->smb2.got_async = true;
3504 * async interim responses are not signed,
3505 * even if the SMB2_HDR_FLAG_SIGNED flag
3508 state->smb2.cancel_flags = SMB2_HDR_FLAG_ASYNC;
3509 state->smb2.cancel_mid = 0;
3510 state->smb2.cancel_aid = async_id;
3512 if (state->smb2.notify_async) {
3513 tevent_req_defer_callback(req, state->ev);
3514 tevent_req_notify_callback(req);
3519 session = state->session;
3520 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3521 session = last_session;
3523 last_session = session;
3525 if (state->smb2.should_sign) {
3526 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3527 return NT_STATUS_ACCESS_DENIED;
3531 if (flags & SMB2_HDR_FLAG_SIGNED) {
3532 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3534 if (session == NULL) {
3535 struct smbXcli_session *s;
3537 s = state->conn->sessions;
3538 for (; s; s = s->next) {
3539 if (s->smb2->session_id != uid) {
3548 if (session == NULL) {
3549 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3552 last_session = session;
3553 signing_key = &session->smb2_channel.signing_key;
3556 if (opcode == SMB2_OP_SESSSETUP) {
3558 * We prefer the channel signing key, if it is
3561 * If we do not have a channel signing key yet,
3562 * we try the main signing key, if it is not
3563 * the final response.
3565 if (signing_key && signing_key->length == 0 &&
3566 !NT_STATUS_IS_OK(status)) {
3567 signing_key = &session->smb2->signing_key;
3570 if (signing_key && signing_key->length == 0) {
3572 * If we do not have a session key to
3573 * verify the signature, we defer the
3574 * signing check to the caller.
3576 * The caller gets NT_STATUS_OK, it
3578 * smb2cli_session_set_session_key()
3580 * smb2cli_session_set_channel_key()
3581 * which will check the signature
3582 * with the channel signing key.
3588 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3589 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3590 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3593 * If the response was encrypted in a SMB2_TRANSFORM
3594 * pdu, which belongs to the correct session,
3595 * we do not need to do signing checks
3597 * It could be the session the response belongs to
3598 * or the session that was used to encrypt the
3599 * SMB2_TRANSFORM request.
3601 if ((session && session->smb2->session_id == uid) ||
3602 (state->smb2.encryption_session_id == uid)) {
3604 was_encrypted = true;
3608 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3610 * if the server returns NT_STATUS_USER_SESSION_DELETED
3611 * the response is not signed and we should
3612 * propagate the NT_STATUS_USER_SESSION_DELETED
3613 * status to the caller.
3615 state->smb2.signing_skipped = true;
3619 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3621 * if the server returns
3622 * NT_STATUS_INVALID_PARAMETER
3623 * the response might not be encrypted.
3625 if (state->smb2.should_encrypt && !was_encrypted) {
3626 state->smb2.signing_skipped = true;
3631 if (state->smb2.should_encrypt && !was_encrypted) {
3632 if (!state->smb2.signing_skipped) {
3633 return NT_STATUS_ACCESS_DENIED;
3637 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3638 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3639 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3641 * if the server returns
3642 * NT_STATUS_NETWORK_NAME_DELETED
3643 * NT_STATUS_FILE_CLOSED
3644 * NT_STATUS_INVALID_PARAMETER
3645 * the response might not be signed
3646 * as this happens before the signing checks.
3648 * If server echos the signature (or all zeros)
3649 * we should report the status from the server
3655 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3656 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3659 state->smb2.signing_skipped = true;
3665 static const uint8_t zeros[16];
3667 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3671 state->smb2.signing_skipped = true;
3678 status = smb2_signing_check_pdu(*signing_key,
3679 state->conn->protocol,
3681 if (!NT_STATUS_IS_OK(status)) {
3683 * If the signing check fails, we disconnect
3690 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3691 (session != NULL) && session->disconnect_expired)
3694 * this should be a short term hack
3695 * until the upper layers have implemented
3696 * re-authentication.
3701 smbXcli_req_unset_pending(req);
3704 * There might be more than one response
3705 * we need to defer the notifications
3707 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3712 tevent_req_defer_callback(req, state->ev);
3716 * Note: here we use talloc_reference() in a way
3717 * that does not expose it to the caller.
3719 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3720 if (tevent_req_nomem(inbuf_ref, req)) {
3724 /* copy the related buffers */
3725 state->smb2.recv_iov[0] = cur[1];
3726 state->smb2.recv_iov[1] = cur[2];
3727 state->smb2.recv_iov[2] = cur[3];
3729 tevent_req_done(req);
3733 return NT_STATUS_RETRY;
3736 return NT_STATUS_OK;
3739 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3740 struct iovec **piov,
3741 const struct smb2cli_req_expected_response *expected,
3742 size_t num_expected)
3744 struct smbXcli_req_state *state =
3745 tevent_req_data(req,
3746 struct smbXcli_req_state);
3749 bool found_status = false;
3750 bool found_size = false;
3757 if (tevent_req_is_in_progress(req) && state->smb2.got_async) {
3758 return STATUS_PENDING;
3761 if (tevent_req_is_nterror(req, &status)) {
3762 for (i=0; i < num_expected; i++) {
3763 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3764 found_status = true;
3770 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3776 if (num_expected == 0) {
3777 found_status = true;
3781 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3782 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3784 for (i=0; i < num_expected; i++) {
3785 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3789 found_status = true;
3790 if (expected[i].body_size == 0) {
3795 if (expected[i].body_size == body_size) {
3801 if (!found_status) {
3805 if (state->smb2.signing_skipped) {
3806 if (num_expected > 0) {
3807 return NT_STATUS_ACCESS_DENIED;
3809 if (!NT_STATUS_IS_ERR(status)) {
3810 return NT_STATUS_ACCESS_DENIED;
3815 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3819 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3825 NTSTATUS smb2cli_req_get_sent_iov(struct tevent_req *req,
3826 struct iovec *sent_iov)
3828 struct smbXcli_req_state *state =
3829 tevent_req_data(req,
3830 struct smbXcli_req_state);
3832 if (tevent_req_is_in_progress(req)) {
3833 return STATUS_PENDING;
3836 sent_iov[0].iov_base = state->smb2.hdr;
3837 sent_iov[0].iov_len = sizeof(state->smb2.hdr);
3839 sent_iov[1].iov_base = discard_const(state->smb2.fixed);
3840 sent_iov[1].iov_len = state->smb2.fixed_len;
3842 if (state->smb2.dyn != NULL) {
3843 sent_iov[2].iov_base = discard_const(state->smb2.dyn);
3844 sent_iov[2].iov_len = state->smb2.dyn_len;
3846 sent_iov[2].iov_base = NULL;
3847 sent_iov[2].iov_len = 0;
3850 return NT_STATUS_OK;
3853 static const struct {
3854 enum protocol_types proto;
3855 const char *smb1_name;
3856 } smb1cli_prots[] = {
3857 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3858 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3859 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3860 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3861 {PROTOCOL_LANMAN2, "LM1.2X002"},
3862 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3863 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3864 {PROTOCOL_LANMAN2, "Samba"},
3865 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3866 {PROTOCOL_NT1, "NT LM 0.12"},
3867 {PROTOCOL_SMB2_02, "SMB 2.002"},
3868 {PROTOCOL_SMB2_10, "SMB 2.???"},
3871 static const struct {
3872 enum protocol_types proto;
3873 uint16_t smb2_dialect;
3874 } smb2cli_prots[] = {
3875 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3876 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3877 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3878 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3879 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3880 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
3881 {PROTOCOL_SMB3_10, SMB3_DIALECT_REVISION_310},
3882 {PROTOCOL_SMB3_11, SMB3_DIALECT_REVISION_311},
3885 struct smbXcli_negprot_state {
3886 struct smbXcli_conn *conn;
3887 struct tevent_context *ev;
3888 uint32_t timeout_msec;
3895 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3896 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3897 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3898 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3899 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3900 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3904 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3905 struct tevent_context *ev,
3906 struct smbXcli_conn *conn,
3907 uint32_t timeout_msec,
3908 enum protocol_types min_protocol,
3909 enum protocol_types max_protocol)
3911 struct tevent_req *req, *subreq;
3912 struct smbXcli_negprot_state *state;
3914 req = tevent_req_create(mem_ctx, &state,
3915 struct smbXcli_negprot_state);
3921 state->timeout_msec = timeout_msec;
3923 if (min_protocol == PROTOCOL_NONE) {
3924 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3925 return tevent_req_post(req, ev);
3928 if (max_protocol == PROTOCOL_NONE) {
3929 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3930 return tevent_req_post(req, ev);
3933 if (min_protocol > max_protocol) {
3934 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3935 return tevent_req_post(req, ev);
3938 conn->min_protocol = min_protocol;
3939 conn->max_protocol = max_protocol;
3940 conn->protocol = PROTOCOL_NONE;
3942 if ((min_protocol < PROTOCOL_SMB2_02) &&
3943 (max_protocol < PROTOCOL_SMB2_02)) {
3947 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3949 subreq = smbXcli_negprot_smb1_subreq(state);
3950 if (tevent_req_nomem(subreq, req)) {
3951 return tevent_req_post(req, ev);
3953 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3957 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3958 (max_protocol >= PROTOCOL_SMB2_02)) {
3962 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3965 * As we're starting with an SMB2 negprot, emulate Windows
3966 * and ask for 31 credits in the initial SMB2 negprot.
3967 * If we don't and leave requested credits at
3968 * zero, MacOSX servers return zero credits on
3969 * the negprot reply and we fail to connect.
3971 smb2cli_conn_set_max_credits(conn,
3972 WINDOWS_CLIENT_PURE_SMB2_NEGPROT_INITIAL_CREDIT_ASK);
3974 subreq = smbXcli_negprot_smb2_subreq(state);
3975 if (tevent_req_nomem(subreq, req)) {
3976 return tevent_req_post(req, ev);
3978 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3983 * We send an SMB1 negprot with the SMB2 dialects
3984 * and expect a SMB1 or a SMB2 response.
3986 * smbXcli_negprot_dispatch_incoming() will fix the
3987 * callback to match protocol of the response.
3989 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3991 subreq = smbXcli_negprot_smb1_subreq(state);
3992 if (tevent_req_nomem(subreq, req)) {
3993 return tevent_req_post(req, ev);
3995 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3999 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
4001 struct tevent_req *req =
4002 tevent_req_callback_data(subreq,
4007 * we just want the low level error
4009 status = tevent_req_simple_recv_ntstatus(subreq);
4010 TALLOC_FREE(subreq);
4011 if (tevent_req_nterror(req, status)) {
4015 /* this should never happen */
4016 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4019 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
4022 DATA_BLOB bytes = data_blob_null;
4026 /* setup the protocol strings */
4027 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4031 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4035 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4039 ok = data_blob_append(state, &bytes, &c, sizeof(c));
4045 * We now it is already ascii and
4046 * we want NULL termination.
4048 ok = data_blob_append(state, &bytes,
4049 smb1cli_prots[i].smb1_name,
4050 strlen(smb1cli_prots[i].smb1_name)+1);
4056 smb1cli_req_flags(state->conn->max_protocol,
4057 state->conn->smb1.client.capabilities,
4062 return smb1cli_req_send(state, state->ev, state->conn,
4066 state->timeout_msec,
4067 0xFFFE, 0, NULL, /* pid, tid, session */
4068 0, NULL, /* wct, vwv */
4069 bytes.length, bytes.data);
4072 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
4074 struct tevent_req *req =
4075 tevent_req_callback_data(subreq,
4077 struct smbXcli_negprot_state *state =
4078 tevent_req_data(req,
4079 struct smbXcli_negprot_state);
4080 struct smbXcli_conn *conn = state->conn;
4081 struct iovec *recv_iov = NULL;
4090 size_t num_prots = 0;
4092 uint32_t client_capabilities = conn->smb1.client.capabilities;
4093 uint32_t both_capabilities;
4094 uint32_t server_capabilities = 0;
4095 uint32_t capabilities;
4096 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
4097 uint32_t server_max_xmit = 0;
4099 uint32_t server_max_mux = 0;
4100 uint16_t server_security_mode = 0;
4101 uint32_t server_session_key = 0;
4102 bool server_readbraw = false;
4103 bool server_writebraw = false;
4104 bool server_lockread = false;
4105 bool server_writeunlock = false;
4106 struct GUID server_guid = GUID_zero();
4107 DATA_BLOB server_gss_blob = data_blob_null;
4108 uint8_t server_challenge[8];
4109 char *server_workgroup = NULL;
4110 char *server_name = NULL;
4111 int server_time_zone = 0;
4112 NTTIME server_system_time = 0;
4113 static const struct smb1cli_req_expected_response expected[] = {
4115 .status = NT_STATUS_OK,
4116 .wct = 0x11, /* NT1 */
4119 .status = NT_STATUS_OK,
4120 .wct = 0x0D, /* LM */
4123 .status = NT_STATUS_OK,
4124 .wct = 0x01, /* CORE */
4128 ZERO_STRUCT(server_challenge);
4130 status = smb1cli_req_recv(subreq, state,
4135 NULL, /* pvwv_offset */
4138 NULL, /* pbytes_offset */
4140 expected, ARRAY_SIZE(expected));
4141 TALLOC_FREE(subreq);
4142 if (tevent_req_nterror(req, status)) {
4146 flags = CVAL(inhdr, HDR_FLG);
4148 protnum = SVAL(vwv, 0);
4150 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4151 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4155 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4159 if (protnum != num_prots) {
4164 conn->protocol = smb1cli_prots[i].proto;
4168 if (conn->protocol == PROTOCOL_NONE) {
4169 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4173 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
4174 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4175 "and the selected protocol level doesn't support it.\n"));
4176 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4180 if (flags & FLAG_SUPPORT_LOCKREAD) {
4181 server_lockread = true;
4182 server_writeunlock = true;
4185 if (conn->protocol >= PROTOCOL_NT1) {
4186 const char *client_signing = NULL;
4187 bool server_mandatory = false;
4188 bool server_allowed = false;
4189 const char *server_signing = NULL;
4194 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4199 server_security_mode = CVAL(vwv + 1, 0);
4200 server_max_mux = SVAL(vwv + 1, 1);
4201 server_max_xmit = IVAL(vwv + 3, 1);
4202 server_session_key = IVAL(vwv + 7, 1);
4203 server_time_zone = SVALS(vwv + 15, 1);
4204 server_time_zone *= 60;
4205 /* this time arrives in real GMT */
4206 server_system_time = BVAL(vwv + 11, 1);
4207 server_capabilities = IVAL(vwv + 9, 1);
4209 key_len = CVAL(vwv + 16, 1);
4211 if (server_capabilities & CAP_RAW_MODE) {
4212 server_readbraw = true;
4213 server_writebraw = true;
4215 if (server_capabilities & CAP_LOCK_AND_READ) {
4216 server_lockread = true;
4219 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4220 DATA_BLOB blob1, blob2;
4222 if (num_bytes < 16) {
4223 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4227 blob1 = data_blob_const(bytes, 16);
4228 status = GUID_from_data_blob(&blob1, &server_guid);
4229 if (tevent_req_nterror(req, status)) {
4233 blob1 = data_blob_const(bytes+16, num_bytes-16);
4234 blob2 = data_blob_dup_talloc(state, blob1);
4235 if (blob1.length > 0 &&
4236 tevent_req_nomem(blob2.data, req)) {
4239 server_gss_blob = blob2;
4241 DATA_BLOB blob1, blob2;
4243 if (num_bytes < key_len) {
4244 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4248 if (key_len != 0 && key_len != 8) {
4249 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4254 memcpy(server_challenge, bytes, 8);
4257 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4258 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4259 if (blob1.length > 0) {
4262 len = utf16_len_n(blob1.data,
4266 ok = convert_string_talloc(state,
4274 status = map_nt_error_from_unix_common(errno);
4275 tevent_req_nterror(req, status);
4280 blob2.data += blob1.length;
4281 blob2.length -= blob1.length;
4282 if (blob2.length > 0) {
4285 len = utf16_len_n(blob1.data,
4289 ok = convert_string_talloc(state,
4297 status = map_nt_error_from_unix_common(errno);
4298 tevent_req_nterror(req, status);
4304 client_signing = "disabled";
4305 if (conn->allow_signing) {
4306 client_signing = "allowed";
4308 if (conn->mandatory_signing) {
4309 client_signing = "required";
4312 server_signing = "not supported";
4313 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4314 server_signing = "supported";
4315 server_allowed = true;
4316 } else if (conn->mandatory_signing) {
4318 * We have mandatory signing as client
4319 * lets assume the server will look at our
4320 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4321 * flag in the session setup
4323 server_signing = "not announced";
4324 server_allowed = true;
4326 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4327 server_signing = "required";
4328 server_mandatory = true;
4331 ok = smb_signing_set_negotiated(conn->smb1.signing,
4335 DEBUG(1,("cli_negprot: SMB signing is required, "
4336 "but client[%s] and server[%s] mismatch\n",
4337 client_signing, server_signing));
4338 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4342 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4348 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4352 server_security_mode = SVAL(vwv + 1, 0);
4353 server_max_xmit = SVAL(vwv + 2, 0);
4354 server_max_mux = SVAL(vwv + 3, 0);
4355 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4356 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4357 server_session_key = IVAL(vwv + 6, 0);
4358 server_time_zone = SVALS(vwv + 10, 0);
4359 server_time_zone *= 60;
4360 /* this time is converted to GMT by make_unix_date */
4361 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4362 unix_to_nt_time(&server_system_time, t);
4363 key_len = SVAL(vwv + 11, 0);
4365 if (num_bytes < key_len) {
4366 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4370 if (key_len != 0 && key_len != 8) {
4371 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4376 memcpy(server_challenge, bytes, 8);
4379 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4380 if (blob1.length > 0) {
4384 len = utf16_len_n(blob1.data,
4388 ok = convert_string_talloc(state,
4396 status = map_nt_error_from_unix_common(errno);
4397 tevent_req_nterror(req, status);
4403 /* the old core protocol */
4404 server_time_zone = get_time_zone(time(NULL));
4405 server_max_xmit = 1024;
4409 if (server_max_xmit < 1024) {
4410 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4414 if (server_max_mux < 1) {
4415 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4420 * Now calculate the negotiated capabilities
4421 * based on the mask for:
4422 * - client only flags
4423 * - flags used in both directions
4424 * - server only flags
4426 both_capabilities = client_capabilities & server_capabilities;
4427 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4428 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4429 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4431 max_xmit = MIN(client_max_xmit, server_max_xmit);
4433 conn->smb1.server.capabilities = server_capabilities;
4434 conn->smb1.capabilities = capabilities;
4436 conn->smb1.server.max_xmit = server_max_xmit;
4437 conn->smb1.max_xmit = max_xmit;
4439 conn->smb1.server.max_mux = server_max_mux;
4441 conn->smb1.server.security_mode = server_security_mode;
4443 conn->smb1.server.readbraw = server_readbraw;
4444 conn->smb1.server.writebraw = server_writebraw;
4445 conn->smb1.server.lockread = server_lockread;
4446 conn->smb1.server.writeunlock = server_writeunlock;
4448 conn->smb1.server.session_key = server_session_key;
4450 talloc_steal(conn, server_gss_blob.data);
4451 conn->smb1.server.gss_blob = server_gss_blob;
4452 conn->smb1.server.guid = server_guid;
4453 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4454 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4455 conn->smb1.server.name = talloc_move(conn, &server_name);
4457 conn->smb1.server.time_zone = server_time_zone;
4458 conn->smb1.server.system_time = server_system_time;
4460 tevent_req_done(req);
4463 static size_t smbXcli_padding_helper(uint32_t offset, size_t n)
4465 if ((offset & (n-1)) == 0) return 0;
4466 return n - (offset & (n-1));
4469 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4473 uint16_t dialect_count = 0;
4474 DATA_BLOB dyn = data_blob_null;
4476 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4480 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4484 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4488 SSVAL(val, 0, smb2cli_prots[i].smb2_dialect);
4490 ok = data_blob_append(state, &dyn, val, sizeof(val));
4498 buf = state->smb2.fixed;
4500 SSVAL(buf, 2, dialect_count);
4501 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4502 SSVAL(buf, 6, 0); /* Reserved */
4503 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
4504 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4506 SIVAL(buf, 8, 0); /* Capabilities */
4508 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4512 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4514 if (!NT_STATUS_IS_OK(status)) {
4517 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4519 memset(buf+12, 0, 16); /* ClientGuid */
4522 if (state->conn->max_protocol >= PROTOCOL_SMB3_10) {
4524 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4528 const uint8_t zeros[8] = {0, };
4532 SSVAL(p, 0, 1); /* HashAlgorithmCount */
4533 SSVAL(p, 2, 32); /* SaltLength */
4534 SSVAL(p, 4, SMB2_PREAUTH_INTEGRITY_SHA512);
4535 generate_random_buffer(p + 6, 32);
4537 b = data_blob_const(p, 38);
4538 status = smb2_negotiate_context_add(state, &c,
4539 SMB2_PREAUTH_INTEGRITY_CAPABILITIES, b);
4540 if (!NT_STATUS_IS_OK(status)) {
4544 SSVAL(p, 0, 2); /* ChiperCount */
4545 SSVAL(p, 2, SMB2_ENCRYPTION_AES128_GCM);
4546 SSVAL(p, 4, SMB2_ENCRYPTION_AES128_CCM);
4548 b = data_blob_const(p, 6);
4549 status = smb2_negotiate_context_add(state, &c,
4550 SMB2_ENCRYPTION_CAPABILITIES, b);
4551 if (!NT_STATUS_IS_OK(status)) {
4555 status = smb2_negotiate_context_push(state, &b, c);
4556 if (!NT_STATUS_IS_OK(status)) {
4560 offset = SMB2_HDR_BODY + sizeof(state->smb2.fixed) + dyn.length;
4561 pad = smbXcli_padding_helper(offset, 8);
4563 ok = data_blob_append(state, &dyn, zeros, pad);
4569 ok = data_blob_append(state, &dyn, b.data, b.length);
4574 SIVAL(buf, 28, offset); /* NegotiateContextOffset */
4575 SSVAL(buf, 32, c.num_contexts); /* NegotiateContextCount */
4576 SSVAL(buf, 34, 0); /* Reserved */
4578 SBVAL(buf, 28, 0); /* Reserved/ClientStartTime */
4581 return smb2cli_req_send(state, state->ev,
4582 state->conn, SMB2_OP_NEGPROT,
4584 state->timeout_msec,
4585 NULL, NULL, /* tcon, session */
4586 state->smb2.fixed, sizeof(state->smb2.fixed),
4587 dyn.data, dyn.length,
4588 UINT16_MAX); /* max_dyn_len */
4591 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4593 struct tevent_req *req =
4594 tevent_req_callback_data(subreq,
4596 struct smbXcli_negprot_state *state =
4597 tevent_req_data(req,
4598 struct smbXcli_negprot_state);
4599 struct smbXcli_conn *conn = state->conn;
4600 size_t security_offset, security_length;
4606 uint16_t dialect_revision;
4607 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4608 uint32_t negotiate_context_offset = 0;
4609 uint16_t negotiate_context_count = 0;
4610 DATA_BLOB negotiate_context_blob = data_blob_null;
4614 struct smb2_negotiate_context *preauth = NULL;
4615 uint16_t hash_count;
4616 uint16_t salt_length;
4617 uint16_t hash_selected;
4618 struct hc_sha512state sctx;
4619 struct smb2_negotiate_context *cipher = NULL;
4620 struct iovec sent_iov[3];
4621 static const struct smb2cli_req_expected_response expected[] = {
4623 .status = NT_STATUS_OK,
4628 status = smb2cli_req_recv(subreq, state, &iov,
4629 expected, ARRAY_SIZE(expected));
4630 if (tevent_req_nterror(req, status)) {
4634 body = (uint8_t *)iov[1].iov_base;
4636 dialect_revision = SVAL(body, 4);
4638 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4639 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4643 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4647 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4651 conn->protocol = smb2cli_prots[i].proto;
4655 if (conn->protocol == PROTOCOL_NONE) {
4656 TALLOC_FREE(subreq);
4658 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
4659 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4663 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4664 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4668 /* make sure we do not loop forever */
4669 state->conn->min_protocol = PROTOCOL_SMB2_02;
4672 * send a SMB2 negprot, in order to negotiate
4675 subreq = smbXcli_negprot_smb2_subreq(state);
4676 if (tevent_req_nomem(subreq, req)) {
4679 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4683 conn->smb2.server.security_mode = SVAL(body, 2);
4684 if (conn->protocol >= PROTOCOL_SMB3_10) {
4685 negotiate_context_count = SVAL(body, 6);
4688 blob = data_blob_const(body + 8, 16);
4689 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4690 if (tevent_req_nterror(req, status)) {
4694 conn->smb2.server.capabilities = IVAL(body, 24);
4695 conn->smb2.server.max_trans_size= IVAL(body, 28);
4696 conn->smb2.server.max_read_size = IVAL(body, 32);
4697 conn->smb2.server.max_write_size= IVAL(body, 36);
4698 conn->smb2.server.system_time = BVAL(body, 40);
4699 conn->smb2.server.start_time = BVAL(body, 48);
4701 security_offset = SVAL(body, 56);
4702 security_length = SVAL(body, 58);
4704 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4705 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4709 if (security_length > iov[2].iov_len) {
4710 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4714 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4717 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4721 if (conn->protocol < PROTOCOL_SMB3_10) {
4722 TALLOC_FREE(subreq);
4724 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4725 conn->smb2.server.cipher = SMB2_ENCRYPTION_AES128_CCM;
4727 tevent_req_done(req);
4731 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4732 tevent_req_nterror(req,
4733 NT_STATUS_INVALID_NETWORK_RESPONSE);
4737 negotiate_context_offset = IVAL(body, 60);
4738 if (negotiate_context_offset < security_offset) {
4739 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4743 ctx_ofs = negotiate_context_offset - security_offset;
4744 if (ctx_ofs > iov[2].iov_len) {
4745 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4748 avail = iov[2].iov_len - security_length;
4749 needed = iov[2].iov_len - ctx_ofs;
4750 if (needed > avail) {
4751 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4755 negotiate_context_blob.data = (uint8_t *)iov[2].iov_base;
4756 negotiate_context_blob.length = iov[2].iov_len;
4758 negotiate_context_blob.data += ctx_ofs;
4759 negotiate_context_blob.length -= ctx_ofs;
4761 status = smb2_negotiate_context_parse(state, negotiate_context_blob, &c);
4762 if (tevent_req_nterror(req, status)) {
4766 if (negotiate_context_count != c.num_contexts) {
4767 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4771 preauth = smb2_negotiate_context_find(&c,
4772 SMB2_PREAUTH_INTEGRITY_CAPABILITIES);
4773 if (preauth == NULL) {
4774 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4778 if (preauth->data.length < 6) {
4779 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4783 hash_count = SVAL(preauth->data.data, 0);
4784 salt_length = SVAL(preauth->data.data, 2);
4785 hash_selected = SVAL(preauth->data.data, 4);
4787 if (hash_count != 1) {
4788 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4792 if (preauth->data.length != (6 + salt_length)) {
4793 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4797 if (hash_selected != SMB2_PREAUTH_INTEGRITY_SHA512) {
4798 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4802 cipher = smb2_negotiate_context_find(&c, SMB2_ENCRYPTION_CAPABILITIES);
4803 if (cipher != NULL) {
4804 uint16_t cipher_count;
4806 if (cipher->data.length < 2) {
4807 tevent_req_nterror(req,
4808 NT_STATUS_INVALID_NETWORK_RESPONSE);
4812 cipher_count = SVAL(cipher->data.data, 0);
4814 if (cipher_count > 1) {
4815 tevent_req_nterror(req,
4816 NT_STATUS_INVALID_NETWORK_RESPONSE);
4820 if (cipher->data.length != (2 + 2 * cipher_count)) {
4821 tevent_req_nterror(req,
4822 NT_STATUS_INVALID_NETWORK_RESPONSE);
4826 if (cipher_count == 1) {
4827 uint16_t cipher_selected;
4829 cipher_selected = SVAL(cipher->data.data, 2);
4831 switch (cipher_selected) {
4832 case SMB2_ENCRYPTION_AES128_GCM:
4833 case SMB2_ENCRYPTION_AES128_CCM:
4834 conn->smb2.server.cipher = cipher_selected;
4840 /* First we hash the request */
4841 smb2cli_req_get_sent_iov(subreq, sent_iov);
4842 samba_SHA512_Init(&sctx);
4843 samba_SHA512_Update(&sctx, conn->smb2.preauth_sha512,
4844 sizeof(conn->smb2.preauth_sha512));
4845 for (i = 0; i < 3; i++) {
4846 samba_SHA512_Update(&sctx, sent_iov[i].iov_base, sent_iov[i].iov_len);
4848 samba_SHA512_Final(conn->smb2.preauth_sha512, &sctx);
4849 TALLOC_FREE(subreq);
4851 /* And now we hash the response */
4852 samba_SHA512_Init(&sctx);
4853 samba_SHA512_Update(&sctx, conn->smb2.preauth_sha512,
4854 sizeof(conn->smb2.preauth_sha512));
4855 for (i = 0; i < 3; i++) {
4856 samba_SHA512_Update(&sctx, iov[i].iov_base, iov[i].iov_len);
4858 samba_SHA512_Final(conn->smb2.preauth_sha512, &sctx);
4860 tevent_req_done(req);
4863 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4864 TALLOC_CTX *tmp_mem,
4867 size_t num_pending = talloc_array_length(conn->pending);
4868 struct tevent_req *subreq;
4869 struct smbXcli_req_state *substate;
4870 struct tevent_req *req;
4871 uint32_t protocol_magic;
4872 size_t inbuf_len = smb_len_nbt(inbuf);
4874 if (num_pending != 1) {
4875 return NT_STATUS_INTERNAL_ERROR;
4878 if (inbuf_len < 4) {
4879 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4882 subreq = conn->pending[0];
4883 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4884 req = tevent_req_callback_data(subreq, struct tevent_req);
4886 protocol_magic = IVAL(inbuf, 4);
4888 switch (protocol_magic) {
4890 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4891 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4892 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4895 if (substate->smb2.recv_iov == NULL) {
4897 * For the SMB1 negprot we have move it.
4899 substate->smb2.recv_iov = substate->smb1.recv_iov;
4900 substate->smb1.recv_iov = NULL;
4904 * we got an SMB2 answer, which consumed sequence number 0
4905 * so we need to use 1 as the next one.
4907 * we also need to set the current credits to 0
4908 * as we consumed the initial one. The SMB2 answer
4909 * hopefully grant us a new credit.
4912 conn->smb2.cur_credits = 0;
4913 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4914 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4915 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4918 DEBUG(10, ("Got non-SMB PDU\n"));
4919 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4922 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4924 return tevent_req_simple_recv_ntstatus(req);
4927 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4928 uint32_t timeout_msec,
4929 enum protocol_types min_protocol,
4930 enum protocol_types max_protocol)
4932 TALLOC_CTX *frame = talloc_stackframe();
4933 struct tevent_context *ev;
4934 struct tevent_req *req;
4935 NTSTATUS status = NT_STATUS_NO_MEMORY;
4938 if (smbXcli_conn_has_async_calls(conn)) {
4940 * Can't use sync call while an async call is in flight
4942 status = NT_STATUS_INVALID_PARAMETER_MIX;
4945 ev = samba_tevent_context_init(frame);
4949 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4950 min_protocol, max_protocol);
4954 ok = tevent_req_poll_ntstatus(req, ev, &status);
4958 status = smbXcli_negprot_recv(req);
4964 struct smb2cli_validate_negotiate_info_state {
4965 struct smbXcli_conn *conn;
4966 DATA_BLOB in_input_buffer;
4967 DATA_BLOB in_output_buffer;
4968 DATA_BLOB out_input_buffer;
4969 DATA_BLOB out_output_buffer;
4973 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq);
4975 struct tevent_req *smb2cli_validate_negotiate_info_send(TALLOC_CTX *mem_ctx,
4976 struct tevent_context *ev,
4977 struct smbXcli_conn *conn,
4978 uint32_t timeout_msec,
4979 struct smbXcli_session *session,
4980 struct smbXcli_tcon *tcon)
4982 struct tevent_req *req;
4983 struct smb2cli_validate_negotiate_info_state *state;
4985 uint16_t dialect_count = 0;
4986 struct tevent_req *subreq;
4987 bool _save_should_sign;
4990 req = tevent_req_create(mem_ctx, &state,
4991 struct smb2cli_validate_negotiate_info_state);
4997 state->in_input_buffer = data_blob_talloc_zero(state,
4998 4 + 16 + 1 + 1 + 2);
4999 if (tevent_req_nomem(state->in_input_buffer.data, req)) {
5000 return tevent_req_post(req, ev);
5002 buf = state->in_input_buffer.data;
5004 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
5005 SIVAL(buf, 0, conn->smb2.client.capabilities);
5007 SIVAL(buf, 0, 0); /* Capabilities */
5009 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
5013 status = GUID_to_ndr_blob(&conn->smb2.client.guid,
5015 if (!NT_STATUS_IS_OK(status)) {
5018 memcpy(buf+4, blob.data, 16); /* ClientGuid */
5020 memset(buf+4, 0, 16); /* ClientGuid */
5022 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
5023 SCVAL(buf, 20, conn->smb2.client.security_mode);
5027 SCVAL(buf, 21, 0); /* reserved */
5029 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
5033 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
5037 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
5041 if (smb2cli_prots[i].proto == state->conn->protocol) {
5042 state->dialect = smb2cli_prots[i].smb2_dialect;
5045 ofs = state->in_input_buffer.length;
5046 ok = data_blob_realloc(state, &state->in_input_buffer,
5049 tevent_req_oom(req);
5050 return tevent_req_post(req, ev);
5053 buf = state->in_input_buffer.data;
5054 SSVAL(buf, ofs, smb2cli_prots[i].smb2_dialect);
5058 buf = state->in_input_buffer.data;
5059 SSVAL(buf, 22, dialect_count);
5061 _save_should_sign = smb2cli_tcon_is_signing_on(tcon);
5062 smb2cli_tcon_should_sign(tcon, true);
5063 subreq = smb2cli_ioctl_send(state, ev, conn,
5064 timeout_msec, session, tcon,
5065 UINT64_MAX, /* in_fid_persistent */
5066 UINT64_MAX, /* in_fid_volatile */
5067 FSCTL_VALIDATE_NEGOTIATE_INFO,
5068 0, /* in_max_input_length */
5069 &state->in_input_buffer,
5070 24, /* in_max_output_length */
5071 &state->in_output_buffer,
5072 SMB2_IOCTL_FLAG_IS_FSCTL);
5073 smb2cli_tcon_should_sign(tcon, _save_should_sign);
5074 if (tevent_req_nomem(subreq, req)) {
5075 return tevent_req_post(req, ev);
5077 tevent_req_set_callback(subreq,
5078 smb2cli_validate_negotiate_info_done,
5084 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq)
5086 struct tevent_req *req =
5087 tevent_req_callback_data(subreq,
5089 struct smb2cli_validate_negotiate_info_state *state =
5090 tevent_req_data(req,
5091 struct smb2cli_validate_negotiate_info_state);
5094 uint32_t capabilities;
5095 DATA_BLOB guid_blob;
5096 struct GUID server_guid;
5097 uint16_t security_mode;
5100 status = smb2cli_ioctl_recv(subreq, state,
5101 &state->out_input_buffer,
5102 &state->out_output_buffer);
5103 TALLOC_FREE(subreq);
5104 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
5106 * The response was signed, but not supported
5108 * Older Windows and Samba releases return
5109 * NT_STATUS_FILE_CLOSED.
5111 tevent_req_done(req);
5114 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_DEVICE_REQUEST)) {
5116 * The response was signed, but not supported
5118 * This is returned by the NTVFS based Samba 4.x file server
5121 tevent_req_done(req);
5124 if (NT_STATUS_EQUAL(status, NT_STATUS_FS_DRIVER_REQUIRED)) {
5126 * The response was signed, but not supported
5128 * This is returned by the NTVFS based Samba 4.x file server
5131 tevent_req_done(req);
5134 if (tevent_req_nterror(req, status)) {
5138 if (state->out_output_buffer.length != 24) {
5139 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5143 buf = state->out_output_buffer.data;
5145 capabilities = IVAL(buf, 0);
5146 guid_blob = data_blob_const(buf + 4, 16);
5147 status = GUID_from_data_blob(&guid_blob, &server_guid);
5148 if (tevent_req_nterror(req, status)) {
5151 security_mode = CVAL(buf, 20);
5152 dialect = SVAL(buf, 22);
5154 if (capabilities != state->conn->smb2.server.capabilities) {
5155 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5159 if (!GUID_equal(&server_guid, &state->conn->smb2.server.guid)) {
5160 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5164 if (security_mode != state->conn->smb2.server.security_mode) {
5165 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5169 if (dialect != state->dialect) {
5170 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5174 tevent_req_done(req);
5177 NTSTATUS smb2cli_validate_negotiate_info_recv(struct tevent_req *req)
5179 return tevent_req_simple_recv_ntstatus(req);
5182 static int smbXcli_session_destructor(struct smbXcli_session *session)
5184 if (session->conn == NULL) {
5188 DLIST_REMOVE(session->conn->sessions, session);
5192 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
5193 struct smbXcli_conn *conn)
5195 struct smbXcli_session *session;
5197 session = talloc_zero(mem_ctx, struct smbXcli_session);
5198 if (session == NULL) {
5201 session->smb2 = talloc_zero(session, struct smb2cli_session);
5202 if (session->smb2 == NULL) {
5203 talloc_free(session);
5206 talloc_set_destructor(session, smbXcli_session_destructor);
5208 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
5209 session->conn = conn;
5211 memcpy(session->smb2_channel.preauth_sha512,
5212 conn->smb2.preauth_sha512,
5213 sizeof(session->smb2_channel.preauth_sha512));
5218 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
5219 struct smbXcli_session *src)
5221 struct smbXcli_session *session;
5223 session = talloc_zero(mem_ctx, struct smbXcli_session);
5224 if (session == NULL) {
5227 session->smb2 = talloc_zero(session, struct smb2cli_session);
5228 if (session->smb2 == NULL) {
5229 talloc_free(session);
5233 session->conn = src->conn;
5234 *session->smb2 = *src->smb2;
5235 session->smb2_channel = src->smb2_channel;
5236 session->disconnect_expired = src->disconnect_expired;
5238 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
5239 talloc_set_destructor(session, smbXcli_session_destructor);
5244 bool smbXcli_session_is_authenticated(struct smbXcli_session *session)
5246 const DATA_BLOB *application_key;
5248 if (session->conn == NULL) {
5253 * If we have an application key we had a session key negotiated
5256 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5257 application_key = &session->smb2->application_key;
5259 application_key = &session->smb1.application_key;
5262 if (application_key->length == 0) {
5269 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
5270 TALLOC_CTX *mem_ctx,
5273 const DATA_BLOB *application_key;
5275 *key = data_blob_null;
5277 if (session->conn == NULL) {
5278 return NT_STATUS_NO_USER_SESSION_KEY;
5281 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5282 application_key = &session->smb2->application_key;
5284 application_key = &session->smb1.application_key;
5287 if (application_key->length == 0) {
5288 return NT_STATUS_NO_USER_SESSION_KEY;
5291 *key = data_blob_dup_talloc(mem_ctx, *application_key);
5292 if (key->data == NULL) {
5293 return NT_STATUS_NO_MEMORY;
5296 return NT_STATUS_OK;
5299 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
5301 session->disconnect_expired = true;
5304 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
5306 return session->smb1.session_id;
5309 void smb1cli_session_set_id(struct smbXcli_session *session,
5310 uint16_t session_id)
5312 session->smb1.session_id = session_id;
5315 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
5316 const DATA_BLOB _session_key)
5318 struct smbXcli_conn *conn = session->conn;
5319 uint8_t session_key[16];
5322 return NT_STATUS_INVALID_PARAMETER_MIX;
5325 if (session->smb1.application_key.length != 0) {
5327 * TODO: do not allow this...
5329 * return NT_STATUS_INVALID_PARAMETER_MIX;
5331 data_blob_clear_free(&session->smb1.application_key);
5332 session->smb1.protected_key = false;
5335 if (_session_key.length == 0) {
5336 return NT_STATUS_OK;
5339 ZERO_STRUCT(session_key);
5340 memcpy(session_key, _session_key.data,
5341 MIN(_session_key.length, sizeof(session_key)));
5343 session->smb1.application_key = data_blob_talloc(session,
5345 sizeof(session_key));
5346 ZERO_STRUCT(session_key);
5347 if (session->smb1.application_key.data == NULL) {
5348 return NT_STATUS_NO_MEMORY;
5351 session->smb1.protected_key = false;
5353 return NT_STATUS_OK;
5356 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
5358 if (session->smb1.protected_key) {
5359 /* already protected */
5360 return NT_STATUS_OK;
5363 if (session->smb1.application_key.length != 16) {
5364 return NT_STATUS_INVALID_PARAMETER_MIX;
5367 smb_key_derivation(session->smb1.application_key.data,
5368 session->smb1.application_key.length,
5369 session->smb1.application_key.data);
5371 session->smb1.protected_key = true;
5373 return NT_STATUS_OK;
5376 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
5378 struct smbXcli_conn *conn = session->conn;
5379 uint8_t security_mode = 0;
5382 return security_mode;
5385 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
5386 if (conn->mandatory_signing) {
5387 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
5390 return security_mode;
5393 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
5395 return session->smb2->session_id;
5398 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
5400 return session->smb2->session_flags;
5403 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
5404 uint64_t session_id,
5405 uint16_t session_flags)
5407 session->smb2->session_id = session_id;
5408 session->smb2->session_flags = session_flags;
5411 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
5413 session->smb2->channel_sequence += 1;
5416 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session *session,
5417 uint16_t channel_sequence)
5421 prev_cs = session->smb2->channel_sequence;
5422 session->smb2->channel_sequence = channel_sequence;
5427 void smb2cli_session_start_replay(struct smbXcli_session *session)
5429 session->smb2->replay_active = true;
5432 void smb2cli_session_stop_replay(struct smbXcli_session *session)
5434 session->smb2->replay_active = false;
5437 NTSTATUS smb2cli_session_update_preauth(struct smbXcli_session *session,
5438 const struct iovec *iov)
5440 struct hc_sha512state sctx;
5443 if (session->conn == NULL) {
5444 return NT_STATUS_INTERNAL_ERROR;
5447 if (session->conn->protocol < PROTOCOL_SMB3_10) {
5448 return NT_STATUS_OK;
5451 if (session->smb2_channel.signing_key.length != 0) {
5452 return NT_STATUS_OK;
5455 samba_SHA512_Init(&sctx);
5456 samba_SHA512_Update(&sctx, session->smb2_channel.preauth_sha512,
5457 sizeof(session->smb2_channel.preauth_sha512));
5458 for (i = 0; i < 3; i++) {
5459 samba_SHA512_Update(&sctx, iov[i].iov_base, iov[i].iov_len);
5461 samba_SHA512_Final(session->smb2_channel.preauth_sha512, &sctx);
5463 return NT_STATUS_OK;
5466 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
5467 const DATA_BLOB _session_key,
5468 const struct iovec *recv_iov)
5470 struct smbXcli_conn *conn = session->conn;
5471 uint16_t no_sign_flags;
5472 uint8_t session_key[16];
5473 bool check_signature = true;
5476 struct _derivation {
5481 struct _derivation signing;
5482 struct _derivation encryption;
5483 struct _derivation decryption;
5484 struct _derivation application;
5486 size_t nonce_size = 0;
5489 return NT_STATUS_INVALID_PARAMETER_MIX;
5492 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
5493 return NT_STATUS_INVALID_PARAMETER_MIX;
5496 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
5498 if (session->smb2->session_flags & no_sign_flags) {
5499 session->smb2->should_sign = false;
5500 return NT_STATUS_OK;
5503 if (session->smb2->signing_key.length != 0) {
5504 return NT_STATUS_INVALID_PARAMETER_MIX;
5507 if (conn->protocol >= PROTOCOL_SMB3_10) {
5508 struct _derivation *d;
5511 p = data_blob_const(session->smb2_channel.preauth_sha512,
5512 sizeof(session->smb2_channel.preauth_sha512));
5514 d = &derivation.signing;
5515 d->label = data_blob_string_const_null("SMBSigningKey");
5518 d = &derivation.encryption;
5519 d->label = data_blob_string_const_null("SMBC2SCipherKey");
5522 d = &derivation.decryption;
5523 d->label = data_blob_string_const_null("SMBS2CCipherKey");
5526 d = &derivation.application;
5527 d->label = data_blob_string_const_null("SMBAppKey");
5530 } else if (conn->protocol >= PROTOCOL_SMB2_24) {
5531 struct _derivation *d;
5533 d = &derivation.signing;
5534 d->label = data_blob_string_const_null("SMB2AESCMAC");
5535 d->context = data_blob_string_const_null("SmbSign");
5537 d = &derivation.encryption;
5538 d->label = data_blob_string_const_null("SMB2AESCCM");
5539 d->context = data_blob_string_const_null("ServerIn ");
5541 d = &derivation.decryption;
5542 d->label = data_blob_string_const_null("SMB2AESCCM");
5543 d->context = data_blob_string_const_null("ServerOut");
5545 d = &derivation.application;
5546 d->label = data_blob_string_const_null("SMB2APP");
5547 d->context = data_blob_string_const_null("SmbRpc");
5550 ZERO_STRUCT(session_key);
5551 memcpy(session_key, _session_key.data,
5552 MIN(_session_key.length, sizeof(session_key)));
5554 session->smb2->signing_key = data_blob_talloc(session,
5556 sizeof(session_key));
5557 if (session->smb2->signing_key.data == NULL) {
5558 ZERO_STRUCT(session_key);
5559 return NT_STATUS_NO_MEMORY;
5562 if (conn->protocol >= PROTOCOL_SMB2_24) {
5563 struct _derivation *d = &derivation.signing;
5565 smb2_key_derivation(session_key, sizeof(session_key),
5566 d->label.data, d->label.length,
5567 d->context.data, d->context.length,
5568 session->smb2->signing_key.data);
5571 session->smb2->encryption_key = data_blob_dup_talloc(session,
5572 session->smb2->signing_key);
5573 if (session->smb2->encryption_key.data == NULL) {
5574 ZERO_STRUCT(session_key);
5575 return NT_STATUS_NO_MEMORY;
5578 if (conn->protocol >= PROTOCOL_SMB2_24) {
5579 struct _derivation *d = &derivation.encryption;
5581 smb2_key_derivation(session_key, sizeof(session_key),
5582 d->label.data, d->label.length,
5583 d->context.data, d->context.length,
5584 session->smb2->encryption_key.data);
5587 session->smb2->decryption_key = data_blob_dup_talloc(session,
5588 session->smb2->signing_key);
5589 if (session->smb2->decryption_key.data == NULL) {
5590 ZERO_STRUCT(session_key);
5591 return NT_STATUS_NO_MEMORY;
5594 if (conn->protocol >= PROTOCOL_SMB2_24) {
5595 struct _derivation *d = &derivation.decryption;
5597 smb2_key_derivation(session_key, sizeof(session_key),
5598 d->label.data, d->label.length,
5599 d->context.data, d->context.length,
5600 session->smb2->decryption_key.data);
5603 session->smb2->application_key = data_blob_dup_talloc(session,
5604 session->smb2->signing_key);
5605 if (session->smb2->application_key.data == NULL) {
5606 ZERO_STRUCT(session_key);
5607 return NT_STATUS_NO_MEMORY;
5610 if (conn->protocol >= PROTOCOL_SMB2_24) {
5611 struct _derivation *d = &derivation.application;
5613 smb2_key_derivation(session_key, sizeof(session_key),
5614 d->label.data, d->label.length,
5615 d->context.data, d->context.length,
5616 session->smb2->application_key.data);
5618 ZERO_STRUCT(session_key);
5620 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
5621 session->smb2->signing_key);
5622 if (session->smb2_channel.signing_key.data == NULL) {
5623 return NT_STATUS_NO_MEMORY;
5626 check_signature = conn->mandatory_signing;
5628 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
5629 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
5631 * Sadly some vendors don't sign the
5632 * final SMB2 session setup response
5634 * At least Windows and Samba are always doing this
5635 * if there's a session key available.
5637 * We only check the signature if it's mandatory
5638 * or SMB2_HDR_FLAG_SIGNED is provided.
5640 check_signature = true;
5643 if (conn->protocol >= PROTOCOL_SMB3_10) {
5644 check_signature = true;
5647 if (check_signature) {
5648 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5649 session->conn->protocol,
5651 if (!NT_STATUS_IS_OK(status)) {
5656 session->smb2->should_sign = false;
5657 session->smb2->should_encrypt = false;
5659 if (conn->desire_signing) {
5660 session->smb2->should_sign = true;
5663 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
5664 session->smb2->should_sign = true;
5667 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
5668 session->smb2->should_encrypt = true;
5671 if (conn->protocol < PROTOCOL_SMB2_24) {
5672 session->smb2->should_encrypt = false;
5675 if (conn->smb2.server.cipher == 0) {
5676 session->smb2->should_encrypt = false;
5680 * CCM and GCM algorithms must never have their
5681 * nonce wrap, or the security of the whole
5682 * communication and the keys is destroyed.
5683 * We must drop the connection once we have
5684 * transfered too much data.
5686 * NOTE: We assume nonces greater than 8 bytes.
5688 generate_random_buffer((uint8_t *)&session->smb2->nonce_high_random,
5689 sizeof(session->smb2->nonce_high_random));
5690 switch (conn->smb2.server.cipher) {
5691 case SMB2_ENCRYPTION_AES128_CCM:
5692 nonce_size = AES_CCM_128_NONCE_SIZE;
5694 case SMB2_ENCRYPTION_AES128_GCM:
5695 nonce_size = AES_GCM_128_IV_SIZE;
5701 session->smb2->nonce_high_max = SMB2_NONCE_HIGH_MAX(nonce_size);
5702 session->smb2->nonce_high = 0;
5703 session->smb2->nonce_low = 0;
5705 return NT_STATUS_OK;
5708 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
5709 struct smbXcli_session *session1,
5710 struct smbXcli_conn *conn,
5711 struct smbXcli_session **_session2)
5713 struct smbXcli_session *session2;
5715 if (session1->smb2->signing_key.length == 0) {
5716 return NT_STATUS_INVALID_PARAMETER_MIX;
5720 return NT_STATUS_INVALID_PARAMETER_MIX;
5723 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
5724 if (session2 == NULL) {
5725 return NT_STATUS_NO_MEMORY;
5727 session2->smb2 = talloc_reference(session2, session1->smb2);
5728 if (session2->smb2 == NULL) {
5729 talloc_free(session2);
5730 return NT_STATUS_NO_MEMORY;
5733 talloc_set_destructor(session2, smbXcli_session_destructor);
5734 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
5735 session2->conn = conn;
5737 memcpy(session2->smb2_channel.preauth_sha512,
5738 conn->smb2.preauth_sha512,
5739 sizeof(session2->smb2_channel.preauth_sha512));
5741 *_session2 = session2;
5742 return NT_STATUS_OK;
5745 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
5746 const DATA_BLOB _channel_key,
5747 const struct iovec *recv_iov)
5749 struct smbXcli_conn *conn = session->conn;
5750 uint8_t channel_key[16];
5752 struct _derivation {
5757 struct _derivation signing;
5761 return NT_STATUS_INVALID_PARAMETER_MIX;
5764 if (session->smb2_channel.signing_key.length != 0) {
5765 return NT_STATUS_INVALID_PARAMETER_MIX;
5768 if (conn->protocol >= PROTOCOL_SMB3_10) {
5769 struct _derivation *d;
5772 p = data_blob_const(session->smb2_channel.preauth_sha512,
5773 sizeof(session->smb2_channel.preauth_sha512));
5775 d = &derivation.signing;
5776 d->label = data_blob_string_const_null("SMBSigningKey");
5778 } else if (conn->protocol >= PROTOCOL_SMB2_24) {
5779 struct _derivation *d;
5781 d = &derivation.signing;
5782 d->label = data_blob_string_const_null("SMB2AESCMAC");
5783 d->context = data_blob_string_const_null("SmbSign");
5786 ZERO_STRUCT(channel_key);
5787 memcpy(channel_key, _channel_key.data,
5788 MIN(_channel_key.length, sizeof(channel_key)));
5790 session->smb2_channel.signing_key = data_blob_talloc(session,
5792 sizeof(channel_key));
5793 if (session->smb2_channel.signing_key.data == NULL) {
5794 ZERO_STRUCT(channel_key);
5795 return NT_STATUS_NO_MEMORY;
5798 if (conn->protocol >= PROTOCOL_SMB2_24) {
5799 struct _derivation *d = &derivation.signing;
5801 smb2_key_derivation(channel_key, sizeof(channel_key),
5802 d->label.data, d->label.length,
5803 d->context.data, d->context.length,
5804 session->smb2_channel.signing_key.data);
5806 ZERO_STRUCT(channel_key);
5808 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5809 session->conn->protocol,
5811 if (!NT_STATUS_IS_OK(status)) {
5815 return NT_STATUS_OK;
5818 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
5820 if (session->smb2->should_encrypt) {
5821 return NT_STATUS_OK;
5824 if (session->conn->protocol < PROTOCOL_SMB2_24) {
5825 return NT_STATUS_NOT_SUPPORTED;
5828 if (session->conn->smb2.server.cipher == 0) {
5829 return NT_STATUS_NOT_SUPPORTED;
5832 if (session->smb2->signing_key.data == NULL) {
5833 return NT_STATUS_NOT_SUPPORTED;
5835 session->smb2->should_encrypt = true;
5836 return NT_STATUS_OK;
5839 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
5841 struct smbXcli_tcon *tcon;
5843 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
5851 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
5852 uint32_t fs_attributes)
5854 tcon->fs_attributes = fs_attributes;
5857 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
5859 return tcon->fs_attributes;
5862 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
5868 if (tcon->is_smb1) {
5869 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
5876 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
5883 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
5885 return tcon->smb1.tcon_id;
5888 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
5890 tcon->is_smb1 = true;
5891 tcon->smb1.tcon_id = tcon_id;
5894 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
5896 uint16_t optional_support,
5897 uint32_t maximal_access,
5898 uint32_t guest_maximal_access,
5899 const char *service,
5900 const char *fs_type)
5902 tcon->is_smb1 = true;
5903 tcon->fs_attributes = 0;
5904 tcon->smb1.tcon_id = tcon_id;
5905 tcon->smb1.optional_support = optional_support;
5906 tcon->smb1.maximal_access = maximal_access;
5907 tcon->smb1.guest_maximal_access = guest_maximal_access;
5909 TALLOC_FREE(tcon->smb1.service);
5910 tcon->smb1.service = talloc_strdup(tcon, service);
5911 if (service != NULL && tcon->smb1.service == NULL) {
5915 TALLOC_FREE(tcon->smb1.fs_type);
5916 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5917 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5924 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5926 return tcon->smb2.tcon_id;
5929 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5931 return tcon->smb2.capabilities;
5934 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5935 struct smbXcli_session *session,
5939 uint32_t capabilities,
5940 uint32_t maximal_access)
5942 tcon->is_smb1 = false;
5943 tcon->fs_attributes = 0;
5944 tcon->smb2.tcon_id = tcon_id;
5945 tcon->smb2.type = type;
5946 tcon->smb2.flags = flags;
5947 tcon->smb2.capabilities = capabilities;
5948 tcon->smb2.maximal_access = maximal_access;
5950 tcon->smb2.should_sign = false;
5951 tcon->smb2.should_encrypt = false;
5953 if (session == NULL) {
5957 tcon->smb2.should_sign = session->smb2->should_sign;
5958 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5960 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5961 tcon->smb2.should_encrypt = true;
5965 void smb2cli_tcon_should_sign(struct smbXcli_tcon *tcon,
5968 tcon->smb2.should_sign = should_sign;
5971 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon *tcon)
5973 if (tcon->smb2.should_encrypt) {
5977 return tcon->smb2.should_sign;
5980 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon *tcon,
5981 bool should_encrypt)
5983 tcon->smb2.should_encrypt = should_encrypt;
5986 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
5988 return tcon->smb2.should_encrypt;