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;
43 struct sockaddr_storage local_ss;
44 struct sockaddr_storage remote_ss;
45 const char *remote_name;
47 struct tevent_queue *outgoing;
48 struct tevent_req **pending;
49 struct tevent_req *read_smb_req;
51 enum protocol_types protocol;
54 bool mandatory_signing;
57 * The incoming dispatch function should return:
58 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
59 * - NT_STATUS_OK, if no more processing is desired, e.g.
60 * the dispatch function called
62 * - All other return values disconnect the connection.
64 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
70 uint32_t capabilities;
75 uint32_t capabilities;
78 uint16_t security_mode;
87 const char *workgroup;
93 uint32_t capabilities;
98 struct smb_signing_state *signing;
99 struct smb_trans_enc_state *trans_enc;
101 struct tevent_req *read_braw_req;
106 uint32_t capabilities;
107 uint16_t security_mode;
112 uint32_t capabilities;
113 uint16_t security_mode;
115 uint32_t max_trans_size;
116 uint32_t max_read_size;
117 uint32_t max_write_size;
124 uint16_t cur_credits;
125 uint16_t max_credits;
128 struct smbXcli_session *sessions;
131 struct smb2cli_session {
133 uint16_t session_flags;
134 DATA_BLOB application_key;
135 DATA_BLOB signing_key;
138 DATA_BLOB encryption_key;
139 DATA_BLOB decryption_key;
142 uint16_t channel_sequence;
145 struct smbXcli_session {
146 struct smbXcli_session *prev, *next;
147 struct smbXcli_conn *conn;
151 DATA_BLOB application_key;
154 struct smb2cli_session *smb2;
157 DATA_BLOB signing_key;
161 struct smbXcli_tcon {
164 uint16_t optional_support;
165 uint32_t maximal_access;
166 uint32_t guest_maximal_access;
175 uint32_t capabilities;
176 uint32_t maximal_access;
181 struct smbXcli_req_state {
182 struct tevent_context *ev;
183 struct smbXcli_conn *conn;
184 struct smbXcli_session *session; /* maybe NULL */
185 struct smbXcli_tcon *tcon; /* maybe NULL */
187 uint8_t length_hdr[4];
194 /* Space for the header including the wct */
195 uint8_t hdr[HDR_VWV];
198 * For normal requests, smb1cli_req_send chooses a mid.
199 * SecondaryV trans requests need to use the mid of the primary
200 * request, so we need a place to store it.
201 * Assume it is set if != 0.
206 uint8_t bytecount_buf[2];
208 #define MAX_SMB_IOV 10
209 /* length_hdr, hdr, words, byte_count, buffers */
210 struct iovec iov[1 + 3 + MAX_SMB_IOV];
215 struct tevent_req **chained_requests;
218 NTSTATUS recv_status;
219 /* always an array of 3 talloc elements */
220 struct iovec *recv_iov;
224 const uint8_t *fixed;
229 uint8_t transform[SMB2_TF_HDR_SIZE];
230 uint8_t hdr[SMB2_HDR_BODY];
231 uint8_t pad[7]; /* padding space for compounding */
234 * always an array of 3 talloc elements
235 * (without a SMB2_TRANSFORM header!)
239 struct iovec *recv_iov;
241 uint16_t credit_charge;
246 bool signing_skipped;
252 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
255 * NT_STATUS_OK, means we do not notify the callers
257 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
259 while (conn->sessions) {
260 conn->sessions->conn = NULL;
261 DLIST_REMOVE(conn->sessions, conn->sessions);
264 if (conn->smb1.trans_enc) {
265 TALLOC_FREE(conn->smb1.trans_enc);
271 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
273 const char *remote_name,
274 enum smb_signing_setting signing_state,
275 uint32_t smb1_capabilities,
276 struct GUID *client_guid,
277 uint32_t smb2_capabilities)
279 struct smbXcli_conn *conn = NULL;
281 struct sockaddr *sa = NULL;
285 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
291 conn->write_fd = dup(fd);
292 if (conn->write_fd == -1) {
296 conn->remote_name = talloc_strdup(conn, remote_name);
297 if (conn->remote_name == NULL) {
302 ss = (void *)&conn->local_ss;
303 sa = (struct sockaddr *)ss;
304 sa_length = sizeof(conn->local_ss);
305 ret = getsockname(fd, sa, &sa_length);
309 ss = (void *)&conn->remote_ss;
310 sa = (struct sockaddr *)ss;
311 sa_length = sizeof(conn->remote_ss);
312 ret = getpeername(fd, sa, &sa_length);
317 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
318 if (conn->outgoing == NULL) {
321 conn->pending = NULL;
323 conn->protocol = PROTOCOL_NONE;
325 switch (signing_state) {
326 case SMB_SIGNING_OFF:
328 conn->allow_signing = false;
329 conn->desire_signing = false;
330 conn->mandatory_signing = false;
332 case SMB_SIGNING_DEFAULT:
333 case SMB_SIGNING_IF_REQUIRED:
334 /* if the server requires it */
335 conn->allow_signing = true;
336 conn->desire_signing = false;
337 conn->mandatory_signing = false;
339 case SMB_SIGNING_REQUIRED:
341 conn->allow_signing = true;
342 conn->desire_signing = true;
343 conn->mandatory_signing = true;
347 conn->smb1.client.capabilities = smb1_capabilities;
348 conn->smb1.client.max_xmit = UINT16_MAX;
350 conn->smb1.capabilities = conn->smb1.client.capabilities;
351 conn->smb1.max_xmit = 1024;
355 /* initialise signing */
356 conn->smb1.signing = smb_signing_init(conn,
358 conn->desire_signing,
359 conn->mandatory_signing);
360 if (!conn->smb1.signing) {
364 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
365 if (conn->mandatory_signing) {
366 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
369 conn->smb2.client.guid = *client_guid;
371 conn->smb2.client.capabilities = smb2_capabilities;
373 conn->smb2.cur_credits = 1;
374 conn->smb2.max_credits = 0;
376 talloc_set_destructor(conn, smbXcli_conn_destructor);
380 if (conn->write_fd != -1) {
381 close(conn->write_fd);
387 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
393 if (conn->read_fd == -1) {
400 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
402 return conn->protocol;
405 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
407 if (conn->protocol >= PROTOCOL_SMB2_02) {
411 if (conn->smb1.capabilities & CAP_UNICODE) {
418 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
420 set_socket_options(conn->read_fd, options);
423 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
425 return &conn->local_ss;
428 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
430 return &conn->remote_ss;
433 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
435 return conn->remote_name;
438 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
440 if (conn->protocol >= PROTOCOL_SMB2_02) {
447 return conn->smb1.server.max_mux;
450 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
452 if (conn->protocol >= PROTOCOL_SMB2_02) {
453 return conn->smb2.server.system_time;
456 return conn->smb1.server.system_time;
459 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
461 if (conn->protocol >= PROTOCOL_SMB2_02) {
462 return &conn->smb2.server.gss_blob;
465 return &conn->smb1.server.gss_blob;
468 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
470 if (conn->protocol >= PROTOCOL_SMB2_02) {
471 return &conn->smb2.server.guid;
474 return &conn->smb1.server.guid;
477 struct smbXcli_conn_samba_suicide_state {
478 struct smbXcli_conn *conn;
483 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
485 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
486 struct tevent_context *ev,
487 struct smbXcli_conn *conn,
490 struct tevent_req *req, *subreq;
491 struct smbXcli_conn_samba_suicide_state *state;
493 req = tevent_req_create(mem_ctx, &state,
494 struct smbXcli_conn_samba_suicide_state);
499 SIVAL(state->buf, 4, 0x74697865);
500 SCVAL(state->buf, 8, exitcode);
501 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
503 state->iov.iov_base = state->buf;
504 state->iov.iov_len = sizeof(state->buf);
506 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
507 false, &state->iov, 1);
508 if (tevent_req_nomem(subreq, req)) {
509 return tevent_req_post(req, ev);
511 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
515 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
517 struct tevent_req *req = tevent_req_callback_data(
518 subreq, struct tevent_req);
519 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
520 req, struct smbXcli_conn_samba_suicide_state);
524 nwritten = writev_recv(subreq, &err);
526 if (nwritten == -1) {
527 NTSTATUS status = map_nt_error_from_unix_common(err);
528 smbXcli_conn_disconnect(state->conn, status);
531 tevent_req_done(req);
534 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
536 return tevent_req_simple_recv_ntstatus(req);
539 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
542 TALLOC_CTX *frame = talloc_stackframe();
543 struct tevent_context *ev;
544 struct tevent_req *req;
545 NTSTATUS status = NT_STATUS_NO_MEMORY;
548 if (smbXcli_conn_has_async_calls(conn)) {
550 * Can't use sync call while an async call is in flight
552 status = NT_STATUS_INVALID_PARAMETER_MIX;
555 ev = tevent_context_init(frame);
559 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
563 ok = tevent_req_poll(req, ev);
565 status = map_nt_error_from_unix_common(errno);
568 status = smbXcli_conn_samba_suicide_recv(req);
574 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
576 return conn->smb1.capabilities;
579 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
581 return conn->smb1.max_xmit;
584 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
586 return conn->smb1.server.session_key;
589 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
591 return conn->smb1.server.challenge;
594 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
596 return conn->smb1.server.security_mode;
599 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
601 return conn->smb1.server.readbraw;
604 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
606 return conn->smb1.server.writebraw;
609 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
611 return conn->smb1.server.lockread;
614 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
616 return conn->smb1.server.writeunlock;
619 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
621 return conn->smb1.server.time_zone;
624 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
625 const DATA_BLOB user_session_key,
626 const DATA_BLOB response)
628 return smb_signing_activate(conn->smb1.signing,
633 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
634 const uint8_t *buf, uint32_t seqnum)
636 const uint8_t *hdr = buf + NBT_HDR_SIZE;
637 size_t len = smb_len_nbt(buf);
639 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
642 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
644 return smb_signing_is_active(conn->smb1.signing);
647 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
648 struct smb_trans_enc_state *es)
650 /* Replace the old state, if any. */
651 if (conn->smb1.trans_enc) {
652 TALLOC_FREE(conn->smb1.trans_enc);
654 conn->smb1.trans_enc = es;
657 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
659 return common_encryption_on(conn->smb1.trans_enc);
663 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
665 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
666 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
668 if (NT_STATUS_IS_OK(status)) {
672 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
676 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
680 * Is the SMB command able to hold an AND_X successor
681 * @param[in] cmd The SMB command in question
682 * @retval Can we add a chained request after "cmd"?
684 bool smb1cli_is_andx_req(uint8_t cmd)
704 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
706 size_t num_pending = talloc_array_length(conn->pending);
712 result = conn->smb1.mid++;
713 if ((result == 0) || (result == 0xffff)) {
717 for (i=0; i<num_pending; i++) {
718 if (result == smb1cli_req_mid(conn->pending[i])) {
723 if (i == num_pending) {
729 void smbXcli_req_unset_pending(struct tevent_req *req)
731 struct smbXcli_req_state *state =
733 struct smbXcli_req_state);
734 struct smbXcli_conn *conn = state->conn;
735 size_t num_pending = talloc_array_length(conn->pending);
738 if (state->smb1.mid != 0) {
740 * This is a [nt]trans[2] request which waits
741 * for more than one reply.
746 talloc_set_destructor(req, NULL);
748 if (num_pending == 1) {
750 * The pending read_smb tevent_req is a child of
751 * conn->pending. So if nothing is pending anymore, we need to
752 * delete the socket read fde.
754 TALLOC_FREE(conn->pending);
755 conn->read_smb_req = NULL;
759 for (i=0; i<num_pending; i++) {
760 if (req == conn->pending[i]) {
764 if (i == num_pending) {
766 * Something's seriously broken. Just returning here is the
767 * right thing nevertheless, the point of this routine is to
768 * remove ourselves from conn->pending.
774 * Remove ourselves from the conn->pending array
776 for (; i < (num_pending - 1); i++) {
777 conn->pending[i] = conn->pending[i+1];
781 * No NULL check here, we're shrinking by sizeof(void *), and
782 * talloc_realloc just adjusts the size for this.
784 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
789 static int smbXcli_req_destructor(struct tevent_req *req)
791 struct smbXcli_req_state *state =
793 struct smbXcli_req_state);
796 * Make sure we really remove it from
797 * the pending array on destruction.
800 smbXcli_req_unset_pending(req);
804 static bool smb1cli_req_cancel(struct tevent_req *req);
805 static bool smb2cli_req_cancel(struct tevent_req *req);
807 static bool smbXcli_req_cancel(struct tevent_req *req)
809 struct smbXcli_req_state *state =
811 struct smbXcli_req_state);
813 if (!smbXcli_conn_is_connected(state->conn)) {
817 if (state->conn->protocol == PROTOCOL_NONE) {
821 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
822 return smb2cli_req_cancel(req);
825 return smb1cli_req_cancel(req);
828 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
830 bool smbXcli_req_set_pending(struct tevent_req *req)
832 struct smbXcli_req_state *state =
834 struct smbXcli_req_state);
835 struct smbXcli_conn *conn;
836 struct tevent_req **pending;
841 if (!smbXcli_conn_is_connected(conn)) {
845 num_pending = talloc_array_length(conn->pending);
847 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
849 if (pending == NULL) {
852 pending[num_pending] = req;
853 conn->pending = pending;
854 talloc_set_destructor(req, smbXcli_req_destructor);
855 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
857 if (!smbXcli_conn_receive_next(conn)) {
859 * the caller should notify the current request
861 * And all other pending requests get notified
862 * by smbXcli_conn_disconnect().
864 smbXcli_req_unset_pending(req);
865 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
872 static void smbXcli_conn_received(struct tevent_req *subreq);
874 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
876 size_t num_pending = talloc_array_length(conn->pending);
877 struct tevent_req *req;
878 struct smbXcli_req_state *state;
880 if (conn->read_smb_req != NULL) {
884 if (num_pending == 0) {
885 if (conn->smb2.mid < UINT64_MAX) {
886 /* no more pending requests, so we are done for now */
891 * If there are no more SMB2 requests possible,
892 * because we are out of message ids,
893 * we need to disconnect.
895 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
899 req = conn->pending[0];
900 state = tevent_req_data(req, struct smbXcli_req_state);
903 * We're the first ones, add the read_smb request that waits for the
904 * answer from the server
906 conn->read_smb_req = read_smb_send(conn->pending,
909 if (conn->read_smb_req == NULL) {
912 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
916 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
918 struct smbXcli_session *session;
920 tevent_queue_stop(conn->outgoing);
922 if (conn->read_fd != -1) {
923 close(conn->read_fd);
925 if (conn->write_fd != -1) {
926 close(conn->write_fd);
931 session = conn->sessions;
932 if (talloc_array_length(conn->pending) == 0) {
934 * if we do not have pending requests
935 * there is no need to update the channel_sequence
939 for (; session; session = session->next) {
940 smb2cli_session_increment_channel_sequence(session);
944 * Cancel all pending requests. We do not do a for-loop walking
945 * conn->pending because that array changes in
946 * smbXcli_req_unset_pending.
948 while (talloc_array_length(conn->pending) > 0) {
949 struct tevent_req *req;
950 struct smbXcli_req_state *state;
951 struct tevent_req **chain;
955 req = conn->pending[0];
956 state = tevent_req_data(req, struct smbXcli_req_state);
958 if (state->smb1.chained_requests == NULL) {
960 * We're dead. No point waiting for trans2
965 smbXcli_req_unset_pending(req);
967 if (NT_STATUS_IS_OK(status)) {
968 /* do not notify the callers */
973 * we need to defer the callback, because we may notify
974 * more then one caller.
976 tevent_req_defer_callback(req, state->ev);
977 tevent_req_nterror(req, status);
981 chain = talloc_move(conn, &state->smb1.chained_requests);
982 num_chained = talloc_array_length(chain);
984 for (i=0; i<num_chained; i++) {
986 state = tevent_req_data(req, struct smbXcli_req_state);
989 * We're dead. No point waiting for trans2
994 smbXcli_req_unset_pending(req);
996 if (NT_STATUS_IS_OK(status)) {
997 /* do not notify the callers */
1002 * we need to defer the callback, because we may notify
1003 * more than one caller.
1005 tevent_req_defer_callback(req, state->ev);
1006 tevent_req_nterror(req, status);
1013 * Fetch a smb request's mid. Only valid after the request has been sent by
1014 * smb1cli_req_send().
1016 uint16_t smb1cli_req_mid(struct tevent_req *req)
1018 struct smbXcli_req_state *state =
1019 tevent_req_data(req,
1020 struct smbXcli_req_state);
1022 if (state->smb1.mid != 0) {
1023 return state->smb1.mid;
1026 return SVAL(state->smb1.hdr, HDR_MID);
1029 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1031 struct smbXcli_req_state *state =
1032 tevent_req_data(req,
1033 struct smbXcli_req_state);
1035 state->smb1.mid = mid;
1038 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1040 struct smbXcli_req_state *state =
1041 tevent_req_data(req,
1042 struct smbXcli_req_state);
1044 return state->smb1.seqnum;
1047 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1049 struct smbXcli_req_state *state =
1050 tevent_req_data(req,
1051 struct smbXcli_req_state);
1053 state->smb1.seqnum = seqnum;
1056 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1060 for (i=0; i<count; i++) {
1061 result += iov[i].iov_len;
1066 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1067 const struct iovec *iov,
1070 size_t len = smbXcli_iov_len(iov, count);
1075 buf = talloc_array(mem_ctx, uint8_t, len);
1080 for (i=0; i<count; i++) {
1081 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1082 copied += iov[i].iov_len;
1087 static void smb1cli_req_flags(enum protocol_types protocol,
1088 uint32_t smb1_capabilities,
1089 uint8_t smb_command,
1090 uint8_t additional_flags,
1091 uint8_t clear_flags,
1093 uint16_t additional_flags2,
1094 uint16_t clear_flags2,
1098 uint16_t flags2 = 0;
1100 if (protocol >= PROTOCOL_LANMAN1) {
1101 flags |= FLAG_CASELESS_PATHNAMES;
1102 flags |= FLAG_CANONICAL_PATHNAMES;
1105 if (protocol >= PROTOCOL_LANMAN2) {
1106 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1107 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1110 if (protocol >= PROTOCOL_NT1) {
1111 flags2 |= FLAGS2_IS_LONG_NAME;
1113 if (smb1_capabilities & CAP_UNICODE) {
1114 flags2 |= FLAGS2_UNICODE_STRINGS;
1116 if (smb1_capabilities & CAP_STATUS32) {
1117 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1119 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1120 flags2 |= FLAGS2_EXTENDED_SECURITY;
1124 flags |= additional_flags;
1125 flags &= ~clear_flags;
1126 flags2 |= additional_flags2;
1127 flags2 &= ~clear_flags2;
1133 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1135 static bool smb1cli_req_cancel(struct tevent_req *req)
1137 struct smbXcli_req_state *state =
1138 tevent_req_data(req,
1139 struct smbXcli_req_state);
1144 struct tevent_req *subreq;
1147 flags = CVAL(state->smb1.hdr, HDR_FLG);
1148 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1149 pid = SVAL(state->smb1.hdr, HDR_PID);
1150 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1151 mid = SVAL(state->smb1.hdr, HDR_MID);
1153 subreq = smb1cli_req_create(state, state->ev,
1163 0, NULL); /* bytes */
1164 if (subreq == NULL) {
1167 smb1cli_req_set_mid(subreq, mid);
1169 status = smb1cli_req_chain_submit(&subreq, 1);
1170 if (!NT_STATUS_IS_OK(status)) {
1171 TALLOC_FREE(subreq);
1174 smb1cli_req_set_mid(subreq, 0);
1176 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1181 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1183 /* we do not care about the result */
1184 TALLOC_FREE(subreq);
1187 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1188 struct tevent_context *ev,
1189 struct smbXcli_conn *conn,
1190 uint8_t smb_command,
1191 uint8_t additional_flags,
1192 uint8_t clear_flags,
1193 uint16_t additional_flags2,
1194 uint16_t clear_flags2,
1195 uint32_t timeout_msec,
1197 struct smbXcli_tcon *tcon,
1198 struct smbXcli_session *session,
1199 uint8_t wct, uint16_t *vwv,
1201 struct iovec *bytes_iov)
1203 struct tevent_req *req;
1204 struct smbXcli_req_state *state;
1206 uint16_t flags2 = 0;
1210 if (iov_count > MAX_SMB_IOV) {
1212 * Should not happen :-)
1217 req = tevent_req_create(mem_ctx, &state,
1218 struct smbXcli_req_state);
1224 state->session = session;
1228 uid = session->smb1.session_id;
1232 tid = tcon->smb1.tcon_id;
1235 state->smb1.recv_cmd = 0xFF;
1236 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1237 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1238 if (state->smb1.recv_iov == NULL) {
1243 smb1cli_req_flags(conn->protocol,
1244 conn->smb1.capabilities,
1253 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1254 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1255 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1256 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1257 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1258 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1259 SSVAL(state->smb1.hdr, HDR_TID, tid);
1260 SSVAL(state->smb1.hdr, HDR_PID, pid);
1261 SSVAL(state->smb1.hdr, HDR_UID, uid);
1262 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1263 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1265 state->smb1.vwv = vwv;
1267 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1269 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1270 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1271 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1272 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1273 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1274 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1275 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1276 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1278 if (iov_count != 0) {
1279 memcpy(&state->smb1.iov[4], bytes_iov,
1280 iov_count * sizeof(*bytes_iov));
1282 state->smb1.iov_count = iov_count + 4;
1284 if (timeout_msec > 0) {
1285 struct timeval endtime;
1287 endtime = timeval_current_ofs_msec(timeout_msec);
1288 if (!tevent_req_set_endtime(req, ev, endtime)) {
1293 switch (smb_command) {
1297 state->one_way = true;
1300 state->one_way = true;
1301 state->smb1.one_way_seqnum = true;
1305 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1306 state->one_way = true;
1314 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1315 struct iovec *iov, int iov_count,
1317 bool one_way_seqnum)
1319 TALLOC_CTX *frame = NULL;
1323 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1324 * iovec directly. MD5Update would do that just fine.
1327 if (iov_count < 4) {
1328 return NT_STATUS_INVALID_PARAMETER_MIX;
1330 if (iov[0].iov_len != NBT_HDR_SIZE) {
1331 return NT_STATUS_INVALID_PARAMETER_MIX;
1333 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1334 return NT_STATUS_INVALID_PARAMETER_MIX;
1336 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1337 return NT_STATUS_INVALID_PARAMETER_MIX;
1339 if (iov[3].iov_len != sizeof(uint16_t)) {
1340 return NT_STATUS_INVALID_PARAMETER_MIX;
1343 frame = talloc_stackframe();
1345 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1347 return NT_STATUS_NO_MEMORY;
1350 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1352 smb_signing_sign_pdu(conn->smb1.signing,
1353 buf, talloc_get_size(buf),
1355 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1358 return NT_STATUS_OK;
1361 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1362 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1363 TALLOC_CTX *tmp_mem,
1366 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1367 struct smbXcli_req_state *state,
1368 struct iovec *iov, int iov_count)
1370 struct tevent_req *subreq;
1375 if (!smbXcli_conn_is_connected(state->conn)) {
1376 return NT_STATUS_CONNECTION_DISCONNECTED;
1379 if (state->conn->protocol > PROTOCOL_NT1) {
1380 return NT_STATUS_REVISION_MISMATCH;
1383 if (iov_count < 4) {
1384 return NT_STATUS_INVALID_PARAMETER_MIX;
1386 if (iov[0].iov_len != NBT_HDR_SIZE) {
1387 return NT_STATUS_INVALID_PARAMETER_MIX;
1389 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1390 return NT_STATUS_INVALID_PARAMETER_MIX;
1392 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1393 return NT_STATUS_INVALID_PARAMETER_MIX;
1395 if (iov[3].iov_len != sizeof(uint16_t)) {
1396 return NT_STATUS_INVALID_PARAMETER_MIX;
1399 cmd = CVAL(iov[1].iov_base, HDR_COM);
1400 if (cmd == SMBreadBraw) {
1401 if (smbXcli_conn_has_async_calls(state->conn)) {
1402 return NT_STATUS_INVALID_PARAMETER_MIX;
1404 state->conn->smb1.read_braw_req = req;
1407 if (state->smb1.mid != 0) {
1408 mid = state->smb1.mid;
1410 mid = smb1cli_alloc_mid(state->conn);
1412 SSVAL(iov[1].iov_base, HDR_MID, mid);
1414 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1416 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1417 &state->smb1.seqnum,
1418 state->smb1.one_way_seqnum);
1420 if (!NT_STATUS_IS_OK(status)) {
1425 * If we supported multiple encrytion contexts
1426 * here we'd look up based on tid.
1428 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1429 char *buf, *enc_buf;
1431 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1433 return NT_STATUS_NO_MEMORY;
1435 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1436 (char *)buf, &enc_buf);
1438 if (!NT_STATUS_IS_OK(status)) {
1439 DEBUG(0, ("Error in encrypting client message: %s\n",
1440 nt_errstr(status)));
1443 buf = (char *)talloc_memdup(state, enc_buf,
1444 smb_len_nbt(enc_buf)+4);
1447 return NT_STATUS_NO_MEMORY;
1449 iov[0].iov_base = (void *)buf;
1450 iov[0].iov_len = talloc_get_size(buf);
1454 if (state->conn->dispatch_incoming == NULL) {
1455 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1458 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1460 subreq = writev_send(state, state->ev, state->conn->outgoing,
1461 state->conn->write_fd, false, iov, iov_count);
1462 if (subreq == NULL) {
1463 return NT_STATUS_NO_MEMORY;
1465 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1466 return NT_STATUS_OK;
1469 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1470 struct tevent_context *ev,
1471 struct smbXcli_conn *conn,
1472 uint8_t smb_command,
1473 uint8_t additional_flags,
1474 uint8_t clear_flags,
1475 uint16_t additional_flags2,
1476 uint16_t clear_flags2,
1477 uint32_t timeout_msec,
1479 struct smbXcli_tcon *tcon,
1480 struct smbXcli_session *session,
1481 uint8_t wct, uint16_t *vwv,
1483 const uint8_t *bytes)
1485 struct tevent_req *req;
1489 iov.iov_base = discard_const_p(void, bytes);
1490 iov.iov_len = num_bytes;
1492 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1493 additional_flags, clear_flags,
1494 additional_flags2, clear_flags2,
1501 if (!tevent_req_is_in_progress(req)) {
1502 return tevent_req_post(req, ev);
1504 status = smb1cli_req_chain_submit(&req, 1);
1505 if (tevent_req_nterror(req, status)) {
1506 return tevent_req_post(req, ev);
1511 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1513 struct tevent_req *req =
1514 tevent_req_callback_data(subreq,
1516 struct smbXcli_req_state *state =
1517 tevent_req_data(req,
1518 struct smbXcli_req_state);
1522 nwritten = writev_recv(subreq, &err);
1523 TALLOC_FREE(subreq);
1524 if (nwritten == -1) {
1525 NTSTATUS status = map_nt_error_from_unix_common(err);
1526 smbXcli_conn_disconnect(state->conn, status);
1530 if (state->one_way) {
1531 state->inbuf = NULL;
1532 tevent_req_done(req);
1536 if (!smbXcli_req_set_pending(req)) {
1537 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1542 static void smbXcli_conn_received(struct tevent_req *subreq)
1544 struct smbXcli_conn *conn =
1545 tevent_req_callback_data(subreq,
1546 struct smbXcli_conn);
1547 TALLOC_CTX *frame = talloc_stackframe();
1553 if (subreq != conn->read_smb_req) {
1554 DEBUG(1, ("Internal error: cli_smb_received called with "
1555 "unexpected subreq\n"));
1556 status = NT_STATUS_INTERNAL_ERROR;
1557 smbXcli_conn_disconnect(conn, status);
1561 conn->read_smb_req = NULL;
1563 received = read_smb_recv(subreq, frame, &inbuf, &err);
1564 TALLOC_FREE(subreq);
1565 if (received == -1) {
1566 status = map_nt_error_from_unix_common(err);
1567 smbXcli_conn_disconnect(conn, status);
1572 status = conn->dispatch_incoming(conn, frame, inbuf);
1574 if (NT_STATUS_IS_OK(status)) {
1576 * We should not do any more processing
1577 * as the dispatch function called
1578 * tevent_req_done().
1581 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1583 * We got an error, so notify all pending requests
1585 smbXcli_conn_disconnect(conn, status);
1590 * We got NT_STATUS_RETRY, so we may ask for a
1591 * next incoming pdu.
1593 if (!smbXcli_conn_receive_next(conn)) {
1594 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1598 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1599 struct iovec **piov, int *pnum_iov)
1610 buflen = smb_len_nbt(buf);
1613 hdr = buf + NBT_HDR_SIZE;
1615 if (buflen < MIN_SMB_SIZE) {
1616 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1620 * This returns iovec elements in the following order:
1635 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1637 return NT_STATUS_NO_MEMORY;
1639 iov[0].iov_base = hdr;
1640 iov[0].iov_len = HDR_WCT;
1643 cmd = CVAL(hdr, HDR_COM);
1647 size_t len = buflen - taken;
1649 struct iovec *iov_tmp;
1656 * we need at least WCT and BCC
1658 needed = sizeof(uint8_t) + sizeof(uint16_t);
1660 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1661 __location__, (int)len, (int)needed));
1666 * Now we check if the specified words are there
1668 wct = CVAL(hdr, wct_ofs);
1669 needed += wct * sizeof(uint16_t);
1671 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1672 __location__, (int)len, (int)needed));
1677 * Now we check if the specified bytes are there
1679 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1680 bcc = SVAL(hdr, bcc_ofs);
1681 needed += bcc * sizeof(uint8_t);
1683 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1684 __location__, (int)len, (int)needed));
1689 * we allocate 2 iovec structures for words and bytes
1691 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1693 if (iov_tmp == NULL) {
1695 return NT_STATUS_NO_MEMORY;
1698 cur = &iov[num_iov];
1701 cur[0].iov_len = wct * sizeof(uint16_t);
1702 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1703 cur[1].iov_len = bcc * sizeof(uint8_t);
1704 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1708 if (!smb1cli_is_andx_req(cmd)) {
1710 * If the current command does not have AndX chanining
1716 if (wct == 0 && bcc == 0) {
1718 * An empty response also ends the chain,
1719 * most likely with an error.
1725 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1726 __location__, (int)wct, (int)cmd));
1729 cmd = CVAL(cur[0].iov_base, 0);
1732 * If it is the end of the chain we are also done.
1736 wct_ofs = SVAL(cur[0].iov_base, 2);
1738 if (wct_ofs < taken) {
1739 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1741 if (wct_ofs > buflen) {
1742 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1746 * we consumed everything up to the start of the next
1752 remaining = buflen - taken;
1754 if (remaining > 0 && num_iov >= 3) {
1756 * The last DATA block gets the remaining
1757 * bytes, this is needed to support
1758 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1760 iov[num_iov-1].iov_len += remaining;
1764 *pnum_iov = num_iov;
1765 return NT_STATUS_OK;
1769 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1772 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1773 TALLOC_CTX *tmp_mem,
1776 struct tevent_req *req;
1777 struct smbXcli_req_state *state;
1784 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1785 size_t len = smb_len_nbt(inbuf);
1786 struct iovec *iov = NULL;
1788 struct tevent_req **chain = NULL;
1789 size_t num_chained = 0;
1790 size_t num_responses = 0;
1792 if (conn->smb1.read_braw_req != NULL) {
1793 req = conn->smb1.read_braw_req;
1794 conn->smb1.read_braw_req = NULL;
1795 state = tevent_req_data(req, struct smbXcli_req_state);
1797 smbXcli_req_unset_pending(req);
1799 if (state->smb1.recv_iov == NULL) {
1801 * For requests with more than
1802 * one response, we have to readd the
1805 state->smb1.recv_iov = talloc_zero_array(state,
1808 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1809 return NT_STATUS_OK;
1813 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1814 state->smb1.recv_iov[0].iov_len = len;
1815 ZERO_STRUCT(state->smb1.recv_iov[1]);
1816 ZERO_STRUCT(state->smb1.recv_iov[2]);
1818 state->smb1.recv_cmd = SMBreadBraw;
1819 state->smb1.recv_status = NT_STATUS_OK;
1820 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1822 tevent_req_done(req);
1823 return NT_STATUS_OK;
1826 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1827 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1828 DEBUG(10, ("Got non-SMB PDU\n"));
1829 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1833 * If we supported multiple encrytion contexts
1834 * here we'd look up based on tid.
1836 if (common_encryption_on(conn->smb1.trans_enc)
1837 && (CVAL(inbuf, 0) == 0)) {
1838 uint16_t enc_ctx_num;
1840 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1841 if (!NT_STATUS_IS_OK(status)) {
1842 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1843 nt_errstr(status)));
1847 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1848 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1850 conn->smb1.trans_enc->enc_ctx_num));
1851 return NT_STATUS_INVALID_HANDLE;
1854 status = common_decrypt_buffer(conn->smb1.trans_enc,
1856 if (!NT_STATUS_IS_OK(status)) {
1857 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1858 nt_errstr(status)));
1861 inhdr = inbuf + NBT_HDR_SIZE;
1862 len = smb_len_nbt(inbuf);
1865 mid = SVAL(inhdr, HDR_MID);
1866 num_pending = talloc_array_length(conn->pending);
1868 for (i=0; i<num_pending; i++) {
1869 if (mid == smb1cli_req_mid(conn->pending[i])) {
1873 if (i == num_pending) {
1874 /* Dump unexpected reply */
1875 return NT_STATUS_RETRY;
1878 oplock_break = false;
1880 if (mid == 0xffff) {
1882 * Paranoia checks that this is really an oplock break request.
1884 oplock_break = (len == 51); /* hdr + 8 words */
1885 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1886 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1887 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1888 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1890 if (!oplock_break) {
1891 /* Dump unexpected reply */
1892 return NT_STATUS_RETRY;
1896 req = conn->pending[i];
1897 state = tevent_req_data(req, struct smbXcli_req_state);
1899 if (!oplock_break /* oplock breaks are not signed */
1900 && !smb_signing_check_pdu(conn->smb1.signing,
1901 inhdr, len, state->smb1.seqnum+1)) {
1902 DEBUG(10, ("cli_check_sign_mac failed\n"));
1903 return NT_STATUS_ACCESS_DENIED;
1906 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1908 if (!NT_STATUS_IS_OK(status)) {
1909 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1910 nt_errstr(status)));
1914 cmd = CVAL(inhdr, HDR_COM);
1915 status = smb1cli_pull_raw_error(inhdr);
1917 if (state->smb1.chained_requests == NULL) {
1919 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1922 smbXcli_req_unset_pending(req);
1924 if (state->smb1.recv_iov == NULL) {
1926 * For requests with more than
1927 * one response, we have to readd the
1930 state->smb1.recv_iov = talloc_zero_array(state,
1933 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1934 return NT_STATUS_OK;
1938 state->smb1.recv_cmd = cmd;
1939 state->smb1.recv_status = status;
1940 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1942 state->smb1.recv_iov[0] = iov[0];
1943 state->smb1.recv_iov[1] = iov[1];
1944 state->smb1.recv_iov[2] = iov[2];
1946 if (talloc_array_length(conn->pending) == 0) {
1947 tevent_req_done(req);
1948 return NT_STATUS_OK;
1951 tevent_req_defer_callback(req, state->ev);
1952 tevent_req_done(req);
1953 return NT_STATUS_RETRY;
1956 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1957 num_chained = talloc_array_length(chain);
1958 num_responses = (num_iov - 1)/2;
1960 if (num_responses > num_chained) {
1961 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1964 for (i=0; i<num_chained; i++) {
1965 size_t iov_idx = 1 + (i*2);
1966 struct iovec *cur = &iov[iov_idx];
1970 state = tevent_req_data(req, struct smbXcli_req_state);
1972 smbXcli_req_unset_pending(req);
1975 * as we finish multiple requests here
1976 * we need to defer the callbacks as
1977 * they could destroy our current stack state.
1979 tevent_req_defer_callback(req, state->ev);
1981 if (i >= num_responses) {
1982 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1986 if (state->smb1.recv_iov == NULL) {
1988 * For requests with more than
1989 * one response, we have to readd the
1992 state->smb1.recv_iov = talloc_zero_array(state,
1995 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2000 state->smb1.recv_cmd = cmd;
2002 if (i == (num_responses - 1)) {
2004 * The last request in the chain gets the status
2006 state->smb1.recv_status = status;
2008 cmd = CVAL(cur[0].iov_base, 0);
2009 state->smb1.recv_status = NT_STATUS_OK;
2012 state->inbuf = inbuf;
2015 * Note: here we use talloc_reference() in a way
2016 * that does not expose it to the caller.
2018 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2019 if (tevent_req_nomem(inbuf_ref, req)) {
2023 /* copy the related buffers */
2024 state->smb1.recv_iov[0] = iov[0];
2025 state->smb1.recv_iov[1] = cur[0];
2026 state->smb1.recv_iov[2] = cur[1];
2028 tevent_req_done(req);
2031 return NT_STATUS_RETRY;
2034 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2035 TALLOC_CTX *mem_ctx,
2036 struct iovec **piov,
2040 uint32_t *pvwv_offset,
2041 uint32_t *pnum_bytes,
2043 uint32_t *pbytes_offset,
2045 const struct smb1cli_req_expected_response *expected,
2046 size_t num_expected)
2048 struct smbXcli_req_state *state =
2049 tevent_req_data(req,
2050 struct smbXcli_req_state);
2051 NTSTATUS status = NT_STATUS_OK;
2052 struct iovec *recv_iov = NULL;
2053 uint8_t *hdr = NULL;
2055 uint32_t vwv_offset = 0;
2056 uint16_t *vwv = NULL;
2057 uint32_t num_bytes = 0;
2058 uint32_t bytes_offset = 0;
2059 uint8_t *bytes = NULL;
2061 bool found_status = false;
2062 bool found_size = false;
2076 if (pvwv_offset != NULL) {
2079 if (pnum_bytes != NULL) {
2082 if (pbytes != NULL) {
2085 if (pbytes_offset != NULL) {
2088 if (pinbuf != NULL) {
2092 if (state->inbuf != NULL) {
2093 recv_iov = state->smb1.recv_iov;
2094 state->smb1.recv_iov = NULL;
2095 if (state->smb1.recv_cmd != SMBreadBraw) {
2096 hdr = (uint8_t *)recv_iov[0].iov_base;
2097 wct = recv_iov[1].iov_len/2;
2098 vwv = (uint16_t *)recv_iov[1].iov_base;
2099 vwv_offset = PTR_DIFF(vwv, hdr);
2100 num_bytes = recv_iov[2].iov_len;
2101 bytes = (uint8_t *)recv_iov[2].iov_base;
2102 bytes_offset = PTR_DIFF(bytes, hdr);
2106 if (tevent_req_is_nterror(req, &status)) {
2107 for (i=0; i < num_expected; i++) {
2108 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2109 found_status = true;
2115 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2121 if (num_expected == 0) {
2122 found_status = true;
2126 status = state->smb1.recv_status;
2128 for (i=0; i < num_expected; i++) {
2129 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2133 found_status = true;
2134 if (expected[i].wct == 0) {
2139 if (expected[i].wct == wct) {
2145 if (!found_status) {
2150 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2154 *piov = talloc_move(mem_ctx, &recv_iov);
2166 if (pvwv_offset != NULL) {
2167 *pvwv_offset = vwv_offset;
2169 if (pnum_bytes != NULL) {
2170 *pnum_bytes = num_bytes;
2172 if (pbytes != NULL) {
2175 if (pbytes_offset != NULL) {
2176 *pbytes_offset = bytes_offset;
2178 if (pinbuf != NULL) {
2179 *pinbuf = state->inbuf;
2185 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2192 for (i=0; i<num_reqs; i++) {
2193 struct smbXcli_req_state *state;
2194 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2195 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2196 state->smb1.iov_count-2);
2197 wct_ofs = (wct_ofs + 3) & ~3;
2202 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2204 struct smbXcli_req_state *first_state =
2205 tevent_req_data(reqs[0],
2206 struct smbXcli_req_state);
2207 struct smbXcli_req_state *state;
2209 size_t chain_padding = 0;
2211 struct iovec *iov = NULL;
2212 struct iovec *this_iov;
2216 if (num_reqs == 1) {
2217 return smb1cli_req_writev_submit(reqs[0], first_state,
2218 first_state->smb1.iov,
2219 first_state->smb1.iov_count);
2223 for (i=0; i<num_reqs; i++) {
2224 if (!tevent_req_is_in_progress(reqs[i])) {
2225 return NT_STATUS_INTERNAL_ERROR;
2228 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2230 if (state->smb1.iov_count < 4) {
2231 return NT_STATUS_INVALID_PARAMETER_MIX;
2236 * The NBT and SMB header
2249 iovlen += state->smb1.iov_count - 2;
2252 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2254 return NT_STATUS_NO_MEMORY;
2257 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2258 first_state, reqs, sizeof(*reqs) * num_reqs);
2259 if (first_state->smb1.chained_requests == NULL) {
2261 return NT_STATUS_NO_MEMORY;
2264 wct_offset = HDR_WCT;
2267 for (i=0; i<num_reqs; i++) {
2268 size_t next_padding = 0;
2271 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2273 if (i < num_reqs-1) {
2274 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2275 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2277 TALLOC_FREE(first_state->smb1.chained_requests);
2278 return NT_STATUS_INVALID_PARAMETER_MIX;
2282 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2283 state->smb1.iov_count-2) + 1;
2284 if ((wct_offset % 4) != 0) {
2285 next_padding = 4 - (wct_offset % 4);
2287 wct_offset += next_padding;
2288 vwv = state->smb1.vwv;
2290 if (i < num_reqs-1) {
2291 struct smbXcli_req_state *next_state =
2292 tevent_req_data(reqs[i+1],
2293 struct smbXcli_req_state);
2294 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2296 SSVAL(vwv+1, 0, wct_offset);
2297 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2298 /* properly end the chain */
2299 SCVAL(vwv+0, 0, 0xff);
2300 SCVAL(vwv+0, 1, 0xff);
2306 * The NBT and SMB header
2308 this_iov[0] = state->smb1.iov[0];
2309 this_iov[1] = state->smb1.iov[1];
2313 * This one is a bit subtle. We have to add
2314 * chain_padding bytes between the requests, and we
2315 * have to also include the wct field of the
2316 * subsequent requests. We use the subsequent header
2317 * for the padding, it contains the wct field in its
2320 this_iov[0].iov_len = chain_padding+1;
2321 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2322 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2323 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2328 * copy the words and bytes
2330 memcpy(this_iov, state->smb1.iov+2,
2331 sizeof(struct iovec) * (state->smb1.iov_count-2));
2332 this_iov += state->smb1.iov_count - 2;
2333 chain_padding = next_padding;
2336 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2337 if (nbt_len > first_state->conn->smb1.max_xmit) {
2339 TALLOC_FREE(first_state->smb1.chained_requests);
2340 return NT_STATUS_INVALID_PARAMETER_MIX;
2343 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2344 if (!NT_STATUS_IS_OK(status)) {
2346 TALLOC_FREE(first_state->smb1.chained_requests);
2350 return NT_STATUS_OK;
2353 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2355 return ((tevent_queue_length(conn->outgoing) != 0)
2356 || (talloc_array_length(conn->pending) != 0));
2359 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2361 return conn->smb2.server.capabilities;
2364 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2366 return conn->smb2.server.security_mode;
2369 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2371 return conn->smb2.server.max_trans_size;
2374 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2376 return conn->smb2.server.max_read_size;
2379 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2381 return conn->smb2.server.max_write_size;
2384 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2385 uint16_t max_credits)
2387 conn->smb2.max_credits = max_credits;
2390 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2392 static bool smb2cli_req_cancel(struct tevent_req *req)
2394 struct smbXcli_req_state *state =
2395 tevent_req_data(req,
2396 struct smbXcli_req_state);
2397 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2398 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2399 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2400 struct smbXcli_tcon *tcon = state->tcon;
2401 struct smbXcli_session *session = state->session;
2402 uint8_t *fixed = state->smb2.pad;
2403 uint16_t fixed_len = 4;
2404 struct tevent_req *subreq;
2405 struct smbXcli_req_state *substate;
2408 SSVAL(fixed, 0, 0x04);
2411 subreq = smb2cli_req_create(state, state->ev,
2419 if (subreq == NULL) {
2422 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2424 if (flags & SMB2_HDR_FLAG_ASYNC) {
2428 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2429 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2430 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2432 status = smb2cli_req_compound_submit(&subreq, 1);
2433 if (!NT_STATUS_IS_OK(status)) {
2434 TALLOC_FREE(subreq);
2438 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2443 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2445 /* we do not care about the result */
2446 TALLOC_FREE(subreq);
2449 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2450 struct tevent_context *ev,
2451 struct smbXcli_conn *conn,
2453 uint32_t additional_flags,
2454 uint32_t clear_flags,
2455 uint32_t timeout_msec,
2456 struct smbXcli_tcon *tcon,
2457 struct smbXcli_session *session,
2458 const uint8_t *fixed,
2463 struct tevent_req *req;
2464 struct smbXcli_req_state *state;
2468 bool use_channel_sequence = false;
2469 uint16_t channel_sequence = 0;
2471 req = tevent_req_create(mem_ctx, &state,
2472 struct smbXcli_req_state);
2479 state->session = session;
2482 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2483 use_channel_sequence = true;
2484 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2485 use_channel_sequence = true;
2489 uid = session->smb2->session_id;
2491 if (use_channel_sequence) {
2492 channel_sequence = session->smb2->channel_sequence;
2495 state->smb2.should_sign = session->smb2->should_sign;
2496 state->smb2.should_encrypt = session->smb2->should_encrypt;
2498 if (cmd == SMB2_OP_SESSSETUP &&
2499 session->smb2->signing_key.length != 0) {
2500 state->smb2.should_sign = true;
2503 if (cmd == SMB2_OP_SESSSETUP &&
2504 session->smb2_channel.signing_key.length == 0) {
2505 state->smb2.should_encrypt = false;
2510 tid = tcon->smb2.tcon_id;
2512 if (tcon->smb2.should_encrypt) {
2513 state->smb2.should_encrypt = true;
2517 if (state->smb2.should_encrypt) {
2518 state->smb2.should_sign = false;
2521 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2522 if (state->smb2.recv_iov == NULL) {
2527 flags |= additional_flags;
2528 flags &= ~clear_flags;
2530 state->smb2.fixed = fixed;
2531 state->smb2.fixed_len = fixed_len;
2532 state->smb2.dyn = dyn;
2533 state->smb2.dyn_len = dyn_len;
2535 if (state->smb2.should_encrypt) {
2536 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2537 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2540 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2541 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2542 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2543 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2544 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2545 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2546 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2547 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2550 case SMB2_OP_CANCEL:
2551 state->one_way = true;
2555 * If this is a dummy request, it will have
2556 * UINT64_MAX as message id.
2557 * If we send on break acknowledgement,
2558 * this gets overwritten later.
2560 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2564 if (timeout_msec > 0) {
2565 struct timeval endtime;
2567 endtime = timeval_current_ofs_msec(timeout_msec);
2568 if (!tevent_req_set_endtime(req, ev, endtime)) {
2576 void smb2cli_req_set_notify_async(struct tevent_req *req)
2578 struct smbXcli_req_state *state =
2579 tevent_req_data(req,
2580 struct smbXcli_req_state);
2582 state->smb2.notify_async = true;
2585 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2586 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2587 TALLOC_CTX *tmp_mem,
2590 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2593 struct smbXcli_req_state *state;
2594 struct tevent_req *subreq;
2596 int i, num_iov, nbt_len;
2599 * 1 for the nbt length
2600 * per request: TRANSFORM, HDR, fixed, dyn, padding
2601 * -1 because the last one does not need padding
2604 iov = talloc_array(reqs[0], struct iovec, 1 + 5*num_reqs - 1);
2606 return NT_STATUS_NO_MEMORY;
2612 for (i=0; i<num_reqs; i++) {
2622 const DATA_BLOB *signing_key = NULL;
2623 const DATA_BLOB *encryption_key = NULL;
2625 if (!tevent_req_is_in_progress(reqs[i])) {
2626 return NT_STATUS_INTERNAL_ERROR;
2629 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2631 if (!smbXcli_conn_is_connected(state->conn)) {
2632 return NT_STATUS_CONNECTION_DISCONNECTED;
2635 if ((state->conn->protocol != PROTOCOL_NONE) &&
2636 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2637 return NT_STATUS_REVISION_MISMATCH;
2640 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2641 if (opcode == SMB2_OP_CANCEL) {
2645 avail = UINT64_MAX - state->conn->smb2.mid;
2647 return NT_STATUS_CONNECTION_ABORTED;
2650 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2651 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2656 charge = MAX(state->smb2.credit_charge, charge);
2658 avail = MIN(avail, state->conn->smb2.cur_credits);
2659 if (avail < charge) {
2660 return NT_STATUS_INTERNAL_ERROR;
2664 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2665 credits = state->conn->smb2.max_credits -
2666 state->conn->smb2.cur_credits;
2668 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2672 mid = state->conn->smb2.mid;
2673 state->conn->smb2.mid += charge;
2674 state->conn->smb2.cur_credits -= charge;
2676 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2677 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2679 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2680 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2683 if (state->session) {
2685 * We prefer the channel signing key if it is
2688 if (state->smb2.should_sign) {
2689 signing_key = &state->session->smb2_channel.signing_key;
2693 * If it is a channel binding, we already have the main
2694 * signing key and try that one.
2696 if (signing_key && signing_key->length == 0) {
2697 signing_key = &state->session->smb2->signing_key;
2701 * If we do not have any session key yet, we skip the
2702 * signing of SMB2_OP_SESSSETUP requests.
2704 if (signing_key && signing_key->length == 0) {
2708 if (state->smb2.should_encrypt) {
2709 encryption_key = &state->session->smb2->encryption_key;
2713 if (encryption_key) {
2715 iov[num_iov].iov_base = state->smb2.transform;
2716 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2721 iov[num_iov].iov_base = state->smb2.hdr;
2722 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2725 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2726 iov[num_iov].iov_len = state->smb2.fixed_len;
2729 if (state->smb2.dyn != NULL) {
2730 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2731 iov[num_iov].iov_len = state->smb2.dyn_len;
2735 reqlen = sizeof(state->smb2.hdr);
2736 reqlen += state->smb2.fixed_len;
2737 reqlen += state->smb2.dyn_len;
2739 if (i < num_reqs-1) {
2740 if ((reqlen % 8) > 0) {
2741 uint8_t pad = 8 - (reqlen % 8);
2742 iov[num_iov].iov_base = state->smb2.pad;
2743 iov[num_iov].iov_len = pad;
2747 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2750 if (encryption_key) {
2755 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2756 state->session->smb2->nonce_low);
2757 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2758 state->session->smb2->nonce_high);
2760 state->session->smb2->nonce_low += 1;
2761 if (state->session->smb2->nonce_low == 0) {
2762 state->session->smb2->nonce_high += 1;
2763 state->session->smb2->nonce_low += 1;
2766 SBVAL(state->smb2.transform, SMB2_TF_MSG_SIZE,
2769 buf = talloc_array(iov, uint8_t, reqlen);
2771 return NT_STATUS_NO_MEMORY;
2774 reqlen += SMB2_TF_HDR_SIZE;
2777 * We copy the buffers before encrypting them,
2778 * this is at least currently needed for the
2779 * to keep state->smb2.hdr.
2781 * Also the callers may expect there buffers
2784 for (vi = hdr_iov; vi < num_iov; vi++) {
2785 struct iovec *v = &iov[vi];
2786 const uint8_t *o = (const uint8_t *)v->iov_base;
2788 memcpy(buf, o, v->iov_len);
2789 v->iov_base = (void *)buf;
2793 status = smb2_signing_encrypt_pdu(*encryption_key,
2794 state->session->conn->protocol,
2795 &iov[tf_iov], num_iov - tf_iov);
2796 if (!NT_STATUS_IS_OK(status)) {
2799 } else if (signing_key) {
2802 status = smb2_signing_sign_pdu(*signing_key,
2803 state->session->conn->protocol,
2804 &iov[hdr_iov], num_iov - hdr_iov);
2805 if (!NT_STATUS_IS_OK(status)) {
2812 ret = smbXcli_req_set_pending(reqs[i]);
2814 return NT_STATUS_NO_MEMORY;
2818 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2819 _smb_setlen_tcp(state->length_hdr, nbt_len);
2820 iov[0].iov_base = state->length_hdr;
2821 iov[0].iov_len = sizeof(state->length_hdr);
2823 if (state->conn->dispatch_incoming == NULL) {
2824 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2827 subreq = writev_send(state, state->ev, state->conn->outgoing,
2828 state->conn->write_fd, false, iov, num_iov);
2829 if (subreq == NULL) {
2830 return NT_STATUS_NO_MEMORY;
2832 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2833 return NT_STATUS_OK;
2836 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2838 struct smbXcli_req_state *state =
2839 tevent_req_data(req,
2840 struct smbXcli_req_state);
2842 state->smb2.credit_charge = charge;
2845 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2846 struct tevent_context *ev,
2847 struct smbXcli_conn *conn,
2849 uint32_t additional_flags,
2850 uint32_t clear_flags,
2851 uint32_t timeout_msec,
2852 struct smbXcli_tcon *tcon,
2853 struct smbXcli_session *session,
2854 const uint8_t *fixed,
2859 struct tevent_req *req;
2862 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2863 additional_flags, clear_flags,
2866 fixed, fixed_len, dyn, dyn_len);
2870 if (!tevent_req_is_in_progress(req)) {
2871 return tevent_req_post(req, ev);
2873 status = smb2cli_req_compound_submit(&req, 1);
2874 if (tevent_req_nterror(req, status)) {
2875 return tevent_req_post(req, ev);
2880 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2882 struct tevent_req *req =
2883 tevent_req_callback_data(subreq,
2885 struct smbXcli_req_state *state =
2886 tevent_req_data(req,
2887 struct smbXcli_req_state);
2891 nwritten = writev_recv(subreq, &err);
2892 TALLOC_FREE(subreq);
2893 if (nwritten == -1) {
2894 /* here, we need to notify all pending requests */
2895 NTSTATUS status = map_nt_error_from_unix_common(err);
2896 smbXcli_conn_disconnect(state->conn, status);
2901 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
2904 TALLOC_CTX *mem_ctx,
2905 struct iovec **piov, int *pnum_iov)
2910 uint8_t *first_hdr = buf;
2912 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2914 return NT_STATUS_NO_MEMORY;
2917 while (taken < buflen) {
2920 size_t len = buflen - taken;
2921 uint8_t *hdr = first_hdr + taken;
2924 size_t next_command_ofs;
2926 struct iovec *iov_tmp;
2929 DEBUG(10, ("%d bytes left, expected at least %d\n",
2933 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
2934 struct smbXcli_session *s;
2936 struct iovec tf_iov[2];
2939 if (len < SMB2_TF_HDR_SIZE) {
2940 DEBUG(10, ("%d bytes left, expected at least %d\n",
2941 (int)len, SMB2_TF_HDR_SIZE));
2945 tf_len = SMB2_TF_HDR_SIZE;
2948 hdr = first_hdr + taken;
2949 len = IVAL(tf, SMB2_TF_MSG_SIZE);
2950 uid = BVAL(tf, SMB2_TF_SESSION_ID);
2953 for (; s; s = s->next) {
2954 if (s->smb2->session_id != uid) {
2961 DEBUG(10, ("unknown session_id %llu\n",
2962 (unsigned long long)uid));
2966 tf_iov[0].iov_base = (void *)tf;
2967 tf_iov[0].iov_len = tf_len;
2968 tf_iov[1].iov_base = (void *)hdr;
2969 tf_iov[1].iov_len = len;
2971 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
2974 if (!NT_STATUS_IS_OK(status)) {
2981 * We need the header plus the body length field
2984 if (len < SMB2_HDR_BODY + 2) {
2985 DEBUG(10, ("%d bytes left, expected at least %d\n",
2986 (int)len, SMB2_HDR_BODY));
2989 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2990 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2994 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2995 DEBUG(10, ("Got HDR len %d, expected %d\n",
2996 SVAL(hdr, 4), SMB2_HDR_BODY));
3001 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3002 body_size = SVAL(hdr, SMB2_HDR_BODY);
3004 if (next_command_ofs != 0) {
3005 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3008 if (next_command_ofs > full_size) {
3011 if (tf && next_command_ofs < len) {
3014 full_size = next_command_ofs;
3016 if (body_size < 2) {
3019 body_size &= 0xfffe;
3021 if (body_size > (full_size - SMB2_HDR_BODY)) {
3025 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3027 if (iov_tmp == NULL) {
3029 return NT_STATUS_NO_MEMORY;
3032 cur = &iov[num_iov];
3035 cur[0].iov_base = tf;
3036 cur[0].iov_len = tf_len;
3037 cur[1].iov_base = hdr;
3038 cur[1].iov_len = SMB2_HDR_BODY;
3039 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3040 cur[2].iov_len = body_size;
3041 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3042 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3048 *pnum_iov = num_iov;
3049 return NT_STATUS_OK;
3053 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3056 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3059 size_t num_pending = talloc_array_length(conn->pending);
3062 for (i=0; i<num_pending; i++) {
3063 struct tevent_req *req = conn->pending[i];
3064 struct smbXcli_req_state *state =
3065 tevent_req_data(req,
3066 struct smbXcli_req_state);
3068 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3075 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3076 TALLOC_CTX *tmp_mem,
3079 struct tevent_req *req;
3080 struct smbXcli_req_state *state = NULL;
3085 struct smbXcli_session *last_session = NULL;
3086 size_t inbuf_len = smb_len_tcp(inbuf);
3088 status = smb2cli_inbuf_parse_compound(conn,
3089 inbuf + NBT_HDR_SIZE,
3093 if (!NT_STATUS_IS_OK(status)) {
3097 for (i=0; i<num_iov; i+=4) {
3098 uint8_t *inbuf_ref = NULL;
3099 struct iovec *cur = &iov[i];
3100 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3101 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3102 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3103 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3104 uint16_t req_opcode;
3106 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3107 uint32_t new_credits;
3108 struct smbXcli_session *session = NULL;
3109 const DATA_BLOB *signing_key = NULL;
3111 new_credits = conn->smb2.cur_credits;
3112 new_credits += credits;
3113 if (new_credits > UINT16_MAX) {
3114 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3116 conn->smb2.cur_credits += credits;
3118 req = smb2cli_conn_find_pending(conn, mid);
3120 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3122 state = tevent_req_data(req, struct smbXcli_req_state);
3124 state->smb2.got_async = false;
3126 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3127 if (opcode != req_opcode) {
3128 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3130 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3132 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3133 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3136 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3137 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3138 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3139 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3142 * async interim responses are not signed,
3143 * even if the SMB2_HDR_FLAG_SIGNED flag
3146 req_flags |= SMB2_HDR_FLAG_ASYNC;
3147 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3148 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3150 if (state->smb2.notify_async) {
3151 state->smb2.got_async = true;
3152 tevent_req_defer_callback(req, state->ev);
3153 tevent_req_notify_callback(req);
3158 session = state->session;
3159 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3160 session = last_session;
3162 last_session = session;
3164 if (state->smb2.should_sign) {
3165 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3166 return NT_STATUS_ACCESS_DENIED;
3170 if (flags & SMB2_HDR_FLAG_SIGNED) {
3171 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3173 if (session == NULL) {
3174 struct smbXcli_session *s;
3176 s = state->conn->sessions;
3177 for (; s; s = s->next) {
3178 if (s->smb2->session_id != uid) {
3187 if (session == NULL) {
3188 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3191 last_session = session;
3192 signing_key = &session->smb2_channel.signing_key;
3195 if (opcode == SMB2_OP_SESSSETUP) {
3197 * We prefer the channel signing key, if it is
3200 * If we do not have a channel signing key yet,
3201 * we try the main signing key, if it is not
3202 * the final response.
3204 if (signing_key && signing_key->length == 0 &&
3205 !NT_STATUS_IS_OK(status)) {
3206 signing_key = &session->smb2->signing_key;
3209 if (signing_key && signing_key->length == 0) {
3211 * If we do not have a session key to
3212 * verify the signature, we defer the
3213 * signing check to the caller.
3215 * The caller gets NT_STATUS_OK, it
3217 * smb2cli_session_set_session_key()
3219 * smb2cli_session_set_channel_key()
3220 * which will check the signature
3221 * with the channel signing key.
3227 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3229 * if the server returns NT_STATUS_USER_SESSION_DELETED
3230 * the response is not signed and we should
3231 * propagate the NT_STATUS_USER_SESSION_DELETED
3232 * status to the caller.
3235 } else if (state->smb2.should_encrypt) {
3236 if (cur[0].iov_len != SMB2_TF_HDR_SIZE) {
3237 return NT_STATUS_ACCESS_DENIED;
3241 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3242 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3243 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3245 * if the server returns
3246 * NT_STATUS_NETWORK_NAME_DELETED
3247 * NT_STATUS_FILE_CLOSED
3248 * NT_STATUS_INVALID_PARAMETER
3249 * the response might not be signed
3250 * as this happens before the signing checks.
3252 * If server echos the signature (or all zeros)
3253 * we should report the status from the server
3259 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3260 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3263 state->smb2.signing_skipped = true;
3269 static const uint8_t zeros[16];
3271 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3275 state->smb2.signing_skipped = true;
3282 status = smb2_signing_check_pdu(*signing_key,
3283 state->conn->protocol,
3285 if (!NT_STATUS_IS_OK(status)) {
3287 * If the signing check fails, we disconnect
3294 smbXcli_req_unset_pending(req);
3297 * There might be more than one response
3298 * we need to defer the notifications
3300 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3305 tevent_req_defer_callback(req, state->ev);
3309 * Note: here we use talloc_reference() in a way
3310 * that does not expose it to the caller.
3312 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3313 if (tevent_req_nomem(inbuf_ref, req)) {
3317 /* copy the related buffers */
3318 state->smb2.recv_iov[0] = cur[1];
3319 state->smb2.recv_iov[1] = cur[2];
3320 state->smb2.recv_iov[2] = cur[3];
3322 tevent_req_done(req);
3326 return NT_STATUS_RETRY;
3329 return NT_STATUS_OK;
3332 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3333 struct iovec **piov,
3334 const struct smb2cli_req_expected_response *expected,
3335 size_t num_expected)
3337 struct smbXcli_req_state *state =
3338 tevent_req_data(req,
3339 struct smbXcli_req_state);
3342 bool found_status = false;
3343 bool found_size = false;
3350 if (state->smb2.got_async) {
3351 return STATUS_PENDING;
3354 if (tevent_req_is_nterror(req, &status)) {
3355 for (i=0; i < num_expected; i++) {
3356 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3357 found_status = true;
3363 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3369 if (num_expected == 0) {
3370 found_status = true;
3374 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3375 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3377 for (i=0; i < num_expected; i++) {
3378 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3382 found_status = true;
3383 if (expected[i].body_size == 0) {
3388 if (expected[i].body_size == body_size) {
3394 if (!found_status) {
3398 if (state->smb2.signing_skipped) {
3399 if (num_expected > 0) {
3400 return NT_STATUS_ACCESS_DENIED;
3402 if (!NT_STATUS_IS_ERR(status)) {
3403 return NT_STATUS_ACCESS_DENIED;
3408 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3412 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3418 static const struct {
3419 enum protocol_types proto;
3420 const char *smb1_name;
3421 } smb1cli_prots[] = {
3422 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3423 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3424 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3425 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3426 {PROTOCOL_LANMAN2, "LM1.2X002"},
3427 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3428 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3429 {PROTOCOL_LANMAN2, "Samba"},
3430 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3431 {PROTOCOL_NT1, "NT LM 0.12"},
3432 {PROTOCOL_SMB2_02, "SMB 2.002"},
3433 {PROTOCOL_SMB2_10, "SMB 2.???"},
3436 static const struct {
3437 enum protocol_types proto;
3438 uint16_t smb2_dialect;
3439 } smb2cli_prots[] = {
3440 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3441 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3442 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3443 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3444 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3447 struct smbXcli_negprot_state {
3448 struct smbXcli_conn *conn;
3449 struct tevent_context *ev;
3450 uint32_t timeout_msec;
3451 enum protocol_types min_protocol;
3452 enum protocol_types max_protocol;
3456 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3460 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3461 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3462 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3463 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3464 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3465 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3469 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3470 struct tevent_context *ev,
3471 struct smbXcli_conn *conn,
3472 uint32_t timeout_msec,
3473 enum protocol_types min_protocol,
3474 enum protocol_types max_protocol)
3476 struct tevent_req *req, *subreq;
3477 struct smbXcli_negprot_state *state;
3479 req = tevent_req_create(mem_ctx, &state,
3480 struct smbXcli_negprot_state);
3486 state->timeout_msec = timeout_msec;
3487 state->min_protocol = min_protocol;
3488 state->max_protocol = max_protocol;
3490 if (min_protocol == PROTOCOL_NONE) {
3491 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3492 return tevent_req_post(req, ev);
3495 if (max_protocol == PROTOCOL_NONE) {
3496 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3497 return tevent_req_post(req, ev);
3500 if (min_protocol > max_protocol) {
3501 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3502 return tevent_req_post(req, ev);
3505 if ((min_protocol < PROTOCOL_SMB2_02) &&
3506 (max_protocol < PROTOCOL_SMB2_02)) {
3510 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3512 subreq = smbXcli_negprot_smb1_subreq(state);
3513 if (tevent_req_nomem(subreq, req)) {
3514 return tevent_req_post(req, ev);
3516 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3520 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3521 (max_protocol >= PROTOCOL_SMB2_02)) {
3525 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3527 subreq = smbXcli_negprot_smb2_subreq(state);
3528 if (tevent_req_nomem(subreq, req)) {
3529 return tevent_req_post(req, ev);
3531 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3536 * We send an SMB1 negprot with the SMB2 dialects
3537 * and expect a SMB1 or a SMB2 response.
3539 * smbXcli_negprot_dispatch_incoming() will fix the
3540 * callback to match protocol of the response.
3542 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3544 subreq = smbXcli_negprot_smb1_subreq(state);
3545 if (tevent_req_nomem(subreq, req)) {
3546 return tevent_req_post(req, ev);
3548 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3552 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3554 struct tevent_req *req =
3555 tevent_req_callback_data(subreq,
3560 * we just want the low level error
3562 status = tevent_req_simple_recv_ntstatus(subreq);
3563 TALLOC_FREE(subreq);
3564 if (tevent_req_nterror(req, status)) {
3568 /* this should never happen */
3569 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3572 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3575 DATA_BLOB bytes = data_blob_null;
3579 /* setup the protocol strings */
3580 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3584 if (smb1cli_prots[i].proto < state->min_protocol) {
3588 if (smb1cli_prots[i].proto > state->max_protocol) {
3592 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3598 * We now it is already ascii and
3599 * we want NULL termination.
3601 ok = data_blob_append(state, &bytes,
3602 smb1cli_prots[i].smb1_name,
3603 strlen(smb1cli_prots[i].smb1_name)+1);
3609 smb1cli_req_flags(state->max_protocol,
3610 state->conn->smb1.client.capabilities,
3615 return smb1cli_req_send(state, state->ev, state->conn,
3619 state->timeout_msec,
3620 0xFFFE, 0, NULL, /* pid, tid, session */
3621 0, NULL, /* wct, vwv */
3622 bytes.length, bytes.data);
3625 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3627 struct tevent_req *req =
3628 tevent_req_callback_data(subreq,
3630 struct smbXcli_negprot_state *state =
3631 tevent_req_data(req,
3632 struct smbXcli_negprot_state);
3633 struct smbXcli_conn *conn = state->conn;
3634 struct iovec *recv_iov = NULL;
3643 size_t num_prots = 0;
3645 uint32_t client_capabilities = conn->smb1.client.capabilities;
3646 uint32_t both_capabilities;
3647 uint32_t server_capabilities = 0;
3648 uint32_t capabilities;
3649 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3650 uint32_t server_max_xmit = 0;
3652 uint32_t server_max_mux = 0;
3653 uint16_t server_security_mode = 0;
3654 uint32_t server_session_key = 0;
3655 bool server_readbraw = false;
3656 bool server_writebraw = false;
3657 bool server_lockread = false;
3658 bool server_writeunlock = false;
3659 struct GUID server_guid = GUID_zero();
3660 DATA_BLOB server_gss_blob = data_blob_null;
3661 uint8_t server_challenge[8];
3662 char *server_workgroup = NULL;
3663 char *server_name = NULL;
3664 int server_time_zone = 0;
3665 NTTIME server_system_time = 0;
3666 static const struct smb1cli_req_expected_response expected[] = {
3668 .status = NT_STATUS_OK,
3669 .wct = 0x11, /* NT1 */
3672 .status = NT_STATUS_OK,
3673 .wct = 0x0D, /* LM */
3676 .status = NT_STATUS_OK,
3677 .wct = 0x01, /* CORE */
3681 ZERO_STRUCT(server_challenge);
3683 status = smb1cli_req_recv(subreq, state,
3688 NULL, /* pvwv_offset */
3691 NULL, /* pbytes_offset */
3693 expected, ARRAY_SIZE(expected));
3694 TALLOC_FREE(subreq);
3695 if (tevent_req_nterror(req, status)) {
3699 flags = CVAL(inhdr, HDR_FLG);
3701 protnum = SVAL(vwv, 0);
3703 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3704 if (smb1cli_prots[i].proto < state->min_protocol) {
3708 if (smb1cli_prots[i].proto > state->max_protocol) {
3712 if (protnum != num_prots) {
3717 conn->protocol = smb1cli_prots[i].proto;
3721 if (conn->protocol == PROTOCOL_NONE) {
3722 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3726 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3727 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3728 "and the selected protocol level doesn't support it.\n"));
3729 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3733 if (flags & FLAG_SUPPORT_LOCKREAD) {
3734 server_lockread = true;
3735 server_writeunlock = true;
3738 if (conn->protocol >= PROTOCOL_NT1) {
3739 const char *client_signing = NULL;
3740 bool server_mandatory = false;
3741 bool server_allowed = false;
3742 const char *server_signing = NULL;
3747 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3752 server_security_mode = CVAL(vwv + 1, 0);
3753 server_max_mux = SVAL(vwv + 1, 1);
3754 server_max_xmit = IVAL(vwv + 3, 1);
3755 server_session_key = IVAL(vwv + 7, 1);
3756 server_time_zone = SVALS(vwv + 15, 1);
3757 server_time_zone *= 60;
3758 /* this time arrives in real GMT */
3759 server_system_time = BVAL(vwv + 11, 1);
3760 server_capabilities = IVAL(vwv + 9, 1);
3762 key_len = CVAL(vwv + 16, 1);
3764 if (server_capabilities & CAP_RAW_MODE) {
3765 server_readbraw = true;
3766 server_writebraw = true;
3768 if (server_capabilities & CAP_LOCK_AND_READ) {
3769 server_lockread = true;
3772 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3773 DATA_BLOB blob1, blob2;
3775 if (num_bytes < 16) {
3776 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3780 blob1 = data_blob_const(bytes, 16);
3781 status = GUID_from_data_blob(&blob1, &server_guid);
3782 if (tevent_req_nterror(req, status)) {
3786 blob1 = data_blob_const(bytes+16, num_bytes-16);
3787 blob2 = data_blob_dup_talloc(state, blob1);
3788 if (blob1.length > 0 &&
3789 tevent_req_nomem(blob2.data, req)) {
3792 server_gss_blob = blob2;
3794 DATA_BLOB blob1, blob2;
3796 if (num_bytes < key_len) {
3797 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3801 if (key_len != 0 && key_len != 8) {
3802 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3807 memcpy(server_challenge, bytes, 8);
3810 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3811 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3812 if (blob1.length > 0) {
3815 len = utf16_len_n(blob1.data,
3819 ok = convert_string_talloc(state,
3827 status = map_nt_error_from_unix_common(errno);
3828 tevent_req_nterror(req, status);
3833 blob2.data += blob1.length;
3834 blob2.length -= blob1.length;
3835 if (blob2.length > 0) {
3838 len = utf16_len_n(blob1.data,
3842 ok = convert_string_talloc(state,
3850 status = map_nt_error_from_unix_common(errno);
3851 tevent_req_nterror(req, status);
3857 client_signing = "disabled";
3858 if (conn->allow_signing) {
3859 client_signing = "allowed";
3861 if (conn->mandatory_signing) {
3862 client_signing = "required";
3865 server_signing = "not supported";
3866 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3867 server_signing = "supported";
3868 server_allowed = true;
3870 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3871 server_signing = "required";
3872 server_mandatory = true;
3875 ok = smb_signing_set_negotiated(conn->smb1.signing,
3879 DEBUG(1,("cli_negprot: SMB signing is required, "
3880 "but client[%s] and server[%s] mismatch\n",
3881 client_signing, server_signing));
3882 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3886 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3892 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3896 server_security_mode = SVAL(vwv + 1, 0);
3897 server_max_xmit = SVAL(vwv + 2, 0);
3898 server_max_mux = SVAL(vwv + 3, 0);
3899 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3900 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3901 server_session_key = IVAL(vwv + 6, 0);
3902 server_time_zone = SVALS(vwv + 10, 0);
3903 server_time_zone *= 60;
3904 /* this time is converted to GMT by make_unix_date */
3905 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3906 unix_to_nt_time(&server_system_time, t);
3907 key_len = SVAL(vwv + 11, 0);
3909 if (num_bytes < key_len) {
3910 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3914 if (key_len != 0 && key_len != 8) {
3915 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3920 memcpy(server_challenge, bytes, 8);
3923 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3924 if (blob1.length > 0) {
3928 len = utf16_len_n(blob1.data,
3932 ok = convert_string_talloc(state,
3940 status = map_nt_error_from_unix_common(errno);
3941 tevent_req_nterror(req, status);
3947 /* the old core protocol */
3948 server_time_zone = get_time_zone(time(NULL));
3949 server_max_xmit = 1024;
3953 if (server_max_xmit < 1024) {
3954 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3958 if (server_max_mux < 1) {
3959 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3964 * Now calculate the negotiated capabilities
3965 * based on the mask for:
3966 * - client only flags
3967 * - flags used in both directions
3968 * - server only flags
3970 both_capabilities = client_capabilities & server_capabilities;
3971 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3972 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3973 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3975 max_xmit = MIN(client_max_xmit, server_max_xmit);
3977 conn->smb1.server.capabilities = server_capabilities;
3978 conn->smb1.capabilities = capabilities;
3980 conn->smb1.server.max_xmit = server_max_xmit;
3981 conn->smb1.max_xmit = max_xmit;
3983 conn->smb1.server.max_mux = server_max_mux;
3985 conn->smb1.server.security_mode = server_security_mode;
3987 conn->smb1.server.readbraw = server_readbraw;
3988 conn->smb1.server.writebraw = server_writebraw;
3989 conn->smb1.server.lockread = server_lockread;
3990 conn->smb1.server.writeunlock = server_writeunlock;
3992 conn->smb1.server.session_key = server_session_key;
3994 talloc_steal(conn, server_gss_blob.data);
3995 conn->smb1.server.gss_blob = server_gss_blob;
3996 conn->smb1.server.guid = server_guid;
3997 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3998 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3999 conn->smb1.server.name = talloc_move(conn, &server_name);
4001 conn->smb1.server.time_zone = server_time_zone;
4002 conn->smb1.server.system_time = server_system_time;
4004 tevent_req_done(req);
4007 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4011 uint16_t dialect_count = 0;
4013 buf = state->smb2.dyn;
4014 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4015 if (smb2cli_prots[i].proto < state->min_protocol) {
4019 if (smb2cli_prots[i].proto > state->max_protocol) {
4023 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4027 buf = state->smb2.fixed;
4029 SSVAL(buf, 2, dialect_count);
4030 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4031 SSVAL(buf, 6, 0); /* Reserved */
4032 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4033 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4035 SIVAL(buf, 8, 0); /* Capabilities */
4037 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4041 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4043 if (!NT_STATUS_IS_OK(status)) {
4046 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4048 memset(buf+12, 0, 16); /* ClientGuid */
4050 SBVAL(buf, 28, 0); /* ClientStartTime */
4052 return smb2cli_req_send(state, state->ev,
4053 state->conn, SMB2_OP_NEGPROT,
4055 state->timeout_msec,
4056 NULL, NULL, /* tcon, session */
4057 state->smb2.fixed, sizeof(state->smb2.fixed),
4058 state->smb2.dyn, dialect_count*2);
4061 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4063 struct tevent_req *req =
4064 tevent_req_callback_data(subreq,
4066 struct smbXcli_negprot_state *state =
4067 tevent_req_data(req,
4068 struct smbXcli_negprot_state);
4069 struct smbXcli_conn *conn = state->conn;
4070 size_t security_offset, security_length;
4076 uint16_t dialect_revision;
4077 static const struct smb2cli_req_expected_response expected[] = {
4079 .status = NT_STATUS_OK,
4084 status = smb2cli_req_recv(subreq, state, &iov,
4085 expected, ARRAY_SIZE(expected));
4086 TALLOC_FREE(subreq);
4087 if (tevent_req_nterror(req, status)) {
4091 body = (uint8_t *)iov[1].iov_base;
4093 dialect_revision = SVAL(body, 4);
4095 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4096 if (smb2cli_prots[i].proto < state->min_protocol) {
4100 if (smb2cli_prots[i].proto > state->max_protocol) {
4104 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4108 conn->protocol = smb2cli_prots[i].proto;
4112 if (conn->protocol == PROTOCOL_NONE) {
4113 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4114 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4118 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4119 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4123 /* make sure we do not loop forever */
4124 state->min_protocol = PROTOCOL_SMB2_02;
4127 * send a SMB2 negprot, in order to negotiate
4130 subreq = smbXcli_negprot_smb2_subreq(state);
4131 if (tevent_req_nomem(subreq, req)) {
4134 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4138 conn->smb2.server.security_mode = SVAL(body, 2);
4140 blob = data_blob_const(body + 8, 16);
4141 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4142 if (tevent_req_nterror(req, status)) {
4146 conn->smb2.server.capabilities = IVAL(body, 24);
4147 conn->smb2.server.max_trans_size= IVAL(body, 28);
4148 conn->smb2.server.max_read_size = IVAL(body, 32);
4149 conn->smb2.server.max_write_size= IVAL(body, 36);
4150 conn->smb2.server.system_time = BVAL(body, 40);
4151 conn->smb2.server.start_time = BVAL(body, 48);
4153 security_offset = SVAL(body, 56);
4154 security_length = SVAL(body, 58);
4156 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4157 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4161 if (security_length > iov[2].iov_len) {
4162 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4166 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4169 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4173 tevent_req_done(req);
4176 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4177 TALLOC_CTX *tmp_mem,
4180 size_t num_pending = talloc_array_length(conn->pending);
4181 struct tevent_req *subreq;
4182 struct smbXcli_req_state *substate;
4183 struct tevent_req *req;
4184 uint32_t protocol_magic;
4185 size_t inbuf_len = smb_len_nbt(inbuf);
4187 if (num_pending != 1) {
4188 return NT_STATUS_INTERNAL_ERROR;
4191 if (inbuf_len < 4) {
4192 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4195 subreq = conn->pending[0];
4196 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4197 req = tevent_req_callback_data(subreq, struct tevent_req);
4199 protocol_magic = IVAL(inbuf, 4);
4201 switch (protocol_magic) {
4203 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4204 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4205 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4208 if (substate->smb2.recv_iov == NULL) {
4210 * For the SMB1 negprot we have move it.
4212 substate->smb2.recv_iov = substate->smb1.recv_iov;
4213 substate->smb1.recv_iov = NULL;
4217 * we got an SMB2 answer, which consumed sequence number 0
4218 * so we need to use 1 as the next one
4221 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4222 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4223 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4226 DEBUG(10, ("Got non-SMB PDU\n"));
4227 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4230 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4232 return tevent_req_simple_recv_ntstatus(req);
4235 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4236 uint32_t timeout_msec,
4237 enum protocol_types min_protocol,
4238 enum protocol_types max_protocol)
4240 TALLOC_CTX *frame = talloc_stackframe();
4241 struct tevent_context *ev;
4242 struct tevent_req *req;
4243 NTSTATUS status = NT_STATUS_NO_MEMORY;
4246 if (smbXcli_conn_has_async_calls(conn)) {
4248 * Can't use sync call while an async call is in flight
4250 status = NT_STATUS_INVALID_PARAMETER_MIX;
4253 ev = tevent_context_init(frame);
4257 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4258 min_protocol, max_protocol);
4262 ok = tevent_req_poll(req, ev);
4264 status = map_nt_error_from_unix_common(errno);
4267 status = smbXcli_negprot_recv(req);
4273 static int smbXcli_session_destructor(struct smbXcli_session *session)
4275 if (session->conn == NULL) {
4279 DLIST_REMOVE(session->conn->sessions, session);
4283 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4284 struct smbXcli_conn *conn)
4286 struct smbXcli_session *session;
4288 session = talloc_zero(mem_ctx, struct smbXcli_session);
4289 if (session == NULL) {
4292 session->smb2 = talloc_zero(session, struct smb2cli_session);
4293 if (session->smb2 == NULL) {
4294 talloc_free(session);
4297 talloc_set_destructor(session, smbXcli_session_destructor);
4299 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4300 session->conn = conn;
4305 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4306 TALLOC_CTX *mem_ctx,
4309 const DATA_BLOB *application_key;
4311 *key = data_blob_null;
4313 if (session->conn == NULL) {
4314 return NT_STATUS_NO_USER_SESSION_KEY;
4317 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4318 application_key = &session->smb2->application_key;
4320 application_key = &session->smb1.application_key;
4323 if (application_key->length == 0) {
4324 return NT_STATUS_NO_USER_SESSION_KEY;
4327 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4328 if (key->data == NULL) {
4329 return NT_STATUS_NO_MEMORY;
4332 return NT_STATUS_OK;
4335 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4337 return session->smb1.session_id;
4340 void smb1cli_session_set_id(struct smbXcli_session *session,
4341 uint16_t session_id)
4343 session->smb1.session_id = session_id;
4346 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4347 const DATA_BLOB _session_key)
4349 struct smbXcli_conn *conn = session->conn;
4350 uint8_t session_key[16];
4353 return NT_STATUS_INVALID_PARAMETER_MIX;
4356 if (session->smb1.application_key.length != 0) {
4358 * TODO: do not allow this...
4360 * return NT_STATUS_INVALID_PARAMETER_MIX;
4362 data_blob_clear_free(&session->smb1.application_key);
4365 if (_session_key.length == 0) {
4366 return NT_STATUS_OK;
4369 ZERO_STRUCT(session_key);
4370 memcpy(session_key, _session_key.data,
4371 MIN(_session_key.length, sizeof(session_key)));
4373 session->smb1.application_key = data_blob_talloc(session,
4375 sizeof(session_key));
4376 ZERO_STRUCT(session_key);
4377 if (session->smb1.application_key.data == NULL) {
4378 return NT_STATUS_NO_MEMORY;
4381 return NT_STATUS_OK;
4384 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4386 struct smbXcli_conn *conn = session->conn;
4387 uint8_t security_mode = 0;
4390 return security_mode;
4393 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4394 if (conn->mandatory_signing) {
4395 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4398 return security_mode;
4401 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4403 return session->smb2->session_id;
4406 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4408 return session->smb2->session_flags;
4411 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4412 uint64_t session_id,
4413 uint16_t session_flags)
4415 session->smb2->session_id = session_id;
4416 session->smb2->session_flags = session_flags;
4419 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4421 session->smb2->channel_sequence += 1;
4424 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4425 const DATA_BLOB _session_key,
4426 const struct iovec *recv_iov)
4428 struct smbXcli_conn *conn = session->conn;
4429 uint16_t no_sign_flags;
4430 uint8_t session_key[16];
4434 return NT_STATUS_INVALID_PARAMETER_MIX;
4437 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4439 if (session->smb2->session_flags & no_sign_flags) {
4440 session->smb2->should_sign = false;
4441 return NT_STATUS_OK;
4444 if (session->smb2->signing_key.length != 0) {
4445 return NT_STATUS_INVALID_PARAMETER_MIX;
4448 ZERO_STRUCT(session_key);
4449 memcpy(session_key, _session_key.data,
4450 MIN(_session_key.length, sizeof(session_key)));
4452 session->smb2->signing_key = data_blob_talloc(session,
4454 sizeof(session_key));
4455 if (session->smb2->signing_key.data == NULL) {
4456 ZERO_STRUCT(session_key);
4457 return NT_STATUS_NO_MEMORY;
4460 if (conn->protocol >= PROTOCOL_SMB2_24) {
4461 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4462 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4464 smb2_key_derivation(session_key, sizeof(session_key),
4465 label.data, label.length,
4466 context.data, context.length,
4467 session->smb2->signing_key.data);
4470 session->smb2->encryption_key = data_blob_dup_talloc(session,
4471 session->smb2->signing_key);
4472 if (session->smb2->encryption_key.data == NULL) {
4473 ZERO_STRUCT(session_key);
4474 return NT_STATUS_NO_MEMORY;
4477 if (conn->protocol >= PROTOCOL_SMB2_24) {
4478 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4479 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4481 smb2_key_derivation(session_key, sizeof(session_key),
4482 label.data, label.length,
4483 context.data, context.length,
4484 session->smb2->encryption_key.data);
4487 session->smb2->decryption_key = data_blob_dup_talloc(session,
4488 session->smb2->signing_key);
4489 if (session->smb2->decryption_key.data == NULL) {
4490 ZERO_STRUCT(session_key);
4491 return NT_STATUS_NO_MEMORY;
4494 if (conn->protocol >= PROTOCOL_SMB2_24) {
4495 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4496 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4498 smb2_key_derivation(session_key, sizeof(session_key),
4499 label.data, label.length,
4500 context.data, context.length,
4501 session->smb2->decryption_key.data);
4504 session->smb2->application_key = data_blob_dup_talloc(session,
4505 session->smb2->signing_key);
4506 if (session->smb2->application_key.data == NULL) {
4507 ZERO_STRUCT(session_key);
4508 return NT_STATUS_NO_MEMORY;
4511 if (conn->protocol >= PROTOCOL_SMB2_24) {
4512 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4513 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4515 smb2_key_derivation(session_key, sizeof(session_key),
4516 label.data, label.length,
4517 context.data, context.length,
4518 session->smb2->application_key.data);
4520 ZERO_STRUCT(session_key);
4522 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4523 session->smb2->signing_key);
4524 if (session->smb2_channel.signing_key.data == NULL) {
4525 return NT_STATUS_NO_MEMORY;
4528 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4529 session->conn->protocol,
4531 if (!NT_STATUS_IS_OK(status)) {
4535 session->smb2->should_sign = false;
4536 session->smb2->should_encrypt = false;
4538 if (conn->desire_signing) {
4539 session->smb2->should_sign = true;
4542 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4543 session->smb2->should_sign = true;
4546 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4547 session->smb2->should_encrypt = true;
4550 if (conn->protocol < PROTOCOL_SMB2_24) {
4551 session->smb2->should_encrypt = false;
4554 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4555 session->smb2->should_encrypt = false;
4558 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4559 sizeof(session->smb2->nonce_high));
4560 session->smb2->nonce_low = 1;
4562 return NT_STATUS_OK;
4565 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4566 struct smbXcli_session *session1,
4567 struct smbXcli_conn *conn,
4568 struct smbXcli_session **_session2)
4570 struct smbXcli_session *session2;
4572 if (session1->smb2->signing_key.length == 0) {
4573 return NT_STATUS_INVALID_PARAMETER_MIX;
4577 return NT_STATUS_INVALID_PARAMETER_MIX;
4580 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4581 if (session2 == NULL) {
4582 return NT_STATUS_NO_MEMORY;
4584 session2->smb2 = talloc_reference(session2, session1->smb2);
4585 if (session2->smb2 == NULL) {
4586 talloc_free(session2);
4587 return NT_STATUS_NO_MEMORY;
4590 talloc_set_destructor(session2, smbXcli_session_destructor);
4591 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4592 session2->conn = conn;
4594 *_session2 = session2;
4595 return NT_STATUS_OK;
4598 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4599 const DATA_BLOB _channel_key,
4600 const struct iovec *recv_iov)
4602 struct smbXcli_conn *conn = session->conn;
4603 uint8_t channel_key[16];
4607 return NT_STATUS_INVALID_PARAMETER_MIX;
4610 if (session->smb2_channel.signing_key.length != 0) {
4611 return NT_STATUS_INVALID_PARAMETER_MIX;
4614 ZERO_STRUCT(channel_key);
4615 memcpy(channel_key, _channel_key.data,
4616 MIN(_channel_key.length, sizeof(channel_key)));
4618 session->smb2_channel.signing_key = data_blob_talloc(session,
4620 sizeof(channel_key));
4621 if (session->smb2_channel.signing_key.data == NULL) {
4622 ZERO_STRUCT(channel_key);
4623 return NT_STATUS_NO_MEMORY;
4626 if (conn->protocol >= PROTOCOL_SMB2_24) {
4627 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4628 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4630 smb2_key_derivation(channel_key, sizeof(channel_key),
4631 label.data, label.length,
4632 context.data, context.length,
4633 session->smb2_channel.signing_key.data);
4635 ZERO_STRUCT(channel_key);
4637 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4638 session->conn->protocol,
4640 if (!NT_STATUS_IS_OK(status)) {
4644 return NT_STATUS_OK;
4647 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4649 struct smbXcli_tcon *tcon;
4651 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4659 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4661 return tcon->smb1.tcon_id;
4664 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4666 tcon->smb1.tcon_id = tcon_id;
4669 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
4671 uint16_t optional_support,
4672 uint32_t maximal_access,
4673 uint32_t guest_maximal_access,
4674 const char *service,
4675 const char *fs_type)
4677 tcon->smb1.tcon_id = tcon_id;
4678 tcon->smb1.optional_support = optional_support;
4679 tcon->smb1.maximal_access = maximal_access;
4680 tcon->smb1.guest_maximal_access = guest_maximal_access;
4682 TALLOC_FREE(tcon->smb1.service);
4683 tcon->smb1.service = talloc_strdup(tcon, service);
4684 if (service != NULL && tcon->smb1.service == NULL) {
4688 TALLOC_FREE(tcon->smb1.fs_type);
4689 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
4690 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
4697 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
4699 return tcon->smb2.tcon_id;
4702 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
4704 return tcon->smb2.capabilities;
4707 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
4708 struct smbXcli_session *session,
4712 uint32_t capabilities,
4713 uint32_t maximal_access)
4715 tcon->smb2.tcon_id = tcon_id;
4716 tcon->smb2.type = type;
4717 tcon->smb2.flags = flags;
4718 tcon->smb2.capabilities = capabilities;
4719 tcon->smb2.maximal_access = maximal_access;
4721 tcon->smb2.should_encrypt = false;
4723 if (session == NULL) {
4727 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
4729 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
4730 tcon->smb2.should_encrypt = true;