libcli/smb: add basic session->smb2.channel_sequence handling
[Samba/gebeck_regimport.git] / libcli / smb / smbXcli_base.c
bloba363b4451c0991dba664a576cb52e85834e37672
1 /*
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/>.
21 #include "includes.h"
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"
35 struct smbXcli_conn;
36 struct smbXcli_req;
37 struct smbXcli_session;
38 struct smbXcli_tcon;
40 struct smbXcli_conn {
41 int read_fd;
42 int write_fd;
43 struct sockaddr_storage local_ss;
44 struct sockaddr_storage remote_ss;
45 const char *remote_name;
47 struct tevent_queue *outgoing;
48 struct tevent_req **pending;
49 struct tevent_req *read_smb_req;
51 enum protocol_types protocol;
52 bool allow_signing;
53 bool desire_signing;
54 bool mandatory_signing;
57 * The incoming dispatch function should return:
58 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
59 * - NT_STATUS_OK, if no more processing is desired, e.g.
60 * the dispatch function called
61 * tevent_req_done().
62 * - All other return values disconnect the connection.
64 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
65 TALLOC_CTX *tmp_mem,
66 uint8_t *inbuf);
68 struct {
69 struct {
70 uint32_t capabilities;
71 uint32_t max_xmit;
72 } client;
74 struct {
75 uint32_t capabilities;
76 uint32_t max_xmit;
77 uint16_t max_mux;
78 uint16_t security_mode;
79 bool readbraw;
80 bool writebraw;
81 bool lockread;
82 bool writeunlock;
83 uint32_t session_key;
84 struct GUID guid;
85 DATA_BLOB gss_blob;
86 uint8_t challenge[8];
87 const char *workgroup;
88 const char *name;
89 int time_zone;
90 NTTIME system_time;
91 } server;
93 uint32_t capabilities;
94 uint32_t max_xmit;
96 uint16_t mid;
98 struct smb_signing_state *signing;
99 struct smb_trans_enc_state *trans_enc;
101 struct tevent_req *read_braw_req;
102 } smb1;
104 struct {
105 struct {
106 uint32_t capabilities;
107 uint16_t security_mode;
108 struct GUID guid;
109 } client;
111 struct {
112 uint32_t capabilities;
113 uint16_t security_mode;
114 struct GUID guid;
115 uint32_t max_trans_size;
116 uint32_t max_read_size;
117 uint32_t max_write_size;
118 NTTIME system_time;
119 NTTIME start_time;
120 DATA_BLOB gss_blob;
121 } server;
123 uint64_t mid;
124 uint16_t cur_credits;
125 uint16_t max_credits;
126 } smb2;
128 struct smbXcli_session *sessions;
131 struct smb2cli_session {
132 uint64_t session_id;
133 uint16_t session_flags;
134 DATA_BLOB application_key;
135 DATA_BLOB signing_key;
136 bool should_sign;
137 bool should_encrypt;
138 DATA_BLOB encryption_key;
139 DATA_BLOB decryption_key;
140 uint64_t nonce_high;
141 uint64_t nonce_low;
142 uint16_t channel_sequence;
145 struct smbXcli_session {
146 struct smbXcli_session *prev, *next;
147 struct smbXcli_conn *conn;
149 struct {
150 uint16_t session_id;
151 } smb1;
153 struct smb2cli_session *smb2;
155 struct {
156 DATA_BLOB signing_key;
157 } smb2_channel;
160 struct smbXcli_tcon {
161 struct {
162 uint16_t tcon_id;
163 uint16_t optional_support;
164 uint32_t maximal_access;
165 uint32_t guest_maximal_access;
166 char *service;
167 char *fs_type;
168 } smb1;
170 struct {
171 uint32_t tcon_id;
172 uint8_t type;
173 uint32_t flags;
174 uint32_t capabilities;
175 uint32_t maximal_access;
176 bool should_encrypt;
177 } smb2;
180 struct smbXcli_req_state {
181 struct tevent_context *ev;
182 struct smbXcli_conn *conn;
183 struct smbXcli_session *session; /* maybe NULL */
184 struct smbXcli_tcon *tcon; /* maybe NULL */
186 uint8_t length_hdr[4];
188 bool one_way;
190 uint8_t *inbuf;
192 struct {
193 /* Space for the header including the wct */
194 uint8_t hdr[HDR_VWV];
197 * For normal requests, smb1cli_req_send chooses a mid.
198 * SecondaryV trans requests need to use the mid of the primary
199 * request, so we need a place to store it.
200 * Assume it is set if != 0.
202 uint16_t mid;
204 uint16_t *vwv;
205 uint8_t bytecount_buf[2];
207 #define MAX_SMB_IOV 10
208 /* length_hdr, hdr, words, byte_count, buffers */
209 struct iovec iov[1 + 3 + MAX_SMB_IOV];
210 int iov_count;
212 bool one_way_seqnum;
213 uint32_t seqnum;
214 struct tevent_req **chained_requests;
216 uint8_t recv_cmd;
217 NTSTATUS recv_status;
218 /* always an array of 3 talloc elements */
219 struct iovec *recv_iov;
220 } smb1;
222 struct {
223 const uint8_t *fixed;
224 uint16_t fixed_len;
225 const uint8_t *dyn;
226 uint32_t dyn_len;
228 uint8_t transform[SMB2_TF_HDR_SIZE];
229 uint8_t hdr[SMB2_HDR_BODY];
230 uint8_t pad[7]; /* padding space for compounding */
233 * always an array of 3 talloc elements
234 * (without a SMB2_TRANSFORM header!)
236 * HDR, BODY, DYN
238 struct iovec *recv_iov;
240 uint16_t credit_charge;
242 bool should_sign;
243 bool should_encrypt;
245 bool signing_skipped;
246 bool notify_async;
247 bool got_async;
248 } smb2;
251 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
254 * NT_STATUS_OK, means we do not notify the callers
256 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
258 while (conn->sessions) {
259 conn->sessions->conn = NULL;
260 DLIST_REMOVE(conn->sessions, conn->sessions);
263 if (conn->smb1.trans_enc) {
264 TALLOC_FREE(conn->smb1.trans_enc);
267 return 0;
270 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
271 int fd,
272 const char *remote_name,
273 enum smb_signing_setting signing_state,
274 uint32_t smb1_capabilities,
275 struct GUID *client_guid,
276 uint32_t smb2_capabilities)
278 struct smbXcli_conn *conn = NULL;
279 void *ss = NULL;
280 struct sockaddr *sa = NULL;
281 socklen_t sa_length;
282 int ret;
284 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
285 if (!conn) {
286 return NULL;
289 conn->read_fd = fd;
290 conn->write_fd = dup(fd);
291 if (conn->write_fd == -1) {
292 goto error;
295 conn->remote_name = talloc_strdup(conn, remote_name);
296 if (conn->remote_name == NULL) {
297 goto error;
301 ss = (void *)&conn->local_ss;
302 sa = (struct sockaddr *)ss;
303 sa_length = sizeof(conn->local_ss);
304 ret = getsockname(fd, sa, &sa_length);
305 if (ret == -1) {
306 goto error;
308 ss = (void *)&conn->remote_ss;
309 sa = (struct sockaddr *)ss;
310 sa_length = sizeof(conn->remote_ss);
311 ret = getpeername(fd, sa, &sa_length);
312 if (ret == -1) {
313 goto error;
316 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
317 if (conn->outgoing == NULL) {
318 goto error;
320 conn->pending = NULL;
322 conn->protocol = PROTOCOL_NONE;
324 switch (signing_state) {
325 case SMB_SIGNING_OFF:
326 /* never */
327 conn->allow_signing = false;
328 conn->desire_signing = false;
329 conn->mandatory_signing = false;
330 break;
331 case SMB_SIGNING_DEFAULT:
332 case SMB_SIGNING_IF_REQUIRED:
333 /* if the server requires it */
334 conn->allow_signing = true;
335 conn->desire_signing = false;
336 conn->mandatory_signing = false;
337 break;
338 case SMB_SIGNING_REQUIRED:
339 /* always */
340 conn->allow_signing = true;
341 conn->desire_signing = true;
342 conn->mandatory_signing = true;
343 break;
346 conn->smb1.client.capabilities = smb1_capabilities;
347 conn->smb1.client.max_xmit = UINT16_MAX;
349 conn->smb1.capabilities = conn->smb1.client.capabilities;
350 conn->smb1.max_xmit = 1024;
352 conn->smb1.mid = 1;
354 /* initialise signing */
355 conn->smb1.signing = smb_signing_init(conn,
356 conn->allow_signing,
357 conn->desire_signing,
358 conn->mandatory_signing);
359 if (!conn->smb1.signing) {
360 goto error;
363 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
364 if (conn->mandatory_signing) {
365 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
367 if (client_guid) {
368 conn->smb2.client.guid = *client_guid;
370 conn->smb2.client.capabilities = smb2_capabilities;
372 conn->smb2.cur_credits = 1;
373 conn->smb2.max_credits = 0;
375 talloc_set_destructor(conn, smbXcli_conn_destructor);
376 return conn;
378 error:
379 if (conn->write_fd != -1) {
380 close(conn->write_fd);
382 TALLOC_FREE(conn);
383 return NULL;
386 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
388 if (conn == NULL) {
389 return false;
392 if (conn->read_fd == -1) {
393 return false;
396 return true;
399 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
401 return conn->protocol;
404 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
406 if (conn->protocol >= PROTOCOL_SMB2_02) {
407 return true;
410 if (conn->smb1.capabilities & CAP_UNICODE) {
411 return true;
414 return false;
417 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
419 set_socket_options(conn->read_fd, options);
422 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
424 return &conn->local_ss;
427 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
429 return &conn->remote_ss;
432 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
434 return conn->remote_name;
437 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
439 if (conn->protocol >= PROTOCOL_SMB2_02) {
441 * TODO...
443 return 1;
446 return conn->smb1.server.max_mux;
449 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
451 if (conn->protocol >= PROTOCOL_SMB2_02) {
452 return conn->smb2.server.system_time;
455 return conn->smb1.server.system_time;
458 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
460 if (conn->protocol >= PROTOCOL_SMB2_02) {
461 return &conn->smb2.server.gss_blob;
464 return &conn->smb1.server.gss_blob;
467 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
469 if (conn->protocol >= PROTOCOL_SMB2_02) {
470 return &conn->smb2.server.guid;
473 return &conn->smb1.server.guid;
476 struct smbXcli_conn_samba_suicide_state {
477 struct smbXcli_conn *conn;
478 struct iovec iov;
479 uint8_t buf[9];
482 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
484 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
485 struct tevent_context *ev,
486 struct smbXcli_conn *conn,
487 uint8_t exitcode)
489 struct tevent_req *req, *subreq;
490 struct smbXcli_conn_samba_suicide_state *state;
492 req = tevent_req_create(mem_ctx, &state,
493 struct smbXcli_conn_samba_suicide_state);
494 if (req == NULL) {
495 return NULL;
497 state->conn = conn;
498 SIVAL(state->buf, 4, 0x74697865);
499 SCVAL(state->buf, 8, exitcode);
500 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
502 state->iov.iov_base = state->buf;
503 state->iov.iov_len = sizeof(state->buf);
505 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
506 false, &state->iov, 1);
507 if (tevent_req_nomem(subreq, req)) {
508 return tevent_req_post(req, ev);
510 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
511 return req;
514 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
516 struct tevent_req *req = tevent_req_callback_data(
517 subreq, struct tevent_req);
518 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
519 req, struct smbXcli_conn_samba_suicide_state);
520 ssize_t nwritten;
521 int err;
523 nwritten = writev_recv(subreq, &err);
524 TALLOC_FREE(subreq);
525 if (nwritten == -1) {
526 NTSTATUS status = map_nt_error_from_unix_common(err);
527 smbXcli_conn_disconnect(state->conn, status);
528 return;
530 tevent_req_done(req);
533 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
535 return tevent_req_simple_recv_ntstatus(req);
538 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
539 uint8_t exitcode)
541 TALLOC_CTX *frame = talloc_stackframe();
542 struct tevent_context *ev;
543 struct tevent_req *req;
544 NTSTATUS status = NT_STATUS_NO_MEMORY;
545 bool ok;
547 if (smbXcli_conn_has_async_calls(conn)) {
549 * Can't use sync call while an async call is in flight
551 status = NT_STATUS_INVALID_PARAMETER_MIX;
552 goto fail;
554 ev = tevent_context_init(frame);
555 if (ev == NULL) {
556 goto fail;
558 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
559 if (req == NULL) {
560 goto fail;
562 ok = tevent_req_poll(req, ev);
563 if (!ok) {
564 status = map_nt_error_from_unix_common(errno);
565 goto fail;
567 status = smbXcli_conn_samba_suicide_recv(req);
568 fail:
569 TALLOC_FREE(frame);
570 return status;
573 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
575 return conn->smb1.capabilities;
578 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
580 return conn->smb1.max_xmit;
583 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
585 return conn->smb1.server.session_key;
588 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
590 return conn->smb1.server.challenge;
593 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
595 return conn->smb1.server.security_mode;
598 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
600 return conn->smb1.server.readbraw;
603 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
605 return conn->smb1.server.writebraw;
608 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
610 return conn->smb1.server.lockread;
613 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
615 return conn->smb1.server.writeunlock;
618 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
620 return conn->smb1.server.time_zone;
623 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
624 const DATA_BLOB user_session_key,
625 const DATA_BLOB response)
627 return smb_signing_activate(conn->smb1.signing,
628 user_session_key,
629 response);
632 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
633 const uint8_t *buf, uint32_t seqnum)
635 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
638 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
640 return smb_signing_is_active(conn->smb1.signing);
643 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
644 struct smb_trans_enc_state *es)
646 /* Replace the old state, if any. */
647 if (conn->smb1.trans_enc) {
648 TALLOC_FREE(conn->smb1.trans_enc);
650 conn->smb1.trans_enc = es;
653 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
655 return common_encryption_on(conn->smb1.trans_enc);
659 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
661 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
662 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
664 if (NT_STATUS_IS_OK(status)) {
665 return NT_STATUS_OK;
668 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
669 return status;
672 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
676 * Is the SMB command able to hold an AND_X successor
677 * @param[in] cmd The SMB command in question
678 * @retval Can we add a chained request after "cmd"?
680 bool smb1cli_is_andx_req(uint8_t cmd)
682 switch (cmd) {
683 case SMBtconX:
684 case SMBlockingX:
685 case SMBopenX:
686 case SMBreadX:
687 case SMBwriteX:
688 case SMBsesssetupX:
689 case SMBulogoffX:
690 case SMBntcreateX:
691 return true;
692 break;
693 default:
694 break;
697 return false;
700 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
702 size_t num_pending = talloc_array_length(conn->pending);
703 uint16_t result;
705 while (true) {
706 size_t i;
708 result = conn->smb1.mid++;
709 if ((result == 0) || (result == 0xffff)) {
710 continue;
713 for (i=0; i<num_pending; i++) {
714 if (result == smb1cli_req_mid(conn->pending[i])) {
715 break;
719 if (i == num_pending) {
720 return result;
725 void smbXcli_req_unset_pending(struct tevent_req *req)
727 struct smbXcli_req_state *state =
728 tevent_req_data(req,
729 struct smbXcli_req_state);
730 struct smbXcli_conn *conn = state->conn;
731 size_t num_pending = talloc_array_length(conn->pending);
732 size_t i;
734 if (state->smb1.mid != 0) {
736 * This is a [nt]trans[2] request which waits
737 * for more than one reply.
739 return;
742 talloc_set_destructor(req, NULL);
744 if (num_pending == 1) {
746 * The pending read_smb tevent_req is a child of
747 * conn->pending. So if nothing is pending anymore, we need to
748 * delete the socket read fde.
750 TALLOC_FREE(conn->pending);
751 conn->read_smb_req = NULL;
752 return;
755 for (i=0; i<num_pending; i++) {
756 if (req == conn->pending[i]) {
757 break;
760 if (i == num_pending) {
762 * Something's seriously broken. Just returning here is the
763 * right thing nevertheless, the point of this routine is to
764 * remove ourselves from conn->pending.
766 return;
770 * Remove ourselves from the conn->pending array
772 for (; i < (num_pending - 1); i++) {
773 conn->pending[i] = conn->pending[i+1];
777 * No NULL check here, we're shrinking by sizeof(void *), and
778 * talloc_realloc just adjusts the size for this.
780 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
781 num_pending - 1);
782 return;
785 static int smbXcli_req_destructor(struct tevent_req *req)
787 struct smbXcli_req_state *state =
788 tevent_req_data(req,
789 struct smbXcli_req_state);
792 * Make sure we really remove it from
793 * the pending array on destruction.
795 state->smb1.mid = 0;
796 smbXcli_req_unset_pending(req);
797 return 0;
800 static bool smb1cli_req_cancel(struct tevent_req *req);
801 static bool smb2cli_req_cancel(struct tevent_req *req);
803 static bool smbXcli_req_cancel(struct tevent_req *req)
805 struct smbXcli_req_state *state =
806 tevent_req_data(req,
807 struct smbXcli_req_state);
809 if (!smbXcli_conn_is_connected(state->conn)) {
810 return false;
813 if (state->conn->protocol == PROTOCOL_NONE) {
814 return false;
817 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
818 return smb2cli_req_cancel(req);
821 return smb1cli_req_cancel(req);
824 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
826 bool smbXcli_req_set_pending(struct tevent_req *req)
828 struct smbXcli_req_state *state =
829 tevent_req_data(req,
830 struct smbXcli_req_state);
831 struct smbXcli_conn *conn;
832 struct tevent_req **pending;
833 size_t num_pending;
835 conn = state->conn;
837 if (!smbXcli_conn_is_connected(conn)) {
838 return false;
841 num_pending = talloc_array_length(conn->pending);
843 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
844 num_pending+1);
845 if (pending == NULL) {
846 return false;
848 pending[num_pending] = req;
849 conn->pending = pending;
850 talloc_set_destructor(req, smbXcli_req_destructor);
851 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
853 if (!smbXcli_conn_receive_next(conn)) {
855 * the caller should notify the current request
857 * And all other pending requests get notified
858 * by smbXcli_conn_disconnect().
860 smbXcli_req_unset_pending(req);
861 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
862 return false;
865 return true;
868 static void smbXcli_conn_received(struct tevent_req *subreq);
870 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
872 size_t num_pending = talloc_array_length(conn->pending);
873 struct tevent_req *req;
874 struct smbXcli_req_state *state;
876 if (conn->read_smb_req != NULL) {
877 return true;
880 if (num_pending == 0) {
881 if (conn->smb2.mid < UINT64_MAX) {
882 /* no more pending requests, so we are done for now */
883 return true;
887 * If there are no more SMB2 requests possible,
888 * because we are out of message ids,
889 * we need to disconnect.
891 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
892 return true;
895 req = conn->pending[0];
896 state = tevent_req_data(req, struct smbXcli_req_state);
899 * We're the first ones, add the read_smb request that waits for the
900 * answer from the server
902 conn->read_smb_req = read_smb_send(conn->pending,
903 state->ev,
904 conn->read_fd);
905 if (conn->read_smb_req == NULL) {
906 return false;
908 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
909 return true;
912 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
914 struct smbXcli_session *session;
916 tevent_queue_stop(conn->outgoing);
918 if (conn->read_fd != -1) {
919 close(conn->read_fd);
921 if (conn->write_fd != -1) {
922 close(conn->write_fd);
924 conn->read_fd = -1;
925 conn->write_fd = -1;
927 session = conn->sessions;
928 if (talloc_array_length(conn->pending) == 0) {
930 * if we do not have pending requests
931 * there is no need to update the channel_sequence
933 session = NULL;
935 for (; session; session = session->next) {
936 smb2cli_session_increment_channel_sequence(session);
940 * Cancel all pending requests. We do not do a for-loop walking
941 * conn->pending because that array changes in
942 * smbXcli_req_unset_pending.
944 while (talloc_array_length(conn->pending) > 0) {
945 struct tevent_req *req;
946 struct smbXcli_req_state *state;
947 struct tevent_req **chain;
948 size_t num_chained;
949 size_t i;
951 req = conn->pending[0];
952 state = tevent_req_data(req, struct smbXcli_req_state);
954 if (state->smb1.chained_requests == NULL) {
956 * We're dead. No point waiting for trans2
957 * replies.
959 state->smb1.mid = 0;
961 smbXcli_req_unset_pending(req);
963 if (NT_STATUS_IS_OK(status)) {
964 /* do not notify the callers */
965 continue;
969 * we need to defer the callback, because we may notify
970 * more then one caller.
972 tevent_req_defer_callback(req, state->ev);
973 tevent_req_nterror(req, status);
974 continue;
977 chain = talloc_move(conn, &state->smb1.chained_requests);
978 num_chained = talloc_array_length(chain);
980 for (i=0; i<num_chained; i++) {
981 req = chain[i];
982 state = tevent_req_data(req, struct smbXcli_req_state);
985 * We're dead. No point waiting for trans2
986 * replies.
988 state->smb1.mid = 0;
990 smbXcli_req_unset_pending(req);
992 if (NT_STATUS_IS_OK(status)) {
993 /* do not notify the callers */
994 continue;
998 * we need to defer the callback, because we may notify
999 * more than one caller.
1001 tevent_req_defer_callback(req, state->ev);
1002 tevent_req_nterror(req, status);
1004 TALLOC_FREE(chain);
1009 * Fetch a smb request's mid. Only valid after the request has been sent by
1010 * smb1cli_req_send().
1012 uint16_t smb1cli_req_mid(struct tevent_req *req)
1014 struct smbXcli_req_state *state =
1015 tevent_req_data(req,
1016 struct smbXcli_req_state);
1018 if (state->smb1.mid != 0) {
1019 return state->smb1.mid;
1022 return SVAL(state->smb1.hdr, HDR_MID);
1025 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1027 struct smbXcli_req_state *state =
1028 tevent_req_data(req,
1029 struct smbXcli_req_state);
1031 state->smb1.mid = mid;
1034 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1036 struct smbXcli_req_state *state =
1037 tevent_req_data(req,
1038 struct smbXcli_req_state);
1040 return state->smb1.seqnum;
1043 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1045 struct smbXcli_req_state *state =
1046 tevent_req_data(req,
1047 struct smbXcli_req_state);
1049 state->smb1.seqnum = seqnum;
1052 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1054 size_t result = 0;
1055 int i;
1056 for (i=0; i<count; i++) {
1057 result += iov[i].iov_len;
1059 return result;
1062 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1063 const struct iovec *iov,
1064 int count)
1066 size_t len = smbXcli_iov_len(iov, count);
1067 size_t copied;
1068 uint8_t *buf;
1069 int i;
1071 buf = talloc_array(mem_ctx, uint8_t, len);
1072 if (buf == NULL) {
1073 return NULL;
1075 copied = 0;
1076 for (i=0; i<count; i++) {
1077 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1078 copied += iov[i].iov_len;
1080 return buf;
1083 static void smb1cli_req_flags(enum protocol_types protocol,
1084 uint32_t smb1_capabilities,
1085 uint8_t smb_command,
1086 uint8_t additional_flags,
1087 uint8_t clear_flags,
1088 uint8_t *_flags,
1089 uint16_t additional_flags2,
1090 uint16_t clear_flags2,
1091 uint16_t *_flags2)
1093 uint8_t flags = 0;
1094 uint16_t flags2 = 0;
1096 if (protocol >= PROTOCOL_LANMAN1) {
1097 flags |= FLAG_CASELESS_PATHNAMES;
1098 flags |= FLAG_CANONICAL_PATHNAMES;
1101 if (protocol >= PROTOCOL_LANMAN2) {
1102 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1103 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1106 if (protocol >= PROTOCOL_NT1) {
1107 flags2 |= FLAGS2_IS_LONG_NAME;
1109 if (smb1_capabilities & CAP_UNICODE) {
1110 flags2 |= FLAGS2_UNICODE_STRINGS;
1112 if (smb1_capabilities & CAP_STATUS32) {
1113 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1115 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1116 flags2 |= FLAGS2_EXTENDED_SECURITY;
1120 flags |= additional_flags;
1121 flags &= ~clear_flags;
1122 flags2 |= additional_flags2;
1123 flags2 &= ~clear_flags2;
1125 *_flags = flags;
1126 *_flags2 = flags2;
1129 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1131 static bool smb1cli_req_cancel(struct tevent_req *req)
1133 struct smbXcli_req_state *state =
1134 tevent_req_data(req,
1135 struct smbXcli_req_state);
1136 uint8_t flags;
1137 uint16_t flags2;
1138 uint32_t pid;
1139 uint16_t mid;
1140 struct tevent_req *subreq;
1141 NTSTATUS status;
1143 flags = CVAL(state->smb1.hdr, HDR_FLG);
1144 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1145 pid = SVAL(state->smb1.hdr, HDR_PID);
1146 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1147 mid = SVAL(state->smb1.hdr, HDR_MID);
1149 subreq = smb1cli_req_create(state, state->ev,
1150 state->conn,
1151 SMBntcancel,
1152 flags, 0,
1153 flags2, 0,
1154 0, /* timeout */
1155 pid,
1156 state->tcon,
1157 state->session,
1158 0, NULL, /* vwv */
1159 0, NULL); /* bytes */
1160 if (subreq == NULL) {
1161 return false;
1163 smb1cli_req_set_mid(subreq, mid);
1165 status = smb1cli_req_chain_submit(&subreq, 1);
1166 if (!NT_STATUS_IS_OK(status)) {
1167 TALLOC_FREE(subreq);
1168 return false;
1170 smb1cli_req_set_mid(subreq, 0);
1172 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1174 return true;
1177 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1179 /* we do not care about the result */
1180 TALLOC_FREE(subreq);
1183 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1184 struct tevent_context *ev,
1185 struct smbXcli_conn *conn,
1186 uint8_t smb_command,
1187 uint8_t additional_flags,
1188 uint8_t clear_flags,
1189 uint16_t additional_flags2,
1190 uint16_t clear_flags2,
1191 uint32_t timeout_msec,
1192 uint32_t pid,
1193 struct smbXcli_tcon *tcon,
1194 struct smbXcli_session *session,
1195 uint8_t wct, uint16_t *vwv,
1196 int iov_count,
1197 struct iovec *bytes_iov)
1199 struct tevent_req *req;
1200 struct smbXcli_req_state *state;
1201 uint8_t flags = 0;
1202 uint16_t flags2 = 0;
1203 uint16_t uid = 0;
1204 uint16_t tid = 0;
1206 if (iov_count > MAX_SMB_IOV) {
1208 * Should not happen :-)
1210 return NULL;
1213 req = tevent_req_create(mem_ctx, &state,
1214 struct smbXcli_req_state);
1215 if (req == NULL) {
1216 return NULL;
1218 state->ev = ev;
1219 state->conn = conn;
1220 state->session = session;
1221 state->tcon = tcon;
1223 if (session) {
1224 uid = session->smb1.session_id;
1227 if (tcon) {
1228 tid = tcon->smb1.tcon_id;
1231 state->smb1.recv_cmd = 0xFF;
1232 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1233 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1234 if (state->smb1.recv_iov == NULL) {
1235 TALLOC_FREE(req);
1236 return NULL;
1239 smb1cli_req_flags(conn->protocol,
1240 conn->smb1.capabilities,
1241 smb_command,
1242 additional_flags,
1243 clear_flags,
1244 &flags,
1245 additional_flags2,
1246 clear_flags2,
1247 &flags2);
1249 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1250 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1251 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1252 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1253 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1254 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1255 SSVAL(state->smb1.hdr, HDR_TID, tid);
1256 SSVAL(state->smb1.hdr, HDR_PID, pid);
1257 SSVAL(state->smb1.hdr, HDR_UID, uid);
1258 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1259 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1261 state->smb1.vwv = vwv;
1263 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1265 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1266 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1267 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1268 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1269 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1270 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1271 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1272 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1274 if (iov_count != 0) {
1275 memcpy(&state->smb1.iov[4], bytes_iov,
1276 iov_count * sizeof(*bytes_iov));
1278 state->smb1.iov_count = iov_count + 4;
1280 if (timeout_msec > 0) {
1281 struct timeval endtime;
1283 endtime = timeval_current_ofs_msec(timeout_msec);
1284 if (!tevent_req_set_endtime(req, ev, endtime)) {
1285 return req;
1289 switch (smb_command) {
1290 case SMBtranss:
1291 case SMBtranss2:
1292 case SMBnttranss:
1293 state->one_way = true;
1294 break;
1295 case SMBntcancel:
1296 state->one_way = true;
1297 state->smb1.one_way_seqnum = true;
1298 break;
1299 case SMBlockingX:
1300 if ((wct == 8) &&
1301 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1302 state->one_way = true;
1304 break;
1307 return req;
1310 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1311 struct iovec *iov, int iov_count,
1312 uint32_t *seqnum,
1313 bool one_way_seqnum)
1315 TALLOC_CTX *frame = NULL;
1316 uint8_t *buf;
1319 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1320 * iovec directly. MD5Update would do that just fine.
1323 if (iov_count < 4) {
1324 return NT_STATUS_INVALID_PARAMETER_MIX;
1326 if (iov[0].iov_len != NBT_HDR_SIZE) {
1327 return NT_STATUS_INVALID_PARAMETER_MIX;
1329 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1330 return NT_STATUS_INVALID_PARAMETER_MIX;
1332 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1333 return NT_STATUS_INVALID_PARAMETER_MIX;
1335 if (iov[3].iov_len != sizeof(uint16_t)) {
1336 return NT_STATUS_INVALID_PARAMETER_MIX;
1339 frame = talloc_stackframe();
1341 buf = smbXcli_iov_concat(frame, iov, iov_count);
1342 if (buf == NULL) {
1343 return NT_STATUS_NO_MEMORY;
1346 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1347 one_way_seqnum);
1348 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1349 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1351 TALLOC_FREE(frame);
1352 return NT_STATUS_OK;
1355 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1356 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1357 TALLOC_CTX *tmp_mem,
1358 uint8_t *inbuf);
1360 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1361 struct smbXcli_req_state *state,
1362 struct iovec *iov, int iov_count)
1364 struct tevent_req *subreq;
1365 NTSTATUS status;
1366 uint8_t cmd;
1367 uint16_t mid;
1369 if (!smbXcli_conn_is_connected(state->conn)) {
1370 return NT_STATUS_CONNECTION_DISCONNECTED;
1373 if (state->conn->protocol > PROTOCOL_NT1) {
1374 return NT_STATUS_REVISION_MISMATCH;
1377 if (iov_count < 4) {
1378 return NT_STATUS_INVALID_PARAMETER_MIX;
1380 if (iov[0].iov_len != NBT_HDR_SIZE) {
1381 return NT_STATUS_INVALID_PARAMETER_MIX;
1383 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1384 return NT_STATUS_INVALID_PARAMETER_MIX;
1386 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1387 return NT_STATUS_INVALID_PARAMETER_MIX;
1389 if (iov[3].iov_len != sizeof(uint16_t)) {
1390 return NT_STATUS_INVALID_PARAMETER_MIX;
1393 cmd = CVAL(iov[1].iov_base, HDR_COM);
1394 if (cmd == SMBreadBraw) {
1395 if (smbXcli_conn_has_async_calls(state->conn)) {
1396 return NT_STATUS_INVALID_PARAMETER_MIX;
1398 state->conn->smb1.read_braw_req = req;
1401 if (state->smb1.mid != 0) {
1402 mid = state->smb1.mid;
1403 } else {
1404 mid = smb1cli_alloc_mid(state->conn);
1406 SSVAL(iov[1].iov_base, HDR_MID, mid);
1408 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1410 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1411 &state->smb1.seqnum,
1412 state->smb1.one_way_seqnum);
1414 if (!NT_STATUS_IS_OK(status)) {
1415 return status;
1419 * If we supported multiple encrytion contexts
1420 * here we'd look up based on tid.
1422 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1423 char *buf, *enc_buf;
1425 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1426 if (buf == NULL) {
1427 return NT_STATUS_NO_MEMORY;
1429 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1430 (char *)buf, &enc_buf);
1431 TALLOC_FREE(buf);
1432 if (!NT_STATUS_IS_OK(status)) {
1433 DEBUG(0, ("Error in encrypting client message: %s\n",
1434 nt_errstr(status)));
1435 return status;
1437 buf = (char *)talloc_memdup(state, enc_buf,
1438 smb_len_nbt(enc_buf)+4);
1439 SAFE_FREE(enc_buf);
1440 if (buf == NULL) {
1441 return NT_STATUS_NO_MEMORY;
1443 iov[0].iov_base = (void *)buf;
1444 iov[0].iov_len = talloc_get_size(buf);
1445 iov_count = 1;
1448 if (state->conn->dispatch_incoming == NULL) {
1449 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1452 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1454 subreq = writev_send(state, state->ev, state->conn->outgoing,
1455 state->conn->write_fd, false, iov, iov_count);
1456 if (subreq == NULL) {
1457 return NT_STATUS_NO_MEMORY;
1459 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1460 return NT_STATUS_OK;
1463 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1464 struct tevent_context *ev,
1465 struct smbXcli_conn *conn,
1466 uint8_t smb_command,
1467 uint8_t additional_flags,
1468 uint8_t clear_flags,
1469 uint16_t additional_flags2,
1470 uint16_t clear_flags2,
1471 uint32_t timeout_msec,
1472 uint32_t pid,
1473 struct smbXcli_tcon *tcon,
1474 struct smbXcli_session *session,
1475 uint8_t wct, uint16_t *vwv,
1476 uint32_t num_bytes,
1477 const uint8_t *bytes)
1479 struct tevent_req *req;
1480 struct iovec iov;
1481 NTSTATUS status;
1483 iov.iov_base = discard_const_p(void, bytes);
1484 iov.iov_len = num_bytes;
1486 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1487 additional_flags, clear_flags,
1488 additional_flags2, clear_flags2,
1489 timeout_msec,
1490 pid, tcon, session,
1491 wct, vwv, 1, &iov);
1492 if (req == NULL) {
1493 return NULL;
1495 if (!tevent_req_is_in_progress(req)) {
1496 return tevent_req_post(req, ev);
1498 status = smb1cli_req_chain_submit(&req, 1);
1499 if (tevent_req_nterror(req, status)) {
1500 return tevent_req_post(req, ev);
1502 return req;
1505 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1507 struct tevent_req *req =
1508 tevent_req_callback_data(subreq,
1509 struct tevent_req);
1510 struct smbXcli_req_state *state =
1511 tevent_req_data(req,
1512 struct smbXcli_req_state);
1513 ssize_t nwritten;
1514 int err;
1516 nwritten = writev_recv(subreq, &err);
1517 TALLOC_FREE(subreq);
1518 if (nwritten == -1) {
1519 NTSTATUS status = map_nt_error_from_unix_common(err);
1520 smbXcli_conn_disconnect(state->conn, status);
1521 return;
1524 if (state->one_way) {
1525 state->inbuf = NULL;
1526 tevent_req_done(req);
1527 return;
1530 if (!smbXcli_req_set_pending(req)) {
1531 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1532 return;
1536 static void smbXcli_conn_received(struct tevent_req *subreq)
1538 struct smbXcli_conn *conn =
1539 tevent_req_callback_data(subreq,
1540 struct smbXcli_conn);
1541 TALLOC_CTX *frame = talloc_stackframe();
1542 NTSTATUS status;
1543 uint8_t *inbuf;
1544 ssize_t received;
1545 int err;
1547 if (subreq != conn->read_smb_req) {
1548 DEBUG(1, ("Internal error: cli_smb_received called with "
1549 "unexpected subreq\n"));
1550 status = NT_STATUS_INTERNAL_ERROR;
1551 smbXcli_conn_disconnect(conn, status);
1552 TALLOC_FREE(frame);
1553 return;
1555 conn->read_smb_req = NULL;
1557 received = read_smb_recv(subreq, frame, &inbuf, &err);
1558 TALLOC_FREE(subreq);
1559 if (received == -1) {
1560 status = map_nt_error_from_unix_common(err);
1561 smbXcli_conn_disconnect(conn, status);
1562 TALLOC_FREE(frame);
1563 return;
1566 status = conn->dispatch_incoming(conn, frame, inbuf);
1567 TALLOC_FREE(frame);
1568 if (NT_STATUS_IS_OK(status)) {
1570 * We should not do any more processing
1571 * as the dispatch function called
1572 * tevent_req_done().
1574 return;
1575 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1577 * We got an error, so notify all pending requests
1579 smbXcli_conn_disconnect(conn, status);
1580 return;
1584 * We got NT_STATUS_RETRY, so we may ask for a
1585 * next incoming pdu.
1587 if (!smbXcli_conn_receive_next(conn)) {
1588 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1592 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1593 struct iovec **piov, int *pnum_iov)
1595 struct iovec *iov;
1596 int num_iov;
1597 size_t buflen;
1598 size_t taken;
1599 size_t remaining;
1600 uint8_t *hdr;
1601 uint8_t cmd;
1602 uint32_t wct_ofs;
1604 buflen = smb_len_nbt(buf);
1605 taken = 0;
1607 hdr = buf + NBT_HDR_SIZE;
1609 if (buflen < MIN_SMB_SIZE) {
1610 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1614 * This returns iovec elements in the following order:
1616 * - SMB header
1618 * - Parameter Block
1619 * - Data Block
1621 * - Parameter Block
1622 * - Data Block
1624 * - Parameter Block
1625 * - Data Block
1627 num_iov = 1;
1629 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1630 if (iov == NULL) {
1631 return NT_STATUS_NO_MEMORY;
1633 iov[0].iov_base = hdr;
1634 iov[0].iov_len = HDR_WCT;
1635 taken += HDR_WCT;
1637 cmd = CVAL(hdr, HDR_COM);
1638 wct_ofs = HDR_WCT;
1640 while (true) {
1641 size_t len = buflen - taken;
1642 struct iovec *cur;
1643 struct iovec *iov_tmp;
1644 uint8_t wct;
1645 uint32_t bcc_ofs;
1646 uint16_t bcc;
1647 size_t needed;
1650 * we need at least WCT and BCC
1652 needed = sizeof(uint8_t) + sizeof(uint16_t);
1653 if (len < needed) {
1654 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1655 __location__, (int)len, (int)needed));
1656 goto inval;
1660 * Now we check if the specified words are there
1662 wct = CVAL(hdr, wct_ofs);
1663 needed += wct * sizeof(uint16_t);
1664 if (len < needed) {
1665 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1666 __location__, (int)len, (int)needed));
1667 goto inval;
1671 * Now we check if the specified bytes are there
1673 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1674 bcc = SVAL(hdr, bcc_ofs);
1675 needed += bcc * sizeof(uint8_t);
1676 if (len < needed) {
1677 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1678 __location__, (int)len, (int)needed));
1679 goto inval;
1683 * we allocate 2 iovec structures for words and bytes
1685 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1686 num_iov + 2);
1687 if (iov_tmp == NULL) {
1688 TALLOC_FREE(iov);
1689 return NT_STATUS_NO_MEMORY;
1691 iov = iov_tmp;
1692 cur = &iov[num_iov];
1693 num_iov += 2;
1695 cur[0].iov_len = wct * sizeof(uint16_t);
1696 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1697 cur[1].iov_len = bcc * sizeof(uint8_t);
1698 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1700 taken += needed;
1702 if (!smb1cli_is_andx_req(cmd)) {
1704 * If the current command does not have AndX chanining
1705 * we are done.
1707 break;
1710 if (wct == 0 && bcc == 0) {
1712 * An empty response also ends the chain,
1713 * most likely with an error.
1715 break;
1718 if (wct < 2) {
1719 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1720 __location__, (int)wct, (int)cmd));
1721 goto inval;
1723 cmd = CVAL(cur[0].iov_base, 0);
1724 if (cmd == 0xFF) {
1726 * If it is the end of the chain we are also done.
1728 break;
1730 wct_ofs = SVAL(cur[0].iov_base, 2);
1732 if (wct_ofs < taken) {
1733 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1735 if (wct_ofs > buflen) {
1736 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1740 * we consumed everything up to the start of the next
1741 * parameter block.
1743 taken = wct_ofs;
1746 remaining = buflen - taken;
1748 if (remaining > 0 && num_iov >= 3) {
1750 * The last DATA block gets the remaining
1751 * bytes, this is needed to support
1752 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1754 iov[num_iov-1].iov_len += remaining;
1757 *piov = iov;
1758 *pnum_iov = num_iov;
1759 return NT_STATUS_OK;
1761 inval:
1762 TALLOC_FREE(iov);
1763 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1766 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1767 TALLOC_CTX *tmp_mem,
1768 uint8_t *inbuf)
1770 struct tevent_req *req;
1771 struct smbXcli_req_state *state;
1772 NTSTATUS status;
1773 size_t num_pending;
1774 size_t i;
1775 uint8_t cmd;
1776 uint16_t mid;
1777 bool oplock_break;
1778 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1779 struct iovec *iov = NULL;
1780 int num_iov = 0;
1781 struct tevent_req **chain = NULL;
1782 size_t num_chained = 0;
1783 size_t num_responses = 0;
1785 if (conn->smb1.read_braw_req != NULL) {
1786 req = conn->smb1.read_braw_req;
1787 conn->smb1.read_braw_req = NULL;
1788 state = tevent_req_data(req, struct smbXcli_req_state);
1790 smbXcli_req_unset_pending(req);
1792 if (state->smb1.recv_iov == NULL) {
1794 * For requests with more than
1795 * one response, we have to readd the
1796 * recv_iov array.
1798 state->smb1.recv_iov = talloc_zero_array(state,
1799 struct iovec,
1801 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1802 return NT_STATUS_OK;
1806 state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
1807 state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
1808 ZERO_STRUCT(state->smb1.recv_iov[1]);
1809 ZERO_STRUCT(state->smb1.recv_iov[2]);
1811 state->smb1.recv_cmd = SMBreadBraw;
1812 state->smb1.recv_status = NT_STATUS_OK;
1813 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1815 tevent_req_done(req);
1816 return NT_STATUS_OK;
1819 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1820 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1821 DEBUG(10, ("Got non-SMB PDU\n"));
1822 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1826 * If we supported multiple encrytion contexts
1827 * here we'd look up based on tid.
1829 if (common_encryption_on(conn->smb1.trans_enc)
1830 && (CVAL(inbuf, 0) == 0)) {
1831 uint16_t enc_ctx_num;
1833 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1834 if (!NT_STATUS_IS_OK(status)) {
1835 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1836 nt_errstr(status)));
1837 return status;
1840 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1841 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1842 enc_ctx_num,
1843 conn->smb1.trans_enc->enc_ctx_num));
1844 return NT_STATUS_INVALID_HANDLE;
1847 status = common_decrypt_buffer(conn->smb1.trans_enc,
1848 (char *)inbuf);
1849 if (!NT_STATUS_IS_OK(status)) {
1850 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1851 nt_errstr(status)));
1852 return status;
1856 mid = SVAL(inhdr, HDR_MID);
1857 num_pending = talloc_array_length(conn->pending);
1859 for (i=0; i<num_pending; i++) {
1860 if (mid == smb1cli_req_mid(conn->pending[i])) {
1861 break;
1864 if (i == num_pending) {
1865 /* Dump unexpected reply */
1866 return NT_STATUS_RETRY;
1869 oplock_break = false;
1871 if (mid == 0xffff) {
1873 * Paranoia checks that this is really an oplock break request.
1875 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1876 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1877 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1878 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1879 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1881 if (!oplock_break) {
1882 /* Dump unexpected reply */
1883 return NT_STATUS_RETRY;
1887 req = conn->pending[i];
1888 state = tevent_req_data(req, struct smbXcli_req_state);
1890 if (!oplock_break /* oplock breaks are not signed */
1891 && !smb_signing_check_pdu(conn->smb1.signing,
1892 inbuf, state->smb1.seqnum+1)) {
1893 DEBUG(10, ("cli_check_sign_mac failed\n"));
1894 return NT_STATUS_ACCESS_DENIED;
1897 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1898 &iov, &num_iov);
1899 if (!NT_STATUS_IS_OK(status)) {
1900 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1901 nt_errstr(status)));
1902 return status;
1905 cmd = CVAL(inhdr, HDR_COM);
1906 status = smb1cli_pull_raw_error(inhdr);
1908 if (state->smb1.chained_requests == NULL) {
1909 if (num_iov != 3) {
1910 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1913 smbXcli_req_unset_pending(req);
1915 if (state->smb1.recv_iov == NULL) {
1917 * For requests with more than
1918 * one response, we have to readd the
1919 * recv_iov array.
1921 state->smb1.recv_iov = talloc_zero_array(state,
1922 struct iovec,
1924 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1925 return NT_STATUS_OK;
1929 state->smb1.recv_cmd = cmd;
1930 state->smb1.recv_status = status;
1931 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1933 state->smb1.recv_iov[0] = iov[0];
1934 state->smb1.recv_iov[1] = iov[1];
1935 state->smb1.recv_iov[2] = iov[2];
1937 if (talloc_array_length(conn->pending) == 0) {
1938 tevent_req_done(req);
1939 return NT_STATUS_OK;
1942 tevent_req_defer_callback(req, state->ev);
1943 tevent_req_done(req);
1944 return NT_STATUS_RETRY;
1947 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1948 num_chained = talloc_array_length(chain);
1949 num_responses = (num_iov - 1)/2;
1951 if (num_responses > num_chained) {
1952 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1955 for (i=0; i<num_chained; i++) {
1956 size_t iov_idx = 1 + (i*2);
1957 struct iovec *cur = &iov[iov_idx];
1958 uint8_t *inbuf_ref;
1960 req = chain[i];
1961 state = tevent_req_data(req, struct smbXcli_req_state);
1963 smbXcli_req_unset_pending(req);
1966 * as we finish multiple requests here
1967 * we need to defer the callbacks as
1968 * they could destroy our current stack state.
1970 tevent_req_defer_callback(req, state->ev);
1972 if (i >= num_responses) {
1973 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1974 continue;
1977 if (state->smb1.recv_iov == NULL) {
1979 * For requests with more than
1980 * one response, we have to readd the
1981 * recv_iov array.
1983 state->smb1.recv_iov = talloc_zero_array(state,
1984 struct iovec,
1986 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1987 continue;
1991 state->smb1.recv_cmd = cmd;
1993 if (i == (num_responses - 1)) {
1995 * The last request in the chain gets the status
1997 state->smb1.recv_status = status;
1998 } else {
1999 cmd = CVAL(cur[0].iov_base, 0);
2000 state->smb1.recv_status = NT_STATUS_OK;
2003 state->inbuf = inbuf;
2006 * Note: here we use talloc_reference() in a way
2007 * that does not expose it to the caller.
2009 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2010 if (tevent_req_nomem(inbuf_ref, req)) {
2011 continue;
2014 /* copy the related buffers */
2015 state->smb1.recv_iov[0] = iov[0];
2016 state->smb1.recv_iov[1] = cur[0];
2017 state->smb1.recv_iov[2] = cur[1];
2019 tevent_req_done(req);
2022 return NT_STATUS_RETRY;
2025 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2026 TALLOC_CTX *mem_ctx,
2027 struct iovec **piov,
2028 uint8_t **phdr,
2029 uint8_t *pwct,
2030 uint16_t **pvwv,
2031 uint32_t *pvwv_offset,
2032 uint32_t *pnum_bytes,
2033 uint8_t **pbytes,
2034 uint32_t *pbytes_offset,
2035 uint8_t **pinbuf,
2036 const struct smb1cli_req_expected_response *expected,
2037 size_t num_expected)
2039 struct smbXcli_req_state *state =
2040 tevent_req_data(req,
2041 struct smbXcli_req_state);
2042 NTSTATUS status = NT_STATUS_OK;
2043 struct iovec *recv_iov = NULL;
2044 uint8_t *hdr = NULL;
2045 uint8_t wct = 0;
2046 uint32_t vwv_offset = 0;
2047 uint16_t *vwv = NULL;
2048 uint32_t num_bytes = 0;
2049 uint32_t bytes_offset = 0;
2050 uint8_t *bytes = NULL;
2051 size_t i;
2052 bool found_status = false;
2053 bool found_size = false;
2055 if (piov != NULL) {
2056 *piov = NULL;
2058 if (phdr != NULL) {
2059 *phdr = 0;
2061 if (pwct != NULL) {
2062 *pwct = 0;
2064 if (pvwv != NULL) {
2065 *pvwv = NULL;
2067 if (pvwv_offset != NULL) {
2068 *pvwv_offset = 0;
2070 if (pnum_bytes != NULL) {
2071 *pnum_bytes = 0;
2073 if (pbytes != NULL) {
2074 *pbytes = NULL;
2076 if (pbytes_offset != NULL) {
2077 *pbytes_offset = 0;
2079 if (pinbuf != NULL) {
2080 *pinbuf = NULL;
2083 if (state->inbuf != NULL) {
2084 recv_iov = state->smb1.recv_iov;
2085 state->smb1.recv_iov = NULL;
2086 if (state->smb1.recv_cmd != SMBreadBraw) {
2087 hdr = (uint8_t *)recv_iov[0].iov_base;
2088 wct = recv_iov[1].iov_len/2;
2089 vwv = (uint16_t *)recv_iov[1].iov_base;
2090 vwv_offset = PTR_DIFF(vwv, hdr);
2091 num_bytes = recv_iov[2].iov_len;
2092 bytes = (uint8_t *)recv_iov[2].iov_base;
2093 bytes_offset = PTR_DIFF(bytes, hdr);
2097 if (tevent_req_is_nterror(req, &status)) {
2098 for (i=0; i < num_expected; i++) {
2099 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2100 found_status = true;
2101 break;
2105 if (found_status) {
2106 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2109 return status;
2112 if (num_expected == 0) {
2113 found_status = true;
2114 found_size = true;
2117 status = state->smb1.recv_status;
2119 for (i=0; i < num_expected; i++) {
2120 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2121 continue;
2124 found_status = true;
2125 if (expected[i].wct == 0) {
2126 found_size = true;
2127 break;
2130 if (expected[i].wct == wct) {
2131 found_size = true;
2132 break;
2136 if (!found_status) {
2137 return status;
2140 if (!found_size) {
2141 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2144 if (piov != NULL) {
2145 *piov = talloc_move(mem_ctx, &recv_iov);
2148 if (phdr != NULL) {
2149 *phdr = hdr;
2151 if (pwct != NULL) {
2152 *pwct = wct;
2154 if (pvwv != NULL) {
2155 *pvwv = vwv;
2157 if (pvwv_offset != NULL) {
2158 *pvwv_offset = vwv_offset;
2160 if (pnum_bytes != NULL) {
2161 *pnum_bytes = num_bytes;
2163 if (pbytes != NULL) {
2164 *pbytes = bytes;
2166 if (pbytes_offset != NULL) {
2167 *pbytes_offset = bytes_offset;
2169 if (pinbuf != NULL) {
2170 *pinbuf = state->inbuf;
2173 return status;
2176 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2178 size_t wct_ofs;
2179 int i;
2181 wct_ofs = HDR_WCT;
2183 for (i=0; i<num_reqs; i++) {
2184 struct smbXcli_req_state *state;
2185 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2186 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2187 state->smb1.iov_count-2);
2188 wct_ofs = (wct_ofs + 3) & ~3;
2190 return wct_ofs;
2193 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2195 struct smbXcli_req_state *first_state =
2196 tevent_req_data(reqs[0],
2197 struct smbXcli_req_state);
2198 struct smbXcli_req_state *state;
2199 size_t wct_offset;
2200 size_t chain_padding = 0;
2201 int i, iovlen;
2202 struct iovec *iov = NULL;
2203 struct iovec *this_iov;
2204 NTSTATUS status;
2205 size_t nbt_len;
2207 if (num_reqs == 1) {
2208 return smb1cli_req_writev_submit(reqs[0], first_state,
2209 first_state->smb1.iov,
2210 first_state->smb1.iov_count);
2213 iovlen = 0;
2214 for (i=0; i<num_reqs; i++) {
2215 if (!tevent_req_is_in_progress(reqs[i])) {
2216 return NT_STATUS_INTERNAL_ERROR;
2219 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2221 if (state->smb1.iov_count < 4) {
2222 return NT_STATUS_INVALID_PARAMETER_MIX;
2225 if (i == 0) {
2227 * The NBT and SMB header
2229 iovlen += 2;
2230 } else {
2232 * Chain padding
2234 iovlen += 1;
2238 * words and bytes
2240 iovlen += state->smb1.iov_count - 2;
2243 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2244 if (iov == NULL) {
2245 return NT_STATUS_NO_MEMORY;
2248 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2249 first_state, reqs, sizeof(*reqs) * num_reqs);
2250 if (first_state->smb1.chained_requests == NULL) {
2251 TALLOC_FREE(iov);
2252 return NT_STATUS_NO_MEMORY;
2255 wct_offset = HDR_WCT;
2256 this_iov = iov;
2258 for (i=0; i<num_reqs; i++) {
2259 size_t next_padding = 0;
2260 uint16_t *vwv;
2262 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2264 if (i < num_reqs-1) {
2265 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2266 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2267 TALLOC_FREE(iov);
2268 TALLOC_FREE(first_state->smb1.chained_requests);
2269 return NT_STATUS_INVALID_PARAMETER_MIX;
2273 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2274 state->smb1.iov_count-2) + 1;
2275 if ((wct_offset % 4) != 0) {
2276 next_padding = 4 - (wct_offset % 4);
2278 wct_offset += next_padding;
2279 vwv = state->smb1.vwv;
2281 if (i < num_reqs-1) {
2282 struct smbXcli_req_state *next_state =
2283 tevent_req_data(reqs[i+1],
2284 struct smbXcli_req_state);
2285 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2286 SCVAL(vwv+0, 1, 0);
2287 SSVAL(vwv+1, 0, wct_offset);
2288 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2289 /* properly end the chain */
2290 SCVAL(vwv+0, 0, 0xff);
2291 SCVAL(vwv+0, 1, 0xff);
2292 SSVAL(vwv+1, 0, 0);
2295 if (i == 0) {
2297 * The NBT and SMB header
2299 this_iov[0] = state->smb1.iov[0];
2300 this_iov[1] = state->smb1.iov[1];
2301 this_iov += 2;
2302 } else {
2304 * This one is a bit subtle. We have to add
2305 * chain_padding bytes between the requests, and we
2306 * have to also include the wct field of the
2307 * subsequent requests. We use the subsequent header
2308 * for the padding, it contains the wct field in its
2309 * last byte.
2311 this_iov[0].iov_len = chain_padding+1;
2312 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2313 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2314 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2315 this_iov += 1;
2319 * copy the words and bytes
2321 memcpy(this_iov, state->smb1.iov+2,
2322 sizeof(struct iovec) * (state->smb1.iov_count-2));
2323 this_iov += state->smb1.iov_count - 2;
2324 chain_padding = next_padding;
2327 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2328 if (nbt_len > first_state->conn->smb1.max_xmit) {
2329 TALLOC_FREE(iov);
2330 TALLOC_FREE(first_state->smb1.chained_requests);
2331 return NT_STATUS_INVALID_PARAMETER_MIX;
2334 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2335 if (!NT_STATUS_IS_OK(status)) {
2336 TALLOC_FREE(iov);
2337 TALLOC_FREE(first_state->smb1.chained_requests);
2338 return status;
2341 return NT_STATUS_OK;
2344 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2346 return ((tevent_queue_length(conn->outgoing) != 0)
2347 || (talloc_array_length(conn->pending) != 0));
2350 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2352 return conn->smb2.server.capabilities;
2355 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2357 return conn->smb2.server.security_mode;
2360 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2362 return conn->smb2.server.max_trans_size;
2365 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2367 return conn->smb2.server.max_read_size;
2370 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2372 return conn->smb2.server.max_write_size;
2375 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2376 uint16_t max_credits)
2378 conn->smb2.max_credits = max_credits;
2381 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2383 static bool smb2cli_req_cancel(struct tevent_req *req)
2385 struct smbXcli_req_state *state =
2386 tevent_req_data(req,
2387 struct smbXcli_req_state);
2388 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2389 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2390 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2391 struct smbXcli_tcon *tcon = state->tcon;
2392 struct smbXcli_session *session = state->session;
2393 uint8_t *fixed = state->smb2.pad;
2394 uint16_t fixed_len = 4;
2395 struct tevent_req *subreq;
2396 struct smbXcli_req_state *substate;
2397 NTSTATUS status;
2399 SSVAL(fixed, 0, 0x04);
2400 SSVAL(fixed, 2, 0);
2402 subreq = smb2cli_req_create(state, state->ev,
2403 state->conn,
2404 SMB2_OP_CANCEL,
2405 flags, 0,
2406 0, /* timeout */
2407 tcon, session,
2408 fixed, fixed_len,
2409 NULL, 0);
2410 if (subreq == NULL) {
2411 return false;
2413 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2415 if (flags & SMB2_HDR_FLAG_ASYNC) {
2416 mid = 0;
2419 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2420 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2421 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2423 status = smb2cli_req_compound_submit(&subreq, 1);
2424 if (!NT_STATUS_IS_OK(status)) {
2425 TALLOC_FREE(subreq);
2426 return false;
2429 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2431 return true;
2434 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2436 /* we do not care about the result */
2437 TALLOC_FREE(subreq);
2440 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2441 struct tevent_context *ev,
2442 struct smbXcli_conn *conn,
2443 uint16_t cmd,
2444 uint32_t additional_flags,
2445 uint32_t clear_flags,
2446 uint32_t timeout_msec,
2447 struct smbXcli_tcon *tcon,
2448 struct smbXcli_session *session,
2449 const uint8_t *fixed,
2450 uint16_t fixed_len,
2451 const uint8_t *dyn,
2452 uint32_t dyn_len)
2454 struct tevent_req *req;
2455 struct smbXcli_req_state *state;
2456 uint32_t flags = 0;
2457 uint32_t tid = 0;
2458 uint64_t uid = 0;
2459 bool use_channel_sequence = false;
2460 uint16_t channel_sequence = 0;
2462 req = tevent_req_create(mem_ctx, &state,
2463 struct smbXcli_req_state);
2464 if (req == NULL) {
2465 return NULL;
2468 state->ev = ev;
2469 state->conn = conn;
2470 state->session = session;
2471 state->tcon = tcon;
2473 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2474 use_channel_sequence = true;
2475 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2476 use_channel_sequence = true;
2479 if (session) {
2480 uid = session->smb2->session_id;
2482 if (use_channel_sequence) {
2483 channel_sequence = session->smb2->channel_sequence;
2486 state->smb2.should_sign = session->smb2->should_sign;
2487 state->smb2.should_encrypt = session->smb2->should_encrypt;
2489 if (cmd == SMB2_OP_SESSSETUP &&
2490 session->smb2->signing_key.length != 0) {
2491 state->smb2.should_sign = true;
2494 if (cmd == SMB2_OP_SESSSETUP &&
2495 session->smb2_channel.signing_key.length == 0) {
2496 state->smb2.should_encrypt = false;
2500 if (tcon) {
2501 tid = tcon->smb2.tcon_id;
2503 if (tcon->smb2.should_encrypt) {
2504 state->smb2.should_encrypt = true;
2508 if (state->smb2.should_encrypt) {
2509 state->smb2.should_sign = false;
2512 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2513 if (state->smb2.recv_iov == NULL) {
2514 TALLOC_FREE(req);
2515 return NULL;
2518 flags |= additional_flags;
2519 flags &= ~clear_flags;
2521 state->smb2.fixed = fixed;
2522 state->smb2.fixed_len = fixed_len;
2523 state->smb2.dyn = dyn;
2524 state->smb2.dyn_len = dyn_len;
2526 if (state->smb2.should_encrypt) {
2527 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2528 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2531 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2532 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2533 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2534 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2535 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2536 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2537 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2538 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2540 switch (cmd) {
2541 case SMB2_OP_CANCEL:
2542 state->one_way = true;
2543 break;
2544 case SMB2_OP_BREAK:
2546 * If this is a dummy request, it will have
2547 * UINT64_MAX as message id.
2548 * If we send on break acknowledgement,
2549 * this gets overwritten later.
2551 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2552 break;
2555 if (timeout_msec > 0) {
2556 struct timeval endtime;
2558 endtime = timeval_current_ofs_msec(timeout_msec);
2559 if (!tevent_req_set_endtime(req, ev, endtime)) {
2560 return req;
2564 return req;
2567 void smb2cli_req_set_notify_async(struct tevent_req *req)
2569 struct smbXcli_req_state *state =
2570 tevent_req_data(req,
2571 struct smbXcli_req_state);
2573 state->smb2.notify_async = true;
2576 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2577 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2578 TALLOC_CTX *tmp_mem,
2579 uint8_t *inbuf);
2581 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2582 int num_reqs)
2584 struct smbXcli_req_state *state;
2585 struct tevent_req *subreq;
2586 struct iovec *iov;
2587 int i, num_iov, nbt_len;
2590 * 1 for the nbt length
2591 * per request: TRANSFORM, HDR, fixed, dyn, padding
2592 * -1 because the last one does not need padding
2595 iov = talloc_array(reqs[0], struct iovec, 1 + 5*num_reqs - 1);
2596 if (iov == NULL) {
2597 return NT_STATUS_NO_MEMORY;
2600 num_iov = 1;
2601 nbt_len = 0;
2603 for (i=0; i<num_reqs; i++) {
2604 int tf_iov;
2605 int hdr_iov;
2606 size_t reqlen;
2607 bool ret;
2608 uint16_t opcode;
2609 uint64_t avail;
2610 uint16_t charge;
2611 uint16_t credits;
2612 uint64_t mid;
2613 const DATA_BLOB *signing_key = NULL;
2614 const DATA_BLOB *encryption_key = NULL;
2616 if (!tevent_req_is_in_progress(reqs[i])) {
2617 return NT_STATUS_INTERNAL_ERROR;
2620 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2622 if (!smbXcli_conn_is_connected(state->conn)) {
2623 return NT_STATUS_CONNECTION_DISCONNECTED;
2626 if ((state->conn->protocol != PROTOCOL_NONE) &&
2627 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2628 return NT_STATUS_REVISION_MISMATCH;
2631 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2632 if (opcode == SMB2_OP_CANCEL) {
2633 goto skip_credits;
2636 avail = UINT64_MAX - state->conn->smb2.mid;
2637 if (avail < 1) {
2638 return NT_STATUS_CONNECTION_ABORTED;
2641 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2642 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2643 } else {
2644 charge = 1;
2647 charge = MAX(state->smb2.credit_charge, charge);
2649 avail = MIN(avail, state->conn->smb2.cur_credits);
2650 if (avail < charge) {
2651 return NT_STATUS_INTERNAL_ERROR;
2654 credits = 0;
2655 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2656 credits = state->conn->smb2.max_credits -
2657 state->conn->smb2.cur_credits;
2659 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2660 credits += 1;
2663 mid = state->conn->smb2.mid;
2664 state->conn->smb2.mid += charge;
2665 state->conn->smb2.cur_credits -= charge;
2667 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2668 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2670 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2671 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2673 skip_credits:
2674 if (state->session) {
2676 * We prefer the channel signing key if it is
2677 * already there.
2679 if (state->smb2.should_sign) {
2680 signing_key = &state->session->smb2_channel.signing_key;
2684 * If it is a channel binding, we already have the main
2685 * signing key and try that one.
2687 if (signing_key && signing_key->length == 0) {
2688 signing_key = &state->session->smb2->signing_key;
2692 * If we do not have any session key yet, we skip the
2693 * signing of SMB2_OP_SESSSETUP requests.
2695 if (signing_key && signing_key->length == 0) {
2696 signing_key = NULL;
2699 if (state->smb2.should_encrypt) {
2700 encryption_key = &state->session->smb2->encryption_key;
2704 if (encryption_key) {
2705 tf_iov = num_iov;
2706 iov[num_iov].iov_base = state->smb2.transform;
2707 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2708 num_iov += 1;
2711 hdr_iov = num_iov;
2712 iov[num_iov].iov_base = state->smb2.hdr;
2713 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2714 num_iov += 1;
2716 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2717 iov[num_iov].iov_len = state->smb2.fixed_len;
2718 num_iov += 1;
2720 if (state->smb2.dyn != NULL) {
2721 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2722 iov[num_iov].iov_len = state->smb2.dyn_len;
2723 num_iov += 1;
2726 reqlen = sizeof(state->smb2.hdr);
2727 reqlen += state->smb2.fixed_len;
2728 reqlen += state->smb2.dyn_len;
2730 if (i < num_reqs-1) {
2731 if ((reqlen % 8) > 0) {
2732 uint8_t pad = 8 - (reqlen % 8);
2733 iov[num_iov].iov_base = state->smb2.pad;
2734 iov[num_iov].iov_len = pad;
2735 num_iov += 1;
2736 reqlen += pad;
2738 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2741 if (encryption_key) {
2742 NTSTATUS status;
2743 uint8_t *buf;
2744 int vi;
2746 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2747 state->session->smb2->nonce_low);
2748 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2749 state->session->smb2->nonce_high);
2751 state->session->smb2->nonce_low += 1;
2752 if (state->session->smb2->nonce_low == 0) {
2753 state->session->smb2->nonce_high += 1;
2754 state->session->smb2->nonce_low += 1;
2757 SBVAL(state->smb2.transform, SMB2_TF_MSG_SIZE,
2758 reqlen);
2760 buf = talloc_array(iov, uint8_t, reqlen);
2761 if (buf == NULL) {
2762 return NT_STATUS_NO_MEMORY;
2765 reqlen += SMB2_TF_HDR_SIZE;
2768 * We copy the buffers before encrypting them,
2769 * this is at least currently needed for the
2770 * to keep state->smb2.hdr.
2772 * Also the callers may expect there buffers
2773 * to be const.
2775 for (vi = hdr_iov; vi < num_iov; vi++) {
2776 struct iovec *v = &iov[vi];
2777 const uint8_t *o = (const uint8_t *)v->iov_base;
2779 memcpy(buf, o, v->iov_len);
2780 v->iov_base = (void *)buf;
2781 buf += v->iov_len;
2784 status = smb2_signing_encrypt_pdu(*encryption_key,
2785 state->session->conn->protocol,
2786 &iov[tf_iov], num_iov - tf_iov);
2787 if (!NT_STATUS_IS_OK(status)) {
2788 return status;
2790 } else if (signing_key) {
2791 NTSTATUS status;
2793 status = smb2_signing_sign_pdu(*signing_key,
2794 state->session->conn->protocol,
2795 &iov[hdr_iov], num_iov - hdr_iov);
2796 if (!NT_STATUS_IS_OK(status)) {
2797 return status;
2801 nbt_len += reqlen;
2803 ret = smbXcli_req_set_pending(reqs[i]);
2804 if (!ret) {
2805 return NT_STATUS_NO_MEMORY;
2809 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2810 _smb_setlen_tcp(state->length_hdr, nbt_len);
2811 iov[0].iov_base = state->length_hdr;
2812 iov[0].iov_len = sizeof(state->length_hdr);
2814 if (state->conn->dispatch_incoming == NULL) {
2815 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2818 subreq = writev_send(state, state->ev, state->conn->outgoing,
2819 state->conn->write_fd, false, iov, num_iov);
2820 if (subreq == NULL) {
2821 return NT_STATUS_NO_MEMORY;
2823 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2824 return NT_STATUS_OK;
2827 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2829 struct smbXcli_req_state *state =
2830 tevent_req_data(req,
2831 struct smbXcli_req_state);
2833 state->smb2.credit_charge = charge;
2836 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2837 struct tevent_context *ev,
2838 struct smbXcli_conn *conn,
2839 uint16_t cmd,
2840 uint32_t additional_flags,
2841 uint32_t clear_flags,
2842 uint32_t timeout_msec,
2843 struct smbXcli_tcon *tcon,
2844 struct smbXcli_session *session,
2845 const uint8_t *fixed,
2846 uint16_t fixed_len,
2847 const uint8_t *dyn,
2848 uint32_t dyn_len)
2850 struct tevent_req *req;
2851 NTSTATUS status;
2853 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2854 additional_flags, clear_flags,
2855 timeout_msec,
2856 tcon, session,
2857 fixed, fixed_len, dyn, dyn_len);
2858 if (req == NULL) {
2859 return NULL;
2861 if (!tevent_req_is_in_progress(req)) {
2862 return tevent_req_post(req, ev);
2864 status = smb2cli_req_compound_submit(&req, 1);
2865 if (tevent_req_nterror(req, status)) {
2866 return tevent_req_post(req, ev);
2868 return req;
2871 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2873 struct tevent_req *req =
2874 tevent_req_callback_data(subreq,
2875 struct tevent_req);
2876 struct smbXcli_req_state *state =
2877 tevent_req_data(req,
2878 struct smbXcli_req_state);
2879 ssize_t nwritten;
2880 int err;
2882 nwritten = writev_recv(subreq, &err);
2883 TALLOC_FREE(subreq);
2884 if (nwritten == -1) {
2885 /* here, we need to notify all pending requests */
2886 NTSTATUS status = map_nt_error_from_unix_common(err);
2887 smbXcli_conn_disconnect(state->conn, status);
2888 return;
2892 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
2893 uint8_t *buf,
2894 size_t buflen,
2895 TALLOC_CTX *mem_ctx,
2896 struct iovec **piov, int *pnum_iov)
2898 struct iovec *iov;
2899 int num_iov = 0;
2900 size_t taken = 0;
2901 uint8_t *first_hdr = buf;
2903 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2904 if (iov == NULL) {
2905 return NT_STATUS_NO_MEMORY;
2908 while (taken < buflen) {
2909 uint8_t *tf = NULL;
2910 size_t tf_len = 0;
2911 size_t len = buflen - taken;
2912 uint8_t *hdr = first_hdr + taken;
2913 struct iovec *cur;
2914 size_t full_size;
2915 size_t next_command_ofs;
2916 uint16_t body_size;
2917 struct iovec *iov_tmp;
2919 if (len < 4) {
2920 DEBUG(10, ("%d bytes left, expected at least %d\n",
2921 (int)len, 4));
2922 goto inval;
2924 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
2925 struct smbXcli_session *s;
2926 uint64_t uid;
2927 struct iovec tf_iov[2];
2928 NTSTATUS status;
2930 if (len < SMB2_TF_HDR_SIZE) {
2931 DEBUG(10, ("%d bytes left, expected at least %d\n",
2932 (int)len, SMB2_TF_HDR_SIZE));
2933 goto inval;
2935 tf = hdr;
2936 tf_len = SMB2_TF_HDR_SIZE;
2937 taken += tf_len;
2939 hdr = first_hdr + taken;
2940 len = IVAL(tf, SMB2_TF_MSG_SIZE);
2941 uid = BVAL(tf, SMB2_TF_SESSION_ID);
2943 s = conn->sessions;
2944 for (; s; s = s->next) {
2945 if (s->smb2->session_id != uid) {
2946 continue;
2948 break;
2951 if (s == NULL) {
2952 DEBUG(10, ("unknown session_id %llu\n",
2953 (unsigned long long)uid));
2954 goto inval;
2957 tf_iov[0].iov_base = (void *)tf;
2958 tf_iov[0].iov_len = tf_len;
2959 tf_iov[1].iov_base = (void *)hdr;
2960 tf_iov[1].iov_len = len;
2962 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
2963 conn->protocol,
2964 tf_iov, 2);
2965 if (!NT_STATUS_IS_OK(status)) {
2966 TALLOC_FREE(iov);
2967 return status;
2972 * We need the header plus the body length field
2975 if (len < SMB2_HDR_BODY + 2) {
2976 DEBUG(10, ("%d bytes left, expected at least %d\n",
2977 (int)len, SMB2_HDR_BODY));
2978 goto inval;
2980 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2981 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2982 IVAL(hdr, 0)));
2983 goto inval;
2985 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2986 DEBUG(10, ("Got HDR len %d, expected %d\n",
2987 SVAL(hdr, 4), SMB2_HDR_BODY));
2988 goto inval;
2991 full_size = len;
2992 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2993 body_size = SVAL(hdr, SMB2_HDR_BODY);
2995 if (next_command_ofs != 0) {
2996 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2997 goto inval;
2999 if (next_command_ofs > full_size) {
3000 goto inval;
3002 if (tf && next_command_ofs < len) {
3003 goto inval;
3005 full_size = next_command_ofs;
3007 if (body_size < 2) {
3008 goto inval;
3010 body_size &= 0xfffe;
3012 if (body_size > (full_size - SMB2_HDR_BODY)) {
3013 goto inval;
3016 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3017 num_iov + 4);
3018 if (iov_tmp == NULL) {
3019 TALLOC_FREE(iov);
3020 return NT_STATUS_NO_MEMORY;
3022 iov = iov_tmp;
3023 cur = &iov[num_iov];
3024 num_iov += 4;
3026 cur[0].iov_base = tf;
3027 cur[0].iov_len = tf_len;
3028 cur[1].iov_base = hdr;
3029 cur[1].iov_len = SMB2_HDR_BODY;
3030 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3031 cur[2].iov_len = body_size;
3032 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3033 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3035 taken += full_size;
3038 *piov = iov;
3039 *pnum_iov = num_iov;
3040 return NT_STATUS_OK;
3042 inval:
3043 TALLOC_FREE(iov);
3044 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3047 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3048 uint64_t mid)
3050 size_t num_pending = talloc_array_length(conn->pending);
3051 size_t i;
3053 for (i=0; i<num_pending; i++) {
3054 struct tevent_req *req = conn->pending[i];
3055 struct smbXcli_req_state *state =
3056 tevent_req_data(req,
3057 struct smbXcli_req_state);
3059 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3060 return req;
3063 return NULL;
3066 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3067 TALLOC_CTX *tmp_mem,
3068 uint8_t *inbuf)
3070 struct tevent_req *req;
3071 struct smbXcli_req_state *state = NULL;
3072 struct iovec *iov;
3073 int i, num_iov;
3074 NTSTATUS status;
3075 bool defer = true;
3076 struct smbXcli_session *last_session = NULL;
3077 size_t inbuf_len = smb_len_tcp(inbuf);
3079 status = smb2cli_inbuf_parse_compound(conn,
3080 inbuf + NBT_HDR_SIZE,
3081 inbuf_len,
3082 tmp_mem,
3083 &iov, &num_iov);
3084 if (!NT_STATUS_IS_OK(status)) {
3085 return status;
3088 for (i=0; i<num_iov; i+=4) {
3089 uint8_t *inbuf_ref = NULL;
3090 struct iovec *cur = &iov[i];
3091 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3092 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3093 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3094 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3095 uint16_t req_opcode;
3096 uint32_t req_flags;
3097 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3098 uint32_t new_credits;
3099 struct smbXcli_session *session = NULL;
3100 const DATA_BLOB *signing_key = NULL;
3102 new_credits = conn->smb2.cur_credits;
3103 new_credits += credits;
3104 if (new_credits > UINT16_MAX) {
3105 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3107 conn->smb2.cur_credits += credits;
3109 req = smb2cli_conn_find_pending(conn, mid);
3110 if (req == NULL) {
3111 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3113 state = tevent_req_data(req, struct smbXcli_req_state);
3115 state->smb2.got_async = false;
3117 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3118 if (opcode != req_opcode) {
3119 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3121 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3123 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3124 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3127 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3128 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3129 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3130 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3133 * async interim responses are not signed,
3134 * even if the SMB2_HDR_FLAG_SIGNED flag
3135 * is set.
3137 req_flags |= SMB2_HDR_FLAG_ASYNC;
3138 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3139 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3141 if (state->smb2.notify_async) {
3142 state->smb2.got_async = true;
3143 tevent_req_defer_callback(req, state->ev);
3144 tevent_req_notify_callback(req);
3146 continue;
3149 session = state->session;
3150 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3151 session = last_session;
3153 last_session = session;
3155 if (state->smb2.should_sign) {
3156 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3157 return NT_STATUS_ACCESS_DENIED;
3161 if (flags & SMB2_HDR_FLAG_SIGNED) {
3162 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3164 if (session == NULL) {
3165 struct smbXcli_session *s;
3167 s = state->conn->sessions;
3168 for (; s; s = s->next) {
3169 if (s->smb2->session_id != uid) {
3170 continue;
3173 session = s;
3174 break;
3178 if (session == NULL) {
3179 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3182 last_session = session;
3183 signing_key = &session->smb2_channel.signing_key;
3186 if (opcode == SMB2_OP_SESSSETUP) {
3188 * We prefer the channel signing key, if it is
3189 * already there.
3191 * If we do not have a channel signing key yet,
3192 * we try the main signing key, if it is not
3193 * the final response.
3195 if (signing_key && signing_key->length == 0 &&
3196 !NT_STATUS_IS_OK(status)) {
3197 signing_key = &session->smb2->signing_key;
3200 if (signing_key && signing_key->length == 0) {
3202 * If we do not have a session key to
3203 * verify the signature, we defer the
3204 * signing check to the caller.
3206 * The caller gets NT_STATUS_OK, it
3207 * has to call
3208 * smb2cli_session_set_session_key()
3209 * or
3210 * smb2cli_session_set_channel_key()
3211 * which will check the signature
3212 * with the channel signing key.
3214 signing_key = NULL;
3218 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3220 * if the server returns NT_STATUS_USER_SESSION_DELETED
3221 * the response is not signed and we should
3222 * propagate the NT_STATUS_USER_SESSION_DELETED
3223 * status to the caller.
3225 signing_key = NULL;
3226 } else if (state->smb2.should_encrypt) {
3227 if (cur[0].iov_len != SMB2_TF_HDR_SIZE) {
3228 return NT_STATUS_ACCESS_DENIED;
3232 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3233 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3234 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3236 * if the server returns
3237 * NT_STATUS_NETWORK_NAME_DELETED
3238 * NT_STATUS_FILE_CLOSED
3239 * NT_STATUS_INVALID_PARAMETER
3240 * the response might not be signed
3241 * as this happens before the signing checks.
3243 * If server echos the signature (or all zeros)
3244 * we should report the status from the server
3245 * to the caller.
3247 if (signing_key) {
3248 int cmp;
3250 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3251 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3252 16);
3253 if (cmp == 0) {
3254 state->smb2.signing_skipped = true;
3255 signing_key = NULL;
3258 if (signing_key) {
3259 int cmp;
3260 static const uint8_t zeros[16];
3262 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3263 zeros,
3264 16);
3265 if (cmp == 0) {
3266 state->smb2.signing_skipped = true;
3267 signing_key = NULL;
3272 if (signing_key) {
3273 status = smb2_signing_check_pdu(*signing_key,
3274 state->conn->protocol,
3275 &cur[1], 3);
3276 if (!NT_STATUS_IS_OK(status)) {
3278 * If the signing check fails, we disconnect
3279 * the connection.
3281 return status;
3285 smbXcli_req_unset_pending(req);
3288 * There might be more than one response
3289 * we need to defer the notifications
3291 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3292 defer = false;
3295 if (defer) {
3296 tevent_req_defer_callback(req, state->ev);
3300 * Note: here we use talloc_reference() in a way
3301 * that does not expose it to the caller.
3303 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3304 if (tevent_req_nomem(inbuf_ref, req)) {
3305 continue;
3308 /* copy the related buffers */
3309 state->smb2.recv_iov[0] = cur[1];
3310 state->smb2.recv_iov[1] = cur[2];
3311 state->smb2.recv_iov[2] = cur[3];
3313 tevent_req_done(req);
3316 if (defer) {
3317 return NT_STATUS_RETRY;
3320 return NT_STATUS_OK;
3323 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3324 struct iovec **piov,
3325 const struct smb2cli_req_expected_response *expected,
3326 size_t num_expected)
3328 struct smbXcli_req_state *state =
3329 tevent_req_data(req,
3330 struct smbXcli_req_state);
3331 NTSTATUS status;
3332 size_t body_size;
3333 bool found_status = false;
3334 bool found_size = false;
3335 size_t i;
3337 if (piov != NULL) {
3338 *piov = NULL;
3341 if (state->smb2.got_async) {
3342 return STATUS_PENDING;
3345 if (tevent_req_is_nterror(req, &status)) {
3346 for (i=0; i < num_expected; i++) {
3347 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3348 found_status = true;
3349 break;
3353 if (found_status) {
3354 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3357 return status;
3360 if (num_expected == 0) {
3361 found_status = true;
3362 found_size = true;
3365 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3366 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3368 for (i=0; i < num_expected; i++) {
3369 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3370 continue;
3373 found_status = true;
3374 if (expected[i].body_size == 0) {
3375 found_size = true;
3376 break;
3379 if (expected[i].body_size == body_size) {
3380 found_size = true;
3381 break;
3385 if (!found_status) {
3386 return status;
3389 if (state->smb2.signing_skipped) {
3390 if (num_expected > 0) {
3391 return NT_STATUS_ACCESS_DENIED;
3393 if (!NT_STATUS_IS_ERR(status)) {
3394 return NT_STATUS_ACCESS_DENIED;
3398 if (!found_size) {
3399 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3402 if (piov != NULL) {
3403 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3406 return status;
3409 static const struct {
3410 enum protocol_types proto;
3411 const char *smb1_name;
3412 } smb1cli_prots[] = {
3413 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3414 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3415 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3416 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3417 {PROTOCOL_LANMAN2, "LM1.2X002"},
3418 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3419 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3420 {PROTOCOL_LANMAN2, "Samba"},
3421 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3422 {PROTOCOL_NT1, "NT LM 0.12"},
3423 {PROTOCOL_SMB2_02, "SMB 2.002"},
3424 {PROTOCOL_SMB2_10, "SMB 2.???"},
3427 static const struct {
3428 enum protocol_types proto;
3429 uint16_t smb2_dialect;
3430 } smb2cli_prots[] = {
3431 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3432 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3433 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3434 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3435 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3438 struct smbXcli_negprot_state {
3439 struct smbXcli_conn *conn;
3440 struct tevent_context *ev;
3441 uint32_t timeout_msec;
3442 enum protocol_types min_protocol;
3443 enum protocol_types max_protocol;
3445 struct {
3446 uint8_t fixed[36];
3447 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3448 } smb2;
3451 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3452 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3453 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3454 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3455 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3456 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3457 TALLOC_CTX *frame,
3458 uint8_t *inbuf);
3460 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3461 struct tevent_context *ev,
3462 struct smbXcli_conn *conn,
3463 uint32_t timeout_msec,
3464 enum protocol_types min_protocol,
3465 enum protocol_types max_protocol)
3467 struct tevent_req *req, *subreq;
3468 struct smbXcli_negprot_state *state;
3470 req = tevent_req_create(mem_ctx, &state,
3471 struct smbXcli_negprot_state);
3472 if (req == NULL) {
3473 return NULL;
3475 state->conn = conn;
3476 state->ev = ev;
3477 state->timeout_msec = timeout_msec;
3478 state->min_protocol = min_protocol;
3479 state->max_protocol = max_protocol;
3481 if (min_protocol == PROTOCOL_NONE) {
3482 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3483 return tevent_req_post(req, ev);
3486 if (max_protocol == PROTOCOL_NONE) {
3487 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3488 return tevent_req_post(req, ev);
3491 if (min_protocol > max_protocol) {
3492 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3493 return tevent_req_post(req, ev);
3496 if ((min_protocol < PROTOCOL_SMB2_02) &&
3497 (max_protocol < PROTOCOL_SMB2_02)) {
3499 * SMB1 only...
3501 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3503 subreq = smbXcli_negprot_smb1_subreq(state);
3504 if (tevent_req_nomem(subreq, req)) {
3505 return tevent_req_post(req, ev);
3507 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3508 return req;
3511 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3512 (max_protocol >= PROTOCOL_SMB2_02)) {
3514 * SMB2 only...
3516 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3518 subreq = smbXcli_negprot_smb2_subreq(state);
3519 if (tevent_req_nomem(subreq, req)) {
3520 return tevent_req_post(req, ev);
3522 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3523 return req;
3527 * We send an SMB1 negprot with the SMB2 dialects
3528 * and expect a SMB1 or a SMB2 response.
3530 * smbXcli_negprot_dispatch_incoming() will fix the
3531 * callback to match protocol of the response.
3533 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3535 subreq = smbXcli_negprot_smb1_subreq(state);
3536 if (tevent_req_nomem(subreq, req)) {
3537 return tevent_req_post(req, ev);
3539 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3540 return req;
3543 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3545 struct tevent_req *req =
3546 tevent_req_callback_data(subreq,
3547 struct tevent_req);
3548 NTSTATUS status;
3551 * we just want the low level error
3553 status = tevent_req_simple_recv_ntstatus(subreq);
3554 TALLOC_FREE(subreq);
3555 if (tevent_req_nterror(req, status)) {
3556 return;
3559 /* this should never happen */
3560 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3563 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3565 size_t i;
3566 DATA_BLOB bytes = data_blob_null;
3567 uint8_t flags;
3568 uint16_t flags2;
3570 /* setup the protocol strings */
3571 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3572 uint8_t c = 2;
3573 bool ok;
3575 if (smb1cli_prots[i].proto < state->min_protocol) {
3576 continue;
3579 if (smb1cli_prots[i].proto > state->max_protocol) {
3580 continue;
3583 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3584 if (!ok) {
3585 return NULL;
3589 * We now it is already ascii and
3590 * we want NULL termination.
3592 ok = data_blob_append(state, &bytes,
3593 smb1cli_prots[i].smb1_name,
3594 strlen(smb1cli_prots[i].smb1_name)+1);
3595 if (!ok) {
3596 return NULL;
3600 smb1cli_req_flags(state->max_protocol,
3601 state->conn->smb1.client.capabilities,
3602 SMBnegprot,
3603 0, 0, &flags,
3604 0, 0, &flags2);
3606 return smb1cli_req_send(state, state->ev, state->conn,
3607 SMBnegprot,
3608 flags, ~flags,
3609 flags2, ~flags2,
3610 state->timeout_msec,
3611 0xFFFE, 0, NULL, /* pid, tid, session */
3612 0, NULL, /* wct, vwv */
3613 bytes.length, bytes.data);
3616 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3618 struct tevent_req *req =
3619 tevent_req_callback_data(subreq,
3620 struct tevent_req);
3621 struct smbXcli_negprot_state *state =
3622 tevent_req_data(req,
3623 struct smbXcli_negprot_state);
3624 struct smbXcli_conn *conn = state->conn;
3625 struct iovec *recv_iov = NULL;
3626 uint8_t *inhdr;
3627 uint8_t wct;
3628 uint16_t *vwv;
3629 uint32_t num_bytes;
3630 uint8_t *bytes;
3631 NTSTATUS status;
3632 uint16_t protnum;
3633 size_t i;
3634 size_t num_prots = 0;
3635 uint8_t flags;
3636 uint32_t client_capabilities = conn->smb1.client.capabilities;
3637 uint32_t both_capabilities;
3638 uint32_t server_capabilities = 0;
3639 uint32_t capabilities;
3640 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3641 uint32_t server_max_xmit = 0;
3642 uint32_t max_xmit;
3643 uint32_t server_max_mux = 0;
3644 uint16_t server_security_mode = 0;
3645 uint32_t server_session_key = 0;
3646 bool server_readbraw = false;
3647 bool server_writebraw = false;
3648 bool server_lockread = false;
3649 bool server_writeunlock = false;
3650 struct GUID server_guid = GUID_zero();
3651 DATA_BLOB server_gss_blob = data_blob_null;
3652 uint8_t server_challenge[8];
3653 char *server_workgroup = NULL;
3654 char *server_name = NULL;
3655 int server_time_zone = 0;
3656 NTTIME server_system_time = 0;
3657 static const struct smb1cli_req_expected_response expected[] = {
3659 .status = NT_STATUS_OK,
3660 .wct = 0x11, /* NT1 */
3663 .status = NT_STATUS_OK,
3664 .wct = 0x0D, /* LM */
3667 .status = NT_STATUS_OK,
3668 .wct = 0x01, /* CORE */
3672 ZERO_STRUCT(server_challenge);
3674 status = smb1cli_req_recv(subreq, state,
3675 &recv_iov,
3676 &inhdr,
3677 &wct,
3678 &vwv,
3679 NULL, /* pvwv_offset */
3680 &num_bytes,
3681 &bytes,
3682 NULL, /* pbytes_offset */
3683 NULL, /* pinbuf */
3684 expected, ARRAY_SIZE(expected));
3685 TALLOC_FREE(subreq);
3686 if (tevent_req_nterror(req, status)) {
3687 return;
3690 flags = CVAL(inhdr, HDR_FLG);
3692 protnum = SVAL(vwv, 0);
3694 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3695 if (smb1cli_prots[i].proto < state->min_protocol) {
3696 continue;
3699 if (smb1cli_prots[i].proto > state->max_protocol) {
3700 continue;
3703 if (protnum != num_prots) {
3704 num_prots++;
3705 continue;
3708 conn->protocol = smb1cli_prots[i].proto;
3709 break;
3712 if (conn->protocol == PROTOCOL_NONE) {
3713 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3714 return;
3717 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3718 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3719 "and the selected protocol level doesn't support it.\n"));
3720 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3721 return;
3724 if (flags & FLAG_SUPPORT_LOCKREAD) {
3725 server_lockread = true;
3726 server_writeunlock = true;
3729 if (conn->protocol >= PROTOCOL_NT1) {
3730 const char *client_signing = NULL;
3731 bool server_mandatory = false;
3732 bool server_allowed = false;
3733 const char *server_signing = NULL;
3734 bool ok;
3735 uint8_t key_len;
3737 if (wct != 0x11) {
3738 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3739 return;
3742 /* NT protocol */
3743 server_security_mode = CVAL(vwv + 1, 0);
3744 server_max_mux = SVAL(vwv + 1, 1);
3745 server_max_xmit = IVAL(vwv + 3, 1);
3746 server_session_key = IVAL(vwv + 7, 1);
3747 server_time_zone = SVALS(vwv + 15, 1);
3748 server_time_zone *= 60;
3749 /* this time arrives in real GMT */
3750 server_system_time = BVAL(vwv + 11, 1);
3751 server_capabilities = IVAL(vwv + 9, 1);
3753 key_len = CVAL(vwv + 16, 1);
3755 if (server_capabilities & CAP_RAW_MODE) {
3756 server_readbraw = true;
3757 server_writebraw = true;
3759 if (server_capabilities & CAP_LOCK_AND_READ) {
3760 server_lockread = true;
3763 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3764 DATA_BLOB blob1, blob2;
3766 if (num_bytes < 16) {
3767 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3768 return;
3771 blob1 = data_blob_const(bytes, 16);
3772 status = GUID_from_data_blob(&blob1, &server_guid);
3773 if (tevent_req_nterror(req, status)) {
3774 return;
3777 blob1 = data_blob_const(bytes+16, num_bytes-16);
3778 blob2 = data_blob_dup_talloc(state, blob1);
3779 if (blob1.length > 0 &&
3780 tevent_req_nomem(blob2.data, req)) {
3781 return;
3783 server_gss_blob = blob2;
3784 } else {
3785 DATA_BLOB blob1, blob2;
3787 if (num_bytes < key_len) {
3788 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3789 return;
3792 if (key_len != 0 && key_len != 8) {
3793 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3794 return;
3797 if (key_len == 8) {
3798 memcpy(server_challenge, bytes, 8);
3801 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3802 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3803 if (blob1.length > 0) {
3804 size_t len;
3806 len = utf16_len_n(blob1.data,
3807 blob1.length);
3808 blob1.length = len;
3810 ok = convert_string_talloc(state,
3811 CH_UTF16LE,
3812 CH_UNIX,
3813 blob1.data,
3814 blob1.length,
3815 &server_workgroup,
3816 &len);
3817 if (!ok) {
3818 status = map_nt_error_from_unix_common(errno);
3819 tevent_req_nterror(req, status);
3820 return;
3824 blob2.data += blob1.length;
3825 blob2.length -= blob1.length;
3826 if (blob2.length > 0) {
3827 size_t len;
3829 len = utf16_len_n(blob1.data,
3830 blob1.length);
3831 blob1.length = len;
3833 ok = convert_string_talloc(state,
3834 CH_UTF16LE,
3835 CH_UNIX,
3836 blob2.data,
3837 blob2.length,
3838 &server_name,
3839 &len);
3840 if (!ok) {
3841 status = map_nt_error_from_unix_common(errno);
3842 tevent_req_nterror(req, status);
3843 return;
3848 client_signing = "disabled";
3849 if (conn->allow_signing) {
3850 client_signing = "allowed";
3852 if (conn->mandatory_signing) {
3853 client_signing = "required";
3856 server_signing = "not supported";
3857 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3858 server_signing = "supported";
3859 server_allowed = true;
3861 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3862 server_signing = "required";
3863 server_mandatory = true;
3866 ok = smb_signing_set_negotiated(conn->smb1.signing,
3867 server_allowed,
3868 server_mandatory);
3869 if (!ok) {
3870 DEBUG(1,("cli_negprot: SMB signing is required, "
3871 "but client[%s] and server[%s] mismatch\n",
3872 client_signing, server_signing));
3873 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3874 return;
3877 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3878 DATA_BLOB blob1;
3879 uint8_t key_len;
3880 time_t t;
3882 if (wct != 0x0D) {
3883 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3884 return;
3887 server_security_mode = SVAL(vwv + 1, 0);
3888 server_max_xmit = SVAL(vwv + 2, 0);
3889 server_max_mux = SVAL(vwv + 3, 0);
3890 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3891 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3892 server_session_key = IVAL(vwv + 6, 0);
3893 server_time_zone = SVALS(vwv + 10, 0);
3894 server_time_zone *= 60;
3895 /* this time is converted to GMT by make_unix_date */
3896 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3897 unix_to_nt_time(&server_system_time, t);
3898 key_len = SVAL(vwv + 11, 0);
3900 if (num_bytes < key_len) {
3901 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3902 return;
3905 if (key_len != 0 && key_len != 8) {
3906 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3907 return;
3910 if (key_len == 8) {
3911 memcpy(server_challenge, bytes, 8);
3914 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3915 if (blob1.length > 0) {
3916 size_t len;
3917 bool ok;
3919 len = utf16_len_n(blob1.data,
3920 blob1.length);
3921 blob1.length = len;
3923 ok = convert_string_talloc(state,
3924 CH_DOS,
3925 CH_UNIX,
3926 blob1.data,
3927 blob1.length,
3928 &server_workgroup,
3929 &len);
3930 if (!ok) {
3931 status = map_nt_error_from_unix_common(errno);
3932 tevent_req_nterror(req, status);
3933 return;
3937 } else {
3938 /* the old core protocol */
3939 server_time_zone = get_time_zone(time(NULL));
3940 server_max_xmit = 1024;
3941 server_max_mux = 1;
3944 if (server_max_xmit < 1024) {
3945 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3946 return;
3949 if (server_max_mux < 1) {
3950 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3951 return;
3955 * Now calculate the negotiated capabilities
3956 * based on the mask for:
3957 * - client only flags
3958 * - flags used in both directions
3959 * - server only flags
3961 both_capabilities = client_capabilities & server_capabilities;
3962 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3963 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3964 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3966 max_xmit = MIN(client_max_xmit, server_max_xmit);
3968 conn->smb1.server.capabilities = server_capabilities;
3969 conn->smb1.capabilities = capabilities;
3971 conn->smb1.server.max_xmit = server_max_xmit;
3972 conn->smb1.max_xmit = max_xmit;
3974 conn->smb1.server.max_mux = server_max_mux;
3976 conn->smb1.server.security_mode = server_security_mode;
3978 conn->smb1.server.readbraw = server_readbraw;
3979 conn->smb1.server.writebraw = server_writebraw;
3980 conn->smb1.server.lockread = server_lockread;
3981 conn->smb1.server.writeunlock = server_writeunlock;
3983 conn->smb1.server.session_key = server_session_key;
3985 talloc_steal(conn, server_gss_blob.data);
3986 conn->smb1.server.gss_blob = server_gss_blob;
3987 conn->smb1.server.guid = server_guid;
3988 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3989 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3990 conn->smb1.server.name = talloc_move(conn, &server_name);
3992 conn->smb1.server.time_zone = server_time_zone;
3993 conn->smb1.server.system_time = server_system_time;
3995 tevent_req_done(req);
3998 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4000 size_t i;
4001 uint8_t *buf;
4002 uint16_t dialect_count = 0;
4004 buf = state->smb2.dyn;
4005 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4006 if (smb2cli_prots[i].proto < state->min_protocol) {
4007 continue;
4010 if (smb2cli_prots[i].proto > state->max_protocol) {
4011 continue;
4014 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4015 dialect_count++;
4018 buf = state->smb2.fixed;
4019 SSVAL(buf, 0, 36);
4020 SSVAL(buf, 2, dialect_count);
4021 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4022 SSVAL(buf, 6, 0); /* Reserved */
4023 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4024 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4025 } else {
4026 SIVAL(buf, 8, 0); /* Capabilities */
4028 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4029 NTSTATUS status;
4030 DATA_BLOB blob;
4032 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4033 state, &blob);
4034 if (!NT_STATUS_IS_OK(status)) {
4035 return NULL;
4037 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4038 } else {
4039 memset(buf+12, 0, 16); /* ClientGuid */
4041 SBVAL(buf, 28, 0); /* ClientStartTime */
4043 return smb2cli_req_send(state, state->ev,
4044 state->conn, SMB2_OP_NEGPROT,
4045 0, 0, /* flags */
4046 state->timeout_msec,
4047 NULL, NULL, /* tcon, session */
4048 state->smb2.fixed, sizeof(state->smb2.fixed),
4049 state->smb2.dyn, dialect_count*2);
4052 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4054 struct tevent_req *req =
4055 tevent_req_callback_data(subreq,
4056 struct tevent_req);
4057 struct smbXcli_negprot_state *state =
4058 tevent_req_data(req,
4059 struct smbXcli_negprot_state);
4060 struct smbXcli_conn *conn = state->conn;
4061 size_t security_offset, security_length;
4062 DATA_BLOB blob;
4063 NTSTATUS status;
4064 struct iovec *iov;
4065 uint8_t *body;
4066 size_t i;
4067 uint16_t dialect_revision;
4068 static const struct smb2cli_req_expected_response expected[] = {
4070 .status = NT_STATUS_OK,
4071 .body_size = 0x41
4075 status = smb2cli_req_recv(subreq, state, &iov,
4076 expected, ARRAY_SIZE(expected));
4077 TALLOC_FREE(subreq);
4078 if (tevent_req_nterror(req, status)) {
4079 return;
4082 body = (uint8_t *)iov[1].iov_base;
4084 dialect_revision = SVAL(body, 4);
4086 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4087 if (smb2cli_prots[i].proto < state->min_protocol) {
4088 continue;
4091 if (smb2cli_prots[i].proto > state->max_protocol) {
4092 continue;
4095 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4096 continue;
4099 conn->protocol = smb2cli_prots[i].proto;
4100 break;
4103 if (conn->protocol == PROTOCOL_NONE) {
4104 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4105 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4106 return;
4109 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4110 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4111 return;
4114 /* make sure we do not loop forever */
4115 state->min_protocol = PROTOCOL_SMB2_02;
4118 * send a SMB2 negprot, in order to negotiate
4119 * the SMB2 dialect.
4121 subreq = smbXcli_negprot_smb2_subreq(state);
4122 if (tevent_req_nomem(subreq, req)) {
4123 return;
4125 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4126 return;
4129 conn->smb2.server.security_mode = SVAL(body, 2);
4131 blob = data_blob_const(body + 8, 16);
4132 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4133 if (tevent_req_nterror(req, status)) {
4134 return;
4137 conn->smb2.server.capabilities = IVAL(body, 24);
4138 conn->smb2.server.max_trans_size= IVAL(body, 28);
4139 conn->smb2.server.max_read_size = IVAL(body, 32);
4140 conn->smb2.server.max_write_size= IVAL(body, 36);
4141 conn->smb2.server.system_time = BVAL(body, 40);
4142 conn->smb2.server.start_time = BVAL(body, 48);
4144 security_offset = SVAL(body, 56);
4145 security_length = SVAL(body, 58);
4147 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4148 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4149 return;
4152 if (security_length > iov[2].iov_len) {
4153 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4154 return;
4157 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4158 iov[2].iov_base,
4159 security_length);
4160 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4161 return;
4164 tevent_req_done(req);
4167 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4168 TALLOC_CTX *tmp_mem,
4169 uint8_t *inbuf)
4171 size_t num_pending = talloc_array_length(conn->pending);
4172 struct tevent_req *subreq;
4173 struct smbXcli_req_state *substate;
4174 struct tevent_req *req;
4175 uint32_t protocol_magic;
4176 size_t inbuf_len = smb_len_nbt(inbuf);
4178 if (num_pending != 1) {
4179 return NT_STATUS_INTERNAL_ERROR;
4182 if (inbuf_len < 4) {
4183 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4186 subreq = conn->pending[0];
4187 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4188 req = tevent_req_callback_data(subreq, struct tevent_req);
4190 protocol_magic = IVAL(inbuf, 4);
4192 switch (protocol_magic) {
4193 case SMB_MAGIC:
4194 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4195 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4196 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4198 case SMB2_MAGIC:
4199 if (substate->smb2.recv_iov == NULL) {
4201 * For the SMB1 negprot we have move it.
4203 substate->smb2.recv_iov = substate->smb1.recv_iov;
4204 substate->smb1.recv_iov = NULL;
4208 * we got an SMB2 answer, which consumed sequence number 0
4209 * so we need to use 1 as the next one
4211 conn->smb2.mid = 1;
4212 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4213 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4214 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4217 DEBUG(10, ("Got non-SMB PDU\n"));
4218 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4221 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4223 return tevent_req_simple_recv_ntstatus(req);
4226 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4227 uint32_t timeout_msec,
4228 enum protocol_types min_protocol,
4229 enum protocol_types max_protocol)
4231 TALLOC_CTX *frame = talloc_stackframe();
4232 struct tevent_context *ev;
4233 struct tevent_req *req;
4234 NTSTATUS status = NT_STATUS_NO_MEMORY;
4235 bool ok;
4237 if (smbXcli_conn_has_async_calls(conn)) {
4239 * Can't use sync call while an async call is in flight
4241 status = NT_STATUS_INVALID_PARAMETER_MIX;
4242 goto fail;
4244 ev = tevent_context_init(frame);
4245 if (ev == NULL) {
4246 goto fail;
4248 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4249 min_protocol, max_protocol);
4250 if (req == NULL) {
4251 goto fail;
4253 ok = tevent_req_poll(req, ev);
4254 if (!ok) {
4255 status = map_nt_error_from_unix_common(errno);
4256 goto fail;
4258 status = smbXcli_negprot_recv(req);
4259 fail:
4260 TALLOC_FREE(frame);
4261 return status;
4264 static int smbXcli_session_destructor(struct smbXcli_session *session)
4266 if (session->conn == NULL) {
4267 return 0;
4270 DLIST_REMOVE(session->conn->sessions, session);
4271 return 0;
4274 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4275 struct smbXcli_conn *conn)
4277 struct smbXcli_session *session;
4279 session = talloc_zero(mem_ctx, struct smbXcli_session);
4280 if (session == NULL) {
4281 return NULL;
4283 session->smb2 = talloc_zero(session, struct smb2cli_session);
4284 if (session->smb2 == NULL) {
4285 talloc_free(session);
4286 return NULL;
4288 talloc_set_destructor(session, smbXcli_session_destructor);
4290 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4291 session->conn = conn;
4293 return session;
4296 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4298 return session->smb1.session_id;
4301 void smb1cli_session_set_id(struct smbXcli_session *session,
4302 uint16_t session_id)
4304 session->smb1.session_id = session_id;
4307 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4309 struct smbXcli_conn *conn = session->conn;
4310 uint8_t security_mode = 0;
4312 if (conn == NULL) {
4313 return security_mode;
4316 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4317 if (conn->mandatory_signing) {
4318 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4321 return security_mode;
4324 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4326 return session->smb2->session_id;
4329 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4331 return session->smb2->session_flags;
4334 NTSTATUS smb2cli_session_application_key(struct smbXcli_session *session,
4335 TALLOC_CTX *mem_ctx,
4336 DATA_BLOB *key)
4338 *key = data_blob_null;
4340 if (session->smb2->application_key.length == 0) {
4341 return NT_STATUS_NO_USER_SESSION_KEY;
4344 *key = data_blob_dup_talloc(mem_ctx, session->smb2->application_key);
4345 if (key->data == NULL) {
4346 return NT_STATUS_NO_MEMORY;
4349 return NT_STATUS_OK;
4352 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4353 uint64_t session_id,
4354 uint16_t session_flags)
4356 session->smb2->session_id = session_id;
4357 session->smb2->session_flags = session_flags;
4360 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4362 session->smb2->channel_sequence += 1;
4365 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4366 const DATA_BLOB _session_key,
4367 const struct iovec *recv_iov)
4369 struct smbXcli_conn *conn = session->conn;
4370 uint16_t no_sign_flags;
4371 uint8_t session_key[16];
4372 NTSTATUS status;
4374 if (conn == NULL) {
4375 return NT_STATUS_INVALID_PARAMETER_MIX;
4378 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4380 if (session->smb2->session_flags & no_sign_flags) {
4381 session->smb2->should_sign = false;
4382 return NT_STATUS_OK;
4385 if (session->smb2->signing_key.length != 0) {
4386 return NT_STATUS_INVALID_PARAMETER_MIX;
4389 ZERO_STRUCT(session_key);
4390 memcpy(session_key, _session_key.data,
4391 MIN(_session_key.length, sizeof(session_key)));
4393 session->smb2->signing_key = data_blob_talloc(session,
4394 session_key,
4395 sizeof(session_key));
4396 if (session->smb2->signing_key.data == NULL) {
4397 ZERO_STRUCT(session_key);
4398 return NT_STATUS_NO_MEMORY;
4401 if (conn->protocol >= PROTOCOL_SMB2_24) {
4402 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4403 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4405 smb2_key_derivation(session_key, sizeof(session_key),
4406 label.data, label.length,
4407 context.data, context.length,
4408 session->smb2->signing_key.data);
4411 session->smb2->encryption_key = data_blob_dup_talloc(session,
4412 session->smb2->signing_key);
4413 if (session->smb2->encryption_key.data == NULL) {
4414 ZERO_STRUCT(session_key);
4415 return NT_STATUS_NO_MEMORY;
4418 if (conn->protocol >= PROTOCOL_SMB2_24) {
4419 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4420 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4422 smb2_key_derivation(session_key, sizeof(session_key),
4423 label.data, label.length,
4424 context.data, context.length,
4425 session->smb2->encryption_key.data);
4428 session->smb2->decryption_key = data_blob_dup_talloc(session,
4429 session->smb2->signing_key);
4430 if (session->smb2->decryption_key.data == NULL) {
4431 ZERO_STRUCT(session_key);
4432 return NT_STATUS_NO_MEMORY;
4435 if (conn->protocol >= PROTOCOL_SMB2_24) {
4436 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4437 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4439 smb2_key_derivation(session_key, sizeof(session_key),
4440 label.data, label.length,
4441 context.data, context.length,
4442 session->smb2->decryption_key.data);
4445 session->smb2->application_key = data_blob_dup_talloc(session,
4446 session->smb2->signing_key);
4447 if (session->smb2->application_key.data == NULL) {
4448 ZERO_STRUCT(session_key);
4449 return NT_STATUS_NO_MEMORY;
4452 if (conn->protocol >= PROTOCOL_SMB2_24) {
4453 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4454 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4456 smb2_key_derivation(session_key, sizeof(session_key),
4457 label.data, label.length,
4458 context.data, context.length,
4459 session->smb2->application_key.data);
4461 ZERO_STRUCT(session_key);
4463 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4464 session->smb2->signing_key);
4465 if (session->smb2_channel.signing_key.data == NULL) {
4466 return NT_STATUS_NO_MEMORY;
4469 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4470 session->conn->protocol,
4471 recv_iov, 3);
4472 if (!NT_STATUS_IS_OK(status)) {
4473 return status;
4476 session->smb2->should_sign = false;
4477 session->smb2->should_encrypt = false;
4479 if (conn->desire_signing) {
4480 session->smb2->should_sign = true;
4483 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4484 session->smb2->should_sign = true;
4487 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4488 session->smb2->should_encrypt = true;
4491 if (conn->protocol < PROTOCOL_SMB2_24) {
4492 session->smb2->should_encrypt = false;
4495 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4496 session->smb2->should_encrypt = false;
4499 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4500 sizeof(session->smb2->nonce_high));
4501 session->smb2->nonce_low = 1;
4503 return NT_STATUS_OK;
4506 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4507 struct smbXcli_session *session1,
4508 struct smbXcli_conn *conn,
4509 struct smbXcli_session **_session2)
4511 struct smbXcli_session *session2;
4513 if (session1->smb2->signing_key.length == 0) {
4514 return NT_STATUS_INVALID_PARAMETER_MIX;
4517 if (conn == NULL) {
4518 return NT_STATUS_INVALID_PARAMETER_MIX;
4521 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4522 if (session2 == NULL) {
4523 return NT_STATUS_NO_MEMORY;
4525 session2->smb2 = talloc_reference(session2, session1->smb2);
4526 if (session2->smb2 == NULL) {
4527 talloc_free(session2);
4528 return NT_STATUS_NO_MEMORY;
4531 talloc_set_destructor(session2, smbXcli_session_destructor);
4532 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4533 session2->conn = conn;
4535 *_session2 = session2;
4536 return NT_STATUS_OK;
4539 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4540 const DATA_BLOB _channel_key,
4541 const struct iovec *recv_iov)
4543 struct smbXcli_conn *conn = session->conn;
4544 uint8_t channel_key[16];
4545 NTSTATUS status;
4547 if (conn == NULL) {
4548 return NT_STATUS_INVALID_PARAMETER_MIX;
4551 if (session->smb2_channel.signing_key.length != 0) {
4552 return NT_STATUS_INVALID_PARAMETER_MIX;
4555 ZERO_STRUCT(channel_key);
4556 memcpy(channel_key, _channel_key.data,
4557 MIN(_channel_key.length, sizeof(channel_key)));
4559 session->smb2_channel.signing_key = data_blob_talloc(session,
4560 channel_key,
4561 sizeof(channel_key));
4562 if (session->smb2_channel.signing_key.data == NULL) {
4563 ZERO_STRUCT(channel_key);
4564 return NT_STATUS_NO_MEMORY;
4567 if (conn->protocol >= PROTOCOL_SMB2_24) {
4568 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4569 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4571 smb2_key_derivation(channel_key, sizeof(channel_key),
4572 label.data, label.length,
4573 context.data, context.length,
4574 session->smb2_channel.signing_key.data);
4576 ZERO_STRUCT(channel_key);
4578 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4579 session->conn->protocol,
4580 recv_iov, 3);
4581 if (!NT_STATUS_IS_OK(status)) {
4582 return status;
4585 return NT_STATUS_OK;
4588 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4590 struct smbXcli_tcon *tcon;
4592 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4593 if (tcon == NULL) {
4594 return NULL;
4597 return tcon;
4600 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4602 return tcon->smb1.tcon_id;
4605 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4607 tcon->smb1.tcon_id = tcon_id;
4610 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
4611 uint16_t tcon_id,
4612 uint16_t optional_support,
4613 uint32_t maximal_access,
4614 uint32_t guest_maximal_access,
4615 const char *service,
4616 const char *fs_type)
4618 tcon->smb1.tcon_id = tcon_id;
4619 tcon->smb1.optional_support = optional_support;
4620 tcon->smb1.maximal_access = maximal_access;
4621 tcon->smb1.guest_maximal_access = guest_maximal_access;
4623 TALLOC_FREE(tcon->smb1.service);
4624 tcon->smb1.service = talloc_strdup(tcon, service);
4625 if (service != NULL && tcon->smb1.service == NULL) {
4626 return false;
4629 TALLOC_FREE(tcon->smb1.fs_type);
4630 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
4631 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
4632 return false;
4635 return true;
4638 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
4640 return tcon->smb2.tcon_id;
4643 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
4645 return tcon->smb2.capabilities;
4648 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
4649 struct smbXcli_session *session,
4650 uint32_t tcon_id,
4651 uint8_t type,
4652 uint32_t flags,
4653 uint32_t capabilities,
4654 uint32_t maximal_access)
4656 tcon->smb2.tcon_id = tcon_id;
4657 tcon->smb2.type = type;
4658 tcon->smb2.flags = flags;
4659 tcon->smb2.capabilities = capabilities;
4660 tcon->smb2.maximal_access = maximal_access;
4662 tcon->smb2.should_encrypt = false;
4664 if (session == NULL) {
4665 return;
4668 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
4670 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
4671 tcon->smb2.should_encrypt = true;