2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Stefan Metzmacher 2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/network.h"
23 #include "../lib/async_req/async_sock.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
26 #include "lib/util/util_net.h"
27 #include "lib/util/dlinklist.h"
28 #include "../libcli/smb/smb_common.h"
29 #include "../libcli/smb/smb_seal.h"
30 #include "../libcli/smb/smb_signing.h"
31 #include "../libcli/smb/read_smb.h"
32 #include "smbXcli_base.h"
33 #include "librpc/ndr/libndr.h"
37 struct smbXcli_session;
43 struct sockaddr_storage local_ss;
44 struct sockaddr_storage remote_ss;
45 const char *remote_name;
47 struct tevent_queue *outgoing;
48 struct tevent_req **pending;
49 struct tevent_req *read_smb_req;
51 enum protocol_types protocol;
54 bool mandatory_signing;
57 * The incoming dispatch function should return:
58 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
59 * - NT_STATUS_OK, if no more processing is desired, e.g.
60 * the dispatch function called
62 * - All other return values disconnect the connection.
64 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
70 uint32_t capabilities;
75 uint32_t capabilities;
78 uint16_t security_mode;
87 const char *workgroup;
93 uint32_t capabilities;
98 struct smb_signing_state *signing;
99 struct smb_trans_enc_state *trans_enc;
101 struct tevent_req *read_braw_req;
106 uint32_t capabilities;
107 uint16_t security_mode;
112 uint32_t capabilities;
113 uint16_t security_mode;
115 uint32_t max_trans_size;
116 uint32_t max_read_size;
117 uint32_t max_write_size;
124 uint16_t cur_credits;
125 uint16_t max_credits;
128 struct smbXcli_session *sessions;
131 struct smb2cli_session {
133 uint16_t session_flags;
134 DATA_BLOB application_key;
135 DATA_BLOB signing_key;
138 DATA_BLOB encryption_key;
139 DATA_BLOB decryption_key;
142 uint16_t channel_sequence;
145 struct smbXcli_session {
146 struct smbXcli_session *prev, *next;
147 struct smbXcli_conn *conn;
151 DATA_BLOB application_key;
155 struct smb2cli_session *smb2;
158 DATA_BLOB signing_key;
162 * this should be a short term hack
163 * until the upper layers have implemented
166 bool disconnect_expired;
169 struct smbXcli_tcon {
172 uint16_t optional_support;
173 uint32_t maximal_access;
174 uint32_t guest_maximal_access;
183 uint32_t capabilities;
184 uint32_t maximal_access;
189 struct smbXcli_req_state {
190 struct tevent_context *ev;
191 struct smbXcli_conn *conn;
192 struct smbXcli_session *session; /* maybe NULL */
193 struct smbXcli_tcon *tcon; /* maybe NULL */
195 uint8_t length_hdr[4];
202 /* Space for the header including the wct */
203 uint8_t hdr[HDR_VWV];
206 * For normal requests, smb1cli_req_send chooses a mid.
207 * SecondaryV trans requests need to use the mid of the primary
208 * request, so we need a place to store it.
209 * Assume it is set if != 0.
214 uint8_t bytecount_buf[2];
216 #define MAX_SMB_IOV 10
217 /* length_hdr, hdr, words, byte_count, buffers */
218 struct iovec iov[1 + 3 + MAX_SMB_IOV];
223 struct tevent_req **chained_requests;
226 NTSTATUS recv_status;
227 /* always an array of 3 talloc elements */
228 struct iovec *recv_iov;
232 const uint8_t *fixed;
237 uint8_t transform[SMB2_TF_HDR_SIZE];
238 uint8_t hdr[SMB2_HDR_BODY];
239 uint8_t pad[7]; /* padding space for compounding */
242 * always an array of 3 talloc elements
243 * (without a SMB2_TRANSFORM header!)
247 struct iovec *recv_iov;
249 uint16_t credit_charge;
253 uint64_t encryption_session_id;
255 bool signing_skipped;
261 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
264 * NT_STATUS_OK, means we do not notify the callers
266 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
268 while (conn->sessions) {
269 conn->sessions->conn = NULL;
270 DLIST_REMOVE(conn->sessions, conn->sessions);
273 if (conn->smb1.trans_enc) {
274 TALLOC_FREE(conn->smb1.trans_enc);
280 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
282 const char *remote_name,
283 enum smb_signing_setting signing_state,
284 uint32_t smb1_capabilities,
285 struct GUID *client_guid,
286 uint32_t smb2_capabilities)
288 struct smbXcli_conn *conn = NULL;
290 struct sockaddr *sa = NULL;
294 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
300 conn->write_fd = dup(fd);
301 if (conn->write_fd == -1) {
305 conn->remote_name = talloc_strdup(conn, remote_name);
306 if (conn->remote_name == NULL) {
311 ss = (void *)&conn->local_ss;
312 sa = (struct sockaddr *)ss;
313 sa_length = sizeof(conn->local_ss);
314 ret = getsockname(fd, sa, &sa_length);
318 ss = (void *)&conn->remote_ss;
319 sa = (struct sockaddr *)ss;
320 sa_length = sizeof(conn->remote_ss);
321 ret = getpeername(fd, sa, &sa_length);
326 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
327 if (conn->outgoing == NULL) {
330 conn->pending = NULL;
332 conn->protocol = PROTOCOL_NONE;
334 switch (signing_state) {
335 case SMB_SIGNING_OFF:
337 conn->allow_signing = false;
338 conn->desire_signing = false;
339 conn->mandatory_signing = false;
341 case SMB_SIGNING_DEFAULT:
342 case SMB_SIGNING_IF_REQUIRED:
343 /* if the server requires it */
344 conn->allow_signing = true;
345 conn->desire_signing = false;
346 conn->mandatory_signing = false;
348 case SMB_SIGNING_REQUIRED:
350 conn->allow_signing = true;
351 conn->desire_signing = true;
352 conn->mandatory_signing = true;
356 conn->smb1.client.capabilities = smb1_capabilities;
357 conn->smb1.client.max_xmit = UINT16_MAX;
359 conn->smb1.capabilities = conn->smb1.client.capabilities;
360 conn->smb1.max_xmit = 1024;
364 /* initialise signing */
365 conn->smb1.signing = smb_signing_init(conn,
367 conn->desire_signing,
368 conn->mandatory_signing);
369 if (!conn->smb1.signing) {
373 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
374 if (conn->mandatory_signing) {
375 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
378 conn->smb2.client.guid = *client_guid;
380 conn->smb2.client.capabilities = smb2_capabilities;
382 conn->smb2.cur_credits = 1;
383 conn->smb2.max_credits = 0;
385 talloc_set_destructor(conn, smbXcli_conn_destructor);
389 if (conn->write_fd != -1) {
390 close(conn->write_fd);
396 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
402 if (conn->read_fd == -1) {
409 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
411 return conn->protocol;
414 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
416 if (conn->protocol >= PROTOCOL_SMB2_02) {
420 if (conn->smb1.capabilities & CAP_UNICODE) {
427 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
429 set_socket_options(conn->read_fd, options);
432 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
434 return &conn->local_ss;
437 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
439 return &conn->remote_ss;
442 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
444 return conn->remote_name;
447 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
449 if (conn->protocol >= PROTOCOL_SMB2_02) {
456 return conn->smb1.server.max_mux;
459 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
461 if (conn->protocol >= PROTOCOL_SMB2_02) {
462 return conn->smb2.server.system_time;
465 return conn->smb1.server.system_time;
468 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
470 if (conn->protocol >= PROTOCOL_SMB2_02) {
471 return &conn->smb2.server.gss_blob;
474 return &conn->smb1.server.gss_blob;
477 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
479 if (conn->protocol >= PROTOCOL_SMB2_02) {
480 return &conn->smb2.server.guid;
483 return &conn->smb1.server.guid;
486 struct smbXcli_conn_samba_suicide_state {
487 struct smbXcli_conn *conn;
492 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
494 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
495 struct tevent_context *ev,
496 struct smbXcli_conn *conn,
499 struct tevent_req *req, *subreq;
500 struct smbXcli_conn_samba_suicide_state *state;
502 req = tevent_req_create(mem_ctx, &state,
503 struct smbXcli_conn_samba_suicide_state);
508 SIVAL(state->buf, 4, 0x74697865);
509 SCVAL(state->buf, 8, exitcode);
510 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
512 state->iov.iov_base = state->buf;
513 state->iov.iov_len = sizeof(state->buf);
515 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
516 false, &state->iov, 1);
517 if (tevent_req_nomem(subreq, req)) {
518 return tevent_req_post(req, ev);
520 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
524 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
526 struct tevent_req *req = tevent_req_callback_data(
527 subreq, struct tevent_req);
528 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
529 req, struct smbXcli_conn_samba_suicide_state);
533 nwritten = writev_recv(subreq, &err);
535 if (nwritten == -1) {
536 NTSTATUS status = map_nt_error_from_unix_common(err);
537 smbXcli_conn_disconnect(state->conn, status);
540 tevent_req_done(req);
543 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
545 return tevent_req_simple_recv_ntstatus(req);
548 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
551 TALLOC_CTX *frame = talloc_stackframe();
552 struct tevent_context *ev;
553 struct tevent_req *req;
554 NTSTATUS status = NT_STATUS_NO_MEMORY;
557 if (smbXcli_conn_has_async_calls(conn)) {
559 * Can't use sync call while an async call is in flight
561 status = NT_STATUS_INVALID_PARAMETER_MIX;
564 ev = samba_tevent_context_init(frame);
568 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
572 ok = tevent_req_poll(req, ev);
574 status = map_nt_error_from_unix_common(errno);
577 status = smbXcli_conn_samba_suicide_recv(req);
583 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
585 return conn->smb1.capabilities;
588 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
590 return conn->smb1.max_xmit;
593 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
595 return conn->smb1.server.session_key;
598 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
600 return conn->smb1.server.challenge;
603 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
605 return conn->smb1.server.security_mode;
608 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
610 return conn->smb1.server.readbraw;
613 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
615 return conn->smb1.server.writebraw;
618 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
620 return conn->smb1.server.lockread;
623 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
625 return conn->smb1.server.writeunlock;
628 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
630 return conn->smb1.server.time_zone;
633 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
634 const DATA_BLOB user_session_key,
635 const DATA_BLOB response)
637 return smb_signing_activate(conn->smb1.signing,
642 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
643 const uint8_t *buf, uint32_t seqnum)
645 const uint8_t *hdr = buf + NBT_HDR_SIZE;
646 size_t len = smb_len_nbt(buf);
648 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
651 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
653 return smb_signing_is_active(conn->smb1.signing);
656 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
657 struct smb_trans_enc_state *es)
659 /* Replace the old state, if any. */
660 if (conn->smb1.trans_enc) {
661 TALLOC_FREE(conn->smb1.trans_enc);
663 conn->smb1.trans_enc = es;
666 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
668 return common_encryption_on(conn->smb1.trans_enc);
672 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
674 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
675 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
677 if (NT_STATUS_IS_OK(status)) {
681 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
685 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
689 * Is the SMB command able to hold an AND_X successor
690 * @param[in] cmd The SMB command in question
691 * @retval Can we add a chained request after "cmd"?
693 bool smb1cli_is_andx_req(uint8_t cmd)
713 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
715 size_t num_pending = talloc_array_length(conn->pending);
721 result = conn->smb1.mid++;
722 if ((result == 0) || (result == 0xffff)) {
726 for (i=0; i<num_pending; i++) {
727 if (result == smb1cli_req_mid(conn->pending[i])) {
732 if (i == num_pending) {
738 void smbXcli_req_unset_pending(struct tevent_req *req)
740 struct smbXcli_req_state *state =
742 struct smbXcli_req_state);
743 struct smbXcli_conn *conn = state->conn;
744 size_t num_pending = talloc_array_length(conn->pending);
747 if (state->smb1.mid != 0) {
749 * This is a [nt]trans[2] request which waits
750 * for more than one reply.
755 talloc_set_destructor(req, NULL);
757 if (num_pending == 1) {
759 * The pending read_smb tevent_req is a child of
760 * conn->pending. So if nothing is pending anymore, we need to
761 * delete the socket read fde.
763 TALLOC_FREE(conn->pending);
764 conn->read_smb_req = NULL;
768 for (i=0; i<num_pending; i++) {
769 if (req == conn->pending[i]) {
773 if (i == num_pending) {
775 * Something's seriously broken. Just returning here is the
776 * right thing nevertheless, the point of this routine is to
777 * remove ourselves from conn->pending.
783 * Remove ourselves from the conn->pending array
785 for (; i < (num_pending - 1); i++) {
786 conn->pending[i] = conn->pending[i+1];
790 * No NULL check here, we're shrinking by sizeof(void *), and
791 * talloc_realloc just adjusts the size for this.
793 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
798 static int smbXcli_req_destructor(struct tevent_req *req)
800 struct smbXcli_req_state *state =
802 struct smbXcli_req_state);
805 * Make sure we really remove it from
806 * the pending array on destruction.
809 smbXcli_req_unset_pending(req);
813 static bool smb1cli_req_cancel(struct tevent_req *req);
814 static bool smb2cli_req_cancel(struct tevent_req *req);
816 static bool smbXcli_req_cancel(struct tevent_req *req)
818 struct smbXcli_req_state *state =
820 struct smbXcli_req_state);
822 if (!smbXcli_conn_is_connected(state->conn)) {
826 if (state->conn->protocol == PROTOCOL_NONE) {
830 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
831 return smb2cli_req_cancel(req);
834 return smb1cli_req_cancel(req);
837 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
839 bool smbXcli_req_set_pending(struct tevent_req *req)
841 struct smbXcli_req_state *state =
843 struct smbXcli_req_state);
844 struct smbXcli_conn *conn;
845 struct tevent_req **pending;
850 if (!smbXcli_conn_is_connected(conn)) {
854 num_pending = talloc_array_length(conn->pending);
856 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
858 if (pending == NULL) {
861 pending[num_pending] = req;
862 conn->pending = pending;
863 talloc_set_destructor(req, smbXcli_req_destructor);
864 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
866 if (!smbXcli_conn_receive_next(conn)) {
868 * the caller should notify the current request
870 * And all other pending requests get notified
871 * by smbXcli_conn_disconnect().
873 smbXcli_req_unset_pending(req);
874 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
881 static void smbXcli_conn_received(struct tevent_req *subreq);
883 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
885 size_t num_pending = talloc_array_length(conn->pending);
886 struct tevent_req *req;
887 struct smbXcli_req_state *state;
889 if (conn->read_smb_req != NULL) {
893 if (num_pending == 0) {
894 if (conn->smb2.mid < UINT64_MAX) {
895 /* no more pending requests, so we are done for now */
900 * If there are no more SMB2 requests possible,
901 * because we are out of message ids,
902 * we need to disconnect.
904 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
908 req = conn->pending[0];
909 state = tevent_req_data(req, struct smbXcli_req_state);
912 * We're the first ones, add the read_smb request that waits for the
913 * answer from the server
915 conn->read_smb_req = read_smb_send(conn->pending,
918 if (conn->read_smb_req == NULL) {
921 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
925 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
927 struct smbXcli_session *session;
929 tevent_queue_stop(conn->outgoing);
931 if (conn->read_fd != -1) {
932 close(conn->read_fd);
934 if (conn->write_fd != -1) {
935 close(conn->write_fd);
940 session = conn->sessions;
941 if (talloc_array_length(conn->pending) == 0) {
943 * if we do not have pending requests
944 * there is no need to update the channel_sequence
948 for (; session; session = session->next) {
949 smb2cli_session_increment_channel_sequence(session);
953 * Cancel all pending requests. We do not do a for-loop walking
954 * conn->pending because that array changes in
955 * smbXcli_req_unset_pending.
957 while (talloc_array_length(conn->pending) > 0) {
958 struct tevent_req *req;
959 struct smbXcli_req_state *state;
960 struct tevent_req **chain;
964 req = conn->pending[0];
965 state = tevent_req_data(req, struct smbXcli_req_state);
967 if (state->smb1.chained_requests == NULL) {
969 * We're dead. No point waiting for trans2
974 smbXcli_req_unset_pending(req);
976 if (NT_STATUS_IS_OK(status)) {
977 /* do not notify the callers */
982 * we need to defer the callback, because we may notify
983 * more then one caller.
985 tevent_req_defer_callback(req, state->ev);
986 tevent_req_nterror(req, status);
990 chain = talloc_move(conn, &state->smb1.chained_requests);
991 num_chained = talloc_array_length(chain);
993 for (i=0; i<num_chained; i++) {
995 state = tevent_req_data(req, struct smbXcli_req_state);
998 * We're dead. No point waiting for trans2
1001 state->smb1.mid = 0;
1003 smbXcli_req_unset_pending(req);
1005 if (NT_STATUS_IS_OK(status)) {
1006 /* do not notify the callers */
1011 * we need to defer the callback, because we may notify
1012 * more than one caller.
1014 tevent_req_defer_callback(req, state->ev);
1015 tevent_req_nterror(req, status);
1022 * Fetch a smb request's mid. Only valid after the request has been sent by
1023 * smb1cli_req_send().
1025 uint16_t smb1cli_req_mid(struct tevent_req *req)
1027 struct smbXcli_req_state *state =
1028 tevent_req_data(req,
1029 struct smbXcli_req_state);
1031 if (state->smb1.mid != 0) {
1032 return state->smb1.mid;
1035 return SVAL(state->smb1.hdr, HDR_MID);
1038 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1040 struct smbXcli_req_state *state =
1041 tevent_req_data(req,
1042 struct smbXcli_req_state);
1044 state->smb1.mid = mid;
1047 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1049 struct smbXcli_req_state *state =
1050 tevent_req_data(req,
1051 struct smbXcli_req_state);
1053 return state->smb1.seqnum;
1056 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1058 struct smbXcli_req_state *state =
1059 tevent_req_data(req,
1060 struct smbXcli_req_state);
1062 state->smb1.seqnum = seqnum;
1065 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1069 for (i=0; i<count; i++) {
1070 result += iov[i].iov_len;
1075 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1076 const struct iovec *iov,
1079 size_t len = smbXcli_iov_len(iov, count);
1084 buf = talloc_array(mem_ctx, uint8_t, len);
1089 for (i=0; i<count; i++) {
1090 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1091 copied += iov[i].iov_len;
1096 static void smb1cli_req_flags(enum protocol_types protocol,
1097 uint32_t smb1_capabilities,
1098 uint8_t smb_command,
1099 uint8_t additional_flags,
1100 uint8_t clear_flags,
1102 uint16_t additional_flags2,
1103 uint16_t clear_flags2,
1107 uint16_t flags2 = 0;
1109 if (protocol >= PROTOCOL_LANMAN1) {
1110 flags |= FLAG_CASELESS_PATHNAMES;
1111 flags |= FLAG_CANONICAL_PATHNAMES;
1114 if (protocol >= PROTOCOL_LANMAN2) {
1115 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1116 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1119 if (protocol >= PROTOCOL_NT1) {
1120 flags2 |= FLAGS2_IS_LONG_NAME;
1122 if (smb1_capabilities & CAP_UNICODE) {
1123 flags2 |= FLAGS2_UNICODE_STRINGS;
1125 if (smb1_capabilities & CAP_STATUS32) {
1126 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1128 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1129 flags2 |= FLAGS2_EXTENDED_SECURITY;
1133 flags |= additional_flags;
1134 flags &= ~clear_flags;
1135 flags2 |= additional_flags2;
1136 flags2 &= ~clear_flags2;
1142 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1144 static bool smb1cli_req_cancel(struct tevent_req *req)
1146 struct smbXcli_req_state *state =
1147 tevent_req_data(req,
1148 struct smbXcli_req_state);
1153 struct tevent_req *subreq;
1156 flags = CVAL(state->smb1.hdr, HDR_FLG);
1157 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1158 pid = SVAL(state->smb1.hdr, HDR_PID);
1159 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1160 mid = SVAL(state->smb1.hdr, HDR_MID);
1162 subreq = smb1cli_req_create(state, state->ev,
1172 0, NULL); /* bytes */
1173 if (subreq == NULL) {
1176 smb1cli_req_set_mid(subreq, mid);
1178 status = smb1cli_req_chain_submit(&subreq, 1);
1179 if (!NT_STATUS_IS_OK(status)) {
1180 TALLOC_FREE(subreq);
1183 smb1cli_req_set_mid(subreq, 0);
1185 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1190 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1192 /* we do not care about the result */
1193 TALLOC_FREE(subreq);
1196 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1197 struct tevent_context *ev,
1198 struct smbXcli_conn *conn,
1199 uint8_t smb_command,
1200 uint8_t additional_flags,
1201 uint8_t clear_flags,
1202 uint16_t additional_flags2,
1203 uint16_t clear_flags2,
1204 uint32_t timeout_msec,
1206 struct smbXcli_tcon *tcon,
1207 struct smbXcli_session *session,
1208 uint8_t wct, uint16_t *vwv,
1210 struct iovec *bytes_iov)
1212 struct tevent_req *req;
1213 struct smbXcli_req_state *state;
1215 uint16_t flags2 = 0;
1219 if (iov_count > MAX_SMB_IOV) {
1221 * Should not happen :-)
1226 req = tevent_req_create(mem_ctx, &state,
1227 struct smbXcli_req_state);
1233 state->session = session;
1237 uid = session->smb1.session_id;
1241 tid = tcon->smb1.tcon_id;
1244 state->smb1.recv_cmd = 0xFF;
1245 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1246 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1247 if (state->smb1.recv_iov == NULL) {
1252 smb1cli_req_flags(conn->protocol,
1253 conn->smb1.capabilities,
1262 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1263 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1264 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1265 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1266 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1267 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1268 SSVAL(state->smb1.hdr, HDR_TID, tid);
1269 SSVAL(state->smb1.hdr, HDR_PID, pid);
1270 SSVAL(state->smb1.hdr, HDR_UID, uid);
1271 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1272 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1274 state->smb1.vwv = vwv;
1276 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1278 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1279 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1280 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1281 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1282 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1283 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1284 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1285 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1287 if (iov_count != 0) {
1288 memcpy(&state->smb1.iov[4], bytes_iov,
1289 iov_count * sizeof(*bytes_iov));
1291 state->smb1.iov_count = iov_count + 4;
1293 if (timeout_msec > 0) {
1294 struct timeval endtime;
1296 endtime = timeval_current_ofs_msec(timeout_msec);
1297 if (!tevent_req_set_endtime(req, ev, endtime)) {
1302 switch (smb_command) {
1306 state->one_way = true;
1309 state->one_way = true;
1310 state->smb1.one_way_seqnum = true;
1314 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1315 state->one_way = true;
1323 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1324 struct iovec *iov, int iov_count,
1326 bool one_way_seqnum)
1328 TALLOC_CTX *frame = NULL;
1332 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1333 * iovec directly. MD5Update would do that just fine.
1336 if (iov_count < 4) {
1337 return NT_STATUS_INVALID_PARAMETER_MIX;
1339 if (iov[0].iov_len != NBT_HDR_SIZE) {
1340 return NT_STATUS_INVALID_PARAMETER_MIX;
1342 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1343 return NT_STATUS_INVALID_PARAMETER_MIX;
1345 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1346 return NT_STATUS_INVALID_PARAMETER_MIX;
1348 if (iov[3].iov_len != sizeof(uint16_t)) {
1349 return NT_STATUS_INVALID_PARAMETER_MIX;
1352 frame = talloc_stackframe();
1354 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1356 return NT_STATUS_NO_MEMORY;
1359 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1361 smb_signing_sign_pdu(conn->smb1.signing,
1362 buf, talloc_get_size(buf),
1364 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1367 return NT_STATUS_OK;
1370 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1371 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1372 TALLOC_CTX *tmp_mem,
1375 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1376 struct smbXcli_req_state *state,
1377 struct iovec *iov, int iov_count)
1379 struct tevent_req *subreq;
1384 if (!smbXcli_conn_is_connected(state->conn)) {
1385 return NT_STATUS_CONNECTION_DISCONNECTED;
1388 if (state->conn->protocol > PROTOCOL_NT1) {
1389 return NT_STATUS_REVISION_MISMATCH;
1392 if (iov_count < 4) {
1393 return NT_STATUS_INVALID_PARAMETER_MIX;
1395 if (iov[0].iov_len != NBT_HDR_SIZE) {
1396 return NT_STATUS_INVALID_PARAMETER_MIX;
1398 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1399 return NT_STATUS_INVALID_PARAMETER_MIX;
1401 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1402 return NT_STATUS_INVALID_PARAMETER_MIX;
1404 if (iov[3].iov_len != sizeof(uint16_t)) {
1405 return NT_STATUS_INVALID_PARAMETER_MIX;
1408 cmd = CVAL(iov[1].iov_base, HDR_COM);
1409 if (cmd == SMBreadBraw) {
1410 if (smbXcli_conn_has_async_calls(state->conn)) {
1411 return NT_STATUS_INVALID_PARAMETER_MIX;
1413 state->conn->smb1.read_braw_req = req;
1416 if (state->smb1.mid != 0) {
1417 mid = state->smb1.mid;
1419 mid = smb1cli_alloc_mid(state->conn);
1421 SSVAL(iov[1].iov_base, HDR_MID, mid);
1423 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1425 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1426 &state->smb1.seqnum,
1427 state->smb1.one_way_seqnum);
1429 if (!NT_STATUS_IS_OK(status)) {
1434 * If we supported multiple encrytion contexts
1435 * here we'd look up based on tid.
1437 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1438 char *buf, *enc_buf;
1440 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1442 return NT_STATUS_NO_MEMORY;
1444 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1445 (char *)buf, &enc_buf);
1447 if (!NT_STATUS_IS_OK(status)) {
1448 DEBUG(0, ("Error in encrypting client message: %s\n",
1449 nt_errstr(status)));
1452 buf = (char *)talloc_memdup(state, enc_buf,
1453 smb_len_nbt(enc_buf)+4);
1456 return NT_STATUS_NO_MEMORY;
1458 iov[0].iov_base = (void *)buf;
1459 iov[0].iov_len = talloc_get_size(buf);
1463 if (state->conn->dispatch_incoming == NULL) {
1464 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1467 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1469 subreq = writev_send(state, state->ev, state->conn->outgoing,
1470 state->conn->write_fd, false, iov, iov_count);
1471 if (subreq == NULL) {
1472 return NT_STATUS_NO_MEMORY;
1474 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1475 return NT_STATUS_OK;
1478 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1479 struct tevent_context *ev,
1480 struct smbXcli_conn *conn,
1481 uint8_t smb_command,
1482 uint8_t additional_flags,
1483 uint8_t clear_flags,
1484 uint16_t additional_flags2,
1485 uint16_t clear_flags2,
1486 uint32_t timeout_msec,
1488 struct smbXcli_tcon *tcon,
1489 struct smbXcli_session *session,
1490 uint8_t wct, uint16_t *vwv,
1492 const uint8_t *bytes)
1494 struct tevent_req *req;
1498 iov.iov_base = discard_const_p(void, bytes);
1499 iov.iov_len = num_bytes;
1501 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1502 additional_flags, clear_flags,
1503 additional_flags2, clear_flags2,
1510 if (!tevent_req_is_in_progress(req)) {
1511 return tevent_req_post(req, ev);
1513 status = smb1cli_req_chain_submit(&req, 1);
1514 if (tevent_req_nterror(req, status)) {
1515 return tevent_req_post(req, ev);
1520 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1522 struct tevent_req *req =
1523 tevent_req_callback_data(subreq,
1525 struct smbXcli_req_state *state =
1526 tevent_req_data(req,
1527 struct smbXcli_req_state);
1531 nwritten = writev_recv(subreq, &err);
1532 TALLOC_FREE(subreq);
1533 if (nwritten == -1) {
1534 NTSTATUS status = map_nt_error_from_unix_common(err);
1535 smbXcli_conn_disconnect(state->conn, status);
1539 if (state->one_way) {
1540 state->inbuf = NULL;
1541 tevent_req_done(req);
1545 if (!smbXcli_req_set_pending(req)) {
1546 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1551 static void smbXcli_conn_received(struct tevent_req *subreq)
1553 struct smbXcli_conn *conn =
1554 tevent_req_callback_data(subreq,
1555 struct smbXcli_conn);
1556 TALLOC_CTX *frame = talloc_stackframe();
1562 if (subreq != conn->read_smb_req) {
1563 DEBUG(1, ("Internal error: cli_smb_received called with "
1564 "unexpected subreq\n"));
1565 status = NT_STATUS_INTERNAL_ERROR;
1566 smbXcli_conn_disconnect(conn, status);
1570 conn->read_smb_req = NULL;
1572 received = read_smb_recv(subreq, frame, &inbuf, &err);
1573 TALLOC_FREE(subreq);
1574 if (received == -1) {
1575 status = map_nt_error_from_unix_common(err);
1576 smbXcli_conn_disconnect(conn, status);
1581 status = conn->dispatch_incoming(conn, frame, inbuf);
1583 if (NT_STATUS_IS_OK(status)) {
1585 * We should not do any more processing
1586 * as the dispatch function called
1587 * tevent_req_done().
1592 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1594 * We got an error, so notify all pending requests
1596 smbXcli_conn_disconnect(conn, status);
1601 * We got NT_STATUS_RETRY, so we may ask for a
1602 * next incoming pdu.
1604 if (!smbXcli_conn_receive_next(conn)) {
1605 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1609 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1610 struct iovec **piov, int *pnum_iov)
1621 size_t min_size = MIN_SMB_SIZE;
1623 buflen = smb_len_tcp(buf);
1626 hdr = buf + NBT_HDR_SIZE;
1628 status = smb1cli_pull_raw_error(hdr);
1629 if (NT_STATUS_IS_ERR(status)) {
1631 * This is an ugly hack to support OS/2
1632 * which skips the byte_count in the DATA block
1633 * on some error responses.
1637 min_size -= sizeof(uint16_t);
1640 if (buflen < min_size) {
1641 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1645 * This returns iovec elements in the following order:
1660 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1662 return NT_STATUS_NO_MEMORY;
1664 iov[0].iov_base = hdr;
1665 iov[0].iov_len = HDR_WCT;
1668 cmd = CVAL(hdr, HDR_COM);
1672 size_t len = buflen - taken;
1674 struct iovec *iov_tmp;
1681 * we need at least WCT
1683 needed = sizeof(uint8_t);
1685 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1686 __location__, (int)len, (int)needed));
1691 * Now we check if the specified words are there
1693 wct = CVAL(hdr, wct_ofs);
1694 needed += wct * sizeof(uint16_t);
1696 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1697 __location__, (int)len, (int)needed));
1701 if ((num_iov == 1) &&
1703 NT_STATUS_IS_ERR(status))
1706 * This is an ugly hack to support OS/2
1707 * which skips the byte_count in the DATA block
1708 * on some error responses.
1712 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1714 if (iov_tmp == NULL) {
1716 return NT_STATUS_NO_MEMORY;
1719 cur = &iov[num_iov];
1723 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1725 cur[1].iov_base = cur[0].iov_base;
1732 * we need at least BCC
1734 needed += sizeof(uint16_t);
1736 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1737 __location__, (int)len, (int)needed));
1742 * Now we check if the specified bytes are there
1744 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1745 bcc = SVAL(hdr, bcc_ofs);
1746 needed += bcc * sizeof(uint8_t);
1748 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1749 __location__, (int)len, (int)needed));
1754 * we allocate 2 iovec structures for words and bytes
1756 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1758 if (iov_tmp == NULL) {
1760 return NT_STATUS_NO_MEMORY;
1763 cur = &iov[num_iov];
1766 cur[0].iov_len = wct * sizeof(uint16_t);
1767 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1768 cur[1].iov_len = bcc * sizeof(uint8_t);
1769 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1773 if (!smb1cli_is_andx_req(cmd)) {
1775 * If the current command does not have AndX chanining
1781 if (wct == 0 && bcc == 0) {
1783 * An empty response also ends the chain,
1784 * most likely with an error.
1790 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1791 __location__, (int)wct, (int)cmd));
1794 cmd = CVAL(cur[0].iov_base, 0);
1797 * If it is the end of the chain we are also done.
1801 wct_ofs = SVAL(cur[0].iov_base, 2);
1803 if (wct_ofs < taken) {
1804 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1806 if (wct_ofs > buflen) {
1807 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1811 * we consumed everything up to the start of the next
1817 remaining = buflen - taken;
1819 if (remaining > 0 && num_iov >= 3) {
1821 * The last DATA block gets the remaining
1822 * bytes, this is needed to support
1823 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1825 iov[num_iov-1].iov_len += remaining;
1829 *pnum_iov = num_iov;
1830 return NT_STATUS_OK;
1834 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1837 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1838 TALLOC_CTX *tmp_mem,
1841 struct tevent_req *req;
1842 struct smbXcli_req_state *state;
1849 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1850 size_t len = smb_len_tcp(inbuf);
1851 struct iovec *iov = NULL;
1853 struct tevent_req **chain = NULL;
1854 size_t num_chained = 0;
1855 size_t num_responses = 0;
1857 if (conn->smb1.read_braw_req != NULL) {
1858 req = conn->smb1.read_braw_req;
1859 conn->smb1.read_braw_req = NULL;
1860 state = tevent_req_data(req, struct smbXcli_req_state);
1862 smbXcli_req_unset_pending(req);
1864 if (state->smb1.recv_iov == NULL) {
1866 * For requests with more than
1867 * one response, we have to readd the
1870 state->smb1.recv_iov = talloc_zero_array(state,
1873 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1874 return NT_STATUS_OK;
1878 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1879 state->smb1.recv_iov[0].iov_len = len;
1880 ZERO_STRUCT(state->smb1.recv_iov[1]);
1881 ZERO_STRUCT(state->smb1.recv_iov[2]);
1883 state->smb1.recv_cmd = SMBreadBraw;
1884 state->smb1.recv_status = NT_STATUS_OK;
1885 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1887 tevent_req_done(req);
1888 return NT_STATUS_OK;
1891 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1892 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1893 DEBUG(10, ("Got non-SMB PDU\n"));
1894 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1898 * If we supported multiple encrytion contexts
1899 * here we'd look up based on tid.
1901 if (common_encryption_on(conn->smb1.trans_enc)
1902 && (CVAL(inbuf, 0) == 0)) {
1903 uint16_t enc_ctx_num;
1905 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1906 if (!NT_STATUS_IS_OK(status)) {
1907 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1908 nt_errstr(status)));
1912 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1913 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1915 conn->smb1.trans_enc->enc_ctx_num));
1916 return NT_STATUS_INVALID_HANDLE;
1919 status = common_decrypt_buffer(conn->smb1.trans_enc,
1921 if (!NT_STATUS_IS_OK(status)) {
1922 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1923 nt_errstr(status)));
1926 inhdr = inbuf + NBT_HDR_SIZE;
1927 len = smb_len_nbt(inbuf);
1930 mid = SVAL(inhdr, HDR_MID);
1931 num_pending = talloc_array_length(conn->pending);
1933 for (i=0; i<num_pending; i++) {
1934 if (mid == smb1cli_req_mid(conn->pending[i])) {
1938 if (i == num_pending) {
1939 /* Dump unexpected reply */
1940 return NT_STATUS_RETRY;
1943 oplock_break = false;
1945 if (mid == 0xffff) {
1947 * Paranoia checks that this is really an oplock break request.
1949 oplock_break = (len == 51); /* hdr + 8 words */
1950 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1951 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1952 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1953 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1955 if (!oplock_break) {
1956 /* Dump unexpected reply */
1957 return NT_STATUS_RETRY;
1961 req = conn->pending[i];
1962 state = tevent_req_data(req, struct smbXcli_req_state);
1964 if (!oplock_break /* oplock breaks are not signed */
1965 && !smb_signing_check_pdu(conn->smb1.signing,
1966 inhdr, len, state->smb1.seqnum+1)) {
1967 DEBUG(10, ("cli_check_sign_mac failed\n"));
1968 return NT_STATUS_ACCESS_DENIED;
1971 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1973 if (!NT_STATUS_IS_OK(status)) {
1974 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1975 nt_errstr(status)));
1979 cmd = CVAL(inhdr, HDR_COM);
1980 status = smb1cli_pull_raw_error(inhdr);
1982 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
1983 (state->session != NULL) && state->session->disconnect_expired)
1986 * this should be a short term hack
1987 * until the upper layers have implemented
1988 * re-authentication.
1993 if (state->smb1.chained_requests == NULL) {
1995 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1998 smbXcli_req_unset_pending(req);
2000 if (state->smb1.recv_iov == NULL) {
2002 * For requests with more than
2003 * one response, we have to readd the
2006 state->smb1.recv_iov = talloc_zero_array(state,
2009 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2010 return NT_STATUS_OK;
2014 state->smb1.recv_cmd = cmd;
2015 state->smb1.recv_status = status;
2016 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2018 state->smb1.recv_iov[0] = iov[0];
2019 state->smb1.recv_iov[1] = iov[1];
2020 state->smb1.recv_iov[2] = iov[2];
2022 if (talloc_array_length(conn->pending) == 0) {
2023 tevent_req_done(req);
2024 return NT_STATUS_OK;
2027 tevent_req_defer_callback(req, state->ev);
2028 tevent_req_done(req);
2029 return NT_STATUS_RETRY;
2032 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2033 num_chained = talloc_array_length(chain);
2034 num_responses = (num_iov - 1)/2;
2036 if (num_responses > num_chained) {
2037 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2040 for (i=0; i<num_chained; i++) {
2041 size_t iov_idx = 1 + (i*2);
2042 struct iovec *cur = &iov[iov_idx];
2046 state = tevent_req_data(req, struct smbXcli_req_state);
2048 smbXcli_req_unset_pending(req);
2051 * as we finish multiple requests here
2052 * we need to defer the callbacks as
2053 * they could destroy our current stack state.
2055 tevent_req_defer_callback(req, state->ev);
2057 if (i >= num_responses) {
2058 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2062 if (state->smb1.recv_iov == NULL) {
2064 * For requests with more than
2065 * one response, we have to readd the
2068 state->smb1.recv_iov = talloc_zero_array(state,
2071 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2076 state->smb1.recv_cmd = cmd;
2078 if (i == (num_responses - 1)) {
2080 * The last request in the chain gets the status
2082 state->smb1.recv_status = status;
2084 cmd = CVAL(cur[0].iov_base, 0);
2085 state->smb1.recv_status = NT_STATUS_OK;
2088 state->inbuf = inbuf;
2091 * Note: here we use talloc_reference() in a way
2092 * that does not expose it to the caller.
2094 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2095 if (tevent_req_nomem(inbuf_ref, req)) {
2099 /* copy the related buffers */
2100 state->smb1.recv_iov[0] = iov[0];
2101 state->smb1.recv_iov[1] = cur[0];
2102 state->smb1.recv_iov[2] = cur[1];
2104 tevent_req_done(req);
2107 return NT_STATUS_RETRY;
2110 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2111 TALLOC_CTX *mem_ctx,
2112 struct iovec **piov,
2116 uint32_t *pvwv_offset,
2117 uint32_t *pnum_bytes,
2119 uint32_t *pbytes_offset,
2121 const struct smb1cli_req_expected_response *expected,
2122 size_t num_expected)
2124 struct smbXcli_req_state *state =
2125 tevent_req_data(req,
2126 struct smbXcli_req_state);
2127 NTSTATUS status = NT_STATUS_OK;
2128 struct iovec *recv_iov = NULL;
2129 uint8_t *hdr = NULL;
2131 uint32_t vwv_offset = 0;
2132 uint16_t *vwv = NULL;
2133 uint32_t num_bytes = 0;
2134 uint32_t bytes_offset = 0;
2135 uint8_t *bytes = NULL;
2137 bool found_status = false;
2138 bool found_size = false;
2152 if (pvwv_offset != NULL) {
2155 if (pnum_bytes != NULL) {
2158 if (pbytes != NULL) {
2161 if (pbytes_offset != NULL) {
2164 if (pinbuf != NULL) {
2168 if (state->inbuf != NULL) {
2169 recv_iov = state->smb1.recv_iov;
2170 state->smb1.recv_iov = NULL;
2171 if (state->smb1.recv_cmd != SMBreadBraw) {
2172 hdr = (uint8_t *)recv_iov[0].iov_base;
2173 wct = recv_iov[1].iov_len/2;
2174 vwv = (uint16_t *)recv_iov[1].iov_base;
2175 vwv_offset = PTR_DIFF(vwv, hdr);
2176 num_bytes = recv_iov[2].iov_len;
2177 bytes = (uint8_t *)recv_iov[2].iov_base;
2178 bytes_offset = PTR_DIFF(bytes, hdr);
2182 if (tevent_req_is_nterror(req, &status)) {
2183 for (i=0; i < num_expected; i++) {
2184 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2185 found_status = true;
2191 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2197 if (num_expected == 0) {
2198 found_status = true;
2202 status = state->smb1.recv_status;
2204 for (i=0; i < num_expected; i++) {
2205 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2209 found_status = true;
2210 if (expected[i].wct == 0) {
2215 if (expected[i].wct == wct) {
2221 if (!found_status) {
2226 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2230 *piov = talloc_move(mem_ctx, &recv_iov);
2242 if (pvwv_offset != NULL) {
2243 *pvwv_offset = vwv_offset;
2245 if (pnum_bytes != NULL) {
2246 *pnum_bytes = num_bytes;
2248 if (pbytes != NULL) {
2251 if (pbytes_offset != NULL) {
2252 *pbytes_offset = bytes_offset;
2254 if (pinbuf != NULL) {
2255 *pinbuf = state->inbuf;
2261 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2268 for (i=0; i<num_reqs; i++) {
2269 struct smbXcli_req_state *state;
2270 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2271 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2272 state->smb1.iov_count-2);
2273 wct_ofs = (wct_ofs + 3) & ~3;
2278 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2280 struct smbXcli_req_state *first_state =
2281 tevent_req_data(reqs[0],
2282 struct smbXcli_req_state);
2283 struct smbXcli_req_state *state;
2285 size_t chain_padding = 0;
2287 struct iovec *iov = NULL;
2288 struct iovec *this_iov;
2292 if (num_reqs == 1) {
2293 return smb1cli_req_writev_submit(reqs[0], first_state,
2294 first_state->smb1.iov,
2295 first_state->smb1.iov_count);
2299 for (i=0; i<num_reqs; i++) {
2300 if (!tevent_req_is_in_progress(reqs[i])) {
2301 return NT_STATUS_INTERNAL_ERROR;
2304 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2306 if (state->smb1.iov_count < 4) {
2307 return NT_STATUS_INVALID_PARAMETER_MIX;
2312 * The NBT and SMB header
2325 iovlen += state->smb1.iov_count - 2;
2328 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2330 return NT_STATUS_NO_MEMORY;
2333 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2334 first_state, reqs, sizeof(*reqs) * num_reqs);
2335 if (first_state->smb1.chained_requests == NULL) {
2337 return NT_STATUS_NO_MEMORY;
2340 wct_offset = HDR_WCT;
2343 for (i=0; i<num_reqs; i++) {
2344 size_t next_padding = 0;
2347 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2349 if (i < num_reqs-1) {
2350 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2351 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2353 TALLOC_FREE(first_state->smb1.chained_requests);
2354 return NT_STATUS_INVALID_PARAMETER_MIX;
2358 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2359 state->smb1.iov_count-2) + 1;
2360 if ((wct_offset % 4) != 0) {
2361 next_padding = 4 - (wct_offset % 4);
2363 wct_offset += next_padding;
2364 vwv = state->smb1.vwv;
2366 if (i < num_reqs-1) {
2367 struct smbXcli_req_state *next_state =
2368 tevent_req_data(reqs[i+1],
2369 struct smbXcli_req_state);
2370 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2372 SSVAL(vwv+1, 0, wct_offset);
2373 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2374 /* properly end the chain */
2375 SCVAL(vwv+0, 0, 0xff);
2376 SCVAL(vwv+0, 1, 0xff);
2382 * The NBT and SMB header
2384 this_iov[0] = state->smb1.iov[0];
2385 this_iov[1] = state->smb1.iov[1];
2389 * This one is a bit subtle. We have to add
2390 * chain_padding bytes between the requests, and we
2391 * have to also include the wct field of the
2392 * subsequent requests. We use the subsequent header
2393 * for the padding, it contains the wct field in its
2396 this_iov[0].iov_len = chain_padding+1;
2397 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2398 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2399 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2404 * copy the words and bytes
2406 memcpy(this_iov, state->smb1.iov+2,
2407 sizeof(struct iovec) * (state->smb1.iov_count-2));
2408 this_iov += state->smb1.iov_count - 2;
2409 chain_padding = next_padding;
2412 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2413 if (nbt_len > first_state->conn->smb1.max_xmit) {
2415 TALLOC_FREE(first_state->smb1.chained_requests);
2416 return NT_STATUS_INVALID_PARAMETER_MIX;
2419 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2420 if (!NT_STATUS_IS_OK(status)) {
2422 TALLOC_FREE(first_state->smb1.chained_requests);
2426 return NT_STATUS_OK;
2429 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2431 return ((tevent_queue_length(conn->outgoing) != 0)
2432 || (talloc_array_length(conn->pending) != 0));
2435 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2437 return conn->smb2.server.capabilities;
2440 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2442 return conn->smb2.server.security_mode;
2445 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2447 return conn->smb2.server.max_trans_size;
2450 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2452 return conn->smb2.server.max_read_size;
2455 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2457 return conn->smb2.server.max_write_size;
2460 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2461 uint16_t max_credits)
2463 conn->smb2.max_credits = max_credits;
2466 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2468 static bool smb2cli_req_cancel(struct tevent_req *req)
2470 struct smbXcli_req_state *state =
2471 tevent_req_data(req,
2472 struct smbXcli_req_state);
2473 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2474 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2475 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2476 struct smbXcli_tcon *tcon = state->tcon;
2477 struct smbXcli_session *session = state->session;
2478 uint8_t *fixed = state->smb2.pad;
2479 uint16_t fixed_len = 4;
2480 struct tevent_req *subreq;
2481 struct smbXcli_req_state *substate;
2484 SSVAL(fixed, 0, 0x04);
2487 subreq = smb2cli_req_create(state, state->ev,
2495 if (subreq == NULL) {
2498 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2501 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2502 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2504 flags &= SMB2_HDR_FLAG_ASYNC;
2506 if (flags & SMB2_HDR_FLAG_ASYNC) {
2510 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2511 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2512 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2514 status = smb2cli_req_compound_submit(&subreq, 1);
2515 if (!NT_STATUS_IS_OK(status)) {
2516 TALLOC_FREE(subreq);
2520 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2525 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2527 /* we do not care about the result */
2528 TALLOC_FREE(subreq);
2531 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2532 struct tevent_context *ev,
2533 struct smbXcli_conn *conn,
2535 uint32_t additional_flags,
2536 uint32_t clear_flags,
2537 uint32_t timeout_msec,
2538 struct smbXcli_tcon *tcon,
2539 struct smbXcli_session *session,
2540 const uint8_t *fixed,
2545 struct tevent_req *req;
2546 struct smbXcli_req_state *state;
2550 bool use_channel_sequence = false;
2551 uint16_t channel_sequence = 0;
2553 req = tevent_req_create(mem_ctx, &state,
2554 struct smbXcli_req_state);
2561 state->session = session;
2564 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2565 use_channel_sequence = true;
2566 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2567 use_channel_sequence = true;
2571 uid = session->smb2->session_id;
2573 if (use_channel_sequence) {
2574 channel_sequence = session->smb2->channel_sequence;
2577 state->smb2.should_sign = session->smb2->should_sign;
2578 state->smb2.should_encrypt = session->smb2->should_encrypt;
2580 if (cmd == SMB2_OP_SESSSETUP &&
2581 session->smb2->signing_key.length != 0) {
2582 state->smb2.should_sign = true;
2585 if (cmd == SMB2_OP_SESSSETUP &&
2586 session->smb2_channel.signing_key.length == 0) {
2587 state->smb2.should_encrypt = false;
2592 tid = tcon->smb2.tcon_id;
2594 if (tcon->smb2.should_encrypt) {
2595 state->smb2.should_encrypt = true;
2599 if (state->smb2.should_encrypt) {
2600 state->smb2.should_sign = false;
2603 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2604 if (state->smb2.recv_iov == NULL) {
2609 flags |= additional_flags;
2610 flags &= ~clear_flags;
2612 state->smb2.fixed = fixed;
2613 state->smb2.fixed_len = fixed_len;
2614 state->smb2.dyn = dyn;
2615 state->smb2.dyn_len = dyn_len;
2617 if (state->smb2.should_encrypt) {
2618 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2619 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2622 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2623 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2624 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2625 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2626 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2627 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2628 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2629 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2632 case SMB2_OP_CANCEL:
2633 state->one_way = true;
2637 * If this is a dummy request, it will have
2638 * UINT64_MAX as message id.
2639 * If we send on break acknowledgement,
2640 * this gets overwritten later.
2642 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2646 if (timeout_msec > 0) {
2647 struct timeval endtime;
2649 endtime = timeval_current_ofs_msec(timeout_msec);
2650 if (!tevent_req_set_endtime(req, ev, endtime)) {
2658 void smb2cli_req_set_notify_async(struct tevent_req *req)
2660 struct smbXcli_req_state *state =
2661 tevent_req_data(req,
2662 struct smbXcli_req_state);
2664 state->smb2.notify_async = true;
2667 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2668 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2669 TALLOC_CTX *tmp_mem,
2672 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2675 struct smbXcli_req_state *state;
2676 struct tevent_req *subreq;
2678 int i, num_iov, nbt_len;
2680 const DATA_BLOB *encryption_key = NULL;
2681 uint64_t encryption_session_id = 0;
2684 * 1 for the nbt length, optional TRANSFORM
2685 * per request: HDR, fixed, dyn, padding
2686 * -1 because the last one does not need padding
2689 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2691 return NT_STATUS_NO_MEMORY;
2698 * the session of the first request that requires encryption
2699 * specifies the encryption key.
2701 for (i=0; i<num_reqs; i++) {
2702 if (!tevent_req_is_in_progress(reqs[i])) {
2703 return NT_STATUS_INTERNAL_ERROR;
2706 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2708 if (!smbXcli_conn_is_connected(state->conn)) {
2709 return NT_STATUS_CONNECTION_DISCONNECTED;
2712 if ((state->conn->protocol != PROTOCOL_NONE) &&
2713 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2714 return NT_STATUS_REVISION_MISMATCH;
2717 if (state->session == NULL) {
2721 if (!state->smb2.should_encrypt) {
2725 encryption_key = &state->session->smb2->encryption_key;
2726 if (encryption_key->length == 0) {
2727 return NT_STATUS_INVALID_PARAMETER_MIX;
2730 encryption_session_id = state->session->smb2->session_id;
2733 iov[num_iov].iov_base = state->smb2.transform;
2734 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2737 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2738 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2739 state->session->smb2->nonce_low);
2740 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2741 state->session->smb2->nonce_high);
2742 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2743 encryption_session_id);
2745 state->session->smb2->nonce_low += 1;
2746 if (state->session->smb2->nonce_low == 0) {
2747 state->session->smb2->nonce_high += 1;
2748 state->session->smb2->nonce_low += 1;
2751 nbt_len += SMB2_TF_HDR_SIZE;
2755 for (i=0; i<num_reqs; i++) {
2764 const DATA_BLOB *signing_key = NULL;
2766 if (!tevent_req_is_in_progress(reqs[i])) {
2767 return NT_STATUS_INTERNAL_ERROR;
2770 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2772 if (!smbXcli_conn_is_connected(state->conn)) {
2773 return NT_STATUS_CONNECTION_DISCONNECTED;
2776 if ((state->conn->protocol != PROTOCOL_NONE) &&
2777 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2778 return NT_STATUS_REVISION_MISMATCH;
2781 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2782 if (opcode == SMB2_OP_CANCEL) {
2786 avail = UINT64_MAX - state->conn->smb2.mid;
2788 return NT_STATUS_CONNECTION_ABORTED;
2791 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2792 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2797 charge = MAX(state->smb2.credit_charge, charge);
2799 avail = MIN(avail, state->conn->smb2.cur_credits);
2800 if (avail < charge) {
2801 return NT_STATUS_INTERNAL_ERROR;
2805 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2806 credits = state->conn->smb2.max_credits -
2807 state->conn->smb2.cur_credits;
2809 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2813 mid = state->conn->smb2.mid;
2814 state->conn->smb2.mid += charge;
2815 state->conn->smb2.cur_credits -= charge;
2817 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2818 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2820 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2821 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2824 if (state->session && encryption_key == NULL) {
2826 * We prefer the channel signing key if it is
2829 if (state->smb2.should_sign) {
2830 signing_key = &state->session->smb2_channel.signing_key;
2834 * If it is a channel binding, we already have the main
2835 * signing key and try that one.
2837 if (signing_key && signing_key->length == 0) {
2838 signing_key = &state->session->smb2->signing_key;
2842 * If we do not have any session key yet, we skip the
2843 * signing of SMB2_OP_SESSSETUP requests.
2845 if (signing_key && signing_key->length == 0) {
2851 iov[num_iov].iov_base = state->smb2.hdr;
2852 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2855 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2856 iov[num_iov].iov_len = state->smb2.fixed_len;
2859 if (state->smb2.dyn != NULL) {
2860 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2861 iov[num_iov].iov_len = state->smb2.dyn_len;
2865 reqlen = sizeof(state->smb2.hdr);
2866 reqlen += state->smb2.fixed_len;
2867 reqlen += state->smb2.dyn_len;
2869 if (i < num_reqs-1) {
2870 if ((reqlen % 8) > 0) {
2871 uint8_t pad = 8 - (reqlen % 8);
2872 iov[num_iov].iov_base = state->smb2.pad;
2873 iov[num_iov].iov_len = pad;
2877 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2880 state->smb2.encryption_session_id = encryption_session_id;
2882 if (signing_key != NULL) {
2885 status = smb2_signing_sign_pdu(*signing_key,
2886 state->session->conn->protocol,
2887 &iov[hdr_iov], num_iov - hdr_iov);
2888 if (!NT_STATUS_IS_OK(status)) {
2895 ret = smbXcli_req_set_pending(reqs[i]);
2897 return NT_STATUS_NO_MEMORY;
2901 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2902 _smb_setlen_tcp(state->length_hdr, nbt_len);
2903 iov[0].iov_base = state->length_hdr;
2904 iov[0].iov_len = sizeof(state->length_hdr);
2906 if (encryption_key != NULL) {
2908 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
2912 buf = talloc_array(iov, uint8_t, buflen);
2914 return NT_STATUS_NO_MEMORY;
2918 * We copy the buffers before encrypting them,
2919 * this is at least currently needed for the
2920 * to keep state->smb2.hdr.
2922 * Also the callers may expect there buffers
2925 for (vi = tf_iov + 1; vi < num_iov; vi++) {
2926 struct iovec *v = &iov[vi];
2927 const uint8_t *o = (const uint8_t *)v->iov_base;
2929 memcpy(buf, o, v->iov_len);
2930 v->iov_base = (void *)buf;
2934 status = smb2_signing_encrypt_pdu(*encryption_key,
2935 state->conn->protocol,
2936 &iov[tf_iov], num_iov - tf_iov);
2937 if (!NT_STATUS_IS_OK(status)) {
2942 if (state->conn->dispatch_incoming == NULL) {
2943 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2946 subreq = writev_send(state, state->ev, state->conn->outgoing,
2947 state->conn->write_fd, false, iov, num_iov);
2948 if (subreq == NULL) {
2949 return NT_STATUS_NO_MEMORY;
2951 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2952 return NT_STATUS_OK;
2955 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2957 struct smbXcli_req_state *state =
2958 tevent_req_data(req,
2959 struct smbXcli_req_state);
2961 state->smb2.credit_charge = charge;
2964 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2965 struct tevent_context *ev,
2966 struct smbXcli_conn *conn,
2968 uint32_t additional_flags,
2969 uint32_t clear_flags,
2970 uint32_t timeout_msec,
2971 struct smbXcli_tcon *tcon,
2972 struct smbXcli_session *session,
2973 const uint8_t *fixed,
2978 struct tevent_req *req;
2981 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2982 additional_flags, clear_flags,
2985 fixed, fixed_len, dyn, dyn_len);
2989 if (!tevent_req_is_in_progress(req)) {
2990 return tevent_req_post(req, ev);
2992 status = smb2cli_req_compound_submit(&req, 1);
2993 if (tevent_req_nterror(req, status)) {
2994 return tevent_req_post(req, ev);
2999 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3001 struct tevent_req *req =
3002 tevent_req_callback_data(subreq,
3004 struct smbXcli_req_state *state =
3005 tevent_req_data(req,
3006 struct smbXcli_req_state);
3010 nwritten = writev_recv(subreq, &err);
3011 TALLOC_FREE(subreq);
3012 if (nwritten == -1) {
3013 /* here, we need to notify all pending requests */
3014 NTSTATUS status = map_nt_error_from_unix_common(err);
3015 smbXcli_conn_disconnect(state->conn, status);
3020 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3023 TALLOC_CTX *mem_ctx,
3024 struct iovec **piov, int *pnum_iov)
3029 uint8_t *first_hdr = buf;
3030 size_t verified_buflen = 0;
3034 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3036 return NT_STATUS_NO_MEMORY;
3039 while (taken < buflen) {
3040 size_t len = buflen - taken;
3041 uint8_t *hdr = first_hdr + taken;
3044 size_t next_command_ofs;
3046 struct iovec *iov_tmp;
3048 if (verified_buflen > taken) {
3049 len = verified_buflen - taken;
3056 DEBUG(10, ("%d bytes left, expected at least %d\n",
3060 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3061 struct smbXcli_session *s;
3063 struct iovec tf_iov[2];
3067 if (len < SMB2_TF_HDR_SIZE) {
3068 DEBUG(10, ("%d bytes left, expected at least %d\n",
3069 (int)len, SMB2_TF_HDR_SIZE));
3073 tf_len = SMB2_TF_HDR_SIZE;
3076 hdr = first_hdr + taken;
3077 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3078 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3080 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3081 DEBUG(10, ("%d bytes left, expected at least %d\n",
3083 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3088 for (; s; s = s->next) {
3089 if (s->smb2->session_id != uid) {
3096 DEBUG(10, ("unknown session_id %llu\n",
3097 (unsigned long long)uid));
3101 tf_iov[0].iov_base = (void *)tf;
3102 tf_iov[0].iov_len = tf_len;
3103 tf_iov[1].iov_base = (void *)hdr;
3104 tf_iov[1].iov_len = enc_len;
3106 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3109 if (!NT_STATUS_IS_OK(status)) {
3114 verified_buflen = taken + enc_len;
3119 * We need the header plus the body length field
3122 if (len < SMB2_HDR_BODY + 2) {
3123 DEBUG(10, ("%d bytes left, expected at least %d\n",
3124 (int)len, SMB2_HDR_BODY));
3127 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3128 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3132 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3133 DEBUG(10, ("Got HDR len %d, expected %d\n",
3134 SVAL(hdr, 4), SMB2_HDR_BODY));
3139 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3140 body_size = SVAL(hdr, SMB2_HDR_BODY);
3142 if (next_command_ofs != 0) {
3143 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3146 if (next_command_ofs > full_size) {
3149 full_size = next_command_ofs;
3151 if (body_size < 2) {
3154 body_size &= 0xfffe;
3156 if (body_size > (full_size - SMB2_HDR_BODY)) {
3160 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3162 if (iov_tmp == NULL) {
3164 return NT_STATUS_NO_MEMORY;
3167 cur = &iov[num_iov];
3170 cur[0].iov_base = tf;
3171 cur[0].iov_len = tf_len;
3172 cur[1].iov_base = hdr;
3173 cur[1].iov_len = SMB2_HDR_BODY;
3174 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3175 cur[2].iov_len = body_size;
3176 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3177 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3183 *pnum_iov = num_iov;
3184 return NT_STATUS_OK;
3188 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3191 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3194 size_t num_pending = talloc_array_length(conn->pending);
3197 for (i=0; i<num_pending; i++) {
3198 struct tevent_req *req = conn->pending[i];
3199 struct smbXcli_req_state *state =
3200 tevent_req_data(req,
3201 struct smbXcli_req_state);
3203 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3210 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3211 TALLOC_CTX *tmp_mem,
3214 struct tevent_req *req;
3215 struct smbXcli_req_state *state = NULL;
3220 struct smbXcli_session *last_session = NULL;
3221 size_t inbuf_len = smb_len_tcp(inbuf);
3223 status = smb2cli_inbuf_parse_compound(conn,
3224 inbuf + NBT_HDR_SIZE,
3228 if (!NT_STATUS_IS_OK(status)) {
3232 for (i=0; i<num_iov; i+=4) {
3233 uint8_t *inbuf_ref = NULL;
3234 struct iovec *cur = &iov[i];
3235 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3236 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3237 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3238 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3239 uint16_t req_opcode;
3241 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3242 uint32_t new_credits;
3243 struct smbXcli_session *session = NULL;
3244 const DATA_BLOB *signing_key = NULL;
3245 bool was_encrypted = false;
3247 new_credits = conn->smb2.cur_credits;
3248 new_credits += credits;
3249 if (new_credits > UINT16_MAX) {
3250 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3252 conn->smb2.cur_credits += credits;
3254 req = smb2cli_conn_find_pending(conn, mid);
3256 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3258 state = tevent_req_data(req, struct smbXcli_req_state);
3260 state->smb2.got_async = false;
3262 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3263 if (opcode != req_opcode) {
3264 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3266 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3268 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3269 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3272 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3273 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3274 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3275 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3278 * async interim responses are not signed,
3279 * even if the SMB2_HDR_FLAG_SIGNED flag
3282 req_flags |= SMB2_HDR_FLAG_ASYNC;
3283 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3284 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3286 if (state->smb2.notify_async) {
3287 state->smb2.got_async = true;
3288 tevent_req_defer_callback(req, state->ev);
3289 tevent_req_notify_callback(req);
3294 session = state->session;
3295 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3296 session = last_session;
3298 last_session = session;
3300 if (state->smb2.should_sign) {
3301 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3302 return NT_STATUS_ACCESS_DENIED;
3306 if (flags & SMB2_HDR_FLAG_SIGNED) {
3307 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3309 if (session == NULL) {
3310 struct smbXcli_session *s;
3312 s = state->conn->sessions;
3313 for (; s; s = s->next) {
3314 if (s->smb2->session_id != uid) {
3323 if (session == NULL) {
3324 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3327 last_session = session;
3328 signing_key = &session->smb2_channel.signing_key;
3331 if (opcode == SMB2_OP_SESSSETUP) {
3333 * We prefer the channel signing key, if it is
3336 * If we do not have a channel signing key yet,
3337 * we try the main signing key, if it is not
3338 * the final response.
3340 if (signing_key && signing_key->length == 0 &&
3341 !NT_STATUS_IS_OK(status)) {
3342 signing_key = &session->smb2->signing_key;
3345 if (signing_key && signing_key->length == 0) {
3347 * If we do not have a session key to
3348 * verify the signature, we defer the
3349 * signing check to the caller.
3351 * The caller gets NT_STATUS_OK, it
3353 * smb2cli_session_set_session_key()
3355 * smb2cli_session_set_channel_key()
3356 * which will check the signature
3357 * with the channel signing key.
3363 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3364 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3365 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3368 * If the response was encrypted in a SMB2_TRANSFORM
3369 * pdu, which belongs to the correct session,
3370 * we do not need to do signing checks
3372 * It could be the session the response belongs to
3373 * or the session that was used to encrypt the
3374 * SMB2_TRANSFORM request.
3376 if ((session && session->smb2->session_id == uid) ||
3377 (state->smb2.encryption_session_id == uid)) {
3379 was_encrypted = true;
3383 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3385 * if the server returns NT_STATUS_USER_SESSION_DELETED
3386 * the response is not signed and we should
3387 * propagate the NT_STATUS_USER_SESSION_DELETED
3388 * status to the caller.
3390 state->smb2.signing_skipped = true;
3394 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3396 * if the server returns
3397 * NT_STATUS_INVALID_PARAMETER
3398 * the response might not be encrypted.
3400 if (state->smb2.should_encrypt && !was_encrypted) {
3401 state->smb2.signing_skipped = true;
3406 if (state->smb2.should_encrypt && !was_encrypted) {
3407 if (!state->smb2.signing_skipped) {
3408 return NT_STATUS_ACCESS_DENIED;
3412 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3413 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3414 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3416 * if the server returns
3417 * NT_STATUS_NETWORK_NAME_DELETED
3418 * NT_STATUS_FILE_CLOSED
3419 * NT_STATUS_INVALID_PARAMETER
3420 * the response might not be signed
3421 * as this happens before the signing checks.
3423 * If server echos the signature (or all zeros)
3424 * we should report the status from the server
3430 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3431 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3434 state->smb2.signing_skipped = true;
3440 static const uint8_t zeros[16];
3442 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3446 state->smb2.signing_skipped = true;
3453 status = smb2_signing_check_pdu(*signing_key,
3454 state->conn->protocol,
3456 if (!NT_STATUS_IS_OK(status)) {
3458 * If the signing check fails, we disconnect
3465 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3466 (session != NULL) && session->disconnect_expired)
3469 * this should be a short term hack
3470 * until the upper layers have implemented
3471 * re-authentication.
3476 smbXcli_req_unset_pending(req);
3479 * There might be more than one response
3480 * we need to defer the notifications
3482 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3487 tevent_req_defer_callback(req, state->ev);
3491 * Note: here we use talloc_reference() in a way
3492 * that does not expose it to the caller.
3494 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3495 if (tevent_req_nomem(inbuf_ref, req)) {
3499 /* copy the related buffers */
3500 state->smb2.recv_iov[0] = cur[1];
3501 state->smb2.recv_iov[1] = cur[2];
3502 state->smb2.recv_iov[2] = cur[3];
3504 tevent_req_done(req);
3508 return NT_STATUS_RETRY;
3511 return NT_STATUS_OK;
3514 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3515 struct iovec **piov,
3516 const struct smb2cli_req_expected_response *expected,
3517 size_t num_expected)
3519 struct smbXcli_req_state *state =
3520 tevent_req_data(req,
3521 struct smbXcli_req_state);
3524 bool found_status = false;
3525 bool found_size = false;
3532 if (state->smb2.got_async) {
3533 return STATUS_PENDING;
3536 if (tevent_req_is_nterror(req, &status)) {
3537 for (i=0; i < num_expected; i++) {
3538 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3539 found_status = true;
3545 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3551 if (num_expected == 0) {
3552 found_status = true;
3556 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3557 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3559 for (i=0; i < num_expected; i++) {
3560 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3564 found_status = true;
3565 if (expected[i].body_size == 0) {
3570 if (expected[i].body_size == body_size) {
3576 if (!found_status) {
3580 if (state->smb2.signing_skipped) {
3581 if (num_expected > 0) {
3582 return NT_STATUS_ACCESS_DENIED;
3584 if (!NT_STATUS_IS_ERR(status)) {
3585 return NT_STATUS_ACCESS_DENIED;
3590 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3594 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3600 static const struct {
3601 enum protocol_types proto;
3602 const char *smb1_name;
3603 } smb1cli_prots[] = {
3604 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3605 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3606 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3607 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3608 {PROTOCOL_LANMAN2, "LM1.2X002"},
3609 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3610 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3611 {PROTOCOL_LANMAN2, "Samba"},
3612 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3613 {PROTOCOL_NT1, "NT LM 0.12"},
3614 {PROTOCOL_SMB2_02, "SMB 2.002"},
3615 {PROTOCOL_SMB2_10, "SMB 2.???"},
3618 static const struct {
3619 enum protocol_types proto;
3620 uint16_t smb2_dialect;
3621 } smb2cli_prots[] = {
3622 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3623 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3624 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3625 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3626 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3629 struct smbXcli_negprot_state {
3630 struct smbXcli_conn *conn;
3631 struct tevent_context *ev;
3632 uint32_t timeout_msec;
3633 enum protocol_types min_protocol;
3634 enum protocol_types max_protocol;
3638 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3642 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3643 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3644 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3645 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3646 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3647 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3651 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3652 struct tevent_context *ev,
3653 struct smbXcli_conn *conn,
3654 uint32_t timeout_msec,
3655 enum protocol_types min_protocol,
3656 enum protocol_types max_protocol)
3658 struct tevent_req *req, *subreq;
3659 struct smbXcli_negprot_state *state;
3661 req = tevent_req_create(mem_ctx, &state,
3662 struct smbXcli_negprot_state);
3668 state->timeout_msec = timeout_msec;
3669 state->min_protocol = min_protocol;
3670 state->max_protocol = max_protocol;
3672 if (min_protocol == PROTOCOL_NONE) {
3673 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3674 return tevent_req_post(req, ev);
3677 if (max_protocol == PROTOCOL_NONE) {
3678 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3679 return tevent_req_post(req, ev);
3682 if (min_protocol > max_protocol) {
3683 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3684 return tevent_req_post(req, ev);
3687 if ((min_protocol < PROTOCOL_SMB2_02) &&
3688 (max_protocol < PROTOCOL_SMB2_02)) {
3692 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3694 subreq = smbXcli_negprot_smb1_subreq(state);
3695 if (tevent_req_nomem(subreq, req)) {
3696 return tevent_req_post(req, ev);
3698 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3702 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3703 (max_protocol >= PROTOCOL_SMB2_02)) {
3707 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3709 subreq = smbXcli_negprot_smb2_subreq(state);
3710 if (tevent_req_nomem(subreq, req)) {
3711 return tevent_req_post(req, ev);
3713 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3718 * We send an SMB1 negprot with the SMB2 dialects
3719 * and expect a SMB1 or a SMB2 response.
3721 * smbXcli_negprot_dispatch_incoming() will fix the
3722 * callback to match protocol of the response.
3724 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3726 subreq = smbXcli_negprot_smb1_subreq(state);
3727 if (tevent_req_nomem(subreq, req)) {
3728 return tevent_req_post(req, ev);
3730 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3734 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3736 struct tevent_req *req =
3737 tevent_req_callback_data(subreq,
3742 * we just want the low level error
3744 status = tevent_req_simple_recv_ntstatus(subreq);
3745 TALLOC_FREE(subreq);
3746 if (tevent_req_nterror(req, status)) {
3750 /* this should never happen */
3751 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3754 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3757 DATA_BLOB bytes = data_blob_null;
3761 /* setup the protocol strings */
3762 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3766 if (smb1cli_prots[i].proto < state->min_protocol) {
3770 if (smb1cli_prots[i].proto > state->max_protocol) {
3774 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3780 * We now it is already ascii and
3781 * we want NULL termination.
3783 ok = data_blob_append(state, &bytes,
3784 smb1cli_prots[i].smb1_name,
3785 strlen(smb1cli_prots[i].smb1_name)+1);
3791 smb1cli_req_flags(state->max_protocol,
3792 state->conn->smb1.client.capabilities,
3797 return smb1cli_req_send(state, state->ev, state->conn,
3801 state->timeout_msec,
3802 0xFFFE, 0, NULL, /* pid, tid, session */
3803 0, NULL, /* wct, vwv */
3804 bytes.length, bytes.data);
3807 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3809 struct tevent_req *req =
3810 tevent_req_callback_data(subreq,
3812 struct smbXcli_negprot_state *state =
3813 tevent_req_data(req,
3814 struct smbXcli_negprot_state);
3815 struct smbXcli_conn *conn = state->conn;
3816 struct iovec *recv_iov = NULL;
3825 size_t num_prots = 0;
3827 uint32_t client_capabilities = conn->smb1.client.capabilities;
3828 uint32_t both_capabilities;
3829 uint32_t server_capabilities = 0;
3830 uint32_t capabilities;
3831 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3832 uint32_t server_max_xmit = 0;
3834 uint32_t server_max_mux = 0;
3835 uint16_t server_security_mode = 0;
3836 uint32_t server_session_key = 0;
3837 bool server_readbraw = false;
3838 bool server_writebraw = false;
3839 bool server_lockread = false;
3840 bool server_writeunlock = false;
3841 struct GUID server_guid = GUID_zero();
3842 DATA_BLOB server_gss_blob = data_blob_null;
3843 uint8_t server_challenge[8];
3844 char *server_workgroup = NULL;
3845 char *server_name = NULL;
3846 int server_time_zone = 0;
3847 NTTIME server_system_time = 0;
3848 static const struct smb1cli_req_expected_response expected[] = {
3850 .status = NT_STATUS_OK,
3851 .wct = 0x11, /* NT1 */
3854 .status = NT_STATUS_OK,
3855 .wct = 0x0D, /* LM */
3858 .status = NT_STATUS_OK,
3859 .wct = 0x01, /* CORE */
3863 ZERO_STRUCT(server_challenge);
3865 status = smb1cli_req_recv(subreq, state,
3870 NULL, /* pvwv_offset */
3873 NULL, /* pbytes_offset */
3875 expected, ARRAY_SIZE(expected));
3876 TALLOC_FREE(subreq);
3877 if (tevent_req_nterror(req, status)) {
3881 flags = CVAL(inhdr, HDR_FLG);
3883 protnum = SVAL(vwv, 0);
3885 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3886 if (smb1cli_prots[i].proto < state->min_protocol) {
3890 if (smb1cli_prots[i].proto > state->max_protocol) {
3894 if (protnum != num_prots) {
3899 conn->protocol = smb1cli_prots[i].proto;
3903 if (conn->protocol == PROTOCOL_NONE) {
3904 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3908 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3909 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3910 "and the selected protocol level doesn't support it.\n"));
3911 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3915 if (flags & FLAG_SUPPORT_LOCKREAD) {
3916 server_lockread = true;
3917 server_writeunlock = true;
3920 if (conn->protocol >= PROTOCOL_NT1) {
3921 const char *client_signing = NULL;
3922 bool server_mandatory = false;
3923 bool server_allowed = false;
3924 const char *server_signing = NULL;
3929 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3934 server_security_mode = CVAL(vwv + 1, 0);
3935 server_max_mux = SVAL(vwv + 1, 1);
3936 server_max_xmit = IVAL(vwv + 3, 1);
3937 server_session_key = IVAL(vwv + 7, 1);
3938 server_time_zone = SVALS(vwv + 15, 1);
3939 server_time_zone *= 60;
3940 /* this time arrives in real GMT */
3941 server_system_time = BVAL(vwv + 11, 1);
3942 server_capabilities = IVAL(vwv + 9, 1);
3944 key_len = CVAL(vwv + 16, 1);
3946 if (server_capabilities & CAP_RAW_MODE) {
3947 server_readbraw = true;
3948 server_writebraw = true;
3950 if (server_capabilities & CAP_LOCK_AND_READ) {
3951 server_lockread = true;
3954 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3955 DATA_BLOB blob1, blob2;
3957 if (num_bytes < 16) {
3958 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3962 blob1 = data_blob_const(bytes, 16);
3963 status = GUID_from_data_blob(&blob1, &server_guid);
3964 if (tevent_req_nterror(req, status)) {
3968 blob1 = data_blob_const(bytes+16, num_bytes-16);
3969 blob2 = data_blob_dup_talloc(state, blob1);
3970 if (blob1.length > 0 &&
3971 tevent_req_nomem(blob2.data, req)) {
3974 server_gss_blob = blob2;
3976 DATA_BLOB blob1, blob2;
3978 if (num_bytes < key_len) {
3979 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3983 if (key_len != 0 && key_len != 8) {
3984 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3989 memcpy(server_challenge, bytes, 8);
3992 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3993 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3994 if (blob1.length > 0) {
3997 len = utf16_len_n(blob1.data,
4001 ok = convert_string_talloc(state,
4009 status = map_nt_error_from_unix_common(errno);
4010 tevent_req_nterror(req, status);
4015 blob2.data += blob1.length;
4016 blob2.length -= blob1.length;
4017 if (blob2.length > 0) {
4020 len = utf16_len_n(blob1.data,
4024 ok = convert_string_talloc(state,
4032 status = map_nt_error_from_unix_common(errno);
4033 tevent_req_nterror(req, status);
4039 client_signing = "disabled";
4040 if (conn->allow_signing) {
4041 client_signing = "allowed";
4043 if (conn->mandatory_signing) {
4044 client_signing = "required";
4047 server_signing = "not supported";
4048 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4049 server_signing = "supported";
4050 server_allowed = true;
4051 } else if (conn->mandatory_signing) {
4053 * We have mandatory signing as client
4054 * lets assume the server will look at our
4055 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4056 * flag in the session setup
4058 server_signing = "not announced";
4059 server_allowed = true;
4061 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4062 server_signing = "required";
4063 server_mandatory = true;
4066 ok = smb_signing_set_negotiated(conn->smb1.signing,
4070 DEBUG(1,("cli_negprot: SMB signing is required, "
4071 "but client[%s] and server[%s] mismatch\n",
4072 client_signing, server_signing));
4073 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4077 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4083 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4087 server_security_mode = SVAL(vwv + 1, 0);
4088 server_max_xmit = SVAL(vwv + 2, 0);
4089 server_max_mux = SVAL(vwv + 3, 0);
4090 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4091 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4092 server_session_key = IVAL(vwv + 6, 0);
4093 server_time_zone = SVALS(vwv + 10, 0);
4094 server_time_zone *= 60;
4095 /* this time is converted to GMT by make_unix_date */
4096 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4097 unix_to_nt_time(&server_system_time, t);
4098 key_len = SVAL(vwv + 11, 0);
4100 if (num_bytes < key_len) {
4101 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4105 if (key_len != 0 && key_len != 8) {
4106 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4111 memcpy(server_challenge, bytes, 8);
4114 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4115 if (blob1.length > 0) {
4119 len = utf16_len_n(blob1.data,
4123 ok = convert_string_talloc(state,
4131 status = map_nt_error_from_unix_common(errno);
4132 tevent_req_nterror(req, status);
4138 /* the old core protocol */
4139 server_time_zone = get_time_zone(time(NULL));
4140 server_max_xmit = 1024;
4144 if (server_max_xmit < 1024) {
4145 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4149 if (server_max_mux < 1) {
4150 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4155 * Now calculate the negotiated capabilities
4156 * based on the mask for:
4157 * - client only flags
4158 * - flags used in both directions
4159 * - server only flags
4161 both_capabilities = client_capabilities & server_capabilities;
4162 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4163 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4164 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4166 max_xmit = MIN(client_max_xmit, server_max_xmit);
4168 conn->smb1.server.capabilities = server_capabilities;
4169 conn->smb1.capabilities = capabilities;
4171 conn->smb1.server.max_xmit = server_max_xmit;
4172 conn->smb1.max_xmit = max_xmit;
4174 conn->smb1.server.max_mux = server_max_mux;
4176 conn->smb1.server.security_mode = server_security_mode;
4178 conn->smb1.server.readbraw = server_readbraw;
4179 conn->smb1.server.writebraw = server_writebraw;
4180 conn->smb1.server.lockread = server_lockread;
4181 conn->smb1.server.writeunlock = server_writeunlock;
4183 conn->smb1.server.session_key = server_session_key;
4185 talloc_steal(conn, server_gss_blob.data);
4186 conn->smb1.server.gss_blob = server_gss_blob;
4187 conn->smb1.server.guid = server_guid;
4188 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4189 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4190 conn->smb1.server.name = talloc_move(conn, &server_name);
4192 conn->smb1.server.time_zone = server_time_zone;
4193 conn->smb1.server.system_time = server_system_time;
4195 tevent_req_done(req);
4198 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4202 uint16_t dialect_count = 0;
4204 buf = state->smb2.dyn;
4205 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4206 if (smb2cli_prots[i].proto < state->min_protocol) {
4210 if (smb2cli_prots[i].proto > state->max_protocol) {
4214 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4218 buf = state->smb2.fixed;
4220 SSVAL(buf, 2, dialect_count);
4221 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4222 SSVAL(buf, 6, 0); /* Reserved */
4223 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4224 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4226 SIVAL(buf, 8, 0); /* Capabilities */
4228 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4232 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4234 if (!NT_STATUS_IS_OK(status)) {
4237 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4239 memset(buf+12, 0, 16); /* ClientGuid */
4241 SBVAL(buf, 28, 0); /* ClientStartTime */
4243 return smb2cli_req_send(state, state->ev,
4244 state->conn, SMB2_OP_NEGPROT,
4246 state->timeout_msec,
4247 NULL, NULL, /* tcon, session */
4248 state->smb2.fixed, sizeof(state->smb2.fixed),
4249 state->smb2.dyn, dialect_count*2);
4252 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4254 struct tevent_req *req =
4255 tevent_req_callback_data(subreq,
4257 struct smbXcli_negprot_state *state =
4258 tevent_req_data(req,
4259 struct smbXcli_negprot_state);
4260 struct smbXcli_conn *conn = state->conn;
4261 size_t security_offset, security_length;
4267 uint16_t dialect_revision;
4268 static const struct smb2cli_req_expected_response expected[] = {
4270 .status = NT_STATUS_OK,
4275 status = smb2cli_req_recv(subreq, state, &iov,
4276 expected, ARRAY_SIZE(expected));
4277 TALLOC_FREE(subreq);
4278 if (tevent_req_nterror(req, status)) {
4282 body = (uint8_t *)iov[1].iov_base;
4284 dialect_revision = SVAL(body, 4);
4286 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4287 if (smb2cli_prots[i].proto < state->min_protocol) {
4291 if (smb2cli_prots[i].proto > state->max_protocol) {
4295 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4299 conn->protocol = smb2cli_prots[i].proto;
4303 if (conn->protocol == PROTOCOL_NONE) {
4304 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4305 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4309 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4310 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4314 /* make sure we do not loop forever */
4315 state->min_protocol = PROTOCOL_SMB2_02;
4318 * send a SMB2 negprot, in order to negotiate
4321 subreq = smbXcli_negprot_smb2_subreq(state);
4322 if (tevent_req_nomem(subreq, req)) {
4325 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4329 conn->smb2.server.security_mode = SVAL(body, 2);
4331 blob = data_blob_const(body + 8, 16);
4332 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4333 if (tevent_req_nterror(req, status)) {
4337 conn->smb2.server.capabilities = IVAL(body, 24);
4338 conn->smb2.server.max_trans_size= IVAL(body, 28);
4339 conn->smb2.server.max_read_size = IVAL(body, 32);
4340 conn->smb2.server.max_write_size= IVAL(body, 36);
4341 conn->smb2.server.system_time = BVAL(body, 40);
4342 conn->smb2.server.start_time = BVAL(body, 48);
4344 security_offset = SVAL(body, 56);
4345 security_length = SVAL(body, 58);
4347 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4348 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4352 if (security_length > iov[2].iov_len) {
4353 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4357 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4360 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4364 tevent_req_done(req);
4367 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4368 TALLOC_CTX *tmp_mem,
4371 size_t num_pending = talloc_array_length(conn->pending);
4372 struct tevent_req *subreq;
4373 struct smbXcli_req_state *substate;
4374 struct tevent_req *req;
4375 uint32_t protocol_magic;
4376 size_t inbuf_len = smb_len_nbt(inbuf);
4378 if (num_pending != 1) {
4379 return NT_STATUS_INTERNAL_ERROR;
4382 if (inbuf_len < 4) {
4383 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4386 subreq = conn->pending[0];
4387 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4388 req = tevent_req_callback_data(subreq, struct tevent_req);
4390 protocol_magic = IVAL(inbuf, 4);
4392 switch (protocol_magic) {
4394 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4395 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4396 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4399 if (substate->smb2.recv_iov == NULL) {
4401 * For the SMB1 negprot we have move it.
4403 substate->smb2.recv_iov = substate->smb1.recv_iov;
4404 substate->smb1.recv_iov = NULL;
4408 * we got an SMB2 answer, which consumed sequence number 0
4409 * so we need to use 1 as the next one
4412 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4413 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4414 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4417 DEBUG(10, ("Got non-SMB PDU\n"));
4418 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4421 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4423 return tevent_req_simple_recv_ntstatus(req);
4426 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4427 uint32_t timeout_msec,
4428 enum protocol_types min_protocol,
4429 enum protocol_types max_protocol)
4431 TALLOC_CTX *frame = talloc_stackframe();
4432 struct tevent_context *ev;
4433 struct tevent_req *req;
4434 NTSTATUS status = NT_STATUS_NO_MEMORY;
4437 if (smbXcli_conn_has_async_calls(conn)) {
4439 * Can't use sync call while an async call is in flight
4441 status = NT_STATUS_INVALID_PARAMETER_MIX;
4444 ev = samba_tevent_context_init(frame);
4448 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4449 min_protocol, max_protocol);
4453 ok = tevent_req_poll(req, ev);
4455 status = map_nt_error_from_unix_common(errno);
4458 status = smbXcli_negprot_recv(req);
4464 static int smbXcli_session_destructor(struct smbXcli_session *session)
4466 if (session->conn == NULL) {
4470 DLIST_REMOVE(session->conn->sessions, session);
4474 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4475 struct smbXcli_conn *conn)
4477 struct smbXcli_session *session;
4479 session = talloc_zero(mem_ctx, struct smbXcli_session);
4480 if (session == NULL) {
4483 session->smb2 = talloc_zero(session, struct smb2cli_session);
4484 if (session->smb2 == NULL) {
4485 talloc_free(session);
4488 talloc_set_destructor(session, smbXcli_session_destructor);
4490 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4491 session->conn = conn;
4496 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
4497 struct smbXcli_session *src)
4499 struct smbXcli_session *session;
4501 session = talloc_zero(mem_ctx, struct smbXcli_session);
4502 if (session == NULL) {
4505 session->smb2 = talloc_zero(session, struct smb2cli_session);
4506 if (session->smb2 == NULL) {
4507 talloc_free(session);
4511 session->conn = src->conn;
4512 *session->smb2 = *src->smb2;
4513 session->smb2_channel = src->smb2_channel;
4514 session->disconnect_expired = src->disconnect_expired;
4516 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
4517 talloc_set_destructor(session, smbXcli_session_destructor);
4523 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4524 TALLOC_CTX *mem_ctx,
4527 const DATA_BLOB *application_key;
4529 *key = data_blob_null;
4531 if (session->conn == NULL) {
4532 return NT_STATUS_NO_USER_SESSION_KEY;
4535 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4536 application_key = &session->smb2->application_key;
4538 application_key = &session->smb1.application_key;
4541 if (application_key->length == 0) {
4542 return NT_STATUS_NO_USER_SESSION_KEY;
4545 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4546 if (key->data == NULL) {
4547 return NT_STATUS_NO_MEMORY;
4550 return NT_STATUS_OK;
4553 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
4555 session->disconnect_expired = true;
4558 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4560 return session->smb1.session_id;
4563 void smb1cli_session_set_id(struct smbXcli_session *session,
4564 uint16_t session_id)
4566 session->smb1.session_id = session_id;
4569 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4570 const DATA_BLOB _session_key)
4572 struct smbXcli_conn *conn = session->conn;
4573 uint8_t session_key[16];
4576 return NT_STATUS_INVALID_PARAMETER_MIX;
4579 if (session->smb1.application_key.length != 0) {
4581 * TODO: do not allow this...
4583 * return NT_STATUS_INVALID_PARAMETER_MIX;
4585 data_blob_clear_free(&session->smb1.application_key);
4586 session->smb1.protected_key = false;
4589 if (_session_key.length == 0) {
4590 return NT_STATUS_OK;
4593 ZERO_STRUCT(session_key);
4594 memcpy(session_key, _session_key.data,
4595 MIN(_session_key.length, sizeof(session_key)));
4597 session->smb1.application_key = data_blob_talloc(session,
4599 sizeof(session_key));
4600 ZERO_STRUCT(session_key);
4601 if (session->smb1.application_key.data == NULL) {
4602 return NT_STATUS_NO_MEMORY;
4605 session->smb1.protected_key = false;
4607 return NT_STATUS_OK;
4610 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4612 if (session->smb1.protected_key) {
4613 /* already protected */
4614 return NT_STATUS_OK;
4617 if (session->smb1.application_key.length != 16) {
4618 return NT_STATUS_INVALID_PARAMETER_MIX;
4621 smb_key_derivation(session->smb1.application_key.data,
4622 session->smb1.application_key.length,
4623 session->smb1.application_key.data);
4625 session->smb1.protected_key = true;
4627 return NT_STATUS_OK;
4630 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4632 struct smbXcli_conn *conn = session->conn;
4633 uint8_t security_mode = 0;
4636 return security_mode;
4639 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4640 if (conn->mandatory_signing) {
4641 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4644 return security_mode;
4647 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4649 return session->smb2->session_id;
4652 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4654 return session->smb2->session_flags;
4657 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4658 uint64_t session_id,
4659 uint16_t session_flags)
4661 session->smb2->session_id = session_id;
4662 session->smb2->session_flags = session_flags;
4665 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4667 session->smb2->channel_sequence += 1;
4670 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4671 const DATA_BLOB _session_key,
4672 const struct iovec *recv_iov)
4674 struct smbXcli_conn *conn = session->conn;
4675 uint16_t no_sign_flags;
4676 uint8_t session_key[16];
4680 return NT_STATUS_INVALID_PARAMETER_MIX;
4683 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4685 if (session->smb2->session_flags & no_sign_flags) {
4686 session->smb2->should_sign = false;
4687 return NT_STATUS_OK;
4690 if (session->smb2->signing_key.length != 0) {
4691 return NT_STATUS_INVALID_PARAMETER_MIX;
4694 ZERO_STRUCT(session_key);
4695 memcpy(session_key, _session_key.data,
4696 MIN(_session_key.length, sizeof(session_key)));
4698 session->smb2->signing_key = data_blob_talloc(session,
4700 sizeof(session_key));
4701 if (session->smb2->signing_key.data == NULL) {
4702 ZERO_STRUCT(session_key);
4703 return NT_STATUS_NO_MEMORY;
4706 if (conn->protocol >= PROTOCOL_SMB2_24) {
4707 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4708 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4710 smb2_key_derivation(session_key, sizeof(session_key),
4711 label.data, label.length,
4712 context.data, context.length,
4713 session->smb2->signing_key.data);
4716 session->smb2->encryption_key = data_blob_dup_talloc(session,
4717 session->smb2->signing_key);
4718 if (session->smb2->encryption_key.data == NULL) {
4719 ZERO_STRUCT(session_key);
4720 return NT_STATUS_NO_MEMORY;
4723 if (conn->protocol >= PROTOCOL_SMB2_24) {
4724 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4725 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4727 smb2_key_derivation(session_key, sizeof(session_key),
4728 label.data, label.length,
4729 context.data, context.length,
4730 session->smb2->encryption_key.data);
4733 session->smb2->decryption_key = data_blob_dup_talloc(session,
4734 session->smb2->signing_key);
4735 if (session->smb2->decryption_key.data == NULL) {
4736 ZERO_STRUCT(session_key);
4737 return NT_STATUS_NO_MEMORY;
4740 if (conn->protocol >= PROTOCOL_SMB2_24) {
4741 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4742 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4744 smb2_key_derivation(session_key, sizeof(session_key),
4745 label.data, label.length,
4746 context.data, context.length,
4747 session->smb2->decryption_key.data);
4750 session->smb2->application_key = data_blob_dup_talloc(session,
4751 session->smb2->signing_key);
4752 if (session->smb2->application_key.data == NULL) {
4753 ZERO_STRUCT(session_key);
4754 return NT_STATUS_NO_MEMORY;
4757 if (conn->protocol >= PROTOCOL_SMB2_24) {
4758 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4759 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4761 smb2_key_derivation(session_key, sizeof(session_key),
4762 label.data, label.length,
4763 context.data, context.length,
4764 session->smb2->application_key.data);
4766 ZERO_STRUCT(session_key);
4768 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4769 session->smb2->signing_key);
4770 if (session->smb2_channel.signing_key.data == NULL) {
4771 return NT_STATUS_NO_MEMORY;
4774 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4775 session->conn->protocol,
4777 if (!NT_STATUS_IS_OK(status)) {
4781 session->smb2->should_sign = false;
4782 session->smb2->should_encrypt = false;
4784 if (conn->desire_signing) {
4785 session->smb2->should_sign = true;
4788 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4789 session->smb2->should_sign = true;
4792 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4793 session->smb2->should_encrypt = true;
4796 if (conn->protocol < PROTOCOL_SMB2_24) {
4797 session->smb2->should_encrypt = false;
4800 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4801 session->smb2->should_encrypt = false;
4804 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4805 sizeof(session->smb2->nonce_high));
4806 session->smb2->nonce_low = 1;
4808 return NT_STATUS_OK;
4811 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4812 struct smbXcli_session *session1,
4813 struct smbXcli_conn *conn,
4814 struct smbXcli_session **_session2)
4816 struct smbXcli_session *session2;
4818 if (session1->smb2->signing_key.length == 0) {
4819 return NT_STATUS_INVALID_PARAMETER_MIX;
4823 return NT_STATUS_INVALID_PARAMETER_MIX;
4826 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4827 if (session2 == NULL) {
4828 return NT_STATUS_NO_MEMORY;
4830 session2->smb2 = talloc_reference(session2, session1->smb2);
4831 if (session2->smb2 == NULL) {
4832 talloc_free(session2);
4833 return NT_STATUS_NO_MEMORY;
4836 talloc_set_destructor(session2, smbXcli_session_destructor);
4837 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4838 session2->conn = conn;
4840 *_session2 = session2;
4841 return NT_STATUS_OK;
4844 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4845 const DATA_BLOB _channel_key,
4846 const struct iovec *recv_iov)
4848 struct smbXcli_conn *conn = session->conn;
4849 uint8_t channel_key[16];
4853 return NT_STATUS_INVALID_PARAMETER_MIX;
4856 if (session->smb2_channel.signing_key.length != 0) {
4857 return NT_STATUS_INVALID_PARAMETER_MIX;
4860 ZERO_STRUCT(channel_key);
4861 memcpy(channel_key, _channel_key.data,
4862 MIN(_channel_key.length, sizeof(channel_key)));
4864 session->smb2_channel.signing_key = data_blob_talloc(session,
4866 sizeof(channel_key));
4867 if (session->smb2_channel.signing_key.data == NULL) {
4868 ZERO_STRUCT(channel_key);
4869 return NT_STATUS_NO_MEMORY;
4872 if (conn->protocol >= PROTOCOL_SMB2_24) {
4873 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4874 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4876 smb2_key_derivation(channel_key, sizeof(channel_key),
4877 label.data, label.length,
4878 context.data, context.length,
4879 session->smb2_channel.signing_key.data);
4881 ZERO_STRUCT(channel_key);
4883 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4884 session->conn->protocol,
4886 if (!NT_STATUS_IS_OK(status)) {
4890 return NT_STATUS_OK;
4893 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4895 struct smbXcli_tcon *tcon;
4897 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4905 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4907 return tcon->smb1.tcon_id;
4910 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4912 tcon->smb1.tcon_id = tcon_id;
4915 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
4917 uint16_t optional_support,
4918 uint32_t maximal_access,
4919 uint32_t guest_maximal_access,
4920 const char *service,
4921 const char *fs_type)
4923 tcon->smb1.tcon_id = tcon_id;
4924 tcon->smb1.optional_support = optional_support;
4925 tcon->smb1.maximal_access = maximal_access;
4926 tcon->smb1.guest_maximal_access = guest_maximal_access;
4928 TALLOC_FREE(tcon->smb1.service);
4929 tcon->smb1.service = talloc_strdup(tcon, service);
4930 if (service != NULL && tcon->smb1.service == NULL) {
4934 TALLOC_FREE(tcon->smb1.fs_type);
4935 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
4936 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
4943 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
4945 return tcon->smb2.tcon_id;
4948 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
4950 return tcon->smb2.capabilities;
4953 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
4954 struct smbXcli_session *session,
4958 uint32_t capabilities,
4959 uint32_t maximal_access)
4961 tcon->smb2.tcon_id = tcon_id;
4962 tcon->smb2.type = type;
4963 tcon->smb2.flags = flags;
4964 tcon->smb2.capabilities = capabilities;
4965 tcon->smb2.maximal_access = maximal_access;
4967 tcon->smb2.should_encrypt = false;
4969 if (session == NULL) {
4973 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
4975 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
4976 tcon->smb2.should_encrypt = true;