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"
37 struct smbXcli_session;
41 struct sockaddr_storage local_ss;
42 struct sockaddr_storage remote_ss;
43 const char *remote_name;
45 struct tevent_queue *outgoing;
46 struct tevent_req **pending;
47 struct tevent_req *read_smb_req;
49 enum protocol_types protocol;
52 bool mandatory_signing;
55 * The incoming dispatch function should return:
56 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
57 * - NT_STATUS_OK, if no more processing is desired, e.g.
58 * the dispatch function called
60 * - All other return values disconnect the connection.
62 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
68 uint32_t capabilities;
73 uint32_t capabilities;
76 uint16_t security_mode;
85 const char *workgroup;
91 uint32_t capabilities;
96 struct smb_signing_state *signing;
97 struct smb_trans_enc_state *trans_enc;
102 uint16_t security_mode;
107 uint32_t capabilities;
108 uint16_t security_mode;
110 uint32_t max_trans_size;
111 uint32_t max_read_size;
112 uint32_t max_write_size;
119 uint16_t cur_credits;
120 uint16_t max_credits;
123 struct smbXcli_session *sessions;
126 struct smbXcli_session {
127 struct smbXcli_session *prev, *next;
128 struct smbXcli_conn *conn;
132 uint16_t session_flags;
133 DATA_BLOB signing_key;
134 DATA_BLOB session_key;
140 struct smbXcli_req_state {
141 struct tevent_context *ev;
142 struct smbXcli_conn *conn;
143 struct smbXcli_session *session; /* maybe NULL */
145 uint8_t length_hdr[4];
152 /* Space for the header including the wct */
153 uint8_t hdr[HDR_VWV];
156 * For normal requests, smb1cli_req_send chooses a mid.
157 * SecondaryV trans requests need to use the mid of the primary
158 * request, so we need a place to store it.
159 * Assume it is set if != 0.
164 uint8_t bytecount_buf[2];
166 #define MAX_SMB_IOV 5
167 /* length_hdr, hdr, words, byte_count, buffers */
168 struct iovec iov[1 + 3 + MAX_SMB_IOV];
173 struct tevent_req **chained_requests;
176 NTSTATUS recv_status;
177 /* always an array of 3 talloc elements */
178 struct iovec *recv_iov;
182 const uint8_t *fixed;
188 uint8_t pad[7]; /* padding space for compounding */
190 /* always an array of 3 talloc elements */
191 struct iovec *recv_iov;
193 uint16_t credit_charge;
195 bool signing_skipped;
201 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
204 * NT_STATUS_OK, means we do not notify the callers
206 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
208 while (conn->sessions) {
209 conn->sessions->conn = NULL;
210 DLIST_REMOVE(conn->sessions, conn->sessions);
213 if (conn->smb1.trans_enc) {
214 common_free_encryption_state(&conn->smb1.trans_enc);
220 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
222 const char *remote_name,
223 enum smb_signing_setting signing_state,
224 uint32_t smb1_capabilities,
225 struct GUID *client_guid)
227 struct smbXcli_conn *conn = NULL;
229 struct sockaddr *sa = NULL;
233 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
238 conn->remote_name = talloc_strdup(conn, remote_name);
239 if (conn->remote_name == NULL) {
245 ss = (void *)&conn->local_ss;
246 sa = (struct sockaddr *)ss;
247 sa_length = sizeof(conn->local_ss);
248 ret = getsockname(fd, sa, &sa_length);
252 ss = (void *)&conn->remote_ss;
253 sa = (struct sockaddr *)ss;
254 sa_length = sizeof(conn->remote_ss);
255 ret = getpeername(fd, sa, &sa_length);
260 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
261 if (conn->outgoing == NULL) {
264 conn->pending = NULL;
266 conn->protocol = PROTOCOL_NONE;
268 switch (signing_state) {
269 case SMB_SIGNING_OFF:
271 conn->allow_signing = false;
272 conn->desire_signing = false;
273 conn->mandatory_signing = false;
275 case SMB_SIGNING_DEFAULT:
276 case SMB_SIGNING_IF_REQUIRED:
277 /* if the server requires it */
278 conn->allow_signing = true;
279 conn->desire_signing = false;
280 conn->mandatory_signing = false;
282 case SMB_SIGNING_REQUIRED:
284 conn->allow_signing = true;
285 conn->desire_signing = true;
286 conn->mandatory_signing = true;
290 conn->smb1.client.capabilities = smb1_capabilities;
291 conn->smb1.client.max_xmit = UINT16_MAX;
293 conn->smb1.capabilities = conn->smb1.client.capabilities;
294 conn->smb1.max_xmit = 1024;
298 /* initialise signing */
299 conn->smb1.signing = smb_signing_init(conn,
301 conn->desire_signing,
302 conn->mandatory_signing);
303 if (!conn->smb1.signing) {
307 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
308 if (conn->mandatory_signing) {
309 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
312 conn->smb2.client.guid = *client_guid;
315 conn->smb2.cur_credits = 1;
316 conn->smb2.max_credits = 0;
318 talloc_set_destructor(conn, smbXcli_conn_destructor);
326 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
332 if (conn->fd == -1) {
339 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
341 return conn->protocol;
344 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
346 if (conn->protocol >= PROTOCOL_SMB2_02) {
350 if (conn->smb1.capabilities & CAP_UNICODE) {
357 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
359 set_socket_options(conn->fd, options);
362 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
364 return &conn->local_ss;
367 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
369 return &conn->remote_ss;
372 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
374 return conn->remote_name;
377 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
379 if (conn->protocol >= PROTOCOL_SMB2_02) {
386 return conn->smb1.server.max_mux;
389 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
391 if (conn->protocol >= PROTOCOL_SMB2_02) {
392 return conn->smb2.server.system_time;
395 return conn->smb1.server.system_time;
398 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
400 if (conn->protocol >= PROTOCOL_SMB2_02) {
401 return &conn->smb2.server.gss_blob;
404 return &conn->smb1.server.gss_blob;
407 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
409 if (conn->protocol >= PROTOCOL_SMB2_02) {
410 return &conn->smb2.server.guid;
413 return &conn->smb1.server.guid;
416 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
418 return conn->smb1.capabilities;
421 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
423 return conn->smb1.max_xmit;
426 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
428 return conn->smb1.server.session_key;
431 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
433 return conn->smb1.server.challenge;
436 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
438 return conn->smb1.server.security_mode;
441 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
443 return conn->smb1.server.time_zone;
446 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
447 const DATA_BLOB user_session_key,
448 const DATA_BLOB response)
450 return smb_signing_activate(conn->smb1.signing,
455 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
456 const uint8_t *buf, uint32_t seqnum)
458 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
461 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
463 return smb_signing_is_active(conn->smb1.signing);
466 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
467 struct smb_trans_enc_state *es)
469 /* Replace the old state, if any. */
470 if (conn->smb1.trans_enc) {
471 common_free_encryption_state(&conn->smb1.trans_enc);
473 conn->smb1.trans_enc = es;
476 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
478 return common_encryption_on(conn->smb1.trans_enc);
482 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
484 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
485 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
487 if (NT_STATUS_IS_OK(status)) {
491 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
495 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
499 * Is the SMB command able to hold an AND_X successor
500 * @param[in] cmd The SMB command in question
501 * @retval Can we add a chained request after "cmd"?
503 bool smb1cli_is_andx_req(uint8_t cmd)
523 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
525 size_t num_pending = talloc_array_length(conn->pending);
531 result = conn->smb1.mid++;
532 if ((result == 0) || (result == 0xffff)) {
536 for (i=0; i<num_pending; i++) {
537 if (result == smb1cli_req_mid(conn->pending[i])) {
542 if (i == num_pending) {
548 void smbXcli_req_unset_pending(struct tevent_req *req)
550 struct smbXcli_req_state *state =
552 struct smbXcli_req_state);
553 struct smbXcli_conn *conn = state->conn;
554 size_t num_pending = talloc_array_length(conn->pending);
557 if (state->smb1.mid != 0) {
559 * This is a [nt]trans[2] request which waits
560 * for more than one reply.
565 talloc_set_destructor(req, NULL);
567 if (num_pending == 1) {
569 * The pending read_smb tevent_req is a child of
570 * conn->pending. So if nothing is pending anymore, we need to
571 * delete the socket read fde.
573 TALLOC_FREE(conn->pending);
574 conn->read_smb_req = NULL;
578 for (i=0; i<num_pending; i++) {
579 if (req == conn->pending[i]) {
583 if (i == num_pending) {
585 * Something's seriously broken. Just returning here is the
586 * right thing nevertheless, the point of this routine is to
587 * remove ourselves from conn->pending.
593 * Remove ourselves from the conn->pending array
595 for (; i < (num_pending - 1); i++) {
596 conn->pending[i] = conn->pending[i+1];
600 * No NULL check here, we're shrinking by sizeof(void *), and
601 * talloc_realloc just adjusts the size for this.
603 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
608 static int smbXcli_req_destructor(struct tevent_req *req)
610 struct smbXcli_req_state *state =
612 struct smbXcli_req_state);
615 * Make sure we really remove it from
616 * the pending array on destruction.
619 smbXcli_req_unset_pending(req);
623 static bool smb1cli_req_cancel(struct tevent_req *req);
624 static bool smb2cli_req_cancel(struct tevent_req *req);
626 static bool smbXcli_req_cancel(struct tevent_req *req)
628 struct smbXcli_req_state *state =
630 struct smbXcli_req_state);
632 if (!smbXcli_conn_is_connected(state->conn)) {
636 if (state->conn->protocol == PROTOCOL_NONE) {
640 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
641 return smb2cli_req_cancel(req);
644 return smb1cli_req_cancel(req);
647 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
649 bool smbXcli_req_set_pending(struct tevent_req *req)
651 struct smbXcli_req_state *state =
653 struct smbXcli_req_state);
654 struct smbXcli_conn *conn;
655 struct tevent_req **pending;
660 if (!smbXcli_conn_is_connected(conn)) {
664 num_pending = talloc_array_length(conn->pending);
666 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
668 if (pending == NULL) {
671 pending[num_pending] = req;
672 conn->pending = pending;
673 talloc_set_destructor(req, smbXcli_req_destructor);
674 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
676 if (!smbXcli_conn_receive_next(conn)) {
678 * the caller should notify the current request
680 * And all other pending requests get notified
681 * by smbXcli_conn_disconnect().
683 smbXcli_req_unset_pending(req);
684 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
691 static void smbXcli_conn_received(struct tevent_req *subreq);
693 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
695 size_t num_pending = talloc_array_length(conn->pending);
696 struct tevent_req *req;
697 struct smbXcli_req_state *state;
699 if (conn->read_smb_req != NULL) {
703 if (num_pending == 0) {
704 if (conn->smb2.mid < UINT64_MAX) {
705 /* no more pending requests, so we are done for now */
710 * If there are no more SMB2 requests possible,
711 * because we are out of message ids,
712 * we need to disconnect.
714 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
718 req = conn->pending[0];
719 state = tevent_req_data(req, struct smbXcli_req_state);
722 * We're the first ones, add the read_smb request that waits for the
723 * answer from the server
725 conn->read_smb_req = read_smb_send(conn->pending, state->ev, conn->fd);
726 if (conn->read_smb_req == NULL) {
729 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
733 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
735 if (conn->fd != -1) {
741 * Cancel all pending requests. We do not do a for-loop walking
742 * conn->pending because that array changes in
743 * smbXcli_req_unset_pending.
745 while (talloc_array_length(conn->pending) > 0) {
746 struct tevent_req *req;
747 struct smbXcli_req_state *state;
748 struct tevent_req **chain;
752 req = conn->pending[0];
753 state = tevent_req_data(req, struct smbXcli_req_state);
755 if (state->smb1.chained_requests == NULL) {
757 * We're dead. No point waiting for trans2
762 smbXcli_req_unset_pending(req);
764 if (NT_STATUS_IS_OK(status)) {
765 /* do not notify the callers */
770 * we need to defer the callback, because we may notify
771 * more then one caller.
773 tevent_req_defer_callback(req, state->ev);
774 tevent_req_nterror(req, status);
778 chain = talloc_move(conn, &state->smb1.chained_requests);
779 num_chained = talloc_array_length(chain);
781 for (i=0; i<num_chained; i++) {
783 state = tevent_req_data(req, struct smbXcli_req_state);
786 * We're dead. No point waiting for trans2
791 smbXcli_req_unset_pending(req);
793 if (NT_STATUS_IS_OK(status)) {
794 /* do not notify the callers */
799 * we need to defer the callback, because we may notify
800 * more then one caller.
802 tevent_req_defer_callback(req, state->ev);
803 tevent_req_nterror(req, status);
810 * Fetch a smb request's mid. Only valid after the request has been sent by
811 * smb1cli_req_send().
813 uint16_t smb1cli_req_mid(struct tevent_req *req)
815 struct smbXcli_req_state *state =
817 struct smbXcli_req_state);
819 if (state->smb1.mid != 0) {
820 return state->smb1.mid;
823 return SVAL(state->smb1.hdr, HDR_MID);
826 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
828 struct smbXcli_req_state *state =
830 struct smbXcli_req_state);
832 state->smb1.mid = mid;
835 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
837 struct smbXcli_req_state *state =
839 struct smbXcli_req_state);
841 return state->smb1.seqnum;
844 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
846 struct smbXcli_req_state *state =
848 struct smbXcli_req_state);
850 state->smb1.seqnum = seqnum;
853 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
857 for (i=0; i<count; i++) {
858 result += iov[i].iov_len;
863 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
864 const struct iovec *iov,
867 size_t len = smbXcli_iov_len(iov, count);
872 buf = talloc_array(mem_ctx, uint8_t, len);
877 for (i=0; i<count; i++) {
878 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
879 copied += iov[i].iov_len;
884 static void smb1cli_req_flags(enum protocol_types protocol,
885 uint32_t smb1_capabilities,
887 uint8_t additional_flags,
890 uint16_t additional_flags2,
891 uint16_t clear_flags2,
897 if (protocol >= PROTOCOL_LANMAN1) {
898 flags |= FLAG_CASELESS_PATHNAMES;
899 flags |= FLAG_CANONICAL_PATHNAMES;
902 if (protocol >= PROTOCOL_LANMAN2) {
903 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
904 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
907 if (protocol >= PROTOCOL_NT1) {
908 flags2 |= FLAGS2_IS_LONG_NAME;
910 if (smb1_capabilities & CAP_UNICODE) {
911 flags2 |= FLAGS2_UNICODE_STRINGS;
913 if (smb1_capabilities & CAP_STATUS32) {
914 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
916 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
917 flags2 |= FLAGS2_EXTENDED_SECURITY;
921 flags |= additional_flags;
922 flags &= ~clear_flags;
923 flags2 |= additional_flags2;
924 flags2 &= ~clear_flags2;
930 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
932 static bool smb1cli_req_cancel(struct tevent_req *req)
934 struct smbXcli_req_state *state =
936 struct smbXcli_req_state);
943 struct tevent_req *subreq;
946 flags = CVAL(state->smb1.hdr, HDR_FLG);
947 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
948 pid = SVAL(state->smb1.hdr, HDR_PID);
949 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
950 tid = SVAL(state->smb1.hdr, HDR_TID);
951 uid = SVAL(state->smb1.hdr, HDR_UID);
952 mid = SVAL(state->smb1.hdr, HDR_MID);
954 subreq = smb1cli_req_create(state, state->ev,
962 0, NULL); /* bytes */
963 if (subreq == NULL) {
966 smb1cli_req_set_mid(subreq, mid);
968 status = smb1cli_req_chain_submit(&subreq, 1);
969 if (!NT_STATUS_IS_OK(status)) {
973 smb1cli_req_set_mid(subreq, 0);
975 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
980 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
982 /* we do not care about the result */
986 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
987 struct tevent_context *ev,
988 struct smbXcli_conn *conn,
990 uint8_t additional_flags,
992 uint16_t additional_flags2,
993 uint16_t clear_flags2,
994 uint32_t timeout_msec,
998 uint8_t wct, uint16_t *vwv,
1000 struct iovec *bytes_iov)
1002 struct tevent_req *req;
1003 struct smbXcli_req_state *state;
1005 uint16_t flags2 = 0;
1007 if (iov_count > MAX_SMB_IOV) {
1009 * Should not happen :-)
1014 req = tevent_req_create(mem_ctx, &state,
1015 struct smbXcli_req_state);
1022 state->smb1.recv_cmd = 0xFF;
1023 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1024 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1025 if (state->smb1.recv_iov == NULL) {
1030 smb1cli_req_flags(conn->protocol,
1031 conn->smb1.capabilities,
1040 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1041 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1042 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1043 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1044 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1045 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1046 SSVAL(state->smb1.hdr, HDR_TID, tid);
1047 SSVAL(state->smb1.hdr, HDR_PID, pid);
1048 SSVAL(state->smb1.hdr, HDR_UID, uid);
1049 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1050 SSVAL(state->smb1.hdr, HDR_WCT, wct);
1052 state->smb1.vwv = vwv;
1054 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1056 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1057 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1058 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1059 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1060 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1061 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1062 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1063 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1065 if (iov_count != 0) {
1066 memcpy(&state->smb1.iov[4], bytes_iov,
1067 iov_count * sizeof(*bytes_iov));
1069 state->smb1.iov_count = iov_count + 4;
1071 if (timeout_msec > 0) {
1072 struct timeval endtime;
1074 endtime = timeval_current_ofs_msec(timeout_msec);
1075 if (!tevent_req_set_endtime(req, ev, endtime)) {
1080 switch (smb_command) {
1084 state->one_way = true;
1087 state->one_way = true;
1088 state->smb1.one_way_seqnum = true;
1092 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1093 state->one_way = true;
1101 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1102 struct iovec *iov, int iov_count,
1104 bool one_way_seqnum)
1109 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1110 * iovec directly. MD5Update would do that just fine.
1113 if (iov_count < 4) {
1114 return NT_STATUS_INVALID_PARAMETER_MIX;
1116 if (iov[0].iov_len != NBT_HDR_SIZE) {
1117 return NT_STATUS_INVALID_PARAMETER_MIX;
1119 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1120 return NT_STATUS_INVALID_PARAMETER_MIX;
1122 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1123 return NT_STATUS_INVALID_PARAMETER_MIX;
1125 if (iov[3].iov_len != sizeof(uint16_t)) {
1126 return NT_STATUS_INVALID_PARAMETER_MIX;
1129 buf = smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1131 return NT_STATUS_NO_MEMORY;
1134 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1136 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1137 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1140 return NT_STATUS_OK;
1143 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1144 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1145 TALLOC_CTX *tmp_mem,
1148 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1149 struct smbXcli_req_state *state,
1150 struct iovec *iov, int iov_count)
1152 struct tevent_req *subreq;
1156 if (!smbXcli_conn_is_connected(state->conn)) {
1157 return NT_STATUS_CONNECTION_DISCONNECTED;
1160 if (state->conn->protocol > PROTOCOL_NT1) {
1161 return NT_STATUS_REVISION_MISMATCH;
1164 if (iov_count < 4) {
1165 return NT_STATUS_INVALID_PARAMETER_MIX;
1167 if (iov[0].iov_len != NBT_HDR_SIZE) {
1168 return NT_STATUS_INVALID_PARAMETER_MIX;
1170 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1171 return NT_STATUS_INVALID_PARAMETER_MIX;
1173 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1174 return NT_STATUS_INVALID_PARAMETER_MIX;
1176 if (iov[3].iov_len != sizeof(uint16_t)) {
1177 return NT_STATUS_INVALID_PARAMETER_MIX;
1180 if (state->smb1.mid != 0) {
1181 mid = state->smb1.mid;
1183 mid = smb1cli_alloc_mid(state->conn);
1185 SSVAL(iov[1].iov_base, HDR_MID, mid);
1187 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1189 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1190 &state->smb1.seqnum,
1191 state->smb1.one_way_seqnum);
1193 if (!NT_STATUS_IS_OK(status)) {
1198 * If we supported multiple encrytion contexts
1199 * here we'd look up based on tid.
1201 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1202 char *buf, *enc_buf;
1204 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1206 return NT_STATUS_NO_MEMORY;
1208 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1209 (char *)buf, &enc_buf);
1211 if (!NT_STATUS_IS_OK(status)) {
1212 DEBUG(0, ("Error in encrypting client message: %s\n",
1213 nt_errstr(status)));
1216 buf = (char *)talloc_memdup(state, enc_buf,
1217 smb_len_nbt(enc_buf)+4);
1220 return NT_STATUS_NO_MEMORY;
1222 iov[0].iov_base = (void *)buf;
1223 iov[0].iov_len = talloc_get_size(buf);
1227 if (state->conn->dispatch_incoming == NULL) {
1228 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1231 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1233 subreq = writev_send(state, state->ev, state->conn->outgoing,
1234 state->conn->fd, false, iov, iov_count);
1235 if (subreq == NULL) {
1236 return NT_STATUS_NO_MEMORY;
1238 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1239 return NT_STATUS_OK;
1242 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1243 struct tevent_context *ev,
1244 struct smbXcli_conn *conn,
1245 uint8_t smb_command,
1246 uint8_t additional_flags,
1247 uint8_t clear_flags,
1248 uint16_t additional_flags2,
1249 uint16_t clear_flags2,
1250 uint32_t timeout_msec,
1254 uint8_t wct, uint16_t *vwv,
1256 const uint8_t *bytes)
1258 struct tevent_req *req;
1262 iov.iov_base = discard_const_p(void, bytes);
1263 iov.iov_len = num_bytes;
1265 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1266 additional_flags, clear_flags,
1267 additional_flags2, clear_flags2,
1274 if (!tevent_req_is_in_progress(req)) {
1275 return tevent_req_post(req, ev);
1277 status = smb1cli_req_chain_submit(&req, 1);
1278 if (tevent_req_nterror(req, status)) {
1279 return tevent_req_post(req, ev);
1284 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1286 struct tevent_req *req =
1287 tevent_req_callback_data(subreq,
1289 struct smbXcli_req_state *state =
1290 tevent_req_data(req,
1291 struct smbXcli_req_state);
1295 nwritten = writev_recv(subreq, &err);
1296 TALLOC_FREE(subreq);
1297 if (nwritten == -1) {
1298 NTSTATUS status = map_nt_error_from_unix_common(err);
1299 smbXcli_conn_disconnect(state->conn, status);
1303 if (state->one_way) {
1304 state->inbuf = NULL;
1305 tevent_req_done(req);
1309 if (!smbXcli_req_set_pending(req)) {
1310 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1315 static void smbXcli_conn_received(struct tevent_req *subreq)
1317 struct smbXcli_conn *conn =
1318 tevent_req_callback_data(subreq,
1319 struct smbXcli_conn);
1320 TALLOC_CTX *frame = talloc_stackframe();
1326 if (subreq != conn->read_smb_req) {
1327 DEBUG(1, ("Internal error: cli_smb_received called with "
1328 "unexpected subreq\n"));
1329 status = NT_STATUS_INTERNAL_ERROR;
1330 smbXcli_conn_disconnect(conn, status);
1334 conn->read_smb_req = NULL;
1336 received = read_smb_recv(subreq, frame, &inbuf, &err);
1337 TALLOC_FREE(subreq);
1338 if (received == -1) {
1339 status = map_nt_error_from_unix_common(err);
1340 smbXcli_conn_disconnect(conn, status);
1345 status = conn->dispatch_incoming(conn, frame, inbuf);
1347 if (NT_STATUS_IS_OK(status)) {
1349 * We should not do any more processing
1350 * as the dispatch function called
1351 * tevent_req_done().
1354 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1356 * We got an error, so notify all pending requests
1358 smbXcli_conn_disconnect(conn, status);
1363 * We got NT_STATUS_RETRY, so we may ask for a
1364 * next incoming pdu.
1366 if (!smbXcli_conn_receive_next(conn)) {
1367 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1371 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1372 struct iovec **piov, int *pnum_iov)
1383 buflen = smb_len_nbt(buf);
1386 hdr = buf + NBT_HDR_SIZE;
1388 if (buflen < MIN_SMB_SIZE) {
1389 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1393 * This returns iovec elements in the following order:
1408 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1410 return NT_STATUS_NO_MEMORY;
1412 iov[0].iov_base = hdr;
1413 iov[0].iov_len = HDR_WCT;
1416 cmd = CVAL(hdr, HDR_COM);
1420 size_t len = buflen - taken;
1422 struct iovec *iov_tmp;
1429 * we need at least WCT and BCC
1431 needed = sizeof(uint8_t) + sizeof(uint16_t);
1433 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1434 __location__, (int)len, (int)needed));
1439 * Now we check if the specified words are there
1441 wct = CVAL(hdr, wct_ofs);
1442 needed += wct * sizeof(uint16_t);
1444 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1445 __location__, (int)len, (int)needed));
1450 * Now we check if the specified bytes are there
1452 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1453 bcc = SVAL(hdr, bcc_ofs);
1454 needed += bcc * sizeof(uint8_t);
1456 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1457 __location__, (int)len, (int)needed));
1462 * we allocate 2 iovec structures for words and bytes
1464 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1466 if (iov_tmp == NULL) {
1468 return NT_STATUS_NO_MEMORY;
1471 cur = &iov[num_iov];
1474 cur[0].iov_len = wct * sizeof(uint16_t);
1475 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1476 cur[1].iov_len = bcc * sizeof(uint8_t);
1477 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1481 if (!smb1cli_is_andx_req(cmd)) {
1483 * If the current command does not have AndX chanining
1489 if (wct == 0 && bcc == 0) {
1491 * An empty response also ends the chain,
1492 * most likely with an error.
1498 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1499 __location__, (int)wct, (int)cmd));
1502 cmd = CVAL(cur[0].iov_base, 0);
1505 * If it is the end of the chain we are also done.
1509 wct_ofs = SVAL(cur[0].iov_base, 2);
1511 if (wct_ofs < taken) {
1512 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1514 if (wct_ofs > buflen) {
1515 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1519 * we consumed everything up to the start of the next
1525 remaining = buflen - taken;
1527 if (remaining > 0 && num_iov >= 3) {
1529 * The last DATA block gets the remaining
1530 * bytes, this is needed to support
1531 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1533 iov[num_iov-1].iov_len += remaining;
1537 *pnum_iov = num_iov;
1538 return NT_STATUS_OK;
1542 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1545 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1546 TALLOC_CTX *tmp_mem,
1549 struct tevent_req *req;
1550 struct smbXcli_req_state *state;
1557 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1558 struct iovec *iov = NULL;
1560 struct tevent_req **chain = NULL;
1561 size_t num_chained = 0;
1562 size_t num_responses = 0;
1564 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1565 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1566 DEBUG(10, ("Got non-SMB PDU\n"));
1567 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1571 * If we supported multiple encrytion contexts
1572 * here we'd look up based on tid.
1574 if (common_encryption_on(conn->smb1.trans_enc)
1575 && (CVAL(inbuf, 0) == 0)) {
1576 uint16_t enc_ctx_num;
1578 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1579 if (!NT_STATUS_IS_OK(status)) {
1580 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1581 nt_errstr(status)));
1585 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1586 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1588 conn->smb1.trans_enc->enc_ctx_num));
1589 return NT_STATUS_INVALID_HANDLE;
1592 status = common_decrypt_buffer(conn->smb1.trans_enc,
1594 if (!NT_STATUS_IS_OK(status)) {
1595 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1596 nt_errstr(status)));
1601 mid = SVAL(inhdr, HDR_MID);
1602 num_pending = talloc_array_length(conn->pending);
1604 for (i=0; i<num_pending; i++) {
1605 if (mid == smb1cli_req_mid(conn->pending[i])) {
1609 if (i == num_pending) {
1610 /* Dump unexpected reply */
1611 return NT_STATUS_RETRY;
1614 oplock_break = false;
1616 if (mid == 0xffff) {
1618 * Paranoia checks that this is really an oplock break request.
1620 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1621 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1622 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1623 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1624 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1626 if (!oplock_break) {
1627 /* Dump unexpected reply */
1628 return NT_STATUS_RETRY;
1632 req = conn->pending[i];
1633 state = tevent_req_data(req, struct smbXcli_req_state);
1635 if (!oplock_break /* oplock breaks are not signed */
1636 && !smb_signing_check_pdu(conn->smb1.signing,
1637 inbuf, state->smb1.seqnum+1)) {
1638 DEBUG(10, ("cli_check_sign_mac failed\n"));
1639 return NT_STATUS_ACCESS_DENIED;
1642 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1644 if (!NT_STATUS_IS_OK(status)) {
1645 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1646 nt_errstr(status)));
1650 cmd = CVAL(inhdr, HDR_COM);
1651 status = smb1cli_pull_raw_error(inhdr);
1653 if (state->smb1.chained_requests == NULL) {
1655 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1658 smbXcli_req_unset_pending(req);
1660 state->smb1.recv_cmd = cmd;
1661 state->smb1.recv_status = status;
1662 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1664 state->smb1.recv_iov[0] = iov[0];
1665 state->smb1.recv_iov[1] = iov[1];
1666 state->smb1.recv_iov[2] = iov[2];
1668 if (talloc_array_length(conn->pending) == 0) {
1669 tevent_req_done(req);
1670 return NT_STATUS_OK;
1673 tevent_req_defer_callback(req, state->ev);
1674 tevent_req_done(req);
1675 return NT_STATUS_RETRY;
1678 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1679 num_chained = talloc_array_length(chain);
1680 num_responses = (num_iov - 1)/2;
1682 if (num_responses > num_chained) {
1683 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1686 for (i=0; i<num_chained; i++) {
1687 size_t iov_idx = 1 + (i*2);
1688 struct iovec *cur = &iov[iov_idx];
1692 state = tevent_req_data(req, struct smbXcli_req_state);
1694 smbXcli_req_unset_pending(req);
1697 * as we finish multiple requests here
1698 * we need to defer the callbacks as
1699 * they could destroy our current stack state.
1701 tevent_req_defer_callback(req, state->ev);
1703 if (i >= num_responses) {
1704 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1708 state->smb1.recv_cmd = cmd;
1710 if (i == (num_responses - 1)) {
1712 * The last request in the chain gets the status
1714 state->smb1.recv_status = status;
1716 cmd = CVAL(cur[0].iov_base, 0);
1717 state->smb1.recv_status = NT_STATUS_OK;
1720 state->inbuf = inbuf;
1723 * Note: here we use talloc_reference() in a way
1724 * that does not expose it to the caller.
1726 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1727 if (tevent_req_nomem(inbuf_ref, req)) {
1731 /* copy the related buffers */
1732 state->smb1.recv_iov[0] = iov[0];
1733 state->smb1.recv_iov[1] = cur[0];
1734 state->smb1.recv_iov[2] = cur[1];
1736 tevent_req_done(req);
1739 return NT_STATUS_RETRY;
1742 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
1743 TALLOC_CTX *mem_ctx,
1744 struct iovec **piov,
1748 uint32_t *pvwv_offset,
1749 uint32_t *pnum_bytes,
1751 uint32_t *pbytes_offset,
1753 const struct smb1cli_req_expected_response *expected,
1754 size_t num_expected)
1756 struct smbXcli_req_state *state =
1757 tevent_req_data(req,
1758 struct smbXcli_req_state);
1759 NTSTATUS status = NT_STATUS_OK;
1760 struct iovec *recv_iov = NULL;
1761 uint8_t *hdr = NULL;
1763 uint32_t vwv_offset = 0;
1764 uint16_t *vwv = NULL;
1765 uint32_t num_bytes = 0;
1766 uint32_t bytes_offset = 0;
1767 uint8_t *bytes = NULL;
1769 bool found_status = false;
1770 bool found_size = false;
1784 if (pvwv_offset != NULL) {
1787 if (pnum_bytes != NULL) {
1790 if (pbytes != NULL) {
1793 if (pbytes_offset != NULL) {
1796 if (pinbuf != NULL) {
1800 if (state->inbuf != NULL) {
1801 recv_iov = state->smb1.recv_iov;
1802 hdr = (uint8_t *)recv_iov[0].iov_base;
1803 wct = recv_iov[1].iov_len/2;
1804 vwv = (uint16_t *)recv_iov[1].iov_base;
1805 vwv_offset = PTR_DIFF(vwv, hdr);
1806 num_bytes = recv_iov[2].iov_len;
1807 bytes = (uint8_t *)recv_iov[2].iov_base;
1808 bytes_offset = PTR_DIFF(bytes, hdr);
1811 if (tevent_req_is_nterror(req, &status)) {
1812 for (i=0; i < num_expected; i++) {
1813 if (NT_STATUS_EQUAL(status, expected[i].status)) {
1814 found_status = true;
1820 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1826 if (num_expected == 0) {
1827 found_status = true;
1831 status = state->smb1.recv_status;
1833 for (i=0; i < num_expected; i++) {
1834 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
1838 found_status = true;
1839 if (expected[i].wct == 0) {
1844 if (expected[i].wct == wct) {
1850 if (!found_status) {
1855 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1859 *piov = talloc_move(mem_ctx, &recv_iov);
1871 if (pvwv_offset != NULL) {
1872 *pvwv_offset = vwv_offset;
1874 if (pnum_bytes != NULL) {
1875 *pnum_bytes = num_bytes;
1877 if (pbytes != NULL) {
1880 if (pbytes_offset != NULL) {
1881 *pbytes_offset = bytes_offset;
1883 if (pinbuf != NULL) {
1884 *pinbuf = state->inbuf;
1890 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
1897 for (i=0; i<num_reqs; i++) {
1898 struct smbXcli_req_state *state;
1899 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
1900 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
1901 state->smb1.iov_count-2);
1902 wct_ofs = (wct_ofs + 3) & ~3;
1907 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
1909 struct smbXcli_req_state *first_state =
1910 tevent_req_data(reqs[0],
1911 struct smbXcli_req_state);
1912 struct smbXcli_req_state *state;
1914 size_t chain_padding = 0;
1916 struct iovec *iov = NULL;
1917 struct iovec *this_iov;
1921 if (num_reqs == 1) {
1922 return smb1cli_req_writev_submit(reqs[0], first_state,
1923 first_state->smb1.iov,
1924 first_state->smb1.iov_count);
1928 for (i=0; i<num_reqs; i++) {
1929 if (!tevent_req_is_in_progress(reqs[i])) {
1930 return NT_STATUS_INTERNAL_ERROR;
1933 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
1935 if (state->smb1.iov_count < 4) {
1936 return NT_STATUS_INVALID_PARAMETER_MIX;
1941 * The NBT and SMB header
1954 iovlen += state->smb1.iov_count - 2;
1957 iov = talloc_zero_array(first_state, struct iovec, iovlen);
1959 return NT_STATUS_NO_MEMORY;
1962 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
1963 first_state, reqs, sizeof(*reqs) * num_reqs);
1964 if (first_state->smb1.chained_requests == NULL) {
1966 return NT_STATUS_NO_MEMORY;
1969 wct_offset = HDR_WCT;
1972 for (i=0; i<num_reqs; i++) {
1973 size_t next_padding = 0;
1976 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
1978 if (i < num_reqs-1) {
1979 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
1980 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
1982 TALLOC_FREE(first_state->smb1.chained_requests);
1983 return NT_STATUS_INVALID_PARAMETER_MIX;
1987 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
1988 state->smb1.iov_count-2) + 1;
1989 if ((wct_offset % 4) != 0) {
1990 next_padding = 4 - (wct_offset % 4);
1992 wct_offset += next_padding;
1993 vwv = state->smb1.vwv;
1995 if (i < num_reqs-1) {
1996 struct smbXcli_req_state *next_state =
1997 tevent_req_data(reqs[i+1],
1998 struct smbXcli_req_state);
1999 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2001 SSVAL(vwv+1, 0, wct_offset);
2002 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2003 /* properly end the chain */
2004 SCVAL(vwv+0, 0, 0xff);
2005 SCVAL(vwv+0, 1, 0xff);
2011 * The NBT and SMB header
2013 this_iov[0] = state->smb1.iov[0];
2014 this_iov[1] = state->smb1.iov[1];
2018 * This one is a bit subtle. We have to add
2019 * chain_padding bytes between the requests, and we
2020 * have to also include the wct field of the
2021 * subsequent requests. We use the subsequent header
2022 * for the padding, it contains the wct field in its
2025 this_iov[0].iov_len = chain_padding+1;
2026 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2027 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2028 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2033 * copy the words and bytes
2035 memcpy(this_iov, state->smb1.iov+2,
2036 sizeof(struct iovec) * (state->smb1.iov_count-2));
2037 this_iov += state->smb1.iov_count - 2;
2038 chain_padding = next_padding;
2041 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2042 if (nbt_len > first_state->conn->smb1.max_xmit) {
2044 TALLOC_FREE(first_state->smb1.chained_requests);
2045 return NT_STATUS_INVALID_PARAMETER_MIX;
2048 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2049 if (!NT_STATUS_IS_OK(status)) {
2051 TALLOC_FREE(first_state->smb1.chained_requests);
2055 return NT_STATUS_OK;
2058 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2060 return ((tevent_queue_length(conn->outgoing) != 0)
2061 || (talloc_array_length(conn->pending) != 0));
2064 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2066 return conn->smb2.server.capabilities;
2069 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2071 return conn->smb2.server.security_mode;
2074 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2076 return conn->smb2.server.max_trans_size;
2079 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2081 return conn->smb2.server.max_read_size;
2084 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2086 return conn->smb2.server.max_write_size;
2089 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2090 uint16_t max_credits)
2092 conn->smb2.max_credits = max_credits;
2095 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2097 static bool smb2cli_req_cancel(struct tevent_req *req)
2099 struct smbXcli_req_state *state =
2100 tevent_req_data(req,
2101 struct smbXcli_req_state);
2102 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2103 uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
2104 uint32_t tid = IVAL(state->smb2.hdr, SMB2_HDR_TID);
2105 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2106 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2107 struct smbXcli_session *session = state->session;
2108 uint8_t *fixed = state->smb2.pad;
2109 uint16_t fixed_len = 4;
2110 struct tevent_req *subreq;
2111 struct smbXcli_req_state *substate;
2114 SSVAL(fixed, 0, 0x04);
2117 subreq = smb2cli_req_create(state, state->ev,
2125 if (subreq == NULL) {
2128 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2130 if (flags & SMB2_HDR_FLAG_ASYNC) {
2134 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2135 SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
2136 SIVAL(substate->smb2.hdr, SMB2_HDR_TID, tid);
2137 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2138 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2140 status = smb2cli_req_compound_submit(&subreq, 1);
2141 if (!NT_STATUS_IS_OK(status)) {
2142 TALLOC_FREE(subreq);
2146 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2151 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2153 /* we do not care about the result */
2154 TALLOC_FREE(subreq);
2157 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2158 struct tevent_context *ev,
2159 struct smbXcli_conn *conn,
2161 uint32_t additional_flags,
2162 uint32_t clear_flags,
2163 uint32_t timeout_msec,
2166 struct smbXcli_session *session,
2167 const uint8_t *fixed,
2172 struct tevent_req *req;
2173 struct smbXcli_req_state *state;
2177 req = tevent_req_create(mem_ctx, &state,
2178 struct smbXcli_req_state);
2185 state->session = session;
2188 uid = session->smb2.session_id;
2191 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2192 if (state->smb2.recv_iov == NULL) {
2197 flags |= additional_flags;
2198 flags &= ~clear_flags;
2200 state->smb2.fixed = fixed;
2201 state->smb2.fixed_len = fixed_len;
2202 state->smb2.dyn = dyn;
2203 state->smb2.dyn_len = dyn_len;
2205 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2206 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2207 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2208 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2209 SIVAL(state->smb2.hdr, SMB2_HDR_PID, pid);
2210 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2211 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2214 case SMB2_OP_CANCEL:
2215 state->one_way = true;
2219 * If this is a dummy request, it will have
2220 * UINT64_MAX as message id.
2221 * If we send on break acknowledgement,
2222 * this gets overwritten later.
2224 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2228 if (timeout_msec > 0) {
2229 struct timeval endtime;
2231 endtime = timeval_current_ofs_msec(timeout_msec);
2232 if (!tevent_req_set_endtime(req, ev, endtime)) {
2240 void smb2cli_req_set_notify_async(struct tevent_req *req)
2242 struct smbXcli_req_state *state =
2243 tevent_req_data(req,
2244 struct smbXcli_req_state);
2246 state->smb2.notify_async = true;
2249 static void smb2cli_writev_done(struct tevent_req *subreq);
2250 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2251 TALLOC_CTX *tmp_mem,
2254 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2257 struct smbXcli_req_state *state;
2258 struct tevent_req *subreq;
2260 int i, num_iov, nbt_len;
2263 * 1 for the nbt length
2264 * per request: HDR, fixed, dyn, padding
2265 * -1 because the last one does not need padding
2268 iov = talloc_array(reqs[0], struct iovec, 1 + 4*num_reqs - 1);
2270 return NT_STATUS_NO_MEMORY;
2276 for (i=0; i<num_reqs; i++) {
2285 bool should_sign = false;
2287 if (!tevent_req_is_in_progress(reqs[i])) {
2288 return NT_STATUS_INTERNAL_ERROR;
2291 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2293 if (!smbXcli_conn_is_connected(state->conn)) {
2294 return NT_STATUS_CONNECTION_DISCONNECTED;
2297 if ((state->conn->protocol != PROTOCOL_NONE) &&
2298 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2299 return NT_STATUS_REVISION_MISMATCH;
2302 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2303 if (opcode == SMB2_OP_CANCEL) {
2307 avail = UINT64_MAX - state->conn->smb2.mid;
2309 return NT_STATUS_CONNECTION_ABORTED;
2312 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2313 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2318 charge = MAX(state->smb2.credit_charge, charge);
2320 avail = MIN(avail, state->conn->smb2.cur_credits);
2321 if (avail < charge) {
2322 return NT_STATUS_INTERNAL_ERROR;
2326 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2327 credits = state->conn->smb2.max_credits -
2328 state->conn->smb2.cur_credits;
2330 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2334 mid = state->conn->smb2.mid;
2335 state->conn->smb2.mid += charge;
2336 state->conn->smb2.cur_credits -= charge;
2338 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2339 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2341 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2342 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2346 iov[num_iov].iov_base = state->smb2.hdr;
2347 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2350 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2351 iov[num_iov].iov_len = state->smb2.fixed_len;
2354 if (state->smb2.dyn != NULL) {
2355 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2356 iov[num_iov].iov_len = state->smb2.dyn_len;
2360 reqlen = sizeof(state->smb2.hdr);
2361 reqlen += state->smb2.fixed_len;
2362 reqlen += state->smb2.dyn_len;
2364 if (i < num_reqs-1) {
2365 if ((reqlen % 8) > 0) {
2366 uint8_t pad = 8 - (reqlen % 8);
2367 iov[num_iov].iov_base = state->smb2.pad;
2368 iov[num_iov].iov_len = pad;
2372 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2376 if (state->session) {
2377 should_sign = state->session->smb2.should_sign;
2378 if (state->session->smb2.channel_setup) {
2386 status = smb2_signing_sign_pdu(state->session->smb2.signing_key,
2387 &iov[hdr_iov], num_iov - hdr_iov);
2388 if (!NT_STATUS_IS_OK(status)) {
2393 ret = smbXcli_req_set_pending(reqs[i]);
2395 return NT_STATUS_NO_MEMORY;
2399 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2400 _smb_setlen_tcp(state->length_hdr, nbt_len);
2401 iov[0].iov_base = state->length_hdr;
2402 iov[0].iov_len = sizeof(state->length_hdr);
2404 if (state->conn->dispatch_incoming == NULL) {
2405 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2408 subreq = writev_send(state, state->ev, state->conn->outgoing,
2409 state->conn->fd, false, iov, num_iov);
2410 if (subreq == NULL) {
2411 return NT_STATUS_NO_MEMORY;
2413 tevent_req_set_callback(subreq, smb2cli_writev_done, reqs[0]);
2414 return NT_STATUS_OK;
2417 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2419 struct smbXcli_req_state *state =
2420 tevent_req_data(req,
2421 struct smbXcli_req_state);
2423 state->smb2.credit_charge = charge;
2426 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2427 struct tevent_context *ev,
2428 struct smbXcli_conn *conn,
2430 uint32_t additional_flags,
2431 uint32_t clear_flags,
2432 uint32_t timeout_msec,
2435 struct smbXcli_session *session,
2436 const uint8_t *fixed,
2441 struct tevent_req *req;
2444 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2445 additional_flags, clear_flags,
2448 fixed, fixed_len, dyn, dyn_len);
2452 if (!tevent_req_is_in_progress(req)) {
2453 return tevent_req_post(req, ev);
2455 status = smb2cli_req_compound_submit(&req, 1);
2456 if (tevent_req_nterror(req, status)) {
2457 return tevent_req_post(req, ev);
2462 static void smb2cli_writev_done(struct tevent_req *subreq)
2464 struct tevent_req *req =
2465 tevent_req_callback_data(subreq,
2467 struct smbXcli_req_state *state =
2468 tevent_req_data(req,
2469 struct smbXcli_req_state);
2473 nwritten = writev_recv(subreq, &err);
2474 TALLOC_FREE(subreq);
2475 if (nwritten == -1) {
2476 /* here, we need to notify all pending requests */
2477 NTSTATUS status = map_nt_error_from_unix_common(err);
2478 smbXcli_conn_disconnect(state->conn, status);
2483 static NTSTATUS smb2cli_inbuf_parse_compound(uint8_t *buf, TALLOC_CTX *mem_ctx,
2484 struct iovec **piov, int *pnum_iov)
2494 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2496 return NT_STATUS_NO_MEMORY;
2499 buflen = smb_len_tcp(buf);
2501 first_hdr = buf + NBT_HDR_SIZE;
2503 while (taken < buflen) {
2504 size_t len = buflen - taken;
2505 uint8_t *hdr = first_hdr + taken;
2508 size_t next_command_ofs;
2510 struct iovec *iov_tmp;
2513 * We need the header plus the body length field
2516 if (len < SMB2_HDR_BODY + 2) {
2517 DEBUG(10, ("%d bytes left, expected at least %d\n",
2518 (int)len, SMB2_HDR_BODY));
2521 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2522 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2526 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2527 DEBUG(10, ("Got HDR len %d, expected %d\n",
2528 SVAL(hdr, 4), SMB2_HDR_BODY));
2533 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2534 body_size = SVAL(hdr, SMB2_HDR_BODY);
2536 if (next_command_ofs != 0) {
2537 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2540 if (next_command_ofs > full_size) {
2543 full_size = next_command_ofs;
2545 if (body_size < 2) {
2548 body_size &= 0xfffe;
2550 if (body_size > (full_size - SMB2_HDR_BODY)) {
2554 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2556 if (iov_tmp == NULL) {
2558 return NT_STATUS_NO_MEMORY;
2561 cur = &iov[num_iov];
2564 cur[0].iov_base = hdr;
2565 cur[0].iov_len = SMB2_HDR_BODY;
2566 cur[1].iov_base = hdr + SMB2_HDR_BODY;
2567 cur[1].iov_len = body_size;
2568 cur[2].iov_base = hdr + SMB2_HDR_BODY + body_size;
2569 cur[2].iov_len = full_size - (SMB2_HDR_BODY + body_size);
2575 *pnum_iov = num_iov;
2576 return NT_STATUS_OK;
2580 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2583 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
2586 size_t num_pending = talloc_array_length(conn->pending);
2589 for (i=0; i<num_pending; i++) {
2590 struct tevent_req *req = conn->pending[i];
2591 struct smbXcli_req_state *state =
2592 tevent_req_data(req,
2593 struct smbXcli_req_state);
2595 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
2602 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2603 TALLOC_CTX *tmp_mem,
2606 struct tevent_req *req;
2607 struct smbXcli_req_state *state = NULL;
2612 struct smbXcli_session *last_session = NULL;
2614 status = smb2cli_inbuf_parse_compound(inbuf, tmp_mem,
2616 if (!NT_STATUS_IS_OK(status)) {
2620 for (i=0; i<num_iov; i+=3) {
2621 uint8_t *inbuf_ref = NULL;
2622 struct iovec *cur = &iov[i];
2623 uint8_t *inhdr = (uint8_t *)cur[0].iov_base;
2624 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
2625 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
2626 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
2627 uint16_t req_opcode;
2629 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
2630 uint32_t new_credits;
2631 struct smbXcli_session *session = NULL;
2632 const DATA_BLOB *signing_key = NULL;
2633 bool should_sign = false;
2635 new_credits = conn->smb2.cur_credits;
2636 new_credits += credits;
2637 if (new_credits > UINT16_MAX) {
2638 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2640 conn->smb2.cur_credits += credits;
2642 req = smb2cli_conn_find_pending(conn, mid);
2644 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2646 state = tevent_req_data(req, struct smbXcli_req_state);
2648 state->smb2.got_async = false;
2650 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2651 if (opcode != req_opcode) {
2652 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2654 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2656 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
2657 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2660 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
2661 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
2662 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
2663 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
2666 * async interim responses are not signed,
2667 * even if the SMB2_HDR_FLAG_SIGNED flag
2670 req_flags |= SMB2_HDR_FLAG_ASYNC;
2671 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
2672 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
2674 if (state->smb2.notify_async) {
2675 state->smb2.got_async = true;
2676 tevent_req_defer_callback(req, state->ev);
2677 tevent_req_notify_callback(req);
2682 session = state->session;
2683 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
2684 session = last_session;
2686 last_session = session;
2689 should_sign = session->smb2.should_sign;
2690 if (session->smb2.channel_setup) {
2696 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
2697 return NT_STATUS_ACCESS_DENIED;
2701 if (flags & SMB2_HDR_FLAG_SIGNED) {
2702 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
2704 if (session == NULL) {
2705 struct smbXcli_session *s;
2707 s = state->conn->sessions;
2708 for (; s; s = s->next) {
2709 if (s->smb2.session_id != uid) {
2718 if (session == NULL) {
2719 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2722 last_session = session;
2723 signing_key = &session->smb2.signing_key;
2726 if ((opcode == SMB2_OP_SESSSETUP) &&
2727 NT_STATUS_IS_OK(status)) {
2729 * the caller has to check the signing
2730 * as only the caller knows the correct
2736 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
2738 * if the server returns NT_STATUS_USER_SESSION_DELETED
2739 * the response is not signed and we should
2740 * propagate the NT_STATUS_USER_SESSION_DELETED
2741 * status to the caller.
2748 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
2749 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
2750 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2752 * if the server returns
2753 * NT_STATUS_NETWORK_NAME_DELETED
2754 * NT_STATUS_FILE_CLOSED
2755 * NT_STATUS_INVALID_PARAMETER
2756 * the response might not be signed
2757 * as this happens before the signing checks.
2759 * If server echos the signature (or all zeros)
2760 * we should report the status from the server
2766 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
2767 state->smb2.hdr+SMB2_HDR_SIGNATURE,
2770 state->smb2.signing_skipped = true;
2776 static const uint8_t zeros[16];
2778 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
2782 state->smb2.signing_skipped = true;
2789 status = smb2_signing_check_pdu(*signing_key, cur, 3);
2790 if (!NT_STATUS_IS_OK(status)) {
2792 * If the signing check fails, we disconnect
2799 smbXcli_req_unset_pending(req);
2802 * There might be more than one response
2803 * we need to defer the notifications
2805 if ((num_iov == 4) && (talloc_array_length(conn->pending) == 0)) {
2810 tevent_req_defer_callback(req, state->ev);
2814 * Note: here we use talloc_reference() in a way
2815 * that does not expose it to the caller.
2817 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
2818 if (tevent_req_nomem(inbuf_ref, req)) {
2822 /* copy the related buffers */
2823 state->smb2.recv_iov[0] = cur[0];
2824 state->smb2.recv_iov[1] = cur[1];
2825 state->smb2.recv_iov[2] = cur[2];
2827 tevent_req_done(req);
2831 return NT_STATUS_RETRY;
2834 return NT_STATUS_OK;
2837 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
2838 struct iovec **piov,
2839 const struct smb2cli_req_expected_response *expected,
2840 size_t num_expected)
2842 struct smbXcli_req_state *state =
2843 tevent_req_data(req,
2844 struct smbXcli_req_state);
2847 bool found_status = false;
2848 bool found_size = false;
2855 if (state->smb2.got_async) {
2856 return STATUS_PENDING;
2859 if (tevent_req_is_nterror(req, &status)) {
2860 for (i=0; i < num_expected; i++) {
2861 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2862 found_status = true;
2868 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2874 if (num_expected == 0) {
2875 found_status = true;
2879 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
2880 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
2882 for (i=0; i < num_expected; i++) {
2883 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2887 found_status = true;
2888 if (expected[i].body_size == 0) {
2893 if (expected[i].body_size == body_size) {
2899 if (!found_status) {
2903 if (state->smb2.signing_skipped) {
2904 if (num_expected > 0) {
2905 return NT_STATUS_ACCESS_DENIED;
2907 if (!NT_STATUS_IS_ERR(status)) {
2908 return NT_STATUS_ACCESS_DENIED;
2913 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2917 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
2923 static const struct {
2924 enum protocol_types proto;
2925 const char *smb1_name;
2926 } smb1cli_prots[] = {
2927 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
2928 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
2929 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
2930 {PROTOCOL_LANMAN1, "LANMAN1.0"},
2931 {PROTOCOL_LANMAN2, "LM1.2X002"},
2932 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
2933 {PROTOCOL_LANMAN2, "LANMAN2.1"},
2934 {PROTOCOL_LANMAN2, "Samba"},
2935 {PROTOCOL_NT1, "NT LANMAN 1.0"},
2936 {PROTOCOL_NT1, "NT LM 0.12"},
2937 {PROTOCOL_SMB2_02, "SMB 2.002"},
2938 {PROTOCOL_SMB2_10, "SMB 2.???"},
2941 static const struct {
2942 enum protocol_types proto;
2943 uint16_t smb2_dialect;
2944 } smb2cli_prots[] = {
2945 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
2946 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
2947 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
2950 struct smbXcli_negprot_state {
2951 struct smbXcli_conn *conn;
2952 struct tevent_context *ev;
2953 uint32_t timeout_msec;
2954 enum protocol_types min_protocol;
2955 enum protocol_types max_protocol;
2959 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
2963 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
2964 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
2965 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
2966 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
2967 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
2968 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
2972 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
2973 struct tevent_context *ev,
2974 struct smbXcli_conn *conn,
2975 uint32_t timeout_msec,
2976 enum protocol_types min_protocol,
2977 enum protocol_types max_protocol)
2979 struct tevent_req *req, *subreq;
2980 struct smbXcli_negprot_state *state;
2982 req = tevent_req_create(mem_ctx, &state,
2983 struct smbXcli_negprot_state);
2989 state->timeout_msec = timeout_msec;
2990 state->min_protocol = min_protocol;
2991 state->max_protocol = max_protocol;
2993 if (min_protocol == PROTOCOL_NONE) {
2994 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
2995 return tevent_req_post(req, ev);
2998 if (max_protocol == PROTOCOL_NONE) {
2999 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3000 return tevent_req_post(req, ev);
3003 if (min_protocol > max_protocol) {
3004 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3005 return tevent_req_post(req, ev);
3008 if ((min_protocol < PROTOCOL_SMB2_02) &&
3009 (max_protocol < PROTOCOL_SMB2_02)) {
3013 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3015 subreq = smbXcli_negprot_smb1_subreq(state);
3016 if (tevent_req_nomem(subreq, req)) {
3017 return tevent_req_post(req, ev);
3019 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3023 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3024 (max_protocol >= PROTOCOL_SMB2_02)) {
3028 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3030 subreq = smbXcli_negprot_smb2_subreq(state);
3031 if (tevent_req_nomem(subreq, req)) {
3032 return tevent_req_post(req, ev);
3034 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3039 * We send an SMB1 negprot with the SMB2 dialects
3040 * and expect a SMB1 or a SMB2 response.
3042 * smbXcli_negprot_dispatch_incoming() will fix the
3043 * callback to match protocol of the response.
3045 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3047 subreq = smbXcli_negprot_smb1_subreq(state);
3048 if (tevent_req_nomem(subreq, req)) {
3049 return tevent_req_post(req, ev);
3051 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3055 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3057 struct tevent_req *req =
3058 tevent_req_callback_data(subreq,
3063 * we just want the low level error
3065 status = tevent_req_simple_recv_ntstatus(subreq);
3066 TALLOC_FREE(subreq);
3067 if (tevent_req_nterror(req, status)) {
3071 /* this should never happen */
3072 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3075 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3078 DATA_BLOB bytes = data_blob_null;
3082 /* setup the protocol strings */
3083 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3087 if (smb1cli_prots[i].proto < state->min_protocol) {
3091 if (smb1cli_prots[i].proto > state->max_protocol) {
3095 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3101 * We now it is already ascii and
3102 * we want NULL termination.
3104 ok = data_blob_append(state, &bytes,
3105 smb1cli_prots[i].smb1_name,
3106 strlen(smb1cli_prots[i].smb1_name)+1);
3112 smb1cli_req_flags(state->max_protocol,
3113 state->conn->smb1.client.capabilities,
3118 return smb1cli_req_send(state, state->ev, state->conn,
3122 state->timeout_msec,
3123 0xFFFE, 0, 0, /* pid, tid, uid */
3124 0, NULL, /* wct, vwv */
3125 bytes.length, bytes.data);
3128 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3130 struct tevent_req *req =
3131 tevent_req_callback_data(subreq,
3133 struct smbXcli_negprot_state *state =
3134 tevent_req_data(req,
3135 struct smbXcli_negprot_state);
3136 struct smbXcli_conn *conn = state->conn;
3137 struct iovec *recv_iov = NULL;
3146 size_t num_prots = 0;
3148 uint32_t client_capabilities = conn->smb1.client.capabilities;
3149 uint32_t both_capabilities;
3150 uint32_t server_capabilities = 0;
3151 uint32_t capabilities;
3152 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3153 uint32_t server_max_xmit = 0;
3155 uint32_t server_max_mux = 0;
3156 uint16_t server_security_mode = 0;
3157 uint32_t server_session_key = 0;
3158 bool server_readbraw = false;
3159 bool server_writebraw = false;
3160 bool server_lockread = false;
3161 bool server_writeunlock = false;
3162 struct GUID server_guid = GUID_zero();
3163 DATA_BLOB server_gss_blob = data_blob_null;
3164 uint8_t server_challenge[8];
3165 char *server_workgroup = NULL;
3166 char *server_name = NULL;
3167 int server_time_zone = 0;
3168 NTTIME server_system_time = 0;
3169 static const struct smb1cli_req_expected_response expected[] = {
3171 .status = NT_STATUS_OK,
3172 .wct = 0x11, /* NT1 */
3175 .status = NT_STATUS_OK,
3176 .wct = 0x0D, /* LM */
3179 .status = NT_STATUS_OK,
3180 .wct = 0x01, /* CORE */
3184 ZERO_STRUCT(server_challenge);
3186 status = smb1cli_req_recv(subreq, state,
3191 NULL, /* pvwv_offset */
3194 NULL, /* pbytes_offset */
3196 expected, ARRAY_SIZE(expected));
3197 TALLOC_FREE(subreq);
3198 if (tevent_req_nterror(req, status)) {
3202 flags = CVAL(inhdr, HDR_FLG);
3204 protnum = SVAL(vwv, 0);
3206 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3207 if (smb1cli_prots[i].proto < state->min_protocol) {
3211 if (smb1cli_prots[i].proto > state->max_protocol) {
3215 if (protnum != num_prots) {
3220 conn->protocol = smb1cli_prots[i].proto;
3224 if (conn->protocol == PROTOCOL_NONE) {
3225 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3229 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3230 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3231 "and the selected protocol level doesn't support it.\n"));
3232 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3236 if (flags & FLAG_SUPPORT_LOCKREAD) {
3237 server_lockread = true;
3238 server_writeunlock = true;
3241 if (conn->protocol >= PROTOCOL_NT1) {
3242 const char *client_signing = NULL;
3243 bool server_mandatory = false;
3244 bool server_allowed = false;
3245 const char *server_signing = NULL;
3250 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3255 server_security_mode = CVAL(vwv + 1, 0);
3256 server_max_mux = SVAL(vwv + 1, 1);
3257 server_max_xmit = IVAL(vwv + 3, 1);
3258 server_session_key = IVAL(vwv + 7, 1);
3259 server_time_zone = SVALS(vwv + 15, 1);
3260 server_time_zone *= 60;
3261 /* this time arrives in real GMT */
3262 server_system_time = BVAL(vwv + 11, 1);
3263 server_capabilities = IVAL(vwv + 9, 1);
3265 key_len = CVAL(vwv + 16, 1);
3267 if (server_capabilities & CAP_RAW_MODE) {
3268 server_readbraw = true;
3269 server_writebraw = true;
3271 if (server_capabilities & CAP_LOCK_AND_READ) {
3272 server_lockread = true;
3275 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3276 DATA_BLOB blob1, blob2;
3278 if (num_bytes < 16) {
3279 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3283 blob1 = data_blob_const(bytes, 16);
3284 status = GUID_from_data_blob(&blob1, &server_guid);
3285 if (tevent_req_nterror(req, status)) {
3289 blob1 = data_blob_const(bytes+16, num_bytes-16);
3290 blob2 = data_blob_dup_talloc(state, blob1);
3291 if (blob1.length > 0 &&
3292 tevent_req_nomem(blob2.data, req)) {
3295 server_gss_blob = blob2;
3297 DATA_BLOB blob1, blob2;
3299 if (num_bytes < key_len) {
3300 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3304 if (key_len != 0 && key_len != 8) {
3305 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3310 memcpy(server_challenge, bytes, 8);
3313 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3314 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3315 if (blob1.length > 0) {
3318 len = utf16_len_n(blob1.data,
3322 ok = convert_string_talloc(state,
3330 status = map_nt_error_from_unix_common(errno);
3331 tevent_req_nterror(req, status);
3336 blob2.data += blob1.length;
3337 blob2.length -= blob1.length;
3338 if (blob2.length > 0) {
3341 len = utf16_len_n(blob1.data,
3345 ok = convert_string_talloc(state,
3353 status = map_nt_error_from_unix_common(errno);
3354 tevent_req_nterror(req, status);
3360 client_signing = "disabled";
3361 if (conn->allow_signing) {
3362 client_signing = "allowed";
3364 if (conn->mandatory_signing) {
3365 client_signing = "required";
3368 server_signing = "not supported";
3369 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3370 server_signing = "supported";
3371 server_allowed = true;
3373 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3374 server_signing = "required";
3375 server_mandatory = true;
3378 ok = smb_signing_set_negotiated(conn->smb1.signing,
3382 DEBUG(1,("cli_negprot: SMB signing is required, "
3383 "but client[%s] and server[%s] mismatch\n",
3384 client_signing, server_signing));
3385 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3389 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3395 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3399 server_security_mode = SVAL(vwv + 1, 0);
3400 server_max_xmit = SVAL(vwv + 2, 0);
3401 server_max_mux = SVAL(vwv + 3, 0);
3402 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3403 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3404 server_session_key = IVAL(vwv + 6, 0);
3405 server_time_zone = SVALS(vwv + 10, 0);
3406 server_time_zone *= 60;
3407 /* this time is converted to GMT by make_unix_date */
3408 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3409 unix_to_nt_time(&server_system_time, t);
3410 key_len = SVAL(vwv + 11, 0);
3412 if (num_bytes < key_len) {
3413 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3417 if (key_len != 0 && key_len != 8) {
3418 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3423 memcpy(server_challenge, bytes, 8);
3426 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3427 if (blob1.length > 0) {
3431 len = utf16_len_n(blob1.data,
3435 ok = convert_string_talloc(state,
3443 status = map_nt_error_from_unix_common(errno);
3444 tevent_req_nterror(req, status);
3450 /* the old core protocol */
3451 server_time_zone = get_time_zone(time(NULL));
3452 server_max_xmit = 1024;
3456 if (server_max_xmit < 1024) {
3457 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3461 if (server_max_mux < 1) {
3462 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3467 * Now calculate the negotiated capabilities
3468 * based on the mask for:
3469 * - client only flags
3470 * - flags used in both directions
3471 * - server only flags
3473 both_capabilities = client_capabilities & server_capabilities;
3474 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3475 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3476 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3478 max_xmit = MIN(client_max_xmit, server_max_xmit);
3480 conn->smb1.server.capabilities = server_capabilities;
3481 conn->smb1.capabilities = capabilities;
3483 conn->smb1.server.max_xmit = server_max_xmit;
3484 conn->smb1.max_xmit = max_xmit;
3486 conn->smb1.server.max_mux = server_max_mux;
3488 conn->smb1.server.security_mode = server_security_mode;
3490 conn->smb1.server.readbraw = server_readbraw;
3491 conn->smb1.server.writebraw = server_writebraw;
3492 conn->smb1.server.lockread = server_lockread;
3493 conn->smb1.server.writeunlock = server_writeunlock;
3495 conn->smb1.server.session_key = server_session_key;
3497 talloc_steal(conn, server_gss_blob.data);
3498 conn->smb1.server.gss_blob = server_gss_blob;
3499 conn->smb1.server.guid = server_guid;
3500 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3501 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3502 conn->smb1.server.name = talloc_move(conn, &server_name);
3504 conn->smb1.server.time_zone = server_time_zone;
3505 conn->smb1.server.system_time = server_system_time;
3507 tevent_req_done(req);
3510 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
3514 uint16_t dialect_count = 0;
3516 buf = state->smb2.dyn;
3517 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3518 if (smb2cli_prots[i].proto < state->min_protocol) {
3522 if (smb2cli_prots[i].proto > state->max_protocol) {
3526 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
3530 buf = state->smb2.fixed;
3532 SSVAL(buf, 2, dialect_count);
3533 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
3534 SSVAL(buf, 6, 0); /* Reserved */
3535 SSVAL(buf, 8, 0); /* Capabilities */
3536 if (state->max_protocol >= PROTOCOL_SMB2_10) {
3540 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
3542 if (!NT_STATUS_IS_OK(status)) {
3545 memcpy(buf+12, blob.data, 16); /* ClientGuid */
3547 memset(buf+12, 0, 16); /* ClientGuid */
3549 SBVAL(buf, 28, 0); /* ClientStartTime */
3551 return smb2cli_req_send(state, state->ev,
3552 state->conn, SMB2_OP_NEGPROT,
3554 state->timeout_msec,
3555 0xFEFF, 0, NULL, /* pid, tid, session */
3556 state->smb2.fixed, sizeof(state->smb2.fixed),
3557 state->smb2.dyn, dialect_count*2);
3560 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
3562 struct tevent_req *req =
3563 tevent_req_callback_data(subreq,
3565 struct smbXcli_negprot_state *state =
3566 tevent_req_data(req,
3567 struct smbXcli_negprot_state);
3568 struct smbXcli_conn *conn = state->conn;
3569 size_t security_offset, security_length;
3575 uint16_t dialect_revision;
3576 static const struct smb2cli_req_expected_response expected[] = {
3578 .status = NT_STATUS_OK,
3583 status = smb2cli_req_recv(subreq, state, &iov,
3584 expected, ARRAY_SIZE(expected));
3585 TALLOC_FREE(subreq);
3586 if (tevent_req_nterror(req, status)) {
3590 body = (uint8_t *)iov[1].iov_base;
3592 dialect_revision = SVAL(body, 4);
3594 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3595 if (smb2cli_prots[i].proto < state->min_protocol) {
3599 if (smb2cli_prots[i].proto > state->max_protocol) {
3603 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
3607 conn->protocol = smb2cli_prots[i].proto;
3611 if (conn->protocol == PROTOCOL_NONE) {
3612 if (state->min_protocol >= PROTOCOL_SMB2_02) {
3613 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3617 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
3618 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3622 /* make sure we do not loop forever */
3623 state->min_protocol = PROTOCOL_SMB2_02;
3626 * send a SMB2 negprot, in order to negotiate
3627 * the SMB2 dialect. This needs to use the
3630 state->conn->smb2.mid = 1;
3631 subreq = smbXcli_negprot_smb2_subreq(state);
3632 if (tevent_req_nomem(subreq, req)) {
3635 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3639 conn->smb2.server.security_mode = SVAL(body, 2);
3641 blob = data_blob_const(body + 8, 16);
3642 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
3643 if (tevent_req_nterror(req, status)) {
3647 conn->smb2.server.capabilities = IVAL(body, 24);
3648 conn->smb2.server.max_trans_size= IVAL(body, 28);
3649 conn->smb2.server.max_read_size = IVAL(body, 32);
3650 conn->smb2.server.max_write_size= IVAL(body, 36);
3651 conn->smb2.server.system_time = BVAL(body, 40);
3652 conn->smb2.server.start_time = BVAL(body, 48);
3654 security_offset = SVAL(body, 56);
3655 security_length = SVAL(body, 58);
3657 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
3658 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3662 if (security_length > iov[2].iov_len) {
3663 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3667 conn->smb2.server.gss_blob = data_blob_talloc(conn,
3670 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
3674 tevent_req_done(req);
3677 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3678 TALLOC_CTX *tmp_mem,
3681 size_t num_pending = talloc_array_length(conn->pending);
3682 struct tevent_req *subreq;
3683 struct smbXcli_req_state *substate;
3684 struct tevent_req *req;
3685 struct smbXcli_negprot_state *state;
3686 uint32_t protocol_magic = IVAL(inbuf, 4);
3688 if (num_pending != 1) {
3689 return NT_STATUS_INTERNAL_ERROR;
3692 subreq = conn->pending[0];
3693 substate = tevent_req_data(subreq, struct smbXcli_req_state);
3694 req = tevent_req_callback_data(subreq, struct tevent_req);
3695 state = tevent_req_data(req, struct smbXcli_negprot_state);
3697 switch (protocol_magic) {
3699 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3700 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3701 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3704 if (substate->smb2.recv_iov == NULL) {
3706 * For the SMB1 negprot we have move it.
3708 substate->smb2.recv_iov = substate->smb1.recv_iov;
3709 substate->smb1.recv_iov = NULL;
3712 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3713 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3714 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3717 DEBUG(10, ("Got non-SMB PDU\n"));
3718 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3721 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
3723 return tevent_req_simple_recv_ntstatus(req);
3726 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
3727 uint32_t timeout_msec,
3728 enum protocol_types min_protocol,
3729 enum protocol_types max_protocol)
3731 TALLOC_CTX *frame = talloc_stackframe();
3732 struct tevent_context *ev;
3733 struct tevent_req *req;
3734 NTSTATUS status = NT_STATUS_NO_MEMORY;
3737 if (smbXcli_conn_has_async_calls(conn)) {
3739 * Can't use sync call while an async call is in flight
3741 status = NT_STATUS_INVALID_PARAMETER_MIX;
3744 ev = tevent_context_init(frame);
3748 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
3749 min_protocol, max_protocol);
3753 ok = tevent_req_poll(req, ev);
3755 status = map_nt_error_from_unix_common(errno);
3758 status = smbXcli_negprot_recv(req);
3764 static int smbXcli_session_destructor(struct smbXcli_session *session)
3766 if (session->conn == NULL) {
3770 DLIST_REMOVE(session->conn->sessions, session);
3774 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
3775 struct smbXcli_conn *conn)
3777 struct smbXcli_session *session;
3779 session = talloc_zero(mem_ctx, struct smbXcli_session);
3780 if (session == NULL) {
3783 talloc_set_destructor(session, smbXcli_session_destructor);
3785 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
3786 session->conn = conn;
3791 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
3793 struct smbXcli_conn *conn = session->conn;
3794 uint8_t security_mode = 0;
3797 return security_mode;
3800 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
3801 if (conn->mandatory_signing) {
3802 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
3805 return security_mode;
3808 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
3810 return session->smb2.session_id;
3813 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
3814 uint64_t session_id,
3815 uint16_t session_flags)
3817 session->smb2.session_id = session_id;
3818 session->smb2.session_flags = session_flags;
3821 NTSTATUS smb2cli_session_update_session_key(struct smbXcli_session *session,
3822 const DATA_BLOB session_key,
3823 const struct iovec *recv_iov)
3825 struct smbXcli_conn *conn = session->conn;
3826 uint16_t no_sign_flags;
3827 DATA_BLOB signing_key;
3831 return NT_STATUS_INVALID_PARAMETER_MIX;
3834 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
3836 if (session->smb2.session_flags & no_sign_flags) {
3837 session->smb2.should_sign = false;
3838 return NT_STATUS_OK;
3841 if (session->smb2.signing_key.length > 0) {
3842 signing_key = session->smb2.signing_key;
3844 signing_key = session_key;
3846 if (session->smb2.channel_setup) {
3847 signing_key = session_key;
3850 status = smb2_signing_check_pdu(signing_key, recv_iov, 3);
3851 if (!NT_STATUS_IS_OK(status)) {
3855 if (!session->smb2.channel_setup) {
3856 session->smb2.session_key = data_blob_dup_talloc(session,
3858 if (session->smb2.session_key.data == NULL) {
3859 return NT_STATUS_NO_MEMORY;
3863 if (session->smb2.channel_setup) {
3864 data_blob_free(&session->smb2.signing_key);
3865 session->smb2.channel_setup = false;
3868 if (session->smb2.signing_key.length > 0) {
3869 return NT_STATUS_OK;
3872 session->smb2.signing_key = data_blob_dup_talloc(session, signing_key);
3873 if (session->smb2.signing_key.data == NULL) {
3874 return NT_STATUS_NO_MEMORY;
3877 session->smb2.should_sign = false;
3879 if (conn->desire_signing) {
3880 session->smb2.should_sign = true;
3883 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
3884 session->smb2.should_sign = true;
3887 return NT_STATUS_OK;
3890 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
3891 struct smbXcli_session *session1,
3892 struct smbXcli_conn *conn,
3893 struct smbXcli_session **_session2)
3895 struct smbXcli_session *session2;
3896 uint16_t no_sign_flags;
3898 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
3900 if (session1->smb2.session_flags & no_sign_flags) {
3901 return NT_STATUS_INVALID_PARAMETER_MIX;
3904 if (session1->smb2.session_key.length == 0) {
3905 return NT_STATUS_INVALID_PARAMETER_MIX;
3908 if (session1->smb2.signing_key.length == 0) {
3909 return NT_STATUS_INVALID_PARAMETER_MIX;
3913 return NT_STATUS_INVALID_PARAMETER_MIX;
3916 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
3917 if (session2 == NULL) {
3918 return NT_STATUS_NO_MEMORY;
3920 session2->smb2.session_id = session1->smb2.session_id;
3921 session2->smb2.session_flags = session1->smb2.session_flags;
3923 session2->smb2.session_key = data_blob_dup_talloc(session2,
3924 session1->smb2.session_key);
3925 if (session2->smb2.session_key.data == NULL) {
3926 return NT_STATUS_NO_MEMORY;
3929 session2->smb2.signing_key = data_blob_dup_talloc(session2,
3930 session1->smb2.signing_key);
3931 if (session2->smb2.signing_key.data == NULL) {
3932 return NT_STATUS_NO_MEMORY;
3935 session2->smb2.should_sign = session1->smb2.should_sign;
3936 session2->smb2.channel_setup = true;
3938 talloc_set_destructor(session2, smbXcli_session_destructor);
3939 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
3940 session2->conn = conn;
3942 *_session2 = session2;
3943 return NT_STATUS_OK;