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 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
639 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
641 return smb_signing_is_active(conn->smb1.signing);
644 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
645 struct smb_trans_enc_state *es)
647 /* Replace the old state, if any. */
648 if (conn->smb1.trans_enc) {
649 TALLOC_FREE(conn->smb1.trans_enc);
651 conn->smb1.trans_enc = es;
654 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
656 return common_encryption_on(conn->smb1.trans_enc);
660 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
662 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
663 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
665 if (NT_STATUS_IS_OK(status)) {
669 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
673 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
677 * Is the SMB command able to hold an AND_X successor
678 * @param[in] cmd The SMB command in question
679 * @retval Can we add a chained request after "cmd"?
681 bool smb1cli_is_andx_req(uint8_t cmd)
701 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
703 size_t num_pending = talloc_array_length(conn->pending);
709 result = conn->smb1.mid++;
710 if ((result == 0) || (result == 0xffff)) {
714 for (i=0; i<num_pending; i++) {
715 if (result == smb1cli_req_mid(conn->pending[i])) {
720 if (i == num_pending) {
726 void smbXcli_req_unset_pending(struct tevent_req *req)
728 struct smbXcli_req_state *state =
730 struct smbXcli_req_state);
731 struct smbXcli_conn *conn = state->conn;
732 size_t num_pending = talloc_array_length(conn->pending);
735 if (state->smb1.mid != 0) {
737 * This is a [nt]trans[2] request which waits
738 * for more than one reply.
743 talloc_set_destructor(req, NULL);
745 if (num_pending == 1) {
747 * The pending read_smb tevent_req is a child of
748 * conn->pending. So if nothing is pending anymore, we need to
749 * delete the socket read fde.
751 TALLOC_FREE(conn->pending);
752 conn->read_smb_req = NULL;
756 for (i=0; i<num_pending; i++) {
757 if (req == conn->pending[i]) {
761 if (i == num_pending) {
763 * Something's seriously broken. Just returning here is the
764 * right thing nevertheless, the point of this routine is to
765 * remove ourselves from conn->pending.
771 * Remove ourselves from the conn->pending array
773 for (; i < (num_pending - 1); i++) {
774 conn->pending[i] = conn->pending[i+1];
778 * No NULL check here, we're shrinking by sizeof(void *), and
779 * talloc_realloc just adjusts the size for this.
781 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
786 static int smbXcli_req_destructor(struct tevent_req *req)
788 struct smbXcli_req_state *state =
790 struct smbXcli_req_state);
793 * Make sure we really remove it from
794 * the pending array on destruction.
797 smbXcli_req_unset_pending(req);
801 static bool smb1cli_req_cancel(struct tevent_req *req);
802 static bool smb2cli_req_cancel(struct tevent_req *req);
804 static bool smbXcli_req_cancel(struct tevent_req *req)
806 struct smbXcli_req_state *state =
808 struct smbXcli_req_state);
810 if (!smbXcli_conn_is_connected(state->conn)) {
814 if (state->conn->protocol == PROTOCOL_NONE) {
818 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
819 return smb2cli_req_cancel(req);
822 return smb1cli_req_cancel(req);
825 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
827 bool smbXcli_req_set_pending(struct tevent_req *req)
829 struct smbXcli_req_state *state =
831 struct smbXcli_req_state);
832 struct smbXcli_conn *conn;
833 struct tevent_req **pending;
838 if (!smbXcli_conn_is_connected(conn)) {
842 num_pending = talloc_array_length(conn->pending);
844 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
846 if (pending == NULL) {
849 pending[num_pending] = req;
850 conn->pending = pending;
851 talloc_set_destructor(req, smbXcli_req_destructor);
852 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
854 if (!smbXcli_conn_receive_next(conn)) {
856 * the caller should notify the current request
858 * And all other pending requests get notified
859 * by smbXcli_conn_disconnect().
861 smbXcli_req_unset_pending(req);
862 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
869 static void smbXcli_conn_received(struct tevent_req *subreq);
871 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
873 size_t num_pending = talloc_array_length(conn->pending);
874 struct tevent_req *req;
875 struct smbXcli_req_state *state;
877 if (conn->read_smb_req != NULL) {
881 if (num_pending == 0) {
882 if (conn->smb2.mid < UINT64_MAX) {
883 /* no more pending requests, so we are done for now */
888 * If there are no more SMB2 requests possible,
889 * because we are out of message ids,
890 * we need to disconnect.
892 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
896 req = conn->pending[0];
897 state = tevent_req_data(req, struct smbXcli_req_state);
900 * We're the first ones, add the read_smb request that waits for the
901 * answer from the server
903 conn->read_smb_req = read_smb_send(conn->pending,
906 if (conn->read_smb_req == NULL) {
909 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
913 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
915 struct smbXcli_session *session;
917 tevent_queue_stop(conn->outgoing);
919 if (conn->read_fd != -1) {
920 close(conn->read_fd);
922 if (conn->write_fd != -1) {
923 close(conn->write_fd);
928 session = conn->sessions;
929 if (talloc_array_length(conn->pending) == 0) {
931 * if we do not have pending requests
932 * there is no need to update the channel_sequence
936 for (; session; session = session->next) {
937 smb2cli_session_increment_channel_sequence(session);
941 * Cancel all pending requests. We do not do a for-loop walking
942 * conn->pending because that array changes in
943 * smbXcli_req_unset_pending.
945 while (talloc_array_length(conn->pending) > 0) {
946 struct tevent_req *req;
947 struct smbXcli_req_state *state;
948 struct tevent_req **chain;
952 req = conn->pending[0];
953 state = tevent_req_data(req, struct smbXcli_req_state);
955 if (state->smb1.chained_requests == NULL) {
957 * We're dead. No point waiting for trans2
962 smbXcli_req_unset_pending(req);
964 if (NT_STATUS_IS_OK(status)) {
965 /* do not notify the callers */
970 * we need to defer the callback, because we may notify
971 * more then one caller.
973 tevent_req_defer_callback(req, state->ev);
974 tevent_req_nterror(req, status);
978 chain = talloc_move(conn, &state->smb1.chained_requests);
979 num_chained = talloc_array_length(chain);
981 for (i=0; i<num_chained; i++) {
983 state = tevent_req_data(req, struct smbXcli_req_state);
986 * We're dead. No point waiting for trans2
991 smbXcli_req_unset_pending(req);
993 if (NT_STATUS_IS_OK(status)) {
994 /* do not notify the callers */
999 * we need to defer the callback, because we may notify
1000 * more than one caller.
1002 tevent_req_defer_callback(req, state->ev);
1003 tevent_req_nterror(req, status);
1010 * Fetch a smb request's mid. Only valid after the request has been sent by
1011 * smb1cli_req_send().
1013 uint16_t smb1cli_req_mid(struct tevent_req *req)
1015 struct smbXcli_req_state *state =
1016 tevent_req_data(req,
1017 struct smbXcli_req_state);
1019 if (state->smb1.mid != 0) {
1020 return state->smb1.mid;
1023 return SVAL(state->smb1.hdr, HDR_MID);
1026 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1028 struct smbXcli_req_state *state =
1029 tevent_req_data(req,
1030 struct smbXcli_req_state);
1032 state->smb1.mid = mid;
1035 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1037 struct smbXcli_req_state *state =
1038 tevent_req_data(req,
1039 struct smbXcli_req_state);
1041 return state->smb1.seqnum;
1044 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1046 struct smbXcli_req_state *state =
1047 tevent_req_data(req,
1048 struct smbXcli_req_state);
1050 state->smb1.seqnum = seqnum;
1053 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1057 for (i=0; i<count; i++) {
1058 result += iov[i].iov_len;
1063 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1064 const struct iovec *iov,
1067 size_t len = smbXcli_iov_len(iov, count);
1072 buf = talloc_array(mem_ctx, uint8_t, len);
1077 for (i=0; i<count; i++) {
1078 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1079 copied += iov[i].iov_len;
1084 static void smb1cli_req_flags(enum protocol_types protocol,
1085 uint32_t smb1_capabilities,
1086 uint8_t smb_command,
1087 uint8_t additional_flags,
1088 uint8_t clear_flags,
1090 uint16_t additional_flags2,
1091 uint16_t clear_flags2,
1095 uint16_t flags2 = 0;
1097 if (protocol >= PROTOCOL_LANMAN1) {
1098 flags |= FLAG_CASELESS_PATHNAMES;
1099 flags |= FLAG_CANONICAL_PATHNAMES;
1102 if (protocol >= PROTOCOL_LANMAN2) {
1103 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1104 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1107 if (protocol >= PROTOCOL_NT1) {
1108 flags2 |= FLAGS2_IS_LONG_NAME;
1110 if (smb1_capabilities & CAP_UNICODE) {
1111 flags2 |= FLAGS2_UNICODE_STRINGS;
1113 if (smb1_capabilities & CAP_STATUS32) {
1114 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1116 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1117 flags2 |= FLAGS2_EXTENDED_SECURITY;
1121 flags |= additional_flags;
1122 flags &= ~clear_flags;
1123 flags2 |= additional_flags2;
1124 flags2 &= ~clear_flags2;
1130 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1132 static bool smb1cli_req_cancel(struct tevent_req *req)
1134 struct smbXcli_req_state *state =
1135 tevent_req_data(req,
1136 struct smbXcli_req_state);
1141 struct tevent_req *subreq;
1144 flags = CVAL(state->smb1.hdr, HDR_FLG);
1145 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1146 pid = SVAL(state->smb1.hdr, HDR_PID);
1147 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1148 mid = SVAL(state->smb1.hdr, HDR_MID);
1150 subreq = smb1cli_req_create(state, state->ev,
1160 0, NULL); /* bytes */
1161 if (subreq == NULL) {
1164 smb1cli_req_set_mid(subreq, mid);
1166 status = smb1cli_req_chain_submit(&subreq, 1);
1167 if (!NT_STATUS_IS_OK(status)) {
1168 TALLOC_FREE(subreq);
1171 smb1cli_req_set_mid(subreq, 0);
1173 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1178 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1180 /* we do not care about the result */
1181 TALLOC_FREE(subreq);
1184 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1185 struct tevent_context *ev,
1186 struct smbXcli_conn *conn,
1187 uint8_t smb_command,
1188 uint8_t additional_flags,
1189 uint8_t clear_flags,
1190 uint16_t additional_flags2,
1191 uint16_t clear_flags2,
1192 uint32_t timeout_msec,
1194 struct smbXcli_tcon *tcon,
1195 struct smbXcli_session *session,
1196 uint8_t wct, uint16_t *vwv,
1198 struct iovec *bytes_iov)
1200 struct tevent_req *req;
1201 struct smbXcli_req_state *state;
1203 uint16_t flags2 = 0;
1207 if (iov_count > MAX_SMB_IOV) {
1209 * Should not happen :-)
1214 req = tevent_req_create(mem_ctx, &state,
1215 struct smbXcli_req_state);
1221 state->session = session;
1225 uid = session->smb1.session_id;
1229 tid = tcon->smb1.tcon_id;
1232 state->smb1.recv_cmd = 0xFF;
1233 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1234 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1235 if (state->smb1.recv_iov == NULL) {
1240 smb1cli_req_flags(conn->protocol,
1241 conn->smb1.capabilities,
1250 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1251 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1252 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1253 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1254 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1255 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1256 SSVAL(state->smb1.hdr, HDR_TID, tid);
1257 SSVAL(state->smb1.hdr, HDR_PID, pid);
1258 SSVAL(state->smb1.hdr, HDR_UID, uid);
1259 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1260 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1262 state->smb1.vwv = vwv;
1264 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1266 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1267 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1268 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1269 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1270 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1271 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1272 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1273 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1275 if (iov_count != 0) {
1276 memcpy(&state->smb1.iov[4], bytes_iov,
1277 iov_count * sizeof(*bytes_iov));
1279 state->smb1.iov_count = iov_count + 4;
1281 if (timeout_msec > 0) {
1282 struct timeval endtime;
1284 endtime = timeval_current_ofs_msec(timeout_msec);
1285 if (!tevent_req_set_endtime(req, ev, endtime)) {
1290 switch (smb_command) {
1294 state->one_way = true;
1297 state->one_way = true;
1298 state->smb1.one_way_seqnum = true;
1302 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1303 state->one_way = true;
1311 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1312 struct iovec *iov, int iov_count,
1314 bool one_way_seqnum)
1316 TALLOC_CTX *frame = NULL;
1320 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1321 * iovec directly. MD5Update would do that just fine.
1324 if (iov_count < 4) {
1325 return NT_STATUS_INVALID_PARAMETER_MIX;
1327 if (iov[0].iov_len != NBT_HDR_SIZE) {
1328 return NT_STATUS_INVALID_PARAMETER_MIX;
1330 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1331 return NT_STATUS_INVALID_PARAMETER_MIX;
1333 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1334 return NT_STATUS_INVALID_PARAMETER_MIX;
1336 if (iov[3].iov_len != sizeof(uint16_t)) {
1337 return NT_STATUS_INVALID_PARAMETER_MIX;
1340 frame = talloc_stackframe();
1342 buf = smbXcli_iov_concat(frame, iov, iov_count);
1344 return NT_STATUS_NO_MEMORY;
1347 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1349 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1350 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1353 return NT_STATUS_OK;
1356 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1357 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1358 TALLOC_CTX *tmp_mem,
1361 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1362 struct smbXcli_req_state *state,
1363 struct iovec *iov, int iov_count)
1365 struct tevent_req *subreq;
1370 if (!smbXcli_conn_is_connected(state->conn)) {
1371 return NT_STATUS_CONNECTION_DISCONNECTED;
1374 if (state->conn->protocol > PROTOCOL_NT1) {
1375 return NT_STATUS_REVISION_MISMATCH;
1378 if (iov_count < 4) {
1379 return NT_STATUS_INVALID_PARAMETER_MIX;
1381 if (iov[0].iov_len != NBT_HDR_SIZE) {
1382 return NT_STATUS_INVALID_PARAMETER_MIX;
1384 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1385 return NT_STATUS_INVALID_PARAMETER_MIX;
1387 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1388 return NT_STATUS_INVALID_PARAMETER_MIX;
1390 if (iov[3].iov_len != sizeof(uint16_t)) {
1391 return NT_STATUS_INVALID_PARAMETER_MIX;
1394 cmd = CVAL(iov[1].iov_base, HDR_COM);
1395 if (cmd == SMBreadBraw) {
1396 if (smbXcli_conn_has_async_calls(state->conn)) {
1397 return NT_STATUS_INVALID_PARAMETER_MIX;
1399 state->conn->smb1.read_braw_req = req;
1402 if (state->smb1.mid != 0) {
1403 mid = state->smb1.mid;
1405 mid = smb1cli_alloc_mid(state->conn);
1407 SSVAL(iov[1].iov_base, HDR_MID, mid);
1409 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1411 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1412 &state->smb1.seqnum,
1413 state->smb1.one_way_seqnum);
1415 if (!NT_STATUS_IS_OK(status)) {
1420 * If we supported multiple encrytion contexts
1421 * here we'd look up based on tid.
1423 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1424 char *buf, *enc_buf;
1426 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1428 return NT_STATUS_NO_MEMORY;
1430 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1431 (char *)buf, &enc_buf);
1433 if (!NT_STATUS_IS_OK(status)) {
1434 DEBUG(0, ("Error in encrypting client message: %s\n",
1435 nt_errstr(status)));
1438 buf = (char *)talloc_memdup(state, enc_buf,
1439 smb_len_nbt(enc_buf)+4);
1442 return NT_STATUS_NO_MEMORY;
1444 iov[0].iov_base = (void *)buf;
1445 iov[0].iov_len = talloc_get_size(buf);
1449 if (state->conn->dispatch_incoming == NULL) {
1450 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1453 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1455 subreq = writev_send(state, state->ev, state->conn->outgoing,
1456 state->conn->write_fd, false, iov, iov_count);
1457 if (subreq == NULL) {
1458 return NT_STATUS_NO_MEMORY;
1460 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1461 return NT_STATUS_OK;
1464 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1465 struct tevent_context *ev,
1466 struct smbXcli_conn *conn,
1467 uint8_t smb_command,
1468 uint8_t additional_flags,
1469 uint8_t clear_flags,
1470 uint16_t additional_flags2,
1471 uint16_t clear_flags2,
1472 uint32_t timeout_msec,
1474 struct smbXcli_tcon *tcon,
1475 struct smbXcli_session *session,
1476 uint8_t wct, uint16_t *vwv,
1478 const uint8_t *bytes)
1480 struct tevent_req *req;
1484 iov.iov_base = discard_const_p(void, bytes);
1485 iov.iov_len = num_bytes;
1487 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1488 additional_flags, clear_flags,
1489 additional_flags2, clear_flags2,
1496 if (!tevent_req_is_in_progress(req)) {
1497 return tevent_req_post(req, ev);
1499 status = smb1cli_req_chain_submit(&req, 1);
1500 if (tevent_req_nterror(req, status)) {
1501 return tevent_req_post(req, ev);
1506 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1508 struct tevent_req *req =
1509 tevent_req_callback_data(subreq,
1511 struct smbXcli_req_state *state =
1512 tevent_req_data(req,
1513 struct smbXcli_req_state);
1517 nwritten = writev_recv(subreq, &err);
1518 TALLOC_FREE(subreq);
1519 if (nwritten == -1) {
1520 NTSTATUS status = map_nt_error_from_unix_common(err);
1521 smbXcli_conn_disconnect(state->conn, status);
1525 if (state->one_way) {
1526 state->inbuf = NULL;
1527 tevent_req_done(req);
1531 if (!smbXcli_req_set_pending(req)) {
1532 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1537 static void smbXcli_conn_received(struct tevent_req *subreq)
1539 struct smbXcli_conn *conn =
1540 tevent_req_callback_data(subreq,
1541 struct smbXcli_conn);
1542 TALLOC_CTX *frame = talloc_stackframe();
1548 if (subreq != conn->read_smb_req) {
1549 DEBUG(1, ("Internal error: cli_smb_received called with "
1550 "unexpected subreq\n"));
1551 status = NT_STATUS_INTERNAL_ERROR;
1552 smbXcli_conn_disconnect(conn, status);
1556 conn->read_smb_req = NULL;
1558 received = read_smb_recv(subreq, frame, &inbuf, &err);
1559 TALLOC_FREE(subreq);
1560 if (received == -1) {
1561 status = map_nt_error_from_unix_common(err);
1562 smbXcli_conn_disconnect(conn, status);
1567 status = conn->dispatch_incoming(conn, frame, inbuf);
1569 if (NT_STATUS_IS_OK(status)) {
1571 * We should not do any more processing
1572 * as the dispatch function called
1573 * tevent_req_done().
1576 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1578 * We got an error, so notify all pending requests
1580 smbXcli_conn_disconnect(conn, status);
1585 * We got NT_STATUS_RETRY, so we may ask for a
1586 * next incoming pdu.
1588 if (!smbXcli_conn_receive_next(conn)) {
1589 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1593 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1594 struct iovec **piov, int *pnum_iov)
1605 buflen = smb_len_nbt(buf);
1608 hdr = buf + NBT_HDR_SIZE;
1610 if (buflen < MIN_SMB_SIZE) {
1611 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1615 * This returns iovec elements in the following order:
1630 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1632 return NT_STATUS_NO_MEMORY;
1634 iov[0].iov_base = hdr;
1635 iov[0].iov_len = HDR_WCT;
1638 cmd = CVAL(hdr, HDR_COM);
1642 size_t len = buflen - taken;
1644 struct iovec *iov_tmp;
1651 * we need at least WCT and BCC
1653 needed = sizeof(uint8_t) + sizeof(uint16_t);
1655 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1656 __location__, (int)len, (int)needed));
1661 * Now we check if the specified words are there
1663 wct = CVAL(hdr, wct_ofs);
1664 needed += wct * sizeof(uint16_t);
1666 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1667 __location__, (int)len, (int)needed));
1672 * Now we check if the specified bytes are there
1674 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1675 bcc = SVAL(hdr, bcc_ofs);
1676 needed += bcc * sizeof(uint8_t);
1678 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1679 __location__, (int)len, (int)needed));
1684 * we allocate 2 iovec structures for words and bytes
1686 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1688 if (iov_tmp == NULL) {
1690 return NT_STATUS_NO_MEMORY;
1693 cur = &iov[num_iov];
1696 cur[0].iov_len = wct * sizeof(uint16_t);
1697 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1698 cur[1].iov_len = bcc * sizeof(uint8_t);
1699 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1703 if (!smb1cli_is_andx_req(cmd)) {
1705 * If the current command does not have AndX chanining
1711 if (wct == 0 && bcc == 0) {
1713 * An empty response also ends the chain,
1714 * most likely with an error.
1720 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1721 __location__, (int)wct, (int)cmd));
1724 cmd = CVAL(cur[0].iov_base, 0);
1727 * If it is the end of the chain we are also done.
1731 wct_ofs = SVAL(cur[0].iov_base, 2);
1733 if (wct_ofs < taken) {
1734 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1736 if (wct_ofs > buflen) {
1737 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1741 * we consumed everything up to the start of the next
1747 remaining = buflen - taken;
1749 if (remaining > 0 && num_iov >= 3) {
1751 * The last DATA block gets the remaining
1752 * bytes, this is needed to support
1753 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1755 iov[num_iov-1].iov_len += remaining;
1759 *pnum_iov = num_iov;
1760 return NT_STATUS_OK;
1764 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1767 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1768 TALLOC_CTX *tmp_mem,
1771 struct tevent_req *req;
1772 struct smbXcli_req_state *state;
1779 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1780 struct iovec *iov = NULL;
1782 struct tevent_req **chain = NULL;
1783 size_t num_chained = 0;
1784 size_t num_responses = 0;
1786 if (conn->smb1.read_braw_req != NULL) {
1787 req = conn->smb1.read_braw_req;
1788 conn->smb1.read_braw_req = NULL;
1789 state = tevent_req_data(req, struct smbXcli_req_state);
1791 smbXcli_req_unset_pending(req);
1793 if (state->smb1.recv_iov == NULL) {
1795 * For requests with more than
1796 * one response, we have to readd the
1799 state->smb1.recv_iov = talloc_zero_array(state,
1802 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1803 return NT_STATUS_OK;
1807 state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
1808 state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
1809 ZERO_STRUCT(state->smb1.recv_iov[1]);
1810 ZERO_STRUCT(state->smb1.recv_iov[2]);
1812 state->smb1.recv_cmd = SMBreadBraw;
1813 state->smb1.recv_status = NT_STATUS_OK;
1814 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1816 tevent_req_done(req);
1817 return NT_STATUS_OK;
1820 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1821 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1822 DEBUG(10, ("Got non-SMB PDU\n"));
1823 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1827 * If we supported multiple encrytion contexts
1828 * here we'd look up based on tid.
1830 if (common_encryption_on(conn->smb1.trans_enc)
1831 && (CVAL(inbuf, 0) == 0)) {
1832 uint16_t enc_ctx_num;
1834 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1835 if (!NT_STATUS_IS_OK(status)) {
1836 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1837 nt_errstr(status)));
1841 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1842 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1844 conn->smb1.trans_enc->enc_ctx_num));
1845 return NT_STATUS_INVALID_HANDLE;
1848 status = common_decrypt_buffer(conn->smb1.trans_enc,
1850 if (!NT_STATUS_IS_OK(status)) {
1851 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1852 nt_errstr(status)));
1857 mid = SVAL(inhdr, HDR_MID);
1858 num_pending = talloc_array_length(conn->pending);
1860 for (i=0; i<num_pending; i++) {
1861 if (mid == smb1cli_req_mid(conn->pending[i])) {
1865 if (i == num_pending) {
1866 /* Dump unexpected reply */
1867 return NT_STATUS_RETRY;
1870 oplock_break = false;
1872 if (mid == 0xffff) {
1874 * Paranoia checks that this is really an oplock break request.
1876 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1877 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1878 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1879 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1880 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1882 if (!oplock_break) {
1883 /* Dump unexpected reply */
1884 return NT_STATUS_RETRY;
1888 req = conn->pending[i];
1889 state = tevent_req_data(req, struct smbXcli_req_state);
1891 if (!oplock_break /* oplock breaks are not signed */
1892 && !smb_signing_check_pdu(conn->smb1.signing,
1893 inbuf, state->smb1.seqnum+1)) {
1894 DEBUG(10, ("cli_check_sign_mac failed\n"));
1895 return NT_STATUS_ACCESS_DENIED;
1898 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1900 if (!NT_STATUS_IS_OK(status)) {
1901 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1902 nt_errstr(status)));
1906 cmd = CVAL(inhdr, HDR_COM);
1907 status = smb1cli_pull_raw_error(inhdr);
1909 if (state->smb1.chained_requests == NULL) {
1911 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1914 smbXcli_req_unset_pending(req);
1916 if (state->smb1.recv_iov == NULL) {
1918 * For requests with more than
1919 * one response, we have to readd the
1922 state->smb1.recv_iov = talloc_zero_array(state,
1925 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1926 return NT_STATUS_OK;
1930 state->smb1.recv_cmd = cmd;
1931 state->smb1.recv_status = status;
1932 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1934 state->smb1.recv_iov[0] = iov[0];
1935 state->smb1.recv_iov[1] = iov[1];
1936 state->smb1.recv_iov[2] = iov[2];
1938 if (talloc_array_length(conn->pending) == 0) {
1939 tevent_req_done(req);
1940 return NT_STATUS_OK;
1943 tevent_req_defer_callback(req, state->ev);
1944 tevent_req_done(req);
1945 return NT_STATUS_RETRY;
1948 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1949 num_chained = talloc_array_length(chain);
1950 num_responses = (num_iov - 1)/2;
1952 if (num_responses > num_chained) {
1953 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1956 for (i=0; i<num_chained; i++) {
1957 size_t iov_idx = 1 + (i*2);
1958 struct iovec *cur = &iov[iov_idx];
1962 state = tevent_req_data(req, struct smbXcli_req_state);
1964 smbXcli_req_unset_pending(req);
1967 * as we finish multiple requests here
1968 * we need to defer the callbacks as
1969 * they could destroy our current stack state.
1971 tevent_req_defer_callback(req, state->ev);
1973 if (i >= num_responses) {
1974 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1978 if (state->smb1.recv_iov == NULL) {
1980 * For requests with more than
1981 * one response, we have to readd the
1984 state->smb1.recv_iov = talloc_zero_array(state,
1987 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1992 state->smb1.recv_cmd = cmd;
1994 if (i == (num_responses - 1)) {
1996 * The last request in the chain gets the status
1998 state->smb1.recv_status = status;
2000 cmd = CVAL(cur[0].iov_base, 0);
2001 state->smb1.recv_status = NT_STATUS_OK;
2004 state->inbuf = inbuf;
2007 * Note: here we use talloc_reference() in a way
2008 * that does not expose it to the caller.
2010 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2011 if (tevent_req_nomem(inbuf_ref, req)) {
2015 /* copy the related buffers */
2016 state->smb1.recv_iov[0] = iov[0];
2017 state->smb1.recv_iov[1] = cur[0];
2018 state->smb1.recv_iov[2] = cur[1];
2020 tevent_req_done(req);
2023 return NT_STATUS_RETRY;
2026 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2027 TALLOC_CTX *mem_ctx,
2028 struct iovec **piov,
2032 uint32_t *pvwv_offset,
2033 uint32_t *pnum_bytes,
2035 uint32_t *pbytes_offset,
2037 const struct smb1cli_req_expected_response *expected,
2038 size_t num_expected)
2040 struct smbXcli_req_state *state =
2041 tevent_req_data(req,
2042 struct smbXcli_req_state);
2043 NTSTATUS status = NT_STATUS_OK;
2044 struct iovec *recv_iov = NULL;
2045 uint8_t *hdr = NULL;
2047 uint32_t vwv_offset = 0;
2048 uint16_t *vwv = NULL;
2049 uint32_t num_bytes = 0;
2050 uint32_t bytes_offset = 0;
2051 uint8_t *bytes = NULL;
2053 bool found_status = false;
2054 bool found_size = false;
2068 if (pvwv_offset != NULL) {
2071 if (pnum_bytes != NULL) {
2074 if (pbytes != NULL) {
2077 if (pbytes_offset != NULL) {
2080 if (pinbuf != NULL) {
2084 if (state->inbuf != NULL) {
2085 recv_iov = state->smb1.recv_iov;
2086 state->smb1.recv_iov = NULL;
2087 if (state->smb1.recv_cmd != SMBreadBraw) {
2088 hdr = (uint8_t *)recv_iov[0].iov_base;
2089 wct = recv_iov[1].iov_len/2;
2090 vwv = (uint16_t *)recv_iov[1].iov_base;
2091 vwv_offset = PTR_DIFF(vwv, hdr);
2092 num_bytes = recv_iov[2].iov_len;
2093 bytes = (uint8_t *)recv_iov[2].iov_base;
2094 bytes_offset = PTR_DIFF(bytes, hdr);
2098 if (tevent_req_is_nterror(req, &status)) {
2099 for (i=0; i < num_expected; i++) {
2100 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2101 found_status = true;
2107 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2113 if (num_expected == 0) {
2114 found_status = true;
2118 status = state->smb1.recv_status;
2120 for (i=0; i < num_expected; i++) {
2121 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2125 found_status = true;
2126 if (expected[i].wct == 0) {
2131 if (expected[i].wct == wct) {
2137 if (!found_status) {
2142 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2146 *piov = talloc_move(mem_ctx, &recv_iov);
2158 if (pvwv_offset != NULL) {
2159 *pvwv_offset = vwv_offset;
2161 if (pnum_bytes != NULL) {
2162 *pnum_bytes = num_bytes;
2164 if (pbytes != NULL) {
2167 if (pbytes_offset != NULL) {
2168 *pbytes_offset = bytes_offset;
2170 if (pinbuf != NULL) {
2171 *pinbuf = state->inbuf;
2177 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2184 for (i=0; i<num_reqs; i++) {
2185 struct smbXcli_req_state *state;
2186 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2187 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2188 state->smb1.iov_count-2);
2189 wct_ofs = (wct_ofs + 3) & ~3;
2194 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2196 struct smbXcli_req_state *first_state =
2197 tevent_req_data(reqs[0],
2198 struct smbXcli_req_state);
2199 struct smbXcli_req_state *state;
2201 size_t chain_padding = 0;
2203 struct iovec *iov = NULL;
2204 struct iovec *this_iov;
2208 if (num_reqs == 1) {
2209 return smb1cli_req_writev_submit(reqs[0], first_state,
2210 first_state->smb1.iov,
2211 first_state->smb1.iov_count);
2215 for (i=0; i<num_reqs; i++) {
2216 if (!tevent_req_is_in_progress(reqs[i])) {
2217 return NT_STATUS_INTERNAL_ERROR;
2220 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2222 if (state->smb1.iov_count < 4) {
2223 return NT_STATUS_INVALID_PARAMETER_MIX;
2228 * The NBT and SMB header
2241 iovlen += state->smb1.iov_count - 2;
2244 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2246 return NT_STATUS_NO_MEMORY;
2249 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2250 first_state, reqs, sizeof(*reqs) * num_reqs);
2251 if (first_state->smb1.chained_requests == NULL) {
2253 return NT_STATUS_NO_MEMORY;
2256 wct_offset = HDR_WCT;
2259 for (i=0; i<num_reqs; i++) {
2260 size_t next_padding = 0;
2263 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2265 if (i < num_reqs-1) {
2266 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2267 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2269 TALLOC_FREE(first_state->smb1.chained_requests);
2270 return NT_STATUS_INVALID_PARAMETER_MIX;
2274 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2275 state->smb1.iov_count-2) + 1;
2276 if ((wct_offset % 4) != 0) {
2277 next_padding = 4 - (wct_offset % 4);
2279 wct_offset += next_padding;
2280 vwv = state->smb1.vwv;
2282 if (i < num_reqs-1) {
2283 struct smbXcli_req_state *next_state =
2284 tevent_req_data(reqs[i+1],
2285 struct smbXcli_req_state);
2286 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2288 SSVAL(vwv+1, 0, wct_offset);
2289 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2290 /* properly end the chain */
2291 SCVAL(vwv+0, 0, 0xff);
2292 SCVAL(vwv+0, 1, 0xff);
2298 * The NBT and SMB header
2300 this_iov[0] = state->smb1.iov[0];
2301 this_iov[1] = state->smb1.iov[1];
2305 * This one is a bit subtle. We have to add
2306 * chain_padding bytes between the requests, and we
2307 * have to also include the wct field of the
2308 * subsequent requests. We use the subsequent header
2309 * for the padding, it contains the wct field in its
2312 this_iov[0].iov_len = chain_padding+1;
2313 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2314 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2315 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2320 * copy the words and bytes
2322 memcpy(this_iov, state->smb1.iov+2,
2323 sizeof(struct iovec) * (state->smb1.iov_count-2));
2324 this_iov += state->smb1.iov_count - 2;
2325 chain_padding = next_padding;
2328 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2329 if (nbt_len > first_state->conn->smb1.max_xmit) {
2331 TALLOC_FREE(first_state->smb1.chained_requests);
2332 return NT_STATUS_INVALID_PARAMETER_MIX;
2335 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2336 if (!NT_STATUS_IS_OK(status)) {
2338 TALLOC_FREE(first_state->smb1.chained_requests);
2342 return NT_STATUS_OK;
2345 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2347 return ((tevent_queue_length(conn->outgoing) != 0)
2348 || (talloc_array_length(conn->pending) != 0));
2351 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2353 return conn->smb2.server.capabilities;
2356 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2358 return conn->smb2.server.security_mode;
2361 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2363 return conn->smb2.server.max_trans_size;
2366 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2368 return conn->smb2.server.max_read_size;
2371 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2373 return conn->smb2.server.max_write_size;
2376 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2377 uint16_t max_credits)
2379 conn->smb2.max_credits = max_credits;
2382 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2384 static bool smb2cli_req_cancel(struct tevent_req *req)
2386 struct smbXcli_req_state *state =
2387 tevent_req_data(req,
2388 struct smbXcli_req_state);
2389 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2390 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2391 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2392 struct smbXcli_tcon *tcon = state->tcon;
2393 struct smbXcli_session *session = state->session;
2394 uint8_t *fixed = state->smb2.pad;
2395 uint16_t fixed_len = 4;
2396 struct tevent_req *subreq;
2397 struct smbXcli_req_state *substate;
2400 SSVAL(fixed, 0, 0x04);
2403 subreq = smb2cli_req_create(state, state->ev,
2411 if (subreq == NULL) {
2414 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2416 if (flags & SMB2_HDR_FLAG_ASYNC) {
2420 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2421 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2422 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2424 status = smb2cli_req_compound_submit(&subreq, 1);
2425 if (!NT_STATUS_IS_OK(status)) {
2426 TALLOC_FREE(subreq);
2430 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2435 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2437 /* we do not care about the result */
2438 TALLOC_FREE(subreq);
2441 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2442 struct tevent_context *ev,
2443 struct smbXcli_conn *conn,
2445 uint32_t additional_flags,
2446 uint32_t clear_flags,
2447 uint32_t timeout_msec,
2448 struct smbXcli_tcon *tcon,
2449 struct smbXcli_session *session,
2450 const uint8_t *fixed,
2455 struct tevent_req *req;
2456 struct smbXcli_req_state *state;
2460 bool use_channel_sequence = false;
2461 uint16_t channel_sequence = 0;
2463 req = tevent_req_create(mem_ctx, &state,
2464 struct smbXcli_req_state);
2471 state->session = session;
2474 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2475 use_channel_sequence = true;
2476 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2477 use_channel_sequence = true;
2481 uid = session->smb2->session_id;
2483 if (use_channel_sequence) {
2484 channel_sequence = session->smb2->channel_sequence;
2487 state->smb2.should_sign = session->smb2->should_sign;
2488 state->smb2.should_encrypt = session->smb2->should_encrypt;
2490 if (cmd == SMB2_OP_SESSSETUP &&
2491 session->smb2->signing_key.length != 0) {
2492 state->smb2.should_sign = true;
2495 if (cmd == SMB2_OP_SESSSETUP &&
2496 session->smb2_channel.signing_key.length == 0) {
2497 state->smb2.should_encrypt = false;
2502 tid = tcon->smb2.tcon_id;
2504 if (tcon->smb2.should_encrypt) {
2505 state->smb2.should_encrypt = true;
2509 if (state->smb2.should_encrypt) {
2510 state->smb2.should_sign = false;
2513 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2514 if (state->smb2.recv_iov == NULL) {
2519 flags |= additional_flags;
2520 flags &= ~clear_flags;
2522 state->smb2.fixed = fixed;
2523 state->smb2.fixed_len = fixed_len;
2524 state->smb2.dyn = dyn;
2525 state->smb2.dyn_len = dyn_len;
2527 if (state->smb2.should_encrypt) {
2528 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2529 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2532 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2533 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2534 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2535 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2536 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2537 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2538 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2539 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2542 case SMB2_OP_CANCEL:
2543 state->one_way = true;
2547 * If this is a dummy request, it will have
2548 * UINT64_MAX as message id.
2549 * If we send on break acknowledgement,
2550 * this gets overwritten later.
2552 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2556 if (timeout_msec > 0) {
2557 struct timeval endtime;
2559 endtime = timeval_current_ofs_msec(timeout_msec);
2560 if (!tevent_req_set_endtime(req, ev, endtime)) {
2568 void smb2cli_req_set_notify_async(struct tevent_req *req)
2570 struct smbXcli_req_state *state =
2571 tevent_req_data(req,
2572 struct smbXcli_req_state);
2574 state->smb2.notify_async = true;
2577 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2578 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2579 TALLOC_CTX *tmp_mem,
2582 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2585 struct smbXcli_req_state *state;
2586 struct tevent_req *subreq;
2588 int i, num_iov, nbt_len;
2591 * 1 for the nbt length
2592 * per request: TRANSFORM, HDR, fixed, dyn, padding
2593 * -1 because the last one does not need padding
2596 iov = talloc_array(reqs[0], struct iovec, 1 + 5*num_reqs - 1);
2598 return NT_STATUS_NO_MEMORY;
2604 for (i=0; i<num_reqs; i++) {
2614 const DATA_BLOB *signing_key = NULL;
2615 const DATA_BLOB *encryption_key = NULL;
2617 if (!tevent_req_is_in_progress(reqs[i])) {
2618 return NT_STATUS_INTERNAL_ERROR;
2621 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2623 if (!smbXcli_conn_is_connected(state->conn)) {
2624 return NT_STATUS_CONNECTION_DISCONNECTED;
2627 if ((state->conn->protocol != PROTOCOL_NONE) &&
2628 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2629 return NT_STATUS_REVISION_MISMATCH;
2632 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2633 if (opcode == SMB2_OP_CANCEL) {
2637 avail = UINT64_MAX - state->conn->smb2.mid;
2639 return NT_STATUS_CONNECTION_ABORTED;
2642 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2643 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2648 charge = MAX(state->smb2.credit_charge, charge);
2650 avail = MIN(avail, state->conn->smb2.cur_credits);
2651 if (avail < charge) {
2652 return NT_STATUS_INTERNAL_ERROR;
2656 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2657 credits = state->conn->smb2.max_credits -
2658 state->conn->smb2.cur_credits;
2660 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2664 mid = state->conn->smb2.mid;
2665 state->conn->smb2.mid += charge;
2666 state->conn->smb2.cur_credits -= charge;
2668 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2669 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2671 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2672 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2675 if (state->session) {
2677 * We prefer the channel signing key if it is
2680 if (state->smb2.should_sign) {
2681 signing_key = &state->session->smb2_channel.signing_key;
2685 * If it is a channel binding, we already have the main
2686 * signing key and try that one.
2688 if (signing_key && signing_key->length == 0) {
2689 signing_key = &state->session->smb2->signing_key;
2693 * If we do not have any session key yet, we skip the
2694 * signing of SMB2_OP_SESSSETUP requests.
2696 if (signing_key && signing_key->length == 0) {
2700 if (state->smb2.should_encrypt) {
2701 encryption_key = &state->session->smb2->encryption_key;
2705 if (encryption_key) {
2707 iov[num_iov].iov_base = state->smb2.transform;
2708 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2713 iov[num_iov].iov_base = state->smb2.hdr;
2714 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2717 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2718 iov[num_iov].iov_len = state->smb2.fixed_len;
2721 if (state->smb2.dyn != NULL) {
2722 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2723 iov[num_iov].iov_len = state->smb2.dyn_len;
2727 reqlen = sizeof(state->smb2.hdr);
2728 reqlen += state->smb2.fixed_len;
2729 reqlen += state->smb2.dyn_len;
2731 if (i < num_reqs-1) {
2732 if ((reqlen % 8) > 0) {
2733 uint8_t pad = 8 - (reqlen % 8);
2734 iov[num_iov].iov_base = state->smb2.pad;
2735 iov[num_iov].iov_len = pad;
2739 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2742 if (encryption_key) {
2747 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2748 state->session->smb2->nonce_low);
2749 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2750 state->session->smb2->nonce_high);
2752 state->session->smb2->nonce_low += 1;
2753 if (state->session->smb2->nonce_low == 0) {
2754 state->session->smb2->nonce_high += 1;
2755 state->session->smb2->nonce_low += 1;
2758 SBVAL(state->smb2.transform, SMB2_TF_MSG_SIZE,
2761 buf = talloc_array(iov, uint8_t, reqlen);
2763 return NT_STATUS_NO_MEMORY;
2766 reqlen += SMB2_TF_HDR_SIZE;
2769 * We copy the buffers before encrypting them,
2770 * this is at least currently needed for the
2771 * to keep state->smb2.hdr.
2773 * Also the callers may expect there buffers
2776 for (vi = hdr_iov; vi < num_iov; vi++) {
2777 struct iovec *v = &iov[vi];
2778 const uint8_t *o = (const uint8_t *)v->iov_base;
2780 memcpy(buf, o, v->iov_len);
2781 v->iov_base = (void *)buf;
2785 status = smb2_signing_encrypt_pdu(*encryption_key,
2786 state->session->conn->protocol,
2787 &iov[tf_iov], num_iov - tf_iov);
2788 if (!NT_STATUS_IS_OK(status)) {
2791 } else if (signing_key) {
2794 status = smb2_signing_sign_pdu(*signing_key,
2795 state->session->conn->protocol,
2796 &iov[hdr_iov], num_iov - hdr_iov);
2797 if (!NT_STATUS_IS_OK(status)) {
2804 ret = smbXcli_req_set_pending(reqs[i]);
2806 return NT_STATUS_NO_MEMORY;
2810 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2811 _smb_setlen_tcp(state->length_hdr, nbt_len);
2812 iov[0].iov_base = state->length_hdr;
2813 iov[0].iov_len = sizeof(state->length_hdr);
2815 if (state->conn->dispatch_incoming == NULL) {
2816 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2819 subreq = writev_send(state, state->ev, state->conn->outgoing,
2820 state->conn->write_fd, false, iov, num_iov);
2821 if (subreq == NULL) {
2822 return NT_STATUS_NO_MEMORY;
2824 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2825 return NT_STATUS_OK;
2828 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2830 struct smbXcli_req_state *state =
2831 tevent_req_data(req,
2832 struct smbXcli_req_state);
2834 state->smb2.credit_charge = charge;
2837 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2838 struct tevent_context *ev,
2839 struct smbXcli_conn *conn,
2841 uint32_t additional_flags,
2842 uint32_t clear_flags,
2843 uint32_t timeout_msec,
2844 struct smbXcli_tcon *tcon,
2845 struct smbXcli_session *session,
2846 const uint8_t *fixed,
2851 struct tevent_req *req;
2854 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2855 additional_flags, clear_flags,
2858 fixed, fixed_len, dyn, dyn_len);
2862 if (!tevent_req_is_in_progress(req)) {
2863 return tevent_req_post(req, ev);
2865 status = smb2cli_req_compound_submit(&req, 1);
2866 if (tevent_req_nterror(req, status)) {
2867 return tevent_req_post(req, ev);
2872 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2874 struct tevent_req *req =
2875 tevent_req_callback_data(subreq,
2877 struct smbXcli_req_state *state =
2878 tevent_req_data(req,
2879 struct smbXcli_req_state);
2883 nwritten = writev_recv(subreq, &err);
2884 TALLOC_FREE(subreq);
2885 if (nwritten == -1) {
2886 /* here, we need to notify all pending requests */
2887 NTSTATUS status = map_nt_error_from_unix_common(err);
2888 smbXcli_conn_disconnect(state->conn, status);
2893 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
2896 TALLOC_CTX *mem_ctx,
2897 struct iovec **piov, int *pnum_iov)
2902 uint8_t *first_hdr = buf;
2904 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2906 return NT_STATUS_NO_MEMORY;
2909 while (taken < buflen) {
2912 size_t len = buflen - taken;
2913 uint8_t *hdr = first_hdr + taken;
2916 size_t next_command_ofs;
2918 struct iovec *iov_tmp;
2921 DEBUG(10, ("%d bytes left, expected at least %d\n",
2925 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
2926 struct smbXcli_session *s;
2928 struct iovec tf_iov[2];
2931 if (len < SMB2_TF_HDR_SIZE) {
2932 DEBUG(10, ("%d bytes left, expected at least %d\n",
2933 (int)len, SMB2_TF_HDR_SIZE));
2937 tf_len = SMB2_TF_HDR_SIZE;
2940 hdr = first_hdr + taken;
2941 len = IVAL(tf, SMB2_TF_MSG_SIZE);
2942 uid = BVAL(tf, SMB2_TF_SESSION_ID);
2945 for (; s; s = s->next) {
2946 if (s->smb2->session_id != uid) {
2953 DEBUG(10, ("unknown session_id %llu\n",
2954 (unsigned long long)uid));
2958 tf_iov[0].iov_base = (void *)tf;
2959 tf_iov[0].iov_len = tf_len;
2960 tf_iov[1].iov_base = (void *)hdr;
2961 tf_iov[1].iov_len = len;
2963 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
2966 if (!NT_STATUS_IS_OK(status)) {
2973 * We need the header plus the body length field
2976 if (len < SMB2_HDR_BODY + 2) {
2977 DEBUG(10, ("%d bytes left, expected at least %d\n",
2978 (int)len, SMB2_HDR_BODY));
2981 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2982 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2986 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2987 DEBUG(10, ("Got HDR len %d, expected %d\n",
2988 SVAL(hdr, 4), SMB2_HDR_BODY));
2993 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2994 body_size = SVAL(hdr, SMB2_HDR_BODY);
2996 if (next_command_ofs != 0) {
2997 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3000 if (next_command_ofs > full_size) {
3003 if (tf && next_command_ofs < len) {
3006 full_size = next_command_ofs;
3008 if (body_size < 2) {
3011 body_size &= 0xfffe;
3013 if (body_size > (full_size - SMB2_HDR_BODY)) {
3017 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3019 if (iov_tmp == NULL) {
3021 return NT_STATUS_NO_MEMORY;
3024 cur = &iov[num_iov];
3027 cur[0].iov_base = tf;
3028 cur[0].iov_len = tf_len;
3029 cur[1].iov_base = hdr;
3030 cur[1].iov_len = SMB2_HDR_BODY;
3031 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3032 cur[2].iov_len = body_size;
3033 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3034 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3040 *pnum_iov = num_iov;
3041 return NT_STATUS_OK;
3045 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3048 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3051 size_t num_pending = talloc_array_length(conn->pending);
3054 for (i=0; i<num_pending; i++) {
3055 struct tevent_req *req = conn->pending[i];
3056 struct smbXcli_req_state *state =
3057 tevent_req_data(req,
3058 struct smbXcli_req_state);
3060 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3067 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3068 TALLOC_CTX *tmp_mem,
3071 struct tevent_req *req;
3072 struct smbXcli_req_state *state = NULL;
3077 struct smbXcli_session *last_session = NULL;
3078 size_t inbuf_len = smb_len_tcp(inbuf);
3080 status = smb2cli_inbuf_parse_compound(conn,
3081 inbuf + NBT_HDR_SIZE,
3085 if (!NT_STATUS_IS_OK(status)) {
3089 for (i=0; i<num_iov; i+=4) {
3090 uint8_t *inbuf_ref = NULL;
3091 struct iovec *cur = &iov[i];
3092 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3093 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3094 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3095 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3096 uint16_t req_opcode;
3098 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3099 uint32_t new_credits;
3100 struct smbXcli_session *session = NULL;
3101 const DATA_BLOB *signing_key = NULL;
3103 new_credits = conn->smb2.cur_credits;
3104 new_credits += credits;
3105 if (new_credits > UINT16_MAX) {
3106 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3108 conn->smb2.cur_credits += credits;
3110 req = smb2cli_conn_find_pending(conn, mid);
3112 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3114 state = tevent_req_data(req, struct smbXcli_req_state);
3116 state->smb2.got_async = false;
3118 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3119 if (opcode != req_opcode) {
3120 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3122 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3124 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3125 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3128 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3129 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3130 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3131 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3134 * async interim responses are not signed,
3135 * even if the SMB2_HDR_FLAG_SIGNED flag
3138 req_flags |= SMB2_HDR_FLAG_ASYNC;
3139 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3140 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3142 if (state->smb2.notify_async) {
3143 state->smb2.got_async = true;
3144 tevent_req_defer_callback(req, state->ev);
3145 tevent_req_notify_callback(req);
3150 session = state->session;
3151 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3152 session = last_session;
3154 last_session = session;
3156 if (state->smb2.should_sign) {
3157 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3158 return NT_STATUS_ACCESS_DENIED;
3162 if (flags & SMB2_HDR_FLAG_SIGNED) {
3163 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3165 if (session == NULL) {
3166 struct smbXcli_session *s;
3168 s = state->conn->sessions;
3169 for (; s; s = s->next) {
3170 if (s->smb2->session_id != uid) {
3179 if (session == NULL) {
3180 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3183 last_session = session;
3184 signing_key = &session->smb2_channel.signing_key;
3187 if (opcode == SMB2_OP_SESSSETUP) {
3189 * We prefer the channel signing key, if it is
3192 * If we do not have a channel signing key yet,
3193 * we try the main signing key, if it is not
3194 * the final response.
3196 if (signing_key && signing_key->length == 0 &&
3197 !NT_STATUS_IS_OK(status)) {
3198 signing_key = &session->smb2->signing_key;
3201 if (signing_key && signing_key->length == 0) {
3203 * If we do not have a session key to
3204 * verify the signature, we defer the
3205 * signing check to the caller.
3207 * The caller gets NT_STATUS_OK, it
3209 * smb2cli_session_set_session_key()
3211 * smb2cli_session_set_channel_key()
3212 * which will check the signature
3213 * with the channel signing key.
3219 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3221 * if the server returns NT_STATUS_USER_SESSION_DELETED
3222 * the response is not signed and we should
3223 * propagate the NT_STATUS_USER_SESSION_DELETED
3224 * status to the caller.
3227 } else if (state->smb2.should_encrypt) {
3228 if (cur[0].iov_len != SMB2_TF_HDR_SIZE) {
3229 return NT_STATUS_ACCESS_DENIED;
3233 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3234 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3235 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3237 * if the server returns
3238 * NT_STATUS_NETWORK_NAME_DELETED
3239 * NT_STATUS_FILE_CLOSED
3240 * NT_STATUS_INVALID_PARAMETER
3241 * the response might not be signed
3242 * as this happens before the signing checks.
3244 * If server echos the signature (or all zeros)
3245 * we should report the status from the server
3251 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3252 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3255 state->smb2.signing_skipped = true;
3261 static const uint8_t zeros[16];
3263 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3267 state->smb2.signing_skipped = true;
3274 status = smb2_signing_check_pdu(*signing_key,
3275 state->conn->protocol,
3277 if (!NT_STATUS_IS_OK(status)) {
3279 * If the signing check fails, we disconnect
3286 smbXcli_req_unset_pending(req);
3289 * There might be more than one response
3290 * we need to defer the notifications
3292 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3297 tevent_req_defer_callback(req, state->ev);
3301 * Note: here we use talloc_reference() in a way
3302 * that does not expose it to the caller.
3304 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3305 if (tevent_req_nomem(inbuf_ref, req)) {
3309 /* copy the related buffers */
3310 state->smb2.recv_iov[0] = cur[1];
3311 state->smb2.recv_iov[1] = cur[2];
3312 state->smb2.recv_iov[2] = cur[3];
3314 tevent_req_done(req);
3318 return NT_STATUS_RETRY;
3321 return NT_STATUS_OK;
3324 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3325 struct iovec **piov,
3326 const struct smb2cli_req_expected_response *expected,
3327 size_t num_expected)
3329 struct smbXcli_req_state *state =
3330 tevent_req_data(req,
3331 struct smbXcli_req_state);
3334 bool found_status = false;
3335 bool found_size = false;
3342 if (state->smb2.got_async) {
3343 return STATUS_PENDING;
3346 if (tevent_req_is_nterror(req, &status)) {
3347 for (i=0; i < num_expected; i++) {
3348 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3349 found_status = true;
3355 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3361 if (num_expected == 0) {
3362 found_status = true;
3366 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3367 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3369 for (i=0; i < num_expected; i++) {
3370 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3374 found_status = true;
3375 if (expected[i].body_size == 0) {
3380 if (expected[i].body_size == body_size) {
3386 if (!found_status) {
3390 if (state->smb2.signing_skipped) {
3391 if (num_expected > 0) {
3392 return NT_STATUS_ACCESS_DENIED;
3394 if (!NT_STATUS_IS_ERR(status)) {
3395 return NT_STATUS_ACCESS_DENIED;
3400 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3404 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3410 static const struct {
3411 enum protocol_types proto;
3412 const char *smb1_name;
3413 } smb1cli_prots[] = {
3414 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3415 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3416 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3417 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3418 {PROTOCOL_LANMAN2, "LM1.2X002"},
3419 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3420 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3421 {PROTOCOL_LANMAN2, "Samba"},
3422 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3423 {PROTOCOL_NT1, "NT LM 0.12"},
3424 {PROTOCOL_SMB2_02, "SMB 2.002"},
3425 {PROTOCOL_SMB2_10, "SMB 2.???"},
3428 static const struct {
3429 enum protocol_types proto;
3430 uint16_t smb2_dialect;
3431 } smb2cli_prots[] = {
3432 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3433 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3434 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3435 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3436 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3439 struct smbXcli_negprot_state {
3440 struct smbXcli_conn *conn;
3441 struct tevent_context *ev;
3442 uint32_t timeout_msec;
3443 enum protocol_types min_protocol;
3444 enum protocol_types max_protocol;
3448 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3452 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3453 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3454 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3455 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3456 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3457 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3461 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3462 struct tevent_context *ev,
3463 struct smbXcli_conn *conn,
3464 uint32_t timeout_msec,
3465 enum protocol_types min_protocol,
3466 enum protocol_types max_protocol)
3468 struct tevent_req *req, *subreq;
3469 struct smbXcli_negprot_state *state;
3471 req = tevent_req_create(mem_ctx, &state,
3472 struct smbXcli_negprot_state);
3478 state->timeout_msec = timeout_msec;
3479 state->min_protocol = min_protocol;
3480 state->max_protocol = max_protocol;
3482 if (min_protocol == PROTOCOL_NONE) {
3483 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3484 return tevent_req_post(req, ev);
3487 if (max_protocol == PROTOCOL_NONE) {
3488 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3489 return tevent_req_post(req, ev);
3492 if (min_protocol > max_protocol) {
3493 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3494 return tevent_req_post(req, ev);
3497 if ((min_protocol < PROTOCOL_SMB2_02) &&
3498 (max_protocol < PROTOCOL_SMB2_02)) {
3502 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3504 subreq = smbXcli_negprot_smb1_subreq(state);
3505 if (tevent_req_nomem(subreq, req)) {
3506 return tevent_req_post(req, ev);
3508 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3512 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3513 (max_protocol >= PROTOCOL_SMB2_02)) {
3517 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3519 subreq = smbXcli_negprot_smb2_subreq(state);
3520 if (tevent_req_nomem(subreq, req)) {
3521 return tevent_req_post(req, ev);
3523 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3528 * We send an SMB1 negprot with the SMB2 dialects
3529 * and expect a SMB1 or a SMB2 response.
3531 * smbXcli_negprot_dispatch_incoming() will fix the
3532 * callback to match protocol of the response.
3534 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3536 subreq = smbXcli_negprot_smb1_subreq(state);
3537 if (tevent_req_nomem(subreq, req)) {
3538 return tevent_req_post(req, ev);
3540 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3544 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3546 struct tevent_req *req =
3547 tevent_req_callback_data(subreq,
3552 * we just want the low level error
3554 status = tevent_req_simple_recv_ntstatus(subreq);
3555 TALLOC_FREE(subreq);
3556 if (tevent_req_nterror(req, status)) {
3560 /* this should never happen */
3561 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3564 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3567 DATA_BLOB bytes = data_blob_null;
3571 /* setup the protocol strings */
3572 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3576 if (smb1cli_prots[i].proto < state->min_protocol) {
3580 if (smb1cli_prots[i].proto > state->max_protocol) {
3584 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3590 * We now it is already ascii and
3591 * we want NULL termination.
3593 ok = data_blob_append(state, &bytes,
3594 smb1cli_prots[i].smb1_name,
3595 strlen(smb1cli_prots[i].smb1_name)+1);
3601 smb1cli_req_flags(state->max_protocol,
3602 state->conn->smb1.client.capabilities,
3607 return smb1cli_req_send(state, state->ev, state->conn,
3611 state->timeout_msec,
3612 0xFFFE, 0, NULL, /* pid, tid, session */
3613 0, NULL, /* wct, vwv */
3614 bytes.length, bytes.data);
3617 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3619 struct tevent_req *req =
3620 tevent_req_callback_data(subreq,
3622 struct smbXcli_negprot_state *state =
3623 tevent_req_data(req,
3624 struct smbXcli_negprot_state);
3625 struct smbXcli_conn *conn = state->conn;
3626 struct iovec *recv_iov = NULL;
3635 size_t num_prots = 0;
3637 uint32_t client_capabilities = conn->smb1.client.capabilities;
3638 uint32_t both_capabilities;
3639 uint32_t server_capabilities = 0;
3640 uint32_t capabilities;
3641 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3642 uint32_t server_max_xmit = 0;
3644 uint32_t server_max_mux = 0;
3645 uint16_t server_security_mode = 0;
3646 uint32_t server_session_key = 0;
3647 bool server_readbraw = false;
3648 bool server_writebraw = false;
3649 bool server_lockread = false;
3650 bool server_writeunlock = false;
3651 struct GUID server_guid = GUID_zero();
3652 DATA_BLOB server_gss_blob = data_blob_null;
3653 uint8_t server_challenge[8];
3654 char *server_workgroup = NULL;
3655 char *server_name = NULL;
3656 int server_time_zone = 0;
3657 NTTIME server_system_time = 0;
3658 static const struct smb1cli_req_expected_response expected[] = {
3660 .status = NT_STATUS_OK,
3661 .wct = 0x11, /* NT1 */
3664 .status = NT_STATUS_OK,
3665 .wct = 0x0D, /* LM */
3668 .status = NT_STATUS_OK,
3669 .wct = 0x01, /* CORE */
3673 ZERO_STRUCT(server_challenge);
3675 status = smb1cli_req_recv(subreq, state,
3680 NULL, /* pvwv_offset */
3683 NULL, /* pbytes_offset */
3685 expected, ARRAY_SIZE(expected));
3686 TALLOC_FREE(subreq);
3687 if (tevent_req_nterror(req, status)) {
3691 flags = CVAL(inhdr, HDR_FLG);
3693 protnum = SVAL(vwv, 0);
3695 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3696 if (smb1cli_prots[i].proto < state->min_protocol) {
3700 if (smb1cli_prots[i].proto > state->max_protocol) {
3704 if (protnum != num_prots) {
3709 conn->protocol = smb1cli_prots[i].proto;
3713 if (conn->protocol == PROTOCOL_NONE) {
3714 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3718 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3719 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3720 "and the selected protocol level doesn't support it.\n"));
3721 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3725 if (flags & FLAG_SUPPORT_LOCKREAD) {
3726 server_lockread = true;
3727 server_writeunlock = true;
3730 if (conn->protocol >= PROTOCOL_NT1) {
3731 const char *client_signing = NULL;
3732 bool server_mandatory = false;
3733 bool server_allowed = false;
3734 const char *server_signing = NULL;
3739 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3744 server_security_mode = CVAL(vwv + 1, 0);
3745 server_max_mux = SVAL(vwv + 1, 1);
3746 server_max_xmit = IVAL(vwv + 3, 1);
3747 server_session_key = IVAL(vwv + 7, 1);
3748 server_time_zone = SVALS(vwv + 15, 1);
3749 server_time_zone *= 60;
3750 /* this time arrives in real GMT */
3751 server_system_time = BVAL(vwv + 11, 1);
3752 server_capabilities = IVAL(vwv + 9, 1);
3754 key_len = CVAL(vwv + 16, 1);
3756 if (server_capabilities & CAP_RAW_MODE) {
3757 server_readbraw = true;
3758 server_writebraw = true;
3760 if (server_capabilities & CAP_LOCK_AND_READ) {
3761 server_lockread = true;
3764 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3765 DATA_BLOB blob1, blob2;
3767 if (num_bytes < 16) {
3768 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3772 blob1 = data_blob_const(bytes, 16);
3773 status = GUID_from_data_blob(&blob1, &server_guid);
3774 if (tevent_req_nterror(req, status)) {
3778 blob1 = data_blob_const(bytes+16, num_bytes-16);
3779 blob2 = data_blob_dup_talloc(state, blob1);
3780 if (blob1.length > 0 &&
3781 tevent_req_nomem(blob2.data, req)) {
3784 server_gss_blob = blob2;
3786 DATA_BLOB blob1, blob2;
3788 if (num_bytes < key_len) {
3789 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3793 if (key_len != 0 && key_len != 8) {
3794 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3799 memcpy(server_challenge, bytes, 8);
3802 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3803 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3804 if (blob1.length > 0) {
3807 len = utf16_len_n(blob1.data,
3811 ok = convert_string_talloc(state,
3819 status = map_nt_error_from_unix_common(errno);
3820 tevent_req_nterror(req, status);
3825 blob2.data += blob1.length;
3826 blob2.length -= blob1.length;
3827 if (blob2.length > 0) {
3830 len = utf16_len_n(blob1.data,
3834 ok = convert_string_talloc(state,
3842 status = map_nt_error_from_unix_common(errno);
3843 tevent_req_nterror(req, status);
3849 client_signing = "disabled";
3850 if (conn->allow_signing) {
3851 client_signing = "allowed";
3853 if (conn->mandatory_signing) {
3854 client_signing = "required";
3857 server_signing = "not supported";
3858 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3859 server_signing = "supported";
3860 server_allowed = true;
3862 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3863 server_signing = "required";
3864 server_mandatory = true;
3867 ok = smb_signing_set_negotiated(conn->smb1.signing,
3871 DEBUG(1,("cli_negprot: SMB signing is required, "
3872 "but client[%s] and server[%s] mismatch\n",
3873 client_signing, server_signing));
3874 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3878 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3884 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3888 server_security_mode = SVAL(vwv + 1, 0);
3889 server_max_xmit = SVAL(vwv + 2, 0);
3890 server_max_mux = SVAL(vwv + 3, 0);
3891 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3892 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3893 server_session_key = IVAL(vwv + 6, 0);
3894 server_time_zone = SVALS(vwv + 10, 0);
3895 server_time_zone *= 60;
3896 /* this time is converted to GMT by make_unix_date */
3897 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3898 unix_to_nt_time(&server_system_time, t);
3899 key_len = SVAL(vwv + 11, 0);
3901 if (num_bytes < key_len) {
3902 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3906 if (key_len != 0 && key_len != 8) {
3907 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3912 memcpy(server_challenge, bytes, 8);
3915 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3916 if (blob1.length > 0) {
3920 len = utf16_len_n(blob1.data,
3924 ok = convert_string_talloc(state,
3932 status = map_nt_error_from_unix_common(errno);
3933 tevent_req_nterror(req, status);
3939 /* the old core protocol */
3940 server_time_zone = get_time_zone(time(NULL));
3941 server_max_xmit = 1024;
3945 if (server_max_xmit < 1024) {
3946 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3950 if (server_max_mux < 1) {
3951 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3956 * Now calculate the negotiated capabilities
3957 * based on the mask for:
3958 * - client only flags
3959 * - flags used in both directions
3960 * - server only flags
3962 both_capabilities = client_capabilities & server_capabilities;
3963 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3964 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3965 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3967 max_xmit = MIN(client_max_xmit, server_max_xmit);
3969 conn->smb1.server.capabilities = server_capabilities;
3970 conn->smb1.capabilities = capabilities;
3972 conn->smb1.server.max_xmit = server_max_xmit;
3973 conn->smb1.max_xmit = max_xmit;
3975 conn->smb1.server.max_mux = server_max_mux;
3977 conn->smb1.server.security_mode = server_security_mode;
3979 conn->smb1.server.readbraw = server_readbraw;
3980 conn->smb1.server.writebraw = server_writebraw;
3981 conn->smb1.server.lockread = server_lockread;
3982 conn->smb1.server.writeunlock = server_writeunlock;
3984 conn->smb1.server.session_key = server_session_key;
3986 talloc_steal(conn, server_gss_blob.data);
3987 conn->smb1.server.gss_blob = server_gss_blob;
3988 conn->smb1.server.guid = server_guid;
3989 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3990 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3991 conn->smb1.server.name = talloc_move(conn, &server_name);
3993 conn->smb1.server.time_zone = server_time_zone;
3994 conn->smb1.server.system_time = server_system_time;
3996 tevent_req_done(req);
3999 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4003 uint16_t dialect_count = 0;
4005 buf = state->smb2.dyn;
4006 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4007 if (smb2cli_prots[i].proto < state->min_protocol) {
4011 if (smb2cli_prots[i].proto > state->max_protocol) {
4015 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4019 buf = state->smb2.fixed;
4021 SSVAL(buf, 2, dialect_count);
4022 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4023 SSVAL(buf, 6, 0); /* Reserved */
4024 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4025 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4027 SIVAL(buf, 8, 0); /* Capabilities */
4029 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4033 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4035 if (!NT_STATUS_IS_OK(status)) {
4038 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4040 memset(buf+12, 0, 16); /* ClientGuid */
4042 SBVAL(buf, 28, 0); /* ClientStartTime */
4044 return smb2cli_req_send(state, state->ev,
4045 state->conn, SMB2_OP_NEGPROT,
4047 state->timeout_msec,
4048 NULL, NULL, /* tcon, session */
4049 state->smb2.fixed, sizeof(state->smb2.fixed),
4050 state->smb2.dyn, dialect_count*2);
4053 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4055 struct tevent_req *req =
4056 tevent_req_callback_data(subreq,
4058 struct smbXcli_negprot_state *state =
4059 tevent_req_data(req,
4060 struct smbXcli_negprot_state);
4061 struct smbXcli_conn *conn = state->conn;
4062 size_t security_offset, security_length;
4068 uint16_t dialect_revision;
4069 static const struct smb2cli_req_expected_response expected[] = {
4071 .status = NT_STATUS_OK,
4076 status = smb2cli_req_recv(subreq, state, &iov,
4077 expected, ARRAY_SIZE(expected));
4078 TALLOC_FREE(subreq);
4079 if (tevent_req_nterror(req, status)) {
4083 body = (uint8_t *)iov[1].iov_base;
4085 dialect_revision = SVAL(body, 4);
4087 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4088 if (smb2cli_prots[i].proto < state->min_protocol) {
4092 if (smb2cli_prots[i].proto > state->max_protocol) {
4096 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4100 conn->protocol = smb2cli_prots[i].proto;
4104 if (conn->protocol == PROTOCOL_NONE) {
4105 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4106 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4110 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4111 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4115 /* make sure we do not loop forever */
4116 state->min_protocol = PROTOCOL_SMB2_02;
4119 * send a SMB2 negprot, in order to negotiate
4122 subreq = smbXcli_negprot_smb2_subreq(state);
4123 if (tevent_req_nomem(subreq, req)) {
4126 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4130 conn->smb2.server.security_mode = SVAL(body, 2);
4132 blob = data_blob_const(body + 8, 16);
4133 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4134 if (tevent_req_nterror(req, status)) {
4138 conn->smb2.server.capabilities = IVAL(body, 24);
4139 conn->smb2.server.max_trans_size= IVAL(body, 28);
4140 conn->smb2.server.max_read_size = IVAL(body, 32);
4141 conn->smb2.server.max_write_size= IVAL(body, 36);
4142 conn->smb2.server.system_time = BVAL(body, 40);
4143 conn->smb2.server.start_time = BVAL(body, 48);
4145 security_offset = SVAL(body, 56);
4146 security_length = SVAL(body, 58);
4148 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4149 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4153 if (security_length > iov[2].iov_len) {
4154 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4158 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4161 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4165 tevent_req_done(req);
4168 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4169 TALLOC_CTX *tmp_mem,
4172 size_t num_pending = talloc_array_length(conn->pending);
4173 struct tevent_req *subreq;
4174 struct smbXcli_req_state *substate;
4175 struct tevent_req *req;
4176 uint32_t protocol_magic;
4177 size_t inbuf_len = smb_len_nbt(inbuf);
4179 if (num_pending != 1) {
4180 return NT_STATUS_INTERNAL_ERROR;
4183 if (inbuf_len < 4) {
4184 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4187 subreq = conn->pending[0];
4188 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4189 req = tevent_req_callback_data(subreq, struct tevent_req);
4191 protocol_magic = IVAL(inbuf, 4);
4193 switch (protocol_magic) {
4195 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4196 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4197 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4200 if (substate->smb2.recv_iov == NULL) {
4202 * For the SMB1 negprot we have move it.
4204 substate->smb2.recv_iov = substate->smb1.recv_iov;
4205 substate->smb1.recv_iov = NULL;
4209 * we got an SMB2 answer, which consumed sequence number 0
4210 * so we need to use 1 as the next one
4213 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4214 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4215 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4218 DEBUG(10, ("Got non-SMB PDU\n"));
4219 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4222 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4224 return tevent_req_simple_recv_ntstatus(req);
4227 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4228 uint32_t timeout_msec,
4229 enum protocol_types min_protocol,
4230 enum protocol_types max_protocol)
4232 TALLOC_CTX *frame = talloc_stackframe();
4233 struct tevent_context *ev;
4234 struct tevent_req *req;
4235 NTSTATUS status = NT_STATUS_NO_MEMORY;
4238 if (smbXcli_conn_has_async_calls(conn)) {
4240 * Can't use sync call while an async call is in flight
4242 status = NT_STATUS_INVALID_PARAMETER_MIX;
4245 ev = tevent_context_init(frame);
4249 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4250 min_protocol, max_protocol);
4254 ok = tevent_req_poll(req, ev);
4256 status = map_nt_error_from_unix_common(errno);
4259 status = smbXcli_negprot_recv(req);
4265 static int smbXcli_session_destructor(struct smbXcli_session *session)
4267 if (session->conn == NULL) {
4271 DLIST_REMOVE(session->conn->sessions, session);
4275 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4276 struct smbXcli_conn *conn)
4278 struct smbXcli_session *session;
4280 session = talloc_zero(mem_ctx, struct smbXcli_session);
4281 if (session == NULL) {
4284 session->smb2 = talloc_zero(session, struct smb2cli_session);
4285 if (session->smb2 == NULL) {
4286 talloc_free(session);
4289 talloc_set_destructor(session, smbXcli_session_destructor);
4291 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4292 session->conn = conn;
4297 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4299 return session->smb1.session_id;
4302 void smb1cli_session_set_id(struct smbXcli_session *session,
4303 uint16_t session_id)
4305 session->smb1.session_id = session_id;
4308 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4309 const DATA_BLOB _session_key)
4311 struct smbXcli_conn *conn = session->conn;
4312 uint8_t session_key[16];
4315 return NT_STATUS_INVALID_PARAMETER_MIX;
4318 if (session->smb1.application_key.length != 0) {
4320 * TODO: do not allow this...
4322 * return NT_STATUS_INVALID_PARAMETER_MIX;
4324 data_blob_clear_free(&session->smb1.application_key);
4327 if (_session_key.length == 0) {
4328 return NT_STATUS_OK;
4331 ZERO_STRUCT(session_key);
4332 memcpy(session_key, _session_key.data,
4333 MIN(_session_key.length, sizeof(session_key)));
4335 session->smb1.application_key = data_blob_talloc(session,
4337 sizeof(session_key));
4338 ZERO_STRUCT(session_key);
4339 if (session->smb1.application_key.data == NULL) {
4340 return NT_STATUS_NO_MEMORY;
4343 return NT_STATUS_OK;
4346 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4348 struct smbXcli_conn *conn = session->conn;
4349 uint8_t security_mode = 0;
4352 return security_mode;
4355 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4356 if (conn->mandatory_signing) {
4357 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4360 return security_mode;
4363 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4365 return session->smb2->session_id;
4368 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4370 return session->smb2->session_flags;
4373 NTSTATUS smb2cli_session_application_key(struct smbXcli_session *session,
4374 TALLOC_CTX *mem_ctx,
4377 *key = data_blob_null;
4379 if (session->smb2->application_key.length == 0) {
4380 return NT_STATUS_NO_USER_SESSION_KEY;
4383 *key = data_blob_dup_talloc(mem_ctx, session->smb2->application_key);
4384 if (key->data == NULL) {
4385 return NT_STATUS_NO_MEMORY;
4388 return NT_STATUS_OK;
4391 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4392 uint64_t session_id,
4393 uint16_t session_flags)
4395 session->smb2->session_id = session_id;
4396 session->smb2->session_flags = session_flags;
4399 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4401 session->smb2->channel_sequence += 1;
4404 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4405 const DATA_BLOB _session_key,
4406 const struct iovec *recv_iov)
4408 struct smbXcli_conn *conn = session->conn;
4409 uint16_t no_sign_flags;
4410 uint8_t session_key[16];
4414 return NT_STATUS_INVALID_PARAMETER_MIX;
4417 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4419 if (session->smb2->session_flags & no_sign_flags) {
4420 session->smb2->should_sign = false;
4421 return NT_STATUS_OK;
4424 if (session->smb2->signing_key.length != 0) {
4425 return NT_STATUS_INVALID_PARAMETER_MIX;
4428 ZERO_STRUCT(session_key);
4429 memcpy(session_key, _session_key.data,
4430 MIN(_session_key.length, sizeof(session_key)));
4432 session->smb2->signing_key = data_blob_talloc(session,
4434 sizeof(session_key));
4435 if (session->smb2->signing_key.data == NULL) {
4436 ZERO_STRUCT(session_key);
4437 return NT_STATUS_NO_MEMORY;
4440 if (conn->protocol >= PROTOCOL_SMB2_24) {
4441 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4442 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4444 smb2_key_derivation(session_key, sizeof(session_key),
4445 label.data, label.length,
4446 context.data, context.length,
4447 session->smb2->signing_key.data);
4450 session->smb2->encryption_key = data_blob_dup_talloc(session,
4451 session->smb2->signing_key);
4452 if (session->smb2->encryption_key.data == NULL) {
4453 ZERO_STRUCT(session_key);
4454 return NT_STATUS_NO_MEMORY;
4457 if (conn->protocol >= PROTOCOL_SMB2_24) {
4458 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4459 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4461 smb2_key_derivation(session_key, sizeof(session_key),
4462 label.data, label.length,
4463 context.data, context.length,
4464 session->smb2->encryption_key.data);
4467 session->smb2->decryption_key = data_blob_dup_talloc(session,
4468 session->smb2->signing_key);
4469 if (session->smb2->decryption_key.data == NULL) {
4470 ZERO_STRUCT(session_key);
4471 return NT_STATUS_NO_MEMORY;
4474 if (conn->protocol >= PROTOCOL_SMB2_24) {
4475 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4476 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4478 smb2_key_derivation(session_key, sizeof(session_key),
4479 label.data, label.length,
4480 context.data, context.length,
4481 session->smb2->decryption_key.data);
4484 session->smb2->application_key = data_blob_dup_talloc(session,
4485 session->smb2->signing_key);
4486 if (session->smb2->application_key.data == NULL) {
4487 ZERO_STRUCT(session_key);
4488 return NT_STATUS_NO_MEMORY;
4491 if (conn->protocol >= PROTOCOL_SMB2_24) {
4492 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4493 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4495 smb2_key_derivation(session_key, sizeof(session_key),
4496 label.data, label.length,
4497 context.data, context.length,
4498 session->smb2->application_key.data);
4500 ZERO_STRUCT(session_key);
4502 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4503 session->smb2->signing_key);
4504 if (session->smb2_channel.signing_key.data == NULL) {
4505 return NT_STATUS_NO_MEMORY;
4508 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4509 session->conn->protocol,
4511 if (!NT_STATUS_IS_OK(status)) {
4515 session->smb2->should_sign = false;
4516 session->smb2->should_encrypt = false;
4518 if (conn->desire_signing) {
4519 session->smb2->should_sign = true;
4522 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4523 session->smb2->should_sign = true;
4526 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4527 session->smb2->should_encrypt = true;
4530 if (conn->protocol < PROTOCOL_SMB2_24) {
4531 session->smb2->should_encrypt = false;
4534 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4535 session->smb2->should_encrypt = false;
4538 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4539 sizeof(session->smb2->nonce_high));
4540 session->smb2->nonce_low = 1;
4542 return NT_STATUS_OK;
4545 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4546 struct smbXcli_session *session1,
4547 struct smbXcli_conn *conn,
4548 struct smbXcli_session **_session2)
4550 struct smbXcli_session *session2;
4552 if (session1->smb2->signing_key.length == 0) {
4553 return NT_STATUS_INVALID_PARAMETER_MIX;
4557 return NT_STATUS_INVALID_PARAMETER_MIX;
4560 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4561 if (session2 == NULL) {
4562 return NT_STATUS_NO_MEMORY;
4564 session2->smb2 = talloc_reference(session2, session1->smb2);
4565 if (session2->smb2 == NULL) {
4566 talloc_free(session2);
4567 return NT_STATUS_NO_MEMORY;
4570 talloc_set_destructor(session2, smbXcli_session_destructor);
4571 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4572 session2->conn = conn;
4574 *_session2 = session2;
4575 return NT_STATUS_OK;
4578 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4579 const DATA_BLOB _channel_key,
4580 const struct iovec *recv_iov)
4582 struct smbXcli_conn *conn = session->conn;
4583 uint8_t channel_key[16];
4587 return NT_STATUS_INVALID_PARAMETER_MIX;
4590 if (session->smb2_channel.signing_key.length != 0) {
4591 return NT_STATUS_INVALID_PARAMETER_MIX;
4594 ZERO_STRUCT(channel_key);
4595 memcpy(channel_key, _channel_key.data,
4596 MIN(_channel_key.length, sizeof(channel_key)));
4598 session->smb2_channel.signing_key = data_blob_talloc(session,
4600 sizeof(channel_key));
4601 if (session->smb2_channel.signing_key.data == NULL) {
4602 ZERO_STRUCT(channel_key);
4603 return NT_STATUS_NO_MEMORY;
4606 if (conn->protocol >= PROTOCOL_SMB2_24) {
4607 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4608 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4610 smb2_key_derivation(channel_key, sizeof(channel_key),
4611 label.data, label.length,
4612 context.data, context.length,
4613 session->smb2_channel.signing_key.data);
4615 ZERO_STRUCT(channel_key);
4617 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4618 session->conn->protocol,
4620 if (!NT_STATUS_IS_OK(status)) {
4624 return NT_STATUS_OK;
4627 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4629 struct smbXcli_tcon *tcon;
4631 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4639 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4641 return tcon->smb1.tcon_id;
4644 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4646 tcon->smb1.tcon_id = tcon_id;
4649 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
4651 uint16_t optional_support,
4652 uint32_t maximal_access,
4653 uint32_t guest_maximal_access,
4654 const char *service,
4655 const char *fs_type)
4657 tcon->smb1.tcon_id = tcon_id;
4658 tcon->smb1.optional_support = optional_support;
4659 tcon->smb1.maximal_access = maximal_access;
4660 tcon->smb1.guest_maximal_access = guest_maximal_access;
4662 TALLOC_FREE(tcon->smb1.service);
4663 tcon->smb1.service = talloc_strdup(tcon, service);
4664 if (service != NULL && tcon->smb1.service == NULL) {
4668 TALLOC_FREE(tcon->smb1.fs_type);
4669 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
4670 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
4677 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
4679 return tcon->smb2.tcon_id;
4682 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
4684 return tcon->smb2.capabilities;
4687 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
4688 struct smbXcli_session *session,
4692 uint32_t capabilities,
4693 uint32_t maximal_access)
4695 tcon->smb2.tcon_id = tcon_id;
4696 tcon->smb2.type = type;
4697 tcon->smb2.flags = flags;
4698 tcon->smb2.capabilities = capabilities;
4699 tcon->smb2.maximal_access = maximal_access;
4701 tcon->smb2.should_encrypt = false;
4703 if (session == NULL) {
4707 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
4709 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
4710 tcon->smb2.should_encrypt = true;