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;
155 struct smb2cli_session *smb2;
158 DATA_BLOB signing_key;
162 * this should be a short term hack
163 * until the upper layers have implemented
166 bool disconnect_expired;
169 struct smbXcli_tcon {
172 uint16_t optional_support;
173 uint32_t maximal_access;
174 uint32_t guest_maximal_access;
183 uint32_t capabilities;
184 uint32_t maximal_access;
189 struct smbXcli_req_state {
190 struct tevent_context *ev;
191 struct smbXcli_conn *conn;
192 struct smbXcli_session *session; /* maybe NULL */
193 struct smbXcli_tcon *tcon; /* maybe NULL */
195 uint8_t length_hdr[4];
202 /* Space for the header including the wct */
203 uint8_t hdr[HDR_VWV];
206 * For normal requests, smb1cli_req_send chooses a mid.
207 * SecondaryV trans requests need to use the mid of the primary
208 * request, so we need a place to store it.
209 * Assume it is set if != 0.
214 uint8_t bytecount_buf[2];
216 #define MAX_SMB_IOV 10
217 /* length_hdr, hdr, words, byte_count, buffers */
218 struct iovec iov[1 + 3 + MAX_SMB_IOV];
223 struct tevent_req **chained_requests;
226 NTSTATUS recv_status;
227 /* always an array of 3 talloc elements */
228 struct iovec *recv_iov;
232 const uint8_t *fixed;
237 uint8_t transform[SMB2_TF_HDR_SIZE];
238 uint8_t hdr[SMB2_HDR_BODY];
239 uint8_t pad[7]; /* padding space for compounding */
242 * always an array of 3 talloc elements
243 * (without a SMB2_TRANSFORM header!)
247 struct iovec *recv_iov;
249 uint16_t credit_charge;
253 uint64_t encryption_session_id;
255 bool signing_skipped;
261 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
264 * NT_STATUS_OK, means we do not notify the callers
266 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
268 while (conn->sessions) {
269 conn->sessions->conn = NULL;
270 DLIST_REMOVE(conn->sessions, conn->sessions);
273 if (conn->smb1.trans_enc) {
274 TALLOC_FREE(conn->smb1.trans_enc);
280 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
282 const char *remote_name,
283 enum smb_signing_setting signing_state,
284 uint32_t smb1_capabilities,
285 struct GUID *client_guid,
286 uint32_t smb2_capabilities)
288 struct smbXcli_conn *conn = NULL;
290 struct sockaddr *sa = NULL;
294 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
300 conn->write_fd = dup(fd);
301 if (conn->write_fd == -1) {
305 conn->remote_name = talloc_strdup(conn, remote_name);
306 if (conn->remote_name == NULL) {
311 ss = (void *)&conn->local_ss;
312 sa = (struct sockaddr *)ss;
313 sa_length = sizeof(conn->local_ss);
314 ret = getsockname(fd, sa, &sa_length);
318 ss = (void *)&conn->remote_ss;
319 sa = (struct sockaddr *)ss;
320 sa_length = sizeof(conn->remote_ss);
321 ret = getpeername(fd, sa, &sa_length);
326 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
327 if (conn->outgoing == NULL) {
330 conn->pending = NULL;
332 conn->protocol = PROTOCOL_NONE;
334 switch (signing_state) {
335 case SMB_SIGNING_OFF:
337 conn->allow_signing = false;
338 conn->desire_signing = false;
339 conn->mandatory_signing = false;
341 case SMB_SIGNING_DEFAULT:
342 case SMB_SIGNING_IF_REQUIRED:
343 /* if the server requires it */
344 conn->allow_signing = true;
345 conn->desire_signing = false;
346 conn->mandatory_signing = false;
348 case SMB_SIGNING_REQUIRED:
350 conn->allow_signing = true;
351 conn->desire_signing = true;
352 conn->mandatory_signing = true;
356 conn->smb1.client.capabilities = smb1_capabilities;
357 conn->smb1.client.max_xmit = UINT16_MAX;
359 conn->smb1.capabilities = conn->smb1.client.capabilities;
360 conn->smb1.max_xmit = 1024;
364 /* initialise signing */
365 conn->smb1.signing = smb_signing_init(conn,
367 conn->desire_signing,
368 conn->mandatory_signing);
369 if (!conn->smb1.signing) {
373 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
374 if (conn->mandatory_signing) {
375 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
378 conn->smb2.client.guid = *client_guid;
380 conn->smb2.client.capabilities = smb2_capabilities;
382 conn->smb2.cur_credits = 1;
383 conn->smb2.max_credits = 0;
385 talloc_set_destructor(conn, smbXcli_conn_destructor);
389 if (conn->write_fd != -1) {
390 close(conn->write_fd);
396 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
402 if (conn->read_fd == -1) {
409 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
411 return conn->protocol;
414 bool smbXcli_conn_use_status32(struct smbXcli_conn *conn)
416 if (conn->protocol >= PROTOCOL_SMB2_02) {
420 if (smb1cli_conn_capabilities(conn) & CAP_STATUS32) {
427 bool smbXcli_conn_use_large_files(struct smbXcli_conn *conn)
429 if (conn->protocol >= PROTOCOL_SMB2_02) {
433 if (smb1cli_conn_capabilities(conn) & CAP_LARGE_FILES) {
440 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
442 if (conn->protocol >= PROTOCOL_SMB2_02) {
446 if (conn->smb1.capabilities & CAP_UNICODE) {
453 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
455 set_socket_options(conn->read_fd, options);
458 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
460 return &conn->local_ss;
463 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
465 return &conn->remote_ss;
468 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
470 return conn->remote_name;
473 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
475 if (conn->protocol >= PROTOCOL_SMB2_02) {
482 return conn->smb1.server.max_mux;
485 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
487 if (conn->protocol >= PROTOCOL_SMB2_02) {
488 return conn->smb2.server.system_time;
491 return conn->smb1.server.system_time;
494 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
496 if (conn->protocol >= PROTOCOL_SMB2_02) {
497 return &conn->smb2.server.gss_blob;
500 return &conn->smb1.server.gss_blob;
503 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
505 if (conn->protocol >= PROTOCOL_SMB2_02) {
506 return &conn->smb2.server.guid;
509 return &conn->smb1.server.guid;
512 struct smbXcli_conn_samba_suicide_state {
513 struct smbXcli_conn *conn;
518 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
520 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
521 struct tevent_context *ev,
522 struct smbXcli_conn *conn,
525 struct tevent_req *req, *subreq;
526 struct smbXcli_conn_samba_suicide_state *state;
528 req = tevent_req_create(mem_ctx, &state,
529 struct smbXcli_conn_samba_suicide_state);
534 SIVAL(state->buf, 4, 0x74697865);
535 SCVAL(state->buf, 8, exitcode);
536 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
538 state->iov.iov_base = state->buf;
539 state->iov.iov_len = sizeof(state->buf);
541 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
542 false, &state->iov, 1);
543 if (tevent_req_nomem(subreq, req)) {
544 return tevent_req_post(req, ev);
546 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
550 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
552 struct tevent_req *req = tevent_req_callback_data(
553 subreq, struct tevent_req);
554 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
555 req, struct smbXcli_conn_samba_suicide_state);
559 nwritten = writev_recv(subreq, &err);
561 if (nwritten == -1) {
562 NTSTATUS status = map_nt_error_from_unix_common(err);
563 smbXcli_conn_disconnect(state->conn, status);
566 tevent_req_done(req);
569 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
571 return tevent_req_simple_recv_ntstatus(req);
574 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
577 TALLOC_CTX *frame = talloc_stackframe();
578 struct tevent_context *ev;
579 struct tevent_req *req;
580 NTSTATUS status = NT_STATUS_NO_MEMORY;
583 if (smbXcli_conn_has_async_calls(conn)) {
585 * Can't use sync call while an async call is in flight
587 status = NT_STATUS_INVALID_PARAMETER_MIX;
590 ev = samba_tevent_context_init(frame);
594 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
598 ok = tevent_req_poll(req, ev);
600 status = map_nt_error_from_unix_common(errno);
603 status = smbXcli_conn_samba_suicide_recv(req);
609 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
611 return conn->smb1.capabilities;
614 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
616 return conn->smb1.max_xmit;
619 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
621 return conn->smb1.server.session_key;
624 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
626 return conn->smb1.server.challenge;
629 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
631 return conn->smb1.server.security_mode;
634 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
636 return conn->smb1.server.readbraw;
639 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
641 return conn->smb1.server.writebraw;
644 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
646 return conn->smb1.server.lockread;
649 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
651 return conn->smb1.server.writeunlock;
654 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
656 return conn->smb1.server.time_zone;
659 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
660 const DATA_BLOB user_session_key,
661 const DATA_BLOB response)
663 return smb_signing_activate(conn->smb1.signing,
668 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
669 const uint8_t *buf, uint32_t seqnum)
671 const uint8_t *hdr = buf + NBT_HDR_SIZE;
672 size_t len = smb_len_nbt(buf);
674 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
677 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
679 return smb_signing_is_active(conn->smb1.signing);
682 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
683 struct smb_trans_enc_state *es)
685 /* Replace the old state, if any. */
686 if (conn->smb1.trans_enc) {
687 TALLOC_FREE(conn->smb1.trans_enc);
689 conn->smb1.trans_enc = es;
692 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
694 return common_encryption_on(conn->smb1.trans_enc);
698 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
700 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
701 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
703 if (NT_STATUS_IS_OK(status)) {
707 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
711 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
715 * Is the SMB command able to hold an AND_X successor
716 * @param[in] cmd The SMB command in question
717 * @retval Can we add a chained request after "cmd"?
719 bool smb1cli_is_andx_req(uint8_t cmd)
739 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
741 size_t num_pending = talloc_array_length(conn->pending);
747 result = conn->smb1.mid++;
748 if ((result == 0) || (result == 0xffff)) {
752 for (i=0; i<num_pending; i++) {
753 if (result == smb1cli_req_mid(conn->pending[i])) {
758 if (i == num_pending) {
764 void smbXcli_req_unset_pending(struct tevent_req *req)
766 struct smbXcli_req_state *state =
768 struct smbXcli_req_state);
769 struct smbXcli_conn *conn = state->conn;
770 size_t num_pending = talloc_array_length(conn->pending);
773 if (state->smb1.mid != 0) {
775 * This is a [nt]trans[2] request which waits
776 * for more than one reply.
781 talloc_set_destructor(req, NULL);
783 if (num_pending == 1) {
785 * The pending read_smb tevent_req is a child of
786 * conn->pending. So if nothing is pending anymore, we need to
787 * delete the socket read fde.
789 TALLOC_FREE(conn->pending);
790 conn->read_smb_req = NULL;
794 for (i=0; i<num_pending; i++) {
795 if (req == conn->pending[i]) {
799 if (i == num_pending) {
801 * Something's seriously broken. Just returning here is the
802 * right thing nevertheless, the point of this routine is to
803 * remove ourselves from conn->pending.
809 * Remove ourselves from the conn->pending array
811 for (; i < (num_pending - 1); i++) {
812 conn->pending[i] = conn->pending[i+1];
816 * No NULL check here, we're shrinking by sizeof(void *), and
817 * talloc_realloc just adjusts the size for this.
819 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
824 static int smbXcli_req_destructor(struct tevent_req *req)
826 struct smbXcli_req_state *state =
828 struct smbXcli_req_state);
831 * Make sure we really remove it from
832 * the pending array on destruction.
835 smbXcli_req_unset_pending(req);
839 static bool smb1cli_req_cancel(struct tevent_req *req);
840 static bool smb2cli_req_cancel(struct tevent_req *req);
842 static bool smbXcli_req_cancel(struct tevent_req *req)
844 struct smbXcli_req_state *state =
846 struct smbXcli_req_state);
848 if (!smbXcli_conn_is_connected(state->conn)) {
852 if (state->conn->protocol == PROTOCOL_NONE) {
856 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
857 return smb2cli_req_cancel(req);
860 return smb1cli_req_cancel(req);
863 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
865 bool smbXcli_req_set_pending(struct tevent_req *req)
867 struct smbXcli_req_state *state =
869 struct smbXcli_req_state);
870 struct smbXcli_conn *conn;
871 struct tevent_req **pending;
876 if (!smbXcli_conn_is_connected(conn)) {
880 num_pending = talloc_array_length(conn->pending);
882 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
884 if (pending == NULL) {
887 pending[num_pending] = req;
888 conn->pending = pending;
889 talloc_set_destructor(req, smbXcli_req_destructor);
890 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
892 if (!smbXcli_conn_receive_next(conn)) {
894 * the caller should notify the current request
896 * And all other pending requests get notified
897 * by smbXcli_conn_disconnect().
899 smbXcli_req_unset_pending(req);
900 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
907 static void smbXcli_conn_received(struct tevent_req *subreq);
909 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
911 size_t num_pending = talloc_array_length(conn->pending);
912 struct tevent_req *req;
913 struct smbXcli_req_state *state;
915 if (conn->read_smb_req != NULL) {
919 if (num_pending == 0) {
920 if (conn->smb2.mid < UINT64_MAX) {
921 /* no more pending requests, so we are done for now */
926 * If there are no more SMB2 requests possible,
927 * because we are out of message ids,
928 * we need to disconnect.
930 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
934 req = conn->pending[0];
935 state = tevent_req_data(req, struct smbXcli_req_state);
938 * We're the first ones, add the read_smb request that waits for the
939 * answer from the server
941 conn->read_smb_req = read_smb_send(conn->pending,
944 if (conn->read_smb_req == NULL) {
947 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
951 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
953 struct smbXcli_session *session;
955 tevent_queue_stop(conn->outgoing);
957 if (conn->read_fd != -1) {
958 close(conn->read_fd);
960 if (conn->write_fd != -1) {
961 close(conn->write_fd);
966 session = conn->sessions;
967 if (talloc_array_length(conn->pending) == 0) {
969 * if we do not have pending requests
970 * there is no need to update the channel_sequence
974 for (; session; session = session->next) {
975 smb2cli_session_increment_channel_sequence(session);
979 * Cancel all pending requests. We do not do a for-loop walking
980 * conn->pending because that array changes in
981 * smbXcli_req_unset_pending.
983 while (talloc_array_length(conn->pending) > 0) {
984 struct tevent_req *req;
985 struct smbXcli_req_state *state;
986 struct tevent_req **chain;
990 req = conn->pending[0];
991 state = tevent_req_data(req, struct smbXcli_req_state);
993 if (state->smb1.chained_requests == NULL) {
995 * We're dead. No point waiting for trans2
1000 smbXcli_req_unset_pending(req);
1002 if (NT_STATUS_IS_OK(status)) {
1003 /* do not notify the callers */
1008 * we need to defer the callback, because we may notify
1009 * more then one caller.
1011 tevent_req_defer_callback(req, state->ev);
1012 tevent_req_nterror(req, status);
1016 chain = talloc_move(conn, &state->smb1.chained_requests);
1017 num_chained = talloc_array_length(chain);
1019 for (i=0; i<num_chained; i++) {
1021 state = tevent_req_data(req, struct smbXcli_req_state);
1024 * We're dead. No point waiting for trans2
1027 state->smb1.mid = 0;
1029 smbXcli_req_unset_pending(req);
1031 if (NT_STATUS_IS_OK(status)) {
1032 /* do not notify the callers */
1037 * we need to defer the callback, because we may notify
1038 * more than one caller.
1040 tevent_req_defer_callback(req, state->ev);
1041 tevent_req_nterror(req, status);
1048 * Fetch a smb request's mid. Only valid after the request has been sent by
1049 * smb1cli_req_send().
1051 uint16_t smb1cli_req_mid(struct tevent_req *req)
1053 struct smbXcli_req_state *state =
1054 tevent_req_data(req,
1055 struct smbXcli_req_state);
1057 if (state->smb1.mid != 0) {
1058 return state->smb1.mid;
1061 return SVAL(state->smb1.hdr, HDR_MID);
1064 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1066 struct smbXcli_req_state *state =
1067 tevent_req_data(req,
1068 struct smbXcli_req_state);
1070 state->smb1.mid = mid;
1073 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1075 struct smbXcli_req_state *state =
1076 tevent_req_data(req,
1077 struct smbXcli_req_state);
1079 return state->smb1.seqnum;
1082 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1084 struct smbXcli_req_state *state =
1085 tevent_req_data(req,
1086 struct smbXcli_req_state);
1088 state->smb1.seqnum = seqnum;
1091 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1095 for (i=0; i<count; i++) {
1096 result += iov[i].iov_len;
1101 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1102 const struct iovec *iov,
1105 size_t len = smbXcli_iov_len(iov, count);
1110 buf = talloc_array(mem_ctx, uint8_t, len);
1115 for (i=0; i<count; i++) {
1116 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1117 copied += iov[i].iov_len;
1122 static void smb1cli_req_flags(enum protocol_types protocol,
1123 uint32_t smb1_capabilities,
1124 uint8_t smb_command,
1125 uint8_t additional_flags,
1126 uint8_t clear_flags,
1128 uint16_t additional_flags2,
1129 uint16_t clear_flags2,
1133 uint16_t flags2 = 0;
1135 if (protocol >= PROTOCOL_LANMAN1) {
1136 flags |= FLAG_CASELESS_PATHNAMES;
1137 flags |= FLAG_CANONICAL_PATHNAMES;
1140 if (protocol >= PROTOCOL_LANMAN2) {
1141 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1142 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1145 if (protocol >= PROTOCOL_NT1) {
1146 flags2 |= FLAGS2_IS_LONG_NAME;
1148 if (smb1_capabilities & CAP_UNICODE) {
1149 flags2 |= FLAGS2_UNICODE_STRINGS;
1151 if (smb1_capabilities & CAP_STATUS32) {
1152 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1154 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1155 flags2 |= FLAGS2_EXTENDED_SECURITY;
1159 flags |= additional_flags;
1160 flags &= ~clear_flags;
1161 flags2 |= additional_flags2;
1162 flags2 &= ~clear_flags2;
1168 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1170 static bool smb1cli_req_cancel(struct tevent_req *req)
1172 struct smbXcli_req_state *state =
1173 tevent_req_data(req,
1174 struct smbXcli_req_state);
1179 struct tevent_req *subreq;
1182 flags = CVAL(state->smb1.hdr, HDR_FLG);
1183 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1184 pid = SVAL(state->smb1.hdr, HDR_PID);
1185 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1186 mid = SVAL(state->smb1.hdr, HDR_MID);
1188 subreq = smb1cli_req_create(state, state->ev,
1198 0, NULL); /* bytes */
1199 if (subreq == NULL) {
1202 smb1cli_req_set_mid(subreq, mid);
1204 status = smb1cli_req_chain_submit(&subreq, 1);
1205 if (!NT_STATUS_IS_OK(status)) {
1206 TALLOC_FREE(subreq);
1209 smb1cli_req_set_mid(subreq, 0);
1211 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1216 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1218 /* we do not care about the result */
1219 TALLOC_FREE(subreq);
1222 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1223 struct tevent_context *ev,
1224 struct smbXcli_conn *conn,
1225 uint8_t smb_command,
1226 uint8_t additional_flags,
1227 uint8_t clear_flags,
1228 uint16_t additional_flags2,
1229 uint16_t clear_flags2,
1230 uint32_t timeout_msec,
1232 struct smbXcli_tcon *tcon,
1233 struct smbXcli_session *session,
1234 uint8_t wct, uint16_t *vwv,
1236 struct iovec *bytes_iov)
1238 struct tevent_req *req;
1239 struct smbXcli_req_state *state;
1241 uint16_t flags2 = 0;
1245 if (iov_count > MAX_SMB_IOV) {
1247 * Should not happen :-)
1252 req = tevent_req_create(mem_ctx, &state,
1253 struct smbXcli_req_state);
1259 state->session = session;
1263 uid = session->smb1.session_id;
1267 tid = tcon->smb1.tcon_id;
1270 state->smb1.recv_cmd = 0xFF;
1271 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1272 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1273 if (state->smb1.recv_iov == NULL) {
1278 smb1cli_req_flags(conn->protocol,
1279 conn->smb1.capabilities,
1288 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1289 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1290 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1291 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1292 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1293 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1294 SSVAL(state->smb1.hdr, HDR_TID, tid);
1295 SSVAL(state->smb1.hdr, HDR_PID, pid);
1296 SSVAL(state->smb1.hdr, HDR_UID, uid);
1297 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1298 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1300 state->smb1.vwv = vwv;
1302 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1304 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1305 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1306 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1307 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1308 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1309 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1310 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1311 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1313 if (iov_count != 0) {
1314 memcpy(&state->smb1.iov[4], bytes_iov,
1315 iov_count * sizeof(*bytes_iov));
1317 state->smb1.iov_count = iov_count + 4;
1319 if (timeout_msec > 0) {
1320 struct timeval endtime;
1322 endtime = timeval_current_ofs_msec(timeout_msec);
1323 if (!tevent_req_set_endtime(req, ev, endtime)) {
1328 switch (smb_command) {
1332 state->one_way = true;
1335 state->one_way = true;
1336 state->smb1.one_way_seqnum = true;
1340 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1341 state->one_way = true;
1349 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1350 struct iovec *iov, int iov_count,
1352 bool one_way_seqnum)
1354 TALLOC_CTX *frame = NULL;
1358 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1359 * iovec directly. MD5Update would do that just fine.
1362 if (iov_count < 4) {
1363 return NT_STATUS_INVALID_PARAMETER_MIX;
1365 if (iov[0].iov_len != NBT_HDR_SIZE) {
1366 return NT_STATUS_INVALID_PARAMETER_MIX;
1368 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1369 return NT_STATUS_INVALID_PARAMETER_MIX;
1371 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1372 return NT_STATUS_INVALID_PARAMETER_MIX;
1374 if (iov[3].iov_len != sizeof(uint16_t)) {
1375 return NT_STATUS_INVALID_PARAMETER_MIX;
1378 frame = talloc_stackframe();
1380 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1382 return NT_STATUS_NO_MEMORY;
1385 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1387 smb_signing_sign_pdu(conn->smb1.signing,
1388 buf, talloc_get_size(buf),
1390 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1393 return NT_STATUS_OK;
1396 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1397 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1398 TALLOC_CTX *tmp_mem,
1401 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1402 struct smbXcli_req_state *state,
1403 struct iovec *iov, int iov_count)
1405 struct tevent_req *subreq;
1410 if (!smbXcli_conn_is_connected(state->conn)) {
1411 return NT_STATUS_CONNECTION_DISCONNECTED;
1414 if (state->conn->protocol > PROTOCOL_NT1) {
1415 return NT_STATUS_REVISION_MISMATCH;
1418 if (iov_count < 4) {
1419 return NT_STATUS_INVALID_PARAMETER_MIX;
1421 if (iov[0].iov_len != NBT_HDR_SIZE) {
1422 return NT_STATUS_INVALID_PARAMETER_MIX;
1424 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1425 return NT_STATUS_INVALID_PARAMETER_MIX;
1427 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1428 return NT_STATUS_INVALID_PARAMETER_MIX;
1430 if (iov[3].iov_len != sizeof(uint16_t)) {
1431 return NT_STATUS_INVALID_PARAMETER_MIX;
1434 cmd = CVAL(iov[1].iov_base, HDR_COM);
1435 if (cmd == SMBreadBraw) {
1436 if (smbXcli_conn_has_async_calls(state->conn)) {
1437 return NT_STATUS_INVALID_PARAMETER_MIX;
1439 state->conn->smb1.read_braw_req = req;
1442 if (state->smb1.mid != 0) {
1443 mid = state->smb1.mid;
1445 mid = smb1cli_alloc_mid(state->conn);
1447 SSVAL(iov[1].iov_base, HDR_MID, mid);
1449 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1451 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1452 &state->smb1.seqnum,
1453 state->smb1.one_way_seqnum);
1455 if (!NT_STATUS_IS_OK(status)) {
1460 * If we supported multiple encrytion contexts
1461 * here we'd look up based on tid.
1463 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1464 char *buf, *enc_buf;
1466 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1468 return NT_STATUS_NO_MEMORY;
1470 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1471 (char *)buf, &enc_buf);
1473 if (!NT_STATUS_IS_OK(status)) {
1474 DEBUG(0, ("Error in encrypting client message: %s\n",
1475 nt_errstr(status)));
1478 buf = (char *)talloc_memdup(state, enc_buf,
1479 smb_len_nbt(enc_buf)+4);
1482 return NT_STATUS_NO_MEMORY;
1484 iov[0].iov_base = (void *)buf;
1485 iov[0].iov_len = talloc_get_size(buf);
1489 if (state->conn->dispatch_incoming == NULL) {
1490 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1493 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1495 subreq = writev_send(state, state->ev, state->conn->outgoing,
1496 state->conn->write_fd, false, iov, iov_count);
1497 if (subreq == NULL) {
1498 return NT_STATUS_NO_MEMORY;
1500 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1501 return NT_STATUS_OK;
1504 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1505 struct tevent_context *ev,
1506 struct smbXcli_conn *conn,
1507 uint8_t smb_command,
1508 uint8_t additional_flags,
1509 uint8_t clear_flags,
1510 uint16_t additional_flags2,
1511 uint16_t clear_flags2,
1512 uint32_t timeout_msec,
1514 struct smbXcli_tcon *tcon,
1515 struct smbXcli_session *session,
1516 uint8_t wct, uint16_t *vwv,
1518 const uint8_t *bytes)
1520 struct tevent_req *req;
1524 iov.iov_base = discard_const_p(void, bytes);
1525 iov.iov_len = num_bytes;
1527 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1528 additional_flags, clear_flags,
1529 additional_flags2, clear_flags2,
1536 if (!tevent_req_is_in_progress(req)) {
1537 return tevent_req_post(req, ev);
1539 status = smb1cli_req_chain_submit(&req, 1);
1540 if (tevent_req_nterror(req, status)) {
1541 return tevent_req_post(req, ev);
1546 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1548 struct tevent_req *req =
1549 tevent_req_callback_data(subreq,
1551 struct smbXcli_req_state *state =
1552 tevent_req_data(req,
1553 struct smbXcli_req_state);
1557 nwritten = writev_recv(subreq, &err);
1558 TALLOC_FREE(subreq);
1559 if (nwritten == -1) {
1560 NTSTATUS status = map_nt_error_from_unix_common(err);
1561 smbXcli_conn_disconnect(state->conn, status);
1565 if (state->one_way) {
1566 state->inbuf = NULL;
1567 tevent_req_done(req);
1571 if (!smbXcli_req_set_pending(req)) {
1572 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1577 static void smbXcli_conn_received(struct tevent_req *subreq)
1579 struct smbXcli_conn *conn =
1580 tevent_req_callback_data(subreq,
1581 struct smbXcli_conn);
1582 TALLOC_CTX *frame = talloc_stackframe();
1588 if (subreq != conn->read_smb_req) {
1589 DEBUG(1, ("Internal error: cli_smb_received called with "
1590 "unexpected subreq\n"));
1591 status = NT_STATUS_INTERNAL_ERROR;
1592 smbXcli_conn_disconnect(conn, status);
1596 conn->read_smb_req = NULL;
1598 received = read_smb_recv(subreq, frame, &inbuf, &err);
1599 TALLOC_FREE(subreq);
1600 if (received == -1) {
1601 status = map_nt_error_from_unix_common(err);
1602 smbXcli_conn_disconnect(conn, status);
1607 status = conn->dispatch_incoming(conn, frame, inbuf);
1609 if (NT_STATUS_IS_OK(status)) {
1611 * We should not do any more processing
1612 * as the dispatch function called
1613 * tevent_req_done().
1616 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1618 * We got an error, so notify all pending requests
1620 smbXcli_conn_disconnect(conn, status);
1625 * We got NT_STATUS_RETRY, so we may ask for a
1626 * next incoming pdu.
1628 if (!smbXcli_conn_receive_next(conn)) {
1629 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1633 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1634 struct iovec **piov, int *pnum_iov)
1645 size_t min_size = MIN_SMB_SIZE;
1647 buflen = smb_len_tcp(buf);
1650 hdr = buf + NBT_HDR_SIZE;
1652 status = smb1cli_pull_raw_error(hdr);
1653 if (NT_STATUS_IS_ERR(status)) {
1655 * This is an ugly hack to support OS/2
1656 * which skips the byte_count in the DATA block
1657 * on some error responses.
1661 min_size -= sizeof(uint16_t);
1664 if (buflen < min_size) {
1665 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1669 * This returns iovec elements in the following order:
1684 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1686 return NT_STATUS_NO_MEMORY;
1688 iov[0].iov_base = hdr;
1689 iov[0].iov_len = HDR_WCT;
1692 cmd = CVAL(hdr, HDR_COM);
1696 size_t len = buflen - taken;
1698 struct iovec *iov_tmp;
1705 * we need at least WCT
1707 needed = sizeof(uint8_t);
1709 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1710 __location__, (int)len, (int)needed));
1715 * Now we check if the specified words are there
1717 wct = CVAL(hdr, wct_ofs);
1718 needed += wct * sizeof(uint16_t);
1720 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1721 __location__, (int)len, (int)needed));
1725 if ((num_iov == 1) &&
1727 NT_STATUS_IS_ERR(status))
1730 * This is an ugly hack to support OS/2
1731 * which skips the byte_count in the DATA block
1732 * on some error responses.
1736 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1738 if (iov_tmp == NULL) {
1740 return NT_STATUS_NO_MEMORY;
1743 cur = &iov[num_iov];
1747 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1749 cur[1].iov_base = cur[0].iov_base;
1756 * we need at least BCC
1758 needed += sizeof(uint16_t);
1760 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1761 __location__, (int)len, (int)needed));
1766 * Now we check if the specified bytes are there
1768 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1769 bcc = SVAL(hdr, bcc_ofs);
1770 needed += bcc * sizeof(uint8_t);
1772 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1773 __location__, (int)len, (int)needed));
1778 * we allocate 2 iovec structures for words and bytes
1780 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1782 if (iov_tmp == NULL) {
1784 return NT_STATUS_NO_MEMORY;
1787 cur = &iov[num_iov];
1790 cur[0].iov_len = wct * sizeof(uint16_t);
1791 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1792 cur[1].iov_len = bcc * sizeof(uint8_t);
1793 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1797 if (!smb1cli_is_andx_req(cmd)) {
1799 * If the current command does not have AndX chanining
1805 if (wct == 0 && bcc == 0) {
1807 * An empty response also ends the chain,
1808 * most likely with an error.
1814 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1815 __location__, (int)wct, (int)cmd));
1818 cmd = CVAL(cur[0].iov_base, 0);
1821 * If it is the end of the chain we are also done.
1825 wct_ofs = SVAL(cur[0].iov_base, 2);
1827 if (wct_ofs < taken) {
1828 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1830 if (wct_ofs > buflen) {
1831 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1835 * we consumed everything up to the start of the next
1841 remaining = buflen - taken;
1843 if (remaining > 0 && num_iov >= 3) {
1845 * The last DATA block gets the remaining
1846 * bytes, this is needed to support
1847 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1849 iov[num_iov-1].iov_len += remaining;
1853 *pnum_iov = num_iov;
1854 return NT_STATUS_OK;
1858 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1861 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1862 TALLOC_CTX *tmp_mem,
1865 struct tevent_req *req;
1866 struct smbXcli_req_state *state;
1873 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1874 size_t len = smb_len_tcp(inbuf);
1875 struct iovec *iov = NULL;
1877 struct tevent_req **chain = NULL;
1878 size_t num_chained = 0;
1879 size_t num_responses = 0;
1881 if (conn->smb1.read_braw_req != NULL) {
1882 req = conn->smb1.read_braw_req;
1883 conn->smb1.read_braw_req = NULL;
1884 state = tevent_req_data(req, struct smbXcli_req_state);
1886 smbXcli_req_unset_pending(req);
1888 if (state->smb1.recv_iov == NULL) {
1890 * For requests with more than
1891 * one response, we have to readd the
1894 state->smb1.recv_iov = talloc_zero_array(state,
1897 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1898 return NT_STATUS_OK;
1902 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1903 state->smb1.recv_iov[0].iov_len = len;
1904 ZERO_STRUCT(state->smb1.recv_iov[1]);
1905 ZERO_STRUCT(state->smb1.recv_iov[2]);
1907 state->smb1.recv_cmd = SMBreadBraw;
1908 state->smb1.recv_status = NT_STATUS_OK;
1909 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1911 tevent_req_done(req);
1912 return NT_STATUS_OK;
1915 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1916 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1917 DEBUG(10, ("Got non-SMB PDU\n"));
1918 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1922 * If we supported multiple encrytion contexts
1923 * here we'd look up based on tid.
1925 if (common_encryption_on(conn->smb1.trans_enc)
1926 && (CVAL(inbuf, 0) == 0)) {
1927 uint16_t enc_ctx_num;
1929 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1930 if (!NT_STATUS_IS_OK(status)) {
1931 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1932 nt_errstr(status)));
1936 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1937 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1939 conn->smb1.trans_enc->enc_ctx_num));
1940 return NT_STATUS_INVALID_HANDLE;
1943 status = common_decrypt_buffer(conn->smb1.trans_enc,
1945 if (!NT_STATUS_IS_OK(status)) {
1946 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1947 nt_errstr(status)));
1950 inhdr = inbuf + NBT_HDR_SIZE;
1951 len = smb_len_nbt(inbuf);
1954 mid = SVAL(inhdr, HDR_MID);
1955 num_pending = talloc_array_length(conn->pending);
1957 for (i=0; i<num_pending; i++) {
1958 if (mid == smb1cli_req_mid(conn->pending[i])) {
1962 if (i == num_pending) {
1963 /* Dump unexpected reply */
1964 return NT_STATUS_RETRY;
1967 oplock_break = false;
1969 if (mid == 0xffff) {
1971 * Paranoia checks that this is really an oplock break request.
1973 oplock_break = (len == 51); /* hdr + 8 words */
1974 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1975 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1976 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1977 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1979 if (!oplock_break) {
1980 /* Dump unexpected reply */
1981 return NT_STATUS_RETRY;
1985 req = conn->pending[i];
1986 state = tevent_req_data(req, struct smbXcli_req_state);
1988 if (!oplock_break /* oplock breaks are not signed */
1989 && !smb_signing_check_pdu(conn->smb1.signing,
1990 inhdr, len, state->smb1.seqnum+1)) {
1991 DEBUG(10, ("cli_check_sign_mac failed\n"));
1992 return NT_STATUS_ACCESS_DENIED;
1995 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1997 if (!NT_STATUS_IS_OK(status)) {
1998 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1999 nt_errstr(status)));
2003 cmd = CVAL(inhdr, HDR_COM);
2004 status = smb1cli_pull_raw_error(inhdr);
2006 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2007 (state->session != NULL) && state->session->disconnect_expired)
2010 * this should be a short term hack
2011 * until the upper layers have implemented
2012 * re-authentication.
2017 if (state->smb1.chained_requests == NULL) {
2019 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2022 smbXcli_req_unset_pending(req);
2024 if (state->smb1.recv_iov == NULL) {
2026 * For requests with more than
2027 * one response, we have to readd the
2030 state->smb1.recv_iov = talloc_zero_array(state,
2033 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2034 return NT_STATUS_OK;
2038 state->smb1.recv_cmd = cmd;
2039 state->smb1.recv_status = status;
2040 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2042 state->smb1.recv_iov[0] = iov[0];
2043 state->smb1.recv_iov[1] = iov[1];
2044 state->smb1.recv_iov[2] = iov[2];
2046 if (talloc_array_length(conn->pending) == 0) {
2047 tevent_req_done(req);
2048 return NT_STATUS_OK;
2051 tevent_req_defer_callback(req, state->ev);
2052 tevent_req_done(req);
2053 return NT_STATUS_RETRY;
2056 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2057 num_chained = talloc_array_length(chain);
2058 num_responses = (num_iov - 1)/2;
2060 if (num_responses > num_chained) {
2061 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2064 for (i=0; i<num_chained; i++) {
2065 size_t iov_idx = 1 + (i*2);
2066 struct iovec *cur = &iov[iov_idx];
2070 state = tevent_req_data(req, struct smbXcli_req_state);
2072 smbXcli_req_unset_pending(req);
2075 * as we finish multiple requests here
2076 * we need to defer the callbacks as
2077 * they could destroy our current stack state.
2079 tevent_req_defer_callback(req, state->ev);
2081 if (i >= num_responses) {
2082 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2086 if (state->smb1.recv_iov == NULL) {
2088 * For requests with more than
2089 * one response, we have to readd the
2092 state->smb1.recv_iov = talloc_zero_array(state,
2095 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2100 state->smb1.recv_cmd = cmd;
2102 if (i == (num_responses - 1)) {
2104 * The last request in the chain gets the status
2106 state->smb1.recv_status = status;
2108 cmd = CVAL(cur[0].iov_base, 0);
2109 state->smb1.recv_status = NT_STATUS_OK;
2112 state->inbuf = inbuf;
2115 * Note: here we use talloc_reference() in a way
2116 * that does not expose it to the caller.
2118 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2119 if (tevent_req_nomem(inbuf_ref, req)) {
2123 /* copy the related buffers */
2124 state->smb1.recv_iov[0] = iov[0];
2125 state->smb1.recv_iov[1] = cur[0];
2126 state->smb1.recv_iov[2] = cur[1];
2128 tevent_req_done(req);
2131 return NT_STATUS_RETRY;
2134 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2135 TALLOC_CTX *mem_ctx,
2136 struct iovec **piov,
2140 uint32_t *pvwv_offset,
2141 uint32_t *pnum_bytes,
2143 uint32_t *pbytes_offset,
2145 const struct smb1cli_req_expected_response *expected,
2146 size_t num_expected)
2148 struct smbXcli_req_state *state =
2149 tevent_req_data(req,
2150 struct smbXcli_req_state);
2151 NTSTATUS status = NT_STATUS_OK;
2152 struct iovec *recv_iov = NULL;
2153 uint8_t *hdr = NULL;
2155 uint32_t vwv_offset = 0;
2156 uint16_t *vwv = NULL;
2157 uint32_t num_bytes = 0;
2158 uint32_t bytes_offset = 0;
2159 uint8_t *bytes = NULL;
2161 bool found_status = false;
2162 bool found_size = false;
2176 if (pvwv_offset != NULL) {
2179 if (pnum_bytes != NULL) {
2182 if (pbytes != NULL) {
2185 if (pbytes_offset != NULL) {
2188 if (pinbuf != NULL) {
2192 if (state->inbuf != NULL) {
2193 recv_iov = state->smb1.recv_iov;
2194 state->smb1.recv_iov = NULL;
2195 if (state->smb1.recv_cmd != SMBreadBraw) {
2196 hdr = (uint8_t *)recv_iov[0].iov_base;
2197 wct = recv_iov[1].iov_len/2;
2198 vwv = (uint16_t *)recv_iov[1].iov_base;
2199 vwv_offset = PTR_DIFF(vwv, hdr);
2200 num_bytes = recv_iov[2].iov_len;
2201 bytes = (uint8_t *)recv_iov[2].iov_base;
2202 bytes_offset = PTR_DIFF(bytes, hdr);
2206 if (tevent_req_is_nterror(req, &status)) {
2207 for (i=0; i < num_expected; i++) {
2208 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2209 found_status = true;
2215 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2221 if (num_expected == 0) {
2222 found_status = true;
2226 status = state->smb1.recv_status;
2228 for (i=0; i < num_expected; i++) {
2229 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2233 found_status = true;
2234 if (expected[i].wct == 0) {
2239 if (expected[i].wct == wct) {
2245 if (!found_status) {
2250 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2254 *piov = talloc_move(mem_ctx, &recv_iov);
2266 if (pvwv_offset != NULL) {
2267 *pvwv_offset = vwv_offset;
2269 if (pnum_bytes != NULL) {
2270 *pnum_bytes = num_bytes;
2272 if (pbytes != NULL) {
2275 if (pbytes_offset != NULL) {
2276 *pbytes_offset = bytes_offset;
2278 if (pinbuf != NULL) {
2279 *pinbuf = state->inbuf;
2285 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2292 for (i=0; i<num_reqs; i++) {
2293 struct smbXcli_req_state *state;
2294 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2295 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2296 state->smb1.iov_count-2);
2297 wct_ofs = (wct_ofs + 3) & ~3;
2302 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2304 struct smbXcli_req_state *first_state =
2305 tevent_req_data(reqs[0],
2306 struct smbXcli_req_state);
2307 struct smbXcli_req_state *state;
2309 size_t chain_padding = 0;
2311 struct iovec *iov = NULL;
2312 struct iovec *this_iov;
2316 if (num_reqs == 1) {
2317 return smb1cli_req_writev_submit(reqs[0], first_state,
2318 first_state->smb1.iov,
2319 first_state->smb1.iov_count);
2323 for (i=0; i<num_reqs; i++) {
2324 if (!tevent_req_is_in_progress(reqs[i])) {
2325 return NT_STATUS_INTERNAL_ERROR;
2328 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2330 if (state->smb1.iov_count < 4) {
2331 return NT_STATUS_INVALID_PARAMETER_MIX;
2336 * The NBT and SMB header
2349 iovlen += state->smb1.iov_count - 2;
2352 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2354 return NT_STATUS_NO_MEMORY;
2357 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2358 first_state, reqs, sizeof(*reqs) * num_reqs);
2359 if (first_state->smb1.chained_requests == NULL) {
2361 return NT_STATUS_NO_MEMORY;
2364 wct_offset = HDR_WCT;
2367 for (i=0; i<num_reqs; i++) {
2368 size_t next_padding = 0;
2371 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2373 if (i < num_reqs-1) {
2374 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2375 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2377 TALLOC_FREE(first_state->smb1.chained_requests);
2378 return NT_STATUS_INVALID_PARAMETER_MIX;
2382 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2383 state->smb1.iov_count-2) + 1;
2384 if ((wct_offset % 4) != 0) {
2385 next_padding = 4 - (wct_offset % 4);
2387 wct_offset += next_padding;
2388 vwv = state->smb1.vwv;
2390 if (i < num_reqs-1) {
2391 struct smbXcli_req_state *next_state =
2392 tevent_req_data(reqs[i+1],
2393 struct smbXcli_req_state);
2394 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2396 SSVAL(vwv+1, 0, wct_offset);
2397 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2398 /* properly end the chain */
2399 SCVAL(vwv+0, 0, 0xff);
2400 SCVAL(vwv+0, 1, 0xff);
2406 * The NBT and SMB header
2408 this_iov[0] = state->smb1.iov[0];
2409 this_iov[1] = state->smb1.iov[1];
2413 * This one is a bit subtle. We have to add
2414 * chain_padding bytes between the requests, and we
2415 * have to also include the wct field of the
2416 * subsequent requests. We use the subsequent header
2417 * for the padding, it contains the wct field in its
2420 this_iov[0].iov_len = chain_padding+1;
2421 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2422 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2423 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2428 * copy the words and bytes
2430 memcpy(this_iov, state->smb1.iov+2,
2431 sizeof(struct iovec) * (state->smb1.iov_count-2));
2432 this_iov += state->smb1.iov_count - 2;
2433 chain_padding = next_padding;
2436 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2437 if (nbt_len > first_state->conn->smb1.max_xmit) {
2439 TALLOC_FREE(first_state->smb1.chained_requests);
2440 return NT_STATUS_INVALID_PARAMETER_MIX;
2443 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2444 if (!NT_STATUS_IS_OK(status)) {
2446 TALLOC_FREE(first_state->smb1.chained_requests);
2450 return NT_STATUS_OK;
2453 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2455 return ((tevent_queue_length(conn->outgoing) != 0)
2456 || (talloc_array_length(conn->pending) != 0));
2459 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2461 if(conn->protocol >= PROTOCOL_SMB2_02) {
2462 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2465 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2468 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2470 return conn->smb2.server.capabilities;
2473 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2475 return conn->smb2.server.security_mode;
2478 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2480 return conn->smb2.server.max_trans_size;
2483 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2485 return conn->smb2.server.max_read_size;
2488 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2490 return conn->smb2.server.max_write_size;
2493 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2494 uint16_t max_credits)
2496 conn->smb2.max_credits = max_credits;
2499 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2501 static bool smb2cli_req_cancel(struct tevent_req *req)
2503 struct smbXcli_req_state *state =
2504 tevent_req_data(req,
2505 struct smbXcli_req_state);
2506 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2507 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2508 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2509 struct smbXcli_tcon *tcon = state->tcon;
2510 struct smbXcli_session *session = state->session;
2511 uint8_t *fixed = state->smb2.pad;
2512 uint16_t fixed_len = 4;
2513 struct tevent_req *subreq;
2514 struct smbXcli_req_state *substate;
2517 SSVAL(fixed, 0, 0x04);
2520 subreq = smb2cli_req_create(state, state->ev,
2528 if (subreq == NULL) {
2531 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2534 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2535 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2537 flags &= SMB2_HDR_FLAG_ASYNC;
2539 if (flags & SMB2_HDR_FLAG_ASYNC) {
2543 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2544 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2545 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2547 status = smb2cli_req_compound_submit(&subreq, 1);
2548 if (!NT_STATUS_IS_OK(status)) {
2549 TALLOC_FREE(subreq);
2553 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2558 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2560 /* we do not care about the result */
2561 TALLOC_FREE(subreq);
2564 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2565 struct tevent_context *ev,
2566 struct smbXcli_conn *conn,
2568 uint32_t additional_flags,
2569 uint32_t clear_flags,
2570 uint32_t timeout_msec,
2571 struct smbXcli_tcon *tcon,
2572 struct smbXcli_session *session,
2573 const uint8_t *fixed,
2578 struct tevent_req *req;
2579 struct smbXcli_req_state *state;
2583 bool use_channel_sequence = false;
2584 uint16_t channel_sequence = 0;
2586 req = tevent_req_create(mem_ctx, &state,
2587 struct smbXcli_req_state);
2594 state->session = session;
2597 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2598 use_channel_sequence = true;
2599 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2600 use_channel_sequence = true;
2604 uid = session->smb2->session_id;
2606 if (use_channel_sequence) {
2607 channel_sequence = session->smb2->channel_sequence;
2610 state->smb2.should_sign = session->smb2->should_sign;
2611 state->smb2.should_encrypt = session->smb2->should_encrypt;
2613 if (cmd == SMB2_OP_SESSSETUP &&
2614 session->smb2->signing_key.length != 0) {
2615 state->smb2.should_sign = true;
2618 if (cmd == SMB2_OP_SESSSETUP &&
2619 session->smb2_channel.signing_key.length == 0) {
2620 state->smb2.should_encrypt = false;
2625 tid = tcon->smb2.tcon_id;
2627 if (tcon->smb2.should_encrypt) {
2628 state->smb2.should_encrypt = true;
2632 if (state->smb2.should_encrypt) {
2633 state->smb2.should_sign = false;
2636 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2637 if (state->smb2.recv_iov == NULL) {
2642 flags |= additional_flags;
2643 flags &= ~clear_flags;
2645 state->smb2.fixed = fixed;
2646 state->smb2.fixed_len = fixed_len;
2647 state->smb2.dyn = dyn;
2648 state->smb2.dyn_len = dyn_len;
2650 if (state->smb2.should_encrypt) {
2651 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2652 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2655 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2656 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2657 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2658 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2659 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2660 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2661 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2662 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2665 case SMB2_OP_CANCEL:
2666 state->one_way = true;
2670 * If this is a dummy request, it will have
2671 * UINT64_MAX as message id.
2672 * If we send on break acknowledgement,
2673 * this gets overwritten later.
2675 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2679 if (timeout_msec > 0) {
2680 struct timeval endtime;
2682 endtime = timeval_current_ofs_msec(timeout_msec);
2683 if (!tevent_req_set_endtime(req, ev, endtime)) {
2691 void smb2cli_req_set_notify_async(struct tevent_req *req)
2693 struct smbXcli_req_state *state =
2694 tevent_req_data(req,
2695 struct smbXcli_req_state);
2697 state->smb2.notify_async = true;
2700 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2701 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2702 TALLOC_CTX *tmp_mem,
2705 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2708 struct smbXcli_req_state *state;
2709 struct tevent_req *subreq;
2711 int i, num_iov, nbt_len;
2713 const DATA_BLOB *encryption_key = NULL;
2714 uint64_t encryption_session_id = 0;
2717 * 1 for the nbt length, optional TRANSFORM
2718 * per request: HDR, fixed, dyn, padding
2719 * -1 because the last one does not need padding
2722 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2724 return NT_STATUS_NO_MEMORY;
2731 * the session of the first request that requires encryption
2732 * specifies the encryption key.
2734 for (i=0; i<num_reqs; i++) {
2735 if (!tevent_req_is_in_progress(reqs[i])) {
2736 return NT_STATUS_INTERNAL_ERROR;
2739 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2741 if (!smbXcli_conn_is_connected(state->conn)) {
2742 return NT_STATUS_CONNECTION_DISCONNECTED;
2745 if ((state->conn->protocol != PROTOCOL_NONE) &&
2746 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2747 return NT_STATUS_REVISION_MISMATCH;
2750 if (state->session == NULL) {
2754 if (!state->smb2.should_encrypt) {
2758 encryption_key = &state->session->smb2->encryption_key;
2759 if (encryption_key->length == 0) {
2760 return NT_STATUS_INVALID_PARAMETER_MIX;
2763 encryption_session_id = state->session->smb2->session_id;
2766 iov[num_iov].iov_base = state->smb2.transform;
2767 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2770 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2771 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2772 state->session->smb2->nonce_low);
2773 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2774 state->session->smb2->nonce_high);
2775 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2776 encryption_session_id);
2778 state->session->smb2->nonce_low += 1;
2779 if (state->session->smb2->nonce_low == 0) {
2780 state->session->smb2->nonce_high += 1;
2781 state->session->smb2->nonce_low += 1;
2784 nbt_len += SMB2_TF_HDR_SIZE;
2788 for (i=0; i<num_reqs; i++) {
2797 const DATA_BLOB *signing_key = NULL;
2799 if (!tevent_req_is_in_progress(reqs[i])) {
2800 return NT_STATUS_INTERNAL_ERROR;
2803 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2805 if (!smbXcli_conn_is_connected(state->conn)) {
2806 return NT_STATUS_CONNECTION_DISCONNECTED;
2809 if ((state->conn->protocol != PROTOCOL_NONE) &&
2810 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2811 return NT_STATUS_REVISION_MISMATCH;
2814 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2815 if (opcode == SMB2_OP_CANCEL) {
2819 avail = UINT64_MAX - state->conn->smb2.mid;
2821 return NT_STATUS_CONNECTION_ABORTED;
2824 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2825 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2830 charge = MAX(state->smb2.credit_charge, charge);
2832 avail = MIN(avail, state->conn->smb2.cur_credits);
2833 if (avail < charge) {
2834 return NT_STATUS_INTERNAL_ERROR;
2838 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2839 credits = state->conn->smb2.max_credits -
2840 state->conn->smb2.cur_credits;
2842 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2846 mid = state->conn->smb2.mid;
2847 state->conn->smb2.mid += charge;
2848 state->conn->smb2.cur_credits -= charge;
2850 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2851 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2853 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2854 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2857 if (state->session && encryption_key == NULL) {
2859 * We prefer the channel signing key if it is
2862 if (state->smb2.should_sign) {
2863 signing_key = &state->session->smb2_channel.signing_key;
2867 * If it is a channel binding, we already have the main
2868 * signing key and try that one.
2870 if (signing_key && signing_key->length == 0) {
2871 signing_key = &state->session->smb2->signing_key;
2875 * If we do not have any session key yet, we skip the
2876 * signing of SMB2_OP_SESSSETUP requests.
2878 if (signing_key && signing_key->length == 0) {
2884 iov[num_iov].iov_base = state->smb2.hdr;
2885 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2888 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2889 iov[num_iov].iov_len = state->smb2.fixed_len;
2892 if (state->smb2.dyn != NULL) {
2893 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2894 iov[num_iov].iov_len = state->smb2.dyn_len;
2898 reqlen = sizeof(state->smb2.hdr);
2899 reqlen += state->smb2.fixed_len;
2900 reqlen += state->smb2.dyn_len;
2902 if (i < num_reqs-1) {
2903 if ((reqlen % 8) > 0) {
2904 uint8_t pad = 8 - (reqlen % 8);
2905 iov[num_iov].iov_base = state->smb2.pad;
2906 iov[num_iov].iov_len = pad;
2910 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2913 state->smb2.encryption_session_id = encryption_session_id;
2915 if (signing_key != NULL) {
2918 status = smb2_signing_sign_pdu(*signing_key,
2919 state->session->conn->protocol,
2920 &iov[hdr_iov], num_iov - hdr_iov);
2921 if (!NT_STATUS_IS_OK(status)) {
2928 ret = smbXcli_req_set_pending(reqs[i]);
2930 return NT_STATUS_NO_MEMORY;
2934 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2935 _smb_setlen_tcp(state->length_hdr, nbt_len);
2936 iov[0].iov_base = state->length_hdr;
2937 iov[0].iov_len = sizeof(state->length_hdr);
2939 if (encryption_key != NULL) {
2941 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
2945 buf = talloc_array(iov, uint8_t, buflen);
2947 return NT_STATUS_NO_MEMORY;
2951 * We copy the buffers before encrypting them,
2952 * this is at least currently needed for the
2953 * to keep state->smb2.hdr.
2955 * Also the callers may expect there buffers
2958 for (vi = tf_iov + 1; vi < num_iov; vi++) {
2959 struct iovec *v = &iov[vi];
2960 const uint8_t *o = (const uint8_t *)v->iov_base;
2962 memcpy(buf, o, v->iov_len);
2963 v->iov_base = (void *)buf;
2967 status = smb2_signing_encrypt_pdu(*encryption_key,
2968 state->conn->protocol,
2969 &iov[tf_iov], num_iov - tf_iov);
2970 if (!NT_STATUS_IS_OK(status)) {
2975 if (state->conn->dispatch_incoming == NULL) {
2976 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2979 subreq = writev_send(state, state->ev, state->conn->outgoing,
2980 state->conn->write_fd, false, iov, num_iov);
2981 if (subreq == NULL) {
2982 return NT_STATUS_NO_MEMORY;
2984 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2985 return NT_STATUS_OK;
2988 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2990 struct smbXcli_req_state *state =
2991 tevent_req_data(req,
2992 struct smbXcli_req_state);
2994 state->smb2.credit_charge = charge;
2997 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2998 struct tevent_context *ev,
2999 struct smbXcli_conn *conn,
3001 uint32_t additional_flags,
3002 uint32_t clear_flags,
3003 uint32_t timeout_msec,
3004 struct smbXcli_tcon *tcon,
3005 struct smbXcli_session *session,
3006 const uint8_t *fixed,
3011 struct tevent_req *req;
3014 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3015 additional_flags, clear_flags,
3018 fixed, fixed_len, dyn, dyn_len);
3022 if (!tevent_req_is_in_progress(req)) {
3023 return tevent_req_post(req, ev);
3025 status = smb2cli_req_compound_submit(&req, 1);
3026 if (tevent_req_nterror(req, status)) {
3027 return tevent_req_post(req, ev);
3032 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3034 struct tevent_req *req =
3035 tevent_req_callback_data(subreq,
3037 struct smbXcli_req_state *state =
3038 tevent_req_data(req,
3039 struct smbXcli_req_state);
3043 nwritten = writev_recv(subreq, &err);
3044 TALLOC_FREE(subreq);
3045 if (nwritten == -1) {
3046 /* here, we need to notify all pending requests */
3047 NTSTATUS status = map_nt_error_from_unix_common(err);
3048 smbXcli_conn_disconnect(state->conn, status);
3053 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3056 TALLOC_CTX *mem_ctx,
3057 struct iovec **piov, int *pnum_iov)
3062 uint8_t *first_hdr = buf;
3063 size_t verified_buflen = 0;
3067 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3069 return NT_STATUS_NO_MEMORY;
3072 while (taken < buflen) {
3073 size_t len = buflen - taken;
3074 uint8_t *hdr = first_hdr + taken;
3077 size_t next_command_ofs;
3079 struct iovec *iov_tmp;
3081 if (verified_buflen > taken) {
3082 len = verified_buflen - taken;
3089 DEBUG(10, ("%d bytes left, expected at least %d\n",
3093 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3094 struct smbXcli_session *s;
3096 struct iovec tf_iov[2];
3100 if (len < SMB2_TF_HDR_SIZE) {
3101 DEBUG(10, ("%d bytes left, expected at least %d\n",
3102 (int)len, SMB2_TF_HDR_SIZE));
3106 tf_len = SMB2_TF_HDR_SIZE;
3109 hdr = first_hdr + taken;
3110 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3111 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3113 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3114 DEBUG(10, ("%d bytes left, expected at least %d\n",
3116 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3121 for (; s; s = s->next) {
3122 if (s->smb2->session_id != uid) {
3129 DEBUG(10, ("unknown session_id %llu\n",
3130 (unsigned long long)uid));
3134 tf_iov[0].iov_base = (void *)tf;
3135 tf_iov[0].iov_len = tf_len;
3136 tf_iov[1].iov_base = (void *)hdr;
3137 tf_iov[1].iov_len = enc_len;
3139 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3142 if (!NT_STATUS_IS_OK(status)) {
3147 verified_buflen = taken + enc_len;
3152 * We need the header plus the body length field
3155 if (len < SMB2_HDR_BODY + 2) {
3156 DEBUG(10, ("%d bytes left, expected at least %d\n",
3157 (int)len, SMB2_HDR_BODY));
3160 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3161 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3165 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3166 DEBUG(10, ("Got HDR len %d, expected %d\n",
3167 SVAL(hdr, 4), SMB2_HDR_BODY));
3172 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3173 body_size = SVAL(hdr, SMB2_HDR_BODY);
3175 if (next_command_ofs != 0) {
3176 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3179 if (next_command_ofs > full_size) {
3182 full_size = next_command_ofs;
3184 if (body_size < 2) {
3187 body_size &= 0xfffe;
3189 if (body_size > (full_size - SMB2_HDR_BODY)) {
3193 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3195 if (iov_tmp == NULL) {
3197 return NT_STATUS_NO_MEMORY;
3200 cur = &iov[num_iov];
3203 cur[0].iov_base = tf;
3204 cur[0].iov_len = tf_len;
3205 cur[1].iov_base = hdr;
3206 cur[1].iov_len = SMB2_HDR_BODY;
3207 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3208 cur[2].iov_len = body_size;
3209 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3210 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3216 *pnum_iov = num_iov;
3217 return NT_STATUS_OK;
3221 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3224 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3227 size_t num_pending = talloc_array_length(conn->pending);
3230 for (i=0; i<num_pending; i++) {
3231 struct tevent_req *req = conn->pending[i];
3232 struct smbXcli_req_state *state =
3233 tevent_req_data(req,
3234 struct smbXcli_req_state);
3236 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3243 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3244 TALLOC_CTX *tmp_mem,
3247 struct tevent_req *req;
3248 struct smbXcli_req_state *state = NULL;
3253 struct smbXcli_session *last_session = NULL;
3254 size_t inbuf_len = smb_len_tcp(inbuf);
3256 status = smb2cli_inbuf_parse_compound(conn,
3257 inbuf + NBT_HDR_SIZE,
3261 if (!NT_STATUS_IS_OK(status)) {
3265 for (i=0; i<num_iov; i+=4) {
3266 uint8_t *inbuf_ref = NULL;
3267 struct iovec *cur = &iov[i];
3268 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3269 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3270 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3271 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3272 uint16_t req_opcode;
3274 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3275 uint32_t new_credits;
3276 struct smbXcli_session *session = NULL;
3277 const DATA_BLOB *signing_key = NULL;
3278 bool was_encrypted = false;
3280 new_credits = conn->smb2.cur_credits;
3281 new_credits += credits;
3282 if (new_credits > UINT16_MAX) {
3283 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3285 conn->smb2.cur_credits += credits;
3287 req = smb2cli_conn_find_pending(conn, mid);
3289 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3291 state = tevent_req_data(req, struct smbXcli_req_state);
3293 state->smb2.got_async = false;
3295 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3296 if (opcode != req_opcode) {
3297 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3299 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3301 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3302 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3305 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3306 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3307 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3308 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3311 * async interim responses are not signed,
3312 * even if the SMB2_HDR_FLAG_SIGNED flag
3315 req_flags |= SMB2_HDR_FLAG_ASYNC;
3316 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3317 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3319 if (state->smb2.notify_async) {
3320 state->smb2.got_async = true;
3321 tevent_req_defer_callback(req, state->ev);
3322 tevent_req_notify_callback(req);
3327 session = state->session;
3328 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3329 session = last_session;
3331 last_session = session;
3333 if (state->smb2.should_sign) {
3334 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3335 return NT_STATUS_ACCESS_DENIED;
3339 if (flags & SMB2_HDR_FLAG_SIGNED) {
3340 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3342 if (session == NULL) {
3343 struct smbXcli_session *s;
3345 s = state->conn->sessions;
3346 for (; s; s = s->next) {
3347 if (s->smb2->session_id != uid) {
3356 if (session == NULL) {
3357 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3360 last_session = session;
3361 signing_key = &session->smb2_channel.signing_key;
3364 if (opcode == SMB2_OP_SESSSETUP) {
3366 * We prefer the channel signing key, if it is
3369 * If we do not have a channel signing key yet,
3370 * we try the main signing key, if it is not
3371 * the final response.
3373 if (signing_key && signing_key->length == 0 &&
3374 !NT_STATUS_IS_OK(status)) {
3375 signing_key = &session->smb2->signing_key;
3378 if (signing_key && signing_key->length == 0) {
3380 * If we do not have a session key to
3381 * verify the signature, we defer the
3382 * signing check to the caller.
3384 * The caller gets NT_STATUS_OK, it
3386 * smb2cli_session_set_session_key()
3388 * smb2cli_session_set_channel_key()
3389 * which will check the signature
3390 * with the channel signing key.
3396 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3397 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3398 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3401 * If the response was encrypted in a SMB2_TRANSFORM
3402 * pdu, which belongs to the correct session,
3403 * we do not need to do signing checks
3405 * It could be the session the response belongs to
3406 * or the session that was used to encrypt the
3407 * SMB2_TRANSFORM request.
3409 if ((session && session->smb2->session_id == uid) ||
3410 (state->smb2.encryption_session_id == uid)) {
3412 was_encrypted = true;
3416 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3418 * if the server returns NT_STATUS_USER_SESSION_DELETED
3419 * the response is not signed and we should
3420 * propagate the NT_STATUS_USER_SESSION_DELETED
3421 * status to the caller.
3423 state->smb2.signing_skipped = true;
3427 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3429 * if the server returns
3430 * NT_STATUS_INVALID_PARAMETER
3431 * the response might not be encrypted.
3433 if (state->smb2.should_encrypt && !was_encrypted) {
3434 state->smb2.signing_skipped = true;
3439 if (state->smb2.should_encrypt && !was_encrypted) {
3440 if (!state->smb2.signing_skipped) {
3441 return NT_STATUS_ACCESS_DENIED;
3445 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3446 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3447 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3449 * if the server returns
3450 * NT_STATUS_NETWORK_NAME_DELETED
3451 * NT_STATUS_FILE_CLOSED
3452 * NT_STATUS_INVALID_PARAMETER
3453 * the response might not be signed
3454 * as this happens before the signing checks.
3456 * If server echos the signature (or all zeros)
3457 * we should report the status from the server
3463 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3464 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3467 state->smb2.signing_skipped = true;
3473 static const uint8_t zeros[16];
3475 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3479 state->smb2.signing_skipped = true;
3486 status = smb2_signing_check_pdu(*signing_key,
3487 state->conn->protocol,
3489 if (!NT_STATUS_IS_OK(status)) {
3491 * If the signing check fails, we disconnect
3498 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3499 (session != NULL) && session->disconnect_expired)
3502 * this should be a short term hack
3503 * until the upper layers have implemented
3504 * re-authentication.
3509 smbXcli_req_unset_pending(req);
3512 * There might be more than one response
3513 * we need to defer the notifications
3515 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3520 tevent_req_defer_callback(req, state->ev);
3524 * Note: here we use talloc_reference() in a way
3525 * that does not expose it to the caller.
3527 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3528 if (tevent_req_nomem(inbuf_ref, req)) {
3532 /* copy the related buffers */
3533 state->smb2.recv_iov[0] = cur[1];
3534 state->smb2.recv_iov[1] = cur[2];
3535 state->smb2.recv_iov[2] = cur[3];
3537 tevent_req_done(req);
3541 return NT_STATUS_RETRY;
3544 return NT_STATUS_OK;
3547 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3548 struct iovec **piov,
3549 const struct smb2cli_req_expected_response *expected,
3550 size_t num_expected)
3552 struct smbXcli_req_state *state =
3553 tevent_req_data(req,
3554 struct smbXcli_req_state);
3557 bool found_status = false;
3558 bool found_size = false;
3565 if (state->smb2.got_async) {
3566 return STATUS_PENDING;
3569 if (tevent_req_is_nterror(req, &status)) {
3570 for (i=0; i < num_expected; i++) {
3571 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3572 found_status = true;
3578 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3584 if (num_expected == 0) {
3585 found_status = true;
3589 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3590 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3592 for (i=0; i < num_expected; i++) {
3593 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3597 found_status = true;
3598 if (expected[i].body_size == 0) {
3603 if (expected[i].body_size == body_size) {
3609 if (!found_status) {
3613 if (state->smb2.signing_skipped) {
3614 if (num_expected > 0) {
3615 return NT_STATUS_ACCESS_DENIED;
3617 if (!NT_STATUS_IS_ERR(status)) {
3618 return NT_STATUS_ACCESS_DENIED;
3623 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3627 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3633 static const struct {
3634 enum protocol_types proto;
3635 const char *smb1_name;
3636 } smb1cli_prots[] = {
3637 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3638 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3639 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3640 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3641 {PROTOCOL_LANMAN2, "LM1.2X002"},
3642 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3643 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3644 {PROTOCOL_LANMAN2, "Samba"},
3645 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3646 {PROTOCOL_NT1, "NT LM 0.12"},
3647 {PROTOCOL_SMB2_02, "SMB 2.002"},
3648 {PROTOCOL_SMB2_10, "SMB 2.???"},
3651 static const struct {
3652 enum protocol_types proto;
3653 uint16_t smb2_dialect;
3654 } smb2cli_prots[] = {
3655 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3656 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3657 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3658 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3659 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3662 struct smbXcli_negprot_state {
3663 struct smbXcli_conn *conn;
3664 struct tevent_context *ev;
3665 uint32_t timeout_msec;
3666 enum protocol_types min_protocol;
3667 enum protocol_types max_protocol;
3671 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3675 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3676 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3677 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3678 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3679 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3680 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3684 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3685 struct tevent_context *ev,
3686 struct smbXcli_conn *conn,
3687 uint32_t timeout_msec,
3688 enum protocol_types min_protocol,
3689 enum protocol_types max_protocol)
3691 struct tevent_req *req, *subreq;
3692 struct smbXcli_negprot_state *state;
3694 req = tevent_req_create(mem_ctx, &state,
3695 struct smbXcli_negprot_state);
3701 state->timeout_msec = timeout_msec;
3702 state->min_protocol = min_protocol;
3703 state->max_protocol = max_protocol;
3705 if (min_protocol == PROTOCOL_NONE) {
3706 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3707 return tevent_req_post(req, ev);
3710 if (max_protocol == PROTOCOL_NONE) {
3711 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3712 return tevent_req_post(req, ev);
3715 if (min_protocol > max_protocol) {
3716 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3717 return tevent_req_post(req, ev);
3720 if ((min_protocol < PROTOCOL_SMB2_02) &&
3721 (max_protocol < PROTOCOL_SMB2_02)) {
3725 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3727 subreq = smbXcli_negprot_smb1_subreq(state);
3728 if (tevent_req_nomem(subreq, req)) {
3729 return tevent_req_post(req, ev);
3731 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3735 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3736 (max_protocol >= PROTOCOL_SMB2_02)) {
3740 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3742 subreq = smbXcli_negprot_smb2_subreq(state);
3743 if (tevent_req_nomem(subreq, req)) {
3744 return tevent_req_post(req, ev);
3746 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3751 * We send an SMB1 negprot with the SMB2 dialects
3752 * and expect a SMB1 or a SMB2 response.
3754 * smbXcli_negprot_dispatch_incoming() will fix the
3755 * callback to match protocol of the response.
3757 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3759 subreq = smbXcli_negprot_smb1_subreq(state);
3760 if (tevent_req_nomem(subreq, req)) {
3761 return tevent_req_post(req, ev);
3763 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3767 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3769 struct tevent_req *req =
3770 tevent_req_callback_data(subreq,
3775 * we just want the low level error
3777 status = tevent_req_simple_recv_ntstatus(subreq);
3778 TALLOC_FREE(subreq);
3779 if (tevent_req_nterror(req, status)) {
3783 /* this should never happen */
3784 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3787 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3790 DATA_BLOB bytes = data_blob_null;
3794 /* setup the protocol strings */
3795 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3799 if (smb1cli_prots[i].proto < state->min_protocol) {
3803 if (smb1cli_prots[i].proto > state->max_protocol) {
3807 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3813 * We now it is already ascii and
3814 * we want NULL termination.
3816 ok = data_blob_append(state, &bytes,
3817 smb1cli_prots[i].smb1_name,
3818 strlen(smb1cli_prots[i].smb1_name)+1);
3824 smb1cli_req_flags(state->max_protocol,
3825 state->conn->smb1.client.capabilities,
3830 return smb1cli_req_send(state, state->ev, state->conn,
3834 state->timeout_msec,
3835 0xFFFE, 0, NULL, /* pid, tid, session */
3836 0, NULL, /* wct, vwv */
3837 bytes.length, bytes.data);
3840 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3842 struct tevent_req *req =
3843 tevent_req_callback_data(subreq,
3845 struct smbXcli_negprot_state *state =
3846 tevent_req_data(req,
3847 struct smbXcli_negprot_state);
3848 struct smbXcli_conn *conn = state->conn;
3849 struct iovec *recv_iov = NULL;
3858 size_t num_prots = 0;
3860 uint32_t client_capabilities = conn->smb1.client.capabilities;
3861 uint32_t both_capabilities;
3862 uint32_t server_capabilities = 0;
3863 uint32_t capabilities;
3864 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3865 uint32_t server_max_xmit = 0;
3867 uint32_t server_max_mux = 0;
3868 uint16_t server_security_mode = 0;
3869 uint32_t server_session_key = 0;
3870 bool server_readbraw = false;
3871 bool server_writebraw = false;
3872 bool server_lockread = false;
3873 bool server_writeunlock = false;
3874 struct GUID server_guid = GUID_zero();
3875 DATA_BLOB server_gss_blob = data_blob_null;
3876 uint8_t server_challenge[8];
3877 char *server_workgroup = NULL;
3878 char *server_name = NULL;
3879 int server_time_zone = 0;
3880 NTTIME server_system_time = 0;
3881 static const struct smb1cli_req_expected_response expected[] = {
3883 .status = NT_STATUS_OK,
3884 .wct = 0x11, /* NT1 */
3887 .status = NT_STATUS_OK,
3888 .wct = 0x0D, /* LM */
3891 .status = NT_STATUS_OK,
3892 .wct = 0x01, /* CORE */
3896 ZERO_STRUCT(server_challenge);
3898 status = smb1cli_req_recv(subreq, state,
3903 NULL, /* pvwv_offset */
3906 NULL, /* pbytes_offset */
3908 expected, ARRAY_SIZE(expected));
3909 TALLOC_FREE(subreq);
3910 if (tevent_req_nterror(req, status)) {
3914 flags = CVAL(inhdr, HDR_FLG);
3916 protnum = SVAL(vwv, 0);
3918 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3919 if (smb1cli_prots[i].proto < state->min_protocol) {
3923 if (smb1cli_prots[i].proto > state->max_protocol) {
3927 if (protnum != num_prots) {
3932 conn->protocol = smb1cli_prots[i].proto;
3936 if (conn->protocol == PROTOCOL_NONE) {
3937 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3941 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3942 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3943 "and the selected protocol level doesn't support it.\n"));
3944 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3948 if (flags & FLAG_SUPPORT_LOCKREAD) {
3949 server_lockread = true;
3950 server_writeunlock = true;
3953 if (conn->protocol >= PROTOCOL_NT1) {
3954 const char *client_signing = NULL;
3955 bool server_mandatory = false;
3956 bool server_allowed = false;
3957 const char *server_signing = NULL;
3962 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3967 server_security_mode = CVAL(vwv + 1, 0);
3968 server_max_mux = SVAL(vwv + 1, 1);
3969 server_max_xmit = IVAL(vwv + 3, 1);
3970 server_session_key = IVAL(vwv + 7, 1);
3971 server_time_zone = SVALS(vwv + 15, 1);
3972 server_time_zone *= 60;
3973 /* this time arrives in real GMT */
3974 server_system_time = BVAL(vwv + 11, 1);
3975 server_capabilities = IVAL(vwv + 9, 1);
3977 key_len = CVAL(vwv + 16, 1);
3979 if (server_capabilities & CAP_RAW_MODE) {
3980 server_readbraw = true;
3981 server_writebraw = true;
3983 if (server_capabilities & CAP_LOCK_AND_READ) {
3984 server_lockread = true;
3987 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3988 DATA_BLOB blob1, blob2;
3990 if (num_bytes < 16) {
3991 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3995 blob1 = data_blob_const(bytes, 16);
3996 status = GUID_from_data_blob(&blob1, &server_guid);
3997 if (tevent_req_nterror(req, status)) {
4001 blob1 = data_blob_const(bytes+16, num_bytes-16);
4002 blob2 = data_blob_dup_talloc(state, blob1);
4003 if (blob1.length > 0 &&
4004 tevent_req_nomem(blob2.data, req)) {
4007 server_gss_blob = blob2;
4009 DATA_BLOB blob1, blob2;
4011 if (num_bytes < key_len) {
4012 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4016 if (key_len != 0 && key_len != 8) {
4017 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4022 memcpy(server_challenge, bytes, 8);
4025 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4026 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4027 if (blob1.length > 0) {
4030 len = utf16_len_n(blob1.data,
4034 ok = convert_string_talloc(state,
4042 status = map_nt_error_from_unix_common(errno);
4043 tevent_req_nterror(req, status);
4048 blob2.data += blob1.length;
4049 blob2.length -= blob1.length;
4050 if (blob2.length > 0) {
4053 len = utf16_len_n(blob1.data,
4057 ok = convert_string_talloc(state,
4065 status = map_nt_error_from_unix_common(errno);
4066 tevent_req_nterror(req, status);
4072 client_signing = "disabled";
4073 if (conn->allow_signing) {
4074 client_signing = "allowed";
4076 if (conn->mandatory_signing) {
4077 client_signing = "required";
4080 server_signing = "not supported";
4081 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4082 server_signing = "supported";
4083 server_allowed = true;
4084 } else if (conn->mandatory_signing) {
4086 * We have mandatory signing as client
4087 * lets assume the server will look at our
4088 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4089 * flag in the session setup
4091 server_signing = "not announced";
4092 server_allowed = true;
4094 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4095 server_signing = "required";
4096 server_mandatory = true;
4099 ok = smb_signing_set_negotiated(conn->smb1.signing,
4103 DEBUG(1,("cli_negprot: SMB signing is required, "
4104 "but client[%s] and server[%s] mismatch\n",
4105 client_signing, server_signing));
4106 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4110 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4116 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4120 server_security_mode = SVAL(vwv + 1, 0);
4121 server_max_xmit = SVAL(vwv + 2, 0);
4122 server_max_mux = SVAL(vwv + 3, 0);
4123 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4124 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4125 server_session_key = IVAL(vwv + 6, 0);
4126 server_time_zone = SVALS(vwv + 10, 0);
4127 server_time_zone *= 60;
4128 /* this time is converted to GMT by make_unix_date */
4129 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4130 unix_to_nt_time(&server_system_time, t);
4131 key_len = SVAL(vwv + 11, 0);
4133 if (num_bytes < key_len) {
4134 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4138 if (key_len != 0 && key_len != 8) {
4139 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4144 memcpy(server_challenge, bytes, 8);
4147 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4148 if (blob1.length > 0) {
4152 len = utf16_len_n(blob1.data,
4156 ok = convert_string_talloc(state,
4164 status = map_nt_error_from_unix_common(errno);
4165 tevent_req_nterror(req, status);
4171 /* the old core protocol */
4172 server_time_zone = get_time_zone(time(NULL));
4173 server_max_xmit = 1024;
4177 if (server_max_xmit < 1024) {
4178 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4182 if (server_max_mux < 1) {
4183 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4188 * Now calculate the negotiated capabilities
4189 * based on the mask for:
4190 * - client only flags
4191 * - flags used in both directions
4192 * - server only flags
4194 both_capabilities = client_capabilities & server_capabilities;
4195 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4196 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4197 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4199 max_xmit = MIN(client_max_xmit, server_max_xmit);
4201 conn->smb1.server.capabilities = server_capabilities;
4202 conn->smb1.capabilities = capabilities;
4204 conn->smb1.server.max_xmit = server_max_xmit;
4205 conn->smb1.max_xmit = max_xmit;
4207 conn->smb1.server.max_mux = server_max_mux;
4209 conn->smb1.server.security_mode = server_security_mode;
4211 conn->smb1.server.readbraw = server_readbraw;
4212 conn->smb1.server.writebraw = server_writebraw;
4213 conn->smb1.server.lockread = server_lockread;
4214 conn->smb1.server.writeunlock = server_writeunlock;
4216 conn->smb1.server.session_key = server_session_key;
4218 talloc_steal(conn, server_gss_blob.data);
4219 conn->smb1.server.gss_blob = server_gss_blob;
4220 conn->smb1.server.guid = server_guid;
4221 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4222 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4223 conn->smb1.server.name = talloc_move(conn, &server_name);
4225 conn->smb1.server.time_zone = server_time_zone;
4226 conn->smb1.server.system_time = server_system_time;
4228 tevent_req_done(req);
4231 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4235 uint16_t dialect_count = 0;
4237 buf = state->smb2.dyn;
4238 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4239 if (smb2cli_prots[i].proto < state->min_protocol) {
4243 if (smb2cli_prots[i].proto > state->max_protocol) {
4247 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4251 buf = state->smb2.fixed;
4253 SSVAL(buf, 2, dialect_count);
4254 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4255 SSVAL(buf, 6, 0); /* Reserved */
4256 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4257 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4259 SIVAL(buf, 8, 0); /* Capabilities */
4261 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4265 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4267 if (!NT_STATUS_IS_OK(status)) {
4270 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4272 memset(buf+12, 0, 16); /* ClientGuid */
4274 SBVAL(buf, 28, 0); /* ClientStartTime */
4276 return smb2cli_req_send(state, state->ev,
4277 state->conn, SMB2_OP_NEGPROT,
4279 state->timeout_msec,
4280 NULL, NULL, /* tcon, session */
4281 state->smb2.fixed, sizeof(state->smb2.fixed),
4282 state->smb2.dyn, dialect_count*2);
4285 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4287 struct tevent_req *req =
4288 tevent_req_callback_data(subreq,
4290 struct smbXcli_negprot_state *state =
4291 tevent_req_data(req,
4292 struct smbXcli_negprot_state);
4293 struct smbXcli_conn *conn = state->conn;
4294 size_t security_offset, security_length;
4300 uint16_t dialect_revision;
4301 static const struct smb2cli_req_expected_response expected[] = {
4303 .status = NT_STATUS_OK,
4308 status = smb2cli_req_recv(subreq, state, &iov,
4309 expected, ARRAY_SIZE(expected));
4310 TALLOC_FREE(subreq);
4311 if (tevent_req_nterror(req, status)) {
4315 body = (uint8_t *)iov[1].iov_base;
4317 dialect_revision = SVAL(body, 4);
4319 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4320 if (smb2cli_prots[i].proto < state->min_protocol) {
4324 if (smb2cli_prots[i].proto > state->max_protocol) {
4328 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4332 conn->protocol = smb2cli_prots[i].proto;
4336 if (conn->protocol == PROTOCOL_NONE) {
4337 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4338 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4342 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4343 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4347 /* make sure we do not loop forever */
4348 state->min_protocol = PROTOCOL_SMB2_02;
4351 * send a SMB2 negprot, in order to negotiate
4354 subreq = smbXcli_negprot_smb2_subreq(state);
4355 if (tevent_req_nomem(subreq, req)) {
4358 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4362 conn->smb2.server.security_mode = SVAL(body, 2);
4364 blob = data_blob_const(body + 8, 16);
4365 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4366 if (tevent_req_nterror(req, status)) {
4370 conn->smb2.server.capabilities = IVAL(body, 24);
4371 conn->smb2.server.max_trans_size= IVAL(body, 28);
4372 conn->smb2.server.max_read_size = IVAL(body, 32);
4373 conn->smb2.server.max_write_size= IVAL(body, 36);
4374 conn->smb2.server.system_time = BVAL(body, 40);
4375 conn->smb2.server.start_time = BVAL(body, 48);
4377 security_offset = SVAL(body, 56);
4378 security_length = SVAL(body, 58);
4380 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4381 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4385 if (security_length > iov[2].iov_len) {
4386 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4390 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4393 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4397 tevent_req_done(req);
4400 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4401 TALLOC_CTX *tmp_mem,
4404 size_t num_pending = talloc_array_length(conn->pending);
4405 struct tevent_req *subreq;
4406 struct smbXcli_req_state *substate;
4407 struct tevent_req *req;
4408 uint32_t protocol_magic;
4409 size_t inbuf_len = smb_len_nbt(inbuf);
4411 if (num_pending != 1) {
4412 return NT_STATUS_INTERNAL_ERROR;
4415 if (inbuf_len < 4) {
4416 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4419 subreq = conn->pending[0];
4420 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4421 req = tevent_req_callback_data(subreq, struct tevent_req);
4423 protocol_magic = IVAL(inbuf, 4);
4425 switch (protocol_magic) {
4427 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4428 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4429 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4432 if (substate->smb2.recv_iov == NULL) {
4434 * For the SMB1 negprot we have move it.
4436 substate->smb2.recv_iov = substate->smb1.recv_iov;
4437 substate->smb1.recv_iov = NULL;
4441 * we got an SMB2 answer, which consumed sequence number 0
4442 * so we need to use 1 as the next one
4445 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4446 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4447 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4450 DEBUG(10, ("Got non-SMB PDU\n"));
4451 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4454 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4456 return tevent_req_simple_recv_ntstatus(req);
4459 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4460 uint32_t timeout_msec,
4461 enum protocol_types min_protocol,
4462 enum protocol_types max_protocol)
4464 TALLOC_CTX *frame = talloc_stackframe();
4465 struct tevent_context *ev;
4466 struct tevent_req *req;
4467 NTSTATUS status = NT_STATUS_NO_MEMORY;
4470 if (smbXcli_conn_has_async_calls(conn)) {
4472 * Can't use sync call while an async call is in flight
4474 status = NT_STATUS_INVALID_PARAMETER_MIX;
4477 ev = samba_tevent_context_init(frame);
4481 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4482 min_protocol, max_protocol);
4486 ok = tevent_req_poll(req, ev);
4488 status = map_nt_error_from_unix_common(errno);
4491 status = smbXcli_negprot_recv(req);
4497 static int smbXcli_session_destructor(struct smbXcli_session *session)
4499 if (session->conn == NULL) {
4503 DLIST_REMOVE(session->conn->sessions, session);
4507 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4508 struct smbXcli_conn *conn)
4510 struct smbXcli_session *session;
4512 session = talloc_zero(mem_ctx, struct smbXcli_session);
4513 if (session == NULL) {
4516 session->smb2 = talloc_zero(session, struct smb2cli_session);
4517 if (session->smb2 == NULL) {
4518 talloc_free(session);
4521 talloc_set_destructor(session, smbXcli_session_destructor);
4523 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4524 session->conn = conn;
4529 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
4530 struct smbXcli_session *src)
4532 struct smbXcli_session *session;
4534 session = talloc_zero(mem_ctx, struct smbXcli_session);
4535 if (session == NULL) {
4538 session->smb2 = talloc_zero(session, struct smb2cli_session);
4539 if (session->smb2 == NULL) {
4540 talloc_free(session);
4544 session->conn = src->conn;
4545 *session->smb2 = *src->smb2;
4546 session->smb2_channel = src->smb2_channel;
4547 session->disconnect_expired = src->disconnect_expired;
4549 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
4550 talloc_set_destructor(session, smbXcli_session_destructor);
4556 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4557 TALLOC_CTX *mem_ctx,
4560 const DATA_BLOB *application_key;
4562 *key = data_blob_null;
4564 if (session->conn == NULL) {
4565 return NT_STATUS_NO_USER_SESSION_KEY;
4568 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4569 application_key = &session->smb2->application_key;
4571 application_key = &session->smb1.application_key;
4574 if (application_key->length == 0) {
4575 return NT_STATUS_NO_USER_SESSION_KEY;
4578 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4579 if (key->data == NULL) {
4580 return NT_STATUS_NO_MEMORY;
4583 return NT_STATUS_OK;
4586 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
4588 session->disconnect_expired = true;
4591 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4593 return session->smb1.session_id;
4596 void smb1cli_session_set_id(struct smbXcli_session *session,
4597 uint16_t session_id)
4599 session->smb1.session_id = session_id;
4602 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4603 const DATA_BLOB _session_key)
4605 struct smbXcli_conn *conn = session->conn;
4606 uint8_t session_key[16];
4609 return NT_STATUS_INVALID_PARAMETER_MIX;
4612 if (session->smb1.application_key.length != 0) {
4614 * TODO: do not allow this...
4616 * return NT_STATUS_INVALID_PARAMETER_MIX;
4618 data_blob_clear_free(&session->smb1.application_key);
4619 session->smb1.protected_key = false;
4622 if (_session_key.length == 0) {
4623 return NT_STATUS_OK;
4626 ZERO_STRUCT(session_key);
4627 memcpy(session_key, _session_key.data,
4628 MIN(_session_key.length, sizeof(session_key)));
4630 session->smb1.application_key = data_blob_talloc(session,
4632 sizeof(session_key));
4633 ZERO_STRUCT(session_key);
4634 if (session->smb1.application_key.data == NULL) {
4635 return NT_STATUS_NO_MEMORY;
4638 session->smb1.protected_key = false;
4640 return NT_STATUS_OK;
4643 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4645 if (session->smb1.protected_key) {
4646 /* already protected */
4647 return NT_STATUS_OK;
4650 if (session->smb1.application_key.length != 16) {
4651 return NT_STATUS_INVALID_PARAMETER_MIX;
4654 smb_key_derivation(session->smb1.application_key.data,
4655 session->smb1.application_key.length,
4656 session->smb1.application_key.data);
4658 session->smb1.protected_key = true;
4660 return NT_STATUS_OK;
4663 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4665 struct smbXcli_conn *conn = session->conn;
4666 uint8_t security_mode = 0;
4669 return security_mode;
4672 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4673 if (conn->mandatory_signing) {
4674 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4677 return security_mode;
4680 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4682 return session->smb2->session_id;
4685 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4687 return session->smb2->session_flags;
4690 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4691 uint64_t session_id,
4692 uint16_t session_flags)
4694 session->smb2->session_id = session_id;
4695 session->smb2->session_flags = session_flags;
4698 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4700 session->smb2->channel_sequence += 1;
4703 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4704 const DATA_BLOB _session_key,
4705 const struct iovec *recv_iov)
4707 struct smbXcli_conn *conn = session->conn;
4708 uint16_t no_sign_flags;
4709 uint8_t session_key[16];
4713 return NT_STATUS_INVALID_PARAMETER_MIX;
4716 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4718 if (session->smb2->session_flags & no_sign_flags) {
4719 session->smb2->should_sign = false;
4720 return NT_STATUS_OK;
4723 if (session->smb2->signing_key.length != 0) {
4724 return NT_STATUS_INVALID_PARAMETER_MIX;
4727 ZERO_STRUCT(session_key);
4728 memcpy(session_key, _session_key.data,
4729 MIN(_session_key.length, sizeof(session_key)));
4731 session->smb2->signing_key = data_blob_talloc(session,
4733 sizeof(session_key));
4734 if (session->smb2->signing_key.data == NULL) {
4735 ZERO_STRUCT(session_key);
4736 return NT_STATUS_NO_MEMORY;
4739 if (conn->protocol >= PROTOCOL_SMB2_24) {
4740 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4741 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4743 smb2_key_derivation(session_key, sizeof(session_key),
4744 label.data, label.length,
4745 context.data, context.length,
4746 session->smb2->signing_key.data);
4749 session->smb2->encryption_key = data_blob_dup_talloc(session,
4750 session->smb2->signing_key);
4751 if (session->smb2->encryption_key.data == NULL) {
4752 ZERO_STRUCT(session_key);
4753 return NT_STATUS_NO_MEMORY;
4756 if (conn->protocol >= PROTOCOL_SMB2_24) {
4757 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4758 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4760 smb2_key_derivation(session_key, sizeof(session_key),
4761 label.data, label.length,
4762 context.data, context.length,
4763 session->smb2->encryption_key.data);
4766 session->smb2->decryption_key = data_blob_dup_talloc(session,
4767 session->smb2->signing_key);
4768 if (session->smb2->decryption_key.data == NULL) {
4769 ZERO_STRUCT(session_key);
4770 return NT_STATUS_NO_MEMORY;
4773 if (conn->protocol >= PROTOCOL_SMB2_24) {
4774 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4775 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4777 smb2_key_derivation(session_key, sizeof(session_key),
4778 label.data, label.length,
4779 context.data, context.length,
4780 session->smb2->decryption_key.data);
4783 session->smb2->application_key = data_blob_dup_talloc(session,
4784 session->smb2->signing_key);
4785 if (session->smb2->application_key.data == NULL) {
4786 ZERO_STRUCT(session_key);
4787 return NT_STATUS_NO_MEMORY;
4790 if (conn->protocol >= PROTOCOL_SMB2_24) {
4791 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4792 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4794 smb2_key_derivation(session_key, sizeof(session_key),
4795 label.data, label.length,
4796 context.data, context.length,
4797 session->smb2->application_key.data);
4799 ZERO_STRUCT(session_key);
4801 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4802 session->smb2->signing_key);
4803 if (session->smb2_channel.signing_key.data == NULL) {
4804 return NT_STATUS_NO_MEMORY;
4807 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4808 session->conn->protocol,
4810 if (!NT_STATUS_IS_OK(status)) {
4814 session->smb2->should_sign = false;
4815 session->smb2->should_encrypt = false;
4817 if (conn->desire_signing) {
4818 session->smb2->should_sign = true;
4821 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4822 session->smb2->should_sign = true;
4825 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4826 session->smb2->should_encrypt = true;
4829 if (conn->protocol < PROTOCOL_SMB2_24) {
4830 session->smb2->should_encrypt = false;
4833 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4834 session->smb2->should_encrypt = false;
4837 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4838 sizeof(session->smb2->nonce_high));
4839 session->smb2->nonce_low = 1;
4841 return NT_STATUS_OK;
4844 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4845 struct smbXcli_session *session1,
4846 struct smbXcli_conn *conn,
4847 struct smbXcli_session **_session2)
4849 struct smbXcli_session *session2;
4851 if (session1->smb2->signing_key.length == 0) {
4852 return NT_STATUS_INVALID_PARAMETER_MIX;
4856 return NT_STATUS_INVALID_PARAMETER_MIX;
4859 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4860 if (session2 == NULL) {
4861 return NT_STATUS_NO_MEMORY;
4863 session2->smb2 = talloc_reference(session2, session1->smb2);
4864 if (session2->smb2 == NULL) {
4865 talloc_free(session2);
4866 return NT_STATUS_NO_MEMORY;
4869 talloc_set_destructor(session2, smbXcli_session_destructor);
4870 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4871 session2->conn = conn;
4873 *_session2 = session2;
4874 return NT_STATUS_OK;
4877 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4878 const DATA_BLOB _channel_key,
4879 const struct iovec *recv_iov)
4881 struct smbXcli_conn *conn = session->conn;
4882 uint8_t channel_key[16];
4886 return NT_STATUS_INVALID_PARAMETER_MIX;
4889 if (session->smb2_channel.signing_key.length != 0) {
4890 return NT_STATUS_INVALID_PARAMETER_MIX;
4893 ZERO_STRUCT(channel_key);
4894 memcpy(channel_key, _channel_key.data,
4895 MIN(_channel_key.length, sizeof(channel_key)));
4897 session->smb2_channel.signing_key = data_blob_talloc(session,
4899 sizeof(channel_key));
4900 if (session->smb2_channel.signing_key.data == NULL) {
4901 ZERO_STRUCT(channel_key);
4902 return NT_STATUS_NO_MEMORY;
4905 if (conn->protocol >= PROTOCOL_SMB2_24) {
4906 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4907 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4909 smb2_key_derivation(channel_key, sizeof(channel_key),
4910 label.data, label.length,
4911 context.data, context.length,
4912 session->smb2_channel.signing_key.data);
4914 ZERO_STRUCT(channel_key);
4916 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4917 session->conn->protocol,
4919 if (!NT_STATUS_IS_OK(status)) {
4923 return NT_STATUS_OK;
4926 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4928 struct smbXcli_tcon *tcon;
4930 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4938 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4940 return tcon->smb1.tcon_id;
4943 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4945 tcon->smb1.tcon_id = tcon_id;
4948 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
4950 uint16_t optional_support,
4951 uint32_t maximal_access,
4952 uint32_t guest_maximal_access,
4953 const char *service,
4954 const char *fs_type)
4956 tcon->smb1.tcon_id = tcon_id;
4957 tcon->smb1.optional_support = optional_support;
4958 tcon->smb1.maximal_access = maximal_access;
4959 tcon->smb1.guest_maximal_access = guest_maximal_access;
4961 TALLOC_FREE(tcon->smb1.service);
4962 tcon->smb1.service = talloc_strdup(tcon, service);
4963 if (service != NULL && tcon->smb1.service == NULL) {
4967 TALLOC_FREE(tcon->smb1.fs_type);
4968 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
4969 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
4976 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
4978 return tcon->smb2.tcon_id;
4981 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
4983 return tcon->smb2.capabilities;
4986 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
4987 struct smbXcli_session *session,
4991 uint32_t capabilities,
4992 uint32_t maximal_access)
4994 tcon->smb2.tcon_id = tcon_id;
4995 tcon->smb2.type = type;
4996 tcon->smb2.flags = flags;
4997 tcon->smb2.capabilities = capabilities;
4998 tcon->smb2.maximal_access = maximal_access;
5000 tcon->smb2.should_encrypt = false;
5002 if (session == NULL) {
5006 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5008 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5009 tcon->smb2.should_encrypt = true;