libcli/smb: verify decrypted SMB2 pdus correctly
[Samba/gebeck_regimport.git] / libcli / smb / smbXcli_base.c
blob45da5fd90f1e029fddbe77bd5ca880698ae9885e
1 /*
2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Stefan Metzmacher 2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/network.h"
23 #include "../lib/async_req/async_sock.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
26 #include "lib/util/util_net.h"
27 #include "lib/util/dlinklist.h"
28 #include "../libcli/smb/smb_common.h"
29 #include "../libcli/smb/smb_seal.h"
30 #include "../libcli/smb/smb_signing.h"
31 #include "../libcli/smb/read_smb.h"
32 #include "smbXcli_base.h"
33 #include "librpc/ndr/libndr.h"
35 struct smbXcli_conn;
36 struct smbXcli_req;
37 struct smbXcli_session;
38 struct smbXcli_tcon;
40 struct smbXcli_conn {
41 int read_fd;
42 int write_fd;
43 struct sockaddr_storage local_ss;
44 struct sockaddr_storage remote_ss;
45 const char *remote_name;
47 struct tevent_queue *outgoing;
48 struct tevent_req **pending;
49 struct tevent_req *read_smb_req;
51 enum protocol_types protocol;
52 bool allow_signing;
53 bool desire_signing;
54 bool mandatory_signing;
57 * The incoming dispatch function should return:
58 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
59 * - NT_STATUS_OK, if no more processing is desired, e.g.
60 * the dispatch function called
61 * tevent_req_done().
62 * - All other return values disconnect the connection.
64 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
65 TALLOC_CTX *tmp_mem,
66 uint8_t *inbuf);
68 struct {
69 struct {
70 uint32_t capabilities;
71 uint32_t max_xmit;
72 } client;
74 struct {
75 uint32_t capabilities;
76 uint32_t max_xmit;
77 uint16_t max_mux;
78 uint16_t security_mode;
79 bool readbraw;
80 bool writebraw;
81 bool lockread;
82 bool writeunlock;
83 uint32_t session_key;
84 struct GUID guid;
85 DATA_BLOB gss_blob;
86 uint8_t challenge[8];
87 const char *workgroup;
88 const char *name;
89 int time_zone;
90 NTTIME system_time;
91 } server;
93 uint32_t capabilities;
94 uint32_t max_xmit;
96 uint16_t mid;
98 struct smb_signing_state *signing;
99 struct smb_trans_enc_state *trans_enc;
101 struct tevent_req *read_braw_req;
102 } smb1;
104 struct {
105 struct {
106 uint32_t capabilities;
107 uint16_t security_mode;
108 struct GUID guid;
109 } client;
111 struct {
112 uint32_t capabilities;
113 uint16_t security_mode;
114 struct GUID guid;
115 uint32_t max_trans_size;
116 uint32_t max_read_size;
117 uint32_t max_write_size;
118 NTTIME system_time;
119 NTTIME start_time;
120 DATA_BLOB gss_blob;
121 } server;
123 uint64_t mid;
124 uint16_t cur_credits;
125 uint16_t max_credits;
126 } smb2;
128 struct smbXcli_session *sessions;
131 struct smb2cli_session {
132 uint64_t session_id;
133 uint16_t session_flags;
134 DATA_BLOB application_key;
135 DATA_BLOB signing_key;
136 bool should_sign;
137 bool should_encrypt;
138 DATA_BLOB encryption_key;
139 DATA_BLOB decryption_key;
140 uint64_t nonce_high;
141 uint64_t nonce_low;
142 uint16_t channel_sequence;
145 struct smbXcli_session {
146 struct smbXcli_session *prev, *next;
147 struct smbXcli_conn *conn;
149 struct {
150 uint16_t session_id;
151 DATA_BLOB application_key;
152 bool protected_key;
153 } smb1;
155 struct smb2cli_session *smb2;
157 struct {
158 DATA_BLOB signing_key;
159 } smb2_channel;
162 struct smbXcli_tcon {
163 struct {
164 uint16_t tcon_id;
165 uint16_t optional_support;
166 uint32_t maximal_access;
167 uint32_t guest_maximal_access;
168 char *service;
169 char *fs_type;
170 } smb1;
172 struct {
173 uint32_t tcon_id;
174 uint8_t type;
175 uint32_t flags;
176 uint32_t capabilities;
177 uint32_t maximal_access;
178 bool should_encrypt;
179 } smb2;
182 struct smbXcli_req_state {
183 struct tevent_context *ev;
184 struct smbXcli_conn *conn;
185 struct smbXcli_session *session; /* maybe NULL */
186 struct smbXcli_tcon *tcon; /* maybe NULL */
188 uint8_t length_hdr[4];
190 bool one_way;
192 uint8_t *inbuf;
194 struct {
195 /* Space for the header including the wct */
196 uint8_t hdr[HDR_VWV];
199 * For normal requests, smb1cli_req_send chooses a mid.
200 * SecondaryV trans requests need to use the mid of the primary
201 * request, so we need a place to store it.
202 * Assume it is set if != 0.
204 uint16_t mid;
206 uint16_t *vwv;
207 uint8_t bytecount_buf[2];
209 #define MAX_SMB_IOV 10
210 /* length_hdr, hdr, words, byte_count, buffers */
211 struct iovec iov[1 + 3 + MAX_SMB_IOV];
212 int iov_count;
214 bool one_way_seqnum;
215 uint32_t seqnum;
216 struct tevent_req **chained_requests;
218 uint8_t recv_cmd;
219 NTSTATUS recv_status;
220 /* always an array of 3 talloc elements */
221 struct iovec *recv_iov;
222 } smb1;
224 struct {
225 const uint8_t *fixed;
226 uint16_t fixed_len;
227 const uint8_t *dyn;
228 uint32_t dyn_len;
230 uint8_t transform[SMB2_TF_HDR_SIZE];
231 uint8_t hdr[SMB2_HDR_BODY];
232 uint8_t pad[7]; /* padding space for compounding */
235 * always an array of 3 talloc elements
236 * (without a SMB2_TRANSFORM header!)
238 * HDR, BODY, DYN
240 struct iovec *recv_iov;
242 uint16_t credit_charge;
244 bool should_sign;
245 bool should_encrypt;
246 uint64_t encryption_session_id;
248 bool signing_skipped;
249 bool notify_async;
250 bool got_async;
251 } smb2;
254 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
257 * NT_STATUS_OK, means we do not notify the callers
259 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
261 while (conn->sessions) {
262 conn->sessions->conn = NULL;
263 DLIST_REMOVE(conn->sessions, conn->sessions);
266 if (conn->smb1.trans_enc) {
267 TALLOC_FREE(conn->smb1.trans_enc);
270 return 0;
273 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
274 int fd,
275 const char *remote_name,
276 enum smb_signing_setting signing_state,
277 uint32_t smb1_capabilities,
278 struct GUID *client_guid,
279 uint32_t smb2_capabilities)
281 struct smbXcli_conn *conn = NULL;
282 void *ss = NULL;
283 struct sockaddr *sa = NULL;
284 socklen_t sa_length;
285 int ret;
287 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
288 if (!conn) {
289 return NULL;
292 conn->read_fd = fd;
293 conn->write_fd = dup(fd);
294 if (conn->write_fd == -1) {
295 goto error;
298 conn->remote_name = talloc_strdup(conn, remote_name);
299 if (conn->remote_name == NULL) {
300 goto error;
304 ss = (void *)&conn->local_ss;
305 sa = (struct sockaddr *)ss;
306 sa_length = sizeof(conn->local_ss);
307 ret = getsockname(fd, sa, &sa_length);
308 if (ret == -1) {
309 goto error;
311 ss = (void *)&conn->remote_ss;
312 sa = (struct sockaddr *)ss;
313 sa_length = sizeof(conn->remote_ss);
314 ret = getpeername(fd, sa, &sa_length);
315 if (ret == -1) {
316 goto error;
319 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
320 if (conn->outgoing == NULL) {
321 goto error;
323 conn->pending = NULL;
325 conn->protocol = PROTOCOL_NONE;
327 switch (signing_state) {
328 case SMB_SIGNING_OFF:
329 /* never */
330 conn->allow_signing = false;
331 conn->desire_signing = false;
332 conn->mandatory_signing = false;
333 break;
334 case SMB_SIGNING_DEFAULT:
335 case SMB_SIGNING_IF_REQUIRED:
336 /* if the server requires it */
337 conn->allow_signing = true;
338 conn->desire_signing = false;
339 conn->mandatory_signing = false;
340 break;
341 case SMB_SIGNING_REQUIRED:
342 /* always */
343 conn->allow_signing = true;
344 conn->desire_signing = true;
345 conn->mandatory_signing = true;
346 break;
349 conn->smb1.client.capabilities = smb1_capabilities;
350 conn->smb1.client.max_xmit = UINT16_MAX;
352 conn->smb1.capabilities = conn->smb1.client.capabilities;
353 conn->smb1.max_xmit = 1024;
355 conn->smb1.mid = 1;
357 /* initialise signing */
358 conn->smb1.signing = smb_signing_init(conn,
359 conn->allow_signing,
360 conn->desire_signing,
361 conn->mandatory_signing);
362 if (!conn->smb1.signing) {
363 goto error;
366 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
367 if (conn->mandatory_signing) {
368 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
370 if (client_guid) {
371 conn->smb2.client.guid = *client_guid;
373 conn->smb2.client.capabilities = smb2_capabilities;
375 conn->smb2.cur_credits = 1;
376 conn->smb2.max_credits = 0;
378 talloc_set_destructor(conn, smbXcli_conn_destructor);
379 return conn;
381 error:
382 if (conn->write_fd != -1) {
383 close(conn->write_fd);
385 TALLOC_FREE(conn);
386 return NULL;
389 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
391 if (conn == NULL) {
392 return false;
395 if (conn->read_fd == -1) {
396 return false;
399 return true;
402 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
404 return conn->protocol;
407 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
409 if (conn->protocol >= PROTOCOL_SMB2_02) {
410 return true;
413 if (conn->smb1.capabilities & CAP_UNICODE) {
414 return true;
417 return false;
420 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
422 set_socket_options(conn->read_fd, options);
425 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
427 return &conn->local_ss;
430 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
432 return &conn->remote_ss;
435 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
437 return conn->remote_name;
440 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
442 if (conn->protocol >= PROTOCOL_SMB2_02) {
444 * TODO...
446 return 1;
449 return conn->smb1.server.max_mux;
452 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
454 if (conn->protocol >= PROTOCOL_SMB2_02) {
455 return conn->smb2.server.system_time;
458 return conn->smb1.server.system_time;
461 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
463 if (conn->protocol >= PROTOCOL_SMB2_02) {
464 return &conn->smb2.server.gss_blob;
467 return &conn->smb1.server.gss_blob;
470 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
472 if (conn->protocol >= PROTOCOL_SMB2_02) {
473 return &conn->smb2.server.guid;
476 return &conn->smb1.server.guid;
479 struct smbXcli_conn_samba_suicide_state {
480 struct smbXcli_conn *conn;
481 struct iovec iov;
482 uint8_t buf[9];
485 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
487 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
488 struct tevent_context *ev,
489 struct smbXcli_conn *conn,
490 uint8_t exitcode)
492 struct tevent_req *req, *subreq;
493 struct smbXcli_conn_samba_suicide_state *state;
495 req = tevent_req_create(mem_ctx, &state,
496 struct smbXcli_conn_samba_suicide_state);
497 if (req == NULL) {
498 return NULL;
500 state->conn = conn;
501 SIVAL(state->buf, 4, 0x74697865);
502 SCVAL(state->buf, 8, exitcode);
503 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
505 state->iov.iov_base = state->buf;
506 state->iov.iov_len = sizeof(state->buf);
508 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
509 false, &state->iov, 1);
510 if (tevent_req_nomem(subreq, req)) {
511 return tevent_req_post(req, ev);
513 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
514 return req;
517 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
519 struct tevent_req *req = tevent_req_callback_data(
520 subreq, struct tevent_req);
521 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
522 req, struct smbXcli_conn_samba_suicide_state);
523 ssize_t nwritten;
524 int err;
526 nwritten = writev_recv(subreq, &err);
527 TALLOC_FREE(subreq);
528 if (nwritten == -1) {
529 NTSTATUS status = map_nt_error_from_unix_common(err);
530 smbXcli_conn_disconnect(state->conn, status);
531 return;
533 tevent_req_done(req);
536 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
538 return tevent_req_simple_recv_ntstatus(req);
541 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
542 uint8_t exitcode)
544 TALLOC_CTX *frame = talloc_stackframe();
545 struct tevent_context *ev;
546 struct tevent_req *req;
547 NTSTATUS status = NT_STATUS_NO_MEMORY;
548 bool ok;
550 if (smbXcli_conn_has_async_calls(conn)) {
552 * Can't use sync call while an async call is in flight
554 status = NT_STATUS_INVALID_PARAMETER_MIX;
555 goto fail;
557 ev = tevent_context_init(frame);
558 if (ev == NULL) {
559 goto fail;
561 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
562 if (req == NULL) {
563 goto fail;
565 ok = tevent_req_poll(req, ev);
566 if (!ok) {
567 status = map_nt_error_from_unix_common(errno);
568 goto fail;
570 status = smbXcli_conn_samba_suicide_recv(req);
571 fail:
572 TALLOC_FREE(frame);
573 return status;
576 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
578 return conn->smb1.capabilities;
581 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
583 return conn->smb1.max_xmit;
586 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
588 return conn->smb1.server.session_key;
591 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
593 return conn->smb1.server.challenge;
596 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
598 return conn->smb1.server.security_mode;
601 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
603 return conn->smb1.server.readbraw;
606 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
608 return conn->smb1.server.writebraw;
611 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
613 return conn->smb1.server.lockread;
616 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
618 return conn->smb1.server.writeunlock;
621 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
623 return conn->smb1.server.time_zone;
626 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
627 const DATA_BLOB user_session_key,
628 const DATA_BLOB response)
630 return smb_signing_activate(conn->smb1.signing,
631 user_session_key,
632 response);
635 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
636 const uint8_t *buf, uint32_t seqnum)
638 const uint8_t *hdr = buf + NBT_HDR_SIZE;
639 size_t len = smb_len_nbt(buf);
641 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
644 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
646 return smb_signing_is_active(conn->smb1.signing);
649 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
650 struct smb_trans_enc_state *es)
652 /* Replace the old state, if any. */
653 if (conn->smb1.trans_enc) {
654 TALLOC_FREE(conn->smb1.trans_enc);
656 conn->smb1.trans_enc = es;
659 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
661 return common_encryption_on(conn->smb1.trans_enc);
665 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
667 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
668 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
670 if (NT_STATUS_IS_OK(status)) {
671 return NT_STATUS_OK;
674 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
675 return status;
678 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
682 * Is the SMB command able to hold an AND_X successor
683 * @param[in] cmd The SMB command in question
684 * @retval Can we add a chained request after "cmd"?
686 bool smb1cli_is_andx_req(uint8_t cmd)
688 switch (cmd) {
689 case SMBtconX:
690 case SMBlockingX:
691 case SMBopenX:
692 case SMBreadX:
693 case SMBwriteX:
694 case SMBsesssetupX:
695 case SMBulogoffX:
696 case SMBntcreateX:
697 return true;
698 break;
699 default:
700 break;
703 return false;
706 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
708 size_t num_pending = talloc_array_length(conn->pending);
709 uint16_t result;
711 while (true) {
712 size_t i;
714 result = conn->smb1.mid++;
715 if ((result == 0) || (result == 0xffff)) {
716 continue;
719 for (i=0; i<num_pending; i++) {
720 if (result == smb1cli_req_mid(conn->pending[i])) {
721 break;
725 if (i == num_pending) {
726 return result;
731 void smbXcli_req_unset_pending(struct tevent_req *req)
733 struct smbXcli_req_state *state =
734 tevent_req_data(req,
735 struct smbXcli_req_state);
736 struct smbXcli_conn *conn = state->conn;
737 size_t num_pending = talloc_array_length(conn->pending);
738 size_t i;
740 if (state->smb1.mid != 0) {
742 * This is a [nt]trans[2] request which waits
743 * for more than one reply.
745 return;
748 talloc_set_destructor(req, NULL);
750 if (num_pending == 1) {
752 * The pending read_smb tevent_req is a child of
753 * conn->pending. So if nothing is pending anymore, we need to
754 * delete the socket read fde.
756 TALLOC_FREE(conn->pending);
757 conn->read_smb_req = NULL;
758 return;
761 for (i=0; i<num_pending; i++) {
762 if (req == conn->pending[i]) {
763 break;
766 if (i == num_pending) {
768 * Something's seriously broken. Just returning here is the
769 * right thing nevertheless, the point of this routine is to
770 * remove ourselves from conn->pending.
772 return;
776 * Remove ourselves from the conn->pending array
778 for (; i < (num_pending - 1); i++) {
779 conn->pending[i] = conn->pending[i+1];
783 * No NULL check here, we're shrinking by sizeof(void *), and
784 * talloc_realloc just adjusts the size for this.
786 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
787 num_pending - 1);
788 return;
791 static int smbXcli_req_destructor(struct tevent_req *req)
793 struct smbXcli_req_state *state =
794 tevent_req_data(req,
795 struct smbXcli_req_state);
798 * Make sure we really remove it from
799 * the pending array on destruction.
801 state->smb1.mid = 0;
802 smbXcli_req_unset_pending(req);
803 return 0;
806 static bool smb1cli_req_cancel(struct tevent_req *req);
807 static bool smb2cli_req_cancel(struct tevent_req *req);
809 static bool smbXcli_req_cancel(struct tevent_req *req)
811 struct smbXcli_req_state *state =
812 tevent_req_data(req,
813 struct smbXcli_req_state);
815 if (!smbXcli_conn_is_connected(state->conn)) {
816 return false;
819 if (state->conn->protocol == PROTOCOL_NONE) {
820 return false;
823 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
824 return smb2cli_req_cancel(req);
827 return smb1cli_req_cancel(req);
830 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
832 bool smbXcli_req_set_pending(struct tevent_req *req)
834 struct smbXcli_req_state *state =
835 tevent_req_data(req,
836 struct smbXcli_req_state);
837 struct smbXcli_conn *conn;
838 struct tevent_req **pending;
839 size_t num_pending;
841 conn = state->conn;
843 if (!smbXcli_conn_is_connected(conn)) {
844 return false;
847 num_pending = talloc_array_length(conn->pending);
849 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
850 num_pending+1);
851 if (pending == NULL) {
852 return false;
854 pending[num_pending] = req;
855 conn->pending = pending;
856 talloc_set_destructor(req, smbXcli_req_destructor);
857 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
859 if (!smbXcli_conn_receive_next(conn)) {
861 * the caller should notify the current request
863 * And all other pending requests get notified
864 * by smbXcli_conn_disconnect().
866 smbXcli_req_unset_pending(req);
867 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
868 return false;
871 return true;
874 static void smbXcli_conn_received(struct tevent_req *subreq);
876 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
878 size_t num_pending = talloc_array_length(conn->pending);
879 struct tevent_req *req;
880 struct smbXcli_req_state *state;
882 if (conn->read_smb_req != NULL) {
883 return true;
886 if (num_pending == 0) {
887 if (conn->smb2.mid < UINT64_MAX) {
888 /* no more pending requests, so we are done for now */
889 return true;
893 * If there are no more SMB2 requests possible,
894 * because we are out of message ids,
895 * we need to disconnect.
897 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
898 return true;
901 req = conn->pending[0];
902 state = tevent_req_data(req, struct smbXcli_req_state);
905 * We're the first ones, add the read_smb request that waits for the
906 * answer from the server
908 conn->read_smb_req = read_smb_send(conn->pending,
909 state->ev,
910 conn->read_fd);
911 if (conn->read_smb_req == NULL) {
912 return false;
914 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
915 return true;
918 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
920 struct smbXcli_session *session;
922 tevent_queue_stop(conn->outgoing);
924 if (conn->read_fd != -1) {
925 close(conn->read_fd);
927 if (conn->write_fd != -1) {
928 close(conn->write_fd);
930 conn->read_fd = -1;
931 conn->write_fd = -1;
933 session = conn->sessions;
934 if (talloc_array_length(conn->pending) == 0) {
936 * if we do not have pending requests
937 * there is no need to update the channel_sequence
939 session = NULL;
941 for (; session; session = session->next) {
942 smb2cli_session_increment_channel_sequence(session);
946 * Cancel all pending requests. We do not do a for-loop walking
947 * conn->pending because that array changes in
948 * smbXcli_req_unset_pending.
950 while (talloc_array_length(conn->pending) > 0) {
951 struct tevent_req *req;
952 struct smbXcli_req_state *state;
953 struct tevent_req **chain;
954 size_t num_chained;
955 size_t i;
957 req = conn->pending[0];
958 state = tevent_req_data(req, struct smbXcli_req_state);
960 if (state->smb1.chained_requests == NULL) {
962 * We're dead. No point waiting for trans2
963 * replies.
965 state->smb1.mid = 0;
967 smbXcli_req_unset_pending(req);
969 if (NT_STATUS_IS_OK(status)) {
970 /* do not notify the callers */
971 continue;
975 * we need to defer the callback, because we may notify
976 * more then one caller.
978 tevent_req_defer_callback(req, state->ev);
979 tevent_req_nterror(req, status);
980 continue;
983 chain = talloc_move(conn, &state->smb1.chained_requests);
984 num_chained = talloc_array_length(chain);
986 for (i=0; i<num_chained; i++) {
987 req = chain[i];
988 state = tevent_req_data(req, struct smbXcli_req_state);
991 * We're dead. No point waiting for trans2
992 * replies.
994 state->smb1.mid = 0;
996 smbXcli_req_unset_pending(req);
998 if (NT_STATUS_IS_OK(status)) {
999 /* do not notify the callers */
1000 continue;
1004 * we need to defer the callback, because we may notify
1005 * more than one caller.
1007 tevent_req_defer_callback(req, state->ev);
1008 tevent_req_nterror(req, status);
1010 TALLOC_FREE(chain);
1015 * Fetch a smb request's mid. Only valid after the request has been sent by
1016 * smb1cli_req_send().
1018 uint16_t smb1cli_req_mid(struct tevent_req *req)
1020 struct smbXcli_req_state *state =
1021 tevent_req_data(req,
1022 struct smbXcli_req_state);
1024 if (state->smb1.mid != 0) {
1025 return state->smb1.mid;
1028 return SVAL(state->smb1.hdr, HDR_MID);
1031 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1033 struct smbXcli_req_state *state =
1034 tevent_req_data(req,
1035 struct smbXcli_req_state);
1037 state->smb1.mid = mid;
1040 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1042 struct smbXcli_req_state *state =
1043 tevent_req_data(req,
1044 struct smbXcli_req_state);
1046 return state->smb1.seqnum;
1049 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1051 struct smbXcli_req_state *state =
1052 tevent_req_data(req,
1053 struct smbXcli_req_state);
1055 state->smb1.seqnum = seqnum;
1058 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1060 size_t result = 0;
1061 int i;
1062 for (i=0; i<count; i++) {
1063 result += iov[i].iov_len;
1065 return result;
1068 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1069 const struct iovec *iov,
1070 int count)
1072 size_t len = smbXcli_iov_len(iov, count);
1073 size_t copied;
1074 uint8_t *buf;
1075 int i;
1077 buf = talloc_array(mem_ctx, uint8_t, len);
1078 if (buf == NULL) {
1079 return NULL;
1081 copied = 0;
1082 for (i=0; i<count; i++) {
1083 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1084 copied += iov[i].iov_len;
1086 return buf;
1089 static void smb1cli_req_flags(enum protocol_types protocol,
1090 uint32_t smb1_capabilities,
1091 uint8_t smb_command,
1092 uint8_t additional_flags,
1093 uint8_t clear_flags,
1094 uint8_t *_flags,
1095 uint16_t additional_flags2,
1096 uint16_t clear_flags2,
1097 uint16_t *_flags2)
1099 uint8_t flags = 0;
1100 uint16_t flags2 = 0;
1102 if (protocol >= PROTOCOL_LANMAN1) {
1103 flags |= FLAG_CASELESS_PATHNAMES;
1104 flags |= FLAG_CANONICAL_PATHNAMES;
1107 if (protocol >= PROTOCOL_LANMAN2) {
1108 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1109 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1112 if (protocol >= PROTOCOL_NT1) {
1113 flags2 |= FLAGS2_IS_LONG_NAME;
1115 if (smb1_capabilities & CAP_UNICODE) {
1116 flags2 |= FLAGS2_UNICODE_STRINGS;
1118 if (smb1_capabilities & CAP_STATUS32) {
1119 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1121 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1122 flags2 |= FLAGS2_EXTENDED_SECURITY;
1126 flags |= additional_flags;
1127 flags &= ~clear_flags;
1128 flags2 |= additional_flags2;
1129 flags2 &= ~clear_flags2;
1131 *_flags = flags;
1132 *_flags2 = flags2;
1135 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1137 static bool smb1cli_req_cancel(struct tevent_req *req)
1139 struct smbXcli_req_state *state =
1140 tevent_req_data(req,
1141 struct smbXcli_req_state);
1142 uint8_t flags;
1143 uint16_t flags2;
1144 uint32_t pid;
1145 uint16_t mid;
1146 struct tevent_req *subreq;
1147 NTSTATUS status;
1149 flags = CVAL(state->smb1.hdr, HDR_FLG);
1150 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1151 pid = SVAL(state->smb1.hdr, HDR_PID);
1152 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1153 mid = SVAL(state->smb1.hdr, HDR_MID);
1155 subreq = smb1cli_req_create(state, state->ev,
1156 state->conn,
1157 SMBntcancel,
1158 flags, 0,
1159 flags2, 0,
1160 0, /* timeout */
1161 pid,
1162 state->tcon,
1163 state->session,
1164 0, NULL, /* vwv */
1165 0, NULL); /* bytes */
1166 if (subreq == NULL) {
1167 return false;
1169 smb1cli_req_set_mid(subreq, mid);
1171 status = smb1cli_req_chain_submit(&subreq, 1);
1172 if (!NT_STATUS_IS_OK(status)) {
1173 TALLOC_FREE(subreq);
1174 return false;
1176 smb1cli_req_set_mid(subreq, 0);
1178 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1180 return true;
1183 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1185 /* we do not care about the result */
1186 TALLOC_FREE(subreq);
1189 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1190 struct tevent_context *ev,
1191 struct smbXcli_conn *conn,
1192 uint8_t smb_command,
1193 uint8_t additional_flags,
1194 uint8_t clear_flags,
1195 uint16_t additional_flags2,
1196 uint16_t clear_flags2,
1197 uint32_t timeout_msec,
1198 uint32_t pid,
1199 struct smbXcli_tcon *tcon,
1200 struct smbXcli_session *session,
1201 uint8_t wct, uint16_t *vwv,
1202 int iov_count,
1203 struct iovec *bytes_iov)
1205 struct tevent_req *req;
1206 struct smbXcli_req_state *state;
1207 uint8_t flags = 0;
1208 uint16_t flags2 = 0;
1209 uint16_t uid = 0;
1210 uint16_t tid = 0;
1212 if (iov_count > MAX_SMB_IOV) {
1214 * Should not happen :-)
1216 return NULL;
1219 req = tevent_req_create(mem_ctx, &state,
1220 struct smbXcli_req_state);
1221 if (req == NULL) {
1222 return NULL;
1224 state->ev = ev;
1225 state->conn = conn;
1226 state->session = session;
1227 state->tcon = tcon;
1229 if (session) {
1230 uid = session->smb1.session_id;
1233 if (tcon) {
1234 tid = tcon->smb1.tcon_id;
1237 state->smb1.recv_cmd = 0xFF;
1238 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1239 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1240 if (state->smb1.recv_iov == NULL) {
1241 TALLOC_FREE(req);
1242 return NULL;
1245 smb1cli_req_flags(conn->protocol,
1246 conn->smb1.capabilities,
1247 smb_command,
1248 additional_flags,
1249 clear_flags,
1250 &flags,
1251 additional_flags2,
1252 clear_flags2,
1253 &flags2);
1255 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1256 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1257 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1258 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1259 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1260 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1261 SSVAL(state->smb1.hdr, HDR_TID, tid);
1262 SSVAL(state->smb1.hdr, HDR_PID, pid);
1263 SSVAL(state->smb1.hdr, HDR_UID, uid);
1264 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1265 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1267 state->smb1.vwv = vwv;
1269 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1271 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1272 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1273 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1274 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1275 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1276 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1277 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1278 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1280 if (iov_count != 0) {
1281 memcpy(&state->smb1.iov[4], bytes_iov,
1282 iov_count * sizeof(*bytes_iov));
1284 state->smb1.iov_count = iov_count + 4;
1286 if (timeout_msec > 0) {
1287 struct timeval endtime;
1289 endtime = timeval_current_ofs_msec(timeout_msec);
1290 if (!tevent_req_set_endtime(req, ev, endtime)) {
1291 return req;
1295 switch (smb_command) {
1296 case SMBtranss:
1297 case SMBtranss2:
1298 case SMBnttranss:
1299 state->one_way = true;
1300 break;
1301 case SMBntcancel:
1302 state->one_way = true;
1303 state->smb1.one_way_seqnum = true;
1304 break;
1305 case SMBlockingX:
1306 if ((wct == 8) &&
1307 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1308 state->one_way = true;
1310 break;
1313 return req;
1316 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1317 struct iovec *iov, int iov_count,
1318 uint32_t *seqnum,
1319 bool one_way_seqnum)
1321 TALLOC_CTX *frame = NULL;
1322 uint8_t *buf;
1325 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1326 * iovec directly. MD5Update would do that just fine.
1329 if (iov_count < 4) {
1330 return NT_STATUS_INVALID_PARAMETER_MIX;
1332 if (iov[0].iov_len != NBT_HDR_SIZE) {
1333 return NT_STATUS_INVALID_PARAMETER_MIX;
1335 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1336 return NT_STATUS_INVALID_PARAMETER_MIX;
1338 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1339 return NT_STATUS_INVALID_PARAMETER_MIX;
1341 if (iov[3].iov_len != sizeof(uint16_t)) {
1342 return NT_STATUS_INVALID_PARAMETER_MIX;
1345 frame = talloc_stackframe();
1347 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1348 if (buf == NULL) {
1349 return NT_STATUS_NO_MEMORY;
1352 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1353 one_way_seqnum);
1354 smb_signing_sign_pdu(conn->smb1.signing,
1355 buf, talloc_get_size(buf),
1356 *seqnum);
1357 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1359 TALLOC_FREE(frame);
1360 return NT_STATUS_OK;
1363 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1364 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1365 TALLOC_CTX *tmp_mem,
1366 uint8_t *inbuf);
1368 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1369 struct smbXcli_req_state *state,
1370 struct iovec *iov, int iov_count)
1372 struct tevent_req *subreq;
1373 NTSTATUS status;
1374 uint8_t cmd;
1375 uint16_t mid;
1377 if (!smbXcli_conn_is_connected(state->conn)) {
1378 return NT_STATUS_CONNECTION_DISCONNECTED;
1381 if (state->conn->protocol > PROTOCOL_NT1) {
1382 return NT_STATUS_REVISION_MISMATCH;
1385 if (iov_count < 4) {
1386 return NT_STATUS_INVALID_PARAMETER_MIX;
1388 if (iov[0].iov_len != NBT_HDR_SIZE) {
1389 return NT_STATUS_INVALID_PARAMETER_MIX;
1391 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1392 return NT_STATUS_INVALID_PARAMETER_MIX;
1394 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1395 return NT_STATUS_INVALID_PARAMETER_MIX;
1397 if (iov[3].iov_len != sizeof(uint16_t)) {
1398 return NT_STATUS_INVALID_PARAMETER_MIX;
1401 cmd = CVAL(iov[1].iov_base, HDR_COM);
1402 if (cmd == SMBreadBraw) {
1403 if (smbXcli_conn_has_async_calls(state->conn)) {
1404 return NT_STATUS_INVALID_PARAMETER_MIX;
1406 state->conn->smb1.read_braw_req = req;
1409 if (state->smb1.mid != 0) {
1410 mid = state->smb1.mid;
1411 } else {
1412 mid = smb1cli_alloc_mid(state->conn);
1414 SSVAL(iov[1].iov_base, HDR_MID, mid);
1416 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1418 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1419 &state->smb1.seqnum,
1420 state->smb1.one_way_seqnum);
1422 if (!NT_STATUS_IS_OK(status)) {
1423 return status;
1427 * If we supported multiple encrytion contexts
1428 * here we'd look up based on tid.
1430 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1431 char *buf, *enc_buf;
1433 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1434 if (buf == NULL) {
1435 return NT_STATUS_NO_MEMORY;
1437 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1438 (char *)buf, &enc_buf);
1439 TALLOC_FREE(buf);
1440 if (!NT_STATUS_IS_OK(status)) {
1441 DEBUG(0, ("Error in encrypting client message: %s\n",
1442 nt_errstr(status)));
1443 return status;
1445 buf = (char *)talloc_memdup(state, enc_buf,
1446 smb_len_nbt(enc_buf)+4);
1447 SAFE_FREE(enc_buf);
1448 if (buf == NULL) {
1449 return NT_STATUS_NO_MEMORY;
1451 iov[0].iov_base = (void *)buf;
1452 iov[0].iov_len = talloc_get_size(buf);
1453 iov_count = 1;
1456 if (state->conn->dispatch_incoming == NULL) {
1457 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1460 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1462 subreq = writev_send(state, state->ev, state->conn->outgoing,
1463 state->conn->write_fd, false, iov, iov_count);
1464 if (subreq == NULL) {
1465 return NT_STATUS_NO_MEMORY;
1467 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1468 return NT_STATUS_OK;
1471 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1472 struct tevent_context *ev,
1473 struct smbXcli_conn *conn,
1474 uint8_t smb_command,
1475 uint8_t additional_flags,
1476 uint8_t clear_flags,
1477 uint16_t additional_flags2,
1478 uint16_t clear_flags2,
1479 uint32_t timeout_msec,
1480 uint32_t pid,
1481 struct smbXcli_tcon *tcon,
1482 struct smbXcli_session *session,
1483 uint8_t wct, uint16_t *vwv,
1484 uint32_t num_bytes,
1485 const uint8_t *bytes)
1487 struct tevent_req *req;
1488 struct iovec iov;
1489 NTSTATUS status;
1491 iov.iov_base = discard_const_p(void, bytes);
1492 iov.iov_len = num_bytes;
1494 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1495 additional_flags, clear_flags,
1496 additional_flags2, clear_flags2,
1497 timeout_msec,
1498 pid, tcon, session,
1499 wct, vwv, 1, &iov);
1500 if (req == NULL) {
1501 return NULL;
1503 if (!tevent_req_is_in_progress(req)) {
1504 return tevent_req_post(req, ev);
1506 status = smb1cli_req_chain_submit(&req, 1);
1507 if (tevent_req_nterror(req, status)) {
1508 return tevent_req_post(req, ev);
1510 return req;
1513 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1515 struct tevent_req *req =
1516 tevent_req_callback_data(subreq,
1517 struct tevent_req);
1518 struct smbXcli_req_state *state =
1519 tevent_req_data(req,
1520 struct smbXcli_req_state);
1521 ssize_t nwritten;
1522 int err;
1524 nwritten = writev_recv(subreq, &err);
1525 TALLOC_FREE(subreq);
1526 if (nwritten == -1) {
1527 NTSTATUS status = map_nt_error_from_unix_common(err);
1528 smbXcli_conn_disconnect(state->conn, status);
1529 return;
1532 if (state->one_way) {
1533 state->inbuf = NULL;
1534 tevent_req_done(req);
1535 return;
1538 if (!smbXcli_req_set_pending(req)) {
1539 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1540 return;
1544 static void smbXcli_conn_received(struct tevent_req *subreq)
1546 struct smbXcli_conn *conn =
1547 tevent_req_callback_data(subreq,
1548 struct smbXcli_conn);
1549 TALLOC_CTX *frame = talloc_stackframe();
1550 NTSTATUS status;
1551 uint8_t *inbuf;
1552 ssize_t received;
1553 int err;
1555 if (subreq != conn->read_smb_req) {
1556 DEBUG(1, ("Internal error: cli_smb_received called with "
1557 "unexpected subreq\n"));
1558 status = NT_STATUS_INTERNAL_ERROR;
1559 smbXcli_conn_disconnect(conn, status);
1560 TALLOC_FREE(frame);
1561 return;
1563 conn->read_smb_req = NULL;
1565 received = read_smb_recv(subreq, frame, &inbuf, &err);
1566 TALLOC_FREE(subreq);
1567 if (received == -1) {
1568 status = map_nt_error_from_unix_common(err);
1569 smbXcli_conn_disconnect(conn, status);
1570 TALLOC_FREE(frame);
1571 return;
1574 status = conn->dispatch_incoming(conn, frame, inbuf);
1575 TALLOC_FREE(frame);
1576 if (NT_STATUS_IS_OK(status)) {
1578 * We should not do any more processing
1579 * as the dispatch function called
1580 * tevent_req_done().
1582 return;
1583 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1585 * We got an error, so notify all pending requests
1587 smbXcli_conn_disconnect(conn, status);
1588 return;
1592 * We got NT_STATUS_RETRY, so we may ask for a
1593 * next incoming pdu.
1595 if (!smbXcli_conn_receive_next(conn)) {
1596 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1600 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1601 struct iovec **piov, int *pnum_iov)
1603 struct iovec *iov;
1604 int num_iov;
1605 size_t buflen;
1606 size_t taken;
1607 size_t remaining;
1608 uint8_t *hdr;
1609 uint8_t cmd;
1610 uint32_t wct_ofs;
1612 buflen = smb_len_nbt(buf);
1613 taken = 0;
1615 hdr = buf + NBT_HDR_SIZE;
1617 if (buflen < MIN_SMB_SIZE) {
1618 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1622 * This returns iovec elements in the following order:
1624 * - SMB header
1626 * - Parameter Block
1627 * - Data Block
1629 * - Parameter Block
1630 * - Data Block
1632 * - Parameter Block
1633 * - Data Block
1635 num_iov = 1;
1637 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1638 if (iov == NULL) {
1639 return NT_STATUS_NO_MEMORY;
1641 iov[0].iov_base = hdr;
1642 iov[0].iov_len = HDR_WCT;
1643 taken += HDR_WCT;
1645 cmd = CVAL(hdr, HDR_COM);
1646 wct_ofs = HDR_WCT;
1648 while (true) {
1649 size_t len = buflen - taken;
1650 struct iovec *cur;
1651 struct iovec *iov_tmp;
1652 uint8_t wct;
1653 uint32_t bcc_ofs;
1654 uint16_t bcc;
1655 size_t needed;
1658 * we need at least WCT and BCC
1660 needed = sizeof(uint8_t) + sizeof(uint16_t);
1661 if (len < needed) {
1662 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1663 __location__, (int)len, (int)needed));
1664 goto inval;
1668 * Now we check if the specified words are there
1670 wct = CVAL(hdr, wct_ofs);
1671 needed += wct * sizeof(uint16_t);
1672 if (len < needed) {
1673 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1674 __location__, (int)len, (int)needed));
1675 goto inval;
1679 * Now we check if the specified bytes are there
1681 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1682 bcc = SVAL(hdr, bcc_ofs);
1683 needed += bcc * sizeof(uint8_t);
1684 if (len < needed) {
1685 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1686 __location__, (int)len, (int)needed));
1687 goto inval;
1691 * we allocate 2 iovec structures for words and bytes
1693 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1694 num_iov + 2);
1695 if (iov_tmp == NULL) {
1696 TALLOC_FREE(iov);
1697 return NT_STATUS_NO_MEMORY;
1699 iov = iov_tmp;
1700 cur = &iov[num_iov];
1701 num_iov += 2;
1703 cur[0].iov_len = wct * sizeof(uint16_t);
1704 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1705 cur[1].iov_len = bcc * sizeof(uint8_t);
1706 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1708 taken += needed;
1710 if (!smb1cli_is_andx_req(cmd)) {
1712 * If the current command does not have AndX chanining
1713 * we are done.
1715 break;
1718 if (wct == 0 && bcc == 0) {
1720 * An empty response also ends the chain,
1721 * most likely with an error.
1723 break;
1726 if (wct < 2) {
1727 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1728 __location__, (int)wct, (int)cmd));
1729 goto inval;
1731 cmd = CVAL(cur[0].iov_base, 0);
1732 if (cmd == 0xFF) {
1734 * If it is the end of the chain we are also done.
1736 break;
1738 wct_ofs = SVAL(cur[0].iov_base, 2);
1740 if (wct_ofs < taken) {
1741 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1743 if (wct_ofs > buflen) {
1744 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1748 * we consumed everything up to the start of the next
1749 * parameter block.
1751 taken = wct_ofs;
1754 remaining = buflen - taken;
1756 if (remaining > 0 && num_iov >= 3) {
1758 * The last DATA block gets the remaining
1759 * bytes, this is needed to support
1760 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1762 iov[num_iov-1].iov_len += remaining;
1765 *piov = iov;
1766 *pnum_iov = num_iov;
1767 return NT_STATUS_OK;
1769 inval:
1770 TALLOC_FREE(iov);
1771 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1774 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1775 TALLOC_CTX *tmp_mem,
1776 uint8_t *inbuf)
1778 struct tevent_req *req;
1779 struct smbXcli_req_state *state;
1780 NTSTATUS status;
1781 size_t num_pending;
1782 size_t i;
1783 uint8_t cmd;
1784 uint16_t mid;
1785 bool oplock_break;
1786 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1787 size_t len = smb_len_nbt(inbuf);
1788 struct iovec *iov = NULL;
1789 int num_iov = 0;
1790 struct tevent_req **chain = NULL;
1791 size_t num_chained = 0;
1792 size_t num_responses = 0;
1794 if (conn->smb1.read_braw_req != NULL) {
1795 req = conn->smb1.read_braw_req;
1796 conn->smb1.read_braw_req = NULL;
1797 state = tevent_req_data(req, struct smbXcli_req_state);
1799 smbXcli_req_unset_pending(req);
1801 if (state->smb1.recv_iov == NULL) {
1803 * For requests with more than
1804 * one response, we have to readd the
1805 * recv_iov array.
1807 state->smb1.recv_iov = talloc_zero_array(state,
1808 struct iovec,
1810 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1811 return NT_STATUS_OK;
1815 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1816 state->smb1.recv_iov[0].iov_len = len;
1817 ZERO_STRUCT(state->smb1.recv_iov[1]);
1818 ZERO_STRUCT(state->smb1.recv_iov[2]);
1820 state->smb1.recv_cmd = SMBreadBraw;
1821 state->smb1.recv_status = NT_STATUS_OK;
1822 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1824 tevent_req_done(req);
1825 return NT_STATUS_OK;
1828 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1829 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1830 DEBUG(10, ("Got non-SMB PDU\n"));
1831 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1835 * If we supported multiple encrytion contexts
1836 * here we'd look up based on tid.
1838 if (common_encryption_on(conn->smb1.trans_enc)
1839 && (CVAL(inbuf, 0) == 0)) {
1840 uint16_t enc_ctx_num;
1842 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1843 if (!NT_STATUS_IS_OK(status)) {
1844 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1845 nt_errstr(status)));
1846 return status;
1849 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1850 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1851 enc_ctx_num,
1852 conn->smb1.trans_enc->enc_ctx_num));
1853 return NT_STATUS_INVALID_HANDLE;
1856 status = common_decrypt_buffer(conn->smb1.trans_enc,
1857 (char *)inbuf);
1858 if (!NT_STATUS_IS_OK(status)) {
1859 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1860 nt_errstr(status)));
1861 return status;
1863 inhdr = inbuf + NBT_HDR_SIZE;
1864 len = smb_len_nbt(inbuf);
1867 mid = SVAL(inhdr, HDR_MID);
1868 num_pending = talloc_array_length(conn->pending);
1870 for (i=0; i<num_pending; i++) {
1871 if (mid == smb1cli_req_mid(conn->pending[i])) {
1872 break;
1875 if (i == num_pending) {
1876 /* Dump unexpected reply */
1877 return NT_STATUS_RETRY;
1880 oplock_break = false;
1882 if (mid == 0xffff) {
1884 * Paranoia checks that this is really an oplock break request.
1886 oplock_break = (len == 51); /* hdr + 8 words */
1887 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1888 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1889 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1890 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1892 if (!oplock_break) {
1893 /* Dump unexpected reply */
1894 return NT_STATUS_RETRY;
1898 req = conn->pending[i];
1899 state = tevent_req_data(req, struct smbXcli_req_state);
1901 if (!oplock_break /* oplock breaks are not signed */
1902 && !smb_signing_check_pdu(conn->smb1.signing,
1903 inhdr, len, state->smb1.seqnum+1)) {
1904 DEBUG(10, ("cli_check_sign_mac failed\n"));
1905 return NT_STATUS_ACCESS_DENIED;
1908 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1909 &iov, &num_iov);
1910 if (!NT_STATUS_IS_OK(status)) {
1911 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1912 nt_errstr(status)));
1913 return status;
1916 cmd = CVAL(inhdr, HDR_COM);
1917 status = smb1cli_pull_raw_error(inhdr);
1919 if (state->smb1.chained_requests == NULL) {
1920 if (num_iov != 3) {
1921 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1924 smbXcli_req_unset_pending(req);
1926 if (state->smb1.recv_iov == NULL) {
1928 * For requests with more than
1929 * one response, we have to readd the
1930 * recv_iov array.
1932 state->smb1.recv_iov = talloc_zero_array(state,
1933 struct iovec,
1935 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1936 return NT_STATUS_OK;
1940 state->smb1.recv_cmd = cmd;
1941 state->smb1.recv_status = status;
1942 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1944 state->smb1.recv_iov[0] = iov[0];
1945 state->smb1.recv_iov[1] = iov[1];
1946 state->smb1.recv_iov[2] = iov[2];
1948 if (talloc_array_length(conn->pending) == 0) {
1949 tevent_req_done(req);
1950 return NT_STATUS_OK;
1953 tevent_req_defer_callback(req, state->ev);
1954 tevent_req_done(req);
1955 return NT_STATUS_RETRY;
1958 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1959 num_chained = talloc_array_length(chain);
1960 num_responses = (num_iov - 1)/2;
1962 if (num_responses > num_chained) {
1963 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1966 for (i=0; i<num_chained; i++) {
1967 size_t iov_idx = 1 + (i*2);
1968 struct iovec *cur = &iov[iov_idx];
1969 uint8_t *inbuf_ref;
1971 req = chain[i];
1972 state = tevent_req_data(req, struct smbXcli_req_state);
1974 smbXcli_req_unset_pending(req);
1977 * as we finish multiple requests here
1978 * we need to defer the callbacks as
1979 * they could destroy our current stack state.
1981 tevent_req_defer_callback(req, state->ev);
1983 if (i >= num_responses) {
1984 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1985 continue;
1988 if (state->smb1.recv_iov == NULL) {
1990 * For requests with more than
1991 * one response, we have to readd the
1992 * recv_iov array.
1994 state->smb1.recv_iov = talloc_zero_array(state,
1995 struct iovec,
1997 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1998 continue;
2002 state->smb1.recv_cmd = cmd;
2004 if (i == (num_responses - 1)) {
2006 * The last request in the chain gets the status
2008 state->smb1.recv_status = status;
2009 } else {
2010 cmd = CVAL(cur[0].iov_base, 0);
2011 state->smb1.recv_status = NT_STATUS_OK;
2014 state->inbuf = inbuf;
2017 * Note: here we use talloc_reference() in a way
2018 * that does not expose it to the caller.
2020 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2021 if (tevent_req_nomem(inbuf_ref, req)) {
2022 continue;
2025 /* copy the related buffers */
2026 state->smb1.recv_iov[0] = iov[0];
2027 state->smb1.recv_iov[1] = cur[0];
2028 state->smb1.recv_iov[2] = cur[1];
2030 tevent_req_done(req);
2033 return NT_STATUS_RETRY;
2036 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2037 TALLOC_CTX *mem_ctx,
2038 struct iovec **piov,
2039 uint8_t **phdr,
2040 uint8_t *pwct,
2041 uint16_t **pvwv,
2042 uint32_t *pvwv_offset,
2043 uint32_t *pnum_bytes,
2044 uint8_t **pbytes,
2045 uint32_t *pbytes_offset,
2046 uint8_t **pinbuf,
2047 const struct smb1cli_req_expected_response *expected,
2048 size_t num_expected)
2050 struct smbXcli_req_state *state =
2051 tevent_req_data(req,
2052 struct smbXcli_req_state);
2053 NTSTATUS status = NT_STATUS_OK;
2054 struct iovec *recv_iov = NULL;
2055 uint8_t *hdr = NULL;
2056 uint8_t wct = 0;
2057 uint32_t vwv_offset = 0;
2058 uint16_t *vwv = NULL;
2059 uint32_t num_bytes = 0;
2060 uint32_t bytes_offset = 0;
2061 uint8_t *bytes = NULL;
2062 size_t i;
2063 bool found_status = false;
2064 bool found_size = false;
2066 if (piov != NULL) {
2067 *piov = NULL;
2069 if (phdr != NULL) {
2070 *phdr = 0;
2072 if (pwct != NULL) {
2073 *pwct = 0;
2075 if (pvwv != NULL) {
2076 *pvwv = NULL;
2078 if (pvwv_offset != NULL) {
2079 *pvwv_offset = 0;
2081 if (pnum_bytes != NULL) {
2082 *pnum_bytes = 0;
2084 if (pbytes != NULL) {
2085 *pbytes = NULL;
2087 if (pbytes_offset != NULL) {
2088 *pbytes_offset = 0;
2090 if (pinbuf != NULL) {
2091 *pinbuf = NULL;
2094 if (state->inbuf != NULL) {
2095 recv_iov = state->smb1.recv_iov;
2096 state->smb1.recv_iov = NULL;
2097 if (state->smb1.recv_cmd != SMBreadBraw) {
2098 hdr = (uint8_t *)recv_iov[0].iov_base;
2099 wct = recv_iov[1].iov_len/2;
2100 vwv = (uint16_t *)recv_iov[1].iov_base;
2101 vwv_offset = PTR_DIFF(vwv, hdr);
2102 num_bytes = recv_iov[2].iov_len;
2103 bytes = (uint8_t *)recv_iov[2].iov_base;
2104 bytes_offset = PTR_DIFF(bytes, hdr);
2108 if (tevent_req_is_nterror(req, &status)) {
2109 for (i=0; i < num_expected; i++) {
2110 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2111 found_status = true;
2112 break;
2116 if (found_status) {
2117 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2120 return status;
2123 if (num_expected == 0) {
2124 found_status = true;
2125 found_size = true;
2128 status = state->smb1.recv_status;
2130 for (i=0; i < num_expected; i++) {
2131 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2132 continue;
2135 found_status = true;
2136 if (expected[i].wct == 0) {
2137 found_size = true;
2138 break;
2141 if (expected[i].wct == wct) {
2142 found_size = true;
2143 break;
2147 if (!found_status) {
2148 return status;
2151 if (!found_size) {
2152 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2155 if (piov != NULL) {
2156 *piov = talloc_move(mem_ctx, &recv_iov);
2159 if (phdr != NULL) {
2160 *phdr = hdr;
2162 if (pwct != NULL) {
2163 *pwct = wct;
2165 if (pvwv != NULL) {
2166 *pvwv = vwv;
2168 if (pvwv_offset != NULL) {
2169 *pvwv_offset = vwv_offset;
2171 if (pnum_bytes != NULL) {
2172 *pnum_bytes = num_bytes;
2174 if (pbytes != NULL) {
2175 *pbytes = bytes;
2177 if (pbytes_offset != NULL) {
2178 *pbytes_offset = bytes_offset;
2180 if (pinbuf != NULL) {
2181 *pinbuf = state->inbuf;
2184 return status;
2187 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2189 size_t wct_ofs;
2190 int i;
2192 wct_ofs = HDR_WCT;
2194 for (i=0; i<num_reqs; i++) {
2195 struct smbXcli_req_state *state;
2196 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2197 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2198 state->smb1.iov_count-2);
2199 wct_ofs = (wct_ofs + 3) & ~3;
2201 return wct_ofs;
2204 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2206 struct smbXcli_req_state *first_state =
2207 tevent_req_data(reqs[0],
2208 struct smbXcli_req_state);
2209 struct smbXcli_req_state *state;
2210 size_t wct_offset;
2211 size_t chain_padding = 0;
2212 int i, iovlen;
2213 struct iovec *iov = NULL;
2214 struct iovec *this_iov;
2215 NTSTATUS status;
2216 size_t nbt_len;
2218 if (num_reqs == 1) {
2219 return smb1cli_req_writev_submit(reqs[0], first_state,
2220 first_state->smb1.iov,
2221 first_state->smb1.iov_count);
2224 iovlen = 0;
2225 for (i=0; i<num_reqs; i++) {
2226 if (!tevent_req_is_in_progress(reqs[i])) {
2227 return NT_STATUS_INTERNAL_ERROR;
2230 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2232 if (state->smb1.iov_count < 4) {
2233 return NT_STATUS_INVALID_PARAMETER_MIX;
2236 if (i == 0) {
2238 * The NBT and SMB header
2240 iovlen += 2;
2241 } else {
2243 * Chain padding
2245 iovlen += 1;
2249 * words and bytes
2251 iovlen += state->smb1.iov_count - 2;
2254 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2255 if (iov == NULL) {
2256 return NT_STATUS_NO_MEMORY;
2259 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2260 first_state, reqs, sizeof(*reqs) * num_reqs);
2261 if (first_state->smb1.chained_requests == NULL) {
2262 TALLOC_FREE(iov);
2263 return NT_STATUS_NO_MEMORY;
2266 wct_offset = HDR_WCT;
2267 this_iov = iov;
2269 for (i=0; i<num_reqs; i++) {
2270 size_t next_padding = 0;
2271 uint16_t *vwv;
2273 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2275 if (i < num_reqs-1) {
2276 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2277 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2278 TALLOC_FREE(iov);
2279 TALLOC_FREE(first_state->smb1.chained_requests);
2280 return NT_STATUS_INVALID_PARAMETER_MIX;
2284 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2285 state->smb1.iov_count-2) + 1;
2286 if ((wct_offset % 4) != 0) {
2287 next_padding = 4 - (wct_offset % 4);
2289 wct_offset += next_padding;
2290 vwv = state->smb1.vwv;
2292 if (i < num_reqs-1) {
2293 struct smbXcli_req_state *next_state =
2294 tevent_req_data(reqs[i+1],
2295 struct smbXcli_req_state);
2296 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2297 SCVAL(vwv+0, 1, 0);
2298 SSVAL(vwv+1, 0, wct_offset);
2299 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2300 /* properly end the chain */
2301 SCVAL(vwv+0, 0, 0xff);
2302 SCVAL(vwv+0, 1, 0xff);
2303 SSVAL(vwv+1, 0, 0);
2306 if (i == 0) {
2308 * The NBT and SMB header
2310 this_iov[0] = state->smb1.iov[0];
2311 this_iov[1] = state->smb1.iov[1];
2312 this_iov += 2;
2313 } else {
2315 * This one is a bit subtle. We have to add
2316 * chain_padding bytes between the requests, and we
2317 * have to also include the wct field of the
2318 * subsequent requests. We use the subsequent header
2319 * for the padding, it contains the wct field in its
2320 * last byte.
2322 this_iov[0].iov_len = chain_padding+1;
2323 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2324 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2325 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2326 this_iov += 1;
2330 * copy the words and bytes
2332 memcpy(this_iov, state->smb1.iov+2,
2333 sizeof(struct iovec) * (state->smb1.iov_count-2));
2334 this_iov += state->smb1.iov_count - 2;
2335 chain_padding = next_padding;
2338 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2339 if (nbt_len > first_state->conn->smb1.max_xmit) {
2340 TALLOC_FREE(iov);
2341 TALLOC_FREE(first_state->smb1.chained_requests);
2342 return NT_STATUS_INVALID_PARAMETER_MIX;
2345 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2346 if (!NT_STATUS_IS_OK(status)) {
2347 TALLOC_FREE(iov);
2348 TALLOC_FREE(first_state->smb1.chained_requests);
2349 return status;
2352 return NT_STATUS_OK;
2355 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2357 return ((tevent_queue_length(conn->outgoing) != 0)
2358 || (talloc_array_length(conn->pending) != 0));
2361 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2363 return conn->smb2.server.capabilities;
2366 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2368 return conn->smb2.server.security_mode;
2371 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2373 return conn->smb2.server.max_trans_size;
2376 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2378 return conn->smb2.server.max_read_size;
2381 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2383 return conn->smb2.server.max_write_size;
2386 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2387 uint16_t max_credits)
2389 conn->smb2.max_credits = max_credits;
2392 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2394 static bool smb2cli_req_cancel(struct tevent_req *req)
2396 struct smbXcli_req_state *state =
2397 tevent_req_data(req,
2398 struct smbXcli_req_state);
2399 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2400 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2401 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2402 struct smbXcli_tcon *tcon = state->tcon;
2403 struct smbXcli_session *session = state->session;
2404 uint8_t *fixed = state->smb2.pad;
2405 uint16_t fixed_len = 4;
2406 struct tevent_req *subreq;
2407 struct smbXcli_req_state *substate;
2408 NTSTATUS status;
2410 SSVAL(fixed, 0, 0x04);
2411 SSVAL(fixed, 2, 0);
2413 subreq = smb2cli_req_create(state, state->ev,
2414 state->conn,
2415 SMB2_OP_CANCEL,
2416 flags, 0,
2417 0, /* timeout */
2418 tcon, session,
2419 fixed, fixed_len,
2420 NULL, 0);
2421 if (subreq == NULL) {
2422 return false;
2424 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2427 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2428 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2430 flags &= SMB2_HDR_FLAG_ASYNC;
2432 if (flags & SMB2_HDR_FLAG_ASYNC) {
2433 mid = 0;
2436 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2437 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2438 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2440 status = smb2cli_req_compound_submit(&subreq, 1);
2441 if (!NT_STATUS_IS_OK(status)) {
2442 TALLOC_FREE(subreq);
2443 return false;
2446 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2448 return true;
2451 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2453 /* we do not care about the result */
2454 TALLOC_FREE(subreq);
2457 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2458 struct tevent_context *ev,
2459 struct smbXcli_conn *conn,
2460 uint16_t cmd,
2461 uint32_t additional_flags,
2462 uint32_t clear_flags,
2463 uint32_t timeout_msec,
2464 struct smbXcli_tcon *tcon,
2465 struct smbXcli_session *session,
2466 const uint8_t *fixed,
2467 uint16_t fixed_len,
2468 const uint8_t *dyn,
2469 uint32_t dyn_len)
2471 struct tevent_req *req;
2472 struct smbXcli_req_state *state;
2473 uint32_t flags = 0;
2474 uint32_t tid = 0;
2475 uint64_t uid = 0;
2476 bool use_channel_sequence = false;
2477 uint16_t channel_sequence = 0;
2479 req = tevent_req_create(mem_ctx, &state,
2480 struct smbXcli_req_state);
2481 if (req == NULL) {
2482 return NULL;
2485 state->ev = ev;
2486 state->conn = conn;
2487 state->session = session;
2488 state->tcon = tcon;
2490 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2491 use_channel_sequence = true;
2492 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2493 use_channel_sequence = true;
2496 if (session) {
2497 uid = session->smb2->session_id;
2499 if (use_channel_sequence) {
2500 channel_sequence = session->smb2->channel_sequence;
2503 state->smb2.should_sign = session->smb2->should_sign;
2504 state->smb2.should_encrypt = session->smb2->should_encrypt;
2506 if (cmd == SMB2_OP_SESSSETUP &&
2507 session->smb2->signing_key.length != 0) {
2508 state->smb2.should_sign = true;
2511 if (cmd == SMB2_OP_SESSSETUP &&
2512 session->smb2_channel.signing_key.length == 0) {
2513 state->smb2.should_encrypt = false;
2517 if (tcon) {
2518 tid = tcon->smb2.tcon_id;
2520 if (tcon->smb2.should_encrypt) {
2521 state->smb2.should_encrypt = true;
2525 if (state->smb2.should_encrypt) {
2526 state->smb2.should_sign = false;
2529 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2530 if (state->smb2.recv_iov == NULL) {
2531 TALLOC_FREE(req);
2532 return NULL;
2535 flags |= additional_flags;
2536 flags &= ~clear_flags;
2538 state->smb2.fixed = fixed;
2539 state->smb2.fixed_len = fixed_len;
2540 state->smb2.dyn = dyn;
2541 state->smb2.dyn_len = dyn_len;
2543 if (state->smb2.should_encrypt) {
2544 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2545 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2548 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2549 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2550 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2551 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2552 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2553 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2554 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2555 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2557 switch (cmd) {
2558 case SMB2_OP_CANCEL:
2559 state->one_way = true;
2560 break;
2561 case SMB2_OP_BREAK:
2563 * If this is a dummy request, it will have
2564 * UINT64_MAX as message id.
2565 * If we send on break acknowledgement,
2566 * this gets overwritten later.
2568 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2569 break;
2572 if (timeout_msec > 0) {
2573 struct timeval endtime;
2575 endtime = timeval_current_ofs_msec(timeout_msec);
2576 if (!tevent_req_set_endtime(req, ev, endtime)) {
2577 return req;
2581 return req;
2584 void smb2cli_req_set_notify_async(struct tevent_req *req)
2586 struct smbXcli_req_state *state =
2587 tevent_req_data(req,
2588 struct smbXcli_req_state);
2590 state->smb2.notify_async = true;
2593 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2594 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2595 TALLOC_CTX *tmp_mem,
2596 uint8_t *inbuf);
2598 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2599 int num_reqs)
2601 struct smbXcli_req_state *state;
2602 struct tevent_req *subreq;
2603 struct iovec *iov;
2604 int i, num_iov, nbt_len;
2605 int tf_iov = -1;
2606 const DATA_BLOB *encryption_key = NULL;
2607 uint64_t encryption_session_id = 0;
2610 * 1 for the nbt length, optional TRANSFORM
2611 * per request: HDR, fixed, dyn, padding
2612 * -1 because the last one does not need padding
2615 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2616 if (iov == NULL) {
2617 return NT_STATUS_NO_MEMORY;
2620 num_iov = 1;
2621 nbt_len = 0;
2624 * the session of the first request that requires encryption
2625 * specifies the encryption key.
2627 for (i=0; i<num_reqs; i++) {
2628 if (!tevent_req_is_in_progress(reqs[i])) {
2629 return NT_STATUS_INTERNAL_ERROR;
2632 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2634 if (!smbXcli_conn_is_connected(state->conn)) {
2635 return NT_STATUS_CONNECTION_DISCONNECTED;
2638 if ((state->conn->protocol != PROTOCOL_NONE) &&
2639 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2640 return NT_STATUS_REVISION_MISMATCH;
2643 if (state->session == NULL) {
2644 continue;
2647 if (!state->smb2.should_encrypt) {
2648 continue;
2651 encryption_key = &state->session->smb2->encryption_key;
2652 if (encryption_key->length == 0) {
2653 return NT_STATUS_INVALID_PARAMETER_MIX;
2656 encryption_session_id = state->session->smb2->session_id;
2658 tf_iov = num_iov;
2659 iov[num_iov].iov_base = state->smb2.transform;
2660 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2661 num_iov += 1;
2663 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2664 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2665 state->session->smb2->nonce_low);
2666 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2667 state->session->smb2->nonce_high);
2668 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2669 encryption_session_id);
2671 state->session->smb2->nonce_low += 1;
2672 if (state->session->smb2->nonce_low == 0) {
2673 state->session->smb2->nonce_high += 1;
2674 state->session->smb2->nonce_low += 1;
2677 nbt_len += SMB2_TF_HDR_SIZE;
2678 break;
2681 for (i=0; i<num_reqs; i++) {
2682 int hdr_iov;
2683 size_t reqlen;
2684 bool ret;
2685 uint16_t opcode;
2686 uint64_t avail;
2687 uint16_t charge;
2688 uint16_t credits;
2689 uint64_t mid;
2690 const DATA_BLOB *signing_key = NULL;
2692 if (!tevent_req_is_in_progress(reqs[i])) {
2693 return NT_STATUS_INTERNAL_ERROR;
2696 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2698 if (!smbXcli_conn_is_connected(state->conn)) {
2699 return NT_STATUS_CONNECTION_DISCONNECTED;
2702 if ((state->conn->protocol != PROTOCOL_NONE) &&
2703 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2704 return NT_STATUS_REVISION_MISMATCH;
2707 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2708 if (opcode == SMB2_OP_CANCEL) {
2709 goto skip_credits;
2712 avail = UINT64_MAX - state->conn->smb2.mid;
2713 if (avail < 1) {
2714 return NT_STATUS_CONNECTION_ABORTED;
2717 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2718 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2719 } else {
2720 charge = 1;
2723 charge = MAX(state->smb2.credit_charge, charge);
2725 avail = MIN(avail, state->conn->smb2.cur_credits);
2726 if (avail < charge) {
2727 return NT_STATUS_INTERNAL_ERROR;
2730 credits = 0;
2731 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2732 credits = state->conn->smb2.max_credits -
2733 state->conn->smb2.cur_credits;
2735 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2736 credits += 1;
2739 mid = state->conn->smb2.mid;
2740 state->conn->smb2.mid += charge;
2741 state->conn->smb2.cur_credits -= charge;
2743 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2744 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2746 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2747 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2749 skip_credits:
2750 if (state->session && encryption_key == NULL) {
2752 * We prefer the channel signing key if it is
2753 * already there.
2755 if (state->smb2.should_sign) {
2756 signing_key = &state->session->smb2_channel.signing_key;
2760 * If it is a channel binding, we already have the main
2761 * signing key and try that one.
2763 if (signing_key && signing_key->length == 0) {
2764 signing_key = &state->session->smb2->signing_key;
2768 * If we do not have any session key yet, we skip the
2769 * signing of SMB2_OP_SESSSETUP requests.
2771 if (signing_key && signing_key->length == 0) {
2772 signing_key = NULL;
2776 hdr_iov = num_iov;
2777 iov[num_iov].iov_base = state->smb2.hdr;
2778 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2779 num_iov += 1;
2781 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2782 iov[num_iov].iov_len = state->smb2.fixed_len;
2783 num_iov += 1;
2785 if (state->smb2.dyn != NULL) {
2786 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2787 iov[num_iov].iov_len = state->smb2.dyn_len;
2788 num_iov += 1;
2791 reqlen = sizeof(state->smb2.hdr);
2792 reqlen += state->smb2.fixed_len;
2793 reqlen += state->smb2.dyn_len;
2795 if (i < num_reqs-1) {
2796 if ((reqlen % 8) > 0) {
2797 uint8_t pad = 8 - (reqlen % 8);
2798 iov[num_iov].iov_base = state->smb2.pad;
2799 iov[num_iov].iov_len = pad;
2800 num_iov += 1;
2801 reqlen += pad;
2803 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2806 state->smb2.encryption_session_id = encryption_session_id;
2808 if (signing_key != NULL) {
2809 NTSTATUS status;
2811 status = smb2_signing_sign_pdu(*signing_key,
2812 state->session->conn->protocol,
2813 &iov[hdr_iov], num_iov - hdr_iov);
2814 if (!NT_STATUS_IS_OK(status)) {
2815 return status;
2819 nbt_len += reqlen;
2821 ret = smbXcli_req_set_pending(reqs[i]);
2822 if (!ret) {
2823 return NT_STATUS_NO_MEMORY;
2827 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2828 _smb_setlen_tcp(state->length_hdr, nbt_len);
2829 iov[0].iov_base = state->length_hdr;
2830 iov[0].iov_len = sizeof(state->length_hdr);
2832 if (encryption_key != NULL) {
2833 NTSTATUS status;
2834 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
2835 uint8_t *buf;
2836 int vi;
2838 buf = talloc_array(iov, uint8_t, buflen);
2839 if (buf == NULL) {
2840 return NT_STATUS_NO_MEMORY;
2844 * We copy the buffers before encrypting them,
2845 * this is at least currently needed for the
2846 * to keep state->smb2.hdr.
2848 * Also the callers may expect there buffers
2849 * to be const.
2851 for (vi = tf_iov + 1; vi < num_iov; vi++) {
2852 struct iovec *v = &iov[vi];
2853 const uint8_t *o = (const uint8_t *)v->iov_base;
2855 memcpy(buf, o, v->iov_len);
2856 v->iov_base = (void *)buf;
2857 buf += v->iov_len;
2860 status = smb2_signing_encrypt_pdu(*encryption_key,
2861 state->conn->protocol,
2862 &iov[tf_iov], num_iov - tf_iov);
2863 if (!NT_STATUS_IS_OK(status)) {
2864 return status;
2868 if (state->conn->dispatch_incoming == NULL) {
2869 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2872 subreq = writev_send(state, state->ev, state->conn->outgoing,
2873 state->conn->write_fd, false, iov, num_iov);
2874 if (subreq == NULL) {
2875 return NT_STATUS_NO_MEMORY;
2877 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2878 return NT_STATUS_OK;
2881 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2883 struct smbXcli_req_state *state =
2884 tevent_req_data(req,
2885 struct smbXcli_req_state);
2887 state->smb2.credit_charge = charge;
2890 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2891 struct tevent_context *ev,
2892 struct smbXcli_conn *conn,
2893 uint16_t cmd,
2894 uint32_t additional_flags,
2895 uint32_t clear_flags,
2896 uint32_t timeout_msec,
2897 struct smbXcli_tcon *tcon,
2898 struct smbXcli_session *session,
2899 const uint8_t *fixed,
2900 uint16_t fixed_len,
2901 const uint8_t *dyn,
2902 uint32_t dyn_len)
2904 struct tevent_req *req;
2905 NTSTATUS status;
2907 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2908 additional_flags, clear_flags,
2909 timeout_msec,
2910 tcon, session,
2911 fixed, fixed_len, dyn, dyn_len);
2912 if (req == NULL) {
2913 return NULL;
2915 if (!tevent_req_is_in_progress(req)) {
2916 return tevent_req_post(req, ev);
2918 status = smb2cli_req_compound_submit(&req, 1);
2919 if (tevent_req_nterror(req, status)) {
2920 return tevent_req_post(req, ev);
2922 return req;
2925 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2927 struct tevent_req *req =
2928 tevent_req_callback_data(subreq,
2929 struct tevent_req);
2930 struct smbXcli_req_state *state =
2931 tevent_req_data(req,
2932 struct smbXcli_req_state);
2933 ssize_t nwritten;
2934 int err;
2936 nwritten = writev_recv(subreq, &err);
2937 TALLOC_FREE(subreq);
2938 if (nwritten == -1) {
2939 /* here, we need to notify all pending requests */
2940 NTSTATUS status = map_nt_error_from_unix_common(err);
2941 smbXcli_conn_disconnect(state->conn, status);
2942 return;
2946 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
2947 uint8_t *buf,
2948 size_t buflen,
2949 TALLOC_CTX *mem_ctx,
2950 struct iovec **piov, int *pnum_iov)
2952 struct iovec *iov;
2953 int num_iov = 0;
2954 size_t taken = 0;
2955 uint8_t *first_hdr = buf;
2956 size_t verified_buflen = 0;
2957 uint8_t *tf = NULL;
2958 size_t tf_len = 0;
2960 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2961 if (iov == NULL) {
2962 return NT_STATUS_NO_MEMORY;
2965 while (taken < buflen) {
2966 size_t len = buflen - taken;
2967 uint8_t *hdr = first_hdr + taken;
2968 struct iovec *cur;
2969 size_t full_size;
2970 size_t next_command_ofs;
2971 uint16_t body_size;
2972 struct iovec *iov_tmp;
2974 if (verified_buflen > taken) {
2975 len = verified_buflen - taken;
2976 } else {
2977 tf = NULL;
2978 tf_len = 0;
2981 if (len < 4) {
2982 DEBUG(10, ("%d bytes left, expected at least %d\n",
2983 (int)len, 4));
2984 goto inval;
2986 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
2987 struct smbXcli_session *s;
2988 uint64_t uid;
2989 struct iovec tf_iov[2];
2990 NTSTATUS status;
2992 if (len < SMB2_TF_HDR_SIZE) {
2993 DEBUG(10, ("%d bytes left, expected at least %d\n",
2994 (int)len, SMB2_TF_HDR_SIZE));
2995 goto inval;
2997 tf = hdr;
2998 tf_len = SMB2_TF_HDR_SIZE;
2999 taken += tf_len;
3001 hdr = first_hdr + taken;
3002 len = IVAL(tf, SMB2_TF_MSG_SIZE);
3003 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3005 s = conn->sessions;
3006 for (; s; s = s->next) {
3007 if (s->smb2->session_id != uid) {
3008 continue;
3010 break;
3013 if (s == NULL) {
3014 DEBUG(10, ("unknown session_id %llu\n",
3015 (unsigned long long)uid));
3016 goto inval;
3019 tf_iov[0].iov_base = (void *)tf;
3020 tf_iov[0].iov_len = tf_len;
3021 tf_iov[1].iov_base = (void *)hdr;
3022 tf_iov[1].iov_len = len;
3024 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3025 conn->protocol,
3026 tf_iov, 2);
3027 if (!NT_STATUS_IS_OK(status)) {
3028 TALLOC_FREE(iov);
3029 return status;
3032 verified_buflen = taken + len;
3036 * We need the header plus the body length field
3039 if (len < SMB2_HDR_BODY + 2) {
3040 DEBUG(10, ("%d bytes left, expected at least %d\n",
3041 (int)len, SMB2_HDR_BODY));
3042 goto inval;
3044 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3045 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3046 IVAL(hdr, 0)));
3047 goto inval;
3049 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3050 DEBUG(10, ("Got HDR len %d, expected %d\n",
3051 SVAL(hdr, 4), SMB2_HDR_BODY));
3052 goto inval;
3055 full_size = len;
3056 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3057 body_size = SVAL(hdr, SMB2_HDR_BODY);
3059 if (next_command_ofs != 0) {
3060 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3061 goto inval;
3063 if (next_command_ofs > full_size) {
3064 goto inval;
3066 full_size = next_command_ofs;
3068 if (body_size < 2) {
3069 goto inval;
3071 body_size &= 0xfffe;
3073 if (body_size > (full_size - SMB2_HDR_BODY)) {
3074 goto inval;
3077 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3078 num_iov + 4);
3079 if (iov_tmp == NULL) {
3080 TALLOC_FREE(iov);
3081 return NT_STATUS_NO_MEMORY;
3083 iov = iov_tmp;
3084 cur = &iov[num_iov];
3085 num_iov += 4;
3087 cur[0].iov_base = tf;
3088 cur[0].iov_len = tf_len;
3089 cur[1].iov_base = hdr;
3090 cur[1].iov_len = SMB2_HDR_BODY;
3091 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3092 cur[2].iov_len = body_size;
3093 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3094 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3096 taken += full_size;
3099 *piov = iov;
3100 *pnum_iov = num_iov;
3101 return NT_STATUS_OK;
3103 inval:
3104 TALLOC_FREE(iov);
3105 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3108 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3109 uint64_t mid)
3111 size_t num_pending = talloc_array_length(conn->pending);
3112 size_t i;
3114 for (i=0; i<num_pending; i++) {
3115 struct tevent_req *req = conn->pending[i];
3116 struct smbXcli_req_state *state =
3117 tevent_req_data(req,
3118 struct smbXcli_req_state);
3120 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3121 return req;
3124 return NULL;
3127 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3128 TALLOC_CTX *tmp_mem,
3129 uint8_t *inbuf)
3131 struct tevent_req *req;
3132 struct smbXcli_req_state *state = NULL;
3133 struct iovec *iov;
3134 int i, num_iov;
3135 NTSTATUS status;
3136 bool defer = true;
3137 struct smbXcli_session *last_session = NULL;
3138 size_t inbuf_len = smb_len_tcp(inbuf);
3140 status = smb2cli_inbuf_parse_compound(conn,
3141 inbuf + NBT_HDR_SIZE,
3142 inbuf_len,
3143 tmp_mem,
3144 &iov, &num_iov);
3145 if (!NT_STATUS_IS_OK(status)) {
3146 return status;
3149 for (i=0; i<num_iov; i+=4) {
3150 uint8_t *inbuf_ref = NULL;
3151 struct iovec *cur = &iov[i];
3152 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3153 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3154 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3155 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3156 uint16_t req_opcode;
3157 uint32_t req_flags;
3158 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3159 uint32_t new_credits;
3160 struct smbXcli_session *session = NULL;
3161 const DATA_BLOB *signing_key = NULL;
3162 bool was_encrypted = false;
3164 new_credits = conn->smb2.cur_credits;
3165 new_credits += credits;
3166 if (new_credits > UINT16_MAX) {
3167 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3169 conn->smb2.cur_credits += credits;
3171 req = smb2cli_conn_find_pending(conn, mid);
3172 if (req == NULL) {
3173 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3175 state = tevent_req_data(req, struct smbXcli_req_state);
3177 state->smb2.got_async = false;
3179 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3180 if (opcode != req_opcode) {
3181 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3183 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3185 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3186 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3189 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3190 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3191 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3192 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3195 * async interim responses are not signed,
3196 * even if the SMB2_HDR_FLAG_SIGNED flag
3197 * is set.
3199 req_flags |= SMB2_HDR_FLAG_ASYNC;
3200 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3201 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3203 if (state->smb2.notify_async) {
3204 state->smb2.got_async = true;
3205 tevent_req_defer_callback(req, state->ev);
3206 tevent_req_notify_callback(req);
3208 continue;
3211 session = state->session;
3212 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3213 session = last_session;
3215 last_session = session;
3217 if (state->smb2.should_sign) {
3218 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3219 return NT_STATUS_ACCESS_DENIED;
3223 if (flags & SMB2_HDR_FLAG_SIGNED) {
3224 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3226 if (session == NULL) {
3227 struct smbXcli_session *s;
3229 s = state->conn->sessions;
3230 for (; s; s = s->next) {
3231 if (s->smb2->session_id != uid) {
3232 continue;
3235 session = s;
3236 break;
3240 if (session == NULL) {
3241 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3244 last_session = session;
3245 signing_key = &session->smb2_channel.signing_key;
3248 if (opcode == SMB2_OP_SESSSETUP) {
3250 * We prefer the channel signing key, if it is
3251 * already there.
3253 * If we do not have a channel signing key yet,
3254 * we try the main signing key, if it is not
3255 * the final response.
3257 if (signing_key && signing_key->length == 0 &&
3258 !NT_STATUS_IS_OK(status)) {
3259 signing_key = &session->smb2->signing_key;
3262 if (signing_key && signing_key->length == 0) {
3264 * If we do not have a session key to
3265 * verify the signature, we defer the
3266 * signing check to the caller.
3268 * The caller gets NT_STATUS_OK, it
3269 * has to call
3270 * smb2cli_session_set_session_key()
3271 * or
3272 * smb2cli_session_set_channel_key()
3273 * which will check the signature
3274 * with the channel signing key.
3276 signing_key = NULL;
3280 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3281 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3282 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3285 * If the response was encrypted in a SMB2_TRANSFORM
3286 * pdu, which belongs to the correct session,
3287 * we do not need to do signing checks
3289 * It could be the session the response belongs to
3290 * or the session that was used to encrypt the
3291 * SMB2_TRANSFORM request.
3293 if ((session && session->smb2->session_id == uid) ||
3294 (state->smb2.encryption_session_id == uid)) {
3295 signing_key = NULL;
3296 was_encrypted = true;
3300 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3302 * if the server returns NT_STATUS_USER_SESSION_DELETED
3303 * the response is not signed and we should
3304 * propagate the NT_STATUS_USER_SESSION_DELETED
3305 * status to the caller.
3307 state->smb2.signing_skipped = true;
3308 signing_key = NULL;
3311 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3313 * if the server returns
3314 * NT_STATUS_INVALID_PARAMETER
3315 * the response might not be encrypted.
3317 if (state->smb2.should_encrypt && !was_encrypted) {
3318 state->smb2.signing_skipped = true;
3319 signing_key = NULL;
3323 if (state->smb2.should_encrypt && !was_encrypted) {
3324 if (!state->smb2.signing_skipped) {
3325 return NT_STATUS_ACCESS_DENIED;
3329 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3330 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3331 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3333 * if the server returns
3334 * NT_STATUS_NETWORK_NAME_DELETED
3335 * NT_STATUS_FILE_CLOSED
3336 * NT_STATUS_INVALID_PARAMETER
3337 * the response might not be signed
3338 * as this happens before the signing checks.
3340 * If server echos the signature (or all zeros)
3341 * we should report the status from the server
3342 * to the caller.
3344 if (signing_key) {
3345 int cmp;
3347 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3348 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3349 16);
3350 if (cmp == 0) {
3351 state->smb2.signing_skipped = true;
3352 signing_key = NULL;
3355 if (signing_key) {
3356 int cmp;
3357 static const uint8_t zeros[16];
3359 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3360 zeros,
3361 16);
3362 if (cmp == 0) {
3363 state->smb2.signing_skipped = true;
3364 signing_key = NULL;
3369 if (signing_key) {
3370 status = smb2_signing_check_pdu(*signing_key,
3371 state->conn->protocol,
3372 &cur[1], 3);
3373 if (!NT_STATUS_IS_OK(status)) {
3375 * If the signing check fails, we disconnect
3376 * the connection.
3378 return status;
3382 smbXcli_req_unset_pending(req);
3385 * There might be more than one response
3386 * we need to defer the notifications
3388 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3389 defer = false;
3392 if (defer) {
3393 tevent_req_defer_callback(req, state->ev);
3397 * Note: here we use talloc_reference() in a way
3398 * that does not expose it to the caller.
3400 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3401 if (tevent_req_nomem(inbuf_ref, req)) {
3402 continue;
3405 /* copy the related buffers */
3406 state->smb2.recv_iov[0] = cur[1];
3407 state->smb2.recv_iov[1] = cur[2];
3408 state->smb2.recv_iov[2] = cur[3];
3410 tevent_req_done(req);
3413 if (defer) {
3414 return NT_STATUS_RETRY;
3417 return NT_STATUS_OK;
3420 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3421 struct iovec **piov,
3422 const struct smb2cli_req_expected_response *expected,
3423 size_t num_expected)
3425 struct smbXcli_req_state *state =
3426 tevent_req_data(req,
3427 struct smbXcli_req_state);
3428 NTSTATUS status;
3429 size_t body_size;
3430 bool found_status = false;
3431 bool found_size = false;
3432 size_t i;
3434 if (piov != NULL) {
3435 *piov = NULL;
3438 if (state->smb2.got_async) {
3439 return STATUS_PENDING;
3442 if (tevent_req_is_nterror(req, &status)) {
3443 for (i=0; i < num_expected; i++) {
3444 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3445 found_status = true;
3446 break;
3450 if (found_status) {
3451 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3454 return status;
3457 if (num_expected == 0) {
3458 found_status = true;
3459 found_size = true;
3462 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3463 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3465 for (i=0; i < num_expected; i++) {
3466 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3467 continue;
3470 found_status = true;
3471 if (expected[i].body_size == 0) {
3472 found_size = true;
3473 break;
3476 if (expected[i].body_size == body_size) {
3477 found_size = true;
3478 break;
3482 if (!found_status) {
3483 return status;
3486 if (state->smb2.signing_skipped) {
3487 if (num_expected > 0) {
3488 return NT_STATUS_ACCESS_DENIED;
3490 if (!NT_STATUS_IS_ERR(status)) {
3491 return NT_STATUS_ACCESS_DENIED;
3495 if (!found_size) {
3496 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3499 if (piov != NULL) {
3500 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3503 return status;
3506 static const struct {
3507 enum protocol_types proto;
3508 const char *smb1_name;
3509 } smb1cli_prots[] = {
3510 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3511 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3512 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3513 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3514 {PROTOCOL_LANMAN2, "LM1.2X002"},
3515 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3516 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3517 {PROTOCOL_LANMAN2, "Samba"},
3518 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3519 {PROTOCOL_NT1, "NT LM 0.12"},
3520 {PROTOCOL_SMB2_02, "SMB 2.002"},
3521 {PROTOCOL_SMB2_10, "SMB 2.???"},
3524 static const struct {
3525 enum protocol_types proto;
3526 uint16_t smb2_dialect;
3527 } smb2cli_prots[] = {
3528 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3529 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3530 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3531 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3532 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3535 struct smbXcli_negprot_state {
3536 struct smbXcli_conn *conn;
3537 struct tevent_context *ev;
3538 uint32_t timeout_msec;
3539 enum protocol_types min_protocol;
3540 enum protocol_types max_protocol;
3542 struct {
3543 uint8_t fixed[36];
3544 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3545 } smb2;
3548 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3549 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3550 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3551 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3552 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3553 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3554 TALLOC_CTX *frame,
3555 uint8_t *inbuf);
3557 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3558 struct tevent_context *ev,
3559 struct smbXcli_conn *conn,
3560 uint32_t timeout_msec,
3561 enum protocol_types min_protocol,
3562 enum protocol_types max_protocol)
3564 struct tevent_req *req, *subreq;
3565 struct smbXcli_negprot_state *state;
3567 req = tevent_req_create(mem_ctx, &state,
3568 struct smbXcli_negprot_state);
3569 if (req == NULL) {
3570 return NULL;
3572 state->conn = conn;
3573 state->ev = ev;
3574 state->timeout_msec = timeout_msec;
3575 state->min_protocol = min_protocol;
3576 state->max_protocol = max_protocol;
3578 if (min_protocol == PROTOCOL_NONE) {
3579 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3580 return tevent_req_post(req, ev);
3583 if (max_protocol == PROTOCOL_NONE) {
3584 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3585 return tevent_req_post(req, ev);
3588 if (min_protocol > max_protocol) {
3589 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3590 return tevent_req_post(req, ev);
3593 if ((min_protocol < PROTOCOL_SMB2_02) &&
3594 (max_protocol < PROTOCOL_SMB2_02)) {
3596 * SMB1 only...
3598 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3600 subreq = smbXcli_negprot_smb1_subreq(state);
3601 if (tevent_req_nomem(subreq, req)) {
3602 return tevent_req_post(req, ev);
3604 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3605 return req;
3608 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3609 (max_protocol >= PROTOCOL_SMB2_02)) {
3611 * SMB2 only...
3613 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3615 subreq = smbXcli_negprot_smb2_subreq(state);
3616 if (tevent_req_nomem(subreq, req)) {
3617 return tevent_req_post(req, ev);
3619 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3620 return req;
3624 * We send an SMB1 negprot with the SMB2 dialects
3625 * and expect a SMB1 or a SMB2 response.
3627 * smbXcli_negprot_dispatch_incoming() will fix the
3628 * callback to match protocol of the response.
3630 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3632 subreq = smbXcli_negprot_smb1_subreq(state);
3633 if (tevent_req_nomem(subreq, req)) {
3634 return tevent_req_post(req, ev);
3636 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3637 return req;
3640 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3642 struct tevent_req *req =
3643 tevent_req_callback_data(subreq,
3644 struct tevent_req);
3645 NTSTATUS status;
3648 * we just want the low level error
3650 status = tevent_req_simple_recv_ntstatus(subreq);
3651 TALLOC_FREE(subreq);
3652 if (tevent_req_nterror(req, status)) {
3653 return;
3656 /* this should never happen */
3657 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3660 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3662 size_t i;
3663 DATA_BLOB bytes = data_blob_null;
3664 uint8_t flags;
3665 uint16_t flags2;
3667 /* setup the protocol strings */
3668 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3669 uint8_t c = 2;
3670 bool ok;
3672 if (smb1cli_prots[i].proto < state->min_protocol) {
3673 continue;
3676 if (smb1cli_prots[i].proto > state->max_protocol) {
3677 continue;
3680 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3681 if (!ok) {
3682 return NULL;
3686 * We now it is already ascii and
3687 * we want NULL termination.
3689 ok = data_blob_append(state, &bytes,
3690 smb1cli_prots[i].smb1_name,
3691 strlen(smb1cli_prots[i].smb1_name)+1);
3692 if (!ok) {
3693 return NULL;
3697 smb1cli_req_flags(state->max_protocol,
3698 state->conn->smb1.client.capabilities,
3699 SMBnegprot,
3700 0, 0, &flags,
3701 0, 0, &flags2);
3703 return smb1cli_req_send(state, state->ev, state->conn,
3704 SMBnegprot,
3705 flags, ~flags,
3706 flags2, ~flags2,
3707 state->timeout_msec,
3708 0xFFFE, 0, NULL, /* pid, tid, session */
3709 0, NULL, /* wct, vwv */
3710 bytes.length, bytes.data);
3713 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3715 struct tevent_req *req =
3716 tevent_req_callback_data(subreq,
3717 struct tevent_req);
3718 struct smbXcli_negprot_state *state =
3719 tevent_req_data(req,
3720 struct smbXcli_negprot_state);
3721 struct smbXcli_conn *conn = state->conn;
3722 struct iovec *recv_iov = NULL;
3723 uint8_t *inhdr;
3724 uint8_t wct;
3725 uint16_t *vwv;
3726 uint32_t num_bytes;
3727 uint8_t *bytes;
3728 NTSTATUS status;
3729 uint16_t protnum;
3730 size_t i;
3731 size_t num_prots = 0;
3732 uint8_t flags;
3733 uint32_t client_capabilities = conn->smb1.client.capabilities;
3734 uint32_t both_capabilities;
3735 uint32_t server_capabilities = 0;
3736 uint32_t capabilities;
3737 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3738 uint32_t server_max_xmit = 0;
3739 uint32_t max_xmit;
3740 uint32_t server_max_mux = 0;
3741 uint16_t server_security_mode = 0;
3742 uint32_t server_session_key = 0;
3743 bool server_readbraw = false;
3744 bool server_writebraw = false;
3745 bool server_lockread = false;
3746 bool server_writeunlock = false;
3747 struct GUID server_guid = GUID_zero();
3748 DATA_BLOB server_gss_blob = data_blob_null;
3749 uint8_t server_challenge[8];
3750 char *server_workgroup = NULL;
3751 char *server_name = NULL;
3752 int server_time_zone = 0;
3753 NTTIME server_system_time = 0;
3754 static const struct smb1cli_req_expected_response expected[] = {
3756 .status = NT_STATUS_OK,
3757 .wct = 0x11, /* NT1 */
3760 .status = NT_STATUS_OK,
3761 .wct = 0x0D, /* LM */
3764 .status = NT_STATUS_OK,
3765 .wct = 0x01, /* CORE */
3769 ZERO_STRUCT(server_challenge);
3771 status = smb1cli_req_recv(subreq, state,
3772 &recv_iov,
3773 &inhdr,
3774 &wct,
3775 &vwv,
3776 NULL, /* pvwv_offset */
3777 &num_bytes,
3778 &bytes,
3779 NULL, /* pbytes_offset */
3780 NULL, /* pinbuf */
3781 expected, ARRAY_SIZE(expected));
3782 TALLOC_FREE(subreq);
3783 if (tevent_req_nterror(req, status)) {
3784 return;
3787 flags = CVAL(inhdr, HDR_FLG);
3789 protnum = SVAL(vwv, 0);
3791 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3792 if (smb1cli_prots[i].proto < state->min_protocol) {
3793 continue;
3796 if (smb1cli_prots[i].proto > state->max_protocol) {
3797 continue;
3800 if (protnum != num_prots) {
3801 num_prots++;
3802 continue;
3805 conn->protocol = smb1cli_prots[i].proto;
3806 break;
3809 if (conn->protocol == PROTOCOL_NONE) {
3810 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3811 return;
3814 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3815 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3816 "and the selected protocol level doesn't support it.\n"));
3817 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3818 return;
3821 if (flags & FLAG_SUPPORT_LOCKREAD) {
3822 server_lockread = true;
3823 server_writeunlock = true;
3826 if (conn->protocol >= PROTOCOL_NT1) {
3827 const char *client_signing = NULL;
3828 bool server_mandatory = false;
3829 bool server_allowed = false;
3830 const char *server_signing = NULL;
3831 bool ok;
3832 uint8_t key_len;
3834 if (wct != 0x11) {
3835 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3836 return;
3839 /* NT protocol */
3840 server_security_mode = CVAL(vwv + 1, 0);
3841 server_max_mux = SVAL(vwv + 1, 1);
3842 server_max_xmit = IVAL(vwv + 3, 1);
3843 server_session_key = IVAL(vwv + 7, 1);
3844 server_time_zone = SVALS(vwv + 15, 1);
3845 server_time_zone *= 60;
3846 /* this time arrives in real GMT */
3847 server_system_time = BVAL(vwv + 11, 1);
3848 server_capabilities = IVAL(vwv + 9, 1);
3850 key_len = CVAL(vwv + 16, 1);
3852 if (server_capabilities & CAP_RAW_MODE) {
3853 server_readbraw = true;
3854 server_writebraw = true;
3856 if (server_capabilities & CAP_LOCK_AND_READ) {
3857 server_lockread = true;
3860 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3861 DATA_BLOB blob1, blob2;
3863 if (num_bytes < 16) {
3864 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3865 return;
3868 blob1 = data_blob_const(bytes, 16);
3869 status = GUID_from_data_blob(&blob1, &server_guid);
3870 if (tevent_req_nterror(req, status)) {
3871 return;
3874 blob1 = data_blob_const(bytes+16, num_bytes-16);
3875 blob2 = data_blob_dup_talloc(state, blob1);
3876 if (blob1.length > 0 &&
3877 tevent_req_nomem(blob2.data, req)) {
3878 return;
3880 server_gss_blob = blob2;
3881 } else {
3882 DATA_BLOB blob1, blob2;
3884 if (num_bytes < key_len) {
3885 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3886 return;
3889 if (key_len != 0 && key_len != 8) {
3890 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3891 return;
3894 if (key_len == 8) {
3895 memcpy(server_challenge, bytes, 8);
3898 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3899 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3900 if (blob1.length > 0) {
3901 size_t len;
3903 len = utf16_len_n(blob1.data,
3904 blob1.length);
3905 blob1.length = len;
3907 ok = convert_string_talloc(state,
3908 CH_UTF16LE,
3909 CH_UNIX,
3910 blob1.data,
3911 blob1.length,
3912 &server_workgroup,
3913 &len);
3914 if (!ok) {
3915 status = map_nt_error_from_unix_common(errno);
3916 tevent_req_nterror(req, status);
3917 return;
3921 blob2.data += blob1.length;
3922 blob2.length -= blob1.length;
3923 if (blob2.length > 0) {
3924 size_t len;
3926 len = utf16_len_n(blob1.data,
3927 blob1.length);
3928 blob1.length = len;
3930 ok = convert_string_talloc(state,
3931 CH_UTF16LE,
3932 CH_UNIX,
3933 blob2.data,
3934 blob2.length,
3935 &server_name,
3936 &len);
3937 if (!ok) {
3938 status = map_nt_error_from_unix_common(errno);
3939 tevent_req_nterror(req, status);
3940 return;
3945 client_signing = "disabled";
3946 if (conn->allow_signing) {
3947 client_signing = "allowed";
3949 if (conn->mandatory_signing) {
3950 client_signing = "required";
3953 server_signing = "not supported";
3954 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3955 server_signing = "supported";
3956 server_allowed = true;
3958 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3959 server_signing = "required";
3960 server_mandatory = true;
3963 ok = smb_signing_set_negotiated(conn->smb1.signing,
3964 server_allowed,
3965 server_mandatory);
3966 if (!ok) {
3967 DEBUG(1,("cli_negprot: SMB signing is required, "
3968 "but client[%s] and server[%s] mismatch\n",
3969 client_signing, server_signing));
3970 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3971 return;
3974 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3975 DATA_BLOB blob1;
3976 uint8_t key_len;
3977 time_t t;
3979 if (wct != 0x0D) {
3980 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3981 return;
3984 server_security_mode = SVAL(vwv + 1, 0);
3985 server_max_xmit = SVAL(vwv + 2, 0);
3986 server_max_mux = SVAL(vwv + 3, 0);
3987 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3988 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3989 server_session_key = IVAL(vwv + 6, 0);
3990 server_time_zone = SVALS(vwv + 10, 0);
3991 server_time_zone *= 60;
3992 /* this time is converted to GMT by make_unix_date */
3993 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3994 unix_to_nt_time(&server_system_time, t);
3995 key_len = SVAL(vwv + 11, 0);
3997 if (num_bytes < key_len) {
3998 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3999 return;
4002 if (key_len != 0 && key_len != 8) {
4003 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4004 return;
4007 if (key_len == 8) {
4008 memcpy(server_challenge, bytes, 8);
4011 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4012 if (blob1.length > 0) {
4013 size_t len;
4014 bool ok;
4016 len = utf16_len_n(blob1.data,
4017 blob1.length);
4018 blob1.length = len;
4020 ok = convert_string_talloc(state,
4021 CH_DOS,
4022 CH_UNIX,
4023 blob1.data,
4024 blob1.length,
4025 &server_workgroup,
4026 &len);
4027 if (!ok) {
4028 status = map_nt_error_from_unix_common(errno);
4029 tevent_req_nterror(req, status);
4030 return;
4034 } else {
4035 /* the old core protocol */
4036 server_time_zone = get_time_zone(time(NULL));
4037 server_max_xmit = 1024;
4038 server_max_mux = 1;
4041 if (server_max_xmit < 1024) {
4042 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4043 return;
4046 if (server_max_mux < 1) {
4047 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4048 return;
4052 * Now calculate the negotiated capabilities
4053 * based on the mask for:
4054 * - client only flags
4055 * - flags used in both directions
4056 * - server only flags
4058 both_capabilities = client_capabilities & server_capabilities;
4059 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4060 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4061 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4063 max_xmit = MIN(client_max_xmit, server_max_xmit);
4065 conn->smb1.server.capabilities = server_capabilities;
4066 conn->smb1.capabilities = capabilities;
4068 conn->smb1.server.max_xmit = server_max_xmit;
4069 conn->smb1.max_xmit = max_xmit;
4071 conn->smb1.server.max_mux = server_max_mux;
4073 conn->smb1.server.security_mode = server_security_mode;
4075 conn->smb1.server.readbraw = server_readbraw;
4076 conn->smb1.server.writebraw = server_writebraw;
4077 conn->smb1.server.lockread = server_lockread;
4078 conn->smb1.server.writeunlock = server_writeunlock;
4080 conn->smb1.server.session_key = server_session_key;
4082 talloc_steal(conn, server_gss_blob.data);
4083 conn->smb1.server.gss_blob = server_gss_blob;
4084 conn->smb1.server.guid = server_guid;
4085 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4086 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4087 conn->smb1.server.name = talloc_move(conn, &server_name);
4089 conn->smb1.server.time_zone = server_time_zone;
4090 conn->smb1.server.system_time = server_system_time;
4092 tevent_req_done(req);
4095 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4097 size_t i;
4098 uint8_t *buf;
4099 uint16_t dialect_count = 0;
4101 buf = state->smb2.dyn;
4102 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4103 if (smb2cli_prots[i].proto < state->min_protocol) {
4104 continue;
4107 if (smb2cli_prots[i].proto > state->max_protocol) {
4108 continue;
4111 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4112 dialect_count++;
4115 buf = state->smb2.fixed;
4116 SSVAL(buf, 0, 36);
4117 SSVAL(buf, 2, dialect_count);
4118 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4119 SSVAL(buf, 6, 0); /* Reserved */
4120 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4121 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4122 } else {
4123 SIVAL(buf, 8, 0); /* Capabilities */
4125 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4126 NTSTATUS status;
4127 DATA_BLOB blob;
4129 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4130 state, &blob);
4131 if (!NT_STATUS_IS_OK(status)) {
4132 return NULL;
4134 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4135 } else {
4136 memset(buf+12, 0, 16); /* ClientGuid */
4138 SBVAL(buf, 28, 0); /* ClientStartTime */
4140 return smb2cli_req_send(state, state->ev,
4141 state->conn, SMB2_OP_NEGPROT,
4142 0, 0, /* flags */
4143 state->timeout_msec,
4144 NULL, NULL, /* tcon, session */
4145 state->smb2.fixed, sizeof(state->smb2.fixed),
4146 state->smb2.dyn, dialect_count*2);
4149 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4151 struct tevent_req *req =
4152 tevent_req_callback_data(subreq,
4153 struct tevent_req);
4154 struct smbXcli_negprot_state *state =
4155 tevent_req_data(req,
4156 struct smbXcli_negprot_state);
4157 struct smbXcli_conn *conn = state->conn;
4158 size_t security_offset, security_length;
4159 DATA_BLOB blob;
4160 NTSTATUS status;
4161 struct iovec *iov;
4162 uint8_t *body;
4163 size_t i;
4164 uint16_t dialect_revision;
4165 static const struct smb2cli_req_expected_response expected[] = {
4167 .status = NT_STATUS_OK,
4168 .body_size = 0x41
4172 status = smb2cli_req_recv(subreq, state, &iov,
4173 expected, ARRAY_SIZE(expected));
4174 TALLOC_FREE(subreq);
4175 if (tevent_req_nterror(req, status)) {
4176 return;
4179 body = (uint8_t *)iov[1].iov_base;
4181 dialect_revision = SVAL(body, 4);
4183 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4184 if (smb2cli_prots[i].proto < state->min_protocol) {
4185 continue;
4188 if (smb2cli_prots[i].proto > state->max_protocol) {
4189 continue;
4192 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4193 continue;
4196 conn->protocol = smb2cli_prots[i].proto;
4197 break;
4200 if (conn->protocol == PROTOCOL_NONE) {
4201 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4202 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4203 return;
4206 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4207 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4208 return;
4211 /* make sure we do not loop forever */
4212 state->min_protocol = PROTOCOL_SMB2_02;
4215 * send a SMB2 negprot, in order to negotiate
4216 * the SMB2 dialect.
4218 subreq = smbXcli_negprot_smb2_subreq(state);
4219 if (tevent_req_nomem(subreq, req)) {
4220 return;
4222 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4223 return;
4226 conn->smb2.server.security_mode = SVAL(body, 2);
4228 blob = data_blob_const(body + 8, 16);
4229 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4230 if (tevent_req_nterror(req, status)) {
4231 return;
4234 conn->smb2.server.capabilities = IVAL(body, 24);
4235 conn->smb2.server.max_trans_size= IVAL(body, 28);
4236 conn->smb2.server.max_read_size = IVAL(body, 32);
4237 conn->smb2.server.max_write_size= IVAL(body, 36);
4238 conn->smb2.server.system_time = BVAL(body, 40);
4239 conn->smb2.server.start_time = BVAL(body, 48);
4241 security_offset = SVAL(body, 56);
4242 security_length = SVAL(body, 58);
4244 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4245 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4246 return;
4249 if (security_length > iov[2].iov_len) {
4250 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4251 return;
4254 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4255 iov[2].iov_base,
4256 security_length);
4257 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4258 return;
4261 tevent_req_done(req);
4264 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4265 TALLOC_CTX *tmp_mem,
4266 uint8_t *inbuf)
4268 size_t num_pending = talloc_array_length(conn->pending);
4269 struct tevent_req *subreq;
4270 struct smbXcli_req_state *substate;
4271 struct tevent_req *req;
4272 uint32_t protocol_magic;
4273 size_t inbuf_len = smb_len_nbt(inbuf);
4275 if (num_pending != 1) {
4276 return NT_STATUS_INTERNAL_ERROR;
4279 if (inbuf_len < 4) {
4280 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4283 subreq = conn->pending[0];
4284 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4285 req = tevent_req_callback_data(subreq, struct tevent_req);
4287 protocol_magic = IVAL(inbuf, 4);
4289 switch (protocol_magic) {
4290 case SMB_MAGIC:
4291 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4292 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4293 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4295 case SMB2_MAGIC:
4296 if (substate->smb2.recv_iov == NULL) {
4298 * For the SMB1 negprot we have move it.
4300 substate->smb2.recv_iov = substate->smb1.recv_iov;
4301 substate->smb1.recv_iov = NULL;
4305 * we got an SMB2 answer, which consumed sequence number 0
4306 * so we need to use 1 as the next one
4308 conn->smb2.mid = 1;
4309 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4310 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4311 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4314 DEBUG(10, ("Got non-SMB PDU\n"));
4315 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4318 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4320 return tevent_req_simple_recv_ntstatus(req);
4323 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4324 uint32_t timeout_msec,
4325 enum protocol_types min_protocol,
4326 enum protocol_types max_protocol)
4328 TALLOC_CTX *frame = talloc_stackframe();
4329 struct tevent_context *ev;
4330 struct tevent_req *req;
4331 NTSTATUS status = NT_STATUS_NO_MEMORY;
4332 bool ok;
4334 if (smbXcli_conn_has_async_calls(conn)) {
4336 * Can't use sync call while an async call is in flight
4338 status = NT_STATUS_INVALID_PARAMETER_MIX;
4339 goto fail;
4341 ev = tevent_context_init(frame);
4342 if (ev == NULL) {
4343 goto fail;
4345 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4346 min_protocol, max_protocol);
4347 if (req == NULL) {
4348 goto fail;
4350 ok = tevent_req_poll(req, ev);
4351 if (!ok) {
4352 status = map_nt_error_from_unix_common(errno);
4353 goto fail;
4355 status = smbXcli_negprot_recv(req);
4356 fail:
4357 TALLOC_FREE(frame);
4358 return status;
4361 static int smbXcli_session_destructor(struct smbXcli_session *session)
4363 if (session->conn == NULL) {
4364 return 0;
4367 DLIST_REMOVE(session->conn->sessions, session);
4368 return 0;
4371 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4372 struct smbXcli_conn *conn)
4374 struct smbXcli_session *session;
4376 session = talloc_zero(mem_ctx, struct smbXcli_session);
4377 if (session == NULL) {
4378 return NULL;
4380 session->smb2 = talloc_zero(session, struct smb2cli_session);
4381 if (session->smb2 == NULL) {
4382 talloc_free(session);
4383 return NULL;
4385 talloc_set_destructor(session, smbXcli_session_destructor);
4387 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4388 session->conn = conn;
4390 return session;
4393 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4394 TALLOC_CTX *mem_ctx,
4395 DATA_BLOB *key)
4397 const DATA_BLOB *application_key;
4399 *key = data_blob_null;
4401 if (session->conn == NULL) {
4402 return NT_STATUS_NO_USER_SESSION_KEY;
4405 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4406 application_key = &session->smb2->application_key;
4407 } else {
4408 application_key = &session->smb1.application_key;
4411 if (application_key->length == 0) {
4412 return NT_STATUS_NO_USER_SESSION_KEY;
4415 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4416 if (key->data == NULL) {
4417 return NT_STATUS_NO_MEMORY;
4420 return NT_STATUS_OK;
4423 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4425 return session->smb1.session_id;
4428 void smb1cli_session_set_id(struct smbXcli_session *session,
4429 uint16_t session_id)
4431 session->smb1.session_id = session_id;
4434 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4435 const DATA_BLOB _session_key)
4437 struct smbXcli_conn *conn = session->conn;
4438 uint8_t session_key[16];
4440 if (conn == NULL) {
4441 return NT_STATUS_INVALID_PARAMETER_MIX;
4444 if (session->smb1.application_key.length != 0) {
4446 * TODO: do not allow this...
4448 * return NT_STATUS_INVALID_PARAMETER_MIX;
4450 data_blob_clear_free(&session->smb1.application_key);
4451 session->smb1.protected_key = false;
4454 if (_session_key.length == 0) {
4455 return NT_STATUS_OK;
4458 ZERO_STRUCT(session_key);
4459 memcpy(session_key, _session_key.data,
4460 MIN(_session_key.length, sizeof(session_key)));
4462 session->smb1.application_key = data_blob_talloc(session,
4463 session_key,
4464 sizeof(session_key));
4465 ZERO_STRUCT(session_key);
4466 if (session->smb1.application_key.data == NULL) {
4467 return NT_STATUS_NO_MEMORY;
4470 session->smb1.protected_key = false;
4472 return NT_STATUS_OK;
4475 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4477 if (session->smb1.protected_key) {
4478 /* already protected */
4479 return NT_STATUS_OK;
4482 if (session->smb1.application_key.length != 16) {
4483 return NT_STATUS_INVALID_PARAMETER_MIX;
4486 smb_key_derivation(session->smb1.application_key.data,
4487 session->smb1.application_key.length,
4488 session->smb1.application_key.data);
4490 session->smb1.protected_key = true;
4492 return NT_STATUS_OK;
4495 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4497 struct smbXcli_conn *conn = session->conn;
4498 uint8_t security_mode = 0;
4500 if (conn == NULL) {
4501 return security_mode;
4504 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4505 if (conn->mandatory_signing) {
4506 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4509 return security_mode;
4512 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4514 return session->smb2->session_id;
4517 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4519 return session->smb2->session_flags;
4522 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4523 uint64_t session_id,
4524 uint16_t session_flags)
4526 session->smb2->session_id = session_id;
4527 session->smb2->session_flags = session_flags;
4530 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4532 session->smb2->channel_sequence += 1;
4535 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4536 const DATA_BLOB _session_key,
4537 const struct iovec *recv_iov)
4539 struct smbXcli_conn *conn = session->conn;
4540 uint16_t no_sign_flags;
4541 uint8_t session_key[16];
4542 NTSTATUS status;
4544 if (conn == NULL) {
4545 return NT_STATUS_INVALID_PARAMETER_MIX;
4548 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4550 if (session->smb2->session_flags & no_sign_flags) {
4551 session->smb2->should_sign = false;
4552 return NT_STATUS_OK;
4555 if (session->smb2->signing_key.length != 0) {
4556 return NT_STATUS_INVALID_PARAMETER_MIX;
4559 ZERO_STRUCT(session_key);
4560 memcpy(session_key, _session_key.data,
4561 MIN(_session_key.length, sizeof(session_key)));
4563 session->smb2->signing_key = data_blob_talloc(session,
4564 session_key,
4565 sizeof(session_key));
4566 if (session->smb2->signing_key.data == NULL) {
4567 ZERO_STRUCT(session_key);
4568 return NT_STATUS_NO_MEMORY;
4571 if (conn->protocol >= PROTOCOL_SMB2_24) {
4572 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4573 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4575 smb2_key_derivation(session_key, sizeof(session_key),
4576 label.data, label.length,
4577 context.data, context.length,
4578 session->smb2->signing_key.data);
4581 session->smb2->encryption_key = data_blob_dup_talloc(session,
4582 session->smb2->signing_key);
4583 if (session->smb2->encryption_key.data == NULL) {
4584 ZERO_STRUCT(session_key);
4585 return NT_STATUS_NO_MEMORY;
4588 if (conn->protocol >= PROTOCOL_SMB2_24) {
4589 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4590 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4592 smb2_key_derivation(session_key, sizeof(session_key),
4593 label.data, label.length,
4594 context.data, context.length,
4595 session->smb2->encryption_key.data);
4598 session->smb2->decryption_key = data_blob_dup_talloc(session,
4599 session->smb2->signing_key);
4600 if (session->smb2->decryption_key.data == NULL) {
4601 ZERO_STRUCT(session_key);
4602 return NT_STATUS_NO_MEMORY;
4605 if (conn->protocol >= PROTOCOL_SMB2_24) {
4606 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4607 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4609 smb2_key_derivation(session_key, sizeof(session_key),
4610 label.data, label.length,
4611 context.data, context.length,
4612 session->smb2->decryption_key.data);
4615 session->smb2->application_key = data_blob_dup_talloc(session,
4616 session->smb2->signing_key);
4617 if (session->smb2->application_key.data == NULL) {
4618 ZERO_STRUCT(session_key);
4619 return NT_STATUS_NO_MEMORY;
4622 if (conn->protocol >= PROTOCOL_SMB2_24) {
4623 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4624 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4626 smb2_key_derivation(session_key, sizeof(session_key),
4627 label.data, label.length,
4628 context.data, context.length,
4629 session->smb2->application_key.data);
4631 ZERO_STRUCT(session_key);
4633 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4634 session->smb2->signing_key);
4635 if (session->smb2_channel.signing_key.data == NULL) {
4636 return NT_STATUS_NO_MEMORY;
4639 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4640 session->conn->protocol,
4641 recv_iov, 3);
4642 if (!NT_STATUS_IS_OK(status)) {
4643 return status;
4646 session->smb2->should_sign = false;
4647 session->smb2->should_encrypt = false;
4649 if (conn->desire_signing) {
4650 session->smb2->should_sign = true;
4653 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4654 session->smb2->should_sign = true;
4657 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4658 session->smb2->should_encrypt = true;
4661 if (conn->protocol < PROTOCOL_SMB2_24) {
4662 session->smb2->should_encrypt = false;
4665 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4666 session->smb2->should_encrypt = false;
4669 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4670 sizeof(session->smb2->nonce_high));
4671 session->smb2->nonce_low = 1;
4673 return NT_STATUS_OK;
4676 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4677 struct smbXcli_session *session1,
4678 struct smbXcli_conn *conn,
4679 struct smbXcli_session **_session2)
4681 struct smbXcli_session *session2;
4683 if (session1->smb2->signing_key.length == 0) {
4684 return NT_STATUS_INVALID_PARAMETER_MIX;
4687 if (conn == NULL) {
4688 return NT_STATUS_INVALID_PARAMETER_MIX;
4691 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4692 if (session2 == NULL) {
4693 return NT_STATUS_NO_MEMORY;
4695 session2->smb2 = talloc_reference(session2, session1->smb2);
4696 if (session2->smb2 == NULL) {
4697 talloc_free(session2);
4698 return NT_STATUS_NO_MEMORY;
4701 talloc_set_destructor(session2, smbXcli_session_destructor);
4702 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4703 session2->conn = conn;
4705 *_session2 = session2;
4706 return NT_STATUS_OK;
4709 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4710 const DATA_BLOB _channel_key,
4711 const struct iovec *recv_iov)
4713 struct smbXcli_conn *conn = session->conn;
4714 uint8_t channel_key[16];
4715 NTSTATUS status;
4717 if (conn == NULL) {
4718 return NT_STATUS_INVALID_PARAMETER_MIX;
4721 if (session->smb2_channel.signing_key.length != 0) {
4722 return NT_STATUS_INVALID_PARAMETER_MIX;
4725 ZERO_STRUCT(channel_key);
4726 memcpy(channel_key, _channel_key.data,
4727 MIN(_channel_key.length, sizeof(channel_key)));
4729 session->smb2_channel.signing_key = data_blob_talloc(session,
4730 channel_key,
4731 sizeof(channel_key));
4732 if (session->smb2_channel.signing_key.data == NULL) {
4733 ZERO_STRUCT(channel_key);
4734 return NT_STATUS_NO_MEMORY;
4737 if (conn->protocol >= PROTOCOL_SMB2_24) {
4738 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4739 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4741 smb2_key_derivation(channel_key, sizeof(channel_key),
4742 label.data, label.length,
4743 context.data, context.length,
4744 session->smb2_channel.signing_key.data);
4746 ZERO_STRUCT(channel_key);
4748 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4749 session->conn->protocol,
4750 recv_iov, 3);
4751 if (!NT_STATUS_IS_OK(status)) {
4752 return status;
4755 return NT_STATUS_OK;
4758 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4760 struct smbXcli_tcon *tcon;
4762 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4763 if (tcon == NULL) {
4764 return NULL;
4767 return tcon;
4770 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4772 return tcon->smb1.tcon_id;
4775 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4777 tcon->smb1.tcon_id = tcon_id;
4780 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
4781 uint16_t tcon_id,
4782 uint16_t optional_support,
4783 uint32_t maximal_access,
4784 uint32_t guest_maximal_access,
4785 const char *service,
4786 const char *fs_type)
4788 tcon->smb1.tcon_id = tcon_id;
4789 tcon->smb1.optional_support = optional_support;
4790 tcon->smb1.maximal_access = maximal_access;
4791 tcon->smb1.guest_maximal_access = guest_maximal_access;
4793 TALLOC_FREE(tcon->smb1.service);
4794 tcon->smb1.service = talloc_strdup(tcon, service);
4795 if (service != NULL && tcon->smb1.service == NULL) {
4796 return false;
4799 TALLOC_FREE(tcon->smb1.fs_type);
4800 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
4801 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
4802 return false;
4805 return true;
4808 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
4810 return tcon->smb2.tcon_id;
4813 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
4815 return tcon->smb2.capabilities;
4818 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
4819 struct smbXcli_session *session,
4820 uint32_t tcon_id,
4821 uint8_t type,
4822 uint32_t flags,
4823 uint32_t capabilities,
4824 uint32_t maximal_access)
4826 tcon->smb2.tcon_id = tcon_id;
4827 tcon->smb2.type = type;
4828 tcon->smb2.flags = flags;
4829 tcon->smb2.capabilities = capabilities;
4830 tcon->smb2.maximal_access = maximal_access;
4832 tcon->smb2.should_encrypt = false;
4834 if (session == NULL) {
4835 return;
4838 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
4840 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
4841 tcon->smb2.should_encrypt = true;