drsuapi: check more carefully the validity of the NC
[Samba/gbeck.git] / libcli / smb / smbXcli_base.c
blob02d0227637174d51ae8929f44c92d58280ec3cc9
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;
1611 NTSTATUS status;
1612 size_t min_size = MIN_SMB_SIZE;
1614 buflen = smb_len_nbt(buf);
1615 taken = 0;
1617 hdr = buf + NBT_HDR_SIZE;
1619 status = smb1cli_pull_raw_error(hdr);
1620 if (NT_STATUS_IS_ERR(status)) {
1622 * This is an ugly hack to support OS/2
1623 * which skips the byte_count in the DATA block
1624 * on some error responses.
1626 * See bug #9096
1628 min_size -= sizeof(uint16_t);
1631 if (buflen < min_size) {
1632 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1636 * This returns iovec elements in the following order:
1638 * - SMB header
1640 * - Parameter Block
1641 * - Data Block
1643 * - Parameter Block
1644 * - Data Block
1646 * - Parameter Block
1647 * - Data Block
1649 num_iov = 1;
1651 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1652 if (iov == NULL) {
1653 return NT_STATUS_NO_MEMORY;
1655 iov[0].iov_base = hdr;
1656 iov[0].iov_len = HDR_WCT;
1657 taken += HDR_WCT;
1659 cmd = CVAL(hdr, HDR_COM);
1660 wct_ofs = HDR_WCT;
1662 while (true) {
1663 size_t len = buflen - taken;
1664 struct iovec *cur;
1665 struct iovec *iov_tmp;
1666 uint8_t wct;
1667 uint32_t bcc_ofs;
1668 uint16_t bcc;
1669 size_t needed;
1672 * we need at least WCT
1674 needed = sizeof(uint8_t);
1675 if (len < needed) {
1676 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1677 __location__, (int)len, (int)needed));
1678 goto inval;
1682 * Now we check if the specified words are there
1684 wct = CVAL(hdr, wct_ofs);
1685 needed += wct * sizeof(uint16_t);
1686 if (len < needed) {
1687 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1688 __location__, (int)len, (int)needed));
1689 goto inval;
1692 if ((num_iov == 1) &&
1693 (len == needed) &&
1694 NT_STATUS_IS_ERR(status))
1697 * This is an ugly hack to support OS/2
1698 * which skips the byte_count in the DATA block
1699 * on some error responses.
1701 * See bug #9096
1703 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1704 num_iov + 2);
1705 if (iov_tmp == NULL) {
1706 TALLOC_FREE(iov);
1707 return NT_STATUS_NO_MEMORY;
1709 iov = iov_tmp;
1710 cur = &iov[num_iov];
1711 num_iov += 2;
1713 cur[0].iov_len = 0;
1714 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1715 cur[1].iov_len = 0;
1716 cur[1].iov_base = cur[0].iov_base;
1718 taken += needed;
1719 break;
1723 * we need at least BCC
1725 needed += sizeof(uint16_t);
1726 if (len < needed) {
1727 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1728 __location__, (int)len, (int)needed));
1729 goto inval;
1733 * Now we check if the specified bytes are there
1735 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1736 bcc = SVAL(hdr, bcc_ofs);
1737 needed += bcc * sizeof(uint8_t);
1738 if (len < needed) {
1739 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1740 __location__, (int)len, (int)needed));
1741 goto inval;
1745 * we allocate 2 iovec structures for words and bytes
1747 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1748 num_iov + 2);
1749 if (iov_tmp == NULL) {
1750 TALLOC_FREE(iov);
1751 return NT_STATUS_NO_MEMORY;
1753 iov = iov_tmp;
1754 cur = &iov[num_iov];
1755 num_iov += 2;
1757 cur[0].iov_len = wct * sizeof(uint16_t);
1758 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1759 cur[1].iov_len = bcc * sizeof(uint8_t);
1760 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1762 taken += needed;
1764 if (!smb1cli_is_andx_req(cmd)) {
1766 * If the current command does not have AndX chanining
1767 * we are done.
1769 break;
1772 if (wct == 0 && bcc == 0) {
1774 * An empty response also ends the chain,
1775 * most likely with an error.
1777 break;
1780 if (wct < 2) {
1781 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1782 __location__, (int)wct, (int)cmd));
1783 goto inval;
1785 cmd = CVAL(cur[0].iov_base, 0);
1786 if (cmd == 0xFF) {
1788 * If it is the end of the chain we are also done.
1790 break;
1792 wct_ofs = SVAL(cur[0].iov_base, 2);
1794 if (wct_ofs < taken) {
1795 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1797 if (wct_ofs > buflen) {
1798 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1802 * we consumed everything up to the start of the next
1803 * parameter block.
1805 taken = wct_ofs;
1808 remaining = buflen - taken;
1810 if (remaining > 0 && num_iov >= 3) {
1812 * The last DATA block gets the remaining
1813 * bytes, this is needed to support
1814 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1816 iov[num_iov-1].iov_len += remaining;
1819 *piov = iov;
1820 *pnum_iov = num_iov;
1821 return NT_STATUS_OK;
1823 inval:
1824 TALLOC_FREE(iov);
1825 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1828 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1829 TALLOC_CTX *tmp_mem,
1830 uint8_t *inbuf)
1832 struct tevent_req *req;
1833 struct smbXcli_req_state *state;
1834 NTSTATUS status;
1835 size_t num_pending;
1836 size_t i;
1837 uint8_t cmd;
1838 uint16_t mid;
1839 bool oplock_break;
1840 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1841 size_t len = smb_len_nbt(inbuf);
1842 struct iovec *iov = NULL;
1843 int num_iov = 0;
1844 struct tevent_req **chain = NULL;
1845 size_t num_chained = 0;
1846 size_t num_responses = 0;
1848 if (conn->smb1.read_braw_req != NULL) {
1849 req = conn->smb1.read_braw_req;
1850 conn->smb1.read_braw_req = NULL;
1851 state = tevent_req_data(req, struct smbXcli_req_state);
1853 smbXcli_req_unset_pending(req);
1855 if (state->smb1.recv_iov == NULL) {
1857 * For requests with more than
1858 * one response, we have to readd the
1859 * recv_iov array.
1861 state->smb1.recv_iov = talloc_zero_array(state,
1862 struct iovec,
1864 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1865 return NT_STATUS_OK;
1869 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1870 state->smb1.recv_iov[0].iov_len = len;
1871 ZERO_STRUCT(state->smb1.recv_iov[1]);
1872 ZERO_STRUCT(state->smb1.recv_iov[2]);
1874 state->smb1.recv_cmd = SMBreadBraw;
1875 state->smb1.recv_status = NT_STATUS_OK;
1876 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1878 tevent_req_done(req);
1879 return NT_STATUS_OK;
1882 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1883 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1884 DEBUG(10, ("Got non-SMB PDU\n"));
1885 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1889 * If we supported multiple encrytion contexts
1890 * here we'd look up based on tid.
1892 if (common_encryption_on(conn->smb1.trans_enc)
1893 && (CVAL(inbuf, 0) == 0)) {
1894 uint16_t enc_ctx_num;
1896 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1897 if (!NT_STATUS_IS_OK(status)) {
1898 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1899 nt_errstr(status)));
1900 return status;
1903 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1904 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1905 enc_ctx_num,
1906 conn->smb1.trans_enc->enc_ctx_num));
1907 return NT_STATUS_INVALID_HANDLE;
1910 status = common_decrypt_buffer(conn->smb1.trans_enc,
1911 (char *)inbuf);
1912 if (!NT_STATUS_IS_OK(status)) {
1913 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1914 nt_errstr(status)));
1915 return status;
1917 inhdr = inbuf + NBT_HDR_SIZE;
1918 len = smb_len_nbt(inbuf);
1921 mid = SVAL(inhdr, HDR_MID);
1922 num_pending = talloc_array_length(conn->pending);
1924 for (i=0; i<num_pending; i++) {
1925 if (mid == smb1cli_req_mid(conn->pending[i])) {
1926 break;
1929 if (i == num_pending) {
1930 /* Dump unexpected reply */
1931 return NT_STATUS_RETRY;
1934 oplock_break = false;
1936 if (mid == 0xffff) {
1938 * Paranoia checks that this is really an oplock break request.
1940 oplock_break = (len == 51); /* hdr + 8 words */
1941 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1942 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1943 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1944 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1946 if (!oplock_break) {
1947 /* Dump unexpected reply */
1948 return NT_STATUS_RETRY;
1952 req = conn->pending[i];
1953 state = tevent_req_data(req, struct smbXcli_req_state);
1955 if (!oplock_break /* oplock breaks are not signed */
1956 && !smb_signing_check_pdu(conn->smb1.signing,
1957 inhdr, len, state->smb1.seqnum+1)) {
1958 DEBUG(10, ("cli_check_sign_mac failed\n"));
1959 return NT_STATUS_ACCESS_DENIED;
1962 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1963 &iov, &num_iov);
1964 if (!NT_STATUS_IS_OK(status)) {
1965 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1966 nt_errstr(status)));
1967 return status;
1970 cmd = CVAL(inhdr, HDR_COM);
1971 status = smb1cli_pull_raw_error(inhdr);
1973 if (state->smb1.chained_requests == NULL) {
1974 if (num_iov != 3) {
1975 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1978 smbXcli_req_unset_pending(req);
1980 if (state->smb1.recv_iov == NULL) {
1982 * For requests with more than
1983 * one response, we have to readd the
1984 * recv_iov array.
1986 state->smb1.recv_iov = talloc_zero_array(state,
1987 struct iovec,
1989 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1990 return NT_STATUS_OK;
1994 state->smb1.recv_cmd = cmd;
1995 state->smb1.recv_status = status;
1996 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1998 state->smb1.recv_iov[0] = iov[0];
1999 state->smb1.recv_iov[1] = iov[1];
2000 state->smb1.recv_iov[2] = iov[2];
2002 if (talloc_array_length(conn->pending) == 0) {
2003 tevent_req_done(req);
2004 return NT_STATUS_OK;
2007 tevent_req_defer_callback(req, state->ev);
2008 tevent_req_done(req);
2009 return NT_STATUS_RETRY;
2012 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2013 num_chained = talloc_array_length(chain);
2014 num_responses = (num_iov - 1)/2;
2016 if (num_responses > num_chained) {
2017 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2020 for (i=0; i<num_chained; i++) {
2021 size_t iov_idx = 1 + (i*2);
2022 struct iovec *cur = &iov[iov_idx];
2023 uint8_t *inbuf_ref;
2025 req = chain[i];
2026 state = tevent_req_data(req, struct smbXcli_req_state);
2028 smbXcli_req_unset_pending(req);
2031 * as we finish multiple requests here
2032 * we need to defer the callbacks as
2033 * they could destroy our current stack state.
2035 tevent_req_defer_callback(req, state->ev);
2037 if (i >= num_responses) {
2038 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2039 continue;
2042 if (state->smb1.recv_iov == NULL) {
2044 * For requests with more than
2045 * one response, we have to readd the
2046 * recv_iov array.
2048 state->smb1.recv_iov = talloc_zero_array(state,
2049 struct iovec,
2051 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2052 continue;
2056 state->smb1.recv_cmd = cmd;
2058 if (i == (num_responses - 1)) {
2060 * The last request in the chain gets the status
2062 state->smb1.recv_status = status;
2063 } else {
2064 cmd = CVAL(cur[0].iov_base, 0);
2065 state->smb1.recv_status = NT_STATUS_OK;
2068 state->inbuf = inbuf;
2071 * Note: here we use talloc_reference() in a way
2072 * that does not expose it to the caller.
2074 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2075 if (tevent_req_nomem(inbuf_ref, req)) {
2076 continue;
2079 /* copy the related buffers */
2080 state->smb1.recv_iov[0] = iov[0];
2081 state->smb1.recv_iov[1] = cur[0];
2082 state->smb1.recv_iov[2] = cur[1];
2084 tevent_req_done(req);
2087 return NT_STATUS_RETRY;
2090 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2091 TALLOC_CTX *mem_ctx,
2092 struct iovec **piov,
2093 uint8_t **phdr,
2094 uint8_t *pwct,
2095 uint16_t **pvwv,
2096 uint32_t *pvwv_offset,
2097 uint32_t *pnum_bytes,
2098 uint8_t **pbytes,
2099 uint32_t *pbytes_offset,
2100 uint8_t **pinbuf,
2101 const struct smb1cli_req_expected_response *expected,
2102 size_t num_expected)
2104 struct smbXcli_req_state *state =
2105 tevent_req_data(req,
2106 struct smbXcli_req_state);
2107 NTSTATUS status = NT_STATUS_OK;
2108 struct iovec *recv_iov = NULL;
2109 uint8_t *hdr = NULL;
2110 uint8_t wct = 0;
2111 uint32_t vwv_offset = 0;
2112 uint16_t *vwv = NULL;
2113 uint32_t num_bytes = 0;
2114 uint32_t bytes_offset = 0;
2115 uint8_t *bytes = NULL;
2116 size_t i;
2117 bool found_status = false;
2118 bool found_size = false;
2120 if (piov != NULL) {
2121 *piov = NULL;
2123 if (phdr != NULL) {
2124 *phdr = 0;
2126 if (pwct != NULL) {
2127 *pwct = 0;
2129 if (pvwv != NULL) {
2130 *pvwv = NULL;
2132 if (pvwv_offset != NULL) {
2133 *pvwv_offset = 0;
2135 if (pnum_bytes != NULL) {
2136 *pnum_bytes = 0;
2138 if (pbytes != NULL) {
2139 *pbytes = NULL;
2141 if (pbytes_offset != NULL) {
2142 *pbytes_offset = 0;
2144 if (pinbuf != NULL) {
2145 *pinbuf = NULL;
2148 if (state->inbuf != NULL) {
2149 recv_iov = state->smb1.recv_iov;
2150 state->smb1.recv_iov = NULL;
2151 if (state->smb1.recv_cmd != SMBreadBraw) {
2152 hdr = (uint8_t *)recv_iov[0].iov_base;
2153 wct = recv_iov[1].iov_len/2;
2154 vwv = (uint16_t *)recv_iov[1].iov_base;
2155 vwv_offset = PTR_DIFF(vwv, hdr);
2156 num_bytes = recv_iov[2].iov_len;
2157 bytes = (uint8_t *)recv_iov[2].iov_base;
2158 bytes_offset = PTR_DIFF(bytes, hdr);
2162 if (tevent_req_is_nterror(req, &status)) {
2163 for (i=0; i < num_expected; i++) {
2164 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2165 found_status = true;
2166 break;
2170 if (found_status) {
2171 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2174 return status;
2177 if (num_expected == 0) {
2178 found_status = true;
2179 found_size = true;
2182 status = state->smb1.recv_status;
2184 for (i=0; i < num_expected; i++) {
2185 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2186 continue;
2189 found_status = true;
2190 if (expected[i].wct == 0) {
2191 found_size = true;
2192 break;
2195 if (expected[i].wct == wct) {
2196 found_size = true;
2197 break;
2201 if (!found_status) {
2202 return status;
2205 if (!found_size) {
2206 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2209 if (piov != NULL) {
2210 *piov = talloc_move(mem_ctx, &recv_iov);
2213 if (phdr != NULL) {
2214 *phdr = hdr;
2216 if (pwct != NULL) {
2217 *pwct = wct;
2219 if (pvwv != NULL) {
2220 *pvwv = vwv;
2222 if (pvwv_offset != NULL) {
2223 *pvwv_offset = vwv_offset;
2225 if (pnum_bytes != NULL) {
2226 *pnum_bytes = num_bytes;
2228 if (pbytes != NULL) {
2229 *pbytes = bytes;
2231 if (pbytes_offset != NULL) {
2232 *pbytes_offset = bytes_offset;
2234 if (pinbuf != NULL) {
2235 *pinbuf = state->inbuf;
2238 return status;
2241 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2243 size_t wct_ofs;
2244 int i;
2246 wct_ofs = HDR_WCT;
2248 for (i=0; i<num_reqs; i++) {
2249 struct smbXcli_req_state *state;
2250 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2251 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2252 state->smb1.iov_count-2);
2253 wct_ofs = (wct_ofs + 3) & ~3;
2255 return wct_ofs;
2258 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2260 struct smbXcli_req_state *first_state =
2261 tevent_req_data(reqs[0],
2262 struct smbXcli_req_state);
2263 struct smbXcli_req_state *state;
2264 size_t wct_offset;
2265 size_t chain_padding = 0;
2266 int i, iovlen;
2267 struct iovec *iov = NULL;
2268 struct iovec *this_iov;
2269 NTSTATUS status;
2270 size_t nbt_len;
2272 if (num_reqs == 1) {
2273 return smb1cli_req_writev_submit(reqs[0], first_state,
2274 first_state->smb1.iov,
2275 first_state->smb1.iov_count);
2278 iovlen = 0;
2279 for (i=0; i<num_reqs; i++) {
2280 if (!tevent_req_is_in_progress(reqs[i])) {
2281 return NT_STATUS_INTERNAL_ERROR;
2284 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2286 if (state->smb1.iov_count < 4) {
2287 return NT_STATUS_INVALID_PARAMETER_MIX;
2290 if (i == 0) {
2292 * The NBT and SMB header
2294 iovlen += 2;
2295 } else {
2297 * Chain padding
2299 iovlen += 1;
2303 * words and bytes
2305 iovlen += state->smb1.iov_count - 2;
2308 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2309 if (iov == NULL) {
2310 return NT_STATUS_NO_MEMORY;
2313 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2314 first_state, reqs, sizeof(*reqs) * num_reqs);
2315 if (first_state->smb1.chained_requests == NULL) {
2316 TALLOC_FREE(iov);
2317 return NT_STATUS_NO_MEMORY;
2320 wct_offset = HDR_WCT;
2321 this_iov = iov;
2323 for (i=0; i<num_reqs; i++) {
2324 size_t next_padding = 0;
2325 uint16_t *vwv;
2327 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2329 if (i < num_reqs-1) {
2330 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2331 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2332 TALLOC_FREE(iov);
2333 TALLOC_FREE(first_state->smb1.chained_requests);
2334 return NT_STATUS_INVALID_PARAMETER_MIX;
2338 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2339 state->smb1.iov_count-2) + 1;
2340 if ((wct_offset % 4) != 0) {
2341 next_padding = 4 - (wct_offset % 4);
2343 wct_offset += next_padding;
2344 vwv = state->smb1.vwv;
2346 if (i < num_reqs-1) {
2347 struct smbXcli_req_state *next_state =
2348 tevent_req_data(reqs[i+1],
2349 struct smbXcli_req_state);
2350 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2351 SCVAL(vwv+0, 1, 0);
2352 SSVAL(vwv+1, 0, wct_offset);
2353 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2354 /* properly end the chain */
2355 SCVAL(vwv+0, 0, 0xff);
2356 SCVAL(vwv+0, 1, 0xff);
2357 SSVAL(vwv+1, 0, 0);
2360 if (i == 0) {
2362 * The NBT and SMB header
2364 this_iov[0] = state->smb1.iov[0];
2365 this_iov[1] = state->smb1.iov[1];
2366 this_iov += 2;
2367 } else {
2369 * This one is a bit subtle. We have to add
2370 * chain_padding bytes between the requests, and we
2371 * have to also include the wct field of the
2372 * subsequent requests. We use the subsequent header
2373 * for the padding, it contains the wct field in its
2374 * last byte.
2376 this_iov[0].iov_len = chain_padding+1;
2377 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2378 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2379 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2380 this_iov += 1;
2384 * copy the words and bytes
2386 memcpy(this_iov, state->smb1.iov+2,
2387 sizeof(struct iovec) * (state->smb1.iov_count-2));
2388 this_iov += state->smb1.iov_count - 2;
2389 chain_padding = next_padding;
2392 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2393 if (nbt_len > first_state->conn->smb1.max_xmit) {
2394 TALLOC_FREE(iov);
2395 TALLOC_FREE(first_state->smb1.chained_requests);
2396 return NT_STATUS_INVALID_PARAMETER_MIX;
2399 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2400 if (!NT_STATUS_IS_OK(status)) {
2401 TALLOC_FREE(iov);
2402 TALLOC_FREE(first_state->smb1.chained_requests);
2403 return status;
2406 return NT_STATUS_OK;
2409 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2411 return ((tevent_queue_length(conn->outgoing) != 0)
2412 || (talloc_array_length(conn->pending) != 0));
2415 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2417 return conn->smb2.server.capabilities;
2420 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2422 return conn->smb2.server.security_mode;
2425 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2427 return conn->smb2.server.max_trans_size;
2430 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2432 return conn->smb2.server.max_read_size;
2435 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2437 return conn->smb2.server.max_write_size;
2440 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2441 uint16_t max_credits)
2443 conn->smb2.max_credits = max_credits;
2446 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2448 static bool smb2cli_req_cancel(struct tevent_req *req)
2450 struct smbXcli_req_state *state =
2451 tevent_req_data(req,
2452 struct smbXcli_req_state);
2453 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2454 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2455 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2456 struct smbXcli_tcon *tcon = state->tcon;
2457 struct smbXcli_session *session = state->session;
2458 uint8_t *fixed = state->smb2.pad;
2459 uint16_t fixed_len = 4;
2460 struct tevent_req *subreq;
2461 struct smbXcli_req_state *substate;
2462 NTSTATUS status;
2464 SSVAL(fixed, 0, 0x04);
2465 SSVAL(fixed, 2, 0);
2467 subreq = smb2cli_req_create(state, state->ev,
2468 state->conn,
2469 SMB2_OP_CANCEL,
2470 flags, 0,
2471 0, /* timeout */
2472 tcon, session,
2473 fixed, fixed_len,
2474 NULL, 0);
2475 if (subreq == NULL) {
2476 return false;
2478 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2481 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2482 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2484 flags &= SMB2_HDR_FLAG_ASYNC;
2486 if (flags & SMB2_HDR_FLAG_ASYNC) {
2487 mid = 0;
2490 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2491 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2492 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2494 status = smb2cli_req_compound_submit(&subreq, 1);
2495 if (!NT_STATUS_IS_OK(status)) {
2496 TALLOC_FREE(subreq);
2497 return false;
2500 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2502 return true;
2505 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2507 /* we do not care about the result */
2508 TALLOC_FREE(subreq);
2511 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2512 struct tevent_context *ev,
2513 struct smbXcli_conn *conn,
2514 uint16_t cmd,
2515 uint32_t additional_flags,
2516 uint32_t clear_flags,
2517 uint32_t timeout_msec,
2518 struct smbXcli_tcon *tcon,
2519 struct smbXcli_session *session,
2520 const uint8_t *fixed,
2521 uint16_t fixed_len,
2522 const uint8_t *dyn,
2523 uint32_t dyn_len)
2525 struct tevent_req *req;
2526 struct smbXcli_req_state *state;
2527 uint32_t flags = 0;
2528 uint32_t tid = 0;
2529 uint64_t uid = 0;
2530 bool use_channel_sequence = false;
2531 uint16_t channel_sequence = 0;
2533 req = tevent_req_create(mem_ctx, &state,
2534 struct smbXcli_req_state);
2535 if (req == NULL) {
2536 return NULL;
2539 state->ev = ev;
2540 state->conn = conn;
2541 state->session = session;
2542 state->tcon = tcon;
2544 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2545 use_channel_sequence = true;
2546 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2547 use_channel_sequence = true;
2550 if (session) {
2551 uid = session->smb2->session_id;
2553 if (use_channel_sequence) {
2554 channel_sequence = session->smb2->channel_sequence;
2557 state->smb2.should_sign = session->smb2->should_sign;
2558 state->smb2.should_encrypt = session->smb2->should_encrypt;
2560 if (cmd == SMB2_OP_SESSSETUP &&
2561 session->smb2->signing_key.length != 0) {
2562 state->smb2.should_sign = true;
2565 if (cmd == SMB2_OP_SESSSETUP &&
2566 session->smb2_channel.signing_key.length == 0) {
2567 state->smb2.should_encrypt = false;
2571 if (tcon) {
2572 tid = tcon->smb2.tcon_id;
2574 if (tcon->smb2.should_encrypt) {
2575 state->smb2.should_encrypt = true;
2579 if (state->smb2.should_encrypt) {
2580 state->smb2.should_sign = false;
2583 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2584 if (state->smb2.recv_iov == NULL) {
2585 TALLOC_FREE(req);
2586 return NULL;
2589 flags |= additional_flags;
2590 flags &= ~clear_flags;
2592 state->smb2.fixed = fixed;
2593 state->smb2.fixed_len = fixed_len;
2594 state->smb2.dyn = dyn;
2595 state->smb2.dyn_len = dyn_len;
2597 if (state->smb2.should_encrypt) {
2598 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2599 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2602 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2603 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2604 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2605 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2606 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2607 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2608 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2609 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2611 switch (cmd) {
2612 case SMB2_OP_CANCEL:
2613 state->one_way = true;
2614 break;
2615 case SMB2_OP_BREAK:
2617 * If this is a dummy request, it will have
2618 * UINT64_MAX as message id.
2619 * If we send on break acknowledgement,
2620 * this gets overwritten later.
2622 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2623 break;
2626 if (timeout_msec > 0) {
2627 struct timeval endtime;
2629 endtime = timeval_current_ofs_msec(timeout_msec);
2630 if (!tevent_req_set_endtime(req, ev, endtime)) {
2631 return req;
2635 return req;
2638 void smb2cli_req_set_notify_async(struct tevent_req *req)
2640 struct smbXcli_req_state *state =
2641 tevent_req_data(req,
2642 struct smbXcli_req_state);
2644 state->smb2.notify_async = true;
2647 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2648 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2649 TALLOC_CTX *tmp_mem,
2650 uint8_t *inbuf);
2652 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2653 int num_reqs)
2655 struct smbXcli_req_state *state;
2656 struct tevent_req *subreq;
2657 struct iovec *iov;
2658 int i, num_iov, nbt_len;
2659 int tf_iov = -1;
2660 const DATA_BLOB *encryption_key = NULL;
2661 uint64_t encryption_session_id = 0;
2664 * 1 for the nbt length, optional TRANSFORM
2665 * per request: HDR, fixed, dyn, padding
2666 * -1 because the last one does not need padding
2669 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2670 if (iov == NULL) {
2671 return NT_STATUS_NO_MEMORY;
2674 num_iov = 1;
2675 nbt_len = 0;
2678 * the session of the first request that requires encryption
2679 * specifies the encryption key.
2681 for (i=0; i<num_reqs; i++) {
2682 if (!tevent_req_is_in_progress(reqs[i])) {
2683 return NT_STATUS_INTERNAL_ERROR;
2686 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2688 if (!smbXcli_conn_is_connected(state->conn)) {
2689 return NT_STATUS_CONNECTION_DISCONNECTED;
2692 if ((state->conn->protocol != PROTOCOL_NONE) &&
2693 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2694 return NT_STATUS_REVISION_MISMATCH;
2697 if (state->session == NULL) {
2698 continue;
2701 if (!state->smb2.should_encrypt) {
2702 continue;
2705 encryption_key = &state->session->smb2->encryption_key;
2706 if (encryption_key->length == 0) {
2707 return NT_STATUS_INVALID_PARAMETER_MIX;
2710 encryption_session_id = state->session->smb2->session_id;
2712 tf_iov = num_iov;
2713 iov[num_iov].iov_base = state->smb2.transform;
2714 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2715 num_iov += 1;
2717 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2718 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2719 state->session->smb2->nonce_low);
2720 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2721 state->session->smb2->nonce_high);
2722 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2723 encryption_session_id);
2725 state->session->smb2->nonce_low += 1;
2726 if (state->session->smb2->nonce_low == 0) {
2727 state->session->smb2->nonce_high += 1;
2728 state->session->smb2->nonce_low += 1;
2731 nbt_len += SMB2_TF_HDR_SIZE;
2732 break;
2735 for (i=0; i<num_reqs; i++) {
2736 int hdr_iov;
2737 size_t reqlen;
2738 bool ret;
2739 uint16_t opcode;
2740 uint64_t avail;
2741 uint16_t charge;
2742 uint16_t credits;
2743 uint64_t mid;
2744 const DATA_BLOB *signing_key = NULL;
2746 if (!tevent_req_is_in_progress(reqs[i])) {
2747 return NT_STATUS_INTERNAL_ERROR;
2750 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2752 if (!smbXcli_conn_is_connected(state->conn)) {
2753 return NT_STATUS_CONNECTION_DISCONNECTED;
2756 if ((state->conn->protocol != PROTOCOL_NONE) &&
2757 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2758 return NT_STATUS_REVISION_MISMATCH;
2761 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2762 if (opcode == SMB2_OP_CANCEL) {
2763 goto skip_credits;
2766 avail = UINT64_MAX - state->conn->smb2.mid;
2767 if (avail < 1) {
2768 return NT_STATUS_CONNECTION_ABORTED;
2771 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2772 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2773 } else {
2774 charge = 1;
2777 charge = MAX(state->smb2.credit_charge, charge);
2779 avail = MIN(avail, state->conn->smb2.cur_credits);
2780 if (avail < charge) {
2781 return NT_STATUS_INTERNAL_ERROR;
2784 credits = 0;
2785 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2786 credits = state->conn->smb2.max_credits -
2787 state->conn->smb2.cur_credits;
2789 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2790 credits += 1;
2793 mid = state->conn->smb2.mid;
2794 state->conn->smb2.mid += charge;
2795 state->conn->smb2.cur_credits -= charge;
2797 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2798 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2800 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2801 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2803 skip_credits:
2804 if (state->session && encryption_key == NULL) {
2806 * We prefer the channel signing key if it is
2807 * already there.
2809 if (state->smb2.should_sign) {
2810 signing_key = &state->session->smb2_channel.signing_key;
2814 * If it is a channel binding, we already have the main
2815 * signing key and try that one.
2817 if (signing_key && signing_key->length == 0) {
2818 signing_key = &state->session->smb2->signing_key;
2822 * If we do not have any session key yet, we skip the
2823 * signing of SMB2_OP_SESSSETUP requests.
2825 if (signing_key && signing_key->length == 0) {
2826 signing_key = NULL;
2830 hdr_iov = num_iov;
2831 iov[num_iov].iov_base = state->smb2.hdr;
2832 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2833 num_iov += 1;
2835 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2836 iov[num_iov].iov_len = state->smb2.fixed_len;
2837 num_iov += 1;
2839 if (state->smb2.dyn != NULL) {
2840 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2841 iov[num_iov].iov_len = state->smb2.dyn_len;
2842 num_iov += 1;
2845 reqlen = sizeof(state->smb2.hdr);
2846 reqlen += state->smb2.fixed_len;
2847 reqlen += state->smb2.dyn_len;
2849 if (i < num_reqs-1) {
2850 if ((reqlen % 8) > 0) {
2851 uint8_t pad = 8 - (reqlen % 8);
2852 iov[num_iov].iov_base = state->smb2.pad;
2853 iov[num_iov].iov_len = pad;
2854 num_iov += 1;
2855 reqlen += pad;
2857 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2860 state->smb2.encryption_session_id = encryption_session_id;
2862 if (signing_key != NULL) {
2863 NTSTATUS status;
2865 status = smb2_signing_sign_pdu(*signing_key,
2866 state->session->conn->protocol,
2867 &iov[hdr_iov], num_iov - hdr_iov);
2868 if (!NT_STATUS_IS_OK(status)) {
2869 return status;
2873 nbt_len += reqlen;
2875 ret = smbXcli_req_set_pending(reqs[i]);
2876 if (!ret) {
2877 return NT_STATUS_NO_MEMORY;
2881 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2882 _smb_setlen_tcp(state->length_hdr, nbt_len);
2883 iov[0].iov_base = state->length_hdr;
2884 iov[0].iov_len = sizeof(state->length_hdr);
2886 if (encryption_key != NULL) {
2887 NTSTATUS status;
2888 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
2889 uint8_t *buf;
2890 int vi;
2892 buf = talloc_array(iov, uint8_t, buflen);
2893 if (buf == NULL) {
2894 return NT_STATUS_NO_MEMORY;
2898 * We copy the buffers before encrypting them,
2899 * this is at least currently needed for the
2900 * to keep state->smb2.hdr.
2902 * Also the callers may expect there buffers
2903 * to be const.
2905 for (vi = tf_iov + 1; vi < num_iov; vi++) {
2906 struct iovec *v = &iov[vi];
2907 const uint8_t *o = (const uint8_t *)v->iov_base;
2909 memcpy(buf, o, v->iov_len);
2910 v->iov_base = (void *)buf;
2911 buf += v->iov_len;
2914 status = smb2_signing_encrypt_pdu(*encryption_key,
2915 state->conn->protocol,
2916 &iov[tf_iov], num_iov - tf_iov);
2917 if (!NT_STATUS_IS_OK(status)) {
2918 return status;
2922 if (state->conn->dispatch_incoming == NULL) {
2923 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2926 subreq = writev_send(state, state->ev, state->conn->outgoing,
2927 state->conn->write_fd, false, iov, num_iov);
2928 if (subreq == NULL) {
2929 return NT_STATUS_NO_MEMORY;
2931 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2932 return NT_STATUS_OK;
2935 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2937 struct smbXcli_req_state *state =
2938 tevent_req_data(req,
2939 struct smbXcli_req_state);
2941 state->smb2.credit_charge = charge;
2944 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2945 struct tevent_context *ev,
2946 struct smbXcli_conn *conn,
2947 uint16_t cmd,
2948 uint32_t additional_flags,
2949 uint32_t clear_flags,
2950 uint32_t timeout_msec,
2951 struct smbXcli_tcon *tcon,
2952 struct smbXcli_session *session,
2953 const uint8_t *fixed,
2954 uint16_t fixed_len,
2955 const uint8_t *dyn,
2956 uint32_t dyn_len)
2958 struct tevent_req *req;
2959 NTSTATUS status;
2961 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2962 additional_flags, clear_flags,
2963 timeout_msec,
2964 tcon, session,
2965 fixed, fixed_len, dyn, dyn_len);
2966 if (req == NULL) {
2967 return NULL;
2969 if (!tevent_req_is_in_progress(req)) {
2970 return tevent_req_post(req, ev);
2972 status = smb2cli_req_compound_submit(&req, 1);
2973 if (tevent_req_nterror(req, status)) {
2974 return tevent_req_post(req, ev);
2976 return req;
2979 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2981 struct tevent_req *req =
2982 tevent_req_callback_data(subreq,
2983 struct tevent_req);
2984 struct smbXcli_req_state *state =
2985 tevent_req_data(req,
2986 struct smbXcli_req_state);
2987 ssize_t nwritten;
2988 int err;
2990 nwritten = writev_recv(subreq, &err);
2991 TALLOC_FREE(subreq);
2992 if (nwritten == -1) {
2993 /* here, we need to notify all pending requests */
2994 NTSTATUS status = map_nt_error_from_unix_common(err);
2995 smbXcli_conn_disconnect(state->conn, status);
2996 return;
3000 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3001 uint8_t *buf,
3002 size_t buflen,
3003 TALLOC_CTX *mem_ctx,
3004 struct iovec **piov, int *pnum_iov)
3006 struct iovec *iov;
3007 int num_iov = 0;
3008 size_t taken = 0;
3009 uint8_t *first_hdr = buf;
3010 size_t verified_buflen = 0;
3011 uint8_t *tf = NULL;
3012 size_t tf_len = 0;
3014 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3015 if (iov == NULL) {
3016 return NT_STATUS_NO_MEMORY;
3019 while (taken < buflen) {
3020 size_t len = buflen - taken;
3021 uint8_t *hdr = first_hdr + taken;
3022 struct iovec *cur;
3023 size_t full_size;
3024 size_t next_command_ofs;
3025 uint16_t body_size;
3026 struct iovec *iov_tmp;
3028 if (verified_buflen > taken) {
3029 len = verified_buflen - taken;
3030 } else {
3031 tf = NULL;
3032 tf_len = 0;
3035 if (len < 4) {
3036 DEBUG(10, ("%d bytes left, expected at least %d\n",
3037 (int)len, 4));
3038 goto inval;
3040 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3041 struct smbXcli_session *s;
3042 uint64_t uid;
3043 struct iovec tf_iov[2];
3044 size_t enc_len;
3045 NTSTATUS status;
3047 if (len < SMB2_TF_HDR_SIZE) {
3048 DEBUG(10, ("%d bytes left, expected at least %d\n",
3049 (int)len, SMB2_TF_HDR_SIZE));
3050 goto inval;
3052 tf = hdr;
3053 tf_len = SMB2_TF_HDR_SIZE;
3054 taken += tf_len;
3056 hdr = first_hdr + taken;
3057 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3058 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3060 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3061 DEBUG(10, ("%d bytes left, expected at least %d\n",
3062 (int)len,
3063 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3064 goto inval;
3067 s = conn->sessions;
3068 for (; s; s = s->next) {
3069 if (s->smb2->session_id != uid) {
3070 continue;
3072 break;
3075 if (s == NULL) {
3076 DEBUG(10, ("unknown session_id %llu\n",
3077 (unsigned long long)uid));
3078 goto inval;
3081 tf_iov[0].iov_base = (void *)tf;
3082 tf_iov[0].iov_len = tf_len;
3083 tf_iov[1].iov_base = (void *)hdr;
3084 tf_iov[1].iov_len = enc_len;
3086 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3087 conn->protocol,
3088 tf_iov, 2);
3089 if (!NT_STATUS_IS_OK(status)) {
3090 TALLOC_FREE(iov);
3091 return status;
3094 verified_buflen = taken + enc_len;
3095 len = enc_len;
3099 * We need the header plus the body length field
3102 if (len < SMB2_HDR_BODY + 2) {
3103 DEBUG(10, ("%d bytes left, expected at least %d\n",
3104 (int)len, SMB2_HDR_BODY));
3105 goto inval;
3107 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3108 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3109 IVAL(hdr, 0)));
3110 goto inval;
3112 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3113 DEBUG(10, ("Got HDR len %d, expected %d\n",
3114 SVAL(hdr, 4), SMB2_HDR_BODY));
3115 goto inval;
3118 full_size = len;
3119 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3120 body_size = SVAL(hdr, SMB2_HDR_BODY);
3122 if (next_command_ofs != 0) {
3123 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3124 goto inval;
3126 if (next_command_ofs > full_size) {
3127 goto inval;
3129 full_size = next_command_ofs;
3131 if (body_size < 2) {
3132 goto inval;
3134 body_size &= 0xfffe;
3136 if (body_size > (full_size - SMB2_HDR_BODY)) {
3137 goto inval;
3140 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3141 num_iov + 4);
3142 if (iov_tmp == NULL) {
3143 TALLOC_FREE(iov);
3144 return NT_STATUS_NO_MEMORY;
3146 iov = iov_tmp;
3147 cur = &iov[num_iov];
3148 num_iov += 4;
3150 cur[0].iov_base = tf;
3151 cur[0].iov_len = tf_len;
3152 cur[1].iov_base = hdr;
3153 cur[1].iov_len = SMB2_HDR_BODY;
3154 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3155 cur[2].iov_len = body_size;
3156 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3157 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3159 taken += full_size;
3162 *piov = iov;
3163 *pnum_iov = num_iov;
3164 return NT_STATUS_OK;
3166 inval:
3167 TALLOC_FREE(iov);
3168 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3171 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3172 uint64_t mid)
3174 size_t num_pending = talloc_array_length(conn->pending);
3175 size_t i;
3177 for (i=0; i<num_pending; i++) {
3178 struct tevent_req *req = conn->pending[i];
3179 struct smbXcli_req_state *state =
3180 tevent_req_data(req,
3181 struct smbXcli_req_state);
3183 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3184 return req;
3187 return NULL;
3190 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3191 TALLOC_CTX *tmp_mem,
3192 uint8_t *inbuf)
3194 struct tevent_req *req;
3195 struct smbXcli_req_state *state = NULL;
3196 struct iovec *iov;
3197 int i, num_iov;
3198 NTSTATUS status;
3199 bool defer = true;
3200 struct smbXcli_session *last_session = NULL;
3201 size_t inbuf_len = smb_len_tcp(inbuf);
3203 status = smb2cli_inbuf_parse_compound(conn,
3204 inbuf + NBT_HDR_SIZE,
3205 inbuf_len,
3206 tmp_mem,
3207 &iov, &num_iov);
3208 if (!NT_STATUS_IS_OK(status)) {
3209 return status;
3212 for (i=0; i<num_iov; i+=4) {
3213 uint8_t *inbuf_ref = NULL;
3214 struct iovec *cur = &iov[i];
3215 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3216 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3217 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3218 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3219 uint16_t req_opcode;
3220 uint32_t req_flags;
3221 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3222 uint32_t new_credits;
3223 struct smbXcli_session *session = NULL;
3224 const DATA_BLOB *signing_key = NULL;
3225 bool was_encrypted = false;
3227 new_credits = conn->smb2.cur_credits;
3228 new_credits += credits;
3229 if (new_credits > UINT16_MAX) {
3230 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3232 conn->smb2.cur_credits += credits;
3234 req = smb2cli_conn_find_pending(conn, mid);
3235 if (req == NULL) {
3236 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3238 state = tevent_req_data(req, struct smbXcli_req_state);
3240 state->smb2.got_async = false;
3242 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3243 if (opcode != req_opcode) {
3244 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3246 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3248 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3249 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3252 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3253 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3254 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3255 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3258 * async interim responses are not signed,
3259 * even if the SMB2_HDR_FLAG_SIGNED flag
3260 * is set.
3262 req_flags |= SMB2_HDR_FLAG_ASYNC;
3263 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3264 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3266 if (state->smb2.notify_async) {
3267 state->smb2.got_async = true;
3268 tevent_req_defer_callback(req, state->ev);
3269 tevent_req_notify_callback(req);
3271 continue;
3274 session = state->session;
3275 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3276 session = last_session;
3278 last_session = session;
3280 if (state->smb2.should_sign) {
3281 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3282 return NT_STATUS_ACCESS_DENIED;
3286 if (flags & SMB2_HDR_FLAG_SIGNED) {
3287 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3289 if (session == NULL) {
3290 struct smbXcli_session *s;
3292 s = state->conn->sessions;
3293 for (; s; s = s->next) {
3294 if (s->smb2->session_id != uid) {
3295 continue;
3298 session = s;
3299 break;
3303 if (session == NULL) {
3304 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3307 last_session = session;
3308 signing_key = &session->smb2_channel.signing_key;
3311 if (opcode == SMB2_OP_SESSSETUP) {
3313 * We prefer the channel signing key, if it is
3314 * already there.
3316 * If we do not have a channel signing key yet,
3317 * we try the main signing key, if it is not
3318 * the final response.
3320 if (signing_key && signing_key->length == 0 &&
3321 !NT_STATUS_IS_OK(status)) {
3322 signing_key = &session->smb2->signing_key;
3325 if (signing_key && signing_key->length == 0) {
3327 * If we do not have a session key to
3328 * verify the signature, we defer the
3329 * signing check to the caller.
3331 * The caller gets NT_STATUS_OK, it
3332 * has to call
3333 * smb2cli_session_set_session_key()
3334 * or
3335 * smb2cli_session_set_channel_key()
3336 * which will check the signature
3337 * with the channel signing key.
3339 signing_key = NULL;
3343 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3344 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3345 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3348 * If the response was encrypted in a SMB2_TRANSFORM
3349 * pdu, which belongs to the correct session,
3350 * we do not need to do signing checks
3352 * It could be the session the response belongs to
3353 * or the session that was used to encrypt the
3354 * SMB2_TRANSFORM request.
3356 if ((session && session->smb2->session_id == uid) ||
3357 (state->smb2.encryption_session_id == uid)) {
3358 signing_key = NULL;
3359 was_encrypted = true;
3363 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3365 * if the server returns NT_STATUS_USER_SESSION_DELETED
3366 * the response is not signed and we should
3367 * propagate the NT_STATUS_USER_SESSION_DELETED
3368 * status to the caller.
3370 state->smb2.signing_skipped = true;
3371 signing_key = NULL;
3374 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3376 * if the server returns
3377 * NT_STATUS_INVALID_PARAMETER
3378 * the response might not be encrypted.
3380 if (state->smb2.should_encrypt && !was_encrypted) {
3381 state->smb2.signing_skipped = true;
3382 signing_key = NULL;
3386 if (state->smb2.should_encrypt && !was_encrypted) {
3387 if (!state->smb2.signing_skipped) {
3388 return NT_STATUS_ACCESS_DENIED;
3392 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3393 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3394 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3396 * if the server returns
3397 * NT_STATUS_NETWORK_NAME_DELETED
3398 * NT_STATUS_FILE_CLOSED
3399 * NT_STATUS_INVALID_PARAMETER
3400 * the response might not be signed
3401 * as this happens before the signing checks.
3403 * If server echos the signature (or all zeros)
3404 * we should report the status from the server
3405 * to the caller.
3407 if (signing_key) {
3408 int cmp;
3410 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3411 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3412 16);
3413 if (cmp == 0) {
3414 state->smb2.signing_skipped = true;
3415 signing_key = NULL;
3418 if (signing_key) {
3419 int cmp;
3420 static const uint8_t zeros[16];
3422 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3423 zeros,
3424 16);
3425 if (cmp == 0) {
3426 state->smb2.signing_skipped = true;
3427 signing_key = NULL;
3432 if (signing_key) {
3433 status = smb2_signing_check_pdu(*signing_key,
3434 state->conn->protocol,
3435 &cur[1], 3);
3436 if (!NT_STATUS_IS_OK(status)) {
3438 * If the signing check fails, we disconnect
3439 * the connection.
3441 return status;
3445 smbXcli_req_unset_pending(req);
3448 * There might be more than one response
3449 * we need to defer the notifications
3451 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3452 defer = false;
3455 if (defer) {
3456 tevent_req_defer_callback(req, state->ev);
3460 * Note: here we use talloc_reference() in a way
3461 * that does not expose it to the caller.
3463 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3464 if (tevent_req_nomem(inbuf_ref, req)) {
3465 continue;
3468 /* copy the related buffers */
3469 state->smb2.recv_iov[0] = cur[1];
3470 state->smb2.recv_iov[1] = cur[2];
3471 state->smb2.recv_iov[2] = cur[3];
3473 tevent_req_done(req);
3476 if (defer) {
3477 return NT_STATUS_RETRY;
3480 return NT_STATUS_OK;
3483 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3484 struct iovec **piov,
3485 const struct smb2cli_req_expected_response *expected,
3486 size_t num_expected)
3488 struct smbXcli_req_state *state =
3489 tevent_req_data(req,
3490 struct smbXcli_req_state);
3491 NTSTATUS status;
3492 size_t body_size;
3493 bool found_status = false;
3494 bool found_size = false;
3495 size_t i;
3497 if (piov != NULL) {
3498 *piov = NULL;
3501 if (state->smb2.got_async) {
3502 return STATUS_PENDING;
3505 if (tevent_req_is_nterror(req, &status)) {
3506 for (i=0; i < num_expected; i++) {
3507 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3508 found_status = true;
3509 break;
3513 if (found_status) {
3514 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3517 return status;
3520 if (num_expected == 0) {
3521 found_status = true;
3522 found_size = true;
3525 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3526 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3528 for (i=0; i < num_expected; i++) {
3529 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3530 continue;
3533 found_status = true;
3534 if (expected[i].body_size == 0) {
3535 found_size = true;
3536 break;
3539 if (expected[i].body_size == body_size) {
3540 found_size = true;
3541 break;
3545 if (!found_status) {
3546 return status;
3549 if (state->smb2.signing_skipped) {
3550 if (num_expected > 0) {
3551 return NT_STATUS_ACCESS_DENIED;
3553 if (!NT_STATUS_IS_ERR(status)) {
3554 return NT_STATUS_ACCESS_DENIED;
3558 if (!found_size) {
3559 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3562 if (piov != NULL) {
3563 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3566 return status;
3569 static const struct {
3570 enum protocol_types proto;
3571 const char *smb1_name;
3572 } smb1cli_prots[] = {
3573 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3574 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3575 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3576 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3577 {PROTOCOL_LANMAN2, "LM1.2X002"},
3578 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3579 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3580 {PROTOCOL_LANMAN2, "Samba"},
3581 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3582 {PROTOCOL_NT1, "NT LM 0.12"},
3583 {PROTOCOL_SMB2_02, "SMB 2.002"},
3584 {PROTOCOL_SMB2_10, "SMB 2.???"},
3587 static const struct {
3588 enum protocol_types proto;
3589 uint16_t smb2_dialect;
3590 } smb2cli_prots[] = {
3591 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3592 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3593 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3594 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3595 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3598 struct smbXcli_negprot_state {
3599 struct smbXcli_conn *conn;
3600 struct tevent_context *ev;
3601 uint32_t timeout_msec;
3602 enum protocol_types min_protocol;
3603 enum protocol_types max_protocol;
3605 struct {
3606 uint8_t fixed[36];
3607 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3608 } smb2;
3611 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3612 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3613 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3614 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3615 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3616 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3617 TALLOC_CTX *frame,
3618 uint8_t *inbuf);
3620 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3621 struct tevent_context *ev,
3622 struct smbXcli_conn *conn,
3623 uint32_t timeout_msec,
3624 enum protocol_types min_protocol,
3625 enum protocol_types max_protocol)
3627 struct tevent_req *req, *subreq;
3628 struct smbXcli_negprot_state *state;
3630 req = tevent_req_create(mem_ctx, &state,
3631 struct smbXcli_negprot_state);
3632 if (req == NULL) {
3633 return NULL;
3635 state->conn = conn;
3636 state->ev = ev;
3637 state->timeout_msec = timeout_msec;
3638 state->min_protocol = min_protocol;
3639 state->max_protocol = max_protocol;
3641 if (min_protocol == PROTOCOL_NONE) {
3642 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3643 return tevent_req_post(req, ev);
3646 if (max_protocol == PROTOCOL_NONE) {
3647 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3648 return tevent_req_post(req, ev);
3651 if (min_protocol > max_protocol) {
3652 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3653 return tevent_req_post(req, ev);
3656 if ((min_protocol < PROTOCOL_SMB2_02) &&
3657 (max_protocol < PROTOCOL_SMB2_02)) {
3659 * SMB1 only...
3661 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3663 subreq = smbXcli_negprot_smb1_subreq(state);
3664 if (tevent_req_nomem(subreq, req)) {
3665 return tevent_req_post(req, ev);
3667 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3668 return req;
3671 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3672 (max_protocol >= PROTOCOL_SMB2_02)) {
3674 * SMB2 only...
3676 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3678 subreq = smbXcli_negprot_smb2_subreq(state);
3679 if (tevent_req_nomem(subreq, req)) {
3680 return tevent_req_post(req, ev);
3682 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3683 return req;
3687 * We send an SMB1 negprot with the SMB2 dialects
3688 * and expect a SMB1 or a SMB2 response.
3690 * smbXcli_negprot_dispatch_incoming() will fix the
3691 * callback to match protocol of the response.
3693 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3695 subreq = smbXcli_negprot_smb1_subreq(state);
3696 if (tevent_req_nomem(subreq, req)) {
3697 return tevent_req_post(req, ev);
3699 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3700 return req;
3703 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3705 struct tevent_req *req =
3706 tevent_req_callback_data(subreq,
3707 struct tevent_req);
3708 NTSTATUS status;
3711 * we just want the low level error
3713 status = tevent_req_simple_recv_ntstatus(subreq);
3714 TALLOC_FREE(subreq);
3715 if (tevent_req_nterror(req, status)) {
3716 return;
3719 /* this should never happen */
3720 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3723 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3725 size_t i;
3726 DATA_BLOB bytes = data_blob_null;
3727 uint8_t flags;
3728 uint16_t flags2;
3730 /* setup the protocol strings */
3731 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3732 uint8_t c = 2;
3733 bool ok;
3735 if (smb1cli_prots[i].proto < state->min_protocol) {
3736 continue;
3739 if (smb1cli_prots[i].proto > state->max_protocol) {
3740 continue;
3743 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3744 if (!ok) {
3745 return NULL;
3749 * We now it is already ascii and
3750 * we want NULL termination.
3752 ok = data_blob_append(state, &bytes,
3753 smb1cli_prots[i].smb1_name,
3754 strlen(smb1cli_prots[i].smb1_name)+1);
3755 if (!ok) {
3756 return NULL;
3760 smb1cli_req_flags(state->max_protocol,
3761 state->conn->smb1.client.capabilities,
3762 SMBnegprot,
3763 0, 0, &flags,
3764 0, 0, &flags2);
3766 return smb1cli_req_send(state, state->ev, state->conn,
3767 SMBnegprot,
3768 flags, ~flags,
3769 flags2, ~flags2,
3770 state->timeout_msec,
3771 0xFFFE, 0, NULL, /* pid, tid, session */
3772 0, NULL, /* wct, vwv */
3773 bytes.length, bytes.data);
3776 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3778 struct tevent_req *req =
3779 tevent_req_callback_data(subreq,
3780 struct tevent_req);
3781 struct smbXcli_negprot_state *state =
3782 tevent_req_data(req,
3783 struct smbXcli_negprot_state);
3784 struct smbXcli_conn *conn = state->conn;
3785 struct iovec *recv_iov = NULL;
3786 uint8_t *inhdr;
3787 uint8_t wct;
3788 uint16_t *vwv;
3789 uint32_t num_bytes;
3790 uint8_t *bytes;
3791 NTSTATUS status;
3792 uint16_t protnum;
3793 size_t i;
3794 size_t num_prots = 0;
3795 uint8_t flags;
3796 uint32_t client_capabilities = conn->smb1.client.capabilities;
3797 uint32_t both_capabilities;
3798 uint32_t server_capabilities = 0;
3799 uint32_t capabilities;
3800 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3801 uint32_t server_max_xmit = 0;
3802 uint32_t max_xmit;
3803 uint32_t server_max_mux = 0;
3804 uint16_t server_security_mode = 0;
3805 uint32_t server_session_key = 0;
3806 bool server_readbraw = false;
3807 bool server_writebraw = false;
3808 bool server_lockread = false;
3809 bool server_writeunlock = false;
3810 struct GUID server_guid = GUID_zero();
3811 DATA_BLOB server_gss_blob = data_blob_null;
3812 uint8_t server_challenge[8];
3813 char *server_workgroup = NULL;
3814 char *server_name = NULL;
3815 int server_time_zone = 0;
3816 NTTIME server_system_time = 0;
3817 static const struct smb1cli_req_expected_response expected[] = {
3819 .status = NT_STATUS_OK,
3820 .wct = 0x11, /* NT1 */
3823 .status = NT_STATUS_OK,
3824 .wct = 0x0D, /* LM */
3827 .status = NT_STATUS_OK,
3828 .wct = 0x01, /* CORE */
3832 ZERO_STRUCT(server_challenge);
3834 status = smb1cli_req_recv(subreq, state,
3835 &recv_iov,
3836 &inhdr,
3837 &wct,
3838 &vwv,
3839 NULL, /* pvwv_offset */
3840 &num_bytes,
3841 &bytes,
3842 NULL, /* pbytes_offset */
3843 NULL, /* pinbuf */
3844 expected, ARRAY_SIZE(expected));
3845 TALLOC_FREE(subreq);
3846 if (tevent_req_nterror(req, status)) {
3847 return;
3850 flags = CVAL(inhdr, HDR_FLG);
3852 protnum = SVAL(vwv, 0);
3854 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3855 if (smb1cli_prots[i].proto < state->min_protocol) {
3856 continue;
3859 if (smb1cli_prots[i].proto > state->max_protocol) {
3860 continue;
3863 if (protnum != num_prots) {
3864 num_prots++;
3865 continue;
3868 conn->protocol = smb1cli_prots[i].proto;
3869 break;
3872 if (conn->protocol == PROTOCOL_NONE) {
3873 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3874 return;
3877 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3878 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3879 "and the selected protocol level doesn't support it.\n"));
3880 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3881 return;
3884 if (flags & FLAG_SUPPORT_LOCKREAD) {
3885 server_lockread = true;
3886 server_writeunlock = true;
3889 if (conn->protocol >= PROTOCOL_NT1) {
3890 const char *client_signing = NULL;
3891 bool server_mandatory = false;
3892 bool server_allowed = false;
3893 const char *server_signing = NULL;
3894 bool ok;
3895 uint8_t key_len;
3897 if (wct != 0x11) {
3898 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3899 return;
3902 /* NT protocol */
3903 server_security_mode = CVAL(vwv + 1, 0);
3904 server_max_mux = SVAL(vwv + 1, 1);
3905 server_max_xmit = IVAL(vwv + 3, 1);
3906 server_session_key = IVAL(vwv + 7, 1);
3907 server_time_zone = SVALS(vwv + 15, 1);
3908 server_time_zone *= 60;
3909 /* this time arrives in real GMT */
3910 server_system_time = BVAL(vwv + 11, 1);
3911 server_capabilities = IVAL(vwv + 9, 1);
3913 key_len = CVAL(vwv + 16, 1);
3915 if (server_capabilities & CAP_RAW_MODE) {
3916 server_readbraw = true;
3917 server_writebraw = true;
3919 if (server_capabilities & CAP_LOCK_AND_READ) {
3920 server_lockread = true;
3923 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3924 DATA_BLOB blob1, blob2;
3926 if (num_bytes < 16) {
3927 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3928 return;
3931 blob1 = data_blob_const(bytes, 16);
3932 status = GUID_from_data_blob(&blob1, &server_guid);
3933 if (tevent_req_nterror(req, status)) {
3934 return;
3937 blob1 = data_blob_const(bytes+16, num_bytes-16);
3938 blob2 = data_blob_dup_talloc(state, blob1);
3939 if (blob1.length > 0 &&
3940 tevent_req_nomem(blob2.data, req)) {
3941 return;
3943 server_gss_blob = blob2;
3944 } else {
3945 DATA_BLOB blob1, blob2;
3947 if (num_bytes < key_len) {
3948 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3949 return;
3952 if (key_len != 0 && key_len != 8) {
3953 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3954 return;
3957 if (key_len == 8) {
3958 memcpy(server_challenge, bytes, 8);
3961 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3962 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3963 if (blob1.length > 0) {
3964 size_t len;
3966 len = utf16_len_n(blob1.data,
3967 blob1.length);
3968 blob1.length = len;
3970 ok = convert_string_talloc(state,
3971 CH_UTF16LE,
3972 CH_UNIX,
3973 blob1.data,
3974 blob1.length,
3975 &server_workgroup,
3976 &len);
3977 if (!ok) {
3978 status = map_nt_error_from_unix_common(errno);
3979 tevent_req_nterror(req, status);
3980 return;
3984 blob2.data += blob1.length;
3985 blob2.length -= blob1.length;
3986 if (blob2.length > 0) {
3987 size_t len;
3989 len = utf16_len_n(blob1.data,
3990 blob1.length);
3991 blob1.length = len;
3993 ok = convert_string_talloc(state,
3994 CH_UTF16LE,
3995 CH_UNIX,
3996 blob2.data,
3997 blob2.length,
3998 &server_name,
3999 &len);
4000 if (!ok) {
4001 status = map_nt_error_from_unix_common(errno);
4002 tevent_req_nterror(req, status);
4003 return;
4008 client_signing = "disabled";
4009 if (conn->allow_signing) {
4010 client_signing = "allowed";
4012 if (conn->mandatory_signing) {
4013 client_signing = "required";
4016 server_signing = "not supported";
4017 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4018 server_signing = "supported";
4019 server_allowed = true;
4021 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4022 server_signing = "required";
4023 server_mandatory = true;
4026 ok = smb_signing_set_negotiated(conn->smb1.signing,
4027 server_allowed,
4028 server_mandatory);
4029 if (!ok) {
4030 DEBUG(1,("cli_negprot: SMB signing is required, "
4031 "but client[%s] and server[%s] mismatch\n",
4032 client_signing, server_signing));
4033 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4034 return;
4037 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4038 DATA_BLOB blob1;
4039 uint8_t key_len;
4040 time_t t;
4042 if (wct != 0x0D) {
4043 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4044 return;
4047 server_security_mode = SVAL(vwv + 1, 0);
4048 server_max_xmit = SVAL(vwv + 2, 0);
4049 server_max_mux = SVAL(vwv + 3, 0);
4050 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4051 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4052 server_session_key = IVAL(vwv + 6, 0);
4053 server_time_zone = SVALS(vwv + 10, 0);
4054 server_time_zone *= 60;
4055 /* this time is converted to GMT by make_unix_date */
4056 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4057 unix_to_nt_time(&server_system_time, t);
4058 key_len = SVAL(vwv + 11, 0);
4060 if (num_bytes < key_len) {
4061 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4062 return;
4065 if (key_len != 0 && key_len != 8) {
4066 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4067 return;
4070 if (key_len == 8) {
4071 memcpy(server_challenge, bytes, 8);
4074 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4075 if (blob1.length > 0) {
4076 size_t len;
4077 bool ok;
4079 len = utf16_len_n(blob1.data,
4080 blob1.length);
4081 blob1.length = len;
4083 ok = convert_string_talloc(state,
4084 CH_DOS,
4085 CH_UNIX,
4086 blob1.data,
4087 blob1.length,
4088 &server_workgroup,
4089 &len);
4090 if (!ok) {
4091 status = map_nt_error_from_unix_common(errno);
4092 tevent_req_nterror(req, status);
4093 return;
4097 } else {
4098 /* the old core protocol */
4099 server_time_zone = get_time_zone(time(NULL));
4100 server_max_xmit = 1024;
4101 server_max_mux = 1;
4104 if (server_max_xmit < 1024) {
4105 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4106 return;
4109 if (server_max_mux < 1) {
4110 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4111 return;
4115 * Now calculate the negotiated capabilities
4116 * based on the mask for:
4117 * - client only flags
4118 * - flags used in both directions
4119 * - server only flags
4121 both_capabilities = client_capabilities & server_capabilities;
4122 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4123 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4124 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4126 max_xmit = MIN(client_max_xmit, server_max_xmit);
4128 conn->smb1.server.capabilities = server_capabilities;
4129 conn->smb1.capabilities = capabilities;
4131 conn->smb1.server.max_xmit = server_max_xmit;
4132 conn->smb1.max_xmit = max_xmit;
4134 conn->smb1.server.max_mux = server_max_mux;
4136 conn->smb1.server.security_mode = server_security_mode;
4138 conn->smb1.server.readbraw = server_readbraw;
4139 conn->smb1.server.writebraw = server_writebraw;
4140 conn->smb1.server.lockread = server_lockread;
4141 conn->smb1.server.writeunlock = server_writeunlock;
4143 conn->smb1.server.session_key = server_session_key;
4145 talloc_steal(conn, server_gss_blob.data);
4146 conn->smb1.server.gss_blob = server_gss_blob;
4147 conn->smb1.server.guid = server_guid;
4148 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4149 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4150 conn->smb1.server.name = talloc_move(conn, &server_name);
4152 conn->smb1.server.time_zone = server_time_zone;
4153 conn->smb1.server.system_time = server_system_time;
4155 tevent_req_done(req);
4158 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4160 size_t i;
4161 uint8_t *buf;
4162 uint16_t dialect_count = 0;
4164 buf = state->smb2.dyn;
4165 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4166 if (smb2cli_prots[i].proto < state->min_protocol) {
4167 continue;
4170 if (smb2cli_prots[i].proto > state->max_protocol) {
4171 continue;
4174 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4175 dialect_count++;
4178 buf = state->smb2.fixed;
4179 SSVAL(buf, 0, 36);
4180 SSVAL(buf, 2, dialect_count);
4181 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4182 SSVAL(buf, 6, 0); /* Reserved */
4183 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4184 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4185 } else {
4186 SIVAL(buf, 8, 0); /* Capabilities */
4188 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4189 NTSTATUS status;
4190 DATA_BLOB blob;
4192 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4193 state, &blob);
4194 if (!NT_STATUS_IS_OK(status)) {
4195 return NULL;
4197 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4198 } else {
4199 memset(buf+12, 0, 16); /* ClientGuid */
4201 SBVAL(buf, 28, 0); /* ClientStartTime */
4203 return smb2cli_req_send(state, state->ev,
4204 state->conn, SMB2_OP_NEGPROT,
4205 0, 0, /* flags */
4206 state->timeout_msec,
4207 NULL, NULL, /* tcon, session */
4208 state->smb2.fixed, sizeof(state->smb2.fixed),
4209 state->smb2.dyn, dialect_count*2);
4212 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4214 struct tevent_req *req =
4215 tevent_req_callback_data(subreq,
4216 struct tevent_req);
4217 struct smbXcli_negprot_state *state =
4218 tevent_req_data(req,
4219 struct smbXcli_negprot_state);
4220 struct smbXcli_conn *conn = state->conn;
4221 size_t security_offset, security_length;
4222 DATA_BLOB blob;
4223 NTSTATUS status;
4224 struct iovec *iov;
4225 uint8_t *body;
4226 size_t i;
4227 uint16_t dialect_revision;
4228 static const struct smb2cli_req_expected_response expected[] = {
4230 .status = NT_STATUS_OK,
4231 .body_size = 0x41
4235 status = smb2cli_req_recv(subreq, state, &iov,
4236 expected, ARRAY_SIZE(expected));
4237 TALLOC_FREE(subreq);
4238 if (tevent_req_nterror(req, status)) {
4239 return;
4242 body = (uint8_t *)iov[1].iov_base;
4244 dialect_revision = SVAL(body, 4);
4246 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4247 if (smb2cli_prots[i].proto < state->min_protocol) {
4248 continue;
4251 if (smb2cli_prots[i].proto > state->max_protocol) {
4252 continue;
4255 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4256 continue;
4259 conn->protocol = smb2cli_prots[i].proto;
4260 break;
4263 if (conn->protocol == PROTOCOL_NONE) {
4264 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4265 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4266 return;
4269 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4270 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4271 return;
4274 /* make sure we do not loop forever */
4275 state->min_protocol = PROTOCOL_SMB2_02;
4278 * send a SMB2 negprot, in order to negotiate
4279 * the SMB2 dialect.
4281 subreq = smbXcli_negprot_smb2_subreq(state);
4282 if (tevent_req_nomem(subreq, req)) {
4283 return;
4285 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4286 return;
4289 conn->smb2.server.security_mode = SVAL(body, 2);
4291 blob = data_blob_const(body + 8, 16);
4292 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4293 if (tevent_req_nterror(req, status)) {
4294 return;
4297 conn->smb2.server.capabilities = IVAL(body, 24);
4298 conn->smb2.server.max_trans_size= IVAL(body, 28);
4299 conn->smb2.server.max_read_size = IVAL(body, 32);
4300 conn->smb2.server.max_write_size= IVAL(body, 36);
4301 conn->smb2.server.system_time = BVAL(body, 40);
4302 conn->smb2.server.start_time = BVAL(body, 48);
4304 security_offset = SVAL(body, 56);
4305 security_length = SVAL(body, 58);
4307 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4308 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4309 return;
4312 if (security_length > iov[2].iov_len) {
4313 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4314 return;
4317 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4318 iov[2].iov_base,
4319 security_length);
4320 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4321 return;
4324 tevent_req_done(req);
4327 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4328 TALLOC_CTX *tmp_mem,
4329 uint8_t *inbuf)
4331 size_t num_pending = talloc_array_length(conn->pending);
4332 struct tevent_req *subreq;
4333 struct smbXcli_req_state *substate;
4334 struct tevent_req *req;
4335 uint32_t protocol_magic;
4336 size_t inbuf_len = smb_len_nbt(inbuf);
4338 if (num_pending != 1) {
4339 return NT_STATUS_INTERNAL_ERROR;
4342 if (inbuf_len < 4) {
4343 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4346 subreq = conn->pending[0];
4347 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4348 req = tevent_req_callback_data(subreq, struct tevent_req);
4350 protocol_magic = IVAL(inbuf, 4);
4352 switch (protocol_magic) {
4353 case SMB_MAGIC:
4354 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4355 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4356 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4358 case SMB2_MAGIC:
4359 if (substate->smb2.recv_iov == NULL) {
4361 * For the SMB1 negprot we have move it.
4363 substate->smb2.recv_iov = substate->smb1.recv_iov;
4364 substate->smb1.recv_iov = NULL;
4368 * we got an SMB2 answer, which consumed sequence number 0
4369 * so we need to use 1 as the next one
4371 conn->smb2.mid = 1;
4372 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4373 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4374 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4377 DEBUG(10, ("Got non-SMB PDU\n"));
4378 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4381 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4383 return tevent_req_simple_recv_ntstatus(req);
4386 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4387 uint32_t timeout_msec,
4388 enum protocol_types min_protocol,
4389 enum protocol_types max_protocol)
4391 TALLOC_CTX *frame = talloc_stackframe();
4392 struct tevent_context *ev;
4393 struct tevent_req *req;
4394 NTSTATUS status = NT_STATUS_NO_MEMORY;
4395 bool ok;
4397 if (smbXcli_conn_has_async_calls(conn)) {
4399 * Can't use sync call while an async call is in flight
4401 status = NT_STATUS_INVALID_PARAMETER_MIX;
4402 goto fail;
4404 ev = tevent_context_init(frame);
4405 if (ev == NULL) {
4406 goto fail;
4408 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4409 min_protocol, max_protocol);
4410 if (req == NULL) {
4411 goto fail;
4413 ok = tevent_req_poll(req, ev);
4414 if (!ok) {
4415 status = map_nt_error_from_unix_common(errno);
4416 goto fail;
4418 status = smbXcli_negprot_recv(req);
4419 fail:
4420 TALLOC_FREE(frame);
4421 return status;
4424 static int smbXcli_session_destructor(struct smbXcli_session *session)
4426 if (session->conn == NULL) {
4427 return 0;
4430 DLIST_REMOVE(session->conn->sessions, session);
4431 return 0;
4434 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4435 struct smbXcli_conn *conn)
4437 struct smbXcli_session *session;
4439 session = talloc_zero(mem_ctx, struct smbXcli_session);
4440 if (session == NULL) {
4441 return NULL;
4443 session->smb2 = talloc_zero(session, struct smb2cli_session);
4444 if (session->smb2 == NULL) {
4445 talloc_free(session);
4446 return NULL;
4448 talloc_set_destructor(session, smbXcli_session_destructor);
4450 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4451 session->conn = conn;
4453 return session;
4456 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4457 TALLOC_CTX *mem_ctx,
4458 DATA_BLOB *key)
4460 const DATA_BLOB *application_key;
4462 *key = data_blob_null;
4464 if (session->conn == NULL) {
4465 return NT_STATUS_NO_USER_SESSION_KEY;
4468 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4469 application_key = &session->smb2->application_key;
4470 } else {
4471 application_key = &session->smb1.application_key;
4474 if (application_key->length == 0) {
4475 return NT_STATUS_NO_USER_SESSION_KEY;
4478 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4479 if (key->data == NULL) {
4480 return NT_STATUS_NO_MEMORY;
4483 return NT_STATUS_OK;
4486 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4488 return session->smb1.session_id;
4491 void smb1cli_session_set_id(struct smbXcli_session *session,
4492 uint16_t session_id)
4494 session->smb1.session_id = session_id;
4497 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4498 const DATA_BLOB _session_key)
4500 struct smbXcli_conn *conn = session->conn;
4501 uint8_t session_key[16];
4503 if (conn == NULL) {
4504 return NT_STATUS_INVALID_PARAMETER_MIX;
4507 if (session->smb1.application_key.length != 0) {
4509 * TODO: do not allow this...
4511 * return NT_STATUS_INVALID_PARAMETER_MIX;
4513 data_blob_clear_free(&session->smb1.application_key);
4514 session->smb1.protected_key = false;
4517 if (_session_key.length == 0) {
4518 return NT_STATUS_OK;
4521 ZERO_STRUCT(session_key);
4522 memcpy(session_key, _session_key.data,
4523 MIN(_session_key.length, sizeof(session_key)));
4525 session->smb1.application_key = data_blob_talloc(session,
4526 session_key,
4527 sizeof(session_key));
4528 ZERO_STRUCT(session_key);
4529 if (session->smb1.application_key.data == NULL) {
4530 return NT_STATUS_NO_MEMORY;
4533 session->smb1.protected_key = false;
4535 return NT_STATUS_OK;
4538 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4540 if (session->smb1.protected_key) {
4541 /* already protected */
4542 return NT_STATUS_OK;
4545 if (session->smb1.application_key.length != 16) {
4546 return NT_STATUS_INVALID_PARAMETER_MIX;
4549 smb_key_derivation(session->smb1.application_key.data,
4550 session->smb1.application_key.length,
4551 session->smb1.application_key.data);
4553 session->smb1.protected_key = true;
4555 return NT_STATUS_OK;
4558 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4560 struct smbXcli_conn *conn = session->conn;
4561 uint8_t security_mode = 0;
4563 if (conn == NULL) {
4564 return security_mode;
4567 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4568 if (conn->mandatory_signing) {
4569 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4572 return security_mode;
4575 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4577 return session->smb2->session_id;
4580 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4582 return session->smb2->session_flags;
4585 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4586 uint64_t session_id,
4587 uint16_t session_flags)
4589 session->smb2->session_id = session_id;
4590 session->smb2->session_flags = session_flags;
4593 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4595 session->smb2->channel_sequence += 1;
4598 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4599 const DATA_BLOB _session_key,
4600 const struct iovec *recv_iov)
4602 struct smbXcli_conn *conn = session->conn;
4603 uint16_t no_sign_flags;
4604 uint8_t session_key[16];
4605 NTSTATUS status;
4607 if (conn == NULL) {
4608 return NT_STATUS_INVALID_PARAMETER_MIX;
4611 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4613 if (session->smb2->session_flags & no_sign_flags) {
4614 session->smb2->should_sign = false;
4615 return NT_STATUS_OK;
4618 if (session->smb2->signing_key.length != 0) {
4619 return NT_STATUS_INVALID_PARAMETER_MIX;
4622 ZERO_STRUCT(session_key);
4623 memcpy(session_key, _session_key.data,
4624 MIN(_session_key.length, sizeof(session_key)));
4626 session->smb2->signing_key = data_blob_talloc(session,
4627 session_key,
4628 sizeof(session_key));
4629 if (session->smb2->signing_key.data == NULL) {
4630 ZERO_STRUCT(session_key);
4631 return NT_STATUS_NO_MEMORY;
4634 if (conn->protocol >= PROTOCOL_SMB2_24) {
4635 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4636 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4638 smb2_key_derivation(session_key, sizeof(session_key),
4639 label.data, label.length,
4640 context.data, context.length,
4641 session->smb2->signing_key.data);
4644 session->smb2->encryption_key = data_blob_dup_talloc(session,
4645 session->smb2->signing_key);
4646 if (session->smb2->encryption_key.data == NULL) {
4647 ZERO_STRUCT(session_key);
4648 return NT_STATUS_NO_MEMORY;
4651 if (conn->protocol >= PROTOCOL_SMB2_24) {
4652 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4653 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4655 smb2_key_derivation(session_key, sizeof(session_key),
4656 label.data, label.length,
4657 context.data, context.length,
4658 session->smb2->encryption_key.data);
4661 session->smb2->decryption_key = data_blob_dup_talloc(session,
4662 session->smb2->signing_key);
4663 if (session->smb2->decryption_key.data == NULL) {
4664 ZERO_STRUCT(session_key);
4665 return NT_STATUS_NO_MEMORY;
4668 if (conn->protocol >= PROTOCOL_SMB2_24) {
4669 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4670 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4672 smb2_key_derivation(session_key, sizeof(session_key),
4673 label.data, label.length,
4674 context.data, context.length,
4675 session->smb2->decryption_key.data);
4678 session->smb2->application_key = data_blob_dup_talloc(session,
4679 session->smb2->signing_key);
4680 if (session->smb2->application_key.data == NULL) {
4681 ZERO_STRUCT(session_key);
4682 return NT_STATUS_NO_MEMORY;
4685 if (conn->protocol >= PROTOCOL_SMB2_24) {
4686 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4687 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4689 smb2_key_derivation(session_key, sizeof(session_key),
4690 label.data, label.length,
4691 context.data, context.length,
4692 session->smb2->application_key.data);
4694 ZERO_STRUCT(session_key);
4696 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4697 session->smb2->signing_key);
4698 if (session->smb2_channel.signing_key.data == NULL) {
4699 return NT_STATUS_NO_MEMORY;
4702 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4703 session->conn->protocol,
4704 recv_iov, 3);
4705 if (!NT_STATUS_IS_OK(status)) {
4706 return status;
4709 session->smb2->should_sign = false;
4710 session->smb2->should_encrypt = false;
4712 if (conn->desire_signing) {
4713 session->smb2->should_sign = true;
4716 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4717 session->smb2->should_sign = true;
4720 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4721 session->smb2->should_encrypt = true;
4724 if (conn->protocol < PROTOCOL_SMB2_24) {
4725 session->smb2->should_encrypt = false;
4728 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4729 session->smb2->should_encrypt = false;
4732 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4733 sizeof(session->smb2->nonce_high));
4734 session->smb2->nonce_low = 1;
4736 return NT_STATUS_OK;
4739 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4740 struct smbXcli_session *session1,
4741 struct smbXcli_conn *conn,
4742 struct smbXcli_session **_session2)
4744 struct smbXcli_session *session2;
4746 if (session1->smb2->signing_key.length == 0) {
4747 return NT_STATUS_INVALID_PARAMETER_MIX;
4750 if (conn == NULL) {
4751 return NT_STATUS_INVALID_PARAMETER_MIX;
4754 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4755 if (session2 == NULL) {
4756 return NT_STATUS_NO_MEMORY;
4758 session2->smb2 = talloc_reference(session2, session1->smb2);
4759 if (session2->smb2 == NULL) {
4760 talloc_free(session2);
4761 return NT_STATUS_NO_MEMORY;
4764 talloc_set_destructor(session2, smbXcli_session_destructor);
4765 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4766 session2->conn = conn;
4768 *_session2 = session2;
4769 return NT_STATUS_OK;
4772 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4773 const DATA_BLOB _channel_key,
4774 const struct iovec *recv_iov)
4776 struct smbXcli_conn *conn = session->conn;
4777 uint8_t channel_key[16];
4778 NTSTATUS status;
4780 if (conn == NULL) {
4781 return NT_STATUS_INVALID_PARAMETER_MIX;
4784 if (session->smb2_channel.signing_key.length != 0) {
4785 return NT_STATUS_INVALID_PARAMETER_MIX;
4788 ZERO_STRUCT(channel_key);
4789 memcpy(channel_key, _channel_key.data,
4790 MIN(_channel_key.length, sizeof(channel_key)));
4792 session->smb2_channel.signing_key = data_blob_talloc(session,
4793 channel_key,
4794 sizeof(channel_key));
4795 if (session->smb2_channel.signing_key.data == NULL) {
4796 ZERO_STRUCT(channel_key);
4797 return NT_STATUS_NO_MEMORY;
4800 if (conn->protocol >= PROTOCOL_SMB2_24) {
4801 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4802 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4804 smb2_key_derivation(channel_key, sizeof(channel_key),
4805 label.data, label.length,
4806 context.data, context.length,
4807 session->smb2_channel.signing_key.data);
4809 ZERO_STRUCT(channel_key);
4811 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4812 session->conn->protocol,
4813 recv_iov, 3);
4814 if (!NT_STATUS_IS_OK(status)) {
4815 return status;
4818 return NT_STATUS_OK;
4821 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4823 struct smbXcli_tcon *tcon;
4825 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4826 if (tcon == NULL) {
4827 return NULL;
4830 return tcon;
4833 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4835 return tcon->smb1.tcon_id;
4838 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4840 tcon->smb1.tcon_id = tcon_id;
4843 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
4844 uint16_t tcon_id,
4845 uint16_t optional_support,
4846 uint32_t maximal_access,
4847 uint32_t guest_maximal_access,
4848 const char *service,
4849 const char *fs_type)
4851 tcon->smb1.tcon_id = tcon_id;
4852 tcon->smb1.optional_support = optional_support;
4853 tcon->smb1.maximal_access = maximal_access;
4854 tcon->smb1.guest_maximal_access = guest_maximal_access;
4856 TALLOC_FREE(tcon->smb1.service);
4857 tcon->smb1.service = talloc_strdup(tcon, service);
4858 if (service != NULL && tcon->smb1.service == NULL) {
4859 return false;
4862 TALLOC_FREE(tcon->smb1.fs_type);
4863 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
4864 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
4865 return false;
4868 return true;
4871 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
4873 return tcon->smb2.tcon_id;
4876 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
4878 return tcon->smb2.capabilities;
4881 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
4882 struct smbXcli_session *session,
4883 uint32_t tcon_id,
4884 uint8_t type,
4885 uint32_t flags,
4886 uint32_t capabilities,
4887 uint32_t maximal_access)
4889 tcon->smb2.tcon_id = tcon_id;
4890 tcon->smb2.type = type;
4891 tcon->smb2.flags = flags;
4892 tcon->smb2.capabilities = capabilities;
4893 tcon->smb2.maximal_access = maximal_access;
4895 tcon->smb2.should_encrypt = false;
4897 if (session == NULL) {
4898 return;
4901 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
4903 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
4904 tcon->smb2.should_encrypt = true;