2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Stefan Metzmacher 2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/network.h"
23 #include "../lib/async_req/async_sock.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
26 #include "lib/util/util_net.h"
27 #include "lib/util/dlinklist.h"
28 #include "../libcli/smb/smb_common.h"
29 #include "../libcli/smb/smb_seal.h"
30 #include "../libcli/smb/smb_signing.h"
31 #include "../libcli/smb/read_smb.h"
32 #include "smbXcli_base.h"
33 #include "librpc/ndr/libndr.h"
34 #include "libcli/smb/smb2_negotiate_context.h"
35 #include <hcrypto/sha.h>
39 struct smbXcli_session;
45 struct sockaddr_storage local_ss;
46 struct sockaddr_storage remote_ss;
47 const char *remote_name;
49 struct tevent_queue *outgoing;
50 struct tevent_req **pending;
51 struct tevent_req *read_smb_req;
53 enum protocol_types min_protocol;
54 enum protocol_types max_protocol;
55 enum protocol_types protocol;
58 bool mandatory_signing;
61 * The incoming dispatch function should return:
62 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
63 * - NT_STATUS_OK, if no more processing is desired, e.g.
64 * the dispatch function called
66 * - All other return values disconnect the connection.
68 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
74 uint32_t capabilities;
79 uint32_t capabilities;
82 uint16_t security_mode;
91 const char *workgroup;
97 uint32_t capabilities;
102 struct smb_signing_state *signing;
103 struct smb_trans_enc_state *trans_enc;
105 struct tevent_req *read_braw_req;
110 uint32_t capabilities;
111 uint16_t security_mode;
116 uint32_t capabilities;
117 uint16_t security_mode;
119 uint32_t max_trans_size;
120 uint32_t max_read_size;
121 uint32_t max_write_size;
129 uint16_t cur_credits;
130 uint16_t max_credits;
132 uint8_t preauth_sha512[64];
135 struct smbXcli_session *sessions;
138 struct smb2cli_session {
140 uint16_t session_flags;
141 DATA_BLOB application_key;
142 DATA_BLOB signing_key;
145 DATA_BLOB encryption_key;
146 DATA_BLOB decryption_key;
149 uint16_t channel_sequence;
153 struct smbXcli_session {
154 struct smbXcli_session *prev, *next;
155 struct smbXcli_conn *conn;
159 DATA_BLOB application_key;
163 struct smb2cli_session *smb2;
166 DATA_BLOB signing_key;
170 * this should be a short term hack
171 * until the upper layers have implemented
174 bool disconnect_expired;
177 struct smbXcli_tcon {
179 uint32_t fs_attributes;
183 uint16_t optional_support;
184 uint32_t maximal_access;
185 uint32_t guest_maximal_access;
194 uint32_t capabilities;
195 uint32_t maximal_access;
201 struct smbXcli_req_state {
202 struct tevent_context *ev;
203 struct smbXcli_conn *conn;
204 struct smbXcli_session *session; /* maybe NULL */
205 struct smbXcli_tcon *tcon; /* maybe NULL */
207 uint8_t length_hdr[4];
214 /* Space for the header including the wct */
215 uint8_t hdr[HDR_VWV];
218 * For normal requests, smb1cli_req_send chooses a mid.
219 * SecondaryV trans requests need to use the mid of the primary
220 * request, so we need a place to store it.
221 * Assume it is set if != 0.
226 uint8_t bytecount_buf[2];
228 #define MAX_SMB_IOV 10
229 /* length_hdr, hdr, words, byte_count, buffers */
230 struct iovec iov[1 + 3 + MAX_SMB_IOV];
235 struct tevent_req **chained_requests;
238 NTSTATUS recv_status;
239 /* always an array of 3 talloc elements */
240 struct iovec *recv_iov;
244 const uint8_t *fixed;
249 uint8_t transform[SMB2_TF_HDR_SIZE];
250 uint8_t hdr[SMB2_HDR_BODY];
251 uint8_t pad[7]; /* padding space for compounding */
254 * always an array of 3 talloc elements
255 * (without a SMB2_TRANSFORM header!)
259 struct iovec *recv_iov;
262 * the expected max for the response dyn_len
264 uint32_t max_dyn_len;
266 uint16_t credit_charge;
270 uint64_t encryption_session_id;
272 bool signing_skipped;
278 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
281 * NT_STATUS_OK, means we do not notify the callers
283 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
285 while (conn->sessions) {
286 conn->sessions->conn = NULL;
287 DLIST_REMOVE(conn->sessions, conn->sessions);
290 if (conn->smb1.trans_enc) {
291 TALLOC_FREE(conn->smb1.trans_enc);
297 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
299 const char *remote_name,
300 enum smb_signing_setting signing_state,
301 uint32_t smb1_capabilities,
302 struct GUID *client_guid,
303 uint32_t smb2_capabilities)
305 struct smbXcli_conn *conn = NULL;
307 struct sockaddr *sa = NULL;
311 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
317 conn->write_fd = dup(fd);
318 if (conn->write_fd == -1) {
322 conn->remote_name = talloc_strdup(conn, remote_name);
323 if (conn->remote_name == NULL) {
328 ss = (void *)&conn->local_ss;
329 sa = (struct sockaddr *)ss;
330 sa_length = sizeof(conn->local_ss);
331 ret = getsockname(fd, sa, &sa_length);
335 ss = (void *)&conn->remote_ss;
336 sa = (struct sockaddr *)ss;
337 sa_length = sizeof(conn->remote_ss);
338 ret = getpeername(fd, sa, &sa_length);
343 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
344 if (conn->outgoing == NULL) {
347 conn->pending = NULL;
349 conn->min_protocol = PROTOCOL_NONE;
350 conn->max_protocol = PROTOCOL_NONE;
351 conn->protocol = PROTOCOL_NONE;
353 switch (signing_state) {
354 case SMB_SIGNING_OFF:
356 conn->allow_signing = false;
357 conn->desire_signing = false;
358 conn->mandatory_signing = false;
360 case SMB_SIGNING_DEFAULT:
361 case SMB_SIGNING_IF_REQUIRED:
362 /* if the server requires it */
363 conn->allow_signing = true;
364 conn->desire_signing = false;
365 conn->mandatory_signing = false;
367 case SMB_SIGNING_REQUIRED:
369 conn->allow_signing = true;
370 conn->desire_signing = true;
371 conn->mandatory_signing = true;
375 conn->smb1.client.capabilities = smb1_capabilities;
376 conn->smb1.client.max_xmit = UINT16_MAX;
378 conn->smb1.capabilities = conn->smb1.client.capabilities;
379 conn->smb1.max_xmit = 1024;
383 /* initialise signing */
384 conn->smb1.signing = smb_signing_init(conn,
386 conn->desire_signing,
387 conn->mandatory_signing);
388 if (!conn->smb1.signing) {
392 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
393 if (conn->mandatory_signing) {
394 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
397 conn->smb2.client.guid = *client_guid;
399 conn->smb2.client.capabilities = smb2_capabilities;
401 conn->smb2.cur_credits = 1;
402 conn->smb2.max_credits = 0;
404 talloc_set_destructor(conn, smbXcli_conn_destructor);
408 if (conn->write_fd != -1) {
409 close(conn->write_fd);
415 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
421 if (conn->read_fd == -1) {
428 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
430 return conn->protocol;
433 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
435 if (conn->protocol >= PROTOCOL_SMB2_02) {
439 if (conn->smb1.capabilities & CAP_UNICODE) {
446 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
448 set_socket_options(conn->read_fd, options);
451 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
453 return &conn->local_ss;
456 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
458 return &conn->remote_ss;
461 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
463 return conn->remote_name;
466 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
468 if (conn->protocol >= PROTOCOL_SMB2_02) {
475 return conn->smb1.server.max_mux;
478 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
480 if (conn->protocol >= PROTOCOL_SMB2_02) {
481 return conn->smb2.server.system_time;
484 return conn->smb1.server.system_time;
487 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
489 if (conn->protocol >= PROTOCOL_SMB2_02) {
490 return &conn->smb2.server.gss_blob;
493 return &conn->smb1.server.gss_blob;
496 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
498 if (conn->protocol >= PROTOCOL_SMB2_02) {
499 return &conn->smb2.server.guid;
502 return &conn->smb1.server.guid;
505 struct smbXcli_conn_samba_suicide_state {
506 struct smbXcli_conn *conn;
511 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
513 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
514 struct tevent_context *ev,
515 struct smbXcli_conn *conn,
518 struct tevent_req *req, *subreq;
519 struct smbXcli_conn_samba_suicide_state *state;
521 req = tevent_req_create(mem_ctx, &state,
522 struct smbXcli_conn_samba_suicide_state);
527 SIVAL(state->buf, 4, 0x74697865);
528 SCVAL(state->buf, 8, exitcode);
529 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
531 state->iov.iov_base = state->buf;
532 state->iov.iov_len = sizeof(state->buf);
534 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
535 false, &state->iov, 1);
536 if (tevent_req_nomem(subreq, req)) {
537 return tevent_req_post(req, ev);
539 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
543 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
545 struct tevent_req *req = tevent_req_callback_data(
546 subreq, struct tevent_req);
547 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
548 req, struct smbXcli_conn_samba_suicide_state);
552 nwritten = writev_recv(subreq, &err);
554 if (nwritten == -1) {
555 NTSTATUS status = map_nt_error_from_unix_common(err);
556 smbXcli_conn_disconnect(state->conn, status);
559 tevent_req_done(req);
562 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
564 return tevent_req_simple_recv_ntstatus(req);
567 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
570 TALLOC_CTX *frame = talloc_stackframe();
571 struct tevent_context *ev;
572 struct tevent_req *req;
573 NTSTATUS status = NT_STATUS_NO_MEMORY;
576 if (smbXcli_conn_has_async_calls(conn)) {
578 * Can't use sync call while an async call is in flight
580 status = NT_STATUS_INVALID_PARAMETER_MIX;
583 ev = samba_tevent_context_init(frame);
587 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
591 ok = tevent_req_poll(req, ev);
593 status = map_nt_error_from_unix_common(errno);
596 status = smbXcli_conn_samba_suicide_recv(req);
602 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
604 return conn->smb1.capabilities;
607 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
609 return conn->smb1.max_xmit;
612 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
615 uint16_t possible = conn->smb1.server.max_mux;
617 pending = tevent_queue_length(conn->outgoing);
618 if (pending >= possible) {
621 pending += talloc_array_length(conn->pending);
622 if (pending >= possible) {
629 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
631 return conn->smb1.server.session_key;
634 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
636 return conn->smb1.server.challenge;
639 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
641 return conn->smb1.server.security_mode;
644 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
646 return conn->smb1.server.readbraw;
649 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
651 return conn->smb1.server.writebraw;
654 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
656 return conn->smb1.server.lockread;
659 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
661 return conn->smb1.server.writeunlock;
664 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
666 return conn->smb1.server.time_zone;
669 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
670 const DATA_BLOB user_session_key,
671 const DATA_BLOB response)
673 return smb_signing_activate(conn->smb1.signing,
678 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
679 const uint8_t *buf, uint32_t seqnum)
681 const uint8_t *hdr = buf + NBT_HDR_SIZE;
682 size_t len = smb_len_nbt(buf);
684 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
687 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
689 return smb_signing_is_active(conn->smb1.signing);
692 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
693 struct smb_trans_enc_state *es)
695 /* Replace the old state, if any. */
696 if (conn->smb1.trans_enc) {
697 TALLOC_FREE(conn->smb1.trans_enc);
699 conn->smb1.trans_enc = es;
702 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
704 return common_encryption_on(conn->smb1.trans_enc);
708 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
710 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
711 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
713 if (NT_STATUS_IS_OK(status)) {
717 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
721 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
725 * Is the SMB command able to hold an AND_X successor
726 * @param[in] cmd The SMB command in question
727 * @retval Can we add a chained request after "cmd"?
729 bool smb1cli_is_andx_req(uint8_t cmd)
749 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
751 size_t num_pending = talloc_array_length(conn->pending);
754 if (conn->protocol == PROTOCOL_NONE) {
756 * This is what windows sends on the SMB1 Negprot request
757 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
765 result = conn->smb1.mid++;
766 if ((result == 0) || (result == 0xffff)) {
770 for (i=0; i<num_pending; i++) {
771 if (result == smb1cli_req_mid(conn->pending[i])) {
776 if (i == num_pending) {
782 void smbXcli_req_unset_pending(struct tevent_req *req)
784 struct smbXcli_req_state *state =
786 struct smbXcli_req_state);
787 struct smbXcli_conn *conn = state->conn;
788 size_t num_pending = talloc_array_length(conn->pending);
791 if (state->smb1.mid != 0) {
793 * This is a [nt]trans[2] request which waits
794 * for more than one reply.
799 tevent_req_set_cleanup_fn(req, NULL);
801 if (num_pending == 1) {
803 * The pending read_smb tevent_req is a child of
804 * conn->pending. So if nothing is pending anymore, we need to
805 * delete the socket read fde.
807 TALLOC_FREE(conn->pending);
808 conn->read_smb_req = NULL;
812 for (i=0; i<num_pending; i++) {
813 if (req == conn->pending[i]) {
817 if (i == num_pending) {
819 * Something's seriously broken. Just returning here is the
820 * right thing nevertheless, the point of this routine is to
821 * remove ourselves from conn->pending.
827 * Remove ourselves from the conn->pending array
829 for (; i < (num_pending - 1); i++) {
830 conn->pending[i] = conn->pending[i+1];
834 * No NULL check here, we're shrinking by sizeof(void *), and
835 * talloc_realloc just adjusts the size for this.
837 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
842 static void smbXcli_req_cleanup(struct tevent_req *req,
843 enum tevent_req_state req_state)
845 struct smbXcli_req_state *state =
847 struct smbXcli_req_state);
850 case TEVENT_REQ_RECEIVED:
852 * Make sure we really remove it from
853 * the pending array on destruction.
856 smbXcli_req_unset_pending(req);
863 static bool smb1cli_req_cancel(struct tevent_req *req);
864 static bool smb2cli_req_cancel(struct tevent_req *req);
866 static bool smbXcli_req_cancel(struct tevent_req *req)
868 struct smbXcli_req_state *state =
870 struct smbXcli_req_state);
872 if (!smbXcli_conn_is_connected(state->conn)) {
876 if (state->conn->protocol == PROTOCOL_NONE) {
880 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
881 return smb2cli_req_cancel(req);
884 return smb1cli_req_cancel(req);
887 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
889 bool smbXcli_req_set_pending(struct tevent_req *req)
891 struct smbXcli_req_state *state =
893 struct smbXcli_req_state);
894 struct smbXcli_conn *conn;
895 struct tevent_req **pending;
900 if (!smbXcli_conn_is_connected(conn)) {
904 num_pending = talloc_array_length(conn->pending);
906 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
908 if (pending == NULL) {
911 pending[num_pending] = req;
912 conn->pending = pending;
913 tevent_req_set_cleanup_fn(req, smbXcli_req_cleanup);
914 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
916 if (!smbXcli_conn_receive_next(conn)) {
918 * the caller should notify the current request
920 * And all other pending requests get notified
921 * by smbXcli_conn_disconnect().
923 smbXcli_req_unset_pending(req);
924 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
931 static void smbXcli_conn_received(struct tevent_req *subreq);
933 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
935 size_t num_pending = talloc_array_length(conn->pending);
936 struct tevent_req *req;
937 struct smbXcli_req_state *state;
939 if (conn->read_smb_req != NULL) {
943 if (num_pending == 0) {
944 if (conn->smb2.mid < UINT64_MAX) {
945 /* no more pending requests, so we are done for now */
950 * If there are no more SMB2 requests possible,
951 * because we are out of message ids,
952 * we need to disconnect.
954 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
958 req = conn->pending[0];
959 state = tevent_req_data(req, struct smbXcli_req_state);
962 * We're the first ones, add the read_smb request that waits for the
963 * answer from the server
965 conn->read_smb_req = read_smb_send(conn->pending,
968 if (conn->read_smb_req == NULL) {
971 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
975 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
977 struct smbXcli_session *session;
979 tevent_queue_stop(conn->outgoing);
981 if (conn->read_fd != -1) {
982 close(conn->read_fd);
984 if (conn->write_fd != -1) {
985 close(conn->write_fd);
990 session = conn->sessions;
991 if (talloc_array_length(conn->pending) == 0) {
993 * if we do not have pending requests
994 * there is no need to update the channel_sequence
998 for (; session; session = session->next) {
999 smb2cli_session_increment_channel_sequence(session);
1003 * Cancel all pending requests. We do not do a for-loop walking
1004 * conn->pending because that array changes in
1005 * smbXcli_req_unset_pending.
1007 while (talloc_array_length(conn->pending) > 0) {
1008 struct tevent_req *req;
1009 struct smbXcli_req_state *state;
1010 struct tevent_req **chain;
1014 req = conn->pending[0];
1015 state = tevent_req_data(req, struct smbXcli_req_state);
1017 if (state->smb1.chained_requests == NULL) {
1019 * We're dead. No point waiting for trans2
1022 state->smb1.mid = 0;
1024 smbXcli_req_unset_pending(req);
1026 if (NT_STATUS_IS_OK(status)) {
1027 /* do not notify the callers */
1032 * we need to defer the callback, because we may notify
1033 * more then one caller.
1035 tevent_req_defer_callback(req, state->ev);
1036 tevent_req_nterror(req, status);
1040 chain = talloc_move(conn, &state->smb1.chained_requests);
1041 num_chained = talloc_array_length(chain);
1043 for (i=0; i<num_chained; i++) {
1045 state = tevent_req_data(req, struct smbXcli_req_state);
1048 * We're dead. No point waiting for trans2
1051 state->smb1.mid = 0;
1053 smbXcli_req_unset_pending(req);
1055 if (NT_STATUS_IS_OK(status)) {
1056 /* do not notify the callers */
1061 * we need to defer the callback, because we may notify
1062 * more than one caller.
1064 tevent_req_defer_callback(req, state->ev);
1065 tevent_req_nterror(req, status);
1072 * Fetch a smb request's mid. Only valid after the request has been sent by
1073 * smb1cli_req_send().
1075 uint16_t smb1cli_req_mid(struct tevent_req *req)
1077 struct smbXcli_req_state *state =
1078 tevent_req_data(req,
1079 struct smbXcli_req_state);
1081 if (state->smb1.mid != 0) {
1082 return state->smb1.mid;
1085 return SVAL(state->smb1.hdr, HDR_MID);
1088 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1090 struct smbXcli_req_state *state =
1091 tevent_req_data(req,
1092 struct smbXcli_req_state);
1094 state->smb1.mid = mid;
1097 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1099 struct smbXcli_req_state *state =
1100 tevent_req_data(req,
1101 struct smbXcli_req_state);
1103 return state->smb1.seqnum;
1106 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1108 struct smbXcli_req_state *state =
1109 tevent_req_data(req,
1110 struct smbXcli_req_state);
1112 state->smb1.seqnum = seqnum;
1115 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1119 for (i=0; i<count; i++) {
1120 result += iov[i].iov_len;
1125 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1126 const struct iovec *iov,
1129 size_t len = smbXcli_iov_len(iov, count);
1134 buf = talloc_array(mem_ctx, uint8_t, len);
1139 for (i=0; i<count; i++) {
1140 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1141 copied += iov[i].iov_len;
1146 static void smb1cli_req_flags(enum protocol_types protocol,
1147 uint32_t smb1_capabilities,
1148 uint8_t smb_command,
1149 uint8_t additional_flags,
1150 uint8_t clear_flags,
1152 uint16_t additional_flags2,
1153 uint16_t clear_flags2,
1157 uint16_t flags2 = 0;
1159 if (protocol >= PROTOCOL_LANMAN1) {
1160 flags |= FLAG_CASELESS_PATHNAMES;
1161 flags |= FLAG_CANONICAL_PATHNAMES;
1164 if (protocol >= PROTOCOL_LANMAN2) {
1165 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1166 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1169 if (protocol >= PROTOCOL_NT1) {
1170 flags2 |= FLAGS2_IS_LONG_NAME;
1172 if (smb1_capabilities & CAP_UNICODE) {
1173 flags2 |= FLAGS2_UNICODE_STRINGS;
1175 if (smb1_capabilities & CAP_STATUS32) {
1176 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1178 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1179 flags2 |= FLAGS2_EXTENDED_SECURITY;
1183 flags |= additional_flags;
1184 flags &= ~clear_flags;
1185 flags2 |= additional_flags2;
1186 flags2 &= ~clear_flags2;
1192 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1194 static bool smb1cli_req_cancel(struct tevent_req *req)
1196 struct smbXcli_req_state *state =
1197 tevent_req_data(req,
1198 struct smbXcli_req_state);
1203 struct tevent_req *subreq;
1206 flags = CVAL(state->smb1.hdr, HDR_FLG);
1207 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1208 pid = SVAL(state->smb1.hdr, HDR_PID);
1209 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1210 mid = SVAL(state->smb1.hdr, HDR_MID);
1212 subreq = smb1cli_req_create(state, state->ev,
1222 0, NULL); /* bytes */
1223 if (subreq == NULL) {
1226 smb1cli_req_set_mid(subreq, mid);
1228 status = smb1cli_req_chain_submit(&subreq, 1);
1229 if (!NT_STATUS_IS_OK(status)) {
1230 TALLOC_FREE(subreq);
1233 smb1cli_req_set_mid(subreq, 0);
1235 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1240 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1242 /* we do not care about the result */
1243 TALLOC_FREE(subreq);
1246 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1247 struct tevent_context *ev,
1248 struct smbXcli_conn *conn,
1249 uint8_t smb_command,
1250 uint8_t additional_flags,
1251 uint8_t clear_flags,
1252 uint16_t additional_flags2,
1253 uint16_t clear_flags2,
1254 uint32_t timeout_msec,
1256 struct smbXcli_tcon *tcon,
1257 struct smbXcli_session *session,
1258 uint8_t wct, uint16_t *vwv,
1260 struct iovec *bytes_iov)
1262 struct tevent_req *req;
1263 struct smbXcli_req_state *state;
1265 uint16_t flags2 = 0;
1269 if (iov_count > MAX_SMB_IOV) {
1271 * Should not happen :-)
1276 req = tevent_req_create(mem_ctx, &state,
1277 struct smbXcli_req_state);
1283 state->session = session;
1287 uid = session->smb1.session_id;
1291 tid = tcon->smb1.tcon_id;
1293 if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
1294 clear_flags |= FLAG_CASELESS_PATHNAMES;
1296 /* Default setting, case insensitive. */
1297 additional_flags |= FLAG_CASELESS_PATHNAMES;
1300 if (smbXcli_conn_dfs_supported(conn) &&
1301 smbXcli_tcon_is_dfs_share(tcon))
1303 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
1307 state->smb1.recv_cmd = 0xFF;
1308 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1309 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1310 if (state->smb1.recv_iov == NULL) {
1315 smb1cli_req_flags(conn->protocol,
1316 conn->smb1.capabilities,
1325 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1326 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1327 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1328 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1329 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1330 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1331 SSVAL(state->smb1.hdr, HDR_TID, tid);
1332 SSVAL(state->smb1.hdr, HDR_PID, pid);
1333 SSVAL(state->smb1.hdr, HDR_UID, uid);
1334 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1335 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1337 state->smb1.vwv = vwv;
1339 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1341 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1342 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1343 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1344 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1345 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1346 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1347 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1348 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1350 if (iov_count != 0) {
1351 memcpy(&state->smb1.iov[4], bytes_iov,
1352 iov_count * sizeof(*bytes_iov));
1354 state->smb1.iov_count = iov_count + 4;
1356 if (timeout_msec > 0) {
1357 struct timeval endtime;
1359 endtime = timeval_current_ofs_msec(timeout_msec);
1360 if (!tevent_req_set_endtime(req, ev, endtime)) {
1365 switch (smb_command) {
1369 state->one_way = true;
1372 state->one_way = true;
1373 state->smb1.one_way_seqnum = true;
1377 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1378 state->one_way = true;
1386 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1387 struct iovec *iov, int iov_count,
1389 bool one_way_seqnum)
1391 TALLOC_CTX *frame = NULL;
1395 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1396 * iovec directly. MD5Update would do that just fine.
1399 if (iov_count < 4) {
1400 return NT_STATUS_INVALID_PARAMETER_MIX;
1402 if (iov[0].iov_len != NBT_HDR_SIZE) {
1403 return NT_STATUS_INVALID_PARAMETER_MIX;
1405 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1406 return NT_STATUS_INVALID_PARAMETER_MIX;
1408 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1409 return NT_STATUS_INVALID_PARAMETER_MIX;
1411 if (iov[3].iov_len != sizeof(uint16_t)) {
1412 return NT_STATUS_INVALID_PARAMETER_MIX;
1415 frame = talloc_stackframe();
1417 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1419 return NT_STATUS_NO_MEMORY;
1422 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1424 smb_signing_sign_pdu(conn->smb1.signing,
1425 buf, talloc_get_size(buf),
1427 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1430 return NT_STATUS_OK;
1433 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1434 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1435 TALLOC_CTX *tmp_mem,
1438 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1439 struct smbXcli_req_state *state,
1440 struct iovec *iov, int iov_count)
1442 struct tevent_req *subreq;
1447 if (!smbXcli_conn_is_connected(state->conn)) {
1448 return NT_STATUS_CONNECTION_DISCONNECTED;
1451 if (state->conn->protocol > PROTOCOL_NT1) {
1452 return NT_STATUS_REVISION_MISMATCH;
1455 if (iov_count < 4) {
1456 return NT_STATUS_INVALID_PARAMETER_MIX;
1458 if (iov[0].iov_len != NBT_HDR_SIZE) {
1459 return NT_STATUS_INVALID_PARAMETER_MIX;
1461 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1462 return NT_STATUS_INVALID_PARAMETER_MIX;
1464 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1465 return NT_STATUS_INVALID_PARAMETER_MIX;
1467 if (iov[3].iov_len != sizeof(uint16_t)) {
1468 return NT_STATUS_INVALID_PARAMETER_MIX;
1471 cmd = CVAL(iov[1].iov_base, HDR_COM);
1472 if (cmd == SMBreadBraw) {
1473 if (smbXcli_conn_has_async_calls(state->conn)) {
1474 return NT_STATUS_INVALID_PARAMETER_MIX;
1476 state->conn->smb1.read_braw_req = req;
1479 if (state->smb1.mid != 0) {
1480 mid = state->smb1.mid;
1482 mid = smb1cli_alloc_mid(state->conn);
1484 SSVAL(iov[1].iov_base, HDR_MID, mid);
1486 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1488 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1489 &state->smb1.seqnum,
1490 state->smb1.one_way_seqnum);
1492 if (!NT_STATUS_IS_OK(status)) {
1497 * If we supported multiple encrytion contexts
1498 * here we'd look up based on tid.
1500 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1501 char *buf, *enc_buf;
1503 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1505 return NT_STATUS_NO_MEMORY;
1507 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1508 (char *)buf, &enc_buf);
1510 if (!NT_STATUS_IS_OK(status)) {
1511 DEBUG(0, ("Error in encrypting client message: %s\n",
1512 nt_errstr(status)));
1515 buf = (char *)talloc_memdup(state, enc_buf,
1516 smb_len_nbt(enc_buf)+4);
1519 return NT_STATUS_NO_MEMORY;
1521 iov[0].iov_base = (void *)buf;
1522 iov[0].iov_len = talloc_get_size(buf);
1526 if (state->conn->dispatch_incoming == NULL) {
1527 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1530 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1532 subreq = writev_send(state, state->ev, state->conn->outgoing,
1533 state->conn->write_fd, false, iov, iov_count);
1534 if (subreq == NULL) {
1535 return NT_STATUS_NO_MEMORY;
1537 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1538 return NT_STATUS_OK;
1541 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1542 struct tevent_context *ev,
1543 struct smbXcli_conn *conn,
1544 uint8_t smb_command,
1545 uint8_t additional_flags,
1546 uint8_t clear_flags,
1547 uint16_t additional_flags2,
1548 uint16_t clear_flags2,
1549 uint32_t timeout_msec,
1551 struct smbXcli_tcon *tcon,
1552 struct smbXcli_session *session,
1553 uint8_t wct, uint16_t *vwv,
1555 const uint8_t *bytes)
1557 struct tevent_req *req;
1561 iov.iov_base = discard_const_p(void, bytes);
1562 iov.iov_len = num_bytes;
1564 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1565 additional_flags, clear_flags,
1566 additional_flags2, clear_flags2,
1573 if (!tevent_req_is_in_progress(req)) {
1574 return tevent_req_post(req, ev);
1576 status = smb1cli_req_chain_submit(&req, 1);
1577 if (tevent_req_nterror(req, status)) {
1578 return tevent_req_post(req, ev);
1583 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1585 struct tevent_req *req =
1586 tevent_req_callback_data(subreq,
1588 struct smbXcli_req_state *state =
1589 tevent_req_data(req,
1590 struct smbXcli_req_state);
1594 nwritten = writev_recv(subreq, &err);
1595 TALLOC_FREE(subreq);
1596 if (nwritten == -1) {
1597 NTSTATUS status = map_nt_error_from_unix_common(err);
1598 smbXcli_conn_disconnect(state->conn, status);
1602 if (state->one_way) {
1603 state->inbuf = NULL;
1604 tevent_req_done(req);
1608 if (!smbXcli_req_set_pending(req)) {
1609 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1614 static void smbXcli_conn_received(struct tevent_req *subreq)
1616 struct smbXcli_conn *conn =
1617 tevent_req_callback_data(subreq,
1618 struct smbXcli_conn);
1619 TALLOC_CTX *frame = talloc_stackframe();
1625 if (subreq != conn->read_smb_req) {
1626 DEBUG(1, ("Internal error: cli_smb_received called with "
1627 "unexpected subreq\n"));
1628 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1632 conn->read_smb_req = NULL;
1634 received = read_smb_recv(subreq, frame, &inbuf, &err);
1635 TALLOC_FREE(subreq);
1636 if (received == -1) {
1637 status = map_nt_error_from_unix_common(err);
1638 smbXcli_conn_disconnect(conn, status);
1643 status = conn->dispatch_incoming(conn, frame, inbuf);
1645 if (NT_STATUS_IS_OK(status)) {
1647 * We should not do any more processing
1648 * as the dispatch function called
1649 * tevent_req_done().
1654 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1656 * We got an error, so notify all pending requests
1658 smbXcli_conn_disconnect(conn, status);
1663 * We got NT_STATUS_RETRY, so we may ask for a
1664 * next incoming pdu.
1666 if (!smbXcli_conn_receive_next(conn)) {
1667 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1671 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1672 struct iovec **piov, int *pnum_iov)
1683 size_t min_size = MIN_SMB_SIZE;
1685 buflen = smb_len_tcp(buf);
1688 hdr = buf + NBT_HDR_SIZE;
1690 status = smb1cli_pull_raw_error(hdr);
1691 if (NT_STATUS_IS_ERR(status)) {
1693 * This is an ugly hack to support OS/2
1694 * which skips the byte_count in the DATA block
1695 * on some error responses.
1699 min_size -= sizeof(uint16_t);
1702 if (buflen < min_size) {
1703 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1707 * This returns iovec elements in the following order:
1722 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1724 return NT_STATUS_NO_MEMORY;
1726 iov[0].iov_base = hdr;
1727 iov[0].iov_len = HDR_WCT;
1730 cmd = CVAL(hdr, HDR_COM);
1734 size_t len = buflen - taken;
1736 struct iovec *iov_tmp;
1743 * we need at least WCT
1745 needed = sizeof(uint8_t);
1747 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1748 __location__, (int)len, (int)needed));
1753 * Now we check if the specified words are there
1755 wct = CVAL(hdr, wct_ofs);
1756 needed += wct * sizeof(uint16_t);
1758 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1759 __location__, (int)len, (int)needed));
1763 if ((num_iov == 1) &&
1765 NT_STATUS_IS_ERR(status))
1768 * This is an ugly hack to support OS/2
1769 * which skips the byte_count in the DATA block
1770 * on some error responses.
1774 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1776 if (iov_tmp == NULL) {
1778 return NT_STATUS_NO_MEMORY;
1781 cur = &iov[num_iov];
1785 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1787 cur[1].iov_base = cur[0].iov_base;
1794 * we need at least BCC
1796 needed += sizeof(uint16_t);
1798 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1799 __location__, (int)len, (int)needed));
1804 * Now we check if the specified bytes are there
1806 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1807 bcc = SVAL(hdr, bcc_ofs);
1808 needed += bcc * sizeof(uint8_t);
1810 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1811 __location__, (int)len, (int)needed));
1816 * we allocate 2 iovec structures for words and bytes
1818 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1820 if (iov_tmp == NULL) {
1822 return NT_STATUS_NO_MEMORY;
1825 cur = &iov[num_iov];
1828 cur[0].iov_len = wct * sizeof(uint16_t);
1829 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1830 cur[1].iov_len = bcc * sizeof(uint8_t);
1831 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1835 if (!smb1cli_is_andx_req(cmd)) {
1837 * If the current command does not have AndX chanining
1843 if (wct == 0 && bcc == 0) {
1845 * An empty response also ends the chain,
1846 * most likely with an error.
1852 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1853 __location__, (int)wct, (int)cmd));
1856 cmd = CVAL(cur[0].iov_base, 0);
1859 * If it is the end of the chain we are also done.
1863 wct_ofs = SVAL(cur[0].iov_base, 2);
1865 if (wct_ofs < taken) {
1866 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1868 if (wct_ofs > buflen) {
1869 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1873 * we consumed everything up to the start of the next
1879 remaining = buflen - taken;
1881 if (remaining > 0 && num_iov >= 3) {
1883 * The last DATA block gets the remaining
1884 * bytes, this is needed to support
1885 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1887 iov[num_iov-1].iov_len += remaining;
1891 *pnum_iov = num_iov;
1892 return NT_STATUS_OK;
1896 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1899 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1900 TALLOC_CTX *tmp_mem,
1903 struct tevent_req *req;
1904 struct smbXcli_req_state *state;
1911 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1912 size_t len = smb_len_tcp(inbuf);
1913 struct iovec *iov = NULL;
1915 struct tevent_req **chain = NULL;
1916 size_t num_chained = 0;
1917 size_t num_responses = 0;
1919 if (conn->smb1.read_braw_req != NULL) {
1920 req = conn->smb1.read_braw_req;
1921 conn->smb1.read_braw_req = NULL;
1922 state = tevent_req_data(req, struct smbXcli_req_state);
1924 smbXcli_req_unset_pending(req);
1926 if (state->smb1.recv_iov == NULL) {
1928 * For requests with more than
1929 * one response, we have to readd the
1932 state->smb1.recv_iov = talloc_zero_array(state,
1935 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1936 return NT_STATUS_OK;
1940 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1941 state->smb1.recv_iov[0].iov_len = len;
1942 ZERO_STRUCT(state->smb1.recv_iov[1]);
1943 ZERO_STRUCT(state->smb1.recv_iov[2]);
1945 state->smb1.recv_cmd = SMBreadBraw;
1946 state->smb1.recv_status = NT_STATUS_OK;
1947 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1949 tevent_req_done(req);
1950 return NT_STATUS_OK;
1953 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1954 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1955 DEBUG(10, ("Got non-SMB PDU\n"));
1956 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1960 * If we supported multiple encrytion contexts
1961 * here we'd look up based on tid.
1963 if (common_encryption_on(conn->smb1.trans_enc)
1964 && (CVAL(inbuf, 0) == 0)) {
1965 uint16_t enc_ctx_num;
1967 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1968 if (!NT_STATUS_IS_OK(status)) {
1969 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1970 nt_errstr(status)));
1974 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1975 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1977 conn->smb1.trans_enc->enc_ctx_num));
1978 return NT_STATUS_INVALID_HANDLE;
1981 status = common_decrypt_buffer(conn->smb1.trans_enc,
1983 if (!NT_STATUS_IS_OK(status)) {
1984 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1985 nt_errstr(status)));
1988 inhdr = inbuf + NBT_HDR_SIZE;
1989 len = smb_len_nbt(inbuf);
1992 mid = SVAL(inhdr, HDR_MID);
1993 num_pending = talloc_array_length(conn->pending);
1995 for (i=0; i<num_pending; i++) {
1996 if (mid == smb1cli_req_mid(conn->pending[i])) {
2000 if (i == num_pending) {
2001 /* Dump unexpected reply */
2002 return NT_STATUS_RETRY;
2005 oplock_break = false;
2007 if (mid == 0xffff) {
2009 * Paranoia checks that this is really an oplock break request.
2011 oplock_break = (len == 51); /* hdr + 8 words */
2012 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
2013 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
2014 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
2015 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
2017 if (!oplock_break) {
2018 /* Dump unexpected reply */
2019 return NT_STATUS_RETRY;
2023 req = conn->pending[i];
2024 state = tevent_req_data(req, struct smbXcli_req_state);
2026 if (!oplock_break /* oplock breaks are not signed */
2027 && !smb_signing_check_pdu(conn->smb1.signing,
2028 inhdr, len, state->smb1.seqnum+1)) {
2029 DEBUG(10, ("cli_check_sign_mac failed\n"));
2030 return NT_STATUS_ACCESS_DENIED;
2033 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2035 if (!NT_STATUS_IS_OK(status)) {
2036 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2037 nt_errstr(status)));
2041 cmd = CVAL(inhdr, HDR_COM);
2042 status = smb1cli_pull_raw_error(inhdr);
2044 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2045 (state->session != NULL) && state->session->disconnect_expired)
2048 * this should be a short term hack
2049 * until the upper layers have implemented
2050 * re-authentication.
2055 if (state->smb1.chained_requests == NULL) {
2057 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2060 smbXcli_req_unset_pending(req);
2062 if (state->smb1.recv_iov == NULL) {
2064 * For requests with more than
2065 * one response, we have to readd the
2068 state->smb1.recv_iov = talloc_zero_array(state,
2071 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2072 return NT_STATUS_OK;
2076 state->smb1.recv_cmd = cmd;
2077 state->smb1.recv_status = status;
2078 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2080 state->smb1.recv_iov[0] = iov[0];
2081 state->smb1.recv_iov[1] = iov[1];
2082 state->smb1.recv_iov[2] = iov[2];
2084 if (talloc_array_length(conn->pending) == 0) {
2085 tevent_req_done(req);
2086 return NT_STATUS_OK;
2089 tevent_req_defer_callback(req, state->ev);
2090 tevent_req_done(req);
2091 return NT_STATUS_RETRY;
2094 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2095 num_chained = talloc_array_length(chain);
2096 num_responses = (num_iov - 1)/2;
2098 if (num_responses > num_chained) {
2099 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2102 for (i=0; i<num_chained; i++) {
2103 size_t iov_idx = 1 + (i*2);
2104 struct iovec *cur = &iov[iov_idx];
2108 state = tevent_req_data(req, struct smbXcli_req_state);
2110 smbXcli_req_unset_pending(req);
2113 * as we finish multiple requests here
2114 * we need to defer the callbacks as
2115 * they could destroy our current stack state.
2117 tevent_req_defer_callback(req, state->ev);
2119 if (i >= num_responses) {
2120 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2124 if (state->smb1.recv_iov == NULL) {
2126 * For requests with more than
2127 * one response, we have to readd the
2130 state->smb1.recv_iov = talloc_zero_array(state,
2133 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2138 state->smb1.recv_cmd = cmd;
2140 if (i == (num_responses - 1)) {
2142 * The last request in the chain gets the status
2144 state->smb1.recv_status = status;
2146 cmd = CVAL(cur[0].iov_base, 0);
2147 state->smb1.recv_status = NT_STATUS_OK;
2150 state->inbuf = inbuf;
2153 * Note: here we use talloc_reference() in a way
2154 * that does not expose it to the caller.
2156 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2157 if (tevent_req_nomem(inbuf_ref, req)) {
2161 /* copy the related buffers */
2162 state->smb1.recv_iov[0] = iov[0];
2163 state->smb1.recv_iov[1] = cur[0];
2164 state->smb1.recv_iov[2] = cur[1];
2166 tevent_req_done(req);
2169 return NT_STATUS_RETRY;
2172 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2173 TALLOC_CTX *mem_ctx,
2174 struct iovec **piov,
2178 uint32_t *pvwv_offset,
2179 uint32_t *pnum_bytes,
2181 uint32_t *pbytes_offset,
2183 const struct smb1cli_req_expected_response *expected,
2184 size_t num_expected)
2186 struct smbXcli_req_state *state =
2187 tevent_req_data(req,
2188 struct smbXcli_req_state);
2189 NTSTATUS status = NT_STATUS_OK;
2190 struct iovec *recv_iov = NULL;
2191 uint8_t *hdr = NULL;
2193 uint32_t vwv_offset = 0;
2194 uint16_t *vwv = NULL;
2195 uint32_t num_bytes = 0;
2196 uint32_t bytes_offset = 0;
2197 uint8_t *bytes = NULL;
2199 bool found_status = false;
2200 bool found_size = false;
2214 if (pvwv_offset != NULL) {
2217 if (pnum_bytes != NULL) {
2220 if (pbytes != NULL) {
2223 if (pbytes_offset != NULL) {
2226 if (pinbuf != NULL) {
2230 if (state->inbuf != NULL) {
2231 recv_iov = state->smb1.recv_iov;
2232 state->smb1.recv_iov = NULL;
2233 if (state->smb1.recv_cmd != SMBreadBraw) {
2234 hdr = (uint8_t *)recv_iov[0].iov_base;
2235 wct = recv_iov[1].iov_len/2;
2236 vwv = (uint16_t *)recv_iov[1].iov_base;
2237 vwv_offset = PTR_DIFF(vwv, hdr);
2238 num_bytes = recv_iov[2].iov_len;
2239 bytes = (uint8_t *)recv_iov[2].iov_base;
2240 bytes_offset = PTR_DIFF(bytes, hdr);
2244 if (tevent_req_is_nterror(req, &status)) {
2245 for (i=0; i < num_expected; i++) {
2246 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2247 found_status = true;
2253 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2259 if (num_expected == 0) {
2260 found_status = true;
2264 status = state->smb1.recv_status;
2266 for (i=0; i < num_expected; i++) {
2267 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2271 found_status = true;
2272 if (expected[i].wct == 0) {
2277 if (expected[i].wct == wct) {
2283 if (!found_status) {
2288 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2292 *piov = talloc_move(mem_ctx, &recv_iov);
2304 if (pvwv_offset != NULL) {
2305 *pvwv_offset = vwv_offset;
2307 if (pnum_bytes != NULL) {
2308 *pnum_bytes = num_bytes;
2310 if (pbytes != NULL) {
2313 if (pbytes_offset != NULL) {
2314 *pbytes_offset = bytes_offset;
2316 if (pinbuf != NULL) {
2317 *pinbuf = state->inbuf;
2323 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2330 for (i=0; i<num_reqs; i++) {
2331 struct smbXcli_req_state *state;
2332 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2333 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2334 state->smb1.iov_count-2);
2335 wct_ofs = (wct_ofs + 3) & ~3;
2340 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2342 struct smbXcli_req_state *first_state =
2343 tevent_req_data(reqs[0],
2344 struct smbXcli_req_state);
2345 struct smbXcli_req_state *state;
2347 size_t chain_padding = 0;
2349 struct iovec *iov = NULL;
2350 struct iovec *this_iov;
2354 if (num_reqs == 1) {
2355 return smb1cli_req_writev_submit(reqs[0], first_state,
2356 first_state->smb1.iov,
2357 first_state->smb1.iov_count);
2361 for (i=0; i<num_reqs; i++) {
2362 if (!tevent_req_is_in_progress(reqs[i])) {
2363 return NT_STATUS_INTERNAL_ERROR;
2366 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2368 if (state->smb1.iov_count < 4) {
2369 return NT_STATUS_INVALID_PARAMETER_MIX;
2374 * The NBT and SMB header
2387 iovlen += state->smb1.iov_count - 2;
2390 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2392 return NT_STATUS_NO_MEMORY;
2395 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2396 first_state, reqs, sizeof(*reqs) * num_reqs);
2397 if (first_state->smb1.chained_requests == NULL) {
2399 return NT_STATUS_NO_MEMORY;
2402 wct_offset = HDR_WCT;
2405 for (i=0; i<num_reqs; i++) {
2406 size_t next_padding = 0;
2409 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2411 if (i < num_reqs-1) {
2412 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2413 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2415 TALLOC_FREE(first_state->smb1.chained_requests);
2416 return NT_STATUS_INVALID_PARAMETER_MIX;
2420 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2421 state->smb1.iov_count-2) + 1;
2422 if ((wct_offset % 4) != 0) {
2423 next_padding = 4 - (wct_offset % 4);
2425 wct_offset += next_padding;
2426 vwv = state->smb1.vwv;
2428 if (i < num_reqs-1) {
2429 struct smbXcli_req_state *next_state =
2430 tevent_req_data(reqs[i+1],
2431 struct smbXcli_req_state);
2432 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2434 SSVAL(vwv+1, 0, wct_offset);
2435 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2436 /* properly end the chain */
2437 SCVAL(vwv+0, 0, 0xff);
2438 SCVAL(vwv+0, 1, 0xff);
2444 * The NBT and SMB header
2446 this_iov[0] = state->smb1.iov[0];
2447 this_iov[1] = state->smb1.iov[1];
2451 * This one is a bit subtle. We have to add
2452 * chain_padding bytes between the requests, and we
2453 * have to also include the wct field of the
2454 * subsequent requests. We use the subsequent header
2455 * for the padding, it contains the wct field in its
2458 this_iov[0].iov_len = chain_padding+1;
2459 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2460 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2461 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2466 * copy the words and bytes
2468 memcpy(this_iov, state->smb1.iov+2,
2469 sizeof(struct iovec) * (state->smb1.iov_count-2));
2470 this_iov += state->smb1.iov_count - 2;
2471 chain_padding = next_padding;
2474 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2475 if (nbt_len > first_state->conn->smb1.max_xmit) {
2477 TALLOC_FREE(first_state->smb1.chained_requests);
2478 return NT_STATUS_INVALID_PARAMETER_MIX;
2481 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2482 if (!NT_STATUS_IS_OK(status)) {
2484 TALLOC_FREE(first_state->smb1.chained_requests);
2488 return NT_STATUS_OK;
2491 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2493 return ((tevent_queue_length(conn->outgoing) != 0)
2494 || (talloc_array_length(conn->pending) != 0));
2497 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2499 if (conn->protocol >= PROTOCOL_SMB2_02) {
2500 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2503 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2506 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2508 uint16_t credits = 1;
2510 if (conn->smb2.cur_credits == 0) {
2511 if (max_dyn_len != NULL) {
2517 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2518 credits = conn->smb2.cur_credits;
2521 if (max_dyn_len != NULL) {
2522 *max_dyn_len = credits * 65536;
2528 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2530 return conn->smb2.server.capabilities;
2533 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2535 return conn->smb2.server.security_mode;
2538 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2540 return conn->smb2.server.max_trans_size;
2543 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2545 return conn->smb2.server.max_read_size;
2548 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2550 return conn->smb2.server.max_write_size;
2553 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2554 uint16_t max_credits)
2556 conn->smb2.max_credits = max_credits;
2559 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2561 static bool smb2cli_req_cancel(struct tevent_req *req)
2563 struct smbXcli_req_state *state =
2564 tevent_req_data(req,
2565 struct smbXcli_req_state);
2566 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2567 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2568 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2569 struct smbXcli_tcon *tcon = state->tcon;
2570 struct smbXcli_session *session = state->session;
2571 uint8_t *fixed = state->smb2.pad;
2572 uint16_t fixed_len = 4;
2573 struct tevent_req *subreq;
2574 struct smbXcli_req_state *substate;
2577 SSVAL(fixed, 0, 0x04);
2580 subreq = smb2cli_req_create(state, state->ev,
2588 if (subreq == NULL) {
2591 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2594 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2595 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2597 flags &= SMB2_HDR_FLAG_ASYNC;
2599 if (flags & SMB2_HDR_FLAG_ASYNC) {
2603 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2604 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2605 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2607 status = smb2cli_req_compound_submit(&subreq, 1);
2608 if (!NT_STATUS_IS_OK(status)) {
2609 TALLOC_FREE(subreq);
2613 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2618 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2620 /* we do not care about the result */
2621 TALLOC_FREE(subreq);
2624 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2625 struct tevent_context *ev,
2626 struct smbXcli_conn *conn,
2628 uint32_t additional_flags,
2629 uint32_t clear_flags,
2630 uint32_t timeout_msec,
2631 struct smbXcli_tcon *tcon,
2632 struct smbXcli_session *session,
2633 const uint8_t *fixed,
2637 uint32_t max_dyn_len)
2639 struct tevent_req *req;
2640 struct smbXcli_req_state *state;
2644 bool use_channel_sequence = false;
2645 uint16_t channel_sequence = 0;
2646 bool use_replay_flag = false;
2648 req = tevent_req_create(mem_ctx, &state,
2649 struct smbXcli_req_state);
2656 state->session = session;
2659 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2660 use_channel_sequence = true;
2661 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2662 use_channel_sequence = true;
2665 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_00) {
2666 use_replay_flag = true;
2670 uid = session->smb2->session_id;
2672 if (use_channel_sequence) {
2673 channel_sequence = session->smb2->channel_sequence;
2676 if (use_replay_flag && session->smb2->replay_active) {
2677 additional_flags |= SMB2_HDR_FLAG_REPLAY_OPERATION;
2680 state->smb2.should_sign = session->smb2->should_sign;
2681 state->smb2.should_encrypt = session->smb2->should_encrypt;
2683 if (cmd == SMB2_OP_SESSSETUP &&
2684 session->smb2->signing_key.length != 0) {
2685 state->smb2.should_sign = true;
2688 if (cmd == SMB2_OP_SESSSETUP &&
2689 session->smb2_channel.signing_key.length == 0) {
2690 state->smb2.should_encrypt = false;
2693 if (additional_flags & SMB2_HDR_FLAG_SIGNED) {
2694 if (session->smb2_channel.signing_key.length == 0) {
2695 tevent_req_nterror(req, NT_STATUS_NO_USER_SESSION_KEY);
2699 additional_flags &= ~SMB2_HDR_FLAG_SIGNED;
2700 state->smb2.should_sign = true;
2705 tid = tcon->smb2.tcon_id;
2707 if (tcon->smb2.should_sign) {
2708 state->smb2.should_sign = true;
2710 if (tcon->smb2.should_encrypt) {
2711 state->smb2.should_encrypt = true;
2715 if (state->smb2.should_encrypt) {
2716 state->smb2.should_sign = false;
2719 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2720 if (state->smb2.recv_iov == NULL) {
2725 flags |= additional_flags;
2726 flags &= ~clear_flags;
2728 state->smb2.fixed = fixed;
2729 state->smb2.fixed_len = fixed_len;
2730 state->smb2.dyn = dyn;
2731 state->smb2.dyn_len = dyn_len;
2732 state->smb2.max_dyn_len = max_dyn_len;
2734 if (state->smb2.should_encrypt) {
2735 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2736 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2739 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2740 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2741 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2742 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2743 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2744 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2745 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2746 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2749 case SMB2_OP_CANCEL:
2750 state->one_way = true;
2754 * If this is a dummy request, it will have
2755 * UINT64_MAX as message id.
2756 * If we send on break acknowledgement,
2757 * this gets overwritten later.
2759 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2763 if (timeout_msec > 0) {
2764 struct timeval endtime;
2766 endtime = timeval_current_ofs_msec(timeout_msec);
2767 if (!tevent_req_set_endtime(req, ev, endtime)) {
2775 void smb2cli_req_set_notify_async(struct tevent_req *req)
2777 struct smbXcli_req_state *state =
2778 tevent_req_data(req,
2779 struct smbXcli_req_state);
2781 state->smb2.notify_async = true;
2784 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2785 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2786 TALLOC_CTX *tmp_mem,
2789 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2792 struct smbXcli_req_state *state;
2793 struct tevent_req *subreq;
2795 int i, num_iov, nbt_len;
2797 const DATA_BLOB *encryption_key = NULL;
2798 uint64_t encryption_session_id = 0;
2801 * 1 for the nbt length, optional TRANSFORM
2802 * per request: HDR, fixed, dyn, padding
2803 * -1 because the last one does not need padding
2806 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2808 return NT_STATUS_NO_MEMORY;
2815 * the session of the first request that requires encryption
2816 * specifies the encryption key.
2818 for (i=0; i<num_reqs; i++) {
2819 if (!tevent_req_is_in_progress(reqs[i])) {
2820 return NT_STATUS_INTERNAL_ERROR;
2823 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2825 if (!smbXcli_conn_is_connected(state->conn)) {
2826 return NT_STATUS_CONNECTION_DISCONNECTED;
2829 if ((state->conn->protocol != PROTOCOL_NONE) &&
2830 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2831 return NT_STATUS_REVISION_MISMATCH;
2834 if (state->session == NULL) {
2838 if (!state->smb2.should_encrypt) {
2842 encryption_key = &state->session->smb2->encryption_key;
2843 if (encryption_key->length == 0) {
2844 return NT_STATUS_INVALID_PARAMETER_MIX;
2847 encryption_session_id = state->session->smb2->session_id;
2850 iov[num_iov].iov_base = state->smb2.transform;
2851 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2854 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2855 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2856 state->session->smb2->nonce_low);
2857 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2858 state->session->smb2->nonce_high);
2859 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2860 encryption_session_id);
2862 state->session->smb2->nonce_low += 1;
2863 if (state->session->smb2->nonce_low == 0) {
2864 state->session->smb2->nonce_high += 1;
2865 state->session->smb2->nonce_low += 1;
2868 nbt_len += SMB2_TF_HDR_SIZE;
2872 for (i=0; i<num_reqs; i++) {
2881 const DATA_BLOB *signing_key = NULL;
2883 if (!tevent_req_is_in_progress(reqs[i])) {
2884 return NT_STATUS_INTERNAL_ERROR;
2887 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2889 if (!smbXcli_conn_is_connected(state->conn)) {
2890 return NT_STATUS_CONNECTION_DISCONNECTED;
2893 if ((state->conn->protocol != PROTOCOL_NONE) &&
2894 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2895 return NT_STATUS_REVISION_MISMATCH;
2898 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2899 if (opcode == SMB2_OP_CANCEL) {
2903 avail = UINT64_MAX - state->conn->smb2.mid;
2905 return NT_STATUS_CONNECTION_ABORTED;
2908 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2909 uint32_t max_dyn_len = 1;
2911 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
2912 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
2914 charge = (max_dyn_len - 1)/ 65536 + 1;
2919 charge = MAX(state->smb2.credit_charge, charge);
2921 avail = MIN(avail, state->conn->smb2.cur_credits);
2922 if (avail < charge) {
2923 return NT_STATUS_INTERNAL_ERROR;
2927 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2928 credits = state->conn->smb2.max_credits -
2929 state->conn->smb2.cur_credits;
2931 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2935 mid = state->conn->smb2.mid;
2936 state->conn->smb2.mid += charge;
2937 state->conn->smb2.cur_credits -= charge;
2939 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2940 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2942 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2943 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2946 if (state->session && encryption_key == NULL) {
2948 * We prefer the channel signing key if it is
2951 if (state->smb2.should_sign) {
2952 signing_key = &state->session->smb2_channel.signing_key;
2956 * If it is a channel binding, we already have the main
2957 * signing key and try that one.
2959 if (signing_key && signing_key->length == 0) {
2960 signing_key = &state->session->smb2->signing_key;
2964 * If we do not have any session key yet, we skip the
2965 * signing of SMB2_OP_SESSSETUP requests.
2967 if (signing_key && signing_key->length == 0) {
2973 iov[num_iov].iov_base = state->smb2.hdr;
2974 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2977 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2978 iov[num_iov].iov_len = state->smb2.fixed_len;
2981 if (state->smb2.dyn != NULL) {
2982 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2983 iov[num_iov].iov_len = state->smb2.dyn_len;
2987 reqlen = sizeof(state->smb2.hdr);
2988 reqlen += state->smb2.fixed_len;
2989 reqlen += state->smb2.dyn_len;
2991 if (i < num_reqs-1) {
2992 if ((reqlen % 8) > 0) {
2993 uint8_t pad = 8 - (reqlen % 8);
2994 iov[num_iov].iov_base = state->smb2.pad;
2995 iov[num_iov].iov_len = pad;
2999 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
3002 state->smb2.encryption_session_id = encryption_session_id;
3004 if (signing_key != NULL) {
3007 status = smb2_signing_sign_pdu(*signing_key,
3008 state->session->conn->protocol,
3009 &iov[hdr_iov], num_iov - hdr_iov);
3010 if (!NT_STATUS_IS_OK(status)) {
3017 ret = smbXcli_req_set_pending(reqs[i]);
3019 return NT_STATUS_NO_MEMORY;
3023 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
3024 _smb_setlen_tcp(state->length_hdr, nbt_len);
3025 iov[0].iov_base = state->length_hdr;
3026 iov[0].iov_len = sizeof(state->length_hdr);
3028 if (encryption_key != NULL) {
3030 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
3034 buf = talloc_array(iov, uint8_t, buflen);
3036 return NT_STATUS_NO_MEMORY;
3040 * We copy the buffers before encrypting them,
3041 * this is at least currently needed for the
3042 * to keep state->smb2.hdr.
3044 * Also the callers may expect there buffers
3047 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3048 struct iovec *v = &iov[vi];
3049 const uint8_t *o = (const uint8_t *)v->iov_base;
3051 memcpy(buf, o, v->iov_len);
3052 v->iov_base = (void *)buf;
3056 status = smb2_signing_encrypt_pdu(*encryption_key,
3057 state->conn->protocol,
3058 &iov[tf_iov], num_iov - tf_iov);
3059 if (!NT_STATUS_IS_OK(status)) {
3064 if (state->conn->dispatch_incoming == NULL) {
3065 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3068 subreq = writev_send(state, state->ev, state->conn->outgoing,
3069 state->conn->write_fd, false, iov, num_iov);
3070 if (subreq == NULL) {
3071 return NT_STATUS_NO_MEMORY;
3073 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3074 return NT_STATUS_OK;
3077 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3079 struct smbXcli_req_state *state =
3080 tevent_req_data(req,
3081 struct smbXcli_req_state);
3083 state->smb2.credit_charge = charge;
3086 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3087 struct tevent_context *ev,
3088 struct smbXcli_conn *conn,
3090 uint32_t additional_flags,
3091 uint32_t clear_flags,
3092 uint32_t timeout_msec,
3093 struct smbXcli_tcon *tcon,
3094 struct smbXcli_session *session,
3095 const uint8_t *fixed,
3099 uint32_t max_dyn_len)
3101 struct tevent_req *req;
3104 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3105 additional_flags, clear_flags,
3114 if (!tevent_req_is_in_progress(req)) {
3115 return tevent_req_post(req, ev);
3117 status = smb2cli_req_compound_submit(&req, 1);
3118 if (tevent_req_nterror(req, status)) {
3119 return tevent_req_post(req, ev);
3124 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3126 struct tevent_req *req =
3127 tevent_req_callback_data(subreq,
3129 struct smbXcli_req_state *state =
3130 tevent_req_data(req,
3131 struct smbXcli_req_state);
3135 nwritten = writev_recv(subreq, &err);
3136 TALLOC_FREE(subreq);
3137 if (nwritten == -1) {
3138 /* here, we need to notify all pending requests */
3139 NTSTATUS status = map_nt_error_from_unix_common(err);
3140 smbXcli_conn_disconnect(state->conn, status);
3145 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3148 TALLOC_CTX *mem_ctx,
3149 struct iovec **piov, int *pnum_iov)
3154 uint8_t *first_hdr = buf;
3155 size_t verified_buflen = 0;
3159 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3161 return NT_STATUS_NO_MEMORY;
3164 while (taken < buflen) {
3165 size_t len = buflen - taken;
3166 uint8_t *hdr = first_hdr + taken;
3169 size_t next_command_ofs;
3171 struct iovec *iov_tmp;
3173 if (verified_buflen > taken) {
3174 len = verified_buflen - taken;
3181 DEBUG(10, ("%d bytes left, expected at least %d\n",
3185 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3186 struct smbXcli_session *s;
3188 struct iovec tf_iov[2];
3192 if (len < SMB2_TF_HDR_SIZE) {
3193 DEBUG(10, ("%d bytes left, expected at least %d\n",
3194 (int)len, SMB2_TF_HDR_SIZE));
3198 tf_len = SMB2_TF_HDR_SIZE;
3201 hdr = first_hdr + taken;
3202 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3203 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3205 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3206 DEBUG(10, ("%d bytes left, expected at least %d\n",
3208 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3213 for (; s; s = s->next) {
3214 if (s->smb2->session_id != uid) {
3221 DEBUG(10, ("unknown session_id %llu\n",
3222 (unsigned long long)uid));
3226 tf_iov[0].iov_base = (void *)tf;
3227 tf_iov[0].iov_len = tf_len;
3228 tf_iov[1].iov_base = (void *)hdr;
3229 tf_iov[1].iov_len = enc_len;
3231 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3234 if (!NT_STATUS_IS_OK(status)) {
3239 verified_buflen = taken + enc_len;
3244 * We need the header plus the body length field
3247 if (len < SMB2_HDR_BODY + 2) {
3248 DEBUG(10, ("%d bytes left, expected at least %d\n",
3249 (int)len, SMB2_HDR_BODY));
3252 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3253 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3257 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3258 DEBUG(10, ("Got HDR len %d, expected %d\n",
3259 SVAL(hdr, 4), SMB2_HDR_BODY));
3264 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3265 body_size = SVAL(hdr, SMB2_HDR_BODY);
3267 if (next_command_ofs != 0) {
3268 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3271 if (next_command_ofs > full_size) {
3274 full_size = next_command_ofs;
3276 if (body_size < 2) {
3279 body_size &= 0xfffe;
3281 if (body_size > (full_size - SMB2_HDR_BODY)) {
3285 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3287 if (iov_tmp == NULL) {
3289 return NT_STATUS_NO_MEMORY;
3292 cur = &iov[num_iov];
3295 cur[0].iov_base = tf;
3296 cur[0].iov_len = tf_len;
3297 cur[1].iov_base = hdr;
3298 cur[1].iov_len = SMB2_HDR_BODY;
3299 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3300 cur[2].iov_len = body_size;
3301 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3302 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3308 *pnum_iov = num_iov;
3309 return NT_STATUS_OK;
3313 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3316 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3319 size_t num_pending = talloc_array_length(conn->pending);
3322 for (i=0; i<num_pending; i++) {
3323 struct tevent_req *req = conn->pending[i];
3324 struct smbXcli_req_state *state =
3325 tevent_req_data(req,
3326 struct smbXcli_req_state);
3328 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3335 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3336 TALLOC_CTX *tmp_mem,
3339 struct tevent_req *req;
3340 struct smbXcli_req_state *state = NULL;
3345 struct smbXcli_session *last_session = NULL;
3346 size_t inbuf_len = smb_len_tcp(inbuf);
3348 status = smb2cli_inbuf_parse_compound(conn,
3349 inbuf + NBT_HDR_SIZE,
3353 if (!NT_STATUS_IS_OK(status)) {
3357 for (i=0; i<num_iov; i+=4) {
3358 uint8_t *inbuf_ref = NULL;
3359 struct iovec *cur = &iov[i];
3360 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3361 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3362 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3363 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3364 uint16_t req_opcode;
3366 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3367 uint32_t new_credits;
3368 struct smbXcli_session *session = NULL;
3369 const DATA_BLOB *signing_key = NULL;
3370 bool was_encrypted = false;
3372 new_credits = conn->smb2.cur_credits;
3373 new_credits += credits;
3374 if (new_credits > UINT16_MAX) {
3375 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3377 conn->smb2.cur_credits += credits;
3379 req = smb2cli_conn_find_pending(conn, mid);
3381 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3383 state = tevent_req_data(req, struct smbXcli_req_state);
3385 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3386 if (opcode != req_opcode) {
3387 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3389 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3391 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3392 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3395 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3396 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3397 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3398 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3400 if (state->smb2.got_async) {
3401 /* We only expect one STATUS_PENDING response */
3402 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3404 state->smb2.got_async = true;
3407 * async interim responses are not signed,
3408 * even if the SMB2_HDR_FLAG_SIGNED flag
3411 req_flags |= SMB2_HDR_FLAG_ASYNC;
3412 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3413 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3415 if (state->smb2.notify_async) {
3416 tevent_req_defer_callback(req, state->ev);
3417 tevent_req_notify_callback(req);
3422 session = state->session;
3423 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3424 session = last_session;
3426 last_session = session;
3428 if (state->smb2.should_sign) {
3429 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3430 return NT_STATUS_ACCESS_DENIED;
3434 if (flags & SMB2_HDR_FLAG_SIGNED) {
3435 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3437 if (session == NULL) {
3438 struct smbXcli_session *s;
3440 s = state->conn->sessions;
3441 for (; s; s = s->next) {
3442 if (s->smb2->session_id != uid) {
3451 if (session == NULL) {
3452 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3455 last_session = session;
3456 signing_key = &session->smb2_channel.signing_key;
3459 if (opcode == SMB2_OP_SESSSETUP) {
3461 * We prefer the channel signing key, if it is
3464 * If we do not have a channel signing key yet,
3465 * we try the main signing key, if it is not
3466 * the final response.
3468 if (signing_key && signing_key->length == 0 &&
3469 !NT_STATUS_IS_OK(status)) {
3470 signing_key = &session->smb2->signing_key;
3473 if (signing_key && signing_key->length == 0) {
3475 * If we do not have a session key to
3476 * verify the signature, we defer the
3477 * signing check to the caller.
3479 * The caller gets NT_STATUS_OK, it
3481 * smb2cli_session_set_session_key()
3483 * smb2cli_session_set_channel_key()
3484 * which will check the signature
3485 * with the channel signing key.
3491 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3492 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3493 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3496 * If the response was encrypted in a SMB2_TRANSFORM
3497 * pdu, which belongs to the correct session,
3498 * we do not need to do signing checks
3500 * It could be the session the response belongs to
3501 * or the session that was used to encrypt the
3502 * SMB2_TRANSFORM request.
3504 if ((session && session->smb2->session_id == uid) ||
3505 (state->smb2.encryption_session_id == uid)) {
3507 was_encrypted = true;
3511 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3513 * if the server returns NT_STATUS_USER_SESSION_DELETED
3514 * the response is not signed and we should
3515 * propagate the NT_STATUS_USER_SESSION_DELETED
3516 * status to the caller.
3518 state->smb2.signing_skipped = true;
3522 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3524 * if the server returns
3525 * NT_STATUS_INVALID_PARAMETER
3526 * the response might not be encrypted.
3528 if (state->smb2.should_encrypt && !was_encrypted) {
3529 state->smb2.signing_skipped = true;
3534 if (state->smb2.should_encrypt && !was_encrypted) {
3535 if (!state->smb2.signing_skipped) {
3536 return NT_STATUS_ACCESS_DENIED;
3540 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3541 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3542 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3544 * if the server returns
3545 * NT_STATUS_NETWORK_NAME_DELETED
3546 * NT_STATUS_FILE_CLOSED
3547 * NT_STATUS_INVALID_PARAMETER
3548 * the response might not be signed
3549 * as this happens before the signing checks.
3551 * If server echos the signature (or all zeros)
3552 * we should report the status from the server
3558 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3559 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3562 state->smb2.signing_skipped = true;
3568 static const uint8_t zeros[16];
3570 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3574 state->smb2.signing_skipped = true;
3581 status = smb2_signing_check_pdu(*signing_key,
3582 state->conn->protocol,
3584 if (!NT_STATUS_IS_OK(status)) {
3586 * If the signing check fails, we disconnect
3593 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3594 (session != NULL) && session->disconnect_expired)
3597 * this should be a short term hack
3598 * until the upper layers have implemented
3599 * re-authentication.
3604 smbXcli_req_unset_pending(req);
3607 * There might be more than one response
3608 * we need to defer the notifications
3610 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3615 tevent_req_defer_callback(req, state->ev);
3619 * Note: here we use talloc_reference() in a way
3620 * that does not expose it to the caller.
3622 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3623 if (tevent_req_nomem(inbuf_ref, req)) {
3627 /* copy the related buffers */
3628 state->smb2.recv_iov[0] = cur[1];
3629 state->smb2.recv_iov[1] = cur[2];
3630 state->smb2.recv_iov[2] = cur[3];
3632 tevent_req_done(req);
3636 return NT_STATUS_RETRY;
3639 return NT_STATUS_OK;
3642 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3643 struct iovec **piov,
3644 const struct smb2cli_req_expected_response *expected,
3645 size_t num_expected)
3647 struct smbXcli_req_state *state =
3648 tevent_req_data(req,
3649 struct smbXcli_req_state);
3652 bool found_status = false;
3653 bool found_size = false;
3660 if (tevent_req_is_in_progress(req) && state->smb2.got_async) {
3661 return STATUS_PENDING;
3664 if (tevent_req_is_nterror(req, &status)) {
3665 for (i=0; i < num_expected; i++) {
3666 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3667 found_status = true;
3673 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3679 if (num_expected == 0) {
3680 found_status = true;
3684 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3685 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3687 for (i=0; i < num_expected; i++) {
3688 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3692 found_status = true;
3693 if (expected[i].body_size == 0) {
3698 if (expected[i].body_size == body_size) {
3704 if (!found_status) {
3708 if (state->smb2.signing_skipped) {
3709 if (num_expected > 0) {
3710 return NT_STATUS_ACCESS_DENIED;
3712 if (!NT_STATUS_IS_ERR(status)) {
3713 return NT_STATUS_ACCESS_DENIED;
3718 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3722 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3728 NTSTATUS smb2cli_req_get_sent_iov(struct tevent_req *req,
3729 struct iovec *sent_iov)
3731 struct smbXcli_req_state *state =
3732 tevent_req_data(req,
3733 struct smbXcli_req_state);
3735 if (tevent_req_is_in_progress(req)) {
3736 return STATUS_PENDING;
3739 sent_iov[0].iov_base = state->smb2.hdr;
3740 sent_iov[0].iov_len = sizeof(state->smb2.hdr);
3742 sent_iov[1].iov_base = discard_const(state->smb2.fixed);
3743 sent_iov[1].iov_len = state->smb2.fixed_len;
3745 if (state->smb2.dyn != NULL) {
3746 sent_iov[2].iov_base = discard_const(state->smb2.dyn);
3747 sent_iov[2].iov_len = state->smb2.dyn_len;
3749 sent_iov[2].iov_base = NULL;
3750 sent_iov[2].iov_len = 0;
3753 return NT_STATUS_OK;
3756 static const struct {
3757 enum protocol_types proto;
3758 const char *smb1_name;
3759 } smb1cli_prots[] = {
3760 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3761 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3762 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3763 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3764 {PROTOCOL_LANMAN2, "LM1.2X002"},
3765 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3766 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3767 {PROTOCOL_LANMAN2, "Samba"},
3768 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3769 {PROTOCOL_NT1, "NT LM 0.12"},
3770 {PROTOCOL_SMB2_02, "SMB 2.002"},
3771 {PROTOCOL_SMB2_10, "SMB 2.???"},
3774 static const struct {
3775 enum protocol_types proto;
3776 uint16_t smb2_dialect;
3777 } smb2cli_prots[] = {
3778 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3779 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3780 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3781 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3782 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3783 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
3784 {PROTOCOL_SMB3_10, SMB3_DIALECT_REVISION_310},
3787 struct smbXcli_negprot_state {
3788 struct smbXcli_conn *conn;
3789 struct tevent_context *ev;
3790 uint32_t timeout_msec;
3797 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3798 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3799 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3800 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3801 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3802 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3806 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3807 struct tevent_context *ev,
3808 struct smbXcli_conn *conn,
3809 uint32_t timeout_msec,
3810 enum protocol_types min_protocol,
3811 enum protocol_types max_protocol)
3813 struct tevent_req *req, *subreq;
3814 struct smbXcli_negprot_state *state;
3816 req = tevent_req_create(mem_ctx, &state,
3817 struct smbXcli_negprot_state);
3823 state->timeout_msec = timeout_msec;
3825 if (min_protocol == PROTOCOL_NONE) {
3826 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3827 return tevent_req_post(req, ev);
3830 if (max_protocol == PROTOCOL_NONE) {
3831 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3832 return tevent_req_post(req, ev);
3835 if (min_protocol > max_protocol) {
3836 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3837 return tevent_req_post(req, ev);
3840 conn->min_protocol = min_protocol;
3841 conn->max_protocol = max_protocol;
3842 conn->protocol = PROTOCOL_NONE;
3844 if ((min_protocol < PROTOCOL_SMB2_02) &&
3845 (max_protocol < PROTOCOL_SMB2_02)) {
3849 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3851 subreq = smbXcli_negprot_smb1_subreq(state);
3852 if (tevent_req_nomem(subreq, req)) {
3853 return tevent_req_post(req, ev);
3855 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3859 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3860 (max_protocol >= PROTOCOL_SMB2_02)) {
3864 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3866 subreq = smbXcli_negprot_smb2_subreq(state);
3867 if (tevent_req_nomem(subreq, req)) {
3868 return tevent_req_post(req, ev);
3870 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3875 * We send an SMB1 negprot with the SMB2 dialects
3876 * and expect a SMB1 or a SMB2 response.
3878 * smbXcli_negprot_dispatch_incoming() will fix the
3879 * callback to match protocol of the response.
3881 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3883 subreq = smbXcli_negprot_smb1_subreq(state);
3884 if (tevent_req_nomem(subreq, req)) {
3885 return tevent_req_post(req, ev);
3887 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3891 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3893 struct tevent_req *req =
3894 tevent_req_callback_data(subreq,
3899 * we just want the low level error
3901 status = tevent_req_simple_recv_ntstatus(subreq);
3902 TALLOC_FREE(subreq);
3903 if (tevent_req_nterror(req, status)) {
3907 /* this should never happen */
3908 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3911 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3914 DATA_BLOB bytes = data_blob_null;
3918 /* setup the protocol strings */
3919 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3923 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
3927 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
3931 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3937 * We now it is already ascii and
3938 * we want NULL termination.
3940 ok = data_blob_append(state, &bytes,
3941 smb1cli_prots[i].smb1_name,
3942 strlen(smb1cli_prots[i].smb1_name)+1);
3948 smb1cli_req_flags(state->conn->max_protocol,
3949 state->conn->smb1.client.capabilities,
3954 return smb1cli_req_send(state, state->ev, state->conn,
3958 state->timeout_msec,
3959 0xFFFE, 0, NULL, /* pid, tid, session */
3960 0, NULL, /* wct, vwv */
3961 bytes.length, bytes.data);
3964 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3966 struct tevent_req *req =
3967 tevent_req_callback_data(subreq,
3969 struct smbXcli_negprot_state *state =
3970 tevent_req_data(req,
3971 struct smbXcli_negprot_state);
3972 struct smbXcli_conn *conn = state->conn;
3973 struct iovec *recv_iov = NULL;
3982 size_t num_prots = 0;
3984 uint32_t client_capabilities = conn->smb1.client.capabilities;
3985 uint32_t both_capabilities;
3986 uint32_t server_capabilities = 0;
3987 uint32_t capabilities;
3988 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3989 uint32_t server_max_xmit = 0;
3991 uint32_t server_max_mux = 0;
3992 uint16_t server_security_mode = 0;
3993 uint32_t server_session_key = 0;
3994 bool server_readbraw = false;
3995 bool server_writebraw = false;
3996 bool server_lockread = false;
3997 bool server_writeunlock = false;
3998 struct GUID server_guid = GUID_zero();
3999 DATA_BLOB server_gss_blob = data_blob_null;
4000 uint8_t server_challenge[8];
4001 char *server_workgroup = NULL;
4002 char *server_name = NULL;
4003 int server_time_zone = 0;
4004 NTTIME server_system_time = 0;
4005 static const struct smb1cli_req_expected_response expected[] = {
4007 .status = NT_STATUS_OK,
4008 .wct = 0x11, /* NT1 */
4011 .status = NT_STATUS_OK,
4012 .wct = 0x0D, /* LM */
4015 .status = NT_STATUS_OK,
4016 .wct = 0x01, /* CORE */
4020 ZERO_STRUCT(server_challenge);
4022 status = smb1cli_req_recv(subreq, state,
4027 NULL, /* pvwv_offset */
4030 NULL, /* pbytes_offset */
4032 expected, ARRAY_SIZE(expected));
4033 TALLOC_FREE(subreq);
4034 if (tevent_req_nterror(req, status)) {
4038 flags = CVAL(inhdr, HDR_FLG);
4040 protnum = SVAL(vwv, 0);
4042 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4043 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4047 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4051 if (protnum != num_prots) {
4056 conn->protocol = smb1cli_prots[i].proto;
4060 if (conn->protocol == PROTOCOL_NONE) {
4061 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4065 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
4066 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4067 "and the selected protocol level doesn't support it.\n"));
4068 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4072 if (flags & FLAG_SUPPORT_LOCKREAD) {
4073 server_lockread = true;
4074 server_writeunlock = true;
4077 if (conn->protocol >= PROTOCOL_NT1) {
4078 const char *client_signing = NULL;
4079 bool server_mandatory = false;
4080 bool server_allowed = false;
4081 const char *server_signing = NULL;
4086 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4091 server_security_mode = CVAL(vwv + 1, 0);
4092 server_max_mux = SVAL(vwv + 1, 1);
4093 server_max_xmit = IVAL(vwv + 3, 1);
4094 server_session_key = IVAL(vwv + 7, 1);
4095 server_time_zone = SVALS(vwv + 15, 1);
4096 server_time_zone *= 60;
4097 /* this time arrives in real GMT */
4098 server_system_time = BVAL(vwv + 11, 1);
4099 server_capabilities = IVAL(vwv + 9, 1);
4101 key_len = CVAL(vwv + 16, 1);
4103 if (server_capabilities & CAP_RAW_MODE) {
4104 server_readbraw = true;
4105 server_writebraw = true;
4107 if (server_capabilities & CAP_LOCK_AND_READ) {
4108 server_lockread = true;
4111 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4112 DATA_BLOB blob1, blob2;
4114 if (num_bytes < 16) {
4115 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4119 blob1 = data_blob_const(bytes, 16);
4120 status = GUID_from_data_blob(&blob1, &server_guid);
4121 if (tevent_req_nterror(req, status)) {
4125 blob1 = data_blob_const(bytes+16, num_bytes-16);
4126 blob2 = data_blob_dup_talloc(state, blob1);
4127 if (blob1.length > 0 &&
4128 tevent_req_nomem(blob2.data, req)) {
4131 server_gss_blob = blob2;
4133 DATA_BLOB blob1, blob2;
4135 if (num_bytes < key_len) {
4136 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4140 if (key_len != 0 && key_len != 8) {
4141 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4146 memcpy(server_challenge, bytes, 8);
4149 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4150 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4151 if (blob1.length > 0) {
4154 len = utf16_len_n(blob1.data,
4158 ok = convert_string_talloc(state,
4166 status = map_nt_error_from_unix_common(errno);
4167 tevent_req_nterror(req, status);
4172 blob2.data += blob1.length;
4173 blob2.length -= blob1.length;
4174 if (blob2.length > 0) {
4177 len = utf16_len_n(blob1.data,
4181 ok = convert_string_talloc(state,
4189 status = map_nt_error_from_unix_common(errno);
4190 tevent_req_nterror(req, status);
4196 client_signing = "disabled";
4197 if (conn->allow_signing) {
4198 client_signing = "allowed";
4200 if (conn->mandatory_signing) {
4201 client_signing = "required";
4204 server_signing = "not supported";
4205 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4206 server_signing = "supported";
4207 server_allowed = true;
4208 } else if (conn->mandatory_signing) {
4210 * We have mandatory signing as client
4211 * lets assume the server will look at our
4212 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4213 * flag in the session setup
4215 server_signing = "not announced";
4216 server_allowed = true;
4218 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4219 server_signing = "required";
4220 server_mandatory = true;
4223 ok = smb_signing_set_negotiated(conn->smb1.signing,
4227 DEBUG(1,("cli_negprot: SMB signing is required, "
4228 "but client[%s] and server[%s] mismatch\n",
4229 client_signing, server_signing));
4230 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4234 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4240 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4244 server_security_mode = SVAL(vwv + 1, 0);
4245 server_max_xmit = SVAL(vwv + 2, 0);
4246 server_max_mux = SVAL(vwv + 3, 0);
4247 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4248 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4249 server_session_key = IVAL(vwv + 6, 0);
4250 server_time_zone = SVALS(vwv + 10, 0);
4251 server_time_zone *= 60;
4252 /* this time is converted to GMT by make_unix_date */
4253 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4254 unix_to_nt_time(&server_system_time, t);
4255 key_len = SVAL(vwv + 11, 0);
4257 if (num_bytes < key_len) {
4258 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4262 if (key_len != 0 && key_len != 8) {
4263 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4268 memcpy(server_challenge, bytes, 8);
4271 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4272 if (blob1.length > 0) {
4276 len = utf16_len_n(blob1.data,
4280 ok = convert_string_talloc(state,
4288 status = map_nt_error_from_unix_common(errno);
4289 tevent_req_nterror(req, status);
4295 /* the old core protocol */
4296 server_time_zone = get_time_zone(time(NULL));
4297 server_max_xmit = 1024;
4301 if (server_max_xmit < 1024) {
4302 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4306 if (server_max_mux < 1) {
4307 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4312 * Now calculate the negotiated capabilities
4313 * based on the mask for:
4314 * - client only flags
4315 * - flags used in both directions
4316 * - server only flags
4318 both_capabilities = client_capabilities & server_capabilities;
4319 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4320 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4321 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4323 max_xmit = MIN(client_max_xmit, server_max_xmit);
4325 conn->smb1.server.capabilities = server_capabilities;
4326 conn->smb1.capabilities = capabilities;
4328 conn->smb1.server.max_xmit = server_max_xmit;
4329 conn->smb1.max_xmit = max_xmit;
4331 conn->smb1.server.max_mux = server_max_mux;
4333 conn->smb1.server.security_mode = server_security_mode;
4335 conn->smb1.server.readbraw = server_readbraw;
4336 conn->smb1.server.writebraw = server_writebraw;
4337 conn->smb1.server.lockread = server_lockread;
4338 conn->smb1.server.writeunlock = server_writeunlock;
4340 conn->smb1.server.session_key = server_session_key;
4342 talloc_steal(conn, server_gss_blob.data);
4343 conn->smb1.server.gss_blob = server_gss_blob;
4344 conn->smb1.server.guid = server_guid;
4345 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4346 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4347 conn->smb1.server.name = talloc_move(conn, &server_name);
4349 conn->smb1.server.time_zone = server_time_zone;
4350 conn->smb1.server.system_time = server_system_time;
4352 tevent_req_done(req);
4355 static size_t smbXcli_padding_helper(uint32_t offset, size_t n)
4357 if ((offset & (n-1)) == 0) return 0;
4358 return n - (offset & (n-1));
4361 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4365 uint16_t dialect_count = 0;
4366 DATA_BLOB dyn = data_blob_null;
4368 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4372 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4376 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4380 SSVAL(val, 0, smb2cli_prots[i].smb2_dialect);
4382 ok = data_blob_append(state, &dyn, val, sizeof(val));
4390 buf = state->smb2.fixed;
4392 SSVAL(buf, 2, dialect_count);
4393 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4394 SSVAL(buf, 6, 0); /* Reserved */
4395 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
4396 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4398 SIVAL(buf, 8, 0); /* Capabilities */
4400 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4404 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4406 if (!NT_STATUS_IS_OK(status)) {
4409 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4411 memset(buf+12, 0, 16); /* ClientGuid */
4414 if (state->conn->max_protocol >= PROTOCOL_SMB3_10) {
4416 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4420 const uint8_t zeros[8] = {0, };
4424 SSVAL(p, 0, 1); /* HashAlgorithmCount */
4425 SSVAL(p, 2, 32); /* SaltLength */
4426 SSVAL(p, 4, SMB2_PREAUTH_INTEGRITY_SHA512);
4427 generate_random_buffer(p + 6, 32);
4429 b = data_blob_const(p, 38);
4430 status = smb2_negotiate_context_add(state, &c,
4431 SMB2_PREAUTH_INTEGRITY_CAPABILITIES, b);
4432 if (!NT_STATUS_IS_OK(status)) {
4436 SSVAL(p, 0, 1); /* ChiperCount */
4437 SSVAL(p, 2, SMB2_ENCRYPTION_AES128_CCM);
4439 b = data_blob_const(p, 4);
4440 status = smb2_negotiate_context_add(state, &c,
4441 SMB2_ENCRYPTION_CAPABILITIES, b);
4442 if (!NT_STATUS_IS_OK(status)) {
4446 status = smb2_negotiate_context_push(state, &b, c);
4447 if (!NT_STATUS_IS_OK(status)) {
4451 offset = SMB2_HDR_BODY + sizeof(state->smb2.fixed) + dyn.length;
4452 pad = smbXcli_padding_helper(offset, 8);
4454 ok = data_blob_append(state, &dyn, zeros, pad);
4460 ok = data_blob_append(state, &dyn, b.data, b.length);
4465 SIVAL(buf, 28, offset); /* NegotiateContextOffset */
4466 SSVAL(buf, 32, c.num_contexts); /* NegotiateContextCount */
4467 SSVAL(buf, 34, 0); /* Reserved */
4469 SBVAL(buf, 28, 0); /* Reserved/ClientStartTime */
4472 return smb2cli_req_send(state, state->ev,
4473 state->conn, SMB2_OP_NEGPROT,
4475 state->timeout_msec,
4476 NULL, NULL, /* tcon, session */
4477 state->smb2.fixed, sizeof(state->smb2.fixed),
4478 dyn.data, dyn.length,
4479 UINT16_MAX); /* max_dyn_len */
4482 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4484 struct tevent_req *req =
4485 tevent_req_callback_data(subreq,
4487 struct smbXcli_negprot_state *state =
4488 tevent_req_data(req,
4489 struct smbXcli_negprot_state);
4490 struct smbXcli_conn *conn = state->conn;
4491 size_t security_offset, security_length;
4497 uint16_t dialect_revision;
4498 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4499 uint32_t negotiate_context_offset = 0;
4500 uint16_t negotiate_context_count = 0;
4501 DATA_BLOB negotiate_context_blob = data_blob_null;
4505 struct smb2_negotiate_context *preauth = NULL;
4506 uint16_t hash_count;
4507 uint16_t salt_length;
4508 uint16_t hash_selected;
4509 struct hc_sha512state sctx;
4510 struct smb2_negotiate_context *cipher = NULL;
4511 struct iovec sent_iov[3];
4512 static const struct smb2cli_req_expected_response expected[] = {
4514 .status = NT_STATUS_OK,
4519 status = smb2cli_req_recv(subreq, state, &iov,
4520 expected, ARRAY_SIZE(expected));
4521 if (tevent_req_nterror(req, status)) {
4525 body = (uint8_t *)iov[1].iov_base;
4527 dialect_revision = SVAL(body, 4);
4529 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4530 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4534 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4538 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4542 conn->protocol = smb2cli_prots[i].proto;
4546 if (conn->protocol == PROTOCOL_NONE) {
4547 TALLOC_FREE(subreq);
4549 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
4550 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4554 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4555 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4559 /* make sure we do not loop forever */
4560 state->conn->min_protocol = PROTOCOL_SMB2_02;
4563 * send a SMB2 negprot, in order to negotiate
4566 subreq = smbXcli_negprot_smb2_subreq(state);
4567 if (tevent_req_nomem(subreq, req)) {
4570 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4574 conn->smb2.server.security_mode = SVAL(body, 2);
4575 if (conn->protocol >= PROTOCOL_SMB3_10) {
4576 negotiate_context_count = SVAL(body, 6);
4579 blob = data_blob_const(body + 8, 16);
4580 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4581 if (tevent_req_nterror(req, status)) {
4585 conn->smb2.server.capabilities = IVAL(body, 24);
4586 conn->smb2.server.max_trans_size= IVAL(body, 28);
4587 conn->smb2.server.max_read_size = IVAL(body, 32);
4588 conn->smb2.server.max_write_size= IVAL(body, 36);
4589 conn->smb2.server.system_time = BVAL(body, 40);
4590 conn->smb2.server.start_time = BVAL(body, 48);
4592 security_offset = SVAL(body, 56);
4593 security_length = SVAL(body, 58);
4595 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4596 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4600 if (security_length > iov[2].iov_len) {
4601 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4605 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4608 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4612 if (conn->protocol < PROTOCOL_SMB3_10) {
4613 TALLOC_FREE(subreq);
4615 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4616 conn->smb2.server.cipher = SMB2_ENCRYPTION_AES128_CCM;
4618 tevent_req_done(req);
4622 negotiate_context_offset = IVAL(body, 60);
4623 if (negotiate_context_offset < security_offset) {
4624 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4628 ctx_ofs = negotiate_context_offset - security_offset;
4629 if (ctx_ofs > iov[2].iov_len) {
4630 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4633 avail = iov[2].iov_len - security_length;
4634 needed = iov[2].iov_len - ctx_ofs;
4635 if (needed > avail) {
4636 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4640 negotiate_context_blob.data = (uint8_t *)iov[2].iov_base;
4641 negotiate_context_blob.length = iov[2].iov_len;
4643 negotiate_context_blob.data += ctx_ofs;
4644 negotiate_context_blob.length -= ctx_ofs;
4646 status = smb2_negotiate_context_parse(state, negotiate_context_blob, &c);
4647 if (tevent_req_nterror(req, status)) {
4651 if (negotiate_context_count != c.num_contexts) {
4652 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4656 preauth = smb2_negotiate_context_find(&c,
4657 SMB2_PREAUTH_INTEGRITY_CAPABILITIES);
4658 if (preauth == NULL) {
4659 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4663 if (preauth->data.length < 6) {
4664 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4668 hash_count = SVAL(preauth->data.data, 0);
4669 salt_length = SVAL(preauth->data.data, 2);
4670 hash_selected = SVAL(preauth->data.data, 4);
4672 if (hash_count != 1) {
4673 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4677 if (preauth->data.length != (6 + salt_length)) {
4678 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4682 if (hash_selected != SMB2_PREAUTH_INTEGRITY_SHA512) {
4683 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4687 cipher = smb2_negotiate_context_find(&c, SMB2_ENCRYPTION_CAPABILITIES);
4688 if (cipher != NULL) {
4689 uint16_t cipher_count;
4691 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4692 tevent_req_nterror(req,
4693 NT_STATUS_INVALID_NETWORK_RESPONSE);
4697 if (cipher->data.length < 2) {
4698 tevent_req_nterror(req,
4699 NT_STATUS_INVALID_NETWORK_RESPONSE);
4703 cipher_count = SVAL(cipher->data.data, 0);
4705 if (cipher_count > 1) {
4706 tevent_req_nterror(req,
4707 NT_STATUS_INVALID_NETWORK_RESPONSE);
4711 if (cipher->data.length != (2 + 2 * cipher_count)) {
4712 tevent_req_nterror(req,
4713 NT_STATUS_INVALID_NETWORK_RESPONSE);
4717 if (cipher_count == 1) {
4718 uint16_t cipher_selected;
4720 cipher_selected = SVAL(cipher->data.data, 2);
4722 if (cipher_selected == SMB2_ENCRYPTION_AES128_CCM) {
4723 conn->smb2.server.cipher = cipher_selected;
4724 conn->smb2.server.capabilities |= SMB2_CAP_ENCRYPTION;
4728 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4729 tevent_req_nterror(req,
4730 NT_STATUS_INVALID_NETWORK_RESPONSE);
4735 if (conn->smb2.server.cipher == 0) {
4737 * We didn't manage to negotiate a common encryption
4740 conn->smb2.server.capabilities &= ~SMB2_CAP_ENCRYPTION;
4743 /* First we hash the request */
4744 smb2cli_req_get_sent_iov(subreq, sent_iov);
4746 SHA512_Update(&sctx, conn->smb2.preauth_sha512,
4747 sizeof(conn->smb2.preauth_sha512));
4748 for (i = 0; i < 3; i++) {
4749 SHA512_Update(&sctx, sent_iov[i].iov_base, sent_iov[i].iov_len);
4751 SHA512_Final(conn->smb2.preauth_sha512, &sctx);
4752 TALLOC_FREE(subreq);
4754 /* And now we hash the response */
4756 SHA512_Update(&sctx, conn->smb2.preauth_sha512,
4757 sizeof(conn->smb2.preauth_sha512));
4758 for (i = 0; i < 3; i++) {
4759 SHA512_Update(&sctx, iov[i].iov_base, iov[i].iov_len);
4761 SHA512_Final(conn->smb2.preauth_sha512, &sctx);
4763 tevent_req_done(req);
4766 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4767 TALLOC_CTX *tmp_mem,
4770 size_t num_pending = talloc_array_length(conn->pending);
4771 struct tevent_req *subreq;
4772 struct smbXcli_req_state *substate;
4773 struct tevent_req *req;
4774 uint32_t protocol_magic;
4775 size_t inbuf_len = smb_len_nbt(inbuf);
4777 if (num_pending != 1) {
4778 return NT_STATUS_INTERNAL_ERROR;
4781 if (inbuf_len < 4) {
4782 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4785 subreq = conn->pending[0];
4786 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4787 req = tevent_req_callback_data(subreq, struct tevent_req);
4789 protocol_magic = IVAL(inbuf, 4);
4791 switch (protocol_magic) {
4793 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4794 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4795 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4798 if (substate->smb2.recv_iov == NULL) {
4800 * For the SMB1 negprot we have move it.
4802 substate->smb2.recv_iov = substate->smb1.recv_iov;
4803 substate->smb1.recv_iov = NULL;
4807 * we got an SMB2 answer, which consumed sequence number 0
4808 * so we need to use 1 as the next one.
4810 * we also need to set the current credits to 0
4811 * as we consumed the initial one. The SMB2 answer
4812 * hopefully grant us a new credit.
4815 conn->smb2.cur_credits = 0;
4816 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4817 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4818 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4821 DEBUG(10, ("Got non-SMB PDU\n"));
4822 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4825 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4827 return tevent_req_simple_recv_ntstatus(req);
4830 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4831 uint32_t timeout_msec,
4832 enum protocol_types min_protocol,
4833 enum protocol_types max_protocol)
4835 TALLOC_CTX *frame = talloc_stackframe();
4836 struct tevent_context *ev;
4837 struct tevent_req *req;
4838 NTSTATUS status = NT_STATUS_NO_MEMORY;
4841 if (smbXcli_conn_has_async_calls(conn)) {
4843 * Can't use sync call while an async call is in flight
4845 status = NT_STATUS_INVALID_PARAMETER_MIX;
4848 ev = samba_tevent_context_init(frame);
4852 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4853 min_protocol, max_protocol);
4857 ok = tevent_req_poll(req, ev);
4859 status = map_nt_error_from_unix_common(errno);
4862 status = smbXcli_negprot_recv(req);
4868 struct smb2cli_validate_negotiate_info_state {
4869 struct smbXcli_conn *conn;
4870 DATA_BLOB in_input_buffer;
4871 DATA_BLOB in_output_buffer;
4872 DATA_BLOB out_input_buffer;
4873 DATA_BLOB out_output_buffer;
4877 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq);
4879 struct tevent_req *smb2cli_validate_negotiate_info_send(TALLOC_CTX *mem_ctx,
4880 struct tevent_context *ev,
4881 struct smbXcli_conn *conn,
4882 uint32_t timeout_msec,
4883 struct smbXcli_session *session,
4884 struct smbXcli_tcon *tcon)
4886 struct tevent_req *req;
4887 struct smb2cli_validate_negotiate_info_state *state;
4889 uint16_t dialect_count = 0;
4890 struct tevent_req *subreq;
4891 bool _save_should_sign;
4894 req = tevent_req_create(mem_ctx, &state,
4895 struct smb2cli_validate_negotiate_info_state);
4901 state->in_input_buffer = data_blob_talloc_zero(state,
4902 4 + 16 + 1 + 1 + 2);
4903 if (tevent_req_nomem(state->in_input_buffer.data, req)) {
4904 return tevent_req_post(req, ev);
4906 buf = state->in_input_buffer.data;
4908 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
4909 SIVAL(buf, 0, conn->smb2.client.capabilities);
4911 SIVAL(buf, 0, 0); /* Capabilities */
4913 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4917 status = GUID_to_ndr_blob(&conn->smb2.client.guid,
4919 if (!NT_STATUS_IS_OK(status)) {
4922 memcpy(buf+4, blob.data, 16); /* ClientGuid */
4924 memset(buf+4, 0, 16); /* ClientGuid */
4926 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
4927 SCVAL(buf, 20, conn->smb2.client.security_mode);
4931 SCVAL(buf, 21, 0); /* reserved */
4933 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4937 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4941 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4945 if (smb2cli_prots[i].proto == state->conn->protocol) {
4946 state->dialect = smb2cli_prots[i].smb2_dialect;
4949 ofs = state->in_input_buffer.length;
4950 ok = data_blob_realloc(state, &state->in_input_buffer,
4953 tevent_req_oom(req);
4954 return tevent_req_post(req, ev);
4957 buf = state->in_input_buffer.data;
4958 SSVAL(buf, ofs, smb2cli_prots[i].smb2_dialect);
4962 buf = state->in_input_buffer.data;
4963 SSVAL(buf, 22, dialect_count);
4965 _save_should_sign = smb2cli_tcon_is_signing_on(tcon);
4966 smb2cli_tcon_should_sign(tcon, true);
4967 subreq = smb2cli_ioctl_send(state, ev, conn,
4968 timeout_msec, session, tcon,
4969 UINT64_MAX, /* in_fid_persistent */
4970 UINT64_MAX, /* in_fid_volatile */
4971 FSCTL_VALIDATE_NEGOTIATE_INFO,
4972 0, /* in_max_input_length */
4973 &state->in_input_buffer,
4974 24, /* in_max_output_length */
4975 &state->in_output_buffer,
4976 SMB2_IOCTL_FLAG_IS_FSCTL);
4977 smb2cli_tcon_should_sign(tcon, _save_should_sign);
4978 if (tevent_req_nomem(subreq, req)) {
4979 return tevent_req_post(req, ev);
4981 tevent_req_set_callback(subreq,
4982 smb2cli_validate_negotiate_info_done,
4988 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq)
4990 struct tevent_req *req =
4991 tevent_req_callback_data(subreq,
4993 struct smb2cli_validate_negotiate_info_state *state =
4994 tevent_req_data(req,
4995 struct smb2cli_validate_negotiate_info_state);
4998 uint32_t capabilities;
4999 DATA_BLOB guid_blob;
5000 struct GUID server_guid;
5001 uint16_t security_mode;
5004 status = smb2cli_ioctl_recv(subreq, state,
5005 &state->out_input_buffer,
5006 &state->out_output_buffer);
5007 TALLOC_FREE(subreq);
5008 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
5010 * The response was signed, but not supported
5012 * Older Windows and Samba releases return
5013 * NT_STATUS_FILE_CLOSED.
5015 tevent_req_done(req);
5018 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_DEVICE_REQUEST)) {
5020 * The response was signed, but not supported
5022 * This is returned by the NTVFS based Samba 4.x file server
5025 tevent_req_done(req);
5028 if (NT_STATUS_EQUAL(status, NT_STATUS_FS_DRIVER_REQUIRED)) {
5030 * The response was signed, but not supported
5032 * This is returned by the NTVFS based Samba 4.x file server
5035 tevent_req_done(req);
5038 if (tevent_req_nterror(req, status)) {
5042 if (state->out_output_buffer.length != 24) {
5043 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5047 buf = state->out_output_buffer.data;
5049 capabilities = IVAL(buf, 0);
5050 guid_blob = data_blob_const(buf + 4, 16);
5051 status = GUID_from_data_blob(&guid_blob, &server_guid);
5052 if (tevent_req_nterror(req, status)) {
5055 security_mode = CVAL(buf, 20);
5056 dialect = SVAL(buf, 22);
5058 if (capabilities != state->conn->smb2.server.capabilities) {
5059 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5063 if (!GUID_equal(&server_guid, &state->conn->smb2.server.guid)) {
5064 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5068 if (security_mode != state->conn->smb2.server.security_mode) {
5069 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5073 if (dialect != state->dialect) {
5074 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5078 tevent_req_done(req);
5081 NTSTATUS smb2cli_validate_negotiate_info_recv(struct tevent_req *req)
5083 return tevent_req_simple_recv_ntstatus(req);
5086 static int smbXcli_session_destructor(struct smbXcli_session *session)
5088 if (session->conn == NULL) {
5092 DLIST_REMOVE(session->conn->sessions, session);
5096 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
5097 struct smbXcli_conn *conn)
5099 struct smbXcli_session *session;
5101 session = talloc_zero(mem_ctx, struct smbXcli_session);
5102 if (session == NULL) {
5105 session->smb2 = talloc_zero(session, struct smb2cli_session);
5106 if (session->smb2 == NULL) {
5107 talloc_free(session);
5110 talloc_set_destructor(session, smbXcli_session_destructor);
5112 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
5113 session->conn = conn;
5118 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
5119 struct smbXcli_session *src)
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);
5133 session->conn = src->conn;
5134 *session->smb2 = *src->smb2;
5135 session->smb2_channel = src->smb2_channel;
5136 session->disconnect_expired = src->disconnect_expired;
5138 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
5139 talloc_set_destructor(session, smbXcli_session_destructor);
5144 bool smbXcli_session_is_authenticated(struct smbXcli_session *session)
5146 const DATA_BLOB *application_key;
5148 if (session->conn == NULL) {
5153 * If we have an application key we had a session key negotiated
5156 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5157 application_key = &session->smb2->application_key;
5159 application_key = &session->smb1.application_key;
5162 if (application_key->length == 0) {
5169 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
5170 TALLOC_CTX *mem_ctx,
5173 const DATA_BLOB *application_key;
5175 *key = data_blob_null;
5177 if (session->conn == NULL) {
5178 return NT_STATUS_NO_USER_SESSION_KEY;
5181 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5182 application_key = &session->smb2->application_key;
5184 application_key = &session->smb1.application_key;
5187 if (application_key->length == 0) {
5188 return NT_STATUS_NO_USER_SESSION_KEY;
5191 *key = data_blob_dup_talloc(mem_ctx, *application_key);
5192 if (key->data == NULL) {
5193 return NT_STATUS_NO_MEMORY;
5196 return NT_STATUS_OK;
5199 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
5201 session->disconnect_expired = true;
5204 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
5206 return session->smb1.session_id;
5209 void smb1cli_session_set_id(struct smbXcli_session *session,
5210 uint16_t session_id)
5212 session->smb1.session_id = session_id;
5215 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
5216 const DATA_BLOB _session_key)
5218 struct smbXcli_conn *conn = session->conn;
5219 uint8_t session_key[16];
5222 return NT_STATUS_INVALID_PARAMETER_MIX;
5225 if (session->smb1.application_key.length != 0) {
5227 * TODO: do not allow this...
5229 * return NT_STATUS_INVALID_PARAMETER_MIX;
5231 data_blob_clear_free(&session->smb1.application_key);
5232 session->smb1.protected_key = false;
5235 if (_session_key.length == 0) {
5236 return NT_STATUS_OK;
5239 ZERO_STRUCT(session_key);
5240 memcpy(session_key, _session_key.data,
5241 MIN(_session_key.length, sizeof(session_key)));
5243 session->smb1.application_key = data_blob_talloc(session,
5245 sizeof(session_key));
5246 ZERO_STRUCT(session_key);
5247 if (session->smb1.application_key.data == NULL) {
5248 return NT_STATUS_NO_MEMORY;
5251 session->smb1.protected_key = false;
5253 return NT_STATUS_OK;
5256 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
5258 if (session->smb1.protected_key) {
5259 /* already protected */
5260 return NT_STATUS_OK;
5263 if (session->smb1.application_key.length != 16) {
5264 return NT_STATUS_INVALID_PARAMETER_MIX;
5267 smb_key_derivation(session->smb1.application_key.data,
5268 session->smb1.application_key.length,
5269 session->smb1.application_key.data);
5271 session->smb1.protected_key = true;
5273 return NT_STATUS_OK;
5276 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
5278 struct smbXcli_conn *conn = session->conn;
5279 uint8_t security_mode = 0;
5282 return security_mode;
5285 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
5286 if (conn->mandatory_signing) {
5287 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
5290 return security_mode;
5293 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
5295 return session->smb2->session_id;
5298 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
5300 return session->smb2->session_flags;
5303 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
5304 uint64_t session_id,
5305 uint16_t session_flags)
5307 session->smb2->session_id = session_id;
5308 session->smb2->session_flags = session_flags;
5311 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
5313 session->smb2->channel_sequence += 1;
5316 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session *session,
5317 uint16_t channel_sequence)
5321 prev_cs = session->smb2->channel_sequence;
5322 session->smb2->channel_sequence = channel_sequence;
5327 void smb2cli_session_start_replay(struct smbXcli_session *session)
5329 session->smb2->replay_active = true;
5332 void smb2cli_session_stop_replay(struct smbXcli_session *session)
5334 session->smb2->replay_active = false;
5337 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
5338 const DATA_BLOB _session_key,
5339 const struct iovec *recv_iov)
5341 struct smbXcli_conn *conn = session->conn;
5342 uint16_t no_sign_flags;
5343 uint8_t session_key[16];
5344 bool check_signature = true;
5349 return NT_STATUS_INVALID_PARAMETER_MIX;
5352 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
5353 return NT_STATUS_INVALID_PARAMETER_MIX;
5356 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
5358 if (session->smb2->session_flags & no_sign_flags) {
5359 session->smb2->should_sign = false;
5360 return NT_STATUS_OK;
5363 if (session->smb2->signing_key.length != 0) {
5364 return NT_STATUS_INVALID_PARAMETER_MIX;
5367 ZERO_STRUCT(session_key);
5368 memcpy(session_key, _session_key.data,
5369 MIN(_session_key.length, sizeof(session_key)));
5371 session->smb2->signing_key = data_blob_talloc(session,
5373 sizeof(session_key));
5374 if (session->smb2->signing_key.data == NULL) {
5375 ZERO_STRUCT(session_key);
5376 return NT_STATUS_NO_MEMORY;
5379 if (conn->protocol >= PROTOCOL_SMB2_24) {
5380 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
5381 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
5383 smb2_key_derivation(session_key, sizeof(session_key),
5384 label.data, label.length,
5385 context.data, context.length,
5386 session->smb2->signing_key.data);
5389 session->smb2->encryption_key = data_blob_dup_talloc(session,
5390 session->smb2->signing_key);
5391 if (session->smb2->encryption_key.data == NULL) {
5392 ZERO_STRUCT(session_key);
5393 return NT_STATUS_NO_MEMORY;
5396 if (conn->protocol >= PROTOCOL_SMB2_24) {
5397 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
5398 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
5400 smb2_key_derivation(session_key, sizeof(session_key),
5401 label.data, label.length,
5402 context.data, context.length,
5403 session->smb2->encryption_key.data);
5406 session->smb2->decryption_key = data_blob_dup_talloc(session,
5407 session->smb2->signing_key);
5408 if (session->smb2->decryption_key.data == NULL) {
5409 ZERO_STRUCT(session_key);
5410 return NT_STATUS_NO_MEMORY;
5413 if (conn->protocol >= PROTOCOL_SMB2_24) {
5414 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
5415 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
5417 smb2_key_derivation(session_key, sizeof(session_key),
5418 label.data, label.length,
5419 context.data, context.length,
5420 session->smb2->decryption_key.data);
5423 session->smb2->application_key = data_blob_dup_talloc(session,
5424 session->smb2->signing_key);
5425 if (session->smb2->application_key.data == NULL) {
5426 ZERO_STRUCT(session_key);
5427 return NT_STATUS_NO_MEMORY;
5430 if (conn->protocol >= PROTOCOL_SMB2_24) {
5431 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
5432 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
5434 smb2_key_derivation(session_key, sizeof(session_key),
5435 label.data, label.length,
5436 context.data, context.length,
5437 session->smb2->application_key.data);
5439 ZERO_STRUCT(session_key);
5441 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
5442 session->smb2->signing_key);
5443 if (session->smb2_channel.signing_key.data == NULL) {
5444 return NT_STATUS_NO_MEMORY;
5447 check_signature = conn->mandatory_signing;
5449 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
5450 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
5452 * Sadly some vendors don't sign the
5453 * final SMB2 session setup response
5455 * At least Windows and Samba are always doing this
5456 * if there's a session key available.
5458 * We only check the signature if it's mandatory
5459 * or SMB2_HDR_FLAG_SIGNED is provided.
5461 check_signature = true;
5464 if (check_signature) {
5465 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5466 session->conn->protocol,
5468 if (!NT_STATUS_IS_OK(status)) {
5473 session->smb2->should_sign = false;
5474 session->smb2->should_encrypt = false;
5476 if (conn->desire_signing) {
5477 session->smb2->should_sign = true;
5480 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
5481 session->smb2->should_sign = true;
5484 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
5485 session->smb2->should_encrypt = true;
5488 if (conn->protocol < PROTOCOL_SMB2_24) {
5489 session->smb2->should_encrypt = false;
5492 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
5493 session->smb2->should_encrypt = false;
5496 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
5497 sizeof(session->smb2->nonce_high));
5498 session->smb2->nonce_low = 1;
5500 return NT_STATUS_OK;
5503 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
5504 struct smbXcli_session *session1,
5505 struct smbXcli_conn *conn,
5506 struct smbXcli_session **_session2)
5508 struct smbXcli_session *session2;
5510 if (session1->smb2->signing_key.length == 0) {
5511 return NT_STATUS_INVALID_PARAMETER_MIX;
5515 return NT_STATUS_INVALID_PARAMETER_MIX;
5518 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
5519 if (session2 == NULL) {
5520 return NT_STATUS_NO_MEMORY;
5522 session2->smb2 = talloc_reference(session2, session1->smb2);
5523 if (session2->smb2 == NULL) {
5524 talloc_free(session2);
5525 return NT_STATUS_NO_MEMORY;
5528 talloc_set_destructor(session2, smbXcli_session_destructor);
5529 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
5530 session2->conn = conn;
5532 *_session2 = session2;
5533 return NT_STATUS_OK;
5536 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
5537 const DATA_BLOB _channel_key,
5538 const struct iovec *recv_iov)
5540 struct smbXcli_conn *conn = session->conn;
5541 uint8_t channel_key[16];
5545 return NT_STATUS_INVALID_PARAMETER_MIX;
5548 if (session->smb2_channel.signing_key.length != 0) {
5549 return NT_STATUS_INVALID_PARAMETER_MIX;
5552 ZERO_STRUCT(channel_key);
5553 memcpy(channel_key, _channel_key.data,
5554 MIN(_channel_key.length, sizeof(channel_key)));
5556 session->smb2_channel.signing_key = data_blob_talloc(session,
5558 sizeof(channel_key));
5559 if (session->smb2_channel.signing_key.data == NULL) {
5560 ZERO_STRUCT(channel_key);
5561 return NT_STATUS_NO_MEMORY;
5564 if (conn->protocol >= PROTOCOL_SMB2_24) {
5565 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
5566 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
5568 smb2_key_derivation(channel_key, sizeof(channel_key),
5569 label.data, label.length,
5570 context.data, context.length,
5571 session->smb2_channel.signing_key.data);
5573 ZERO_STRUCT(channel_key);
5575 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5576 session->conn->protocol,
5578 if (!NT_STATUS_IS_OK(status)) {
5582 return NT_STATUS_OK;
5585 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
5587 if (session->smb2->should_encrypt) {
5588 return NT_STATUS_OK;
5591 if (session->conn->protocol < PROTOCOL_SMB2_24) {
5592 return NT_STATUS_NOT_SUPPORTED;
5595 if (!(session->conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
5596 return NT_STATUS_NOT_SUPPORTED;
5599 if (session->smb2->signing_key.data == NULL) {
5600 return NT_STATUS_NOT_SUPPORTED;
5602 session->smb2->should_encrypt = true;
5603 return NT_STATUS_OK;
5606 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
5608 struct smbXcli_tcon *tcon;
5610 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
5618 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
5619 uint32_t fs_attributes)
5621 tcon->fs_attributes = fs_attributes;
5624 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
5626 return tcon->fs_attributes;
5629 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
5635 if (tcon->is_smb1) {
5636 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
5643 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
5650 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
5652 return tcon->smb1.tcon_id;
5655 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
5657 tcon->is_smb1 = true;
5658 tcon->smb1.tcon_id = tcon_id;
5661 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
5663 uint16_t optional_support,
5664 uint32_t maximal_access,
5665 uint32_t guest_maximal_access,
5666 const char *service,
5667 const char *fs_type)
5669 tcon->is_smb1 = true;
5670 tcon->fs_attributes = 0;
5671 tcon->smb1.tcon_id = tcon_id;
5672 tcon->smb1.optional_support = optional_support;
5673 tcon->smb1.maximal_access = maximal_access;
5674 tcon->smb1.guest_maximal_access = guest_maximal_access;
5676 TALLOC_FREE(tcon->smb1.service);
5677 tcon->smb1.service = talloc_strdup(tcon, service);
5678 if (service != NULL && tcon->smb1.service == NULL) {
5682 TALLOC_FREE(tcon->smb1.fs_type);
5683 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5684 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5691 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5693 return tcon->smb2.tcon_id;
5696 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5698 return tcon->smb2.capabilities;
5701 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5702 struct smbXcli_session *session,
5706 uint32_t capabilities,
5707 uint32_t maximal_access)
5709 tcon->is_smb1 = false;
5710 tcon->fs_attributes = 0;
5711 tcon->smb2.tcon_id = tcon_id;
5712 tcon->smb2.type = type;
5713 tcon->smb2.flags = flags;
5714 tcon->smb2.capabilities = capabilities;
5715 tcon->smb2.maximal_access = maximal_access;
5717 tcon->smb2.should_sign = false;
5718 tcon->smb2.should_encrypt = false;
5720 if (session == NULL) {
5724 tcon->smb2.should_sign = session->smb2->should_sign;
5725 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5727 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5728 tcon->smb2.should_encrypt = true;
5732 void smb2cli_tcon_should_sign(struct smbXcli_tcon *tcon,
5735 tcon->smb2.should_sign = should_sign;
5738 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon *tcon)
5740 if (tcon->smb2.should_encrypt) {
5744 return tcon->smb2.should_sign;
5747 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon *tcon,
5748 bool should_encrypt)
5750 tcon->smb2.should_encrypt = should_encrypt;
5753 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
5755 return tcon->smb2.should_encrypt;