libcli: add smbXcli_req_endtime
[Samba.git] / libcli / smb / smbXcli_base.c
blobb7bf5796ba81470cd2756c55234e4d1fd960515c
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 "lib/util/iov_buf.h"
29 #include "../libcli/smb/smb_common.h"
30 #include "../libcli/smb/smb_seal.h"
31 #include "../libcli/smb/smb_signing.h"
32 #include "../libcli/smb/read_smb.h"
33 #include "smbXcli_base.h"
34 #include "librpc/ndr/libndr.h"
35 #include "libcli/smb/smb2_negotiate_context.h"
36 #include "lib/crypto/sha512.h"
37 #include "lib/crypto/aes.h"
38 #include "lib/crypto/aes_ccm_128.h"
39 #include "lib/crypto/aes_gcm_128.h"
41 struct smbXcli_conn;
42 struct smbXcli_req;
43 struct smbXcli_session;
44 struct smbXcli_tcon;
46 struct smbXcli_conn {
47 int sock_fd;
48 struct sockaddr_storage local_ss;
49 struct sockaddr_storage remote_ss;
50 const char *remote_name;
52 struct tevent_queue *outgoing;
53 struct tevent_req **pending;
54 struct tevent_req *read_smb_req;
55 struct tevent_req *suicide_req;
57 enum protocol_types min_protocol;
58 enum protocol_types max_protocol;
59 enum protocol_types protocol;
60 bool allow_signing;
61 bool desire_signing;
62 bool mandatory_signing;
65 * The incoming dispatch function should return:
66 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
67 * - NT_STATUS_OK, if no more processing is desired, e.g.
68 * the dispatch function called
69 * tevent_req_done().
70 * - All other return values disconnect the connection.
72 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
73 TALLOC_CTX *tmp_mem,
74 uint8_t *inbuf);
76 struct {
77 struct {
78 uint32_t capabilities;
79 uint32_t max_xmit;
80 } client;
82 struct {
83 uint32_t capabilities;
84 uint32_t max_xmit;
85 uint16_t max_mux;
86 uint16_t security_mode;
87 bool readbraw;
88 bool writebraw;
89 bool lockread;
90 bool writeunlock;
91 uint32_t session_key;
92 struct GUID guid;
93 DATA_BLOB gss_blob;
94 uint8_t challenge[8];
95 const char *workgroup;
96 const char *name;
97 int time_zone;
98 NTTIME system_time;
99 } server;
101 uint32_t capabilities;
102 uint32_t max_xmit;
104 uint16_t mid;
106 struct smb_signing_state *signing;
107 struct smb_trans_enc_state *trans_enc;
109 struct tevent_req *read_braw_req;
110 } smb1;
112 struct {
113 struct {
114 uint32_t capabilities;
115 uint16_t security_mode;
116 struct GUID guid;
117 } client;
119 struct {
120 uint32_t capabilities;
121 uint16_t security_mode;
122 struct GUID guid;
123 uint32_t max_trans_size;
124 uint32_t max_read_size;
125 uint32_t max_write_size;
126 NTTIME system_time;
127 NTTIME start_time;
128 DATA_BLOB gss_blob;
129 uint16_t cipher;
130 } server;
132 uint64_t mid;
133 uint16_t cur_credits;
134 uint16_t max_credits;
136 uint32_t cc_chunk_len;
137 uint32_t cc_max_chunks;
139 uint8_t io_priority;
141 bool force_channel_sequence;
143 uint8_t preauth_sha512[64];
144 } smb2;
146 struct smbXcli_session *sessions;
149 struct smb2cli_session {
150 uint64_t session_id;
151 uint16_t session_flags;
152 DATA_BLOB application_key;
153 DATA_BLOB signing_key;
154 bool should_sign;
155 bool should_encrypt;
156 DATA_BLOB encryption_key;
157 DATA_BLOB decryption_key;
158 uint64_t nonce_high_random;
159 uint64_t nonce_high_max;
160 uint64_t nonce_high;
161 uint64_t nonce_low;
162 uint16_t channel_sequence;
163 bool replay_active;
166 struct smbXcli_session {
167 struct smbXcli_session *prev, *next;
168 struct smbXcli_conn *conn;
170 struct {
171 uint16_t session_id;
172 uint16_t action;
173 DATA_BLOB application_key;
174 bool protected_key;
175 } smb1;
177 struct smb2cli_session *smb2;
179 struct {
180 DATA_BLOB signing_key;
181 uint8_t preauth_sha512[64];
182 } smb2_channel;
185 * this should be a short term hack
186 * until the upper layers have implemented
187 * re-authentication.
189 bool disconnect_expired;
192 struct smbXcli_tcon {
193 bool is_smb1;
194 uint32_t fs_attributes;
196 struct {
197 uint16_t tcon_id;
198 uint16_t optional_support;
199 uint32_t maximal_access;
200 uint32_t guest_maximal_access;
201 char *service;
202 char *fs_type;
203 } smb1;
205 struct {
206 uint32_t tcon_id;
207 uint8_t type;
208 uint32_t flags;
209 uint32_t capabilities;
210 uint32_t maximal_access;
211 bool should_sign;
212 bool should_encrypt;
213 } smb2;
216 struct smbXcli_req_state {
217 struct tevent_context *ev;
218 struct smbXcli_conn *conn;
219 struct smbXcli_session *session; /* maybe NULL */
220 struct smbXcli_tcon *tcon; /* maybe NULL */
222 uint8_t length_hdr[4];
224 bool one_way;
226 uint8_t *inbuf;
228 struct tevent_req *write_req;
230 struct timeval endtime;
232 struct {
233 /* Space for the header including the wct */
234 uint8_t hdr[HDR_VWV];
237 * For normal requests, smb1cli_req_send chooses a mid.
238 * SecondaryV trans requests need to use the mid of the primary
239 * request, so we need a place to store it.
240 * Assume it is set if != 0.
242 uint16_t mid;
244 uint16_t *vwv;
245 uint8_t bytecount_buf[2];
247 #define MAX_SMB_IOV 10
248 /* length_hdr, hdr, words, byte_count, buffers */
249 struct iovec iov[1 + 3 + MAX_SMB_IOV];
250 int iov_count;
252 bool one_way_seqnum;
253 uint32_t seqnum;
254 struct tevent_req **chained_requests;
256 uint8_t recv_cmd;
257 NTSTATUS recv_status;
258 /* always an array of 3 talloc elements */
259 struct iovec *recv_iov;
260 } smb1;
262 struct {
263 const uint8_t *fixed;
264 uint16_t fixed_len;
265 const uint8_t *dyn;
266 uint32_t dyn_len;
268 uint8_t transform[SMB2_TF_HDR_SIZE];
269 uint8_t hdr[SMB2_HDR_BODY];
270 uint8_t pad[7]; /* padding space for compounding */
273 * always an array of 3 talloc elements
274 * (without a SMB2_TRANSFORM header!)
276 * HDR, BODY, DYN
278 struct iovec *recv_iov;
281 * the expected max for the response dyn_len
283 uint32_t max_dyn_len;
285 uint16_t credit_charge;
287 bool should_sign;
288 bool should_encrypt;
289 uint64_t encryption_session_id;
291 bool signing_skipped;
292 bool notify_async;
293 bool got_async;
294 uint16_t cancel_flags;
295 uint64_t cancel_mid;
296 uint64_t cancel_aid;
297 } smb2;
300 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
303 * NT_STATUS_OK, means we do not notify the callers
305 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
307 while (conn->sessions) {
308 conn->sessions->conn = NULL;
309 DLIST_REMOVE(conn->sessions, conn->sessions);
312 if (conn->smb1.trans_enc) {
313 TALLOC_FREE(conn->smb1.trans_enc);
316 return 0;
319 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
320 int fd,
321 const char *remote_name,
322 enum smb_signing_setting signing_state,
323 uint32_t smb1_capabilities,
324 struct GUID *client_guid,
325 uint32_t smb2_capabilities)
327 struct smbXcli_conn *conn = NULL;
328 void *ss = NULL;
329 struct sockaddr *sa = NULL;
330 socklen_t sa_length;
331 int ret;
333 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
334 if (!conn) {
335 return NULL;
338 conn->sock_fd = fd;
340 conn->remote_name = talloc_strdup(conn, remote_name);
341 if (conn->remote_name == NULL) {
342 goto error;
345 ss = (void *)&conn->local_ss;
346 sa = (struct sockaddr *)ss;
347 sa_length = sizeof(conn->local_ss);
348 ret = getsockname(fd, sa, &sa_length);
349 if (ret == -1) {
350 goto error;
352 ss = (void *)&conn->remote_ss;
353 sa = (struct sockaddr *)ss;
354 sa_length = sizeof(conn->remote_ss);
355 ret = getpeername(fd, sa, &sa_length);
356 if (ret == -1) {
357 goto error;
360 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
361 if (conn->outgoing == NULL) {
362 goto error;
364 conn->pending = NULL;
366 conn->min_protocol = PROTOCOL_NONE;
367 conn->max_protocol = PROTOCOL_NONE;
368 conn->protocol = PROTOCOL_NONE;
370 switch (signing_state) {
371 case SMB_SIGNING_OFF:
372 /* never */
373 conn->allow_signing = false;
374 conn->desire_signing = false;
375 conn->mandatory_signing = false;
376 break;
377 case SMB_SIGNING_DEFAULT:
378 case SMB_SIGNING_IF_REQUIRED:
379 /* if the server requires it */
380 conn->allow_signing = true;
381 conn->desire_signing = false;
382 conn->mandatory_signing = false;
383 break;
384 case SMB_SIGNING_DESIRED:
385 /* if the server desires it */
386 conn->allow_signing = true;
387 conn->desire_signing = true;
388 conn->mandatory_signing = false;
389 break;
390 case SMB_SIGNING_IPC_DEFAULT:
391 case SMB_SIGNING_REQUIRED:
392 /* always */
393 conn->allow_signing = true;
394 conn->desire_signing = true;
395 conn->mandatory_signing = true;
396 break;
399 conn->smb1.client.capabilities = smb1_capabilities;
400 conn->smb1.client.max_xmit = UINT16_MAX;
402 conn->smb1.capabilities = conn->smb1.client.capabilities;
403 conn->smb1.max_xmit = 1024;
405 conn->smb1.mid = 1;
407 /* initialise signing */
408 conn->smb1.signing = smb_signing_init(conn,
409 conn->allow_signing,
410 conn->desire_signing,
411 conn->mandatory_signing);
412 if (!conn->smb1.signing) {
413 goto error;
416 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
417 if (conn->mandatory_signing) {
418 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
420 if (client_guid) {
421 conn->smb2.client.guid = *client_guid;
423 conn->smb2.client.capabilities = smb2_capabilities;
425 conn->smb2.cur_credits = 1;
426 conn->smb2.max_credits = 0;
427 conn->smb2.io_priority = 1;
430 * Samba and Windows servers accept a maximum of 16 MiB with a maximum
431 * chunk length of 1 MiB.
433 conn->smb2.cc_chunk_len = 1024 * 1024;
434 conn->smb2.cc_max_chunks = 16;
436 talloc_set_destructor(conn, smbXcli_conn_destructor);
437 return conn;
439 error:
440 TALLOC_FREE(conn);
441 return NULL;
444 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
446 if (conn == NULL) {
447 return false;
450 if (conn->sock_fd == -1) {
451 return false;
454 return true;
457 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
459 return conn->protocol;
462 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
464 if (conn->protocol >= PROTOCOL_SMB2_02) {
465 return true;
468 if (conn->smb1.capabilities & CAP_UNICODE) {
469 return true;
472 return false;
475 bool smbXcli_conn_signing_mandatory(struct smbXcli_conn *conn)
477 return conn->mandatory_signing;
481 * [MS-SMB] 2.2.2.3.5 - SMB1 support for passing through
482 * query/set commands to the file system
484 bool smbXcli_conn_support_passthrough(struct smbXcli_conn *conn)
486 if (conn->protocol >= PROTOCOL_SMB2_02) {
487 return true;
490 if (conn->smb1.capabilities & CAP_W2K_SMBS) {
491 return true;
494 return false;
497 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
499 set_socket_options(conn->sock_fd, options);
502 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
504 return &conn->local_ss;
507 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
509 return &conn->remote_ss;
512 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
514 return conn->remote_name;
517 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
519 if (conn->protocol >= PROTOCOL_SMB2_02) {
521 * TODO...
523 return 1;
526 return conn->smb1.server.max_mux;
529 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
531 if (conn->protocol >= PROTOCOL_SMB2_02) {
532 return conn->smb2.server.system_time;
535 return conn->smb1.server.system_time;
538 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
540 if (conn->protocol >= PROTOCOL_SMB2_02) {
541 return &conn->smb2.server.gss_blob;
544 return &conn->smb1.server.gss_blob;
547 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
549 if (conn->protocol >= PROTOCOL_SMB2_02) {
550 return &conn->smb2.server.guid;
553 return &conn->smb1.server.guid;
556 bool smbXcli_conn_get_force_channel_sequence(struct smbXcli_conn *conn)
558 return conn->smb2.force_channel_sequence;
561 void smbXcli_conn_set_force_channel_sequence(struct smbXcli_conn *conn,
562 bool v)
564 conn->smb2.force_channel_sequence = v;
567 struct smbXcli_conn_samba_suicide_state {
568 struct smbXcli_conn *conn;
569 struct iovec iov;
570 uint8_t buf[9];
571 struct tevent_req *write_req;
574 static void smbXcli_conn_samba_suicide_cleanup(struct tevent_req *req,
575 enum tevent_req_state req_state);
576 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
578 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
579 struct tevent_context *ev,
580 struct smbXcli_conn *conn,
581 uint8_t exitcode)
583 struct tevent_req *req, *subreq;
584 struct smbXcli_conn_samba_suicide_state *state;
586 req = tevent_req_create(mem_ctx, &state,
587 struct smbXcli_conn_samba_suicide_state);
588 if (req == NULL) {
589 return NULL;
591 state->conn = conn;
592 SIVAL(state->buf, 4, 0x74697865);
593 SCVAL(state->buf, 8, exitcode);
594 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
596 if (conn->suicide_req != NULL) {
597 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
598 return tevent_req_post(req, ev);
601 state->iov.iov_base = state->buf;
602 state->iov.iov_len = sizeof(state->buf);
604 subreq = writev_send(state, ev, conn->outgoing, conn->sock_fd,
605 false, &state->iov, 1);
606 if (tevent_req_nomem(subreq, req)) {
607 return tevent_req_post(req, ev);
609 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
610 state->write_req = subreq;
612 tevent_req_set_cleanup_fn(req, smbXcli_conn_samba_suicide_cleanup);
615 * We need to use tevent_req_defer_callback()
616 * in order to allow smbXcli_conn_disconnect()
617 * to do a safe cleanup.
619 tevent_req_defer_callback(req, ev);
620 conn->suicide_req = req;
622 return req;
625 static void smbXcli_conn_samba_suicide_cleanup(struct tevent_req *req,
626 enum tevent_req_state req_state)
628 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
629 req, struct smbXcli_conn_samba_suicide_state);
631 TALLOC_FREE(state->write_req);
633 if (state->conn == NULL) {
634 return;
637 if (state->conn->suicide_req == req) {
638 state->conn->suicide_req = NULL;
640 state->conn = NULL;
643 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
645 struct tevent_req *req = tevent_req_callback_data(
646 subreq, struct tevent_req);
647 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
648 req, struct smbXcli_conn_samba_suicide_state);
649 ssize_t nwritten;
650 int err;
652 state->write_req = NULL;
654 nwritten = writev_recv(subreq, &err);
655 TALLOC_FREE(subreq);
656 if (nwritten == -1) {
657 /* here, we need to notify all pending requests */
658 NTSTATUS status = map_nt_error_from_unix_common(err);
659 smbXcli_conn_disconnect(state->conn, status);
660 return;
662 tevent_req_done(req);
665 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
667 return tevent_req_simple_recv_ntstatus(req);
670 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
671 uint8_t exitcode)
673 TALLOC_CTX *frame = talloc_stackframe();
674 struct tevent_context *ev;
675 struct tevent_req *req;
676 NTSTATUS status = NT_STATUS_NO_MEMORY;
677 bool ok;
679 if (smbXcli_conn_has_async_calls(conn)) {
681 * Can't use sync call while an async call is in flight
683 status = NT_STATUS_INVALID_PARAMETER_MIX;
684 goto fail;
686 ev = samba_tevent_context_init(frame);
687 if (ev == NULL) {
688 goto fail;
690 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
691 if (req == NULL) {
692 goto fail;
694 ok = tevent_req_poll_ntstatus(req, ev, &status);
695 if (!ok) {
696 goto fail;
698 status = smbXcli_conn_samba_suicide_recv(req);
699 fail:
700 TALLOC_FREE(frame);
701 return status;
704 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
706 return conn->smb1.capabilities;
709 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
711 return conn->smb1.max_xmit;
714 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
716 size_t pending = talloc_array_length(conn->pending);
717 uint16_t possible = conn->smb1.server.max_mux;
719 if (pending >= possible) {
720 return false;
723 return true;
726 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
728 return conn->smb1.server.session_key;
731 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
733 return conn->smb1.server.challenge;
736 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
738 return conn->smb1.server.security_mode;
741 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
743 return conn->smb1.server.readbraw;
746 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
748 return conn->smb1.server.writebraw;
751 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
753 return conn->smb1.server.lockread;
756 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
758 return conn->smb1.server.writeunlock;
761 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
763 return conn->smb1.server.time_zone;
766 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
767 const DATA_BLOB user_session_key,
768 const DATA_BLOB response)
770 return smb_signing_activate(conn->smb1.signing,
771 user_session_key,
772 response);
775 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
776 const uint8_t *buf, uint32_t seqnum)
778 const uint8_t *hdr = buf + NBT_HDR_SIZE;
779 size_t len = smb_len_nbt(buf);
781 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
784 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
786 return smb_signing_is_active(conn->smb1.signing);
789 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
790 struct smb_trans_enc_state *es)
792 /* Replace the old state, if any. */
793 if (conn->smb1.trans_enc) {
794 TALLOC_FREE(conn->smb1.trans_enc);
796 conn->smb1.trans_enc = es;
799 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
801 return common_encryption_on(conn->smb1.trans_enc);
805 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
807 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
808 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
810 if (NT_STATUS_IS_OK(status)) {
811 return NT_STATUS_OK;
814 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
815 return status;
818 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
822 * Is the SMB command able to hold an AND_X successor
823 * @param[in] cmd The SMB command in question
824 * @retval Can we add a chained request after "cmd"?
826 bool smb1cli_is_andx_req(uint8_t cmd)
828 switch (cmd) {
829 case SMBtconX:
830 case SMBlockingX:
831 case SMBopenX:
832 case SMBreadX:
833 case SMBwriteX:
834 case SMBsesssetupX:
835 case SMBulogoffX:
836 case SMBntcreateX:
837 return true;
838 break;
839 default:
840 break;
843 return false;
846 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
848 size_t num_pending = talloc_array_length(conn->pending);
849 uint16_t result;
851 if (conn->protocol == PROTOCOL_NONE) {
853 * This is what windows sends on the SMB1 Negprot request
854 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
856 return 0;
859 while (true) {
860 size_t i;
862 result = conn->smb1.mid++;
863 if ((result == 0) || (result == 0xffff)) {
864 continue;
867 for (i=0; i<num_pending; i++) {
868 if (result == smb1cli_req_mid(conn->pending[i])) {
869 break;
873 if (i == num_pending) {
874 return result;
879 static NTSTATUS smbXcli_req_cancel_write_req(struct tevent_req *req)
881 struct smbXcli_req_state *state =
882 tevent_req_data(req,
883 struct smbXcli_req_state);
884 struct smbXcli_conn *conn = state->conn;
885 size_t num_pending = talloc_array_length(conn->pending);
886 ssize_t ret;
887 int err;
888 bool ok;
890 if (state->write_req == NULL) {
891 return NT_STATUS_OK;
895 * Check if it's possible to cancel the request.
896 * If the result is true it's not too late.
897 * See writev_cancel().
899 ok = tevent_req_cancel(state->write_req);
900 if (ok) {
901 TALLOC_FREE(state->write_req);
903 if (conn->protocol >= PROTOCOL_SMB2_02) {
905 * SMB2 has a sane signing state.
907 return NT_STATUS_OK;
910 if (num_pending > 1) {
912 * We have more pending requests following us. This
913 * means the signing state will be broken for them.
915 * As a solution we could add the requests directly to
916 * our outgoing queue and do the signing in the trigger
917 * function and then use writev_send() without passing a
918 * queue. That way we'll only sign packets we're most
919 * likely send to the wire.
921 return NT_STATUS_REQUEST_OUT_OF_SEQUENCE;
925 * If we're the only request that's
926 * pending, we're able to recover the signing
927 * state.
929 smb_signing_cancel_reply(conn->smb1.signing,
930 state->smb1.one_way_seqnum);
931 return NT_STATUS_OK;
934 ret = writev_recv(state->write_req, &err);
935 TALLOC_FREE(state->write_req);
936 if (ret == -1) {
937 return map_nt_error_from_unix_common(err);
940 return NT_STATUS_OK;
943 void smbXcli_req_unset_pending(struct tevent_req *req)
945 struct smbXcli_req_state *state =
946 tevent_req_data(req,
947 struct smbXcli_req_state);
948 struct smbXcli_conn *conn = state->conn;
949 size_t num_pending = talloc_array_length(conn->pending);
950 size_t i;
951 NTSTATUS cancel_status;
953 cancel_status = smbXcli_req_cancel_write_req(req);
955 if (state->smb1.mid != 0) {
957 * This is a [nt]trans[2] request which waits
958 * for more than one reply.
960 if (!NT_STATUS_IS_OK(cancel_status)) {
962 * If the write_req cancel didn't work
963 * we can't use the connection anymore.
965 smbXcli_conn_disconnect(conn, cancel_status);
966 return;
968 return;
971 tevent_req_set_cleanup_fn(req, NULL);
973 if (num_pending == 1) {
975 * The pending read_smb tevent_req is a child of
976 * conn->pending. So if nothing is pending anymore, we need to
977 * delete the socket read fde.
979 /* TODO: smbXcli_conn_cancel_read_req */
980 TALLOC_FREE(conn->pending);
981 conn->read_smb_req = NULL;
983 if (!NT_STATUS_IS_OK(cancel_status)) {
985 * If the write_req cancel didn't work
986 * we can't use the connection anymore.
988 smbXcli_conn_disconnect(conn, cancel_status);
989 return;
991 return;
994 for (i=0; i<num_pending; i++) {
995 if (req == conn->pending[i]) {
996 break;
999 if (i == num_pending) {
1001 * Something's seriously broken. Just returning here is the
1002 * right thing nevertheless, the point of this routine is to
1003 * remove ourselves from conn->pending.
1006 if (!NT_STATUS_IS_OK(cancel_status)) {
1008 * If the write_req cancel didn't work
1009 * we can't use the connection anymore.
1011 smbXcli_conn_disconnect(conn, cancel_status);
1012 return;
1014 return;
1018 * Remove ourselves from the conn->pending array
1020 for (; i < (num_pending - 1); i++) {
1021 conn->pending[i] = conn->pending[i+1];
1025 * No NULL check here, we're shrinking by sizeof(void *), and
1026 * talloc_realloc just adjusts the size for this.
1028 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
1029 num_pending - 1);
1031 if (!NT_STATUS_IS_OK(cancel_status)) {
1033 * If the write_req cancel didn't work
1034 * we can't use the connection anymore.
1036 smbXcli_conn_disconnect(conn, cancel_status);
1037 return;
1039 return;
1042 static void smbXcli_req_cleanup(struct tevent_req *req,
1043 enum tevent_req_state req_state)
1045 struct smbXcli_req_state *state =
1046 tevent_req_data(req,
1047 struct smbXcli_req_state);
1048 struct smbXcli_conn *conn = state->conn;
1049 NTSTATUS cancel_status;
1051 switch (req_state) {
1052 case TEVENT_REQ_RECEIVED:
1054 * Make sure we really remove it from
1055 * the pending array on destruction.
1057 * smbXcli_req_unset_pending() calls
1058 * smbXcli_req_cancel_write_req() internal
1060 state->smb1.mid = 0;
1061 smbXcli_req_unset_pending(req);
1062 return;
1063 default:
1064 cancel_status = smbXcli_req_cancel_write_req(req);
1065 if (!NT_STATUS_IS_OK(cancel_status)) {
1067 * If the write_req cancel didn't work
1068 * we can't use the connection anymore.
1070 smbXcli_conn_disconnect(conn, cancel_status);
1071 return;
1073 return;
1077 static bool smb1cli_req_cancel(struct tevent_req *req);
1078 static bool smb2cli_req_cancel(struct tevent_req *req);
1080 static bool smbXcli_req_cancel(struct tevent_req *req)
1082 struct smbXcli_req_state *state =
1083 tevent_req_data(req,
1084 struct smbXcli_req_state);
1086 if (!smbXcli_conn_is_connected(state->conn)) {
1087 return false;
1090 if (state->conn->protocol == PROTOCOL_NONE) {
1091 return false;
1094 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
1095 return smb2cli_req_cancel(req);
1098 return smb1cli_req_cancel(req);
1101 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
1103 bool smbXcli_req_set_pending(struct tevent_req *req)
1105 struct smbXcli_req_state *state =
1106 tevent_req_data(req,
1107 struct smbXcli_req_state);
1108 struct smbXcli_conn *conn;
1109 struct tevent_req **pending;
1110 size_t num_pending;
1112 conn = state->conn;
1114 if (!smbXcli_conn_is_connected(conn)) {
1115 return false;
1118 num_pending = talloc_array_length(conn->pending);
1120 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
1121 num_pending+1);
1122 if (pending == NULL) {
1123 return false;
1125 pending[num_pending] = req;
1126 conn->pending = pending;
1127 tevent_req_set_cleanup_fn(req, smbXcli_req_cleanup);
1128 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1130 if (!smbXcli_conn_receive_next(conn)) {
1132 * the caller should notify the current request
1134 * And all other pending requests get notified
1135 * by smbXcli_conn_disconnect().
1137 smbXcli_req_unset_pending(req);
1138 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1139 return false;
1142 return true;
1145 static void smbXcli_conn_received(struct tevent_req *subreq);
1147 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
1149 size_t num_pending = talloc_array_length(conn->pending);
1150 struct tevent_req *req;
1151 struct smbXcli_req_state *state;
1153 if (conn->read_smb_req != NULL) {
1154 return true;
1157 if (num_pending == 0) {
1158 if (conn->smb2.mid < UINT64_MAX) {
1159 /* no more pending requests, so we are done for now */
1160 return true;
1164 * If there are no more SMB2 requests possible,
1165 * because we are out of message ids,
1166 * we need to disconnect.
1168 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
1169 return true;
1172 req = conn->pending[0];
1173 state = tevent_req_data(req, struct smbXcli_req_state);
1176 * We're the first ones, add the read_smb request that waits for the
1177 * answer from the server
1179 conn->read_smb_req = read_smb_send(conn->pending,
1180 state->ev,
1181 conn->sock_fd);
1182 if (conn->read_smb_req == NULL) {
1183 return false;
1185 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
1186 return true;
1189 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
1191 struct smbXcli_session *session;
1192 int sock_fd = conn->sock_fd;
1194 tevent_queue_stop(conn->outgoing);
1196 conn->sock_fd = -1;
1198 session = conn->sessions;
1199 if (talloc_array_length(conn->pending) == 0) {
1201 * if we do not have pending requests
1202 * there is no need to update the channel_sequence
1204 session = NULL;
1206 for (; session; session = session->next) {
1207 smb2cli_session_increment_channel_sequence(session);
1210 if (conn->suicide_req != NULL) {
1212 * smbXcli_conn_samba_suicide_send()
1213 * used tevent_req_defer_callback() already.
1215 if (!NT_STATUS_IS_OK(status)) {
1216 tevent_req_nterror(conn->suicide_req, status);
1218 conn->suicide_req = NULL;
1222 * Cancel all pending requests. We do not do a for-loop walking
1223 * conn->pending because that array changes in
1224 * smbXcli_req_unset_pending.
1226 while (talloc_array_length(conn->pending) > 0) {
1227 struct tevent_req *req;
1228 struct smbXcli_req_state *state;
1229 struct tevent_req **chain;
1230 size_t num_chained;
1231 size_t i;
1233 req = conn->pending[0];
1234 state = tevent_req_data(req, struct smbXcli_req_state);
1236 if (state->smb1.chained_requests == NULL) {
1237 bool in_progress;
1240 * We're dead. No point waiting for trans2
1241 * replies.
1243 state->smb1.mid = 0;
1245 smbXcli_req_unset_pending(req);
1247 if (NT_STATUS_IS_OK(status)) {
1248 /* do not notify the callers */
1249 continue;
1252 in_progress = tevent_req_is_in_progress(req);
1253 if (!in_progress) {
1255 * already finished
1257 continue;
1261 * we need to defer the callback, because we may notify
1262 * more then one caller.
1264 tevent_req_defer_callback(req, state->ev);
1265 tevent_req_nterror(req, status);
1266 continue;
1269 chain = talloc_move(conn, &state->smb1.chained_requests);
1270 num_chained = talloc_array_length(chain);
1272 for (i=0; i<num_chained; i++) {
1273 bool in_progress;
1275 req = chain[i];
1276 state = tevent_req_data(req, struct smbXcli_req_state);
1279 * We're dead. No point waiting for trans2
1280 * replies.
1282 state->smb1.mid = 0;
1284 smbXcli_req_unset_pending(req);
1286 if (NT_STATUS_IS_OK(status)) {
1287 /* do not notify the callers */
1288 continue;
1291 in_progress = tevent_req_is_in_progress(req);
1292 if (!in_progress) {
1294 * already finished
1296 continue;
1300 * we need to defer the callback, because we may notify
1301 * more than one caller.
1303 tevent_req_defer_callback(req, state->ev);
1304 tevent_req_nterror(req, status);
1306 TALLOC_FREE(chain);
1309 if (sock_fd != -1) {
1310 close(sock_fd);
1315 * Fetch a smb request's mid. Only valid after the request has been sent by
1316 * smb1cli_req_send().
1318 uint16_t smb1cli_req_mid(struct tevent_req *req)
1320 struct smbXcli_req_state *state =
1321 tevent_req_data(req,
1322 struct smbXcli_req_state);
1324 if (state->smb1.mid != 0) {
1325 return state->smb1.mid;
1328 return SVAL(state->smb1.hdr, HDR_MID);
1331 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1333 struct smbXcli_req_state *state =
1334 tevent_req_data(req,
1335 struct smbXcli_req_state);
1337 state->smb1.mid = mid;
1340 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1342 struct smbXcli_req_state *state =
1343 tevent_req_data(req,
1344 struct smbXcli_req_state);
1346 return state->smb1.seqnum;
1349 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1351 struct smbXcli_req_state *state =
1352 tevent_req_data(req,
1353 struct smbXcli_req_state);
1355 state->smb1.seqnum = seqnum;
1358 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1360 ssize_t ret = iov_buflen(iov, count);
1362 /* Ignore the overflow case for now ... */
1363 return ret;
1366 static void smb1cli_req_flags(enum protocol_types protocol,
1367 uint32_t smb1_capabilities,
1368 uint8_t smb_command,
1369 uint8_t additional_flags,
1370 uint8_t clear_flags,
1371 uint8_t *_flags,
1372 uint16_t additional_flags2,
1373 uint16_t clear_flags2,
1374 uint16_t *_flags2)
1376 uint8_t flags = 0;
1377 uint16_t flags2 = 0;
1379 if (protocol >= PROTOCOL_LANMAN1) {
1380 flags |= FLAG_CASELESS_PATHNAMES;
1381 flags |= FLAG_CANONICAL_PATHNAMES;
1384 if (protocol >= PROTOCOL_LANMAN2) {
1385 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1386 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1389 if (protocol >= PROTOCOL_NT1) {
1390 flags2 |= FLAGS2_IS_LONG_NAME;
1392 if (smb1_capabilities & CAP_UNICODE) {
1393 flags2 |= FLAGS2_UNICODE_STRINGS;
1395 if (smb1_capabilities & CAP_STATUS32) {
1396 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1398 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1399 flags2 |= FLAGS2_EXTENDED_SECURITY;
1403 flags |= additional_flags;
1404 flags &= ~clear_flags;
1405 flags2 |= additional_flags2;
1406 flags2 &= ~clear_flags2;
1408 *_flags = flags;
1409 *_flags2 = flags2;
1412 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1414 static bool smb1cli_req_cancel(struct tevent_req *req)
1416 struct smbXcli_req_state *state =
1417 tevent_req_data(req,
1418 struct smbXcli_req_state);
1419 uint8_t flags;
1420 uint16_t flags2;
1421 uint32_t pid;
1422 uint16_t mid;
1423 struct tevent_req *subreq;
1424 NTSTATUS status;
1426 flags = CVAL(state->smb1.hdr, HDR_FLG);
1427 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1428 pid = SVAL(state->smb1.hdr, HDR_PID);
1429 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1430 mid = SVAL(state->smb1.hdr, HDR_MID);
1432 subreq = smb1cli_req_create(state, state->ev,
1433 state->conn,
1434 SMBntcancel,
1435 flags, 0,
1436 flags2, 0,
1437 0, /* timeout */
1438 pid,
1439 state->tcon,
1440 state->session,
1441 0, NULL, /* vwv */
1442 0, NULL); /* bytes */
1443 if (subreq == NULL) {
1444 return false;
1446 smb1cli_req_set_mid(subreq, mid);
1448 status = smb1cli_req_chain_submit(&subreq, 1);
1449 if (!NT_STATUS_IS_OK(status)) {
1450 TALLOC_FREE(subreq);
1451 return false;
1453 smb1cli_req_set_mid(subreq, 0);
1455 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1457 return true;
1460 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1462 /* we do not care about the result */
1463 TALLOC_FREE(subreq);
1466 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1467 struct tevent_context *ev,
1468 struct smbXcli_conn *conn,
1469 uint8_t smb_command,
1470 uint8_t additional_flags,
1471 uint8_t clear_flags,
1472 uint16_t additional_flags2,
1473 uint16_t clear_flags2,
1474 uint32_t timeout_msec,
1475 uint32_t pid,
1476 struct smbXcli_tcon *tcon,
1477 struct smbXcli_session *session,
1478 uint8_t wct, uint16_t *vwv,
1479 int iov_count,
1480 struct iovec *bytes_iov)
1482 struct tevent_req *req;
1483 struct smbXcli_req_state *state;
1484 uint8_t flags = 0;
1485 uint16_t flags2 = 0;
1486 uint16_t uid = 0;
1487 uint16_t tid = 0;
1488 ssize_t num_bytes;
1490 if (iov_count > MAX_SMB_IOV) {
1492 * Should not happen :-)
1494 return NULL;
1497 req = tevent_req_create(mem_ctx, &state,
1498 struct smbXcli_req_state);
1499 if (req == NULL) {
1500 return NULL;
1502 state->ev = ev;
1503 state->conn = conn;
1504 state->session = session;
1505 state->tcon = tcon;
1507 if (session) {
1508 uid = session->smb1.session_id;
1511 if (tcon) {
1512 tid = tcon->smb1.tcon_id;
1514 if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
1515 clear_flags |= FLAG_CASELESS_PATHNAMES;
1516 } else {
1517 /* Default setting, case insensitive. */
1518 additional_flags |= FLAG_CASELESS_PATHNAMES;
1521 if (smbXcli_conn_dfs_supported(conn) &&
1522 smbXcli_tcon_is_dfs_share(tcon))
1524 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
1528 state->smb1.recv_cmd = 0xFF;
1529 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1530 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1531 if (state->smb1.recv_iov == NULL) {
1532 TALLOC_FREE(req);
1533 return NULL;
1536 smb1cli_req_flags(conn->protocol,
1537 conn->smb1.capabilities,
1538 smb_command,
1539 additional_flags,
1540 clear_flags,
1541 &flags,
1542 additional_flags2,
1543 clear_flags2,
1544 &flags2);
1546 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1547 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1548 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1549 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1550 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1551 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1552 SSVAL(state->smb1.hdr, HDR_TID, tid);
1553 SSVAL(state->smb1.hdr, HDR_PID, pid);
1554 SSVAL(state->smb1.hdr, HDR_UID, uid);
1555 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1556 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1558 state->smb1.vwv = vwv;
1560 num_bytes = iov_buflen(bytes_iov, iov_count);
1561 if (num_bytes == -1) {
1563 * I'd love to add a check for num_bytes<=UINT16_MAX here, but
1564 * the smbclient->samba connections can lie and transfer more.
1566 TALLOC_FREE(req);
1567 return NULL;
1570 SSVAL(state->smb1.bytecount_buf, 0, num_bytes);
1572 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1573 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1574 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1575 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1576 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1577 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1578 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1579 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1581 if (iov_count != 0) {
1582 memcpy(&state->smb1.iov[4], bytes_iov,
1583 iov_count * sizeof(*bytes_iov));
1585 state->smb1.iov_count = iov_count + 4;
1587 if (timeout_msec > 0) {
1588 struct timeval endtime;
1590 endtime = timeval_current_ofs_msec(timeout_msec);
1591 if (!tevent_req_set_endtime(req, ev, endtime)) {
1592 return req;
1596 switch (smb_command) {
1597 case SMBtranss:
1598 case SMBtranss2:
1599 case SMBnttranss:
1600 state->one_way = true;
1601 break;
1602 case SMBntcancel:
1603 state->one_way = true;
1604 state->smb1.one_way_seqnum = true;
1605 break;
1606 case SMBlockingX:
1607 if ((wct == 8) &&
1608 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1609 state->one_way = true;
1611 break;
1614 return req;
1617 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1618 struct iovec *iov, int iov_count,
1619 uint32_t *seqnum,
1620 bool one_way_seqnum)
1622 TALLOC_CTX *frame = NULL;
1623 uint8_t *buf;
1626 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1627 * iovec directly. MD5Update would do that just fine.
1630 if (iov_count < 4) {
1631 return NT_STATUS_INVALID_PARAMETER_MIX;
1633 if (iov[0].iov_len != NBT_HDR_SIZE) {
1634 return NT_STATUS_INVALID_PARAMETER_MIX;
1636 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1637 return NT_STATUS_INVALID_PARAMETER_MIX;
1639 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1640 return NT_STATUS_INVALID_PARAMETER_MIX;
1642 if (iov[3].iov_len != sizeof(uint16_t)) {
1643 return NT_STATUS_INVALID_PARAMETER_MIX;
1646 frame = talloc_stackframe();
1648 buf = iov_concat(frame, &iov[1], iov_count - 1);
1649 if (buf == NULL) {
1650 return NT_STATUS_NO_MEMORY;
1653 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1654 one_way_seqnum);
1655 smb_signing_sign_pdu(conn->smb1.signing,
1656 buf, talloc_get_size(buf),
1657 *seqnum);
1658 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1660 TALLOC_FREE(frame);
1661 return NT_STATUS_OK;
1664 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1665 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1666 TALLOC_CTX *tmp_mem,
1667 uint8_t *inbuf);
1669 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1670 struct smbXcli_req_state *state,
1671 struct iovec *iov, int iov_count)
1673 struct tevent_req *subreq;
1674 NTSTATUS status;
1675 uint8_t cmd;
1676 uint16_t mid;
1677 ssize_t nbtlen;
1679 if (!smbXcli_conn_is_connected(state->conn)) {
1680 return NT_STATUS_CONNECTION_DISCONNECTED;
1683 if (state->conn->protocol > PROTOCOL_NT1) {
1684 DBG_ERR("called for dialect[%s] server[%s]\n",
1685 smb_protocol_types_string(state->conn->protocol),
1686 smbXcli_conn_remote_name(state->conn));
1687 return NT_STATUS_REVISION_MISMATCH;
1690 if (iov_count < 4) {
1691 return NT_STATUS_INVALID_PARAMETER_MIX;
1693 if (iov[0].iov_len != NBT_HDR_SIZE) {
1694 return NT_STATUS_INVALID_PARAMETER_MIX;
1696 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1697 return NT_STATUS_INVALID_PARAMETER_MIX;
1699 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1700 return NT_STATUS_INVALID_PARAMETER_MIX;
1702 if (iov[3].iov_len != sizeof(uint16_t)) {
1703 return NT_STATUS_INVALID_PARAMETER_MIX;
1706 cmd = CVAL(iov[1].iov_base, HDR_COM);
1707 if (cmd == SMBreadBraw) {
1708 if (smbXcli_conn_has_async_calls(state->conn)) {
1709 return NT_STATUS_INVALID_PARAMETER_MIX;
1711 state->conn->smb1.read_braw_req = req;
1714 if (state->smb1.mid != 0) {
1715 mid = state->smb1.mid;
1716 } else {
1717 mid = smb1cli_alloc_mid(state->conn);
1719 SSVAL(iov[1].iov_base, HDR_MID, mid);
1721 nbtlen = iov_buflen(&iov[1], iov_count-1);
1722 if ((nbtlen == -1) || (nbtlen > 0x1FFFF)) {
1723 return NT_STATUS_INVALID_PARAMETER_MIX;
1726 _smb_setlen_nbt(iov[0].iov_base, nbtlen);
1728 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1729 &state->smb1.seqnum,
1730 state->smb1.one_way_seqnum);
1732 if (!NT_STATUS_IS_OK(status)) {
1733 return status;
1737 * If we supported multiple encrytion contexts
1738 * here we'd look up based on tid.
1740 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1741 char *buf, *enc_buf;
1743 buf = (char *)iov_concat(talloc_tos(), iov, iov_count);
1744 if (buf == NULL) {
1745 return NT_STATUS_NO_MEMORY;
1747 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1748 (char *)buf, &enc_buf);
1749 TALLOC_FREE(buf);
1750 if (!NT_STATUS_IS_OK(status)) {
1751 DEBUG(0, ("Error in encrypting client message: %s\n",
1752 nt_errstr(status)));
1753 return status;
1755 buf = (char *)talloc_memdup(state, enc_buf,
1756 smb_len_nbt(enc_buf)+4);
1757 SAFE_FREE(enc_buf);
1758 if (buf == NULL) {
1759 return NT_STATUS_NO_MEMORY;
1761 iov[0].iov_base = (void *)buf;
1762 iov[0].iov_len = talloc_get_size(buf);
1763 iov_count = 1;
1766 if (state->conn->dispatch_incoming == NULL) {
1767 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1770 if (!smbXcli_req_set_pending(req)) {
1771 return NT_STATUS_NO_MEMORY;
1774 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1776 subreq = writev_send(state, state->ev, state->conn->outgoing,
1777 state->conn->sock_fd, false, iov, iov_count);
1778 if (subreq == NULL) {
1779 return NT_STATUS_NO_MEMORY;
1781 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1782 state->write_req = subreq;
1784 return NT_STATUS_OK;
1787 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1788 struct tevent_context *ev,
1789 struct smbXcli_conn *conn,
1790 uint8_t smb_command,
1791 uint8_t additional_flags,
1792 uint8_t clear_flags,
1793 uint16_t additional_flags2,
1794 uint16_t clear_flags2,
1795 uint32_t timeout_msec,
1796 uint32_t pid,
1797 struct smbXcli_tcon *tcon,
1798 struct smbXcli_session *session,
1799 uint8_t wct, uint16_t *vwv,
1800 uint32_t num_bytes,
1801 const uint8_t *bytes)
1803 struct tevent_req *req;
1804 struct iovec iov;
1805 NTSTATUS status;
1807 iov.iov_base = discard_const_p(void, bytes);
1808 iov.iov_len = num_bytes;
1810 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1811 additional_flags, clear_flags,
1812 additional_flags2, clear_flags2,
1813 timeout_msec,
1814 pid, tcon, session,
1815 wct, vwv, 1, &iov);
1816 if (req == NULL) {
1817 return NULL;
1819 if (!tevent_req_is_in_progress(req)) {
1820 return tevent_req_post(req, ev);
1822 status = smb1cli_req_chain_submit(&req, 1);
1823 if (tevent_req_nterror(req, status)) {
1824 return tevent_req_post(req, ev);
1826 return req;
1829 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1831 struct tevent_req *req =
1832 tevent_req_callback_data(subreq,
1833 struct tevent_req);
1834 struct smbXcli_req_state *state =
1835 tevent_req_data(req,
1836 struct smbXcli_req_state);
1837 ssize_t nwritten;
1838 int err;
1840 state->write_req = NULL;
1842 nwritten = writev_recv(subreq, &err);
1843 TALLOC_FREE(subreq);
1844 if (nwritten == -1) {
1845 /* here, we need to notify all pending requests */
1846 NTSTATUS status = map_nt_error_from_unix_common(err);
1847 smbXcli_conn_disconnect(state->conn, status);
1848 return;
1851 if (state->one_way) {
1852 state->inbuf = NULL;
1853 tevent_req_done(req);
1854 return;
1858 static void smbXcli_conn_received(struct tevent_req *subreq)
1860 struct smbXcli_conn *conn =
1861 tevent_req_callback_data(subreq,
1862 struct smbXcli_conn);
1863 TALLOC_CTX *frame = talloc_stackframe();
1864 NTSTATUS status;
1865 uint8_t *inbuf;
1866 ssize_t received;
1867 int err;
1869 if (subreq != conn->read_smb_req) {
1870 DEBUG(1, ("Internal error: cli_smb_received called with "
1871 "unexpected subreq\n"));
1872 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1873 TALLOC_FREE(frame);
1874 return;
1876 conn->read_smb_req = NULL;
1878 received = read_smb_recv(subreq, frame, &inbuf, &err);
1879 TALLOC_FREE(subreq);
1880 if (received == -1) {
1881 status = map_nt_error_from_unix_common(err);
1882 smbXcli_conn_disconnect(conn, status);
1883 TALLOC_FREE(frame);
1884 return;
1887 status = conn->dispatch_incoming(conn, frame, inbuf);
1888 TALLOC_FREE(frame);
1889 if (NT_STATUS_IS_OK(status)) {
1891 * We should not do any more processing
1892 * as the dispatch function called
1893 * tevent_req_done().
1895 return;
1898 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1900 * We got an error, so notify all pending requests
1902 smbXcli_conn_disconnect(conn, status);
1903 return;
1907 * We got NT_STATUS_RETRY, so we may ask for a
1908 * next incoming pdu.
1910 if (!smbXcli_conn_receive_next(conn)) {
1911 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1915 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1916 struct iovec **piov, int *pnum_iov)
1918 struct iovec *iov;
1919 size_t num_iov;
1920 size_t buflen;
1921 size_t taken;
1922 size_t remaining;
1923 uint8_t *hdr;
1924 uint8_t cmd;
1925 uint32_t wct_ofs;
1926 NTSTATUS status;
1927 size_t min_size = MIN_SMB_SIZE;
1929 buflen = smb_len_tcp(buf);
1930 taken = 0;
1932 hdr = buf + NBT_HDR_SIZE;
1934 status = smb1cli_pull_raw_error(hdr);
1935 if (NT_STATUS_IS_ERR(status)) {
1937 * This is an ugly hack to support OS/2
1938 * which skips the byte_count in the DATA block
1939 * on some error responses.
1941 * See bug #9096
1943 min_size -= sizeof(uint16_t);
1946 if (buflen < min_size) {
1947 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1951 * This returns iovec elements in the following order:
1953 * - SMB header
1955 * - Parameter Block
1956 * - Data Block
1958 * - Parameter Block
1959 * - Data Block
1961 * - Parameter Block
1962 * - Data Block
1964 num_iov = 1;
1966 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1967 if (iov == NULL) {
1968 return NT_STATUS_NO_MEMORY;
1970 iov[0].iov_base = hdr;
1971 iov[0].iov_len = HDR_WCT;
1972 taken += HDR_WCT;
1974 cmd = CVAL(hdr, HDR_COM);
1975 wct_ofs = HDR_WCT;
1977 while (true) {
1978 size_t len = buflen - taken;
1979 struct iovec *cur;
1980 struct iovec *iov_tmp;
1981 uint8_t wct;
1982 uint32_t bcc_ofs;
1983 uint16_t bcc;
1984 size_t needed;
1987 * we need at least WCT
1989 needed = sizeof(uint8_t);
1990 if (len < needed) {
1991 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1992 __location__, (int)len, (int)needed));
1993 goto inval;
1997 * Now we check if the specified words are there
1999 wct = CVAL(hdr, wct_ofs);
2000 needed += wct * sizeof(uint16_t);
2001 if (len < needed) {
2002 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2003 __location__, (int)len, (int)needed));
2004 goto inval;
2007 if ((num_iov == 1) &&
2008 (len == needed) &&
2009 NT_STATUS_IS_ERR(status))
2012 * This is an ugly hack to support OS/2
2013 * which skips the byte_count in the DATA block
2014 * on some error responses.
2016 * See bug #9096
2018 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2019 num_iov + 2);
2020 if (iov_tmp == NULL) {
2021 TALLOC_FREE(iov);
2022 return NT_STATUS_NO_MEMORY;
2024 iov = iov_tmp;
2025 cur = &iov[num_iov];
2026 num_iov += 2;
2028 cur[0].iov_len = 0;
2029 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
2030 cur[1].iov_len = 0;
2031 cur[1].iov_base = cur[0].iov_base;
2033 taken += needed;
2034 break;
2038 * we need at least BCC
2040 needed += sizeof(uint16_t);
2041 if (len < needed) {
2042 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2043 __location__, (int)len, (int)needed));
2044 goto inval;
2048 * Now we check if the specified bytes are there
2050 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
2051 bcc = SVAL(hdr, bcc_ofs);
2052 needed += bcc * sizeof(uint8_t);
2053 if (len < needed) {
2054 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2055 __location__, (int)len, (int)needed));
2056 goto inval;
2060 * we allocate 2 iovec structures for words and bytes
2062 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2063 num_iov + 2);
2064 if (iov_tmp == NULL) {
2065 TALLOC_FREE(iov);
2066 return NT_STATUS_NO_MEMORY;
2068 iov = iov_tmp;
2069 cur = &iov[num_iov];
2070 num_iov += 2;
2072 cur[0].iov_len = wct * sizeof(uint16_t);
2073 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
2074 cur[1].iov_len = bcc * sizeof(uint8_t);
2075 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
2077 taken += needed;
2079 if (!smb1cli_is_andx_req(cmd)) {
2081 * If the current command does not have AndX chanining
2082 * we are done.
2084 break;
2087 if (wct == 0 && bcc == 0) {
2089 * An empty response also ends the chain,
2090 * most likely with an error.
2092 break;
2095 if (wct < 2) {
2096 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
2097 __location__, (int)wct, (int)cmd));
2098 goto inval;
2100 cmd = CVAL(cur[0].iov_base, 0);
2101 if (cmd == 0xFF) {
2103 * If it is the end of the chain we are also done.
2105 break;
2107 wct_ofs = SVAL(cur[0].iov_base, 2);
2109 if (wct_ofs < taken) {
2110 goto inval;
2112 if (wct_ofs > buflen) {
2113 goto inval;
2117 * we consumed everything up to the start of the next
2118 * parameter block.
2120 taken = wct_ofs;
2123 remaining = buflen - taken;
2125 if (remaining > 0 && num_iov >= 3) {
2127 * The last DATA block gets the remaining
2128 * bytes, this is needed to support
2129 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
2131 iov[num_iov-1].iov_len += remaining;
2134 *piov = iov;
2135 *pnum_iov = num_iov;
2136 return NT_STATUS_OK;
2138 inval:
2139 TALLOC_FREE(iov);
2140 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2143 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2144 TALLOC_CTX *tmp_mem,
2145 uint8_t *inbuf)
2147 struct tevent_req *req;
2148 struct smbXcli_req_state *state;
2149 NTSTATUS status;
2150 size_t num_pending;
2151 size_t i;
2152 uint8_t cmd;
2153 uint16_t mid;
2154 bool oplock_break;
2155 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
2156 size_t len = smb_len_tcp(inbuf);
2157 struct iovec *iov = NULL;
2158 int num_iov = 0;
2159 struct tevent_req **chain = NULL;
2160 size_t num_chained = 0;
2161 size_t num_responses = 0;
2163 if (conn->smb1.read_braw_req != NULL) {
2164 req = conn->smb1.read_braw_req;
2165 conn->smb1.read_braw_req = NULL;
2166 state = tevent_req_data(req, struct smbXcli_req_state);
2168 smbXcli_req_unset_pending(req);
2170 if (state->smb1.recv_iov == NULL) {
2172 * For requests with more than
2173 * one response, we have to readd the
2174 * recv_iov array.
2176 state->smb1.recv_iov = talloc_zero_array(state,
2177 struct iovec,
2179 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2180 return NT_STATUS_OK;
2184 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
2185 state->smb1.recv_iov[0].iov_len = len;
2186 ZERO_STRUCT(state->smb1.recv_iov[1]);
2187 ZERO_STRUCT(state->smb1.recv_iov[2]);
2189 state->smb1.recv_cmd = SMBreadBraw;
2190 state->smb1.recv_status = NT_STATUS_OK;
2191 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2193 tevent_req_done(req);
2194 return NT_STATUS_OK;
2197 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
2198 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
2199 DEBUG(10, ("Got non-SMB PDU\n"));
2200 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2204 * If we supported multiple encrytion contexts
2205 * here we'd look up based on tid.
2207 if (common_encryption_on(conn->smb1.trans_enc)
2208 && (CVAL(inbuf, 0) == 0)) {
2209 uint16_t enc_ctx_num;
2211 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
2212 if (!NT_STATUS_IS_OK(status)) {
2213 DEBUG(10, ("get_enc_ctx_num returned %s\n",
2214 nt_errstr(status)));
2215 return status;
2218 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
2219 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
2220 enc_ctx_num,
2221 conn->smb1.trans_enc->enc_ctx_num));
2222 return NT_STATUS_INVALID_HANDLE;
2225 status = common_decrypt_buffer(conn->smb1.trans_enc,
2226 (char *)inbuf);
2227 if (!NT_STATUS_IS_OK(status)) {
2228 DEBUG(10, ("common_decrypt_buffer returned %s\n",
2229 nt_errstr(status)));
2230 return status;
2232 inhdr = inbuf + NBT_HDR_SIZE;
2233 len = smb_len_nbt(inbuf);
2236 mid = SVAL(inhdr, HDR_MID);
2237 num_pending = talloc_array_length(conn->pending);
2239 for (i=0; i<num_pending; i++) {
2240 if (mid == smb1cli_req_mid(conn->pending[i])) {
2241 break;
2244 if (i == num_pending) {
2245 /* Dump unexpected reply */
2246 return NT_STATUS_RETRY;
2249 oplock_break = false;
2251 if (mid == 0xffff) {
2253 * Paranoia checks that this is really an oplock break request.
2255 oplock_break = (len == 51); /* hdr + 8 words */
2256 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
2257 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
2258 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
2259 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
2261 if (!oplock_break) {
2262 /* Dump unexpected reply */
2263 return NT_STATUS_RETRY;
2267 req = conn->pending[i];
2268 state = tevent_req_data(req, struct smbXcli_req_state);
2270 if (!oplock_break /* oplock breaks are not signed */
2271 && !smb_signing_check_pdu(conn->smb1.signing,
2272 inhdr, len, state->smb1.seqnum+1)) {
2273 DEBUG(10, ("cli_check_sign_mac failed\n"));
2274 return NT_STATUS_ACCESS_DENIED;
2277 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2278 &iov, &num_iov);
2279 if (!NT_STATUS_IS_OK(status)) {
2280 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2281 nt_errstr(status)));
2282 return status;
2285 cmd = CVAL(inhdr, HDR_COM);
2286 status = smb1cli_pull_raw_error(inhdr);
2288 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2289 (state->session != NULL) && state->session->disconnect_expired)
2292 * this should be a short term hack
2293 * until the upper layers have implemented
2294 * re-authentication.
2296 return status;
2299 if (state->smb1.chained_requests == NULL) {
2300 if (num_iov != 3) {
2301 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2304 smbXcli_req_unset_pending(req);
2306 if (state->smb1.recv_iov == NULL) {
2308 * For requests with more than
2309 * one response, we have to readd the
2310 * recv_iov array.
2312 state->smb1.recv_iov = talloc_zero_array(state,
2313 struct iovec,
2315 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2316 return NT_STATUS_OK;
2320 state->smb1.recv_cmd = cmd;
2321 state->smb1.recv_status = status;
2322 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2324 state->smb1.recv_iov[0] = iov[0];
2325 state->smb1.recv_iov[1] = iov[1];
2326 state->smb1.recv_iov[2] = iov[2];
2328 if (talloc_array_length(conn->pending) == 0) {
2329 tevent_req_done(req);
2330 return NT_STATUS_OK;
2333 tevent_req_defer_callback(req, state->ev);
2334 tevent_req_done(req);
2335 return NT_STATUS_RETRY;
2338 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2339 num_chained = talloc_array_length(chain);
2340 num_responses = (num_iov - 1)/2;
2342 if (num_responses > num_chained) {
2343 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2346 for (i=0; i<num_chained; i++) {
2347 size_t iov_idx = 1 + (i*2);
2348 struct iovec *cur = &iov[iov_idx];
2349 uint8_t *inbuf_ref;
2351 req = chain[i];
2352 state = tevent_req_data(req, struct smbXcli_req_state);
2354 smbXcli_req_unset_pending(req);
2357 * as we finish multiple requests here
2358 * we need to defer the callbacks as
2359 * they could destroy our current stack state.
2361 tevent_req_defer_callback(req, state->ev);
2363 if (i >= num_responses) {
2364 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2365 continue;
2368 if (state->smb1.recv_iov == NULL) {
2370 * For requests with more than
2371 * one response, we have to readd the
2372 * recv_iov array.
2374 state->smb1.recv_iov = talloc_zero_array(state,
2375 struct iovec,
2377 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2378 continue;
2382 state->smb1.recv_cmd = cmd;
2384 if (i == (num_responses - 1)) {
2386 * The last request in the chain gets the status
2388 state->smb1.recv_status = status;
2389 } else {
2390 cmd = CVAL(cur[0].iov_base, 0);
2391 state->smb1.recv_status = NT_STATUS_OK;
2394 state->inbuf = inbuf;
2397 * Note: here we use talloc_reference() in a way
2398 * that does not expose it to the caller.
2400 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2401 if (tevent_req_nomem(inbuf_ref, req)) {
2402 continue;
2405 /* copy the related buffers */
2406 state->smb1.recv_iov[0] = iov[0];
2407 state->smb1.recv_iov[1] = cur[0];
2408 state->smb1.recv_iov[2] = cur[1];
2410 tevent_req_done(req);
2413 return NT_STATUS_RETRY;
2416 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2417 TALLOC_CTX *mem_ctx,
2418 struct iovec **piov,
2419 uint8_t **phdr,
2420 uint8_t *pwct,
2421 uint16_t **pvwv,
2422 uint32_t *pvwv_offset,
2423 uint32_t *pnum_bytes,
2424 uint8_t **pbytes,
2425 uint32_t *pbytes_offset,
2426 uint8_t **pinbuf,
2427 const struct smb1cli_req_expected_response *expected,
2428 size_t num_expected)
2430 struct smbXcli_req_state *state =
2431 tevent_req_data(req,
2432 struct smbXcli_req_state);
2433 NTSTATUS status = NT_STATUS_OK;
2434 struct iovec *recv_iov = NULL;
2435 uint8_t *hdr = NULL;
2436 uint8_t wct = 0;
2437 uint32_t vwv_offset = 0;
2438 uint16_t *vwv = NULL;
2439 uint32_t num_bytes = 0;
2440 uint32_t bytes_offset = 0;
2441 uint8_t *bytes = NULL;
2442 size_t i;
2443 bool found_status = false;
2444 bool found_size = false;
2446 if (piov != NULL) {
2447 *piov = NULL;
2449 if (phdr != NULL) {
2450 *phdr = 0;
2452 if (pwct != NULL) {
2453 *pwct = 0;
2455 if (pvwv != NULL) {
2456 *pvwv = NULL;
2458 if (pvwv_offset != NULL) {
2459 *pvwv_offset = 0;
2461 if (pnum_bytes != NULL) {
2462 *pnum_bytes = 0;
2464 if (pbytes != NULL) {
2465 *pbytes = NULL;
2467 if (pbytes_offset != NULL) {
2468 *pbytes_offset = 0;
2470 if (pinbuf != NULL) {
2471 *pinbuf = NULL;
2474 if (state->inbuf != NULL) {
2475 recv_iov = state->smb1.recv_iov;
2476 state->smb1.recv_iov = NULL;
2477 if (state->smb1.recv_cmd != SMBreadBraw) {
2478 hdr = (uint8_t *)recv_iov[0].iov_base;
2479 wct = recv_iov[1].iov_len/2;
2480 vwv = (uint16_t *)recv_iov[1].iov_base;
2481 vwv_offset = PTR_DIFF(vwv, hdr);
2482 num_bytes = recv_iov[2].iov_len;
2483 bytes = (uint8_t *)recv_iov[2].iov_base;
2484 bytes_offset = PTR_DIFF(bytes, hdr);
2488 if (tevent_req_is_nterror(req, &status)) {
2489 for (i=0; i < num_expected; i++) {
2490 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2491 found_status = true;
2492 break;
2496 if (found_status) {
2497 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2500 return status;
2503 if (num_expected == 0) {
2504 found_status = true;
2505 found_size = true;
2508 status = state->smb1.recv_status;
2510 for (i=0; i < num_expected; i++) {
2511 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2512 continue;
2515 found_status = true;
2516 if (expected[i].wct == 0) {
2517 found_size = true;
2518 break;
2521 if (expected[i].wct == wct) {
2522 found_size = true;
2523 break;
2527 if (!found_status) {
2528 return status;
2531 if (!found_size) {
2532 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2535 if (piov != NULL) {
2536 *piov = talloc_move(mem_ctx, &recv_iov);
2539 if (phdr != NULL) {
2540 *phdr = hdr;
2542 if (pwct != NULL) {
2543 *pwct = wct;
2545 if (pvwv != NULL) {
2546 *pvwv = vwv;
2548 if (pvwv_offset != NULL) {
2549 *pvwv_offset = vwv_offset;
2551 if (pnum_bytes != NULL) {
2552 *pnum_bytes = num_bytes;
2554 if (pbytes != NULL) {
2555 *pbytes = bytes;
2557 if (pbytes_offset != NULL) {
2558 *pbytes_offset = bytes_offset;
2560 if (pinbuf != NULL) {
2561 *pinbuf = state->inbuf;
2564 return status;
2567 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2569 size_t wct_ofs;
2570 int i;
2572 wct_ofs = HDR_WCT;
2574 for (i=0; i<num_reqs; i++) {
2575 struct smbXcli_req_state *state;
2576 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2577 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2578 state->smb1.iov_count-2);
2579 wct_ofs = (wct_ofs + 3) & ~3;
2581 return wct_ofs;
2584 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2586 struct smbXcli_req_state *first_state =
2587 tevent_req_data(reqs[0],
2588 struct smbXcli_req_state);
2589 struct smbXcli_req_state *state;
2590 size_t wct_offset;
2591 size_t chain_padding = 0;
2592 int i, iovlen;
2593 struct iovec *iov = NULL;
2594 struct iovec *this_iov;
2595 NTSTATUS status;
2596 ssize_t nbt_len;
2598 if (num_reqs == 1) {
2599 return smb1cli_req_writev_submit(reqs[0], first_state,
2600 first_state->smb1.iov,
2601 first_state->smb1.iov_count);
2604 iovlen = 0;
2605 for (i=0; i<num_reqs; i++) {
2606 if (!tevent_req_is_in_progress(reqs[i])) {
2607 return NT_STATUS_INTERNAL_ERROR;
2610 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2612 if (state->smb1.iov_count < 4) {
2613 return NT_STATUS_INVALID_PARAMETER_MIX;
2616 if (i == 0) {
2618 * The NBT and SMB header
2620 iovlen += 2;
2621 } else {
2623 * Chain padding
2625 iovlen += 1;
2629 * words and bytes
2631 iovlen += state->smb1.iov_count - 2;
2634 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2635 if (iov == NULL) {
2636 return NT_STATUS_NO_MEMORY;
2639 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2640 first_state, reqs, sizeof(*reqs) * num_reqs);
2641 if (first_state->smb1.chained_requests == NULL) {
2642 TALLOC_FREE(iov);
2643 return NT_STATUS_NO_MEMORY;
2646 wct_offset = HDR_WCT;
2647 this_iov = iov;
2649 for (i=0; i<num_reqs; i++) {
2650 size_t next_padding = 0;
2651 uint16_t *vwv;
2653 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2655 if (i < num_reqs-1) {
2656 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2657 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2658 TALLOC_FREE(iov);
2659 TALLOC_FREE(first_state->smb1.chained_requests);
2660 return NT_STATUS_INVALID_PARAMETER_MIX;
2664 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2665 state->smb1.iov_count-2) + 1;
2666 if ((wct_offset % 4) != 0) {
2667 next_padding = 4 - (wct_offset % 4);
2669 wct_offset += next_padding;
2670 vwv = state->smb1.vwv;
2672 if (i < num_reqs-1) {
2673 struct smbXcli_req_state *next_state =
2674 tevent_req_data(reqs[i+1],
2675 struct smbXcli_req_state);
2676 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2677 SCVAL(vwv+0, 1, 0);
2678 SSVAL(vwv+1, 0, wct_offset);
2679 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2680 /* properly end the chain */
2681 SCVAL(vwv+0, 0, 0xff);
2682 SCVAL(vwv+0, 1, 0xff);
2683 SSVAL(vwv+1, 0, 0);
2686 if (i == 0) {
2688 * The NBT and SMB header
2690 this_iov[0] = state->smb1.iov[0];
2691 this_iov[1] = state->smb1.iov[1];
2692 this_iov += 2;
2693 } else {
2695 * This one is a bit subtle. We have to add
2696 * chain_padding bytes between the requests, and we
2697 * have to also include the wct field of the
2698 * subsequent requests. We use the subsequent header
2699 * for the padding, it contains the wct field in its
2700 * last byte.
2702 this_iov[0].iov_len = chain_padding+1;
2703 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2704 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2705 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2706 this_iov += 1;
2710 * copy the words and bytes
2712 memcpy(this_iov, state->smb1.iov+2,
2713 sizeof(struct iovec) * (state->smb1.iov_count-2));
2714 this_iov += state->smb1.iov_count - 2;
2715 chain_padding = next_padding;
2718 nbt_len = iov_buflen(&iov[1], iovlen-1);
2719 if ((nbt_len == -1) || (nbt_len > first_state->conn->smb1.max_xmit)) {
2720 TALLOC_FREE(iov);
2721 TALLOC_FREE(first_state->smb1.chained_requests);
2722 return NT_STATUS_INVALID_PARAMETER_MIX;
2725 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2726 if (!NT_STATUS_IS_OK(status)) {
2727 TALLOC_FREE(iov);
2728 TALLOC_FREE(first_state->smb1.chained_requests);
2729 return status;
2732 return NT_STATUS_OK;
2735 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2737 return ((tevent_queue_length(conn->outgoing) != 0)
2738 || (talloc_array_length(conn->pending) != 0));
2741 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2743 if (conn->protocol >= PROTOCOL_SMB2_02) {
2744 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2747 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2750 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2752 uint16_t credits = 1;
2754 if (conn->smb2.cur_credits == 0) {
2755 if (max_dyn_len != NULL) {
2756 *max_dyn_len = 0;
2758 return false;
2761 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2762 credits = conn->smb2.cur_credits;
2765 if (max_dyn_len != NULL) {
2766 *max_dyn_len = credits * 65536;
2769 return true;
2772 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2774 return conn->smb2.server.capabilities;
2777 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2779 return conn->smb2.server.security_mode;
2782 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2784 return conn->smb2.server.max_trans_size;
2787 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2789 return conn->smb2.server.max_read_size;
2792 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2794 return conn->smb2.server.max_write_size;
2797 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2798 uint16_t max_credits)
2800 conn->smb2.max_credits = max_credits;
2803 uint16_t smb2cli_conn_get_cur_credits(struct smbXcli_conn *conn)
2805 return conn->smb2.cur_credits;
2808 uint8_t smb2cli_conn_get_io_priority(struct smbXcli_conn *conn)
2810 if (conn->protocol < PROTOCOL_SMB3_11) {
2811 return 0;
2814 return conn->smb2.io_priority;
2817 void smb2cli_conn_set_io_priority(struct smbXcli_conn *conn,
2818 uint8_t io_priority)
2820 conn->smb2.io_priority = io_priority;
2823 uint32_t smb2cli_conn_cc_chunk_len(struct smbXcli_conn *conn)
2825 return conn->smb2.cc_chunk_len;
2828 void smb2cli_conn_set_cc_chunk_len(struct smbXcli_conn *conn,
2829 uint32_t chunk_len)
2831 conn->smb2.cc_chunk_len = chunk_len;
2834 uint32_t smb2cli_conn_cc_max_chunks(struct smbXcli_conn *conn)
2836 return conn->smb2.cc_max_chunks;
2839 void smb2cli_conn_set_cc_max_chunks(struct smbXcli_conn *conn,
2840 uint32_t max_chunks)
2842 conn->smb2.cc_max_chunks = max_chunks;
2845 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2847 static bool smb2cli_req_cancel(struct tevent_req *req)
2849 struct smbXcli_req_state *state =
2850 tevent_req_data(req,
2851 struct smbXcli_req_state);
2852 struct smbXcli_tcon *tcon = state->tcon;
2853 struct smbXcli_session *session = state->session;
2854 uint8_t *fixed = state->smb2.pad;
2855 uint16_t fixed_len = 4;
2856 struct tevent_req *subreq;
2857 struct smbXcli_req_state *substate;
2858 NTSTATUS status;
2860 SSVAL(fixed, 0, 0x04);
2861 SSVAL(fixed, 2, 0);
2863 subreq = smb2cli_req_create(state, state->ev,
2864 state->conn,
2865 SMB2_OP_CANCEL,
2866 0, 0, /* flags */
2867 0, /* timeout */
2868 tcon, session,
2869 fixed, fixed_len,
2870 NULL, 0, 0);
2871 if (subreq == NULL) {
2872 return false;
2874 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2876 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, state->smb2.cancel_flags);
2877 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, state->smb2.cancel_mid);
2878 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, state->smb2.cancel_aid);
2880 status = smb2cli_req_compound_submit(&subreq, 1);
2881 if (!NT_STATUS_IS_OK(status)) {
2882 TALLOC_FREE(subreq);
2883 return false;
2886 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2888 return true;
2891 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2893 /* we do not care about the result */
2894 TALLOC_FREE(subreq);
2897 struct timeval smbXcli_req_endtime(struct tevent_req *req)
2899 struct smbXcli_req_state *state = tevent_req_data(
2900 req, struct smbXcli_req_state);
2902 return state->endtime;
2905 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2906 struct tevent_context *ev,
2907 struct smbXcli_conn *conn,
2908 uint16_t cmd,
2909 uint32_t additional_flags,
2910 uint32_t clear_flags,
2911 uint32_t timeout_msec,
2912 struct smbXcli_tcon *tcon,
2913 struct smbXcli_session *session,
2914 const uint8_t *fixed,
2915 uint16_t fixed_len,
2916 const uint8_t *dyn,
2917 uint32_t dyn_len,
2918 uint32_t max_dyn_len)
2920 struct tevent_req *req;
2921 struct smbXcli_req_state *state;
2922 uint32_t flags = 0;
2923 uint32_t tid = 0;
2924 uint64_t uid = 0;
2925 bool use_channel_sequence = conn->smb2.force_channel_sequence;
2926 uint16_t channel_sequence = 0;
2927 bool use_replay_flag = false;
2929 req = tevent_req_create(mem_ctx, &state,
2930 struct smbXcli_req_state);
2931 if (req == NULL) {
2932 return NULL;
2935 state->ev = ev;
2936 state->conn = conn;
2937 state->session = session;
2938 state->tcon = tcon;
2940 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2941 use_channel_sequence = true;
2942 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2943 use_channel_sequence = true;
2946 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_00) {
2947 use_replay_flag = true;
2950 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_11) {
2951 flags |= SMB2_PRIORITY_VALUE_TO_MASK(conn->smb2.io_priority);
2954 if (session) {
2955 uid = session->smb2->session_id;
2957 if (use_channel_sequence) {
2958 channel_sequence = session->smb2->channel_sequence;
2961 if (use_replay_flag && session->smb2->replay_active) {
2962 additional_flags |= SMB2_HDR_FLAG_REPLAY_OPERATION;
2965 state->smb2.should_sign = session->smb2->should_sign;
2966 state->smb2.should_encrypt = session->smb2->should_encrypt;
2968 if (cmd == SMB2_OP_SESSSETUP &&
2969 session->smb2_channel.signing_key.length == 0 &&
2970 session->smb2->signing_key.length != 0)
2973 * a session bind needs to be signed
2975 state->smb2.should_sign = true;
2978 if (cmd == SMB2_OP_SESSSETUP &&
2979 session->smb2_channel.signing_key.length == 0) {
2980 state->smb2.should_encrypt = false;
2983 if (additional_flags & SMB2_HDR_FLAG_SIGNED) {
2984 if (session->smb2_channel.signing_key.length == 0) {
2985 tevent_req_nterror(req, NT_STATUS_NO_USER_SESSION_KEY);
2986 return req;
2989 additional_flags &= ~SMB2_HDR_FLAG_SIGNED;
2990 state->smb2.should_sign = true;
2994 if (tcon) {
2995 tid = tcon->smb2.tcon_id;
2997 if (tcon->smb2.should_sign) {
2998 state->smb2.should_sign = true;
3000 if (tcon->smb2.should_encrypt) {
3001 state->smb2.should_encrypt = true;
3005 if (state->smb2.should_encrypt) {
3006 state->smb2.should_sign = false;
3009 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
3010 if (state->smb2.recv_iov == NULL) {
3011 TALLOC_FREE(req);
3012 return NULL;
3015 flags |= additional_flags;
3016 flags &= ~clear_flags;
3018 state->smb2.fixed = fixed;
3019 state->smb2.fixed_len = fixed_len;
3020 state->smb2.dyn = dyn;
3021 state->smb2.dyn_len = dyn_len;
3022 state->smb2.max_dyn_len = max_dyn_len;
3024 if (state->smb2.should_encrypt) {
3025 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
3026 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
3029 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
3030 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
3031 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
3032 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
3033 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
3034 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
3035 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
3036 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
3038 switch (cmd) {
3039 case SMB2_OP_CANCEL:
3040 state->one_way = true;
3041 break;
3042 case SMB2_OP_BREAK:
3044 * If this is a dummy request, it will have
3045 * UINT64_MAX as message id.
3046 * If we send on break acknowledgement,
3047 * this gets overwritten later.
3049 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
3050 break;
3053 if (timeout_msec > 0) {
3054 struct timeval endtime;
3056 endtime = timeval_current_ofs_msec(timeout_msec);
3057 if (!tevent_req_set_endtime(req, ev, endtime)) {
3058 return req;
3062 return req;
3065 void smb2cli_req_set_notify_async(struct tevent_req *req)
3067 struct smbXcli_req_state *state =
3068 tevent_req_data(req,
3069 struct smbXcli_req_state);
3071 state->smb2.notify_async = true;
3074 static void smb2cli_req_writev_done(struct tevent_req *subreq);
3075 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3076 TALLOC_CTX *tmp_mem,
3077 uint8_t *inbuf);
3079 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
3080 int num_reqs)
3082 struct smbXcli_req_state *state;
3083 struct tevent_req *subreq;
3084 struct iovec *iov;
3085 int i, num_iov, nbt_len;
3086 int tf_iov = -1;
3087 const DATA_BLOB *encryption_key = NULL;
3088 uint64_t encryption_session_id = 0;
3089 uint64_t nonce_high = UINT64_MAX;
3090 uint64_t nonce_low = UINT64_MAX;
3093 * 1 for the nbt length, optional TRANSFORM
3094 * per request: HDR, fixed, dyn, padding
3095 * -1 because the last one does not need padding
3098 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
3099 if (iov == NULL) {
3100 return NT_STATUS_NO_MEMORY;
3103 num_iov = 1;
3104 nbt_len = 0;
3107 * the session of the first request that requires encryption
3108 * specifies the encryption key.
3110 for (i=0; i<num_reqs; i++) {
3111 if (!tevent_req_is_in_progress(reqs[i])) {
3112 return NT_STATUS_INTERNAL_ERROR;
3115 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
3117 if (!smbXcli_conn_is_connected(state->conn)) {
3118 return NT_STATUS_CONNECTION_DISCONNECTED;
3121 if ((state->conn->protocol != PROTOCOL_NONE) &&
3122 (state->conn->protocol < PROTOCOL_SMB2_02)) {
3123 return NT_STATUS_REVISION_MISMATCH;
3126 if (state->session == NULL) {
3127 continue;
3130 if (!state->smb2.should_encrypt) {
3131 continue;
3134 encryption_key = &state->session->smb2->encryption_key;
3135 if (encryption_key->length == 0) {
3136 return NT_STATUS_INVALID_PARAMETER_MIX;
3139 encryption_session_id = state->session->smb2->session_id;
3141 state->session->smb2->nonce_low += 1;
3142 if (state->session->smb2->nonce_low == 0) {
3143 state->session->smb2->nonce_high += 1;
3144 state->session->smb2->nonce_low += 1;
3148 * CCM and GCM algorithms must never have their
3149 * nonce wrap, or the security of the whole
3150 * communication and the keys is destroyed.
3151 * We must drop the connection once we have
3152 * transfered too much data.
3154 * NOTE: We assume nonces greater than 8 bytes.
3156 if (state->session->smb2->nonce_high >=
3157 state->session->smb2->nonce_high_max)
3159 return NT_STATUS_ENCRYPTION_FAILED;
3162 nonce_high = state->session->smb2->nonce_high_random;
3163 nonce_high += state->session->smb2->nonce_high;
3164 nonce_low = state->session->smb2->nonce_low;
3166 tf_iov = num_iov;
3167 iov[num_iov].iov_base = state->smb2.transform;
3168 iov[num_iov].iov_len = sizeof(state->smb2.transform);
3169 num_iov += 1;
3171 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
3172 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
3173 nonce_low);
3174 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
3175 nonce_high);
3176 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
3177 encryption_session_id);
3179 nbt_len += SMB2_TF_HDR_SIZE;
3180 break;
3183 for (i=0; i<num_reqs; i++) {
3184 int hdr_iov;
3185 size_t reqlen;
3186 bool ret;
3187 uint16_t opcode;
3188 uint64_t avail;
3189 uint16_t charge;
3190 uint16_t credits;
3191 uint64_t mid;
3192 const DATA_BLOB *signing_key = NULL;
3194 if (!tevent_req_is_in_progress(reqs[i])) {
3195 return NT_STATUS_INTERNAL_ERROR;
3198 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
3200 if (!smbXcli_conn_is_connected(state->conn)) {
3201 return NT_STATUS_CONNECTION_DISCONNECTED;
3204 if ((state->conn->protocol != PROTOCOL_NONE) &&
3205 (state->conn->protocol < PROTOCOL_SMB2_02)) {
3206 return NT_STATUS_REVISION_MISMATCH;
3209 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3210 if (opcode == SMB2_OP_CANCEL) {
3211 goto skip_credits;
3214 avail = UINT64_MAX - state->conn->smb2.mid;
3215 if (avail < 1) {
3216 return NT_STATUS_CONNECTION_ABORTED;
3219 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
3220 uint32_t max_dyn_len = 1;
3222 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
3223 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
3225 charge = (max_dyn_len - 1)/ 65536 + 1;
3226 } else {
3227 charge = 1;
3230 charge = MAX(state->smb2.credit_charge, charge);
3232 avail = MIN(avail, state->conn->smb2.cur_credits);
3233 if (avail < charge) {
3234 return NT_STATUS_INTERNAL_ERROR;
3237 credits = 0;
3238 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
3239 credits = state->conn->smb2.max_credits -
3240 state->conn->smb2.cur_credits;
3242 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
3243 credits += 1;
3246 mid = state->conn->smb2.mid;
3247 state->conn->smb2.mid += charge;
3248 state->conn->smb2.cur_credits -= charge;
3250 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
3251 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
3253 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
3254 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
3256 state->smb2.cancel_flags = 0;
3257 state->smb2.cancel_mid = mid;
3258 state->smb2.cancel_aid = 0;
3260 skip_credits:
3261 if (state->session && encryption_key == NULL) {
3263 * We prefer the channel signing key if it is
3264 * already there.
3266 if (state->smb2.should_sign) {
3267 signing_key = &state->session->smb2_channel.signing_key;
3271 * If it is a channel binding, we already have the main
3272 * signing key and try that one.
3274 if (signing_key && signing_key->length == 0) {
3275 signing_key = &state->session->smb2->signing_key;
3279 * If we do not have any session key yet, we skip the
3280 * signing of SMB2_OP_SESSSETUP requests.
3282 if (signing_key && signing_key->length == 0) {
3283 signing_key = NULL;
3287 hdr_iov = num_iov;
3288 iov[num_iov].iov_base = state->smb2.hdr;
3289 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
3290 num_iov += 1;
3292 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
3293 iov[num_iov].iov_len = state->smb2.fixed_len;
3294 num_iov += 1;
3296 if (state->smb2.dyn != NULL) {
3297 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
3298 iov[num_iov].iov_len = state->smb2.dyn_len;
3299 num_iov += 1;
3302 reqlen = sizeof(state->smb2.hdr);
3303 reqlen += state->smb2.fixed_len;
3304 reqlen += state->smb2.dyn_len;
3306 if (i < num_reqs-1) {
3307 if ((reqlen % 8) > 0) {
3308 uint8_t pad = 8 - (reqlen % 8);
3309 iov[num_iov].iov_base = state->smb2.pad;
3310 iov[num_iov].iov_len = pad;
3311 num_iov += 1;
3312 reqlen += pad;
3314 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
3317 state->smb2.encryption_session_id = encryption_session_id;
3319 if (signing_key != NULL) {
3320 NTSTATUS status;
3322 status = smb2_signing_sign_pdu(*signing_key,
3323 state->session->conn->protocol,
3324 &iov[hdr_iov], num_iov - hdr_iov);
3325 if (!NT_STATUS_IS_OK(status)) {
3326 return status;
3330 nbt_len += reqlen;
3332 ret = smbXcli_req_set_pending(reqs[i]);
3333 if (!ret) {
3334 return NT_STATUS_NO_MEMORY;
3338 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
3339 _smb_setlen_tcp(state->length_hdr, nbt_len);
3340 iov[0].iov_base = state->length_hdr;
3341 iov[0].iov_len = sizeof(state->length_hdr);
3343 if (encryption_key != NULL) {
3344 NTSTATUS status;
3345 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
3346 uint8_t *buf;
3347 int vi;
3349 buf = talloc_array(iov, uint8_t, buflen);
3350 if (buf == NULL) {
3351 return NT_STATUS_NO_MEMORY;
3355 * We copy the buffers before encrypting them,
3356 * this is at least currently needed for the
3357 * to keep state->smb2.hdr.
3359 * Also the callers may expect there buffers
3360 * to be const.
3362 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3363 struct iovec *v = &iov[vi];
3364 const uint8_t *o = (const uint8_t *)v->iov_base;
3366 memcpy(buf, o, v->iov_len);
3367 v->iov_base = (void *)buf;
3368 buf += v->iov_len;
3371 status = smb2_signing_encrypt_pdu(*encryption_key,
3372 state->conn->smb2.server.cipher,
3373 &iov[tf_iov], num_iov - tf_iov);
3374 if (!NT_STATUS_IS_OK(status)) {
3375 return status;
3379 if (state->conn->dispatch_incoming == NULL) {
3380 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3383 subreq = writev_send(state, state->ev, state->conn->outgoing,
3384 state->conn->sock_fd, false, iov, num_iov);
3385 if (subreq == NULL) {
3386 return NT_STATUS_NO_MEMORY;
3388 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3389 state->write_req = subreq;
3391 return NT_STATUS_OK;
3394 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3396 struct smbXcli_req_state *state =
3397 tevent_req_data(req,
3398 struct smbXcli_req_state);
3400 state->smb2.credit_charge = charge;
3403 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3404 struct tevent_context *ev,
3405 struct smbXcli_conn *conn,
3406 uint16_t cmd,
3407 uint32_t additional_flags,
3408 uint32_t clear_flags,
3409 uint32_t timeout_msec,
3410 struct smbXcli_tcon *tcon,
3411 struct smbXcli_session *session,
3412 const uint8_t *fixed,
3413 uint16_t fixed_len,
3414 const uint8_t *dyn,
3415 uint32_t dyn_len,
3416 uint32_t max_dyn_len)
3418 struct tevent_req *req;
3419 NTSTATUS status;
3421 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3422 additional_flags, clear_flags,
3423 timeout_msec,
3424 tcon, session,
3425 fixed, fixed_len,
3426 dyn, dyn_len,
3427 max_dyn_len);
3428 if (req == NULL) {
3429 return NULL;
3431 if (!tevent_req_is_in_progress(req)) {
3432 return tevent_req_post(req, ev);
3434 status = smb2cli_req_compound_submit(&req, 1);
3435 if (tevent_req_nterror(req, status)) {
3436 return tevent_req_post(req, ev);
3438 return req;
3441 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3443 struct tevent_req *req =
3444 tevent_req_callback_data(subreq,
3445 struct tevent_req);
3446 struct smbXcli_req_state *state =
3447 tevent_req_data(req,
3448 struct smbXcli_req_state);
3449 ssize_t nwritten;
3450 int err;
3452 state->write_req = NULL;
3454 nwritten = writev_recv(subreq, &err);
3455 TALLOC_FREE(subreq);
3456 if (nwritten == -1) {
3457 /* here, we need to notify all pending requests */
3458 NTSTATUS status = map_nt_error_from_unix_common(err);
3459 smbXcli_conn_disconnect(state->conn, status);
3460 return;
3464 static struct smbXcli_session* smbXcli_session_by_uid(struct smbXcli_conn *conn,
3465 uint64_t uid)
3467 struct smbXcli_session *s = conn->sessions;
3469 for (; s; s = s->next) {
3470 if (s->smb2->session_id != uid) {
3471 continue;
3473 break;
3476 return s;
3479 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3480 uint8_t *buf,
3481 size_t buflen,
3482 TALLOC_CTX *mem_ctx,
3483 struct iovec **piov,
3484 size_t *pnum_iov)
3486 struct iovec *iov;
3487 int num_iov = 0;
3488 size_t taken = 0;
3489 uint8_t *first_hdr = buf;
3490 size_t verified_buflen = 0;
3491 uint8_t *tf = NULL;
3492 size_t tf_len = 0;
3494 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3495 if (iov == NULL) {
3496 return NT_STATUS_NO_MEMORY;
3499 while (taken < buflen) {
3500 size_t len = buflen - taken;
3501 uint8_t *hdr = first_hdr + taken;
3502 struct iovec *cur;
3503 size_t full_size;
3504 size_t next_command_ofs;
3505 uint16_t body_size;
3506 struct iovec *iov_tmp;
3508 if (verified_buflen > taken) {
3509 len = verified_buflen - taken;
3510 } else {
3511 tf = NULL;
3512 tf_len = 0;
3515 if (len < 4) {
3516 DEBUG(10, ("%d bytes left, expected at least %d\n",
3517 (int)len, 4));
3518 goto inval;
3520 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3521 struct smbXcli_session *s;
3522 uint64_t uid;
3523 struct iovec tf_iov[2];
3524 size_t enc_len;
3525 NTSTATUS status;
3527 if (len < SMB2_TF_HDR_SIZE) {
3528 DEBUG(10, ("%d bytes left, expected at least %d\n",
3529 (int)len, SMB2_TF_HDR_SIZE));
3530 goto inval;
3532 tf = hdr;
3533 tf_len = SMB2_TF_HDR_SIZE;
3534 taken += tf_len;
3536 hdr = first_hdr + taken;
3537 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3538 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3540 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3541 DEBUG(10, ("%d bytes left, expected at least %d\n",
3542 (int)len,
3543 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3544 goto inval;
3547 s = smbXcli_session_by_uid(conn, uid);
3548 if (s == NULL) {
3549 DEBUG(10, ("unknown session_id %llu\n",
3550 (unsigned long long)uid));
3551 goto inval;
3554 tf_iov[0].iov_base = (void *)tf;
3555 tf_iov[0].iov_len = tf_len;
3556 tf_iov[1].iov_base = (void *)hdr;
3557 tf_iov[1].iov_len = enc_len;
3559 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3560 conn->smb2.server.cipher,
3561 tf_iov, 2);
3562 if (!NT_STATUS_IS_OK(status)) {
3563 TALLOC_FREE(iov);
3564 return status;
3567 verified_buflen = taken + enc_len;
3568 len = enc_len;
3572 * We need the header plus the body length field
3575 if (len < SMB2_HDR_BODY + 2) {
3576 DEBUG(10, ("%d bytes left, expected at least %d\n",
3577 (int)len, SMB2_HDR_BODY));
3578 goto inval;
3580 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3581 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3582 IVAL(hdr, 0)));
3583 goto inval;
3585 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3586 DEBUG(10, ("Got HDR len %d, expected %d\n",
3587 SVAL(hdr, 4), SMB2_HDR_BODY));
3588 goto inval;
3591 full_size = len;
3592 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3593 body_size = SVAL(hdr, SMB2_HDR_BODY);
3595 if (next_command_ofs != 0) {
3596 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3597 goto inval;
3599 if (next_command_ofs > full_size) {
3600 goto inval;
3602 full_size = next_command_ofs;
3604 if (body_size < 2) {
3605 goto inval;
3607 body_size &= 0xfffe;
3609 if (body_size > (full_size - SMB2_HDR_BODY)) {
3610 goto inval;
3613 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3614 num_iov + 4);
3615 if (iov_tmp == NULL) {
3616 TALLOC_FREE(iov);
3617 return NT_STATUS_NO_MEMORY;
3619 iov = iov_tmp;
3620 cur = &iov[num_iov];
3621 num_iov += 4;
3623 cur[0].iov_base = tf;
3624 cur[0].iov_len = tf_len;
3625 cur[1].iov_base = hdr;
3626 cur[1].iov_len = SMB2_HDR_BODY;
3627 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3628 cur[2].iov_len = body_size;
3629 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3630 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3632 taken += full_size;
3635 *piov = iov;
3636 *pnum_iov = num_iov;
3637 return NT_STATUS_OK;
3639 inval:
3640 TALLOC_FREE(iov);
3641 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3644 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3645 uint64_t mid)
3647 size_t num_pending = talloc_array_length(conn->pending);
3648 size_t i;
3650 for (i=0; i<num_pending; i++) {
3651 struct tevent_req *req = conn->pending[i];
3652 struct smbXcli_req_state *state =
3653 tevent_req_data(req,
3654 struct smbXcli_req_state);
3656 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3657 return req;
3660 return NULL;
3663 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3664 TALLOC_CTX *tmp_mem,
3665 uint8_t *inbuf)
3667 struct tevent_req *req;
3668 struct smbXcli_req_state *state = NULL;
3669 struct iovec *iov = NULL;
3670 size_t i, num_iov = 0;
3671 NTSTATUS status;
3672 bool defer = true;
3673 struct smbXcli_session *last_session = NULL;
3674 size_t inbuf_len = smb_len_tcp(inbuf);
3676 status = smb2cli_inbuf_parse_compound(conn,
3677 inbuf + NBT_HDR_SIZE,
3678 inbuf_len,
3679 tmp_mem,
3680 &iov, &num_iov);
3681 if (!NT_STATUS_IS_OK(status)) {
3682 return status;
3685 for (i=0; i<num_iov; i+=4) {
3686 uint8_t *inbuf_ref = NULL;
3687 struct iovec *cur = &iov[i];
3688 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3689 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3690 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3691 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3692 uint16_t req_opcode;
3693 uint32_t req_flags;
3694 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3695 uint32_t new_credits;
3696 struct smbXcli_session *session = NULL;
3697 const DATA_BLOB *signing_key = NULL;
3698 bool was_encrypted = false;
3700 new_credits = conn->smb2.cur_credits;
3701 new_credits += credits;
3702 if (new_credits > UINT16_MAX) {
3703 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3705 conn->smb2.cur_credits += credits;
3707 req = smb2cli_conn_find_pending(conn, mid);
3708 if (req == NULL) {
3709 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3711 state = tevent_req_data(req, struct smbXcli_req_state);
3713 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3714 if (opcode != req_opcode) {
3715 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3717 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3719 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3720 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3723 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3724 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3725 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3726 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3728 if (state->smb2.got_async) {
3729 /* We only expect one STATUS_PENDING response */
3730 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3732 state->smb2.got_async = true;
3735 * async interim responses are not signed,
3736 * even if the SMB2_HDR_FLAG_SIGNED flag
3737 * is set.
3739 state->smb2.cancel_flags = SMB2_HDR_FLAG_ASYNC;
3740 state->smb2.cancel_mid = 0;
3741 state->smb2.cancel_aid = async_id;
3743 if (state->smb2.notify_async) {
3744 tevent_req_defer_callback(req, state->ev);
3745 tevent_req_notify_callback(req);
3747 continue;
3750 session = state->session;
3751 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3752 session = last_session;
3754 last_session = session;
3756 if (state->smb2.should_sign) {
3757 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3758 return NT_STATUS_ACCESS_DENIED;
3762 if (flags & SMB2_HDR_FLAG_SIGNED) {
3763 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3765 if (session == NULL) {
3766 session = smbXcli_session_by_uid(state->conn,
3767 uid);
3770 if (session == NULL) {
3771 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3774 last_session = session;
3775 signing_key = &session->smb2_channel.signing_key;
3778 if (opcode == SMB2_OP_SESSSETUP) {
3780 * We prefer the channel signing key, if it is
3781 * already there.
3783 * If we do not have a channel signing key yet,
3784 * we try the main signing key, if it is not
3785 * the final response.
3787 if (signing_key && signing_key->length == 0 &&
3788 !NT_STATUS_IS_OK(status)) {
3789 signing_key = &session->smb2->signing_key;
3792 if (signing_key && signing_key->length == 0) {
3794 * If we do not have a session key to
3795 * verify the signature, we defer the
3796 * signing check to the caller.
3798 * The caller gets NT_STATUS_OK, it
3799 * has to call
3800 * smb2cli_session_set_session_key()
3801 * or
3802 * smb2cli_session_set_channel_key()
3803 * which will check the signature
3804 * with the channel signing key.
3806 signing_key = NULL;
3810 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3811 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3812 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3815 * If the response was encrypted in a SMB2_TRANSFORM
3816 * pdu, which belongs to the correct session,
3817 * we do not need to do signing checks
3819 * It could be the session the response belongs to
3820 * or the session that was used to encrypt the
3821 * SMB2_TRANSFORM request.
3823 if ((session && session->smb2->session_id == uid) ||
3824 (state->smb2.encryption_session_id == uid)) {
3825 signing_key = NULL;
3826 was_encrypted = true;
3830 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3832 * if the server returns NT_STATUS_USER_SESSION_DELETED
3833 * the response is not signed and we should
3834 * propagate the NT_STATUS_USER_SESSION_DELETED
3835 * status to the caller.
3837 state->smb2.signing_skipped = true;
3838 signing_key = NULL;
3841 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3843 * if the server returns
3844 * NT_STATUS_INVALID_PARAMETER
3845 * the response might not be encrypted.
3847 if (state->smb2.should_encrypt && !was_encrypted) {
3848 state->smb2.signing_skipped = true;
3849 signing_key = NULL;
3853 if (state->smb2.should_encrypt && !was_encrypted) {
3854 if (!state->smb2.signing_skipped) {
3855 return NT_STATUS_ACCESS_DENIED;
3859 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3860 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3861 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3863 * if the server returns
3864 * NT_STATUS_NETWORK_NAME_DELETED
3865 * NT_STATUS_FILE_CLOSED
3866 * NT_STATUS_INVALID_PARAMETER
3867 * the response might not be signed
3868 * as this happens before the signing checks.
3870 * If server echos the signature (or all zeros)
3871 * we should report the status from the server
3872 * to the caller.
3874 if (signing_key) {
3875 int cmp;
3877 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3878 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3879 16);
3880 if (cmp == 0) {
3881 state->smb2.signing_skipped = true;
3882 signing_key = NULL;
3885 if (signing_key) {
3886 bool zero;
3887 zero = all_zero(inhdr+SMB2_HDR_SIGNATURE, 16);
3888 if (zero) {
3889 state->smb2.signing_skipped = true;
3890 signing_key = NULL;
3895 if (signing_key) {
3896 status = smb2_signing_check_pdu(*signing_key,
3897 state->conn->protocol,
3898 &cur[1], 3);
3899 if (!NT_STATUS_IS_OK(status)) {
3901 * If the signing check fails, we disconnect
3902 * the connection.
3904 return status;
3908 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3909 (session != NULL) && session->disconnect_expired)
3912 * this should be a short term hack
3913 * until the upper layers have implemented
3914 * re-authentication.
3916 return status;
3919 smbXcli_req_unset_pending(req);
3922 * There might be more than one response
3923 * we need to defer the notifications
3925 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3926 defer = false;
3929 if (defer) {
3930 tevent_req_defer_callback(req, state->ev);
3934 * Note: here we use talloc_reference() in a way
3935 * that does not expose it to the caller.
3937 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3938 if (tevent_req_nomem(inbuf_ref, req)) {
3939 continue;
3942 /* copy the related buffers */
3943 state->smb2.recv_iov[0] = cur[1];
3944 state->smb2.recv_iov[1] = cur[2];
3945 state->smb2.recv_iov[2] = cur[3];
3947 tevent_req_done(req);
3950 if (defer) {
3951 return NT_STATUS_RETRY;
3954 return NT_STATUS_OK;
3957 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3958 struct iovec **piov,
3959 const struct smb2cli_req_expected_response *expected,
3960 size_t num_expected)
3962 struct smbXcli_req_state *state =
3963 tevent_req_data(req,
3964 struct smbXcli_req_state);
3965 NTSTATUS status;
3966 size_t body_size;
3967 bool found_status = false;
3968 bool found_size = false;
3969 size_t i;
3971 if (piov != NULL) {
3972 *piov = NULL;
3975 if (tevent_req_is_in_progress(req) && state->smb2.got_async) {
3976 return STATUS_PENDING;
3979 if (tevent_req_is_nterror(req, &status)) {
3980 for (i=0; i < num_expected; i++) {
3981 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3982 found_status = true;
3983 break;
3987 if (found_status) {
3988 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3991 return status;
3994 if (num_expected == 0) {
3995 found_status = true;
3996 found_size = true;
3999 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
4000 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
4002 for (i=0; i < num_expected; i++) {
4003 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
4004 continue;
4007 found_status = true;
4008 if (expected[i].body_size == 0) {
4009 found_size = true;
4010 break;
4013 if (expected[i].body_size == body_size) {
4014 found_size = true;
4015 break;
4019 if (!found_status) {
4020 return status;
4023 if (state->smb2.signing_skipped) {
4024 if (num_expected > 0) {
4025 return NT_STATUS_ACCESS_DENIED;
4027 if (!NT_STATUS_IS_ERR(status)) {
4028 return NT_STATUS_ACCESS_DENIED;
4032 if (!found_size) {
4033 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4036 if (piov != NULL) {
4037 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
4040 return status;
4043 NTSTATUS smb2cli_req_get_sent_iov(struct tevent_req *req,
4044 struct iovec *sent_iov)
4046 struct smbXcli_req_state *state =
4047 tevent_req_data(req,
4048 struct smbXcli_req_state);
4050 if (tevent_req_is_in_progress(req)) {
4051 return STATUS_PENDING;
4054 sent_iov[0].iov_base = state->smb2.hdr;
4055 sent_iov[0].iov_len = sizeof(state->smb2.hdr);
4057 sent_iov[1].iov_base = discard_const(state->smb2.fixed);
4058 sent_iov[1].iov_len = state->smb2.fixed_len;
4060 if (state->smb2.dyn != NULL) {
4061 sent_iov[2].iov_base = discard_const(state->smb2.dyn);
4062 sent_iov[2].iov_len = state->smb2.dyn_len;
4063 } else {
4064 sent_iov[2].iov_base = NULL;
4065 sent_iov[2].iov_len = 0;
4068 return NT_STATUS_OK;
4071 static const struct {
4072 enum protocol_types proto;
4073 const char *smb1_name;
4074 } smb1cli_prots[] = {
4075 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
4076 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
4077 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
4078 {PROTOCOL_LANMAN1, "LANMAN1.0"},
4079 {PROTOCOL_LANMAN2, "LM1.2X002"},
4080 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
4081 {PROTOCOL_LANMAN2, "LANMAN2.1"},
4082 {PROTOCOL_LANMAN2, "Samba"},
4083 {PROTOCOL_NT1, "NT LANMAN 1.0"},
4084 {PROTOCOL_NT1, "NT LM 0.12"},
4085 {PROTOCOL_SMB2_02, "SMB 2.002"},
4086 {PROTOCOL_SMB2_10, "SMB 2.???"},
4089 static const struct {
4090 enum protocol_types proto;
4091 uint16_t smb2_dialect;
4092 } smb2cli_prots[] = {
4093 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
4094 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
4095 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
4096 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
4097 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
4098 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
4099 {PROTOCOL_SMB3_10, SMB3_DIALECT_REVISION_310},
4100 {PROTOCOL_SMB3_11, SMB3_DIALECT_REVISION_311},
4103 struct smbXcli_negprot_state {
4104 struct smbXcli_conn *conn;
4105 struct tevent_context *ev;
4106 uint32_t timeout_msec;
4108 struct {
4109 uint8_t fixed[36];
4110 } smb2;
4113 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
4114 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
4115 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
4116 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
4117 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
4118 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4119 TALLOC_CTX *frame,
4120 uint8_t *inbuf);
4122 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
4123 struct tevent_context *ev,
4124 struct smbXcli_conn *conn,
4125 uint32_t timeout_msec,
4126 enum protocol_types min_protocol,
4127 enum protocol_types max_protocol,
4128 uint16_t max_credits)
4130 struct tevent_req *req, *subreq;
4131 struct smbXcli_negprot_state *state;
4133 req = tevent_req_create(mem_ctx, &state,
4134 struct smbXcli_negprot_state);
4135 if (req == NULL) {
4136 return NULL;
4138 state->conn = conn;
4139 state->ev = ev;
4140 state->timeout_msec = timeout_msec;
4142 if (min_protocol == PROTOCOL_NONE) {
4143 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
4144 return tevent_req_post(req, ev);
4147 if (max_protocol == PROTOCOL_NONE) {
4148 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
4149 return tevent_req_post(req, ev);
4152 if (min_protocol > max_protocol) {
4153 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
4154 return tevent_req_post(req, ev);
4157 conn->min_protocol = min_protocol;
4158 conn->max_protocol = max_protocol;
4159 conn->protocol = PROTOCOL_NONE;
4161 if (max_protocol >= PROTOCOL_SMB2_02) {
4162 conn->smb2.max_credits = max_credits;
4165 if ((min_protocol < PROTOCOL_SMB2_02) &&
4166 (max_protocol < PROTOCOL_SMB2_02)) {
4168 * SMB1 only...
4170 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4172 subreq = smbXcli_negprot_smb1_subreq(state);
4173 if (tevent_req_nomem(subreq, req)) {
4174 return tevent_req_post(req, ev);
4176 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4177 return req;
4180 if ((min_protocol >= PROTOCOL_SMB2_02) &&
4181 (max_protocol >= PROTOCOL_SMB2_02)) {
4183 * SMB2 only...
4185 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4187 subreq = smbXcli_negprot_smb2_subreq(state);
4188 if (tevent_req_nomem(subreq, req)) {
4189 return tevent_req_post(req, ev);
4191 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4192 return req;
4196 * We send an SMB1 negprot with the SMB2 dialects
4197 * and expect a SMB1 or a SMB2 response.
4199 * smbXcli_negprot_dispatch_incoming() will fix the
4200 * callback to match protocol of the response.
4202 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
4204 subreq = smbXcli_negprot_smb1_subreq(state);
4205 if (tevent_req_nomem(subreq, req)) {
4206 return tevent_req_post(req, ev);
4208 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
4209 return req;
4212 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
4214 struct tevent_req *req =
4215 tevent_req_callback_data(subreq,
4216 struct tevent_req);
4217 NTSTATUS status;
4220 * we just want the low level error
4222 status = tevent_req_simple_recv_ntstatus(subreq);
4223 TALLOC_FREE(subreq);
4224 if (tevent_req_nterror(req, status)) {
4225 return;
4228 /* this should never happen */
4229 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4232 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
4234 size_t i;
4235 DATA_BLOB bytes = data_blob_null;
4236 uint8_t flags;
4237 uint16_t flags2;
4239 /* setup the protocol strings */
4240 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4241 uint8_t c = 2;
4242 bool ok;
4244 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4245 continue;
4248 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4249 continue;
4252 ok = data_blob_append(state, &bytes, &c, sizeof(c));
4253 if (!ok) {
4254 return NULL;
4258 * We now it is already ascii and
4259 * we want NULL termination.
4261 ok = data_blob_append(state, &bytes,
4262 smb1cli_prots[i].smb1_name,
4263 strlen(smb1cli_prots[i].smb1_name)+1);
4264 if (!ok) {
4265 return NULL;
4269 smb1cli_req_flags(state->conn->max_protocol,
4270 state->conn->smb1.client.capabilities,
4271 SMBnegprot,
4272 0, 0, &flags,
4273 0, 0, &flags2);
4275 return smb1cli_req_send(state, state->ev, state->conn,
4276 SMBnegprot,
4277 flags, ~flags,
4278 flags2, ~flags2,
4279 state->timeout_msec,
4280 0xFFFE, 0, NULL, /* pid, tid, session */
4281 0, NULL, /* wct, vwv */
4282 bytes.length, bytes.data);
4285 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
4287 struct tevent_req *req =
4288 tevent_req_callback_data(subreq,
4289 struct tevent_req);
4290 struct smbXcli_negprot_state *state =
4291 tevent_req_data(req,
4292 struct smbXcli_negprot_state);
4293 struct smbXcli_conn *conn = state->conn;
4294 struct iovec *recv_iov = NULL;
4295 uint8_t *inhdr;
4296 uint8_t wct;
4297 uint16_t *vwv;
4298 uint32_t num_bytes;
4299 uint8_t *bytes;
4300 NTSTATUS status;
4301 uint16_t protnum;
4302 size_t i;
4303 size_t num_prots = 0;
4304 uint8_t flags;
4305 uint32_t client_capabilities = conn->smb1.client.capabilities;
4306 uint32_t both_capabilities;
4307 uint32_t server_capabilities = 0;
4308 uint32_t capabilities;
4309 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
4310 uint32_t server_max_xmit = 0;
4311 uint32_t max_xmit;
4312 uint32_t server_max_mux = 0;
4313 uint16_t server_security_mode = 0;
4314 uint32_t server_session_key = 0;
4315 bool server_readbraw = false;
4316 bool server_writebraw = false;
4317 bool server_lockread = false;
4318 bool server_writeunlock = false;
4319 struct GUID server_guid = GUID_zero();
4320 DATA_BLOB server_gss_blob = data_blob_null;
4321 uint8_t server_challenge[8];
4322 char *server_workgroup = NULL;
4323 char *server_name = NULL;
4324 int server_time_zone = 0;
4325 NTTIME server_system_time = 0;
4326 static const struct smb1cli_req_expected_response expected[] = {
4328 .status = NT_STATUS_OK,
4329 .wct = 0x11, /* NT1 */
4332 .status = NT_STATUS_OK,
4333 .wct = 0x0D, /* LM */
4336 .status = NT_STATUS_OK,
4337 .wct = 0x01, /* CORE */
4341 ZERO_STRUCT(server_challenge);
4343 status = smb1cli_req_recv(subreq, state,
4344 &recv_iov,
4345 &inhdr,
4346 &wct,
4347 &vwv,
4348 NULL, /* pvwv_offset */
4349 &num_bytes,
4350 &bytes,
4351 NULL, /* pbytes_offset */
4352 NULL, /* pinbuf */
4353 expected, ARRAY_SIZE(expected));
4354 TALLOC_FREE(subreq);
4355 if (tevent_req_nterror(req, status)) {
4356 return;
4359 flags = CVAL(inhdr, HDR_FLG);
4361 protnum = SVAL(vwv, 0);
4363 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4364 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4365 continue;
4368 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4369 continue;
4372 if (protnum != num_prots) {
4373 num_prots++;
4374 continue;
4377 conn->protocol = smb1cli_prots[i].proto;
4378 break;
4381 if (conn->protocol == PROTOCOL_NONE) {
4382 DBG_ERR("No compatible protocol selected by server.\n");
4383 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4384 return;
4387 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
4388 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4389 "and the selected protocol level doesn't support it.\n"));
4390 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4391 return;
4394 if (flags & FLAG_SUPPORT_LOCKREAD) {
4395 server_lockread = true;
4396 server_writeunlock = true;
4399 if (conn->protocol >= PROTOCOL_NT1) {
4400 const char *client_signing = NULL;
4401 bool server_mandatory = false;
4402 bool server_allowed = false;
4403 const char *server_signing = NULL;
4404 bool ok;
4405 uint8_t key_len;
4407 if (wct != 0x11) {
4408 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4409 return;
4412 /* NT protocol */
4413 server_security_mode = CVAL(vwv + 1, 0);
4414 server_max_mux = SVAL(vwv + 1, 1);
4415 server_max_xmit = IVAL(vwv + 3, 1);
4416 server_session_key = IVAL(vwv + 7, 1);
4417 server_time_zone = SVALS(vwv + 15, 1);
4418 server_time_zone *= 60;
4419 /* this time arrives in real GMT */
4420 server_system_time = BVAL(vwv + 11, 1);
4421 server_capabilities = IVAL(vwv + 9, 1);
4423 key_len = CVAL(vwv + 16, 1);
4425 if (server_capabilities & CAP_RAW_MODE) {
4426 server_readbraw = true;
4427 server_writebraw = true;
4429 if (server_capabilities & CAP_LOCK_AND_READ) {
4430 server_lockread = true;
4433 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4434 DATA_BLOB blob1, blob2;
4436 if (num_bytes < 16) {
4437 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4438 return;
4441 blob1 = data_blob_const(bytes, 16);
4442 status = GUID_from_data_blob(&blob1, &server_guid);
4443 if (tevent_req_nterror(req, status)) {
4444 return;
4447 blob1 = data_blob_const(bytes+16, num_bytes-16);
4448 blob2 = data_blob_dup_talloc(state, blob1);
4449 if (blob1.length > 0 &&
4450 tevent_req_nomem(blob2.data, req)) {
4451 return;
4453 server_gss_blob = blob2;
4454 } else {
4455 DATA_BLOB blob1, blob2;
4457 if (num_bytes < key_len) {
4458 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4459 return;
4462 if (key_len != 0 && key_len != 8) {
4463 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4464 return;
4467 if (key_len == 8) {
4468 memcpy(server_challenge, bytes, 8);
4471 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4472 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4473 if (blob1.length > 0) {
4474 size_t len;
4476 len = utf16_len_n(blob1.data,
4477 blob1.length);
4478 blob1.length = len;
4480 ok = convert_string_talloc(state,
4481 CH_UTF16LE,
4482 CH_UNIX,
4483 blob1.data,
4484 blob1.length,
4485 &server_workgroup,
4486 &len);
4487 if (!ok) {
4488 status = map_nt_error_from_unix_common(errno);
4489 tevent_req_nterror(req, status);
4490 return;
4494 blob2.data += blob1.length;
4495 blob2.length -= blob1.length;
4496 if (blob2.length > 0) {
4497 size_t len;
4499 len = utf16_len_n(blob1.data,
4500 blob1.length);
4501 blob1.length = len;
4503 ok = convert_string_talloc(state,
4504 CH_UTF16LE,
4505 CH_UNIX,
4506 blob2.data,
4507 blob2.length,
4508 &server_name,
4509 &len);
4510 if (!ok) {
4511 status = map_nt_error_from_unix_common(errno);
4512 tevent_req_nterror(req, status);
4513 return;
4518 client_signing = "disabled";
4519 if (conn->allow_signing) {
4520 client_signing = "allowed";
4522 if (conn->mandatory_signing) {
4523 client_signing = "required";
4526 server_signing = "not supported";
4527 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4528 server_signing = "supported";
4529 server_allowed = true;
4530 } else if (conn->mandatory_signing) {
4532 * We have mandatory signing as client
4533 * lets assume the server will look at our
4534 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4535 * flag in the session setup
4537 server_signing = "not announced";
4538 server_allowed = true;
4540 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4541 server_signing = "required";
4542 server_mandatory = true;
4545 ok = smb_signing_set_negotiated(conn->smb1.signing,
4546 server_allowed,
4547 server_mandatory);
4548 if (!ok) {
4549 DEBUG(1,("cli_negprot: SMB signing is required, "
4550 "but client[%s] and server[%s] mismatch\n",
4551 client_signing, server_signing));
4552 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4553 return;
4556 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4557 DATA_BLOB blob1;
4558 uint8_t key_len;
4559 time_t t;
4561 if (wct != 0x0D) {
4562 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4563 return;
4566 server_security_mode = SVAL(vwv + 1, 0);
4567 server_max_xmit = SVAL(vwv + 2, 0);
4568 server_max_mux = SVAL(vwv + 3, 0);
4569 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4570 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4571 server_session_key = IVAL(vwv + 6, 0);
4572 server_time_zone = SVALS(vwv + 10, 0);
4573 server_time_zone *= 60;
4574 /* this time is converted to GMT by make_unix_date */
4575 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4576 unix_to_nt_time(&server_system_time, t);
4577 key_len = SVAL(vwv + 11, 0);
4579 if (num_bytes < key_len) {
4580 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4581 return;
4584 if (key_len != 0 && key_len != 8) {
4585 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4586 return;
4589 if (key_len == 8) {
4590 memcpy(server_challenge, bytes, 8);
4593 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4594 if (blob1.length > 0) {
4595 size_t len;
4596 bool ok;
4598 len = utf16_len_n(blob1.data,
4599 blob1.length);
4600 blob1.length = len;
4602 ok = convert_string_talloc(state,
4603 CH_DOS,
4604 CH_UNIX,
4605 blob1.data,
4606 blob1.length,
4607 &server_workgroup,
4608 &len);
4609 if (!ok) {
4610 status = map_nt_error_from_unix_common(errno);
4611 tevent_req_nterror(req, status);
4612 return;
4616 } else {
4617 /* the old core protocol */
4618 server_time_zone = get_time_zone(time(NULL));
4619 server_max_xmit = 1024;
4620 server_max_mux = 1;
4623 if (server_max_xmit < 1024) {
4624 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4625 return;
4628 if (server_max_mux < 1) {
4629 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4630 return;
4634 * Now calculate the negotiated capabilities
4635 * based on the mask for:
4636 * - client only flags
4637 * - flags used in both directions
4638 * - server only flags
4640 both_capabilities = client_capabilities & server_capabilities;
4641 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4642 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4643 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4645 max_xmit = MIN(client_max_xmit, server_max_xmit);
4647 conn->smb1.server.capabilities = server_capabilities;
4648 conn->smb1.capabilities = capabilities;
4650 conn->smb1.server.max_xmit = server_max_xmit;
4651 conn->smb1.max_xmit = max_xmit;
4653 conn->smb1.server.max_mux = server_max_mux;
4655 conn->smb1.server.security_mode = server_security_mode;
4657 conn->smb1.server.readbraw = server_readbraw;
4658 conn->smb1.server.writebraw = server_writebraw;
4659 conn->smb1.server.lockread = server_lockread;
4660 conn->smb1.server.writeunlock = server_writeunlock;
4662 conn->smb1.server.session_key = server_session_key;
4664 talloc_steal(conn, server_gss_blob.data);
4665 conn->smb1.server.gss_blob = server_gss_blob;
4666 conn->smb1.server.guid = server_guid;
4667 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4668 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4669 conn->smb1.server.name = talloc_move(conn, &server_name);
4671 conn->smb1.server.time_zone = server_time_zone;
4672 conn->smb1.server.system_time = server_system_time;
4674 tevent_req_done(req);
4677 static size_t smbXcli_padding_helper(uint32_t offset, size_t n)
4679 if ((offset & (n-1)) == 0) return 0;
4680 return n - (offset & (n-1));
4683 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4685 size_t i;
4686 uint8_t *buf;
4687 uint16_t dialect_count = 0;
4688 DATA_BLOB dyn = data_blob_null;
4690 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4691 bool ok;
4692 uint8_t val[2];
4694 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4695 continue;
4698 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4699 continue;
4702 SSVAL(val, 0, smb2cli_prots[i].smb2_dialect);
4704 ok = data_blob_append(state, &dyn, val, sizeof(val));
4705 if (!ok) {
4706 return NULL;
4709 dialect_count++;
4712 buf = state->smb2.fixed;
4713 SSVAL(buf, 0, 36);
4714 SSVAL(buf, 2, dialect_count);
4715 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4716 SSVAL(buf, 6, 0); /* Reserved */
4717 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
4718 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4719 } else {
4720 SIVAL(buf, 8, 0); /* Capabilities */
4722 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4723 NTSTATUS status;
4724 DATA_BLOB blob;
4726 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4727 state, &blob);
4728 if (!NT_STATUS_IS_OK(status)) {
4729 return NULL;
4731 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4732 } else {
4733 memset(buf+12, 0, 16); /* ClientGuid */
4736 if (state->conn->max_protocol >= PROTOCOL_SMB3_10) {
4737 NTSTATUS status;
4738 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4739 uint32_t offset;
4740 DATA_BLOB b;
4741 uint8_t p[38];
4742 const uint8_t zeros[8] = {0, };
4743 size_t pad;
4744 bool ok;
4746 SSVAL(p, 0, 1); /* HashAlgorithmCount */
4747 SSVAL(p, 2, 32); /* SaltLength */
4748 SSVAL(p, 4, SMB2_PREAUTH_INTEGRITY_SHA512);
4749 generate_random_buffer(p + 6, 32);
4751 b = data_blob_const(p, 38);
4752 status = smb2_negotiate_context_add(state, &c,
4753 SMB2_PREAUTH_INTEGRITY_CAPABILITIES, b);
4754 if (!NT_STATUS_IS_OK(status)) {
4755 return NULL;
4758 SSVAL(p, 0, 2); /* ChiperCount */
4760 * For now we preferr CCM because our implementation
4761 * is faster than GCM, see bug #11451.
4763 SSVAL(p, 2, SMB2_ENCRYPTION_AES128_CCM);
4764 SSVAL(p, 4, SMB2_ENCRYPTION_AES128_GCM);
4766 b = data_blob_const(p, 6);
4767 status = smb2_negotiate_context_add(state, &c,
4768 SMB2_ENCRYPTION_CAPABILITIES, b);
4769 if (!NT_STATUS_IS_OK(status)) {
4770 return NULL;
4773 status = smb2_negotiate_context_push(state, &b, c);
4774 if (!NT_STATUS_IS_OK(status)) {
4775 return NULL;
4778 offset = SMB2_HDR_BODY + sizeof(state->smb2.fixed) + dyn.length;
4779 pad = smbXcli_padding_helper(offset, 8);
4781 ok = data_blob_append(state, &dyn, zeros, pad);
4782 if (!ok) {
4783 return NULL;
4785 offset += pad;
4787 ok = data_blob_append(state, &dyn, b.data, b.length);
4788 if (!ok) {
4789 return NULL;
4792 SIVAL(buf, 28, offset); /* NegotiateContextOffset */
4793 SSVAL(buf, 32, c.num_contexts); /* NegotiateContextCount */
4794 SSVAL(buf, 34, 0); /* Reserved */
4795 } else {
4796 SBVAL(buf, 28, 0); /* Reserved/ClientStartTime */
4799 return smb2cli_req_send(state, state->ev,
4800 state->conn, SMB2_OP_NEGPROT,
4801 0, 0, /* flags */
4802 state->timeout_msec,
4803 NULL, NULL, /* tcon, session */
4804 state->smb2.fixed, sizeof(state->smb2.fixed),
4805 dyn.data, dyn.length,
4806 UINT16_MAX); /* max_dyn_len */
4809 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4811 struct tevent_req *req =
4812 tevent_req_callback_data(subreq,
4813 struct tevent_req);
4814 struct smbXcli_negprot_state *state =
4815 tevent_req_data(req,
4816 struct smbXcli_negprot_state);
4817 struct smbXcli_conn *conn = state->conn;
4818 size_t security_offset, security_length;
4819 DATA_BLOB blob;
4820 NTSTATUS status;
4821 struct iovec *iov;
4822 uint8_t *body;
4823 size_t i;
4824 uint16_t dialect_revision;
4825 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4826 uint32_t negotiate_context_offset = 0;
4827 uint16_t negotiate_context_count = 0;
4828 DATA_BLOB negotiate_context_blob = data_blob_null;
4829 size_t avail;
4830 size_t ctx_ofs;
4831 size_t needed;
4832 struct smb2_negotiate_context *preauth = NULL;
4833 uint16_t hash_count;
4834 uint16_t salt_length;
4835 uint16_t hash_selected;
4836 struct hc_sha512state sctx;
4837 struct smb2_negotiate_context *cipher = NULL;
4838 struct iovec sent_iov[3];
4839 static const struct smb2cli_req_expected_response expected[] = {
4841 .status = NT_STATUS_OK,
4842 .body_size = 0x41
4846 status = smb2cli_req_recv(subreq, state, &iov,
4847 expected, ARRAY_SIZE(expected));
4848 if (tevent_req_nterror(req, status)) {
4849 return;
4852 body = (uint8_t *)iov[1].iov_base;
4854 dialect_revision = SVAL(body, 4);
4856 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4857 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4858 continue;
4861 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4862 continue;
4865 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4866 continue;
4869 conn->protocol = smb2cli_prots[i].proto;
4870 break;
4873 if (conn->protocol == PROTOCOL_NONE) {
4874 TALLOC_FREE(subreq);
4876 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
4877 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4878 return;
4881 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4882 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4883 return;
4886 /* make sure we do not loop forever */
4887 state->conn->min_protocol = PROTOCOL_SMB2_02;
4890 * send a SMB2 negprot, in order to negotiate
4891 * the SMB2 dialect.
4893 subreq = smbXcli_negprot_smb2_subreq(state);
4894 if (tevent_req_nomem(subreq, req)) {
4895 return;
4897 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4898 return;
4901 conn->smb2.server.security_mode = SVAL(body, 2);
4902 if (conn->protocol >= PROTOCOL_SMB3_10) {
4903 negotiate_context_count = SVAL(body, 6);
4906 blob = data_blob_const(body + 8, 16);
4907 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4908 if (tevent_req_nterror(req, status)) {
4909 return;
4912 conn->smb2.server.capabilities = IVAL(body, 24);
4913 conn->smb2.server.max_trans_size= IVAL(body, 28);
4914 conn->smb2.server.max_read_size = IVAL(body, 32);
4915 conn->smb2.server.max_write_size= IVAL(body, 36);
4916 conn->smb2.server.system_time = BVAL(body, 40);
4917 conn->smb2.server.start_time = BVAL(body, 48);
4919 security_offset = SVAL(body, 56);
4920 security_length = SVAL(body, 58);
4922 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4923 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4924 return;
4927 if (security_length > iov[2].iov_len) {
4928 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4929 return;
4932 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4933 iov[2].iov_base,
4934 security_length);
4935 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4936 return;
4939 if (conn->protocol < PROTOCOL_SMB3_10) {
4940 TALLOC_FREE(subreq);
4942 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4943 conn->smb2.server.cipher = SMB2_ENCRYPTION_AES128_CCM;
4945 tevent_req_done(req);
4946 return;
4950 * Here we are now at SMB3_11, so encryption should be
4951 * negotiated via context, not capabilities.
4954 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4956 * Server set SMB2_CAP_ENCRYPTION capability,
4957 * but *SHOULD* not, not *MUST* not. Just mask it off.
4958 * NetApp seems to do this:
4959 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13009
4961 conn->smb2.server.capabilities &= ~SMB2_CAP_ENCRYPTION;
4964 negotiate_context_offset = IVAL(body, 60);
4965 if (negotiate_context_offset < security_offset) {
4966 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4967 return;
4970 ctx_ofs = negotiate_context_offset - security_offset;
4971 if (ctx_ofs > iov[2].iov_len) {
4972 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4973 return;
4975 avail = iov[2].iov_len - security_length;
4976 needed = iov[2].iov_len - ctx_ofs;
4977 if (needed > avail) {
4978 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4979 return;
4982 negotiate_context_blob.data = (uint8_t *)iov[2].iov_base;
4983 negotiate_context_blob.length = iov[2].iov_len;
4985 negotiate_context_blob.data += ctx_ofs;
4986 negotiate_context_blob.length -= ctx_ofs;
4988 status = smb2_negotiate_context_parse(state, negotiate_context_blob, &c);
4989 if (tevent_req_nterror(req, status)) {
4990 return;
4993 if (negotiate_context_count != c.num_contexts) {
4994 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4995 return;
4998 preauth = smb2_negotiate_context_find(&c,
4999 SMB2_PREAUTH_INTEGRITY_CAPABILITIES);
5000 if (preauth == NULL) {
5001 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5002 return;
5005 if (preauth->data.length < 6) {
5006 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5007 return;
5010 hash_count = SVAL(preauth->data.data, 0);
5011 salt_length = SVAL(preauth->data.data, 2);
5012 hash_selected = SVAL(preauth->data.data, 4);
5014 if (hash_count != 1) {
5015 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5016 return;
5019 if (preauth->data.length != (6 + salt_length)) {
5020 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5021 return;
5024 if (hash_selected != SMB2_PREAUTH_INTEGRITY_SHA512) {
5025 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5026 return;
5029 cipher = smb2_negotiate_context_find(&c, SMB2_ENCRYPTION_CAPABILITIES);
5030 if (cipher != NULL) {
5031 uint16_t cipher_count;
5033 if (cipher->data.length < 2) {
5034 tevent_req_nterror(req,
5035 NT_STATUS_INVALID_NETWORK_RESPONSE);
5036 return;
5039 cipher_count = SVAL(cipher->data.data, 0);
5041 if (cipher_count > 1) {
5042 tevent_req_nterror(req,
5043 NT_STATUS_INVALID_NETWORK_RESPONSE);
5044 return;
5047 if (cipher->data.length != (2 + 2 * cipher_count)) {
5048 tevent_req_nterror(req,
5049 NT_STATUS_INVALID_NETWORK_RESPONSE);
5050 return;
5053 if (cipher_count == 1) {
5054 uint16_t cipher_selected;
5056 cipher_selected = SVAL(cipher->data.data, 2);
5058 switch (cipher_selected) {
5059 case SMB2_ENCRYPTION_AES128_GCM:
5060 case SMB2_ENCRYPTION_AES128_CCM:
5061 conn->smb2.server.cipher = cipher_selected;
5062 break;
5067 /* First we hash the request */
5068 smb2cli_req_get_sent_iov(subreq, sent_iov);
5069 samba_SHA512_Init(&sctx);
5070 samba_SHA512_Update(&sctx, conn->smb2.preauth_sha512,
5071 sizeof(conn->smb2.preauth_sha512));
5072 for (i = 0; i < 3; i++) {
5073 samba_SHA512_Update(&sctx, sent_iov[i].iov_base, sent_iov[i].iov_len);
5075 samba_SHA512_Final(conn->smb2.preauth_sha512, &sctx);
5076 TALLOC_FREE(subreq);
5078 /* And now we hash the response */
5079 samba_SHA512_Init(&sctx);
5080 samba_SHA512_Update(&sctx, conn->smb2.preauth_sha512,
5081 sizeof(conn->smb2.preauth_sha512));
5082 for (i = 0; i < 3; i++) {
5083 samba_SHA512_Update(&sctx, iov[i].iov_base, iov[i].iov_len);
5085 samba_SHA512_Final(conn->smb2.preauth_sha512, &sctx);
5087 tevent_req_done(req);
5090 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
5091 TALLOC_CTX *tmp_mem,
5092 uint8_t *inbuf)
5094 size_t num_pending = talloc_array_length(conn->pending);
5095 struct tevent_req *subreq;
5096 struct smbXcli_req_state *substate;
5097 struct tevent_req *req;
5098 uint32_t protocol_magic;
5099 size_t inbuf_len = smb_len_nbt(inbuf);
5101 if (num_pending != 1) {
5102 return NT_STATUS_INTERNAL_ERROR;
5105 if (inbuf_len < 4) {
5106 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5109 subreq = conn->pending[0];
5110 substate = tevent_req_data(subreq, struct smbXcli_req_state);
5111 req = tevent_req_callback_data(subreq, struct tevent_req);
5113 protocol_magic = IVAL(inbuf, 4);
5115 switch (protocol_magic) {
5116 case SMB_MAGIC:
5117 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
5118 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
5119 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
5121 case SMB2_MAGIC:
5122 if (substate->smb2.recv_iov == NULL) {
5124 * For the SMB1 negprot we have move it.
5126 substate->smb2.recv_iov = substate->smb1.recv_iov;
5127 substate->smb1.recv_iov = NULL;
5131 * we got an SMB2 answer, which consumed sequence number 0
5132 * so we need to use 1 as the next one.
5134 * we also need to set the current credits to 0
5135 * as we consumed the initial one. The SMB2 answer
5136 * hopefully grant us a new credit.
5138 conn->smb2.mid = 1;
5139 conn->smb2.cur_credits = 0;
5140 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
5141 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
5142 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
5145 DEBUG(10, ("Got non-SMB PDU\n"));
5146 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5149 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
5151 return tevent_req_simple_recv_ntstatus(req);
5154 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
5155 uint32_t timeout_msec,
5156 enum protocol_types min_protocol,
5157 enum protocol_types max_protocol)
5159 TALLOC_CTX *frame = talloc_stackframe();
5160 struct tevent_context *ev;
5161 struct tevent_req *req;
5162 NTSTATUS status = NT_STATUS_NO_MEMORY;
5163 bool ok;
5165 if (smbXcli_conn_has_async_calls(conn)) {
5167 * Can't use sync call while an async call is in flight
5169 status = NT_STATUS_INVALID_PARAMETER_MIX;
5170 goto fail;
5172 ev = samba_tevent_context_init(frame);
5173 if (ev == NULL) {
5174 goto fail;
5176 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
5177 min_protocol, max_protocol,
5178 WINDOWS_CLIENT_PURE_SMB2_NEGPROT_INITIAL_CREDIT_ASK);
5179 if (req == NULL) {
5180 goto fail;
5182 ok = tevent_req_poll_ntstatus(req, ev, &status);
5183 if (!ok) {
5184 goto fail;
5186 status = smbXcli_negprot_recv(req);
5187 fail:
5188 TALLOC_FREE(frame);
5189 return status;
5192 struct smb2cli_validate_negotiate_info_state {
5193 struct smbXcli_conn *conn;
5194 DATA_BLOB in_input_buffer;
5195 DATA_BLOB in_output_buffer;
5196 DATA_BLOB out_input_buffer;
5197 DATA_BLOB out_output_buffer;
5198 uint16_t dialect;
5201 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq);
5203 struct tevent_req *smb2cli_validate_negotiate_info_send(TALLOC_CTX *mem_ctx,
5204 struct tevent_context *ev,
5205 struct smbXcli_conn *conn,
5206 uint32_t timeout_msec,
5207 struct smbXcli_session *session,
5208 struct smbXcli_tcon *tcon)
5210 struct tevent_req *req;
5211 struct smb2cli_validate_negotiate_info_state *state;
5212 uint8_t *buf;
5213 uint16_t dialect_count = 0;
5214 struct tevent_req *subreq;
5215 bool _save_should_sign;
5216 size_t i;
5218 req = tevent_req_create(mem_ctx, &state,
5219 struct smb2cli_validate_negotiate_info_state);
5220 if (req == NULL) {
5221 return NULL;
5223 state->conn = conn;
5225 state->in_input_buffer = data_blob_talloc_zero(state,
5226 4 + 16 + 1 + 1 + 2);
5227 if (tevent_req_nomem(state->in_input_buffer.data, req)) {
5228 return tevent_req_post(req, ev);
5230 buf = state->in_input_buffer.data;
5232 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
5233 SIVAL(buf, 0, conn->smb2.client.capabilities);
5234 } else {
5235 SIVAL(buf, 0, 0); /* Capabilities */
5237 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
5238 NTSTATUS status;
5239 DATA_BLOB blob;
5241 status = GUID_to_ndr_blob(&conn->smb2.client.guid,
5242 state, &blob);
5243 if (!NT_STATUS_IS_OK(status)) {
5244 return NULL;
5246 memcpy(buf+4, blob.data, 16); /* ClientGuid */
5247 } else {
5248 memset(buf+4, 0, 16); /* ClientGuid */
5250 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
5251 SCVAL(buf, 20, conn->smb2.client.security_mode);
5252 } else {
5253 SCVAL(buf, 20, 0);
5255 SCVAL(buf, 21, 0); /* reserved */
5257 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
5258 bool ok;
5259 size_t ofs;
5261 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
5262 continue;
5265 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
5266 continue;
5269 if (smb2cli_prots[i].proto == state->conn->protocol) {
5270 state->dialect = smb2cli_prots[i].smb2_dialect;
5273 ofs = state->in_input_buffer.length;
5274 ok = data_blob_realloc(state, &state->in_input_buffer,
5275 ofs + 2);
5276 if (!ok) {
5277 tevent_req_oom(req);
5278 return tevent_req_post(req, ev);
5281 buf = state->in_input_buffer.data;
5282 SSVAL(buf, ofs, smb2cli_prots[i].smb2_dialect);
5284 dialect_count++;
5286 buf = state->in_input_buffer.data;
5287 SSVAL(buf, 22, dialect_count);
5289 _save_should_sign = smb2cli_tcon_is_signing_on(tcon);
5290 smb2cli_tcon_should_sign(tcon, true);
5291 subreq = smb2cli_ioctl_send(state, ev, conn,
5292 timeout_msec, session, tcon,
5293 UINT64_MAX, /* in_fid_persistent */
5294 UINT64_MAX, /* in_fid_volatile */
5295 FSCTL_VALIDATE_NEGOTIATE_INFO,
5296 0, /* in_max_input_length */
5297 &state->in_input_buffer,
5298 24, /* in_max_output_length */
5299 &state->in_output_buffer,
5300 SMB2_IOCTL_FLAG_IS_FSCTL);
5301 smb2cli_tcon_should_sign(tcon, _save_should_sign);
5302 if (tevent_req_nomem(subreq, req)) {
5303 return tevent_req_post(req, ev);
5305 tevent_req_set_callback(subreq,
5306 smb2cli_validate_negotiate_info_done,
5307 req);
5309 return req;
5312 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq)
5314 struct tevent_req *req =
5315 tevent_req_callback_data(subreq,
5316 struct tevent_req);
5317 struct smb2cli_validate_negotiate_info_state *state =
5318 tevent_req_data(req,
5319 struct smb2cli_validate_negotiate_info_state);
5320 NTSTATUS status;
5321 const uint8_t *buf;
5322 uint32_t capabilities;
5323 DATA_BLOB guid_blob;
5324 struct GUID server_guid;
5325 uint16_t security_mode;
5326 uint16_t dialect;
5328 status = smb2cli_ioctl_recv(subreq, state,
5329 &state->out_input_buffer,
5330 &state->out_output_buffer);
5331 TALLOC_FREE(subreq);
5332 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
5334 * The response was signed, but not supported
5336 * Older Windows and Samba releases return
5337 * NT_STATUS_FILE_CLOSED.
5339 tevent_req_done(req);
5340 return;
5342 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_DEVICE_REQUEST)) {
5344 * The response was signed, but not supported
5346 * This is returned by the NTVFS based Samba 4.x file server
5347 * for file shares.
5349 tevent_req_done(req);
5350 return;
5352 if (NT_STATUS_EQUAL(status, NT_STATUS_FS_DRIVER_REQUIRED)) {
5354 * The response was signed, but not supported
5356 * This is returned by the NTVFS based Samba 4.x file server
5357 * for ipc shares.
5359 tevent_req_done(req);
5360 return;
5362 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
5364 * The response was signed, but not supported
5366 * This might be returned by older Windows versions or by
5367 * NetApp SMB server implementations.
5369 * See
5371 * https://blogs.msdn.microsoft.com/openspecification/2012/06/28/smb3-secure-dialect-negotiation/
5374 tevent_req_done(req);
5375 return;
5377 if (tevent_req_nterror(req, status)) {
5378 return;
5381 if (state->out_output_buffer.length != 24) {
5382 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5383 return;
5386 buf = state->out_output_buffer.data;
5388 capabilities = IVAL(buf, 0);
5389 guid_blob = data_blob_const(buf + 4, 16);
5390 status = GUID_from_data_blob(&guid_blob, &server_guid);
5391 if (tevent_req_nterror(req, status)) {
5392 return;
5394 security_mode = CVAL(buf, 20);
5395 dialect = SVAL(buf, 22);
5397 if (capabilities != state->conn->smb2.server.capabilities) {
5398 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5399 return;
5402 if (!GUID_equal(&server_guid, &state->conn->smb2.server.guid)) {
5403 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5404 return;
5407 if (security_mode != state->conn->smb2.server.security_mode) {
5408 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5409 return;
5412 if (dialect != state->dialect) {
5413 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5414 return;
5417 tevent_req_done(req);
5420 NTSTATUS smb2cli_validate_negotiate_info_recv(struct tevent_req *req)
5422 return tevent_req_simple_recv_ntstatus(req);
5425 static int smbXcli_session_destructor(struct smbXcli_session *session)
5427 if (session->conn == NULL) {
5428 return 0;
5431 DLIST_REMOVE(session->conn->sessions, session);
5432 return 0;
5435 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
5436 struct smbXcli_conn *conn)
5438 struct smbXcli_session *session;
5440 session = talloc_zero(mem_ctx, struct smbXcli_session);
5441 if (session == NULL) {
5442 return NULL;
5444 session->smb2 = talloc_zero(session, struct smb2cli_session);
5445 if (session->smb2 == NULL) {
5446 talloc_free(session);
5447 return NULL;
5449 talloc_set_destructor(session, smbXcli_session_destructor);
5451 DLIST_ADD_END(conn->sessions, session);
5452 session->conn = conn;
5454 memcpy(session->smb2_channel.preauth_sha512,
5455 conn->smb2.preauth_sha512,
5456 sizeof(session->smb2_channel.preauth_sha512));
5458 return session;
5461 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
5462 struct smbXcli_session *src)
5464 struct smbXcli_session *session;
5466 session = talloc_zero(mem_ctx, struct smbXcli_session);
5467 if (session == NULL) {
5468 return NULL;
5470 session->smb2 = talloc_zero(session, struct smb2cli_session);
5471 if (session->smb2 == NULL) {
5472 talloc_free(session);
5473 return NULL;
5476 session->conn = src->conn;
5477 *session->smb2 = *src->smb2;
5478 session->smb2_channel = src->smb2_channel;
5479 session->disconnect_expired = src->disconnect_expired;
5481 DLIST_ADD_END(src->conn->sessions, session);
5482 talloc_set_destructor(session, smbXcli_session_destructor);
5484 return session;
5487 bool smbXcli_session_is_guest(struct smbXcli_session *session)
5489 if (session == NULL) {
5490 return false;
5493 if (session->conn == NULL) {
5494 return false;
5497 if (session->conn->mandatory_signing) {
5498 return false;
5501 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5502 if (session->smb2->session_flags & SMB2_SESSION_FLAG_IS_GUEST) {
5503 return true;
5505 return false;
5508 if (session->smb1.action & SMB_SETUP_GUEST) {
5509 return true;
5512 return false;
5515 bool smbXcli_session_is_authenticated(struct smbXcli_session *session)
5517 const DATA_BLOB *application_key;
5519 if (session == NULL) {
5520 return false;
5523 if (session->conn == NULL) {
5524 return false;
5528 * If we have an application key we had a session key negotiated
5529 * at auth time.
5531 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5532 application_key = &session->smb2->application_key;
5533 } else {
5534 application_key = &session->smb1.application_key;
5537 if (application_key->length == 0) {
5538 return false;
5541 return true;
5544 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
5545 TALLOC_CTX *mem_ctx,
5546 DATA_BLOB *key)
5548 const DATA_BLOB *application_key;
5550 *key = data_blob_null;
5552 if (session->conn == NULL) {
5553 return NT_STATUS_NO_USER_SESSION_KEY;
5556 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5557 application_key = &session->smb2->application_key;
5558 } else {
5559 application_key = &session->smb1.application_key;
5562 if (application_key->length == 0) {
5563 return NT_STATUS_NO_USER_SESSION_KEY;
5566 *key = data_blob_dup_talloc(mem_ctx, *application_key);
5567 if (key->data == NULL) {
5568 return NT_STATUS_NO_MEMORY;
5571 return NT_STATUS_OK;
5574 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
5576 session->disconnect_expired = true;
5579 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
5581 return session->smb1.session_id;
5584 void smb1cli_session_set_id(struct smbXcli_session *session,
5585 uint16_t session_id)
5587 session->smb1.session_id = session_id;
5590 void smb1cli_session_set_action(struct smbXcli_session *session,
5591 uint16_t action)
5593 session->smb1.action = action;
5596 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
5597 const DATA_BLOB _session_key)
5599 struct smbXcli_conn *conn = session->conn;
5600 uint8_t session_key[16];
5602 if (conn == NULL) {
5603 return NT_STATUS_INVALID_PARAMETER_MIX;
5606 if (session->smb1.application_key.length != 0) {
5608 * TODO: do not allow this...
5610 * return NT_STATUS_INVALID_PARAMETER_MIX;
5612 data_blob_clear_free(&session->smb1.application_key);
5613 session->smb1.protected_key = false;
5616 if (_session_key.length == 0) {
5617 return NT_STATUS_OK;
5620 ZERO_STRUCT(session_key);
5621 memcpy(session_key, _session_key.data,
5622 MIN(_session_key.length, sizeof(session_key)));
5624 session->smb1.application_key = data_blob_talloc(session,
5625 session_key,
5626 sizeof(session_key));
5627 ZERO_STRUCT(session_key);
5628 if (session->smb1.application_key.data == NULL) {
5629 return NT_STATUS_NO_MEMORY;
5632 session->smb1.protected_key = false;
5634 return NT_STATUS_OK;
5637 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
5639 if (session->smb1.protected_key) {
5640 /* already protected */
5641 return NT_STATUS_OK;
5644 if (session->smb1.application_key.length != 16) {
5645 return NT_STATUS_INVALID_PARAMETER_MIX;
5648 smb_key_derivation(session->smb1.application_key.data,
5649 session->smb1.application_key.length,
5650 session->smb1.application_key.data);
5652 session->smb1.protected_key = true;
5654 return NT_STATUS_OK;
5657 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
5659 struct smbXcli_conn *conn = session->conn;
5660 uint8_t security_mode = 0;
5662 if (conn == NULL) {
5663 return security_mode;
5666 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
5667 if (conn->mandatory_signing) {
5668 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
5670 if (session->smb2->should_sign) {
5671 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
5674 return security_mode;
5677 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
5679 return session->smb2->session_id;
5682 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
5684 return session->smb2->session_flags;
5687 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
5688 uint64_t session_id,
5689 uint16_t session_flags)
5691 session->smb2->session_id = session_id;
5692 session->smb2->session_flags = session_flags;
5695 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
5697 session->smb2->channel_sequence += 1;
5700 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session *session,
5701 uint16_t channel_sequence)
5703 uint16_t prev_cs;
5705 prev_cs = session->smb2->channel_sequence;
5706 session->smb2->channel_sequence = channel_sequence;
5708 return prev_cs;
5711 uint16_t smb2cli_session_current_channel_sequence(struct smbXcli_session *session)
5713 return session->smb2->channel_sequence;
5716 void smb2cli_session_start_replay(struct smbXcli_session *session)
5718 session->smb2->replay_active = true;
5721 void smb2cli_session_stop_replay(struct smbXcli_session *session)
5723 session->smb2->replay_active = false;
5726 NTSTATUS smb2cli_session_update_preauth(struct smbXcli_session *session,
5727 const struct iovec *iov)
5729 struct hc_sha512state sctx;
5730 size_t i;
5732 if (session->conn == NULL) {
5733 return NT_STATUS_INTERNAL_ERROR;
5736 if (session->conn->protocol < PROTOCOL_SMB3_10) {
5737 return NT_STATUS_OK;
5740 if (session->smb2_channel.signing_key.length != 0) {
5741 return NT_STATUS_OK;
5744 samba_SHA512_Init(&sctx);
5745 samba_SHA512_Update(&sctx, session->smb2_channel.preauth_sha512,
5746 sizeof(session->smb2_channel.preauth_sha512));
5747 for (i = 0; i < 3; i++) {
5748 samba_SHA512_Update(&sctx, iov[i].iov_base, iov[i].iov_len);
5750 samba_SHA512_Final(session->smb2_channel.preauth_sha512, &sctx);
5752 return NT_STATUS_OK;
5755 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
5756 const DATA_BLOB _session_key,
5757 const struct iovec *recv_iov)
5759 struct smbXcli_conn *conn = session->conn;
5760 uint16_t no_sign_flags = 0;
5761 uint8_t session_key[16];
5762 bool check_signature = true;
5763 uint32_t hdr_flags;
5764 NTSTATUS status;
5765 struct _derivation {
5766 DATA_BLOB label;
5767 DATA_BLOB context;
5769 struct {
5770 struct _derivation signing;
5771 struct _derivation encryption;
5772 struct _derivation decryption;
5773 struct _derivation application;
5774 } derivation = { };
5775 size_t nonce_size = 0;
5777 if (conn == NULL) {
5778 return NT_STATUS_INVALID_PARAMETER_MIX;
5781 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
5782 return NT_STATUS_INVALID_PARAMETER_MIX;
5785 if (!conn->mandatory_signing) {
5787 * only allow guest sessions without
5788 * mandatory signing.
5790 * If we try an authentication with username != ""
5791 * and the server let us in without verifying the
5792 * password we don't have a negotiated session key
5793 * for signing.
5795 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST;
5798 if (session->smb2->session_flags & no_sign_flags) {
5799 session->smb2->should_sign = false;
5800 return NT_STATUS_OK;
5803 if (session->smb2->signing_key.length != 0) {
5804 return NT_STATUS_INVALID_PARAMETER_MIX;
5807 if (conn->protocol >= PROTOCOL_SMB3_10) {
5808 struct _derivation *d;
5809 DATA_BLOB p;
5811 p = data_blob_const(session->smb2_channel.preauth_sha512,
5812 sizeof(session->smb2_channel.preauth_sha512));
5814 d = &derivation.signing;
5815 d->label = data_blob_string_const_null("SMBSigningKey");
5816 d->context = p;
5818 d = &derivation.encryption;
5819 d->label = data_blob_string_const_null("SMBC2SCipherKey");
5820 d->context = p;
5822 d = &derivation.decryption;
5823 d->label = data_blob_string_const_null("SMBS2CCipherKey");
5824 d->context = p;
5826 d = &derivation.application;
5827 d->label = data_blob_string_const_null("SMBAppKey");
5828 d->context = p;
5830 } else if (conn->protocol >= PROTOCOL_SMB2_24) {
5831 struct _derivation *d;
5833 d = &derivation.signing;
5834 d->label = data_blob_string_const_null("SMB2AESCMAC");
5835 d->context = data_blob_string_const_null("SmbSign");
5837 d = &derivation.encryption;
5838 d->label = data_blob_string_const_null("SMB2AESCCM");
5839 d->context = data_blob_string_const_null("ServerIn ");
5841 d = &derivation.decryption;
5842 d->label = data_blob_string_const_null("SMB2AESCCM");
5843 d->context = data_blob_string_const_null("ServerOut");
5845 d = &derivation.application;
5846 d->label = data_blob_string_const_null("SMB2APP");
5847 d->context = data_blob_string_const_null("SmbRpc");
5850 ZERO_STRUCT(session_key);
5851 memcpy(session_key, _session_key.data,
5852 MIN(_session_key.length, sizeof(session_key)));
5854 session->smb2->signing_key = data_blob_talloc(session,
5855 session_key,
5856 sizeof(session_key));
5857 if (session->smb2->signing_key.data == NULL) {
5858 ZERO_STRUCT(session_key);
5859 return NT_STATUS_NO_MEMORY;
5862 if (conn->protocol >= PROTOCOL_SMB2_24) {
5863 struct _derivation *d = &derivation.signing;
5865 smb2_key_derivation(session_key, sizeof(session_key),
5866 d->label.data, d->label.length,
5867 d->context.data, d->context.length,
5868 session->smb2->signing_key.data);
5871 session->smb2->encryption_key = data_blob_dup_talloc(session,
5872 session->smb2->signing_key);
5873 if (session->smb2->encryption_key.data == NULL) {
5874 ZERO_STRUCT(session_key);
5875 return NT_STATUS_NO_MEMORY;
5878 if (conn->protocol >= PROTOCOL_SMB2_24) {
5879 struct _derivation *d = &derivation.encryption;
5881 smb2_key_derivation(session_key, sizeof(session_key),
5882 d->label.data, d->label.length,
5883 d->context.data, d->context.length,
5884 session->smb2->encryption_key.data);
5887 session->smb2->decryption_key = data_blob_dup_talloc(session,
5888 session->smb2->signing_key);
5889 if (session->smb2->decryption_key.data == NULL) {
5890 ZERO_STRUCT(session_key);
5891 return NT_STATUS_NO_MEMORY;
5894 if (conn->protocol >= PROTOCOL_SMB2_24) {
5895 struct _derivation *d = &derivation.decryption;
5897 smb2_key_derivation(session_key, sizeof(session_key),
5898 d->label.data, d->label.length,
5899 d->context.data, d->context.length,
5900 session->smb2->decryption_key.data);
5903 session->smb2->application_key = data_blob_dup_talloc(session,
5904 session->smb2->signing_key);
5905 if (session->smb2->application_key.data == NULL) {
5906 ZERO_STRUCT(session_key);
5907 return NT_STATUS_NO_MEMORY;
5910 if (conn->protocol >= PROTOCOL_SMB2_24) {
5911 struct _derivation *d = &derivation.application;
5913 smb2_key_derivation(session_key, sizeof(session_key),
5914 d->label.data, d->label.length,
5915 d->context.data, d->context.length,
5916 session->smb2->application_key.data);
5918 ZERO_STRUCT(session_key);
5920 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
5921 session->smb2->signing_key);
5922 if (session->smb2_channel.signing_key.data == NULL) {
5923 return NT_STATUS_NO_MEMORY;
5926 check_signature = conn->mandatory_signing;
5928 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
5929 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
5931 * Sadly some vendors don't sign the
5932 * final SMB2 session setup response
5934 * At least Windows and Samba are always doing this
5935 * if there's a session key available.
5937 * We only check the signature if it's mandatory
5938 * or SMB2_HDR_FLAG_SIGNED is provided.
5940 check_signature = true;
5943 if (conn->protocol >= PROTOCOL_SMB3_10) {
5944 check_signature = true;
5947 if (check_signature) {
5948 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5949 session->conn->protocol,
5950 recv_iov, 3);
5951 if (!NT_STATUS_IS_OK(status)) {
5952 return status;
5956 session->smb2->should_sign = false;
5957 session->smb2->should_encrypt = false;
5959 if (conn->desire_signing) {
5960 session->smb2->should_sign = true;
5963 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
5964 session->smb2->should_sign = true;
5967 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
5968 session->smb2->should_encrypt = true;
5971 if (conn->protocol < PROTOCOL_SMB2_24) {
5972 session->smb2->should_encrypt = false;
5975 if (conn->smb2.server.cipher == 0) {
5976 session->smb2->should_encrypt = false;
5980 * CCM and GCM algorithms must never have their
5981 * nonce wrap, or the security of the whole
5982 * communication and the keys is destroyed.
5983 * We must drop the connection once we have
5984 * transfered too much data.
5986 * NOTE: We assume nonces greater than 8 bytes.
5988 generate_random_buffer((uint8_t *)&session->smb2->nonce_high_random,
5989 sizeof(session->smb2->nonce_high_random));
5990 switch (conn->smb2.server.cipher) {
5991 case SMB2_ENCRYPTION_AES128_CCM:
5992 nonce_size = AES_CCM_128_NONCE_SIZE;
5993 break;
5994 case SMB2_ENCRYPTION_AES128_GCM:
5995 nonce_size = AES_GCM_128_IV_SIZE;
5996 break;
5997 default:
5998 nonce_size = 0;
5999 break;
6001 session->smb2->nonce_high_max = SMB2_NONCE_HIGH_MAX(nonce_size);
6002 session->smb2->nonce_high = 0;
6003 session->smb2->nonce_low = 0;
6005 return NT_STATUS_OK;
6008 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
6009 struct smbXcli_session *session1,
6010 struct smbXcli_conn *conn,
6011 struct smbXcli_session **_session2)
6013 struct smbXcli_session *session2;
6015 if (session1->smb2->signing_key.length == 0) {
6016 return NT_STATUS_INVALID_PARAMETER_MIX;
6019 if (conn == NULL) {
6020 return NT_STATUS_INVALID_PARAMETER_MIX;
6023 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
6024 if (session2 == NULL) {
6025 return NT_STATUS_NO_MEMORY;
6027 session2->smb2 = talloc_reference(session2, session1->smb2);
6028 if (session2->smb2 == NULL) {
6029 talloc_free(session2);
6030 return NT_STATUS_NO_MEMORY;
6033 talloc_set_destructor(session2, smbXcli_session_destructor);
6034 DLIST_ADD_END(conn->sessions, session2);
6035 session2->conn = conn;
6037 memcpy(session2->smb2_channel.preauth_sha512,
6038 conn->smb2.preauth_sha512,
6039 sizeof(session2->smb2_channel.preauth_sha512));
6041 *_session2 = session2;
6042 return NT_STATUS_OK;
6045 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
6046 const DATA_BLOB _channel_key,
6047 const struct iovec *recv_iov)
6049 struct smbXcli_conn *conn = session->conn;
6050 uint8_t channel_key[16];
6051 NTSTATUS status;
6052 struct _derivation {
6053 DATA_BLOB label;
6054 DATA_BLOB context;
6056 struct {
6057 struct _derivation signing;
6058 } derivation = { };
6060 if (conn == NULL) {
6061 return NT_STATUS_INVALID_PARAMETER_MIX;
6064 if (session->smb2_channel.signing_key.length != 0) {
6065 return NT_STATUS_INVALID_PARAMETER_MIX;
6068 if (conn->protocol >= PROTOCOL_SMB3_10) {
6069 struct _derivation *d;
6070 DATA_BLOB p;
6072 p = data_blob_const(session->smb2_channel.preauth_sha512,
6073 sizeof(session->smb2_channel.preauth_sha512));
6075 d = &derivation.signing;
6076 d->label = data_blob_string_const_null("SMBSigningKey");
6077 d->context = p;
6078 } else if (conn->protocol >= PROTOCOL_SMB2_24) {
6079 struct _derivation *d;
6081 d = &derivation.signing;
6082 d->label = data_blob_string_const_null("SMB2AESCMAC");
6083 d->context = data_blob_string_const_null("SmbSign");
6086 ZERO_STRUCT(channel_key);
6087 memcpy(channel_key, _channel_key.data,
6088 MIN(_channel_key.length, sizeof(channel_key)));
6090 session->smb2_channel.signing_key = data_blob_talloc(session,
6091 channel_key,
6092 sizeof(channel_key));
6093 if (session->smb2_channel.signing_key.data == NULL) {
6094 ZERO_STRUCT(channel_key);
6095 return NT_STATUS_NO_MEMORY;
6098 if (conn->protocol >= PROTOCOL_SMB2_24) {
6099 struct _derivation *d = &derivation.signing;
6101 smb2_key_derivation(channel_key, sizeof(channel_key),
6102 d->label.data, d->label.length,
6103 d->context.data, d->context.length,
6104 session->smb2_channel.signing_key.data);
6106 ZERO_STRUCT(channel_key);
6108 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
6109 session->conn->protocol,
6110 recv_iov, 3);
6111 if (!NT_STATUS_IS_OK(status)) {
6112 return status;
6115 return NT_STATUS_OK;
6118 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
6120 if (!session->smb2->should_sign) {
6122 * We need required signing on the session
6123 * in order to prevent man in the middle attacks.
6125 return NT_STATUS_INVALID_PARAMETER_MIX;
6128 if (session->smb2->should_encrypt) {
6129 return NT_STATUS_OK;
6132 if (session->conn->protocol < PROTOCOL_SMB2_24) {
6133 return NT_STATUS_NOT_SUPPORTED;
6136 if (session->conn->smb2.server.cipher == 0) {
6137 return NT_STATUS_NOT_SUPPORTED;
6140 if (session->smb2->signing_key.data == NULL) {
6141 return NT_STATUS_NOT_SUPPORTED;
6143 session->smb2->should_encrypt = true;
6144 return NT_STATUS_OK;
6147 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
6149 struct smbXcli_tcon *tcon;
6151 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
6152 if (tcon == NULL) {
6153 return NULL;
6156 return tcon;
6160 * Return a deep structure copy of a struct smbXcli_tcon *
6163 struct smbXcli_tcon *smbXcli_tcon_copy(TALLOC_CTX *mem_ctx,
6164 const struct smbXcli_tcon *tcon_in)
6166 struct smbXcli_tcon *tcon;
6168 tcon = talloc_memdup(mem_ctx, tcon_in, sizeof(struct smbXcli_tcon));
6169 if (tcon == NULL) {
6170 return NULL;
6173 /* Deal with the SMB1 strings. */
6174 if (tcon_in->smb1.service != NULL) {
6175 tcon->smb1.service = talloc_strdup(tcon, tcon_in->smb1.service);
6176 if (tcon->smb1.service == NULL) {
6177 TALLOC_FREE(tcon);
6178 return NULL;
6181 if (tcon->smb1.fs_type != NULL) {
6182 tcon->smb1.fs_type = talloc_strdup(tcon, tcon_in->smb1.fs_type);
6183 if (tcon->smb1.fs_type == NULL) {
6184 TALLOC_FREE(tcon);
6185 return NULL;
6188 return tcon;
6191 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
6192 uint32_t fs_attributes)
6194 tcon->fs_attributes = fs_attributes;
6197 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
6199 return tcon->fs_attributes;
6202 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
6204 if (tcon == NULL) {
6205 return false;
6208 if (tcon->is_smb1) {
6209 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
6210 return true;
6213 return false;
6216 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
6217 return true;
6220 return false;
6223 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
6225 return tcon->smb1.tcon_id;
6228 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
6230 tcon->is_smb1 = true;
6231 tcon->smb1.tcon_id = tcon_id;
6234 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
6235 uint16_t tcon_id,
6236 uint16_t optional_support,
6237 uint32_t maximal_access,
6238 uint32_t guest_maximal_access,
6239 const char *service,
6240 const char *fs_type)
6242 tcon->is_smb1 = true;
6243 tcon->fs_attributes = 0;
6244 tcon->smb1.tcon_id = tcon_id;
6245 tcon->smb1.optional_support = optional_support;
6246 tcon->smb1.maximal_access = maximal_access;
6247 tcon->smb1.guest_maximal_access = guest_maximal_access;
6249 TALLOC_FREE(tcon->smb1.service);
6250 tcon->smb1.service = talloc_strdup(tcon, service);
6251 if (service != NULL && tcon->smb1.service == NULL) {
6252 return false;
6255 TALLOC_FREE(tcon->smb1.fs_type);
6256 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
6257 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
6258 return false;
6261 return true;
6264 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
6266 return tcon->smb2.tcon_id;
6269 void smb2cli_tcon_set_id(struct smbXcli_tcon *tcon, uint32_t tcon_id)
6271 tcon->smb2.tcon_id = tcon_id;
6274 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
6276 return tcon->smb2.capabilities;
6279 uint32_t smb2cli_tcon_flags(struct smbXcli_tcon *tcon)
6281 return tcon->smb2.flags;
6284 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
6285 struct smbXcli_session *session,
6286 uint32_t tcon_id,
6287 uint8_t type,
6288 uint32_t flags,
6289 uint32_t capabilities,
6290 uint32_t maximal_access)
6292 tcon->is_smb1 = false;
6293 tcon->fs_attributes = 0;
6294 tcon->smb2.tcon_id = tcon_id;
6295 tcon->smb2.type = type;
6296 tcon->smb2.flags = flags;
6297 tcon->smb2.capabilities = capabilities;
6298 tcon->smb2.maximal_access = maximal_access;
6300 tcon->smb2.should_sign = false;
6301 tcon->smb2.should_encrypt = false;
6303 if (session == NULL) {
6304 return;
6307 tcon->smb2.should_sign = session->smb2->should_sign;
6308 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
6310 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
6311 tcon->smb2.should_encrypt = true;
6315 void smb2cli_tcon_should_sign(struct smbXcli_tcon *tcon,
6316 bool should_sign)
6318 tcon->smb2.should_sign = should_sign;
6321 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon *tcon)
6323 if (tcon->smb2.should_encrypt) {
6324 return true;
6327 return tcon->smb2.should_sign;
6330 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon *tcon,
6331 bool should_encrypt)
6333 tcon->smb2.should_encrypt = should_encrypt;
6336 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
6338 return tcon->smb2.should_encrypt;
6341 void smb2cli_conn_set_mid(struct smbXcli_conn *conn, uint64_t mid)
6343 conn->smb2.mid = mid;
6346 uint64_t smb2cli_conn_get_mid(struct smbXcli_conn *conn)
6348 return conn->smb2.mid;