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;
199 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
202 * NT_STATUS_OK, means we do not notify the callers
204 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
206 while (conn->sessions) {
207 conn->sessions->conn = NULL;
208 DLIST_REMOVE(conn->sessions, conn->sessions);
211 if (conn->smb1.trans_enc) {
212 common_free_encryption_state(&conn->smb1.trans_enc);
218 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
220 const char *remote_name,
221 enum smb_signing_setting signing_state,
222 uint32_t smb1_capabilities,
223 struct GUID *client_guid)
225 struct smbXcli_conn *conn = NULL;
227 struct sockaddr *sa = NULL;
231 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
236 conn->remote_name = talloc_strdup(conn, remote_name);
237 if (conn->remote_name == NULL) {
243 ss = (void *)&conn->local_ss;
244 sa = (struct sockaddr *)ss;
245 sa_length = sizeof(conn->local_ss);
246 ret = getsockname(fd, sa, &sa_length);
250 ss = (void *)&conn->remote_ss;
251 sa = (struct sockaddr *)ss;
252 sa_length = sizeof(conn->remote_ss);
253 ret = getpeername(fd, sa, &sa_length);
258 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
259 if (conn->outgoing == NULL) {
262 conn->pending = NULL;
264 conn->protocol = PROTOCOL_NONE;
266 switch (signing_state) {
267 case SMB_SIGNING_OFF:
269 conn->allow_signing = false;
270 conn->desire_signing = false;
271 conn->mandatory_signing = false;
273 case SMB_SIGNING_DEFAULT:
274 case SMB_SIGNING_IF_REQUIRED:
275 /* if the server requires it */
276 conn->allow_signing = true;
277 conn->desire_signing = false;
278 conn->mandatory_signing = false;
280 case SMB_SIGNING_REQUIRED:
282 conn->allow_signing = true;
283 conn->desire_signing = true;
284 conn->mandatory_signing = true;
288 conn->smb1.client.capabilities = smb1_capabilities;
289 conn->smb1.client.max_xmit = UINT16_MAX;
291 conn->smb1.capabilities = conn->smb1.client.capabilities;
292 conn->smb1.max_xmit = 1024;
296 /* initialise signing */
297 conn->smb1.signing = smb_signing_init(conn,
299 conn->desire_signing,
300 conn->mandatory_signing);
301 if (!conn->smb1.signing) {
305 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
306 if (conn->mandatory_signing) {
307 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
310 conn->smb2.client.guid = *client_guid;
313 conn->smb2.cur_credits = 1;
314 conn->smb2.max_credits = 0;
316 talloc_set_destructor(conn, smbXcli_conn_destructor);
324 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
330 if (conn->fd == -1) {
337 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
339 return conn->protocol;
342 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
344 if (conn->protocol >= PROTOCOL_SMB2_02) {
348 if (conn->smb1.capabilities & CAP_UNICODE) {
355 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
357 set_socket_options(conn->fd, options);
360 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
362 return &conn->local_ss;
365 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
367 return &conn->remote_ss;
370 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
372 return conn->remote_name;
375 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
377 if (conn->protocol >= PROTOCOL_SMB2_02) {
384 return conn->smb1.server.max_mux;
387 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
389 if (conn->protocol >= PROTOCOL_SMB2_02) {
390 return conn->smb2.server.system_time;
393 return conn->smb1.server.system_time;
396 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
398 if (conn->protocol >= PROTOCOL_SMB2_02) {
399 return &conn->smb2.server.gss_blob;
402 return &conn->smb1.server.gss_blob;
405 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
407 if (conn->protocol >= PROTOCOL_SMB2_02) {
408 return &conn->smb2.server.guid;
411 return &conn->smb1.server.guid;
414 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
416 return conn->smb1.capabilities;
419 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
421 return conn->smb1.max_xmit;
424 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
426 return conn->smb1.server.session_key;
429 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
431 return conn->smb1.server.challenge;
434 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
436 return conn->smb1.server.security_mode;
439 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
441 return conn->smb1.server.time_zone;
444 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
445 const DATA_BLOB user_session_key,
446 const DATA_BLOB response)
448 return smb_signing_activate(conn->smb1.signing,
453 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
454 const uint8_t *buf, uint32_t seqnum)
456 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
459 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
461 return smb_signing_is_active(conn->smb1.signing);
464 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
465 struct smb_trans_enc_state *es)
467 /* Replace the old state, if any. */
468 if (conn->smb1.trans_enc) {
469 common_free_encryption_state(&conn->smb1.trans_enc);
471 conn->smb1.trans_enc = es;
474 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
476 return common_encryption_on(conn->smb1.trans_enc);
480 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
482 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
483 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
485 if (NT_STATUS_IS_OK(status)) {
489 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
493 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
497 * Is the SMB command able to hold an AND_X successor
498 * @param[in] cmd The SMB command in question
499 * @retval Can we add a chained request after "cmd"?
501 bool smb1cli_is_andx_req(uint8_t cmd)
521 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
523 size_t num_pending = talloc_array_length(conn->pending);
529 result = conn->smb1.mid++;
530 if ((result == 0) || (result == 0xffff)) {
534 for (i=0; i<num_pending; i++) {
535 if (result == smb1cli_req_mid(conn->pending[i])) {
540 if (i == num_pending) {
546 void smbXcli_req_unset_pending(struct tevent_req *req)
548 struct smbXcli_req_state *state =
550 struct smbXcli_req_state);
551 struct smbXcli_conn *conn = state->conn;
552 size_t num_pending = talloc_array_length(conn->pending);
555 if (state->smb1.mid != 0) {
557 * This is a [nt]trans[2] request which waits
558 * for more than one reply.
563 talloc_set_destructor(req, NULL);
565 if (num_pending == 1) {
567 * The pending read_smb tevent_req is a child of
568 * conn->pending. So if nothing is pending anymore, we need to
569 * delete the socket read fde.
571 TALLOC_FREE(conn->pending);
572 conn->read_smb_req = NULL;
576 for (i=0; i<num_pending; i++) {
577 if (req == conn->pending[i]) {
581 if (i == num_pending) {
583 * Something's seriously broken. Just returning here is the
584 * right thing nevertheless, the point of this routine is to
585 * remove ourselves from conn->pending.
591 * Remove ourselves from the conn->pending array
593 for (; i < (num_pending - 1); i++) {
594 conn->pending[i] = conn->pending[i+1];
598 * No NULL check here, we're shrinking by sizeof(void *), and
599 * talloc_realloc just adjusts the size for this.
601 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
606 static int smbXcli_req_destructor(struct tevent_req *req)
608 struct smbXcli_req_state *state =
610 struct smbXcli_req_state);
613 * Make sure we really remove it from
614 * the pending array on destruction.
617 smbXcli_req_unset_pending(req);
621 static bool smb1cli_req_cancel(struct tevent_req *req);
622 static bool smb2cli_req_cancel(struct tevent_req *req);
624 static bool smbXcli_req_cancel(struct tevent_req *req)
626 struct smbXcli_req_state *state =
628 struct smbXcli_req_state);
630 if (!smbXcli_conn_is_connected(state->conn)) {
634 if (state->conn->protocol == PROTOCOL_NONE) {
638 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
639 return smb2cli_req_cancel(req);
642 return smb1cli_req_cancel(req);
645 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
647 bool smbXcli_req_set_pending(struct tevent_req *req)
649 struct smbXcli_req_state *state =
651 struct smbXcli_req_state);
652 struct smbXcli_conn *conn;
653 struct tevent_req **pending;
658 if (!smbXcli_conn_is_connected(conn)) {
662 num_pending = talloc_array_length(conn->pending);
664 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
666 if (pending == NULL) {
669 pending[num_pending] = req;
670 conn->pending = pending;
671 talloc_set_destructor(req, smbXcli_req_destructor);
672 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
674 if (!smbXcli_conn_receive_next(conn)) {
676 * the caller should notify the current request
678 * And all other pending requests get notified
679 * by smbXcli_conn_disconnect().
681 smbXcli_req_unset_pending(req);
682 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
689 static void smbXcli_conn_received(struct tevent_req *subreq);
691 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
693 size_t num_pending = talloc_array_length(conn->pending);
694 struct tevent_req *req;
695 struct smbXcli_req_state *state;
697 if (conn->read_smb_req != NULL) {
701 if (num_pending == 0) {
702 if (conn->smb2.mid < UINT64_MAX) {
703 /* no more pending requests, so we are done for now */
708 * If there are no more SMB2 requests possible,
709 * because we are out of message ids,
710 * we need to disconnect.
712 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
716 req = conn->pending[0];
717 state = tevent_req_data(req, struct smbXcli_req_state);
720 * We're the first ones, add the read_smb request that waits for the
721 * answer from the server
723 conn->read_smb_req = read_smb_send(conn->pending, state->ev, conn->fd);
724 if (conn->read_smb_req == NULL) {
727 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
731 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
733 if (conn->fd != -1) {
739 * Cancel all pending requests. We do not do a for-loop walking
740 * conn->pending because that array changes in
741 * smbXcli_req_unset_pending.
743 while (talloc_array_length(conn->pending) > 0) {
744 struct tevent_req *req;
745 struct smbXcli_req_state *state;
746 struct tevent_req **chain;
750 req = conn->pending[0];
751 state = tevent_req_data(req, struct smbXcli_req_state);
753 if (state->smb1.chained_requests == NULL) {
755 * We're dead. No point waiting for trans2
760 smbXcli_req_unset_pending(req);
762 if (NT_STATUS_IS_OK(status)) {
763 /* do not notify the callers */
768 * we need to defer the callback, because we may notify
769 * more then one caller.
771 tevent_req_defer_callback(req, state->ev);
772 tevent_req_nterror(req, status);
776 chain = talloc_move(conn, &state->smb1.chained_requests);
777 num_chained = talloc_array_length(chain);
779 for (i=0; i<num_chained; i++) {
781 state = tevent_req_data(req, struct smbXcli_req_state);
784 * We're dead. No point waiting for trans2
789 smbXcli_req_unset_pending(req);
791 if (NT_STATUS_IS_OK(status)) {
792 /* do not notify the callers */
797 * we need to defer the callback, because we may notify
798 * more then one caller.
800 tevent_req_defer_callback(req, state->ev);
801 tevent_req_nterror(req, status);
808 * Fetch a smb request's mid. Only valid after the request has been sent by
809 * smb1cli_req_send().
811 uint16_t smb1cli_req_mid(struct tevent_req *req)
813 struct smbXcli_req_state *state =
815 struct smbXcli_req_state);
817 if (state->smb1.mid != 0) {
818 return state->smb1.mid;
821 return SVAL(state->smb1.hdr, HDR_MID);
824 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
826 struct smbXcli_req_state *state =
828 struct smbXcli_req_state);
830 state->smb1.mid = mid;
833 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
835 struct smbXcli_req_state *state =
837 struct smbXcli_req_state);
839 return state->smb1.seqnum;
842 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
844 struct smbXcli_req_state *state =
846 struct smbXcli_req_state);
848 state->smb1.seqnum = seqnum;
851 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
855 for (i=0; i<count; i++) {
856 result += iov[i].iov_len;
861 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
862 const struct iovec *iov,
865 size_t len = smbXcli_iov_len(iov, count);
870 buf = talloc_array(mem_ctx, uint8_t, len);
875 for (i=0; i<count; i++) {
876 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
877 copied += iov[i].iov_len;
882 static void smb1cli_req_flags(enum protocol_types protocol,
883 uint32_t smb1_capabilities,
885 uint8_t additional_flags,
888 uint16_t additional_flags2,
889 uint16_t clear_flags2,
895 if (protocol >= PROTOCOL_LANMAN1) {
896 flags |= FLAG_CASELESS_PATHNAMES;
897 flags |= FLAG_CANONICAL_PATHNAMES;
900 if (protocol >= PROTOCOL_LANMAN2) {
901 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
902 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
905 if (protocol >= PROTOCOL_NT1) {
906 flags2 |= FLAGS2_IS_LONG_NAME;
908 if (smb1_capabilities & CAP_UNICODE) {
909 flags2 |= FLAGS2_UNICODE_STRINGS;
911 if (smb1_capabilities & CAP_STATUS32) {
912 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
914 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
915 flags2 |= FLAGS2_EXTENDED_SECURITY;
919 flags |= additional_flags;
920 flags &= ~clear_flags;
921 flags2 |= additional_flags2;
922 flags2 &= ~clear_flags2;
928 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
930 static bool smb1cli_req_cancel(struct tevent_req *req)
932 struct smbXcli_req_state *state =
934 struct smbXcli_req_state);
941 struct tevent_req *subreq;
944 flags = CVAL(state->smb1.hdr, HDR_FLG);
945 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
946 pid = SVAL(state->smb1.hdr, HDR_PID);
947 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
948 tid = SVAL(state->smb1.hdr, HDR_TID);
949 uid = SVAL(state->smb1.hdr, HDR_UID);
950 mid = SVAL(state->smb1.hdr, HDR_MID);
952 subreq = smb1cli_req_create(state, state->ev,
960 0, NULL); /* bytes */
961 if (subreq == NULL) {
964 smb1cli_req_set_mid(subreq, mid);
966 status = smb1cli_req_chain_submit(&subreq, 1);
967 if (!NT_STATUS_IS_OK(status)) {
971 smb1cli_req_set_mid(subreq, 0);
973 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
978 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
980 /* we do not care about the result */
984 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
985 struct tevent_context *ev,
986 struct smbXcli_conn *conn,
988 uint8_t additional_flags,
990 uint16_t additional_flags2,
991 uint16_t clear_flags2,
992 uint32_t timeout_msec,
996 uint8_t wct, uint16_t *vwv,
998 struct iovec *bytes_iov)
1000 struct tevent_req *req;
1001 struct smbXcli_req_state *state;
1003 uint16_t flags2 = 0;
1005 if (iov_count > MAX_SMB_IOV) {
1007 * Should not happen :-)
1012 req = tevent_req_create(mem_ctx, &state,
1013 struct smbXcli_req_state);
1020 state->smb1.recv_cmd = 0xFF;
1021 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1022 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1023 if (state->smb1.recv_iov == NULL) {
1028 smb1cli_req_flags(conn->protocol,
1029 conn->smb1.capabilities,
1038 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1039 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1040 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1041 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1042 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1043 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1044 SSVAL(state->smb1.hdr, HDR_TID, tid);
1045 SSVAL(state->smb1.hdr, HDR_PID, pid);
1046 SSVAL(state->smb1.hdr, HDR_UID, uid);
1047 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1048 SSVAL(state->smb1.hdr, HDR_WCT, wct);
1050 state->smb1.vwv = vwv;
1052 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1054 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1055 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1056 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1057 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1058 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1059 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1060 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1061 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1063 if (iov_count != 0) {
1064 memcpy(&state->smb1.iov[4], bytes_iov,
1065 iov_count * sizeof(*bytes_iov));
1067 state->smb1.iov_count = iov_count + 4;
1069 if (timeout_msec > 0) {
1070 struct timeval endtime;
1072 endtime = timeval_current_ofs_msec(timeout_msec);
1073 if (!tevent_req_set_endtime(req, ev, endtime)) {
1078 switch (smb_command) {
1082 state->one_way = true;
1085 state->one_way = true;
1086 state->smb1.one_way_seqnum = true;
1090 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1091 state->one_way = true;
1099 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1100 struct iovec *iov, int iov_count,
1102 bool one_way_seqnum)
1107 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1108 * iovec directly. MD5Update would do that just fine.
1111 if (iov_count < 4) {
1112 return NT_STATUS_INVALID_PARAMETER_MIX;
1114 if (iov[0].iov_len != NBT_HDR_SIZE) {
1115 return NT_STATUS_INVALID_PARAMETER_MIX;
1117 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1118 return NT_STATUS_INVALID_PARAMETER_MIX;
1120 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1121 return NT_STATUS_INVALID_PARAMETER_MIX;
1123 if (iov[3].iov_len != sizeof(uint16_t)) {
1124 return NT_STATUS_INVALID_PARAMETER_MIX;
1127 buf = smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1129 return NT_STATUS_NO_MEMORY;
1132 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1134 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1135 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1138 return NT_STATUS_OK;
1141 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1142 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1143 TALLOC_CTX *tmp_mem,
1146 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1147 struct smbXcli_req_state *state,
1148 struct iovec *iov, int iov_count)
1150 struct tevent_req *subreq;
1154 if (!smbXcli_conn_is_connected(state->conn)) {
1155 return NT_STATUS_CONNECTION_DISCONNECTED;
1158 if (state->conn->protocol > PROTOCOL_NT1) {
1159 return NT_STATUS_REVISION_MISMATCH;
1162 if (iov_count < 4) {
1163 return NT_STATUS_INVALID_PARAMETER_MIX;
1165 if (iov[0].iov_len != NBT_HDR_SIZE) {
1166 return NT_STATUS_INVALID_PARAMETER_MIX;
1168 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1169 return NT_STATUS_INVALID_PARAMETER_MIX;
1171 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1172 return NT_STATUS_INVALID_PARAMETER_MIX;
1174 if (iov[3].iov_len != sizeof(uint16_t)) {
1175 return NT_STATUS_INVALID_PARAMETER_MIX;
1178 if (state->smb1.mid != 0) {
1179 mid = state->smb1.mid;
1181 mid = smb1cli_alloc_mid(state->conn);
1183 SSVAL(iov[1].iov_base, HDR_MID, mid);
1185 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1187 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1188 &state->smb1.seqnum,
1189 state->smb1.one_way_seqnum);
1191 if (!NT_STATUS_IS_OK(status)) {
1196 * If we supported multiple encrytion contexts
1197 * here we'd look up based on tid.
1199 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1200 char *buf, *enc_buf;
1202 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1204 return NT_STATUS_NO_MEMORY;
1206 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1207 (char *)buf, &enc_buf);
1209 if (!NT_STATUS_IS_OK(status)) {
1210 DEBUG(0, ("Error in encrypting client message: %s\n",
1211 nt_errstr(status)));
1214 buf = (char *)talloc_memdup(state, enc_buf,
1215 smb_len_nbt(enc_buf)+4);
1218 return NT_STATUS_NO_MEMORY;
1220 iov[0].iov_base = (void *)buf;
1221 iov[0].iov_len = talloc_get_size(buf);
1225 if (state->conn->dispatch_incoming == NULL) {
1226 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1229 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1231 subreq = writev_send(state, state->ev, state->conn->outgoing,
1232 state->conn->fd, false, iov, iov_count);
1233 if (subreq == NULL) {
1234 return NT_STATUS_NO_MEMORY;
1236 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1237 return NT_STATUS_OK;
1240 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1241 struct tevent_context *ev,
1242 struct smbXcli_conn *conn,
1243 uint8_t smb_command,
1244 uint8_t additional_flags,
1245 uint8_t clear_flags,
1246 uint16_t additional_flags2,
1247 uint16_t clear_flags2,
1248 uint32_t timeout_msec,
1252 uint8_t wct, uint16_t *vwv,
1254 const uint8_t *bytes)
1256 struct tevent_req *req;
1260 iov.iov_base = discard_const_p(void, bytes);
1261 iov.iov_len = num_bytes;
1263 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1264 additional_flags, clear_flags,
1265 additional_flags2, clear_flags2,
1272 if (!tevent_req_is_in_progress(req)) {
1273 return tevent_req_post(req, ev);
1275 status = smb1cli_req_chain_submit(&req, 1);
1276 if (tevent_req_nterror(req, status)) {
1277 return tevent_req_post(req, ev);
1282 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1284 struct tevent_req *req =
1285 tevent_req_callback_data(subreq,
1287 struct smbXcli_req_state *state =
1288 tevent_req_data(req,
1289 struct smbXcli_req_state);
1293 nwritten = writev_recv(subreq, &err);
1294 TALLOC_FREE(subreq);
1295 if (nwritten == -1) {
1296 NTSTATUS status = map_nt_error_from_unix_common(err);
1297 smbXcli_conn_disconnect(state->conn, status);
1301 if (state->one_way) {
1302 state->inbuf = NULL;
1303 tevent_req_done(req);
1307 if (!smbXcli_req_set_pending(req)) {
1308 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1313 static void smbXcli_conn_received(struct tevent_req *subreq)
1315 struct smbXcli_conn *conn =
1316 tevent_req_callback_data(subreq,
1317 struct smbXcli_conn);
1318 TALLOC_CTX *frame = talloc_stackframe();
1324 if (subreq != conn->read_smb_req) {
1325 DEBUG(1, ("Internal error: cli_smb_received called with "
1326 "unexpected subreq\n"));
1327 status = NT_STATUS_INTERNAL_ERROR;
1328 smbXcli_conn_disconnect(conn, status);
1332 conn->read_smb_req = NULL;
1334 received = read_smb_recv(subreq, frame, &inbuf, &err);
1335 TALLOC_FREE(subreq);
1336 if (received == -1) {
1337 status = map_nt_error_from_unix_common(err);
1338 smbXcli_conn_disconnect(conn, status);
1343 status = conn->dispatch_incoming(conn, frame, inbuf);
1345 if (NT_STATUS_IS_OK(status)) {
1347 * We should not do any more processing
1348 * as the dispatch function called
1349 * tevent_req_done().
1352 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1354 * We got an error, so notify all pending requests
1356 smbXcli_conn_disconnect(conn, status);
1361 * We got NT_STATUS_RETRY, so we may ask for a
1362 * next incoming pdu.
1364 if (!smbXcli_conn_receive_next(conn)) {
1365 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1369 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1370 struct iovec **piov, int *pnum_iov)
1381 buflen = smb_len_nbt(buf);
1384 hdr = buf + NBT_HDR_SIZE;
1386 if (buflen < MIN_SMB_SIZE) {
1387 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1391 * This returns iovec elements in the following order:
1406 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1408 return NT_STATUS_NO_MEMORY;
1410 iov[0].iov_base = hdr;
1411 iov[0].iov_len = HDR_WCT;
1414 cmd = CVAL(hdr, HDR_COM);
1418 size_t len = buflen - taken;
1420 struct iovec *iov_tmp;
1427 * we need at least WCT and BCC
1429 needed = sizeof(uint8_t) + sizeof(uint16_t);
1431 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1432 __location__, (int)len, (int)needed));
1437 * Now we check if the specified words are there
1439 wct = CVAL(hdr, wct_ofs);
1440 needed += wct * sizeof(uint16_t);
1442 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1443 __location__, (int)len, (int)needed));
1448 * Now we check if the specified bytes are there
1450 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1451 bcc = SVAL(hdr, bcc_ofs);
1452 needed += bcc * sizeof(uint8_t);
1454 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1455 __location__, (int)len, (int)needed));
1460 * we allocate 2 iovec structures for words and bytes
1462 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1464 if (iov_tmp == NULL) {
1466 return NT_STATUS_NO_MEMORY;
1469 cur = &iov[num_iov];
1472 cur[0].iov_len = wct * sizeof(uint16_t);
1473 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1474 cur[1].iov_len = bcc * sizeof(uint8_t);
1475 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1479 if (!smb1cli_is_andx_req(cmd)) {
1481 * If the current command does not have AndX chanining
1487 if (wct == 0 && bcc == 0) {
1489 * An empty response also ends the chain,
1490 * most likely with an error.
1496 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1497 __location__, (int)wct, (int)cmd));
1500 cmd = CVAL(cur[0].iov_base, 0);
1503 * If it is the end of the chain we are also done.
1507 wct_ofs = SVAL(cur[0].iov_base, 2);
1509 if (wct_ofs < taken) {
1510 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1512 if (wct_ofs > buflen) {
1513 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1517 * we consumed everything up to the start of the next
1523 remaining = buflen - taken;
1525 if (remaining > 0 && num_iov >= 3) {
1527 * The last DATA block gets the remaining
1528 * bytes, this is needed to support
1529 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1531 iov[num_iov-1].iov_len += remaining;
1535 *pnum_iov = num_iov;
1536 return NT_STATUS_OK;
1540 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1543 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1544 TALLOC_CTX *tmp_mem,
1547 struct tevent_req *req;
1548 struct smbXcli_req_state *state;
1555 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1556 struct iovec *iov = NULL;
1558 struct tevent_req **chain = NULL;
1559 size_t num_chained = 0;
1560 size_t num_responses = 0;
1562 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1563 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1564 DEBUG(10, ("Got non-SMB PDU\n"));
1565 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1569 * If we supported multiple encrytion contexts
1570 * here we'd look up based on tid.
1572 if (common_encryption_on(conn->smb1.trans_enc)
1573 && (CVAL(inbuf, 0) == 0)) {
1574 uint16_t enc_ctx_num;
1576 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1577 if (!NT_STATUS_IS_OK(status)) {
1578 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1579 nt_errstr(status)));
1583 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1584 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1586 conn->smb1.trans_enc->enc_ctx_num));
1587 return NT_STATUS_INVALID_HANDLE;
1590 status = common_decrypt_buffer(conn->smb1.trans_enc,
1592 if (!NT_STATUS_IS_OK(status)) {
1593 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1594 nt_errstr(status)));
1599 mid = SVAL(inhdr, HDR_MID);
1600 num_pending = talloc_array_length(conn->pending);
1602 for (i=0; i<num_pending; i++) {
1603 if (mid == smb1cli_req_mid(conn->pending[i])) {
1607 if (i == num_pending) {
1608 /* Dump unexpected reply */
1609 return NT_STATUS_RETRY;
1612 oplock_break = false;
1614 if (mid == 0xffff) {
1616 * Paranoia checks that this is really an oplock break request.
1618 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1619 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1620 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1621 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1622 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1624 if (!oplock_break) {
1625 /* Dump unexpected reply */
1626 return NT_STATUS_RETRY;
1630 req = conn->pending[i];
1631 state = tevent_req_data(req, struct smbXcli_req_state);
1633 if (!oplock_break /* oplock breaks are not signed */
1634 && !smb_signing_check_pdu(conn->smb1.signing,
1635 inbuf, state->smb1.seqnum+1)) {
1636 DEBUG(10, ("cli_check_sign_mac failed\n"));
1637 return NT_STATUS_ACCESS_DENIED;
1640 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1642 if (!NT_STATUS_IS_OK(status)) {
1643 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1644 nt_errstr(status)));
1648 cmd = CVAL(inhdr, HDR_COM);
1649 status = smb1cli_pull_raw_error(inhdr);
1651 if (state->smb1.chained_requests == NULL) {
1653 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1656 smbXcli_req_unset_pending(req);
1658 state->smb1.recv_cmd = cmd;
1659 state->smb1.recv_status = status;
1660 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1662 state->smb1.recv_iov[0] = iov[0];
1663 state->smb1.recv_iov[1] = iov[1];
1664 state->smb1.recv_iov[2] = iov[2];
1666 if (talloc_array_length(conn->pending) == 0) {
1667 tevent_req_done(req);
1668 return NT_STATUS_OK;
1671 tevent_req_defer_callback(req, state->ev);
1672 tevent_req_done(req);
1673 return NT_STATUS_RETRY;
1676 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1677 num_chained = talloc_array_length(chain);
1678 num_responses = (num_iov - 1)/2;
1680 if (num_responses > num_chained) {
1681 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1684 for (i=0; i<num_chained; i++) {
1685 size_t iov_idx = 1 + (i*2);
1686 struct iovec *cur = &iov[iov_idx];
1690 state = tevent_req_data(req, struct smbXcli_req_state);
1692 smbXcli_req_unset_pending(req);
1695 * as we finish multiple requests here
1696 * we need to defer the callbacks as
1697 * they could destroy our current stack state.
1699 tevent_req_defer_callback(req, state->ev);
1701 if (i >= num_responses) {
1702 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1706 state->smb1.recv_cmd = cmd;
1708 if (i == (num_responses - 1)) {
1710 * The last request in the chain gets the status
1712 state->smb1.recv_status = status;
1714 cmd = CVAL(cur[0].iov_base, 0);
1715 state->smb1.recv_status = NT_STATUS_OK;
1718 state->inbuf = inbuf;
1721 * Note: here we use talloc_reference() in a way
1722 * that does not expose it to the caller.
1724 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1725 if (tevent_req_nomem(inbuf_ref, req)) {
1729 /* copy the related buffers */
1730 state->smb1.recv_iov[0] = iov[0];
1731 state->smb1.recv_iov[1] = cur[0];
1732 state->smb1.recv_iov[2] = cur[1];
1734 tevent_req_done(req);
1737 return NT_STATUS_RETRY;
1740 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
1741 TALLOC_CTX *mem_ctx,
1742 struct iovec **piov,
1746 uint32_t *pvwv_offset,
1747 uint32_t *pnum_bytes,
1749 uint32_t *pbytes_offset,
1751 const struct smb1cli_req_expected_response *expected,
1752 size_t num_expected)
1754 struct smbXcli_req_state *state =
1755 tevent_req_data(req,
1756 struct smbXcli_req_state);
1757 NTSTATUS status = NT_STATUS_OK;
1758 struct iovec *recv_iov = NULL;
1759 uint8_t *hdr = NULL;
1761 uint32_t vwv_offset = 0;
1762 uint16_t *vwv = NULL;
1763 uint32_t num_bytes = 0;
1764 uint32_t bytes_offset = 0;
1765 uint8_t *bytes = NULL;
1767 bool found_status = false;
1768 bool found_size = false;
1782 if (pvwv_offset != NULL) {
1785 if (pnum_bytes != NULL) {
1788 if (pbytes != NULL) {
1791 if (pbytes_offset != NULL) {
1794 if (pinbuf != NULL) {
1798 if (state->inbuf != NULL) {
1799 recv_iov = state->smb1.recv_iov;
1800 hdr = (uint8_t *)recv_iov[0].iov_base;
1801 wct = recv_iov[1].iov_len/2;
1802 vwv = (uint16_t *)recv_iov[1].iov_base;
1803 vwv_offset = PTR_DIFF(vwv, hdr);
1804 num_bytes = recv_iov[2].iov_len;
1805 bytes = (uint8_t *)recv_iov[2].iov_base;
1806 bytes_offset = PTR_DIFF(bytes, hdr);
1809 if (tevent_req_is_nterror(req, &status)) {
1810 for (i=0; i < num_expected; i++) {
1811 if (NT_STATUS_EQUAL(status, expected[i].status)) {
1812 found_status = true;
1818 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1824 if (num_expected == 0) {
1825 found_status = true;
1829 status = state->smb1.recv_status;
1831 for (i=0; i < num_expected; i++) {
1832 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
1836 found_status = true;
1837 if (expected[i].wct == 0) {
1842 if (expected[i].wct == wct) {
1848 if (!found_status) {
1853 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1857 *piov = talloc_move(mem_ctx, &recv_iov);
1869 if (pvwv_offset != NULL) {
1870 *pvwv_offset = vwv_offset;
1872 if (pnum_bytes != NULL) {
1873 *pnum_bytes = num_bytes;
1875 if (pbytes != NULL) {
1878 if (pbytes_offset != NULL) {
1879 *pbytes_offset = bytes_offset;
1881 if (pinbuf != NULL) {
1882 *pinbuf = state->inbuf;
1888 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
1895 for (i=0; i<num_reqs; i++) {
1896 struct smbXcli_req_state *state;
1897 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
1898 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
1899 state->smb1.iov_count-2);
1900 wct_ofs = (wct_ofs + 3) & ~3;
1905 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
1907 struct smbXcli_req_state *first_state =
1908 tevent_req_data(reqs[0],
1909 struct smbXcli_req_state);
1910 struct smbXcli_req_state *state;
1912 size_t chain_padding = 0;
1914 struct iovec *iov = NULL;
1915 struct iovec *this_iov;
1919 if (num_reqs == 1) {
1920 return smb1cli_req_writev_submit(reqs[0], first_state,
1921 first_state->smb1.iov,
1922 first_state->smb1.iov_count);
1926 for (i=0; i<num_reqs; i++) {
1927 if (!tevent_req_is_in_progress(reqs[i])) {
1928 return NT_STATUS_INTERNAL_ERROR;
1931 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
1933 if (state->smb1.iov_count < 4) {
1934 return NT_STATUS_INVALID_PARAMETER_MIX;
1939 * The NBT and SMB header
1952 iovlen += state->smb1.iov_count - 2;
1955 iov = talloc_zero_array(first_state, struct iovec, iovlen);
1957 return NT_STATUS_NO_MEMORY;
1960 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
1961 first_state, reqs, sizeof(*reqs) * num_reqs);
1962 if (first_state->smb1.chained_requests == NULL) {
1964 return NT_STATUS_NO_MEMORY;
1967 wct_offset = HDR_WCT;
1970 for (i=0; i<num_reqs; i++) {
1971 size_t next_padding = 0;
1974 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
1976 if (i < num_reqs-1) {
1977 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
1978 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
1980 TALLOC_FREE(first_state->smb1.chained_requests);
1981 return NT_STATUS_INVALID_PARAMETER_MIX;
1985 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
1986 state->smb1.iov_count-2) + 1;
1987 if ((wct_offset % 4) != 0) {
1988 next_padding = 4 - (wct_offset % 4);
1990 wct_offset += next_padding;
1991 vwv = state->smb1.vwv;
1993 if (i < num_reqs-1) {
1994 struct smbXcli_req_state *next_state =
1995 tevent_req_data(reqs[i+1],
1996 struct smbXcli_req_state);
1997 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
1999 SSVAL(vwv+1, 0, wct_offset);
2000 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2001 /* properly end the chain */
2002 SCVAL(vwv+0, 0, 0xff);
2003 SCVAL(vwv+0, 1, 0xff);
2009 * The NBT and SMB header
2011 this_iov[0] = state->smb1.iov[0];
2012 this_iov[1] = state->smb1.iov[1];
2016 * This one is a bit subtle. We have to add
2017 * chain_padding bytes between the requests, and we
2018 * have to also include the wct field of the
2019 * subsequent requests. We use the subsequent header
2020 * for the padding, it contains the wct field in its
2023 this_iov[0].iov_len = chain_padding+1;
2024 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2025 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2026 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2031 * copy the words and bytes
2033 memcpy(this_iov, state->smb1.iov+2,
2034 sizeof(struct iovec) * (state->smb1.iov_count-2));
2035 this_iov += state->smb1.iov_count - 2;
2036 chain_padding = next_padding;
2039 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2040 if (nbt_len > first_state->conn->smb1.max_xmit) {
2042 TALLOC_FREE(first_state->smb1.chained_requests);
2043 return NT_STATUS_INVALID_PARAMETER_MIX;
2046 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2047 if (!NT_STATUS_IS_OK(status)) {
2049 TALLOC_FREE(first_state->smb1.chained_requests);
2053 return NT_STATUS_OK;
2056 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2058 return ((tevent_queue_length(conn->outgoing) != 0)
2059 || (talloc_array_length(conn->pending) != 0));
2062 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2064 return conn->smb2.server.capabilities;
2067 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2069 return conn->smb2.server.security_mode;
2072 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2074 return conn->smb2.server.max_trans_size;
2077 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2079 return conn->smb2.server.max_read_size;
2082 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2084 return conn->smb2.server.max_write_size;
2087 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2088 uint16_t max_credits)
2090 conn->smb2.max_credits = max_credits;
2093 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2095 static bool smb2cli_req_cancel(struct tevent_req *req)
2097 struct smbXcli_req_state *state =
2098 tevent_req_data(req,
2099 struct smbXcli_req_state);
2100 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2101 uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
2102 uint32_t tid = IVAL(state->smb2.hdr, SMB2_HDR_TID);
2103 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2104 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2105 struct smbXcli_session *session = state->session;
2106 uint8_t *fixed = state->smb2.pad;
2107 uint16_t fixed_len = 4;
2108 struct tevent_req *subreq;
2109 struct smbXcli_req_state *substate;
2112 SSVAL(fixed, 0, 0x04);
2115 subreq = smb2cli_req_create(state, state->ev,
2123 if (subreq == NULL) {
2126 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2128 if (flags & SMB2_HDR_FLAG_ASYNC) {
2132 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2133 SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
2134 SIVAL(substate->smb2.hdr, SMB2_HDR_TID, tid);
2135 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2136 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2138 status = smb2cli_req_compound_submit(&subreq, 1);
2139 if (!NT_STATUS_IS_OK(status)) {
2140 TALLOC_FREE(subreq);
2144 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2149 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2151 /* we do not care about the result */
2152 TALLOC_FREE(subreq);
2155 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2156 struct tevent_context *ev,
2157 struct smbXcli_conn *conn,
2159 uint32_t additional_flags,
2160 uint32_t clear_flags,
2161 uint32_t timeout_msec,
2164 struct smbXcli_session *session,
2165 const uint8_t *fixed,
2170 struct tevent_req *req;
2171 struct smbXcli_req_state *state;
2175 req = tevent_req_create(mem_ctx, &state,
2176 struct smbXcli_req_state);
2183 state->session = session;
2186 uid = session->smb2.session_id;
2189 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2190 if (state->smb2.recv_iov == NULL) {
2195 flags |= additional_flags;
2196 flags &= ~clear_flags;
2198 state->smb2.fixed = fixed;
2199 state->smb2.fixed_len = fixed_len;
2200 state->smb2.dyn = dyn;
2201 state->smb2.dyn_len = dyn_len;
2203 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2204 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2205 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2206 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2207 SIVAL(state->smb2.hdr, SMB2_HDR_PID, pid);
2208 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2209 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2212 case SMB2_OP_CANCEL:
2213 state->one_way = true;
2217 * If this is a dummy request, it will have
2218 * UINT64_MAX as message id.
2219 * If we send on break acknowledgement,
2220 * this gets overwritten later.
2222 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2226 if (timeout_msec > 0) {
2227 struct timeval endtime;
2229 endtime = timeval_current_ofs_msec(timeout_msec);
2230 if (!tevent_req_set_endtime(req, ev, endtime)) {
2238 static void smb2cli_writev_done(struct tevent_req *subreq);
2239 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2240 TALLOC_CTX *tmp_mem,
2243 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2246 struct smbXcli_req_state *state;
2247 struct tevent_req *subreq;
2249 int i, num_iov, nbt_len;
2252 * 1 for the nbt length
2253 * per request: HDR, fixed, dyn, padding
2254 * -1 because the last one does not need padding
2257 iov = talloc_array(reqs[0], struct iovec, 1 + 4*num_reqs - 1);
2259 return NT_STATUS_NO_MEMORY;
2265 for (i=0; i<num_reqs; i++) {
2274 bool should_sign = false;
2276 if (!tevent_req_is_in_progress(reqs[i])) {
2277 return NT_STATUS_INTERNAL_ERROR;
2280 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2282 if (!smbXcli_conn_is_connected(state->conn)) {
2283 return NT_STATUS_CONNECTION_DISCONNECTED;
2286 if ((state->conn->protocol != PROTOCOL_NONE) &&
2287 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2288 return NT_STATUS_REVISION_MISMATCH;
2291 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2292 if (opcode == SMB2_OP_CANCEL) {
2296 avail = UINT64_MAX - state->conn->smb2.mid;
2298 return NT_STATUS_CONNECTION_ABORTED;
2301 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2302 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2307 charge = MAX(state->smb2.credit_charge, charge);
2309 avail = MIN(avail, state->conn->smb2.cur_credits);
2310 if (avail < charge) {
2311 return NT_STATUS_INTERNAL_ERROR;
2315 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2316 credits = state->conn->smb2.max_credits -
2317 state->conn->smb2.cur_credits;
2319 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2323 mid = state->conn->smb2.mid;
2324 state->conn->smb2.mid += charge;
2325 state->conn->smb2.cur_credits -= charge;
2327 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2328 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2330 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2331 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2335 iov[num_iov].iov_base = state->smb2.hdr;
2336 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2339 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2340 iov[num_iov].iov_len = state->smb2.fixed_len;
2343 if (state->smb2.dyn != NULL) {
2344 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2345 iov[num_iov].iov_len = state->smb2.dyn_len;
2349 reqlen = sizeof(state->smb2.hdr);
2350 reqlen += state->smb2.fixed_len;
2351 reqlen += state->smb2.dyn_len;
2353 if (i < num_reqs-1) {
2354 if ((reqlen % 8) > 0) {
2355 uint8_t pad = 8 - (reqlen % 8);
2356 iov[num_iov].iov_base = state->smb2.pad;
2357 iov[num_iov].iov_len = pad;
2361 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2365 if (state->session) {
2366 should_sign = state->session->smb2.should_sign;
2367 if (state->session->smb2.channel_setup) {
2375 status = smb2_signing_sign_pdu(state->session->smb2.signing_key,
2376 &iov[hdr_iov], num_iov - hdr_iov);
2377 if (!NT_STATUS_IS_OK(status)) {
2382 ret = smbXcli_req_set_pending(reqs[i]);
2384 return NT_STATUS_NO_MEMORY;
2388 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2389 _smb_setlen_tcp(state->length_hdr, nbt_len);
2390 iov[0].iov_base = state->length_hdr;
2391 iov[0].iov_len = sizeof(state->length_hdr);
2393 if (state->conn->dispatch_incoming == NULL) {
2394 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2397 subreq = writev_send(state, state->ev, state->conn->outgoing,
2398 state->conn->fd, false, iov, num_iov);
2399 if (subreq == NULL) {
2400 return NT_STATUS_NO_MEMORY;
2402 tevent_req_set_callback(subreq, smb2cli_writev_done, reqs[0]);
2403 return NT_STATUS_OK;
2406 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2408 struct smbXcli_req_state *state =
2409 tevent_req_data(req,
2410 struct smbXcli_req_state);
2412 state->smb2.credit_charge = charge;
2415 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2416 struct tevent_context *ev,
2417 struct smbXcli_conn *conn,
2419 uint32_t additional_flags,
2420 uint32_t clear_flags,
2421 uint32_t timeout_msec,
2424 struct smbXcli_session *session,
2425 const uint8_t *fixed,
2430 struct tevent_req *req;
2433 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2434 additional_flags, clear_flags,
2437 fixed, fixed_len, dyn, dyn_len);
2441 if (!tevent_req_is_in_progress(req)) {
2442 return tevent_req_post(req, ev);
2444 status = smb2cli_req_compound_submit(&req, 1);
2445 if (tevent_req_nterror(req, status)) {
2446 return tevent_req_post(req, ev);
2451 static void smb2cli_writev_done(struct tevent_req *subreq)
2453 struct tevent_req *req =
2454 tevent_req_callback_data(subreq,
2456 struct smbXcli_req_state *state =
2457 tevent_req_data(req,
2458 struct smbXcli_req_state);
2462 nwritten = writev_recv(subreq, &err);
2463 TALLOC_FREE(subreq);
2464 if (nwritten == -1) {
2465 /* here, we need to notify all pending requests */
2466 NTSTATUS status = map_nt_error_from_unix_common(err);
2467 smbXcli_conn_disconnect(state->conn, status);
2472 static NTSTATUS smb2cli_inbuf_parse_compound(uint8_t *buf, TALLOC_CTX *mem_ctx,
2473 struct iovec **piov, int *pnum_iov)
2483 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2485 return NT_STATUS_NO_MEMORY;
2488 buflen = smb_len_tcp(buf);
2490 first_hdr = buf + NBT_HDR_SIZE;
2492 while (taken < buflen) {
2493 size_t len = buflen - taken;
2494 uint8_t *hdr = first_hdr + taken;
2497 size_t next_command_ofs;
2499 struct iovec *iov_tmp;
2502 * We need the header plus the body length field
2505 if (len < SMB2_HDR_BODY + 2) {
2506 DEBUG(10, ("%d bytes left, expected at least %d\n",
2507 (int)len, SMB2_HDR_BODY));
2510 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2511 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2515 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2516 DEBUG(10, ("Got HDR len %d, expected %d\n",
2517 SVAL(hdr, 4), SMB2_HDR_BODY));
2522 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2523 body_size = SVAL(hdr, SMB2_HDR_BODY);
2525 if (next_command_ofs != 0) {
2526 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2529 if (next_command_ofs > full_size) {
2532 full_size = next_command_ofs;
2534 if (body_size < 2) {
2537 body_size &= 0xfffe;
2539 if (body_size > (full_size - SMB2_HDR_BODY)) {
2543 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2545 if (iov_tmp == NULL) {
2547 return NT_STATUS_NO_MEMORY;
2550 cur = &iov[num_iov];
2553 cur[0].iov_base = hdr;
2554 cur[0].iov_len = SMB2_HDR_BODY;
2555 cur[1].iov_base = hdr + SMB2_HDR_BODY;
2556 cur[1].iov_len = body_size;
2557 cur[2].iov_base = hdr + SMB2_HDR_BODY + body_size;
2558 cur[2].iov_len = full_size - (SMB2_HDR_BODY + body_size);
2564 *pnum_iov = num_iov;
2565 return NT_STATUS_OK;
2569 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2572 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
2575 size_t num_pending = talloc_array_length(conn->pending);
2578 for (i=0; i<num_pending; i++) {
2579 struct tevent_req *req = conn->pending[i];
2580 struct smbXcli_req_state *state =
2581 tevent_req_data(req,
2582 struct smbXcli_req_state);
2584 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
2591 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2592 TALLOC_CTX *tmp_mem,
2595 struct tevent_req *req;
2596 struct smbXcli_req_state *state = NULL;
2601 struct smbXcli_session *last_session = NULL;
2603 status = smb2cli_inbuf_parse_compound(inbuf, tmp_mem,
2605 if (!NT_STATUS_IS_OK(status)) {
2609 for (i=0; i<num_iov; i+=3) {
2610 uint8_t *inbuf_ref = NULL;
2611 struct iovec *cur = &iov[i];
2612 uint8_t *inhdr = (uint8_t *)cur[0].iov_base;
2613 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
2614 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
2615 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
2616 uint16_t req_opcode;
2618 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
2619 uint32_t new_credits;
2620 struct smbXcli_session *session = NULL;
2621 const DATA_BLOB *signing_key = NULL;
2622 bool should_sign = false;
2624 new_credits = conn->smb2.cur_credits;
2625 new_credits += credits;
2626 if (new_credits > UINT16_MAX) {
2627 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2629 conn->smb2.cur_credits += credits;
2631 req = smb2cli_conn_find_pending(conn, mid);
2633 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2635 state = tevent_req_data(req, struct smbXcli_req_state);
2637 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2638 if (opcode != req_opcode) {
2639 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2641 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2643 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
2644 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2647 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
2648 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
2649 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
2650 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
2653 * async interim responses are not signed,
2654 * even if the SMB2_HDR_FLAG_SIGNED flag
2657 req_flags |= SMB2_HDR_FLAG_ASYNC;
2658 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
2659 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
2663 session = state->session;
2664 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
2665 session = last_session;
2667 last_session = session;
2670 should_sign = session->smb2.should_sign;
2671 if (session->smb2.channel_setup) {
2677 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
2678 return NT_STATUS_ACCESS_DENIED;
2682 if (flags & SMB2_HDR_FLAG_SIGNED) {
2683 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
2685 if (session == NULL) {
2686 struct smbXcli_session *s;
2688 s = state->conn->sessions;
2689 for (; s; s = s->next) {
2690 if (s->smb2.session_id != uid) {
2699 if (session == NULL) {
2700 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2703 last_session = session;
2704 signing_key = &session->smb2.signing_key;
2707 if ((opcode == SMB2_OP_SESSSETUP) &&
2708 NT_STATUS_IS_OK(status)) {
2710 * the caller has to check the signing
2711 * as only the caller knows the correct
2717 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
2719 * if the server returns NT_STATUS_USER_SESSION_DELETED
2720 * the response is not signed and we should
2721 * propagate the NT_STATUS_USER_SESSION_DELETED
2722 * status to the caller.
2729 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
2730 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
2731 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2733 * if the server returns
2734 * NT_STATUS_NETWORK_NAME_DELETED
2735 * NT_STATUS_FILE_CLOSED
2736 * NT_STATUS_INVALID_PARAMETER
2737 * the response might not be signed
2738 * as this happens before the signing checks.
2740 * If server echos the signature (or all zeros)
2741 * we should report the status from the server
2747 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
2748 state->smb2.hdr+SMB2_HDR_SIGNATURE,
2751 state->smb2.signing_skipped = true;
2757 static const uint8_t zeros[16];
2759 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
2763 state->smb2.signing_skipped = true;
2770 status = smb2_signing_check_pdu(*signing_key, cur, 3);
2771 if (!NT_STATUS_IS_OK(status)) {
2773 * If the signing check fails, we disconnect
2780 smbXcli_req_unset_pending(req);
2783 * There might be more than one response
2784 * we need to defer the notifications
2786 if ((num_iov == 4) && (talloc_array_length(conn->pending) == 0)) {
2791 tevent_req_defer_callback(req, state->ev);
2795 * Note: here we use talloc_reference() in a way
2796 * that does not expose it to the caller.
2798 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
2799 if (tevent_req_nomem(inbuf_ref, req)) {
2803 /* copy the related buffers */
2804 state->smb2.recv_iov[0] = cur[0];
2805 state->smb2.recv_iov[1] = cur[1];
2806 state->smb2.recv_iov[2] = cur[2];
2808 tevent_req_done(req);
2812 return NT_STATUS_RETRY;
2815 return NT_STATUS_OK;
2818 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
2819 struct iovec **piov,
2820 const struct smb2cli_req_expected_response *expected,
2821 size_t num_expected)
2823 struct smbXcli_req_state *state =
2824 tevent_req_data(req,
2825 struct smbXcli_req_state);
2828 bool found_status = false;
2829 bool found_size = false;
2836 if (tevent_req_is_nterror(req, &status)) {
2837 for (i=0; i < num_expected; i++) {
2838 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2839 found_status = true;
2845 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2851 if (num_expected == 0) {
2852 found_status = true;
2856 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
2857 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
2859 for (i=0; i < num_expected; i++) {
2860 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2864 found_status = true;
2865 if (expected[i].body_size == 0) {
2870 if (expected[i].body_size == body_size) {
2876 if (!found_status) {
2880 if (state->smb2.signing_skipped) {
2881 if (num_expected > 0) {
2882 return NT_STATUS_ACCESS_DENIED;
2884 if (!NT_STATUS_IS_ERR(status)) {
2885 return NT_STATUS_ACCESS_DENIED;
2890 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2894 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
2900 static const struct {
2901 enum protocol_types proto;
2902 const char *smb1_name;
2903 } smb1cli_prots[] = {
2904 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
2905 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
2906 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
2907 {PROTOCOL_LANMAN1, "LANMAN1.0"},
2908 {PROTOCOL_LANMAN2, "LM1.2X002"},
2909 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
2910 {PROTOCOL_LANMAN2, "LANMAN2.1"},
2911 {PROTOCOL_LANMAN2, "Samba"},
2912 {PROTOCOL_NT1, "NT LANMAN 1.0"},
2913 {PROTOCOL_NT1, "NT LM 0.12"},
2914 {PROTOCOL_SMB2_02, "SMB 2.002"},
2915 {PROTOCOL_SMB2_10, "SMB 2.???"},
2918 static const struct {
2919 enum protocol_types proto;
2920 uint16_t smb2_dialect;
2921 } smb2cli_prots[] = {
2922 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
2923 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
2924 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
2927 struct smbXcli_negprot_state {
2928 struct smbXcli_conn *conn;
2929 struct tevent_context *ev;
2930 uint32_t timeout_msec;
2931 enum protocol_types min_protocol;
2932 enum protocol_types max_protocol;
2936 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
2940 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
2941 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
2942 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
2943 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
2944 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
2945 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
2949 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
2950 struct tevent_context *ev,
2951 struct smbXcli_conn *conn,
2952 uint32_t timeout_msec,
2953 enum protocol_types min_protocol,
2954 enum protocol_types max_protocol)
2956 struct tevent_req *req, *subreq;
2957 struct smbXcli_negprot_state *state;
2959 req = tevent_req_create(mem_ctx, &state,
2960 struct smbXcli_negprot_state);
2966 state->timeout_msec = timeout_msec;
2967 state->min_protocol = min_protocol;
2968 state->max_protocol = max_protocol;
2970 if (min_protocol == PROTOCOL_NONE) {
2971 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
2972 return tevent_req_post(req, ev);
2975 if (max_protocol == PROTOCOL_NONE) {
2976 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
2977 return tevent_req_post(req, ev);
2980 if (min_protocol > max_protocol) {
2981 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
2982 return tevent_req_post(req, ev);
2985 if ((min_protocol < PROTOCOL_SMB2_02) &&
2986 (max_protocol < PROTOCOL_SMB2_02)) {
2990 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
2992 subreq = smbXcli_negprot_smb1_subreq(state);
2993 if (tevent_req_nomem(subreq, req)) {
2994 return tevent_req_post(req, ev);
2996 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3000 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3001 (max_protocol >= PROTOCOL_SMB2_02)) {
3005 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3007 subreq = smbXcli_negprot_smb2_subreq(state);
3008 if (tevent_req_nomem(subreq, req)) {
3009 return tevent_req_post(req, ev);
3011 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3016 * We send an SMB1 negprot with the SMB2 dialects
3017 * and expect a SMB1 or a SMB2 response.
3019 * smbXcli_negprot_dispatch_incoming() will fix the
3020 * callback to match protocol of the response.
3022 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3024 subreq = smbXcli_negprot_smb1_subreq(state);
3025 if (tevent_req_nomem(subreq, req)) {
3026 return tevent_req_post(req, ev);
3028 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3032 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3034 struct tevent_req *req =
3035 tevent_req_callback_data(subreq,
3040 * we just want the low level error
3042 status = tevent_req_simple_recv_ntstatus(subreq);
3043 TALLOC_FREE(subreq);
3044 if (tevent_req_nterror(req, status)) {
3048 /* this should never happen */
3049 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3052 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3055 DATA_BLOB bytes = data_blob_null;
3059 /* setup the protocol strings */
3060 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3064 if (smb1cli_prots[i].proto < state->min_protocol) {
3068 if (smb1cli_prots[i].proto > state->max_protocol) {
3072 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3078 * We now it is already ascii and
3079 * we want NULL termination.
3081 ok = data_blob_append(state, &bytes,
3082 smb1cli_prots[i].smb1_name,
3083 strlen(smb1cli_prots[i].smb1_name)+1);
3089 smb1cli_req_flags(state->max_protocol,
3090 state->conn->smb1.client.capabilities,
3095 return smb1cli_req_send(state, state->ev, state->conn,
3099 state->timeout_msec,
3100 0xFFFE, 0, 0, /* pid, tid, uid */
3101 0, NULL, /* wct, vwv */
3102 bytes.length, bytes.data);
3105 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3107 struct tevent_req *req =
3108 tevent_req_callback_data(subreq,
3110 struct smbXcli_negprot_state *state =
3111 tevent_req_data(req,
3112 struct smbXcli_negprot_state);
3113 struct smbXcli_conn *conn = state->conn;
3114 struct iovec *recv_iov = NULL;
3123 size_t num_prots = 0;
3125 uint32_t client_capabilities = conn->smb1.client.capabilities;
3126 uint32_t both_capabilities;
3127 uint32_t server_capabilities = 0;
3128 uint32_t capabilities;
3129 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3130 uint32_t server_max_xmit = 0;
3132 uint32_t server_max_mux = 0;
3133 uint16_t server_security_mode = 0;
3134 uint32_t server_session_key = 0;
3135 bool server_readbraw = false;
3136 bool server_writebraw = false;
3137 bool server_lockread = false;
3138 bool server_writeunlock = false;
3139 struct GUID server_guid = GUID_zero();
3140 DATA_BLOB server_gss_blob = data_blob_null;
3141 uint8_t server_challenge[8];
3142 char *server_workgroup = NULL;
3143 char *server_name = NULL;
3144 int server_time_zone = 0;
3145 NTTIME server_system_time = 0;
3146 static const struct smb1cli_req_expected_response expected[] = {
3148 .status = NT_STATUS_OK,
3149 .wct = 0x11, /* NT1 */
3152 .status = NT_STATUS_OK,
3153 .wct = 0x0D, /* LM */
3156 .status = NT_STATUS_OK,
3157 .wct = 0x01, /* CORE */
3161 ZERO_STRUCT(server_challenge);
3163 status = smb1cli_req_recv(subreq, state,
3168 NULL, /* pvwv_offset */
3171 NULL, /* pbytes_offset */
3173 expected, ARRAY_SIZE(expected));
3174 TALLOC_FREE(subreq);
3175 if (tevent_req_nterror(req, status)) {
3179 flags = CVAL(inhdr, HDR_FLG);
3181 protnum = SVAL(vwv, 0);
3183 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3184 if (smb1cli_prots[i].proto < state->min_protocol) {
3188 if (smb1cli_prots[i].proto > state->max_protocol) {
3192 if (protnum != num_prots) {
3197 conn->protocol = smb1cli_prots[i].proto;
3201 if (conn->protocol == PROTOCOL_NONE) {
3202 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3206 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3207 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3208 "and the selected protocol level doesn't support it.\n"));
3209 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3213 if (flags & FLAG_SUPPORT_LOCKREAD) {
3214 server_lockread = true;
3215 server_writeunlock = true;
3218 if (conn->protocol >= PROTOCOL_NT1) {
3219 const char *client_signing = NULL;
3220 bool server_mandatory = false;
3221 bool server_allowed = false;
3222 const char *server_signing = NULL;
3227 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3232 server_security_mode = CVAL(vwv + 1, 0);
3233 server_max_mux = SVAL(vwv + 1, 1);
3234 server_max_xmit = IVAL(vwv + 3, 1);
3235 server_session_key = IVAL(vwv + 7, 1);
3236 server_time_zone = SVALS(vwv + 15, 1);
3237 server_time_zone *= 60;
3238 /* this time arrives in real GMT */
3239 server_system_time = BVAL(vwv + 11, 1);
3240 server_capabilities = IVAL(vwv + 9, 1);
3242 key_len = CVAL(vwv + 16, 1);
3244 if (server_capabilities & CAP_RAW_MODE) {
3245 server_readbraw = true;
3246 server_writebraw = true;
3248 if (server_capabilities & CAP_LOCK_AND_READ) {
3249 server_lockread = true;
3252 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3253 DATA_BLOB blob1, blob2;
3255 if (num_bytes < 16) {
3256 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3260 blob1 = data_blob_const(bytes, 16);
3261 status = GUID_from_data_blob(&blob1, &server_guid);
3262 if (tevent_req_nterror(req, status)) {
3266 blob1 = data_blob_const(bytes+16, num_bytes-16);
3267 blob2 = data_blob_dup_talloc(state, blob1);
3268 if (blob1.length > 0 &&
3269 tevent_req_nomem(blob2.data, req)) {
3272 server_gss_blob = blob2;
3274 DATA_BLOB blob1, blob2;
3276 if (num_bytes < key_len) {
3277 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3281 if (key_len != 0 && key_len != 8) {
3282 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3287 memcpy(server_challenge, bytes, 8);
3290 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3291 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3292 if (blob1.length > 0) {
3295 len = utf16_len_n(blob1.data,
3299 ok = convert_string_talloc(state,
3307 status = map_nt_error_from_unix_common(errno);
3308 tevent_req_nterror(req, status);
3313 blob2.data += blob1.length;
3314 blob2.length -= blob1.length;
3315 if (blob2.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);
3337 client_signing = "disabled";
3338 if (conn->allow_signing) {
3339 client_signing = "allowed";
3341 if (conn->mandatory_signing) {
3342 client_signing = "required";
3345 server_signing = "not supported";
3346 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3347 server_signing = "supported";
3348 server_allowed = true;
3350 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3351 server_signing = "required";
3352 server_mandatory = true;
3355 ok = smb_signing_set_negotiated(conn->smb1.signing,
3359 DEBUG(1,("cli_negprot: SMB signing is required, "
3360 "but client[%s] and server[%s] mismatch\n",
3361 client_signing, server_signing));
3362 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3366 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3372 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3376 server_security_mode = SVAL(vwv + 1, 0);
3377 server_max_xmit = SVAL(vwv + 2, 0);
3378 server_max_mux = SVAL(vwv + 3, 0);
3379 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3380 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3381 server_session_key = IVAL(vwv + 6, 0);
3382 server_time_zone = SVALS(vwv + 10, 0);
3383 server_time_zone *= 60;
3384 /* this time is converted to GMT by make_unix_date */
3385 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3386 unix_to_nt_time(&server_system_time, t);
3387 key_len = SVAL(vwv + 11, 0);
3389 if (num_bytes < key_len) {
3390 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3394 if (key_len != 0 && key_len != 8) {
3395 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3400 memcpy(server_challenge, bytes, 8);
3403 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3404 if (blob1.length > 0) {
3408 len = utf16_len_n(blob1.data,
3412 ok = convert_string_talloc(state,
3420 status = map_nt_error_from_unix_common(errno);
3421 tevent_req_nterror(req, status);
3427 /* the old core protocol */
3428 server_time_zone = get_time_zone(time(NULL));
3429 server_max_xmit = 1024;
3433 if (server_max_xmit < 1024) {
3434 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3438 if (server_max_mux < 1) {
3439 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3444 * Now calculate the negotiated capabilities
3445 * based on the mask for:
3446 * - client only flags
3447 * - flags used in both directions
3448 * - server only flags
3450 both_capabilities = client_capabilities & server_capabilities;
3451 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3452 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3453 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3455 max_xmit = MIN(client_max_xmit, server_max_xmit);
3457 conn->smb1.server.capabilities = server_capabilities;
3458 conn->smb1.capabilities = capabilities;
3460 conn->smb1.server.max_xmit = server_max_xmit;
3461 conn->smb1.max_xmit = max_xmit;
3463 conn->smb1.server.max_mux = server_max_mux;
3465 conn->smb1.server.security_mode = server_security_mode;
3467 conn->smb1.server.readbraw = server_readbraw;
3468 conn->smb1.server.writebraw = server_writebraw;
3469 conn->smb1.server.lockread = server_lockread;
3470 conn->smb1.server.writeunlock = server_writeunlock;
3472 conn->smb1.server.session_key = server_session_key;
3474 talloc_steal(conn, server_gss_blob.data);
3475 conn->smb1.server.gss_blob = server_gss_blob;
3476 conn->smb1.server.guid = server_guid;
3477 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3478 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3479 conn->smb1.server.name = talloc_move(conn, &server_name);
3481 conn->smb1.server.time_zone = server_time_zone;
3482 conn->smb1.server.system_time = server_system_time;
3484 tevent_req_done(req);
3487 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
3491 uint16_t dialect_count = 0;
3493 buf = state->smb2.dyn;
3494 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3495 if (smb2cli_prots[i].proto < state->min_protocol) {
3499 if (smb2cli_prots[i].proto > state->max_protocol) {
3503 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
3507 buf = state->smb2.fixed;
3509 SSVAL(buf, 2, dialect_count);
3510 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
3511 SSVAL(buf, 6, 0); /* Reserved */
3512 SSVAL(buf, 8, 0); /* Capabilities */
3513 if (state->max_protocol >= PROTOCOL_SMB2_10) {
3517 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
3519 if (!NT_STATUS_IS_OK(status)) {
3522 memcpy(buf+12, blob.data, 16); /* ClientGuid */
3524 memset(buf+12, 0, 16); /* ClientGuid */
3526 SBVAL(buf, 28, 0); /* ClientStartTime */
3528 return smb2cli_req_send(state, state->ev,
3529 state->conn, SMB2_OP_NEGPROT,
3531 state->timeout_msec,
3532 0xFEFF, 0, NULL, /* pid, tid, session */
3533 state->smb2.fixed, sizeof(state->smb2.fixed),
3534 state->smb2.dyn, dialect_count*2);
3537 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
3539 struct tevent_req *req =
3540 tevent_req_callback_data(subreq,
3542 struct smbXcli_negprot_state *state =
3543 tevent_req_data(req,
3544 struct smbXcli_negprot_state);
3545 struct smbXcli_conn *conn = state->conn;
3546 size_t security_offset, security_length;
3552 uint16_t dialect_revision;
3553 static const struct smb2cli_req_expected_response expected[] = {
3555 .status = NT_STATUS_OK,
3560 status = smb2cli_req_recv(subreq, state, &iov,
3561 expected, ARRAY_SIZE(expected));
3562 TALLOC_FREE(subreq);
3563 if (tevent_req_nterror(req, status)) {
3567 body = (uint8_t *)iov[1].iov_base;
3569 dialect_revision = SVAL(body, 4);
3571 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3572 if (smb2cli_prots[i].proto < state->min_protocol) {
3576 if (smb2cli_prots[i].proto > state->max_protocol) {
3580 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
3584 conn->protocol = smb2cli_prots[i].proto;
3588 if (conn->protocol == PROTOCOL_NONE) {
3589 if (state->min_protocol >= PROTOCOL_SMB2_02) {
3590 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3594 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
3595 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3599 /* make sure we do not loop forever */
3600 state->min_protocol = PROTOCOL_SMB2_02;
3603 * send a SMB2 negprot, in order to negotiate
3604 * the SMB2 dialect. This needs to use the
3607 state->conn->smb2.mid = 1;
3608 subreq = smbXcli_negprot_smb2_subreq(state);
3609 if (tevent_req_nomem(subreq, req)) {
3612 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3616 conn->smb2.server.security_mode = SVAL(body, 2);
3618 blob = data_blob_const(body + 8, 16);
3619 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
3620 if (tevent_req_nterror(req, status)) {
3624 conn->smb2.server.capabilities = IVAL(body, 24);
3625 conn->smb2.server.max_trans_size= IVAL(body, 28);
3626 conn->smb2.server.max_read_size = IVAL(body, 32);
3627 conn->smb2.server.max_write_size= IVAL(body, 36);
3628 conn->smb2.server.system_time = BVAL(body, 40);
3629 conn->smb2.server.start_time = BVAL(body, 48);
3631 security_offset = SVAL(body, 56);
3632 security_length = SVAL(body, 58);
3634 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
3635 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3639 if (security_length > iov[2].iov_len) {
3640 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3644 conn->smb2.server.gss_blob = data_blob_talloc(conn,
3647 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
3651 tevent_req_done(req);
3654 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3655 TALLOC_CTX *tmp_mem,
3658 size_t num_pending = talloc_array_length(conn->pending);
3659 struct tevent_req *subreq;
3660 struct smbXcli_req_state *substate;
3661 struct tevent_req *req;
3662 struct smbXcli_negprot_state *state;
3663 uint32_t protocol_magic = IVAL(inbuf, 4);
3665 if (num_pending != 1) {
3666 return NT_STATUS_INTERNAL_ERROR;
3669 subreq = conn->pending[0];
3670 substate = tevent_req_data(subreq, struct smbXcli_req_state);
3671 req = tevent_req_callback_data(subreq, struct tevent_req);
3672 state = tevent_req_data(req, struct smbXcli_negprot_state);
3674 switch (protocol_magic) {
3676 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3677 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3678 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3681 if (substate->smb2.recv_iov == NULL) {
3683 * For the SMB1 negprot we have move it.
3685 substate->smb2.recv_iov = substate->smb1.recv_iov;
3686 substate->smb1.recv_iov = NULL;
3689 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3690 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3691 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3694 DEBUG(10, ("Got non-SMB PDU\n"));
3695 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3698 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
3700 return tevent_req_simple_recv_ntstatus(req);
3703 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
3704 uint32_t timeout_msec,
3705 enum protocol_types min_protocol,
3706 enum protocol_types max_protocol)
3708 TALLOC_CTX *frame = talloc_stackframe();
3709 struct tevent_context *ev;
3710 struct tevent_req *req;
3711 NTSTATUS status = NT_STATUS_NO_MEMORY;
3714 if (smbXcli_conn_has_async_calls(conn)) {
3716 * Can't use sync call while an async call is in flight
3718 status = NT_STATUS_INVALID_PARAMETER_MIX;
3721 ev = tevent_context_init(frame);
3725 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
3726 min_protocol, max_protocol);
3730 ok = tevent_req_poll(req, ev);
3732 status = map_nt_error_from_unix_common(errno);
3735 status = smbXcli_negprot_recv(req);
3741 static int smbXcli_session_destructor(struct smbXcli_session *session)
3743 if (session->conn == NULL) {
3747 DLIST_REMOVE(session->conn->sessions, session);
3751 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
3752 struct smbXcli_conn *conn)
3754 struct smbXcli_session *session;
3756 session = talloc_zero(mem_ctx, struct smbXcli_session);
3757 if (session == NULL) {
3760 talloc_set_destructor(session, smbXcli_session_destructor);
3762 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
3763 session->conn = conn;
3768 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
3770 struct smbXcli_conn *conn = session->conn;
3771 uint8_t security_mode = 0;
3774 return security_mode;
3777 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
3778 if (conn->mandatory_signing) {
3779 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
3782 return security_mode;
3785 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
3787 return session->smb2.session_id;
3790 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
3791 uint64_t session_id,
3792 uint16_t session_flags)
3794 session->smb2.session_id = session_id;
3795 session->smb2.session_flags = session_flags;
3798 NTSTATUS smb2cli_session_update_session_key(struct smbXcli_session *session,
3799 const DATA_BLOB session_key,
3800 const struct iovec *recv_iov)
3802 struct smbXcli_conn *conn = session->conn;
3803 uint16_t no_sign_flags;
3804 DATA_BLOB signing_key;
3808 return NT_STATUS_INVALID_PARAMETER_MIX;
3811 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
3813 if (session->smb2.session_flags & no_sign_flags) {
3814 session->smb2.should_sign = false;
3815 return NT_STATUS_OK;
3818 if (session->smb2.signing_key.length > 0) {
3819 signing_key = session->smb2.signing_key;
3821 signing_key = session_key;
3823 if (session->smb2.channel_setup) {
3824 signing_key = session_key;
3827 status = smb2_signing_check_pdu(signing_key, recv_iov, 3);
3828 if (!NT_STATUS_IS_OK(status)) {
3832 if (!session->smb2.channel_setup) {
3833 session->smb2.session_key = data_blob_dup_talloc(session,
3835 if (session->smb2.session_key.data == NULL) {
3836 return NT_STATUS_NO_MEMORY;
3840 if (session->smb2.channel_setup) {
3841 data_blob_free(&session->smb2.signing_key);
3842 session->smb2.channel_setup = false;
3845 if (session->smb2.signing_key.length > 0) {
3846 return NT_STATUS_OK;
3849 session->smb2.signing_key = data_blob_dup_talloc(session, signing_key);
3850 if (session->smb2.signing_key.data == NULL) {
3851 return NT_STATUS_NO_MEMORY;
3854 session->smb2.should_sign = false;
3856 if (conn->desire_signing) {
3857 session->smb2.should_sign = true;
3860 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
3861 session->smb2.should_sign = true;
3864 return NT_STATUS_OK;
3867 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
3868 struct smbXcli_session *session1,
3869 struct smbXcli_conn *conn,
3870 struct smbXcli_session **_session2)
3872 struct smbXcli_session *session2;
3873 uint16_t no_sign_flags;
3875 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
3877 if (session1->smb2.session_flags & no_sign_flags) {
3878 return NT_STATUS_INVALID_PARAMETER_MIX;
3881 if (session1->smb2.session_key.length == 0) {
3882 return NT_STATUS_INVALID_PARAMETER_MIX;
3885 if (session1->smb2.signing_key.length == 0) {
3886 return NT_STATUS_INVALID_PARAMETER_MIX;
3890 return NT_STATUS_INVALID_PARAMETER_MIX;
3893 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
3894 if (session2 == NULL) {
3895 return NT_STATUS_NO_MEMORY;
3897 session2->smb2.session_id = session1->smb2.session_id;
3898 session2->smb2.session_flags = session1->smb2.session_flags;
3900 session2->smb2.session_key = data_blob_dup_talloc(session2,
3901 session1->smb2.session_key);
3902 if (session2->smb2.session_key.data == NULL) {
3903 return NT_STATUS_NO_MEMORY;
3906 session2->smb2.signing_key = data_blob_dup_talloc(session2,
3907 session1->smb2.signing_key);
3908 if (session2->smb2.signing_key.data == NULL) {
3909 return NT_STATUS_NO_MEMORY;
3912 session2->smb2.should_sign = session1->smb2.should_sign;
3913 session2->smb2.channel_setup = true;
3915 talloc_set_destructor(session2, smbXcli_session_destructor);
3916 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
3917 session2->conn = conn;
3919 *_session2 = session2;
3920 return NT_STATUS_OK;