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;
100 struct tevent_req *read_braw_req;
105 uint32_t capabilities;
106 uint16_t security_mode;
111 uint32_t capabilities;
112 uint16_t security_mode;
114 uint32_t max_trans_size;
115 uint32_t max_read_size;
116 uint32_t max_write_size;
123 uint16_t cur_credits;
124 uint16_t max_credits;
127 struct smbXcli_session *sessions;
130 struct smbXcli_session {
131 struct smbXcli_session *prev, *next;
132 struct smbXcli_conn *conn;
136 uint16_t session_flags;
137 DATA_BLOB application_key;
138 DATA_BLOB signing_key;
141 DATA_BLOB encryption_key;
142 DATA_BLOB decryption_key;
143 uint64_t channel_nonce;
144 uint64_t channel_next;
145 DATA_BLOB channel_signing_key;
149 struct smbXcli_req_state {
150 struct tevent_context *ev;
151 struct smbXcli_conn *conn;
152 struct smbXcli_session *session; /* maybe NULL */
154 uint8_t length_hdr[4];
161 /* Space for the header including the wct */
162 uint8_t hdr[HDR_VWV];
165 * For normal requests, smb1cli_req_send chooses a mid.
166 * SecondaryV trans requests need to use the mid of the primary
167 * request, so we need a place to store it.
168 * Assume it is set if != 0.
173 uint8_t bytecount_buf[2];
175 #define MAX_SMB_IOV 10
176 /* length_hdr, hdr, words, byte_count, buffers */
177 struct iovec iov[1 + 3 + MAX_SMB_IOV];
182 struct tevent_req **chained_requests;
185 NTSTATUS recv_status;
186 /* always an array of 3 talloc elements */
187 struct iovec *recv_iov;
191 const uint8_t *fixed;
196 uint8_t transform[SMB2_TF_HDR_SIZE];
197 uint8_t hdr[SMB2_HDR_BODY];
198 uint8_t pad[7]; /* padding space for compounding */
201 * always an array of 3 talloc elements
202 * (without a SMB2_TRANSFORM header!)
206 struct iovec *recv_iov;
208 uint16_t credit_charge;
213 bool signing_skipped;
219 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
222 * NT_STATUS_OK, means we do not notify the callers
224 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
226 while (conn->sessions) {
227 conn->sessions->conn = NULL;
228 DLIST_REMOVE(conn->sessions, conn->sessions);
231 if (conn->smb1.trans_enc) {
232 TALLOC_FREE(conn->smb1.trans_enc);
238 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
240 const char *remote_name,
241 enum smb_signing_setting signing_state,
242 uint32_t smb1_capabilities,
243 struct GUID *client_guid,
244 uint32_t smb2_capabilities)
246 struct smbXcli_conn *conn = NULL;
248 struct sockaddr *sa = NULL;
252 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
258 conn->write_fd = dup(fd);
259 if (conn->write_fd == -1) {
263 conn->remote_name = talloc_strdup(conn, remote_name);
264 if (conn->remote_name == NULL) {
269 ss = (void *)&conn->local_ss;
270 sa = (struct sockaddr *)ss;
271 sa_length = sizeof(conn->local_ss);
272 ret = getsockname(fd, sa, &sa_length);
276 ss = (void *)&conn->remote_ss;
277 sa = (struct sockaddr *)ss;
278 sa_length = sizeof(conn->remote_ss);
279 ret = getpeername(fd, sa, &sa_length);
284 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
285 if (conn->outgoing == NULL) {
288 conn->pending = NULL;
290 conn->protocol = PROTOCOL_NONE;
292 switch (signing_state) {
293 case SMB_SIGNING_OFF:
295 conn->allow_signing = false;
296 conn->desire_signing = false;
297 conn->mandatory_signing = false;
299 case SMB_SIGNING_DEFAULT:
300 case SMB_SIGNING_IF_REQUIRED:
301 /* if the server requires it */
302 conn->allow_signing = true;
303 conn->desire_signing = false;
304 conn->mandatory_signing = false;
306 case SMB_SIGNING_REQUIRED:
308 conn->allow_signing = true;
309 conn->desire_signing = true;
310 conn->mandatory_signing = true;
314 conn->smb1.client.capabilities = smb1_capabilities;
315 conn->smb1.client.max_xmit = UINT16_MAX;
317 conn->smb1.capabilities = conn->smb1.client.capabilities;
318 conn->smb1.max_xmit = 1024;
322 /* initialise signing */
323 conn->smb1.signing = smb_signing_init(conn,
325 conn->desire_signing,
326 conn->mandatory_signing);
327 if (!conn->smb1.signing) {
331 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
332 if (conn->mandatory_signing) {
333 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
336 conn->smb2.client.guid = *client_guid;
338 conn->smb2.client.capabilities = smb2_capabilities;
340 conn->smb2.cur_credits = 1;
341 conn->smb2.max_credits = 0;
343 talloc_set_destructor(conn, smbXcli_conn_destructor);
347 if (conn->write_fd != -1) {
348 close(conn->write_fd);
354 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
360 if (conn->read_fd == -1) {
367 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
369 return conn->protocol;
372 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
374 if (conn->protocol >= PROTOCOL_SMB2_02) {
378 if (conn->smb1.capabilities & CAP_UNICODE) {
385 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
387 set_socket_options(conn->read_fd, options);
390 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
392 return &conn->local_ss;
395 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
397 return &conn->remote_ss;
400 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
402 return conn->remote_name;
405 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
407 if (conn->protocol >= PROTOCOL_SMB2_02) {
414 return conn->smb1.server.max_mux;
417 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
419 if (conn->protocol >= PROTOCOL_SMB2_02) {
420 return conn->smb2.server.system_time;
423 return conn->smb1.server.system_time;
426 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
428 if (conn->protocol >= PROTOCOL_SMB2_02) {
429 return &conn->smb2.server.gss_blob;
432 return &conn->smb1.server.gss_blob;
435 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
437 if (conn->protocol >= PROTOCOL_SMB2_02) {
438 return &conn->smb2.server.guid;
441 return &conn->smb1.server.guid;
444 struct smbXcli_conn_samba_suicide_state {
445 struct smbXcli_conn *conn;
450 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
452 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
453 struct tevent_context *ev,
454 struct smbXcli_conn *conn,
457 struct tevent_req *req, *subreq;
458 struct smbXcli_conn_samba_suicide_state *state;
460 req = tevent_req_create(mem_ctx, &state,
461 struct smbXcli_conn_samba_suicide_state);
466 SIVAL(state->buf, 4, 0x74697865);
467 SCVAL(state->buf, 8, exitcode);
468 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
470 state->iov.iov_base = state->buf;
471 state->iov.iov_len = sizeof(state->buf);
473 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
474 false, &state->iov, 1);
475 if (tevent_req_nomem(subreq, req)) {
476 return tevent_req_post(req, ev);
478 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
482 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
484 struct tevent_req *req = tevent_req_callback_data(
485 subreq, struct tevent_req);
486 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
487 req, struct smbXcli_conn_samba_suicide_state);
491 nwritten = writev_recv(subreq, &err);
493 if (nwritten == -1) {
494 NTSTATUS status = map_nt_error_from_unix_common(err);
495 smbXcli_conn_disconnect(state->conn, status);
498 tevent_req_done(req);
501 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
503 return tevent_req_simple_recv_ntstatus(req);
506 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
509 TALLOC_CTX *frame = talloc_stackframe();
510 struct tevent_context *ev;
511 struct tevent_req *req;
512 NTSTATUS status = NT_STATUS_NO_MEMORY;
515 if (smbXcli_conn_has_async_calls(conn)) {
517 * Can't use sync call while an async call is in flight
519 status = NT_STATUS_INVALID_PARAMETER_MIX;
522 ev = tevent_context_init(frame);
526 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
530 ok = tevent_req_poll(req, ev);
532 status = map_nt_error_from_unix_common(errno);
535 status = smbXcli_conn_samba_suicide_recv(req);
541 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
543 return conn->smb1.capabilities;
546 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
548 return conn->smb1.max_xmit;
551 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
553 return conn->smb1.server.session_key;
556 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
558 return conn->smb1.server.challenge;
561 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
563 return conn->smb1.server.security_mode;
566 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
568 return conn->smb1.server.readbraw;
571 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
573 return conn->smb1.server.writebraw;
576 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
578 return conn->smb1.server.lockread;
581 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
583 return conn->smb1.server.writeunlock;
586 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
588 return conn->smb1.server.time_zone;
591 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
592 const DATA_BLOB user_session_key,
593 const DATA_BLOB response)
595 return smb_signing_activate(conn->smb1.signing,
600 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
601 const uint8_t *buf, uint32_t seqnum)
603 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
606 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
608 return smb_signing_is_active(conn->smb1.signing);
611 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
612 struct smb_trans_enc_state *es)
614 /* Replace the old state, if any. */
615 if (conn->smb1.trans_enc) {
616 TALLOC_FREE(conn->smb1.trans_enc);
618 conn->smb1.trans_enc = es;
621 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
623 return common_encryption_on(conn->smb1.trans_enc);
627 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
629 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
630 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
632 if (NT_STATUS_IS_OK(status)) {
636 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
640 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
644 * Is the SMB command able to hold an AND_X successor
645 * @param[in] cmd The SMB command in question
646 * @retval Can we add a chained request after "cmd"?
648 bool smb1cli_is_andx_req(uint8_t cmd)
668 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
670 size_t num_pending = talloc_array_length(conn->pending);
676 result = conn->smb1.mid++;
677 if ((result == 0) || (result == 0xffff)) {
681 for (i=0; i<num_pending; i++) {
682 if (result == smb1cli_req_mid(conn->pending[i])) {
687 if (i == num_pending) {
693 void smbXcli_req_unset_pending(struct tevent_req *req)
695 struct smbXcli_req_state *state =
697 struct smbXcli_req_state);
698 struct smbXcli_conn *conn = state->conn;
699 size_t num_pending = talloc_array_length(conn->pending);
702 if (state->smb1.mid != 0) {
704 * This is a [nt]trans[2] request which waits
705 * for more than one reply.
710 talloc_set_destructor(req, NULL);
712 if (num_pending == 1) {
714 * The pending read_smb tevent_req is a child of
715 * conn->pending. So if nothing is pending anymore, we need to
716 * delete the socket read fde.
718 TALLOC_FREE(conn->pending);
719 conn->read_smb_req = NULL;
723 for (i=0; i<num_pending; i++) {
724 if (req == conn->pending[i]) {
728 if (i == num_pending) {
730 * Something's seriously broken. Just returning here is the
731 * right thing nevertheless, the point of this routine is to
732 * remove ourselves from conn->pending.
738 * Remove ourselves from the conn->pending array
740 for (; i < (num_pending - 1); i++) {
741 conn->pending[i] = conn->pending[i+1];
745 * No NULL check here, we're shrinking by sizeof(void *), and
746 * talloc_realloc just adjusts the size for this.
748 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
753 static int smbXcli_req_destructor(struct tevent_req *req)
755 struct smbXcli_req_state *state =
757 struct smbXcli_req_state);
760 * Make sure we really remove it from
761 * the pending array on destruction.
764 smbXcli_req_unset_pending(req);
768 static bool smb1cli_req_cancel(struct tevent_req *req);
769 static bool smb2cli_req_cancel(struct tevent_req *req);
771 static bool smbXcli_req_cancel(struct tevent_req *req)
773 struct smbXcli_req_state *state =
775 struct smbXcli_req_state);
777 if (!smbXcli_conn_is_connected(state->conn)) {
781 if (state->conn->protocol == PROTOCOL_NONE) {
785 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
786 return smb2cli_req_cancel(req);
789 return smb1cli_req_cancel(req);
792 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
794 bool smbXcli_req_set_pending(struct tevent_req *req)
796 struct smbXcli_req_state *state =
798 struct smbXcli_req_state);
799 struct smbXcli_conn *conn;
800 struct tevent_req **pending;
805 if (!smbXcli_conn_is_connected(conn)) {
809 num_pending = talloc_array_length(conn->pending);
811 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
813 if (pending == NULL) {
816 pending[num_pending] = req;
817 conn->pending = pending;
818 talloc_set_destructor(req, smbXcli_req_destructor);
819 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
821 if (!smbXcli_conn_receive_next(conn)) {
823 * the caller should notify the current request
825 * And all other pending requests get notified
826 * by smbXcli_conn_disconnect().
828 smbXcli_req_unset_pending(req);
829 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
836 static void smbXcli_conn_received(struct tevent_req *subreq);
838 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
840 size_t num_pending = talloc_array_length(conn->pending);
841 struct tevent_req *req;
842 struct smbXcli_req_state *state;
844 if (conn->read_smb_req != NULL) {
848 if (num_pending == 0) {
849 if (conn->smb2.mid < UINT64_MAX) {
850 /* no more pending requests, so we are done for now */
855 * If there are no more SMB2 requests possible,
856 * because we are out of message ids,
857 * we need to disconnect.
859 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
863 req = conn->pending[0];
864 state = tevent_req_data(req, struct smbXcli_req_state);
867 * We're the first ones, add the read_smb request that waits for the
868 * answer from the server
870 conn->read_smb_req = read_smb_send(conn->pending,
873 if (conn->read_smb_req == NULL) {
876 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
880 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
882 tevent_queue_stop(conn->outgoing);
884 if (conn->read_fd != -1) {
885 close(conn->read_fd);
887 if (conn->write_fd != -1) {
888 close(conn->write_fd);
894 * Cancel all pending requests. We do not do a for-loop walking
895 * conn->pending because that array changes in
896 * smbXcli_req_unset_pending.
898 while (talloc_array_length(conn->pending) > 0) {
899 struct tevent_req *req;
900 struct smbXcli_req_state *state;
901 struct tevent_req **chain;
905 req = conn->pending[0];
906 state = tevent_req_data(req, struct smbXcli_req_state);
908 if (state->smb1.chained_requests == NULL) {
910 * We're dead. No point waiting for trans2
915 smbXcli_req_unset_pending(req);
917 if (NT_STATUS_IS_OK(status)) {
918 /* do not notify the callers */
923 * we need to defer the callback, because we may notify
924 * more then one caller.
926 tevent_req_defer_callback(req, state->ev);
927 tevent_req_nterror(req, status);
931 chain = talloc_move(conn, &state->smb1.chained_requests);
932 num_chained = talloc_array_length(chain);
934 for (i=0; i<num_chained; i++) {
936 state = tevent_req_data(req, struct smbXcli_req_state);
939 * We're dead. No point waiting for trans2
944 smbXcli_req_unset_pending(req);
946 if (NT_STATUS_IS_OK(status)) {
947 /* do not notify the callers */
952 * we need to defer the callback, because we may notify
953 * more than one caller.
955 tevent_req_defer_callback(req, state->ev);
956 tevent_req_nterror(req, status);
963 * Fetch a smb request's mid. Only valid after the request has been sent by
964 * smb1cli_req_send().
966 uint16_t smb1cli_req_mid(struct tevent_req *req)
968 struct smbXcli_req_state *state =
970 struct smbXcli_req_state);
972 if (state->smb1.mid != 0) {
973 return state->smb1.mid;
976 return SVAL(state->smb1.hdr, HDR_MID);
979 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
981 struct smbXcli_req_state *state =
983 struct smbXcli_req_state);
985 state->smb1.mid = mid;
988 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
990 struct smbXcli_req_state *state =
992 struct smbXcli_req_state);
994 return state->smb1.seqnum;
997 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
999 struct smbXcli_req_state *state =
1000 tevent_req_data(req,
1001 struct smbXcli_req_state);
1003 state->smb1.seqnum = seqnum;
1006 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1010 for (i=0; i<count; i++) {
1011 result += iov[i].iov_len;
1016 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1017 const struct iovec *iov,
1020 size_t len = smbXcli_iov_len(iov, count);
1025 buf = talloc_array(mem_ctx, uint8_t, len);
1030 for (i=0; i<count; i++) {
1031 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1032 copied += iov[i].iov_len;
1037 static void smb1cli_req_flags(enum protocol_types protocol,
1038 uint32_t smb1_capabilities,
1039 uint8_t smb_command,
1040 uint8_t additional_flags,
1041 uint8_t clear_flags,
1043 uint16_t additional_flags2,
1044 uint16_t clear_flags2,
1048 uint16_t flags2 = 0;
1050 if (protocol >= PROTOCOL_LANMAN1) {
1051 flags |= FLAG_CASELESS_PATHNAMES;
1052 flags |= FLAG_CANONICAL_PATHNAMES;
1055 if (protocol >= PROTOCOL_LANMAN2) {
1056 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1057 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1060 if (protocol >= PROTOCOL_NT1) {
1061 flags2 |= FLAGS2_IS_LONG_NAME;
1063 if (smb1_capabilities & CAP_UNICODE) {
1064 flags2 |= FLAGS2_UNICODE_STRINGS;
1066 if (smb1_capabilities & CAP_STATUS32) {
1067 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1069 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1070 flags2 |= FLAGS2_EXTENDED_SECURITY;
1074 flags |= additional_flags;
1075 flags &= ~clear_flags;
1076 flags2 |= additional_flags2;
1077 flags2 &= ~clear_flags2;
1083 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1085 static bool smb1cli_req_cancel(struct tevent_req *req)
1087 struct smbXcli_req_state *state =
1088 tevent_req_data(req,
1089 struct smbXcli_req_state);
1096 struct tevent_req *subreq;
1099 flags = CVAL(state->smb1.hdr, HDR_FLG);
1100 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1101 pid = SVAL(state->smb1.hdr, HDR_PID);
1102 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1103 tid = SVAL(state->smb1.hdr, HDR_TID);
1104 uid = SVAL(state->smb1.hdr, HDR_UID);
1105 mid = SVAL(state->smb1.hdr, HDR_MID);
1107 subreq = smb1cli_req_create(state, state->ev,
1115 0, NULL); /* bytes */
1116 if (subreq == NULL) {
1119 smb1cli_req_set_mid(subreq, mid);
1121 status = smb1cli_req_chain_submit(&subreq, 1);
1122 if (!NT_STATUS_IS_OK(status)) {
1123 TALLOC_FREE(subreq);
1126 smb1cli_req_set_mid(subreq, 0);
1128 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1133 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1135 /* we do not care about the result */
1136 TALLOC_FREE(subreq);
1139 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1140 struct tevent_context *ev,
1141 struct smbXcli_conn *conn,
1142 uint8_t smb_command,
1143 uint8_t additional_flags,
1144 uint8_t clear_flags,
1145 uint16_t additional_flags2,
1146 uint16_t clear_flags2,
1147 uint32_t timeout_msec,
1151 uint8_t wct, uint16_t *vwv,
1153 struct iovec *bytes_iov)
1155 struct tevent_req *req;
1156 struct smbXcli_req_state *state;
1158 uint16_t flags2 = 0;
1160 if (iov_count > MAX_SMB_IOV) {
1162 * Should not happen :-)
1167 req = tevent_req_create(mem_ctx, &state,
1168 struct smbXcli_req_state);
1175 state->smb1.recv_cmd = 0xFF;
1176 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1177 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1178 if (state->smb1.recv_iov == NULL) {
1183 smb1cli_req_flags(conn->protocol,
1184 conn->smb1.capabilities,
1193 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1194 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1195 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1196 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1197 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1198 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1199 SSVAL(state->smb1.hdr, HDR_TID, tid);
1200 SSVAL(state->smb1.hdr, HDR_PID, pid);
1201 SSVAL(state->smb1.hdr, HDR_UID, uid);
1202 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1203 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1205 state->smb1.vwv = vwv;
1207 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1209 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1210 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1211 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1212 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1213 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1214 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1215 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1216 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1218 if (iov_count != 0) {
1219 memcpy(&state->smb1.iov[4], bytes_iov,
1220 iov_count * sizeof(*bytes_iov));
1222 state->smb1.iov_count = iov_count + 4;
1224 if (timeout_msec > 0) {
1225 struct timeval endtime;
1227 endtime = timeval_current_ofs_msec(timeout_msec);
1228 if (!tevent_req_set_endtime(req, ev, endtime)) {
1233 switch (smb_command) {
1237 state->one_way = true;
1240 state->one_way = true;
1241 state->smb1.one_way_seqnum = true;
1245 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1246 state->one_way = true;
1254 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1255 struct iovec *iov, int iov_count,
1257 bool one_way_seqnum)
1259 TALLOC_CTX *frame = NULL;
1263 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1264 * iovec directly. MD5Update would do that just fine.
1267 if (iov_count < 4) {
1268 return NT_STATUS_INVALID_PARAMETER_MIX;
1270 if (iov[0].iov_len != NBT_HDR_SIZE) {
1271 return NT_STATUS_INVALID_PARAMETER_MIX;
1273 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1274 return NT_STATUS_INVALID_PARAMETER_MIX;
1276 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1277 return NT_STATUS_INVALID_PARAMETER_MIX;
1279 if (iov[3].iov_len != sizeof(uint16_t)) {
1280 return NT_STATUS_INVALID_PARAMETER_MIX;
1283 frame = talloc_stackframe();
1285 buf = smbXcli_iov_concat(frame, iov, iov_count);
1287 return NT_STATUS_NO_MEMORY;
1290 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1292 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1293 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1296 return NT_STATUS_OK;
1299 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1300 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1301 TALLOC_CTX *tmp_mem,
1304 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1305 struct smbXcli_req_state *state,
1306 struct iovec *iov, int iov_count)
1308 struct tevent_req *subreq;
1313 if (!smbXcli_conn_is_connected(state->conn)) {
1314 return NT_STATUS_CONNECTION_DISCONNECTED;
1317 if (state->conn->protocol > PROTOCOL_NT1) {
1318 return NT_STATUS_REVISION_MISMATCH;
1321 if (iov_count < 4) {
1322 return NT_STATUS_INVALID_PARAMETER_MIX;
1324 if (iov[0].iov_len != NBT_HDR_SIZE) {
1325 return NT_STATUS_INVALID_PARAMETER_MIX;
1327 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1328 return NT_STATUS_INVALID_PARAMETER_MIX;
1330 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1331 return NT_STATUS_INVALID_PARAMETER_MIX;
1333 if (iov[3].iov_len != sizeof(uint16_t)) {
1334 return NT_STATUS_INVALID_PARAMETER_MIX;
1337 cmd = CVAL(iov[1].iov_base, HDR_COM);
1338 if (cmd == SMBreadBraw) {
1339 if (smbXcli_conn_has_async_calls(state->conn)) {
1340 return NT_STATUS_INVALID_PARAMETER_MIX;
1342 state->conn->smb1.read_braw_req = req;
1345 if (state->smb1.mid != 0) {
1346 mid = state->smb1.mid;
1348 mid = smb1cli_alloc_mid(state->conn);
1350 SSVAL(iov[1].iov_base, HDR_MID, mid);
1352 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1354 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1355 &state->smb1.seqnum,
1356 state->smb1.one_way_seqnum);
1358 if (!NT_STATUS_IS_OK(status)) {
1363 * If we supported multiple encrytion contexts
1364 * here we'd look up based on tid.
1366 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1367 char *buf, *enc_buf;
1369 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1371 return NT_STATUS_NO_MEMORY;
1373 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1374 (char *)buf, &enc_buf);
1376 if (!NT_STATUS_IS_OK(status)) {
1377 DEBUG(0, ("Error in encrypting client message: %s\n",
1378 nt_errstr(status)));
1381 buf = (char *)talloc_memdup(state, enc_buf,
1382 smb_len_nbt(enc_buf)+4);
1385 return NT_STATUS_NO_MEMORY;
1387 iov[0].iov_base = (void *)buf;
1388 iov[0].iov_len = talloc_get_size(buf);
1392 if (state->conn->dispatch_incoming == NULL) {
1393 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1396 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1398 subreq = writev_send(state, state->ev, state->conn->outgoing,
1399 state->conn->write_fd, false, iov, iov_count);
1400 if (subreq == NULL) {
1401 return NT_STATUS_NO_MEMORY;
1403 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1404 return NT_STATUS_OK;
1407 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1408 struct tevent_context *ev,
1409 struct smbXcli_conn *conn,
1410 uint8_t smb_command,
1411 uint8_t additional_flags,
1412 uint8_t clear_flags,
1413 uint16_t additional_flags2,
1414 uint16_t clear_flags2,
1415 uint32_t timeout_msec,
1419 uint8_t wct, uint16_t *vwv,
1421 const uint8_t *bytes)
1423 struct tevent_req *req;
1427 iov.iov_base = discard_const_p(void, bytes);
1428 iov.iov_len = num_bytes;
1430 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1431 additional_flags, clear_flags,
1432 additional_flags2, clear_flags2,
1439 if (!tevent_req_is_in_progress(req)) {
1440 return tevent_req_post(req, ev);
1442 status = smb1cli_req_chain_submit(&req, 1);
1443 if (tevent_req_nterror(req, status)) {
1444 return tevent_req_post(req, ev);
1449 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1451 struct tevent_req *req =
1452 tevent_req_callback_data(subreq,
1454 struct smbXcli_req_state *state =
1455 tevent_req_data(req,
1456 struct smbXcli_req_state);
1460 nwritten = writev_recv(subreq, &err);
1461 TALLOC_FREE(subreq);
1462 if (nwritten == -1) {
1463 NTSTATUS status = map_nt_error_from_unix_common(err);
1464 smbXcli_conn_disconnect(state->conn, status);
1468 if (state->one_way) {
1469 state->inbuf = NULL;
1470 tevent_req_done(req);
1474 if (!smbXcli_req_set_pending(req)) {
1475 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1480 static void smbXcli_conn_received(struct tevent_req *subreq)
1482 struct smbXcli_conn *conn =
1483 tevent_req_callback_data(subreq,
1484 struct smbXcli_conn);
1485 TALLOC_CTX *frame = talloc_stackframe();
1491 if (subreq != conn->read_smb_req) {
1492 DEBUG(1, ("Internal error: cli_smb_received called with "
1493 "unexpected subreq\n"));
1494 status = NT_STATUS_INTERNAL_ERROR;
1495 smbXcli_conn_disconnect(conn, status);
1499 conn->read_smb_req = NULL;
1501 received = read_smb_recv(subreq, frame, &inbuf, &err);
1502 TALLOC_FREE(subreq);
1503 if (received == -1) {
1504 status = map_nt_error_from_unix_common(err);
1505 smbXcli_conn_disconnect(conn, status);
1510 status = conn->dispatch_incoming(conn, frame, inbuf);
1512 if (NT_STATUS_IS_OK(status)) {
1514 * We should not do any more processing
1515 * as the dispatch function called
1516 * tevent_req_done().
1519 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1521 * We got an error, so notify all pending requests
1523 smbXcli_conn_disconnect(conn, status);
1528 * We got NT_STATUS_RETRY, so we may ask for a
1529 * next incoming pdu.
1531 if (!smbXcli_conn_receive_next(conn)) {
1532 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1536 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1537 struct iovec **piov, int *pnum_iov)
1548 buflen = smb_len_nbt(buf);
1551 hdr = buf + NBT_HDR_SIZE;
1553 if (buflen < MIN_SMB_SIZE) {
1554 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1558 * This returns iovec elements in the following order:
1573 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1575 return NT_STATUS_NO_MEMORY;
1577 iov[0].iov_base = hdr;
1578 iov[0].iov_len = HDR_WCT;
1581 cmd = CVAL(hdr, HDR_COM);
1585 size_t len = buflen - taken;
1587 struct iovec *iov_tmp;
1594 * we need at least WCT and BCC
1596 needed = sizeof(uint8_t) + sizeof(uint16_t);
1598 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1599 __location__, (int)len, (int)needed));
1604 * Now we check if the specified words are there
1606 wct = CVAL(hdr, wct_ofs);
1607 needed += wct * sizeof(uint16_t);
1609 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1610 __location__, (int)len, (int)needed));
1615 * Now we check if the specified bytes are there
1617 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1618 bcc = SVAL(hdr, bcc_ofs);
1619 needed += bcc * sizeof(uint8_t);
1621 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1622 __location__, (int)len, (int)needed));
1627 * we allocate 2 iovec structures for words and bytes
1629 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1631 if (iov_tmp == NULL) {
1633 return NT_STATUS_NO_MEMORY;
1636 cur = &iov[num_iov];
1639 cur[0].iov_len = wct * sizeof(uint16_t);
1640 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1641 cur[1].iov_len = bcc * sizeof(uint8_t);
1642 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1646 if (!smb1cli_is_andx_req(cmd)) {
1648 * If the current command does not have AndX chanining
1654 if (wct == 0 && bcc == 0) {
1656 * An empty response also ends the chain,
1657 * most likely with an error.
1663 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1664 __location__, (int)wct, (int)cmd));
1667 cmd = CVAL(cur[0].iov_base, 0);
1670 * If it is the end of the chain we are also done.
1674 wct_ofs = SVAL(cur[0].iov_base, 2);
1676 if (wct_ofs < taken) {
1677 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1679 if (wct_ofs > buflen) {
1680 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1684 * we consumed everything up to the start of the next
1690 remaining = buflen - taken;
1692 if (remaining > 0 && num_iov >= 3) {
1694 * The last DATA block gets the remaining
1695 * bytes, this is needed to support
1696 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1698 iov[num_iov-1].iov_len += remaining;
1702 *pnum_iov = num_iov;
1703 return NT_STATUS_OK;
1707 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1710 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1711 TALLOC_CTX *tmp_mem,
1714 struct tevent_req *req;
1715 struct smbXcli_req_state *state;
1722 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1723 struct iovec *iov = NULL;
1725 struct tevent_req **chain = NULL;
1726 size_t num_chained = 0;
1727 size_t num_responses = 0;
1729 if (conn->smb1.read_braw_req != NULL) {
1730 req = conn->smb1.read_braw_req;
1731 conn->smb1.read_braw_req = NULL;
1732 state = tevent_req_data(req, struct smbXcli_req_state);
1734 smbXcli_req_unset_pending(req);
1736 if (state->smb1.recv_iov == NULL) {
1738 * For requests with more than
1739 * one response, we have to readd the
1742 state->smb1.recv_iov = talloc_zero_array(state,
1745 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1746 return NT_STATUS_OK;
1750 state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
1751 state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
1752 ZERO_STRUCT(state->smb1.recv_iov[1]);
1753 ZERO_STRUCT(state->smb1.recv_iov[2]);
1755 state->smb1.recv_cmd = SMBreadBraw;
1756 state->smb1.recv_status = NT_STATUS_OK;
1757 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1759 tevent_req_done(req);
1760 return NT_STATUS_OK;
1763 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1764 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1765 DEBUG(10, ("Got non-SMB PDU\n"));
1766 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1770 * If we supported multiple encrytion contexts
1771 * here we'd look up based on tid.
1773 if (common_encryption_on(conn->smb1.trans_enc)
1774 && (CVAL(inbuf, 0) == 0)) {
1775 uint16_t enc_ctx_num;
1777 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1778 if (!NT_STATUS_IS_OK(status)) {
1779 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1780 nt_errstr(status)));
1784 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1785 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1787 conn->smb1.trans_enc->enc_ctx_num));
1788 return NT_STATUS_INVALID_HANDLE;
1791 status = common_decrypt_buffer(conn->smb1.trans_enc,
1793 if (!NT_STATUS_IS_OK(status)) {
1794 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1795 nt_errstr(status)));
1800 mid = SVAL(inhdr, HDR_MID);
1801 num_pending = talloc_array_length(conn->pending);
1803 for (i=0; i<num_pending; i++) {
1804 if (mid == smb1cli_req_mid(conn->pending[i])) {
1808 if (i == num_pending) {
1809 /* Dump unexpected reply */
1810 return NT_STATUS_RETRY;
1813 oplock_break = false;
1815 if (mid == 0xffff) {
1817 * Paranoia checks that this is really an oplock break request.
1819 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1820 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1821 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1822 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1823 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1825 if (!oplock_break) {
1826 /* Dump unexpected reply */
1827 return NT_STATUS_RETRY;
1831 req = conn->pending[i];
1832 state = tevent_req_data(req, struct smbXcli_req_state);
1834 if (!oplock_break /* oplock breaks are not signed */
1835 && !smb_signing_check_pdu(conn->smb1.signing,
1836 inbuf, state->smb1.seqnum+1)) {
1837 DEBUG(10, ("cli_check_sign_mac failed\n"));
1838 return NT_STATUS_ACCESS_DENIED;
1841 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1843 if (!NT_STATUS_IS_OK(status)) {
1844 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1845 nt_errstr(status)));
1849 cmd = CVAL(inhdr, HDR_COM);
1850 status = smb1cli_pull_raw_error(inhdr);
1852 if (state->smb1.chained_requests == NULL) {
1854 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1857 smbXcli_req_unset_pending(req);
1859 if (state->smb1.recv_iov == NULL) {
1861 * For requests with more than
1862 * one response, we have to readd the
1865 state->smb1.recv_iov = talloc_zero_array(state,
1868 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1869 return NT_STATUS_OK;
1873 state->smb1.recv_cmd = cmd;
1874 state->smb1.recv_status = status;
1875 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1877 state->smb1.recv_iov[0] = iov[0];
1878 state->smb1.recv_iov[1] = iov[1];
1879 state->smb1.recv_iov[2] = iov[2];
1881 if (talloc_array_length(conn->pending) == 0) {
1882 tevent_req_done(req);
1883 return NT_STATUS_OK;
1886 tevent_req_defer_callback(req, state->ev);
1887 tevent_req_done(req);
1888 return NT_STATUS_RETRY;
1891 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1892 num_chained = talloc_array_length(chain);
1893 num_responses = (num_iov - 1)/2;
1895 if (num_responses > num_chained) {
1896 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1899 for (i=0; i<num_chained; i++) {
1900 size_t iov_idx = 1 + (i*2);
1901 struct iovec *cur = &iov[iov_idx];
1905 state = tevent_req_data(req, struct smbXcli_req_state);
1907 smbXcli_req_unset_pending(req);
1910 * as we finish multiple requests here
1911 * we need to defer the callbacks as
1912 * they could destroy our current stack state.
1914 tevent_req_defer_callback(req, state->ev);
1916 if (i >= num_responses) {
1917 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1921 if (state->smb1.recv_iov == NULL) {
1923 * For requests with more than
1924 * one response, we have to readd the
1927 state->smb1.recv_iov = talloc_zero_array(state,
1930 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1935 state->smb1.recv_cmd = cmd;
1937 if (i == (num_responses - 1)) {
1939 * The last request in the chain gets the status
1941 state->smb1.recv_status = status;
1943 cmd = CVAL(cur[0].iov_base, 0);
1944 state->smb1.recv_status = NT_STATUS_OK;
1947 state->inbuf = inbuf;
1950 * Note: here we use talloc_reference() in a way
1951 * that does not expose it to the caller.
1953 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1954 if (tevent_req_nomem(inbuf_ref, req)) {
1958 /* copy the related buffers */
1959 state->smb1.recv_iov[0] = iov[0];
1960 state->smb1.recv_iov[1] = cur[0];
1961 state->smb1.recv_iov[2] = cur[1];
1963 tevent_req_done(req);
1966 return NT_STATUS_RETRY;
1969 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
1970 TALLOC_CTX *mem_ctx,
1971 struct iovec **piov,
1975 uint32_t *pvwv_offset,
1976 uint32_t *pnum_bytes,
1978 uint32_t *pbytes_offset,
1980 const struct smb1cli_req_expected_response *expected,
1981 size_t num_expected)
1983 struct smbXcli_req_state *state =
1984 tevent_req_data(req,
1985 struct smbXcli_req_state);
1986 NTSTATUS status = NT_STATUS_OK;
1987 struct iovec *recv_iov = NULL;
1988 uint8_t *hdr = NULL;
1990 uint32_t vwv_offset = 0;
1991 uint16_t *vwv = NULL;
1992 uint32_t num_bytes = 0;
1993 uint32_t bytes_offset = 0;
1994 uint8_t *bytes = NULL;
1996 bool found_status = false;
1997 bool found_size = false;
2011 if (pvwv_offset != NULL) {
2014 if (pnum_bytes != NULL) {
2017 if (pbytes != NULL) {
2020 if (pbytes_offset != NULL) {
2023 if (pinbuf != NULL) {
2027 if (state->inbuf != NULL) {
2028 recv_iov = state->smb1.recv_iov;
2029 state->smb1.recv_iov = NULL;
2030 if (state->smb1.recv_cmd != SMBreadBraw) {
2031 hdr = (uint8_t *)recv_iov[0].iov_base;
2032 wct = recv_iov[1].iov_len/2;
2033 vwv = (uint16_t *)recv_iov[1].iov_base;
2034 vwv_offset = PTR_DIFF(vwv, hdr);
2035 num_bytes = recv_iov[2].iov_len;
2036 bytes = (uint8_t *)recv_iov[2].iov_base;
2037 bytes_offset = PTR_DIFF(bytes, hdr);
2041 if (tevent_req_is_nterror(req, &status)) {
2042 for (i=0; i < num_expected; i++) {
2043 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2044 found_status = true;
2050 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2056 if (num_expected == 0) {
2057 found_status = true;
2061 status = state->smb1.recv_status;
2063 for (i=0; i < num_expected; i++) {
2064 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2068 found_status = true;
2069 if (expected[i].wct == 0) {
2074 if (expected[i].wct == wct) {
2080 if (!found_status) {
2085 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2089 *piov = talloc_move(mem_ctx, &recv_iov);
2101 if (pvwv_offset != NULL) {
2102 *pvwv_offset = vwv_offset;
2104 if (pnum_bytes != NULL) {
2105 *pnum_bytes = num_bytes;
2107 if (pbytes != NULL) {
2110 if (pbytes_offset != NULL) {
2111 *pbytes_offset = bytes_offset;
2113 if (pinbuf != NULL) {
2114 *pinbuf = state->inbuf;
2120 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2127 for (i=0; i<num_reqs; i++) {
2128 struct smbXcli_req_state *state;
2129 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2130 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2131 state->smb1.iov_count-2);
2132 wct_ofs = (wct_ofs + 3) & ~3;
2137 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2139 struct smbXcli_req_state *first_state =
2140 tevent_req_data(reqs[0],
2141 struct smbXcli_req_state);
2142 struct smbXcli_req_state *state;
2144 size_t chain_padding = 0;
2146 struct iovec *iov = NULL;
2147 struct iovec *this_iov;
2151 if (num_reqs == 1) {
2152 return smb1cli_req_writev_submit(reqs[0], first_state,
2153 first_state->smb1.iov,
2154 first_state->smb1.iov_count);
2158 for (i=0; i<num_reqs; i++) {
2159 if (!tevent_req_is_in_progress(reqs[i])) {
2160 return NT_STATUS_INTERNAL_ERROR;
2163 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2165 if (state->smb1.iov_count < 4) {
2166 return NT_STATUS_INVALID_PARAMETER_MIX;
2171 * The NBT and SMB header
2184 iovlen += state->smb1.iov_count - 2;
2187 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2189 return NT_STATUS_NO_MEMORY;
2192 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2193 first_state, reqs, sizeof(*reqs) * num_reqs);
2194 if (first_state->smb1.chained_requests == NULL) {
2196 return NT_STATUS_NO_MEMORY;
2199 wct_offset = HDR_WCT;
2202 for (i=0; i<num_reqs; i++) {
2203 size_t next_padding = 0;
2206 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2208 if (i < num_reqs-1) {
2209 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2210 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2212 TALLOC_FREE(first_state->smb1.chained_requests);
2213 return NT_STATUS_INVALID_PARAMETER_MIX;
2217 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2218 state->smb1.iov_count-2) + 1;
2219 if ((wct_offset % 4) != 0) {
2220 next_padding = 4 - (wct_offset % 4);
2222 wct_offset += next_padding;
2223 vwv = state->smb1.vwv;
2225 if (i < num_reqs-1) {
2226 struct smbXcli_req_state *next_state =
2227 tevent_req_data(reqs[i+1],
2228 struct smbXcli_req_state);
2229 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2231 SSVAL(vwv+1, 0, wct_offset);
2232 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2233 /* properly end the chain */
2234 SCVAL(vwv+0, 0, 0xff);
2235 SCVAL(vwv+0, 1, 0xff);
2241 * The NBT and SMB header
2243 this_iov[0] = state->smb1.iov[0];
2244 this_iov[1] = state->smb1.iov[1];
2248 * This one is a bit subtle. We have to add
2249 * chain_padding bytes between the requests, and we
2250 * have to also include the wct field of the
2251 * subsequent requests. We use the subsequent header
2252 * for the padding, it contains the wct field in its
2255 this_iov[0].iov_len = chain_padding+1;
2256 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2257 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2258 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2263 * copy the words and bytes
2265 memcpy(this_iov, state->smb1.iov+2,
2266 sizeof(struct iovec) * (state->smb1.iov_count-2));
2267 this_iov += state->smb1.iov_count - 2;
2268 chain_padding = next_padding;
2271 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2272 if (nbt_len > first_state->conn->smb1.max_xmit) {
2274 TALLOC_FREE(first_state->smb1.chained_requests);
2275 return NT_STATUS_INVALID_PARAMETER_MIX;
2278 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2279 if (!NT_STATUS_IS_OK(status)) {
2281 TALLOC_FREE(first_state->smb1.chained_requests);
2285 return NT_STATUS_OK;
2288 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2290 return ((tevent_queue_length(conn->outgoing) != 0)
2291 || (talloc_array_length(conn->pending) != 0));
2294 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2296 return conn->smb2.server.capabilities;
2299 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2301 return conn->smb2.server.security_mode;
2304 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2306 return conn->smb2.server.max_trans_size;
2309 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2311 return conn->smb2.server.max_read_size;
2314 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2316 return conn->smb2.server.max_write_size;
2319 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2320 uint16_t max_credits)
2322 conn->smb2.max_credits = max_credits;
2325 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2327 static bool smb2cli_req_cancel(struct tevent_req *req)
2329 struct smbXcli_req_state *state =
2330 tevent_req_data(req,
2331 struct smbXcli_req_state);
2332 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2333 uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
2334 uint32_t tid = IVAL(state->smb2.hdr, SMB2_HDR_TID);
2335 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2336 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2337 struct smbXcli_session *session = state->session;
2338 uint8_t *fixed = state->smb2.pad;
2339 uint16_t fixed_len = 4;
2340 struct tevent_req *subreq;
2341 struct smbXcli_req_state *substate;
2344 SSVAL(fixed, 0, 0x04);
2347 subreq = smb2cli_req_create(state, state->ev,
2355 if (subreq == NULL) {
2358 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2360 if (flags & SMB2_HDR_FLAG_ASYNC) {
2364 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2365 SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
2366 SIVAL(substate->smb2.hdr, SMB2_HDR_TID, tid);
2367 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2368 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2370 status = smb2cli_req_compound_submit(&subreq, 1);
2371 if (!NT_STATUS_IS_OK(status)) {
2372 TALLOC_FREE(subreq);
2376 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2381 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2383 /* we do not care about the result */
2384 TALLOC_FREE(subreq);
2387 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2388 struct tevent_context *ev,
2389 struct smbXcli_conn *conn,
2391 uint32_t additional_flags,
2392 uint32_t clear_flags,
2393 uint32_t timeout_msec,
2396 struct smbXcli_session *session,
2397 const uint8_t *fixed,
2402 struct tevent_req *req;
2403 struct smbXcli_req_state *state;
2407 req = tevent_req_create(mem_ctx, &state,
2408 struct smbXcli_req_state);
2415 state->session = session;
2418 uid = session->smb2.session_id;
2420 state->smb2.should_sign = session->smb2.should_sign;
2421 state->smb2.should_encrypt = session->smb2.should_encrypt;
2423 /* TODO: turn on encryption based on the tree connect. */
2425 if (cmd == SMB2_OP_SESSSETUP &&
2426 session->smb2.signing_key.length != 0) {
2427 state->smb2.should_sign = true;
2430 if (cmd == SMB2_OP_SESSSETUP &&
2431 session->smb2.channel_signing_key.length == 0) {
2432 state->smb2.should_encrypt = false;
2435 if (state->smb2.should_encrypt) {
2436 state->smb2.should_sign = false;
2440 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2441 if (state->smb2.recv_iov == NULL) {
2446 flags |= additional_flags;
2447 flags &= ~clear_flags;
2449 state->smb2.fixed = fixed;
2450 state->smb2.fixed_len = fixed_len;
2451 state->smb2.dyn = dyn;
2452 state->smb2.dyn_len = dyn_len;
2454 if (state->smb2.should_encrypt) {
2455 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2456 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2459 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2460 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2461 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2462 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2463 SIVAL(state->smb2.hdr, SMB2_HDR_PID, pid);
2464 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2465 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2468 case SMB2_OP_CANCEL:
2469 state->one_way = true;
2473 * If this is a dummy request, it will have
2474 * UINT64_MAX as message id.
2475 * If we send on break acknowledgement,
2476 * this gets overwritten later.
2478 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2482 if (timeout_msec > 0) {
2483 struct timeval endtime;
2485 endtime = timeval_current_ofs_msec(timeout_msec);
2486 if (!tevent_req_set_endtime(req, ev, endtime)) {
2494 void smb2cli_req_set_notify_async(struct tevent_req *req)
2496 struct smbXcli_req_state *state =
2497 tevent_req_data(req,
2498 struct smbXcli_req_state);
2500 state->smb2.notify_async = true;
2503 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2504 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2505 TALLOC_CTX *tmp_mem,
2508 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2511 struct smbXcli_req_state *state;
2512 struct tevent_req *subreq;
2514 int i, num_iov, nbt_len;
2517 * 1 for the nbt length
2518 * per request: TRANSFORM, HDR, fixed, dyn, padding
2519 * -1 because the last one does not need padding
2522 iov = talloc_array(reqs[0], struct iovec, 1 + 5*num_reqs - 1);
2524 return NT_STATUS_NO_MEMORY;
2530 for (i=0; i<num_reqs; i++) {
2540 const DATA_BLOB *signing_key = NULL;
2541 const DATA_BLOB *encryption_key = NULL;
2543 if (!tevent_req_is_in_progress(reqs[i])) {
2544 return NT_STATUS_INTERNAL_ERROR;
2547 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2549 if (!smbXcli_conn_is_connected(state->conn)) {
2550 return NT_STATUS_CONNECTION_DISCONNECTED;
2553 if ((state->conn->protocol != PROTOCOL_NONE) &&
2554 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2555 return NT_STATUS_REVISION_MISMATCH;
2558 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2559 if (opcode == SMB2_OP_CANCEL) {
2563 avail = UINT64_MAX - state->conn->smb2.mid;
2565 return NT_STATUS_CONNECTION_ABORTED;
2568 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2569 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2574 charge = MAX(state->smb2.credit_charge, charge);
2576 avail = MIN(avail, state->conn->smb2.cur_credits);
2577 if (avail < charge) {
2578 return NT_STATUS_INTERNAL_ERROR;
2582 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2583 credits = state->conn->smb2.max_credits -
2584 state->conn->smb2.cur_credits;
2586 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2590 mid = state->conn->smb2.mid;
2591 state->conn->smb2.mid += charge;
2592 state->conn->smb2.cur_credits -= charge;
2594 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2595 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2597 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2598 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2601 if (state->session) {
2603 * We prefer the channel signing key if it is
2606 if (state->smb2.should_sign) {
2607 signing_key = &state->session->smb2.channel_signing_key;
2611 * If it is a channel binding, we already have the main
2612 * signing key and try that one.
2614 if (signing_key && signing_key->length == 0) {
2615 signing_key = &state->session->smb2.signing_key;
2619 * If we do not have any session key yet, we skip the
2620 * signing of SMB2_OP_SESSSETUP requests.
2622 if (signing_key && signing_key->length == 0) {
2626 if (state->smb2.should_encrypt) {
2627 encryption_key = &state->session->smb2.encryption_key;
2631 if (encryption_key) {
2633 iov[num_iov].iov_base = state->smb2.transform;
2634 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2639 iov[num_iov].iov_base = state->smb2.hdr;
2640 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2643 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2644 iov[num_iov].iov_len = state->smb2.fixed_len;
2647 if (state->smb2.dyn != NULL) {
2648 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2649 iov[num_iov].iov_len = state->smb2.dyn_len;
2653 reqlen = sizeof(state->smb2.hdr);
2654 reqlen += state->smb2.fixed_len;
2655 reqlen += state->smb2.dyn_len;
2657 if (i < num_reqs-1) {
2658 if ((reqlen % 8) > 0) {
2659 uint8_t pad = 8 - (reqlen % 8);
2660 iov[num_iov].iov_base = state->smb2.pad;
2661 iov[num_iov].iov_len = pad;
2665 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2668 if (encryption_key) {
2673 SBVAL(state->smb2.transform, SMB2_TF_NONCE, mid);
2674 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2675 state->session->smb2.channel_nonce);
2676 SBVAL(state->smb2.transform, SMB2_TF_MSG_SIZE,
2679 buf = talloc_array(iov, uint8_t, reqlen);
2681 return NT_STATUS_NO_MEMORY;
2684 reqlen += SMB2_TF_HDR_SIZE;
2687 * We copy the buffers before encrypting them,
2688 * this is at least currently needed for the
2689 * to keep state->smb2.hdr.
2691 * Also the callers may expect there buffers
2694 for (vi = hdr_iov; vi < num_iov; vi++) {
2695 struct iovec *v = &iov[vi];
2696 const uint8_t *o = (const uint8_t *)v->iov_base;
2698 memcpy(buf, o, v->iov_len);
2699 v->iov_base = (void *)buf;
2703 status = smb2_signing_encrypt_pdu(*encryption_key,
2704 state->session->conn->protocol,
2705 &iov[tf_iov], num_iov - tf_iov);
2706 if (!NT_STATUS_IS_OK(status)) {
2709 } else if (signing_key) {
2712 status = smb2_signing_sign_pdu(*signing_key,
2713 state->session->conn->protocol,
2714 &iov[hdr_iov], num_iov - hdr_iov);
2715 if (!NT_STATUS_IS_OK(status)) {
2722 ret = smbXcli_req_set_pending(reqs[i]);
2724 return NT_STATUS_NO_MEMORY;
2728 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2729 _smb_setlen_tcp(state->length_hdr, nbt_len);
2730 iov[0].iov_base = state->length_hdr;
2731 iov[0].iov_len = sizeof(state->length_hdr);
2733 if (state->conn->dispatch_incoming == NULL) {
2734 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2737 subreq = writev_send(state, state->ev, state->conn->outgoing,
2738 state->conn->write_fd, false, iov, num_iov);
2739 if (subreq == NULL) {
2740 return NT_STATUS_NO_MEMORY;
2742 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2743 return NT_STATUS_OK;
2746 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2748 struct smbXcli_req_state *state =
2749 tevent_req_data(req,
2750 struct smbXcli_req_state);
2752 state->smb2.credit_charge = charge;
2755 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2756 struct tevent_context *ev,
2757 struct smbXcli_conn *conn,
2759 uint32_t additional_flags,
2760 uint32_t clear_flags,
2761 uint32_t timeout_msec,
2764 struct smbXcli_session *session,
2765 const uint8_t *fixed,
2770 struct tevent_req *req;
2773 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2774 additional_flags, clear_flags,
2777 fixed, fixed_len, dyn, dyn_len);
2781 if (!tevent_req_is_in_progress(req)) {
2782 return tevent_req_post(req, ev);
2784 status = smb2cli_req_compound_submit(&req, 1);
2785 if (tevent_req_nterror(req, status)) {
2786 return tevent_req_post(req, ev);
2791 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2793 struct tevent_req *req =
2794 tevent_req_callback_data(subreq,
2796 struct smbXcli_req_state *state =
2797 tevent_req_data(req,
2798 struct smbXcli_req_state);
2802 nwritten = writev_recv(subreq, &err);
2803 TALLOC_FREE(subreq);
2804 if (nwritten == -1) {
2805 /* here, we need to notify all pending requests */
2806 NTSTATUS status = map_nt_error_from_unix_common(err);
2807 smbXcli_conn_disconnect(state->conn, status);
2812 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
2815 TALLOC_CTX *mem_ctx,
2816 struct iovec **piov, int *pnum_iov)
2821 uint8_t *first_hdr = buf;
2823 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2825 return NT_STATUS_NO_MEMORY;
2828 while (taken < buflen) {
2831 size_t len = buflen - taken;
2832 uint8_t *hdr = first_hdr + taken;
2835 size_t next_command_ofs;
2837 struct iovec *iov_tmp;
2840 DEBUG(10, ("%d bytes left, expected at least %d\n",
2844 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
2845 struct smbXcli_session *s;
2847 struct iovec tf_iov[2];
2850 if (len < SMB2_TF_HDR_SIZE) {
2851 DEBUG(10, ("%d bytes left, expected at least %d\n",
2852 (int)len, SMB2_TF_HDR_SIZE));
2856 tf_len = SMB2_TF_HDR_SIZE;
2859 hdr = first_hdr + taken;
2860 len = IVAL(tf, SMB2_TF_MSG_SIZE);
2861 uid = BVAL(tf, SMB2_TF_SESSION_ID);
2864 for (; s; s = s->next) {
2865 if (s->smb2.session_id != uid) {
2872 DEBUG(10, ("unknown session_id %llu\n",
2873 (unsigned long long)uid));
2877 tf_iov[0].iov_base = (void *)tf;
2878 tf_iov[0].iov_len = tf_len;
2879 tf_iov[1].iov_base = (void *)hdr;
2880 tf_iov[1].iov_len = len;
2882 status = smb2_signing_decrypt_pdu(s->smb2.decryption_key,
2885 if (!NT_STATUS_IS_OK(status)) {
2892 * We need the header plus the body length field
2895 if (len < SMB2_HDR_BODY + 2) {
2896 DEBUG(10, ("%d bytes left, expected at least %d\n",
2897 (int)len, SMB2_HDR_BODY));
2900 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2901 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2905 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2906 DEBUG(10, ("Got HDR len %d, expected %d\n",
2907 SVAL(hdr, 4), SMB2_HDR_BODY));
2912 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2913 body_size = SVAL(hdr, SMB2_HDR_BODY);
2915 if (next_command_ofs != 0) {
2916 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2919 if (next_command_ofs > full_size) {
2922 if (tf && next_command_ofs < len) {
2925 full_size = next_command_ofs;
2927 if (body_size < 2) {
2930 body_size &= 0xfffe;
2932 if (body_size > (full_size - SMB2_HDR_BODY)) {
2936 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2938 if (iov_tmp == NULL) {
2940 return NT_STATUS_NO_MEMORY;
2943 cur = &iov[num_iov];
2946 cur[0].iov_base = tf;
2947 cur[0].iov_len = tf_len;
2948 cur[1].iov_base = hdr;
2949 cur[1].iov_len = SMB2_HDR_BODY;
2950 cur[2].iov_base = hdr + SMB2_HDR_BODY;
2951 cur[2].iov_len = body_size;
2952 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
2953 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
2959 *pnum_iov = num_iov;
2960 return NT_STATUS_OK;
2964 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2967 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
2970 size_t num_pending = talloc_array_length(conn->pending);
2973 for (i=0; i<num_pending; i++) {
2974 struct tevent_req *req = conn->pending[i];
2975 struct smbXcli_req_state *state =
2976 tevent_req_data(req,
2977 struct smbXcli_req_state);
2979 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
2986 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2987 TALLOC_CTX *tmp_mem,
2990 struct tevent_req *req;
2991 struct smbXcli_req_state *state = NULL;
2996 struct smbXcli_session *last_session = NULL;
2997 size_t inbuf_len = smb_len_tcp(inbuf);
2999 status = smb2cli_inbuf_parse_compound(conn,
3000 inbuf + NBT_HDR_SIZE,
3004 if (!NT_STATUS_IS_OK(status)) {
3008 for (i=0; i<num_iov; i+=4) {
3009 uint8_t *inbuf_ref = NULL;
3010 struct iovec *cur = &iov[i];
3011 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3012 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3013 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3014 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3015 uint16_t req_opcode;
3017 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3018 uint32_t new_credits;
3019 struct smbXcli_session *session = NULL;
3020 const DATA_BLOB *signing_key = NULL;
3022 new_credits = conn->smb2.cur_credits;
3023 new_credits += credits;
3024 if (new_credits > UINT16_MAX) {
3025 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3027 conn->smb2.cur_credits += credits;
3029 req = smb2cli_conn_find_pending(conn, mid);
3031 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3033 state = tevent_req_data(req, struct smbXcli_req_state);
3035 state->smb2.got_async = false;
3037 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3038 if (opcode != req_opcode) {
3039 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3041 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3043 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3044 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3047 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3048 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3049 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3050 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3053 * async interim responses are not signed,
3054 * even if the SMB2_HDR_FLAG_SIGNED flag
3057 req_flags |= SMB2_HDR_FLAG_ASYNC;
3058 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3059 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3061 if (state->smb2.notify_async) {
3062 state->smb2.got_async = true;
3063 tevent_req_defer_callback(req, state->ev);
3064 tevent_req_notify_callback(req);
3069 session = state->session;
3070 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3071 session = last_session;
3073 last_session = session;
3075 if (state->smb2.should_sign) {
3076 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3077 return NT_STATUS_ACCESS_DENIED;
3081 if (flags & SMB2_HDR_FLAG_SIGNED) {
3082 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3084 if (session == NULL) {
3085 struct smbXcli_session *s;
3087 s = state->conn->sessions;
3088 for (; s; s = s->next) {
3089 if (s->smb2.session_id != uid) {
3098 if (session == NULL) {
3099 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3102 last_session = session;
3103 signing_key = &session->smb2.channel_signing_key;
3106 if (opcode == SMB2_OP_SESSSETUP) {
3108 * We prefer the channel signing key, if it is
3111 * If we do not have a channel signing key yet,
3112 * we try the main signing key, if it is not
3113 * the final response.
3115 if (signing_key && signing_key->length == 0 &&
3116 !NT_STATUS_IS_OK(status)) {
3117 signing_key = &session->smb2.signing_key;
3120 if (signing_key && signing_key->length == 0) {
3122 * If we do not have a session key to
3123 * verify the signature, we defer the
3124 * signing check to the caller.
3126 * The caller gets NT_STATUS_OK, it
3128 * smb2cli_session_set_session_key()
3130 * smb2cli_session_set_channel_key()
3131 * which will check the signature
3132 * with the channel signing key.
3138 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3140 * if the server returns NT_STATUS_USER_SESSION_DELETED
3141 * the response is not signed and we should
3142 * propagate the NT_STATUS_USER_SESSION_DELETED
3143 * status to the caller.
3146 } else if (state->smb2.should_encrypt) {
3147 if (cur[0].iov_len != SMB2_TF_HDR_SIZE) {
3148 return NT_STATUS_ACCESS_DENIED;
3152 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3153 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3154 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3156 * if the server returns
3157 * NT_STATUS_NETWORK_NAME_DELETED
3158 * NT_STATUS_FILE_CLOSED
3159 * NT_STATUS_INVALID_PARAMETER
3160 * the response might not be signed
3161 * as this happens before the signing checks.
3163 * If server echos the signature (or all zeros)
3164 * we should report the status from the server
3170 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3171 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3174 state->smb2.signing_skipped = true;
3180 static const uint8_t zeros[16];
3182 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3186 state->smb2.signing_skipped = true;
3193 status = smb2_signing_check_pdu(*signing_key,
3194 state->conn->protocol,
3196 if (!NT_STATUS_IS_OK(status)) {
3198 * If the signing check fails, we disconnect
3205 smbXcli_req_unset_pending(req);
3208 * There might be more than one response
3209 * we need to defer the notifications
3211 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3216 tevent_req_defer_callback(req, state->ev);
3220 * Note: here we use talloc_reference() in a way
3221 * that does not expose it to the caller.
3223 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3224 if (tevent_req_nomem(inbuf_ref, req)) {
3228 /* copy the related buffers */
3229 state->smb2.recv_iov[0] = cur[1];
3230 state->smb2.recv_iov[1] = cur[2];
3231 state->smb2.recv_iov[2] = cur[3];
3233 tevent_req_done(req);
3237 return NT_STATUS_RETRY;
3240 return NT_STATUS_OK;
3243 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3244 struct iovec **piov,
3245 const struct smb2cli_req_expected_response *expected,
3246 size_t num_expected)
3248 struct smbXcli_req_state *state =
3249 tevent_req_data(req,
3250 struct smbXcli_req_state);
3253 bool found_status = false;
3254 bool found_size = false;
3261 if (state->smb2.got_async) {
3262 return STATUS_PENDING;
3265 if (tevent_req_is_nterror(req, &status)) {
3266 for (i=0; i < num_expected; i++) {
3267 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3268 found_status = true;
3274 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3280 if (num_expected == 0) {
3281 found_status = true;
3285 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3286 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3288 for (i=0; i < num_expected; i++) {
3289 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3293 found_status = true;
3294 if (expected[i].body_size == 0) {
3299 if (expected[i].body_size == body_size) {
3305 if (!found_status) {
3309 if (state->smb2.signing_skipped) {
3310 if (num_expected > 0) {
3311 return NT_STATUS_ACCESS_DENIED;
3313 if (!NT_STATUS_IS_ERR(status)) {
3314 return NT_STATUS_ACCESS_DENIED;
3319 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3323 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3329 static const struct {
3330 enum protocol_types proto;
3331 const char *smb1_name;
3332 } smb1cli_prots[] = {
3333 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3334 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3335 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3336 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3337 {PROTOCOL_LANMAN2, "LM1.2X002"},
3338 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3339 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3340 {PROTOCOL_LANMAN2, "Samba"},
3341 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3342 {PROTOCOL_NT1, "NT LM 0.12"},
3343 {PROTOCOL_SMB2_02, "SMB 2.002"},
3344 {PROTOCOL_SMB2_10, "SMB 2.???"},
3347 static const struct {
3348 enum protocol_types proto;
3349 uint16_t smb2_dialect;
3350 } smb2cli_prots[] = {
3351 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3352 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3353 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3354 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3355 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3358 struct smbXcli_negprot_state {
3359 struct smbXcli_conn *conn;
3360 struct tevent_context *ev;
3361 uint32_t timeout_msec;
3362 enum protocol_types min_protocol;
3363 enum protocol_types max_protocol;
3367 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3371 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3372 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3373 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3374 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3375 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3376 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3380 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3381 struct tevent_context *ev,
3382 struct smbXcli_conn *conn,
3383 uint32_t timeout_msec,
3384 enum protocol_types min_protocol,
3385 enum protocol_types max_protocol)
3387 struct tevent_req *req, *subreq;
3388 struct smbXcli_negprot_state *state;
3390 req = tevent_req_create(mem_ctx, &state,
3391 struct smbXcli_negprot_state);
3397 state->timeout_msec = timeout_msec;
3398 state->min_protocol = min_protocol;
3399 state->max_protocol = max_protocol;
3401 if (min_protocol == PROTOCOL_NONE) {
3402 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3403 return tevent_req_post(req, ev);
3406 if (max_protocol == PROTOCOL_NONE) {
3407 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3408 return tevent_req_post(req, ev);
3411 if (min_protocol > max_protocol) {
3412 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3413 return tevent_req_post(req, ev);
3416 if ((min_protocol < PROTOCOL_SMB2_02) &&
3417 (max_protocol < PROTOCOL_SMB2_02)) {
3421 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3423 subreq = smbXcli_negprot_smb1_subreq(state);
3424 if (tevent_req_nomem(subreq, req)) {
3425 return tevent_req_post(req, ev);
3427 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3431 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3432 (max_protocol >= PROTOCOL_SMB2_02)) {
3436 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3438 subreq = smbXcli_negprot_smb2_subreq(state);
3439 if (tevent_req_nomem(subreq, req)) {
3440 return tevent_req_post(req, ev);
3442 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3447 * We send an SMB1 negprot with the SMB2 dialects
3448 * and expect a SMB1 or a SMB2 response.
3450 * smbXcli_negprot_dispatch_incoming() will fix the
3451 * callback to match protocol of the response.
3453 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3455 subreq = smbXcli_negprot_smb1_subreq(state);
3456 if (tevent_req_nomem(subreq, req)) {
3457 return tevent_req_post(req, ev);
3459 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3463 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3465 struct tevent_req *req =
3466 tevent_req_callback_data(subreq,
3471 * we just want the low level error
3473 status = tevent_req_simple_recv_ntstatus(subreq);
3474 TALLOC_FREE(subreq);
3475 if (tevent_req_nterror(req, status)) {
3479 /* this should never happen */
3480 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3483 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3486 DATA_BLOB bytes = data_blob_null;
3490 /* setup the protocol strings */
3491 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3495 if (smb1cli_prots[i].proto < state->min_protocol) {
3499 if (smb1cli_prots[i].proto > state->max_protocol) {
3503 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3509 * We now it is already ascii and
3510 * we want NULL termination.
3512 ok = data_blob_append(state, &bytes,
3513 smb1cli_prots[i].smb1_name,
3514 strlen(smb1cli_prots[i].smb1_name)+1);
3520 smb1cli_req_flags(state->max_protocol,
3521 state->conn->smb1.client.capabilities,
3526 return smb1cli_req_send(state, state->ev, state->conn,
3530 state->timeout_msec,
3531 0xFFFE, 0, 0, /* pid, tid, uid */
3532 0, NULL, /* wct, vwv */
3533 bytes.length, bytes.data);
3536 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3538 struct tevent_req *req =
3539 tevent_req_callback_data(subreq,
3541 struct smbXcli_negprot_state *state =
3542 tevent_req_data(req,
3543 struct smbXcli_negprot_state);
3544 struct smbXcli_conn *conn = state->conn;
3545 struct iovec *recv_iov = NULL;
3554 size_t num_prots = 0;
3556 uint32_t client_capabilities = conn->smb1.client.capabilities;
3557 uint32_t both_capabilities;
3558 uint32_t server_capabilities = 0;
3559 uint32_t capabilities;
3560 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3561 uint32_t server_max_xmit = 0;
3563 uint32_t server_max_mux = 0;
3564 uint16_t server_security_mode = 0;
3565 uint32_t server_session_key = 0;
3566 bool server_readbraw = false;
3567 bool server_writebraw = false;
3568 bool server_lockread = false;
3569 bool server_writeunlock = false;
3570 struct GUID server_guid = GUID_zero();
3571 DATA_BLOB server_gss_blob = data_blob_null;
3572 uint8_t server_challenge[8];
3573 char *server_workgroup = NULL;
3574 char *server_name = NULL;
3575 int server_time_zone = 0;
3576 NTTIME server_system_time = 0;
3577 static const struct smb1cli_req_expected_response expected[] = {
3579 .status = NT_STATUS_OK,
3580 .wct = 0x11, /* NT1 */
3583 .status = NT_STATUS_OK,
3584 .wct = 0x0D, /* LM */
3587 .status = NT_STATUS_OK,
3588 .wct = 0x01, /* CORE */
3592 ZERO_STRUCT(server_challenge);
3594 status = smb1cli_req_recv(subreq, state,
3599 NULL, /* pvwv_offset */
3602 NULL, /* pbytes_offset */
3604 expected, ARRAY_SIZE(expected));
3605 TALLOC_FREE(subreq);
3606 if (tevent_req_nterror(req, status)) {
3610 flags = CVAL(inhdr, HDR_FLG);
3612 protnum = SVAL(vwv, 0);
3614 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3615 if (smb1cli_prots[i].proto < state->min_protocol) {
3619 if (smb1cli_prots[i].proto > state->max_protocol) {
3623 if (protnum != num_prots) {
3628 conn->protocol = smb1cli_prots[i].proto;
3632 if (conn->protocol == PROTOCOL_NONE) {
3633 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3637 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3638 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3639 "and the selected protocol level doesn't support it.\n"));
3640 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3644 if (flags & FLAG_SUPPORT_LOCKREAD) {
3645 server_lockread = true;
3646 server_writeunlock = true;
3649 if (conn->protocol >= PROTOCOL_NT1) {
3650 const char *client_signing = NULL;
3651 bool server_mandatory = false;
3652 bool server_allowed = false;
3653 const char *server_signing = NULL;
3658 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3663 server_security_mode = CVAL(vwv + 1, 0);
3664 server_max_mux = SVAL(vwv + 1, 1);
3665 server_max_xmit = IVAL(vwv + 3, 1);
3666 server_session_key = IVAL(vwv + 7, 1);
3667 server_time_zone = SVALS(vwv + 15, 1);
3668 server_time_zone *= 60;
3669 /* this time arrives in real GMT */
3670 server_system_time = BVAL(vwv + 11, 1);
3671 server_capabilities = IVAL(vwv + 9, 1);
3673 key_len = CVAL(vwv + 16, 1);
3675 if (server_capabilities & CAP_RAW_MODE) {
3676 server_readbraw = true;
3677 server_writebraw = true;
3679 if (server_capabilities & CAP_LOCK_AND_READ) {
3680 server_lockread = true;
3683 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3684 DATA_BLOB blob1, blob2;
3686 if (num_bytes < 16) {
3687 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3691 blob1 = data_blob_const(bytes, 16);
3692 status = GUID_from_data_blob(&blob1, &server_guid);
3693 if (tevent_req_nterror(req, status)) {
3697 blob1 = data_blob_const(bytes+16, num_bytes-16);
3698 blob2 = data_blob_dup_talloc(state, blob1);
3699 if (blob1.length > 0 &&
3700 tevent_req_nomem(blob2.data, req)) {
3703 server_gss_blob = blob2;
3705 DATA_BLOB blob1, blob2;
3707 if (num_bytes < key_len) {
3708 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3712 if (key_len != 0 && key_len != 8) {
3713 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3718 memcpy(server_challenge, bytes, 8);
3721 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3722 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3723 if (blob1.length > 0) {
3726 len = utf16_len_n(blob1.data,
3730 ok = convert_string_talloc(state,
3738 status = map_nt_error_from_unix_common(errno);
3739 tevent_req_nterror(req, status);
3744 blob2.data += blob1.length;
3745 blob2.length -= blob1.length;
3746 if (blob2.length > 0) {
3749 len = utf16_len_n(blob1.data,
3753 ok = convert_string_talloc(state,
3761 status = map_nt_error_from_unix_common(errno);
3762 tevent_req_nterror(req, status);
3768 client_signing = "disabled";
3769 if (conn->allow_signing) {
3770 client_signing = "allowed";
3772 if (conn->mandatory_signing) {
3773 client_signing = "required";
3776 server_signing = "not supported";
3777 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3778 server_signing = "supported";
3779 server_allowed = true;
3781 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3782 server_signing = "required";
3783 server_mandatory = true;
3786 ok = smb_signing_set_negotiated(conn->smb1.signing,
3790 DEBUG(1,("cli_negprot: SMB signing is required, "
3791 "but client[%s] and server[%s] mismatch\n",
3792 client_signing, server_signing));
3793 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3797 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3803 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3807 server_security_mode = SVAL(vwv + 1, 0);
3808 server_max_xmit = SVAL(vwv + 2, 0);
3809 server_max_mux = SVAL(vwv + 3, 0);
3810 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3811 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3812 server_session_key = IVAL(vwv + 6, 0);
3813 server_time_zone = SVALS(vwv + 10, 0);
3814 server_time_zone *= 60;
3815 /* this time is converted to GMT by make_unix_date */
3816 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3817 unix_to_nt_time(&server_system_time, t);
3818 key_len = SVAL(vwv + 11, 0);
3820 if (num_bytes < key_len) {
3821 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3825 if (key_len != 0 && key_len != 8) {
3826 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3831 memcpy(server_challenge, bytes, 8);
3834 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3835 if (blob1.length > 0) {
3839 len = utf16_len_n(blob1.data,
3843 ok = convert_string_talloc(state,
3851 status = map_nt_error_from_unix_common(errno);
3852 tevent_req_nterror(req, status);
3858 /* the old core protocol */
3859 server_time_zone = get_time_zone(time(NULL));
3860 server_max_xmit = 1024;
3864 if (server_max_xmit < 1024) {
3865 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3869 if (server_max_mux < 1) {
3870 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3875 * Now calculate the negotiated capabilities
3876 * based on the mask for:
3877 * - client only flags
3878 * - flags used in both directions
3879 * - server only flags
3881 both_capabilities = client_capabilities & server_capabilities;
3882 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3883 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3884 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3886 max_xmit = MIN(client_max_xmit, server_max_xmit);
3888 conn->smb1.server.capabilities = server_capabilities;
3889 conn->smb1.capabilities = capabilities;
3891 conn->smb1.server.max_xmit = server_max_xmit;
3892 conn->smb1.max_xmit = max_xmit;
3894 conn->smb1.server.max_mux = server_max_mux;
3896 conn->smb1.server.security_mode = server_security_mode;
3898 conn->smb1.server.readbraw = server_readbraw;
3899 conn->smb1.server.writebraw = server_writebraw;
3900 conn->smb1.server.lockread = server_lockread;
3901 conn->smb1.server.writeunlock = server_writeunlock;
3903 conn->smb1.server.session_key = server_session_key;
3905 talloc_steal(conn, server_gss_blob.data);
3906 conn->smb1.server.gss_blob = server_gss_blob;
3907 conn->smb1.server.guid = server_guid;
3908 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3909 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3910 conn->smb1.server.name = talloc_move(conn, &server_name);
3912 conn->smb1.server.time_zone = server_time_zone;
3913 conn->smb1.server.system_time = server_system_time;
3915 tevent_req_done(req);
3918 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
3922 uint16_t dialect_count = 0;
3924 buf = state->smb2.dyn;
3925 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3926 if (smb2cli_prots[i].proto < state->min_protocol) {
3930 if (smb2cli_prots[i].proto > state->max_protocol) {
3934 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
3938 buf = state->smb2.fixed;
3940 SSVAL(buf, 2, dialect_count);
3941 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
3942 SSVAL(buf, 6, 0); /* Reserved */
3943 if (state->max_protocol >= PROTOCOL_SMB2_22) {
3944 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
3946 SIVAL(buf, 8, 0); /* Capabilities */
3948 if (state->max_protocol >= PROTOCOL_SMB2_10) {
3952 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
3954 if (!NT_STATUS_IS_OK(status)) {
3957 memcpy(buf+12, blob.data, 16); /* ClientGuid */
3959 memset(buf+12, 0, 16); /* ClientGuid */
3961 SBVAL(buf, 28, 0); /* ClientStartTime */
3963 return smb2cli_req_send(state, state->ev,
3964 state->conn, SMB2_OP_NEGPROT,
3966 state->timeout_msec,
3967 0xFEFF, 0, NULL, /* pid, tid, session */
3968 state->smb2.fixed, sizeof(state->smb2.fixed),
3969 state->smb2.dyn, dialect_count*2);
3972 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
3974 struct tevent_req *req =
3975 tevent_req_callback_data(subreq,
3977 struct smbXcli_negprot_state *state =
3978 tevent_req_data(req,
3979 struct smbXcli_negprot_state);
3980 struct smbXcli_conn *conn = state->conn;
3981 size_t security_offset, security_length;
3987 uint16_t dialect_revision;
3988 static const struct smb2cli_req_expected_response expected[] = {
3990 .status = NT_STATUS_OK,
3995 status = smb2cli_req_recv(subreq, state, &iov,
3996 expected, ARRAY_SIZE(expected));
3997 TALLOC_FREE(subreq);
3998 if (tevent_req_nterror(req, status)) {
4002 body = (uint8_t *)iov[1].iov_base;
4004 dialect_revision = SVAL(body, 4);
4006 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4007 if (smb2cli_prots[i].proto < state->min_protocol) {
4011 if (smb2cli_prots[i].proto > state->max_protocol) {
4015 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4019 conn->protocol = smb2cli_prots[i].proto;
4023 if (conn->protocol == PROTOCOL_NONE) {
4024 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4025 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4029 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4030 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4034 /* make sure we do not loop forever */
4035 state->min_protocol = PROTOCOL_SMB2_02;
4038 * send a SMB2 negprot, in order to negotiate
4041 subreq = smbXcli_negprot_smb2_subreq(state);
4042 if (tevent_req_nomem(subreq, req)) {
4045 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4049 conn->smb2.server.security_mode = SVAL(body, 2);
4051 blob = data_blob_const(body + 8, 16);
4052 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4053 if (tevent_req_nterror(req, status)) {
4057 conn->smb2.server.capabilities = IVAL(body, 24);
4058 conn->smb2.server.max_trans_size= IVAL(body, 28);
4059 conn->smb2.server.max_read_size = IVAL(body, 32);
4060 conn->smb2.server.max_write_size= IVAL(body, 36);
4061 conn->smb2.server.system_time = BVAL(body, 40);
4062 conn->smb2.server.start_time = BVAL(body, 48);
4064 security_offset = SVAL(body, 56);
4065 security_length = SVAL(body, 58);
4067 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4068 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4072 if (security_length > iov[2].iov_len) {
4073 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4077 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4080 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4084 tevent_req_done(req);
4087 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4088 TALLOC_CTX *tmp_mem,
4091 size_t num_pending = talloc_array_length(conn->pending);
4092 struct tevent_req *subreq;
4093 struct smbXcli_req_state *substate;
4094 struct tevent_req *req;
4095 uint32_t protocol_magic;
4096 size_t inbuf_len = smb_len_nbt(inbuf);
4098 if (num_pending != 1) {
4099 return NT_STATUS_INTERNAL_ERROR;
4102 if (inbuf_len < 4) {
4103 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4106 subreq = conn->pending[0];
4107 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4108 req = tevent_req_callback_data(subreq, struct tevent_req);
4110 protocol_magic = IVAL(inbuf, 4);
4112 switch (protocol_magic) {
4114 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4115 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4116 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4119 if (substate->smb2.recv_iov == NULL) {
4121 * For the SMB1 negprot we have move it.
4123 substate->smb2.recv_iov = substate->smb1.recv_iov;
4124 substate->smb1.recv_iov = NULL;
4128 * we got an SMB2 answer, which consumed sequence number 0
4129 * so we need to use 1 as the next one
4132 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4133 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4134 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4137 DEBUG(10, ("Got non-SMB PDU\n"));
4138 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4141 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4143 return tevent_req_simple_recv_ntstatus(req);
4146 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4147 uint32_t timeout_msec,
4148 enum protocol_types min_protocol,
4149 enum protocol_types max_protocol)
4151 TALLOC_CTX *frame = talloc_stackframe();
4152 struct tevent_context *ev;
4153 struct tevent_req *req;
4154 NTSTATUS status = NT_STATUS_NO_MEMORY;
4157 if (smbXcli_conn_has_async_calls(conn)) {
4159 * Can't use sync call while an async call is in flight
4161 status = NT_STATUS_INVALID_PARAMETER_MIX;
4164 ev = tevent_context_init(frame);
4168 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4169 min_protocol, max_protocol);
4173 ok = tevent_req_poll(req, ev);
4175 status = map_nt_error_from_unix_common(errno);
4178 status = smbXcli_negprot_recv(req);
4184 static int smbXcli_session_destructor(struct smbXcli_session *session)
4186 if (session->conn == NULL) {
4190 DLIST_REMOVE(session->conn->sessions, session);
4194 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4195 struct smbXcli_conn *conn)
4197 struct smbXcli_session *session;
4199 session = talloc_zero(mem_ctx, struct smbXcli_session);
4200 if (session == NULL) {
4203 talloc_set_destructor(session, smbXcli_session_destructor);
4205 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4206 session->conn = conn;
4211 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4213 struct smbXcli_conn *conn = session->conn;
4214 uint8_t security_mode = 0;
4217 return security_mode;
4220 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4221 if (conn->mandatory_signing) {
4222 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4225 return security_mode;
4228 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4230 return session->smb2.session_id;
4233 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4235 return session->smb2.session_flags;
4238 NTSTATUS smb2cli_session_application_key(struct smbXcli_session *session,
4239 TALLOC_CTX *mem_ctx,
4242 *key = data_blob_null;
4244 if (session->smb2.application_key.length == 0) {
4245 return NT_STATUS_NO_USER_SESSION_KEY;
4248 *key = data_blob_dup_talloc(mem_ctx, session->smb2.application_key);
4249 if (key->data == NULL) {
4250 return NT_STATUS_NO_MEMORY;
4253 return NT_STATUS_OK;
4256 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4257 uint64_t session_id,
4258 uint16_t session_flags)
4260 session->smb2.session_id = session_id;
4261 session->smb2.session_flags = session_flags;
4264 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4265 const DATA_BLOB _session_key,
4266 const struct iovec *recv_iov)
4268 struct smbXcli_conn *conn = session->conn;
4269 uint16_t no_sign_flags;
4270 uint8_t session_key[16];
4274 return NT_STATUS_INVALID_PARAMETER_MIX;
4277 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4279 if (session->smb2.session_flags & no_sign_flags) {
4280 session->smb2.should_sign = false;
4281 return NT_STATUS_OK;
4284 if (session->smb2.signing_key.length != 0) {
4285 return NT_STATUS_INVALID_PARAMETER_MIX;
4288 ZERO_STRUCT(session_key);
4289 memcpy(session_key, _session_key.data,
4290 MIN(_session_key.length, sizeof(session_key)));
4292 session->smb2.signing_key = data_blob_talloc(session,
4294 sizeof(session_key));
4295 if (session->smb2.signing_key.data == NULL) {
4296 ZERO_STRUCT(session_key);
4297 return NT_STATUS_NO_MEMORY;
4300 if (conn->protocol >= PROTOCOL_SMB2_24) {
4301 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4302 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4304 smb2_key_derivation(session_key, sizeof(session_key),
4305 label.data, label.length,
4306 context.data, context.length,
4307 session->smb2.signing_key.data);
4310 session->smb2.encryption_key = data_blob_dup_talloc(session,
4311 session->smb2.signing_key);
4312 if (session->smb2.encryption_key.data == NULL) {
4313 ZERO_STRUCT(session_key);
4314 return NT_STATUS_NO_MEMORY;
4317 if (conn->protocol >= PROTOCOL_SMB2_24) {
4318 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4319 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4321 smb2_key_derivation(session_key, sizeof(session_key),
4322 label.data, label.length,
4323 context.data, context.length,
4324 session->smb2.encryption_key.data);
4327 session->smb2.decryption_key = data_blob_dup_talloc(session,
4328 session->smb2.signing_key);
4329 if (session->smb2.decryption_key.data == NULL) {
4330 ZERO_STRUCT(session_key);
4331 return NT_STATUS_NO_MEMORY;
4334 if (conn->protocol >= PROTOCOL_SMB2_24) {
4335 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4336 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4338 smb2_key_derivation(session_key, sizeof(session_key),
4339 label.data, label.length,
4340 context.data, context.length,
4341 session->smb2.decryption_key.data);
4344 session->smb2.application_key = data_blob_dup_talloc(session,
4345 session->smb2.signing_key);
4346 if (session->smb2.application_key.data == NULL) {
4347 ZERO_STRUCT(session_key);
4348 return NT_STATUS_NO_MEMORY;
4351 if (conn->protocol >= PROTOCOL_SMB2_24) {
4352 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4353 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4355 smb2_key_derivation(session_key, sizeof(session_key),
4356 label.data, label.length,
4357 context.data, context.length,
4358 session->smb2.application_key.data);
4360 ZERO_STRUCT(session_key);
4362 session->smb2.channel_signing_key = data_blob_dup_talloc(session,
4363 session->smb2.signing_key);
4364 if (session->smb2.channel_signing_key.data == NULL) {
4365 return NT_STATUS_NO_MEMORY;
4368 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4369 session->conn->protocol,
4371 if (!NT_STATUS_IS_OK(status)) {
4375 session->smb2.should_sign = false;
4376 session->smb2.should_encrypt = false;
4378 if (conn->desire_signing) {
4379 session->smb2.should_sign = true;
4382 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4383 session->smb2.should_sign = true;
4386 if (session->smb2.session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4387 session->smb2.should_encrypt = true;
4390 if (conn->protocol < PROTOCOL_SMB2_24) {
4391 session->smb2.should_encrypt = false;
4394 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4395 session->smb2.should_encrypt = false;
4398 generate_random_buffer((uint8_t *)&session->smb2.channel_nonce,
4399 sizeof(session->smb2.channel_nonce));
4400 session->smb2.channel_next = 1;
4402 return NT_STATUS_OK;
4405 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4406 struct smbXcli_session *session1,
4407 struct smbXcli_conn *conn,
4408 struct smbXcli_session **_session2)
4410 struct smbXcli_session *session2;
4412 if (session1->smb2.signing_key.length == 0) {
4413 return NT_STATUS_INVALID_PARAMETER_MIX;
4416 if (session1->smb2.channel_next == 0) {
4417 return NT_STATUS_INVALID_PARAMETER_MIX;
4421 return NT_STATUS_INVALID_PARAMETER_MIX;
4424 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4425 if (session2 == NULL) {
4426 return NT_STATUS_NO_MEMORY;
4428 session2->smb2.session_id = session1->smb2.session_id;
4429 session2->smb2.session_flags = session1->smb2.session_flags;
4431 session2->smb2.signing_key = data_blob_dup_talloc(session2,
4432 session1->smb2.signing_key);
4433 if (session2->smb2.signing_key.data == NULL) {
4434 return NT_STATUS_NO_MEMORY;
4437 session2->smb2.application_key = data_blob_dup_talloc(session2,
4438 session1->smb2.application_key);
4439 if (session2->smb2.application_key.data == NULL) {
4440 return NT_STATUS_NO_MEMORY;
4443 session2->smb2.should_sign = session1->smb2.should_sign;
4444 session2->smb2.should_encrypt = session1->smb2.should_encrypt;
4446 session2->smb2.channel_nonce = session1->smb2.channel_nonce;
4447 session2->smb2.channel_nonce += session1->smb2.channel_next;
4448 session1->smb2.channel_next++;
4450 session2->smb2.encryption_key = data_blob_dup_talloc(session2,
4451 session1->smb2.encryption_key);
4452 if (session2->smb2.encryption_key.data == NULL) {
4453 return NT_STATUS_NO_MEMORY;
4456 session2->smb2.decryption_key = data_blob_dup_talloc(session2,
4457 session1->smb2.decryption_key);
4458 if (session2->smb2.decryption_key.data == NULL) {
4459 return NT_STATUS_NO_MEMORY;
4462 talloc_set_destructor(session2, smbXcli_session_destructor);
4463 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4464 session2->conn = conn;
4466 *_session2 = session2;
4467 return NT_STATUS_OK;
4470 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4471 const DATA_BLOB _channel_key,
4472 const struct iovec *recv_iov)
4474 struct smbXcli_conn *conn = session->conn;
4475 uint8_t channel_key[16];
4479 return NT_STATUS_INVALID_PARAMETER_MIX;
4482 if (session->smb2.channel_signing_key.length != 0) {
4483 return NT_STATUS_INVALID_PARAMETER_MIX;
4486 ZERO_STRUCT(channel_key);
4487 memcpy(channel_key, _channel_key.data,
4488 MIN(_channel_key.length, sizeof(channel_key)));
4490 session->smb2.channel_signing_key = data_blob_talloc(session,
4492 sizeof(channel_key));
4493 if (session->smb2.channel_signing_key.data == NULL) {
4494 ZERO_STRUCT(channel_key);
4495 return NT_STATUS_NO_MEMORY;
4498 if (conn->protocol >= PROTOCOL_SMB2_24) {
4499 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4500 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4502 smb2_key_derivation(channel_key, sizeof(channel_key),
4503 label.data, label.length,
4504 context.data, context.length,
4505 session->smb2.channel_signing_key.data);
4507 ZERO_STRUCT(channel_key);
4509 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4510 session->conn->protocol,
4512 if (!NT_STATUS_IS_OK(status)) {
4516 return NT_STATUS_OK;