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 min_protocol;
52 enum protocol_types max_protocol;
53 enum protocol_types protocol;
56 bool mandatory_signing;
59 * The incoming dispatch function should return:
60 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
61 * - NT_STATUS_OK, if no more processing is desired, e.g.
62 * the dispatch function called
64 * - All other return values disconnect the connection.
66 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
72 uint32_t capabilities;
77 uint32_t capabilities;
80 uint16_t security_mode;
89 const char *workgroup;
95 uint32_t capabilities;
100 struct smb_signing_state *signing;
101 struct smb_trans_enc_state *trans_enc;
103 struct tevent_req *read_braw_req;
108 uint32_t capabilities;
109 uint16_t security_mode;
114 uint32_t capabilities;
115 uint16_t security_mode;
117 uint32_t max_trans_size;
118 uint32_t max_read_size;
119 uint32_t max_write_size;
126 uint16_t cur_credits;
127 uint16_t max_credits;
130 struct smbXcli_session *sessions;
133 struct smb2cli_session {
135 uint16_t session_flags;
136 DATA_BLOB application_key;
137 DATA_BLOB signing_key;
140 DATA_BLOB encryption_key;
141 DATA_BLOB decryption_key;
144 uint16_t channel_sequence;
148 struct smbXcli_session {
149 struct smbXcli_session *prev, *next;
150 struct smbXcli_conn *conn;
154 DATA_BLOB application_key;
158 struct smb2cli_session *smb2;
161 DATA_BLOB signing_key;
165 * this should be a short term hack
166 * until the upper layers have implemented
169 bool disconnect_expired;
172 struct smbXcli_tcon {
174 uint32_t fs_attributes;
178 uint16_t optional_support;
179 uint32_t maximal_access;
180 uint32_t guest_maximal_access;
189 uint32_t capabilities;
190 uint32_t maximal_access;
196 struct smbXcli_req_state {
197 struct tevent_context *ev;
198 struct smbXcli_conn *conn;
199 struct smbXcli_session *session; /* maybe NULL */
200 struct smbXcli_tcon *tcon; /* maybe NULL */
202 uint8_t length_hdr[4];
209 /* Space for the header including the wct */
210 uint8_t hdr[HDR_VWV];
213 * For normal requests, smb1cli_req_send chooses a mid.
214 * SecondaryV trans requests need to use the mid of the primary
215 * request, so we need a place to store it.
216 * Assume it is set if != 0.
221 uint8_t bytecount_buf[2];
223 #define MAX_SMB_IOV 10
224 /* length_hdr, hdr, words, byte_count, buffers */
225 struct iovec iov[1 + 3 + MAX_SMB_IOV];
230 struct tevent_req **chained_requests;
233 NTSTATUS recv_status;
234 /* always an array of 3 talloc elements */
235 struct iovec *recv_iov;
239 const uint8_t *fixed;
244 uint8_t transform[SMB2_TF_HDR_SIZE];
245 uint8_t hdr[SMB2_HDR_BODY];
246 uint8_t pad[7]; /* padding space for compounding */
249 * always an array of 3 talloc elements
250 * (without a SMB2_TRANSFORM header!)
254 struct iovec *recv_iov;
257 * the expected max for the response dyn_len
259 uint32_t max_dyn_len;
261 uint16_t credit_charge;
265 uint64_t encryption_session_id;
267 bool signing_skipped;
273 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
276 * NT_STATUS_OK, means we do not notify the callers
278 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
280 while (conn->sessions) {
281 conn->sessions->conn = NULL;
282 DLIST_REMOVE(conn->sessions, conn->sessions);
285 if (conn->smb1.trans_enc) {
286 TALLOC_FREE(conn->smb1.trans_enc);
292 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
294 const char *remote_name,
295 enum smb_signing_setting signing_state,
296 uint32_t smb1_capabilities,
297 struct GUID *client_guid,
298 uint32_t smb2_capabilities)
300 struct smbXcli_conn *conn = NULL;
302 struct sockaddr *sa = NULL;
306 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
312 conn->write_fd = dup(fd);
313 if (conn->write_fd == -1) {
317 conn->remote_name = talloc_strdup(conn, remote_name);
318 if (conn->remote_name == NULL) {
323 ss = (void *)&conn->local_ss;
324 sa = (struct sockaddr *)ss;
325 sa_length = sizeof(conn->local_ss);
326 ret = getsockname(fd, sa, &sa_length);
330 ss = (void *)&conn->remote_ss;
331 sa = (struct sockaddr *)ss;
332 sa_length = sizeof(conn->remote_ss);
333 ret = getpeername(fd, sa, &sa_length);
338 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
339 if (conn->outgoing == NULL) {
342 conn->pending = NULL;
344 conn->min_protocol = PROTOCOL_NONE;
345 conn->max_protocol = PROTOCOL_NONE;
346 conn->protocol = PROTOCOL_NONE;
348 switch (signing_state) {
349 case SMB_SIGNING_OFF:
351 conn->allow_signing = false;
352 conn->desire_signing = false;
353 conn->mandatory_signing = false;
355 case SMB_SIGNING_DEFAULT:
356 case SMB_SIGNING_IF_REQUIRED:
357 /* if the server requires it */
358 conn->allow_signing = true;
359 conn->desire_signing = false;
360 conn->mandatory_signing = false;
362 case SMB_SIGNING_REQUIRED:
364 conn->allow_signing = true;
365 conn->desire_signing = true;
366 conn->mandatory_signing = true;
370 conn->smb1.client.capabilities = smb1_capabilities;
371 conn->smb1.client.max_xmit = UINT16_MAX;
373 conn->smb1.capabilities = conn->smb1.client.capabilities;
374 conn->smb1.max_xmit = 1024;
378 /* initialise signing */
379 conn->smb1.signing = smb_signing_init(conn,
381 conn->desire_signing,
382 conn->mandatory_signing);
383 if (!conn->smb1.signing) {
387 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
388 if (conn->mandatory_signing) {
389 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
392 conn->smb2.client.guid = *client_guid;
394 conn->smb2.client.capabilities = smb2_capabilities;
396 conn->smb2.cur_credits = 1;
397 conn->smb2.max_credits = 0;
399 talloc_set_destructor(conn, smbXcli_conn_destructor);
403 if (conn->write_fd != -1) {
404 close(conn->write_fd);
410 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
416 if (conn->read_fd == -1) {
423 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
425 return conn->protocol;
428 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
430 if (conn->protocol >= PROTOCOL_SMB2_02) {
434 if (conn->smb1.capabilities & CAP_UNICODE) {
441 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
443 set_socket_options(conn->read_fd, options);
446 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
448 return &conn->local_ss;
451 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
453 return &conn->remote_ss;
456 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
458 return conn->remote_name;
461 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
463 if (conn->protocol >= PROTOCOL_SMB2_02) {
470 return conn->smb1.server.max_mux;
473 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
475 if (conn->protocol >= PROTOCOL_SMB2_02) {
476 return conn->smb2.server.system_time;
479 return conn->smb1.server.system_time;
482 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
484 if (conn->protocol >= PROTOCOL_SMB2_02) {
485 return &conn->smb2.server.gss_blob;
488 return &conn->smb1.server.gss_blob;
491 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
493 if (conn->protocol >= PROTOCOL_SMB2_02) {
494 return &conn->smb2.server.guid;
497 return &conn->smb1.server.guid;
500 struct smbXcli_conn_samba_suicide_state {
501 struct smbXcli_conn *conn;
506 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
508 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
509 struct tevent_context *ev,
510 struct smbXcli_conn *conn,
513 struct tevent_req *req, *subreq;
514 struct smbXcli_conn_samba_suicide_state *state;
516 req = tevent_req_create(mem_ctx, &state,
517 struct smbXcli_conn_samba_suicide_state);
522 SIVAL(state->buf, 4, 0x74697865);
523 SCVAL(state->buf, 8, exitcode);
524 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
526 state->iov.iov_base = state->buf;
527 state->iov.iov_len = sizeof(state->buf);
529 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
530 false, &state->iov, 1);
531 if (tevent_req_nomem(subreq, req)) {
532 return tevent_req_post(req, ev);
534 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
538 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
540 struct tevent_req *req = tevent_req_callback_data(
541 subreq, struct tevent_req);
542 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
543 req, struct smbXcli_conn_samba_suicide_state);
547 nwritten = writev_recv(subreq, &err);
549 if (nwritten == -1) {
550 NTSTATUS status = map_nt_error_from_unix_common(err);
551 smbXcli_conn_disconnect(state->conn, status);
554 tevent_req_done(req);
557 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
559 return tevent_req_simple_recv_ntstatus(req);
562 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
565 TALLOC_CTX *frame = talloc_stackframe();
566 struct tevent_context *ev;
567 struct tevent_req *req;
568 NTSTATUS status = NT_STATUS_NO_MEMORY;
571 if (smbXcli_conn_has_async_calls(conn)) {
573 * Can't use sync call while an async call is in flight
575 status = NT_STATUS_INVALID_PARAMETER_MIX;
578 ev = samba_tevent_context_init(frame);
582 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
586 ok = tevent_req_poll(req, ev);
588 status = map_nt_error_from_unix_common(errno);
591 status = smbXcli_conn_samba_suicide_recv(req);
597 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
599 return conn->smb1.capabilities;
602 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
604 return conn->smb1.max_xmit;
607 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
610 uint16_t possible = conn->smb1.server.max_mux;
612 pending = tevent_queue_length(conn->outgoing);
613 if (pending >= possible) {
616 pending += talloc_array_length(conn->pending);
617 if (pending >= possible) {
624 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
626 return conn->smb1.server.session_key;
629 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
631 return conn->smb1.server.challenge;
634 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
636 return conn->smb1.server.security_mode;
639 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
641 return conn->smb1.server.readbraw;
644 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
646 return conn->smb1.server.writebraw;
649 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
651 return conn->smb1.server.lockread;
654 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
656 return conn->smb1.server.writeunlock;
659 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
661 return conn->smb1.server.time_zone;
664 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
665 const DATA_BLOB user_session_key,
666 const DATA_BLOB response)
668 return smb_signing_activate(conn->smb1.signing,
673 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
674 const uint8_t *buf, uint32_t seqnum)
676 const uint8_t *hdr = buf + NBT_HDR_SIZE;
677 size_t len = smb_len_nbt(buf);
679 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
682 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
684 return smb_signing_is_active(conn->smb1.signing);
687 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
688 struct smb_trans_enc_state *es)
690 /* Replace the old state, if any. */
691 if (conn->smb1.trans_enc) {
692 TALLOC_FREE(conn->smb1.trans_enc);
694 conn->smb1.trans_enc = es;
697 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
699 return common_encryption_on(conn->smb1.trans_enc);
703 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
705 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
706 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
708 if (NT_STATUS_IS_OK(status)) {
712 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
716 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
720 * Is the SMB command able to hold an AND_X successor
721 * @param[in] cmd The SMB command in question
722 * @retval Can we add a chained request after "cmd"?
724 bool smb1cli_is_andx_req(uint8_t cmd)
744 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
746 size_t num_pending = talloc_array_length(conn->pending);
749 if (conn->protocol == PROTOCOL_NONE) {
751 * This is what windows sends on the SMB1 Negprot request
752 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
760 result = conn->smb1.mid++;
761 if ((result == 0) || (result == 0xffff)) {
765 for (i=0; i<num_pending; i++) {
766 if (result == smb1cli_req_mid(conn->pending[i])) {
771 if (i == num_pending) {
777 void smbXcli_req_unset_pending(struct tevent_req *req)
779 struct smbXcli_req_state *state =
781 struct smbXcli_req_state);
782 struct smbXcli_conn *conn = state->conn;
783 size_t num_pending = talloc_array_length(conn->pending);
786 if (state->smb1.mid != 0) {
788 * This is a [nt]trans[2] request which waits
789 * for more than one reply.
794 tevent_req_set_cleanup_fn(req, NULL);
796 if (num_pending == 1) {
798 * The pending read_smb tevent_req is a child of
799 * conn->pending. So if nothing is pending anymore, we need to
800 * delete the socket read fde.
802 TALLOC_FREE(conn->pending);
803 conn->read_smb_req = NULL;
807 for (i=0; i<num_pending; i++) {
808 if (req == conn->pending[i]) {
812 if (i == num_pending) {
814 * Something's seriously broken. Just returning here is the
815 * right thing nevertheless, the point of this routine is to
816 * remove ourselves from conn->pending.
822 * Remove ourselves from the conn->pending array
824 for (; i < (num_pending - 1); i++) {
825 conn->pending[i] = conn->pending[i+1];
829 * No NULL check here, we're shrinking by sizeof(void *), and
830 * talloc_realloc just adjusts the size for this.
832 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
837 static void smbXcli_req_cleanup(struct tevent_req *req,
838 enum tevent_req_state req_state)
840 struct smbXcli_req_state *state =
842 struct smbXcli_req_state);
845 case TEVENT_REQ_RECEIVED:
847 * Make sure we really remove it from
848 * the pending array on destruction.
851 smbXcli_req_unset_pending(req);
858 static bool smb1cli_req_cancel(struct tevent_req *req);
859 static bool smb2cli_req_cancel(struct tevent_req *req);
861 static bool smbXcli_req_cancel(struct tevent_req *req)
863 struct smbXcli_req_state *state =
865 struct smbXcli_req_state);
867 if (!smbXcli_conn_is_connected(state->conn)) {
871 if (state->conn->protocol == PROTOCOL_NONE) {
875 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
876 return smb2cli_req_cancel(req);
879 return smb1cli_req_cancel(req);
882 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
884 bool smbXcli_req_set_pending(struct tevent_req *req)
886 struct smbXcli_req_state *state =
888 struct smbXcli_req_state);
889 struct smbXcli_conn *conn;
890 struct tevent_req **pending;
895 if (!smbXcli_conn_is_connected(conn)) {
899 num_pending = talloc_array_length(conn->pending);
901 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
903 if (pending == NULL) {
906 pending[num_pending] = req;
907 conn->pending = pending;
908 tevent_req_set_cleanup_fn(req, smbXcli_req_cleanup);
909 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
911 if (!smbXcli_conn_receive_next(conn)) {
913 * the caller should notify the current request
915 * And all other pending requests get notified
916 * by smbXcli_conn_disconnect().
918 smbXcli_req_unset_pending(req);
919 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
926 static void smbXcli_conn_received(struct tevent_req *subreq);
928 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
930 size_t num_pending = talloc_array_length(conn->pending);
931 struct tevent_req *req;
932 struct smbXcli_req_state *state;
934 if (conn->read_smb_req != NULL) {
938 if (num_pending == 0) {
939 if (conn->smb2.mid < UINT64_MAX) {
940 /* no more pending requests, so we are done for now */
945 * If there are no more SMB2 requests possible,
946 * because we are out of message ids,
947 * we need to disconnect.
949 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
953 req = conn->pending[0];
954 state = tevent_req_data(req, struct smbXcli_req_state);
957 * We're the first ones, add the read_smb request that waits for the
958 * answer from the server
960 conn->read_smb_req = read_smb_send(conn->pending,
963 if (conn->read_smb_req == NULL) {
966 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
970 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
972 struct smbXcli_session *session;
974 tevent_queue_stop(conn->outgoing);
976 if (conn->read_fd != -1) {
977 close(conn->read_fd);
979 if (conn->write_fd != -1) {
980 close(conn->write_fd);
985 session = conn->sessions;
986 if (talloc_array_length(conn->pending) == 0) {
988 * if we do not have pending requests
989 * there is no need to update the channel_sequence
993 for (; session; session = session->next) {
994 smb2cli_session_increment_channel_sequence(session);
998 * Cancel all pending requests. We do not do a for-loop walking
999 * conn->pending because that array changes in
1000 * smbXcli_req_unset_pending.
1002 while (talloc_array_length(conn->pending) > 0) {
1003 struct tevent_req *req;
1004 struct smbXcli_req_state *state;
1005 struct tevent_req **chain;
1009 req = conn->pending[0];
1010 state = tevent_req_data(req, struct smbXcli_req_state);
1012 if (state->smb1.chained_requests == NULL) {
1014 * We're dead. No point waiting for trans2
1017 state->smb1.mid = 0;
1019 smbXcli_req_unset_pending(req);
1021 if (NT_STATUS_IS_OK(status)) {
1022 /* do not notify the callers */
1027 * we need to defer the callback, because we may notify
1028 * more then one caller.
1030 tevent_req_defer_callback(req, state->ev);
1031 tevent_req_nterror(req, status);
1035 chain = talloc_move(conn, &state->smb1.chained_requests);
1036 num_chained = talloc_array_length(chain);
1038 for (i=0; i<num_chained; i++) {
1040 state = tevent_req_data(req, struct smbXcli_req_state);
1043 * We're dead. No point waiting for trans2
1046 state->smb1.mid = 0;
1048 smbXcli_req_unset_pending(req);
1050 if (NT_STATUS_IS_OK(status)) {
1051 /* do not notify the callers */
1056 * we need to defer the callback, because we may notify
1057 * more than one caller.
1059 tevent_req_defer_callback(req, state->ev);
1060 tevent_req_nterror(req, status);
1067 * Fetch a smb request's mid. Only valid after the request has been sent by
1068 * smb1cli_req_send().
1070 uint16_t smb1cli_req_mid(struct tevent_req *req)
1072 struct smbXcli_req_state *state =
1073 tevent_req_data(req,
1074 struct smbXcli_req_state);
1076 if (state->smb1.mid != 0) {
1077 return state->smb1.mid;
1080 return SVAL(state->smb1.hdr, HDR_MID);
1083 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1085 struct smbXcli_req_state *state =
1086 tevent_req_data(req,
1087 struct smbXcli_req_state);
1089 state->smb1.mid = mid;
1092 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1094 struct smbXcli_req_state *state =
1095 tevent_req_data(req,
1096 struct smbXcli_req_state);
1098 return state->smb1.seqnum;
1101 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1103 struct smbXcli_req_state *state =
1104 tevent_req_data(req,
1105 struct smbXcli_req_state);
1107 state->smb1.seqnum = seqnum;
1110 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1114 for (i=0; i<count; i++) {
1115 result += iov[i].iov_len;
1120 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1121 const struct iovec *iov,
1124 size_t len = smbXcli_iov_len(iov, count);
1129 buf = talloc_array(mem_ctx, uint8_t, len);
1134 for (i=0; i<count; i++) {
1135 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1136 copied += iov[i].iov_len;
1141 static void smb1cli_req_flags(enum protocol_types protocol,
1142 uint32_t smb1_capabilities,
1143 uint8_t smb_command,
1144 uint8_t additional_flags,
1145 uint8_t clear_flags,
1147 uint16_t additional_flags2,
1148 uint16_t clear_flags2,
1152 uint16_t flags2 = 0;
1154 if (protocol >= PROTOCOL_LANMAN1) {
1155 flags |= FLAG_CASELESS_PATHNAMES;
1156 flags |= FLAG_CANONICAL_PATHNAMES;
1159 if (protocol >= PROTOCOL_LANMAN2) {
1160 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1161 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1164 if (protocol >= PROTOCOL_NT1) {
1165 flags2 |= FLAGS2_IS_LONG_NAME;
1167 if (smb1_capabilities & CAP_UNICODE) {
1168 flags2 |= FLAGS2_UNICODE_STRINGS;
1170 if (smb1_capabilities & CAP_STATUS32) {
1171 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1173 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1174 flags2 |= FLAGS2_EXTENDED_SECURITY;
1178 flags |= additional_flags;
1179 flags &= ~clear_flags;
1180 flags2 |= additional_flags2;
1181 flags2 &= ~clear_flags2;
1187 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1189 static bool smb1cli_req_cancel(struct tevent_req *req)
1191 struct smbXcli_req_state *state =
1192 tevent_req_data(req,
1193 struct smbXcli_req_state);
1198 struct tevent_req *subreq;
1201 flags = CVAL(state->smb1.hdr, HDR_FLG);
1202 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1203 pid = SVAL(state->smb1.hdr, HDR_PID);
1204 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1205 mid = SVAL(state->smb1.hdr, HDR_MID);
1207 subreq = smb1cli_req_create(state, state->ev,
1217 0, NULL); /* bytes */
1218 if (subreq == NULL) {
1221 smb1cli_req_set_mid(subreq, mid);
1223 status = smb1cli_req_chain_submit(&subreq, 1);
1224 if (!NT_STATUS_IS_OK(status)) {
1225 TALLOC_FREE(subreq);
1228 smb1cli_req_set_mid(subreq, 0);
1230 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1235 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1237 /* we do not care about the result */
1238 TALLOC_FREE(subreq);
1241 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1242 struct tevent_context *ev,
1243 struct smbXcli_conn *conn,
1244 uint8_t smb_command,
1245 uint8_t additional_flags,
1246 uint8_t clear_flags,
1247 uint16_t additional_flags2,
1248 uint16_t clear_flags2,
1249 uint32_t timeout_msec,
1251 struct smbXcli_tcon *tcon,
1252 struct smbXcli_session *session,
1253 uint8_t wct, uint16_t *vwv,
1255 struct iovec *bytes_iov)
1257 struct tevent_req *req;
1258 struct smbXcli_req_state *state;
1260 uint16_t flags2 = 0;
1264 if (iov_count > MAX_SMB_IOV) {
1266 * Should not happen :-)
1271 req = tevent_req_create(mem_ctx, &state,
1272 struct smbXcli_req_state);
1278 state->session = session;
1282 uid = session->smb1.session_id;
1286 tid = tcon->smb1.tcon_id;
1288 if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
1289 clear_flags |= FLAG_CASELESS_PATHNAMES;
1291 /* Default setting, case insensitive. */
1292 additional_flags |= FLAG_CASELESS_PATHNAMES;
1295 if (smbXcli_conn_dfs_supported(conn) &&
1296 smbXcli_tcon_is_dfs_share(tcon))
1298 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
1302 state->smb1.recv_cmd = 0xFF;
1303 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1304 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1305 if (state->smb1.recv_iov == NULL) {
1310 smb1cli_req_flags(conn->protocol,
1311 conn->smb1.capabilities,
1320 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1321 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1322 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1323 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1324 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1325 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1326 SSVAL(state->smb1.hdr, HDR_TID, tid);
1327 SSVAL(state->smb1.hdr, HDR_PID, pid);
1328 SSVAL(state->smb1.hdr, HDR_UID, uid);
1329 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1330 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1332 state->smb1.vwv = vwv;
1334 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1336 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1337 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1338 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1339 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1340 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1341 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1342 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1343 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1345 if (iov_count != 0) {
1346 memcpy(&state->smb1.iov[4], bytes_iov,
1347 iov_count * sizeof(*bytes_iov));
1349 state->smb1.iov_count = iov_count + 4;
1351 if (timeout_msec > 0) {
1352 struct timeval endtime;
1354 endtime = timeval_current_ofs_msec(timeout_msec);
1355 if (!tevent_req_set_endtime(req, ev, endtime)) {
1360 switch (smb_command) {
1364 state->one_way = true;
1367 state->one_way = true;
1368 state->smb1.one_way_seqnum = true;
1372 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1373 state->one_way = true;
1381 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1382 struct iovec *iov, int iov_count,
1384 bool one_way_seqnum)
1386 TALLOC_CTX *frame = NULL;
1390 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1391 * iovec directly. MD5Update would do that just fine.
1394 if (iov_count < 4) {
1395 return NT_STATUS_INVALID_PARAMETER_MIX;
1397 if (iov[0].iov_len != NBT_HDR_SIZE) {
1398 return NT_STATUS_INVALID_PARAMETER_MIX;
1400 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1401 return NT_STATUS_INVALID_PARAMETER_MIX;
1403 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1404 return NT_STATUS_INVALID_PARAMETER_MIX;
1406 if (iov[3].iov_len != sizeof(uint16_t)) {
1407 return NT_STATUS_INVALID_PARAMETER_MIX;
1410 frame = talloc_stackframe();
1412 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1414 return NT_STATUS_NO_MEMORY;
1417 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1419 smb_signing_sign_pdu(conn->smb1.signing,
1420 buf, talloc_get_size(buf),
1422 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1425 return NT_STATUS_OK;
1428 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1429 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1430 TALLOC_CTX *tmp_mem,
1433 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1434 struct smbXcli_req_state *state,
1435 struct iovec *iov, int iov_count)
1437 struct tevent_req *subreq;
1442 if (!smbXcli_conn_is_connected(state->conn)) {
1443 return NT_STATUS_CONNECTION_DISCONNECTED;
1446 if (state->conn->protocol > PROTOCOL_NT1) {
1447 return NT_STATUS_REVISION_MISMATCH;
1450 if (iov_count < 4) {
1451 return NT_STATUS_INVALID_PARAMETER_MIX;
1453 if (iov[0].iov_len != NBT_HDR_SIZE) {
1454 return NT_STATUS_INVALID_PARAMETER_MIX;
1456 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1457 return NT_STATUS_INVALID_PARAMETER_MIX;
1459 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1460 return NT_STATUS_INVALID_PARAMETER_MIX;
1462 if (iov[3].iov_len != sizeof(uint16_t)) {
1463 return NT_STATUS_INVALID_PARAMETER_MIX;
1466 cmd = CVAL(iov[1].iov_base, HDR_COM);
1467 if (cmd == SMBreadBraw) {
1468 if (smbXcli_conn_has_async_calls(state->conn)) {
1469 return NT_STATUS_INVALID_PARAMETER_MIX;
1471 state->conn->smb1.read_braw_req = req;
1474 if (state->smb1.mid != 0) {
1475 mid = state->smb1.mid;
1477 mid = smb1cli_alloc_mid(state->conn);
1479 SSVAL(iov[1].iov_base, HDR_MID, mid);
1481 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1483 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1484 &state->smb1.seqnum,
1485 state->smb1.one_way_seqnum);
1487 if (!NT_STATUS_IS_OK(status)) {
1492 * If we supported multiple encrytion contexts
1493 * here we'd look up based on tid.
1495 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1496 char *buf, *enc_buf;
1498 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1500 return NT_STATUS_NO_MEMORY;
1502 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1503 (char *)buf, &enc_buf);
1505 if (!NT_STATUS_IS_OK(status)) {
1506 DEBUG(0, ("Error in encrypting client message: %s\n",
1507 nt_errstr(status)));
1510 buf = (char *)talloc_memdup(state, enc_buf,
1511 smb_len_nbt(enc_buf)+4);
1514 return NT_STATUS_NO_MEMORY;
1516 iov[0].iov_base = (void *)buf;
1517 iov[0].iov_len = talloc_get_size(buf);
1521 if (state->conn->dispatch_incoming == NULL) {
1522 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1525 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1527 subreq = writev_send(state, state->ev, state->conn->outgoing,
1528 state->conn->write_fd, false, iov, iov_count);
1529 if (subreq == NULL) {
1530 return NT_STATUS_NO_MEMORY;
1532 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1533 return NT_STATUS_OK;
1536 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1537 struct tevent_context *ev,
1538 struct smbXcli_conn *conn,
1539 uint8_t smb_command,
1540 uint8_t additional_flags,
1541 uint8_t clear_flags,
1542 uint16_t additional_flags2,
1543 uint16_t clear_flags2,
1544 uint32_t timeout_msec,
1546 struct smbXcli_tcon *tcon,
1547 struct smbXcli_session *session,
1548 uint8_t wct, uint16_t *vwv,
1550 const uint8_t *bytes)
1552 struct tevent_req *req;
1556 iov.iov_base = discard_const_p(void, bytes);
1557 iov.iov_len = num_bytes;
1559 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1560 additional_flags, clear_flags,
1561 additional_flags2, clear_flags2,
1568 if (!tevent_req_is_in_progress(req)) {
1569 return tevent_req_post(req, ev);
1571 status = smb1cli_req_chain_submit(&req, 1);
1572 if (tevent_req_nterror(req, status)) {
1573 return tevent_req_post(req, ev);
1578 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1580 struct tevent_req *req =
1581 tevent_req_callback_data(subreq,
1583 struct smbXcli_req_state *state =
1584 tevent_req_data(req,
1585 struct smbXcli_req_state);
1589 nwritten = writev_recv(subreq, &err);
1590 TALLOC_FREE(subreq);
1591 if (nwritten == -1) {
1592 NTSTATUS status = map_nt_error_from_unix_common(err);
1593 smbXcli_conn_disconnect(state->conn, status);
1597 if (state->one_way) {
1598 state->inbuf = NULL;
1599 tevent_req_done(req);
1603 if (!smbXcli_req_set_pending(req)) {
1604 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1609 static void smbXcli_conn_received(struct tevent_req *subreq)
1611 struct smbXcli_conn *conn =
1612 tevent_req_callback_data(subreq,
1613 struct smbXcli_conn);
1614 TALLOC_CTX *frame = talloc_stackframe();
1620 if (subreq != conn->read_smb_req) {
1621 DEBUG(1, ("Internal error: cli_smb_received called with "
1622 "unexpected subreq\n"));
1623 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1627 conn->read_smb_req = NULL;
1629 received = read_smb_recv(subreq, frame, &inbuf, &err);
1630 TALLOC_FREE(subreq);
1631 if (received == -1) {
1632 status = map_nt_error_from_unix_common(err);
1633 smbXcli_conn_disconnect(conn, status);
1638 status = conn->dispatch_incoming(conn, frame, inbuf);
1640 if (NT_STATUS_IS_OK(status)) {
1642 * We should not do any more processing
1643 * as the dispatch function called
1644 * tevent_req_done().
1649 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1651 * We got an error, so notify all pending requests
1653 smbXcli_conn_disconnect(conn, status);
1658 * We got NT_STATUS_RETRY, so we may ask for a
1659 * next incoming pdu.
1661 if (!smbXcli_conn_receive_next(conn)) {
1662 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1666 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1667 struct iovec **piov, int *pnum_iov)
1678 size_t min_size = MIN_SMB_SIZE;
1680 buflen = smb_len_tcp(buf);
1683 hdr = buf + NBT_HDR_SIZE;
1685 status = smb1cli_pull_raw_error(hdr);
1686 if (NT_STATUS_IS_ERR(status)) {
1688 * This is an ugly hack to support OS/2
1689 * which skips the byte_count in the DATA block
1690 * on some error responses.
1694 min_size -= sizeof(uint16_t);
1697 if (buflen < min_size) {
1698 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1702 * This returns iovec elements in the following order:
1717 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1719 return NT_STATUS_NO_MEMORY;
1721 iov[0].iov_base = hdr;
1722 iov[0].iov_len = HDR_WCT;
1725 cmd = CVAL(hdr, HDR_COM);
1729 size_t len = buflen - taken;
1731 struct iovec *iov_tmp;
1738 * we need at least WCT
1740 needed = sizeof(uint8_t);
1742 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1743 __location__, (int)len, (int)needed));
1748 * Now we check if the specified words are there
1750 wct = CVAL(hdr, wct_ofs);
1751 needed += wct * sizeof(uint16_t);
1753 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1754 __location__, (int)len, (int)needed));
1758 if ((num_iov == 1) &&
1760 NT_STATUS_IS_ERR(status))
1763 * This is an ugly hack to support OS/2
1764 * which skips the byte_count in the DATA block
1765 * on some error responses.
1769 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1771 if (iov_tmp == NULL) {
1773 return NT_STATUS_NO_MEMORY;
1776 cur = &iov[num_iov];
1780 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1782 cur[1].iov_base = cur[0].iov_base;
1789 * we need at least BCC
1791 needed += sizeof(uint16_t);
1793 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1794 __location__, (int)len, (int)needed));
1799 * Now we check if the specified bytes are there
1801 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1802 bcc = SVAL(hdr, bcc_ofs);
1803 needed += bcc * sizeof(uint8_t);
1805 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1806 __location__, (int)len, (int)needed));
1811 * we allocate 2 iovec structures for words and bytes
1813 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1815 if (iov_tmp == NULL) {
1817 return NT_STATUS_NO_MEMORY;
1820 cur = &iov[num_iov];
1823 cur[0].iov_len = wct * sizeof(uint16_t);
1824 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1825 cur[1].iov_len = bcc * sizeof(uint8_t);
1826 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1830 if (!smb1cli_is_andx_req(cmd)) {
1832 * If the current command does not have AndX chanining
1838 if (wct == 0 && bcc == 0) {
1840 * An empty response also ends the chain,
1841 * most likely with an error.
1847 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1848 __location__, (int)wct, (int)cmd));
1851 cmd = CVAL(cur[0].iov_base, 0);
1854 * If it is the end of the chain we are also done.
1858 wct_ofs = SVAL(cur[0].iov_base, 2);
1860 if (wct_ofs < taken) {
1861 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1863 if (wct_ofs > buflen) {
1864 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1868 * we consumed everything up to the start of the next
1874 remaining = buflen - taken;
1876 if (remaining > 0 && num_iov >= 3) {
1878 * The last DATA block gets the remaining
1879 * bytes, this is needed to support
1880 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1882 iov[num_iov-1].iov_len += remaining;
1886 *pnum_iov = num_iov;
1887 return NT_STATUS_OK;
1891 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1894 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1895 TALLOC_CTX *tmp_mem,
1898 struct tevent_req *req;
1899 struct smbXcli_req_state *state;
1906 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1907 size_t len = smb_len_tcp(inbuf);
1908 struct iovec *iov = NULL;
1910 struct tevent_req **chain = NULL;
1911 size_t num_chained = 0;
1912 size_t num_responses = 0;
1914 if (conn->smb1.read_braw_req != NULL) {
1915 req = conn->smb1.read_braw_req;
1916 conn->smb1.read_braw_req = NULL;
1917 state = tevent_req_data(req, struct smbXcli_req_state);
1919 smbXcli_req_unset_pending(req);
1921 if (state->smb1.recv_iov == NULL) {
1923 * For requests with more than
1924 * one response, we have to readd the
1927 state->smb1.recv_iov = talloc_zero_array(state,
1930 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1931 return NT_STATUS_OK;
1935 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1936 state->smb1.recv_iov[0].iov_len = len;
1937 ZERO_STRUCT(state->smb1.recv_iov[1]);
1938 ZERO_STRUCT(state->smb1.recv_iov[2]);
1940 state->smb1.recv_cmd = SMBreadBraw;
1941 state->smb1.recv_status = NT_STATUS_OK;
1942 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1944 tevent_req_done(req);
1945 return NT_STATUS_OK;
1948 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1949 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1950 DEBUG(10, ("Got non-SMB PDU\n"));
1951 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1955 * If we supported multiple encrytion contexts
1956 * here we'd look up based on tid.
1958 if (common_encryption_on(conn->smb1.trans_enc)
1959 && (CVAL(inbuf, 0) == 0)) {
1960 uint16_t enc_ctx_num;
1962 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1963 if (!NT_STATUS_IS_OK(status)) {
1964 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1965 nt_errstr(status)));
1969 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1970 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1972 conn->smb1.trans_enc->enc_ctx_num));
1973 return NT_STATUS_INVALID_HANDLE;
1976 status = common_decrypt_buffer(conn->smb1.trans_enc,
1978 if (!NT_STATUS_IS_OK(status)) {
1979 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1980 nt_errstr(status)));
1983 inhdr = inbuf + NBT_HDR_SIZE;
1984 len = smb_len_nbt(inbuf);
1987 mid = SVAL(inhdr, HDR_MID);
1988 num_pending = talloc_array_length(conn->pending);
1990 for (i=0; i<num_pending; i++) {
1991 if (mid == smb1cli_req_mid(conn->pending[i])) {
1995 if (i == num_pending) {
1996 /* Dump unexpected reply */
1997 return NT_STATUS_RETRY;
2000 oplock_break = false;
2002 if (mid == 0xffff) {
2004 * Paranoia checks that this is really an oplock break request.
2006 oplock_break = (len == 51); /* hdr + 8 words */
2007 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
2008 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
2009 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
2010 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
2012 if (!oplock_break) {
2013 /* Dump unexpected reply */
2014 return NT_STATUS_RETRY;
2018 req = conn->pending[i];
2019 state = tevent_req_data(req, struct smbXcli_req_state);
2021 if (!oplock_break /* oplock breaks are not signed */
2022 && !smb_signing_check_pdu(conn->smb1.signing,
2023 inhdr, len, state->smb1.seqnum+1)) {
2024 DEBUG(10, ("cli_check_sign_mac failed\n"));
2025 return NT_STATUS_ACCESS_DENIED;
2028 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2030 if (!NT_STATUS_IS_OK(status)) {
2031 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2032 nt_errstr(status)));
2036 cmd = CVAL(inhdr, HDR_COM);
2037 status = smb1cli_pull_raw_error(inhdr);
2039 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2040 (state->session != NULL) && state->session->disconnect_expired)
2043 * this should be a short term hack
2044 * until the upper layers have implemented
2045 * re-authentication.
2050 if (state->smb1.chained_requests == NULL) {
2052 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2055 smbXcli_req_unset_pending(req);
2057 if (state->smb1.recv_iov == NULL) {
2059 * For requests with more than
2060 * one response, we have to readd the
2063 state->smb1.recv_iov = talloc_zero_array(state,
2066 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2067 return NT_STATUS_OK;
2071 state->smb1.recv_cmd = cmd;
2072 state->smb1.recv_status = status;
2073 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2075 state->smb1.recv_iov[0] = iov[0];
2076 state->smb1.recv_iov[1] = iov[1];
2077 state->smb1.recv_iov[2] = iov[2];
2079 if (talloc_array_length(conn->pending) == 0) {
2080 tevent_req_done(req);
2081 return NT_STATUS_OK;
2084 tevent_req_defer_callback(req, state->ev);
2085 tevent_req_done(req);
2086 return NT_STATUS_RETRY;
2089 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2090 num_chained = talloc_array_length(chain);
2091 num_responses = (num_iov - 1)/2;
2093 if (num_responses > num_chained) {
2094 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2097 for (i=0; i<num_chained; i++) {
2098 size_t iov_idx = 1 + (i*2);
2099 struct iovec *cur = &iov[iov_idx];
2103 state = tevent_req_data(req, struct smbXcli_req_state);
2105 smbXcli_req_unset_pending(req);
2108 * as we finish multiple requests here
2109 * we need to defer the callbacks as
2110 * they could destroy our current stack state.
2112 tevent_req_defer_callback(req, state->ev);
2114 if (i >= num_responses) {
2115 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2119 if (state->smb1.recv_iov == NULL) {
2121 * For requests with more than
2122 * one response, we have to readd the
2125 state->smb1.recv_iov = talloc_zero_array(state,
2128 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2133 state->smb1.recv_cmd = cmd;
2135 if (i == (num_responses - 1)) {
2137 * The last request in the chain gets the status
2139 state->smb1.recv_status = status;
2141 cmd = CVAL(cur[0].iov_base, 0);
2142 state->smb1.recv_status = NT_STATUS_OK;
2145 state->inbuf = inbuf;
2148 * Note: here we use talloc_reference() in a way
2149 * that does not expose it to the caller.
2151 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2152 if (tevent_req_nomem(inbuf_ref, req)) {
2156 /* copy the related buffers */
2157 state->smb1.recv_iov[0] = iov[0];
2158 state->smb1.recv_iov[1] = cur[0];
2159 state->smb1.recv_iov[2] = cur[1];
2161 tevent_req_done(req);
2164 return NT_STATUS_RETRY;
2167 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2168 TALLOC_CTX *mem_ctx,
2169 struct iovec **piov,
2173 uint32_t *pvwv_offset,
2174 uint32_t *pnum_bytes,
2176 uint32_t *pbytes_offset,
2178 const struct smb1cli_req_expected_response *expected,
2179 size_t num_expected)
2181 struct smbXcli_req_state *state =
2182 tevent_req_data(req,
2183 struct smbXcli_req_state);
2184 NTSTATUS status = NT_STATUS_OK;
2185 struct iovec *recv_iov = NULL;
2186 uint8_t *hdr = NULL;
2188 uint32_t vwv_offset = 0;
2189 uint16_t *vwv = NULL;
2190 uint32_t num_bytes = 0;
2191 uint32_t bytes_offset = 0;
2192 uint8_t *bytes = NULL;
2194 bool found_status = false;
2195 bool found_size = false;
2209 if (pvwv_offset != NULL) {
2212 if (pnum_bytes != NULL) {
2215 if (pbytes != NULL) {
2218 if (pbytes_offset != NULL) {
2221 if (pinbuf != NULL) {
2225 if (state->inbuf != NULL) {
2226 recv_iov = state->smb1.recv_iov;
2227 state->smb1.recv_iov = NULL;
2228 if (state->smb1.recv_cmd != SMBreadBraw) {
2229 hdr = (uint8_t *)recv_iov[0].iov_base;
2230 wct = recv_iov[1].iov_len/2;
2231 vwv = (uint16_t *)recv_iov[1].iov_base;
2232 vwv_offset = PTR_DIFF(vwv, hdr);
2233 num_bytes = recv_iov[2].iov_len;
2234 bytes = (uint8_t *)recv_iov[2].iov_base;
2235 bytes_offset = PTR_DIFF(bytes, hdr);
2239 if (tevent_req_is_nterror(req, &status)) {
2240 for (i=0; i < num_expected; i++) {
2241 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2242 found_status = true;
2248 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2254 if (num_expected == 0) {
2255 found_status = true;
2259 status = state->smb1.recv_status;
2261 for (i=0; i < num_expected; i++) {
2262 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2266 found_status = true;
2267 if (expected[i].wct == 0) {
2272 if (expected[i].wct == wct) {
2278 if (!found_status) {
2283 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2287 *piov = talloc_move(mem_ctx, &recv_iov);
2299 if (pvwv_offset != NULL) {
2300 *pvwv_offset = vwv_offset;
2302 if (pnum_bytes != NULL) {
2303 *pnum_bytes = num_bytes;
2305 if (pbytes != NULL) {
2308 if (pbytes_offset != NULL) {
2309 *pbytes_offset = bytes_offset;
2311 if (pinbuf != NULL) {
2312 *pinbuf = state->inbuf;
2318 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2325 for (i=0; i<num_reqs; i++) {
2326 struct smbXcli_req_state *state;
2327 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2328 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2329 state->smb1.iov_count-2);
2330 wct_ofs = (wct_ofs + 3) & ~3;
2335 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2337 struct smbXcli_req_state *first_state =
2338 tevent_req_data(reqs[0],
2339 struct smbXcli_req_state);
2340 struct smbXcli_req_state *state;
2342 size_t chain_padding = 0;
2344 struct iovec *iov = NULL;
2345 struct iovec *this_iov;
2349 if (num_reqs == 1) {
2350 return smb1cli_req_writev_submit(reqs[0], first_state,
2351 first_state->smb1.iov,
2352 first_state->smb1.iov_count);
2356 for (i=0; i<num_reqs; i++) {
2357 if (!tevent_req_is_in_progress(reqs[i])) {
2358 return NT_STATUS_INTERNAL_ERROR;
2361 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2363 if (state->smb1.iov_count < 4) {
2364 return NT_STATUS_INVALID_PARAMETER_MIX;
2369 * The NBT and SMB header
2382 iovlen += state->smb1.iov_count - 2;
2385 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2387 return NT_STATUS_NO_MEMORY;
2390 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2391 first_state, reqs, sizeof(*reqs) * num_reqs);
2392 if (first_state->smb1.chained_requests == NULL) {
2394 return NT_STATUS_NO_MEMORY;
2397 wct_offset = HDR_WCT;
2400 for (i=0; i<num_reqs; i++) {
2401 size_t next_padding = 0;
2404 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2406 if (i < num_reqs-1) {
2407 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2408 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2410 TALLOC_FREE(first_state->smb1.chained_requests);
2411 return NT_STATUS_INVALID_PARAMETER_MIX;
2415 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2416 state->smb1.iov_count-2) + 1;
2417 if ((wct_offset % 4) != 0) {
2418 next_padding = 4 - (wct_offset % 4);
2420 wct_offset += next_padding;
2421 vwv = state->smb1.vwv;
2423 if (i < num_reqs-1) {
2424 struct smbXcli_req_state *next_state =
2425 tevent_req_data(reqs[i+1],
2426 struct smbXcli_req_state);
2427 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2429 SSVAL(vwv+1, 0, wct_offset);
2430 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2431 /* properly end the chain */
2432 SCVAL(vwv+0, 0, 0xff);
2433 SCVAL(vwv+0, 1, 0xff);
2439 * The NBT and SMB header
2441 this_iov[0] = state->smb1.iov[0];
2442 this_iov[1] = state->smb1.iov[1];
2446 * This one is a bit subtle. We have to add
2447 * chain_padding bytes between the requests, and we
2448 * have to also include the wct field of the
2449 * subsequent requests. We use the subsequent header
2450 * for the padding, it contains the wct field in its
2453 this_iov[0].iov_len = chain_padding+1;
2454 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2455 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2456 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2461 * copy the words and bytes
2463 memcpy(this_iov, state->smb1.iov+2,
2464 sizeof(struct iovec) * (state->smb1.iov_count-2));
2465 this_iov += state->smb1.iov_count - 2;
2466 chain_padding = next_padding;
2469 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2470 if (nbt_len > first_state->conn->smb1.max_xmit) {
2472 TALLOC_FREE(first_state->smb1.chained_requests);
2473 return NT_STATUS_INVALID_PARAMETER_MIX;
2476 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2477 if (!NT_STATUS_IS_OK(status)) {
2479 TALLOC_FREE(first_state->smb1.chained_requests);
2483 return NT_STATUS_OK;
2486 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2488 return ((tevent_queue_length(conn->outgoing) != 0)
2489 || (talloc_array_length(conn->pending) != 0));
2492 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2494 if (conn->protocol >= PROTOCOL_SMB2_02) {
2495 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2498 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2501 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2503 uint16_t credits = 1;
2505 if (conn->smb2.cur_credits == 0) {
2506 if (max_dyn_len != NULL) {
2512 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2513 credits = conn->smb2.cur_credits;
2516 if (max_dyn_len != NULL) {
2517 *max_dyn_len = credits * 65536;
2523 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2525 return conn->smb2.server.capabilities;
2528 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2530 return conn->smb2.server.security_mode;
2533 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2535 return conn->smb2.server.max_trans_size;
2538 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2540 return conn->smb2.server.max_read_size;
2543 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2545 return conn->smb2.server.max_write_size;
2548 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2549 uint16_t max_credits)
2551 conn->smb2.max_credits = max_credits;
2554 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2556 static bool smb2cli_req_cancel(struct tevent_req *req)
2558 struct smbXcli_req_state *state =
2559 tevent_req_data(req,
2560 struct smbXcli_req_state);
2561 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2562 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2563 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2564 struct smbXcli_tcon *tcon = state->tcon;
2565 struct smbXcli_session *session = state->session;
2566 uint8_t *fixed = state->smb2.pad;
2567 uint16_t fixed_len = 4;
2568 struct tevent_req *subreq;
2569 struct smbXcli_req_state *substate;
2572 SSVAL(fixed, 0, 0x04);
2575 subreq = smb2cli_req_create(state, state->ev,
2583 if (subreq == NULL) {
2586 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2589 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2590 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2592 flags &= SMB2_HDR_FLAG_ASYNC;
2594 if (flags & SMB2_HDR_FLAG_ASYNC) {
2598 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2599 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2600 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2602 status = smb2cli_req_compound_submit(&subreq, 1);
2603 if (!NT_STATUS_IS_OK(status)) {
2604 TALLOC_FREE(subreq);
2608 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2613 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2615 /* we do not care about the result */
2616 TALLOC_FREE(subreq);
2619 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2620 struct tevent_context *ev,
2621 struct smbXcli_conn *conn,
2623 uint32_t additional_flags,
2624 uint32_t clear_flags,
2625 uint32_t timeout_msec,
2626 struct smbXcli_tcon *tcon,
2627 struct smbXcli_session *session,
2628 const uint8_t *fixed,
2632 uint32_t max_dyn_len)
2634 struct tevent_req *req;
2635 struct smbXcli_req_state *state;
2639 bool use_channel_sequence = false;
2640 uint16_t channel_sequence = 0;
2641 bool use_replay_flag = false;
2643 req = tevent_req_create(mem_ctx, &state,
2644 struct smbXcli_req_state);
2651 state->session = session;
2654 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2655 use_channel_sequence = true;
2656 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2657 use_channel_sequence = true;
2660 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_00) {
2661 use_replay_flag = true;
2665 uid = session->smb2->session_id;
2667 if (use_channel_sequence) {
2668 channel_sequence = session->smb2->channel_sequence;
2671 if (use_replay_flag && session->smb2->replay_active) {
2672 additional_flags |= SMB2_HDR_FLAG_REPLAY_OPERATION;
2675 state->smb2.should_sign = session->smb2->should_sign;
2676 state->smb2.should_encrypt = session->smb2->should_encrypt;
2678 if (cmd == SMB2_OP_SESSSETUP &&
2679 session->smb2->signing_key.length != 0) {
2680 state->smb2.should_sign = true;
2683 if (cmd == SMB2_OP_SESSSETUP &&
2684 session->smb2_channel.signing_key.length == 0) {
2685 state->smb2.should_encrypt = false;
2688 if (additional_flags & SMB2_HDR_FLAG_SIGNED) {
2689 if (session->smb2_channel.signing_key.length == 0) {
2690 tevent_req_nterror(req, NT_STATUS_NO_USER_SESSION_KEY);
2694 additional_flags &= ~SMB2_HDR_FLAG_SIGNED;
2695 state->smb2.should_sign = true;
2700 tid = tcon->smb2.tcon_id;
2702 if (tcon->smb2.should_sign) {
2703 state->smb2.should_sign = true;
2705 if (tcon->smb2.should_encrypt) {
2706 state->smb2.should_encrypt = true;
2710 if (state->smb2.should_encrypt) {
2711 state->smb2.should_sign = false;
2714 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2715 if (state->smb2.recv_iov == NULL) {
2720 flags |= additional_flags;
2721 flags &= ~clear_flags;
2723 state->smb2.fixed = fixed;
2724 state->smb2.fixed_len = fixed_len;
2725 state->smb2.dyn = dyn;
2726 state->smb2.dyn_len = dyn_len;
2727 state->smb2.max_dyn_len = max_dyn_len;
2729 if (state->smb2.should_encrypt) {
2730 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2731 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2734 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2735 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2736 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2737 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2738 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2739 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2740 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2741 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2744 case SMB2_OP_CANCEL:
2745 state->one_way = true;
2749 * If this is a dummy request, it will have
2750 * UINT64_MAX as message id.
2751 * If we send on break acknowledgement,
2752 * this gets overwritten later.
2754 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2758 if (timeout_msec > 0) {
2759 struct timeval endtime;
2761 endtime = timeval_current_ofs_msec(timeout_msec);
2762 if (!tevent_req_set_endtime(req, ev, endtime)) {
2770 void smb2cli_req_set_notify_async(struct tevent_req *req)
2772 struct smbXcli_req_state *state =
2773 tevent_req_data(req,
2774 struct smbXcli_req_state);
2776 state->smb2.notify_async = true;
2779 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2780 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2781 TALLOC_CTX *tmp_mem,
2784 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2787 struct smbXcli_req_state *state;
2788 struct tevent_req *subreq;
2790 int i, num_iov, nbt_len;
2792 const DATA_BLOB *encryption_key = NULL;
2793 uint64_t encryption_session_id = 0;
2796 * 1 for the nbt length, optional TRANSFORM
2797 * per request: HDR, fixed, dyn, padding
2798 * -1 because the last one does not need padding
2801 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2803 return NT_STATUS_NO_MEMORY;
2810 * the session of the first request that requires encryption
2811 * specifies the encryption key.
2813 for (i=0; i<num_reqs; i++) {
2814 if (!tevent_req_is_in_progress(reqs[i])) {
2815 return NT_STATUS_INTERNAL_ERROR;
2818 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2820 if (!smbXcli_conn_is_connected(state->conn)) {
2821 return NT_STATUS_CONNECTION_DISCONNECTED;
2824 if ((state->conn->protocol != PROTOCOL_NONE) &&
2825 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2826 return NT_STATUS_REVISION_MISMATCH;
2829 if (state->session == NULL) {
2833 if (!state->smb2.should_encrypt) {
2837 encryption_key = &state->session->smb2->encryption_key;
2838 if (encryption_key->length == 0) {
2839 return NT_STATUS_INVALID_PARAMETER_MIX;
2842 encryption_session_id = state->session->smb2->session_id;
2845 iov[num_iov].iov_base = state->smb2.transform;
2846 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2849 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2850 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2851 state->session->smb2->nonce_low);
2852 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2853 state->session->smb2->nonce_high);
2854 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2855 encryption_session_id);
2857 state->session->smb2->nonce_low += 1;
2858 if (state->session->smb2->nonce_low == 0) {
2859 state->session->smb2->nonce_high += 1;
2860 state->session->smb2->nonce_low += 1;
2863 nbt_len += SMB2_TF_HDR_SIZE;
2867 for (i=0; i<num_reqs; i++) {
2876 const DATA_BLOB *signing_key = NULL;
2878 if (!tevent_req_is_in_progress(reqs[i])) {
2879 return NT_STATUS_INTERNAL_ERROR;
2882 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2884 if (!smbXcli_conn_is_connected(state->conn)) {
2885 return NT_STATUS_CONNECTION_DISCONNECTED;
2888 if ((state->conn->protocol != PROTOCOL_NONE) &&
2889 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2890 return NT_STATUS_REVISION_MISMATCH;
2893 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2894 if (opcode == SMB2_OP_CANCEL) {
2898 avail = UINT64_MAX - state->conn->smb2.mid;
2900 return NT_STATUS_CONNECTION_ABORTED;
2903 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2904 uint32_t max_dyn_len = 1;
2906 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
2907 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
2909 charge = (max_dyn_len - 1)/ 65536 + 1;
2914 charge = MAX(state->smb2.credit_charge, charge);
2916 avail = MIN(avail, state->conn->smb2.cur_credits);
2917 if (avail < charge) {
2918 return NT_STATUS_INTERNAL_ERROR;
2922 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2923 credits = state->conn->smb2.max_credits -
2924 state->conn->smb2.cur_credits;
2926 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2930 mid = state->conn->smb2.mid;
2931 state->conn->smb2.mid += charge;
2932 state->conn->smb2.cur_credits -= charge;
2934 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2935 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2937 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2938 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2941 if (state->session && encryption_key == NULL) {
2943 * We prefer the channel signing key if it is
2946 if (state->smb2.should_sign) {
2947 signing_key = &state->session->smb2_channel.signing_key;
2951 * If it is a channel binding, we already have the main
2952 * signing key and try that one.
2954 if (signing_key && signing_key->length == 0) {
2955 signing_key = &state->session->smb2->signing_key;
2959 * If we do not have any session key yet, we skip the
2960 * signing of SMB2_OP_SESSSETUP requests.
2962 if (signing_key && signing_key->length == 0) {
2968 iov[num_iov].iov_base = state->smb2.hdr;
2969 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2972 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2973 iov[num_iov].iov_len = state->smb2.fixed_len;
2976 if (state->smb2.dyn != NULL) {
2977 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2978 iov[num_iov].iov_len = state->smb2.dyn_len;
2982 reqlen = sizeof(state->smb2.hdr);
2983 reqlen += state->smb2.fixed_len;
2984 reqlen += state->smb2.dyn_len;
2986 if (i < num_reqs-1) {
2987 if ((reqlen % 8) > 0) {
2988 uint8_t pad = 8 - (reqlen % 8);
2989 iov[num_iov].iov_base = state->smb2.pad;
2990 iov[num_iov].iov_len = pad;
2994 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2997 state->smb2.encryption_session_id = encryption_session_id;
2999 if (signing_key != NULL) {
3002 status = smb2_signing_sign_pdu(*signing_key,
3003 state->session->conn->protocol,
3004 &iov[hdr_iov], num_iov - hdr_iov);
3005 if (!NT_STATUS_IS_OK(status)) {
3012 ret = smbXcli_req_set_pending(reqs[i]);
3014 return NT_STATUS_NO_MEMORY;
3018 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
3019 _smb_setlen_tcp(state->length_hdr, nbt_len);
3020 iov[0].iov_base = state->length_hdr;
3021 iov[0].iov_len = sizeof(state->length_hdr);
3023 if (encryption_key != NULL) {
3025 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
3029 buf = talloc_array(iov, uint8_t, buflen);
3031 return NT_STATUS_NO_MEMORY;
3035 * We copy the buffers before encrypting them,
3036 * this is at least currently needed for the
3037 * to keep state->smb2.hdr.
3039 * Also the callers may expect there buffers
3042 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3043 struct iovec *v = &iov[vi];
3044 const uint8_t *o = (const uint8_t *)v->iov_base;
3046 memcpy(buf, o, v->iov_len);
3047 v->iov_base = (void *)buf;
3051 status = smb2_signing_encrypt_pdu(*encryption_key,
3052 state->conn->protocol,
3053 &iov[tf_iov], num_iov - tf_iov);
3054 if (!NT_STATUS_IS_OK(status)) {
3059 if (state->conn->dispatch_incoming == NULL) {
3060 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3063 subreq = writev_send(state, state->ev, state->conn->outgoing,
3064 state->conn->write_fd, false, iov, num_iov);
3065 if (subreq == NULL) {
3066 return NT_STATUS_NO_MEMORY;
3068 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3069 return NT_STATUS_OK;
3072 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3074 struct smbXcli_req_state *state =
3075 tevent_req_data(req,
3076 struct smbXcli_req_state);
3078 state->smb2.credit_charge = charge;
3081 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3082 struct tevent_context *ev,
3083 struct smbXcli_conn *conn,
3085 uint32_t additional_flags,
3086 uint32_t clear_flags,
3087 uint32_t timeout_msec,
3088 struct smbXcli_tcon *tcon,
3089 struct smbXcli_session *session,
3090 const uint8_t *fixed,
3094 uint32_t max_dyn_len)
3096 struct tevent_req *req;
3099 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3100 additional_flags, clear_flags,
3109 if (!tevent_req_is_in_progress(req)) {
3110 return tevent_req_post(req, ev);
3112 status = smb2cli_req_compound_submit(&req, 1);
3113 if (tevent_req_nterror(req, status)) {
3114 return tevent_req_post(req, ev);
3119 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3121 struct tevent_req *req =
3122 tevent_req_callback_data(subreq,
3124 struct smbXcli_req_state *state =
3125 tevent_req_data(req,
3126 struct smbXcli_req_state);
3130 nwritten = writev_recv(subreq, &err);
3131 TALLOC_FREE(subreq);
3132 if (nwritten == -1) {
3133 /* here, we need to notify all pending requests */
3134 NTSTATUS status = map_nt_error_from_unix_common(err);
3135 smbXcli_conn_disconnect(state->conn, status);
3140 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3143 TALLOC_CTX *mem_ctx,
3144 struct iovec **piov, int *pnum_iov)
3149 uint8_t *first_hdr = buf;
3150 size_t verified_buflen = 0;
3154 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3156 return NT_STATUS_NO_MEMORY;
3159 while (taken < buflen) {
3160 size_t len = buflen - taken;
3161 uint8_t *hdr = first_hdr + taken;
3164 size_t next_command_ofs;
3166 struct iovec *iov_tmp;
3168 if (verified_buflen > taken) {
3169 len = verified_buflen - taken;
3176 DEBUG(10, ("%d bytes left, expected at least %d\n",
3180 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3181 struct smbXcli_session *s;
3183 struct iovec tf_iov[2];
3187 if (len < SMB2_TF_HDR_SIZE) {
3188 DEBUG(10, ("%d bytes left, expected at least %d\n",
3189 (int)len, SMB2_TF_HDR_SIZE));
3193 tf_len = SMB2_TF_HDR_SIZE;
3196 hdr = first_hdr + taken;
3197 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3198 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3200 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3201 DEBUG(10, ("%d bytes left, expected at least %d\n",
3203 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3208 for (; s; s = s->next) {
3209 if (s->smb2->session_id != uid) {
3216 DEBUG(10, ("unknown session_id %llu\n",
3217 (unsigned long long)uid));
3221 tf_iov[0].iov_base = (void *)tf;
3222 tf_iov[0].iov_len = tf_len;
3223 tf_iov[1].iov_base = (void *)hdr;
3224 tf_iov[1].iov_len = enc_len;
3226 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3229 if (!NT_STATUS_IS_OK(status)) {
3234 verified_buflen = taken + enc_len;
3239 * We need the header plus the body length field
3242 if (len < SMB2_HDR_BODY + 2) {
3243 DEBUG(10, ("%d bytes left, expected at least %d\n",
3244 (int)len, SMB2_HDR_BODY));
3247 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3248 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3252 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3253 DEBUG(10, ("Got HDR len %d, expected %d\n",
3254 SVAL(hdr, 4), SMB2_HDR_BODY));
3259 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3260 body_size = SVAL(hdr, SMB2_HDR_BODY);
3262 if (next_command_ofs != 0) {
3263 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3266 if (next_command_ofs > full_size) {
3269 full_size = next_command_ofs;
3271 if (body_size < 2) {
3274 body_size &= 0xfffe;
3276 if (body_size > (full_size - SMB2_HDR_BODY)) {
3280 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3282 if (iov_tmp == NULL) {
3284 return NT_STATUS_NO_MEMORY;
3287 cur = &iov[num_iov];
3290 cur[0].iov_base = tf;
3291 cur[0].iov_len = tf_len;
3292 cur[1].iov_base = hdr;
3293 cur[1].iov_len = SMB2_HDR_BODY;
3294 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3295 cur[2].iov_len = body_size;
3296 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3297 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3303 *pnum_iov = num_iov;
3304 return NT_STATUS_OK;
3308 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3311 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3314 size_t num_pending = talloc_array_length(conn->pending);
3317 for (i=0; i<num_pending; i++) {
3318 struct tevent_req *req = conn->pending[i];
3319 struct smbXcli_req_state *state =
3320 tevent_req_data(req,
3321 struct smbXcli_req_state);
3323 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3330 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3331 TALLOC_CTX *tmp_mem,
3334 struct tevent_req *req;
3335 struct smbXcli_req_state *state = NULL;
3340 struct smbXcli_session *last_session = NULL;
3341 size_t inbuf_len = smb_len_tcp(inbuf);
3343 status = smb2cli_inbuf_parse_compound(conn,
3344 inbuf + NBT_HDR_SIZE,
3348 if (!NT_STATUS_IS_OK(status)) {
3352 for (i=0; i<num_iov; i+=4) {
3353 uint8_t *inbuf_ref = NULL;
3354 struct iovec *cur = &iov[i];
3355 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3356 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3357 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3358 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3359 uint16_t req_opcode;
3361 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3362 uint32_t new_credits;
3363 struct smbXcli_session *session = NULL;
3364 const DATA_BLOB *signing_key = NULL;
3365 bool was_encrypted = false;
3367 new_credits = conn->smb2.cur_credits;
3368 new_credits += credits;
3369 if (new_credits > UINT16_MAX) {
3370 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3372 conn->smb2.cur_credits += credits;
3374 req = smb2cli_conn_find_pending(conn, mid);
3376 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3378 state = tevent_req_data(req, struct smbXcli_req_state);
3380 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3381 if (opcode != req_opcode) {
3382 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3384 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3386 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3387 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3390 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3391 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3392 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3393 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3395 if (state->smb2.got_async) {
3396 /* We only expect one STATUS_PENDING response */
3397 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3399 state->smb2.got_async = true;
3402 * async interim responses are not signed,
3403 * even if the SMB2_HDR_FLAG_SIGNED flag
3406 req_flags |= SMB2_HDR_FLAG_ASYNC;
3407 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3408 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3410 if (state->smb2.notify_async) {
3411 tevent_req_defer_callback(req, state->ev);
3412 tevent_req_notify_callback(req);
3417 session = state->session;
3418 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3419 session = last_session;
3421 last_session = session;
3423 if (state->smb2.should_sign) {
3424 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3425 return NT_STATUS_ACCESS_DENIED;
3429 if (flags & SMB2_HDR_FLAG_SIGNED) {
3430 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3432 if (session == NULL) {
3433 struct smbXcli_session *s;
3435 s = state->conn->sessions;
3436 for (; s; s = s->next) {
3437 if (s->smb2->session_id != uid) {
3446 if (session == NULL) {
3447 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3450 last_session = session;
3451 signing_key = &session->smb2_channel.signing_key;
3454 if (opcode == SMB2_OP_SESSSETUP) {
3456 * We prefer the channel signing key, if it is
3459 * If we do not have a channel signing key yet,
3460 * we try the main signing key, if it is not
3461 * the final response.
3463 if (signing_key && signing_key->length == 0 &&
3464 !NT_STATUS_IS_OK(status)) {
3465 signing_key = &session->smb2->signing_key;
3468 if (signing_key && signing_key->length == 0) {
3470 * If we do not have a session key to
3471 * verify the signature, we defer the
3472 * signing check to the caller.
3474 * The caller gets NT_STATUS_OK, it
3476 * smb2cli_session_set_session_key()
3478 * smb2cli_session_set_channel_key()
3479 * which will check the signature
3480 * with the channel signing key.
3486 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3487 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3488 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3491 * If the response was encrypted in a SMB2_TRANSFORM
3492 * pdu, which belongs to the correct session,
3493 * we do not need to do signing checks
3495 * It could be the session the response belongs to
3496 * or the session that was used to encrypt the
3497 * SMB2_TRANSFORM request.
3499 if ((session && session->smb2->session_id == uid) ||
3500 (state->smb2.encryption_session_id == uid)) {
3502 was_encrypted = true;
3506 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3508 * if the server returns NT_STATUS_USER_SESSION_DELETED
3509 * the response is not signed and we should
3510 * propagate the NT_STATUS_USER_SESSION_DELETED
3511 * status to the caller.
3513 state->smb2.signing_skipped = true;
3517 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3519 * if the server returns
3520 * NT_STATUS_INVALID_PARAMETER
3521 * the response might not be encrypted.
3523 if (state->smb2.should_encrypt && !was_encrypted) {
3524 state->smb2.signing_skipped = true;
3529 if (state->smb2.should_encrypt && !was_encrypted) {
3530 if (!state->smb2.signing_skipped) {
3531 return NT_STATUS_ACCESS_DENIED;
3535 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3536 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3537 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3539 * if the server returns
3540 * NT_STATUS_NETWORK_NAME_DELETED
3541 * NT_STATUS_FILE_CLOSED
3542 * NT_STATUS_INVALID_PARAMETER
3543 * the response might not be signed
3544 * as this happens before the signing checks.
3546 * If server echos the signature (or all zeros)
3547 * we should report the status from the server
3553 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3554 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3557 state->smb2.signing_skipped = true;
3563 static const uint8_t zeros[16];
3565 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3569 state->smb2.signing_skipped = true;
3576 status = smb2_signing_check_pdu(*signing_key,
3577 state->conn->protocol,
3579 if (!NT_STATUS_IS_OK(status)) {
3581 * If the signing check fails, we disconnect
3588 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3589 (session != NULL) && session->disconnect_expired)
3592 * this should be a short term hack
3593 * until the upper layers have implemented
3594 * re-authentication.
3599 smbXcli_req_unset_pending(req);
3602 * There might be more than one response
3603 * we need to defer the notifications
3605 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3610 tevent_req_defer_callback(req, state->ev);
3614 * Note: here we use talloc_reference() in a way
3615 * that does not expose it to the caller.
3617 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3618 if (tevent_req_nomem(inbuf_ref, req)) {
3622 /* copy the related buffers */
3623 state->smb2.recv_iov[0] = cur[1];
3624 state->smb2.recv_iov[1] = cur[2];
3625 state->smb2.recv_iov[2] = cur[3];
3627 tevent_req_done(req);
3631 return NT_STATUS_RETRY;
3634 return NT_STATUS_OK;
3637 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3638 struct iovec **piov,
3639 const struct smb2cli_req_expected_response *expected,
3640 size_t num_expected)
3642 struct smbXcli_req_state *state =
3643 tevent_req_data(req,
3644 struct smbXcli_req_state);
3647 bool found_status = false;
3648 bool found_size = false;
3655 if (tevent_req_is_in_progress(req) && state->smb2.got_async) {
3656 return STATUS_PENDING;
3659 if (tevent_req_is_nterror(req, &status)) {
3660 for (i=0; i < num_expected; i++) {
3661 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3662 found_status = true;
3668 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3674 if (num_expected == 0) {
3675 found_status = true;
3679 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3680 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3682 for (i=0; i < num_expected; i++) {
3683 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3687 found_status = true;
3688 if (expected[i].body_size == 0) {
3693 if (expected[i].body_size == body_size) {
3699 if (!found_status) {
3703 if (state->smb2.signing_skipped) {
3704 if (num_expected > 0) {
3705 return NT_STATUS_ACCESS_DENIED;
3707 if (!NT_STATUS_IS_ERR(status)) {
3708 return NT_STATUS_ACCESS_DENIED;
3713 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3717 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3723 NTSTATUS smb2cli_req_get_sent_iov(struct tevent_req *req,
3724 struct iovec *sent_iov)
3726 struct smbXcli_req_state *state =
3727 tevent_req_data(req,
3728 struct smbXcli_req_state);
3730 if (tevent_req_is_in_progress(req)) {
3731 return STATUS_PENDING;
3734 sent_iov[0].iov_base = state->smb2.hdr;
3735 sent_iov[0].iov_len = sizeof(state->smb2.hdr);
3737 sent_iov[1].iov_base = discard_const(state->smb2.fixed);
3738 sent_iov[1].iov_len = state->smb2.fixed_len;
3740 if (state->smb2.dyn != NULL) {
3741 sent_iov[2].iov_base = discard_const(state->smb2.dyn);
3742 sent_iov[2].iov_len = state->smb2.dyn_len;
3744 sent_iov[2].iov_base = NULL;
3745 sent_iov[2].iov_len = 0;
3748 return NT_STATUS_OK;
3751 static const struct {
3752 enum protocol_types proto;
3753 const char *smb1_name;
3754 } smb1cli_prots[] = {
3755 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3756 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3757 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3758 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3759 {PROTOCOL_LANMAN2, "LM1.2X002"},
3760 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3761 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3762 {PROTOCOL_LANMAN2, "Samba"},
3763 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3764 {PROTOCOL_NT1, "NT LM 0.12"},
3765 {PROTOCOL_SMB2_02, "SMB 2.002"},
3766 {PROTOCOL_SMB2_10, "SMB 2.???"},
3769 static const struct {
3770 enum protocol_types proto;
3771 uint16_t smb2_dialect;
3772 } smb2cli_prots[] = {
3773 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3774 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3775 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3776 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3777 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3778 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
3779 {PROTOCOL_SMB3_10, SMB3_DIALECT_REVISION_310},
3782 struct smbXcli_negprot_state {
3783 struct smbXcli_conn *conn;
3784 struct tevent_context *ev;
3785 uint32_t timeout_msec;
3789 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3793 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3794 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3795 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3796 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3797 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3798 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3802 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3803 struct tevent_context *ev,
3804 struct smbXcli_conn *conn,
3805 uint32_t timeout_msec,
3806 enum protocol_types min_protocol,
3807 enum protocol_types max_protocol)
3809 struct tevent_req *req, *subreq;
3810 struct smbXcli_negprot_state *state;
3812 req = tevent_req_create(mem_ctx, &state,
3813 struct smbXcli_negprot_state);
3819 state->timeout_msec = timeout_msec;
3821 if (min_protocol == PROTOCOL_NONE) {
3822 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3823 return tevent_req_post(req, ev);
3826 if (max_protocol == PROTOCOL_NONE) {
3827 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3828 return tevent_req_post(req, ev);
3831 if (min_protocol > max_protocol) {
3832 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3833 return tevent_req_post(req, ev);
3836 conn->min_protocol = min_protocol;
3837 conn->max_protocol = max_protocol;
3838 conn->protocol = PROTOCOL_NONE;
3840 if ((min_protocol < PROTOCOL_SMB2_02) &&
3841 (max_protocol < PROTOCOL_SMB2_02)) {
3845 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3847 subreq = smbXcli_negprot_smb1_subreq(state);
3848 if (tevent_req_nomem(subreq, req)) {
3849 return tevent_req_post(req, ev);
3851 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3855 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3856 (max_protocol >= PROTOCOL_SMB2_02)) {
3860 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3862 subreq = smbXcli_negprot_smb2_subreq(state);
3863 if (tevent_req_nomem(subreq, req)) {
3864 return tevent_req_post(req, ev);
3866 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3871 * We send an SMB1 negprot with the SMB2 dialects
3872 * and expect a SMB1 or a SMB2 response.
3874 * smbXcli_negprot_dispatch_incoming() will fix the
3875 * callback to match protocol of the response.
3877 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3879 subreq = smbXcli_negprot_smb1_subreq(state);
3880 if (tevent_req_nomem(subreq, req)) {
3881 return tevent_req_post(req, ev);
3883 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3887 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3889 struct tevent_req *req =
3890 tevent_req_callback_data(subreq,
3895 * we just want the low level error
3897 status = tevent_req_simple_recv_ntstatus(subreq);
3898 TALLOC_FREE(subreq);
3899 if (tevent_req_nterror(req, status)) {
3903 /* this should never happen */
3904 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3907 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3910 DATA_BLOB bytes = data_blob_null;
3914 /* setup the protocol strings */
3915 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3919 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
3923 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
3927 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3933 * We now it is already ascii and
3934 * we want NULL termination.
3936 ok = data_blob_append(state, &bytes,
3937 smb1cli_prots[i].smb1_name,
3938 strlen(smb1cli_prots[i].smb1_name)+1);
3944 smb1cli_req_flags(state->conn->max_protocol,
3945 state->conn->smb1.client.capabilities,
3950 return smb1cli_req_send(state, state->ev, state->conn,
3954 state->timeout_msec,
3955 0xFFFE, 0, NULL, /* pid, tid, session */
3956 0, NULL, /* wct, vwv */
3957 bytes.length, bytes.data);
3960 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3962 struct tevent_req *req =
3963 tevent_req_callback_data(subreq,
3965 struct smbXcli_negprot_state *state =
3966 tevent_req_data(req,
3967 struct smbXcli_negprot_state);
3968 struct smbXcli_conn *conn = state->conn;
3969 struct iovec *recv_iov = NULL;
3978 size_t num_prots = 0;
3980 uint32_t client_capabilities = conn->smb1.client.capabilities;
3981 uint32_t both_capabilities;
3982 uint32_t server_capabilities = 0;
3983 uint32_t capabilities;
3984 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3985 uint32_t server_max_xmit = 0;
3987 uint32_t server_max_mux = 0;
3988 uint16_t server_security_mode = 0;
3989 uint32_t server_session_key = 0;
3990 bool server_readbraw = false;
3991 bool server_writebraw = false;
3992 bool server_lockread = false;
3993 bool server_writeunlock = false;
3994 struct GUID server_guid = GUID_zero();
3995 DATA_BLOB server_gss_blob = data_blob_null;
3996 uint8_t server_challenge[8];
3997 char *server_workgroup = NULL;
3998 char *server_name = NULL;
3999 int server_time_zone = 0;
4000 NTTIME server_system_time = 0;
4001 static const struct smb1cli_req_expected_response expected[] = {
4003 .status = NT_STATUS_OK,
4004 .wct = 0x11, /* NT1 */
4007 .status = NT_STATUS_OK,
4008 .wct = 0x0D, /* LM */
4011 .status = NT_STATUS_OK,
4012 .wct = 0x01, /* CORE */
4016 ZERO_STRUCT(server_challenge);
4018 status = smb1cli_req_recv(subreq, state,
4023 NULL, /* pvwv_offset */
4026 NULL, /* pbytes_offset */
4028 expected, ARRAY_SIZE(expected));
4029 TALLOC_FREE(subreq);
4030 if (tevent_req_nterror(req, status)) {
4034 flags = CVAL(inhdr, HDR_FLG);
4036 protnum = SVAL(vwv, 0);
4038 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4039 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4043 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4047 if (protnum != num_prots) {
4052 conn->protocol = smb1cli_prots[i].proto;
4056 if (conn->protocol == PROTOCOL_NONE) {
4057 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4061 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
4062 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4063 "and the selected protocol level doesn't support it.\n"));
4064 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4068 if (flags & FLAG_SUPPORT_LOCKREAD) {
4069 server_lockread = true;
4070 server_writeunlock = true;
4073 if (conn->protocol >= PROTOCOL_NT1) {
4074 const char *client_signing = NULL;
4075 bool server_mandatory = false;
4076 bool server_allowed = false;
4077 const char *server_signing = NULL;
4082 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4087 server_security_mode = CVAL(vwv + 1, 0);
4088 server_max_mux = SVAL(vwv + 1, 1);
4089 server_max_xmit = IVAL(vwv + 3, 1);
4090 server_session_key = IVAL(vwv + 7, 1);
4091 server_time_zone = SVALS(vwv + 15, 1);
4092 server_time_zone *= 60;
4093 /* this time arrives in real GMT */
4094 server_system_time = BVAL(vwv + 11, 1);
4095 server_capabilities = IVAL(vwv + 9, 1);
4097 key_len = CVAL(vwv + 16, 1);
4099 if (server_capabilities & CAP_RAW_MODE) {
4100 server_readbraw = true;
4101 server_writebraw = true;
4103 if (server_capabilities & CAP_LOCK_AND_READ) {
4104 server_lockread = true;
4107 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4108 DATA_BLOB blob1, blob2;
4110 if (num_bytes < 16) {
4111 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4115 blob1 = data_blob_const(bytes, 16);
4116 status = GUID_from_data_blob(&blob1, &server_guid);
4117 if (tevent_req_nterror(req, status)) {
4121 blob1 = data_blob_const(bytes+16, num_bytes-16);
4122 blob2 = data_blob_dup_talloc(state, blob1);
4123 if (blob1.length > 0 &&
4124 tevent_req_nomem(blob2.data, req)) {
4127 server_gss_blob = blob2;
4129 DATA_BLOB blob1, blob2;
4131 if (num_bytes < key_len) {
4132 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4136 if (key_len != 0 && key_len != 8) {
4137 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4142 memcpy(server_challenge, bytes, 8);
4145 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4146 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4147 if (blob1.length > 0) {
4150 len = utf16_len_n(blob1.data,
4154 ok = convert_string_talloc(state,
4162 status = map_nt_error_from_unix_common(errno);
4163 tevent_req_nterror(req, status);
4168 blob2.data += blob1.length;
4169 blob2.length -= blob1.length;
4170 if (blob2.length > 0) {
4173 len = utf16_len_n(blob1.data,
4177 ok = convert_string_talloc(state,
4185 status = map_nt_error_from_unix_common(errno);
4186 tevent_req_nterror(req, status);
4192 client_signing = "disabled";
4193 if (conn->allow_signing) {
4194 client_signing = "allowed";
4196 if (conn->mandatory_signing) {
4197 client_signing = "required";
4200 server_signing = "not supported";
4201 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4202 server_signing = "supported";
4203 server_allowed = true;
4204 } else if (conn->mandatory_signing) {
4206 * We have mandatory signing as client
4207 * lets assume the server will look at our
4208 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4209 * flag in the session setup
4211 server_signing = "not announced";
4212 server_allowed = true;
4214 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4215 server_signing = "required";
4216 server_mandatory = true;
4219 ok = smb_signing_set_negotiated(conn->smb1.signing,
4223 DEBUG(1,("cli_negprot: SMB signing is required, "
4224 "but client[%s] and server[%s] mismatch\n",
4225 client_signing, server_signing));
4226 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4230 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4236 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4240 server_security_mode = SVAL(vwv + 1, 0);
4241 server_max_xmit = SVAL(vwv + 2, 0);
4242 server_max_mux = SVAL(vwv + 3, 0);
4243 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4244 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4245 server_session_key = IVAL(vwv + 6, 0);
4246 server_time_zone = SVALS(vwv + 10, 0);
4247 server_time_zone *= 60;
4248 /* this time is converted to GMT by make_unix_date */
4249 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4250 unix_to_nt_time(&server_system_time, t);
4251 key_len = SVAL(vwv + 11, 0);
4253 if (num_bytes < key_len) {
4254 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4258 if (key_len != 0 && key_len != 8) {
4259 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4264 memcpy(server_challenge, bytes, 8);
4267 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4268 if (blob1.length > 0) {
4272 len = utf16_len_n(blob1.data,
4276 ok = convert_string_talloc(state,
4284 status = map_nt_error_from_unix_common(errno);
4285 tevent_req_nterror(req, status);
4291 /* the old core protocol */
4292 server_time_zone = get_time_zone(time(NULL));
4293 server_max_xmit = 1024;
4297 if (server_max_xmit < 1024) {
4298 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4302 if (server_max_mux < 1) {
4303 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4308 * Now calculate the negotiated capabilities
4309 * based on the mask for:
4310 * - client only flags
4311 * - flags used in both directions
4312 * - server only flags
4314 both_capabilities = client_capabilities & server_capabilities;
4315 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4316 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4317 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4319 max_xmit = MIN(client_max_xmit, server_max_xmit);
4321 conn->smb1.server.capabilities = server_capabilities;
4322 conn->smb1.capabilities = capabilities;
4324 conn->smb1.server.max_xmit = server_max_xmit;
4325 conn->smb1.max_xmit = max_xmit;
4327 conn->smb1.server.max_mux = server_max_mux;
4329 conn->smb1.server.security_mode = server_security_mode;
4331 conn->smb1.server.readbraw = server_readbraw;
4332 conn->smb1.server.writebraw = server_writebraw;
4333 conn->smb1.server.lockread = server_lockread;
4334 conn->smb1.server.writeunlock = server_writeunlock;
4336 conn->smb1.server.session_key = server_session_key;
4338 talloc_steal(conn, server_gss_blob.data);
4339 conn->smb1.server.gss_blob = server_gss_blob;
4340 conn->smb1.server.guid = server_guid;
4341 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4342 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4343 conn->smb1.server.name = talloc_move(conn, &server_name);
4345 conn->smb1.server.time_zone = server_time_zone;
4346 conn->smb1.server.system_time = server_system_time;
4348 tevent_req_done(req);
4351 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4355 uint16_t dialect_count = 0;
4357 buf = state->smb2.dyn;
4358 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4359 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4363 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4367 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4371 buf = state->smb2.fixed;
4373 SSVAL(buf, 2, dialect_count);
4374 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4375 SSVAL(buf, 6, 0); /* Reserved */
4376 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
4377 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4379 SIVAL(buf, 8, 0); /* Capabilities */
4381 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4385 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4387 if (!NT_STATUS_IS_OK(status)) {
4390 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4392 memset(buf+12, 0, 16); /* ClientGuid */
4394 SBVAL(buf, 28, 0); /* ClientStartTime */
4396 return smb2cli_req_send(state, state->ev,
4397 state->conn, SMB2_OP_NEGPROT,
4399 state->timeout_msec,
4400 NULL, NULL, /* tcon, session */
4401 state->smb2.fixed, sizeof(state->smb2.fixed),
4402 state->smb2.dyn, dialect_count*2,
4403 UINT16_MAX); /* max_dyn_len */
4406 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4408 struct tevent_req *req =
4409 tevent_req_callback_data(subreq,
4411 struct smbXcli_negprot_state *state =
4412 tevent_req_data(req,
4413 struct smbXcli_negprot_state);
4414 struct smbXcli_conn *conn = state->conn;
4415 size_t security_offset, security_length;
4421 uint16_t dialect_revision;
4422 static const struct smb2cli_req_expected_response expected[] = {
4424 .status = NT_STATUS_OK,
4429 status = smb2cli_req_recv(subreq, state, &iov,
4430 expected, ARRAY_SIZE(expected));
4431 TALLOC_FREE(subreq);
4432 if (tevent_req_nterror(req, status)) {
4436 body = (uint8_t *)iov[1].iov_base;
4438 dialect_revision = SVAL(body, 4);
4440 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4441 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4445 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4449 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4453 conn->protocol = smb2cli_prots[i].proto;
4457 if (conn->protocol == PROTOCOL_NONE) {
4458 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
4459 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4463 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4464 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4468 /* make sure we do not loop forever */
4469 state->conn->min_protocol = PROTOCOL_SMB2_02;
4472 * send a SMB2 negprot, in order to negotiate
4475 subreq = smbXcli_negprot_smb2_subreq(state);
4476 if (tevent_req_nomem(subreq, req)) {
4479 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4483 conn->smb2.server.security_mode = SVAL(body, 2);
4485 blob = data_blob_const(body + 8, 16);
4486 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4487 if (tevent_req_nterror(req, status)) {
4491 conn->smb2.server.capabilities = IVAL(body, 24);
4492 conn->smb2.server.max_trans_size= IVAL(body, 28);
4493 conn->smb2.server.max_read_size = IVAL(body, 32);
4494 conn->smb2.server.max_write_size= IVAL(body, 36);
4495 conn->smb2.server.system_time = BVAL(body, 40);
4496 conn->smb2.server.start_time = BVAL(body, 48);
4498 security_offset = SVAL(body, 56);
4499 security_length = SVAL(body, 58);
4501 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4502 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4506 if (security_length > iov[2].iov_len) {
4507 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4511 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4514 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4518 tevent_req_done(req);
4521 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4522 TALLOC_CTX *tmp_mem,
4525 size_t num_pending = talloc_array_length(conn->pending);
4526 struct tevent_req *subreq;
4527 struct smbXcli_req_state *substate;
4528 struct tevent_req *req;
4529 uint32_t protocol_magic;
4530 size_t inbuf_len = smb_len_nbt(inbuf);
4532 if (num_pending != 1) {
4533 return NT_STATUS_INTERNAL_ERROR;
4536 if (inbuf_len < 4) {
4537 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4540 subreq = conn->pending[0];
4541 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4542 req = tevent_req_callback_data(subreq, struct tevent_req);
4544 protocol_magic = IVAL(inbuf, 4);
4546 switch (protocol_magic) {
4548 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4549 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4550 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4553 if (substate->smb2.recv_iov == NULL) {
4555 * For the SMB1 negprot we have move it.
4557 substate->smb2.recv_iov = substate->smb1.recv_iov;
4558 substate->smb1.recv_iov = NULL;
4562 * we got an SMB2 answer, which consumed sequence number 0
4563 * so we need to use 1 as the next one.
4565 * we also need to set the current credits to 0
4566 * as we consumed the initial one. The SMB2 answer
4567 * hopefully grant us a new credit.
4570 conn->smb2.cur_credits = 0;
4571 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4572 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4573 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4576 DEBUG(10, ("Got non-SMB PDU\n"));
4577 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4580 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4582 return tevent_req_simple_recv_ntstatus(req);
4585 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4586 uint32_t timeout_msec,
4587 enum protocol_types min_protocol,
4588 enum protocol_types max_protocol)
4590 TALLOC_CTX *frame = talloc_stackframe();
4591 struct tevent_context *ev;
4592 struct tevent_req *req;
4593 NTSTATUS status = NT_STATUS_NO_MEMORY;
4596 if (smbXcli_conn_has_async_calls(conn)) {
4598 * Can't use sync call while an async call is in flight
4600 status = NT_STATUS_INVALID_PARAMETER_MIX;
4603 ev = samba_tevent_context_init(frame);
4607 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4608 min_protocol, max_protocol);
4612 ok = tevent_req_poll(req, ev);
4614 status = map_nt_error_from_unix_common(errno);
4617 status = smbXcli_negprot_recv(req);
4623 struct smb2cli_validate_negotiate_info_state {
4624 struct smbXcli_conn *conn;
4625 DATA_BLOB in_input_buffer;
4626 DATA_BLOB in_output_buffer;
4627 DATA_BLOB out_input_buffer;
4628 DATA_BLOB out_output_buffer;
4632 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq);
4634 struct tevent_req *smb2cli_validate_negotiate_info_send(TALLOC_CTX *mem_ctx,
4635 struct tevent_context *ev,
4636 struct smbXcli_conn *conn,
4637 uint32_t timeout_msec,
4638 struct smbXcli_session *session,
4639 struct smbXcli_tcon *tcon)
4641 struct tevent_req *req;
4642 struct smb2cli_validate_negotiate_info_state *state;
4644 uint16_t dialect_count = 0;
4645 struct tevent_req *subreq;
4646 bool _save_should_sign;
4649 req = tevent_req_create(mem_ctx, &state,
4650 struct smb2cli_validate_negotiate_info_state);
4656 state->in_input_buffer = data_blob_talloc_zero(state,
4657 4 + 16 + 1 + 1 + 2);
4658 if (tevent_req_nomem(state->in_input_buffer.data, req)) {
4659 return tevent_req_post(req, ev);
4661 buf = state->in_input_buffer.data;
4663 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
4664 SIVAL(buf, 0, conn->smb2.client.capabilities);
4666 SIVAL(buf, 0, 0); /* Capabilities */
4668 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4672 status = GUID_to_ndr_blob(&conn->smb2.client.guid,
4674 if (!NT_STATUS_IS_OK(status)) {
4677 memcpy(buf+4, blob.data, 16); /* ClientGuid */
4679 memset(buf+4, 0, 16); /* ClientGuid */
4681 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
4682 SCVAL(buf, 20, conn->smb2.client.security_mode);
4686 SCVAL(buf, 21, 0); /* reserved */
4688 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4692 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4696 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4700 if (smb2cli_prots[i].proto == state->conn->protocol) {
4701 state->dialect = smb2cli_prots[i].smb2_dialect;
4704 ofs = state->in_input_buffer.length;
4705 ok = data_blob_realloc(state, &state->in_input_buffer,
4708 tevent_req_oom(req);
4709 return tevent_req_post(req, ev);
4712 buf = state->in_input_buffer.data;
4713 SSVAL(buf, ofs, smb2cli_prots[i].smb2_dialect);
4717 buf = state->in_input_buffer.data;
4718 SSVAL(buf, 22, dialect_count);
4720 _save_should_sign = smb2cli_tcon_is_signing_on(tcon);
4721 smb2cli_tcon_should_sign(tcon, true);
4722 subreq = smb2cli_ioctl_send(state, ev, conn,
4723 timeout_msec, session, tcon,
4724 UINT64_MAX, /* in_fid_persistent */
4725 UINT64_MAX, /* in_fid_volatile */
4726 FSCTL_VALIDATE_NEGOTIATE_INFO,
4727 0, /* in_max_input_length */
4728 &state->in_input_buffer,
4729 24, /* in_max_output_length */
4730 &state->in_output_buffer,
4731 SMB2_IOCTL_FLAG_IS_FSCTL);
4732 smb2cli_tcon_should_sign(tcon, _save_should_sign);
4733 if (tevent_req_nomem(subreq, req)) {
4734 return tevent_req_post(req, ev);
4736 tevent_req_set_callback(subreq,
4737 smb2cli_validate_negotiate_info_done,
4743 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq)
4745 struct tevent_req *req =
4746 tevent_req_callback_data(subreq,
4748 struct smb2cli_validate_negotiate_info_state *state =
4749 tevent_req_data(req,
4750 struct smb2cli_validate_negotiate_info_state);
4753 uint32_t capabilities;
4754 DATA_BLOB guid_blob;
4755 struct GUID server_guid;
4756 uint16_t security_mode;
4759 status = smb2cli_ioctl_recv(subreq, state,
4760 &state->out_input_buffer,
4761 &state->out_output_buffer);
4762 TALLOC_FREE(subreq);
4763 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
4765 * The response was signed, but not supported
4767 * Older Windows and Samba releases return
4768 * NT_STATUS_FILE_CLOSED.
4770 tevent_req_done(req);
4773 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_DEVICE_REQUEST)) {
4775 * The response was signed, but not supported
4777 * This is returned by the NTVFS based Samba 4.x file server
4780 tevent_req_done(req);
4783 if (NT_STATUS_EQUAL(status, NT_STATUS_FS_DRIVER_REQUIRED)) {
4785 * The response was signed, but not supported
4787 * This is returned by the NTVFS based Samba 4.x file server
4790 tevent_req_done(req);
4793 if (tevent_req_nterror(req, status)) {
4797 if (state->out_output_buffer.length != 24) {
4798 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4802 buf = state->out_output_buffer.data;
4804 capabilities = IVAL(buf, 0);
4805 guid_blob = data_blob_const(buf + 4, 16);
4806 status = GUID_from_data_blob(&guid_blob, &server_guid);
4807 if (tevent_req_nterror(req, status)) {
4810 security_mode = CVAL(buf, 20);
4811 dialect = SVAL(buf, 22);
4813 if (capabilities != state->conn->smb2.server.capabilities) {
4814 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4818 if (!GUID_equal(&server_guid, &state->conn->smb2.server.guid)) {
4819 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4823 if (security_mode != state->conn->smb2.server.security_mode) {
4824 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4828 if (dialect != state->dialect) {
4829 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4833 tevent_req_done(req);
4836 NTSTATUS smb2cli_validate_negotiate_info_recv(struct tevent_req *req)
4838 return tevent_req_simple_recv_ntstatus(req);
4841 static int smbXcli_session_destructor(struct smbXcli_session *session)
4843 if (session->conn == NULL) {
4847 DLIST_REMOVE(session->conn->sessions, session);
4851 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4852 struct smbXcli_conn *conn)
4854 struct smbXcli_session *session;
4856 session = talloc_zero(mem_ctx, struct smbXcli_session);
4857 if (session == NULL) {
4860 session->smb2 = talloc_zero(session, struct smb2cli_session);
4861 if (session->smb2 == NULL) {
4862 talloc_free(session);
4865 talloc_set_destructor(session, smbXcli_session_destructor);
4867 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4868 session->conn = conn;
4873 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
4874 struct smbXcli_session *src)
4876 struct smbXcli_session *session;
4878 session = talloc_zero(mem_ctx, struct smbXcli_session);
4879 if (session == NULL) {
4882 session->smb2 = talloc_zero(session, struct smb2cli_session);
4883 if (session->smb2 == NULL) {
4884 talloc_free(session);
4888 session->conn = src->conn;
4889 *session->smb2 = *src->smb2;
4890 session->smb2_channel = src->smb2_channel;
4891 session->disconnect_expired = src->disconnect_expired;
4893 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
4894 talloc_set_destructor(session, smbXcli_session_destructor);
4899 bool smbXcli_session_is_authenticated(struct smbXcli_session *session)
4901 const DATA_BLOB *application_key;
4903 if (session->conn == NULL) {
4908 * If we have an application key we had a session key negotiated
4911 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4912 application_key = &session->smb2->application_key;
4914 application_key = &session->smb1.application_key;
4917 if (application_key->length == 0) {
4924 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4925 TALLOC_CTX *mem_ctx,
4928 const DATA_BLOB *application_key;
4930 *key = data_blob_null;
4932 if (session->conn == NULL) {
4933 return NT_STATUS_NO_USER_SESSION_KEY;
4936 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4937 application_key = &session->smb2->application_key;
4939 application_key = &session->smb1.application_key;
4942 if (application_key->length == 0) {
4943 return NT_STATUS_NO_USER_SESSION_KEY;
4946 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4947 if (key->data == NULL) {
4948 return NT_STATUS_NO_MEMORY;
4951 return NT_STATUS_OK;
4954 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
4956 session->disconnect_expired = true;
4959 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4961 return session->smb1.session_id;
4964 void smb1cli_session_set_id(struct smbXcli_session *session,
4965 uint16_t session_id)
4967 session->smb1.session_id = session_id;
4970 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4971 const DATA_BLOB _session_key)
4973 struct smbXcli_conn *conn = session->conn;
4974 uint8_t session_key[16];
4977 return NT_STATUS_INVALID_PARAMETER_MIX;
4980 if (session->smb1.application_key.length != 0) {
4982 * TODO: do not allow this...
4984 * return NT_STATUS_INVALID_PARAMETER_MIX;
4986 data_blob_clear_free(&session->smb1.application_key);
4987 session->smb1.protected_key = false;
4990 if (_session_key.length == 0) {
4991 return NT_STATUS_OK;
4994 ZERO_STRUCT(session_key);
4995 memcpy(session_key, _session_key.data,
4996 MIN(_session_key.length, sizeof(session_key)));
4998 session->smb1.application_key = data_blob_talloc(session,
5000 sizeof(session_key));
5001 ZERO_STRUCT(session_key);
5002 if (session->smb1.application_key.data == NULL) {
5003 return NT_STATUS_NO_MEMORY;
5006 session->smb1.protected_key = false;
5008 return NT_STATUS_OK;
5011 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
5013 if (session->smb1.protected_key) {
5014 /* already protected */
5015 return NT_STATUS_OK;
5018 if (session->smb1.application_key.length != 16) {
5019 return NT_STATUS_INVALID_PARAMETER_MIX;
5022 smb_key_derivation(session->smb1.application_key.data,
5023 session->smb1.application_key.length,
5024 session->smb1.application_key.data);
5026 session->smb1.protected_key = true;
5028 return NT_STATUS_OK;
5031 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
5033 struct smbXcli_conn *conn = session->conn;
5034 uint8_t security_mode = 0;
5037 return security_mode;
5040 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
5041 if (conn->mandatory_signing) {
5042 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
5045 return security_mode;
5048 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
5050 return session->smb2->session_id;
5053 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
5055 return session->smb2->session_flags;
5058 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
5059 uint64_t session_id,
5060 uint16_t session_flags)
5062 session->smb2->session_id = session_id;
5063 session->smb2->session_flags = session_flags;
5066 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
5068 session->smb2->channel_sequence += 1;
5071 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session *session,
5072 uint16_t channel_sequence)
5076 prev_cs = session->smb2->channel_sequence;
5077 session->smb2->channel_sequence = channel_sequence;
5082 void smb2cli_session_start_replay(struct smbXcli_session *session)
5084 session->smb2->replay_active = true;
5087 void smb2cli_session_stop_replay(struct smbXcli_session *session)
5089 session->smb2->replay_active = false;
5092 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
5093 const DATA_BLOB _session_key,
5094 const struct iovec *recv_iov)
5096 struct smbXcli_conn *conn = session->conn;
5097 uint16_t no_sign_flags;
5098 uint8_t session_key[16];
5099 bool check_signature = true;
5104 return NT_STATUS_INVALID_PARAMETER_MIX;
5107 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
5108 return NT_STATUS_INVALID_PARAMETER_MIX;
5111 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
5113 if (session->smb2->session_flags & no_sign_flags) {
5114 session->smb2->should_sign = false;
5115 return NT_STATUS_OK;
5118 if (session->smb2->signing_key.length != 0) {
5119 return NT_STATUS_INVALID_PARAMETER_MIX;
5122 ZERO_STRUCT(session_key);
5123 memcpy(session_key, _session_key.data,
5124 MIN(_session_key.length, sizeof(session_key)));
5126 session->smb2->signing_key = data_blob_talloc(session,
5128 sizeof(session_key));
5129 if (session->smb2->signing_key.data == NULL) {
5130 ZERO_STRUCT(session_key);
5131 return NT_STATUS_NO_MEMORY;
5134 if (conn->protocol >= PROTOCOL_SMB2_24) {
5135 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
5136 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
5138 smb2_key_derivation(session_key, sizeof(session_key),
5139 label.data, label.length,
5140 context.data, context.length,
5141 session->smb2->signing_key.data);
5144 session->smb2->encryption_key = data_blob_dup_talloc(session,
5145 session->smb2->signing_key);
5146 if (session->smb2->encryption_key.data == NULL) {
5147 ZERO_STRUCT(session_key);
5148 return NT_STATUS_NO_MEMORY;
5151 if (conn->protocol >= PROTOCOL_SMB2_24) {
5152 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
5153 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
5155 smb2_key_derivation(session_key, sizeof(session_key),
5156 label.data, label.length,
5157 context.data, context.length,
5158 session->smb2->encryption_key.data);
5161 session->smb2->decryption_key = data_blob_dup_talloc(session,
5162 session->smb2->signing_key);
5163 if (session->smb2->decryption_key.data == NULL) {
5164 ZERO_STRUCT(session_key);
5165 return NT_STATUS_NO_MEMORY;
5168 if (conn->protocol >= PROTOCOL_SMB2_24) {
5169 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
5170 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
5172 smb2_key_derivation(session_key, sizeof(session_key),
5173 label.data, label.length,
5174 context.data, context.length,
5175 session->smb2->decryption_key.data);
5178 session->smb2->application_key = data_blob_dup_talloc(session,
5179 session->smb2->signing_key);
5180 if (session->smb2->application_key.data == NULL) {
5181 ZERO_STRUCT(session_key);
5182 return NT_STATUS_NO_MEMORY;
5185 if (conn->protocol >= PROTOCOL_SMB2_24) {
5186 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
5187 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
5189 smb2_key_derivation(session_key, sizeof(session_key),
5190 label.data, label.length,
5191 context.data, context.length,
5192 session->smb2->application_key.data);
5194 ZERO_STRUCT(session_key);
5196 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
5197 session->smb2->signing_key);
5198 if (session->smb2_channel.signing_key.data == NULL) {
5199 return NT_STATUS_NO_MEMORY;
5202 check_signature = conn->mandatory_signing;
5204 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
5205 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
5207 * Sadly some vendors don't sign the
5208 * final SMB2 session setup response
5210 * At least Windows and Samba are always doing this
5211 * if there's a session key available.
5213 * We only check the signature if it's mandatory
5214 * or SMB2_HDR_FLAG_SIGNED is provided.
5216 check_signature = true;
5219 if (check_signature) {
5220 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5221 session->conn->protocol,
5223 if (!NT_STATUS_IS_OK(status)) {
5228 session->smb2->should_sign = false;
5229 session->smb2->should_encrypt = false;
5231 if (conn->desire_signing) {
5232 session->smb2->should_sign = true;
5235 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
5236 session->smb2->should_sign = true;
5239 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
5240 session->smb2->should_encrypt = true;
5243 if (conn->protocol < PROTOCOL_SMB2_24) {
5244 session->smb2->should_encrypt = false;
5247 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
5248 session->smb2->should_encrypt = false;
5251 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
5252 sizeof(session->smb2->nonce_high));
5253 session->smb2->nonce_low = 1;
5255 return NT_STATUS_OK;
5258 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
5259 struct smbXcli_session *session1,
5260 struct smbXcli_conn *conn,
5261 struct smbXcli_session **_session2)
5263 struct smbXcli_session *session2;
5265 if (session1->smb2->signing_key.length == 0) {
5266 return NT_STATUS_INVALID_PARAMETER_MIX;
5270 return NT_STATUS_INVALID_PARAMETER_MIX;
5273 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
5274 if (session2 == NULL) {
5275 return NT_STATUS_NO_MEMORY;
5277 session2->smb2 = talloc_reference(session2, session1->smb2);
5278 if (session2->smb2 == NULL) {
5279 talloc_free(session2);
5280 return NT_STATUS_NO_MEMORY;
5283 talloc_set_destructor(session2, smbXcli_session_destructor);
5284 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
5285 session2->conn = conn;
5287 *_session2 = session2;
5288 return NT_STATUS_OK;
5291 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
5292 const DATA_BLOB _channel_key,
5293 const struct iovec *recv_iov)
5295 struct smbXcli_conn *conn = session->conn;
5296 uint8_t channel_key[16];
5300 return NT_STATUS_INVALID_PARAMETER_MIX;
5303 if (session->smb2_channel.signing_key.length != 0) {
5304 return NT_STATUS_INVALID_PARAMETER_MIX;
5307 ZERO_STRUCT(channel_key);
5308 memcpy(channel_key, _channel_key.data,
5309 MIN(_channel_key.length, sizeof(channel_key)));
5311 session->smb2_channel.signing_key = data_blob_talloc(session,
5313 sizeof(channel_key));
5314 if (session->smb2_channel.signing_key.data == NULL) {
5315 ZERO_STRUCT(channel_key);
5316 return NT_STATUS_NO_MEMORY;
5319 if (conn->protocol >= PROTOCOL_SMB2_24) {
5320 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
5321 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
5323 smb2_key_derivation(channel_key, sizeof(channel_key),
5324 label.data, label.length,
5325 context.data, context.length,
5326 session->smb2_channel.signing_key.data);
5328 ZERO_STRUCT(channel_key);
5330 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5331 session->conn->protocol,
5333 if (!NT_STATUS_IS_OK(status)) {
5337 return NT_STATUS_OK;
5340 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
5342 if (session->smb2->should_encrypt) {
5343 return NT_STATUS_OK;
5346 if (session->conn->protocol < PROTOCOL_SMB2_24) {
5347 return NT_STATUS_NOT_SUPPORTED;
5350 if (!(session->conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
5351 return NT_STATUS_NOT_SUPPORTED;
5354 if (session->smb2->signing_key.data == NULL) {
5355 return NT_STATUS_NOT_SUPPORTED;
5357 session->smb2->should_encrypt = true;
5358 return NT_STATUS_OK;
5361 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
5363 struct smbXcli_tcon *tcon;
5365 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
5373 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
5374 uint32_t fs_attributes)
5376 tcon->fs_attributes = fs_attributes;
5379 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
5381 return tcon->fs_attributes;
5384 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
5390 if (tcon->is_smb1) {
5391 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
5398 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
5405 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
5407 return tcon->smb1.tcon_id;
5410 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
5412 tcon->is_smb1 = true;
5413 tcon->smb1.tcon_id = tcon_id;
5416 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
5418 uint16_t optional_support,
5419 uint32_t maximal_access,
5420 uint32_t guest_maximal_access,
5421 const char *service,
5422 const char *fs_type)
5424 tcon->is_smb1 = true;
5425 tcon->fs_attributes = 0;
5426 tcon->smb1.tcon_id = tcon_id;
5427 tcon->smb1.optional_support = optional_support;
5428 tcon->smb1.maximal_access = maximal_access;
5429 tcon->smb1.guest_maximal_access = guest_maximal_access;
5431 TALLOC_FREE(tcon->smb1.service);
5432 tcon->smb1.service = talloc_strdup(tcon, service);
5433 if (service != NULL && tcon->smb1.service == NULL) {
5437 TALLOC_FREE(tcon->smb1.fs_type);
5438 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5439 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5446 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5448 return tcon->smb2.tcon_id;
5451 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5453 return tcon->smb2.capabilities;
5456 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5457 struct smbXcli_session *session,
5461 uint32_t capabilities,
5462 uint32_t maximal_access)
5464 tcon->is_smb1 = false;
5465 tcon->fs_attributes = 0;
5466 tcon->smb2.tcon_id = tcon_id;
5467 tcon->smb2.type = type;
5468 tcon->smb2.flags = flags;
5469 tcon->smb2.capabilities = capabilities;
5470 tcon->smb2.maximal_access = maximal_access;
5472 tcon->smb2.should_sign = false;
5473 tcon->smb2.should_encrypt = false;
5475 if (session == NULL) {
5479 tcon->smb2.should_sign = session->smb2->should_sign;
5480 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5482 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5483 tcon->smb2.should_encrypt = true;
5487 void smb2cli_tcon_should_sign(struct smbXcli_tcon *tcon,
5490 tcon->smb2.should_sign = should_sign;
5493 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon *tcon)
5495 if (tcon->smb2.should_encrypt) {
5499 return tcon->smb2.should_sign;
5502 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon *tcon,
5503 bool should_encrypt)
5505 tcon->smb2.should_encrypt = should_encrypt;
5508 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
5510 return tcon->smb2.should_encrypt;