2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Stefan Metzmacher 2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/network.h"
23 #include "../lib/async_req/async_sock.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
26 #include "lib/util/util_net.h"
27 #include "lib/util/dlinklist.h"
28 #include "../libcli/smb/smb_common.h"
29 #include "../libcli/smb/smb_seal.h"
30 #include "../libcli/smb/smb_signing.h"
31 #include "../libcli/smb/read_smb.h"
32 #include "smbXcli_base.h"
33 #include "librpc/ndr/libndr.h"
37 struct smbXcli_session;
43 struct sockaddr_storage local_ss;
44 struct sockaddr_storage remote_ss;
45 const char *remote_name;
47 struct tevent_queue *outgoing;
48 struct tevent_req **pending;
49 struct tevent_req *read_smb_req;
51 enum protocol_types protocol;
54 bool mandatory_signing;
57 * The incoming dispatch function should return:
58 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
59 * - NT_STATUS_OK, if no more processing is desired, e.g.
60 * the dispatch function called
62 * - All other return values disconnect the connection.
64 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
70 uint32_t capabilities;
75 uint32_t capabilities;
78 uint16_t security_mode;
87 const char *workgroup;
93 uint32_t capabilities;
98 struct smb_signing_state *signing;
99 struct smb_trans_enc_state *trans_enc;
101 struct tevent_req *read_braw_req;
106 uint32_t capabilities;
107 uint16_t security_mode;
112 uint32_t capabilities;
113 uint16_t security_mode;
115 uint32_t max_trans_size;
116 uint32_t max_read_size;
117 uint32_t max_write_size;
124 uint16_t cur_credits;
125 uint16_t max_credits;
128 struct smbXcli_session *sessions;
131 struct smb2cli_session {
133 uint16_t session_flags;
134 DATA_BLOB application_key;
135 DATA_BLOB signing_key;
138 DATA_BLOB encryption_key;
139 DATA_BLOB decryption_key;
142 uint16_t channel_sequence;
145 struct smbXcli_session {
146 struct smbXcli_session *prev, *next;
147 struct smbXcli_conn *conn;
151 DATA_BLOB application_key;
155 struct smb2cli_session *smb2;
158 DATA_BLOB signing_key;
162 * this should be a short term hack
163 * until the upper layers have implemented
166 bool disconnect_expired;
169 struct smbXcli_tcon {
172 uint16_t optional_support;
173 uint32_t maximal_access;
174 uint32_t guest_maximal_access;
183 uint32_t capabilities;
184 uint32_t maximal_access;
189 struct smbXcli_req_state {
190 struct tevent_context *ev;
191 struct smbXcli_conn *conn;
192 struct smbXcli_session *session; /* maybe NULL */
193 struct smbXcli_tcon *tcon; /* maybe NULL */
195 uint8_t length_hdr[4];
202 /* Space for the header including the wct */
203 uint8_t hdr[HDR_VWV];
206 * For normal requests, smb1cli_req_send chooses a mid.
207 * SecondaryV trans requests need to use the mid of the primary
208 * request, so we need a place to store it.
209 * Assume it is set if != 0.
214 uint8_t bytecount_buf[2];
216 #define MAX_SMB_IOV 10
217 /* length_hdr, hdr, words, byte_count, buffers */
218 struct iovec iov[1 + 3 + MAX_SMB_IOV];
223 struct tevent_req **chained_requests;
226 NTSTATUS recv_status;
227 /* always an array of 3 talloc elements */
228 struct iovec *recv_iov;
232 const uint8_t *fixed;
237 uint8_t transform[SMB2_TF_HDR_SIZE];
238 uint8_t hdr[SMB2_HDR_BODY];
239 uint8_t pad[7]; /* padding space for compounding */
242 * always an array of 3 talloc elements
243 * (without a SMB2_TRANSFORM header!)
247 struct iovec *recv_iov;
249 uint16_t credit_charge;
253 uint64_t encryption_session_id;
255 bool signing_skipped;
261 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
264 * NT_STATUS_OK, means we do not notify the callers
266 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
268 while (conn->sessions) {
269 conn->sessions->conn = NULL;
270 DLIST_REMOVE(conn->sessions, conn->sessions);
273 if (conn->smb1.trans_enc) {
274 TALLOC_FREE(conn->smb1.trans_enc);
280 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
282 const char *remote_name,
283 enum smb_signing_setting signing_state,
284 uint32_t smb1_capabilities,
285 struct GUID *client_guid,
286 uint32_t smb2_capabilities)
288 struct smbXcli_conn *conn = NULL;
290 struct sockaddr *sa = NULL;
294 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
300 conn->write_fd = dup(fd);
301 if (conn->write_fd == -1) {
305 conn->remote_name = talloc_strdup(conn, remote_name);
306 if (conn->remote_name == NULL) {
311 ss = (void *)&conn->local_ss;
312 sa = (struct sockaddr *)ss;
313 sa_length = sizeof(conn->local_ss);
314 ret = getsockname(fd, sa, &sa_length);
318 ss = (void *)&conn->remote_ss;
319 sa = (struct sockaddr *)ss;
320 sa_length = sizeof(conn->remote_ss);
321 ret = getpeername(fd, sa, &sa_length);
326 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
327 if (conn->outgoing == NULL) {
330 conn->pending = NULL;
332 conn->protocol = PROTOCOL_NONE;
334 switch (signing_state) {
335 case SMB_SIGNING_OFF:
337 conn->allow_signing = false;
338 conn->desire_signing = false;
339 conn->mandatory_signing = false;
341 case SMB_SIGNING_DEFAULT:
342 case SMB_SIGNING_IF_REQUIRED:
343 /* if the server requires it */
344 conn->allow_signing = true;
345 conn->desire_signing = false;
346 conn->mandatory_signing = false;
348 case SMB_SIGNING_REQUIRED:
350 conn->allow_signing = true;
351 conn->desire_signing = true;
352 conn->mandatory_signing = true;
356 conn->smb1.client.capabilities = smb1_capabilities;
357 conn->smb1.client.max_xmit = UINT16_MAX;
359 conn->smb1.capabilities = conn->smb1.client.capabilities;
360 conn->smb1.max_xmit = 1024;
364 /* initialise signing */
365 conn->smb1.signing = smb_signing_init(conn,
367 conn->desire_signing,
368 conn->mandatory_signing);
369 if (!conn->smb1.signing) {
373 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
374 if (conn->mandatory_signing) {
375 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
378 conn->smb2.client.guid = *client_guid;
380 conn->smb2.client.capabilities = smb2_capabilities;
382 conn->smb2.cur_credits = 1;
383 conn->smb2.max_credits = 0;
385 talloc_set_destructor(conn, smbXcli_conn_destructor);
389 if (conn->write_fd != -1) {
390 close(conn->write_fd);
396 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
402 if (conn->read_fd == -1) {
409 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
411 return conn->protocol;
414 bool smbXcli_conn_nt_smbs_supported(struct smbXcli_conn *conn)
416 if (conn->protocol >= PROTOCOL_SMB2_02) {
420 if (smb1cli_conn_capabilities(conn) & CAP_NT_SMBS) {
427 bool smbXcli_conn_use_status32(struct smbXcli_conn *conn)
429 if (conn->protocol >= PROTOCOL_SMB2_02) {
433 if (smb1cli_conn_capabilities(conn) & CAP_STATUS32) {
440 bool smbXcli_conn_use_large_files(struct smbXcli_conn *conn)
442 if (conn->protocol >= PROTOCOL_SMB2_02) {
446 if (smb1cli_conn_capabilities(conn) & CAP_LARGE_FILES) {
453 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
455 if (conn->protocol >= PROTOCOL_SMB2_02) {
459 if (conn->smb1.capabilities & CAP_UNICODE) {
466 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
468 set_socket_options(conn->read_fd, options);
471 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
473 return &conn->local_ss;
476 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
478 return &conn->remote_ss;
481 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
483 return conn->remote_name;
486 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
488 if (conn->protocol >= PROTOCOL_SMB2_02) {
495 return conn->smb1.server.max_mux;
498 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
500 if (conn->protocol >= PROTOCOL_SMB2_02) {
501 return conn->smb2.server.system_time;
504 return conn->smb1.server.system_time;
507 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
509 if (conn->protocol >= PROTOCOL_SMB2_02) {
510 return &conn->smb2.server.gss_blob;
513 return &conn->smb1.server.gss_blob;
516 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
518 if (conn->protocol >= PROTOCOL_SMB2_02) {
519 return &conn->smb2.server.guid;
522 return &conn->smb1.server.guid;
525 struct smbXcli_conn_samba_suicide_state {
526 struct smbXcli_conn *conn;
531 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
533 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
534 struct tevent_context *ev,
535 struct smbXcli_conn *conn,
538 struct tevent_req *req, *subreq;
539 struct smbXcli_conn_samba_suicide_state *state;
541 req = tevent_req_create(mem_ctx, &state,
542 struct smbXcli_conn_samba_suicide_state);
547 SIVAL(state->buf, 4, 0x74697865);
548 SCVAL(state->buf, 8, exitcode);
549 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
551 state->iov.iov_base = state->buf;
552 state->iov.iov_len = sizeof(state->buf);
554 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
555 false, &state->iov, 1);
556 if (tevent_req_nomem(subreq, req)) {
557 return tevent_req_post(req, ev);
559 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
563 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
565 struct tevent_req *req = tevent_req_callback_data(
566 subreq, struct tevent_req);
567 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
568 req, struct smbXcli_conn_samba_suicide_state);
572 nwritten = writev_recv(subreq, &err);
574 if (nwritten == -1) {
575 NTSTATUS status = map_nt_error_from_unix_common(err);
576 smbXcli_conn_disconnect(state->conn, status);
579 tevent_req_done(req);
582 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
584 return tevent_req_simple_recv_ntstatus(req);
587 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
590 TALLOC_CTX *frame = talloc_stackframe();
591 struct tevent_context *ev;
592 struct tevent_req *req;
593 NTSTATUS status = NT_STATUS_NO_MEMORY;
596 if (smbXcli_conn_has_async_calls(conn)) {
598 * Can't use sync call while an async call is in flight
600 status = NT_STATUS_INVALID_PARAMETER_MIX;
603 ev = samba_tevent_context_init(frame);
607 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
611 ok = tevent_req_poll(req, ev);
613 status = map_nt_error_from_unix_common(errno);
616 status = smbXcli_conn_samba_suicide_recv(req);
622 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
624 return conn->smb1.capabilities;
627 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
629 return conn->smb1.max_xmit;
632 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
634 return conn->smb1.server.session_key;
637 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
639 return conn->smb1.server.challenge;
642 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
644 return conn->smb1.server.security_mode;
647 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
649 return conn->smb1.server.readbraw;
652 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
654 return conn->smb1.server.writebraw;
657 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
659 return conn->smb1.server.lockread;
662 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
664 return conn->smb1.server.writeunlock;
667 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
669 return conn->smb1.server.time_zone;
672 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
673 const DATA_BLOB user_session_key,
674 const DATA_BLOB response)
676 return smb_signing_activate(conn->smb1.signing,
681 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
682 const uint8_t *buf, uint32_t seqnum)
684 const uint8_t *hdr = buf + NBT_HDR_SIZE;
685 size_t len = smb_len_nbt(buf);
687 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
690 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
692 return smb_signing_is_active(conn->smb1.signing);
695 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
696 struct smb_trans_enc_state *es)
698 /* Replace the old state, if any. */
699 if (conn->smb1.trans_enc) {
700 TALLOC_FREE(conn->smb1.trans_enc);
702 conn->smb1.trans_enc = es;
705 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
707 return common_encryption_on(conn->smb1.trans_enc);
711 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
713 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
714 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
716 if (NT_STATUS_IS_OK(status)) {
720 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
724 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
728 * Is the SMB command able to hold an AND_X successor
729 * @param[in] cmd The SMB command in question
730 * @retval Can we add a chained request after "cmd"?
732 bool smb1cli_is_andx_req(uint8_t cmd)
752 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
754 size_t num_pending = talloc_array_length(conn->pending);
760 result = conn->smb1.mid++;
761 if ((result == 0) || (result == 0xffff)) {
765 for (i=0; i<num_pending; i++) {
766 if (result == smb1cli_req_mid(conn->pending[i])) {
771 if (i == num_pending) {
777 void smbXcli_req_unset_pending(struct tevent_req *req)
779 struct smbXcli_req_state *state =
781 struct smbXcli_req_state);
782 struct smbXcli_conn *conn = state->conn;
783 size_t num_pending = talloc_array_length(conn->pending);
786 if (state->smb1.mid != 0) {
788 * This is a [nt]trans[2] request which waits
789 * for more than one reply.
794 talloc_set_destructor(req, NULL);
796 if (num_pending == 1) {
798 * The pending read_smb tevent_req is a child of
799 * conn->pending. So if nothing is pending anymore, we need to
800 * delete the socket read fde.
802 TALLOC_FREE(conn->pending);
803 conn->read_smb_req = NULL;
807 for (i=0; i<num_pending; i++) {
808 if (req == conn->pending[i]) {
812 if (i == num_pending) {
814 * Something's seriously broken. Just returning here is the
815 * right thing nevertheless, the point of this routine is to
816 * remove ourselves from conn->pending.
822 * Remove ourselves from the conn->pending array
824 for (; i < (num_pending - 1); i++) {
825 conn->pending[i] = conn->pending[i+1];
829 * No NULL check here, we're shrinking by sizeof(void *), and
830 * talloc_realloc just adjusts the size for this.
832 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
837 static int smbXcli_req_destructor(struct tevent_req *req)
839 struct smbXcli_req_state *state =
841 struct smbXcli_req_state);
844 * Make sure we really remove it from
845 * the pending array on destruction.
848 smbXcli_req_unset_pending(req);
852 static bool smb1cli_req_cancel(struct tevent_req *req);
853 static bool smb2cli_req_cancel(struct tevent_req *req);
855 static bool smbXcli_req_cancel(struct tevent_req *req)
857 struct smbXcli_req_state *state =
859 struct smbXcli_req_state);
861 if (!smbXcli_conn_is_connected(state->conn)) {
865 if (state->conn->protocol == PROTOCOL_NONE) {
869 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
870 return smb2cli_req_cancel(req);
873 return smb1cli_req_cancel(req);
876 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
878 bool smbXcli_req_set_pending(struct tevent_req *req)
880 struct smbXcli_req_state *state =
882 struct smbXcli_req_state);
883 struct smbXcli_conn *conn;
884 struct tevent_req **pending;
889 if (!smbXcli_conn_is_connected(conn)) {
893 num_pending = talloc_array_length(conn->pending);
895 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
897 if (pending == NULL) {
900 pending[num_pending] = req;
901 conn->pending = pending;
902 talloc_set_destructor(req, smbXcli_req_destructor);
903 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
905 if (!smbXcli_conn_receive_next(conn)) {
907 * the caller should notify the current request
909 * And all other pending requests get notified
910 * by smbXcli_conn_disconnect().
912 smbXcli_req_unset_pending(req);
913 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
920 static void smbXcli_conn_received(struct tevent_req *subreq);
922 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
924 size_t num_pending = talloc_array_length(conn->pending);
925 struct tevent_req *req;
926 struct smbXcli_req_state *state;
928 if (conn->read_smb_req != NULL) {
932 if (num_pending == 0) {
933 if (conn->smb2.mid < UINT64_MAX) {
934 /* no more pending requests, so we are done for now */
939 * If there are no more SMB2 requests possible,
940 * because we are out of message ids,
941 * we need to disconnect.
943 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
947 req = conn->pending[0];
948 state = tevent_req_data(req, struct smbXcli_req_state);
951 * We're the first ones, add the read_smb request that waits for the
952 * answer from the server
954 conn->read_smb_req = read_smb_send(conn->pending,
957 if (conn->read_smb_req == NULL) {
960 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
964 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
966 struct smbXcli_session *session;
968 tevent_queue_stop(conn->outgoing);
970 if (conn->read_fd != -1) {
971 close(conn->read_fd);
973 if (conn->write_fd != -1) {
974 close(conn->write_fd);
979 session = conn->sessions;
980 if (talloc_array_length(conn->pending) == 0) {
982 * if we do not have pending requests
983 * there is no need to update the channel_sequence
987 for (; session; session = session->next) {
988 smb2cli_session_increment_channel_sequence(session);
992 * Cancel all pending requests. We do not do a for-loop walking
993 * conn->pending because that array changes in
994 * smbXcli_req_unset_pending.
996 while (talloc_array_length(conn->pending) > 0) {
997 struct tevent_req *req;
998 struct smbXcli_req_state *state;
999 struct tevent_req **chain;
1003 req = conn->pending[0];
1004 state = tevent_req_data(req, struct smbXcli_req_state);
1006 if (state->smb1.chained_requests == NULL) {
1008 * We're dead. No point waiting for trans2
1011 state->smb1.mid = 0;
1013 smbXcli_req_unset_pending(req);
1015 if (NT_STATUS_IS_OK(status)) {
1016 /* do not notify the callers */
1021 * we need to defer the callback, because we may notify
1022 * more then one caller.
1024 tevent_req_defer_callback(req, state->ev);
1025 tevent_req_nterror(req, status);
1029 chain = talloc_move(conn, &state->smb1.chained_requests);
1030 num_chained = talloc_array_length(chain);
1032 for (i=0; i<num_chained; i++) {
1034 state = tevent_req_data(req, struct smbXcli_req_state);
1037 * We're dead. No point waiting for trans2
1040 state->smb1.mid = 0;
1042 smbXcli_req_unset_pending(req);
1044 if (NT_STATUS_IS_OK(status)) {
1045 /* do not notify the callers */
1050 * we need to defer the callback, because we may notify
1051 * more than one caller.
1053 tevent_req_defer_callback(req, state->ev);
1054 tevent_req_nterror(req, status);
1061 * Fetch a smb request's mid. Only valid after the request has been sent by
1062 * smb1cli_req_send().
1064 uint16_t smb1cli_req_mid(struct tevent_req *req)
1066 struct smbXcli_req_state *state =
1067 tevent_req_data(req,
1068 struct smbXcli_req_state);
1070 if (state->smb1.mid != 0) {
1071 return state->smb1.mid;
1074 return SVAL(state->smb1.hdr, HDR_MID);
1077 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1079 struct smbXcli_req_state *state =
1080 tevent_req_data(req,
1081 struct smbXcli_req_state);
1083 state->smb1.mid = mid;
1086 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1088 struct smbXcli_req_state *state =
1089 tevent_req_data(req,
1090 struct smbXcli_req_state);
1092 return state->smb1.seqnum;
1095 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1097 struct smbXcli_req_state *state =
1098 tevent_req_data(req,
1099 struct smbXcli_req_state);
1101 state->smb1.seqnum = seqnum;
1104 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1108 for (i=0; i<count; i++) {
1109 result += iov[i].iov_len;
1114 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1115 const struct iovec *iov,
1118 size_t len = smbXcli_iov_len(iov, count);
1123 buf = talloc_array(mem_ctx, uint8_t, len);
1128 for (i=0; i<count; i++) {
1129 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1130 copied += iov[i].iov_len;
1135 static void smb1cli_req_flags(enum protocol_types protocol,
1136 uint32_t smb1_capabilities,
1137 uint8_t smb_command,
1138 uint8_t additional_flags,
1139 uint8_t clear_flags,
1141 uint16_t additional_flags2,
1142 uint16_t clear_flags2,
1146 uint16_t flags2 = 0;
1148 if (protocol >= PROTOCOL_LANMAN1) {
1149 flags |= FLAG_CASELESS_PATHNAMES;
1150 flags |= FLAG_CANONICAL_PATHNAMES;
1153 if (protocol >= PROTOCOL_LANMAN2) {
1154 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1155 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1158 if (protocol >= PROTOCOL_NT1) {
1159 flags2 |= FLAGS2_IS_LONG_NAME;
1161 if (smb1_capabilities & CAP_UNICODE) {
1162 flags2 |= FLAGS2_UNICODE_STRINGS;
1164 if (smb1_capabilities & CAP_STATUS32) {
1165 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1167 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1168 flags2 |= FLAGS2_EXTENDED_SECURITY;
1172 flags |= additional_flags;
1173 flags &= ~clear_flags;
1174 flags2 |= additional_flags2;
1175 flags2 &= ~clear_flags2;
1181 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1183 static bool smb1cli_req_cancel(struct tevent_req *req)
1185 struct smbXcli_req_state *state =
1186 tevent_req_data(req,
1187 struct smbXcli_req_state);
1192 struct tevent_req *subreq;
1195 flags = CVAL(state->smb1.hdr, HDR_FLG);
1196 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1197 pid = SVAL(state->smb1.hdr, HDR_PID);
1198 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1199 mid = SVAL(state->smb1.hdr, HDR_MID);
1201 subreq = smb1cli_req_create(state, state->ev,
1211 0, NULL); /* bytes */
1212 if (subreq == NULL) {
1215 smb1cli_req_set_mid(subreq, mid);
1217 status = smb1cli_req_chain_submit(&subreq, 1);
1218 if (!NT_STATUS_IS_OK(status)) {
1219 TALLOC_FREE(subreq);
1222 smb1cli_req_set_mid(subreq, 0);
1224 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1229 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1231 /* we do not care about the result */
1232 TALLOC_FREE(subreq);
1235 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1236 struct tevent_context *ev,
1237 struct smbXcli_conn *conn,
1238 uint8_t smb_command,
1239 uint8_t additional_flags,
1240 uint8_t clear_flags,
1241 uint16_t additional_flags2,
1242 uint16_t clear_flags2,
1243 uint32_t timeout_msec,
1245 struct smbXcli_tcon *tcon,
1246 struct smbXcli_session *session,
1247 uint8_t wct, uint16_t *vwv,
1249 struct iovec *bytes_iov)
1251 struct tevent_req *req;
1252 struct smbXcli_req_state *state;
1254 uint16_t flags2 = 0;
1258 if (iov_count > MAX_SMB_IOV) {
1260 * Should not happen :-)
1265 req = tevent_req_create(mem_ctx, &state,
1266 struct smbXcli_req_state);
1272 state->session = session;
1276 uid = session->smb1.session_id;
1280 tid = tcon->smb1.tcon_id;
1283 state->smb1.recv_cmd = 0xFF;
1284 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1285 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1286 if (state->smb1.recv_iov == NULL) {
1291 smb1cli_req_flags(conn->protocol,
1292 conn->smb1.capabilities,
1301 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1302 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1303 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1304 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1305 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1306 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1307 SSVAL(state->smb1.hdr, HDR_TID, tid);
1308 SSVAL(state->smb1.hdr, HDR_PID, pid);
1309 SSVAL(state->smb1.hdr, HDR_UID, uid);
1310 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1311 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1313 state->smb1.vwv = vwv;
1315 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1317 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1318 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1319 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1320 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1321 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1322 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1323 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1324 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1326 if (iov_count != 0) {
1327 memcpy(&state->smb1.iov[4], bytes_iov,
1328 iov_count * sizeof(*bytes_iov));
1330 state->smb1.iov_count = iov_count + 4;
1332 if (timeout_msec > 0) {
1333 struct timeval endtime;
1335 endtime = timeval_current_ofs_msec(timeout_msec);
1336 if (!tevent_req_set_endtime(req, ev, endtime)) {
1341 switch (smb_command) {
1345 state->one_way = true;
1348 state->one_way = true;
1349 state->smb1.one_way_seqnum = true;
1353 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1354 state->one_way = true;
1362 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1363 struct iovec *iov, int iov_count,
1365 bool one_way_seqnum)
1367 TALLOC_CTX *frame = NULL;
1371 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1372 * iovec directly. MD5Update would do that just fine.
1375 if (iov_count < 4) {
1376 return NT_STATUS_INVALID_PARAMETER_MIX;
1378 if (iov[0].iov_len != NBT_HDR_SIZE) {
1379 return NT_STATUS_INVALID_PARAMETER_MIX;
1381 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1382 return NT_STATUS_INVALID_PARAMETER_MIX;
1384 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1385 return NT_STATUS_INVALID_PARAMETER_MIX;
1387 if (iov[3].iov_len != sizeof(uint16_t)) {
1388 return NT_STATUS_INVALID_PARAMETER_MIX;
1391 frame = talloc_stackframe();
1393 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1395 return NT_STATUS_NO_MEMORY;
1398 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1400 smb_signing_sign_pdu(conn->smb1.signing,
1401 buf, talloc_get_size(buf),
1403 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1406 return NT_STATUS_OK;
1409 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1410 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1411 TALLOC_CTX *tmp_mem,
1414 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1415 struct smbXcli_req_state *state,
1416 struct iovec *iov, int iov_count)
1418 struct tevent_req *subreq;
1423 if (!smbXcli_conn_is_connected(state->conn)) {
1424 return NT_STATUS_CONNECTION_DISCONNECTED;
1427 if (state->conn->protocol > PROTOCOL_NT1) {
1428 return NT_STATUS_REVISION_MISMATCH;
1431 if (iov_count < 4) {
1432 return NT_STATUS_INVALID_PARAMETER_MIX;
1434 if (iov[0].iov_len != NBT_HDR_SIZE) {
1435 return NT_STATUS_INVALID_PARAMETER_MIX;
1437 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1438 return NT_STATUS_INVALID_PARAMETER_MIX;
1440 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1441 return NT_STATUS_INVALID_PARAMETER_MIX;
1443 if (iov[3].iov_len != sizeof(uint16_t)) {
1444 return NT_STATUS_INVALID_PARAMETER_MIX;
1447 cmd = CVAL(iov[1].iov_base, HDR_COM);
1448 if (cmd == SMBreadBraw) {
1449 if (smbXcli_conn_has_async_calls(state->conn)) {
1450 return NT_STATUS_INVALID_PARAMETER_MIX;
1452 state->conn->smb1.read_braw_req = req;
1455 if (state->smb1.mid != 0) {
1456 mid = state->smb1.mid;
1458 mid = smb1cli_alloc_mid(state->conn);
1460 SSVAL(iov[1].iov_base, HDR_MID, mid);
1462 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1464 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1465 &state->smb1.seqnum,
1466 state->smb1.one_way_seqnum);
1468 if (!NT_STATUS_IS_OK(status)) {
1473 * If we supported multiple encrytion contexts
1474 * here we'd look up based on tid.
1476 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1477 char *buf, *enc_buf;
1479 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1481 return NT_STATUS_NO_MEMORY;
1483 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1484 (char *)buf, &enc_buf);
1486 if (!NT_STATUS_IS_OK(status)) {
1487 DEBUG(0, ("Error in encrypting client message: %s\n",
1488 nt_errstr(status)));
1491 buf = (char *)talloc_memdup(state, enc_buf,
1492 smb_len_nbt(enc_buf)+4);
1495 return NT_STATUS_NO_MEMORY;
1497 iov[0].iov_base = (void *)buf;
1498 iov[0].iov_len = talloc_get_size(buf);
1502 if (state->conn->dispatch_incoming == NULL) {
1503 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1506 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1508 subreq = writev_send(state, state->ev, state->conn->outgoing,
1509 state->conn->write_fd, false, iov, iov_count);
1510 if (subreq == NULL) {
1511 return NT_STATUS_NO_MEMORY;
1513 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1514 return NT_STATUS_OK;
1517 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1518 struct tevent_context *ev,
1519 struct smbXcli_conn *conn,
1520 uint8_t smb_command,
1521 uint8_t additional_flags,
1522 uint8_t clear_flags,
1523 uint16_t additional_flags2,
1524 uint16_t clear_flags2,
1525 uint32_t timeout_msec,
1527 struct smbXcli_tcon *tcon,
1528 struct smbXcli_session *session,
1529 uint8_t wct, uint16_t *vwv,
1531 const uint8_t *bytes)
1533 struct tevent_req *req;
1537 iov.iov_base = discard_const_p(void, bytes);
1538 iov.iov_len = num_bytes;
1540 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1541 additional_flags, clear_flags,
1542 additional_flags2, clear_flags2,
1549 if (!tevent_req_is_in_progress(req)) {
1550 return tevent_req_post(req, ev);
1552 status = smb1cli_req_chain_submit(&req, 1);
1553 if (tevent_req_nterror(req, status)) {
1554 return tevent_req_post(req, ev);
1559 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1561 struct tevent_req *req =
1562 tevent_req_callback_data(subreq,
1564 struct smbXcli_req_state *state =
1565 tevent_req_data(req,
1566 struct smbXcli_req_state);
1570 nwritten = writev_recv(subreq, &err);
1571 TALLOC_FREE(subreq);
1572 if (nwritten == -1) {
1573 NTSTATUS status = map_nt_error_from_unix_common(err);
1574 smbXcli_conn_disconnect(state->conn, status);
1578 if (state->one_way) {
1579 state->inbuf = NULL;
1580 tevent_req_done(req);
1584 if (!smbXcli_req_set_pending(req)) {
1585 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1590 static void smbXcli_conn_received(struct tevent_req *subreq)
1592 struct smbXcli_conn *conn =
1593 tevent_req_callback_data(subreq,
1594 struct smbXcli_conn);
1595 TALLOC_CTX *frame = talloc_stackframe();
1601 if (subreq != conn->read_smb_req) {
1602 DEBUG(1, ("Internal error: cli_smb_received called with "
1603 "unexpected subreq\n"));
1604 status = NT_STATUS_INTERNAL_ERROR;
1605 smbXcli_conn_disconnect(conn, status);
1609 conn->read_smb_req = NULL;
1611 received = read_smb_recv(subreq, frame, &inbuf, &err);
1612 TALLOC_FREE(subreq);
1613 if (received == -1) {
1614 status = map_nt_error_from_unix_common(err);
1615 smbXcli_conn_disconnect(conn, status);
1620 status = conn->dispatch_incoming(conn, frame, inbuf);
1622 if (NT_STATUS_IS_OK(status)) {
1624 * We should not do any more processing
1625 * as the dispatch function called
1626 * tevent_req_done().
1629 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1631 * We got an error, so notify all pending requests
1633 smbXcli_conn_disconnect(conn, status);
1638 * We got NT_STATUS_RETRY, so we may ask for a
1639 * next incoming pdu.
1641 if (!smbXcli_conn_receive_next(conn)) {
1642 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1646 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1647 struct iovec **piov, int *pnum_iov)
1658 size_t min_size = MIN_SMB_SIZE;
1660 buflen = smb_len_tcp(buf);
1663 hdr = buf + NBT_HDR_SIZE;
1665 status = smb1cli_pull_raw_error(hdr);
1666 if (NT_STATUS_IS_ERR(status)) {
1668 * This is an ugly hack to support OS/2
1669 * which skips the byte_count in the DATA block
1670 * on some error responses.
1674 min_size -= sizeof(uint16_t);
1677 if (buflen < min_size) {
1678 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1682 * This returns iovec elements in the following order:
1697 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1699 return NT_STATUS_NO_MEMORY;
1701 iov[0].iov_base = hdr;
1702 iov[0].iov_len = HDR_WCT;
1705 cmd = CVAL(hdr, HDR_COM);
1709 size_t len = buflen - taken;
1711 struct iovec *iov_tmp;
1718 * we need at least WCT
1720 needed = sizeof(uint8_t);
1722 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1723 __location__, (int)len, (int)needed));
1728 * Now we check if the specified words are there
1730 wct = CVAL(hdr, wct_ofs);
1731 needed += wct * sizeof(uint16_t);
1733 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1734 __location__, (int)len, (int)needed));
1738 if ((num_iov == 1) &&
1740 NT_STATUS_IS_ERR(status))
1743 * This is an ugly hack to support OS/2
1744 * which skips the byte_count in the DATA block
1745 * on some error responses.
1749 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1751 if (iov_tmp == NULL) {
1753 return NT_STATUS_NO_MEMORY;
1756 cur = &iov[num_iov];
1760 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1762 cur[1].iov_base = cur[0].iov_base;
1769 * we need at least BCC
1771 needed += sizeof(uint16_t);
1773 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1774 __location__, (int)len, (int)needed));
1779 * Now we check if the specified bytes are there
1781 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1782 bcc = SVAL(hdr, bcc_ofs);
1783 needed += bcc * sizeof(uint8_t);
1785 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1786 __location__, (int)len, (int)needed));
1791 * we allocate 2 iovec structures for words and bytes
1793 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1795 if (iov_tmp == NULL) {
1797 return NT_STATUS_NO_MEMORY;
1800 cur = &iov[num_iov];
1803 cur[0].iov_len = wct * sizeof(uint16_t);
1804 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1805 cur[1].iov_len = bcc * sizeof(uint8_t);
1806 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1810 if (!smb1cli_is_andx_req(cmd)) {
1812 * If the current command does not have AndX chanining
1818 if (wct == 0 && bcc == 0) {
1820 * An empty response also ends the chain,
1821 * most likely with an error.
1827 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1828 __location__, (int)wct, (int)cmd));
1831 cmd = CVAL(cur[0].iov_base, 0);
1834 * If it is the end of the chain we are also done.
1838 wct_ofs = SVAL(cur[0].iov_base, 2);
1840 if (wct_ofs < taken) {
1841 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1843 if (wct_ofs > buflen) {
1844 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1848 * we consumed everything up to the start of the next
1854 remaining = buflen - taken;
1856 if (remaining > 0 && num_iov >= 3) {
1858 * The last DATA block gets the remaining
1859 * bytes, this is needed to support
1860 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1862 iov[num_iov-1].iov_len += remaining;
1866 *pnum_iov = num_iov;
1867 return NT_STATUS_OK;
1871 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1874 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1875 TALLOC_CTX *tmp_mem,
1878 struct tevent_req *req;
1879 struct smbXcli_req_state *state;
1886 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1887 size_t len = smb_len_tcp(inbuf);
1888 struct iovec *iov = NULL;
1890 struct tevent_req **chain = NULL;
1891 size_t num_chained = 0;
1892 size_t num_responses = 0;
1894 if (conn->smb1.read_braw_req != NULL) {
1895 req = conn->smb1.read_braw_req;
1896 conn->smb1.read_braw_req = NULL;
1897 state = tevent_req_data(req, struct smbXcli_req_state);
1899 smbXcli_req_unset_pending(req);
1901 if (state->smb1.recv_iov == NULL) {
1903 * For requests with more than
1904 * one response, we have to readd the
1907 state->smb1.recv_iov = talloc_zero_array(state,
1910 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1911 return NT_STATUS_OK;
1915 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1916 state->smb1.recv_iov[0].iov_len = len;
1917 ZERO_STRUCT(state->smb1.recv_iov[1]);
1918 ZERO_STRUCT(state->smb1.recv_iov[2]);
1920 state->smb1.recv_cmd = SMBreadBraw;
1921 state->smb1.recv_status = NT_STATUS_OK;
1922 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1924 tevent_req_done(req);
1925 return NT_STATUS_OK;
1928 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1929 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1930 DEBUG(10, ("Got non-SMB PDU\n"));
1931 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1935 * If we supported multiple encrytion contexts
1936 * here we'd look up based on tid.
1938 if (common_encryption_on(conn->smb1.trans_enc)
1939 && (CVAL(inbuf, 0) == 0)) {
1940 uint16_t enc_ctx_num;
1942 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1943 if (!NT_STATUS_IS_OK(status)) {
1944 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1945 nt_errstr(status)));
1949 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1950 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1952 conn->smb1.trans_enc->enc_ctx_num));
1953 return NT_STATUS_INVALID_HANDLE;
1956 status = common_decrypt_buffer(conn->smb1.trans_enc,
1958 if (!NT_STATUS_IS_OK(status)) {
1959 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1960 nt_errstr(status)));
1963 inhdr = inbuf + NBT_HDR_SIZE;
1964 len = smb_len_nbt(inbuf);
1967 mid = SVAL(inhdr, HDR_MID);
1968 num_pending = talloc_array_length(conn->pending);
1970 for (i=0; i<num_pending; i++) {
1971 if (mid == smb1cli_req_mid(conn->pending[i])) {
1975 if (i == num_pending) {
1976 /* Dump unexpected reply */
1977 return NT_STATUS_RETRY;
1980 oplock_break = false;
1982 if (mid == 0xffff) {
1984 * Paranoia checks that this is really an oplock break request.
1986 oplock_break = (len == 51); /* hdr + 8 words */
1987 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1988 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1989 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1990 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1992 if (!oplock_break) {
1993 /* Dump unexpected reply */
1994 return NT_STATUS_RETRY;
1998 req = conn->pending[i];
1999 state = tevent_req_data(req, struct smbXcli_req_state);
2001 if (!oplock_break /* oplock breaks are not signed */
2002 && !smb_signing_check_pdu(conn->smb1.signing,
2003 inhdr, len, state->smb1.seqnum+1)) {
2004 DEBUG(10, ("cli_check_sign_mac failed\n"));
2005 return NT_STATUS_ACCESS_DENIED;
2008 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2010 if (!NT_STATUS_IS_OK(status)) {
2011 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2012 nt_errstr(status)));
2016 cmd = CVAL(inhdr, HDR_COM);
2017 status = smb1cli_pull_raw_error(inhdr);
2019 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2020 (state->session != NULL) && state->session->disconnect_expired)
2023 * this should be a short term hack
2024 * until the upper layers have implemented
2025 * re-authentication.
2030 if (state->smb1.chained_requests == NULL) {
2032 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2035 smbXcli_req_unset_pending(req);
2037 if (state->smb1.recv_iov == NULL) {
2039 * For requests with more than
2040 * one response, we have to readd the
2043 state->smb1.recv_iov = talloc_zero_array(state,
2046 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2047 return NT_STATUS_OK;
2051 state->smb1.recv_cmd = cmd;
2052 state->smb1.recv_status = status;
2053 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2055 state->smb1.recv_iov[0] = iov[0];
2056 state->smb1.recv_iov[1] = iov[1];
2057 state->smb1.recv_iov[2] = iov[2];
2059 if (talloc_array_length(conn->pending) == 0) {
2060 tevent_req_done(req);
2061 return NT_STATUS_OK;
2064 tevent_req_defer_callback(req, state->ev);
2065 tevent_req_done(req);
2066 return NT_STATUS_RETRY;
2069 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2070 num_chained = talloc_array_length(chain);
2071 num_responses = (num_iov - 1)/2;
2073 if (num_responses > num_chained) {
2074 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2077 for (i=0; i<num_chained; i++) {
2078 size_t iov_idx = 1 + (i*2);
2079 struct iovec *cur = &iov[iov_idx];
2083 state = tevent_req_data(req, struct smbXcli_req_state);
2085 smbXcli_req_unset_pending(req);
2088 * as we finish multiple requests here
2089 * we need to defer the callbacks as
2090 * they could destroy our current stack state.
2092 tevent_req_defer_callback(req, state->ev);
2094 if (i >= num_responses) {
2095 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2099 if (state->smb1.recv_iov == NULL) {
2101 * For requests with more than
2102 * one response, we have to readd the
2105 state->smb1.recv_iov = talloc_zero_array(state,
2108 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2113 state->smb1.recv_cmd = cmd;
2115 if (i == (num_responses - 1)) {
2117 * The last request in the chain gets the status
2119 state->smb1.recv_status = status;
2121 cmd = CVAL(cur[0].iov_base, 0);
2122 state->smb1.recv_status = NT_STATUS_OK;
2125 state->inbuf = inbuf;
2128 * Note: here we use talloc_reference() in a way
2129 * that does not expose it to the caller.
2131 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2132 if (tevent_req_nomem(inbuf_ref, req)) {
2136 /* copy the related buffers */
2137 state->smb1.recv_iov[0] = iov[0];
2138 state->smb1.recv_iov[1] = cur[0];
2139 state->smb1.recv_iov[2] = cur[1];
2141 tevent_req_done(req);
2144 return NT_STATUS_RETRY;
2147 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2148 TALLOC_CTX *mem_ctx,
2149 struct iovec **piov,
2153 uint32_t *pvwv_offset,
2154 uint32_t *pnum_bytes,
2156 uint32_t *pbytes_offset,
2158 const struct smb1cli_req_expected_response *expected,
2159 size_t num_expected)
2161 struct smbXcli_req_state *state =
2162 tevent_req_data(req,
2163 struct smbXcli_req_state);
2164 NTSTATUS status = NT_STATUS_OK;
2165 struct iovec *recv_iov = NULL;
2166 uint8_t *hdr = NULL;
2168 uint32_t vwv_offset = 0;
2169 uint16_t *vwv = NULL;
2170 uint32_t num_bytes = 0;
2171 uint32_t bytes_offset = 0;
2172 uint8_t *bytes = NULL;
2174 bool found_status = false;
2175 bool found_size = false;
2189 if (pvwv_offset != NULL) {
2192 if (pnum_bytes != NULL) {
2195 if (pbytes != NULL) {
2198 if (pbytes_offset != NULL) {
2201 if (pinbuf != NULL) {
2205 if (state->inbuf != NULL) {
2206 recv_iov = state->smb1.recv_iov;
2207 state->smb1.recv_iov = NULL;
2208 if (state->smb1.recv_cmd != SMBreadBraw) {
2209 hdr = (uint8_t *)recv_iov[0].iov_base;
2210 wct = recv_iov[1].iov_len/2;
2211 vwv = (uint16_t *)recv_iov[1].iov_base;
2212 vwv_offset = PTR_DIFF(vwv, hdr);
2213 num_bytes = recv_iov[2].iov_len;
2214 bytes = (uint8_t *)recv_iov[2].iov_base;
2215 bytes_offset = PTR_DIFF(bytes, hdr);
2219 if (tevent_req_is_nterror(req, &status)) {
2220 for (i=0; i < num_expected; i++) {
2221 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2222 found_status = true;
2228 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2234 if (num_expected == 0) {
2235 found_status = true;
2239 status = state->smb1.recv_status;
2241 for (i=0; i < num_expected; i++) {
2242 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2246 found_status = true;
2247 if (expected[i].wct == 0) {
2252 if (expected[i].wct == wct) {
2258 if (!found_status) {
2263 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2267 *piov = talloc_move(mem_ctx, &recv_iov);
2279 if (pvwv_offset != NULL) {
2280 *pvwv_offset = vwv_offset;
2282 if (pnum_bytes != NULL) {
2283 *pnum_bytes = num_bytes;
2285 if (pbytes != NULL) {
2288 if (pbytes_offset != NULL) {
2289 *pbytes_offset = bytes_offset;
2291 if (pinbuf != NULL) {
2292 *pinbuf = state->inbuf;
2298 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2305 for (i=0; i<num_reqs; i++) {
2306 struct smbXcli_req_state *state;
2307 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2308 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2309 state->smb1.iov_count-2);
2310 wct_ofs = (wct_ofs + 3) & ~3;
2315 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2317 struct smbXcli_req_state *first_state =
2318 tevent_req_data(reqs[0],
2319 struct smbXcli_req_state);
2320 struct smbXcli_req_state *state;
2322 size_t chain_padding = 0;
2324 struct iovec *iov = NULL;
2325 struct iovec *this_iov;
2329 if (num_reqs == 1) {
2330 return smb1cli_req_writev_submit(reqs[0], first_state,
2331 first_state->smb1.iov,
2332 first_state->smb1.iov_count);
2336 for (i=0; i<num_reqs; i++) {
2337 if (!tevent_req_is_in_progress(reqs[i])) {
2338 return NT_STATUS_INTERNAL_ERROR;
2341 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2343 if (state->smb1.iov_count < 4) {
2344 return NT_STATUS_INVALID_PARAMETER_MIX;
2349 * The NBT and SMB header
2362 iovlen += state->smb1.iov_count - 2;
2365 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2367 return NT_STATUS_NO_MEMORY;
2370 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2371 first_state, reqs, sizeof(*reqs) * num_reqs);
2372 if (first_state->smb1.chained_requests == NULL) {
2374 return NT_STATUS_NO_MEMORY;
2377 wct_offset = HDR_WCT;
2380 for (i=0; i<num_reqs; i++) {
2381 size_t next_padding = 0;
2384 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2386 if (i < num_reqs-1) {
2387 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2388 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2390 TALLOC_FREE(first_state->smb1.chained_requests);
2391 return NT_STATUS_INVALID_PARAMETER_MIX;
2395 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2396 state->smb1.iov_count-2) + 1;
2397 if ((wct_offset % 4) != 0) {
2398 next_padding = 4 - (wct_offset % 4);
2400 wct_offset += next_padding;
2401 vwv = state->smb1.vwv;
2403 if (i < num_reqs-1) {
2404 struct smbXcli_req_state *next_state =
2405 tevent_req_data(reqs[i+1],
2406 struct smbXcli_req_state);
2407 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2409 SSVAL(vwv+1, 0, wct_offset);
2410 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2411 /* properly end the chain */
2412 SCVAL(vwv+0, 0, 0xff);
2413 SCVAL(vwv+0, 1, 0xff);
2419 * The NBT and SMB header
2421 this_iov[0] = state->smb1.iov[0];
2422 this_iov[1] = state->smb1.iov[1];
2426 * This one is a bit subtle. We have to add
2427 * chain_padding bytes between the requests, and we
2428 * have to also include the wct field of the
2429 * subsequent requests. We use the subsequent header
2430 * for the padding, it contains the wct field in its
2433 this_iov[0].iov_len = chain_padding+1;
2434 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2435 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2436 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2441 * copy the words and bytes
2443 memcpy(this_iov, state->smb1.iov+2,
2444 sizeof(struct iovec) * (state->smb1.iov_count-2));
2445 this_iov += state->smb1.iov_count - 2;
2446 chain_padding = next_padding;
2449 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2450 if (nbt_len > first_state->conn->smb1.max_xmit) {
2452 TALLOC_FREE(first_state->smb1.chained_requests);
2453 return NT_STATUS_INVALID_PARAMETER_MIX;
2456 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2457 if (!NT_STATUS_IS_OK(status)) {
2459 TALLOC_FREE(first_state->smb1.chained_requests);
2463 return NT_STATUS_OK;
2466 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2468 return ((tevent_queue_length(conn->outgoing) != 0)
2469 || (talloc_array_length(conn->pending) != 0));
2472 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2474 if (conn->protocol >= PROTOCOL_SMB2_02) {
2475 if (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS) {
2482 if (smb1cli_conn_capabilities(conn) & CAP_DFS) {
2489 bool smbXcli_tcon_is_dfsroot(struct smbXcli_tcon *tcon)
2491 return tcon->is_dfsroot;
2494 void smb1cli_tcon_set_optional_support(struct smbXcli_tcon *tcon,
2497 if (tcon->conn == NULL) {
2501 if (optional & SMB_SHARE_IN_DFS) {
2502 tcon->is_dfsroot = true;
2505 if (!(smb1cli_conn_capabilities(tcon->conn) & CAP_DFS)) {
2506 tcon->is_dfsroot = false;
2510 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2512 return conn->smb2.server.capabilities;
2515 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2517 return conn->smb2.server.security_mode;
2520 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2522 return conn->smb2.server.max_trans_size;
2525 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2527 return conn->smb2.server.max_read_size;
2530 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2532 return conn->smb2.server.max_write_size;
2535 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2536 uint16_t max_credits)
2538 conn->smb2.max_credits = max_credits;
2541 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2543 static bool smb2cli_req_cancel(struct tevent_req *req)
2545 struct smbXcli_req_state *state =
2546 tevent_req_data(req,
2547 struct smbXcli_req_state);
2548 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2549 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2550 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2551 struct smbXcli_tcon *tcon = state->tcon;
2552 struct smbXcli_session *session = state->session;
2553 uint8_t *fixed = state->smb2.pad;
2554 uint16_t fixed_len = 4;
2555 struct tevent_req *subreq;
2556 struct smbXcli_req_state *substate;
2559 SSVAL(fixed, 0, 0x04);
2562 subreq = smb2cli_req_create(state, state->ev,
2570 if (subreq == NULL) {
2573 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2576 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2577 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2579 flags &= SMB2_HDR_FLAG_ASYNC;
2581 if (flags & SMB2_HDR_FLAG_ASYNC) {
2585 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2586 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2587 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2589 status = smb2cli_req_compound_submit(&subreq, 1);
2590 if (!NT_STATUS_IS_OK(status)) {
2591 TALLOC_FREE(subreq);
2595 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2600 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2602 /* we do not care about the result */
2603 TALLOC_FREE(subreq);
2606 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2607 struct tevent_context *ev,
2608 struct smbXcli_conn *conn,
2610 uint32_t additional_flags,
2611 uint32_t clear_flags,
2612 uint32_t timeout_msec,
2613 struct smbXcli_tcon *tcon,
2614 struct smbXcli_session *session,
2615 const uint8_t *fixed,
2620 struct tevent_req *req;
2621 struct smbXcli_req_state *state;
2625 bool use_channel_sequence = false;
2626 uint16_t channel_sequence = 0;
2628 req = tevent_req_create(mem_ctx, &state,
2629 struct smbXcli_req_state);
2636 state->session = session;
2639 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2640 use_channel_sequence = true;
2641 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2642 use_channel_sequence = true;
2646 uid = session->smb2->session_id;
2648 if (use_channel_sequence) {
2649 channel_sequence = session->smb2->channel_sequence;
2652 state->smb2.should_sign = session->smb2->should_sign;
2653 state->smb2.should_encrypt = session->smb2->should_encrypt;
2655 if (cmd == SMB2_OP_SESSSETUP &&
2656 session->smb2->signing_key.length != 0) {
2657 state->smb2.should_sign = true;
2660 if (cmd == SMB2_OP_SESSSETUP &&
2661 session->smb2_channel.signing_key.length == 0) {
2662 state->smb2.should_encrypt = false;
2667 tid = tcon->smb2.tcon_id;
2669 if (tcon->smb2.should_encrypt) {
2670 state->smb2.should_encrypt = true;
2674 if (state->smb2.should_encrypt) {
2675 state->smb2.should_sign = false;
2678 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2679 if (state->smb2.recv_iov == NULL) {
2684 flags |= additional_flags;
2685 flags &= ~clear_flags;
2687 state->smb2.fixed = fixed;
2688 state->smb2.fixed_len = fixed_len;
2689 state->smb2.dyn = dyn;
2690 state->smb2.dyn_len = dyn_len;
2692 if (state->smb2.should_encrypt) {
2693 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2694 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2697 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2698 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2699 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2700 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2701 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2702 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2703 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2704 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2707 case SMB2_OP_CANCEL:
2708 state->one_way = true;
2712 * If this is a dummy request, it will have
2713 * UINT64_MAX as message id.
2714 * If we send on break acknowledgement,
2715 * this gets overwritten later.
2717 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2721 if (timeout_msec > 0) {
2722 struct timeval endtime;
2724 endtime = timeval_current_ofs_msec(timeout_msec);
2725 if (!tevent_req_set_endtime(req, ev, endtime)) {
2733 void smb2cli_req_set_notify_async(struct tevent_req *req)
2735 struct smbXcli_req_state *state =
2736 tevent_req_data(req,
2737 struct smbXcli_req_state);
2739 state->smb2.notify_async = true;
2742 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2743 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2744 TALLOC_CTX *tmp_mem,
2747 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2750 struct smbXcli_req_state *state;
2751 struct tevent_req *subreq;
2753 int i, num_iov, nbt_len;
2755 const DATA_BLOB *encryption_key = NULL;
2756 uint64_t encryption_session_id = 0;
2759 * 1 for the nbt length, optional TRANSFORM
2760 * per request: HDR, fixed, dyn, padding
2761 * -1 because the last one does not need padding
2764 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2766 return NT_STATUS_NO_MEMORY;
2773 * the session of the first request that requires encryption
2774 * specifies the encryption key.
2776 for (i=0; i<num_reqs; i++) {
2777 if (!tevent_req_is_in_progress(reqs[i])) {
2778 return NT_STATUS_INTERNAL_ERROR;
2781 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2783 if (!smbXcli_conn_is_connected(state->conn)) {
2784 return NT_STATUS_CONNECTION_DISCONNECTED;
2787 if ((state->conn->protocol != PROTOCOL_NONE) &&
2788 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2789 return NT_STATUS_REVISION_MISMATCH;
2792 if (state->session == NULL) {
2796 if (!state->smb2.should_encrypt) {
2800 encryption_key = &state->session->smb2->encryption_key;
2801 if (encryption_key->length == 0) {
2802 return NT_STATUS_INVALID_PARAMETER_MIX;
2805 encryption_session_id = state->session->smb2->session_id;
2808 iov[num_iov].iov_base = state->smb2.transform;
2809 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2812 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2813 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2814 state->session->smb2->nonce_low);
2815 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2816 state->session->smb2->nonce_high);
2817 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2818 encryption_session_id);
2820 state->session->smb2->nonce_low += 1;
2821 if (state->session->smb2->nonce_low == 0) {
2822 state->session->smb2->nonce_high += 1;
2823 state->session->smb2->nonce_low += 1;
2826 nbt_len += SMB2_TF_HDR_SIZE;
2830 for (i=0; i<num_reqs; i++) {
2839 const DATA_BLOB *signing_key = NULL;
2841 if (!tevent_req_is_in_progress(reqs[i])) {
2842 return NT_STATUS_INTERNAL_ERROR;
2845 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2847 if (!smbXcli_conn_is_connected(state->conn)) {
2848 return NT_STATUS_CONNECTION_DISCONNECTED;
2851 if ((state->conn->protocol != PROTOCOL_NONE) &&
2852 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2853 return NT_STATUS_REVISION_MISMATCH;
2856 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2857 if (opcode == SMB2_OP_CANCEL) {
2861 avail = UINT64_MAX - state->conn->smb2.mid;
2863 return NT_STATUS_CONNECTION_ABORTED;
2866 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2867 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2872 charge = MAX(state->smb2.credit_charge, charge);
2874 avail = MIN(avail, state->conn->smb2.cur_credits);
2875 if (avail < charge) {
2876 return NT_STATUS_INTERNAL_ERROR;
2880 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2881 credits = state->conn->smb2.max_credits -
2882 state->conn->smb2.cur_credits;
2884 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2888 mid = state->conn->smb2.mid;
2889 state->conn->smb2.mid += charge;
2890 state->conn->smb2.cur_credits -= charge;
2892 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2893 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2895 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2896 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2899 if (state->session && encryption_key == NULL) {
2901 * We prefer the channel signing key if it is
2904 if (state->smb2.should_sign) {
2905 signing_key = &state->session->smb2_channel.signing_key;
2909 * If it is a channel binding, we already have the main
2910 * signing key and try that one.
2912 if (signing_key && signing_key->length == 0) {
2913 signing_key = &state->session->smb2->signing_key;
2917 * If we do not have any session key yet, we skip the
2918 * signing of SMB2_OP_SESSSETUP requests.
2920 if (signing_key && signing_key->length == 0) {
2926 iov[num_iov].iov_base = state->smb2.hdr;
2927 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2930 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2931 iov[num_iov].iov_len = state->smb2.fixed_len;
2934 if (state->smb2.dyn != NULL) {
2935 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2936 iov[num_iov].iov_len = state->smb2.dyn_len;
2940 reqlen = sizeof(state->smb2.hdr);
2941 reqlen += state->smb2.fixed_len;
2942 reqlen += state->smb2.dyn_len;
2944 if (i < num_reqs-1) {
2945 if ((reqlen % 8) > 0) {
2946 uint8_t pad = 8 - (reqlen % 8);
2947 iov[num_iov].iov_base = state->smb2.pad;
2948 iov[num_iov].iov_len = pad;
2952 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2955 state->smb2.encryption_session_id = encryption_session_id;
2957 if (signing_key != NULL) {
2960 status = smb2_signing_sign_pdu(*signing_key,
2961 state->session->conn->protocol,
2962 &iov[hdr_iov], num_iov - hdr_iov);
2963 if (!NT_STATUS_IS_OK(status)) {
2970 ret = smbXcli_req_set_pending(reqs[i]);
2972 return NT_STATUS_NO_MEMORY;
2976 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2977 _smb_setlen_tcp(state->length_hdr, nbt_len);
2978 iov[0].iov_base = state->length_hdr;
2979 iov[0].iov_len = sizeof(state->length_hdr);
2981 if (encryption_key != NULL) {
2983 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
2987 buf = talloc_array(iov, uint8_t, buflen);
2989 return NT_STATUS_NO_MEMORY;
2993 * We copy the buffers before encrypting them,
2994 * this is at least currently needed for the
2995 * to keep state->smb2.hdr.
2997 * Also the callers may expect there buffers
3000 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3001 struct iovec *v = &iov[vi];
3002 const uint8_t *o = (const uint8_t *)v->iov_base;
3004 memcpy(buf, o, v->iov_len);
3005 v->iov_base = (void *)buf;
3009 status = smb2_signing_encrypt_pdu(*encryption_key,
3010 state->conn->protocol,
3011 &iov[tf_iov], num_iov - tf_iov);
3012 if (!NT_STATUS_IS_OK(status)) {
3017 if (state->conn->dispatch_incoming == NULL) {
3018 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3021 subreq = writev_send(state, state->ev, state->conn->outgoing,
3022 state->conn->write_fd, false, iov, num_iov);
3023 if (subreq == NULL) {
3024 return NT_STATUS_NO_MEMORY;
3026 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3027 return NT_STATUS_OK;
3030 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3032 struct smbXcli_req_state *state =
3033 tevent_req_data(req,
3034 struct smbXcli_req_state);
3036 state->smb2.credit_charge = charge;
3039 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3040 struct tevent_context *ev,
3041 struct smbXcli_conn *conn,
3043 uint32_t additional_flags,
3044 uint32_t clear_flags,
3045 uint32_t timeout_msec,
3046 struct smbXcli_tcon *tcon,
3047 struct smbXcli_session *session,
3048 const uint8_t *fixed,
3053 struct tevent_req *req;
3056 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3057 additional_flags, clear_flags,
3060 fixed, fixed_len, dyn, dyn_len);
3064 if (!tevent_req_is_in_progress(req)) {
3065 return tevent_req_post(req, ev);
3067 status = smb2cli_req_compound_submit(&req, 1);
3068 if (tevent_req_nterror(req, status)) {
3069 return tevent_req_post(req, ev);
3074 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3076 struct tevent_req *req =
3077 tevent_req_callback_data(subreq,
3079 struct smbXcli_req_state *state =
3080 tevent_req_data(req,
3081 struct smbXcli_req_state);
3085 nwritten = writev_recv(subreq, &err);
3086 TALLOC_FREE(subreq);
3087 if (nwritten == -1) {
3088 /* here, we need to notify all pending requests */
3089 NTSTATUS status = map_nt_error_from_unix_common(err);
3090 smbXcli_conn_disconnect(state->conn, status);
3095 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3098 TALLOC_CTX *mem_ctx,
3099 struct iovec **piov, int *pnum_iov)
3104 uint8_t *first_hdr = buf;
3105 size_t verified_buflen = 0;
3109 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3111 return NT_STATUS_NO_MEMORY;
3114 while (taken < buflen) {
3115 size_t len = buflen - taken;
3116 uint8_t *hdr = first_hdr + taken;
3119 size_t next_command_ofs;
3121 struct iovec *iov_tmp;
3123 if (verified_buflen > taken) {
3124 len = verified_buflen - taken;
3131 DEBUG(10, ("%d bytes left, expected at least %d\n",
3135 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3136 struct smbXcli_session *s;
3138 struct iovec tf_iov[2];
3142 if (len < SMB2_TF_HDR_SIZE) {
3143 DEBUG(10, ("%d bytes left, expected at least %d\n",
3144 (int)len, SMB2_TF_HDR_SIZE));
3148 tf_len = SMB2_TF_HDR_SIZE;
3151 hdr = first_hdr + taken;
3152 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3153 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3155 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3156 DEBUG(10, ("%d bytes left, expected at least %d\n",
3158 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3163 for (; s; s = s->next) {
3164 if (s->smb2->session_id != uid) {
3171 DEBUG(10, ("unknown session_id %llu\n",
3172 (unsigned long long)uid));
3176 tf_iov[0].iov_base = (void *)tf;
3177 tf_iov[0].iov_len = tf_len;
3178 tf_iov[1].iov_base = (void *)hdr;
3179 tf_iov[1].iov_len = enc_len;
3181 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3184 if (!NT_STATUS_IS_OK(status)) {
3189 verified_buflen = taken + enc_len;
3194 * We need the header plus the body length field
3197 if (len < SMB2_HDR_BODY + 2) {
3198 DEBUG(10, ("%d bytes left, expected at least %d\n",
3199 (int)len, SMB2_HDR_BODY));
3202 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3203 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3207 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3208 DEBUG(10, ("Got HDR len %d, expected %d\n",
3209 SVAL(hdr, 4), SMB2_HDR_BODY));
3214 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3215 body_size = SVAL(hdr, SMB2_HDR_BODY);
3217 if (next_command_ofs != 0) {
3218 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3221 if (next_command_ofs > full_size) {
3224 full_size = next_command_ofs;
3226 if (body_size < 2) {
3229 body_size &= 0xfffe;
3231 if (body_size > (full_size - SMB2_HDR_BODY)) {
3235 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3237 if (iov_tmp == NULL) {
3239 return NT_STATUS_NO_MEMORY;
3242 cur = &iov[num_iov];
3245 cur[0].iov_base = tf;
3246 cur[0].iov_len = tf_len;
3247 cur[1].iov_base = hdr;
3248 cur[1].iov_len = SMB2_HDR_BODY;
3249 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3250 cur[2].iov_len = body_size;
3251 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3252 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3258 *pnum_iov = num_iov;
3259 return NT_STATUS_OK;
3263 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3266 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3269 size_t num_pending = talloc_array_length(conn->pending);
3272 for (i=0; i<num_pending; i++) {
3273 struct tevent_req *req = conn->pending[i];
3274 struct smbXcli_req_state *state =
3275 tevent_req_data(req,
3276 struct smbXcli_req_state);
3278 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3285 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3286 TALLOC_CTX *tmp_mem,
3289 struct tevent_req *req;
3290 struct smbXcli_req_state *state = NULL;
3295 struct smbXcli_session *last_session = NULL;
3296 size_t inbuf_len = smb_len_tcp(inbuf);
3298 status = smb2cli_inbuf_parse_compound(conn,
3299 inbuf + NBT_HDR_SIZE,
3303 if (!NT_STATUS_IS_OK(status)) {
3307 for (i=0; i<num_iov; i+=4) {
3308 uint8_t *inbuf_ref = NULL;
3309 struct iovec *cur = &iov[i];
3310 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3311 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3312 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3313 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3314 uint16_t req_opcode;
3316 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3317 uint32_t new_credits;
3318 struct smbXcli_session *session = NULL;
3319 const DATA_BLOB *signing_key = NULL;
3320 bool was_encrypted = false;
3322 new_credits = conn->smb2.cur_credits;
3323 new_credits += credits;
3324 if (new_credits > UINT16_MAX) {
3325 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3327 conn->smb2.cur_credits += credits;
3329 req = smb2cli_conn_find_pending(conn, mid);
3331 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3333 state = tevent_req_data(req, struct smbXcli_req_state);
3335 state->smb2.got_async = false;
3337 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3338 if (opcode != req_opcode) {
3339 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3341 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3343 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3344 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3347 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3348 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3349 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3350 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3353 * async interim responses are not signed,
3354 * even if the SMB2_HDR_FLAG_SIGNED flag
3357 req_flags |= SMB2_HDR_FLAG_ASYNC;
3358 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3359 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3361 if (state->smb2.notify_async) {
3362 state->smb2.got_async = true;
3363 tevent_req_defer_callback(req, state->ev);
3364 tevent_req_notify_callback(req);
3369 session = state->session;
3370 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3371 session = last_session;
3373 last_session = session;
3375 if (state->smb2.should_sign) {
3376 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3377 return NT_STATUS_ACCESS_DENIED;
3381 if (flags & SMB2_HDR_FLAG_SIGNED) {
3382 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3384 if (session == NULL) {
3385 struct smbXcli_session *s;
3387 s = state->conn->sessions;
3388 for (; s; s = s->next) {
3389 if (s->smb2->session_id != uid) {
3398 if (session == NULL) {
3399 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3402 last_session = session;
3403 signing_key = &session->smb2_channel.signing_key;
3406 if (opcode == SMB2_OP_SESSSETUP) {
3408 * We prefer the channel signing key, if it is
3411 * If we do not have a channel signing key yet,
3412 * we try the main signing key, if it is not
3413 * the final response.
3415 if (signing_key && signing_key->length == 0 &&
3416 !NT_STATUS_IS_OK(status)) {
3417 signing_key = &session->smb2->signing_key;
3420 if (signing_key && signing_key->length == 0) {
3422 * If we do not have a session key to
3423 * verify the signature, we defer the
3424 * signing check to the caller.
3426 * The caller gets NT_STATUS_OK, it
3428 * smb2cli_session_set_session_key()
3430 * smb2cli_session_set_channel_key()
3431 * which will check the signature
3432 * with the channel signing key.
3438 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3439 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3440 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3443 * If the response was encrypted in a SMB2_TRANSFORM
3444 * pdu, which belongs to the correct session,
3445 * we do not need to do signing checks
3447 * It could be the session the response belongs to
3448 * or the session that was used to encrypt the
3449 * SMB2_TRANSFORM request.
3451 if ((session && session->smb2->session_id == uid) ||
3452 (state->smb2.encryption_session_id == uid)) {
3454 was_encrypted = true;
3458 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3460 * if the server returns NT_STATUS_USER_SESSION_DELETED
3461 * the response is not signed and we should
3462 * propagate the NT_STATUS_USER_SESSION_DELETED
3463 * status to the caller.
3465 state->smb2.signing_skipped = true;
3469 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3471 * if the server returns
3472 * NT_STATUS_INVALID_PARAMETER
3473 * the response might not be encrypted.
3475 if (state->smb2.should_encrypt && !was_encrypted) {
3476 state->smb2.signing_skipped = true;
3481 if (state->smb2.should_encrypt && !was_encrypted) {
3482 if (!state->smb2.signing_skipped) {
3483 return NT_STATUS_ACCESS_DENIED;
3487 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3488 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3489 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3491 * if the server returns
3492 * NT_STATUS_NETWORK_NAME_DELETED
3493 * NT_STATUS_FILE_CLOSED
3494 * NT_STATUS_INVALID_PARAMETER
3495 * the response might not be signed
3496 * as this happens before the signing checks.
3498 * If server echos the signature (or all zeros)
3499 * we should report the status from the server
3505 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3506 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3509 state->smb2.signing_skipped = true;
3515 static const uint8_t zeros[16];
3517 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3521 state->smb2.signing_skipped = true;
3528 status = smb2_signing_check_pdu(*signing_key,
3529 state->conn->protocol,
3531 if (!NT_STATUS_IS_OK(status)) {
3533 * If the signing check fails, we disconnect
3540 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3541 (session != NULL) && session->disconnect_expired)
3544 * this should be a short term hack
3545 * until the upper layers have implemented
3546 * re-authentication.
3551 smbXcli_req_unset_pending(req);
3554 * There might be more than one response
3555 * we need to defer the notifications
3557 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3562 tevent_req_defer_callback(req, state->ev);
3566 * Note: here we use talloc_reference() in a way
3567 * that does not expose it to the caller.
3569 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3570 if (tevent_req_nomem(inbuf_ref, req)) {
3574 /* copy the related buffers */
3575 state->smb2.recv_iov[0] = cur[1];
3576 state->smb2.recv_iov[1] = cur[2];
3577 state->smb2.recv_iov[2] = cur[3];
3579 tevent_req_done(req);
3583 return NT_STATUS_RETRY;
3586 return NT_STATUS_OK;
3589 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3590 struct iovec **piov,
3591 const struct smb2cli_req_expected_response *expected,
3592 size_t num_expected)
3594 struct smbXcli_req_state *state =
3595 tevent_req_data(req,
3596 struct smbXcli_req_state);
3599 bool found_status = false;
3600 bool found_size = false;
3607 if (state->smb2.got_async) {
3608 return STATUS_PENDING;
3611 if (tevent_req_is_nterror(req, &status)) {
3612 for (i=0; i < num_expected; i++) {
3613 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3614 found_status = true;
3620 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3626 if (num_expected == 0) {
3627 found_status = true;
3631 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3632 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3634 for (i=0; i < num_expected; i++) {
3635 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3639 found_status = true;
3640 if (expected[i].body_size == 0) {
3645 if (expected[i].body_size == body_size) {
3651 if (!found_status) {
3655 if (state->smb2.signing_skipped) {
3656 if (num_expected > 0) {
3657 return NT_STATUS_ACCESS_DENIED;
3659 if (!NT_STATUS_IS_ERR(status)) {
3660 return NT_STATUS_ACCESS_DENIED;
3665 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3669 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3675 static const struct {
3676 enum protocol_types proto;
3677 const char *smb1_name;
3678 } smb1cli_prots[] = {
3679 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3680 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3681 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3682 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3683 {PROTOCOL_LANMAN2, "LM1.2X002"},
3684 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3685 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3686 {PROTOCOL_LANMAN2, "Samba"},
3687 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3688 {PROTOCOL_NT1, "NT LM 0.12"},
3689 {PROTOCOL_SMB2_02, "SMB 2.002"},
3690 {PROTOCOL_SMB2_10, "SMB 2.???"},
3693 static const struct {
3694 enum protocol_types proto;
3695 uint16_t smb2_dialect;
3696 } smb2cli_prots[] = {
3697 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3698 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3699 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3700 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3701 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3704 struct smbXcli_negprot_state {
3705 struct smbXcli_conn *conn;
3706 struct tevent_context *ev;
3707 uint32_t timeout_msec;
3708 enum protocol_types min_protocol;
3709 enum protocol_types max_protocol;
3713 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3717 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3718 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3719 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3720 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3721 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3722 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3726 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3727 struct tevent_context *ev,
3728 struct smbXcli_conn *conn,
3729 uint32_t timeout_msec,
3730 enum protocol_types min_protocol,
3731 enum protocol_types max_protocol)
3733 struct tevent_req *req, *subreq;
3734 struct smbXcli_negprot_state *state;
3736 req = tevent_req_create(mem_ctx, &state,
3737 struct smbXcli_negprot_state);
3743 state->timeout_msec = timeout_msec;
3744 state->min_protocol = min_protocol;
3745 state->max_protocol = max_protocol;
3747 if (min_protocol == PROTOCOL_NONE) {
3748 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3749 return tevent_req_post(req, ev);
3752 if (max_protocol == PROTOCOL_NONE) {
3753 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3754 return tevent_req_post(req, ev);
3757 if (min_protocol > max_protocol) {
3758 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3759 return tevent_req_post(req, ev);
3762 if ((min_protocol < PROTOCOL_SMB2_02) &&
3763 (max_protocol < PROTOCOL_SMB2_02)) {
3767 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3769 subreq = smbXcli_negprot_smb1_subreq(state);
3770 if (tevent_req_nomem(subreq, req)) {
3771 return tevent_req_post(req, ev);
3773 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3777 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3778 (max_protocol >= PROTOCOL_SMB2_02)) {
3782 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3784 subreq = smbXcli_negprot_smb2_subreq(state);
3785 if (tevent_req_nomem(subreq, req)) {
3786 return tevent_req_post(req, ev);
3788 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3793 * We send an SMB1 negprot with the SMB2 dialects
3794 * and expect a SMB1 or a SMB2 response.
3796 * smbXcli_negprot_dispatch_incoming() will fix the
3797 * callback to match protocol of the response.
3799 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3801 subreq = smbXcli_negprot_smb1_subreq(state);
3802 if (tevent_req_nomem(subreq, req)) {
3803 return tevent_req_post(req, ev);
3805 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3809 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3811 struct tevent_req *req =
3812 tevent_req_callback_data(subreq,
3817 * we just want the low level error
3819 status = tevent_req_simple_recv_ntstatus(subreq);
3820 TALLOC_FREE(subreq);
3821 if (tevent_req_nterror(req, status)) {
3825 /* this should never happen */
3826 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3829 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3832 DATA_BLOB bytes = data_blob_null;
3836 /* setup the protocol strings */
3837 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3841 if (smb1cli_prots[i].proto < state->min_protocol) {
3845 if (smb1cli_prots[i].proto > state->max_protocol) {
3849 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3855 * We now it is already ascii and
3856 * we want NULL termination.
3858 ok = data_blob_append(state, &bytes,
3859 smb1cli_prots[i].smb1_name,
3860 strlen(smb1cli_prots[i].smb1_name)+1);
3866 smb1cli_req_flags(state->max_protocol,
3867 state->conn->smb1.client.capabilities,
3872 return smb1cli_req_send(state, state->ev, state->conn,
3876 state->timeout_msec,
3877 0xFFFE, 0, NULL, /* pid, tid, session */
3878 0, NULL, /* wct, vwv */
3879 bytes.length, bytes.data);
3882 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3884 struct tevent_req *req =
3885 tevent_req_callback_data(subreq,
3887 struct smbXcli_negprot_state *state =
3888 tevent_req_data(req,
3889 struct smbXcli_negprot_state);
3890 struct smbXcli_conn *conn = state->conn;
3891 struct iovec *recv_iov = NULL;
3900 size_t num_prots = 0;
3902 uint32_t client_capabilities = conn->smb1.client.capabilities;
3903 uint32_t both_capabilities;
3904 uint32_t server_capabilities = 0;
3905 uint32_t capabilities;
3906 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3907 uint32_t server_max_xmit = 0;
3909 uint32_t server_max_mux = 0;
3910 uint16_t server_security_mode = 0;
3911 uint32_t server_session_key = 0;
3912 bool server_readbraw = false;
3913 bool server_writebraw = false;
3914 bool server_lockread = false;
3915 bool server_writeunlock = false;
3916 struct GUID server_guid = GUID_zero();
3917 DATA_BLOB server_gss_blob = data_blob_null;
3918 uint8_t server_challenge[8];
3919 char *server_workgroup = NULL;
3920 char *server_name = NULL;
3921 int server_time_zone = 0;
3922 NTTIME server_system_time = 0;
3923 static const struct smb1cli_req_expected_response expected[] = {
3925 .status = NT_STATUS_OK,
3926 .wct = 0x11, /* NT1 */
3929 .status = NT_STATUS_OK,
3930 .wct = 0x0D, /* LM */
3933 .status = NT_STATUS_OK,
3934 .wct = 0x01, /* CORE */
3938 ZERO_STRUCT(server_challenge);
3940 status = smb1cli_req_recv(subreq, state,
3945 NULL, /* pvwv_offset */
3948 NULL, /* pbytes_offset */
3950 expected, ARRAY_SIZE(expected));
3951 TALLOC_FREE(subreq);
3952 if (tevent_req_nterror(req, status)) {
3956 flags = CVAL(inhdr, HDR_FLG);
3958 protnum = SVAL(vwv, 0);
3960 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3961 if (smb1cli_prots[i].proto < state->min_protocol) {
3965 if (smb1cli_prots[i].proto > state->max_protocol) {
3969 if (protnum != num_prots) {
3974 conn->protocol = smb1cli_prots[i].proto;
3978 if (conn->protocol == PROTOCOL_NONE) {
3979 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3983 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3984 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3985 "and the selected protocol level doesn't support it.\n"));
3986 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3990 if (flags & FLAG_SUPPORT_LOCKREAD) {
3991 server_lockread = true;
3992 server_writeunlock = true;
3995 if (conn->protocol >= PROTOCOL_NT1) {
3996 const char *client_signing = NULL;
3997 bool server_mandatory = false;
3998 bool server_allowed = false;
3999 const char *server_signing = NULL;
4004 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4009 server_security_mode = CVAL(vwv + 1, 0);
4010 server_max_mux = SVAL(vwv + 1, 1);
4011 server_max_xmit = IVAL(vwv + 3, 1);
4012 server_session_key = IVAL(vwv + 7, 1);
4013 server_time_zone = SVALS(vwv + 15, 1);
4014 server_time_zone *= 60;
4015 /* this time arrives in real GMT */
4016 server_system_time = BVAL(vwv + 11, 1);
4017 server_capabilities = IVAL(vwv + 9, 1);
4019 key_len = CVAL(vwv + 16, 1);
4021 if (server_capabilities & CAP_RAW_MODE) {
4022 server_readbraw = true;
4023 server_writebraw = true;
4025 if (server_capabilities & CAP_LOCK_AND_READ) {
4026 server_lockread = true;
4029 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4030 DATA_BLOB blob1, blob2;
4032 if (num_bytes < 16) {
4033 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4037 blob1 = data_blob_const(bytes, 16);
4038 status = GUID_from_data_blob(&blob1, &server_guid);
4039 if (tevent_req_nterror(req, status)) {
4043 blob1 = data_blob_const(bytes+16, num_bytes-16);
4044 blob2 = data_blob_dup_talloc(state, blob1);
4045 if (blob1.length > 0 &&
4046 tevent_req_nomem(blob2.data, req)) {
4049 server_gss_blob = blob2;
4051 DATA_BLOB blob1, blob2;
4053 if (num_bytes < key_len) {
4054 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4058 if (key_len != 0 && key_len != 8) {
4059 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4064 memcpy(server_challenge, bytes, 8);
4067 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4068 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4069 if (blob1.length > 0) {
4072 len = utf16_len_n(blob1.data,
4076 ok = convert_string_talloc(state,
4084 status = map_nt_error_from_unix_common(errno);
4085 tevent_req_nterror(req, status);
4090 blob2.data += blob1.length;
4091 blob2.length -= blob1.length;
4092 if (blob2.length > 0) {
4095 len = utf16_len_n(blob1.data,
4099 ok = convert_string_talloc(state,
4107 status = map_nt_error_from_unix_common(errno);
4108 tevent_req_nterror(req, status);
4114 client_signing = "disabled";
4115 if (conn->allow_signing) {
4116 client_signing = "allowed";
4118 if (conn->mandatory_signing) {
4119 client_signing = "required";
4122 server_signing = "not supported";
4123 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4124 server_signing = "supported";
4125 server_allowed = true;
4126 } else if (conn->mandatory_signing) {
4128 * We have mandatory signing as client
4129 * lets assume the server will look at our
4130 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4131 * flag in the session setup
4133 server_signing = "not announced";
4134 server_allowed = true;
4136 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4137 server_signing = "required";
4138 server_mandatory = true;
4141 ok = smb_signing_set_negotiated(conn->smb1.signing,
4145 DEBUG(1,("cli_negprot: SMB signing is required, "
4146 "but client[%s] and server[%s] mismatch\n",
4147 client_signing, server_signing));
4148 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4152 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4158 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4162 server_security_mode = SVAL(vwv + 1, 0);
4163 server_max_xmit = SVAL(vwv + 2, 0);
4164 server_max_mux = SVAL(vwv + 3, 0);
4165 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4166 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4167 server_session_key = IVAL(vwv + 6, 0);
4168 server_time_zone = SVALS(vwv + 10, 0);
4169 server_time_zone *= 60;
4170 /* this time is converted to GMT by make_unix_date */
4171 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4172 unix_to_nt_time(&server_system_time, t);
4173 key_len = SVAL(vwv + 11, 0);
4175 if (num_bytes < key_len) {
4176 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4180 if (key_len != 0 && key_len != 8) {
4181 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4186 memcpy(server_challenge, bytes, 8);
4189 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4190 if (blob1.length > 0) {
4194 len = utf16_len_n(blob1.data,
4198 ok = convert_string_talloc(state,
4206 status = map_nt_error_from_unix_common(errno);
4207 tevent_req_nterror(req, status);
4213 /* the old core protocol */
4214 server_time_zone = get_time_zone(time(NULL));
4215 server_max_xmit = 1024;
4219 if (server_max_xmit < 1024) {
4220 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4224 if (server_max_mux < 1) {
4225 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4230 * Now calculate the negotiated capabilities
4231 * based on the mask for:
4232 * - client only flags
4233 * - flags used in both directions
4234 * - server only flags
4236 both_capabilities = client_capabilities & server_capabilities;
4237 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4238 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4239 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4241 max_xmit = MIN(client_max_xmit, server_max_xmit);
4243 conn->smb1.server.capabilities = server_capabilities;
4244 conn->smb1.capabilities = capabilities;
4246 conn->smb1.server.max_xmit = server_max_xmit;
4247 conn->smb1.max_xmit = max_xmit;
4249 conn->smb1.server.max_mux = server_max_mux;
4251 conn->smb1.server.security_mode = server_security_mode;
4253 conn->smb1.server.readbraw = server_readbraw;
4254 conn->smb1.server.writebraw = server_writebraw;
4255 conn->smb1.server.lockread = server_lockread;
4256 conn->smb1.server.writeunlock = server_writeunlock;
4258 conn->smb1.server.session_key = server_session_key;
4260 talloc_steal(conn, server_gss_blob.data);
4261 conn->smb1.server.gss_blob = server_gss_blob;
4262 conn->smb1.server.guid = server_guid;
4263 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4264 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4265 conn->smb1.server.name = talloc_move(conn, &server_name);
4267 conn->smb1.server.time_zone = server_time_zone;
4268 conn->smb1.server.system_time = server_system_time;
4270 tevent_req_done(req);
4273 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4277 uint16_t dialect_count = 0;
4279 buf = state->smb2.dyn;
4280 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4281 if (smb2cli_prots[i].proto < state->min_protocol) {
4285 if (smb2cli_prots[i].proto > state->max_protocol) {
4289 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4293 buf = state->smb2.fixed;
4295 SSVAL(buf, 2, dialect_count);
4296 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4297 SSVAL(buf, 6, 0); /* Reserved */
4298 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4299 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4301 SIVAL(buf, 8, 0); /* Capabilities */
4303 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4307 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4309 if (!NT_STATUS_IS_OK(status)) {
4312 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4314 memset(buf+12, 0, 16); /* ClientGuid */
4316 SBVAL(buf, 28, 0); /* ClientStartTime */
4318 return smb2cli_req_send(state, state->ev,
4319 state->conn, SMB2_OP_NEGPROT,
4321 state->timeout_msec,
4322 NULL, NULL, /* tcon, session */
4323 state->smb2.fixed, sizeof(state->smb2.fixed),
4324 state->smb2.dyn, dialect_count*2);
4327 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4329 struct tevent_req *req =
4330 tevent_req_callback_data(subreq,
4332 struct smbXcli_negprot_state *state =
4333 tevent_req_data(req,
4334 struct smbXcli_negprot_state);
4335 struct smbXcli_conn *conn = state->conn;
4336 size_t security_offset, security_length;
4342 uint16_t dialect_revision;
4343 static const struct smb2cli_req_expected_response expected[] = {
4345 .status = NT_STATUS_OK,
4350 status = smb2cli_req_recv(subreq, state, &iov,
4351 expected, ARRAY_SIZE(expected));
4352 TALLOC_FREE(subreq);
4353 if (tevent_req_nterror(req, status)) {
4357 body = (uint8_t *)iov[1].iov_base;
4359 dialect_revision = SVAL(body, 4);
4361 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4362 if (smb2cli_prots[i].proto < state->min_protocol) {
4366 if (smb2cli_prots[i].proto > state->max_protocol) {
4370 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4374 conn->protocol = smb2cli_prots[i].proto;
4378 if (conn->protocol == PROTOCOL_NONE) {
4379 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4380 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4384 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4385 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4389 /* make sure we do not loop forever */
4390 state->min_protocol = PROTOCOL_SMB2_02;
4393 * send a SMB2 negprot, in order to negotiate
4396 subreq = smbXcli_negprot_smb2_subreq(state);
4397 if (tevent_req_nomem(subreq, req)) {
4400 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4404 conn->smb2.server.security_mode = SVAL(body, 2);
4406 blob = data_blob_const(body + 8, 16);
4407 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4408 if (tevent_req_nterror(req, status)) {
4412 conn->smb2.server.capabilities = IVAL(body, 24);
4413 conn->smb2.server.max_trans_size= IVAL(body, 28);
4414 conn->smb2.server.max_read_size = IVAL(body, 32);
4415 conn->smb2.server.max_write_size= IVAL(body, 36);
4416 conn->smb2.server.system_time = BVAL(body, 40);
4417 conn->smb2.server.start_time = BVAL(body, 48);
4419 security_offset = SVAL(body, 56);
4420 security_length = SVAL(body, 58);
4422 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4423 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4427 if (security_length > iov[2].iov_len) {
4428 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4432 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4435 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4439 tevent_req_done(req);
4442 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4443 TALLOC_CTX *tmp_mem,
4446 size_t num_pending = talloc_array_length(conn->pending);
4447 struct tevent_req *subreq;
4448 struct smbXcli_req_state *substate;
4449 struct tevent_req *req;
4450 uint32_t protocol_magic;
4451 size_t inbuf_len = smb_len_nbt(inbuf);
4453 if (num_pending != 1) {
4454 return NT_STATUS_INTERNAL_ERROR;
4457 if (inbuf_len < 4) {
4458 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4461 subreq = conn->pending[0];
4462 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4463 req = tevent_req_callback_data(subreq, struct tevent_req);
4465 protocol_magic = IVAL(inbuf, 4);
4467 switch (protocol_magic) {
4469 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4470 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4471 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4474 if (substate->smb2.recv_iov == NULL) {
4476 * For the SMB1 negprot we have move it.
4478 substate->smb2.recv_iov = substate->smb1.recv_iov;
4479 substate->smb1.recv_iov = NULL;
4483 * we got an SMB2 answer, which consumed sequence number 0
4484 * so we need to use 1 as the next one
4487 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4488 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4489 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4492 DEBUG(10, ("Got non-SMB PDU\n"));
4493 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4496 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4498 return tevent_req_simple_recv_ntstatus(req);
4501 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4502 uint32_t timeout_msec,
4503 enum protocol_types min_protocol,
4504 enum protocol_types max_protocol)
4506 TALLOC_CTX *frame = talloc_stackframe();
4507 struct tevent_context *ev;
4508 struct tevent_req *req;
4509 NTSTATUS status = NT_STATUS_NO_MEMORY;
4512 if (smbXcli_conn_has_async_calls(conn)) {
4514 * Can't use sync call while an async call is in flight
4516 status = NT_STATUS_INVALID_PARAMETER_MIX;
4519 ev = samba_tevent_context_init(frame);
4523 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4524 min_protocol, max_protocol);
4528 ok = tevent_req_poll(req, ev);
4530 status = map_nt_error_from_unix_common(errno);
4533 status = smbXcli_negprot_recv(req);
4539 static int smbXcli_session_destructor(struct smbXcli_session *session)
4541 if (session->conn == NULL) {
4545 DLIST_REMOVE(session->conn->sessions, session);
4549 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4550 struct smbXcli_conn *conn)
4552 struct smbXcli_session *session;
4554 session = talloc_zero(mem_ctx, struct smbXcli_session);
4555 if (session == NULL) {
4558 session->smb2 = talloc_zero(session, struct smb2cli_session);
4559 if (session->smb2 == NULL) {
4560 talloc_free(session);
4563 talloc_set_destructor(session, smbXcli_session_destructor);
4565 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4566 session->conn = conn;
4571 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
4572 struct smbXcli_session *src)
4574 struct smbXcli_session *session;
4576 session = talloc_zero(mem_ctx, struct smbXcli_session);
4577 if (session == NULL) {
4580 session->smb2 = talloc_zero(session, struct smb2cli_session);
4581 if (session->smb2 == NULL) {
4582 talloc_free(session);
4586 session->conn = src->conn;
4587 *session->smb2 = *src->smb2;
4588 session->smb2_channel = src->smb2_channel;
4589 session->disconnect_expired = src->disconnect_expired;
4591 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
4592 talloc_set_destructor(session, smbXcli_session_destructor);
4598 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4599 TALLOC_CTX *mem_ctx,
4602 const DATA_BLOB *application_key;
4604 *key = data_blob_null;
4606 if (session->conn == NULL) {
4607 return NT_STATUS_NO_USER_SESSION_KEY;
4610 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4611 application_key = &session->smb2->application_key;
4613 application_key = &session->smb1.application_key;
4616 if (application_key->length == 0) {
4617 return NT_STATUS_NO_USER_SESSION_KEY;
4620 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4621 if (key->data == NULL) {
4622 return NT_STATUS_NO_MEMORY;
4625 return NT_STATUS_OK;
4628 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
4630 session->disconnect_expired = true;
4633 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4635 return session->smb1.session_id;
4638 void smb1cli_session_set_id(struct smbXcli_session *session,
4639 uint16_t session_id)
4641 session->smb1.session_id = session_id;
4644 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4645 const DATA_BLOB _session_key)
4647 struct smbXcli_conn *conn = session->conn;
4648 uint8_t session_key[16];
4651 return NT_STATUS_INVALID_PARAMETER_MIX;
4654 if (session->smb1.application_key.length != 0) {
4656 * TODO: do not allow this...
4658 * return NT_STATUS_INVALID_PARAMETER_MIX;
4660 data_blob_clear_free(&session->smb1.application_key);
4661 session->smb1.protected_key = false;
4664 if (_session_key.length == 0) {
4665 return NT_STATUS_OK;
4668 ZERO_STRUCT(session_key);
4669 memcpy(session_key, _session_key.data,
4670 MIN(_session_key.length, sizeof(session_key)));
4672 session->smb1.application_key = data_blob_talloc(session,
4674 sizeof(session_key));
4675 ZERO_STRUCT(session_key);
4676 if (session->smb1.application_key.data == NULL) {
4677 return NT_STATUS_NO_MEMORY;
4680 session->smb1.protected_key = false;
4682 return NT_STATUS_OK;
4685 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4687 if (session->smb1.protected_key) {
4688 /* already protected */
4689 return NT_STATUS_OK;
4692 if (session->smb1.application_key.length != 16) {
4693 return NT_STATUS_INVALID_PARAMETER_MIX;
4696 smb_key_derivation(session->smb1.application_key.data,
4697 session->smb1.application_key.length,
4698 session->smb1.application_key.data);
4700 session->smb1.protected_key = true;
4702 return NT_STATUS_OK;
4705 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4707 struct smbXcli_conn *conn = session->conn;
4708 uint8_t security_mode = 0;
4711 return security_mode;
4714 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4715 if (conn->mandatory_signing) {
4716 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4719 return security_mode;
4722 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4724 return session->smb2->session_id;
4727 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4729 return session->smb2->session_flags;
4732 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4733 uint64_t session_id,
4734 uint16_t session_flags)
4736 session->smb2->session_id = session_id;
4737 session->smb2->session_flags = session_flags;
4740 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4742 session->smb2->channel_sequence += 1;
4745 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4746 const DATA_BLOB _session_key,
4747 const struct iovec *recv_iov)
4749 struct smbXcli_conn *conn = session->conn;
4750 uint16_t no_sign_flags;
4751 uint8_t session_key[16];
4755 return NT_STATUS_INVALID_PARAMETER_MIX;
4758 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4760 if (session->smb2->session_flags & no_sign_flags) {
4761 session->smb2->should_sign = false;
4762 return NT_STATUS_OK;
4765 if (session->smb2->signing_key.length != 0) {
4766 return NT_STATUS_INVALID_PARAMETER_MIX;
4769 ZERO_STRUCT(session_key);
4770 memcpy(session_key, _session_key.data,
4771 MIN(_session_key.length, sizeof(session_key)));
4773 session->smb2->signing_key = data_blob_talloc(session,
4775 sizeof(session_key));
4776 if (session->smb2->signing_key.data == NULL) {
4777 ZERO_STRUCT(session_key);
4778 return NT_STATUS_NO_MEMORY;
4781 if (conn->protocol >= PROTOCOL_SMB2_24) {
4782 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4783 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4785 smb2_key_derivation(session_key, sizeof(session_key),
4786 label.data, label.length,
4787 context.data, context.length,
4788 session->smb2->signing_key.data);
4791 session->smb2->encryption_key = data_blob_dup_talloc(session,
4792 session->smb2->signing_key);
4793 if (session->smb2->encryption_key.data == NULL) {
4794 ZERO_STRUCT(session_key);
4795 return NT_STATUS_NO_MEMORY;
4798 if (conn->protocol >= PROTOCOL_SMB2_24) {
4799 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4800 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4802 smb2_key_derivation(session_key, sizeof(session_key),
4803 label.data, label.length,
4804 context.data, context.length,
4805 session->smb2->encryption_key.data);
4808 session->smb2->decryption_key = data_blob_dup_talloc(session,
4809 session->smb2->signing_key);
4810 if (session->smb2->decryption_key.data == NULL) {
4811 ZERO_STRUCT(session_key);
4812 return NT_STATUS_NO_MEMORY;
4815 if (conn->protocol >= PROTOCOL_SMB2_24) {
4816 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4817 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4819 smb2_key_derivation(session_key, sizeof(session_key),
4820 label.data, label.length,
4821 context.data, context.length,
4822 session->smb2->decryption_key.data);
4825 session->smb2->application_key = data_blob_dup_talloc(session,
4826 session->smb2->signing_key);
4827 if (session->smb2->application_key.data == NULL) {
4828 ZERO_STRUCT(session_key);
4829 return NT_STATUS_NO_MEMORY;
4832 if (conn->protocol >= PROTOCOL_SMB2_24) {
4833 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4834 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4836 smb2_key_derivation(session_key, sizeof(session_key),
4837 label.data, label.length,
4838 context.data, context.length,
4839 session->smb2->application_key.data);
4841 ZERO_STRUCT(session_key);
4843 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4844 session->smb2->signing_key);
4845 if (session->smb2_channel.signing_key.data == NULL) {
4846 return NT_STATUS_NO_MEMORY;
4849 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4850 session->conn->protocol,
4852 if (!NT_STATUS_IS_OK(status)) {
4856 session->smb2->should_sign = false;
4857 session->smb2->should_encrypt = false;
4859 if (conn->desire_signing) {
4860 session->smb2->should_sign = true;
4863 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4864 session->smb2->should_sign = true;
4867 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4868 session->smb2->should_encrypt = true;
4871 if (conn->protocol < PROTOCOL_SMB2_24) {
4872 session->smb2->should_encrypt = false;
4875 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4876 session->smb2->should_encrypt = false;
4879 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4880 sizeof(session->smb2->nonce_high));
4881 session->smb2->nonce_low = 1;
4883 return NT_STATUS_OK;
4886 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4887 struct smbXcli_session *session1,
4888 struct smbXcli_conn *conn,
4889 struct smbXcli_session **_session2)
4891 struct smbXcli_session *session2;
4893 if (session1->smb2->signing_key.length == 0) {
4894 return NT_STATUS_INVALID_PARAMETER_MIX;
4898 return NT_STATUS_INVALID_PARAMETER_MIX;
4901 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4902 if (session2 == NULL) {
4903 return NT_STATUS_NO_MEMORY;
4905 session2->smb2 = talloc_reference(session2, session1->smb2);
4906 if (session2->smb2 == NULL) {
4907 talloc_free(session2);
4908 return NT_STATUS_NO_MEMORY;
4911 talloc_set_destructor(session2, smbXcli_session_destructor);
4912 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4913 session2->conn = conn;
4915 *_session2 = session2;
4916 return NT_STATUS_OK;
4919 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4920 const DATA_BLOB _channel_key,
4921 const struct iovec *recv_iov)
4923 struct smbXcli_conn *conn = session->conn;
4924 uint8_t channel_key[16];
4928 return NT_STATUS_INVALID_PARAMETER_MIX;
4931 if (session->smb2_channel.signing_key.length != 0) {
4932 return NT_STATUS_INVALID_PARAMETER_MIX;
4935 ZERO_STRUCT(channel_key);
4936 memcpy(channel_key, _channel_key.data,
4937 MIN(_channel_key.length, sizeof(channel_key)));
4939 session->smb2_channel.signing_key = data_blob_talloc(session,
4941 sizeof(channel_key));
4942 if (session->smb2_channel.signing_key.data == NULL) {
4943 ZERO_STRUCT(channel_key);
4944 return NT_STATUS_NO_MEMORY;
4947 if (conn->protocol >= PROTOCOL_SMB2_24) {
4948 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4949 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4951 smb2_key_derivation(channel_key, sizeof(channel_key),
4952 label.data, label.length,
4953 context.data, context.length,
4954 session->smb2_channel.signing_key.data);
4956 ZERO_STRUCT(channel_key);
4958 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4959 session->conn->protocol,
4961 if (!NT_STATUS_IS_OK(status)) {
4965 return NT_STATUS_OK;
4968 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4970 struct smbXcli_tcon *tcon;
4972 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4980 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4982 return tcon->smb1.tcon_id;
4985 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4987 tcon->smb1.tcon_id = tcon_id;
4990 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
4992 uint16_t optional_support,
4993 uint32_t maximal_access,
4994 uint32_t guest_maximal_access,
4995 const char *service,
4996 const char *fs_type)
4998 tcon->smb1.tcon_id = tcon_id;
4999 tcon->smb1.optional_support = optional_support;
5000 tcon->smb1.maximal_access = maximal_access;
5001 tcon->smb1.guest_maximal_access = guest_maximal_access;
5003 TALLOC_FREE(tcon->smb1.service);
5004 tcon->smb1.service = talloc_strdup(tcon, service);
5005 if (service != NULL && tcon->smb1.service == NULL) {
5009 TALLOC_FREE(tcon->smb1.fs_type);
5010 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5011 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5018 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5020 return tcon->smb2.tcon_id;
5023 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5025 return tcon->smb2.capabilities;
5028 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5029 struct smbXcli_session *session,
5033 uint32_t capabilities,
5034 uint32_t maximal_access)
5036 tcon->smb2.tcon_id = tcon_id;
5037 tcon->smb2.type = type;
5038 tcon->smb2.flags = flags;
5039 tcon->smb2.capabilities = capabilities;
5040 tcon->smb2.maximal_access = maximal_access;
5042 tcon->smb2.should_encrypt = false;
5044 if (session == NULL) {
5048 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5050 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5051 tcon->smb2.should_encrypt = true;