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 "lib/util/iov_buf.h"
29 #include "../libcli/smb/smb_common.h"
30 #include "../libcli/smb/smb_seal.h"
31 #include "../libcli/smb/smb_signing.h"
32 #include "../libcli/smb/read_smb.h"
33 #include "smbXcli_base.h"
34 #include "librpc/ndr/libndr.h"
35 #include "libcli/smb/smb2_negotiate_context.h"
36 #include "lib/crypto/sha512.h"
37 #include "lib/crypto/aes.h"
38 #include "lib/crypto/aes_ccm_128.h"
39 #include "lib/crypto/aes_gcm_128.h"
43 struct smbXcli_session;
48 struct sockaddr_storage local_ss;
49 struct sockaddr_storage remote_ss;
50 const char *remote_name;
52 struct tevent_queue *outgoing;
53 struct tevent_req **pending;
54 struct tevent_req *read_smb_req;
55 struct tevent_req *suicide_req;
57 enum protocol_types min_protocol;
58 enum protocol_types max_protocol;
59 enum protocol_types protocol;
62 bool mandatory_signing;
65 * The incoming dispatch function should return:
66 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
67 * - NT_STATUS_OK, if no more processing is desired, e.g.
68 * the dispatch function called
70 * - All other return values disconnect the connection.
72 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
78 uint32_t capabilities;
83 uint32_t capabilities;
86 uint16_t security_mode;
95 const char *workgroup;
101 uint32_t capabilities;
106 struct smb_signing_state *signing;
107 struct smb_trans_enc_state *trans_enc;
109 struct tevent_req *read_braw_req;
114 uint32_t capabilities;
115 uint16_t security_mode;
120 uint32_t capabilities;
121 uint16_t security_mode;
123 uint32_t max_trans_size;
124 uint32_t max_read_size;
125 uint32_t max_write_size;
133 uint16_t cur_credits;
134 uint16_t max_credits;
136 uint32_t cc_chunk_len;
137 uint32_t cc_max_chunks;
141 uint8_t preauth_sha512[64];
144 struct smbXcli_session *sessions;
147 struct smb2cli_session {
149 uint16_t session_flags;
150 DATA_BLOB application_key;
151 DATA_BLOB signing_key;
154 DATA_BLOB encryption_key;
155 DATA_BLOB decryption_key;
156 uint64_t nonce_high_random;
157 uint64_t nonce_high_max;
160 uint16_t channel_sequence;
164 struct smbXcli_session {
165 struct smbXcli_session *prev, *next;
166 struct smbXcli_conn *conn;
171 DATA_BLOB application_key;
175 struct smb2cli_session *smb2;
178 DATA_BLOB signing_key;
179 uint8_t preauth_sha512[64];
183 * this should be a short term hack
184 * until the upper layers have implemented
187 bool disconnect_expired;
190 struct smbXcli_tcon {
192 uint32_t fs_attributes;
196 uint16_t optional_support;
197 uint32_t maximal_access;
198 uint32_t guest_maximal_access;
207 uint32_t capabilities;
208 uint32_t maximal_access;
214 struct smbXcli_req_state {
215 struct tevent_context *ev;
216 struct smbXcli_conn *conn;
217 struct smbXcli_session *session; /* maybe NULL */
218 struct smbXcli_tcon *tcon; /* maybe NULL */
220 uint8_t length_hdr[4];
226 struct tevent_req *write_req;
229 /* Space for the header including the wct */
230 uint8_t hdr[HDR_VWV];
233 * For normal requests, smb1cli_req_send chooses a mid.
234 * SecondaryV trans requests need to use the mid of the primary
235 * request, so we need a place to store it.
236 * Assume it is set if != 0.
241 uint8_t bytecount_buf[2];
243 #define MAX_SMB_IOV 10
244 /* length_hdr, hdr, words, byte_count, buffers */
245 struct iovec iov[1 + 3 + MAX_SMB_IOV];
250 struct tevent_req **chained_requests;
253 NTSTATUS recv_status;
254 /* always an array of 3 talloc elements */
255 struct iovec *recv_iov;
259 const uint8_t *fixed;
264 uint8_t transform[SMB2_TF_HDR_SIZE];
265 uint8_t hdr[SMB2_HDR_BODY];
266 uint8_t pad[7]; /* padding space for compounding */
269 * always an array of 3 talloc elements
270 * (without a SMB2_TRANSFORM header!)
274 struct iovec *recv_iov;
277 * the expected max for the response dyn_len
279 uint32_t max_dyn_len;
281 uint16_t credit_charge;
285 uint64_t encryption_session_id;
287 bool signing_skipped;
290 uint16_t cancel_flags;
296 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
299 * NT_STATUS_OK, means we do not notify the callers
301 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
303 while (conn->sessions) {
304 conn->sessions->conn = NULL;
305 DLIST_REMOVE(conn->sessions, conn->sessions);
308 if (conn->smb1.trans_enc) {
309 TALLOC_FREE(conn->smb1.trans_enc);
315 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
317 const char *remote_name,
318 enum smb_signing_setting signing_state,
319 uint32_t smb1_capabilities,
320 struct GUID *client_guid,
321 uint32_t smb2_capabilities)
323 struct smbXcli_conn *conn = NULL;
325 struct sockaddr *sa = NULL;
329 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
336 conn->remote_name = talloc_strdup(conn, remote_name);
337 if (conn->remote_name == NULL) {
341 ss = (void *)&conn->local_ss;
342 sa = (struct sockaddr *)ss;
343 sa_length = sizeof(conn->local_ss);
344 ret = getsockname(fd, sa, &sa_length);
348 ss = (void *)&conn->remote_ss;
349 sa = (struct sockaddr *)ss;
350 sa_length = sizeof(conn->remote_ss);
351 ret = getpeername(fd, sa, &sa_length);
356 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
357 if (conn->outgoing == NULL) {
360 conn->pending = NULL;
362 conn->min_protocol = PROTOCOL_NONE;
363 conn->max_protocol = PROTOCOL_NONE;
364 conn->protocol = PROTOCOL_NONE;
366 switch (signing_state) {
367 case SMB_SIGNING_OFF:
369 conn->allow_signing = false;
370 conn->desire_signing = false;
371 conn->mandatory_signing = false;
373 case SMB_SIGNING_DEFAULT:
374 case SMB_SIGNING_IF_REQUIRED:
375 /* if the server requires it */
376 conn->allow_signing = true;
377 conn->desire_signing = false;
378 conn->mandatory_signing = false;
380 case SMB_SIGNING_DESIRED:
381 /* if the server desires it */
382 conn->allow_signing = true;
383 conn->desire_signing = true;
384 conn->mandatory_signing = false;
386 case SMB_SIGNING_IPC_DEFAULT:
387 case SMB_SIGNING_REQUIRED:
389 conn->allow_signing = true;
390 conn->desire_signing = true;
391 conn->mandatory_signing = true;
395 conn->smb1.client.capabilities = smb1_capabilities;
396 conn->smb1.client.max_xmit = UINT16_MAX;
398 conn->smb1.capabilities = conn->smb1.client.capabilities;
399 conn->smb1.max_xmit = 1024;
403 /* initialise signing */
404 conn->smb1.signing = smb_signing_init(conn,
406 conn->desire_signing,
407 conn->mandatory_signing);
408 if (!conn->smb1.signing) {
412 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
413 if (conn->mandatory_signing) {
414 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
417 conn->smb2.client.guid = *client_guid;
419 conn->smb2.client.capabilities = smb2_capabilities;
421 conn->smb2.cur_credits = 1;
422 conn->smb2.max_credits = 0;
423 conn->smb2.io_priority = 1;
426 * Samba and Windows servers accept a maximum of 16 MiB with a maximum
427 * chunk length of 1 MiB.
429 conn->smb2.cc_chunk_len = 1024 * 1024;
430 conn->smb2.cc_max_chunks = 16;
432 talloc_set_destructor(conn, smbXcli_conn_destructor);
440 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
446 if (conn->sock_fd == -1) {
453 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
455 return conn->protocol;
458 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
460 if (conn->protocol >= PROTOCOL_SMB2_02) {
464 if (conn->smb1.capabilities & CAP_UNICODE) {
471 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
473 set_socket_options(conn->sock_fd, options);
476 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
478 return &conn->local_ss;
481 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
483 return &conn->remote_ss;
486 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
488 return conn->remote_name;
491 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
493 if (conn->protocol >= PROTOCOL_SMB2_02) {
500 return conn->smb1.server.max_mux;
503 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
505 if (conn->protocol >= PROTOCOL_SMB2_02) {
506 return conn->smb2.server.system_time;
509 return conn->smb1.server.system_time;
512 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
514 if (conn->protocol >= PROTOCOL_SMB2_02) {
515 return &conn->smb2.server.gss_blob;
518 return &conn->smb1.server.gss_blob;
521 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
523 if (conn->protocol >= PROTOCOL_SMB2_02) {
524 return &conn->smb2.server.guid;
527 return &conn->smb1.server.guid;
530 struct smbXcli_conn_samba_suicide_state {
531 struct smbXcli_conn *conn;
534 struct tevent_req *write_req;
537 static void smbXcli_conn_samba_suicide_cleanup(struct tevent_req *req,
538 enum tevent_req_state req_state);
539 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
541 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
542 struct tevent_context *ev,
543 struct smbXcli_conn *conn,
546 struct tevent_req *req, *subreq;
547 struct smbXcli_conn_samba_suicide_state *state;
549 req = tevent_req_create(mem_ctx, &state,
550 struct smbXcli_conn_samba_suicide_state);
555 SIVAL(state->buf, 4, 0x74697865);
556 SCVAL(state->buf, 8, exitcode);
557 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
559 if (conn->suicide_req != NULL) {
560 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
561 return tevent_req_post(req, ev);
564 state->iov.iov_base = state->buf;
565 state->iov.iov_len = sizeof(state->buf);
567 subreq = writev_send(state, ev, conn->outgoing, conn->sock_fd,
568 false, &state->iov, 1);
569 if (tevent_req_nomem(subreq, req)) {
570 return tevent_req_post(req, ev);
572 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
573 state->write_req = subreq;
575 tevent_req_set_cleanup_fn(req, smbXcli_conn_samba_suicide_cleanup);
578 * We need to use tevent_req_defer_callback()
579 * in order to allow smbXcli_conn_disconnect()
580 * to do a safe cleanup.
582 tevent_req_defer_callback(req, ev);
583 conn->suicide_req = req;
588 static void smbXcli_conn_samba_suicide_cleanup(struct tevent_req *req,
589 enum tevent_req_state req_state)
591 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
592 req, struct smbXcli_conn_samba_suicide_state);
594 TALLOC_FREE(state->write_req);
596 if (state->conn == NULL) {
600 if (state->conn->suicide_req == req) {
601 state->conn->suicide_req = NULL;
606 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
608 struct tevent_req *req = tevent_req_callback_data(
609 subreq, struct tevent_req);
610 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
611 req, struct smbXcli_conn_samba_suicide_state);
615 state->write_req = NULL;
617 nwritten = writev_recv(subreq, &err);
619 if (nwritten == -1) {
620 /* here, we need to notify all pending requests */
621 NTSTATUS status = map_nt_error_from_unix_common(err);
622 smbXcli_conn_disconnect(state->conn, status);
625 tevent_req_done(req);
628 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
630 return tevent_req_simple_recv_ntstatus(req);
633 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
636 TALLOC_CTX *frame = talloc_stackframe();
637 struct tevent_context *ev;
638 struct tevent_req *req;
639 NTSTATUS status = NT_STATUS_NO_MEMORY;
642 if (smbXcli_conn_has_async_calls(conn)) {
644 * Can't use sync call while an async call is in flight
646 status = NT_STATUS_INVALID_PARAMETER_MIX;
649 ev = samba_tevent_context_init(frame);
653 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
657 ok = tevent_req_poll_ntstatus(req, ev, &status);
661 status = smbXcli_conn_samba_suicide_recv(req);
667 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
669 return conn->smb1.capabilities;
672 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
674 return conn->smb1.max_xmit;
677 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
679 size_t pending = talloc_array_length(conn->pending);
680 uint16_t possible = conn->smb1.server.max_mux;
682 if (pending >= possible) {
689 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
691 return conn->smb1.server.session_key;
694 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
696 return conn->smb1.server.challenge;
699 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
701 return conn->smb1.server.security_mode;
704 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
706 return conn->smb1.server.readbraw;
709 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
711 return conn->smb1.server.writebraw;
714 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
716 return conn->smb1.server.lockread;
719 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
721 return conn->smb1.server.writeunlock;
724 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
726 return conn->smb1.server.time_zone;
729 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
730 const DATA_BLOB user_session_key,
731 const DATA_BLOB response)
733 return smb_signing_activate(conn->smb1.signing,
738 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
739 const uint8_t *buf, uint32_t seqnum)
741 const uint8_t *hdr = buf + NBT_HDR_SIZE;
742 size_t len = smb_len_nbt(buf);
744 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
747 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
749 return smb_signing_is_active(conn->smb1.signing);
752 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
753 struct smb_trans_enc_state *es)
755 /* Replace the old state, if any. */
756 if (conn->smb1.trans_enc) {
757 TALLOC_FREE(conn->smb1.trans_enc);
759 conn->smb1.trans_enc = es;
762 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
764 return common_encryption_on(conn->smb1.trans_enc);
768 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
770 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
771 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
773 if (NT_STATUS_IS_OK(status)) {
777 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
781 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
785 * Is the SMB command able to hold an AND_X successor
786 * @param[in] cmd The SMB command in question
787 * @retval Can we add a chained request after "cmd"?
789 bool smb1cli_is_andx_req(uint8_t cmd)
809 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
811 size_t num_pending = talloc_array_length(conn->pending);
814 if (conn->protocol == PROTOCOL_NONE) {
816 * This is what windows sends on the SMB1 Negprot request
817 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
825 result = conn->smb1.mid++;
826 if ((result == 0) || (result == 0xffff)) {
830 for (i=0; i<num_pending; i++) {
831 if (result == smb1cli_req_mid(conn->pending[i])) {
836 if (i == num_pending) {
842 static NTSTATUS smbXcli_req_cancel_write_req(struct tevent_req *req)
844 struct smbXcli_req_state *state =
846 struct smbXcli_req_state);
847 struct smbXcli_conn *conn = state->conn;
848 size_t num_pending = talloc_array_length(conn->pending);
853 if (state->write_req == NULL) {
858 * Check if it's possible to cancel the request.
859 * If the result is true it's not to late.
860 * See writev_cancel().
862 ok = tevent_req_cancel(state->write_req);
864 TALLOC_FREE(state->write_req);
866 if (conn->protocol >= PROTOCOL_SMB2_02) {
868 * SMB2 has a sane signing state.
873 if (num_pending > 1) {
875 * We have more pending requests following us. This
876 * means the signing state will be broken for them.
878 * As a solution we could add the requests directly to
879 * our outgoing queue and do the signing in the trigger
880 * function and then use writev_send() without passing a
881 * queue. That way we'll only sign packets we're most
882 * likely send to the wire.
884 return NT_STATUS_REQUEST_OUT_OF_SEQUENCE;
888 * If we're the only request that's
889 * pending, we're able to recover the signing
892 smb_signing_cancel_reply(conn->smb1.signing,
893 state->smb1.one_way_seqnum);
897 ret = writev_recv(state->write_req, &err);
898 TALLOC_FREE(state->write_req);
900 return map_nt_error_from_unix_common(err);
906 void smbXcli_req_unset_pending(struct tevent_req *req)
908 struct smbXcli_req_state *state =
910 struct smbXcli_req_state);
911 struct smbXcli_conn *conn = state->conn;
912 size_t num_pending = talloc_array_length(conn->pending);
914 NTSTATUS cancel_status;
916 cancel_status = smbXcli_req_cancel_write_req(req);
918 if (state->smb1.mid != 0) {
920 * This is a [nt]trans[2] request which waits
921 * for more than one reply.
923 if (!NT_STATUS_IS_OK(cancel_status)) {
925 * If the write_req cancel didn't work
926 * we can't use the connection anymore.
928 smbXcli_conn_disconnect(conn, cancel_status);
934 tevent_req_set_cleanup_fn(req, NULL);
936 if (num_pending == 1) {
938 * The pending read_smb tevent_req is a child of
939 * conn->pending. So if nothing is pending anymore, we need to
940 * delete the socket read fde.
942 /* TODO: smbXcli_conn_cancel_read_req */
943 TALLOC_FREE(conn->pending);
944 conn->read_smb_req = NULL;
946 if (!NT_STATUS_IS_OK(cancel_status)) {
948 * If the write_req cancel didn't work
949 * we can't use the connection anymore.
951 smbXcli_conn_disconnect(conn, cancel_status);
957 for (i=0; i<num_pending; i++) {
958 if (req == conn->pending[i]) {
962 if (i == num_pending) {
964 * Something's seriously broken. Just returning here is the
965 * right thing nevertheless, the point of this routine is to
966 * remove ourselves from conn->pending.
969 if (!NT_STATUS_IS_OK(cancel_status)) {
971 * If the write_req cancel didn't work
972 * we can't use the connection anymore.
974 smbXcli_conn_disconnect(conn, cancel_status);
981 * Remove ourselves from the conn->pending array
983 for (; i < (num_pending - 1); i++) {
984 conn->pending[i] = conn->pending[i+1];
988 * No NULL check here, we're shrinking by sizeof(void *), and
989 * talloc_realloc just adjusts the size for this.
991 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
994 if (!NT_STATUS_IS_OK(cancel_status)) {
996 * If the write_req cancel didn't work
997 * we can't use the connection anymore.
999 smbXcli_conn_disconnect(conn, cancel_status);
1005 static void smbXcli_req_cleanup(struct tevent_req *req,
1006 enum tevent_req_state req_state)
1008 struct smbXcli_req_state *state =
1009 tevent_req_data(req,
1010 struct smbXcli_req_state);
1011 struct smbXcli_conn *conn = state->conn;
1012 NTSTATUS cancel_status;
1014 switch (req_state) {
1015 case TEVENT_REQ_RECEIVED:
1017 * Make sure we really remove it from
1018 * the pending array on destruction.
1020 * smbXcli_req_unset_pending() calls
1021 * smbXcli_req_cancel_write_req() internal
1023 state->smb1.mid = 0;
1024 smbXcli_req_unset_pending(req);
1027 cancel_status = smbXcli_req_cancel_write_req(req);
1028 if (!NT_STATUS_IS_OK(cancel_status)) {
1030 * If the write_req cancel didn't work
1031 * we can't use the connection anymore.
1033 smbXcli_conn_disconnect(conn, cancel_status);
1040 static bool smb1cli_req_cancel(struct tevent_req *req);
1041 static bool smb2cli_req_cancel(struct tevent_req *req);
1043 static bool smbXcli_req_cancel(struct tevent_req *req)
1045 struct smbXcli_req_state *state =
1046 tevent_req_data(req,
1047 struct smbXcli_req_state);
1049 if (!smbXcli_conn_is_connected(state->conn)) {
1053 if (state->conn->protocol == PROTOCOL_NONE) {
1057 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
1058 return smb2cli_req_cancel(req);
1061 return smb1cli_req_cancel(req);
1064 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
1066 bool smbXcli_req_set_pending(struct tevent_req *req)
1068 struct smbXcli_req_state *state =
1069 tevent_req_data(req,
1070 struct smbXcli_req_state);
1071 struct smbXcli_conn *conn;
1072 struct tevent_req **pending;
1077 if (!smbXcli_conn_is_connected(conn)) {
1081 num_pending = talloc_array_length(conn->pending);
1083 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
1085 if (pending == NULL) {
1088 pending[num_pending] = req;
1089 conn->pending = pending;
1090 tevent_req_set_cleanup_fn(req, smbXcli_req_cleanup);
1091 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1093 if (!smbXcli_conn_receive_next(conn)) {
1095 * the caller should notify the current request
1097 * And all other pending requests get notified
1098 * by smbXcli_conn_disconnect().
1100 smbXcli_req_unset_pending(req);
1101 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1108 static void smbXcli_conn_received(struct tevent_req *subreq);
1110 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
1112 size_t num_pending = talloc_array_length(conn->pending);
1113 struct tevent_req *req;
1114 struct smbXcli_req_state *state;
1116 if (conn->read_smb_req != NULL) {
1120 if (num_pending == 0) {
1121 if (conn->smb2.mid < UINT64_MAX) {
1122 /* no more pending requests, so we are done for now */
1127 * If there are no more SMB2 requests possible,
1128 * because we are out of message ids,
1129 * we need to disconnect.
1131 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
1135 req = conn->pending[0];
1136 state = tevent_req_data(req, struct smbXcli_req_state);
1139 * We're the first ones, add the read_smb request that waits for the
1140 * answer from the server
1142 conn->read_smb_req = read_smb_send(conn->pending,
1145 if (conn->read_smb_req == NULL) {
1148 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
1152 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
1154 struct smbXcli_session *session;
1155 int sock_fd = conn->sock_fd;
1157 tevent_queue_stop(conn->outgoing);
1161 session = conn->sessions;
1162 if (talloc_array_length(conn->pending) == 0) {
1164 * if we do not have pending requests
1165 * there is no need to update the channel_sequence
1169 for (; session; session = session->next) {
1170 smb2cli_session_increment_channel_sequence(session);
1173 if (conn->suicide_req != NULL) {
1175 * smbXcli_conn_samba_suicide_send()
1176 * used tevent_req_defer_callback() already.
1178 if (!NT_STATUS_IS_OK(status)) {
1179 tevent_req_nterror(conn->suicide_req, status);
1181 conn->suicide_req = NULL;
1185 * Cancel all pending requests. We do not do a for-loop walking
1186 * conn->pending because that array changes in
1187 * smbXcli_req_unset_pending.
1189 while (talloc_array_length(conn->pending) > 0) {
1190 struct tevent_req *req;
1191 struct smbXcli_req_state *state;
1192 struct tevent_req **chain;
1196 req = conn->pending[0];
1197 state = tevent_req_data(req, struct smbXcli_req_state);
1199 if (state->smb1.chained_requests == NULL) {
1203 * We're dead. No point waiting for trans2
1206 state->smb1.mid = 0;
1208 smbXcli_req_unset_pending(req);
1210 if (NT_STATUS_IS_OK(status)) {
1211 /* do not notify the callers */
1215 in_progress = tevent_req_is_in_progress(req);
1224 * we need to defer the callback, because we may notify
1225 * more then one caller.
1227 tevent_req_defer_callback(req, state->ev);
1228 tevent_req_nterror(req, status);
1232 chain = talloc_move(conn, &state->smb1.chained_requests);
1233 num_chained = talloc_array_length(chain);
1235 for (i=0; i<num_chained; i++) {
1239 state = tevent_req_data(req, struct smbXcli_req_state);
1242 * We're dead. No point waiting for trans2
1245 state->smb1.mid = 0;
1247 smbXcli_req_unset_pending(req);
1249 if (NT_STATUS_IS_OK(status)) {
1250 /* do not notify the callers */
1254 in_progress = tevent_req_is_in_progress(req);
1263 * we need to defer the callback, because we may notify
1264 * more than one caller.
1266 tevent_req_defer_callback(req, state->ev);
1267 tevent_req_nterror(req, status);
1272 if (sock_fd != -1) {
1278 * Fetch a smb request's mid. Only valid after the request has been sent by
1279 * smb1cli_req_send().
1281 uint16_t smb1cli_req_mid(struct tevent_req *req)
1283 struct smbXcli_req_state *state =
1284 tevent_req_data(req,
1285 struct smbXcli_req_state);
1287 if (state->smb1.mid != 0) {
1288 return state->smb1.mid;
1291 return SVAL(state->smb1.hdr, HDR_MID);
1294 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1296 struct smbXcli_req_state *state =
1297 tevent_req_data(req,
1298 struct smbXcli_req_state);
1300 state->smb1.mid = mid;
1303 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1305 struct smbXcli_req_state *state =
1306 tevent_req_data(req,
1307 struct smbXcli_req_state);
1309 return state->smb1.seqnum;
1312 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1314 struct smbXcli_req_state *state =
1315 tevent_req_data(req,
1316 struct smbXcli_req_state);
1318 state->smb1.seqnum = seqnum;
1321 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1323 ssize_t ret = iov_buflen(iov, count);
1325 /* Ignore the overflow case for now ... */
1329 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1330 const struct iovec *iov,
1336 buflen = iov_buflen(iov, count);
1341 buf = talloc_array(mem_ctx, uint8_t, buflen);
1346 iov_buf(iov, count, buf, buflen);
1351 static void smb1cli_req_flags(enum protocol_types protocol,
1352 uint32_t smb1_capabilities,
1353 uint8_t smb_command,
1354 uint8_t additional_flags,
1355 uint8_t clear_flags,
1357 uint16_t additional_flags2,
1358 uint16_t clear_flags2,
1362 uint16_t flags2 = 0;
1364 if (protocol >= PROTOCOL_LANMAN1) {
1365 flags |= FLAG_CASELESS_PATHNAMES;
1366 flags |= FLAG_CANONICAL_PATHNAMES;
1369 if (protocol >= PROTOCOL_LANMAN2) {
1370 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1371 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1374 if (protocol >= PROTOCOL_NT1) {
1375 flags2 |= FLAGS2_IS_LONG_NAME;
1377 if (smb1_capabilities & CAP_UNICODE) {
1378 flags2 |= FLAGS2_UNICODE_STRINGS;
1380 if (smb1_capabilities & CAP_STATUS32) {
1381 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1383 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1384 flags2 |= FLAGS2_EXTENDED_SECURITY;
1388 flags |= additional_flags;
1389 flags &= ~clear_flags;
1390 flags2 |= additional_flags2;
1391 flags2 &= ~clear_flags2;
1397 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1399 static bool smb1cli_req_cancel(struct tevent_req *req)
1401 struct smbXcli_req_state *state =
1402 tevent_req_data(req,
1403 struct smbXcli_req_state);
1408 struct tevent_req *subreq;
1411 flags = CVAL(state->smb1.hdr, HDR_FLG);
1412 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1413 pid = SVAL(state->smb1.hdr, HDR_PID);
1414 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1415 mid = SVAL(state->smb1.hdr, HDR_MID);
1417 subreq = smb1cli_req_create(state, state->ev,
1427 0, NULL); /* bytes */
1428 if (subreq == NULL) {
1431 smb1cli_req_set_mid(subreq, mid);
1433 status = smb1cli_req_chain_submit(&subreq, 1);
1434 if (!NT_STATUS_IS_OK(status)) {
1435 TALLOC_FREE(subreq);
1438 smb1cli_req_set_mid(subreq, 0);
1440 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1445 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1447 /* we do not care about the result */
1448 TALLOC_FREE(subreq);
1451 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1452 struct tevent_context *ev,
1453 struct smbXcli_conn *conn,
1454 uint8_t smb_command,
1455 uint8_t additional_flags,
1456 uint8_t clear_flags,
1457 uint16_t additional_flags2,
1458 uint16_t clear_flags2,
1459 uint32_t timeout_msec,
1461 struct smbXcli_tcon *tcon,
1462 struct smbXcli_session *session,
1463 uint8_t wct, uint16_t *vwv,
1465 struct iovec *bytes_iov)
1467 struct tevent_req *req;
1468 struct smbXcli_req_state *state;
1470 uint16_t flags2 = 0;
1475 if (iov_count > MAX_SMB_IOV) {
1477 * Should not happen :-)
1482 req = tevent_req_create(mem_ctx, &state,
1483 struct smbXcli_req_state);
1489 state->session = session;
1493 uid = session->smb1.session_id;
1497 tid = tcon->smb1.tcon_id;
1499 if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
1500 clear_flags |= FLAG_CASELESS_PATHNAMES;
1502 /* Default setting, case insensitive. */
1503 additional_flags |= FLAG_CASELESS_PATHNAMES;
1506 if (smbXcli_conn_dfs_supported(conn) &&
1507 smbXcli_tcon_is_dfs_share(tcon))
1509 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
1513 state->smb1.recv_cmd = 0xFF;
1514 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1515 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1516 if (state->smb1.recv_iov == NULL) {
1521 smb1cli_req_flags(conn->protocol,
1522 conn->smb1.capabilities,
1531 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1532 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1533 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1534 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1535 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1536 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1537 SSVAL(state->smb1.hdr, HDR_TID, tid);
1538 SSVAL(state->smb1.hdr, HDR_PID, pid);
1539 SSVAL(state->smb1.hdr, HDR_UID, uid);
1540 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1541 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1543 state->smb1.vwv = vwv;
1545 num_bytes = iov_buflen(bytes_iov, iov_count);
1546 if (num_bytes == -1) {
1548 * I'd love to add a check for num_bytes<=UINT16_MAX here, but
1549 * the smbclient->samba connections can lie and transfer more.
1555 SSVAL(state->smb1.bytecount_buf, 0, num_bytes);
1557 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1558 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1559 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1560 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1561 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1562 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1563 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1564 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1566 if (iov_count != 0) {
1567 memcpy(&state->smb1.iov[4], bytes_iov,
1568 iov_count * sizeof(*bytes_iov));
1570 state->smb1.iov_count = iov_count + 4;
1572 if (timeout_msec > 0) {
1573 struct timeval endtime;
1575 endtime = timeval_current_ofs_msec(timeout_msec);
1576 if (!tevent_req_set_endtime(req, ev, endtime)) {
1581 switch (smb_command) {
1585 state->one_way = true;
1588 state->one_way = true;
1589 state->smb1.one_way_seqnum = true;
1593 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1594 state->one_way = true;
1602 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1603 struct iovec *iov, int iov_count,
1605 bool one_way_seqnum)
1607 TALLOC_CTX *frame = NULL;
1611 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1612 * iovec directly. MD5Update would do that just fine.
1615 if (iov_count < 4) {
1616 return NT_STATUS_INVALID_PARAMETER_MIX;
1618 if (iov[0].iov_len != NBT_HDR_SIZE) {
1619 return NT_STATUS_INVALID_PARAMETER_MIX;
1621 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1622 return NT_STATUS_INVALID_PARAMETER_MIX;
1624 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1625 return NT_STATUS_INVALID_PARAMETER_MIX;
1627 if (iov[3].iov_len != sizeof(uint16_t)) {
1628 return NT_STATUS_INVALID_PARAMETER_MIX;
1631 frame = talloc_stackframe();
1633 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1635 return NT_STATUS_NO_MEMORY;
1638 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1640 smb_signing_sign_pdu(conn->smb1.signing,
1641 buf, talloc_get_size(buf),
1643 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1646 return NT_STATUS_OK;
1649 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1650 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1651 TALLOC_CTX *tmp_mem,
1654 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1655 struct smbXcli_req_state *state,
1656 struct iovec *iov, int iov_count)
1658 struct tevent_req *subreq;
1664 if (!smbXcli_conn_is_connected(state->conn)) {
1665 return NT_STATUS_CONNECTION_DISCONNECTED;
1668 if (state->conn->protocol > PROTOCOL_NT1) {
1669 return NT_STATUS_REVISION_MISMATCH;
1672 if (iov_count < 4) {
1673 return NT_STATUS_INVALID_PARAMETER_MIX;
1675 if (iov[0].iov_len != NBT_HDR_SIZE) {
1676 return NT_STATUS_INVALID_PARAMETER_MIX;
1678 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1679 return NT_STATUS_INVALID_PARAMETER_MIX;
1681 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1682 return NT_STATUS_INVALID_PARAMETER_MIX;
1684 if (iov[3].iov_len != sizeof(uint16_t)) {
1685 return NT_STATUS_INVALID_PARAMETER_MIX;
1688 cmd = CVAL(iov[1].iov_base, HDR_COM);
1689 if (cmd == SMBreadBraw) {
1690 if (smbXcli_conn_has_async_calls(state->conn)) {
1691 return NT_STATUS_INVALID_PARAMETER_MIX;
1693 state->conn->smb1.read_braw_req = req;
1696 if (state->smb1.mid != 0) {
1697 mid = state->smb1.mid;
1699 mid = smb1cli_alloc_mid(state->conn);
1701 SSVAL(iov[1].iov_base, HDR_MID, mid);
1703 nbtlen = iov_buflen(&iov[1], iov_count-1);
1704 if ((nbtlen == -1) || (nbtlen > 0x1FFFF)) {
1705 return NT_STATUS_INVALID_PARAMETER_MIX;
1708 _smb_setlen_nbt(iov[0].iov_base, nbtlen);
1710 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1711 &state->smb1.seqnum,
1712 state->smb1.one_way_seqnum);
1714 if (!NT_STATUS_IS_OK(status)) {
1719 * If we supported multiple encrytion contexts
1720 * here we'd look up based on tid.
1722 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1723 char *buf, *enc_buf;
1725 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1727 return NT_STATUS_NO_MEMORY;
1729 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1730 (char *)buf, &enc_buf);
1732 if (!NT_STATUS_IS_OK(status)) {
1733 DEBUG(0, ("Error in encrypting client message: %s\n",
1734 nt_errstr(status)));
1737 buf = (char *)talloc_memdup(state, enc_buf,
1738 smb_len_nbt(enc_buf)+4);
1741 return NT_STATUS_NO_MEMORY;
1743 iov[0].iov_base = (void *)buf;
1744 iov[0].iov_len = talloc_get_size(buf);
1748 if (state->conn->dispatch_incoming == NULL) {
1749 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1752 if (!smbXcli_req_set_pending(req)) {
1753 return NT_STATUS_NO_MEMORY;
1756 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1758 subreq = writev_send(state, state->ev, state->conn->outgoing,
1759 state->conn->sock_fd, false, iov, iov_count);
1760 if (subreq == NULL) {
1761 return NT_STATUS_NO_MEMORY;
1763 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1764 state->write_req = subreq;
1766 return NT_STATUS_OK;
1769 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1770 struct tevent_context *ev,
1771 struct smbXcli_conn *conn,
1772 uint8_t smb_command,
1773 uint8_t additional_flags,
1774 uint8_t clear_flags,
1775 uint16_t additional_flags2,
1776 uint16_t clear_flags2,
1777 uint32_t timeout_msec,
1779 struct smbXcli_tcon *tcon,
1780 struct smbXcli_session *session,
1781 uint8_t wct, uint16_t *vwv,
1783 const uint8_t *bytes)
1785 struct tevent_req *req;
1789 iov.iov_base = discard_const_p(void, bytes);
1790 iov.iov_len = num_bytes;
1792 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1793 additional_flags, clear_flags,
1794 additional_flags2, clear_flags2,
1801 if (!tevent_req_is_in_progress(req)) {
1802 return tevent_req_post(req, ev);
1804 status = smb1cli_req_chain_submit(&req, 1);
1805 if (tevent_req_nterror(req, status)) {
1806 return tevent_req_post(req, ev);
1811 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1813 struct tevent_req *req =
1814 tevent_req_callback_data(subreq,
1816 struct smbXcli_req_state *state =
1817 tevent_req_data(req,
1818 struct smbXcli_req_state);
1822 state->write_req = NULL;
1824 nwritten = writev_recv(subreq, &err);
1825 TALLOC_FREE(subreq);
1826 if (nwritten == -1) {
1827 /* here, we need to notify all pending requests */
1828 NTSTATUS status = map_nt_error_from_unix_common(err);
1829 smbXcli_conn_disconnect(state->conn, status);
1833 if (state->one_way) {
1834 state->inbuf = NULL;
1835 tevent_req_done(req);
1840 static void smbXcli_conn_received(struct tevent_req *subreq)
1842 struct smbXcli_conn *conn =
1843 tevent_req_callback_data(subreq,
1844 struct smbXcli_conn);
1845 TALLOC_CTX *frame = talloc_stackframe();
1851 if (subreq != conn->read_smb_req) {
1852 DEBUG(1, ("Internal error: cli_smb_received called with "
1853 "unexpected subreq\n"));
1854 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1858 conn->read_smb_req = NULL;
1860 received = read_smb_recv(subreq, frame, &inbuf, &err);
1861 TALLOC_FREE(subreq);
1862 if (received == -1) {
1863 status = map_nt_error_from_unix_common(err);
1864 smbXcli_conn_disconnect(conn, status);
1869 status = conn->dispatch_incoming(conn, frame, inbuf);
1871 if (NT_STATUS_IS_OK(status)) {
1873 * We should not do any more processing
1874 * as the dispatch function called
1875 * tevent_req_done().
1880 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1882 * We got an error, so notify all pending requests
1884 smbXcli_conn_disconnect(conn, status);
1889 * We got NT_STATUS_RETRY, so we may ask for a
1890 * next incoming pdu.
1892 if (!smbXcli_conn_receive_next(conn)) {
1893 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1897 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1898 struct iovec **piov, int *pnum_iov)
1909 size_t min_size = MIN_SMB_SIZE;
1911 buflen = smb_len_tcp(buf);
1914 hdr = buf + NBT_HDR_SIZE;
1916 status = smb1cli_pull_raw_error(hdr);
1917 if (NT_STATUS_IS_ERR(status)) {
1919 * This is an ugly hack to support OS/2
1920 * which skips the byte_count in the DATA block
1921 * on some error responses.
1925 min_size -= sizeof(uint16_t);
1928 if (buflen < min_size) {
1929 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1933 * This returns iovec elements in the following order:
1948 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1950 return NT_STATUS_NO_MEMORY;
1952 iov[0].iov_base = hdr;
1953 iov[0].iov_len = HDR_WCT;
1956 cmd = CVAL(hdr, HDR_COM);
1960 size_t len = buflen - taken;
1962 struct iovec *iov_tmp;
1969 * we need at least WCT
1971 needed = sizeof(uint8_t);
1973 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1974 __location__, (int)len, (int)needed));
1979 * Now we check if the specified words are there
1981 wct = CVAL(hdr, wct_ofs);
1982 needed += wct * sizeof(uint16_t);
1984 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1985 __location__, (int)len, (int)needed));
1989 if ((num_iov == 1) &&
1991 NT_STATUS_IS_ERR(status))
1994 * This is an ugly hack to support OS/2
1995 * which skips the byte_count in the DATA block
1996 * on some error responses.
2000 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2002 if (iov_tmp == NULL) {
2004 return NT_STATUS_NO_MEMORY;
2007 cur = &iov[num_iov];
2011 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
2013 cur[1].iov_base = cur[0].iov_base;
2020 * we need at least BCC
2022 needed += sizeof(uint16_t);
2024 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2025 __location__, (int)len, (int)needed));
2030 * Now we check if the specified bytes are there
2032 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
2033 bcc = SVAL(hdr, bcc_ofs);
2034 needed += bcc * sizeof(uint8_t);
2036 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2037 __location__, (int)len, (int)needed));
2042 * we allocate 2 iovec structures for words and bytes
2044 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2046 if (iov_tmp == NULL) {
2048 return NT_STATUS_NO_MEMORY;
2051 cur = &iov[num_iov];
2054 cur[0].iov_len = wct * sizeof(uint16_t);
2055 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
2056 cur[1].iov_len = bcc * sizeof(uint8_t);
2057 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
2061 if (!smb1cli_is_andx_req(cmd)) {
2063 * If the current command does not have AndX chanining
2069 if (wct == 0 && bcc == 0) {
2071 * An empty response also ends the chain,
2072 * most likely with an error.
2078 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
2079 __location__, (int)wct, (int)cmd));
2082 cmd = CVAL(cur[0].iov_base, 0);
2085 * If it is the end of the chain we are also done.
2089 wct_ofs = SVAL(cur[0].iov_base, 2);
2091 if (wct_ofs < taken) {
2092 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2094 if (wct_ofs > buflen) {
2095 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2099 * we consumed everything up to the start of the next
2105 remaining = buflen - taken;
2107 if (remaining > 0 && num_iov >= 3) {
2109 * The last DATA block gets the remaining
2110 * bytes, this is needed to support
2111 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
2113 iov[num_iov-1].iov_len += remaining;
2117 *pnum_iov = num_iov;
2118 return NT_STATUS_OK;
2122 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2125 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2126 TALLOC_CTX *tmp_mem,
2129 struct tevent_req *req;
2130 struct smbXcli_req_state *state;
2137 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
2138 size_t len = smb_len_tcp(inbuf);
2139 struct iovec *iov = NULL;
2141 struct tevent_req **chain = NULL;
2142 size_t num_chained = 0;
2143 size_t num_responses = 0;
2145 if (conn->smb1.read_braw_req != NULL) {
2146 req = conn->smb1.read_braw_req;
2147 conn->smb1.read_braw_req = NULL;
2148 state = tevent_req_data(req, struct smbXcli_req_state);
2150 smbXcli_req_unset_pending(req);
2152 if (state->smb1.recv_iov == NULL) {
2154 * For requests with more than
2155 * one response, we have to readd the
2158 state->smb1.recv_iov = talloc_zero_array(state,
2161 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2162 return NT_STATUS_OK;
2166 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
2167 state->smb1.recv_iov[0].iov_len = len;
2168 ZERO_STRUCT(state->smb1.recv_iov[1]);
2169 ZERO_STRUCT(state->smb1.recv_iov[2]);
2171 state->smb1.recv_cmd = SMBreadBraw;
2172 state->smb1.recv_status = NT_STATUS_OK;
2173 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2175 tevent_req_done(req);
2176 return NT_STATUS_OK;
2179 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
2180 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
2181 DEBUG(10, ("Got non-SMB PDU\n"));
2182 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2186 * If we supported multiple encrytion contexts
2187 * here we'd look up based on tid.
2189 if (common_encryption_on(conn->smb1.trans_enc)
2190 && (CVAL(inbuf, 0) == 0)) {
2191 uint16_t enc_ctx_num;
2193 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
2194 if (!NT_STATUS_IS_OK(status)) {
2195 DEBUG(10, ("get_enc_ctx_num returned %s\n",
2196 nt_errstr(status)));
2200 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
2201 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
2203 conn->smb1.trans_enc->enc_ctx_num));
2204 return NT_STATUS_INVALID_HANDLE;
2207 status = common_decrypt_buffer(conn->smb1.trans_enc,
2209 if (!NT_STATUS_IS_OK(status)) {
2210 DEBUG(10, ("common_decrypt_buffer returned %s\n",
2211 nt_errstr(status)));
2214 inhdr = inbuf + NBT_HDR_SIZE;
2215 len = smb_len_nbt(inbuf);
2218 mid = SVAL(inhdr, HDR_MID);
2219 num_pending = talloc_array_length(conn->pending);
2221 for (i=0; i<num_pending; i++) {
2222 if (mid == smb1cli_req_mid(conn->pending[i])) {
2226 if (i == num_pending) {
2227 /* Dump unexpected reply */
2228 return NT_STATUS_RETRY;
2231 oplock_break = false;
2233 if (mid == 0xffff) {
2235 * Paranoia checks that this is really an oplock break request.
2237 oplock_break = (len == 51); /* hdr + 8 words */
2238 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
2239 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
2240 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
2241 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
2243 if (!oplock_break) {
2244 /* Dump unexpected reply */
2245 return NT_STATUS_RETRY;
2249 req = conn->pending[i];
2250 state = tevent_req_data(req, struct smbXcli_req_state);
2252 if (!oplock_break /* oplock breaks are not signed */
2253 && !smb_signing_check_pdu(conn->smb1.signing,
2254 inhdr, len, state->smb1.seqnum+1)) {
2255 DEBUG(10, ("cli_check_sign_mac failed\n"));
2256 return NT_STATUS_ACCESS_DENIED;
2259 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2261 if (!NT_STATUS_IS_OK(status)) {
2262 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2263 nt_errstr(status)));
2267 cmd = CVAL(inhdr, HDR_COM);
2268 status = smb1cli_pull_raw_error(inhdr);
2270 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2271 (state->session != NULL) && state->session->disconnect_expired)
2274 * this should be a short term hack
2275 * until the upper layers have implemented
2276 * re-authentication.
2281 if (state->smb1.chained_requests == NULL) {
2283 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2286 smbXcli_req_unset_pending(req);
2288 if (state->smb1.recv_iov == NULL) {
2290 * For requests with more than
2291 * one response, we have to readd the
2294 state->smb1.recv_iov = talloc_zero_array(state,
2297 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2298 return NT_STATUS_OK;
2302 state->smb1.recv_cmd = cmd;
2303 state->smb1.recv_status = status;
2304 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2306 state->smb1.recv_iov[0] = iov[0];
2307 state->smb1.recv_iov[1] = iov[1];
2308 state->smb1.recv_iov[2] = iov[2];
2310 if (talloc_array_length(conn->pending) == 0) {
2311 tevent_req_done(req);
2312 return NT_STATUS_OK;
2315 tevent_req_defer_callback(req, state->ev);
2316 tevent_req_done(req);
2317 return NT_STATUS_RETRY;
2320 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2321 num_chained = talloc_array_length(chain);
2322 num_responses = (num_iov - 1)/2;
2324 if (num_responses > num_chained) {
2325 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2328 for (i=0; i<num_chained; i++) {
2329 size_t iov_idx = 1 + (i*2);
2330 struct iovec *cur = &iov[iov_idx];
2334 state = tevent_req_data(req, struct smbXcli_req_state);
2336 smbXcli_req_unset_pending(req);
2339 * as we finish multiple requests here
2340 * we need to defer the callbacks as
2341 * they could destroy our current stack state.
2343 tevent_req_defer_callback(req, state->ev);
2345 if (i >= num_responses) {
2346 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2350 if (state->smb1.recv_iov == NULL) {
2352 * For requests with more than
2353 * one response, we have to readd the
2356 state->smb1.recv_iov = talloc_zero_array(state,
2359 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2364 state->smb1.recv_cmd = cmd;
2366 if (i == (num_responses - 1)) {
2368 * The last request in the chain gets the status
2370 state->smb1.recv_status = status;
2372 cmd = CVAL(cur[0].iov_base, 0);
2373 state->smb1.recv_status = NT_STATUS_OK;
2376 state->inbuf = inbuf;
2379 * Note: here we use talloc_reference() in a way
2380 * that does not expose it to the caller.
2382 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2383 if (tevent_req_nomem(inbuf_ref, req)) {
2387 /* copy the related buffers */
2388 state->smb1.recv_iov[0] = iov[0];
2389 state->smb1.recv_iov[1] = cur[0];
2390 state->smb1.recv_iov[2] = cur[1];
2392 tevent_req_done(req);
2395 return NT_STATUS_RETRY;
2398 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2399 TALLOC_CTX *mem_ctx,
2400 struct iovec **piov,
2404 uint32_t *pvwv_offset,
2405 uint32_t *pnum_bytes,
2407 uint32_t *pbytes_offset,
2409 const struct smb1cli_req_expected_response *expected,
2410 size_t num_expected)
2412 struct smbXcli_req_state *state =
2413 tevent_req_data(req,
2414 struct smbXcli_req_state);
2415 NTSTATUS status = NT_STATUS_OK;
2416 struct iovec *recv_iov = NULL;
2417 uint8_t *hdr = NULL;
2419 uint32_t vwv_offset = 0;
2420 uint16_t *vwv = NULL;
2421 uint32_t num_bytes = 0;
2422 uint32_t bytes_offset = 0;
2423 uint8_t *bytes = NULL;
2425 bool found_status = false;
2426 bool found_size = false;
2440 if (pvwv_offset != NULL) {
2443 if (pnum_bytes != NULL) {
2446 if (pbytes != NULL) {
2449 if (pbytes_offset != NULL) {
2452 if (pinbuf != NULL) {
2456 if (state->inbuf != NULL) {
2457 recv_iov = state->smb1.recv_iov;
2458 state->smb1.recv_iov = NULL;
2459 if (state->smb1.recv_cmd != SMBreadBraw) {
2460 hdr = (uint8_t *)recv_iov[0].iov_base;
2461 wct = recv_iov[1].iov_len/2;
2462 vwv = (uint16_t *)recv_iov[1].iov_base;
2463 vwv_offset = PTR_DIFF(vwv, hdr);
2464 num_bytes = recv_iov[2].iov_len;
2465 bytes = (uint8_t *)recv_iov[2].iov_base;
2466 bytes_offset = PTR_DIFF(bytes, hdr);
2470 if (tevent_req_is_nterror(req, &status)) {
2471 for (i=0; i < num_expected; i++) {
2472 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2473 found_status = true;
2479 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2485 if (num_expected == 0) {
2486 found_status = true;
2490 status = state->smb1.recv_status;
2492 for (i=0; i < num_expected; i++) {
2493 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2497 found_status = true;
2498 if (expected[i].wct == 0) {
2503 if (expected[i].wct == wct) {
2509 if (!found_status) {
2514 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2518 *piov = talloc_move(mem_ctx, &recv_iov);
2530 if (pvwv_offset != NULL) {
2531 *pvwv_offset = vwv_offset;
2533 if (pnum_bytes != NULL) {
2534 *pnum_bytes = num_bytes;
2536 if (pbytes != NULL) {
2539 if (pbytes_offset != NULL) {
2540 *pbytes_offset = bytes_offset;
2542 if (pinbuf != NULL) {
2543 *pinbuf = state->inbuf;
2549 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2556 for (i=0; i<num_reqs; i++) {
2557 struct smbXcli_req_state *state;
2558 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2559 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2560 state->smb1.iov_count-2);
2561 wct_ofs = (wct_ofs + 3) & ~3;
2566 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2568 struct smbXcli_req_state *first_state =
2569 tevent_req_data(reqs[0],
2570 struct smbXcli_req_state);
2571 struct smbXcli_req_state *state;
2573 size_t chain_padding = 0;
2575 struct iovec *iov = NULL;
2576 struct iovec *this_iov;
2580 if (num_reqs == 1) {
2581 return smb1cli_req_writev_submit(reqs[0], first_state,
2582 first_state->smb1.iov,
2583 first_state->smb1.iov_count);
2587 for (i=0; i<num_reqs; i++) {
2588 if (!tevent_req_is_in_progress(reqs[i])) {
2589 return NT_STATUS_INTERNAL_ERROR;
2592 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2594 if (state->smb1.iov_count < 4) {
2595 return NT_STATUS_INVALID_PARAMETER_MIX;
2600 * The NBT and SMB header
2613 iovlen += state->smb1.iov_count - 2;
2616 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2618 return NT_STATUS_NO_MEMORY;
2621 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2622 first_state, reqs, sizeof(*reqs) * num_reqs);
2623 if (first_state->smb1.chained_requests == NULL) {
2625 return NT_STATUS_NO_MEMORY;
2628 wct_offset = HDR_WCT;
2631 for (i=0; i<num_reqs; i++) {
2632 size_t next_padding = 0;
2635 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2637 if (i < num_reqs-1) {
2638 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2639 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2641 TALLOC_FREE(first_state->smb1.chained_requests);
2642 return NT_STATUS_INVALID_PARAMETER_MIX;
2646 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2647 state->smb1.iov_count-2) + 1;
2648 if ((wct_offset % 4) != 0) {
2649 next_padding = 4 - (wct_offset % 4);
2651 wct_offset += next_padding;
2652 vwv = state->smb1.vwv;
2654 if (i < num_reqs-1) {
2655 struct smbXcli_req_state *next_state =
2656 tevent_req_data(reqs[i+1],
2657 struct smbXcli_req_state);
2658 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2660 SSVAL(vwv+1, 0, wct_offset);
2661 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2662 /* properly end the chain */
2663 SCVAL(vwv+0, 0, 0xff);
2664 SCVAL(vwv+0, 1, 0xff);
2670 * The NBT and SMB header
2672 this_iov[0] = state->smb1.iov[0];
2673 this_iov[1] = state->smb1.iov[1];
2677 * This one is a bit subtle. We have to add
2678 * chain_padding bytes between the requests, and we
2679 * have to also include the wct field of the
2680 * subsequent requests. We use the subsequent header
2681 * for the padding, it contains the wct field in its
2684 this_iov[0].iov_len = chain_padding+1;
2685 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2686 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2687 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2692 * copy the words and bytes
2694 memcpy(this_iov, state->smb1.iov+2,
2695 sizeof(struct iovec) * (state->smb1.iov_count-2));
2696 this_iov += state->smb1.iov_count - 2;
2697 chain_padding = next_padding;
2700 nbt_len = iov_buflen(&iov[1], iovlen-1);
2701 if ((nbt_len == -1) || (nbt_len > first_state->conn->smb1.max_xmit)) {
2703 TALLOC_FREE(first_state->smb1.chained_requests);
2704 return NT_STATUS_INVALID_PARAMETER_MIX;
2707 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2708 if (!NT_STATUS_IS_OK(status)) {
2710 TALLOC_FREE(first_state->smb1.chained_requests);
2714 return NT_STATUS_OK;
2717 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2719 return ((tevent_queue_length(conn->outgoing) != 0)
2720 || (talloc_array_length(conn->pending) != 0));
2723 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2725 if (conn->protocol >= PROTOCOL_SMB2_02) {
2726 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2729 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2732 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2734 uint16_t credits = 1;
2736 if (conn->smb2.cur_credits == 0) {
2737 if (max_dyn_len != NULL) {
2743 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2744 credits = conn->smb2.cur_credits;
2747 if (max_dyn_len != NULL) {
2748 *max_dyn_len = credits * 65536;
2754 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2756 return conn->smb2.server.capabilities;
2759 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2761 return conn->smb2.server.security_mode;
2764 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2766 return conn->smb2.server.max_trans_size;
2769 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2771 return conn->smb2.server.max_read_size;
2774 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2776 return conn->smb2.server.max_write_size;
2779 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2780 uint16_t max_credits)
2782 conn->smb2.max_credits = max_credits;
2785 uint16_t smb2cli_conn_get_cur_credits(struct smbXcli_conn *conn)
2787 return conn->smb2.cur_credits;
2790 uint8_t smb2cli_conn_get_io_priority(struct smbXcli_conn *conn)
2792 if (conn->protocol < PROTOCOL_SMB3_11) {
2796 return conn->smb2.io_priority;
2799 void smb2cli_conn_set_io_priority(struct smbXcli_conn *conn,
2800 uint8_t io_priority)
2802 conn->smb2.io_priority = io_priority;
2805 uint32_t smb2cli_conn_cc_chunk_len(struct smbXcli_conn *conn)
2807 return conn->smb2.cc_chunk_len;
2810 void smb2cli_conn_set_cc_chunk_len(struct smbXcli_conn *conn,
2813 conn->smb2.cc_chunk_len = chunk_len;
2816 uint32_t smb2cli_conn_cc_max_chunks(struct smbXcli_conn *conn)
2818 return conn->smb2.cc_max_chunks;
2821 void smb2cli_conn_set_cc_max_chunks(struct smbXcli_conn *conn,
2822 uint32_t max_chunks)
2824 conn->smb2.cc_max_chunks = max_chunks;
2827 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2829 static bool smb2cli_req_cancel(struct tevent_req *req)
2831 struct smbXcli_req_state *state =
2832 tevent_req_data(req,
2833 struct smbXcli_req_state);
2834 struct smbXcli_tcon *tcon = state->tcon;
2835 struct smbXcli_session *session = state->session;
2836 uint8_t *fixed = state->smb2.pad;
2837 uint16_t fixed_len = 4;
2838 struct tevent_req *subreq;
2839 struct smbXcli_req_state *substate;
2842 SSVAL(fixed, 0, 0x04);
2845 subreq = smb2cli_req_create(state, state->ev,
2853 if (subreq == NULL) {
2856 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2858 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, state->smb2.cancel_flags);
2859 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, state->smb2.cancel_mid);
2860 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, state->smb2.cancel_aid);
2862 status = smb2cli_req_compound_submit(&subreq, 1);
2863 if (!NT_STATUS_IS_OK(status)) {
2864 TALLOC_FREE(subreq);
2868 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2873 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2875 /* we do not care about the result */
2876 TALLOC_FREE(subreq);
2879 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2880 struct tevent_context *ev,
2881 struct smbXcli_conn *conn,
2883 uint32_t additional_flags,
2884 uint32_t clear_flags,
2885 uint32_t timeout_msec,
2886 struct smbXcli_tcon *tcon,
2887 struct smbXcli_session *session,
2888 const uint8_t *fixed,
2892 uint32_t max_dyn_len)
2894 struct tevent_req *req;
2895 struct smbXcli_req_state *state;
2899 bool use_channel_sequence = false;
2900 uint16_t channel_sequence = 0;
2901 bool use_replay_flag = false;
2903 req = tevent_req_create(mem_ctx, &state,
2904 struct smbXcli_req_state);
2911 state->session = session;
2914 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2915 use_channel_sequence = true;
2916 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2917 use_channel_sequence = true;
2920 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_00) {
2921 use_replay_flag = true;
2924 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_11) {
2925 flags |= SMB2_PRIORITY_VALUE_TO_MASK(conn->smb2.io_priority);
2929 uid = session->smb2->session_id;
2931 if (use_channel_sequence) {
2932 channel_sequence = session->smb2->channel_sequence;
2935 if (use_replay_flag && session->smb2->replay_active) {
2936 additional_flags |= SMB2_HDR_FLAG_REPLAY_OPERATION;
2939 state->smb2.should_sign = session->smb2->should_sign;
2940 state->smb2.should_encrypt = session->smb2->should_encrypt;
2942 if (cmd == SMB2_OP_SESSSETUP &&
2943 session->smb2_channel.signing_key.length == 0 &&
2944 session->smb2->signing_key.length != 0)
2947 * a session bind needs to be signed
2949 state->smb2.should_sign = true;
2952 if (cmd == SMB2_OP_SESSSETUP &&
2953 session->smb2_channel.signing_key.length == 0) {
2954 state->smb2.should_encrypt = false;
2957 if (additional_flags & SMB2_HDR_FLAG_SIGNED) {
2958 if (session->smb2_channel.signing_key.length == 0) {
2959 tevent_req_nterror(req, NT_STATUS_NO_USER_SESSION_KEY);
2963 additional_flags &= ~SMB2_HDR_FLAG_SIGNED;
2964 state->smb2.should_sign = true;
2969 tid = tcon->smb2.tcon_id;
2971 if (tcon->smb2.should_sign) {
2972 state->smb2.should_sign = true;
2974 if (tcon->smb2.should_encrypt) {
2975 state->smb2.should_encrypt = true;
2979 if (state->smb2.should_encrypt) {
2980 state->smb2.should_sign = false;
2983 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2984 if (state->smb2.recv_iov == NULL) {
2989 flags |= additional_flags;
2990 flags &= ~clear_flags;
2992 state->smb2.fixed = fixed;
2993 state->smb2.fixed_len = fixed_len;
2994 state->smb2.dyn = dyn;
2995 state->smb2.dyn_len = dyn_len;
2996 state->smb2.max_dyn_len = max_dyn_len;
2998 if (state->smb2.should_encrypt) {
2999 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
3000 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
3003 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
3004 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
3005 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
3006 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
3007 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
3008 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
3009 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
3010 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
3013 case SMB2_OP_CANCEL:
3014 state->one_way = true;
3018 * If this is a dummy request, it will have
3019 * UINT64_MAX as message id.
3020 * If we send on break acknowledgement,
3021 * this gets overwritten later.
3023 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
3027 if (timeout_msec > 0) {
3028 struct timeval endtime;
3030 endtime = timeval_current_ofs_msec(timeout_msec);
3031 if (!tevent_req_set_endtime(req, ev, endtime)) {
3039 void smb2cli_req_set_notify_async(struct tevent_req *req)
3041 struct smbXcli_req_state *state =
3042 tevent_req_data(req,
3043 struct smbXcli_req_state);
3045 state->smb2.notify_async = true;
3048 static void smb2cli_req_writev_done(struct tevent_req *subreq);
3049 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3050 TALLOC_CTX *tmp_mem,
3053 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
3056 struct smbXcli_req_state *state;
3057 struct tevent_req *subreq;
3059 int i, num_iov, nbt_len;
3061 const DATA_BLOB *encryption_key = NULL;
3062 uint64_t encryption_session_id = 0;
3063 uint64_t nonce_high = UINT64_MAX;
3064 uint64_t nonce_low = UINT64_MAX;
3067 * 1 for the nbt length, optional TRANSFORM
3068 * per request: HDR, fixed, dyn, padding
3069 * -1 because the last one does not need padding
3072 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
3074 return NT_STATUS_NO_MEMORY;
3081 * the session of the first request that requires encryption
3082 * specifies the encryption key.
3084 for (i=0; i<num_reqs; i++) {
3085 if (!tevent_req_is_in_progress(reqs[i])) {
3086 return NT_STATUS_INTERNAL_ERROR;
3089 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
3091 if (!smbXcli_conn_is_connected(state->conn)) {
3092 return NT_STATUS_CONNECTION_DISCONNECTED;
3095 if ((state->conn->protocol != PROTOCOL_NONE) &&
3096 (state->conn->protocol < PROTOCOL_SMB2_02)) {
3097 return NT_STATUS_REVISION_MISMATCH;
3100 if (state->session == NULL) {
3104 if (!state->smb2.should_encrypt) {
3108 encryption_key = &state->session->smb2->encryption_key;
3109 if (encryption_key->length == 0) {
3110 return NT_STATUS_INVALID_PARAMETER_MIX;
3113 encryption_session_id = state->session->smb2->session_id;
3115 state->session->smb2->nonce_low += 1;
3116 if (state->session->smb2->nonce_low == 0) {
3117 state->session->smb2->nonce_high += 1;
3118 state->session->smb2->nonce_low += 1;
3122 * CCM and GCM algorithms must never have their
3123 * nonce wrap, or the security of the whole
3124 * communication and the keys is destroyed.
3125 * We must drop the connection once we have
3126 * transfered too much data.
3128 * NOTE: We assume nonces greater than 8 bytes.
3130 if (state->session->smb2->nonce_high >=
3131 state->session->smb2->nonce_high_max)
3133 return NT_STATUS_ENCRYPTION_FAILED;
3136 nonce_high = state->session->smb2->nonce_high_random;
3137 nonce_high += state->session->smb2->nonce_high;
3138 nonce_low = state->session->smb2->nonce_low;
3141 iov[num_iov].iov_base = state->smb2.transform;
3142 iov[num_iov].iov_len = sizeof(state->smb2.transform);
3145 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
3146 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
3148 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
3150 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
3151 encryption_session_id);
3153 nbt_len += SMB2_TF_HDR_SIZE;
3157 for (i=0; i<num_reqs; i++) {
3166 const DATA_BLOB *signing_key = NULL;
3168 if (!tevent_req_is_in_progress(reqs[i])) {
3169 return NT_STATUS_INTERNAL_ERROR;
3172 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
3174 if (!smbXcli_conn_is_connected(state->conn)) {
3175 return NT_STATUS_CONNECTION_DISCONNECTED;
3178 if ((state->conn->protocol != PROTOCOL_NONE) &&
3179 (state->conn->protocol < PROTOCOL_SMB2_02)) {
3180 return NT_STATUS_REVISION_MISMATCH;
3183 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3184 if (opcode == SMB2_OP_CANCEL) {
3188 avail = UINT64_MAX - state->conn->smb2.mid;
3190 return NT_STATUS_CONNECTION_ABORTED;
3193 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
3194 uint32_t max_dyn_len = 1;
3196 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
3197 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
3199 charge = (max_dyn_len - 1)/ 65536 + 1;
3204 charge = MAX(state->smb2.credit_charge, charge);
3206 avail = MIN(avail, state->conn->smb2.cur_credits);
3207 if (avail < charge) {
3208 return NT_STATUS_INTERNAL_ERROR;
3212 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
3213 credits = state->conn->smb2.max_credits -
3214 state->conn->smb2.cur_credits;
3216 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
3220 mid = state->conn->smb2.mid;
3221 state->conn->smb2.mid += charge;
3222 state->conn->smb2.cur_credits -= charge;
3224 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
3225 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
3227 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
3228 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
3230 state->smb2.cancel_flags = 0;
3231 state->smb2.cancel_mid = mid;
3232 state->smb2.cancel_aid = 0;
3235 if (state->session && encryption_key == NULL) {
3237 * We prefer the channel signing key if it is
3240 if (state->smb2.should_sign) {
3241 signing_key = &state->session->smb2_channel.signing_key;
3245 * If it is a channel binding, we already have the main
3246 * signing key and try that one.
3248 if (signing_key && signing_key->length == 0) {
3249 signing_key = &state->session->smb2->signing_key;
3253 * If we do not have any session key yet, we skip the
3254 * signing of SMB2_OP_SESSSETUP requests.
3256 if (signing_key && signing_key->length == 0) {
3262 iov[num_iov].iov_base = state->smb2.hdr;
3263 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
3266 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
3267 iov[num_iov].iov_len = state->smb2.fixed_len;
3270 if (state->smb2.dyn != NULL) {
3271 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
3272 iov[num_iov].iov_len = state->smb2.dyn_len;
3276 reqlen = sizeof(state->smb2.hdr);
3277 reqlen += state->smb2.fixed_len;
3278 reqlen += state->smb2.dyn_len;
3280 if (i < num_reqs-1) {
3281 if ((reqlen % 8) > 0) {
3282 uint8_t pad = 8 - (reqlen % 8);
3283 iov[num_iov].iov_base = state->smb2.pad;
3284 iov[num_iov].iov_len = pad;
3288 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
3291 state->smb2.encryption_session_id = encryption_session_id;
3293 if (signing_key != NULL) {
3296 status = smb2_signing_sign_pdu(*signing_key,
3297 state->session->conn->protocol,
3298 &iov[hdr_iov], num_iov - hdr_iov);
3299 if (!NT_STATUS_IS_OK(status)) {
3306 ret = smbXcli_req_set_pending(reqs[i]);
3308 return NT_STATUS_NO_MEMORY;
3312 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
3313 _smb_setlen_tcp(state->length_hdr, nbt_len);
3314 iov[0].iov_base = state->length_hdr;
3315 iov[0].iov_len = sizeof(state->length_hdr);
3317 if (encryption_key != NULL) {
3319 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
3323 buf = talloc_array(iov, uint8_t, buflen);
3325 return NT_STATUS_NO_MEMORY;
3329 * We copy the buffers before encrypting them,
3330 * this is at least currently needed for the
3331 * to keep state->smb2.hdr.
3333 * Also the callers may expect there buffers
3336 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3337 struct iovec *v = &iov[vi];
3338 const uint8_t *o = (const uint8_t *)v->iov_base;
3340 memcpy(buf, o, v->iov_len);
3341 v->iov_base = (void *)buf;
3345 status = smb2_signing_encrypt_pdu(*encryption_key,
3346 state->conn->smb2.server.cipher,
3347 &iov[tf_iov], num_iov - tf_iov);
3348 if (!NT_STATUS_IS_OK(status)) {
3353 if (state->conn->dispatch_incoming == NULL) {
3354 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3357 subreq = writev_send(state, state->ev, state->conn->outgoing,
3358 state->conn->sock_fd, false, iov, num_iov);
3359 if (subreq == NULL) {
3360 return NT_STATUS_NO_MEMORY;
3362 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3363 state->write_req = subreq;
3365 return NT_STATUS_OK;
3368 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3370 struct smbXcli_req_state *state =
3371 tevent_req_data(req,
3372 struct smbXcli_req_state);
3374 state->smb2.credit_charge = charge;
3377 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3378 struct tevent_context *ev,
3379 struct smbXcli_conn *conn,
3381 uint32_t additional_flags,
3382 uint32_t clear_flags,
3383 uint32_t timeout_msec,
3384 struct smbXcli_tcon *tcon,
3385 struct smbXcli_session *session,
3386 const uint8_t *fixed,
3390 uint32_t max_dyn_len)
3392 struct tevent_req *req;
3395 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3396 additional_flags, clear_flags,
3405 if (!tevent_req_is_in_progress(req)) {
3406 return tevent_req_post(req, ev);
3408 status = smb2cli_req_compound_submit(&req, 1);
3409 if (tevent_req_nterror(req, status)) {
3410 return tevent_req_post(req, ev);
3415 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3417 struct tevent_req *req =
3418 tevent_req_callback_data(subreq,
3420 struct smbXcli_req_state *state =
3421 tevent_req_data(req,
3422 struct smbXcli_req_state);
3426 state->write_req = NULL;
3428 nwritten = writev_recv(subreq, &err);
3429 TALLOC_FREE(subreq);
3430 if (nwritten == -1) {
3431 /* here, we need to notify all pending requests */
3432 NTSTATUS status = map_nt_error_from_unix_common(err);
3433 smbXcli_conn_disconnect(state->conn, status);
3438 static struct smbXcli_session* smbXcli_session_by_uid(struct smbXcli_conn *conn,
3441 struct smbXcli_session *s = conn->sessions;
3443 for (; s; s = s->next) {
3444 if (s->smb2->session_id != uid) {
3453 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3456 TALLOC_CTX *mem_ctx,
3457 struct iovec **piov, int *pnum_iov)
3462 uint8_t *first_hdr = buf;
3463 size_t verified_buflen = 0;
3467 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3469 return NT_STATUS_NO_MEMORY;
3472 while (taken < buflen) {
3473 size_t len = buflen - taken;
3474 uint8_t *hdr = first_hdr + taken;
3477 size_t next_command_ofs;
3479 struct iovec *iov_tmp;
3481 if (verified_buflen > taken) {
3482 len = verified_buflen - taken;
3489 DEBUG(10, ("%d bytes left, expected at least %d\n",
3493 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3494 struct smbXcli_session *s;
3496 struct iovec tf_iov[2];
3500 if (len < SMB2_TF_HDR_SIZE) {
3501 DEBUG(10, ("%d bytes left, expected at least %d\n",
3502 (int)len, SMB2_TF_HDR_SIZE));
3506 tf_len = SMB2_TF_HDR_SIZE;
3509 hdr = first_hdr + taken;
3510 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3511 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3513 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3514 DEBUG(10, ("%d bytes left, expected at least %d\n",
3516 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3520 s = smbXcli_session_by_uid(conn, uid);
3522 DEBUG(10, ("unknown session_id %llu\n",
3523 (unsigned long long)uid));
3527 tf_iov[0].iov_base = (void *)tf;
3528 tf_iov[0].iov_len = tf_len;
3529 tf_iov[1].iov_base = (void *)hdr;
3530 tf_iov[1].iov_len = enc_len;
3532 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3533 conn->smb2.server.cipher,
3535 if (!NT_STATUS_IS_OK(status)) {
3540 verified_buflen = taken + enc_len;
3545 * We need the header plus the body length field
3548 if (len < SMB2_HDR_BODY + 2) {
3549 DEBUG(10, ("%d bytes left, expected at least %d\n",
3550 (int)len, SMB2_HDR_BODY));
3553 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3554 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3558 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3559 DEBUG(10, ("Got HDR len %d, expected %d\n",
3560 SVAL(hdr, 4), SMB2_HDR_BODY));
3565 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3566 body_size = SVAL(hdr, SMB2_HDR_BODY);
3568 if (next_command_ofs != 0) {
3569 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3572 if (next_command_ofs > full_size) {
3575 full_size = next_command_ofs;
3577 if (body_size < 2) {
3580 body_size &= 0xfffe;
3582 if (body_size > (full_size - SMB2_HDR_BODY)) {
3586 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3588 if (iov_tmp == NULL) {
3590 return NT_STATUS_NO_MEMORY;
3593 cur = &iov[num_iov];
3596 cur[0].iov_base = tf;
3597 cur[0].iov_len = tf_len;
3598 cur[1].iov_base = hdr;
3599 cur[1].iov_len = SMB2_HDR_BODY;
3600 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3601 cur[2].iov_len = body_size;
3602 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3603 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3609 *pnum_iov = num_iov;
3610 return NT_STATUS_OK;
3614 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3617 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3620 size_t num_pending = talloc_array_length(conn->pending);
3623 for (i=0; i<num_pending; i++) {
3624 struct tevent_req *req = conn->pending[i];
3625 struct smbXcli_req_state *state =
3626 tevent_req_data(req,
3627 struct smbXcli_req_state);
3629 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3636 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3637 TALLOC_CTX *tmp_mem,
3640 struct tevent_req *req;
3641 struct smbXcli_req_state *state = NULL;
3642 struct iovec *iov = NULL;
3646 struct smbXcli_session *last_session = NULL;
3647 size_t inbuf_len = smb_len_tcp(inbuf);
3649 status = smb2cli_inbuf_parse_compound(conn,
3650 inbuf + NBT_HDR_SIZE,
3654 if (!NT_STATUS_IS_OK(status)) {
3658 for (i=0; i<num_iov; i+=4) {
3659 uint8_t *inbuf_ref = NULL;
3660 struct iovec *cur = &iov[i];
3661 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3662 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3663 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3664 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3665 uint16_t req_opcode;
3667 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3668 uint32_t new_credits;
3669 struct smbXcli_session *session = NULL;
3670 const DATA_BLOB *signing_key = NULL;
3671 bool was_encrypted = false;
3673 new_credits = conn->smb2.cur_credits;
3674 new_credits += credits;
3675 if (new_credits > UINT16_MAX) {
3676 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3678 conn->smb2.cur_credits += credits;
3680 req = smb2cli_conn_find_pending(conn, mid);
3682 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3684 state = tevent_req_data(req, struct smbXcli_req_state);
3686 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3687 if (opcode != req_opcode) {
3688 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3690 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3692 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3693 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3696 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3697 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3698 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3699 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3701 if (state->smb2.got_async) {
3702 /* We only expect one STATUS_PENDING response */
3703 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3705 state->smb2.got_async = true;
3708 * async interim responses are not signed,
3709 * even if the SMB2_HDR_FLAG_SIGNED flag
3712 state->smb2.cancel_flags = SMB2_HDR_FLAG_ASYNC;
3713 state->smb2.cancel_mid = 0;
3714 state->smb2.cancel_aid = async_id;
3716 if (state->smb2.notify_async) {
3717 tevent_req_defer_callback(req, state->ev);
3718 tevent_req_notify_callback(req);
3723 session = state->session;
3724 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3725 session = last_session;
3727 last_session = session;
3729 if (state->smb2.should_sign) {
3730 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3731 return NT_STATUS_ACCESS_DENIED;
3735 if (flags & SMB2_HDR_FLAG_SIGNED) {
3736 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3738 if (session == NULL) {
3739 session = smbXcli_session_by_uid(state->conn,
3743 if (session == NULL) {
3744 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3747 last_session = session;
3748 signing_key = &session->smb2_channel.signing_key;
3751 if (opcode == SMB2_OP_SESSSETUP) {
3753 * We prefer the channel signing key, if it is
3756 * If we do not have a channel signing key yet,
3757 * we try the main signing key, if it is not
3758 * the final response.
3760 if (signing_key && signing_key->length == 0 &&
3761 !NT_STATUS_IS_OK(status)) {
3762 signing_key = &session->smb2->signing_key;
3765 if (signing_key && signing_key->length == 0) {
3767 * If we do not have a session key to
3768 * verify the signature, we defer the
3769 * signing check to the caller.
3771 * The caller gets NT_STATUS_OK, it
3773 * smb2cli_session_set_session_key()
3775 * smb2cli_session_set_channel_key()
3776 * which will check the signature
3777 * with the channel signing key.
3783 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3784 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3785 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3788 * If the response was encrypted in a SMB2_TRANSFORM
3789 * pdu, which belongs to the correct session,
3790 * we do not need to do signing checks
3792 * It could be the session the response belongs to
3793 * or the session that was used to encrypt the
3794 * SMB2_TRANSFORM request.
3796 if ((session && session->smb2->session_id == uid) ||
3797 (state->smb2.encryption_session_id == uid)) {
3799 was_encrypted = true;
3803 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3805 * if the server returns NT_STATUS_USER_SESSION_DELETED
3806 * the response is not signed and we should
3807 * propagate the NT_STATUS_USER_SESSION_DELETED
3808 * status to the caller.
3810 state->smb2.signing_skipped = true;
3814 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3816 * if the server returns
3817 * NT_STATUS_INVALID_PARAMETER
3818 * the response might not be encrypted.
3820 if (state->smb2.should_encrypt && !was_encrypted) {
3821 state->smb2.signing_skipped = true;
3826 if (state->smb2.should_encrypt && !was_encrypted) {
3827 if (!state->smb2.signing_skipped) {
3828 return NT_STATUS_ACCESS_DENIED;
3832 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3833 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3834 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3836 * if the server returns
3837 * NT_STATUS_NETWORK_NAME_DELETED
3838 * NT_STATUS_FILE_CLOSED
3839 * NT_STATUS_INVALID_PARAMETER
3840 * the response might not be signed
3841 * as this happens before the signing checks.
3843 * If server echos the signature (or all zeros)
3844 * we should report the status from the server
3850 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3851 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3854 state->smb2.signing_skipped = true;
3860 zero = all_zero(inhdr+SMB2_HDR_SIGNATURE, 16);
3862 state->smb2.signing_skipped = true;
3869 status = smb2_signing_check_pdu(*signing_key,
3870 state->conn->protocol,
3872 if (!NT_STATUS_IS_OK(status)) {
3874 * If the signing check fails, we disconnect
3881 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3882 (session != NULL) && session->disconnect_expired)
3885 * this should be a short term hack
3886 * until the upper layers have implemented
3887 * re-authentication.
3892 smbXcli_req_unset_pending(req);
3895 * There might be more than one response
3896 * we need to defer the notifications
3898 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3903 tevent_req_defer_callback(req, state->ev);
3907 * Note: here we use talloc_reference() in a way
3908 * that does not expose it to the caller.
3910 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3911 if (tevent_req_nomem(inbuf_ref, req)) {
3915 /* copy the related buffers */
3916 state->smb2.recv_iov[0] = cur[1];
3917 state->smb2.recv_iov[1] = cur[2];
3918 state->smb2.recv_iov[2] = cur[3];
3920 tevent_req_done(req);
3924 return NT_STATUS_RETRY;
3927 return NT_STATUS_OK;
3930 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3931 struct iovec **piov,
3932 const struct smb2cli_req_expected_response *expected,
3933 size_t num_expected)
3935 struct smbXcli_req_state *state =
3936 tevent_req_data(req,
3937 struct smbXcli_req_state);
3940 bool found_status = false;
3941 bool found_size = false;
3948 if (tevent_req_is_in_progress(req) && state->smb2.got_async) {
3949 return STATUS_PENDING;
3952 if (tevent_req_is_nterror(req, &status)) {
3953 for (i=0; i < num_expected; i++) {
3954 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3955 found_status = true;
3961 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3967 if (num_expected == 0) {
3968 found_status = true;
3972 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3973 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3975 for (i=0; i < num_expected; i++) {
3976 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3980 found_status = true;
3981 if (expected[i].body_size == 0) {
3986 if (expected[i].body_size == body_size) {
3992 if (!found_status) {
3996 if (state->smb2.signing_skipped) {
3997 if (num_expected > 0) {
3998 return NT_STATUS_ACCESS_DENIED;
4000 if (!NT_STATUS_IS_ERR(status)) {
4001 return NT_STATUS_ACCESS_DENIED;
4006 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4010 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
4016 NTSTATUS smb2cli_req_get_sent_iov(struct tevent_req *req,
4017 struct iovec *sent_iov)
4019 struct smbXcli_req_state *state =
4020 tevent_req_data(req,
4021 struct smbXcli_req_state);
4023 if (tevent_req_is_in_progress(req)) {
4024 return STATUS_PENDING;
4027 sent_iov[0].iov_base = state->smb2.hdr;
4028 sent_iov[0].iov_len = sizeof(state->smb2.hdr);
4030 sent_iov[1].iov_base = discard_const(state->smb2.fixed);
4031 sent_iov[1].iov_len = state->smb2.fixed_len;
4033 if (state->smb2.dyn != NULL) {
4034 sent_iov[2].iov_base = discard_const(state->smb2.dyn);
4035 sent_iov[2].iov_len = state->smb2.dyn_len;
4037 sent_iov[2].iov_base = NULL;
4038 sent_iov[2].iov_len = 0;
4041 return NT_STATUS_OK;
4044 static const struct {
4045 enum protocol_types proto;
4046 const char *smb1_name;
4047 } smb1cli_prots[] = {
4048 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
4049 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
4050 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
4051 {PROTOCOL_LANMAN1, "LANMAN1.0"},
4052 {PROTOCOL_LANMAN2, "LM1.2X002"},
4053 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
4054 {PROTOCOL_LANMAN2, "LANMAN2.1"},
4055 {PROTOCOL_LANMAN2, "Samba"},
4056 {PROTOCOL_NT1, "NT LANMAN 1.0"},
4057 {PROTOCOL_NT1, "NT LM 0.12"},
4058 {PROTOCOL_SMB2_02, "SMB 2.002"},
4059 {PROTOCOL_SMB2_10, "SMB 2.???"},
4062 static const struct {
4063 enum protocol_types proto;
4064 uint16_t smb2_dialect;
4065 } smb2cli_prots[] = {
4066 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
4067 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
4068 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
4069 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
4070 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
4071 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
4072 {PROTOCOL_SMB3_10, SMB3_DIALECT_REVISION_310},
4073 {PROTOCOL_SMB3_11, SMB3_DIALECT_REVISION_311},
4076 struct smbXcli_negprot_state {
4077 struct smbXcli_conn *conn;
4078 struct tevent_context *ev;
4079 uint32_t timeout_msec;
4086 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
4087 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
4088 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
4089 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
4090 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
4091 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4095 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
4096 struct tevent_context *ev,
4097 struct smbXcli_conn *conn,
4098 uint32_t timeout_msec,
4099 enum protocol_types min_protocol,
4100 enum protocol_types max_protocol,
4101 uint16_t max_credits)
4103 struct tevent_req *req, *subreq;
4104 struct smbXcli_negprot_state *state;
4106 req = tevent_req_create(mem_ctx, &state,
4107 struct smbXcli_negprot_state);
4113 state->timeout_msec = timeout_msec;
4115 if (min_protocol == PROTOCOL_NONE) {
4116 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
4117 return tevent_req_post(req, ev);
4120 if (max_protocol == PROTOCOL_NONE) {
4121 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
4122 return tevent_req_post(req, ev);
4125 if (min_protocol > max_protocol) {
4126 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
4127 return tevent_req_post(req, ev);
4130 conn->min_protocol = min_protocol;
4131 conn->max_protocol = max_protocol;
4132 conn->protocol = PROTOCOL_NONE;
4134 if (max_protocol >= PROTOCOL_SMB2_02) {
4135 conn->smb2.max_credits = max_credits;
4138 if ((min_protocol < PROTOCOL_SMB2_02) &&
4139 (max_protocol < PROTOCOL_SMB2_02)) {
4143 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4145 subreq = smbXcli_negprot_smb1_subreq(state);
4146 if (tevent_req_nomem(subreq, req)) {
4147 return tevent_req_post(req, ev);
4149 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4153 if ((min_protocol >= PROTOCOL_SMB2_02) &&
4154 (max_protocol >= PROTOCOL_SMB2_02)) {
4158 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4160 subreq = smbXcli_negprot_smb2_subreq(state);
4161 if (tevent_req_nomem(subreq, req)) {
4162 return tevent_req_post(req, ev);
4164 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4169 * We send an SMB1 negprot with the SMB2 dialects
4170 * and expect a SMB1 or a SMB2 response.
4172 * smbXcli_negprot_dispatch_incoming() will fix the
4173 * callback to match protocol of the response.
4175 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
4177 subreq = smbXcli_negprot_smb1_subreq(state);
4178 if (tevent_req_nomem(subreq, req)) {
4179 return tevent_req_post(req, ev);
4181 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
4185 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
4187 struct tevent_req *req =
4188 tevent_req_callback_data(subreq,
4193 * we just want the low level error
4195 status = tevent_req_simple_recv_ntstatus(subreq);
4196 TALLOC_FREE(subreq);
4197 if (tevent_req_nterror(req, status)) {
4201 /* this should never happen */
4202 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4205 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
4208 DATA_BLOB bytes = data_blob_null;
4212 /* setup the protocol strings */
4213 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4217 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4221 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4225 ok = data_blob_append(state, &bytes, &c, sizeof(c));
4231 * We now it is already ascii and
4232 * we want NULL termination.
4234 ok = data_blob_append(state, &bytes,
4235 smb1cli_prots[i].smb1_name,
4236 strlen(smb1cli_prots[i].smb1_name)+1);
4242 smb1cli_req_flags(state->conn->max_protocol,
4243 state->conn->smb1.client.capabilities,
4248 return smb1cli_req_send(state, state->ev, state->conn,
4252 state->timeout_msec,
4253 0xFFFE, 0, NULL, /* pid, tid, session */
4254 0, NULL, /* wct, vwv */
4255 bytes.length, bytes.data);
4258 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
4260 struct tevent_req *req =
4261 tevent_req_callback_data(subreq,
4263 struct smbXcli_negprot_state *state =
4264 tevent_req_data(req,
4265 struct smbXcli_negprot_state);
4266 struct smbXcli_conn *conn = state->conn;
4267 struct iovec *recv_iov = NULL;
4276 size_t num_prots = 0;
4278 uint32_t client_capabilities = conn->smb1.client.capabilities;
4279 uint32_t both_capabilities;
4280 uint32_t server_capabilities = 0;
4281 uint32_t capabilities;
4282 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
4283 uint32_t server_max_xmit = 0;
4285 uint32_t server_max_mux = 0;
4286 uint16_t server_security_mode = 0;
4287 uint32_t server_session_key = 0;
4288 bool server_readbraw = false;
4289 bool server_writebraw = false;
4290 bool server_lockread = false;
4291 bool server_writeunlock = false;
4292 struct GUID server_guid = GUID_zero();
4293 DATA_BLOB server_gss_blob = data_blob_null;
4294 uint8_t server_challenge[8];
4295 char *server_workgroup = NULL;
4296 char *server_name = NULL;
4297 int server_time_zone = 0;
4298 NTTIME server_system_time = 0;
4299 static const struct smb1cli_req_expected_response expected[] = {
4301 .status = NT_STATUS_OK,
4302 .wct = 0x11, /* NT1 */
4305 .status = NT_STATUS_OK,
4306 .wct = 0x0D, /* LM */
4309 .status = NT_STATUS_OK,
4310 .wct = 0x01, /* CORE */
4314 ZERO_STRUCT(server_challenge);
4316 status = smb1cli_req_recv(subreq, state,
4321 NULL, /* pvwv_offset */
4324 NULL, /* pbytes_offset */
4326 expected, ARRAY_SIZE(expected));
4327 TALLOC_FREE(subreq);
4328 if (tevent_req_nterror(req, status)) {
4332 flags = CVAL(inhdr, HDR_FLG);
4334 protnum = SVAL(vwv, 0);
4336 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4337 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4341 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4345 if (protnum != num_prots) {
4350 conn->protocol = smb1cli_prots[i].proto;
4354 if (conn->protocol == PROTOCOL_NONE) {
4355 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4359 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
4360 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4361 "and the selected protocol level doesn't support it.\n"));
4362 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4366 if (flags & FLAG_SUPPORT_LOCKREAD) {
4367 server_lockread = true;
4368 server_writeunlock = true;
4371 if (conn->protocol >= PROTOCOL_NT1) {
4372 const char *client_signing = NULL;
4373 bool server_mandatory = false;
4374 bool server_allowed = false;
4375 const char *server_signing = NULL;
4380 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4385 server_security_mode = CVAL(vwv + 1, 0);
4386 server_max_mux = SVAL(vwv + 1, 1);
4387 server_max_xmit = IVAL(vwv + 3, 1);
4388 server_session_key = IVAL(vwv + 7, 1);
4389 server_time_zone = SVALS(vwv + 15, 1);
4390 server_time_zone *= 60;
4391 /* this time arrives in real GMT */
4392 server_system_time = BVAL(vwv + 11, 1);
4393 server_capabilities = IVAL(vwv + 9, 1);
4395 key_len = CVAL(vwv + 16, 1);
4397 if (server_capabilities & CAP_RAW_MODE) {
4398 server_readbraw = true;
4399 server_writebraw = true;
4401 if (server_capabilities & CAP_LOCK_AND_READ) {
4402 server_lockread = true;
4405 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4406 DATA_BLOB blob1, blob2;
4408 if (num_bytes < 16) {
4409 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4413 blob1 = data_blob_const(bytes, 16);
4414 status = GUID_from_data_blob(&blob1, &server_guid);
4415 if (tevent_req_nterror(req, status)) {
4419 blob1 = data_blob_const(bytes+16, num_bytes-16);
4420 blob2 = data_blob_dup_talloc(state, blob1);
4421 if (blob1.length > 0 &&
4422 tevent_req_nomem(blob2.data, req)) {
4425 server_gss_blob = blob2;
4427 DATA_BLOB blob1, blob2;
4429 if (num_bytes < key_len) {
4430 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4434 if (key_len != 0 && key_len != 8) {
4435 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4440 memcpy(server_challenge, bytes, 8);
4443 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4444 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4445 if (blob1.length > 0) {
4448 len = utf16_len_n(blob1.data,
4452 ok = convert_string_talloc(state,
4460 status = map_nt_error_from_unix_common(errno);
4461 tevent_req_nterror(req, status);
4466 blob2.data += blob1.length;
4467 blob2.length -= blob1.length;
4468 if (blob2.length > 0) {
4471 len = utf16_len_n(blob1.data,
4475 ok = convert_string_talloc(state,
4483 status = map_nt_error_from_unix_common(errno);
4484 tevent_req_nterror(req, status);
4490 client_signing = "disabled";
4491 if (conn->allow_signing) {
4492 client_signing = "allowed";
4494 if (conn->mandatory_signing) {
4495 client_signing = "required";
4498 server_signing = "not supported";
4499 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4500 server_signing = "supported";
4501 server_allowed = true;
4502 } else if (conn->mandatory_signing) {
4504 * We have mandatory signing as client
4505 * lets assume the server will look at our
4506 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4507 * flag in the session setup
4509 server_signing = "not announced";
4510 server_allowed = true;
4512 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4513 server_signing = "required";
4514 server_mandatory = true;
4517 ok = smb_signing_set_negotiated(conn->smb1.signing,
4521 DEBUG(1,("cli_negprot: SMB signing is required, "
4522 "but client[%s] and server[%s] mismatch\n",
4523 client_signing, server_signing));
4524 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4528 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4534 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4538 server_security_mode = SVAL(vwv + 1, 0);
4539 server_max_xmit = SVAL(vwv + 2, 0);
4540 server_max_mux = SVAL(vwv + 3, 0);
4541 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4542 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4543 server_session_key = IVAL(vwv + 6, 0);
4544 server_time_zone = SVALS(vwv + 10, 0);
4545 server_time_zone *= 60;
4546 /* this time is converted to GMT by make_unix_date */
4547 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4548 unix_to_nt_time(&server_system_time, t);
4549 key_len = SVAL(vwv + 11, 0);
4551 if (num_bytes < key_len) {
4552 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4556 if (key_len != 0 && key_len != 8) {
4557 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4562 memcpy(server_challenge, bytes, 8);
4565 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4566 if (blob1.length > 0) {
4570 len = utf16_len_n(blob1.data,
4574 ok = convert_string_talloc(state,
4582 status = map_nt_error_from_unix_common(errno);
4583 tevent_req_nterror(req, status);
4589 /* the old core protocol */
4590 server_time_zone = get_time_zone(time(NULL));
4591 server_max_xmit = 1024;
4595 if (server_max_xmit < 1024) {
4596 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4600 if (server_max_mux < 1) {
4601 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4606 * Now calculate the negotiated capabilities
4607 * based on the mask for:
4608 * - client only flags
4609 * - flags used in both directions
4610 * - server only flags
4612 both_capabilities = client_capabilities & server_capabilities;
4613 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4614 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4615 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4617 max_xmit = MIN(client_max_xmit, server_max_xmit);
4619 conn->smb1.server.capabilities = server_capabilities;
4620 conn->smb1.capabilities = capabilities;
4622 conn->smb1.server.max_xmit = server_max_xmit;
4623 conn->smb1.max_xmit = max_xmit;
4625 conn->smb1.server.max_mux = server_max_mux;
4627 conn->smb1.server.security_mode = server_security_mode;
4629 conn->smb1.server.readbraw = server_readbraw;
4630 conn->smb1.server.writebraw = server_writebraw;
4631 conn->smb1.server.lockread = server_lockread;
4632 conn->smb1.server.writeunlock = server_writeunlock;
4634 conn->smb1.server.session_key = server_session_key;
4636 talloc_steal(conn, server_gss_blob.data);
4637 conn->smb1.server.gss_blob = server_gss_blob;
4638 conn->smb1.server.guid = server_guid;
4639 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4640 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4641 conn->smb1.server.name = talloc_move(conn, &server_name);
4643 conn->smb1.server.time_zone = server_time_zone;
4644 conn->smb1.server.system_time = server_system_time;
4646 tevent_req_done(req);
4649 static size_t smbXcli_padding_helper(uint32_t offset, size_t n)
4651 if ((offset & (n-1)) == 0) return 0;
4652 return n - (offset & (n-1));
4655 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4659 uint16_t dialect_count = 0;
4660 DATA_BLOB dyn = data_blob_null;
4662 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4666 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4670 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4674 SSVAL(val, 0, smb2cli_prots[i].smb2_dialect);
4676 ok = data_blob_append(state, &dyn, val, sizeof(val));
4684 buf = state->smb2.fixed;
4686 SSVAL(buf, 2, dialect_count);
4687 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4688 SSVAL(buf, 6, 0); /* Reserved */
4689 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
4690 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4692 SIVAL(buf, 8, 0); /* Capabilities */
4694 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4698 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4700 if (!NT_STATUS_IS_OK(status)) {
4703 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4705 memset(buf+12, 0, 16); /* ClientGuid */
4708 if (state->conn->max_protocol >= PROTOCOL_SMB3_10) {
4710 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4714 const uint8_t zeros[8] = {0, };
4718 SSVAL(p, 0, 1); /* HashAlgorithmCount */
4719 SSVAL(p, 2, 32); /* SaltLength */
4720 SSVAL(p, 4, SMB2_PREAUTH_INTEGRITY_SHA512);
4721 generate_random_buffer(p + 6, 32);
4723 b = data_blob_const(p, 38);
4724 status = smb2_negotiate_context_add(state, &c,
4725 SMB2_PREAUTH_INTEGRITY_CAPABILITIES, b);
4726 if (!NT_STATUS_IS_OK(status)) {
4730 SSVAL(p, 0, 2); /* ChiperCount */
4732 * For now we preferr CCM because our implementation
4733 * is faster than GCM, see bug #11451.
4735 SSVAL(p, 2, SMB2_ENCRYPTION_AES128_CCM);
4736 SSVAL(p, 4, SMB2_ENCRYPTION_AES128_GCM);
4738 b = data_blob_const(p, 6);
4739 status = smb2_negotiate_context_add(state, &c,
4740 SMB2_ENCRYPTION_CAPABILITIES, b);
4741 if (!NT_STATUS_IS_OK(status)) {
4745 status = smb2_negotiate_context_push(state, &b, c);
4746 if (!NT_STATUS_IS_OK(status)) {
4750 offset = SMB2_HDR_BODY + sizeof(state->smb2.fixed) + dyn.length;
4751 pad = smbXcli_padding_helper(offset, 8);
4753 ok = data_blob_append(state, &dyn, zeros, pad);
4759 ok = data_blob_append(state, &dyn, b.data, b.length);
4764 SIVAL(buf, 28, offset); /* NegotiateContextOffset */
4765 SSVAL(buf, 32, c.num_contexts); /* NegotiateContextCount */
4766 SSVAL(buf, 34, 0); /* Reserved */
4768 SBVAL(buf, 28, 0); /* Reserved/ClientStartTime */
4771 return smb2cli_req_send(state, state->ev,
4772 state->conn, SMB2_OP_NEGPROT,
4774 state->timeout_msec,
4775 NULL, NULL, /* tcon, session */
4776 state->smb2.fixed, sizeof(state->smb2.fixed),
4777 dyn.data, dyn.length,
4778 UINT16_MAX); /* max_dyn_len */
4781 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4783 struct tevent_req *req =
4784 tevent_req_callback_data(subreq,
4786 struct smbXcli_negprot_state *state =
4787 tevent_req_data(req,
4788 struct smbXcli_negprot_state);
4789 struct smbXcli_conn *conn = state->conn;
4790 size_t security_offset, security_length;
4796 uint16_t dialect_revision;
4797 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4798 uint32_t negotiate_context_offset = 0;
4799 uint16_t negotiate_context_count = 0;
4800 DATA_BLOB negotiate_context_blob = data_blob_null;
4804 struct smb2_negotiate_context *preauth = NULL;
4805 uint16_t hash_count;
4806 uint16_t salt_length;
4807 uint16_t hash_selected;
4808 struct hc_sha512state sctx;
4809 struct smb2_negotiate_context *cipher = NULL;
4810 struct iovec sent_iov[3];
4811 static const struct smb2cli_req_expected_response expected[] = {
4813 .status = NT_STATUS_OK,
4818 status = smb2cli_req_recv(subreq, state, &iov,
4819 expected, ARRAY_SIZE(expected));
4820 if (tevent_req_nterror(req, status)) {
4824 body = (uint8_t *)iov[1].iov_base;
4826 dialect_revision = SVAL(body, 4);
4828 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4829 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4833 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4837 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4841 conn->protocol = smb2cli_prots[i].proto;
4845 if (conn->protocol == PROTOCOL_NONE) {
4846 TALLOC_FREE(subreq);
4848 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
4849 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4853 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4854 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4858 /* make sure we do not loop forever */
4859 state->conn->min_protocol = PROTOCOL_SMB2_02;
4862 * send a SMB2 negprot, in order to negotiate
4865 subreq = smbXcli_negprot_smb2_subreq(state);
4866 if (tevent_req_nomem(subreq, req)) {
4869 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4873 conn->smb2.server.security_mode = SVAL(body, 2);
4874 if (conn->protocol >= PROTOCOL_SMB3_10) {
4875 negotiate_context_count = SVAL(body, 6);
4878 blob = data_blob_const(body + 8, 16);
4879 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4880 if (tevent_req_nterror(req, status)) {
4884 conn->smb2.server.capabilities = IVAL(body, 24);
4885 conn->smb2.server.max_trans_size= IVAL(body, 28);
4886 conn->smb2.server.max_read_size = IVAL(body, 32);
4887 conn->smb2.server.max_write_size= IVAL(body, 36);
4888 conn->smb2.server.system_time = BVAL(body, 40);
4889 conn->smb2.server.start_time = BVAL(body, 48);
4891 security_offset = SVAL(body, 56);
4892 security_length = SVAL(body, 58);
4894 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4895 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4899 if (security_length > iov[2].iov_len) {
4900 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4904 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4907 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4911 if (conn->protocol < PROTOCOL_SMB3_10) {
4912 TALLOC_FREE(subreq);
4914 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4915 conn->smb2.server.cipher = SMB2_ENCRYPTION_AES128_CCM;
4917 tevent_req_done(req);
4921 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4922 tevent_req_nterror(req,
4923 NT_STATUS_INVALID_NETWORK_RESPONSE);
4927 negotiate_context_offset = IVAL(body, 60);
4928 if (negotiate_context_offset < security_offset) {
4929 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4933 ctx_ofs = negotiate_context_offset - security_offset;
4934 if (ctx_ofs > iov[2].iov_len) {
4935 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4938 avail = iov[2].iov_len - security_length;
4939 needed = iov[2].iov_len - ctx_ofs;
4940 if (needed > avail) {
4941 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4945 negotiate_context_blob.data = (uint8_t *)iov[2].iov_base;
4946 negotiate_context_blob.length = iov[2].iov_len;
4948 negotiate_context_blob.data += ctx_ofs;
4949 negotiate_context_blob.length -= ctx_ofs;
4951 status = smb2_negotiate_context_parse(state, negotiate_context_blob, &c);
4952 if (tevent_req_nterror(req, status)) {
4956 if (negotiate_context_count != c.num_contexts) {
4957 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4961 preauth = smb2_negotiate_context_find(&c,
4962 SMB2_PREAUTH_INTEGRITY_CAPABILITIES);
4963 if (preauth == NULL) {
4964 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4968 if (preauth->data.length < 6) {
4969 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4973 hash_count = SVAL(preauth->data.data, 0);
4974 salt_length = SVAL(preauth->data.data, 2);
4975 hash_selected = SVAL(preauth->data.data, 4);
4977 if (hash_count != 1) {
4978 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4982 if (preauth->data.length != (6 + salt_length)) {
4983 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4987 if (hash_selected != SMB2_PREAUTH_INTEGRITY_SHA512) {
4988 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4992 cipher = smb2_negotiate_context_find(&c, SMB2_ENCRYPTION_CAPABILITIES);
4993 if (cipher != NULL) {
4994 uint16_t cipher_count;
4996 if (cipher->data.length < 2) {
4997 tevent_req_nterror(req,
4998 NT_STATUS_INVALID_NETWORK_RESPONSE);
5002 cipher_count = SVAL(cipher->data.data, 0);
5004 if (cipher_count > 1) {
5005 tevent_req_nterror(req,
5006 NT_STATUS_INVALID_NETWORK_RESPONSE);
5010 if (cipher->data.length != (2 + 2 * cipher_count)) {
5011 tevent_req_nterror(req,
5012 NT_STATUS_INVALID_NETWORK_RESPONSE);
5016 if (cipher_count == 1) {
5017 uint16_t cipher_selected;
5019 cipher_selected = SVAL(cipher->data.data, 2);
5021 switch (cipher_selected) {
5022 case SMB2_ENCRYPTION_AES128_GCM:
5023 case SMB2_ENCRYPTION_AES128_CCM:
5024 conn->smb2.server.cipher = cipher_selected;
5030 /* First we hash the request */
5031 smb2cli_req_get_sent_iov(subreq, sent_iov);
5032 samba_SHA512_Init(&sctx);
5033 samba_SHA512_Update(&sctx, conn->smb2.preauth_sha512,
5034 sizeof(conn->smb2.preauth_sha512));
5035 for (i = 0; i < 3; i++) {
5036 samba_SHA512_Update(&sctx, sent_iov[i].iov_base, sent_iov[i].iov_len);
5038 samba_SHA512_Final(conn->smb2.preauth_sha512, &sctx);
5039 TALLOC_FREE(subreq);
5041 /* And now we hash the response */
5042 samba_SHA512_Init(&sctx);
5043 samba_SHA512_Update(&sctx, conn->smb2.preauth_sha512,
5044 sizeof(conn->smb2.preauth_sha512));
5045 for (i = 0; i < 3; i++) {
5046 samba_SHA512_Update(&sctx, iov[i].iov_base, iov[i].iov_len);
5048 samba_SHA512_Final(conn->smb2.preauth_sha512, &sctx);
5050 tevent_req_done(req);
5053 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
5054 TALLOC_CTX *tmp_mem,
5057 size_t num_pending = talloc_array_length(conn->pending);
5058 struct tevent_req *subreq;
5059 struct smbXcli_req_state *substate;
5060 struct tevent_req *req;
5061 uint32_t protocol_magic;
5062 size_t inbuf_len = smb_len_nbt(inbuf);
5064 if (num_pending != 1) {
5065 return NT_STATUS_INTERNAL_ERROR;
5068 if (inbuf_len < 4) {
5069 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5072 subreq = conn->pending[0];
5073 substate = tevent_req_data(subreq, struct smbXcli_req_state);
5074 req = tevent_req_callback_data(subreq, struct tevent_req);
5076 protocol_magic = IVAL(inbuf, 4);
5078 switch (protocol_magic) {
5080 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
5081 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
5082 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
5085 if (substate->smb2.recv_iov == NULL) {
5087 * For the SMB1 negprot we have move it.
5089 substate->smb2.recv_iov = substate->smb1.recv_iov;
5090 substate->smb1.recv_iov = NULL;
5094 * we got an SMB2 answer, which consumed sequence number 0
5095 * so we need to use 1 as the next one.
5097 * we also need to set the current credits to 0
5098 * as we consumed the initial one. The SMB2 answer
5099 * hopefully grant us a new credit.
5102 conn->smb2.cur_credits = 0;
5103 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
5104 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
5105 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
5108 DEBUG(10, ("Got non-SMB PDU\n"));
5109 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5112 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
5114 return tevent_req_simple_recv_ntstatus(req);
5117 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
5118 uint32_t timeout_msec,
5119 enum protocol_types min_protocol,
5120 enum protocol_types max_protocol)
5122 TALLOC_CTX *frame = talloc_stackframe();
5123 struct tevent_context *ev;
5124 struct tevent_req *req;
5125 NTSTATUS status = NT_STATUS_NO_MEMORY;
5128 if (smbXcli_conn_has_async_calls(conn)) {
5130 * Can't use sync call while an async call is in flight
5132 status = NT_STATUS_INVALID_PARAMETER_MIX;
5135 ev = samba_tevent_context_init(frame);
5139 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
5140 min_protocol, max_protocol,
5141 WINDOWS_CLIENT_PURE_SMB2_NEGPROT_INITIAL_CREDIT_ASK);
5145 ok = tevent_req_poll_ntstatus(req, ev, &status);
5149 status = smbXcli_negprot_recv(req);
5155 struct smb2cli_validate_negotiate_info_state {
5156 struct smbXcli_conn *conn;
5157 DATA_BLOB in_input_buffer;
5158 DATA_BLOB in_output_buffer;
5159 DATA_BLOB out_input_buffer;
5160 DATA_BLOB out_output_buffer;
5164 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq);
5166 struct tevent_req *smb2cli_validate_negotiate_info_send(TALLOC_CTX *mem_ctx,
5167 struct tevent_context *ev,
5168 struct smbXcli_conn *conn,
5169 uint32_t timeout_msec,
5170 struct smbXcli_session *session,
5171 struct smbXcli_tcon *tcon)
5173 struct tevent_req *req;
5174 struct smb2cli_validate_negotiate_info_state *state;
5176 uint16_t dialect_count = 0;
5177 struct tevent_req *subreq;
5178 bool _save_should_sign;
5181 req = tevent_req_create(mem_ctx, &state,
5182 struct smb2cli_validate_negotiate_info_state);
5188 state->in_input_buffer = data_blob_talloc_zero(state,
5189 4 + 16 + 1 + 1 + 2);
5190 if (tevent_req_nomem(state->in_input_buffer.data, req)) {
5191 return tevent_req_post(req, ev);
5193 buf = state->in_input_buffer.data;
5195 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
5196 SIVAL(buf, 0, conn->smb2.client.capabilities);
5198 SIVAL(buf, 0, 0); /* Capabilities */
5200 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
5204 status = GUID_to_ndr_blob(&conn->smb2.client.guid,
5206 if (!NT_STATUS_IS_OK(status)) {
5209 memcpy(buf+4, blob.data, 16); /* ClientGuid */
5211 memset(buf+4, 0, 16); /* ClientGuid */
5213 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
5214 SCVAL(buf, 20, conn->smb2.client.security_mode);
5218 SCVAL(buf, 21, 0); /* reserved */
5220 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
5224 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
5228 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
5232 if (smb2cli_prots[i].proto == state->conn->protocol) {
5233 state->dialect = smb2cli_prots[i].smb2_dialect;
5236 ofs = state->in_input_buffer.length;
5237 ok = data_blob_realloc(state, &state->in_input_buffer,
5240 tevent_req_oom(req);
5241 return tevent_req_post(req, ev);
5244 buf = state->in_input_buffer.data;
5245 SSVAL(buf, ofs, smb2cli_prots[i].smb2_dialect);
5249 buf = state->in_input_buffer.data;
5250 SSVAL(buf, 22, dialect_count);
5252 _save_should_sign = smb2cli_tcon_is_signing_on(tcon);
5253 smb2cli_tcon_should_sign(tcon, true);
5254 subreq = smb2cli_ioctl_send(state, ev, conn,
5255 timeout_msec, session, tcon,
5256 UINT64_MAX, /* in_fid_persistent */
5257 UINT64_MAX, /* in_fid_volatile */
5258 FSCTL_VALIDATE_NEGOTIATE_INFO,
5259 0, /* in_max_input_length */
5260 &state->in_input_buffer,
5261 24, /* in_max_output_length */
5262 &state->in_output_buffer,
5263 SMB2_IOCTL_FLAG_IS_FSCTL);
5264 smb2cli_tcon_should_sign(tcon, _save_should_sign);
5265 if (tevent_req_nomem(subreq, req)) {
5266 return tevent_req_post(req, ev);
5268 tevent_req_set_callback(subreq,
5269 smb2cli_validate_negotiate_info_done,
5275 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq)
5277 struct tevent_req *req =
5278 tevent_req_callback_data(subreq,
5280 struct smb2cli_validate_negotiate_info_state *state =
5281 tevent_req_data(req,
5282 struct smb2cli_validate_negotiate_info_state);
5285 uint32_t capabilities;
5286 DATA_BLOB guid_blob;
5287 struct GUID server_guid;
5288 uint16_t security_mode;
5291 status = smb2cli_ioctl_recv(subreq, state,
5292 &state->out_input_buffer,
5293 &state->out_output_buffer);
5294 TALLOC_FREE(subreq);
5295 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
5297 * The response was signed, but not supported
5299 * Older Windows and Samba releases return
5300 * NT_STATUS_FILE_CLOSED.
5302 tevent_req_done(req);
5305 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_DEVICE_REQUEST)) {
5307 * The response was signed, but not supported
5309 * This is returned by the NTVFS based Samba 4.x file server
5312 tevent_req_done(req);
5315 if (NT_STATUS_EQUAL(status, NT_STATUS_FS_DRIVER_REQUIRED)) {
5317 * The response was signed, but not supported
5319 * This is returned by the NTVFS based Samba 4.x file server
5322 tevent_req_done(req);
5325 if (tevent_req_nterror(req, status)) {
5329 if (state->out_output_buffer.length != 24) {
5330 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5334 buf = state->out_output_buffer.data;
5336 capabilities = IVAL(buf, 0);
5337 guid_blob = data_blob_const(buf + 4, 16);
5338 status = GUID_from_data_blob(&guid_blob, &server_guid);
5339 if (tevent_req_nterror(req, status)) {
5342 security_mode = CVAL(buf, 20);
5343 dialect = SVAL(buf, 22);
5345 if (capabilities != state->conn->smb2.server.capabilities) {
5346 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5350 if (!GUID_equal(&server_guid, &state->conn->smb2.server.guid)) {
5351 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5355 if (security_mode != state->conn->smb2.server.security_mode) {
5356 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5360 if (dialect != state->dialect) {
5361 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5365 tevent_req_done(req);
5368 NTSTATUS smb2cli_validate_negotiate_info_recv(struct tevent_req *req)
5370 return tevent_req_simple_recv_ntstatus(req);
5373 static int smbXcli_session_destructor(struct smbXcli_session *session)
5375 if (session->conn == NULL) {
5379 DLIST_REMOVE(session->conn->sessions, session);
5383 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
5384 struct smbXcli_conn *conn)
5386 struct smbXcli_session *session;
5388 session = talloc_zero(mem_ctx, struct smbXcli_session);
5389 if (session == NULL) {
5392 session->smb2 = talloc_zero(session, struct smb2cli_session);
5393 if (session->smb2 == NULL) {
5394 talloc_free(session);
5397 talloc_set_destructor(session, smbXcli_session_destructor);
5399 DLIST_ADD_END(conn->sessions, session);
5400 session->conn = conn;
5402 memcpy(session->smb2_channel.preauth_sha512,
5403 conn->smb2.preauth_sha512,
5404 sizeof(session->smb2_channel.preauth_sha512));
5409 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
5410 struct smbXcli_session *src)
5412 struct smbXcli_session *session;
5414 session = talloc_zero(mem_ctx, struct smbXcli_session);
5415 if (session == NULL) {
5418 session->smb2 = talloc_zero(session, struct smb2cli_session);
5419 if (session->smb2 == NULL) {
5420 talloc_free(session);
5424 session->conn = src->conn;
5425 *session->smb2 = *src->smb2;
5426 session->smb2_channel = src->smb2_channel;
5427 session->disconnect_expired = src->disconnect_expired;
5429 DLIST_ADD_END(src->conn->sessions, session);
5430 talloc_set_destructor(session, smbXcli_session_destructor);
5435 bool smbXcli_session_is_guest(struct smbXcli_session *session)
5437 if (session == NULL) {
5441 if (session->conn == NULL) {
5445 if (session->conn->mandatory_signing) {
5449 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5450 if (session->smb2->session_flags & SMB2_SESSION_FLAG_IS_GUEST) {
5456 if (session->smb1.action & SMB_SETUP_GUEST) {
5463 bool smbXcli_session_is_authenticated(struct smbXcli_session *session)
5465 const DATA_BLOB *application_key;
5467 if (session == NULL) {
5471 if (session->conn == NULL) {
5476 * If we have an application key we had a session key negotiated
5479 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5480 application_key = &session->smb2->application_key;
5482 application_key = &session->smb1.application_key;
5485 if (application_key->length == 0) {
5492 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
5493 TALLOC_CTX *mem_ctx,
5496 const DATA_BLOB *application_key;
5498 *key = data_blob_null;
5500 if (session->conn == NULL) {
5501 return NT_STATUS_NO_USER_SESSION_KEY;
5504 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5505 application_key = &session->smb2->application_key;
5507 application_key = &session->smb1.application_key;
5510 if (application_key->length == 0) {
5511 return NT_STATUS_NO_USER_SESSION_KEY;
5514 *key = data_blob_dup_talloc(mem_ctx, *application_key);
5515 if (key->data == NULL) {
5516 return NT_STATUS_NO_MEMORY;
5519 return NT_STATUS_OK;
5522 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
5524 session->disconnect_expired = true;
5527 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
5529 return session->smb1.session_id;
5532 void smb1cli_session_set_id(struct smbXcli_session *session,
5533 uint16_t session_id)
5535 session->smb1.session_id = session_id;
5538 void smb1cli_session_set_action(struct smbXcli_session *session,
5541 session->smb1.action = action;
5544 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
5545 const DATA_BLOB _session_key)
5547 struct smbXcli_conn *conn = session->conn;
5548 uint8_t session_key[16];
5551 return NT_STATUS_INVALID_PARAMETER_MIX;
5554 if (session->smb1.application_key.length != 0) {
5556 * TODO: do not allow this...
5558 * return NT_STATUS_INVALID_PARAMETER_MIX;
5560 data_blob_clear_free(&session->smb1.application_key);
5561 session->smb1.protected_key = false;
5564 if (_session_key.length == 0) {
5565 return NT_STATUS_OK;
5568 ZERO_STRUCT(session_key);
5569 memcpy(session_key, _session_key.data,
5570 MIN(_session_key.length, sizeof(session_key)));
5572 session->smb1.application_key = data_blob_talloc(session,
5574 sizeof(session_key));
5575 ZERO_STRUCT(session_key);
5576 if (session->smb1.application_key.data == NULL) {
5577 return NT_STATUS_NO_MEMORY;
5580 session->smb1.protected_key = false;
5582 return NT_STATUS_OK;
5585 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
5587 if (session->smb1.protected_key) {
5588 /* already protected */
5589 return NT_STATUS_OK;
5592 if (session->smb1.application_key.length != 16) {
5593 return NT_STATUS_INVALID_PARAMETER_MIX;
5596 smb_key_derivation(session->smb1.application_key.data,
5597 session->smb1.application_key.length,
5598 session->smb1.application_key.data);
5600 session->smb1.protected_key = true;
5602 return NT_STATUS_OK;
5605 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
5607 struct smbXcli_conn *conn = session->conn;
5608 uint8_t security_mode = 0;
5611 return security_mode;
5614 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
5615 if (conn->mandatory_signing) {
5616 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
5618 if (session->smb2->should_sign) {
5619 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
5622 return security_mode;
5625 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
5627 return session->smb2->session_id;
5630 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
5632 return session->smb2->session_flags;
5635 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
5636 uint64_t session_id,
5637 uint16_t session_flags)
5639 session->smb2->session_id = session_id;
5640 session->smb2->session_flags = session_flags;
5643 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
5645 session->smb2->channel_sequence += 1;
5648 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session *session,
5649 uint16_t channel_sequence)
5653 prev_cs = session->smb2->channel_sequence;
5654 session->smb2->channel_sequence = channel_sequence;
5659 uint16_t smb2cli_session_current_channel_sequence(struct smbXcli_session *session)
5661 return session->smb2->channel_sequence;
5664 void smb2cli_session_start_replay(struct smbXcli_session *session)
5666 session->smb2->replay_active = true;
5669 void smb2cli_session_stop_replay(struct smbXcli_session *session)
5671 session->smb2->replay_active = false;
5674 NTSTATUS smb2cli_session_update_preauth(struct smbXcli_session *session,
5675 const struct iovec *iov)
5677 struct hc_sha512state sctx;
5680 if (session->conn == NULL) {
5681 return NT_STATUS_INTERNAL_ERROR;
5684 if (session->conn->protocol < PROTOCOL_SMB3_10) {
5685 return NT_STATUS_OK;
5688 if (session->smb2_channel.signing_key.length != 0) {
5689 return NT_STATUS_OK;
5692 samba_SHA512_Init(&sctx);
5693 samba_SHA512_Update(&sctx, session->smb2_channel.preauth_sha512,
5694 sizeof(session->smb2_channel.preauth_sha512));
5695 for (i = 0; i < 3; i++) {
5696 samba_SHA512_Update(&sctx, iov[i].iov_base, iov[i].iov_len);
5698 samba_SHA512_Final(session->smb2_channel.preauth_sha512, &sctx);
5700 return NT_STATUS_OK;
5703 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
5704 const DATA_BLOB _session_key,
5705 const struct iovec *recv_iov)
5707 struct smbXcli_conn *conn = session->conn;
5708 uint16_t no_sign_flags = 0;
5709 uint8_t session_key[16];
5710 bool check_signature = true;
5713 struct _derivation {
5718 struct _derivation signing;
5719 struct _derivation encryption;
5720 struct _derivation decryption;
5721 struct _derivation application;
5723 size_t nonce_size = 0;
5726 return NT_STATUS_INVALID_PARAMETER_MIX;
5729 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
5730 return NT_STATUS_INVALID_PARAMETER_MIX;
5733 if (!conn->mandatory_signing) {
5735 * only allow guest sessions without
5736 * mandatory signing.
5738 * If we try an authentication with username != ""
5739 * and the server let us in without verifying the
5740 * password we don't have a negotiated session key
5743 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST;
5746 if (session->smb2->session_flags & no_sign_flags) {
5747 session->smb2->should_sign = false;
5748 return NT_STATUS_OK;
5751 if (session->smb2->signing_key.length != 0) {
5752 return NT_STATUS_INVALID_PARAMETER_MIX;
5755 if (conn->protocol >= PROTOCOL_SMB3_10) {
5756 struct _derivation *d;
5759 p = data_blob_const(session->smb2_channel.preauth_sha512,
5760 sizeof(session->smb2_channel.preauth_sha512));
5762 d = &derivation.signing;
5763 d->label = data_blob_string_const_null("SMBSigningKey");
5766 d = &derivation.encryption;
5767 d->label = data_blob_string_const_null("SMBC2SCipherKey");
5770 d = &derivation.decryption;
5771 d->label = data_blob_string_const_null("SMBS2CCipherKey");
5774 d = &derivation.application;
5775 d->label = data_blob_string_const_null("SMBAppKey");
5778 } else if (conn->protocol >= PROTOCOL_SMB2_24) {
5779 struct _derivation *d;
5781 d = &derivation.signing;
5782 d->label = data_blob_string_const_null("SMB2AESCMAC");
5783 d->context = data_blob_string_const_null("SmbSign");
5785 d = &derivation.encryption;
5786 d->label = data_blob_string_const_null("SMB2AESCCM");
5787 d->context = data_blob_string_const_null("ServerIn ");
5789 d = &derivation.decryption;
5790 d->label = data_blob_string_const_null("SMB2AESCCM");
5791 d->context = data_blob_string_const_null("ServerOut");
5793 d = &derivation.application;
5794 d->label = data_blob_string_const_null("SMB2APP");
5795 d->context = data_blob_string_const_null("SmbRpc");
5798 ZERO_STRUCT(session_key);
5799 memcpy(session_key, _session_key.data,
5800 MIN(_session_key.length, sizeof(session_key)));
5802 session->smb2->signing_key = data_blob_talloc(session,
5804 sizeof(session_key));
5805 if (session->smb2->signing_key.data == NULL) {
5806 ZERO_STRUCT(session_key);
5807 return NT_STATUS_NO_MEMORY;
5810 if (conn->protocol >= PROTOCOL_SMB2_24) {
5811 struct _derivation *d = &derivation.signing;
5813 smb2_key_derivation(session_key, sizeof(session_key),
5814 d->label.data, d->label.length,
5815 d->context.data, d->context.length,
5816 session->smb2->signing_key.data);
5819 session->smb2->encryption_key = data_blob_dup_talloc(session,
5820 session->smb2->signing_key);
5821 if (session->smb2->encryption_key.data == NULL) {
5822 ZERO_STRUCT(session_key);
5823 return NT_STATUS_NO_MEMORY;
5826 if (conn->protocol >= PROTOCOL_SMB2_24) {
5827 struct _derivation *d = &derivation.encryption;
5829 smb2_key_derivation(session_key, sizeof(session_key),
5830 d->label.data, d->label.length,
5831 d->context.data, d->context.length,
5832 session->smb2->encryption_key.data);
5835 session->smb2->decryption_key = data_blob_dup_talloc(session,
5836 session->smb2->signing_key);
5837 if (session->smb2->decryption_key.data == NULL) {
5838 ZERO_STRUCT(session_key);
5839 return NT_STATUS_NO_MEMORY;
5842 if (conn->protocol >= PROTOCOL_SMB2_24) {
5843 struct _derivation *d = &derivation.decryption;
5845 smb2_key_derivation(session_key, sizeof(session_key),
5846 d->label.data, d->label.length,
5847 d->context.data, d->context.length,
5848 session->smb2->decryption_key.data);
5851 session->smb2->application_key = data_blob_dup_talloc(session,
5852 session->smb2->signing_key);
5853 if (session->smb2->application_key.data == NULL) {
5854 ZERO_STRUCT(session_key);
5855 return NT_STATUS_NO_MEMORY;
5858 if (conn->protocol >= PROTOCOL_SMB2_24) {
5859 struct _derivation *d = &derivation.application;
5861 smb2_key_derivation(session_key, sizeof(session_key),
5862 d->label.data, d->label.length,
5863 d->context.data, d->context.length,
5864 session->smb2->application_key.data);
5866 ZERO_STRUCT(session_key);
5868 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
5869 session->smb2->signing_key);
5870 if (session->smb2_channel.signing_key.data == NULL) {
5871 return NT_STATUS_NO_MEMORY;
5874 check_signature = conn->mandatory_signing;
5876 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
5877 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
5879 * Sadly some vendors don't sign the
5880 * final SMB2 session setup response
5882 * At least Windows and Samba are always doing this
5883 * if there's a session key available.
5885 * We only check the signature if it's mandatory
5886 * or SMB2_HDR_FLAG_SIGNED is provided.
5888 check_signature = true;
5891 if (conn->protocol >= PROTOCOL_SMB3_10) {
5892 check_signature = true;
5895 if (check_signature) {
5896 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5897 session->conn->protocol,
5899 if (!NT_STATUS_IS_OK(status)) {
5904 session->smb2->should_sign = false;
5905 session->smb2->should_encrypt = false;
5907 if (conn->desire_signing) {
5908 session->smb2->should_sign = true;
5911 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
5912 session->smb2->should_sign = true;
5915 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
5916 session->smb2->should_encrypt = true;
5919 if (conn->protocol < PROTOCOL_SMB2_24) {
5920 session->smb2->should_encrypt = false;
5923 if (conn->smb2.server.cipher == 0) {
5924 session->smb2->should_encrypt = false;
5928 * CCM and GCM algorithms must never have their
5929 * nonce wrap, or the security of the whole
5930 * communication and the keys is destroyed.
5931 * We must drop the connection once we have
5932 * transfered too much data.
5934 * NOTE: We assume nonces greater than 8 bytes.
5936 generate_random_buffer((uint8_t *)&session->smb2->nonce_high_random,
5937 sizeof(session->smb2->nonce_high_random));
5938 switch (conn->smb2.server.cipher) {
5939 case SMB2_ENCRYPTION_AES128_CCM:
5940 nonce_size = AES_CCM_128_NONCE_SIZE;
5942 case SMB2_ENCRYPTION_AES128_GCM:
5943 nonce_size = AES_GCM_128_IV_SIZE;
5949 session->smb2->nonce_high_max = SMB2_NONCE_HIGH_MAX(nonce_size);
5950 session->smb2->nonce_high = 0;
5951 session->smb2->nonce_low = 0;
5953 return NT_STATUS_OK;
5956 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
5957 struct smbXcli_session *session1,
5958 struct smbXcli_conn *conn,
5959 struct smbXcli_session **_session2)
5961 struct smbXcli_session *session2;
5963 if (session1->smb2->signing_key.length == 0) {
5964 return NT_STATUS_INVALID_PARAMETER_MIX;
5968 return NT_STATUS_INVALID_PARAMETER_MIX;
5971 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
5972 if (session2 == NULL) {
5973 return NT_STATUS_NO_MEMORY;
5975 session2->smb2 = talloc_reference(session2, session1->smb2);
5976 if (session2->smb2 == NULL) {
5977 talloc_free(session2);
5978 return NT_STATUS_NO_MEMORY;
5981 talloc_set_destructor(session2, smbXcli_session_destructor);
5982 DLIST_ADD_END(conn->sessions, session2);
5983 session2->conn = conn;
5985 memcpy(session2->smb2_channel.preauth_sha512,
5986 conn->smb2.preauth_sha512,
5987 sizeof(session2->smb2_channel.preauth_sha512));
5989 *_session2 = session2;
5990 return NT_STATUS_OK;
5993 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
5994 const DATA_BLOB _channel_key,
5995 const struct iovec *recv_iov)
5997 struct smbXcli_conn *conn = session->conn;
5998 uint8_t channel_key[16];
6000 struct _derivation {
6005 struct _derivation signing;
6009 return NT_STATUS_INVALID_PARAMETER_MIX;
6012 if (session->smb2_channel.signing_key.length != 0) {
6013 return NT_STATUS_INVALID_PARAMETER_MIX;
6016 if (conn->protocol >= PROTOCOL_SMB3_10) {
6017 struct _derivation *d;
6020 p = data_blob_const(session->smb2_channel.preauth_sha512,
6021 sizeof(session->smb2_channel.preauth_sha512));
6023 d = &derivation.signing;
6024 d->label = data_blob_string_const_null("SMBSigningKey");
6026 } else if (conn->protocol >= PROTOCOL_SMB2_24) {
6027 struct _derivation *d;
6029 d = &derivation.signing;
6030 d->label = data_blob_string_const_null("SMB2AESCMAC");
6031 d->context = data_blob_string_const_null("SmbSign");
6034 ZERO_STRUCT(channel_key);
6035 memcpy(channel_key, _channel_key.data,
6036 MIN(_channel_key.length, sizeof(channel_key)));
6038 session->smb2_channel.signing_key = data_blob_talloc(session,
6040 sizeof(channel_key));
6041 if (session->smb2_channel.signing_key.data == NULL) {
6042 ZERO_STRUCT(channel_key);
6043 return NT_STATUS_NO_MEMORY;
6046 if (conn->protocol >= PROTOCOL_SMB2_24) {
6047 struct _derivation *d = &derivation.signing;
6049 smb2_key_derivation(channel_key, sizeof(channel_key),
6050 d->label.data, d->label.length,
6051 d->context.data, d->context.length,
6052 session->smb2_channel.signing_key.data);
6054 ZERO_STRUCT(channel_key);
6056 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
6057 session->conn->protocol,
6059 if (!NT_STATUS_IS_OK(status)) {
6063 return NT_STATUS_OK;
6066 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
6068 if (!session->smb2->should_sign) {
6070 * We need required signing on the session
6071 * in order to prevent man in the middle attacks.
6073 return NT_STATUS_INVALID_PARAMETER_MIX;
6076 if (session->smb2->should_encrypt) {
6077 return NT_STATUS_OK;
6080 if (session->conn->protocol < PROTOCOL_SMB2_24) {
6081 return NT_STATUS_NOT_SUPPORTED;
6084 if (session->conn->smb2.server.cipher == 0) {
6085 return NT_STATUS_NOT_SUPPORTED;
6088 if (session->smb2->signing_key.data == NULL) {
6089 return NT_STATUS_NOT_SUPPORTED;
6091 session->smb2->should_encrypt = true;
6092 return NT_STATUS_OK;
6095 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
6097 struct smbXcli_tcon *tcon;
6099 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
6107 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
6108 uint32_t fs_attributes)
6110 tcon->fs_attributes = fs_attributes;
6113 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
6115 return tcon->fs_attributes;
6118 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
6124 if (tcon->is_smb1) {
6125 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
6132 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
6139 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
6141 return tcon->smb1.tcon_id;
6144 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
6146 tcon->is_smb1 = true;
6147 tcon->smb1.tcon_id = tcon_id;
6150 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
6152 uint16_t optional_support,
6153 uint32_t maximal_access,
6154 uint32_t guest_maximal_access,
6155 const char *service,
6156 const char *fs_type)
6158 tcon->is_smb1 = true;
6159 tcon->fs_attributes = 0;
6160 tcon->smb1.tcon_id = tcon_id;
6161 tcon->smb1.optional_support = optional_support;
6162 tcon->smb1.maximal_access = maximal_access;
6163 tcon->smb1.guest_maximal_access = guest_maximal_access;
6165 TALLOC_FREE(tcon->smb1.service);
6166 tcon->smb1.service = talloc_strdup(tcon, service);
6167 if (service != NULL && tcon->smb1.service == NULL) {
6171 TALLOC_FREE(tcon->smb1.fs_type);
6172 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
6173 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
6180 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
6182 return tcon->smb2.tcon_id;
6185 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
6187 return tcon->smb2.capabilities;
6190 uint32_t smb2cli_tcon_flags(struct smbXcli_tcon *tcon)
6192 return tcon->smb2.flags;
6195 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
6196 struct smbXcli_session *session,
6200 uint32_t capabilities,
6201 uint32_t maximal_access)
6203 tcon->is_smb1 = false;
6204 tcon->fs_attributes = 0;
6205 tcon->smb2.tcon_id = tcon_id;
6206 tcon->smb2.type = type;
6207 tcon->smb2.flags = flags;
6208 tcon->smb2.capabilities = capabilities;
6209 tcon->smb2.maximal_access = maximal_access;
6211 tcon->smb2.should_sign = false;
6212 tcon->smb2.should_encrypt = false;
6214 if (session == NULL) {
6218 tcon->smb2.should_sign = session->smb2->should_sign;
6219 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
6221 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
6222 tcon->smb2.should_encrypt = true;
6226 void smb2cli_tcon_should_sign(struct smbXcli_tcon *tcon,
6229 tcon->smb2.should_sign = should_sign;
6232 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon *tcon)
6234 if (tcon->smb2.should_encrypt) {
6238 return tcon->smb2.should_sign;
6241 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon *tcon,
6242 bool should_encrypt)
6244 tcon->smb2.should_encrypt = should_encrypt;
6247 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
6249 return tcon->smb2.should_encrypt;