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;
42 struct sockaddr_storage local_ss;
43 struct sockaddr_storage remote_ss;
44 const char *remote_name;
46 struct tevent_queue *outgoing;
47 struct tevent_req **pending;
48 struct tevent_req *read_smb_req;
50 enum protocol_types protocol;
53 bool mandatory_signing;
56 * The incoming dispatch function should return:
57 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
58 * - NT_STATUS_OK, if no more processing is desired, e.g.
59 * the dispatch function called
61 * - All other return values disconnect the connection.
63 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
69 uint32_t capabilities;
74 uint32_t capabilities;
77 uint16_t security_mode;
86 const char *workgroup;
92 uint32_t capabilities;
97 struct smb_signing_state *signing;
98 struct smb_trans_enc_state *trans_enc;
103 uint16_t security_mode;
108 uint32_t capabilities;
109 uint16_t security_mode;
111 uint32_t max_trans_size;
112 uint32_t max_read_size;
113 uint32_t max_write_size;
120 uint16_t cur_credits;
121 uint16_t max_credits;
124 struct smbXcli_session *sessions;
127 struct smbXcli_session {
128 struct smbXcli_session *prev, *next;
129 struct smbXcli_conn *conn;
133 uint16_t session_flags;
134 DATA_BLOB signing_key;
135 DATA_BLOB session_key;
141 struct smbXcli_req_state {
142 struct tevent_context *ev;
143 struct smbXcli_conn *conn;
144 struct smbXcli_session *session; /* maybe NULL */
146 uint8_t length_hdr[4];
153 /* Space for the header including the wct */
154 uint8_t hdr[HDR_VWV];
157 * For normal requests, smb1cli_req_send chooses a mid.
158 * SecondaryV trans requests need to use the mid of the primary
159 * request, so we need a place to store it.
160 * Assume it is set if != 0.
165 uint8_t bytecount_buf[2];
167 #define MAX_SMB_IOV 10
168 /* length_hdr, hdr, words, byte_count, buffers */
169 struct iovec iov[1 + 3 + MAX_SMB_IOV];
174 struct tevent_req **chained_requests;
177 NTSTATUS recv_status;
178 /* always an array of 3 talloc elements */
179 struct iovec *recv_iov;
183 const uint8_t *fixed;
189 uint8_t pad[7]; /* padding space for compounding */
191 /* always an array of 3 talloc elements */
192 struct iovec *recv_iov;
194 uint16_t credit_charge;
196 bool signing_skipped;
202 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
205 * NT_STATUS_OK, means we do not notify the callers
207 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
209 while (conn->sessions) {
210 conn->sessions->conn = NULL;
211 DLIST_REMOVE(conn->sessions, conn->sessions);
214 if (conn->smb1.trans_enc) {
215 common_free_encryption_state(&conn->smb1.trans_enc);
221 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
223 const char *remote_name,
224 enum smb_signing_setting signing_state,
225 uint32_t smb1_capabilities,
226 struct GUID *client_guid)
228 struct smbXcli_conn *conn = NULL;
230 struct sockaddr *sa = NULL;
234 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
240 conn->write_fd = dup(fd);
241 if (conn->write_fd == -1) {
245 conn->remote_name = talloc_strdup(conn, remote_name);
246 if (conn->remote_name == NULL) {
251 ss = (void *)&conn->local_ss;
252 sa = (struct sockaddr *)ss;
253 sa_length = sizeof(conn->local_ss);
254 ret = getsockname(fd, sa, &sa_length);
258 ss = (void *)&conn->remote_ss;
259 sa = (struct sockaddr *)ss;
260 sa_length = sizeof(conn->remote_ss);
261 ret = getpeername(fd, sa, &sa_length);
266 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
267 if (conn->outgoing == NULL) {
270 conn->pending = NULL;
272 conn->protocol = PROTOCOL_NONE;
274 switch (signing_state) {
275 case SMB_SIGNING_OFF:
277 conn->allow_signing = false;
278 conn->desire_signing = false;
279 conn->mandatory_signing = false;
281 case SMB_SIGNING_DEFAULT:
282 case SMB_SIGNING_IF_REQUIRED:
283 /* if the server requires it */
284 conn->allow_signing = true;
285 conn->desire_signing = false;
286 conn->mandatory_signing = false;
288 case SMB_SIGNING_REQUIRED:
290 conn->allow_signing = true;
291 conn->desire_signing = true;
292 conn->mandatory_signing = true;
296 conn->smb1.client.capabilities = smb1_capabilities;
297 conn->smb1.client.max_xmit = UINT16_MAX;
299 conn->smb1.capabilities = conn->smb1.client.capabilities;
300 conn->smb1.max_xmit = 1024;
304 /* initialise signing */
305 conn->smb1.signing = smb_signing_init(conn,
307 conn->desire_signing,
308 conn->mandatory_signing);
309 if (!conn->smb1.signing) {
313 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
314 if (conn->mandatory_signing) {
315 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
318 conn->smb2.client.guid = *client_guid;
321 conn->smb2.cur_credits = 1;
322 conn->smb2.max_credits = 0;
324 talloc_set_destructor(conn, smbXcli_conn_destructor);
328 if (conn->write_fd != -1) {
329 close(conn->write_fd);
335 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
341 if (conn->read_fd == -1) {
348 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
350 return conn->protocol;
353 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
355 if (conn->protocol >= PROTOCOL_SMB2_02) {
359 if (conn->smb1.capabilities & CAP_UNICODE) {
366 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
368 set_socket_options(conn->read_fd, options);
371 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
373 return &conn->local_ss;
376 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
378 return &conn->remote_ss;
381 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
383 return conn->remote_name;
386 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
388 if (conn->protocol >= PROTOCOL_SMB2_02) {
395 return conn->smb1.server.max_mux;
398 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
400 if (conn->protocol >= PROTOCOL_SMB2_02) {
401 return conn->smb2.server.system_time;
404 return conn->smb1.server.system_time;
407 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
409 if (conn->protocol >= PROTOCOL_SMB2_02) {
410 return &conn->smb2.server.gss_blob;
413 return &conn->smb1.server.gss_blob;
416 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
418 if (conn->protocol >= PROTOCOL_SMB2_02) {
419 return &conn->smb2.server.guid;
422 return &conn->smb1.server.guid;
425 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
427 return conn->smb1.capabilities;
430 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
432 return conn->smb1.max_xmit;
435 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
437 return conn->smb1.server.session_key;
440 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
442 return conn->smb1.server.challenge;
445 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
447 return conn->smb1.server.security_mode;
450 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
452 return conn->smb1.server.readbraw;
455 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
457 return conn->smb1.server.writebraw;
460 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
462 return conn->smb1.server.lockread;
465 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
467 return conn->smb1.server.writeunlock;
470 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
472 return conn->smb1.server.time_zone;
475 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
476 const DATA_BLOB user_session_key,
477 const DATA_BLOB response)
479 return smb_signing_activate(conn->smb1.signing,
484 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
485 const uint8_t *buf, uint32_t seqnum)
487 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
490 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
492 return smb_signing_is_active(conn->smb1.signing);
495 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
496 struct smb_trans_enc_state *es)
498 /* Replace the old state, if any. */
499 if (conn->smb1.trans_enc) {
500 common_free_encryption_state(&conn->smb1.trans_enc);
502 conn->smb1.trans_enc = es;
505 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
507 return common_encryption_on(conn->smb1.trans_enc);
511 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
513 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
514 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
516 if (NT_STATUS_IS_OK(status)) {
520 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
524 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
528 * Is the SMB command able to hold an AND_X successor
529 * @param[in] cmd The SMB command in question
530 * @retval Can we add a chained request after "cmd"?
532 bool smb1cli_is_andx_req(uint8_t cmd)
552 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
554 size_t num_pending = talloc_array_length(conn->pending);
560 result = conn->smb1.mid++;
561 if ((result == 0) || (result == 0xffff)) {
565 for (i=0; i<num_pending; i++) {
566 if (result == smb1cli_req_mid(conn->pending[i])) {
571 if (i == num_pending) {
577 void smbXcli_req_unset_pending(struct tevent_req *req)
579 struct smbXcli_req_state *state =
581 struct smbXcli_req_state);
582 struct smbXcli_conn *conn = state->conn;
583 size_t num_pending = talloc_array_length(conn->pending);
586 if (state->smb1.mid != 0) {
588 * This is a [nt]trans[2] request which waits
589 * for more than one reply.
594 talloc_set_destructor(req, NULL);
596 if (num_pending == 1) {
598 * The pending read_smb tevent_req is a child of
599 * conn->pending. So if nothing is pending anymore, we need to
600 * delete the socket read fde.
602 TALLOC_FREE(conn->pending);
603 conn->read_smb_req = NULL;
607 for (i=0; i<num_pending; i++) {
608 if (req == conn->pending[i]) {
612 if (i == num_pending) {
614 * Something's seriously broken. Just returning here is the
615 * right thing nevertheless, the point of this routine is to
616 * remove ourselves from conn->pending.
622 * Remove ourselves from the conn->pending array
624 for (; i < (num_pending - 1); i++) {
625 conn->pending[i] = conn->pending[i+1];
629 * No NULL check here, we're shrinking by sizeof(void *), and
630 * talloc_realloc just adjusts the size for this.
632 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
637 static int smbXcli_req_destructor(struct tevent_req *req)
639 struct smbXcli_req_state *state =
641 struct smbXcli_req_state);
644 * Make sure we really remove it from
645 * the pending array on destruction.
648 smbXcli_req_unset_pending(req);
652 static bool smb1cli_req_cancel(struct tevent_req *req);
653 static bool smb2cli_req_cancel(struct tevent_req *req);
655 static bool smbXcli_req_cancel(struct tevent_req *req)
657 struct smbXcli_req_state *state =
659 struct smbXcli_req_state);
661 if (!smbXcli_conn_is_connected(state->conn)) {
665 if (state->conn->protocol == PROTOCOL_NONE) {
669 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
670 return smb2cli_req_cancel(req);
673 return smb1cli_req_cancel(req);
676 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
678 bool smbXcli_req_set_pending(struct tevent_req *req)
680 struct smbXcli_req_state *state =
682 struct smbXcli_req_state);
683 struct smbXcli_conn *conn;
684 struct tevent_req **pending;
689 if (!smbXcli_conn_is_connected(conn)) {
693 num_pending = talloc_array_length(conn->pending);
695 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
697 if (pending == NULL) {
700 pending[num_pending] = req;
701 conn->pending = pending;
702 talloc_set_destructor(req, smbXcli_req_destructor);
703 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
705 if (!smbXcli_conn_receive_next(conn)) {
707 * the caller should notify the current request
709 * And all other pending requests get notified
710 * by smbXcli_conn_disconnect().
712 smbXcli_req_unset_pending(req);
713 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
720 static void smbXcli_conn_received(struct tevent_req *subreq);
722 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
724 size_t num_pending = talloc_array_length(conn->pending);
725 struct tevent_req *req;
726 struct smbXcli_req_state *state;
728 if (conn->read_smb_req != NULL) {
732 if (num_pending == 0) {
733 if (conn->smb2.mid < UINT64_MAX) {
734 /* no more pending requests, so we are done for now */
739 * If there are no more SMB2 requests possible,
740 * because we are out of message ids,
741 * we need to disconnect.
743 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
747 req = conn->pending[0];
748 state = tevent_req_data(req, struct smbXcli_req_state);
751 * We're the first ones, add the read_smb request that waits for the
752 * answer from the server
754 conn->read_smb_req = read_smb_send(conn->pending,
757 if (conn->read_smb_req == NULL) {
760 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
764 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
766 tevent_queue_stop(conn->outgoing);
768 if (conn->read_fd != -1) {
769 close(conn->read_fd);
771 if (conn->write_fd != -1) {
772 close(conn->write_fd);
778 * Cancel all pending requests. We do not do a for-loop walking
779 * conn->pending because that array changes in
780 * smbXcli_req_unset_pending.
782 while (talloc_array_length(conn->pending) > 0) {
783 struct tevent_req *req;
784 struct smbXcli_req_state *state;
785 struct tevent_req **chain;
789 req = conn->pending[0];
790 state = tevent_req_data(req, struct smbXcli_req_state);
792 if (state->smb1.chained_requests == NULL) {
794 * We're dead. No point waiting for trans2
799 smbXcli_req_unset_pending(req);
801 if (NT_STATUS_IS_OK(status)) {
802 /* do not notify the callers */
807 * we need to defer the callback, because we may notify
808 * more then one caller.
810 tevent_req_defer_callback(req, state->ev);
811 tevent_req_nterror(req, status);
815 chain = talloc_move(conn, &state->smb1.chained_requests);
816 num_chained = talloc_array_length(chain);
818 for (i=0; i<num_chained; i++) {
820 state = tevent_req_data(req, struct smbXcli_req_state);
823 * We're dead. No point waiting for trans2
828 smbXcli_req_unset_pending(req);
830 if (NT_STATUS_IS_OK(status)) {
831 /* do not notify the callers */
836 * we need to defer the callback, because we may notify
837 * more than one caller.
839 tevent_req_defer_callback(req, state->ev);
840 tevent_req_nterror(req, status);
847 * Fetch a smb request's mid. Only valid after the request has been sent by
848 * smb1cli_req_send().
850 uint16_t smb1cli_req_mid(struct tevent_req *req)
852 struct smbXcli_req_state *state =
854 struct smbXcli_req_state);
856 if (state->smb1.mid != 0) {
857 return state->smb1.mid;
860 return SVAL(state->smb1.hdr, HDR_MID);
863 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
865 struct smbXcli_req_state *state =
867 struct smbXcli_req_state);
869 state->smb1.mid = mid;
872 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
874 struct smbXcli_req_state *state =
876 struct smbXcli_req_state);
878 return state->smb1.seqnum;
881 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
883 struct smbXcli_req_state *state =
885 struct smbXcli_req_state);
887 state->smb1.seqnum = seqnum;
890 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
894 for (i=0; i<count; i++) {
895 result += iov[i].iov_len;
900 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
901 const struct iovec *iov,
904 size_t len = smbXcli_iov_len(iov, count);
909 buf = talloc_array(mem_ctx, uint8_t, len);
914 for (i=0; i<count; i++) {
915 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
916 copied += iov[i].iov_len;
921 static void smb1cli_req_flags(enum protocol_types protocol,
922 uint32_t smb1_capabilities,
924 uint8_t additional_flags,
927 uint16_t additional_flags2,
928 uint16_t clear_flags2,
934 if (protocol >= PROTOCOL_LANMAN1) {
935 flags |= FLAG_CASELESS_PATHNAMES;
936 flags |= FLAG_CANONICAL_PATHNAMES;
939 if (protocol >= PROTOCOL_LANMAN2) {
940 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
941 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
944 if (protocol >= PROTOCOL_NT1) {
945 flags2 |= FLAGS2_IS_LONG_NAME;
947 if (smb1_capabilities & CAP_UNICODE) {
948 flags2 |= FLAGS2_UNICODE_STRINGS;
950 if (smb1_capabilities & CAP_STATUS32) {
951 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
953 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
954 flags2 |= FLAGS2_EXTENDED_SECURITY;
958 flags |= additional_flags;
959 flags &= ~clear_flags;
960 flags2 |= additional_flags2;
961 flags2 &= ~clear_flags2;
967 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
969 static bool smb1cli_req_cancel(struct tevent_req *req)
971 struct smbXcli_req_state *state =
973 struct smbXcli_req_state);
980 struct tevent_req *subreq;
983 flags = CVAL(state->smb1.hdr, HDR_FLG);
984 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
985 pid = SVAL(state->smb1.hdr, HDR_PID);
986 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
987 tid = SVAL(state->smb1.hdr, HDR_TID);
988 uid = SVAL(state->smb1.hdr, HDR_UID);
989 mid = SVAL(state->smb1.hdr, HDR_MID);
991 subreq = smb1cli_req_create(state, state->ev,
999 0, NULL); /* bytes */
1000 if (subreq == NULL) {
1003 smb1cli_req_set_mid(subreq, mid);
1005 status = smb1cli_req_chain_submit(&subreq, 1);
1006 if (!NT_STATUS_IS_OK(status)) {
1007 TALLOC_FREE(subreq);
1010 smb1cli_req_set_mid(subreq, 0);
1012 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1017 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1019 /* we do not care about the result */
1020 TALLOC_FREE(subreq);
1023 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1024 struct tevent_context *ev,
1025 struct smbXcli_conn *conn,
1026 uint8_t smb_command,
1027 uint8_t additional_flags,
1028 uint8_t clear_flags,
1029 uint16_t additional_flags2,
1030 uint16_t clear_flags2,
1031 uint32_t timeout_msec,
1035 uint8_t wct, uint16_t *vwv,
1037 struct iovec *bytes_iov)
1039 struct tevent_req *req;
1040 struct smbXcli_req_state *state;
1042 uint16_t flags2 = 0;
1044 if (iov_count > MAX_SMB_IOV) {
1046 * Should not happen :-)
1051 req = tevent_req_create(mem_ctx, &state,
1052 struct smbXcli_req_state);
1059 state->smb1.recv_cmd = 0xFF;
1060 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1061 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1062 if (state->smb1.recv_iov == NULL) {
1067 smb1cli_req_flags(conn->protocol,
1068 conn->smb1.capabilities,
1077 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1078 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1079 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1080 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1081 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1082 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1083 SSVAL(state->smb1.hdr, HDR_TID, tid);
1084 SSVAL(state->smb1.hdr, HDR_PID, pid);
1085 SSVAL(state->smb1.hdr, HDR_UID, uid);
1086 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1087 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1089 state->smb1.vwv = vwv;
1091 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1093 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1094 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1095 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1096 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1097 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1098 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1099 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1100 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1102 if (iov_count != 0) {
1103 memcpy(&state->smb1.iov[4], bytes_iov,
1104 iov_count * sizeof(*bytes_iov));
1106 state->smb1.iov_count = iov_count + 4;
1108 if (timeout_msec > 0) {
1109 struct timeval endtime;
1111 endtime = timeval_current_ofs_msec(timeout_msec);
1112 if (!tevent_req_set_endtime(req, ev, endtime)) {
1117 switch (smb_command) {
1121 state->one_way = true;
1124 state->one_way = true;
1125 state->smb1.one_way_seqnum = true;
1129 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1130 state->one_way = true;
1138 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1139 struct iovec *iov, int iov_count,
1141 bool one_way_seqnum)
1143 TALLOC_CTX *frame = NULL;
1147 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1148 * iovec directly. MD5Update would do that just fine.
1151 if (iov_count < 4) {
1152 return NT_STATUS_INVALID_PARAMETER_MIX;
1154 if (iov[0].iov_len != NBT_HDR_SIZE) {
1155 return NT_STATUS_INVALID_PARAMETER_MIX;
1157 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1158 return NT_STATUS_INVALID_PARAMETER_MIX;
1160 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1161 return NT_STATUS_INVALID_PARAMETER_MIX;
1163 if (iov[3].iov_len != sizeof(uint16_t)) {
1164 return NT_STATUS_INVALID_PARAMETER_MIX;
1167 frame = talloc_stackframe();
1169 buf = smbXcli_iov_concat(frame, iov, iov_count);
1171 return NT_STATUS_NO_MEMORY;
1174 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1176 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1177 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1180 return NT_STATUS_OK;
1183 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1184 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1185 TALLOC_CTX *tmp_mem,
1188 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1189 struct smbXcli_req_state *state,
1190 struct iovec *iov, int iov_count)
1192 struct tevent_req *subreq;
1196 if (!smbXcli_conn_is_connected(state->conn)) {
1197 return NT_STATUS_CONNECTION_DISCONNECTED;
1200 if (state->conn->protocol > PROTOCOL_NT1) {
1201 return NT_STATUS_REVISION_MISMATCH;
1204 if (iov_count < 4) {
1205 return NT_STATUS_INVALID_PARAMETER_MIX;
1207 if (iov[0].iov_len != NBT_HDR_SIZE) {
1208 return NT_STATUS_INVALID_PARAMETER_MIX;
1210 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1211 return NT_STATUS_INVALID_PARAMETER_MIX;
1213 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1214 return NT_STATUS_INVALID_PARAMETER_MIX;
1216 if (iov[3].iov_len != sizeof(uint16_t)) {
1217 return NT_STATUS_INVALID_PARAMETER_MIX;
1220 if (state->smb1.mid != 0) {
1221 mid = state->smb1.mid;
1223 mid = smb1cli_alloc_mid(state->conn);
1225 SSVAL(iov[1].iov_base, HDR_MID, mid);
1227 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1229 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1230 &state->smb1.seqnum,
1231 state->smb1.one_way_seqnum);
1233 if (!NT_STATUS_IS_OK(status)) {
1238 * If we supported multiple encrytion contexts
1239 * here we'd look up based on tid.
1241 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1242 char *buf, *enc_buf;
1244 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1246 return NT_STATUS_NO_MEMORY;
1248 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1249 (char *)buf, &enc_buf);
1251 if (!NT_STATUS_IS_OK(status)) {
1252 DEBUG(0, ("Error in encrypting client message: %s\n",
1253 nt_errstr(status)));
1256 buf = (char *)talloc_memdup(state, enc_buf,
1257 smb_len_nbt(enc_buf)+4);
1260 return NT_STATUS_NO_MEMORY;
1262 iov[0].iov_base = (void *)buf;
1263 iov[0].iov_len = talloc_get_size(buf);
1267 if (state->conn->dispatch_incoming == NULL) {
1268 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1271 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1273 subreq = writev_send(state, state->ev, state->conn->outgoing,
1274 state->conn->write_fd, false, iov, iov_count);
1275 if (subreq == NULL) {
1276 return NT_STATUS_NO_MEMORY;
1278 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1279 return NT_STATUS_OK;
1282 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1283 struct tevent_context *ev,
1284 struct smbXcli_conn *conn,
1285 uint8_t smb_command,
1286 uint8_t additional_flags,
1287 uint8_t clear_flags,
1288 uint16_t additional_flags2,
1289 uint16_t clear_flags2,
1290 uint32_t timeout_msec,
1294 uint8_t wct, uint16_t *vwv,
1296 const uint8_t *bytes)
1298 struct tevent_req *req;
1302 iov.iov_base = discard_const_p(void, bytes);
1303 iov.iov_len = num_bytes;
1305 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1306 additional_flags, clear_flags,
1307 additional_flags2, clear_flags2,
1314 if (!tevent_req_is_in_progress(req)) {
1315 return tevent_req_post(req, ev);
1317 status = smb1cli_req_chain_submit(&req, 1);
1318 if (tevent_req_nterror(req, status)) {
1319 return tevent_req_post(req, ev);
1324 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1326 struct tevent_req *req =
1327 tevent_req_callback_data(subreq,
1329 struct smbXcli_req_state *state =
1330 tevent_req_data(req,
1331 struct smbXcli_req_state);
1335 nwritten = writev_recv(subreq, &err);
1336 TALLOC_FREE(subreq);
1337 if (nwritten == -1) {
1338 NTSTATUS status = map_nt_error_from_unix_common(err);
1339 smbXcli_conn_disconnect(state->conn, status);
1343 if (state->one_way) {
1344 state->inbuf = NULL;
1345 tevent_req_done(req);
1349 if (!smbXcli_req_set_pending(req)) {
1350 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1355 static void smbXcli_conn_received(struct tevent_req *subreq)
1357 struct smbXcli_conn *conn =
1358 tevent_req_callback_data(subreq,
1359 struct smbXcli_conn);
1360 TALLOC_CTX *frame = talloc_stackframe();
1366 if (subreq != conn->read_smb_req) {
1367 DEBUG(1, ("Internal error: cli_smb_received called with "
1368 "unexpected subreq\n"));
1369 status = NT_STATUS_INTERNAL_ERROR;
1370 smbXcli_conn_disconnect(conn, status);
1374 conn->read_smb_req = NULL;
1376 received = read_smb_recv(subreq, frame, &inbuf, &err);
1377 TALLOC_FREE(subreq);
1378 if (received == -1) {
1379 status = map_nt_error_from_unix_common(err);
1380 smbXcli_conn_disconnect(conn, status);
1385 status = conn->dispatch_incoming(conn, frame, inbuf);
1387 if (NT_STATUS_IS_OK(status)) {
1389 * We should not do any more processing
1390 * as the dispatch function called
1391 * tevent_req_done().
1394 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1396 * We got an error, so notify all pending requests
1398 smbXcli_conn_disconnect(conn, status);
1403 * We got NT_STATUS_RETRY, so we may ask for a
1404 * next incoming pdu.
1406 if (!smbXcli_conn_receive_next(conn)) {
1407 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1411 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1412 struct iovec **piov, int *pnum_iov)
1423 buflen = smb_len_nbt(buf);
1426 hdr = buf + NBT_HDR_SIZE;
1428 if (buflen < MIN_SMB_SIZE) {
1429 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1433 * This returns iovec elements in the following order:
1448 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1450 return NT_STATUS_NO_MEMORY;
1452 iov[0].iov_base = hdr;
1453 iov[0].iov_len = HDR_WCT;
1456 cmd = CVAL(hdr, HDR_COM);
1460 size_t len = buflen - taken;
1462 struct iovec *iov_tmp;
1469 * we need at least WCT and BCC
1471 needed = sizeof(uint8_t) + sizeof(uint16_t);
1473 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1474 __location__, (int)len, (int)needed));
1479 * Now we check if the specified words are there
1481 wct = CVAL(hdr, wct_ofs);
1482 needed += wct * sizeof(uint16_t);
1484 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1485 __location__, (int)len, (int)needed));
1490 * Now we check if the specified bytes are there
1492 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1493 bcc = SVAL(hdr, bcc_ofs);
1494 needed += bcc * sizeof(uint8_t);
1496 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1497 __location__, (int)len, (int)needed));
1502 * we allocate 2 iovec structures for words and bytes
1504 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1506 if (iov_tmp == NULL) {
1508 return NT_STATUS_NO_MEMORY;
1511 cur = &iov[num_iov];
1514 cur[0].iov_len = wct * sizeof(uint16_t);
1515 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1516 cur[1].iov_len = bcc * sizeof(uint8_t);
1517 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1521 if (!smb1cli_is_andx_req(cmd)) {
1523 * If the current command does not have AndX chanining
1529 if (wct == 0 && bcc == 0) {
1531 * An empty response also ends the chain,
1532 * most likely with an error.
1538 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1539 __location__, (int)wct, (int)cmd));
1542 cmd = CVAL(cur[0].iov_base, 0);
1545 * If it is the end of the chain we are also done.
1549 wct_ofs = SVAL(cur[0].iov_base, 2);
1551 if (wct_ofs < taken) {
1552 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1554 if (wct_ofs > buflen) {
1555 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1559 * we consumed everything up to the start of the next
1565 remaining = buflen - taken;
1567 if (remaining > 0 && num_iov >= 3) {
1569 * The last DATA block gets the remaining
1570 * bytes, this is needed to support
1571 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1573 iov[num_iov-1].iov_len += remaining;
1577 *pnum_iov = num_iov;
1578 return NT_STATUS_OK;
1582 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1585 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1586 TALLOC_CTX *tmp_mem,
1589 struct tevent_req *req;
1590 struct smbXcli_req_state *state;
1597 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1598 struct iovec *iov = NULL;
1600 struct tevent_req **chain = NULL;
1601 size_t num_chained = 0;
1602 size_t num_responses = 0;
1604 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1605 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1606 DEBUG(10, ("Got non-SMB PDU\n"));
1607 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1611 * If we supported multiple encrytion contexts
1612 * here we'd look up based on tid.
1614 if (common_encryption_on(conn->smb1.trans_enc)
1615 && (CVAL(inbuf, 0) == 0)) {
1616 uint16_t enc_ctx_num;
1618 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1619 if (!NT_STATUS_IS_OK(status)) {
1620 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1621 nt_errstr(status)));
1625 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1626 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1628 conn->smb1.trans_enc->enc_ctx_num));
1629 return NT_STATUS_INVALID_HANDLE;
1632 status = common_decrypt_buffer(conn->smb1.trans_enc,
1634 if (!NT_STATUS_IS_OK(status)) {
1635 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1636 nt_errstr(status)));
1641 mid = SVAL(inhdr, HDR_MID);
1642 num_pending = talloc_array_length(conn->pending);
1644 for (i=0; i<num_pending; i++) {
1645 if (mid == smb1cli_req_mid(conn->pending[i])) {
1649 if (i == num_pending) {
1650 /* Dump unexpected reply */
1651 return NT_STATUS_RETRY;
1654 oplock_break = false;
1656 if (mid == 0xffff) {
1658 * Paranoia checks that this is really an oplock break request.
1660 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1661 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1662 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1663 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1664 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1666 if (!oplock_break) {
1667 /* Dump unexpected reply */
1668 return NT_STATUS_RETRY;
1672 req = conn->pending[i];
1673 state = tevent_req_data(req, struct smbXcli_req_state);
1675 if (!oplock_break /* oplock breaks are not signed */
1676 && !smb_signing_check_pdu(conn->smb1.signing,
1677 inbuf, state->smb1.seqnum+1)) {
1678 DEBUG(10, ("cli_check_sign_mac failed\n"));
1679 return NT_STATUS_ACCESS_DENIED;
1682 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1684 if (!NT_STATUS_IS_OK(status)) {
1685 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1686 nt_errstr(status)));
1690 cmd = CVAL(inhdr, HDR_COM);
1691 status = smb1cli_pull_raw_error(inhdr);
1693 if (state->smb1.chained_requests == NULL) {
1695 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1698 smbXcli_req_unset_pending(req);
1700 if (state->smb1.recv_iov == NULL) {
1702 * For requests with more than
1703 * one response, we have to readd the
1706 state->smb1.recv_iov = talloc_zero_array(state,
1709 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1710 return NT_STATUS_OK;
1714 state->smb1.recv_cmd = cmd;
1715 state->smb1.recv_status = status;
1716 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1718 state->smb1.recv_iov[0] = iov[0];
1719 state->smb1.recv_iov[1] = iov[1];
1720 state->smb1.recv_iov[2] = iov[2];
1722 if (talloc_array_length(conn->pending) == 0) {
1723 tevent_req_done(req);
1724 return NT_STATUS_OK;
1727 tevent_req_defer_callback(req, state->ev);
1728 tevent_req_done(req);
1729 return NT_STATUS_RETRY;
1732 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1733 num_chained = talloc_array_length(chain);
1734 num_responses = (num_iov - 1)/2;
1736 if (num_responses > num_chained) {
1737 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1740 for (i=0; i<num_chained; i++) {
1741 size_t iov_idx = 1 + (i*2);
1742 struct iovec *cur = &iov[iov_idx];
1746 state = tevent_req_data(req, struct smbXcli_req_state);
1748 smbXcli_req_unset_pending(req);
1751 * as we finish multiple requests here
1752 * we need to defer the callbacks as
1753 * they could destroy our current stack state.
1755 tevent_req_defer_callback(req, state->ev);
1757 if (i >= num_responses) {
1758 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1762 if (state->smb1.recv_iov == NULL) {
1764 * For requests with more than
1765 * one response, we have to readd the
1768 state->smb1.recv_iov = talloc_zero_array(state,
1771 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1776 state->smb1.recv_cmd = cmd;
1778 if (i == (num_responses - 1)) {
1780 * The last request in the chain gets the status
1782 state->smb1.recv_status = status;
1784 cmd = CVAL(cur[0].iov_base, 0);
1785 state->smb1.recv_status = NT_STATUS_OK;
1788 state->inbuf = inbuf;
1791 * Note: here we use talloc_reference() in a way
1792 * that does not expose it to the caller.
1794 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1795 if (tevent_req_nomem(inbuf_ref, req)) {
1799 /* copy the related buffers */
1800 state->smb1.recv_iov[0] = iov[0];
1801 state->smb1.recv_iov[1] = cur[0];
1802 state->smb1.recv_iov[2] = cur[1];
1804 tevent_req_done(req);
1807 return NT_STATUS_RETRY;
1810 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
1811 TALLOC_CTX *mem_ctx,
1812 struct iovec **piov,
1816 uint32_t *pvwv_offset,
1817 uint32_t *pnum_bytes,
1819 uint32_t *pbytes_offset,
1821 const struct smb1cli_req_expected_response *expected,
1822 size_t num_expected)
1824 struct smbXcli_req_state *state =
1825 tevent_req_data(req,
1826 struct smbXcli_req_state);
1827 NTSTATUS status = NT_STATUS_OK;
1828 struct iovec *recv_iov = NULL;
1829 uint8_t *hdr = NULL;
1831 uint32_t vwv_offset = 0;
1832 uint16_t *vwv = NULL;
1833 uint32_t num_bytes = 0;
1834 uint32_t bytes_offset = 0;
1835 uint8_t *bytes = NULL;
1837 bool found_status = false;
1838 bool found_size = false;
1852 if (pvwv_offset != NULL) {
1855 if (pnum_bytes != NULL) {
1858 if (pbytes != NULL) {
1861 if (pbytes_offset != NULL) {
1864 if (pinbuf != NULL) {
1868 if (state->inbuf != NULL) {
1869 recv_iov = state->smb1.recv_iov;
1870 state->smb1.recv_iov = NULL;
1871 hdr = (uint8_t *)recv_iov[0].iov_base;
1872 wct = recv_iov[1].iov_len/2;
1873 vwv = (uint16_t *)recv_iov[1].iov_base;
1874 vwv_offset = PTR_DIFF(vwv, hdr);
1875 num_bytes = recv_iov[2].iov_len;
1876 bytes = (uint8_t *)recv_iov[2].iov_base;
1877 bytes_offset = PTR_DIFF(bytes, hdr);
1880 if (tevent_req_is_nterror(req, &status)) {
1881 for (i=0; i < num_expected; i++) {
1882 if (NT_STATUS_EQUAL(status, expected[i].status)) {
1883 found_status = true;
1889 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1895 if (num_expected == 0) {
1896 found_status = true;
1900 status = state->smb1.recv_status;
1902 for (i=0; i < num_expected; i++) {
1903 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
1907 found_status = true;
1908 if (expected[i].wct == 0) {
1913 if (expected[i].wct == wct) {
1919 if (!found_status) {
1924 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1928 *piov = talloc_move(mem_ctx, &recv_iov);
1940 if (pvwv_offset != NULL) {
1941 *pvwv_offset = vwv_offset;
1943 if (pnum_bytes != NULL) {
1944 *pnum_bytes = num_bytes;
1946 if (pbytes != NULL) {
1949 if (pbytes_offset != NULL) {
1950 *pbytes_offset = bytes_offset;
1952 if (pinbuf != NULL) {
1953 *pinbuf = state->inbuf;
1959 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
1966 for (i=0; i<num_reqs; i++) {
1967 struct smbXcli_req_state *state;
1968 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
1969 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
1970 state->smb1.iov_count-2);
1971 wct_ofs = (wct_ofs + 3) & ~3;
1976 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
1978 struct smbXcli_req_state *first_state =
1979 tevent_req_data(reqs[0],
1980 struct smbXcli_req_state);
1981 struct smbXcli_req_state *state;
1983 size_t chain_padding = 0;
1985 struct iovec *iov = NULL;
1986 struct iovec *this_iov;
1990 if (num_reqs == 1) {
1991 return smb1cli_req_writev_submit(reqs[0], first_state,
1992 first_state->smb1.iov,
1993 first_state->smb1.iov_count);
1997 for (i=0; i<num_reqs; i++) {
1998 if (!tevent_req_is_in_progress(reqs[i])) {
1999 return NT_STATUS_INTERNAL_ERROR;
2002 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2004 if (state->smb1.iov_count < 4) {
2005 return NT_STATUS_INVALID_PARAMETER_MIX;
2010 * The NBT and SMB header
2023 iovlen += state->smb1.iov_count - 2;
2026 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2028 return NT_STATUS_NO_MEMORY;
2031 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2032 first_state, reqs, sizeof(*reqs) * num_reqs);
2033 if (first_state->smb1.chained_requests == NULL) {
2035 return NT_STATUS_NO_MEMORY;
2038 wct_offset = HDR_WCT;
2041 for (i=0; i<num_reqs; i++) {
2042 size_t next_padding = 0;
2045 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2047 if (i < num_reqs-1) {
2048 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2049 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2051 TALLOC_FREE(first_state->smb1.chained_requests);
2052 return NT_STATUS_INVALID_PARAMETER_MIX;
2056 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2057 state->smb1.iov_count-2) + 1;
2058 if ((wct_offset % 4) != 0) {
2059 next_padding = 4 - (wct_offset % 4);
2061 wct_offset += next_padding;
2062 vwv = state->smb1.vwv;
2064 if (i < num_reqs-1) {
2065 struct smbXcli_req_state *next_state =
2066 tevent_req_data(reqs[i+1],
2067 struct smbXcli_req_state);
2068 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2070 SSVAL(vwv+1, 0, wct_offset);
2071 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2072 /* properly end the chain */
2073 SCVAL(vwv+0, 0, 0xff);
2074 SCVAL(vwv+0, 1, 0xff);
2080 * The NBT and SMB header
2082 this_iov[0] = state->smb1.iov[0];
2083 this_iov[1] = state->smb1.iov[1];
2087 * This one is a bit subtle. We have to add
2088 * chain_padding bytes between the requests, and we
2089 * have to also include the wct field of the
2090 * subsequent requests. We use the subsequent header
2091 * for the padding, it contains the wct field in its
2094 this_iov[0].iov_len = chain_padding+1;
2095 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2096 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2097 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2102 * copy the words and bytes
2104 memcpy(this_iov, state->smb1.iov+2,
2105 sizeof(struct iovec) * (state->smb1.iov_count-2));
2106 this_iov += state->smb1.iov_count - 2;
2107 chain_padding = next_padding;
2110 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2111 if (nbt_len > first_state->conn->smb1.max_xmit) {
2113 TALLOC_FREE(first_state->smb1.chained_requests);
2114 return NT_STATUS_INVALID_PARAMETER_MIX;
2117 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2118 if (!NT_STATUS_IS_OK(status)) {
2120 TALLOC_FREE(first_state->smb1.chained_requests);
2124 return NT_STATUS_OK;
2127 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2129 return ((tevent_queue_length(conn->outgoing) != 0)
2130 || (talloc_array_length(conn->pending) != 0));
2133 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2135 return conn->smb2.server.capabilities;
2138 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2140 return conn->smb2.server.security_mode;
2143 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2145 return conn->smb2.server.max_trans_size;
2148 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2150 return conn->smb2.server.max_read_size;
2153 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2155 return conn->smb2.server.max_write_size;
2158 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2159 uint16_t max_credits)
2161 conn->smb2.max_credits = max_credits;
2164 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2166 static bool smb2cli_req_cancel(struct tevent_req *req)
2168 struct smbXcli_req_state *state =
2169 tevent_req_data(req,
2170 struct smbXcli_req_state);
2171 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2172 uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
2173 uint32_t tid = IVAL(state->smb2.hdr, SMB2_HDR_TID);
2174 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2175 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2176 struct smbXcli_session *session = state->session;
2177 uint8_t *fixed = state->smb2.pad;
2178 uint16_t fixed_len = 4;
2179 struct tevent_req *subreq;
2180 struct smbXcli_req_state *substate;
2183 SSVAL(fixed, 0, 0x04);
2186 subreq = smb2cli_req_create(state, state->ev,
2194 if (subreq == NULL) {
2197 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2199 if (flags & SMB2_HDR_FLAG_ASYNC) {
2203 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2204 SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
2205 SIVAL(substate->smb2.hdr, SMB2_HDR_TID, tid);
2206 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2207 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2209 status = smb2cli_req_compound_submit(&subreq, 1);
2210 if (!NT_STATUS_IS_OK(status)) {
2211 TALLOC_FREE(subreq);
2215 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2220 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2222 /* we do not care about the result */
2223 TALLOC_FREE(subreq);
2226 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2227 struct tevent_context *ev,
2228 struct smbXcli_conn *conn,
2230 uint32_t additional_flags,
2231 uint32_t clear_flags,
2232 uint32_t timeout_msec,
2235 struct smbXcli_session *session,
2236 const uint8_t *fixed,
2241 struct tevent_req *req;
2242 struct smbXcli_req_state *state;
2246 req = tevent_req_create(mem_ctx, &state,
2247 struct smbXcli_req_state);
2254 state->session = session;
2257 uid = session->smb2.session_id;
2260 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2261 if (state->smb2.recv_iov == NULL) {
2266 flags |= additional_flags;
2267 flags &= ~clear_flags;
2269 state->smb2.fixed = fixed;
2270 state->smb2.fixed_len = fixed_len;
2271 state->smb2.dyn = dyn;
2272 state->smb2.dyn_len = dyn_len;
2274 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2275 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2276 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2277 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2278 SIVAL(state->smb2.hdr, SMB2_HDR_PID, pid);
2279 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2280 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2283 case SMB2_OP_CANCEL:
2284 state->one_way = true;
2288 * If this is a dummy request, it will have
2289 * UINT64_MAX as message id.
2290 * If we send on break acknowledgement,
2291 * this gets overwritten later.
2293 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2297 if (timeout_msec > 0) {
2298 struct timeval endtime;
2300 endtime = timeval_current_ofs_msec(timeout_msec);
2301 if (!tevent_req_set_endtime(req, ev, endtime)) {
2309 void smb2cli_req_set_notify_async(struct tevent_req *req)
2311 struct smbXcli_req_state *state =
2312 tevent_req_data(req,
2313 struct smbXcli_req_state);
2315 state->smb2.notify_async = true;
2318 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2319 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2320 TALLOC_CTX *tmp_mem,
2323 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2326 struct smbXcli_req_state *state;
2327 struct tevent_req *subreq;
2329 int i, num_iov, nbt_len;
2332 * 1 for the nbt length
2333 * per request: HDR, fixed, dyn, padding
2334 * -1 because the last one does not need padding
2337 iov = talloc_array(reqs[0], struct iovec, 1 + 4*num_reqs - 1);
2339 return NT_STATUS_NO_MEMORY;
2345 for (i=0; i<num_reqs; i++) {
2354 bool should_sign = false;
2356 if (!tevent_req_is_in_progress(reqs[i])) {
2357 return NT_STATUS_INTERNAL_ERROR;
2360 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2362 if (!smbXcli_conn_is_connected(state->conn)) {
2363 return NT_STATUS_CONNECTION_DISCONNECTED;
2366 if ((state->conn->protocol != PROTOCOL_NONE) &&
2367 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2368 return NT_STATUS_REVISION_MISMATCH;
2371 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2372 if (opcode == SMB2_OP_CANCEL) {
2376 avail = UINT64_MAX - state->conn->smb2.mid;
2378 return NT_STATUS_CONNECTION_ABORTED;
2381 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2382 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2387 charge = MAX(state->smb2.credit_charge, charge);
2389 avail = MIN(avail, state->conn->smb2.cur_credits);
2390 if (avail < charge) {
2391 return NT_STATUS_INTERNAL_ERROR;
2395 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2396 credits = state->conn->smb2.max_credits -
2397 state->conn->smb2.cur_credits;
2399 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2403 mid = state->conn->smb2.mid;
2404 state->conn->smb2.mid += charge;
2405 state->conn->smb2.cur_credits -= charge;
2407 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2408 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2410 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2411 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2415 iov[num_iov].iov_base = state->smb2.hdr;
2416 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2419 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2420 iov[num_iov].iov_len = state->smb2.fixed_len;
2423 if (state->smb2.dyn != NULL) {
2424 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2425 iov[num_iov].iov_len = state->smb2.dyn_len;
2429 reqlen = sizeof(state->smb2.hdr);
2430 reqlen += state->smb2.fixed_len;
2431 reqlen += state->smb2.dyn_len;
2433 if (i < num_reqs-1) {
2434 if ((reqlen % 8) > 0) {
2435 uint8_t pad = 8 - (reqlen % 8);
2436 iov[num_iov].iov_base = state->smb2.pad;
2437 iov[num_iov].iov_len = pad;
2441 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2445 if (state->session) {
2446 should_sign = state->session->smb2.should_sign;
2447 if (state->session->smb2.channel_setup) {
2455 status = smb2_signing_sign_pdu(state->session->smb2.signing_key,
2456 &iov[hdr_iov], num_iov - hdr_iov);
2457 if (!NT_STATUS_IS_OK(status)) {
2462 ret = smbXcli_req_set_pending(reqs[i]);
2464 return NT_STATUS_NO_MEMORY;
2468 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2469 _smb_setlen_tcp(state->length_hdr, nbt_len);
2470 iov[0].iov_base = state->length_hdr;
2471 iov[0].iov_len = sizeof(state->length_hdr);
2473 if (state->conn->dispatch_incoming == NULL) {
2474 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2477 subreq = writev_send(state, state->ev, state->conn->outgoing,
2478 state->conn->write_fd, false, iov, num_iov);
2479 if (subreq == NULL) {
2480 return NT_STATUS_NO_MEMORY;
2482 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2483 return NT_STATUS_OK;
2486 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2488 struct smbXcli_req_state *state =
2489 tevent_req_data(req,
2490 struct smbXcli_req_state);
2492 state->smb2.credit_charge = charge;
2495 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2496 struct tevent_context *ev,
2497 struct smbXcli_conn *conn,
2499 uint32_t additional_flags,
2500 uint32_t clear_flags,
2501 uint32_t timeout_msec,
2504 struct smbXcli_session *session,
2505 const uint8_t *fixed,
2510 struct tevent_req *req;
2513 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2514 additional_flags, clear_flags,
2517 fixed, fixed_len, dyn, dyn_len);
2521 if (!tevent_req_is_in_progress(req)) {
2522 return tevent_req_post(req, ev);
2524 status = smb2cli_req_compound_submit(&req, 1);
2525 if (tevent_req_nterror(req, status)) {
2526 return tevent_req_post(req, ev);
2531 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2533 struct tevent_req *req =
2534 tevent_req_callback_data(subreq,
2536 struct smbXcli_req_state *state =
2537 tevent_req_data(req,
2538 struct smbXcli_req_state);
2542 nwritten = writev_recv(subreq, &err);
2543 TALLOC_FREE(subreq);
2544 if (nwritten == -1) {
2545 /* here, we need to notify all pending requests */
2546 NTSTATUS status = map_nt_error_from_unix_common(err);
2547 smbXcli_conn_disconnect(state->conn, status);
2552 static NTSTATUS smb2cli_inbuf_parse_compound(uint8_t *buf, TALLOC_CTX *mem_ctx,
2553 struct iovec **piov, int *pnum_iov)
2563 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2565 return NT_STATUS_NO_MEMORY;
2568 buflen = smb_len_tcp(buf);
2570 first_hdr = buf + NBT_HDR_SIZE;
2572 while (taken < buflen) {
2573 size_t len = buflen - taken;
2574 uint8_t *hdr = first_hdr + taken;
2577 size_t next_command_ofs;
2579 struct iovec *iov_tmp;
2582 * We need the header plus the body length field
2585 if (len < SMB2_HDR_BODY + 2) {
2586 DEBUG(10, ("%d bytes left, expected at least %d\n",
2587 (int)len, SMB2_HDR_BODY));
2590 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2591 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2595 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2596 DEBUG(10, ("Got HDR len %d, expected %d\n",
2597 SVAL(hdr, 4), SMB2_HDR_BODY));
2602 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2603 body_size = SVAL(hdr, SMB2_HDR_BODY);
2605 if (next_command_ofs != 0) {
2606 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2609 if (next_command_ofs > full_size) {
2612 full_size = next_command_ofs;
2614 if (body_size < 2) {
2617 body_size &= 0xfffe;
2619 if (body_size > (full_size - SMB2_HDR_BODY)) {
2623 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2625 if (iov_tmp == NULL) {
2627 return NT_STATUS_NO_MEMORY;
2630 cur = &iov[num_iov];
2633 cur[0].iov_base = hdr;
2634 cur[0].iov_len = SMB2_HDR_BODY;
2635 cur[1].iov_base = hdr + SMB2_HDR_BODY;
2636 cur[1].iov_len = body_size;
2637 cur[2].iov_base = hdr + SMB2_HDR_BODY + body_size;
2638 cur[2].iov_len = full_size - (SMB2_HDR_BODY + body_size);
2644 *pnum_iov = num_iov;
2645 return NT_STATUS_OK;
2649 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2652 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
2655 size_t num_pending = talloc_array_length(conn->pending);
2658 for (i=0; i<num_pending; i++) {
2659 struct tevent_req *req = conn->pending[i];
2660 struct smbXcli_req_state *state =
2661 tevent_req_data(req,
2662 struct smbXcli_req_state);
2664 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
2671 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2672 TALLOC_CTX *tmp_mem,
2675 struct tevent_req *req;
2676 struct smbXcli_req_state *state = NULL;
2681 struct smbXcli_session *last_session = NULL;
2683 status = smb2cli_inbuf_parse_compound(inbuf, tmp_mem,
2685 if (!NT_STATUS_IS_OK(status)) {
2689 for (i=0; i<num_iov; i+=3) {
2690 uint8_t *inbuf_ref = NULL;
2691 struct iovec *cur = &iov[i];
2692 uint8_t *inhdr = (uint8_t *)cur[0].iov_base;
2693 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
2694 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
2695 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
2696 uint16_t req_opcode;
2698 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
2699 uint32_t new_credits;
2700 struct smbXcli_session *session = NULL;
2701 const DATA_BLOB *signing_key = NULL;
2702 bool should_sign = false;
2704 new_credits = conn->smb2.cur_credits;
2705 new_credits += credits;
2706 if (new_credits > UINT16_MAX) {
2707 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2709 conn->smb2.cur_credits += credits;
2711 req = smb2cli_conn_find_pending(conn, mid);
2713 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2715 state = tevent_req_data(req, struct smbXcli_req_state);
2717 state->smb2.got_async = false;
2719 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2720 if (opcode != req_opcode) {
2721 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2723 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2725 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
2726 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2729 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
2730 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
2731 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
2732 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
2735 * async interim responses are not signed,
2736 * even if the SMB2_HDR_FLAG_SIGNED flag
2739 req_flags |= SMB2_HDR_FLAG_ASYNC;
2740 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
2741 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
2743 if (state->smb2.notify_async) {
2744 state->smb2.got_async = true;
2745 tevent_req_defer_callback(req, state->ev);
2746 tevent_req_notify_callback(req);
2751 session = state->session;
2752 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
2753 session = last_session;
2755 last_session = session;
2758 should_sign = session->smb2.should_sign;
2759 if (session->smb2.channel_setup) {
2765 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
2766 return NT_STATUS_ACCESS_DENIED;
2770 if (flags & SMB2_HDR_FLAG_SIGNED) {
2771 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
2773 if (session == NULL) {
2774 struct smbXcli_session *s;
2776 s = state->conn->sessions;
2777 for (; s; s = s->next) {
2778 if (s->smb2.session_id != uid) {
2787 if (session == NULL) {
2788 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2791 last_session = session;
2792 signing_key = &session->smb2.signing_key;
2795 if ((opcode == SMB2_OP_SESSSETUP) &&
2796 NT_STATUS_IS_OK(status)) {
2798 * the caller has to check the signing
2799 * as only the caller knows the correct
2805 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
2807 * if the server returns NT_STATUS_USER_SESSION_DELETED
2808 * the response is not signed and we should
2809 * propagate the NT_STATUS_USER_SESSION_DELETED
2810 * status to the caller.
2817 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
2818 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
2819 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
2821 * if the server returns
2822 * NT_STATUS_NETWORK_NAME_DELETED
2823 * NT_STATUS_FILE_CLOSED
2824 * NT_STATUS_INVALID_PARAMETER
2825 * the response might not be signed
2826 * as this happens before the signing checks.
2828 * If server echos the signature (or all zeros)
2829 * we should report the status from the server
2835 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
2836 state->smb2.hdr+SMB2_HDR_SIGNATURE,
2839 state->smb2.signing_skipped = true;
2845 static const uint8_t zeros[16];
2847 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
2851 state->smb2.signing_skipped = true;
2858 status = smb2_signing_check_pdu(*signing_key, cur, 3);
2859 if (!NT_STATUS_IS_OK(status)) {
2861 * If the signing check fails, we disconnect
2868 smbXcli_req_unset_pending(req);
2871 * There might be more than one response
2872 * we need to defer the notifications
2874 if ((num_iov == 4) && (talloc_array_length(conn->pending) == 0)) {
2879 tevent_req_defer_callback(req, state->ev);
2883 * Note: here we use talloc_reference() in a way
2884 * that does not expose it to the caller.
2886 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
2887 if (tevent_req_nomem(inbuf_ref, req)) {
2891 /* copy the related buffers */
2892 state->smb2.recv_iov[0] = cur[0];
2893 state->smb2.recv_iov[1] = cur[1];
2894 state->smb2.recv_iov[2] = cur[2];
2896 tevent_req_done(req);
2900 return NT_STATUS_RETRY;
2903 return NT_STATUS_OK;
2906 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
2907 struct iovec **piov,
2908 const struct smb2cli_req_expected_response *expected,
2909 size_t num_expected)
2911 struct smbXcli_req_state *state =
2912 tevent_req_data(req,
2913 struct smbXcli_req_state);
2916 bool found_status = false;
2917 bool found_size = false;
2924 if (state->smb2.got_async) {
2925 return STATUS_PENDING;
2928 if (tevent_req_is_nterror(req, &status)) {
2929 for (i=0; i < num_expected; i++) {
2930 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2931 found_status = true;
2937 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2943 if (num_expected == 0) {
2944 found_status = true;
2948 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
2949 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
2951 for (i=0; i < num_expected; i++) {
2952 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2956 found_status = true;
2957 if (expected[i].body_size == 0) {
2962 if (expected[i].body_size == body_size) {
2968 if (!found_status) {
2972 if (state->smb2.signing_skipped) {
2973 if (num_expected > 0) {
2974 return NT_STATUS_ACCESS_DENIED;
2976 if (!NT_STATUS_IS_ERR(status)) {
2977 return NT_STATUS_ACCESS_DENIED;
2982 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2986 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
2992 static const struct {
2993 enum protocol_types proto;
2994 const char *smb1_name;
2995 } smb1cli_prots[] = {
2996 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
2997 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
2998 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
2999 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3000 {PROTOCOL_LANMAN2, "LM1.2X002"},
3001 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3002 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3003 {PROTOCOL_LANMAN2, "Samba"},
3004 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3005 {PROTOCOL_NT1, "NT LM 0.12"},
3006 {PROTOCOL_SMB2_02, "SMB 2.002"},
3007 {PROTOCOL_SMB2_10, "SMB 2.???"},
3010 static const struct {
3011 enum protocol_types proto;
3012 uint16_t smb2_dialect;
3013 } smb2cli_prots[] = {
3014 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3015 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3016 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3019 struct smbXcli_negprot_state {
3020 struct smbXcli_conn *conn;
3021 struct tevent_context *ev;
3022 uint32_t timeout_msec;
3023 enum protocol_types min_protocol;
3024 enum protocol_types max_protocol;
3028 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3032 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3033 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3034 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3035 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3036 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3037 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3041 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3042 struct tevent_context *ev,
3043 struct smbXcli_conn *conn,
3044 uint32_t timeout_msec,
3045 enum protocol_types min_protocol,
3046 enum protocol_types max_protocol)
3048 struct tevent_req *req, *subreq;
3049 struct smbXcli_negprot_state *state;
3051 req = tevent_req_create(mem_ctx, &state,
3052 struct smbXcli_negprot_state);
3058 state->timeout_msec = timeout_msec;
3059 state->min_protocol = min_protocol;
3060 state->max_protocol = max_protocol;
3062 if (min_protocol == PROTOCOL_NONE) {
3063 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3064 return tevent_req_post(req, ev);
3067 if (max_protocol == PROTOCOL_NONE) {
3068 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3069 return tevent_req_post(req, ev);
3072 if (min_protocol > max_protocol) {
3073 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3074 return tevent_req_post(req, ev);
3077 if ((min_protocol < PROTOCOL_SMB2_02) &&
3078 (max_protocol < PROTOCOL_SMB2_02)) {
3082 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3084 subreq = smbXcli_negprot_smb1_subreq(state);
3085 if (tevent_req_nomem(subreq, req)) {
3086 return tevent_req_post(req, ev);
3088 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3092 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3093 (max_protocol >= PROTOCOL_SMB2_02)) {
3097 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3099 subreq = smbXcli_negprot_smb2_subreq(state);
3100 if (tevent_req_nomem(subreq, req)) {
3101 return tevent_req_post(req, ev);
3103 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3108 * We send an SMB1 negprot with the SMB2 dialects
3109 * and expect a SMB1 or a SMB2 response.
3111 * smbXcli_negprot_dispatch_incoming() will fix the
3112 * callback to match protocol of the response.
3114 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3116 subreq = smbXcli_negprot_smb1_subreq(state);
3117 if (tevent_req_nomem(subreq, req)) {
3118 return tevent_req_post(req, ev);
3120 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3124 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3126 struct tevent_req *req =
3127 tevent_req_callback_data(subreq,
3132 * we just want the low level error
3134 status = tevent_req_simple_recv_ntstatus(subreq);
3135 TALLOC_FREE(subreq);
3136 if (tevent_req_nterror(req, status)) {
3140 /* this should never happen */
3141 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3144 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3147 DATA_BLOB bytes = data_blob_null;
3151 /* setup the protocol strings */
3152 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3156 if (smb1cli_prots[i].proto < state->min_protocol) {
3160 if (smb1cli_prots[i].proto > state->max_protocol) {
3164 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3170 * We now it is already ascii and
3171 * we want NULL termination.
3173 ok = data_blob_append(state, &bytes,
3174 smb1cli_prots[i].smb1_name,
3175 strlen(smb1cli_prots[i].smb1_name)+1);
3181 smb1cli_req_flags(state->max_protocol,
3182 state->conn->smb1.client.capabilities,
3187 return smb1cli_req_send(state, state->ev, state->conn,
3191 state->timeout_msec,
3192 0xFFFE, 0, 0, /* pid, tid, uid */
3193 0, NULL, /* wct, vwv */
3194 bytes.length, bytes.data);
3197 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3199 struct tevent_req *req =
3200 tevent_req_callback_data(subreq,
3202 struct smbXcli_negprot_state *state =
3203 tevent_req_data(req,
3204 struct smbXcli_negprot_state);
3205 struct smbXcli_conn *conn = state->conn;
3206 struct iovec *recv_iov = NULL;
3215 size_t num_prots = 0;
3217 uint32_t client_capabilities = conn->smb1.client.capabilities;
3218 uint32_t both_capabilities;
3219 uint32_t server_capabilities = 0;
3220 uint32_t capabilities;
3221 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3222 uint32_t server_max_xmit = 0;
3224 uint32_t server_max_mux = 0;
3225 uint16_t server_security_mode = 0;
3226 uint32_t server_session_key = 0;
3227 bool server_readbraw = false;
3228 bool server_writebraw = false;
3229 bool server_lockread = false;
3230 bool server_writeunlock = false;
3231 struct GUID server_guid = GUID_zero();
3232 DATA_BLOB server_gss_blob = data_blob_null;
3233 uint8_t server_challenge[8];
3234 char *server_workgroup = NULL;
3235 char *server_name = NULL;
3236 int server_time_zone = 0;
3237 NTTIME server_system_time = 0;
3238 static const struct smb1cli_req_expected_response expected[] = {
3240 .status = NT_STATUS_OK,
3241 .wct = 0x11, /* NT1 */
3244 .status = NT_STATUS_OK,
3245 .wct = 0x0D, /* LM */
3248 .status = NT_STATUS_OK,
3249 .wct = 0x01, /* CORE */
3253 ZERO_STRUCT(server_challenge);
3255 status = smb1cli_req_recv(subreq, state,
3260 NULL, /* pvwv_offset */
3263 NULL, /* pbytes_offset */
3265 expected, ARRAY_SIZE(expected));
3266 TALLOC_FREE(subreq);
3267 if (tevent_req_nterror(req, status)) {
3271 flags = CVAL(inhdr, HDR_FLG);
3273 protnum = SVAL(vwv, 0);
3275 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3276 if (smb1cli_prots[i].proto < state->min_protocol) {
3280 if (smb1cli_prots[i].proto > state->max_protocol) {
3284 if (protnum != num_prots) {
3289 conn->protocol = smb1cli_prots[i].proto;
3293 if (conn->protocol == PROTOCOL_NONE) {
3294 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3298 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3299 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3300 "and the selected protocol level doesn't support it.\n"));
3301 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3305 if (flags & FLAG_SUPPORT_LOCKREAD) {
3306 server_lockread = true;
3307 server_writeunlock = true;
3310 if (conn->protocol >= PROTOCOL_NT1) {
3311 const char *client_signing = NULL;
3312 bool server_mandatory = false;
3313 bool server_allowed = false;
3314 const char *server_signing = NULL;
3319 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3324 server_security_mode = CVAL(vwv + 1, 0);
3325 server_max_mux = SVAL(vwv + 1, 1);
3326 server_max_xmit = IVAL(vwv + 3, 1);
3327 server_session_key = IVAL(vwv + 7, 1);
3328 server_time_zone = SVALS(vwv + 15, 1);
3329 server_time_zone *= 60;
3330 /* this time arrives in real GMT */
3331 server_system_time = BVAL(vwv + 11, 1);
3332 server_capabilities = IVAL(vwv + 9, 1);
3334 key_len = CVAL(vwv + 16, 1);
3336 if (server_capabilities & CAP_RAW_MODE) {
3337 server_readbraw = true;
3338 server_writebraw = true;
3340 if (server_capabilities & CAP_LOCK_AND_READ) {
3341 server_lockread = true;
3344 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3345 DATA_BLOB blob1, blob2;
3347 if (num_bytes < 16) {
3348 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3352 blob1 = data_blob_const(bytes, 16);
3353 status = GUID_from_data_blob(&blob1, &server_guid);
3354 if (tevent_req_nterror(req, status)) {
3358 blob1 = data_blob_const(bytes+16, num_bytes-16);
3359 blob2 = data_blob_dup_talloc(state, blob1);
3360 if (blob1.length > 0 &&
3361 tevent_req_nomem(blob2.data, req)) {
3364 server_gss_blob = blob2;
3366 DATA_BLOB blob1, blob2;
3368 if (num_bytes < key_len) {
3369 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3373 if (key_len != 0 && key_len != 8) {
3374 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3379 memcpy(server_challenge, bytes, 8);
3382 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3383 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3384 if (blob1.length > 0) {
3387 len = utf16_len_n(blob1.data,
3391 ok = convert_string_talloc(state,
3399 status = map_nt_error_from_unix_common(errno);
3400 tevent_req_nterror(req, status);
3405 blob2.data += blob1.length;
3406 blob2.length -= blob1.length;
3407 if (blob2.length > 0) {
3410 len = utf16_len_n(blob1.data,
3414 ok = convert_string_talloc(state,
3422 status = map_nt_error_from_unix_common(errno);
3423 tevent_req_nterror(req, status);
3429 client_signing = "disabled";
3430 if (conn->allow_signing) {
3431 client_signing = "allowed";
3433 if (conn->mandatory_signing) {
3434 client_signing = "required";
3437 server_signing = "not supported";
3438 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3439 server_signing = "supported";
3440 server_allowed = true;
3442 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3443 server_signing = "required";
3444 server_mandatory = true;
3447 ok = smb_signing_set_negotiated(conn->smb1.signing,
3451 DEBUG(1,("cli_negprot: SMB signing is required, "
3452 "but client[%s] and server[%s] mismatch\n",
3453 client_signing, server_signing));
3454 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3458 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3464 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3468 server_security_mode = SVAL(vwv + 1, 0);
3469 server_max_xmit = SVAL(vwv + 2, 0);
3470 server_max_mux = SVAL(vwv + 3, 0);
3471 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3472 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3473 server_session_key = IVAL(vwv + 6, 0);
3474 server_time_zone = SVALS(vwv + 10, 0);
3475 server_time_zone *= 60;
3476 /* this time is converted to GMT by make_unix_date */
3477 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3478 unix_to_nt_time(&server_system_time, t);
3479 key_len = SVAL(vwv + 11, 0);
3481 if (num_bytes < key_len) {
3482 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3486 if (key_len != 0 && key_len != 8) {
3487 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3492 memcpy(server_challenge, bytes, 8);
3495 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3496 if (blob1.length > 0) {
3500 len = utf16_len_n(blob1.data,
3504 ok = convert_string_talloc(state,
3512 status = map_nt_error_from_unix_common(errno);
3513 tevent_req_nterror(req, status);
3519 /* the old core protocol */
3520 server_time_zone = get_time_zone(time(NULL));
3521 server_max_xmit = 1024;
3525 if (server_max_xmit < 1024) {
3526 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3530 if (server_max_mux < 1) {
3531 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3536 * Now calculate the negotiated capabilities
3537 * based on the mask for:
3538 * - client only flags
3539 * - flags used in both directions
3540 * - server only flags
3542 both_capabilities = client_capabilities & server_capabilities;
3543 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3544 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3545 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3547 max_xmit = MIN(client_max_xmit, server_max_xmit);
3549 conn->smb1.server.capabilities = server_capabilities;
3550 conn->smb1.capabilities = capabilities;
3552 conn->smb1.server.max_xmit = server_max_xmit;
3553 conn->smb1.max_xmit = max_xmit;
3555 conn->smb1.server.max_mux = server_max_mux;
3557 conn->smb1.server.security_mode = server_security_mode;
3559 conn->smb1.server.readbraw = server_readbraw;
3560 conn->smb1.server.writebraw = server_writebraw;
3561 conn->smb1.server.lockread = server_lockread;
3562 conn->smb1.server.writeunlock = server_writeunlock;
3564 conn->smb1.server.session_key = server_session_key;
3566 talloc_steal(conn, server_gss_blob.data);
3567 conn->smb1.server.gss_blob = server_gss_blob;
3568 conn->smb1.server.guid = server_guid;
3569 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3570 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3571 conn->smb1.server.name = talloc_move(conn, &server_name);
3573 conn->smb1.server.time_zone = server_time_zone;
3574 conn->smb1.server.system_time = server_system_time;
3576 tevent_req_done(req);
3579 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
3583 uint16_t dialect_count = 0;
3585 buf = state->smb2.dyn;
3586 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3587 if (smb2cli_prots[i].proto < state->min_protocol) {
3591 if (smb2cli_prots[i].proto > state->max_protocol) {
3595 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
3599 buf = state->smb2.fixed;
3601 SSVAL(buf, 2, dialect_count);
3602 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
3603 SSVAL(buf, 6, 0); /* Reserved */
3604 SSVAL(buf, 8, 0); /* Capabilities */
3605 if (state->max_protocol >= PROTOCOL_SMB2_10) {
3609 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
3611 if (!NT_STATUS_IS_OK(status)) {
3614 memcpy(buf+12, blob.data, 16); /* ClientGuid */
3616 memset(buf+12, 0, 16); /* ClientGuid */
3618 SBVAL(buf, 28, 0); /* ClientStartTime */
3620 return smb2cli_req_send(state, state->ev,
3621 state->conn, SMB2_OP_NEGPROT,
3623 state->timeout_msec,
3624 0xFEFF, 0, NULL, /* pid, tid, session */
3625 state->smb2.fixed, sizeof(state->smb2.fixed),
3626 state->smb2.dyn, dialect_count*2);
3629 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
3631 struct tevent_req *req =
3632 tevent_req_callback_data(subreq,
3634 struct smbXcli_negprot_state *state =
3635 tevent_req_data(req,
3636 struct smbXcli_negprot_state);
3637 struct smbXcli_conn *conn = state->conn;
3638 size_t security_offset, security_length;
3644 uint16_t dialect_revision;
3645 static const struct smb2cli_req_expected_response expected[] = {
3647 .status = NT_STATUS_OK,
3652 status = smb2cli_req_recv(subreq, state, &iov,
3653 expected, ARRAY_SIZE(expected));
3654 TALLOC_FREE(subreq);
3655 if (tevent_req_nterror(req, status)) {
3659 body = (uint8_t *)iov[1].iov_base;
3661 dialect_revision = SVAL(body, 4);
3663 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3664 if (smb2cli_prots[i].proto < state->min_protocol) {
3668 if (smb2cli_prots[i].proto > state->max_protocol) {
3672 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
3676 conn->protocol = smb2cli_prots[i].proto;
3680 if (conn->protocol == PROTOCOL_NONE) {
3681 if (state->min_protocol >= PROTOCOL_SMB2_02) {
3682 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3686 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
3687 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3691 /* make sure we do not loop forever */
3692 state->min_protocol = PROTOCOL_SMB2_02;
3695 * send a SMB2 negprot, in order to negotiate
3696 * the SMB2 dialect. This needs to use the
3699 state->conn->smb2.mid = 1;
3700 subreq = smbXcli_negprot_smb2_subreq(state);
3701 if (tevent_req_nomem(subreq, req)) {
3704 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3708 conn->smb2.server.security_mode = SVAL(body, 2);
3710 blob = data_blob_const(body + 8, 16);
3711 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
3712 if (tevent_req_nterror(req, status)) {
3716 conn->smb2.server.capabilities = IVAL(body, 24);
3717 conn->smb2.server.max_trans_size= IVAL(body, 28);
3718 conn->smb2.server.max_read_size = IVAL(body, 32);
3719 conn->smb2.server.max_write_size= IVAL(body, 36);
3720 conn->smb2.server.system_time = BVAL(body, 40);
3721 conn->smb2.server.start_time = BVAL(body, 48);
3723 security_offset = SVAL(body, 56);
3724 security_length = SVAL(body, 58);
3726 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
3727 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3731 if (security_length > iov[2].iov_len) {
3732 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3736 conn->smb2.server.gss_blob = data_blob_talloc(conn,
3739 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
3743 tevent_req_done(req);
3746 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3747 TALLOC_CTX *tmp_mem,
3750 size_t num_pending = talloc_array_length(conn->pending);
3751 struct tevent_req *subreq;
3752 struct smbXcli_req_state *substate;
3753 struct tevent_req *req;
3754 uint32_t protocol_magic = IVAL(inbuf, 4);
3756 if (num_pending != 1) {
3757 return NT_STATUS_INTERNAL_ERROR;
3760 subreq = conn->pending[0];
3761 substate = tevent_req_data(subreq, struct smbXcli_req_state);
3762 req = tevent_req_callback_data(subreq, struct tevent_req);
3764 switch (protocol_magic) {
3766 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3767 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3768 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3771 if (substate->smb2.recv_iov == NULL) {
3773 * For the SMB1 negprot we have move it.
3775 substate->smb2.recv_iov = substate->smb1.recv_iov;
3776 substate->smb1.recv_iov = NULL;
3779 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3780 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3781 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
3784 DEBUG(10, ("Got non-SMB PDU\n"));
3785 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3788 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
3790 return tevent_req_simple_recv_ntstatus(req);
3793 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
3794 uint32_t timeout_msec,
3795 enum protocol_types min_protocol,
3796 enum protocol_types max_protocol)
3798 TALLOC_CTX *frame = talloc_stackframe();
3799 struct tevent_context *ev;
3800 struct tevent_req *req;
3801 NTSTATUS status = NT_STATUS_NO_MEMORY;
3804 if (smbXcli_conn_has_async_calls(conn)) {
3806 * Can't use sync call while an async call is in flight
3808 status = NT_STATUS_INVALID_PARAMETER_MIX;
3811 ev = tevent_context_init(frame);
3815 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
3816 min_protocol, max_protocol);
3820 ok = tevent_req_poll(req, ev);
3822 status = map_nt_error_from_unix_common(errno);
3825 status = smbXcli_negprot_recv(req);
3831 static int smbXcli_session_destructor(struct smbXcli_session *session)
3833 if (session->conn == NULL) {
3837 DLIST_REMOVE(session->conn->sessions, session);
3841 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
3842 struct smbXcli_conn *conn)
3844 struct smbXcli_session *session;
3846 session = talloc_zero(mem_ctx, struct smbXcli_session);
3847 if (session == NULL) {
3850 talloc_set_destructor(session, smbXcli_session_destructor);
3852 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
3853 session->conn = conn;
3858 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
3860 struct smbXcli_conn *conn = session->conn;
3861 uint8_t security_mode = 0;
3864 return security_mode;
3867 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
3868 if (conn->mandatory_signing) {
3869 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
3872 return security_mode;
3875 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
3877 return session->smb2.session_id;
3880 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
3881 uint64_t session_id,
3882 uint16_t session_flags)
3884 session->smb2.session_id = session_id;
3885 session->smb2.session_flags = session_flags;
3888 NTSTATUS smb2cli_session_update_session_key(struct smbXcli_session *session,
3889 const DATA_BLOB session_key,
3890 const struct iovec *recv_iov)
3892 struct smbXcli_conn *conn = session->conn;
3893 uint16_t no_sign_flags;
3894 DATA_BLOB signing_key;
3898 return NT_STATUS_INVALID_PARAMETER_MIX;
3901 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
3903 if (session->smb2.session_flags & no_sign_flags) {
3904 session->smb2.should_sign = false;
3905 return NT_STATUS_OK;
3908 if (session->smb2.signing_key.length > 0) {
3909 signing_key = session->smb2.signing_key;
3911 signing_key = session_key;
3913 if (session->smb2.channel_setup) {
3914 signing_key = session_key;
3917 status = smb2_signing_check_pdu(signing_key, recv_iov, 3);
3918 if (!NT_STATUS_IS_OK(status)) {
3922 if (!session->smb2.channel_setup) {
3923 session->smb2.session_key = data_blob_dup_talloc(session,
3925 if (session->smb2.session_key.data == NULL) {
3926 return NT_STATUS_NO_MEMORY;
3930 if (session->smb2.channel_setup) {
3931 data_blob_free(&session->smb2.signing_key);
3932 session->smb2.channel_setup = false;
3935 if (session->smb2.signing_key.length > 0) {
3936 return NT_STATUS_OK;
3939 session->smb2.signing_key = data_blob_dup_talloc(session, signing_key);
3940 if (session->smb2.signing_key.data == NULL) {
3941 return NT_STATUS_NO_MEMORY;
3944 session->smb2.should_sign = false;
3946 if (conn->desire_signing) {
3947 session->smb2.should_sign = true;
3950 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
3951 session->smb2.should_sign = true;
3954 return NT_STATUS_OK;
3957 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
3958 struct smbXcli_session *session1,
3959 struct smbXcli_conn *conn,
3960 struct smbXcli_session **_session2)
3962 struct smbXcli_session *session2;
3963 uint16_t no_sign_flags;
3965 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
3967 if (session1->smb2.session_flags & no_sign_flags) {
3968 return NT_STATUS_INVALID_PARAMETER_MIX;
3971 if (session1->smb2.session_key.length == 0) {
3972 return NT_STATUS_INVALID_PARAMETER_MIX;
3975 if (session1->smb2.signing_key.length == 0) {
3976 return NT_STATUS_INVALID_PARAMETER_MIX;
3980 return NT_STATUS_INVALID_PARAMETER_MIX;
3983 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
3984 if (session2 == NULL) {
3985 return NT_STATUS_NO_MEMORY;
3987 session2->smb2.session_id = session1->smb2.session_id;
3988 session2->smb2.session_flags = session1->smb2.session_flags;
3990 session2->smb2.session_key = data_blob_dup_talloc(session2,
3991 session1->smb2.session_key);
3992 if (session2->smb2.session_key.data == NULL) {
3993 return NT_STATUS_NO_MEMORY;
3996 session2->smb2.signing_key = data_blob_dup_talloc(session2,
3997 session1->smb2.signing_key);
3998 if (session2->smb2.signing_key.data == NULL) {
3999 return NT_STATUS_NO_MEMORY;
4002 session2->smb2.should_sign = session1->smb2.should_sign;
4003 session2->smb2.channel_setup = true;
4005 talloc_set_destructor(session2, smbXcli_session_destructor);
4006 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4007 session2->conn = conn;
4009 *_session2 = session2;
4010 return NT_STATUS_OK;