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;
144 struct smbXcli_session {
145 struct smbXcli_session *prev, *next;
146 struct smbXcli_conn *conn;
152 struct smb2cli_session *smb2;
155 DATA_BLOB signing_key;
159 struct smbXcli_tcon {
162 uint16_t optional_support;
163 uint32_t maximal_access;
164 uint32_t guest_maximal_access;
173 uint32_t capabilities;
174 uint32_t maximal_access;
179 struct smbXcli_req_state {
180 struct tevent_context *ev;
181 struct smbXcli_conn *conn;
182 struct smbXcli_session *session; /* maybe NULL */
183 struct smbXcli_tcon *tcon; /* maybe NULL */
185 uint8_t length_hdr[4];
192 /* Space for the header including the wct */
193 uint8_t hdr[HDR_VWV];
196 * For normal requests, smb1cli_req_send chooses a mid.
197 * SecondaryV trans requests need to use the mid of the primary
198 * request, so we need a place to store it.
199 * Assume it is set if != 0.
204 uint8_t bytecount_buf[2];
206 #define MAX_SMB_IOV 10
207 /* length_hdr, hdr, words, byte_count, buffers */
208 struct iovec iov[1 + 3 + MAX_SMB_IOV];
213 struct tevent_req **chained_requests;
216 NTSTATUS recv_status;
217 /* always an array of 3 talloc elements */
218 struct iovec *recv_iov;
222 const uint8_t *fixed;
227 uint8_t transform[SMB2_TF_HDR_SIZE];
228 uint8_t hdr[SMB2_HDR_BODY];
229 uint8_t pad[7]; /* padding space for compounding */
232 * always an array of 3 talloc elements
233 * (without a SMB2_TRANSFORM header!)
237 struct iovec *recv_iov;
239 uint16_t credit_charge;
244 bool signing_skipped;
250 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
253 * NT_STATUS_OK, means we do not notify the callers
255 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
257 while (conn->sessions) {
258 conn->sessions->conn = NULL;
259 DLIST_REMOVE(conn->sessions, conn->sessions);
262 if (conn->smb1.trans_enc) {
263 TALLOC_FREE(conn->smb1.trans_enc);
269 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
271 const char *remote_name,
272 enum smb_signing_setting signing_state,
273 uint32_t smb1_capabilities,
274 struct GUID *client_guid,
275 uint32_t smb2_capabilities)
277 struct smbXcli_conn *conn = NULL;
279 struct sockaddr *sa = NULL;
283 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
289 conn->write_fd = dup(fd);
290 if (conn->write_fd == -1) {
294 conn->remote_name = talloc_strdup(conn, remote_name);
295 if (conn->remote_name == NULL) {
300 ss = (void *)&conn->local_ss;
301 sa = (struct sockaddr *)ss;
302 sa_length = sizeof(conn->local_ss);
303 ret = getsockname(fd, sa, &sa_length);
307 ss = (void *)&conn->remote_ss;
308 sa = (struct sockaddr *)ss;
309 sa_length = sizeof(conn->remote_ss);
310 ret = getpeername(fd, sa, &sa_length);
315 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
316 if (conn->outgoing == NULL) {
319 conn->pending = NULL;
321 conn->protocol = PROTOCOL_NONE;
323 switch (signing_state) {
324 case SMB_SIGNING_OFF:
326 conn->allow_signing = false;
327 conn->desire_signing = false;
328 conn->mandatory_signing = false;
330 case SMB_SIGNING_DEFAULT:
331 case SMB_SIGNING_IF_REQUIRED:
332 /* if the server requires it */
333 conn->allow_signing = true;
334 conn->desire_signing = false;
335 conn->mandatory_signing = false;
337 case SMB_SIGNING_REQUIRED:
339 conn->allow_signing = true;
340 conn->desire_signing = true;
341 conn->mandatory_signing = true;
345 conn->smb1.client.capabilities = smb1_capabilities;
346 conn->smb1.client.max_xmit = UINT16_MAX;
348 conn->smb1.capabilities = conn->smb1.client.capabilities;
349 conn->smb1.max_xmit = 1024;
353 /* initialise signing */
354 conn->smb1.signing = smb_signing_init(conn,
356 conn->desire_signing,
357 conn->mandatory_signing);
358 if (!conn->smb1.signing) {
362 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
363 if (conn->mandatory_signing) {
364 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
367 conn->smb2.client.guid = *client_guid;
369 conn->smb2.client.capabilities = smb2_capabilities;
371 conn->smb2.cur_credits = 1;
372 conn->smb2.max_credits = 0;
374 talloc_set_destructor(conn, smbXcli_conn_destructor);
378 if (conn->write_fd != -1) {
379 close(conn->write_fd);
385 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
391 if (conn->read_fd == -1) {
398 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
400 return conn->protocol;
403 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
405 if (conn->protocol >= PROTOCOL_SMB2_02) {
409 if (conn->smb1.capabilities & CAP_UNICODE) {
416 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
418 set_socket_options(conn->read_fd, options);
421 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
423 return &conn->local_ss;
426 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
428 return &conn->remote_ss;
431 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
433 return conn->remote_name;
436 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
438 if (conn->protocol >= PROTOCOL_SMB2_02) {
445 return conn->smb1.server.max_mux;
448 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
450 if (conn->protocol >= PROTOCOL_SMB2_02) {
451 return conn->smb2.server.system_time;
454 return conn->smb1.server.system_time;
457 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
459 if (conn->protocol >= PROTOCOL_SMB2_02) {
460 return &conn->smb2.server.gss_blob;
463 return &conn->smb1.server.gss_blob;
466 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
468 if (conn->protocol >= PROTOCOL_SMB2_02) {
469 return &conn->smb2.server.guid;
472 return &conn->smb1.server.guid;
475 struct smbXcli_conn_samba_suicide_state {
476 struct smbXcli_conn *conn;
481 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
483 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
484 struct tevent_context *ev,
485 struct smbXcli_conn *conn,
488 struct tevent_req *req, *subreq;
489 struct smbXcli_conn_samba_suicide_state *state;
491 req = tevent_req_create(mem_ctx, &state,
492 struct smbXcli_conn_samba_suicide_state);
497 SIVAL(state->buf, 4, 0x74697865);
498 SCVAL(state->buf, 8, exitcode);
499 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
501 state->iov.iov_base = state->buf;
502 state->iov.iov_len = sizeof(state->buf);
504 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
505 false, &state->iov, 1);
506 if (tevent_req_nomem(subreq, req)) {
507 return tevent_req_post(req, ev);
509 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
513 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
515 struct tevent_req *req = tevent_req_callback_data(
516 subreq, struct tevent_req);
517 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
518 req, struct smbXcli_conn_samba_suicide_state);
522 nwritten = writev_recv(subreq, &err);
524 if (nwritten == -1) {
525 NTSTATUS status = map_nt_error_from_unix_common(err);
526 smbXcli_conn_disconnect(state->conn, status);
529 tevent_req_done(req);
532 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
534 return tevent_req_simple_recv_ntstatus(req);
537 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
540 TALLOC_CTX *frame = talloc_stackframe();
541 struct tevent_context *ev;
542 struct tevent_req *req;
543 NTSTATUS status = NT_STATUS_NO_MEMORY;
546 if (smbXcli_conn_has_async_calls(conn)) {
548 * Can't use sync call while an async call is in flight
550 status = NT_STATUS_INVALID_PARAMETER_MIX;
553 ev = tevent_context_init(frame);
557 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
561 ok = tevent_req_poll(req, ev);
563 status = map_nt_error_from_unix_common(errno);
566 status = smbXcli_conn_samba_suicide_recv(req);
572 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
574 return conn->smb1.capabilities;
577 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
579 return conn->smb1.max_xmit;
582 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
584 return conn->smb1.server.session_key;
587 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
589 return conn->smb1.server.challenge;
592 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
594 return conn->smb1.server.security_mode;
597 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
599 return conn->smb1.server.readbraw;
602 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
604 return conn->smb1.server.writebraw;
607 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
609 return conn->smb1.server.lockread;
612 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
614 return conn->smb1.server.writeunlock;
617 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
619 return conn->smb1.server.time_zone;
622 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
623 const DATA_BLOB user_session_key,
624 const DATA_BLOB response)
626 return smb_signing_activate(conn->smb1.signing,
631 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
632 const uint8_t *buf, uint32_t seqnum)
634 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
637 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
639 return smb_signing_is_active(conn->smb1.signing);
642 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
643 struct smb_trans_enc_state *es)
645 /* Replace the old state, if any. */
646 if (conn->smb1.trans_enc) {
647 TALLOC_FREE(conn->smb1.trans_enc);
649 conn->smb1.trans_enc = es;
652 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
654 return common_encryption_on(conn->smb1.trans_enc);
658 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
660 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
661 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
663 if (NT_STATUS_IS_OK(status)) {
667 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
671 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
675 * Is the SMB command able to hold an AND_X successor
676 * @param[in] cmd The SMB command in question
677 * @retval Can we add a chained request after "cmd"?
679 bool smb1cli_is_andx_req(uint8_t cmd)
699 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
701 size_t num_pending = talloc_array_length(conn->pending);
707 result = conn->smb1.mid++;
708 if ((result == 0) || (result == 0xffff)) {
712 for (i=0; i<num_pending; i++) {
713 if (result == smb1cli_req_mid(conn->pending[i])) {
718 if (i == num_pending) {
724 void smbXcli_req_unset_pending(struct tevent_req *req)
726 struct smbXcli_req_state *state =
728 struct smbXcli_req_state);
729 struct smbXcli_conn *conn = state->conn;
730 size_t num_pending = talloc_array_length(conn->pending);
733 if (state->smb1.mid != 0) {
735 * This is a [nt]trans[2] request which waits
736 * for more than one reply.
741 talloc_set_destructor(req, NULL);
743 if (num_pending == 1) {
745 * The pending read_smb tevent_req is a child of
746 * conn->pending. So if nothing is pending anymore, we need to
747 * delete the socket read fde.
749 TALLOC_FREE(conn->pending);
750 conn->read_smb_req = NULL;
754 for (i=0; i<num_pending; i++) {
755 if (req == conn->pending[i]) {
759 if (i == num_pending) {
761 * Something's seriously broken. Just returning here is the
762 * right thing nevertheless, the point of this routine is to
763 * remove ourselves from conn->pending.
769 * Remove ourselves from the conn->pending array
771 for (; i < (num_pending - 1); i++) {
772 conn->pending[i] = conn->pending[i+1];
776 * No NULL check here, we're shrinking by sizeof(void *), and
777 * talloc_realloc just adjusts the size for this.
779 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
784 static int smbXcli_req_destructor(struct tevent_req *req)
786 struct smbXcli_req_state *state =
788 struct smbXcli_req_state);
791 * Make sure we really remove it from
792 * the pending array on destruction.
795 smbXcli_req_unset_pending(req);
799 static bool smb1cli_req_cancel(struct tevent_req *req);
800 static bool smb2cli_req_cancel(struct tevent_req *req);
802 static bool smbXcli_req_cancel(struct tevent_req *req)
804 struct smbXcli_req_state *state =
806 struct smbXcli_req_state);
808 if (!smbXcli_conn_is_connected(state->conn)) {
812 if (state->conn->protocol == PROTOCOL_NONE) {
816 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
817 return smb2cli_req_cancel(req);
820 return smb1cli_req_cancel(req);
823 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
825 bool smbXcli_req_set_pending(struct tevent_req *req)
827 struct smbXcli_req_state *state =
829 struct smbXcli_req_state);
830 struct smbXcli_conn *conn;
831 struct tevent_req **pending;
836 if (!smbXcli_conn_is_connected(conn)) {
840 num_pending = talloc_array_length(conn->pending);
842 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
844 if (pending == NULL) {
847 pending[num_pending] = req;
848 conn->pending = pending;
849 talloc_set_destructor(req, smbXcli_req_destructor);
850 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
852 if (!smbXcli_conn_receive_next(conn)) {
854 * the caller should notify the current request
856 * And all other pending requests get notified
857 * by smbXcli_conn_disconnect().
859 smbXcli_req_unset_pending(req);
860 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
867 static void smbXcli_conn_received(struct tevent_req *subreq);
869 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
871 size_t num_pending = talloc_array_length(conn->pending);
872 struct tevent_req *req;
873 struct smbXcli_req_state *state;
875 if (conn->read_smb_req != NULL) {
879 if (num_pending == 0) {
880 if (conn->smb2.mid < UINT64_MAX) {
881 /* no more pending requests, so we are done for now */
886 * If there are no more SMB2 requests possible,
887 * because we are out of message ids,
888 * we need to disconnect.
890 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
894 req = conn->pending[0];
895 state = tevent_req_data(req, struct smbXcli_req_state);
898 * We're the first ones, add the read_smb request that waits for the
899 * answer from the server
901 conn->read_smb_req = read_smb_send(conn->pending,
904 if (conn->read_smb_req == NULL) {
907 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
911 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
913 tevent_queue_stop(conn->outgoing);
915 if (conn->read_fd != -1) {
916 close(conn->read_fd);
918 if (conn->write_fd != -1) {
919 close(conn->write_fd);
925 * Cancel all pending requests. We do not do a for-loop walking
926 * conn->pending because that array changes in
927 * smbXcli_req_unset_pending.
929 while (talloc_array_length(conn->pending) > 0) {
930 struct tevent_req *req;
931 struct smbXcli_req_state *state;
932 struct tevent_req **chain;
936 req = conn->pending[0];
937 state = tevent_req_data(req, struct smbXcli_req_state);
939 if (state->smb1.chained_requests == NULL) {
941 * We're dead. No point waiting for trans2
946 smbXcli_req_unset_pending(req);
948 if (NT_STATUS_IS_OK(status)) {
949 /* do not notify the callers */
954 * we need to defer the callback, because we may notify
955 * more then one caller.
957 tevent_req_defer_callback(req, state->ev);
958 tevent_req_nterror(req, status);
962 chain = talloc_move(conn, &state->smb1.chained_requests);
963 num_chained = talloc_array_length(chain);
965 for (i=0; i<num_chained; i++) {
967 state = tevent_req_data(req, struct smbXcli_req_state);
970 * We're dead. No point waiting for trans2
975 smbXcli_req_unset_pending(req);
977 if (NT_STATUS_IS_OK(status)) {
978 /* do not notify the callers */
983 * we need to defer the callback, because we may notify
984 * more than one caller.
986 tevent_req_defer_callback(req, state->ev);
987 tevent_req_nterror(req, status);
994 * Fetch a smb request's mid. Only valid after the request has been sent by
995 * smb1cli_req_send().
997 uint16_t smb1cli_req_mid(struct tevent_req *req)
999 struct smbXcli_req_state *state =
1000 tevent_req_data(req,
1001 struct smbXcli_req_state);
1003 if (state->smb1.mid != 0) {
1004 return state->smb1.mid;
1007 return SVAL(state->smb1.hdr, HDR_MID);
1010 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1012 struct smbXcli_req_state *state =
1013 tevent_req_data(req,
1014 struct smbXcli_req_state);
1016 state->smb1.mid = mid;
1019 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1021 struct smbXcli_req_state *state =
1022 tevent_req_data(req,
1023 struct smbXcli_req_state);
1025 return state->smb1.seqnum;
1028 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1030 struct smbXcli_req_state *state =
1031 tevent_req_data(req,
1032 struct smbXcli_req_state);
1034 state->smb1.seqnum = seqnum;
1037 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1041 for (i=0; i<count; i++) {
1042 result += iov[i].iov_len;
1047 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1048 const struct iovec *iov,
1051 size_t len = smbXcli_iov_len(iov, count);
1056 buf = talloc_array(mem_ctx, uint8_t, len);
1061 for (i=0; i<count; i++) {
1062 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1063 copied += iov[i].iov_len;
1068 static void smb1cli_req_flags(enum protocol_types protocol,
1069 uint32_t smb1_capabilities,
1070 uint8_t smb_command,
1071 uint8_t additional_flags,
1072 uint8_t clear_flags,
1074 uint16_t additional_flags2,
1075 uint16_t clear_flags2,
1079 uint16_t flags2 = 0;
1081 if (protocol >= PROTOCOL_LANMAN1) {
1082 flags |= FLAG_CASELESS_PATHNAMES;
1083 flags |= FLAG_CANONICAL_PATHNAMES;
1086 if (protocol >= PROTOCOL_LANMAN2) {
1087 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1088 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1091 if (protocol >= PROTOCOL_NT1) {
1092 flags2 |= FLAGS2_IS_LONG_NAME;
1094 if (smb1_capabilities & CAP_UNICODE) {
1095 flags2 |= FLAGS2_UNICODE_STRINGS;
1097 if (smb1_capabilities & CAP_STATUS32) {
1098 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1100 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1101 flags2 |= FLAGS2_EXTENDED_SECURITY;
1105 flags |= additional_flags;
1106 flags &= ~clear_flags;
1107 flags2 |= additional_flags2;
1108 flags2 &= ~clear_flags2;
1114 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1116 static bool smb1cli_req_cancel(struct tevent_req *req)
1118 struct smbXcli_req_state *state =
1119 tevent_req_data(req,
1120 struct smbXcli_req_state);
1125 struct tevent_req *subreq;
1128 flags = CVAL(state->smb1.hdr, HDR_FLG);
1129 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1130 pid = SVAL(state->smb1.hdr, HDR_PID);
1131 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1132 mid = SVAL(state->smb1.hdr, HDR_MID);
1134 subreq = smb1cli_req_create(state, state->ev,
1144 0, NULL); /* bytes */
1145 if (subreq == NULL) {
1148 smb1cli_req_set_mid(subreq, mid);
1150 status = smb1cli_req_chain_submit(&subreq, 1);
1151 if (!NT_STATUS_IS_OK(status)) {
1152 TALLOC_FREE(subreq);
1155 smb1cli_req_set_mid(subreq, 0);
1157 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1162 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1164 /* we do not care about the result */
1165 TALLOC_FREE(subreq);
1168 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1169 struct tevent_context *ev,
1170 struct smbXcli_conn *conn,
1171 uint8_t smb_command,
1172 uint8_t additional_flags,
1173 uint8_t clear_flags,
1174 uint16_t additional_flags2,
1175 uint16_t clear_flags2,
1176 uint32_t timeout_msec,
1178 struct smbXcli_tcon *tcon,
1179 struct smbXcli_session *session,
1180 uint8_t wct, uint16_t *vwv,
1182 struct iovec *bytes_iov)
1184 struct tevent_req *req;
1185 struct smbXcli_req_state *state;
1187 uint16_t flags2 = 0;
1191 if (iov_count > MAX_SMB_IOV) {
1193 * Should not happen :-)
1198 req = tevent_req_create(mem_ctx, &state,
1199 struct smbXcli_req_state);
1205 state->session = session;
1209 uid = session->smb1.session_id;
1213 tid = tcon->smb1.tcon_id;
1216 state->smb1.recv_cmd = 0xFF;
1217 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1218 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1219 if (state->smb1.recv_iov == NULL) {
1224 smb1cli_req_flags(conn->protocol,
1225 conn->smb1.capabilities,
1234 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1235 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1236 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1237 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1238 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1239 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1240 SSVAL(state->smb1.hdr, HDR_TID, tid);
1241 SSVAL(state->smb1.hdr, HDR_PID, pid);
1242 SSVAL(state->smb1.hdr, HDR_UID, uid);
1243 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1244 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1246 state->smb1.vwv = vwv;
1248 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1250 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1251 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1252 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1253 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1254 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1255 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1256 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1257 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1259 if (iov_count != 0) {
1260 memcpy(&state->smb1.iov[4], bytes_iov,
1261 iov_count * sizeof(*bytes_iov));
1263 state->smb1.iov_count = iov_count + 4;
1265 if (timeout_msec > 0) {
1266 struct timeval endtime;
1268 endtime = timeval_current_ofs_msec(timeout_msec);
1269 if (!tevent_req_set_endtime(req, ev, endtime)) {
1274 switch (smb_command) {
1278 state->one_way = true;
1281 state->one_way = true;
1282 state->smb1.one_way_seqnum = true;
1286 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1287 state->one_way = true;
1295 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1296 struct iovec *iov, int iov_count,
1298 bool one_way_seqnum)
1300 TALLOC_CTX *frame = NULL;
1304 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1305 * iovec directly. MD5Update would do that just fine.
1308 if (iov_count < 4) {
1309 return NT_STATUS_INVALID_PARAMETER_MIX;
1311 if (iov[0].iov_len != NBT_HDR_SIZE) {
1312 return NT_STATUS_INVALID_PARAMETER_MIX;
1314 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1315 return NT_STATUS_INVALID_PARAMETER_MIX;
1317 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1318 return NT_STATUS_INVALID_PARAMETER_MIX;
1320 if (iov[3].iov_len != sizeof(uint16_t)) {
1321 return NT_STATUS_INVALID_PARAMETER_MIX;
1324 frame = talloc_stackframe();
1326 buf = smbXcli_iov_concat(frame, iov, iov_count);
1328 return NT_STATUS_NO_MEMORY;
1331 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1333 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1334 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1337 return NT_STATUS_OK;
1340 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1341 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1342 TALLOC_CTX *tmp_mem,
1345 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1346 struct smbXcli_req_state *state,
1347 struct iovec *iov, int iov_count)
1349 struct tevent_req *subreq;
1354 if (!smbXcli_conn_is_connected(state->conn)) {
1355 return NT_STATUS_CONNECTION_DISCONNECTED;
1358 if (state->conn->protocol > PROTOCOL_NT1) {
1359 return NT_STATUS_REVISION_MISMATCH;
1362 if (iov_count < 4) {
1363 return NT_STATUS_INVALID_PARAMETER_MIX;
1365 if (iov[0].iov_len != NBT_HDR_SIZE) {
1366 return NT_STATUS_INVALID_PARAMETER_MIX;
1368 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1369 return NT_STATUS_INVALID_PARAMETER_MIX;
1371 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1372 return NT_STATUS_INVALID_PARAMETER_MIX;
1374 if (iov[3].iov_len != sizeof(uint16_t)) {
1375 return NT_STATUS_INVALID_PARAMETER_MIX;
1378 cmd = CVAL(iov[1].iov_base, HDR_COM);
1379 if (cmd == SMBreadBraw) {
1380 if (smbXcli_conn_has_async_calls(state->conn)) {
1381 return NT_STATUS_INVALID_PARAMETER_MIX;
1383 state->conn->smb1.read_braw_req = req;
1386 if (state->smb1.mid != 0) {
1387 mid = state->smb1.mid;
1389 mid = smb1cli_alloc_mid(state->conn);
1391 SSVAL(iov[1].iov_base, HDR_MID, mid);
1393 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1395 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1396 &state->smb1.seqnum,
1397 state->smb1.one_way_seqnum);
1399 if (!NT_STATUS_IS_OK(status)) {
1404 * If we supported multiple encrytion contexts
1405 * here we'd look up based on tid.
1407 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1408 char *buf, *enc_buf;
1410 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1412 return NT_STATUS_NO_MEMORY;
1414 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1415 (char *)buf, &enc_buf);
1417 if (!NT_STATUS_IS_OK(status)) {
1418 DEBUG(0, ("Error in encrypting client message: %s\n",
1419 nt_errstr(status)));
1422 buf = (char *)talloc_memdup(state, enc_buf,
1423 smb_len_nbt(enc_buf)+4);
1426 return NT_STATUS_NO_MEMORY;
1428 iov[0].iov_base = (void *)buf;
1429 iov[0].iov_len = talloc_get_size(buf);
1433 if (state->conn->dispatch_incoming == NULL) {
1434 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1437 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1439 subreq = writev_send(state, state->ev, state->conn->outgoing,
1440 state->conn->write_fd, false, iov, iov_count);
1441 if (subreq == NULL) {
1442 return NT_STATUS_NO_MEMORY;
1444 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1445 return NT_STATUS_OK;
1448 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1449 struct tevent_context *ev,
1450 struct smbXcli_conn *conn,
1451 uint8_t smb_command,
1452 uint8_t additional_flags,
1453 uint8_t clear_flags,
1454 uint16_t additional_flags2,
1455 uint16_t clear_flags2,
1456 uint32_t timeout_msec,
1458 struct smbXcli_tcon *tcon,
1459 struct smbXcli_session *session,
1460 uint8_t wct, uint16_t *vwv,
1462 const uint8_t *bytes)
1464 struct tevent_req *req;
1468 iov.iov_base = discard_const_p(void, bytes);
1469 iov.iov_len = num_bytes;
1471 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1472 additional_flags, clear_flags,
1473 additional_flags2, clear_flags2,
1480 if (!tevent_req_is_in_progress(req)) {
1481 return tevent_req_post(req, ev);
1483 status = smb1cli_req_chain_submit(&req, 1);
1484 if (tevent_req_nterror(req, status)) {
1485 return tevent_req_post(req, ev);
1490 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1492 struct tevent_req *req =
1493 tevent_req_callback_data(subreq,
1495 struct smbXcli_req_state *state =
1496 tevent_req_data(req,
1497 struct smbXcli_req_state);
1501 nwritten = writev_recv(subreq, &err);
1502 TALLOC_FREE(subreq);
1503 if (nwritten == -1) {
1504 NTSTATUS status = map_nt_error_from_unix_common(err);
1505 smbXcli_conn_disconnect(state->conn, status);
1509 if (state->one_way) {
1510 state->inbuf = NULL;
1511 tevent_req_done(req);
1515 if (!smbXcli_req_set_pending(req)) {
1516 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1521 static void smbXcli_conn_received(struct tevent_req *subreq)
1523 struct smbXcli_conn *conn =
1524 tevent_req_callback_data(subreq,
1525 struct smbXcli_conn);
1526 TALLOC_CTX *frame = talloc_stackframe();
1532 if (subreq != conn->read_smb_req) {
1533 DEBUG(1, ("Internal error: cli_smb_received called with "
1534 "unexpected subreq\n"));
1535 status = NT_STATUS_INTERNAL_ERROR;
1536 smbXcli_conn_disconnect(conn, status);
1540 conn->read_smb_req = NULL;
1542 received = read_smb_recv(subreq, frame, &inbuf, &err);
1543 TALLOC_FREE(subreq);
1544 if (received == -1) {
1545 status = map_nt_error_from_unix_common(err);
1546 smbXcli_conn_disconnect(conn, status);
1551 status = conn->dispatch_incoming(conn, frame, inbuf);
1553 if (NT_STATUS_IS_OK(status)) {
1555 * We should not do any more processing
1556 * as the dispatch function called
1557 * tevent_req_done().
1560 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1562 * We got an error, so notify all pending requests
1564 smbXcli_conn_disconnect(conn, status);
1569 * We got NT_STATUS_RETRY, so we may ask for a
1570 * next incoming pdu.
1572 if (!smbXcli_conn_receive_next(conn)) {
1573 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1577 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1578 struct iovec **piov, int *pnum_iov)
1589 buflen = smb_len_nbt(buf);
1592 hdr = buf + NBT_HDR_SIZE;
1594 if (buflen < MIN_SMB_SIZE) {
1595 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1599 * This returns iovec elements in the following order:
1614 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1616 return NT_STATUS_NO_MEMORY;
1618 iov[0].iov_base = hdr;
1619 iov[0].iov_len = HDR_WCT;
1622 cmd = CVAL(hdr, HDR_COM);
1626 size_t len = buflen - taken;
1628 struct iovec *iov_tmp;
1635 * we need at least WCT and BCC
1637 needed = sizeof(uint8_t) + sizeof(uint16_t);
1639 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1640 __location__, (int)len, (int)needed));
1645 * Now we check if the specified words are there
1647 wct = CVAL(hdr, wct_ofs);
1648 needed += wct * sizeof(uint16_t);
1650 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1651 __location__, (int)len, (int)needed));
1656 * Now we check if the specified bytes are there
1658 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1659 bcc = SVAL(hdr, bcc_ofs);
1660 needed += bcc * sizeof(uint8_t);
1662 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1663 __location__, (int)len, (int)needed));
1668 * we allocate 2 iovec structures for words and bytes
1670 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1672 if (iov_tmp == NULL) {
1674 return NT_STATUS_NO_MEMORY;
1677 cur = &iov[num_iov];
1680 cur[0].iov_len = wct * sizeof(uint16_t);
1681 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1682 cur[1].iov_len = bcc * sizeof(uint8_t);
1683 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1687 if (!smb1cli_is_andx_req(cmd)) {
1689 * If the current command does not have AndX chanining
1695 if (wct == 0 && bcc == 0) {
1697 * An empty response also ends the chain,
1698 * most likely with an error.
1704 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1705 __location__, (int)wct, (int)cmd));
1708 cmd = CVAL(cur[0].iov_base, 0);
1711 * If it is the end of the chain we are also done.
1715 wct_ofs = SVAL(cur[0].iov_base, 2);
1717 if (wct_ofs < taken) {
1718 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1720 if (wct_ofs > buflen) {
1721 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1725 * we consumed everything up to the start of the next
1731 remaining = buflen - taken;
1733 if (remaining > 0 && num_iov >= 3) {
1735 * The last DATA block gets the remaining
1736 * bytes, this is needed to support
1737 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1739 iov[num_iov-1].iov_len += remaining;
1743 *pnum_iov = num_iov;
1744 return NT_STATUS_OK;
1748 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1751 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1752 TALLOC_CTX *tmp_mem,
1755 struct tevent_req *req;
1756 struct smbXcli_req_state *state;
1763 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1764 struct iovec *iov = NULL;
1766 struct tevent_req **chain = NULL;
1767 size_t num_chained = 0;
1768 size_t num_responses = 0;
1770 if (conn->smb1.read_braw_req != NULL) {
1771 req = conn->smb1.read_braw_req;
1772 conn->smb1.read_braw_req = NULL;
1773 state = tevent_req_data(req, struct smbXcli_req_state);
1775 smbXcli_req_unset_pending(req);
1777 if (state->smb1.recv_iov == NULL) {
1779 * For requests with more than
1780 * one response, we have to readd the
1783 state->smb1.recv_iov = talloc_zero_array(state,
1786 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1787 return NT_STATUS_OK;
1791 state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
1792 state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
1793 ZERO_STRUCT(state->smb1.recv_iov[1]);
1794 ZERO_STRUCT(state->smb1.recv_iov[2]);
1796 state->smb1.recv_cmd = SMBreadBraw;
1797 state->smb1.recv_status = NT_STATUS_OK;
1798 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1800 tevent_req_done(req);
1801 return NT_STATUS_OK;
1804 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1805 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1806 DEBUG(10, ("Got non-SMB PDU\n"));
1807 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1811 * If we supported multiple encrytion contexts
1812 * here we'd look up based on tid.
1814 if (common_encryption_on(conn->smb1.trans_enc)
1815 && (CVAL(inbuf, 0) == 0)) {
1816 uint16_t enc_ctx_num;
1818 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1819 if (!NT_STATUS_IS_OK(status)) {
1820 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1821 nt_errstr(status)));
1825 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1826 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1828 conn->smb1.trans_enc->enc_ctx_num));
1829 return NT_STATUS_INVALID_HANDLE;
1832 status = common_decrypt_buffer(conn->smb1.trans_enc,
1834 if (!NT_STATUS_IS_OK(status)) {
1835 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1836 nt_errstr(status)));
1841 mid = SVAL(inhdr, HDR_MID);
1842 num_pending = talloc_array_length(conn->pending);
1844 for (i=0; i<num_pending; i++) {
1845 if (mid == smb1cli_req_mid(conn->pending[i])) {
1849 if (i == num_pending) {
1850 /* Dump unexpected reply */
1851 return NT_STATUS_RETRY;
1854 oplock_break = false;
1856 if (mid == 0xffff) {
1858 * Paranoia checks that this is really an oplock break request.
1860 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1861 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1862 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1863 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1864 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1866 if (!oplock_break) {
1867 /* Dump unexpected reply */
1868 return NT_STATUS_RETRY;
1872 req = conn->pending[i];
1873 state = tevent_req_data(req, struct smbXcli_req_state);
1875 if (!oplock_break /* oplock breaks are not signed */
1876 && !smb_signing_check_pdu(conn->smb1.signing,
1877 inbuf, state->smb1.seqnum+1)) {
1878 DEBUG(10, ("cli_check_sign_mac failed\n"));
1879 return NT_STATUS_ACCESS_DENIED;
1882 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1884 if (!NT_STATUS_IS_OK(status)) {
1885 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1886 nt_errstr(status)));
1890 cmd = CVAL(inhdr, HDR_COM);
1891 status = smb1cli_pull_raw_error(inhdr);
1893 if (state->smb1.chained_requests == NULL) {
1895 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1898 smbXcli_req_unset_pending(req);
1900 if (state->smb1.recv_iov == NULL) {
1902 * For requests with more than
1903 * one response, we have to readd the
1906 state->smb1.recv_iov = talloc_zero_array(state,
1909 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1910 return NT_STATUS_OK;
1914 state->smb1.recv_cmd = cmd;
1915 state->smb1.recv_status = status;
1916 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1918 state->smb1.recv_iov[0] = iov[0];
1919 state->smb1.recv_iov[1] = iov[1];
1920 state->smb1.recv_iov[2] = iov[2];
1922 if (talloc_array_length(conn->pending) == 0) {
1923 tevent_req_done(req);
1924 return NT_STATUS_OK;
1927 tevent_req_defer_callback(req, state->ev);
1928 tevent_req_done(req);
1929 return NT_STATUS_RETRY;
1932 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1933 num_chained = talloc_array_length(chain);
1934 num_responses = (num_iov - 1)/2;
1936 if (num_responses > num_chained) {
1937 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1940 for (i=0; i<num_chained; i++) {
1941 size_t iov_idx = 1 + (i*2);
1942 struct iovec *cur = &iov[iov_idx];
1946 state = tevent_req_data(req, struct smbXcli_req_state);
1948 smbXcli_req_unset_pending(req);
1951 * as we finish multiple requests here
1952 * we need to defer the callbacks as
1953 * they could destroy our current stack state.
1955 tevent_req_defer_callback(req, state->ev);
1957 if (i >= num_responses) {
1958 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1962 if (state->smb1.recv_iov == NULL) {
1964 * For requests with more than
1965 * one response, we have to readd the
1968 state->smb1.recv_iov = talloc_zero_array(state,
1971 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1976 state->smb1.recv_cmd = cmd;
1978 if (i == (num_responses - 1)) {
1980 * The last request in the chain gets the status
1982 state->smb1.recv_status = status;
1984 cmd = CVAL(cur[0].iov_base, 0);
1985 state->smb1.recv_status = NT_STATUS_OK;
1988 state->inbuf = inbuf;
1991 * Note: here we use talloc_reference() in a way
1992 * that does not expose it to the caller.
1994 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1995 if (tevent_req_nomem(inbuf_ref, req)) {
1999 /* copy the related buffers */
2000 state->smb1.recv_iov[0] = iov[0];
2001 state->smb1.recv_iov[1] = cur[0];
2002 state->smb1.recv_iov[2] = cur[1];
2004 tevent_req_done(req);
2007 return NT_STATUS_RETRY;
2010 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2011 TALLOC_CTX *mem_ctx,
2012 struct iovec **piov,
2016 uint32_t *pvwv_offset,
2017 uint32_t *pnum_bytes,
2019 uint32_t *pbytes_offset,
2021 const struct smb1cli_req_expected_response *expected,
2022 size_t num_expected)
2024 struct smbXcli_req_state *state =
2025 tevent_req_data(req,
2026 struct smbXcli_req_state);
2027 NTSTATUS status = NT_STATUS_OK;
2028 struct iovec *recv_iov = NULL;
2029 uint8_t *hdr = NULL;
2031 uint32_t vwv_offset = 0;
2032 uint16_t *vwv = NULL;
2033 uint32_t num_bytes = 0;
2034 uint32_t bytes_offset = 0;
2035 uint8_t *bytes = NULL;
2037 bool found_status = false;
2038 bool found_size = false;
2052 if (pvwv_offset != NULL) {
2055 if (pnum_bytes != NULL) {
2058 if (pbytes != NULL) {
2061 if (pbytes_offset != NULL) {
2064 if (pinbuf != NULL) {
2068 if (state->inbuf != NULL) {
2069 recv_iov = state->smb1.recv_iov;
2070 state->smb1.recv_iov = NULL;
2071 if (state->smb1.recv_cmd != SMBreadBraw) {
2072 hdr = (uint8_t *)recv_iov[0].iov_base;
2073 wct = recv_iov[1].iov_len/2;
2074 vwv = (uint16_t *)recv_iov[1].iov_base;
2075 vwv_offset = PTR_DIFF(vwv, hdr);
2076 num_bytes = recv_iov[2].iov_len;
2077 bytes = (uint8_t *)recv_iov[2].iov_base;
2078 bytes_offset = PTR_DIFF(bytes, hdr);
2082 if (tevent_req_is_nterror(req, &status)) {
2083 for (i=0; i < num_expected; i++) {
2084 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2085 found_status = true;
2091 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2097 if (num_expected == 0) {
2098 found_status = true;
2102 status = state->smb1.recv_status;
2104 for (i=0; i < num_expected; i++) {
2105 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2109 found_status = true;
2110 if (expected[i].wct == 0) {
2115 if (expected[i].wct == wct) {
2121 if (!found_status) {
2126 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2130 *piov = talloc_move(mem_ctx, &recv_iov);
2142 if (pvwv_offset != NULL) {
2143 *pvwv_offset = vwv_offset;
2145 if (pnum_bytes != NULL) {
2146 *pnum_bytes = num_bytes;
2148 if (pbytes != NULL) {
2151 if (pbytes_offset != NULL) {
2152 *pbytes_offset = bytes_offset;
2154 if (pinbuf != NULL) {
2155 *pinbuf = state->inbuf;
2161 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2168 for (i=0; i<num_reqs; i++) {
2169 struct smbXcli_req_state *state;
2170 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2171 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2172 state->smb1.iov_count-2);
2173 wct_ofs = (wct_ofs + 3) & ~3;
2178 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2180 struct smbXcli_req_state *first_state =
2181 tevent_req_data(reqs[0],
2182 struct smbXcli_req_state);
2183 struct smbXcli_req_state *state;
2185 size_t chain_padding = 0;
2187 struct iovec *iov = NULL;
2188 struct iovec *this_iov;
2192 if (num_reqs == 1) {
2193 return smb1cli_req_writev_submit(reqs[0], first_state,
2194 first_state->smb1.iov,
2195 first_state->smb1.iov_count);
2199 for (i=0; i<num_reqs; i++) {
2200 if (!tevent_req_is_in_progress(reqs[i])) {
2201 return NT_STATUS_INTERNAL_ERROR;
2204 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2206 if (state->smb1.iov_count < 4) {
2207 return NT_STATUS_INVALID_PARAMETER_MIX;
2212 * The NBT and SMB header
2225 iovlen += state->smb1.iov_count - 2;
2228 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2230 return NT_STATUS_NO_MEMORY;
2233 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2234 first_state, reqs, sizeof(*reqs) * num_reqs);
2235 if (first_state->smb1.chained_requests == NULL) {
2237 return NT_STATUS_NO_MEMORY;
2240 wct_offset = HDR_WCT;
2243 for (i=0; i<num_reqs; i++) {
2244 size_t next_padding = 0;
2247 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2249 if (i < num_reqs-1) {
2250 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2251 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2253 TALLOC_FREE(first_state->smb1.chained_requests);
2254 return NT_STATUS_INVALID_PARAMETER_MIX;
2258 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2259 state->smb1.iov_count-2) + 1;
2260 if ((wct_offset % 4) != 0) {
2261 next_padding = 4 - (wct_offset % 4);
2263 wct_offset += next_padding;
2264 vwv = state->smb1.vwv;
2266 if (i < num_reqs-1) {
2267 struct smbXcli_req_state *next_state =
2268 tevent_req_data(reqs[i+1],
2269 struct smbXcli_req_state);
2270 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2272 SSVAL(vwv+1, 0, wct_offset);
2273 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2274 /* properly end the chain */
2275 SCVAL(vwv+0, 0, 0xff);
2276 SCVAL(vwv+0, 1, 0xff);
2282 * The NBT and SMB header
2284 this_iov[0] = state->smb1.iov[0];
2285 this_iov[1] = state->smb1.iov[1];
2289 * This one is a bit subtle. We have to add
2290 * chain_padding bytes between the requests, and we
2291 * have to also include the wct field of the
2292 * subsequent requests. We use the subsequent header
2293 * for the padding, it contains the wct field in its
2296 this_iov[0].iov_len = chain_padding+1;
2297 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2298 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2299 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2304 * copy the words and bytes
2306 memcpy(this_iov, state->smb1.iov+2,
2307 sizeof(struct iovec) * (state->smb1.iov_count-2));
2308 this_iov += state->smb1.iov_count - 2;
2309 chain_padding = next_padding;
2312 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2313 if (nbt_len > first_state->conn->smb1.max_xmit) {
2315 TALLOC_FREE(first_state->smb1.chained_requests);
2316 return NT_STATUS_INVALID_PARAMETER_MIX;
2319 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2320 if (!NT_STATUS_IS_OK(status)) {
2322 TALLOC_FREE(first_state->smb1.chained_requests);
2326 return NT_STATUS_OK;
2329 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2331 return ((tevent_queue_length(conn->outgoing) != 0)
2332 || (talloc_array_length(conn->pending) != 0));
2335 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2337 return conn->smb2.server.capabilities;
2340 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2342 return conn->smb2.server.security_mode;
2345 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2347 return conn->smb2.server.max_trans_size;
2350 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2352 return conn->smb2.server.max_read_size;
2355 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2357 return conn->smb2.server.max_write_size;
2360 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2361 uint16_t max_credits)
2363 conn->smb2.max_credits = max_credits;
2366 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2368 static bool smb2cli_req_cancel(struct tevent_req *req)
2370 struct smbXcli_req_state *state =
2371 tevent_req_data(req,
2372 struct smbXcli_req_state);
2373 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2374 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2375 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2376 struct smbXcli_tcon *tcon = state->tcon;
2377 struct smbXcli_session *session = state->session;
2378 uint8_t *fixed = state->smb2.pad;
2379 uint16_t fixed_len = 4;
2380 struct tevent_req *subreq;
2381 struct smbXcli_req_state *substate;
2384 SSVAL(fixed, 0, 0x04);
2387 subreq = smb2cli_req_create(state, state->ev,
2395 if (subreq == NULL) {
2398 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2400 if (flags & SMB2_HDR_FLAG_ASYNC) {
2404 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2405 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2406 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2408 status = smb2cli_req_compound_submit(&subreq, 1);
2409 if (!NT_STATUS_IS_OK(status)) {
2410 TALLOC_FREE(subreq);
2414 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2419 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2421 /* we do not care about the result */
2422 TALLOC_FREE(subreq);
2425 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2426 struct tevent_context *ev,
2427 struct smbXcli_conn *conn,
2429 uint32_t additional_flags,
2430 uint32_t clear_flags,
2431 uint32_t timeout_msec,
2432 struct smbXcli_tcon *tcon,
2433 struct smbXcli_session *session,
2434 const uint8_t *fixed,
2439 struct tevent_req *req;
2440 struct smbXcli_req_state *state;
2445 req = tevent_req_create(mem_ctx, &state,
2446 struct smbXcli_req_state);
2453 state->session = session;
2457 uid = session->smb2->session_id;
2459 state->smb2.should_sign = session->smb2->should_sign;
2460 state->smb2.should_encrypt = session->smb2->should_encrypt;
2462 if (cmd == SMB2_OP_SESSSETUP &&
2463 session->smb2->signing_key.length != 0) {
2464 state->smb2.should_sign = true;
2467 if (cmd == SMB2_OP_SESSSETUP &&
2468 session->smb2_channel.signing_key.length == 0) {
2469 state->smb2.should_encrypt = false;
2474 tid = tcon->smb2.tcon_id;
2476 if (tcon->smb2.should_encrypt) {
2477 state->smb2.should_encrypt = true;
2481 if (state->smb2.should_encrypt) {
2482 state->smb2.should_sign = false;
2485 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2486 if (state->smb2.recv_iov == NULL) {
2491 flags |= additional_flags;
2492 flags &= ~clear_flags;
2494 state->smb2.fixed = fixed;
2495 state->smb2.fixed_len = fixed_len;
2496 state->smb2.dyn = dyn;
2497 state->smb2.dyn_len = dyn_len;
2499 if (state->smb2.should_encrypt) {
2500 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2501 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2504 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2505 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2506 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2507 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2508 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2509 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2510 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2513 case SMB2_OP_CANCEL:
2514 state->one_way = true;
2518 * If this is a dummy request, it will have
2519 * UINT64_MAX as message id.
2520 * If we send on break acknowledgement,
2521 * this gets overwritten later.
2523 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2527 if (timeout_msec > 0) {
2528 struct timeval endtime;
2530 endtime = timeval_current_ofs_msec(timeout_msec);
2531 if (!tevent_req_set_endtime(req, ev, endtime)) {
2539 void smb2cli_req_set_notify_async(struct tevent_req *req)
2541 struct smbXcli_req_state *state =
2542 tevent_req_data(req,
2543 struct smbXcli_req_state);
2545 state->smb2.notify_async = true;
2548 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2549 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2550 TALLOC_CTX *tmp_mem,
2553 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2556 struct smbXcli_req_state *state;
2557 struct tevent_req *subreq;
2559 int i, num_iov, nbt_len;
2562 * 1 for the nbt length
2563 * per request: TRANSFORM, HDR, fixed, dyn, padding
2564 * -1 because the last one does not need padding
2567 iov = talloc_array(reqs[0], struct iovec, 1 + 5*num_reqs - 1);
2569 return NT_STATUS_NO_MEMORY;
2575 for (i=0; i<num_reqs; i++) {
2585 const DATA_BLOB *signing_key = NULL;
2586 const DATA_BLOB *encryption_key = NULL;
2588 if (!tevent_req_is_in_progress(reqs[i])) {
2589 return NT_STATUS_INTERNAL_ERROR;
2592 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2594 if (!smbXcli_conn_is_connected(state->conn)) {
2595 return NT_STATUS_CONNECTION_DISCONNECTED;
2598 if ((state->conn->protocol != PROTOCOL_NONE) &&
2599 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2600 return NT_STATUS_REVISION_MISMATCH;
2603 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2604 if (opcode == SMB2_OP_CANCEL) {
2608 avail = UINT64_MAX - state->conn->smb2.mid;
2610 return NT_STATUS_CONNECTION_ABORTED;
2613 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2614 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2619 charge = MAX(state->smb2.credit_charge, charge);
2621 avail = MIN(avail, state->conn->smb2.cur_credits);
2622 if (avail < charge) {
2623 return NT_STATUS_INTERNAL_ERROR;
2627 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2628 credits = state->conn->smb2.max_credits -
2629 state->conn->smb2.cur_credits;
2631 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2635 mid = state->conn->smb2.mid;
2636 state->conn->smb2.mid += charge;
2637 state->conn->smb2.cur_credits -= charge;
2639 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2640 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2642 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2643 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2646 if (state->session) {
2648 * We prefer the channel signing key if it is
2651 if (state->smb2.should_sign) {
2652 signing_key = &state->session->smb2_channel.signing_key;
2656 * If it is a channel binding, we already have the main
2657 * signing key and try that one.
2659 if (signing_key && signing_key->length == 0) {
2660 signing_key = &state->session->smb2->signing_key;
2664 * If we do not have any session key yet, we skip the
2665 * signing of SMB2_OP_SESSSETUP requests.
2667 if (signing_key && signing_key->length == 0) {
2671 if (state->smb2.should_encrypt) {
2672 encryption_key = &state->session->smb2->encryption_key;
2676 if (encryption_key) {
2678 iov[num_iov].iov_base = state->smb2.transform;
2679 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2684 iov[num_iov].iov_base = state->smb2.hdr;
2685 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2688 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2689 iov[num_iov].iov_len = state->smb2.fixed_len;
2692 if (state->smb2.dyn != NULL) {
2693 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2694 iov[num_iov].iov_len = state->smb2.dyn_len;
2698 reqlen = sizeof(state->smb2.hdr);
2699 reqlen += state->smb2.fixed_len;
2700 reqlen += state->smb2.dyn_len;
2702 if (i < num_reqs-1) {
2703 if ((reqlen % 8) > 0) {
2704 uint8_t pad = 8 - (reqlen % 8);
2705 iov[num_iov].iov_base = state->smb2.pad;
2706 iov[num_iov].iov_len = pad;
2710 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2713 if (encryption_key) {
2718 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2719 state->session->smb2->nonce_low);
2720 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2721 state->session->smb2->nonce_high);
2723 state->session->smb2->nonce_low += 1;
2724 if (state->session->smb2->nonce_low == 0) {
2725 state->session->smb2->nonce_high += 1;
2726 state->session->smb2->nonce_low += 1;
2729 SBVAL(state->smb2.transform, SMB2_TF_MSG_SIZE,
2732 buf = talloc_array(iov, uint8_t, reqlen);
2734 return NT_STATUS_NO_MEMORY;
2737 reqlen += SMB2_TF_HDR_SIZE;
2740 * We copy the buffers before encrypting them,
2741 * this is at least currently needed for the
2742 * to keep state->smb2.hdr.
2744 * Also the callers may expect there buffers
2747 for (vi = hdr_iov; vi < num_iov; vi++) {
2748 struct iovec *v = &iov[vi];
2749 const uint8_t *o = (const uint8_t *)v->iov_base;
2751 memcpy(buf, o, v->iov_len);
2752 v->iov_base = (void *)buf;
2756 status = smb2_signing_encrypt_pdu(*encryption_key,
2757 state->session->conn->protocol,
2758 &iov[tf_iov], num_iov - tf_iov);
2759 if (!NT_STATUS_IS_OK(status)) {
2762 } else if (signing_key) {
2765 status = smb2_signing_sign_pdu(*signing_key,
2766 state->session->conn->protocol,
2767 &iov[hdr_iov], num_iov - hdr_iov);
2768 if (!NT_STATUS_IS_OK(status)) {
2775 ret = smbXcli_req_set_pending(reqs[i]);
2777 return NT_STATUS_NO_MEMORY;
2781 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2782 _smb_setlen_tcp(state->length_hdr, nbt_len);
2783 iov[0].iov_base = state->length_hdr;
2784 iov[0].iov_len = sizeof(state->length_hdr);
2786 if (state->conn->dispatch_incoming == NULL) {
2787 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2790 subreq = writev_send(state, state->ev, state->conn->outgoing,
2791 state->conn->write_fd, false, iov, num_iov);
2792 if (subreq == NULL) {
2793 return NT_STATUS_NO_MEMORY;
2795 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2796 return NT_STATUS_OK;
2799 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2801 struct smbXcli_req_state *state =
2802 tevent_req_data(req,
2803 struct smbXcli_req_state);
2805 state->smb2.credit_charge = charge;
2808 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2809 struct tevent_context *ev,
2810 struct smbXcli_conn *conn,
2812 uint32_t additional_flags,
2813 uint32_t clear_flags,
2814 uint32_t timeout_msec,
2815 struct smbXcli_tcon *tcon,
2816 struct smbXcli_session *session,
2817 const uint8_t *fixed,
2822 struct tevent_req *req;
2825 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2826 additional_flags, clear_flags,
2829 fixed, fixed_len, dyn, dyn_len);
2833 if (!tevent_req_is_in_progress(req)) {
2834 return tevent_req_post(req, ev);
2836 status = smb2cli_req_compound_submit(&req, 1);
2837 if (tevent_req_nterror(req, status)) {
2838 return tevent_req_post(req, ev);
2843 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2845 struct tevent_req *req =
2846 tevent_req_callback_data(subreq,
2848 struct smbXcli_req_state *state =
2849 tevent_req_data(req,
2850 struct smbXcli_req_state);
2854 nwritten = writev_recv(subreq, &err);
2855 TALLOC_FREE(subreq);
2856 if (nwritten == -1) {
2857 /* here, we need to notify all pending requests */
2858 NTSTATUS status = map_nt_error_from_unix_common(err);
2859 smbXcli_conn_disconnect(state->conn, status);
2864 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
2867 TALLOC_CTX *mem_ctx,
2868 struct iovec **piov, int *pnum_iov)
2873 uint8_t *first_hdr = buf;
2875 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2877 return NT_STATUS_NO_MEMORY;
2880 while (taken < buflen) {
2883 size_t len = buflen - taken;
2884 uint8_t *hdr = first_hdr + taken;
2887 size_t next_command_ofs;
2889 struct iovec *iov_tmp;
2892 DEBUG(10, ("%d bytes left, expected at least %d\n",
2896 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
2897 struct smbXcli_session *s;
2899 struct iovec tf_iov[2];
2902 if (len < SMB2_TF_HDR_SIZE) {
2903 DEBUG(10, ("%d bytes left, expected at least %d\n",
2904 (int)len, SMB2_TF_HDR_SIZE));
2908 tf_len = SMB2_TF_HDR_SIZE;
2911 hdr = first_hdr + taken;
2912 len = IVAL(tf, SMB2_TF_MSG_SIZE);
2913 uid = BVAL(tf, SMB2_TF_SESSION_ID);
2916 for (; s; s = s->next) {
2917 if (s->smb2->session_id != uid) {
2924 DEBUG(10, ("unknown session_id %llu\n",
2925 (unsigned long long)uid));
2929 tf_iov[0].iov_base = (void *)tf;
2930 tf_iov[0].iov_len = tf_len;
2931 tf_iov[1].iov_base = (void *)hdr;
2932 tf_iov[1].iov_len = len;
2934 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
2937 if (!NT_STATUS_IS_OK(status)) {
2944 * We need the header plus the body length field
2947 if (len < SMB2_HDR_BODY + 2) {
2948 DEBUG(10, ("%d bytes left, expected at least %d\n",
2949 (int)len, SMB2_HDR_BODY));
2952 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2953 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2957 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2958 DEBUG(10, ("Got HDR len %d, expected %d\n",
2959 SVAL(hdr, 4), SMB2_HDR_BODY));
2964 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2965 body_size = SVAL(hdr, SMB2_HDR_BODY);
2967 if (next_command_ofs != 0) {
2968 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2971 if (next_command_ofs > full_size) {
2974 if (tf && next_command_ofs < len) {
2977 full_size = next_command_ofs;
2979 if (body_size < 2) {
2982 body_size &= 0xfffe;
2984 if (body_size > (full_size - SMB2_HDR_BODY)) {
2988 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2990 if (iov_tmp == NULL) {
2992 return NT_STATUS_NO_MEMORY;
2995 cur = &iov[num_iov];
2998 cur[0].iov_base = tf;
2999 cur[0].iov_len = tf_len;
3000 cur[1].iov_base = hdr;
3001 cur[1].iov_len = SMB2_HDR_BODY;
3002 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3003 cur[2].iov_len = body_size;
3004 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3005 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3011 *pnum_iov = num_iov;
3012 return NT_STATUS_OK;
3016 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3019 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3022 size_t num_pending = talloc_array_length(conn->pending);
3025 for (i=0; i<num_pending; i++) {
3026 struct tevent_req *req = conn->pending[i];
3027 struct smbXcli_req_state *state =
3028 tevent_req_data(req,
3029 struct smbXcli_req_state);
3031 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3038 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3039 TALLOC_CTX *tmp_mem,
3042 struct tevent_req *req;
3043 struct smbXcli_req_state *state = NULL;
3048 struct smbXcli_session *last_session = NULL;
3049 size_t inbuf_len = smb_len_tcp(inbuf);
3051 status = smb2cli_inbuf_parse_compound(conn,
3052 inbuf + NBT_HDR_SIZE,
3056 if (!NT_STATUS_IS_OK(status)) {
3060 for (i=0; i<num_iov; i+=4) {
3061 uint8_t *inbuf_ref = NULL;
3062 struct iovec *cur = &iov[i];
3063 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3064 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3065 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3066 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3067 uint16_t req_opcode;
3069 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3070 uint32_t new_credits;
3071 struct smbXcli_session *session = NULL;
3072 const DATA_BLOB *signing_key = NULL;
3074 new_credits = conn->smb2.cur_credits;
3075 new_credits += credits;
3076 if (new_credits > UINT16_MAX) {
3077 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3079 conn->smb2.cur_credits += credits;
3081 req = smb2cli_conn_find_pending(conn, mid);
3083 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3085 state = tevent_req_data(req, struct smbXcli_req_state);
3087 state->smb2.got_async = false;
3089 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3090 if (opcode != req_opcode) {
3091 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3093 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3095 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3096 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3099 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3100 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3101 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3102 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3105 * async interim responses are not signed,
3106 * even if the SMB2_HDR_FLAG_SIGNED flag
3109 req_flags |= SMB2_HDR_FLAG_ASYNC;
3110 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3111 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3113 if (state->smb2.notify_async) {
3114 state->smb2.got_async = true;
3115 tevent_req_defer_callback(req, state->ev);
3116 tevent_req_notify_callback(req);
3121 session = state->session;
3122 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3123 session = last_session;
3125 last_session = session;
3127 if (state->smb2.should_sign) {
3128 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3129 return NT_STATUS_ACCESS_DENIED;
3133 if (flags & SMB2_HDR_FLAG_SIGNED) {
3134 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3136 if (session == NULL) {
3137 struct smbXcli_session *s;
3139 s = state->conn->sessions;
3140 for (; s; s = s->next) {
3141 if (s->smb2->session_id != uid) {
3150 if (session == NULL) {
3151 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3154 last_session = session;
3155 signing_key = &session->smb2_channel.signing_key;
3158 if (opcode == SMB2_OP_SESSSETUP) {
3160 * We prefer the channel signing key, if it is
3163 * If we do not have a channel signing key yet,
3164 * we try the main signing key, if it is not
3165 * the final response.
3167 if (signing_key && signing_key->length == 0 &&
3168 !NT_STATUS_IS_OK(status)) {
3169 signing_key = &session->smb2->signing_key;
3172 if (signing_key && signing_key->length == 0) {
3174 * If we do not have a session key to
3175 * verify the signature, we defer the
3176 * signing check to the caller.
3178 * The caller gets NT_STATUS_OK, it
3180 * smb2cli_session_set_session_key()
3182 * smb2cli_session_set_channel_key()
3183 * which will check the signature
3184 * with the channel signing key.
3190 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3192 * if the server returns NT_STATUS_USER_SESSION_DELETED
3193 * the response is not signed and we should
3194 * propagate the NT_STATUS_USER_SESSION_DELETED
3195 * status to the caller.
3198 } else if (state->smb2.should_encrypt) {
3199 if (cur[0].iov_len != SMB2_TF_HDR_SIZE) {
3200 return NT_STATUS_ACCESS_DENIED;
3204 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3205 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3206 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3208 * if the server returns
3209 * NT_STATUS_NETWORK_NAME_DELETED
3210 * NT_STATUS_FILE_CLOSED
3211 * NT_STATUS_INVALID_PARAMETER
3212 * the response might not be signed
3213 * as this happens before the signing checks.
3215 * If server echos the signature (or all zeros)
3216 * we should report the status from the server
3222 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3223 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3226 state->smb2.signing_skipped = true;
3232 static const uint8_t zeros[16];
3234 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3238 state->smb2.signing_skipped = true;
3245 status = smb2_signing_check_pdu(*signing_key,
3246 state->conn->protocol,
3248 if (!NT_STATUS_IS_OK(status)) {
3250 * If the signing check fails, we disconnect
3257 smbXcli_req_unset_pending(req);
3260 * There might be more than one response
3261 * we need to defer the notifications
3263 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3268 tevent_req_defer_callback(req, state->ev);
3272 * Note: here we use talloc_reference() in a way
3273 * that does not expose it to the caller.
3275 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3276 if (tevent_req_nomem(inbuf_ref, req)) {
3280 /* copy the related buffers */
3281 state->smb2.recv_iov[0] = cur[1];
3282 state->smb2.recv_iov[1] = cur[2];
3283 state->smb2.recv_iov[2] = cur[3];
3285 tevent_req_done(req);
3289 return NT_STATUS_RETRY;
3292 return NT_STATUS_OK;
3295 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3296 struct iovec **piov,
3297 const struct smb2cli_req_expected_response *expected,
3298 size_t num_expected)
3300 struct smbXcli_req_state *state =
3301 tevent_req_data(req,
3302 struct smbXcli_req_state);
3305 bool found_status = false;
3306 bool found_size = false;
3313 if (state->smb2.got_async) {
3314 return STATUS_PENDING;
3317 if (tevent_req_is_nterror(req, &status)) {
3318 for (i=0; i < num_expected; i++) {
3319 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3320 found_status = true;
3326 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3332 if (num_expected == 0) {
3333 found_status = true;
3337 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3338 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3340 for (i=0; i < num_expected; i++) {
3341 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3345 found_status = true;
3346 if (expected[i].body_size == 0) {
3351 if (expected[i].body_size == body_size) {
3357 if (!found_status) {
3361 if (state->smb2.signing_skipped) {
3362 if (num_expected > 0) {
3363 return NT_STATUS_ACCESS_DENIED;
3365 if (!NT_STATUS_IS_ERR(status)) {
3366 return NT_STATUS_ACCESS_DENIED;
3371 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3375 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3381 static const struct {
3382 enum protocol_types proto;
3383 const char *smb1_name;
3384 } smb1cli_prots[] = {
3385 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3386 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3387 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3388 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3389 {PROTOCOL_LANMAN2, "LM1.2X002"},
3390 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3391 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3392 {PROTOCOL_LANMAN2, "Samba"},
3393 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3394 {PROTOCOL_NT1, "NT LM 0.12"},
3395 {PROTOCOL_SMB2_02, "SMB 2.002"},
3396 {PROTOCOL_SMB2_10, "SMB 2.???"},
3399 static const struct {
3400 enum protocol_types proto;
3401 uint16_t smb2_dialect;
3402 } smb2cli_prots[] = {
3403 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3404 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3405 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3406 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3407 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3410 struct smbXcli_negprot_state {
3411 struct smbXcli_conn *conn;
3412 struct tevent_context *ev;
3413 uint32_t timeout_msec;
3414 enum protocol_types min_protocol;
3415 enum protocol_types max_protocol;
3419 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3423 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3424 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3425 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3426 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3427 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3428 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3432 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3433 struct tevent_context *ev,
3434 struct smbXcli_conn *conn,
3435 uint32_t timeout_msec,
3436 enum protocol_types min_protocol,
3437 enum protocol_types max_protocol)
3439 struct tevent_req *req, *subreq;
3440 struct smbXcli_negprot_state *state;
3442 req = tevent_req_create(mem_ctx, &state,
3443 struct smbXcli_negprot_state);
3449 state->timeout_msec = timeout_msec;
3450 state->min_protocol = min_protocol;
3451 state->max_protocol = max_protocol;
3453 if (min_protocol == PROTOCOL_NONE) {
3454 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3455 return tevent_req_post(req, ev);
3458 if (max_protocol == PROTOCOL_NONE) {
3459 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3460 return tevent_req_post(req, ev);
3463 if (min_protocol > max_protocol) {
3464 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3465 return tevent_req_post(req, ev);
3468 if ((min_protocol < PROTOCOL_SMB2_02) &&
3469 (max_protocol < PROTOCOL_SMB2_02)) {
3473 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3475 subreq = smbXcli_negprot_smb1_subreq(state);
3476 if (tevent_req_nomem(subreq, req)) {
3477 return tevent_req_post(req, ev);
3479 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3483 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3484 (max_protocol >= PROTOCOL_SMB2_02)) {
3488 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3490 subreq = smbXcli_negprot_smb2_subreq(state);
3491 if (tevent_req_nomem(subreq, req)) {
3492 return tevent_req_post(req, ev);
3494 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3499 * We send an SMB1 negprot with the SMB2 dialects
3500 * and expect a SMB1 or a SMB2 response.
3502 * smbXcli_negprot_dispatch_incoming() will fix the
3503 * callback to match protocol of the response.
3505 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3507 subreq = smbXcli_negprot_smb1_subreq(state);
3508 if (tevent_req_nomem(subreq, req)) {
3509 return tevent_req_post(req, ev);
3511 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3515 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3517 struct tevent_req *req =
3518 tevent_req_callback_data(subreq,
3523 * we just want the low level error
3525 status = tevent_req_simple_recv_ntstatus(subreq);
3526 TALLOC_FREE(subreq);
3527 if (tevent_req_nterror(req, status)) {
3531 /* this should never happen */
3532 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3535 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3538 DATA_BLOB bytes = data_blob_null;
3542 /* setup the protocol strings */
3543 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3547 if (smb1cli_prots[i].proto < state->min_protocol) {
3551 if (smb1cli_prots[i].proto > state->max_protocol) {
3555 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3561 * We now it is already ascii and
3562 * we want NULL termination.
3564 ok = data_blob_append(state, &bytes,
3565 smb1cli_prots[i].smb1_name,
3566 strlen(smb1cli_prots[i].smb1_name)+1);
3572 smb1cli_req_flags(state->max_protocol,
3573 state->conn->smb1.client.capabilities,
3578 return smb1cli_req_send(state, state->ev, state->conn,
3582 state->timeout_msec,
3583 0xFFFE, 0, NULL, /* pid, tid, session */
3584 0, NULL, /* wct, vwv */
3585 bytes.length, bytes.data);
3588 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3590 struct tevent_req *req =
3591 tevent_req_callback_data(subreq,
3593 struct smbXcli_negprot_state *state =
3594 tevent_req_data(req,
3595 struct smbXcli_negprot_state);
3596 struct smbXcli_conn *conn = state->conn;
3597 struct iovec *recv_iov = NULL;
3606 size_t num_prots = 0;
3608 uint32_t client_capabilities = conn->smb1.client.capabilities;
3609 uint32_t both_capabilities;
3610 uint32_t server_capabilities = 0;
3611 uint32_t capabilities;
3612 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3613 uint32_t server_max_xmit = 0;
3615 uint32_t server_max_mux = 0;
3616 uint16_t server_security_mode = 0;
3617 uint32_t server_session_key = 0;
3618 bool server_readbraw = false;
3619 bool server_writebraw = false;
3620 bool server_lockread = false;
3621 bool server_writeunlock = false;
3622 struct GUID server_guid = GUID_zero();
3623 DATA_BLOB server_gss_blob = data_blob_null;
3624 uint8_t server_challenge[8];
3625 char *server_workgroup = NULL;
3626 char *server_name = NULL;
3627 int server_time_zone = 0;
3628 NTTIME server_system_time = 0;
3629 static const struct smb1cli_req_expected_response expected[] = {
3631 .status = NT_STATUS_OK,
3632 .wct = 0x11, /* NT1 */
3635 .status = NT_STATUS_OK,
3636 .wct = 0x0D, /* LM */
3639 .status = NT_STATUS_OK,
3640 .wct = 0x01, /* CORE */
3644 ZERO_STRUCT(server_challenge);
3646 status = smb1cli_req_recv(subreq, state,
3651 NULL, /* pvwv_offset */
3654 NULL, /* pbytes_offset */
3656 expected, ARRAY_SIZE(expected));
3657 TALLOC_FREE(subreq);
3658 if (tevent_req_nterror(req, status)) {
3662 flags = CVAL(inhdr, HDR_FLG);
3664 protnum = SVAL(vwv, 0);
3666 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3667 if (smb1cli_prots[i].proto < state->min_protocol) {
3671 if (smb1cli_prots[i].proto > state->max_protocol) {
3675 if (protnum != num_prots) {
3680 conn->protocol = smb1cli_prots[i].proto;
3684 if (conn->protocol == PROTOCOL_NONE) {
3685 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3689 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3690 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3691 "and the selected protocol level doesn't support it.\n"));
3692 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3696 if (flags & FLAG_SUPPORT_LOCKREAD) {
3697 server_lockread = true;
3698 server_writeunlock = true;
3701 if (conn->protocol >= PROTOCOL_NT1) {
3702 const char *client_signing = NULL;
3703 bool server_mandatory = false;
3704 bool server_allowed = false;
3705 const char *server_signing = NULL;
3710 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3715 server_security_mode = CVAL(vwv + 1, 0);
3716 server_max_mux = SVAL(vwv + 1, 1);
3717 server_max_xmit = IVAL(vwv + 3, 1);
3718 server_session_key = IVAL(vwv + 7, 1);
3719 server_time_zone = SVALS(vwv + 15, 1);
3720 server_time_zone *= 60;
3721 /* this time arrives in real GMT */
3722 server_system_time = BVAL(vwv + 11, 1);
3723 server_capabilities = IVAL(vwv + 9, 1);
3725 key_len = CVAL(vwv + 16, 1);
3727 if (server_capabilities & CAP_RAW_MODE) {
3728 server_readbraw = true;
3729 server_writebraw = true;
3731 if (server_capabilities & CAP_LOCK_AND_READ) {
3732 server_lockread = true;
3735 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3736 DATA_BLOB blob1, blob2;
3738 if (num_bytes < 16) {
3739 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3743 blob1 = data_blob_const(bytes, 16);
3744 status = GUID_from_data_blob(&blob1, &server_guid);
3745 if (tevent_req_nterror(req, status)) {
3749 blob1 = data_blob_const(bytes+16, num_bytes-16);
3750 blob2 = data_blob_dup_talloc(state, blob1);
3751 if (blob1.length > 0 &&
3752 tevent_req_nomem(blob2.data, req)) {
3755 server_gss_blob = blob2;
3757 DATA_BLOB blob1, blob2;
3759 if (num_bytes < key_len) {
3760 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3764 if (key_len != 0 && key_len != 8) {
3765 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3770 memcpy(server_challenge, bytes, 8);
3773 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3774 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3775 if (blob1.length > 0) {
3778 len = utf16_len_n(blob1.data,
3782 ok = convert_string_talloc(state,
3790 status = map_nt_error_from_unix_common(errno);
3791 tevent_req_nterror(req, status);
3796 blob2.data += blob1.length;
3797 blob2.length -= blob1.length;
3798 if (blob2.length > 0) {
3801 len = utf16_len_n(blob1.data,
3805 ok = convert_string_talloc(state,
3813 status = map_nt_error_from_unix_common(errno);
3814 tevent_req_nterror(req, status);
3820 client_signing = "disabled";
3821 if (conn->allow_signing) {
3822 client_signing = "allowed";
3824 if (conn->mandatory_signing) {
3825 client_signing = "required";
3828 server_signing = "not supported";
3829 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3830 server_signing = "supported";
3831 server_allowed = true;
3833 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3834 server_signing = "required";
3835 server_mandatory = true;
3838 ok = smb_signing_set_negotiated(conn->smb1.signing,
3842 DEBUG(1,("cli_negprot: SMB signing is required, "
3843 "but client[%s] and server[%s] mismatch\n",
3844 client_signing, server_signing));
3845 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3849 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3855 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3859 server_security_mode = SVAL(vwv + 1, 0);
3860 server_max_xmit = SVAL(vwv + 2, 0);
3861 server_max_mux = SVAL(vwv + 3, 0);
3862 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3863 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3864 server_session_key = IVAL(vwv + 6, 0);
3865 server_time_zone = SVALS(vwv + 10, 0);
3866 server_time_zone *= 60;
3867 /* this time is converted to GMT by make_unix_date */
3868 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3869 unix_to_nt_time(&server_system_time, t);
3870 key_len = SVAL(vwv + 11, 0);
3872 if (num_bytes < key_len) {
3873 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3877 if (key_len != 0 && key_len != 8) {
3878 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3883 memcpy(server_challenge, bytes, 8);
3886 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3887 if (blob1.length > 0) {
3891 len = utf16_len_n(blob1.data,
3895 ok = convert_string_talloc(state,
3903 status = map_nt_error_from_unix_common(errno);
3904 tevent_req_nterror(req, status);
3910 /* the old core protocol */
3911 server_time_zone = get_time_zone(time(NULL));
3912 server_max_xmit = 1024;
3916 if (server_max_xmit < 1024) {
3917 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3921 if (server_max_mux < 1) {
3922 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3927 * Now calculate the negotiated capabilities
3928 * based on the mask for:
3929 * - client only flags
3930 * - flags used in both directions
3931 * - server only flags
3933 both_capabilities = client_capabilities & server_capabilities;
3934 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3935 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3936 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3938 max_xmit = MIN(client_max_xmit, server_max_xmit);
3940 conn->smb1.server.capabilities = server_capabilities;
3941 conn->smb1.capabilities = capabilities;
3943 conn->smb1.server.max_xmit = server_max_xmit;
3944 conn->smb1.max_xmit = max_xmit;
3946 conn->smb1.server.max_mux = server_max_mux;
3948 conn->smb1.server.security_mode = server_security_mode;
3950 conn->smb1.server.readbraw = server_readbraw;
3951 conn->smb1.server.writebraw = server_writebraw;
3952 conn->smb1.server.lockread = server_lockread;
3953 conn->smb1.server.writeunlock = server_writeunlock;
3955 conn->smb1.server.session_key = server_session_key;
3957 talloc_steal(conn, server_gss_blob.data);
3958 conn->smb1.server.gss_blob = server_gss_blob;
3959 conn->smb1.server.guid = server_guid;
3960 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3961 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3962 conn->smb1.server.name = talloc_move(conn, &server_name);
3964 conn->smb1.server.time_zone = server_time_zone;
3965 conn->smb1.server.system_time = server_system_time;
3967 tevent_req_done(req);
3970 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
3974 uint16_t dialect_count = 0;
3976 buf = state->smb2.dyn;
3977 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3978 if (smb2cli_prots[i].proto < state->min_protocol) {
3982 if (smb2cli_prots[i].proto > state->max_protocol) {
3986 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
3990 buf = state->smb2.fixed;
3992 SSVAL(buf, 2, dialect_count);
3993 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
3994 SSVAL(buf, 6, 0); /* Reserved */
3995 if (state->max_protocol >= PROTOCOL_SMB2_22) {
3996 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
3998 SIVAL(buf, 8, 0); /* Capabilities */
4000 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4004 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4006 if (!NT_STATUS_IS_OK(status)) {
4009 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4011 memset(buf+12, 0, 16); /* ClientGuid */
4013 SBVAL(buf, 28, 0); /* ClientStartTime */
4015 return smb2cli_req_send(state, state->ev,
4016 state->conn, SMB2_OP_NEGPROT,
4018 state->timeout_msec,
4019 NULL, NULL, /* tcon, session */
4020 state->smb2.fixed, sizeof(state->smb2.fixed),
4021 state->smb2.dyn, dialect_count*2);
4024 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4026 struct tevent_req *req =
4027 tevent_req_callback_data(subreq,
4029 struct smbXcli_negprot_state *state =
4030 tevent_req_data(req,
4031 struct smbXcli_negprot_state);
4032 struct smbXcli_conn *conn = state->conn;
4033 size_t security_offset, security_length;
4039 uint16_t dialect_revision;
4040 static const struct smb2cli_req_expected_response expected[] = {
4042 .status = NT_STATUS_OK,
4047 status = smb2cli_req_recv(subreq, state, &iov,
4048 expected, ARRAY_SIZE(expected));
4049 TALLOC_FREE(subreq);
4050 if (tevent_req_nterror(req, status)) {
4054 body = (uint8_t *)iov[1].iov_base;
4056 dialect_revision = SVAL(body, 4);
4058 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4059 if (smb2cli_prots[i].proto < state->min_protocol) {
4063 if (smb2cli_prots[i].proto > state->max_protocol) {
4067 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4071 conn->protocol = smb2cli_prots[i].proto;
4075 if (conn->protocol == PROTOCOL_NONE) {
4076 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4077 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4081 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4082 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4086 /* make sure we do not loop forever */
4087 state->min_protocol = PROTOCOL_SMB2_02;
4090 * send a SMB2 negprot, in order to negotiate
4093 subreq = smbXcli_negprot_smb2_subreq(state);
4094 if (tevent_req_nomem(subreq, req)) {
4097 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4101 conn->smb2.server.security_mode = SVAL(body, 2);
4103 blob = data_blob_const(body + 8, 16);
4104 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4105 if (tevent_req_nterror(req, status)) {
4109 conn->smb2.server.capabilities = IVAL(body, 24);
4110 conn->smb2.server.max_trans_size= IVAL(body, 28);
4111 conn->smb2.server.max_read_size = IVAL(body, 32);
4112 conn->smb2.server.max_write_size= IVAL(body, 36);
4113 conn->smb2.server.system_time = BVAL(body, 40);
4114 conn->smb2.server.start_time = BVAL(body, 48);
4116 security_offset = SVAL(body, 56);
4117 security_length = SVAL(body, 58);
4119 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4120 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4124 if (security_length > iov[2].iov_len) {
4125 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4129 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4132 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4136 tevent_req_done(req);
4139 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4140 TALLOC_CTX *tmp_mem,
4143 size_t num_pending = talloc_array_length(conn->pending);
4144 struct tevent_req *subreq;
4145 struct smbXcli_req_state *substate;
4146 struct tevent_req *req;
4147 uint32_t protocol_magic;
4148 size_t inbuf_len = smb_len_nbt(inbuf);
4150 if (num_pending != 1) {
4151 return NT_STATUS_INTERNAL_ERROR;
4154 if (inbuf_len < 4) {
4155 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4158 subreq = conn->pending[0];
4159 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4160 req = tevent_req_callback_data(subreq, struct tevent_req);
4162 protocol_magic = IVAL(inbuf, 4);
4164 switch (protocol_magic) {
4166 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4167 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4168 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4171 if (substate->smb2.recv_iov == NULL) {
4173 * For the SMB1 negprot we have move it.
4175 substate->smb2.recv_iov = substate->smb1.recv_iov;
4176 substate->smb1.recv_iov = NULL;
4180 * we got an SMB2 answer, which consumed sequence number 0
4181 * so we need to use 1 as the next one
4184 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4185 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4186 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4189 DEBUG(10, ("Got non-SMB PDU\n"));
4190 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4193 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4195 return tevent_req_simple_recv_ntstatus(req);
4198 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4199 uint32_t timeout_msec,
4200 enum protocol_types min_protocol,
4201 enum protocol_types max_protocol)
4203 TALLOC_CTX *frame = talloc_stackframe();
4204 struct tevent_context *ev;
4205 struct tevent_req *req;
4206 NTSTATUS status = NT_STATUS_NO_MEMORY;
4209 if (smbXcli_conn_has_async_calls(conn)) {
4211 * Can't use sync call while an async call is in flight
4213 status = NT_STATUS_INVALID_PARAMETER_MIX;
4216 ev = tevent_context_init(frame);
4220 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4221 min_protocol, max_protocol);
4225 ok = tevent_req_poll(req, ev);
4227 status = map_nt_error_from_unix_common(errno);
4230 status = smbXcli_negprot_recv(req);
4236 static int smbXcli_session_destructor(struct smbXcli_session *session)
4238 if (session->conn == NULL) {
4242 DLIST_REMOVE(session->conn->sessions, session);
4246 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4247 struct smbXcli_conn *conn)
4249 struct smbXcli_session *session;
4251 session = talloc_zero(mem_ctx, struct smbXcli_session);
4252 if (session == NULL) {
4255 session->smb2 = talloc_zero(session, struct smb2cli_session);
4256 if (session->smb2 == NULL) {
4257 talloc_free(session);
4260 talloc_set_destructor(session, smbXcli_session_destructor);
4262 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4263 session->conn = conn;
4268 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4270 return session->smb1.session_id;
4273 void smb1cli_session_set_id(struct smbXcli_session *session,
4274 uint16_t session_id)
4276 session->smb1.session_id = session_id;
4279 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4281 struct smbXcli_conn *conn = session->conn;
4282 uint8_t security_mode = 0;
4285 return security_mode;
4288 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4289 if (conn->mandatory_signing) {
4290 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4293 return security_mode;
4296 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4298 return session->smb2->session_id;
4301 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4303 return session->smb2->session_flags;
4306 NTSTATUS smb2cli_session_application_key(struct smbXcli_session *session,
4307 TALLOC_CTX *mem_ctx,
4310 *key = data_blob_null;
4312 if (session->smb2->application_key.length == 0) {
4313 return NT_STATUS_NO_USER_SESSION_KEY;
4316 *key = data_blob_dup_talloc(mem_ctx, session->smb2->application_key);
4317 if (key->data == NULL) {
4318 return NT_STATUS_NO_MEMORY;
4321 return NT_STATUS_OK;
4324 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4325 uint64_t session_id,
4326 uint16_t session_flags)
4328 session->smb2->session_id = session_id;
4329 session->smb2->session_flags = session_flags;
4332 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4333 const DATA_BLOB _session_key,
4334 const struct iovec *recv_iov)
4336 struct smbXcli_conn *conn = session->conn;
4337 uint16_t no_sign_flags;
4338 uint8_t session_key[16];
4342 return NT_STATUS_INVALID_PARAMETER_MIX;
4345 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4347 if (session->smb2->session_flags & no_sign_flags) {
4348 session->smb2->should_sign = false;
4349 return NT_STATUS_OK;
4352 if (session->smb2->signing_key.length != 0) {
4353 return NT_STATUS_INVALID_PARAMETER_MIX;
4356 ZERO_STRUCT(session_key);
4357 memcpy(session_key, _session_key.data,
4358 MIN(_session_key.length, sizeof(session_key)));
4360 session->smb2->signing_key = data_blob_talloc(session,
4362 sizeof(session_key));
4363 if (session->smb2->signing_key.data == NULL) {
4364 ZERO_STRUCT(session_key);
4365 return NT_STATUS_NO_MEMORY;
4368 if (conn->protocol >= PROTOCOL_SMB2_24) {
4369 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4370 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4372 smb2_key_derivation(session_key, sizeof(session_key),
4373 label.data, label.length,
4374 context.data, context.length,
4375 session->smb2->signing_key.data);
4378 session->smb2->encryption_key = data_blob_dup_talloc(session,
4379 session->smb2->signing_key);
4380 if (session->smb2->encryption_key.data == NULL) {
4381 ZERO_STRUCT(session_key);
4382 return NT_STATUS_NO_MEMORY;
4385 if (conn->protocol >= PROTOCOL_SMB2_24) {
4386 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4387 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4389 smb2_key_derivation(session_key, sizeof(session_key),
4390 label.data, label.length,
4391 context.data, context.length,
4392 session->smb2->encryption_key.data);
4395 session->smb2->decryption_key = data_blob_dup_talloc(session,
4396 session->smb2->signing_key);
4397 if (session->smb2->decryption_key.data == NULL) {
4398 ZERO_STRUCT(session_key);
4399 return NT_STATUS_NO_MEMORY;
4402 if (conn->protocol >= PROTOCOL_SMB2_24) {
4403 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4404 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4406 smb2_key_derivation(session_key, sizeof(session_key),
4407 label.data, label.length,
4408 context.data, context.length,
4409 session->smb2->decryption_key.data);
4412 session->smb2->application_key = data_blob_dup_talloc(session,
4413 session->smb2->signing_key);
4414 if (session->smb2->application_key.data == NULL) {
4415 ZERO_STRUCT(session_key);
4416 return NT_STATUS_NO_MEMORY;
4419 if (conn->protocol >= PROTOCOL_SMB2_24) {
4420 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4421 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4423 smb2_key_derivation(session_key, sizeof(session_key),
4424 label.data, label.length,
4425 context.data, context.length,
4426 session->smb2->application_key.data);
4428 ZERO_STRUCT(session_key);
4430 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4431 session->smb2->signing_key);
4432 if (session->smb2_channel.signing_key.data == NULL) {
4433 return NT_STATUS_NO_MEMORY;
4436 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4437 session->conn->protocol,
4439 if (!NT_STATUS_IS_OK(status)) {
4443 session->smb2->should_sign = false;
4444 session->smb2->should_encrypt = false;
4446 if (conn->desire_signing) {
4447 session->smb2->should_sign = true;
4450 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4451 session->smb2->should_sign = true;
4454 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4455 session->smb2->should_encrypt = true;
4458 if (conn->protocol < PROTOCOL_SMB2_24) {
4459 session->smb2->should_encrypt = false;
4462 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4463 session->smb2->should_encrypt = false;
4466 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4467 sizeof(session->smb2->nonce_high));
4468 session->smb2->nonce_low = 1;
4470 return NT_STATUS_OK;
4473 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4474 struct smbXcli_session *session1,
4475 struct smbXcli_conn *conn,
4476 struct smbXcli_session **_session2)
4478 struct smbXcli_session *session2;
4480 if (session1->smb2->signing_key.length == 0) {
4481 return NT_STATUS_INVALID_PARAMETER_MIX;
4485 return NT_STATUS_INVALID_PARAMETER_MIX;
4488 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4489 if (session2 == NULL) {
4490 return NT_STATUS_NO_MEMORY;
4492 session2->smb2 = talloc_reference(session2, session1->smb2);
4493 if (session2->smb2 == NULL) {
4494 talloc_free(session2);
4495 return NT_STATUS_NO_MEMORY;
4498 talloc_set_destructor(session2, smbXcli_session_destructor);
4499 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4500 session2->conn = conn;
4502 *_session2 = session2;
4503 return NT_STATUS_OK;
4506 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4507 const DATA_BLOB _channel_key,
4508 const struct iovec *recv_iov)
4510 struct smbXcli_conn *conn = session->conn;
4511 uint8_t channel_key[16];
4515 return NT_STATUS_INVALID_PARAMETER_MIX;
4518 if (session->smb2_channel.signing_key.length != 0) {
4519 return NT_STATUS_INVALID_PARAMETER_MIX;
4522 ZERO_STRUCT(channel_key);
4523 memcpy(channel_key, _channel_key.data,
4524 MIN(_channel_key.length, sizeof(channel_key)));
4526 session->smb2_channel.signing_key = data_blob_talloc(session,
4528 sizeof(channel_key));
4529 if (session->smb2_channel.signing_key.data == NULL) {
4530 ZERO_STRUCT(channel_key);
4531 return NT_STATUS_NO_MEMORY;
4534 if (conn->protocol >= PROTOCOL_SMB2_24) {
4535 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4536 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4538 smb2_key_derivation(channel_key, sizeof(channel_key),
4539 label.data, label.length,
4540 context.data, context.length,
4541 session->smb2_channel.signing_key.data);
4543 ZERO_STRUCT(channel_key);
4545 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4546 session->conn->protocol,
4548 if (!NT_STATUS_IS_OK(status)) {
4552 return NT_STATUS_OK;
4555 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4557 struct smbXcli_tcon *tcon;
4559 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4567 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4569 return tcon->smb1.tcon_id;
4572 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4574 tcon->smb1.tcon_id = tcon_id;
4577 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
4579 uint16_t optional_support,
4580 uint32_t maximal_access,
4581 uint32_t guest_maximal_access,
4582 const char *service,
4583 const char *fs_type)
4585 tcon->smb1.tcon_id = tcon_id;
4586 tcon->smb1.optional_support = optional_support;
4587 tcon->smb1.maximal_access = maximal_access;
4588 tcon->smb1.guest_maximal_access = guest_maximal_access;
4590 TALLOC_FREE(tcon->smb1.service);
4591 tcon->smb1.service = talloc_strdup(tcon, service);
4592 if (service != NULL && tcon->smb1.service == NULL) {
4596 TALLOC_FREE(tcon->smb1.fs_type);
4597 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
4598 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
4605 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
4607 return tcon->smb2.tcon_id;
4610 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
4612 return tcon->smb2.capabilities;
4615 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
4616 struct smbXcli_session *session,
4620 uint32_t capabilities,
4621 uint32_t maximal_access)
4623 tcon->smb2.tcon_id = tcon_id;
4624 tcon->smb2.type = type;
4625 tcon->smb2.flags = flags;
4626 tcon->smb2.capabilities = capabilities;
4627 tcon->smb2.maximal_access = maximal_access;
4629 tcon->smb2.should_encrypt = false;
4631 if (session == NULL) {
4635 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
4637 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
4638 tcon->smb2.should_encrypt = true;