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"
40 struct smbXcli_session;
46 struct sockaddr_storage local_ss;
47 struct sockaddr_storage remote_ss;
48 const char *remote_name;
50 struct tevent_queue *outgoing;
51 struct tevent_req **pending;
52 struct tevent_req *read_smb_req;
54 enum protocol_types min_protocol;
55 enum protocol_types max_protocol;
56 enum protocol_types protocol;
59 bool mandatory_signing;
62 * The incoming dispatch function should return:
63 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
64 * - NT_STATUS_OK, if no more processing is desired, e.g.
65 * the dispatch function called
67 * - All other return values disconnect the connection.
69 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
75 uint32_t capabilities;
80 uint32_t capabilities;
83 uint16_t security_mode;
92 const char *workgroup;
98 uint32_t capabilities;
103 struct smb_signing_state *signing;
104 struct smb_trans_enc_state *trans_enc;
106 struct tevent_req *read_braw_req;
111 uint32_t capabilities;
112 uint16_t security_mode;
117 uint32_t capabilities;
118 uint16_t security_mode;
120 uint32_t max_trans_size;
121 uint32_t max_read_size;
122 uint32_t max_write_size;
130 uint16_t cur_credits;
131 uint16_t max_credits;
133 uint8_t preauth_sha512[64];
136 struct smbXcli_session *sessions;
139 struct smb2cli_session {
141 uint16_t session_flags;
142 DATA_BLOB application_key;
143 DATA_BLOB signing_key;
146 DATA_BLOB encryption_key;
147 DATA_BLOB decryption_key;
150 uint16_t channel_sequence;
154 struct smbXcli_session {
155 struct smbXcli_session *prev, *next;
156 struct smbXcli_conn *conn;
160 DATA_BLOB application_key;
164 struct smb2cli_session *smb2;
167 DATA_BLOB signing_key;
168 uint8_t preauth_sha512[64];
172 * this should be a short term hack
173 * until the upper layers have implemented
176 bool disconnect_expired;
179 struct smbXcli_tcon {
181 uint32_t fs_attributes;
185 uint16_t optional_support;
186 uint32_t maximal_access;
187 uint32_t guest_maximal_access;
196 uint32_t capabilities;
197 uint32_t maximal_access;
203 struct smbXcli_req_state {
204 struct tevent_context *ev;
205 struct smbXcli_conn *conn;
206 struct smbXcli_session *session; /* maybe NULL */
207 struct smbXcli_tcon *tcon; /* maybe NULL */
209 uint8_t length_hdr[4];
216 /* Space for the header including the wct */
217 uint8_t hdr[HDR_VWV];
220 * For normal requests, smb1cli_req_send chooses a mid.
221 * SecondaryV trans requests need to use the mid of the primary
222 * request, so we need a place to store it.
223 * Assume it is set if != 0.
228 uint8_t bytecount_buf[2];
230 #define MAX_SMB_IOV 10
231 /* length_hdr, hdr, words, byte_count, buffers */
232 struct iovec iov[1 + 3 + MAX_SMB_IOV];
237 struct tevent_req **chained_requests;
240 NTSTATUS recv_status;
241 /* always an array of 3 talloc elements */
242 struct iovec *recv_iov;
246 const uint8_t *fixed;
251 uint8_t transform[SMB2_TF_HDR_SIZE];
252 uint8_t hdr[SMB2_HDR_BODY];
253 uint8_t pad[7]; /* padding space for compounding */
256 * always an array of 3 talloc elements
257 * (without a SMB2_TRANSFORM header!)
261 struct iovec *recv_iov;
264 * the expected max for the response dyn_len
266 uint32_t max_dyn_len;
268 uint16_t credit_charge;
272 uint64_t encryption_session_id;
274 bool signing_skipped;
280 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
283 * NT_STATUS_OK, means we do not notify the callers
285 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
287 while (conn->sessions) {
288 conn->sessions->conn = NULL;
289 DLIST_REMOVE(conn->sessions, conn->sessions);
292 if (conn->smb1.trans_enc) {
293 TALLOC_FREE(conn->smb1.trans_enc);
299 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
301 const char *remote_name,
302 enum smb_signing_setting signing_state,
303 uint32_t smb1_capabilities,
304 struct GUID *client_guid,
305 uint32_t smb2_capabilities)
307 struct smbXcli_conn *conn = NULL;
309 struct sockaddr *sa = NULL;
313 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
319 conn->write_fd = dup(fd);
320 if (conn->write_fd == -1) {
324 conn->remote_name = talloc_strdup(conn, remote_name);
325 if (conn->remote_name == NULL) {
330 ss = (void *)&conn->local_ss;
331 sa = (struct sockaddr *)ss;
332 sa_length = sizeof(conn->local_ss);
333 ret = getsockname(fd, sa, &sa_length);
337 ss = (void *)&conn->remote_ss;
338 sa = (struct sockaddr *)ss;
339 sa_length = sizeof(conn->remote_ss);
340 ret = getpeername(fd, sa, &sa_length);
345 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
346 if (conn->outgoing == NULL) {
349 conn->pending = NULL;
351 conn->min_protocol = PROTOCOL_NONE;
352 conn->max_protocol = PROTOCOL_NONE;
353 conn->protocol = PROTOCOL_NONE;
355 switch (signing_state) {
356 case SMB_SIGNING_OFF:
358 conn->allow_signing = false;
359 conn->desire_signing = false;
360 conn->mandatory_signing = false;
362 case SMB_SIGNING_DEFAULT:
363 case SMB_SIGNING_IF_REQUIRED:
364 /* if the server requires it */
365 conn->allow_signing = true;
366 conn->desire_signing = false;
367 conn->mandatory_signing = false;
369 case SMB_SIGNING_REQUIRED:
371 conn->allow_signing = true;
372 conn->desire_signing = true;
373 conn->mandatory_signing = true;
377 conn->smb1.client.capabilities = smb1_capabilities;
378 conn->smb1.client.max_xmit = UINT16_MAX;
380 conn->smb1.capabilities = conn->smb1.client.capabilities;
381 conn->smb1.max_xmit = 1024;
385 /* initialise signing */
386 conn->smb1.signing = smb_signing_init(conn,
388 conn->desire_signing,
389 conn->mandatory_signing);
390 if (!conn->smb1.signing) {
394 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
395 if (conn->mandatory_signing) {
396 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
399 conn->smb2.client.guid = *client_guid;
401 conn->smb2.client.capabilities = smb2_capabilities;
403 conn->smb2.cur_credits = 1;
404 conn->smb2.max_credits = 0;
406 talloc_set_destructor(conn, smbXcli_conn_destructor);
410 if (conn->write_fd != -1) {
411 close(conn->write_fd);
417 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
423 if (conn->read_fd == -1) {
430 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
432 return conn->protocol;
435 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
437 if (conn->protocol >= PROTOCOL_SMB2_02) {
441 if (conn->smb1.capabilities & CAP_UNICODE) {
448 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
450 set_socket_options(conn->read_fd, options);
453 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
455 return &conn->local_ss;
458 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
460 return &conn->remote_ss;
463 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
465 return conn->remote_name;
468 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
470 if (conn->protocol >= PROTOCOL_SMB2_02) {
477 return conn->smb1.server.max_mux;
480 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
482 if (conn->protocol >= PROTOCOL_SMB2_02) {
483 return conn->smb2.server.system_time;
486 return conn->smb1.server.system_time;
489 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
491 if (conn->protocol >= PROTOCOL_SMB2_02) {
492 return &conn->smb2.server.gss_blob;
495 return &conn->smb1.server.gss_blob;
498 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
500 if (conn->protocol >= PROTOCOL_SMB2_02) {
501 return &conn->smb2.server.guid;
504 return &conn->smb1.server.guid;
507 struct smbXcli_conn_samba_suicide_state {
508 struct smbXcli_conn *conn;
513 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
515 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
516 struct tevent_context *ev,
517 struct smbXcli_conn *conn,
520 struct tevent_req *req, *subreq;
521 struct smbXcli_conn_samba_suicide_state *state;
523 req = tevent_req_create(mem_ctx, &state,
524 struct smbXcli_conn_samba_suicide_state);
529 SIVAL(state->buf, 4, 0x74697865);
530 SCVAL(state->buf, 8, exitcode);
531 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
533 state->iov.iov_base = state->buf;
534 state->iov.iov_len = sizeof(state->buf);
536 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
537 false, &state->iov, 1);
538 if (tevent_req_nomem(subreq, req)) {
539 return tevent_req_post(req, ev);
541 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
545 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
547 struct tevent_req *req = tevent_req_callback_data(
548 subreq, struct tevent_req);
549 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
550 req, struct smbXcli_conn_samba_suicide_state);
554 nwritten = writev_recv(subreq, &err);
556 if (nwritten == -1) {
557 NTSTATUS status = map_nt_error_from_unix_common(err);
558 smbXcli_conn_disconnect(state->conn, status);
561 tevent_req_done(req);
564 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
566 return tevent_req_simple_recv_ntstatus(req);
569 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
572 TALLOC_CTX *frame = talloc_stackframe();
573 struct tevent_context *ev;
574 struct tevent_req *req;
575 NTSTATUS status = NT_STATUS_NO_MEMORY;
578 if (smbXcli_conn_has_async_calls(conn)) {
580 * Can't use sync call while an async call is in flight
582 status = NT_STATUS_INVALID_PARAMETER_MIX;
585 ev = samba_tevent_context_init(frame);
589 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
593 ok = tevent_req_poll(req, ev);
595 status = map_nt_error_from_unix_common(errno);
598 status = smbXcli_conn_samba_suicide_recv(req);
604 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
606 return conn->smb1.capabilities;
609 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
611 return conn->smb1.max_xmit;
614 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
617 uint16_t possible = conn->smb1.server.max_mux;
619 pending = tevent_queue_length(conn->outgoing);
620 if (pending >= possible) {
623 pending += talloc_array_length(conn->pending);
624 if (pending >= possible) {
631 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
633 return conn->smb1.server.session_key;
636 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
638 return conn->smb1.server.challenge;
641 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
643 return conn->smb1.server.security_mode;
646 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
648 return conn->smb1.server.readbraw;
651 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
653 return conn->smb1.server.writebraw;
656 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
658 return conn->smb1.server.lockread;
661 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
663 return conn->smb1.server.writeunlock;
666 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
668 return conn->smb1.server.time_zone;
671 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
672 const DATA_BLOB user_session_key,
673 const DATA_BLOB response)
675 return smb_signing_activate(conn->smb1.signing,
680 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
681 const uint8_t *buf, uint32_t seqnum)
683 const uint8_t *hdr = buf + NBT_HDR_SIZE;
684 size_t len = smb_len_nbt(buf);
686 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
689 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
691 return smb_signing_is_active(conn->smb1.signing);
694 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
695 struct smb_trans_enc_state *es)
697 /* Replace the old state, if any. */
698 if (conn->smb1.trans_enc) {
699 TALLOC_FREE(conn->smb1.trans_enc);
701 conn->smb1.trans_enc = es;
704 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
706 return common_encryption_on(conn->smb1.trans_enc);
710 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
712 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
713 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
715 if (NT_STATUS_IS_OK(status)) {
719 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
723 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
727 * Is the SMB command able to hold an AND_X successor
728 * @param[in] cmd The SMB command in question
729 * @retval Can we add a chained request after "cmd"?
731 bool smb1cli_is_andx_req(uint8_t cmd)
751 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
753 size_t num_pending = talloc_array_length(conn->pending);
756 if (conn->protocol == PROTOCOL_NONE) {
758 * This is what windows sends on the SMB1 Negprot request
759 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
767 result = conn->smb1.mid++;
768 if ((result == 0) || (result == 0xffff)) {
772 for (i=0; i<num_pending; i++) {
773 if (result == smb1cli_req_mid(conn->pending[i])) {
778 if (i == num_pending) {
784 void smbXcli_req_unset_pending(struct tevent_req *req)
786 struct smbXcli_req_state *state =
788 struct smbXcli_req_state);
789 struct smbXcli_conn *conn = state->conn;
790 size_t num_pending = talloc_array_length(conn->pending);
793 if (state->smb1.mid != 0) {
795 * This is a [nt]trans[2] request which waits
796 * for more than one reply.
801 tevent_req_set_cleanup_fn(req, NULL);
803 if (num_pending == 1) {
805 * The pending read_smb tevent_req is a child of
806 * conn->pending. So if nothing is pending anymore, we need to
807 * delete the socket read fde.
809 TALLOC_FREE(conn->pending);
810 conn->read_smb_req = NULL;
814 for (i=0; i<num_pending; i++) {
815 if (req == conn->pending[i]) {
819 if (i == num_pending) {
821 * Something's seriously broken. Just returning here is the
822 * right thing nevertheless, the point of this routine is to
823 * remove ourselves from conn->pending.
829 * Remove ourselves from the conn->pending array
831 for (; i < (num_pending - 1); i++) {
832 conn->pending[i] = conn->pending[i+1];
836 * No NULL check here, we're shrinking by sizeof(void *), and
837 * talloc_realloc just adjusts the size for this.
839 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
844 static void smbXcli_req_cleanup(struct tevent_req *req,
845 enum tevent_req_state req_state)
847 struct smbXcli_req_state *state =
849 struct smbXcli_req_state);
852 case TEVENT_REQ_RECEIVED:
854 * Make sure we really remove it from
855 * the pending array on destruction.
858 smbXcli_req_unset_pending(req);
865 static bool smb1cli_req_cancel(struct tevent_req *req);
866 static bool smb2cli_req_cancel(struct tevent_req *req);
868 static bool smbXcli_req_cancel(struct tevent_req *req)
870 struct smbXcli_req_state *state =
872 struct smbXcli_req_state);
874 if (!smbXcli_conn_is_connected(state->conn)) {
878 if (state->conn->protocol == PROTOCOL_NONE) {
882 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
883 return smb2cli_req_cancel(req);
886 return smb1cli_req_cancel(req);
889 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
891 bool smbXcli_req_set_pending(struct tevent_req *req)
893 struct smbXcli_req_state *state =
895 struct smbXcli_req_state);
896 struct smbXcli_conn *conn;
897 struct tevent_req **pending;
902 if (!smbXcli_conn_is_connected(conn)) {
906 num_pending = talloc_array_length(conn->pending);
908 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
910 if (pending == NULL) {
913 pending[num_pending] = req;
914 conn->pending = pending;
915 tevent_req_set_cleanup_fn(req, smbXcli_req_cleanup);
916 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
918 if (!smbXcli_conn_receive_next(conn)) {
920 * the caller should notify the current request
922 * And all other pending requests get notified
923 * by smbXcli_conn_disconnect().
925 smbXcli_req_unset_pending(req);
926 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
933 static void smbXcli_conn_received(struct tevent_req *subreq);
935 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
937 size_t num_pending = talloc_array_length(conn->pending);
938 struct tevent_req *req;
939 struct smbXcli_req_state *state;
941 if (conn->read_smb_req != NULL) {
945 if (num_pending == 0) {
946 if (conn->smb2.mid < UINT64_MAX) {
947 /* no more pending requests, so we are done for now */
952 * If there are no more SMB2 requests possible,
953 * because we are out of message ids,
954 * we need to disconnect.
956 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
960 req = conn->pending[0];
961 state = tevent_req_data(req, struct smbXcli_req_state);
964 * We're the first ones, add the read_smb request that waits for the
965 * answer from the server
967 conn->read_smb_req = read_smb_send(conn->pending,
970 if (conn->read_smb_req == NULL) {
973 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
977 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
979 struct smbXcli_session *session;
981 tevent_queue_stop(conn->outgoing);
983 if (conn->read_fd != -1) {
984 close(conn->read_fd);
986 if (conn->write_fd != -1) {
987 close(conn->write_fd);
992 session = conn->sessions;
993 if (talloc_array_length(conn->pending) == 0) {
995 * if we do not have pending requests
996 * there is no need to update the channel_sequence
1000 for (; session; session = session->next) {
1001 smb2cli_session_increment_channel_sequence(session);
1005 * Cancel all pending requests. We do not do a for-loop walking
1006 * conn->pending because that array changes in
1007 * smbXcli_req_unset_pending.
1009 while (talloc_array_length(conn->pending) > 0) {
1010 struct tevent_req *req;
1011 struct smbXcli_req_state *state;
1012 struct tevent_req **chain;
1016 req = conn->pending[0];
1017 state = tevent_req_data(req, struct smbXcli_req_state);
1019 if (state->smb1.chained_requests == NULL) {
1021 * We're dead. No point waiting for trans2
1024 state->smb1.mid = 0;
1026 smbXcli_req_unset_pending(req);
1028 if (NT_STATUS_IS_OK(status)) {
1029 /* do not notify the callers */
1034 * we need to defer the callback, because we may notify
1035 * more then one caller.
1037 tevent_req_defer_callback(req, state->ev);
1038 tevent_req_nterror(req, status);
1042 chain = talloc_move(conn, &state->smb1.chained_requests);
1043 num_chained = talloc_array_length(chain);
1045 for (i=0; i<num_chained; i++) {
1047 state = tevent_req_data(req, struct smbXcli_req_state);
1050 * We're dead. No point waiting for trans2
1053 state->smb1.mid = 0;
1055 smbXcli_req_unset_pending(req);
1057 if (NT_STATUS_IS_OK(status)) {
1058 /* do not notify the callers */
1063 * we need to defer the callback, because we may notify
1064 * more than one caller.
1066 tevent_req_defer_callback(req, state->ev);
1067 tevent_req_nterror(req, status);
1074 * Fetch a smb request's mid. Only valid after the request has been sent by
1075 * smb1cli_req_send().
1077 uint16_t smb1cli_req_mid(struct tevent_req *req)
1079 struct smbXcli_req_state *state =
1080 tevent_req_data(req,
1081 struct smbXcli_req_state);
1083 if (state->smb1.mid != 0) {
1084 return state->smb1.mid;
1087 return SVAL(state->smb1.hdr, HDR_MID);
1090 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1092 struct smbXcli_req_state *state =
1093 tevent_req_data(req,
1094 struct smbXcli_req_state);
1096 state->smb1.mid = mid;
1099 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1101 struct smbXcli_req_state *state =
1102 tevent_req_data(req,
1103 struct smbXcli_req_state);
1105 return state->smb1.seqnum;
1108 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1110 struct smbXcli_req_state *state =
1111 tevent_req_data(req,
1112 struct smbXcli_req_state);
1114 state->smb1.seqnum = seqnum;
1117 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1119 ssize_t ret = iov_buflen(iov, count);
1121 /* Ignore the overflow case for now ... */
1125 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1126 const struct iovec *iov,
1132 buflen = iov_buflen(iov, count);
1137 buf = talloc_array(mem_ctx, uint8_t, buflen);
1142 iov_buf(iov, count, buf, buflen);
1147 static void smb1cli_req_flags(enum protocol_types protocol,
1148 uint32_t smb1_capabilities,
1149 uint8_t smb_command,
1150 uint8_t additional_flags,
1151 uint8_t clear_flags,
1153 uint16_t additional_flags2,
1154 uint16_t clear_flags2,
1158 uint16_t flags2 = 0;
1160 if (protocol >= PROTOCOL_LANMAN1) {
1161 flags |= FLAG_CASELESS_PATHNAMES;
1162 flags |= FLAG_CANONICAL_PATHNAMES;
1165 if (protocol >= PROTOCOL_LANMAN2) {
1166 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1167 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1170 if (protocol >= PROTOCOL_NT1) {
1171 flags2 |= FLAGS2_IS_LONG_NAME;
1173 if (smb1_capabilities & CAP_UNICODE) {
1174 flags2 |= FLAGS2_UNICODE_STRINGS;
1176 if (smb1_capabilities & CAP_STATUS32) {
1177 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1179 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1180 flags2 |= FLAGS2_EXTENDED_SECURITY;
1184 flags |= additional_flags;
1185 flags &= ~clear_flags;
1186 flags2 |= additional_flags2;
1187 flags2 &= ~clear_flags2;
1193 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1195 static bool smb1cli_req_cancel(struct tevent_req *req)
1197 struct smbXcli_req_state *state =
1198 tevent_req_data(req,
1199 struct smbXcli_req_state);
1204 struct tevent_req *subreq;
1207 flags = CVAL(state->smb1.hdr, HDR_FLG);
1208 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1209 pid = SVAL(state->smb1.hdr, HDR_PID);
1210 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1211 mid = SVAL(state->smb1.hdr, HDR_MID);
1213 subreq = smb1cli_req_create(state, state->ev,
1223 0, NULL); /* bytes */
1224 if (subreq == NULL) {
1227 smb1cli_req_set_mid(subreq, mid);
1229 status = smb1cli_req_chain_submit(&subreq, 1);
1230 if (!NT_STATUS_IS_OK(status)) {
1231 TALLOC_FREE(subreq);
1234 smb1cli_req_set_mid(subreq, 0);
1236 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1241 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1243 /* we do not care about the result */
1244 TALLOC_FREE(subreq);
1247 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1248 struct tevent_context *ev,
1249 struct smbXcli_conn *conn,
1250 uint8_t smb_command,
1251 uint8_t additional_flags,
1252 uint8_t clear_flags,
1253 uint16_t additional_flags2,
1254 uint16_t clear_flags2,
1255 uint32_t timeout_msec,
1257 struct smbXcli_tcon *tcon,
1258 struct smbXcli_session *session,
1259 uint8_t wct, uint16_t *vwv,
1261 struct iovec *bytes_iov)
1263 struct tevent_req *req;
1264 struct smbXcli_req_state *state;
1266 uint16_t flags2 = 0;
1271 if (iov_count > MAX_SMB_IOV) {
1273 * Should not happen :-)
1278 req = tevent_req_create(mem_ctx, &state,
1279 struct smbXcli_req_state);
1285 state->session = session;
1289 uid = session->smb1.session_id;
1293 tid = tcon->smb1.tcon_id;
1295 if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
1296 clear_flags |= FLAG_CASELESS_PATHNAMES;
1298 /* Default setting, case insensitive. */
1299 additional_flags |= FLAG_CASELESS_PATHNAMES;
1302 if (smbXcli_conn_dfs_supported(conn) &&
1303 smbXcli_tcon_is_dfs_share(tcon))
1305 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
1309 state->smb1.recv_cmd = 0xFF;
1310 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1311 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1312 if (state->smb1.recv_iov == NULL) {
1317 smb1cli_req_flags(conn->protocol,
1318 conn->smb1.capabilities,
1327 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1328 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1329 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1330 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1331 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1332 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1333 SSVAL(state->smb1.hdr, HDR_TID, tid);
1334 SSVAL(state->smb1.hdr, HDR_PID, pid);
1335 SSVAL(state->smb1.hdr, HDR_UID, uid);
1336 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1337 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1339 state->smb1.vwv = vwv;
1341 num_bytes = iov_buflen(bytes_iov, iov_count);
1342 if (num_bytes == -1) {
1344 * I'd love to add a check for num_bytes<=UINT16_MAX here, but
1345 * the smbclient->samba connections can lie and transfer more.
1351 SSVAL(state->smb1.bytecount_buf, 0, num_bytes);
1353 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1354 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1355 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1356 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1357 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1358 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1359 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1360 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1362 if (iov_count != 0) {
1363 memcpy(&state->smb1.iov[4], bytes_iov,
1364 iov_count * sizeof(*bytes_iov));
1366 state->smb1.iov_count = iov_count + 4;
1368 if (timeout_msec > 0) {
1369 struct timeval endtime;
1371 endtime = timeval_current_ofs_msec(timeout_msec);
1372 if (!tevent_req_set_endtime(req, ev, endtime)) {
1377 switch (smb_command) {
1381 state->one_way = true;
1384 state->one_way = true;
1385 state->smb1.one_way_seqnum = true;
1389 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1390 state->one_way = true;
1398 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1399 struct iovec *iov, int iov_count,
1401 bool one_way_seqnum)
1403 TALLOC_CTX *frame = NULL;
1407 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1408 * iovec directly. MD5Update would do that just fine.
1411 if (iov_count < 4) {
1412 return NT_STATUS_INVALID_PARAMETER_MIX;
1414 if (iov[0].iov_len != NBT_HDR_SIZE) {
1415 return NT_STATUS_INVALID_PARAMETER_MIX;
1417 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1418 return NT_STATUS_INVALID_PARAMETER_MIX;
1420 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1421 return NT_STATUS_INVALID_PARAMETER_MIX;
1423 if (iov[3].iov_len != sizeof(uint16_t)) {
1424 return NT_STATUS_INVALID_PARAMETER_MIX;
1427 frame = talloc_stackframe();
1429 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1431 return NT_STATUS_NO_MEMORY;
1434 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1436 smb_signing_sign_pdu(conn->smb1.signing,
1437 buf, talloc_get_size(buf),
1439 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1442 return NT_STATUS_OK;
1445 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1446 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1447 TALLOC_CTX *tmp_mem,
1450 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1451 struct smbXcli_req_state *state,
1452 struct iovec *iov, int iov_count)
1454 struct tevent_req *subreq;
1460 if (!smbXcli_conn_is_connected(state->conn)) {
1461 return NT_STATUS_CONNECTION_DISCONNECTED;
1464 if (state->conn->protocol > PROTOCOL_NT1) {
1465 return NT_STATUS_REVISION_MISMATCH;
1468 if (iov_count < 4) {
1469 return NT_STATUS_INVALID_PARAMETER_MIX;
1471 if (iov[0].iov_len != NBT_HDR_SIZE) {
1472 return NT_STATUS_INVALID_PARAMETER_MIX;
1474 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1475 return NT_STATUS_INVALID_PARAMETER_MIX;
1477 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1478 return NT_STATUS_INVALID_PARAMETER_MIX;
1480 if (iov[3].iov_len != sizeof(uint16_t)) {
1481 return NT_STATUS_INVALID_PARAMETER_MIX;
1484 cmd = CVAL(iov[1].iov_base, HDR_COM);
1485 if (cmd == SMBreadBraw) {
1486 if (smbXcli_conn_has_async_calls(state->conn)) {
1487 return NT_STATUS_INVALID_PARAMETER_MIX;
1489 state->conn->smb1.read_braw_req = req;
1492 if (state->smb1.mid != 0) {
1493 mid = state->smb1.mid;
1495 mid = smb1cli_alloc_mid(state->conn);
1497 SSVAL(iov[1].iov_base, HDR_MID, mid);
1499 nbtlen = iov_buflen(&iov[1], iov_count-1);
1500 if ((nbtlen == -1) || (nbtlen > 0x1FFFF)) {
1501 return NT_STATUS_INVALID_PARAMETER_MIX;
1504 _smb_setlen_nbt(iov[0].iov_base, nbtlen);
1506 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1507 &state->smb1.seqnum,
1508 state->smb1.one_way_seqnum);
1510 if (!NT_STATUS_IS_OK(status)) {
1515 * If we supported multiple encrytion contexts
1516 * here we'd look up based on tid.
1518 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1519 char *buf, *enc_buf;
1521 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1523 return NT_STATUS_NO_MEMORY;
1525 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1526 (char *)buf, &enc_buf);
1528 if (!NT_STATUS_IS_OK(status)) {
1529 DEBUG(0, ("Error in encrypting client message: %s\n",
1530 nt_errstr(status)));
1533 buf = (char *)talloc_memdup(state, enc_buf,
1534 smb_len_nbt(enc_buf)+4);
1537 return NT_STATUS_NO_MEMORY;
1539 iov[0].iov_base = (void *)buf;
1540 iov[0].iov_len = talloc_get_size(buf);
1544 if (state->conn->dispatch_incoming == NULL) {
1545 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1548 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1550 subreq = writev_send(state, state->ev, state->conn->outgoing,
1551 state->conn->write_fd, false, iov, iov_count);
1552 if (subreq == NULL) {
1553 return NT_STATUS_NO_MEMORY;
1555 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1556 return NT_STATUS_OK;
1559 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1560 struct tevent_context *ev,
1561 struct smbXcli_conn *conn,
1562 uint8_t smb_command,
1563 uint8_t additional_flags,
1564 uint8_t clear_flags,
1565 uint16_t additional_flags2,
1566 uint16_t clear_flags2,
1567 uint32_t timeout_msec,
1569 struct smbXcli_tcon *tcon,
1570 struct smbXcli_session *session,
1571 uint8_t wct, uint16_t *vwv,
1573 const uint8_t *bytes)
1575 struct tevent_req *req;
1579 iov.iov_base = discard_const_p(void, bytes);
1580 iov.iov_len = num_bytes;
1582 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1583 additional_flags, clear_flags,
1584 additional_flags2, clear_flags2,
1591 if (!tevent_req_is_in_progress(req)) {
1592 return tevent_req_post(req, ev);
1594 status = smb1cli_req_chain_submit(&req, 1);
1595 if (tevent_req_nterror(req, status)) {
1596 return tevent_req_post(req, ev);
1601 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1603 struct tevent_req *req =
1604 tevent_req_callback_data(subreq,
1606 struct smbXcli_req_state *state =
1607 tevent_req_data(req,
1608 struct smbXcli_req_state);
1612 nwritten = writev_recv(subreq, &err);
1613 TALLOC_FREE(subreq);
1614 if (nwritten == -1) {
1615 NTSTATUS status = map_nt_error_from_unix_common(err);
1616 smbXcli_conn_disconnect(state->conn, status);
1620 if (state->one_way) {
1621 state->inbuf = NULL;
1622 tevent_req_done(req);
1626 if (!smbXcli_req_set_pending(req)) {
1627 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1632 static void smbXcli_conn_received(struct tevent_req *subreq)
1634 struct smbXcli_conn *conn =
1635 tevent_req_callback_data(subreq,
1636 struct smbXcli_conn);
1637 TALLOC_CTX *frame = talloc_stackframe();
1643 if (subreq != conn->read_smb_req) {
1644 DEBUG(1, ("Internal error: cli_smb_received called with "
1645 "unexpected subreq\n"));
1646 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1650 conn->read_smb_req = NULL;
1652 received = read_smb_recv(subreq, frame, &inbuf, &err);
1653 TALLOC_FREE(subreq);
1654 if (received == -1) {
1655 status = map_nt_error_from_unix_common(err);
1656 smbXcli_conn_disconnect(conn, status);
1661 status = conn->dispatch_incoming(conn, frame, inbuf);
1663 if (NT_STATUS_IS_OK(status)) {
1665 * We should not do any more processing
1666 * as the dispatch function called
1667 * tevent_req_done().
1672 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1674 * We got an error, so notify all pending requests
1676 smbXcli_conn_disconnect(conn, status);
1681 * We got NT_STATUS_RETRY, so we may ask for a
1682 * next incoming pdu.
1684 if (!smbXcli_conn_receive_next(conn)) {
1685 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1689 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1690 struct iovec **piov, int *pnum_iov)
1701 size_t min_size = MIN_SMB_SIZE;
1703 buflen = smb_len_tcp(buf);
1706 hdr = buf + NBT_HDR_SIZE;
1708 status = smb1cli_pull_raw_error(hdr);
1709 if (NT_STATUS_IS_ERR(status)) {
1711 * This is an ugly hack to support OS/2
1712 * which skips the byte_count in the DATA block
1713 * on some error responses.
1717 min_size -= sizeof(uint16_t);
1720 if (buflen < min_size) {
1721 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1725 * This returns iovec elements in the following order:
1740 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1742 return NT_STATUS_NO_MEMORY;
1744 iov[0].iov_base = hdr;
1745 iov[0].iov_len = HDR_WCT;
1748 cmd = CVAL(hdr, HDR_COM);
1752 size_t len = buflen - taken;
1754 struct iovec *iov_tmp;
1761 * we need at least WCT
1763 needed = sizeof(uint8_t);
1765 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1766 __location__, (int)len, (int)needed));
1771 * Now we check if the specified words are there
1773 wct = CVAL(hdr, wct_ofs);
1774 needed += wct * sizeof(uint16_t);
1776 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1777 __location__, (int)len, (int)needed));
1781 if ((num_iov == 1) &&
1783 NT_STATUS_IS_ERR(status))
1786 * This is an ugly hack to support OS/2
1787 * which skips the byte_count in the DATA block
1788 * on some error responses.
1792 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1794 if (iov_tmp == NULL) {
1796 return NT_STATUS_NO_MEMORY;
1799 cur = &iov[num_iov];
1803 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1805 cur[1].iov_base = cur[0].iov_base;
1812 * we need at least BCC
1814 needed += sizeof(uint16_t);
1816 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1817 __location__, (int)len, (int)needed));
1822 * Now we check if the specified bytes are there
1824 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1825 bcc = SVAL(hdr, bcc_ofs);
1826 needed += bcc * sizeof(uint8_t);
1828 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1829 __location__, (int)len, (int)needed));
1834 * we allocate 2 iovec structures for words and bytes
1836 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1838 if (iov_tmp == NULL) {
1840 return NT_STATUS_NO_MEMORY;
1843 cur = &iov[num_iov];
1846 cur[0].iov_len = wct * sizeof(uint16_t);
1847 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1848 cur[1].iov_len = bcc * sizeof(uint8_t);
1849 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1853 if (!smb1cli_is_andx_req(cmd)) {
1855 * If the current command does not have AndX chanining
1861 if (wct == 0 && bcc == 0) {
1863 * An empty response also ends the chain,
1864 * most likely with an error.
1870 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1871 __location__, (int)wct, (int)cmd));
1874 cmd = CVAL(cur[0].iov_base, 0);
1877 * If it is the end of the chain we are also done.
1881 wct_ofs = SVAL(cur[0].iov_base, 2);
1883 if (wct_ofs < taken) {
1884 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1886 if (wct_ofs > buflen) {
1887 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1891 * we consumed everything up to the start of the next
1897 remaining = buflen - taken;
1899 if (remaining > 0 && num_iov >= 3) {
1901 * The last DATA block gets the remaining
1902 * bytes, this is needed to support
1903 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1905 iov[num_iov-1].iov_len += remaining;
1909 *pnum_iov = num_iov;
1910 return NT_STATUS_OK;
1914 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1917 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1918 TALLOC_CTX *tmp_mem,
1921 struct tevent_req *req;
1922 struct smbXcli_req_state *state;
1929 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1930 size_t len = smb_len_tcp(inbuf);
1931 struct iovec *iov = NULL;
1933 struct tevent_req **chain = NULL;
1934 size_t num_chained = 0;
1935 size_t num_responses = 0;
1937 if (conn->smb1.read_braw_req != NULL) {
1938 req = conn->smb1.read_braw_req;
1939 conn->smb1.read_braw_req = NULL;
1940 state = tevent_req_data(req, struct smbXcli_req_state);
1942 smbXcli_req_unset_pending(req);
1944 if (state->smb1.recv_iov == NULL) {
1946 * For requests with more than
1947 * one response, we have to readd the
1950 state->smb1.recv_iov = talloc_zero_array(state,
1953 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1954 return NT_STATUS_OK;
1958 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1959 state->smb1.recv_iov[0].iov_len = len;
1960 ZERO_STRUCT(state->smb1.recv_iov[1]);
1961 ZERO_STRUCT(state->smb1.recv_iov[2]);
1963 state->smb1.recv_cmd = SMBreadBraw;
1964 state->smb1.recv_status = NT_STATUS_OK;
1965 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1967 tevent_req_done(req);
1968 return NT_STATUS_OK;
1971 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1972 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1973 DEBUG(10, ("Got non-SMB PDU\n"));
1974 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1978 * If we supported multiple encrytion contexts
1979 * here we'd look up based on tid.
1981 if (common_encryption_on(conn->smb1.trans_enc)
1982 && (CVAL(inbuf, 0) == 0)) {
1983 uint16_t enc_ctx_num;
1985 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1986 if (!NT_STATUS_IS_OK(status)) {
1987 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1988 nt_errstr(status)));
1992 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1993 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1995 conn->smb1.trans_enc->enc_ctx_num));
1996 return NT_STATUS_INVALID_HANDLE;
1999 status = common_decrypt_buffer(conn->smb1.trans_enc,
2001 if (!NT_STATUS_IS_OK(status)) {
2002 DEBUG(10, ("common_decrypt_buffer returned %s\n",
2003 nt_errstr(status)));
2006 inhdr = inbuf + NBT_HDR_SIZE;
2007 len = smb_len_nbt(inbuf);
2010 mid = SVAL(inhdr, HDR_MID);
2011 num_pending = talloc_array_length(conn->pending);
2013 for (i=0; i<num_pending; i++) {
2014 if (mid == smb1cli_req_mid(conn->pending[i])) {
2018 if (i == num_pending) {
2019 /* Dump unexpected reply */
2020 return NT_STATUS_RETRY;
2023 oplock_break = false;
2025 if (mid == 0xffff) {
2027 * Paranoia checks that this is really an oplock break request.
2029 oplock_break = (len == 51); /* hdr + 8 words */
2030 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
2031 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
2032 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
2033 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
2035 if (!oplock_break) {
2036 /* Dump unexpected reply */
2037 return NT_STATUS_RETRY;
2041 req = conn->pending[i];
2042 state = tevent_req_data(req, struct smbXcli_req_state);
2044 if (!oplock_break /* oplock breaks are not signed */
2045 && !smb_signing_check_pdu(conn->smb1.signing,
2046 inhdr, len, state->smb1.seqnum+1)) {
2047 DEBUG(10, ("cli_check_sign_mac failed\n"));
2048 return NT_STATUS_ACCESS_DENIED;
2051 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2053 if (!NT_STATUS_IS_OK(status)) {
2054 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2055 nt_errstr(status)));
2059 cmd = CVAL(inhdr, HDR_COM);
2060 status = smb1cli_pull_raw_error(inhdr);
2062 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2063 (state->session != NULL) && state->session->disconnect_expired)
2066 * this should be a short term hack
2067 * until the upper layers have implemented
2068 * re-authentication.
2073 if (state->smb1.chained_requests == NULL) {
2075 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2078 smbXcli_req_unset_pending(req);
2080 if (state->smb1.recv_iov == NULL) {
2082 * For requests with more than
2083 * one response, we have to readd the
2086 state->smb1.recv_iov = talloc_zero_array(state,
2089 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2090 return NT_STATUS_OK;
2094 state->smb1.recv_cmd = cmd;
2095 state->smb1.recv_status = status;
2096 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2098 state->smb1.recv_iov[0] = iov[0];
2099 state->smb1.recv_iov[1] = iov[1];
2100 state->smb1.recv_iov[2] = iov[2];
2102 if (talloc_array_length(conn->pending) == 0) {
2103 tevent_req_done(req);
2104 return NT_STATUS_OK;
2107 tevent_req_defer_callback(req, state->ev);
2108 tevent_req_done(req);
2109 return NT_STATUS_RETRY;
2112 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2113 num_chained = talloc_array_length(chain);
2114 num_responses = (num_iov - 1)/2;
2116 if (num_responses > num_chained) {
2117 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2120 for (i=0; i<num_chained; i++) {
2121 size_t iov_idx = 1 + (i*2);
2122 struct iovec *cur = &iov[iov_idx];
2126 state = tevent_req_data(req, struct smbXcli_req_state);
2128 smbXcli_req_unset_pending(req);
2131 * as we finish multiple requests here
2132 * we need to defer the callbacks as
2133 * they could destroy our current stack state.
2135 tevent_req_defer_callback(req, state->ev);
2137 if (i >= num_responses) {
2138 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2142 if (state->smb1.recv_iov == NULL) {
2144 * For requests with more than
2145 * one response, we have to readd the
2148 state->smb1.recv_iov = talloc_zero_array(state,
2151 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2156 state->smb1.recv_cmd = cmd;
2158 if (i == (num_responses - 1)) {
2160 * The last request in the chain gets the status
2162 state->smb1.recv_status = status;
2164 cmd = CVAL(cur[0].iov_base, 0);
2165 state->smb1.recv_status = NT_STATUS_OK;
2168 state->inbuf = inbuf;
2171 * Note: here we use talloc_reference() in a way
2172 * that does not expose it to the caller.
2174 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2175 if (tevent_req_nomem(inbuf_ref, req)) {
2179 /* copy the related buffers */
2180 state->smb1.recv_iov[0] = iov[0];
2181 state->smb1.recv_iov[1] = cur[0];
2182 state->smb1.recv_iov[2] = cur[1];
2184 tevent_req_done(req);
2187 return NT_STATUS_RETRY;
2190 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2191 TALLOC_CTX *mem_ctx,
2192 struct iovec **piov,
2196 uint32_t *pvwv_offset,
2197 uint32_t *pnum_bytes,
2199 uint32_t *pbytes_offset,
2201 const struct smb1cli_req_expected_response *expected,
2202 size_t num_expected)
2204 struct smbXcli_req_state *state =
2205 tevent_req_data(req,
2206 struct smbXcli_req_state);
2207 NTSTATUS status = NT_STATUS_OK;
2208 struct iovec *recv_iov = NULL;
2209 uint8_t *hdr = NULL;
2211 uint32_t vwv_offset = 0;
2212 uint16_t *vwv = NULL;
2213 uint32_t num_bytes = 0;
2214 uint32_t bytes_offset = 0;
2215 uint8_t *bytes = NULL;
2217 bool found_status = false;
2218 bool found_size = false;
2232 if (pvwv_offset != NULL) {
2235 if (pnum_bytes != NULL) {
2238 if (pbytes != NULL) {
2241 if (pbytes_offset != NULL) {
2244 if (pinbuf != NULL) {
2248 if (state->inbuf != NULL) {
2249 recv_iov = state->smb1.recv_iov;
2250 state->smb1.recv_iov = NULL;
2251 if (state->smb1.recv_cmd != SMBreadBraw) {
2252 hdr = (uint8_t *)recv_iov[0].iov_base;
2253 wct = recv_iov[1].iov_len/2;
2254 vwv = (uint16_t *)recv_iov[1].iov_base;
2255 vwv_offset = PTR_DIFF(vwv, hdr);
2256 num_bytes = recv_iov[2].iov_len;
2257 bytes = (uint8_t *)recv_iov[2].iov_base;
2258 bytes_offset = PTR_DIFF(bytes, hdr);
2262 if (tevent_req_is_nterror(req, &status)) {
2263 for (i=0; i < num_expected; i++) {
2264 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2265 found_status = true;
2271 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2277 if (num_expected == 0) {
2278 found_status = true;
2282 status = state->smb1.recv_status;
2284 for (i=0; i < num_expected; i++) {
2285 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2289 found_status = true;
2290 if (expected[i].wct == 0) {
2295 if (expected[i].wct == wct) {
2301 if (!found_status) {
2306 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2310 *piov = talloc_move(mem_ctx, &recv_iov);
2322 if (pvwv_offset != NULL) {
2323 *pvwv_offset = vwv_offset;
2325 if (pnum_bytes != NULL) {
2326 *pnum_bytes = num_bytes;
2328 if (pbytes != NULL) {
2331 if (pbytes_offset != NULL) {
2332 *pbytes_offset = bytes_offset;
2334 if (pinbuf != NULL) {
2335 *pinbuf = state->inbuf;
2341 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2348 for (i=0; i<num_reqs; i++) {
2349 struct smbXcli_req_state *state;
2350 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2351 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2352 state->smb1.iov_count-2);
2353 wct_ofs = (wct_ofs + 3) & ~3;
2358 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2360 struct smbXcli_req_state *first_state =
2361 tevent_req_data(reqs[0],
2362 struct smbXcli_req_state);
2363 struct smbXcli_req_state *state;
2365 size_t chain_padding = 0;
2367 struct iovec *iov = NULL;
2368 struct iovec *this_iov;
2372 if (num_reqs == 1) {
2373 return smb1cli_req_writev_submit(reqs[0], first_state,
2374 first_state->smb1.iov,
2375 first_state->smb1.iov_count);
2379 for (i=0; i<num_reqs; i++) {
2380 if (!tevent_req_is_in_progress(reqs[i])) {
2381 return NT_STATUS_INTERNAL_ERROR;
2384 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2386 if (state->smb1.iov_count < 4) {
2387 return NT_STATUS_INVALID_PARAMETER_MIX;
2392 * The NBT and SMB header
2405 iovlen += state->smb1.iov_count - 2;
2408 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2410 return NT_STATUS_NO_MEMORY;
2413 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2414 first_state, reqs, sizeof(*reqs) * num_reqs);
2415 if (first_state->smb1.chained_requests == NULL) {
2417 return NT_STATUS_NO_MEMORY;
2420 wct_offset = HDR_WCT;
2423 for (i=0; i<num_reqs; i++) {
2424 size_t next_padding = 0;
2427 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2429 if (i < num_reqs-1) {
2430 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2431 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2433 TALLOC_FREE(first_state->smb1.chained_requests);
2434 return NT_STATUS_INVALID_PARAMETER_MIX;
2438 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2439 state->smb1.iov_count-2) + 1;
2440 if ((wct_offset % 4) != 0) {
2441 next_padding = 4 - (wct_offset % 4);
2443 wct_offset += next_padding;
2444 vwv = state->smb1.vwv;
2446 if (i < num_reqs-1) {
2447 struct smbXcli_req_state *next_state =
2448 tevent_req_data(reqs[i+1],
2449 struct smbXcli_req_state);
2450 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2452 SSVAL(vwv+1, 0, wct_offset);
2453 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2454 /* properly end the chain */
2455 SCVAL(vwv+0, 0, 0xff);
2456 SCVAL(vwv+0, 1, 0xff);
2462 * The NBT and SMB header
2464 this_iov[0] = state->smb1.iov[0];
2465 this_iov[1] = state->smb1.iov[1];
2469 * This one is a bit subtle. We have to add
2470 * chain_padding bytes between the requests, and we
2471 * have to also include the wct field of the
2472 * subsequent requests. We use the subsequent header
2473 * for the padding, it contains the wct field in its
2476 this_iov[0].iov_len = chain_padding+1;
2477 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2478 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2479 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2484 * copy the words and bytes
2486 memcpy(this_iov, state->smb1.iov+2,
2487 sizeof(struct iovec) * (state->smb1.iov_count-2));
2488 this_iov += state->smb1.iov_count - 2;
2489 chain_padding = next_padding;
2492 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2493 if (nbt_len > first_state->conn->smb1.max_xmit) {
2495 TALLOC_FREE(first_state->smb1.chained_requests);
2496 return NT_STATUS_INVALID_PARAMETER_MIX;
2499 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2500 if (!NT_STATUS_IS_OK(status)) {
2502 TALLOC_FREE(first_state->smb1.chained_requests);
2506 return NT_STATUS_OK;
2509 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2511 return ((tevent_queue_length(conn->outgoing) != 0)
2512 || (talloc_array_length(conn->pending) != 0));
2515 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2517 if (conn->protocol >= PROTOCOL_SMB2_02) {
2518 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2521 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2524 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2526 uint16_t credits = 1;
2528 if (conn->smb2.cur_credits == 0) {
2529 if (max_dyn_len != NULL) {
2535 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2536 credits = conn->smb2.cur_credits;
2539 if (max_dyn_len != NULL) {
2540 *max_dyn_len = credits * 65536;
2546 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2548 return conn->smb2.server.capabilities;
2551 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2553 return conn->smb2.server.security_mode;
2556 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2558 return conn->smb2.server.max_trans_size;
2561 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2563 return conn->smb2.server.max_read_size;
2566 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2568 return conn->smb2.server.max_write_size;
2571 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2572 uint16_t max_credits)
2574 conn->smb2.max_credits = max_credits;
2577 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2579 static bool smb2cli_req_cancel(struct tevent_req *req)
2581 struct smbXcli_req_state *state =
2582 tevent_req_data(req,
2583 struct smbXcli_req_state);
2584 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2585 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2586 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2587 struct smbXcli_tcon *tcon = state->tcon;
2588 struct smbXcli_session *session = state->session;
2589 uint8_t *fixed = state->smb2.pad;
2590 uint16_t fixed_len = 4;
2591 struct tevent_req *subreq;
2592 struct smbXcli_req_state *substate;
2595 SSVAL(fixed, 0, 0x04);
2598 subreq = smb2cli_req_create(state, state->ev,
2606 if (subreq == NULL) {
2609 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2612 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2613 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2615 flags &= SMB2_HDR_FLAG_ASYNC;
2617 if (flags & SMB2_HDR_FLAG_ASYNC) {
2621 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2622 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2623 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2625 status = smb2cli_req_compound_submit(&subreq, 1);
2626 if (!NT_STATUS_IS_OK(status)) {
2627 TALLOC_FREE(subreq);
2631 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2636 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2638 /* we do not care about the result */
2639 TALLOC_FREE(subreq);
2642 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2643 struct tevent_context *ev,
2644 struct smbXcli_conn *conn,
2646 uint32_t additional_flags,
2647 uint32_t clear_flags,
2648 uint32_t timeout_msec,
2649 struct smbXcli_tcon *tcon,
2650 struct smbXcli_session *session,
2651 const uint8_t *fixed,
2655 uint32_t max_dyn_len)
2657 struct tevent_req *req;
2658 struct smbXcli_req_state *state;
2662 bool use_channel_sequence = false;
2663 uint16_t channel_sequence = 0;
2664 bool use_replay_flag = false;
2666 req = tevent_req_create(mem_ctx, &state,
2667 struct smbXcli_req_state);
2674 state->session = session;
2677 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2678 use_channel_sequence = true;
2679 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2680 use_channel_sequence = true;
2683 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_00) {
2684 use_replay_flag = true;
2688 uid = session->smb2->session_id;
2690 if (use_channel_sequence) {
2691 channel_sequence = session->smb2->channel_sequence;
2694 if (use_replay_flag && session->smb2->replay_active) {
2695 additional_flags |= SMB2_HDR_FLAG_REPLAY_OPERATION;
2698 state->smb2.should_sign = session->smb2->should_sign;
2699 state->smb2.should_encrypt = session->smb2->should_encrypt;
2701 if (cmd == SMB2_OP_SESSSETUP &&
2702 session->smb2_channel.signing_key.length == 0 &&
2703 session->smb2->signing_key.length != 0)
2706 * a session bind needs to be signed
2708 state->smb2.should_sign = true;
2711 if (cmd == SMB2_OP_SESSSETUP &&
2712 session->smb2_channel.signing_key.length == 0) {
2713 state->smb2.should_encrypt = false;
2716 if (additional_flags & SMB2_HDR_FLAG_SIGNED) {
2717 if (session->smb2_channel.signing_key.length == 0) {
2718 tevent_req_nterror(req, NT_STATUS_NO_USER_SESSION_KEY);
2722 additional_flags &= ~SMB2_HDR_FLAG_SIGNED;
2723 state->smb2.should_sign = true;
2728 tid = tcon->smb2.tcon_id;
2730 if (tcon->smb2.should_sign) {
2731 state->smb2.should_sign = true;
2733 if (tcon->smb2.should_encrypt) {
2734 state->smb2.should_encrypt = true;
2738 if (state->smb2.should_encrypt) {
2739 state->smb2.should_sign = false;
2742 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2743 if (state->smb2.recv_iov == NULL) {
2748 flags |= additional_flags;
2749 flags &= ~clear_flags;
2751 state->smb2.fixed = fixed;
2752 state->smb2.fixed_len = fixed_len;
2753 state->smb2.dyn = dyn;
2754 state->smb2.dyn_len = dyn_len;
2755 state->smb2.max_dyn_len = max_dyn_len;
2757 if (state->smb2.should_encrypt) {
2758 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2759 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2762 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2763 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2764 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2765 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2766 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2767 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2768 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2769 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2772 case SMB2_OP_CANCEL:
2773 state->one_way = true;
2777 * If this is a dummy request, it will have
2778 * UINT64_MAX as message id.
2779 * If we send on break acknowledgement,
2780 * this gets overwritten later.
2782 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2786 if (timeout_msec > 0) {
2787 struct timeval endtime;
2789 endtime = timeval_current_ofs_msec(timeout_msec);
2790 if (!tevent_req_set_endtime(req, ev, endtime)) {
2798 void smb2cli_req_set_notify_async(struct tevent_req *req)
2800 struct smbXcli_req_state *state =
2801 tevent_req_data(req,
2802 struct smbXcli_req_state);
2804 state->smb2.notify_async = true;
2807 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2808 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2809 TALLOC_CTX *tmp_mem,
2812 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2815 struct smbXcli_req_state *state;
2816 struct tevent_req *subreq;
2818 int i, num_iov, nbt_len;
2820 const DATA_BLOB *encryption_key = NULL;
2821 uint64_t encryption_session_id = 0;
2824 * 1 for the nbt length, optional TRANSFORM
2825 * per request: HDR, fixed, dyn, padding
2826 * -1 because the last one does not need padding
2829 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2831 return NT_STATUS_NO_MEMORY;
2838 * the session of the first request that requires encryption
2839 * specifies the encryption key.
2841 for (i=0; i<num_reqs; i++) {
2842 if (!tevent_req_is_in_progress(reqs[i])) {
2843 return NT_STATUS_INTERNAL_ERROR;
2846 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2848 if (!smbXcli_conn_is_connected(state->conn)) {
2849 return NT_STATUS_CONNECTION_DISCONNECTED;
2852 if ((state->conn->protocol != PROTOCOL_NONE) &&
2853 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2854 return NT_STATUS_REVISION_MISMATCH;
2857 if (state->session == NULL) {
2861 if (!state->smb2.should_encrypt) {
2865 encryption_key = &state->session->smb2->encryption_key;
2866 if (encryption_key->length == 0) {
2867 return NT_STATUS_INVALID_PARAMETER_MIX;
2870 encryption_session_id = state->session->smb2->session_id;
2873 iov[num_iov].iov_base = state->smb2.transform;
2874 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2877 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2878 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2879 state->session->smb2->nonce_low);
2880 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2881 state->session->smb2->nonce_high);
2882 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2883 encryption_session_id);
2885 state->session->smb2->nonce_low += 1;
2886 if (state->session->smb2->nonce_low == 0) {
2887 state->session->smb2->nonce_high += 1;
2888 state->session->smb2->nonce_low += 1;
2891 nbt_len += SMB2_TF_HDR_SIZE;
2895 for (i=0; i<num_reqs; i++) {
2904 const DATA_BLOB *signing_key = NULL;
2906 if (!tevent_req_is_in_progress(reqs[i])) {
2907 return NT_STATUS_INTERNAL_ERROR;
2910 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2912 if (!smbXcli_conn_is_connected(state->conn)) {
2913 return NT_STATUS_CONNECTION_DISCONNECTED;
2916 if ((state->conn->protocol != PROTOCOL_NONE) &&
2917 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2918 return NT_STATUS_REVISION_MISMATCH;
2921 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2922 if (opcode == SMB2_OP_CANCEL) {
2926 avail = UINT64_MAX - state->conn->smb2.mid;
2928 return NT_STATUS_CONNECTION_ABORTED;
2931 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2932 uint32_t max_dyn_len = 1;
2934 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
2935 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
2937 charge = (max_dyn_len - 1)/ 65536 + 1;
2942 charge = MAX(state->smb2.credit_charge, charge);
2944 avail = MIN(avail, state->conn->smb2.cur_credits);
2945 if (avail < charge) {
2946 return NT_STATUS_INTERNAL_ERROR;
2950 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2951 credits = state->conn->smb2.max_credits -
2952 state->conn->smb2.cur_credits;
2954 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2958 mid = state->conn->smb2.mid;
2959 state->conn->smb2.mid += charge;
2960 state->conn->smb2.cur_credits -= charge;
2962 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2963 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2965 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2966 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2969 if (state->session && encryption_key == NULL) {
2971 * We prefer the channel signing key if it is
2974 if (state->smb2.should_sign) {
2975 signing_key = &state->session->smb2_channel.signing_key;
2979 * If it is a channel binding, we already have the main
2980 * signing key and try that one.
2982 if (signing_key && signing_key->length == 0) {
2983 signing_key = &state->session->smb2->signing_key;
2987 * If we do not have any session key yet, we skip the
2988 * signing of SMB2_OP_SESSSETUP requests.
2990 if (signing_key && signing_key->length == 0) {
2996 iov[num_iov].iov_base = state->smb2.hdr;
2997 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
3000 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
3001 iov[num_iov].iov_len = state->smb2.fixed_len;
3004 if (state->smb2.dyn != NULL) {
3005 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
3006 iov[num_iov].iov_len = state->smb2.dyn_len;
3010 reqlen = sizeof(state->smb2.hdr);
3011 reqlen += state->smb2.fixed_len;
3012 reqlen += state->smb2.dyn_len;
3014 if (i < num_reqs-1) {
3015 if ((reqlen % 8) > 0) {
3016 uint8_t pad = 8 - (reqlen % 8);
3017 iov[num_iov].iov_base = state->smb2.pad;
3018 iov[num_iov].iov_len = pad;
3022 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
3025 state->smb2.encryption_session_id = encryption_session_id;
3027 if (signing_key != NULL) {
3030 status = smb2_signing_sign_pdu(*signing_key,
3031 state->session->conn->protocol,
3032 &iov[hdr_iov], num_iov - hdr_iov);
3033 if (!NT_STATUS_IS_OK(status)) {
3040 ret = smbXcli_req_set_pending(reqs[i]);
3042 return NT_STATUS_NO_MEMORY;
3046 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
3047 _smb_setlen_tcp(state->length_hdr, nbt_len);
3048 iov[0].iov_base = state->length_hdr;
3049 iov[0].iov_len = sizeof(state->length_hdr);
3051 if (encryption_key != NULL) {
3053 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
3057 buf = talloc_array(iov, uint8_t, buflen);
3059 return NT_STATUS_NO_MEMORY;
3063 * We copy the buffers before encrypting them,
3064 * this is at least currently needed for the
3065 * to keep state->smb2.hdr.
3067 * Also the callers may expect there buffers
3070 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3071 struct iovec *v = &iov[vi];
3072 const uint8_t *o = (const uint8_t *)v->iov_base;
3074 memcpy(buf, o, v->iov_len);
3075 v->iov_base = (void *)buf;
3079 status = smb2_signing_encrypt_pdu(*encryption_key,
3080 state->conn->smb2.server.cipher,
3081 &iov[tf_iov], num_iov - tf_iov);
3082 if (!NT_STATUS_IS_OK(status)) {
3087 if (state->conn->dispatch_incoming == NULL) {
3088 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3091 subreq = writev_send(state, state->ev, state->conn->outgoing,
3092 state->conn->write_fd, false, iov, num_iov);
3093 if (subreq == NULL) {
3094 return NT_STATUS_NO_MEMORY;
3096 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3097 return NT_STATUS_OK;
3100 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3102 struct smbXcli_req_state *state =
3103 tevent_req_data(req,
3104 struct smbXcli_req_state);
3106 state->smb2.credit_charge = charge;
3109 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3110 struct tevent_context *ev,
3111 struct smbXcli_conn *conn,
3113 uint32_t additional_flags,
3114 uint32_t clear_flags,
3115 uint32_t timeout_msec,
3116 struct smbXcli_tcon *tcon,
3117 struct smbXcli_session *session,
3118 const uint8_t *fixed,
3122 uint32_t max_dyn_len)
3124 struct tevent_req *req;
3127 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3128 additional_flags, clear_flags,
3137 if (!tevent_req_is_in_progress(req)) {
3138 return tevent_req_post(req, ev);
3140 status = smb2cli_req_compound_submit(&req, 1);
3141 if (tevent_req_nterror(req, status)) {
3142 return tevent_req_post(req, ev);
3147 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3149 struct tevent_req *req =
3150 tevent_req_callback_data(subreq,
3152 struct smbXcli_req_state *state =
3153 tevent_req_data(req,
3154 struct smbXcli_req_state);
3158 nwritten = writev_recv(subreq, &err);
3159 TALLOC_FREE(subreq);
3160 if (nwritten == -1) {
3161 /* here, we need to notify all pending requests */
3162 NTSTATUS status = map_nt_error_from_unix_common(err);
3163 smbXcli_conn_disconnect(state->conn, status);
3168 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3171 TALLOC_CTX *mem_ctx,
3172 struct iovec **piov, int *pnum_iov)
3177 uint8_t *first_hdr = buf;
3178 size_t verified_buflen = 0;
3182 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3184 return NT_STATUS_NO_MEMORY;
3187 while (taken < buflen) {
3188 size_t len = buflen - taken;
3189 uint8_t *hdr = first_hdr + taken;
3192 size_t next_command_ofs;
3194 struct iovec *iov_tmp;
3196 if (verified_buflen > taken) {
3197 len = verified_buflen - taken;
3204 DEBUG(10, ("%d bytes left, expected at least %d\n",
3208 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3209 struct smbXcli_session *s;
3211 struct iovec tf_iov[2];
3215 if (len < SMB2_TF_HDR_SIZE) {
3216 DEBUG(10, ("%d bytes left, expected at least %d\n",
3217 (int)len, SMB2_TF_HDR_SIZE));
3221 tf_len = SMB2_TF_HDR_SIZE;
3224 hdr = first_hdr + taken;
3225 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3226 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3228 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3229 DEBUG(10, ("%d bytes left, expected at least %d\n",
3231 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3236 for (; s; s = s->next) {
3237 if (s->smb2->session_id != uid) {
3244 DEBUG(10, ("unknown session_id %llu\n",
3245 (unsigned long long)uid));
3249 tf_iov[0].iov_base = (void *)tf;
3250 tf_iov[0].iov_len = tf_len;
3251 tf_iov[1].iov_base = (void *)hdr;
3252 tf_iov[1].iov_len = enc_len;
3254 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3255 conn->smb2.server.cipher,
3257 if (!NT_STATUS_IS_OK(status)) {
3262 verified_buflen = taken + enc_len;
3267 * We need the header plus the body length field
3270 if (len < SMB2_HDR_BODY + 2) {
3271 DEBUG(10, ("%d bytes left, expected at least %d\n",
3272 (int)len, SMB2_HDR_BODY));
3275 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3276 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3280 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3281 DEBUG(10, ("Got HDR len %d, expected %d\n",
3282 SVAL(hdr, 4), SMB2_HDR_BODY));
3287 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3288 body_size = SVAL(hdr, SMB2_HDR_BODY);
3290 if (next_command_ofs != 0) {
3291 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3294 if (next_command_ofs > full_size) {
3297 full_size = next_command_ofs;
3299 if (body_size < 2) {
3302 body_size &= 0xfffe;
3304 if (body_size > (full_size - SMB2_HDR_BODY)) {
3308 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3310 if (iov_tmp == NULL) {
3312 return NT_STATUS_NO_MEMORY;
3315 cur = &iov[num_iov];
3318 cur[0].iov_base = tf;
3319 cur[0].iov_len = tf_len;
3320 cur[1].iov_base = hdr;
3321 cur[1].iov_len = SMB2_HDR_BODY;
3322 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3323 cur[2].iov_len = body_size;
3324 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3325 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3331 *pnum_iov = num_iov;
3332 return NT_STATUS_OK;
3336 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3339 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3342 size_t num_pending = talloc_array_length(conn->pending);
3345 for (i=0; i<num_pending; i++) {
3346 struct tevent_req *req = conn->pending[i];
3347 struct smbXcli_req_state *state =
3348 tevent_req_data(req,
3349 struct smbXcli_req_state);
3351 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3358 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3359 TALLOC_CTX *tmp_mem,
3362 struct tevent_req *req;
3363 struct smbXcli_req_state *state = NULL;
3368 struct smbXcli_session *last_session = NULL;
3369 size_t inbuf_len = smb_len_tcp(inbuf);
3371 status = smb2cli_inbuf_parse_compound(conn,
3372 inbuf + NBT_HDR_SIZE,
3376 if (!NT_STATUS_IS_OK(status)) {
3380 for (i=0; i<num_iov; i+=4) {
3381 uint8_t *inbuf_ref = NULL;
3382 struct iovec *cur = &iov[i];
3383 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3384 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3385 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3386 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3387 uint16_t req_opcode;
3389 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3390 uint32_t new_credits;
3391 struct smbXcli_session *session = NULL;
3392 const DATA_BLOB *signing_key = NULL;
3393 bool was_encrypted = false;
3395 new_credits = conn->smb2.cur_credits;
3396 new_credits += credits;
3397 if (new_credits > UINT16_MAX) {
3398 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3400 conn->smb2.cur_credits += credits;
3402 req = smb2cli_conn_find_pending(conn, mid);
3404 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3406 state = tevent_req_data(req, struct smbXcli_req_state);
3408 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3409 if (opcode != req_opcode) {
3410 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3412 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3414 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3415 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3418 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3419 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3420 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3421 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3423 if (state->smb2.got_async) {
3424 /* We only expect one STATUS_PENDING response */
3425 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3427 state->smb2.got_async = true;
3430 * async interim responses are not signed,
3431 * even if the SMB2_HDR_FLAG_SIGNED flag
3434 req_flags |= SMB2_HDR_FLAG_ASYNC;
3435 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3436 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3438 if (state->smb2.notify_async) {
3439 tevent_req_defer_callback(req, state->ev);
3440 tevent_req_notify_callback(req);
3445 session = state->session;
3446 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3447 session = last_session;
3449 last_session = session;
3451 if (state->smb2.should_sign) {
3452 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3453 return NT_STATUS_ACCESS_DENIED;
3457 if (flags & SMB2_HDR_FLAG_SIGNED) {
3458 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3460 if (session == NULL) {
3461 struct smbXcli_session *s;
3463 s = state->conn->sessions;
3464 for (; s; s = s->next) {
3465 if (s->smb2->session_id != uid) {
3474 if (session == NULL) {
3475 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3478 last_session = session;
3479 signing_key = &session->smb2_channel.signing_key;
3482 if (opcode == SMB2_OP_SESSSETUP) {
3484 * We prefer the channel signing key, if it is
3487 * If we do not have a channel signing key yet,
3488 * we try the main signing key, if it is not
3489 * the final response.
3491 if (signing_key && signing_key->length == 0 &&
3492 !NT_STATUS_IS_OK(status)) {
3493 signing_key = &session->smb2->signing_key;
3496 if (signing_key && signing_key->length == 0) {
3498 * If we do not have a session key to
3499 * verify the signature, we defer the
3500 * signing check to the caller.
3502 * The caller gets NT_STATUS_OK, it
3504 * smb2cli_session_set_session_key()
3506 * smb2cli_session_set_channel_key()
3507 * which will check the signature
3508 * with the channel signing key.
3514 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3515 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3516 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3519 * If the response was encrypted in a SMB2_TRANSFORM
3520 * pdu, which belongs to the correct session,
3521 * we do not need to do signing checks
3523 * It could be the session the response belongs to
3524 * or the session that was used to encrypt the
3525 * SMB2_TRANSFORM request.
3527 if ((session && session->smb2->session_id == uid) ||
3528 (state->smb2.encryption_session_id == uid)) {
3530 was_encrypted = true;
3534 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3536 * if the server returns NT_STATUS_USER_SESSION_DELETED
3537 * the response is not signed and we should
3538 * propagate the NT_STATUS_USER_SESSION_DELETED
3539 * status to the caller.
3541 state->smb2.signing_skipped = true;
3545 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3547 * if the server returns
3548 * NT_STATUS_INVALID_PARAMETER
3549 * the response might not be encrypted.
3551 if (state->smb2.should_encrypt && !was_encrypted) {
3552 state->smb2.signing_skipped = true;
3557 if (state->smb2.should_encrypt && !was_encrypted) {
3558 if (!state->smb2.signing_skipped) {
3559 return NT_STATUS_ACCESS_DENIED;
3563 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3564 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3565 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3567 * if the server returns
3568 * NT_STATUS_NETWORK_NAME_DELETED
3569 * NT_STATUS_FILE_CLOSED
3570 * NT_STATUS_INVALID_PARAMETER
3571 * the response might not be signed
3572 * as this happens before the signing checks.
3574 * If server echos the signature (or all zeros)
3575 * we should report the status from the server
3581 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3582 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3585 state->smb2.signing_skipped = true;
3591 static const uint8_t zeros[16];
3593 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3597 state->smb2.signing_skipped = true;
3604 status = smb2_signing_check_pdu(*signing_key,
3605 state->conn->protocol,
3607 if (!NT_STATUS_IS_OK(status)) {
3609 * If the signing check fails, we disconnect
3616 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3617 (session != NULL) && session->disconnect_expired)
3620 * this should be a short term hack
3621 * until the upper layers have implemented
3622 * re-authentication.
3627 smbXcli_req_unset_pending(req);
3630 * There might be more than one response
3631 * we need to defer the notifications
3633 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3638 tevent_req_defer_callback(req, state->ev);
3642 * Note: here we use talloc_reference() in a way
3643 * that does not expose it to the caller.
3645 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3646 if (tevent_req_nomem(inbuf_ref, req)) {
3650 /* copy the related buffers */
3651 state->smb2.recv_iov[0] = cur[1];
3652 state->smb2.recv_iov[1] = cur[2];
3653 state->smb2.recv_iov[2] = cur[3];
3655 tevent_req_done(req);
3659 return NT_STATUS_RETRY;
3662 return NT_STATUS_OK;
3665 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3666 struct iovec **piov,
3667 const struct smb2cli_req_expected_response *expected,
3668 size_t num_expected)
3670 struct smbXcli_req_state *state =
3671 tevent_req_data(req,
3672 struct smbXcli_req_state);
3675 bool found_status = false;
3676 bool found_size = false;
3683 if (tevent_req_is_in_progress(req) && state->smb2.got_async) {
3684 return STATUS_PENDING;
3687 if (tevent_req_is_nterror(req, &status)) {
3688 for (i=0; i < num_expected; i++) {
3689 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3690 found_status = true;
3696 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3702 if (num_expected == 0) {
3703 found_status = true;
3707 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3708 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3710 for (i=0; i < num_expected; i++) {
3711 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3715 found_status = true;
3716 if (expected[i].body_size == 0) {
3721 if (expected[i].body_size == body_size) {
3727 if (!found_status) {
3731 if (state->smb2.signing_skipped) {
3732 if (num_expected > 0) {
3733 return NT_STATUS_ACCESS_DENIED;
3735 if (!NT_STATUS_IS_ERR(status)) {
3736 return NT_STATUS_ACCESS_DENIED;
3741 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3745 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3751 NTSTATUS smb2cli_req_get_sent_iov(struct tevent_req *req,
3752 struct iovec *sent_iov)
3754 struct smbXcli_req_state *state =
3755 tevent_req_data(req,
3756 struct smbXcli_req_state);
3758 if (tevent_req_is_in_progress(req)) {
3759 return STATUS_PENDING;
3762 sent_iov[0].iov_base = state->smb2.hdr;
3763 sent_iov[0].iov_len = sizeof(state->smb2.hdr);
3765 sent_iov[1].iov_base = discard_const(state->smb2.fixed);
3766 sent_iov[1].iov_len = state->smb2.fixed_len;
3768 if (state->smb2.dyn != NULL) {
3769 sent_iov[2].iov_base = discard_const(state->smb2.dyn);
3770 sent_iov[2].iov_len = state->smb2.dyn_len;
3772 sent_iov[2].iov_base = NULL;
3773 sent_iov[2].iov_len = 0;
3776 return NT_STATUS_OK;
3779 static const struct {
3780 enum protocol_types proto;
3781 const char *smb1_name;
3782 } smb1cli_prots[] = {
3783 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3784 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3785 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3786 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3787 {PROTOCOL_LANMAN2, "LM1.2X002"},
3788 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3789 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3790 {PROTOCOL_LANMAN2, "Samba"},
3791 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3792 {PROTOCOL_NT1, "NT LM 0.12"},
3793 {PROTOCOL_SMB2_02, "SMB 2.002"},
3794 {PROTOCOL_SMB2_10, "SMB 2.???"},
3797 static const struct {
3798 enum protocol_types proto;
3799 uint16_t smb2_dialect;
3800 } smb2cli_prots[] = {
3801 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3802 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3803 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3804 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3805 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3806 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
3807 {PROTOCOL_SMB3_10, SMB3_DIALECT_REVISION_310},
3810 struct smbXcli_negprot_state {
3811 struct smbXcli_conn *conn;
3812 struct tevent_context *ev;
3813 uint32_t timeout_msec;
3820 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3821 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3822 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3823 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3824 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3825 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3829 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3830 struct tevent_context *ev,
3831 struct smbXcli_conn *conn,
3832 uint32_t timeout_msec,
3833 enum protocol_types min_protocol,
3834 enum protocol_types max_protocol)
3836 struct tevent_req *req, *subreq;
3837 struct smbXcli_negprot_state *state;
3839 req = tevent_req_create(mem_ctx, &state,
3840 struct smbXcli_negprot_state);
3846 state->timeout_msec = timeout_msec;
3848 if (min_protocol == PROTOCOL_NONE) {
3849 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3850 return tevent_req_post(req, ev);
3853 if (max_protocol == PROTOCOL_NONE) {
3854 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3855 return tevent_req_post(req, ev);
3858 if (min_protocol > max_protocol) {
3859 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3860 return tevent_req_post(req, ev);
3863 conn->min_protocol = min_protocol;
3864 conn->max_protocol = max_protocol;
3865 conn->protocol = PROTOCOL_NONE;
3867 if ((min_protocol < PROTOCOL_SMB2_02) &&
3868 (max_protocol < PROTOCOL_SMB2_02)) {
3872 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3874 subreq = smbXcli_negprot_smb1_subreq(state);
3875 if (tevent_req_nomem(subreq, req)) {
3876 return tevent_req_post(req, ev);
3878 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3882 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3883 (max_protocol >= PROTOCOL_SMB2_02)) {
3887 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3890 * As we're starting with an SMB2 negprot, emulate Windows
3891 * and ask for 31 credits in the initial SMB2 negprot.
3892 * If we don't and leave requested credits at
3893 * zero, MacOSX servers return zero credits on
3894 * the negprot reply and we fail to connect.
3896 smb2cli_conn_set_max_credits(conn,
3897 WINDOWS_CLIENT_PURE_SMB2_NEGPROT_INITIAL_CREDIT_ASK);
3899 subreq = smbXcli_negprot_smb2_subreq(state);
3900 if (tevent_req_nomem(subreq, req)) {
3901 return tevent_req_post(req, ev);
3903 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3908 * We send an SMB1 negprot with the SMB2 dialects
3909 * and expect a SMB1 or a SMB2 response.
3911 * smbXcli_negprot_dispatch_incoming() will fix the
3912 * callback to match protocol of the response.
3914 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3916 subreq = smbXcli_negprot_smb1_subreq(state);
3917 if (tevent_req_nomem(subreq, req)) {
3918 return tevent_req_post(req, ev);
3920 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3924 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3926 struct tevent_req *req =
3927 tevent_req_callback_data(subreq,
3932 * we just want the low level error
3934 status = tevent_req_simple_recv_ntstatus(subreq);
3935 TALLOC_FREE(subreq);
3936 if (tevent_req_nterror(req, status)) {
3940 /* this should never happen */
3941 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3944 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3947 DATA_BLOB bytes = data_blob_null;
3951 /* setup the protocol strings */
3952 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3956 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
3960 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
3964 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3970 * We now it is already ascii and
3971 * we want NULL termination.
3973 ok = data_blob_append(state, &bytes,
3974 smb1cli_prots[i].smb1_name,
3975 strlen(smb1cli_prots[i].smb1_name)+1);
3981 smb1cli_req_flags(state->conn->max_protocol,
3982 state->conn->smb1.client.capabilities,
3987 return smb1cli_req_send(state, state->ev, state->conn,
3991 state->timeout_msec,
3992 0xFFFE, 0, NULL, /* pid, tid, session */
3993 0, NULL, /* wct, vwv */
3994 bytes.length, bytes.data);
3997 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3999 struct tevent_req *req =
4000 tevent_req_callback_data(subreq,
4002 struct smbXcli_negprot_state *state =
4003 tevent_req_data(req,
4004 struct smbXcli_negprot_state);
4005 struct smbXcli_conn *conn = state->conn;
4006 struct iovec *recv_iov = NULL;
4015 size_t num_prots = 0;
4017 uint32_t client_capabilities = conn->smb1.client.capabilities;
4018 uint32_t both_capabilities;
4019 uint32_t server_capabilities = 0;
4020 uint32_t capabilities;
4021 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
4022 uint32_t server_max_xmit = 0;
4024 uint32_t server_max_mux = 0;
4025 uint16_t server_security_mode = 0;
4026 uint32_t server_session_key = 0;
4027 bool server_readbraw = false;
4028 bool server_writebraw = false;
4029 bool server_lockread = false;
4030 bool server_writeunlock = false;
4031 struct GUID server_guid = GUID_zero();
4032 DATA_BLOB server_gss_blob = data_blob_null;
4033 uint8_t server_challenge[8];
4034 char *server_workgroup = NULL;
4035 char *server_name = NULL;
4036 int server_time_zone = 0;
4037 NTTIME server_system_time = 0;
4038 static const struct smb1cli_req_expected_response expected[] = {
4040 .status = NT_STATUS_OK,
4041 .wct = 0x11, /* NT1 */
4044 .status = NT_STATUS_OK,
4045 .wct = 0x0D, /* LM */
4048 .status = NT_STATUS_OK,
4049 .wct = 0x01, /* CORE */
4053 ZERO_STRUCT(server_challenge);
4055 status = smb1cli_req_recv(subreq, state,
4060 NULL, /* pvwv_offset */
4063 NULL, /* pbytes_offset */
4065 expected, ARRAY_SIZE(expected));
4066 TALLOC_FREE(subreq);
4067 if (tevent_req_nterror(req, status)) {
4071 flags = CVAL(inhdr, HDR_FLG);
4073 protnum = SVAL(vwv, 0);
4075 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4076 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4080 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4084 if (protnum != num_prots) {
4089 conn->protocol = smb1cli_prots[i].proto;
4093 if (conn->protocol == PROTOCOL_NONE) {
4094 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4098 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
4099 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4100 "and the selected protocol level doesn't support it.\n"));
4101 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4105 if (flags & FLAG_SUPPORT_LOCKREAD) {
4106 server_lockread = true;
4107 server_writeunlock = true;
4110 if (conn->protocol >= PROTOCOL_NT1) {
4111 const char *client_signing = NULL;
4112 bool server_mandatory = false;
4113 bool server_allowed = false;
4114 const char *server_signing = NULL;
4119 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4124 server_security_mode = CVAL(vwv + 1, 0);
4125 server_max_mux = SVAL(vwv + 1, 1);
4126 server_max_xmit = IVAL(vwv + 3, 1);
4127 server_session_key = IVAL(vwv + 7, 1);
4128 server_time_zone = SVALS(vwv + 15, 1);
4129 server_time_zone *= 60;
4130 /* this time arrives in real GMT */
4131 server_system_time = BVAL(vwv + 11, 1);
4132 server_capabilities = IVAL(vwv + 9, 1);
4134 key_len = CVAL(vwv + 16, 1);
4136 if (server_capabilities & CAP_RAW_MODE) {
4137 server_readbraw = true;
4138 server_writebraw = true;
4140 if (server_capabilities & CAP_LOCK_AND_READ) {
4141 server_lockread = true;
4144 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4145 DATA_BLOB blob1, blob2;
4147 if (num_bytes < 16) {
4148 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4152 blob1 = data_blob_const(bytes, 16);
4153 status = GUID_from_data_blob(&blob1, &server_guid);
4154 if (tevent_req_nterror(req, status)) {
4158 blob1 = data_blob_const(bytes+16, num_bytes-16);
4159 blob2 = data_blob_dup_talloc(state, blob1);
4160 if (blob1.length > 0 &&
4161 tevent_req_nomem(blob2.data, req)) {
4164 server_gss_blob = blob2;
4166 DATA_BLOB blob1, blob2;
4168 if (num_bytes < key_len) {
4169 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4173 if (key_len != 0 && key_len != 8) {
4174 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4179 memcpy(server_challenge, bytes, 8);
4182 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4183 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4184 if (blob1.length > 0) {
4187 len = utf16_len_n(blob1.data,
4191 ok = convert_string_talloc(state,
4199 status = map_nt_error_from_unix_common(errno);
4200 tevent_req_nterror(req, status);
4205 blob2.data += blob1.length;
4206 blob2.length -= blob1.length;
4207 if (blob2.length > 0) {
4210 len = utf16_len_n(blob1.data,
4214 ok = convert_string_talloc(state,
4222 status = map_nt_error_from_unix_common(errno);
4223 tevent_req_nterror(req, status);
4229 client_signing = "disabled";
4230 if (conn->allow_signing) {
4231 client_signing = "allowed";
4233 if (conn->mandatory_signing) {
4234 client_signing = "required";
4237 server_signing = "not supported";
4238 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4239 server_signing = "supported";
4240 server_allowed = true;
4241 } else if (conn->mandatory_signing) {
4243 * We have mandatory signing as client
4244 * lets assume the server will look at our
4245 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4246 * flag in the session setup
4248 server_signing = "not announced";
4249 server_allowed = true;
4251 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4252 server_signing = "required";
4253 server_mandatory = true;
4256 ok = smb_signing_set_negotiated(conn->smb1.signing,
4260 DEBUG(1,("cli_negprot: SMB signing is required, "
4261 "but client[%s] and server[%s] mismatch\n",
4262 client_signing, server_signing));
4263 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4267 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4273 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4277 server_security_mode = SVAL(vwv + 1, 0);
4278 server_max_xmit = SVAL(vwv + 2, 0);
4279 server_max_mux = SVAL(vwv + 3, 0);
4280 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4281 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4282 server_session_key = IVAL(vwv + 6, 0);
4283 server_time_zone = SVALS(vwv + 10, 0);
4284 server_time_zone *= 60;
4285 /* this time is converted to GMT by make_unix_date */
4286 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4287 unix_to_nt_time(&server_system_time, t);
4288 key_len = SVAL(vwv + 11, 0);
4290 if (num_bytes < key_len) {
4291 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4295 if (key_len != 0 && key_len != 8) {
4296 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4301 memcpy(server_challenge, bytes, 8);
4304 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4305 if (blob1.length > 0) {
4309 len = utf16_len_n(blob1.data,
4313 ok = convert_string_talloc(state,
4321 status = map_nt_error_from_unix_common(errno);
4322 tevent_req_nterror(req, status);
4328 /* the old core protocol */
4329 server_time_zone = get_time_zone(time(NULL));
4330 server_max_xmit = 1024;
4334 if (server_max_xmit < 1024) {
4335 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4339 if (server_max_mux < 1) {
4340 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4345 * Now calculate the negotiated capabilities
4346 * based on the mask for:
4347 * - client only flags
4348 * - flags used in both directions
4349 * - server only flags
4351 both_capabilities = client_capabilities & server_capabilities;
4352 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4353 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4354 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4356 max_xmit = MIN(client_max_xmit, server_max_xmit);
4358 conn->smb1.server.capabilities = server_capabilities;
4359 conn->smb1.capabilities = capabilities;
4361 conn->smb1.server.max_xmit = server_max_xmit;
4362 conn->smb1.max_xmit = max_xmit;
4364 conn->smb1.server.max_mux = server_max_mux;
4366 conn->smb1.server.security_mode = server_security_mode;
4368 conn->smb1.server.readbraw = server_readbraw;
4369 conn->smb1.server.writebraw = server_writebraw;
4370 conn->smb1.server.lockread = server_lockread;
4371 conn->smb1.server.writeunlock = server_writeunlock;
4373 conn->smb1.server.session_key = server_session_key;
4375 talloc_steal(conn, server_gss_blob.data);
4376 conn->smb1.server.gss_blob = server_gss_blob;
4377 conn->smb1.server.guid = server_guid;
4378 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4379 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4380 conn->smb1.server.name = talloc_move(conn, &server_name);
4382 conn->smb1.server.time_zone = server_time_zone;
4383 conn->smb1.server.system_time = server_system_time;
4385 tevent_req_done(req);
4388 static size_t smbXcli_padding_helper(uint32_t offset, size_t n)
4390 if ((offset & (n-1)) == 0) return 0;
4391 return n - (offset & (n-1));
4394 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4398 uint16_t dialect_count = 0;
4399 DATA_BLOB dyn = data_blob_null;
4401 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4405 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4409 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4413 SSVAL(val, 0, smb2cli_prots[i].smb2_dialect);
4415 ok = data_blob_append(state, &dyn, val, sizeof(val));
4423 buf = state->smb2.fixed;
4425 SSVAL(buf, 2, dialect_count);
4426 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4427 SSVAL(buf, 6, 0); /* Reserved */
4428 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
4429 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4431 SIVAL(buf, 8, 0); /* Capabilities */
4433 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4437 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4439 if (!NT_STATUS_IS_OK(status)) {
4442 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4444 memset(buf+12, 0, 16); /* ClientGuid */
4447 if (state->conn->max_protocol >= PROTOCOL_SMB3_10) {
4449 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4453 const uint8_t zeros[8] = {0, };
4457 SSVAL(p, 0, 1); /* HashAlgorithmCount */
4458 SSVAL(p, 2, 32); /* SaltLength */
4459 SSVAL(p, 4, SMB2_PREAUTH_INTEGRITY_SHA512);
4460 generate_random_buffer(p + 6, 32);
4462 b = data_blob_const(p, 38);
4463 status = smb2_negotiate_context_add(state, &c,
4464 SMB2_PREAUTH_INTEGRITY_CAPABILITIES, b);
4465 if (!NT_STATUS_IS_OK(status)) {
4469 SSVAL(p, 0, 2); /* ChiperCount */
4470 SSVAL(p, 2, SMB2_ENCRYPTION_AES128_GCM);
4471 SSVAL(p, 4, SMB2_ENCRYPTION_AES128_CCM);
4473 b = data_blob_const(p, 6);
4474 status = smb2_negotiate_context_add(state, &c,
4475 SMB2_ENCRYPTION_CAPABILITIES, b);
4476 if (!NT_STATUS_IS_OK(status)) {
4480 status = smb2_negotiate_context_push(state, &b, c);
4481 if (!NT_STATUS_IS_OK(status)) {
4485 offset = SMB2_HDR_BODY + sizeof(state->smb2.fixed) + dyn.length;
4486 pad = smbXcli_padding_helper(offset, 8);
4488 ok = data_blob_append(state, &dyn, zeros, pad);
4494 ok = data_blob_append(state, &dyn, b.data, b.length);
4499 SIVAL(buf, 28, offset); /* NegotiateContextOffset */
4500 SSVAL(buf, 32, c.num_contexts); /* NegotiateContextCount */
4501 SSVAL(buf, 34, 0); /* Reserved */
4503 SBVAL(buf, 28, 0); /* Reserved/ClientStartTime */
4506 return smb2cli_req_send(state, state->ev,
4507 state->conn, SMB2_OP_NEGPROT,
4509 state->timeout_msec,
4510 NULL, NULL, /* tcon, session */
4511 state->smb2.fixed, sizeof(state->smb2.fixed),
4512 dyn.data, dyn.length,
4513 UINT16_MAX); /* max_dyn_len */
4516 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4518 struct tevent_req *req =
4519 tevent_req_callback_data(subreq,
4521 struct smbXcli_negprot_state *state =
4522 tevent_req_data(req,
4523 struct smbXcli_negprot_state);
4524 struct smbXcli_conn *conn = state->conn;
4525 size_t security_offset, security_length;
4531 uint16_t dialect_revision;
4532 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4533 uint32_t negotiate_context_offset = 0;
4534 uint16_t negotiate_context_count = 0;
4535 DATA_BLOB negotiate_context_blob = data_blob_null;
4539 struct smb2_negotiate_context *preauth = NULL;
4540 uint16_t hash_count;
4541 uint16_t salt_length;
4542 uint16_t hash_selected;
4543 struct hc_sha512state sctx;
4544 struct smb2_negotiate_context *cipher = NULL;
4545 struct iovec sent_iov[3];
4546 static const struct smb2cli_req_expected_response expected[] = {
4548 .status = NT_STATUS_OK,
4553 status = smb2cli_req_recv(subreq, state, &iov,
4554 expected, ARRAY_SIZE(expected));
4555 if (tevent_req_nterror(req, status)) {
4559 body = (uint8_t *)iov[1].iov_base;
4561 dialect_revision = SVAL(body, 4);
4563 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4564 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4568 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4572 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4576 conn->protocol = smb2cli_prots[i].proto;
4580 if (conn->protocol == PROTOCOL_NONE) {
4581 TALLOC_FREE(subreq);
4583 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
4584 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4588 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4589 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4593 /* make sure we do not loop forever */
4594 state->conn->min_protocol = PROTOCOL_SMB2_02;
4597 * send a SMB2 negprot, in order to negotiate
4600 subreq = smbXcli_negprot_smb2_subreq(state);
4601 if (tevent_req_nomem(subreq, req)) {
4604 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4608 conn->smb2.server.security_mode = SVAL(body, 2);
4609 if (conn->protocol >= PROTOCOL_SMB3_10) {
4610 negotiate_context_count = SVAL(body, 6);
4613 blob = data_blob_const(body + 8, 16);
4614 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4615 if (tevent_req_nterror(req, status)) {
4619 conn->smb2.server.capabilities = IVAL(body, 24);
4620 conn->smb2.server.max_trans_size= IVAL(body, 28);
4621 conn->smb2.server.max_read_size = IVAL(body, 32);
4622 conn->smb2.server.max_write_size= IVAL(body, 36);
4623 conn->smb2.server.system_time = BVAL(body, 40);
4624 conn->smb2.server.start_time = BVAL(body, 48);
4626 security_offset = SVAL(body, 56);
4627 security_length = SVAL(body, 58);
4629 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4630 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4634 if (security_length > iov[2].iov_len) {
4635 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4639 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4642 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4646 if (conn->protocol < PROTOCOL_SMB3_10) {
4647 TALLOC_FREE(subreq);
4649 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4650 conn->smb2.server.cipher = SMB2_ENCRYPTION_AES128_CCM;
4652 tevent_req_done(req);
4656 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4657 tevent_req_nterror(req,
4658 NT_STATUS_INVALID_NETWORK_RESPONSE);
4662 negotiate_context_offset = IVAL(body, 60);
4663 if (negotiate_context_offset < security_offset) {
4664 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4668 ctx_ofs = negotiate_context_offset - security_offset;
4669 if (ctx_ofs > iov[2].iov_len) {
4670 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4673 avail = iov[2].iov_len - security_length;
4674 needed = iov[2].iov_len - ctx_ofs;
4675 if (needed > avail) {
4676 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4680 negotiate_context_blob.data = (uint8_t *)iov[2].iov_base;
4681 negotiate_context_blob.length = iov[2].iov_len;
4683 negotiate_context_blob.data += ctx_ofs;
4684 negotiate_context_blob.length -= ctx_ofs;
4686 status = smb2_negotiate_context_parse(state, negotiate_context_blob, &c);
4687 if (tevent_req_nterror(req, status)) {
4691 if (negotiate_context_count != c.num_contexts) {
4692 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4696 preauth = smb2_negotiate_context_find(&c,
4697 SMB2_PREAUTH_INTEGRITY_CAPABILITIES);
4698 if (preauth == NULL) {
4699 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4703 if (preauth->data.length < 6) {
4704 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4708 hash_count = SVAL(preauth->data.data, 0);
4709 salt_length = SVAL(preauth->data.data, 2);
4710 hash_selected = SVAL(preauth->data.data, 4);
4712 if (hash_count != 1) {
4713 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4717 if (preauth->data.length != (6 + salt_length)) {
4718 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4722 if (hash_selected != SMB2_PREAUTH_INTEGRITY_SHA512) {
4723 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4727 cipher = smb2_negotiate_context_find(&c, SMB2_ENCRYPTION_CAPABILITIES);
4728 if (cipher != NULL) {
4729 uint16_t cipher_count;
4731 if (cipher->data.length < 2) {
4732 tevent_req_nterror(req,
4733 NT_STATUS_INVALID_NETWORK_RESPONSE);
4737 cipher_count = SVAL(cipher->data.data, 0);
4739 if (cipher_count > 1) {
4740 tevent_req_nterror(req,
4741 NT_STATUS_INVALID_NETWORK_RESPONSE);
4745 if (cipher->data.length != (2 + 2 * cipher_count)) {
4746 tevent_req_nterror(req,
4747 NT_STATUS_INVALID_NETWORK_RESPONSE);
4751 if (cipher_count == 1) {
4752 uint16_t cipher_selected;
4754 cipher_selected = SVAL(cipher->data.data, 2);
4756 switch (cipher_selected) {
4757 case SMB2_ENCRYPTION_AES128_GCM:
4758 case SMB2_ENCRYPTION_AES128_CCM:
4759 conn->smb2.server.cipher = cipher_selected;
4765 /* First we hash the request */
4766 smb2cli_req_get_sent_iov(subreq, sent_iov);
4767 samba_SHA512_Init(&sctx);
4768 samba_SHA512_Update(&sctx, conn->smb2.preauth_sha512,
4769 sizeof(conn->smb2.preauth_sha512));
4770 for (i = 0; i < 3; i++) {
4771 samba_SHA512_Update(&sctx, sent_iov[i].iov_base, sent_iov[i].iov_len);
4773 samba_SHA512_Final(conn->smb2.preauth_sha512, &sctx);
4774 TALLOC_FREE(subreq);
4776 /* And now we hash the response */
4777 samba_SHA512_Init(&sctx);
4778 samba_SHA512_Update(&sctx, conn->smb2.preauth_sha512,
4779 sizeof(conn->smb2.preauth_sha512));
4780 for (i = 0; i < 3; i++) {
4781 samba_SHA512_Update(&sctx, iov[i].iov_base, iov[i].iov_len);
4783 samba_SHA512_Final(conn->smb2.preauth_sha512, &sctx);
4785 tevent_req_done(req);
4788 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4789 TALLOC_CTX *tmp_mem,
4792 size_t num_pending = talloc_array_length(conn->pending);
4793 struct tevent_req *subreq;
4794 struct smbXcli_req_state *substate;
4795 struct tevent_req *req;
4796 uint32_t protocol_magic;
4797 size_t inbuf_len = smb_len_nbt(inbuf);
4799 if (num_pending != 1) {
4800 return NT_STATUS_INTERNAL_ERROR;
4803 if (inbuf_len < 4) {
4804 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4807 subreq = conn->pending[0];
4808 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4809 req = tevent_req_callback_data(subreq, struct tevent_req);
4811 protocol_magic = IVAL(inbuf, 4);
4813 switch (protocol_magic) {
4815 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4816 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4817 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4820 if (substate->smb2.recv_iov == NULL) {
4822 * For the SMB1 negprot we have move it.
4824 substate->smb2.recv_iov = substate->smb1.recv_iov;
4825 substate->smb1.recv_iov = NULL;
4829 * we got an SMB2 answer, which consumed sequence number 0
4830 * so we need to use 1 as the next one.
4832 * we also need to set the current credits to 0
4833 * as we consumed the initial one. The SMB2 answer
4834 * hopefully grant us a new credit.
4837 conn->smb2.cur_credits = 0;
4838 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4839 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4840 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4843 DEBUG(10, ("Got non-SMB PDU\n"));
4844 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4847 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4849 return tevent_req_simple_recv_ntstatus(req);
4852 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4853 uint32_t timeout_msec,
4854 enum protocol_types min_protocol,
4855 enum protocol_types max_protocol)
4857 TALLOC_CTX *frame = talloc_stackframe();
4858 struct tevent_context *ev;
4859 struct tevent_req *req;
4860 NTSTATUS status = NT_STATUS_NO_MEMORY;
4863 if (smbXcli_conn_has_async_calls(conn)) {
4865 * Can't use sync call while an async call is in flight
4867 status = NT_STATUS_INVALID_PARAMETER_MIX;
4870 ev = samba_tevent_context_init(frame);
4874 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4875 min_protocol, max_protocol);
4879 ok = tevent_req_poll(req, ev);
4881 status = map_nt_error_from_unix_common(errno);
4884 status = smbXcli_negprot_recv(req);
4890 struct smb2cli_validate_negotiate_info_state {
4891 struct smbXcli_conn *conn;
4892 DATA_BLOB in_input_buffer;
4893 DATA_BLOB in_output_buffer;
4894 DATA_BLOB out_input_buffer;
4895 DATA_BLOB out_output_buffer;
4899 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq);
4901 struct tevent_req *smb2cli_validate_negotiate_info_send(TALLOC_CTX *mem_ctx,
4902 struct tevent_context *ev,
4903 struct smbXcli_conn *conn,
4904 uint32_t timeout_msec,
4905 struct smbXcli_session *session,
4906 struct smbXcli_tcon *tcon)
4908 struct tevent_req *req;
4909 struct smb2cli_validate_negotiate_info_state *state;
4911 uint16_t dialect_count = 0;
4912 struct tevent_req *subreq;
4913 bool _save_should_sign;
4916 req = tevent_req_create(mem_ctx, &state,
4917 struct smb2cli_validate_negotiate_info_state);
4923 state->in_input_buffer = data_blob_talloc_zero(state,
4924 4 + 16 + 1 + 1 + 2);
4925 if (tevent_req_nomem(state->in_input_buffer.data, req)) {
4926 return tevent_req_post(req, ev);
4928 buf = state->in_input_buffer.data;
4930 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
4931 SIVAL(buf, 0, conn->smb2.client.capabilities);
4933 SIVAL(buf, 0, 0); /* Capabilities */
4935 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4939 status = GUID_to_ndr_blob(&conn->smb2.client.guid,
4941 if (!NT_STATUS_IS_OK(status)) {
4944 memcpy(buf+4, blob.data, 16); /* ClientGuid */
4946 memset(buf+4, 0, 16); /* ClientGuid */
4948 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
4949 SCVAL(buf, 20, conn->smb2.client.security_mode);
4953 SCVAL(buf, 21, 0); /* reserved */
4955 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4959 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4963 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4967 if (smb2cli_prots[i].proto == state->conn->protocol) {
4968 state->dialect = smb2cli_prots[i].smb2_dialect;
4971 ofs = state->in_input_buffer.length;
4972 ok = data_blob_realloc(state, &state->in_input_buffer,
4975 tevent_req_oom(req);
4976 return tevent_req_post(req, ev);
4979 buf = state->in_input_buffer.data;
4980 SSVAL(buf, ofs, smb2cli_prots[i].smb2_dialect);
4984 buf = state->in_input_buffer.data;
4985 SSVAL(buf, 22, dialect_count);
4987 _save_should_sign = smb2cli_tcon_is_signing_on(tcon);
4988 smb2cli_tcon_should_sign(tcon, true);
4989 subreq = smb2cli_ioctl_send(state, ev, conn,
4990 timeout_msec, session, tcon,
4991 UINT64_MAX, /* in_fid_persistent */
4992 UINT64_MAX, /* in_fid_volatile */
4993 FSCTL_VALIDATE_NEGOTIATE_INFO,
4994 0, /* in_max_input_length */
4995 &state->in_input_buffer,
4996 24, /* in_max_output_length */
4997 &state->in_output_buffer,
4998 SMB2_IOCTL_FLAG_IS_FSCTL);
4999 smb2cli_tcon_should_sign(tcon, _save_should_sign);
5000 if (tevent_req_nomem(subreq, req)) {
5001 return tevent_req_post(req, ev);
5003 tevent_req_set_callback(subreq,
5004 smb2cli_validate_negotiate_info_done,
5010 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq)
5012 struct tevent_req *req =
5013 tevent_req_callback_data(subreq,
5015 struct smb2cli_validate_negotiate_info_state *state =
5016 tevent_req_data(req,
5017 struct smb2cli_validate_negotiate_info_state);
5020 uint32_t capabilities;
5021 DATA_BLOB guid_blob;
5022 struct GUID server_guid;
5023 uint16_t security_mode;
5026 status = smb2cli_ioctl_recv(subreq, state,
5027 &state->out_input_buffer,
5028 &state->out_output_buffer);
5029 TALLOC_FREE(subreq);
5030 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
5032 * The response was signed, but not supported
5034 * Older Windows and Samba releases return
5035 * NT_STATUS_FILE_CLOSED.
5037 tevent_req_done(req);
5040 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_DEVICE_REQUEST)) {
5042 * The response was signed, but not supported
5044 * This is returned by the NTVFS based Samba 4.x file server
5047 tevent_req_done(req);
5050 if (NT_STATUS_EQUAL(status, NT_STATUS_FS_DRIVER_REQUIRED)) {
5052 * The response was signed, but not supported
5054 * This is returned by the NTVFS based Samba 4.x file server
5057 tevent_req_done(req);
5060 if (tevent_req_nterror(req, status)) {
5064 if (state->out_output_buffer.length != 24) {
5065 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5069 buf = state->out_output_buffer.data;
5071 capabilities = IVAL(buf, 0);
5072 guid_blob = data_blob_const(buf + 4, 16);
5073 status = GUID_from_data_blob(&guid_blob, &server_guid);
5074 if (tevent_req_nterror(req, status)) {
5077 security_mode = CVAL(buf, 20);
5078 dialect = SVAL(buf, 22);
5080 if (capabilities != state->conn->smb2.server.capabilities) {
5081 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5085 if (!GUID_equal(&server_guid, &state->conn->smb2.server.guid)) {
5086 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5090 if (security_mode != state->conn->smb2.server.security_mode) {
5091 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5095 if (dialect != state->dialect) {
5096 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5100 tevent_req_done(req);
5103 NTSTATUS smb2cli_validate_negotiate_info_recv(struct tevent_req *req)
5105 return tevent_req_simple_recv_ntstatus(req);
5108 static int smbXcli_session_destructor(struct smbXcli_session *session)
5110 if (session->conn == NULL) {
5114 DLIST_REMOVE(session->conn->sessions, session);
5118 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
5119 struct smbXcli_conn *conn)
5121 struct smbXcli_session *session;
5123 session = talloc_zero(mem_ctx, struct smbXcli_session);
5124 if (session == NULL) {
5127 session->smb2 = talloc_zero(session, struct smb2cli_session);
5128 if (session->smb2 == NULL) {
5129 talloc_free(session);
5132 talloc_set_destructor(session, smbXcli_session_destructor);
5134 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
5135 session->conn = conn;
5137 memcpy(session->smb2_channel.preauth_sha512,
5138 conn->smb2.preauth_sha512,
5139 sizeof(session->smb2_channel.preauth_sha512));
5144 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
5145 struct smbXcli_session *src)
5147 struct smbXcli_session *session;
5149 session = talloc_zero(mem_ctx, struct smbXcli_session);
5150 if (session == NULL) {
5153 session->smb2 = talloc_zero(session, struct smb2cli_session);
5154 if (session->smb2 == NULL) {
5155 talloc_free(session);
5159 session->conn = src->conn;
5160 *session->smb2 = *src->smb2;
5161 session->smb2_channel = src->smb2_channel;
5162 session->disconnect_expired = src->disconnect_expired;
5164 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
5165 talloc_set_destructor(session, smbXcli_session_destructor);
5170 bool smbXcli_session_is_authenticated(struct smbXcli_session *session)
5172 const DATA_BLOB *application_key;
5174 if (session->conn == NULL) {
5179 * If we have an application key we had a session key negotiated
5182 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5183 application_key = &session->smb2->application_key;
5185 application_key = &session->smb1.application_key;
5188 if (application_key->length == 0) {
5195 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
5196 TALLOC_CTX *mem_ctx,
5199 const DATA_BLOB *application_key;
5201 *key = data_blob_null;
5203 if (session->conn == NULL) {
5204 return NT_STATUS_NO_USER_SESSION_KEY;
5207 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5208 application_key = &session->smb2->application_key;
5210 application_key = &session->smb1.application_key;
5213 if (application_key->length == 0) {
5214 return NT_STATUS_NO_USER_SESSION_KEY;
5217 *key = data_blob_dup_talloc(mem_ctx, *application_key);
5218 if (key->data == NULL) {
5219 return NT_STATUS_NO_MEMORY;
5222 return NT_STATUS_OK;
5225 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
5227 session->disconnect_expired = true;
5230 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
5232 return session->smb1.session_id;
5235 void smb1cli_session_set_id(struct smbXcli_session *session,
5236 uint16_t session_id)
5238 session->smb1.session_id = session_id;
5241 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
5242 const DATA_BLOB _session_key)
5244 struct smbXcli_conn *conn = session->conn;
5245 uint8_t session_key[16];
5248 return NT_STATUS_INVALID_PARAMETER_MIX;
5251 if (session->smb1.application_key.length != 0) {
5253 * TODO: do not allow this...
5255 * return NT_STATUS_INVALID_PARAMETER_MIX;
5257 data_blob_clear_free(&session->smb1.application_key);
5258 session->smb1.protected_key = false;
5261 if (_session_key.length == 0) {
5262 return NT_STATUS_OK;
5265 ZERO_STRUCT(session_key);
5266 memcpy(session_key, _session_key.data,
5267 MIN(_session_key.length, sizeof(session_key)));
5269 session->smb1.application_key = data_blob_talloc(session,
5271 sizeof(session_key));
5272 ZERO_STRUCT(session_key);
5273 if (session->smb1.application_key.data == NULL) {
5274 return NT_STATUS_NO_MEMORY;
5277 session->smb1.protected_key = false;
5279 return NT_STATUS_OK;
5282 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
5284 if (session->smb1.protected_key) {
5285 /* already protected */
5286 return NT_STATUS_OK;
5289 if (session->smb1.application_key.length != 16) {
5290 return NT_STATUS_INVALID_PARAMETER_MIX;
5293 smb_key_derivation(session->smb1.application_key.data,
5294 session->smb1.application_key.length,
5295 session->smb1.application_key.data);
5297 session->smb1.protected_key = true;
5299 return NT_STATUS_OK;
5302 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
5304 struct smbXcli_conn *conn = session->conn;
5305 uint8_t security_mode = 0;
5308 return security_mode;
5311 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
5312 if (conn->mandatory_signing) {
5313 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
5316 return security_mode;
5319 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
5321 return session->smb2->session_id;
5324 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
5326 return session->smb2->session_flags;
5329 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
5330 uint64_t session_id,
5331 uint16_t session_flags)
5333 session->smb2->session_id = session_id;
5334 session->smb2->session_flags = session_flags;
5337 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
5339 session->smb2->channel_sequence += 1;
5342 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session *session,
5343 uint16_t channel_sequence)
5347 prev_cs = session->smb2->channel_sequence;
5348 session->smb2->channel_sequence = channel_sequence;
5353 void smb2cli_session_start_replay(struct smbXcli_session *session)
5355 session->smb2->replay_active = true;
5358 void smb2cli_session_stop_replay(struct smbXcli_session *session)
5360 session->smb2->replay_active = false;
5363 NTSTATUS smb2cli_session_update_preauth(struct smbXcli_session *session,
5364 const struct iovec *iov)
5366 struct hc_sha512state sctx;
5369 if (session->conn == NULL) {
5370 return NT_STATUS_INTERNAL_ERROR;
5373 if (session->conn->protocol < PROTOCOL_SMB3_10) {
5374 return NT_STATUS_OK;
5377 if (session->smb2_channel.signing_key.length != 0) {
5378 return NT_STATUS_OK;
5381 samba_SHA512_Init(&sctx);
5382 samba_SHA512_Update(&sctx, session->smb2_channel.preauth_sha512,
5383 sizeof(session->smb2_channel.preauth_sha512));
5384 for (i = 0; i < 3; i++) {
5385 samba_SHA512_Update(&sctx, iov[i].iov_base, iov[i].iov_len);
5387 samba_SHA512_Final(session->smb2_channel.preauth_sha512, &sctx);
5389 return NT_STATUS_OK;
5392 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
5393 const DATA_BLOB _session_key,
5394 const struct iovec *recv_iov)
5396 struct smbXcli_conn *conn = session->conn;
5397 uint16_t no_sign_flags;
5398 uint8_t session_key[16];
5399 bool check_signature = true;
5402 struct _derivation {
5407 struct _derivation signing;
5408 struct _derivation encryption;
5409 struct _derivation decryption;
5410 struct _derivation application;
5414 return NT_STATUS_INVALID_PARAMETER_MIX;
5417 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
5418 return NT_STATUS_INVALID_PARAMETER_MIX;
5421 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
5423 if (session->smb2->session_flags & no_sign_flags) {
5424 session->smb2->should_sign = false;
5425 return NT_STATUS_OK;
5428 if (session->smb2->signing_key.length != 0) {
5429 return NT_STATUS_INVALID_PARAMETER_MIX;
5432 if (conn->protocol >= PROTOCOL_SMB3_10) {
5433 struct _derivation *d;
5436 p = data_blob_const(session->smb2_channel.preauth_sha512,
5437 sizeof(session->smb2_channel.preauth_sha512));
5439 d = &derivation.signing;
5440 d->label = data_blob_string_const_null("SMBSigningKey");
5443 d = &derivation.encryption;
5444 d->label = data_blob_string_const_null("SMBC2SCipherKey");
5447 d = &derivation.decryption;
5448 d->label = data_blob_string_const_null("SMBS2CCipherKey");
5451 d = &derivation.application;
5452 d->label = data_blob_string_const_null("SMBAppKey");
5455 } else if (conn->protocol >= PROTOCOL_SMB2_24) {
5456 struct _derivation *d;
5458 d = &derivation.signing;
5459 d->label = data_blob_string_const_null("SMB2AESCMAC");
5460 d->context = data_blob_string_const_null("SmbSign");
5462 d = &derivation.encryption;
5463 d->label = data_blob_string_const_null("SMB2AESCCM");
5464 d->context = data_blob_string_const_null("ServerIn ");
5466 d = &derivation.decryption;
5467 d->label = data_blob_string_const_null("SMB2AESCCM");
5468 d->context = data_blob_string_const_null("ServerOut");
5470 d = &derivation.application;
5471 d->label = data_blob_string_const_null("SMB2APP");
5472 d->context = data_blob_string_const_null("SmbRpc");
5475 ZERO_STRUCT(session_key);
5476 memcpy(session_key, _session_key.data,
5477 MIN(_session_key.length, sizeof(session_key)));
5479 session->smb2->signing_key = data_blob_talloc(session,
5481 sizeof(session_key));
5482 if (session->smb2->signing_key.data == NULL) {
5483 ZERO_STRUCT(session_key);
5484 return NT_STATUS_NO_MEMORY;
5487 if (conn->protocol >= PROTOCOL_SMB2_24) {
5488 struct _derivation *d = &derivation.signing;
5490 smb2_key_derivation(session_key, sizeof(session_key),
5491 d->label.data, d->label.length,
5492 d->context.data, d->context.length,
5493 session->smb2->signing_key.data);
5496 session->smb2->encryption_key = data_blob_dup_talloc(session,
5497 session->smb2->signing_key);
5498 if (session->smb2->encryption_key.data == NULL) {
5499 ZERO_STRUCT(session_key);
5500 return NT_STATUS_NO_MEMORY;
5503 if (conn->protocol >= PROTOCOL_SMB2_24) {
5504 struct _derivation *d = &derivation.encryption;
5506 smb2_key_derivation(session_key, sizeof(session_key),
5507 d->label.data, d->label.length,
5508 d->context.data, d->context.length,
5509 session->smb2->encryption_key.data);
5512 session->smb2->decryption_key = data_blob_dup_talloc(session,
5513 session->smb2->signing_key);
5514 if (session->smb2->decryption_key.data == NULL) {
5515 ZERO_STRUCT(session_key);
5516 return NT_STATUS_NO_MEMORY;
5519 if (conn->protocol >= PROTOCOL_SMB2_24) {
5520 struct _derivation *d = &derivation.decryption;
5522 smb2_key_derivation(session_key, sizeof(session_key),
5523 d->label.data, d->label.length,
5524 d->context.data, d->context.length,
5525 session->smb2->decryption_key.data);
5528 session->smb2->application_key = data_blob_dup_talloc(session,
5529 session->smb2->signing_key);
5530 if (session->smb2->application_key.data == NULL) {
5531 ZERO_STRUCT(session_key);
5532 return NT_STATUS_NO_MEMORY;
5535 if (conn->protocol >= PROTOCOL_SMB2_24) {
5536 struct _derivation *d = &derivation.application;
5538 smb2_key_derivation(session_key, sizeof(session_key),
5539 d->label.data, d->label.length,
5540 d->context.data, d->context.length,
5541 session->smb2->application_key.data);
5543 ZERO_STRUCT(session_key);
5545 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
5546 session->smb2->signing_key);
5547 if (session->smb2_channel.signing_key.data == NULL) {
5548 return NT_STATUS_NO_MEMORY;
5551 check_signature = conn->mandatory_signing;
5553 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
5554 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
5556 * Sadly some vendors don't sign the
5557 * final SMB2 session setup response
5559 * At least Windows and Samba are always doing this
5560 * if there's a session key available.
5562 * We only check the signature if it's mandatory
5563 * or SMB2_HDR_FLAG_SIGNED is provided.
5565 check_signature = true;
5568 if (conn->protocol >= PROTOCOL_SMB3_10) {
5569 check_signature = true;
5572 if (check_signature) {
5573 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5574 session->conn->protocol,
5576 if (!NT_STATUS_IS_OK(status)) {
5581 session->smb2->should_sign = false;
5582 session->smb2->should_encrypt = false;
5584 if (conn->desire_signing) {
5585 session->smb2->should_sign = true;
5588 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
5589 session->smb2->should_sign = true;
5592 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
5593 session->smb2->should_encrypt = true;
5596 if (conn->protocol < PROTOCOL_SMB2_24) {
5597 session->smb2->should_encrypt = false;
5600 if (conn->smb2.server.cipher == 0) {
5601 session->smb2->should_encrypt = false;
5604 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
5605 sizeof(session->smb2->nonce_high));
5606 session->smb2->nonce_low = 1;
5608 return NT_STATUS_OK;
5611 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
5612 struct smbXcli_session *session1,
5613 struct smbXcli_conn *conn,
5614 struct smbXcli_session **_session2)
5616 struct smbXcli_session *session2;
5618 if (session1->smb2->signing_key.length == 0) {
5619 return NT_STATUS_INVALID_PARAMETER_MIX;
5623 return NT_STATUS_INVALID_PARAMETER_MIX;
5626 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
5627 if (session2 == NULL) {
5628 return NT_STATUS_NO_MEMORY;
5630 session2->smb2 = talloc_reference(session2, session1->smb2);
5631 if (session2->smb2 == NULL) {
5632 talloc_free(session2);
5633 return NT_STATUS_NO_MEMORY;
5636 talloc_set_destructor(session2, smbXcli_session_destructor);
5637 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
5638 session2->conn = conn;
5640 memcpy(session2->smb2_channel.preauth_sha512,
5641 conn->smb2.preauth_sha512,
5642 sizeof(session2->smb2_channel.preauth_sha512));
5644 *_session2 = session2;
5645 return NT_STATUS_OK;
5648 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
5649 const DATA_BLOB _channel_key,
5650 const struct iovec *recv_iov)
5652 struct smbXcli_conn *conn = session->conn;
5653 uint8_t channel_key[16];
5655 struct _derivation {
5660 struct _derivation signing;
5664 return NT_STATUS_INVALID_PARAMETER_MIX;
5667 if (session->smb2_channel.signing_key.length != 0) {
5668 return NT_STATUS_INVALID_PARAMETER_MIX;
5671 if (conn->protocol >= PROTOCOL_SMB3_10) {
5672 struct _derivation *d;
5675 p = data_blob_const(session->smb2_channel.preauth_sha512,
5676 sizeof(session->smb2_channel.preauth_sha512));
5678 d = &derivation.signing;
5679 d->label = data_blob_string_const_null("SMBSigningKey");
5681 } else if (conn->protocol >= PROTOCOL_SMB2_24) {
5682 struct _derivation *d;
5684 d = &derivation.signing;
5685 d->label = data_blob_string_const_null("SMB2AESCMAC");
5686 d->context = data_blob_string_const_null("SmbSign");
5689 ZERO_STRUCT(channel_key);
5690 memcpy(channel_key, _channel_key.data,
5691 MIN(_channel_key.length, sizeof(channel_key)));
5693 session->smb2_channel.signing_key = data_blob_talloc(session,
5695 sizeof(channel_key));
5696 if (session->smb2_channel.signing_key.data == NULL) {
5697 ZERO_STRUCT(channel_key);
5698 return NT_STATUS_NO_MEMORY;
5701 if (conn->protocol >= PROTOCOL_SMB2_24) {
5702 struct _derivation *d = &derivation.signing;
5704 smb2_key_derivation(channel_key, sizeof(channel_key),
5705 d->label.data, d->label.length,
5706 d->context.data, d->context.length,
5707 session->smb2_channel.signing_key.data);
5709 ZERO_STRUCT(channel_key);
5711 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5712 session->conn->protocol,
5714 if (!NT_STATUS_IS_OK(status)) {
5718 return NT_STATUS_OK;
5721 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
5723 if (session->smb2->should_encrypt) {
5724 return NT_STATUS_OK;
5727 if (session->conn->protocol < PROTOCOL_SMB2_24) {
5728 return NT_STATUS_NOT_SUPPORTED;
5731 if (session->conn->smb2.server.cipher == 0) {
5732 return NT_STATUS_NOT_SUPPORTED;
5735 if (session->smb2->signing_key.data == NULL) {
5736 return NT_STATUS_NOT_SUPPORTED;
5738 session->smb2->should_encrypt = true;
5739 return NT_STATUS_OK;
5742 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
5744 struct smbXcli_tcon *tcon;
5746 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
5754 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
5755 uint32_t fs_attributes)
5757 tcon->fs_attributes = fs_attributes;
5760 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
5762 return tcon->fs_attributes;
5765 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
5771 if (tcon->is_smb1) {
5772 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
5779 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
5786 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
5788 return tcon->smb1.tcon_id;
5791 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
5793 tcon->is_smb1 = true;
5794 tcon->smb1.tcon_id = tcon_id;
5797 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
5799 uint16_t optional_support,
5800 uint32_t maximal_access,
5801 uint32_t guest_maximal_access,
5802 const char *service,
5803 const char *fs_type)
5805 tcon->is_smb1 = true;
5806 tcon->fs_attributes = 0;
5807 tcon->smb1.tcon_id = tcon_id;
5808 tcon->smb1.optional_support = optional_support;
5809 tcon->smb1.maximal_access = maximal_access;
5810 tcon->smb1.guest_maximal_access = guest_maximal_access;
5812 TALLOC_FREE(tcon->smb1.service);
5813 tcon->smb1.service = talloc_strdup(tcon, service);
5814 if (service != NULL && tcon->smb1.service == NULL) {
5818 TALLOC_FREE(tcon->smb1.fs_type);
5819 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5820 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5827 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5829 return tcon->smb2.tcon_id;
5832 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5834 return tcon->smb2.capabilities;
5837 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5838 struct smbXcli_session *session,
5842 uint32_t capabilities,
5843 uint32_t maximal_access)
5845 tcon->is_smb1 = false;
5846 tcon->fs_attributes = 0;
5847 tcon->smb2.tcon_id = tcon_id;
5848 tcon->smb2.type = type;
5849 tcon->smb2.flags = flags;
5850 tcon->smb2.capabilities = capabilities;
5851 tcon->smb2.maximal_access = maximal_access;
5853 tcon->smb2.should_sign = false;
5854 tcon->smb2.should_encrypt = false;
5856 if (session == NULL) {
5860 tcon->smb2.should_sign = session->smb2->should_sign;
5861 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5863 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5864 tcon->smb2.should_encrypt = true;
5868 void smb2cli_tcon_should_sign(struct smbXcli_tcon *tcon,
5871 tcon->smb2.should_sign = should_sign;
5874 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon *tcon)
5876 if (tcon->smb2.should_encrypt) {
5880 return tcon->smb2.should_sign;
5883 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon *tcon,
5884 bool should_encrypt)
5886 tcon->smb2.should_encrypt = should_encrypt;
5889 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
5891 return tcon->smb2.should_encrypt;