libcli/smb: fix the credit handling on a SMB1 => SMB2 negotiate
[Samba/wip.git] / libcli / smb / smbXcli_base.c
blob3b24f9a07aafb1acb3c77ab96278f449cac6f1c9
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 * this should be a short term hack
163 * until the upper layers have implemented
164 * re-authentication.
166 bool disconnect_expired;
169 struct smbXcli_tcon {
170 struct {
171 uint16_t tcon_id;
172 uint16_t optional_support;
173 uint32_t maximal_access;
174 uint32_t guest_maximal_access;
175 char *service;
176 char *fs_type;
177 } smb1;
179 struct {
180 uint32_t tcon_id;
181 uint8_t type;
182 uint32_t flags;
183 uint32_t capabilities;
184 uint32_t maximal_access;
185 bool should_encrypt;
186 } smb2;
189 struct smbXcli_req_state {
190 struct tevent_context *ev;
191 struct smbXcli_conn *conn;
192 struct smbXcli_session *session; /* maybe NULL */
193 struct smbXcli_tcon *tcon; /* maybe NULL */
195 uint8_t length_hdr[4];
197 bool one_way;
199 uint8_t *inbuf;
201 struct {
202 /* Space for the header including the wct */
203 uint8_t hdr[HDR_VWV];
206 * For normal requests, smb1cli_req_send chooses a mid.
207 * SecondaryV trans requests need to use the mid of the primary
208 * request, so we need a place to store it.
209 * Assume it is set if != 0.
211 uint16_t mid;
213 uint16_t *vwv;
214 uint8_t bytecount_buf[2];
216 #define MAX_SMB_IOV 10
217 /* length_hdr, hdr, words, byte_count, buffers */
218 struct iovec iov[1 + 3 + MAX_SMB_IOV];
219 int iov_count;
221 bool one_way_seqnum;
222 uint32_t seqnum;
223 struct tevent_req **chained_requests;
225 uint8_t recv_cmd;
226 NTSTATUS recv_status;
227 /* always an array of 3 talloc elements */
228 struct iovec *recv_iov;
229 } smb1;
231 struct {
232 const uint8_t *fixed;
233 uint16_t fixed_len;
234 const uint8_t *dyn;
235 uint32_t dyn_len;
237 uint8_t transform[SMB2_TF_HDR_SIZE];
238 uint8_t hdr[SMB2_HDR_BODY];
239 uint8_t pad[7]; /* padding space for compounding */
242 * always an array of 3 talloc elements
243 * (without a SMB2_TRANSFORM header!)
245 * HDR, BODY, DYN
247 struct iovec *recv_iov;
249 uint16_t credit_charge;
251 bool should_sign;
252 bool should_encrypt;
253 uint64_t encryption_session_id;
255 bool signing_skipped;
256 bool notify_async;
257 bool got_async;
258 } smb2;
261 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
264 * NT_STATUS_OK, means we do not notify the callers
266 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
268 while (conn->sessions) {
269 conn->sessions->conn = NULL;
270 DLIST_REMOVE(conn->sessions, conn->sessions);
273 if (conn->smb1.trans_enc) {
274 TALLOC_FREE(conn->smb1.trans_enc);
277 return 0;
280 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
281 int fd,
282 const char *remote_name,
283 enum smb_signing_setting signing_state,
284 uint32_t smb1_capabilities,
285 struct GUID *client_guid,
286 uint32_t smb2_capabilities)
288 struct smbXcli_conn *conn = NULL;
289 void *ss = NULL;
290 struct sockaddr *sa = NULL;
291 socklen_t sa_length;
292 int ret;
294 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
295 if (!conn) {
296 return NULL;
299 conn->read_fd = fd;
300 conn->write_fd = dup(fd);
301 if (conn->write_fd == -1) {
302 goto error;
305 conn->remote_name = talloc_strdup(conn, remote_name);
306 if (conn->remote_name == NULL) {
307 goto error;
311 ss = (void *)&conn->local_ss;
312 sa = (struct sockaddr *)ss;
313 sa_length = sizeof(conn->local_ss);
314 ret = getsockname(fd, sa, &sa_length);
315 if (ret == -1) {
316 goto error;
318 ss = (void *)&conn->remote_ss;
319 sa = (struct sockaddr *)ss;
320 sa_length = sizeof(conn->remote_ss);
321 ret = getpeername(fd, sa, &sa_length);
322 if (ret == -1) {
323 goto error;
326 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
327 if (conn->outgoing == NULL) {
328 goto error;
330 conn->pending = NULL;
332 conn->protocol = PROTOCOL_NONE;
334 switch (signing_state) {
335 case SMB_SIGNING_OFF:
336 /* never */
337 conn->allow_signing = false;
338 conn->desire_signing = false;
339 conn->mandatory_signing = false;
340 break;
341 case SMB_SIGNING_DEFAULT:
342 case SMB_SIGNING_IF_REQUIRED:
343 /* if the server requires it */
344 conn->allow_signing = true;
345 conn->desire_signing = false;
346 conn->mandatory_signing = false;
347 break;
348 case SMB_SIGNING_REQUIRED:
349 /* always */
350 conn->allow_signing = true;
351 conn->desire_signing = true;
352 conn->mandatory_signing = true;
353 break;
356 conn->smb1.client.capabilities = smb1_capabilities;
357 conn->smb1.client.max_xmit = UINT16_MAX;
359 conn->smb1.capabilities = conn->smb1.client.capabilities;
360 conn->smb1.max_xmit = 1024;
362 conn->smb1.mid = 1;
364 /* initialise signing */
365 conn->smb1.signing = smb_signing_init(conn,
366 conn->allow_signing,
367 conn->desire_signing,
368 conn->mandatory_signing);
369 if (!conn->smb1.signing) {
370 goto error;
373 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
374 if (conn->mandatory_signing) {
375 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
377 if (client_guid) {
378 conn->smb2.client.guid = *client_guid;
380 conn->smb2.client.capabilities = smb2_capabilities;
382 conn->smb2.cur_credits = 1;
383 conn->smb2.max_credits = 0;
385 talloc_set_destructor(conn, smbXcli_conn_destructor);
386 return conn;
388 error:
389 if (conn->write_fd != -1) {
390 close(conn->write_fd);
392 TALLOC_FREE(conn);
393 return NULL;
396 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
398 if (conn == NULL) {
399 return false;
402 if (conn->read_fd == -1) {
403 return false;
406 return true;
409 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
411 return conn->protocol;
414 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
416 if (conn->protocol >= PROTOCOL_SMB2_02) {
417 return true;
420 if (conn->smb1.capabilities & CAP_UNICODE) {
421 return true;
424 return false;
427 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
429 set_socket_options(conn->read_fd, options);
432 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
434 return &conn->local_ss;
437 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
439 return &conn->remote_ss;
442 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
444 return conn->remote_name;
447 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
449 if (conn->protocol >= PROTOCOL_SMB2_02) {
451 * TODO...
453 return 1;
456 return conn->smb1.server.max_mux;
459 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
461 if (conn->protocol >= PROTOCOL_SMB2_02) {
462 return conn->smb2.server.system_time;
465 return conn->smb1.server.system_time;
468 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
470 if (conn->protocol >= PROTOCOL_SMB2_02) {
471 return &conn->smb2.server.gss_blob;
474 return &conn->smb1.server.gss_blob;
477 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
479 if (conn->protocol >= PROTOCOL_SMB2_02) {
480 return &conn->smb2.server.guid;
483 return &conn->smb1.server.guid;
486 struct smbXcli_conn_samba_suicide_state {
487 struct smbXcli_conn *conn;
488 struct iovec iov;
489 uint8_t buf[9];
492 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
494 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
495 struct tevent_context *ev,
496 struct smbXcli_conn *conn,
497 uint8_t exitcode)
499 struct tevent_req *req, *subreq;
500 struct smbXcli_conn_samba_suicide_state *state;
502 req = tevent_req_create(mem_ctx, &state,
503 struct smbXcli_conn_samba_suicide_state);
504 if (req == NULL) {
505 return NULL;
507 state->conn = conn;
508 SIVAL(state->buf, 4, 0x74697865);
509 SCVAL(state->buf, 8, exitcode);
510 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
512 state->iov.iov_base = state->buf;
513 state->iov.iov_len = sizeof(state->buf);
515 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
516 false, &state->iov, 1);
517 if (tevent_req_nomem(subreq, req)) {
518 return tevent_req_post(req, ev);
520 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
521 return req;
524 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
526 struct tevent_req *req = tevent_req_callback_data(
527 subreq, struct tevent_req);
528 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
529 req, struct smbXcli_conn_samba_suicide_state);
530 ssize_t nwritten;
531 int err;
533 nwritten = writev_recv(subreq, &err);
534 TALLOC_FREE(subreq);
535 if (nwritten == -1) {
536 NTSTATUS status = map_nt_error_from_unix_common(err);
537 smbXcli_conn_disconnect(state->conn, status);
538 return;
540 tevent_req_done(req);
543 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
545 return tevent_req_simple_recv_ntstatus(req);
548 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
549 uint8_t exitcode)
551 TALLOC_CTX *frame = talloc_stackframe();
552 struct tevent_context *ev;
553 struct tevent_req *req;
554 NTSTATUS status = NT_STATUS_NO_MEMORY;
555 bool ok;
557 if (smbXcli_conn_has_async_calls(conn)) {
559 * Can't use sync call while an async call is in flight
561 status = NT_STATUS_INVALID_PARAMETER_MIX;
562 goto fail;
564 ev = samba_tevent_context_init(frame);
565 if (ev == NULL) {
566 goto fail;
568 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
569 if (req == NULL) {
570 goto fail;
572 ok = tevent_req_poll(req, ev);
573 if (!ok) {
574 status = map_nt_error_from_unix_common(errno);
575 goto fail;
577 status = smbXcli_conn_samba_suicide_recv(req);
578 fail:
579 TALLOC_FREE(frame);
580 return status;
583 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
585 return conn->smb1.capabilities;
588 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
590 return conn->smb1.max_xmit;
593 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
595 return conn->smb1.server.session_key;
598 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
600 return conn->smb1.server.challenge;
603 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
605 return conn->smb1.server.security_mode;
608 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
610 return conn->smb1.server.readbraw;
613 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
615 return conn->smb1.server.writebraw;
618 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
620 return conn->smb1.server.lockread;
623 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
625 return conn->smb1.server.writeunlock;
628 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
630 return conn->smb1.server.time_zone;
633 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
634 const DATA_BLOB user_session_key,
635 const DATA_BLOB response)
637 return smb_signing_activate(conn->smb1.signing,
638 user_session_key,
639 response);
642 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
643 const uint8_t *buf, uint32_t seqnum)
645 const uint8_t *hdr = buf + NBT_HDR_SIZE;
646 size_t len = smb_len_nbt(buf);
648 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
651 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
653 return smb_signing_is_active(conn->smb1.signing);
656 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
657 struct smb_trans_enc_state *es)
659 /* Replace the old state, if any. */
660 if (conn->smb1.trans_enc) {
661 TALLOC_FREE(conn->smb1.trans_enc);
663 conn->smb1.trans_enc = es;
666 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
668 return common_encryption_on(conn->smb1.trans_enc);
672 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
674 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
675 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
677 if (NT_STATUS_IS_OK(status)) {
678 return NT_STATUS_OK;
681 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
682 return status;
685 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
689 * Is the SMB command able to hold an AND_X successor
690 * @param[in] cmd The SMB command in question
691 * @retval Can we add a chained request after "cmd"?
693 bool smb1cli_is_andx_req(uint8_t cmd)
695 switch (cmd) {
696 case SMBtconX:
697 case SMBlockingX:
698 case SMBopenX:
699 case SMBreadX:
700 case SMBwriteX:
701 case SMBsesssetupX:
702 case SMBulogoffX:
703 case SMBntcreateX:
704 return true;
705 break;
706 default:
707 break;
710 return false;
713 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
715 size_t num_pending = talloc_array_length(conn->pending);
716 uint16_t result;
718 while (true) {
719 size_t i;
721 result = conn->smb1.mid++;
722 if ((result == 0) || (result == 0xffff)) {
723 continue;
726 for (i=0; i<num_pending; i++) {
727 if (result == smb1cli_req_mid(conn->pending[i])) {
728 break;
732 if (i == num_pending) {
733 return result;
738 void smbXcli_req_unset_pending(struct tevent_req *req)
740 struct smbXcli_req_state *state =
741 tevent_req_data(req,
742 struct smbXcli_req_state);
743 struct smbXcli_conn *conn = state->conn;
744 size_t num_pending = talloc_array_length(conn->pending);
745 size_t i;
747 if (state->smb1.mid != 0) {
749 * This is a [nt]trans[2] request which waits
750 * for more than one reply.
752 return;
755 talloc_set_destructor(req, NULL);
757 if (num_pending == 1) {
759 * The pending read_smb tevent_req is a child of
760 * conn->pending. So if nothing is pending anymore, we need to
761 * delete the socket read fde.
763 TALLOC_FREE(conn->pending);
764 conn->read_smb_req = NULL;
765 return;
768 for (i=0; i<num_pending; i++) {
769 if (req == conn->pending[i]) {
770 break;
773 if (i == num_pending) {
775 * Something's seriously broken. Just returning here is the
776 * right thing nevertheless, the point of this routine is to
777 * remove ourselves from conn->pending.
779 return;
783 * Remove ourselves from the conn->pending array
785 for (; i < (num_pending - 1); i++) {
786 conn->pending[i] = conn->pending[i+1];
790 * No NULL check here, we're shrinking by sizeof(void *), and
791 * talloc_realloc just adjusts the size for this.
793 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
794 num_pending - 1);
795 return;
798 static int smbXcli_req_destructor(struct tevent_req *req)
800 struct smbXcli_req_state *state =
801 tevent_req_data(req,
802 struct smbXcli_req_state);
805 * Make sure we really remove it from
806 * the pending array on destruction.
808 state->smb1.mid = 0;
809 smbXcli_req_unset_pending(req);
810 return 0;
813 static bool smb1cli_req_cancel(struct tevent_req *req);
814 static bool smb2cli_req_cancel(struct tevent_req *req);
816 static bool smbXcli_req_cancel(struct tevent_req *req)
818 struct smbXcli_req_state *state =
819 tevent_req_data(req,
820 struct smbXcli_req_state);
822 if (!smbXcli_conn_is_connected(state->conn)) {
823 return false;
826 if (state->conn->protocol == PROTOCOL_NONE) {
827 return false;
830 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
831 return smb2cli_req_cancel(req);
834 return smb1cli_req_cancel(req);
837 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
839 bool smbXcli_req_set_pending(struct tevent_req *req)
841 struct smbXcli_req_state *state =
842 tevent_req_data(req,
843 struct smbXcli_req_state);
844 struct smbXcli_conn *conn;
845 struct tevent_req **pending;
846 size_t num_pending;
848 conn = state->conn;
850 if (!smbXcli_conn_is_connected(conn)) {
851 return false;
854 num_pending = talloc_array_length(conn->pending);
856 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
857 num_pending+1);
858 if (pending == NULL) {
859 return false;
861 pending[num_pending] = req;
862 conn->pending = pending;
863 talloc_set_destructor(req, smbXcli_req_destructor);
864 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
866 if (!smbXcli_conn_receive_next(conn)) {
868 * the caller should notify the current request
870 * And all other pending requests get notified
871 * by smbXcli_conn_disconnect().
873 smbXcli_req_unset_pending(req);
874 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
875 return false;
878 return true;
881 static void smbXcli_conn_received(struct tevent_req *subreq);
883 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
885 size_t num_pending = talloc_array_length(conn->pending);
886 struct tevent_req *req;
887 struct smbXcli_req_state *state;
889 if (conn->read_smb_req != NULL) {
890 return true;
893 if (num_pending == 0) {
894 if (conn->smb2.mid < UINT64_MAX) {
895 /* no more pending requests, so we are done for now */
896 return true;
900 * If there are no more SMB2 requests possible,
901 * because we are out of message ids,
902 * we need to disconnect.
904 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
905 return true;
908 req = conn->pending[0];
909 state = tevent_req_data(req, struct smbXcli_req_state);
912 * We're the first ones, add the read_smb request that waits for the
913 * answer from the server
915 conn->read_smb_req = read_smb_send(conn->pending,
916 state->ev,
917 conn->read_fd);
918 if (conn->read_smb_req == NULL) {
919 return false;
921 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
922 return true;
925 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
927 struct smbXcli_session *session;
929 tevent_queue_stop(conn->outgoing);
931 if (conn->read_fd != -1) {
932 close(conn->read_fd);
934 if (conn->write_fd != -1) {
935 close(conn->write_fd);
937 conn->read_fd = -1;
938 conn->write_fd = -1;
940 session = conn->sessions;
941 if (talloc_array_length(conn->pending) == 0) {
943 * if we do not have pending requests
944 * there is no need to update the channel_sequence
946 session = NULL;
948 for (; session; session = session->next) {
949 smb2cli_session_increment_channel_sequence(session);
953 * Cancel all pending requests. We do not do a for-loop walking
954 * conn->pending because that array changes in
955 * smbXcli_req_unset_pending.
957 while (talloc_array_length(conn->pending) > 0) {
958 struct tevent_req *req;
959 struct smbXcli_req_state *state;
960 struct tevent_req **chain;
961 size_t num_chained;
962 size_t i;
964 req = conn->pending[0];
965 state = tevent_req_data(req, struct smbXcli_req_state);
967 if (state->smb1.chained_requests == NULL) {
969 * We're dead. No point waiting for trans2
970 * replies.
972 state->smb1.mid = 0;
974 smbXcli_req_unset_pending(req);
976 if (NT_STATUS_IS_OK(status)) {
977 /* do not notify the callers */
978 continue;
982 * we need to defer the callback, because we may notify
983 * more then one caller.
985 tevent_req_defer_callback(req, state->ev);
986 tevent_req_nterror(req, status);
987 continue;
990 chain = talloc_move(conn, &state->smb1.chained_requests);
991 num_chained = talloc_array_length(chain);
993 for (i=0; i<num_chained; i++) {
994 req = chain[i];
995 state = tevent_req_data(req, struct smbXcli_req_state);
998 * We're dead. No point waiting for trans2
999 * replies.
1001 state->smb1.mid = 0;
1003 smbXcli_req_unset_pending(req);
1005 if (NT_STATUS_IS_OK(status)) {
1006 /* do not notify the callers */
1007 continue;
1011 * we need to defer the callback, because we may notify
1012 * more than one caller.
1014 tevent_req_defer_callback(req, state->ev);
1015 tevent_req_nterror(req, status);
1017 TALLOC_FREE(chain);
1022 * Fetch a smb request's mid. Only valid after the request has been sent by
1023 * smb1cli_req_send().
1025 uint16_t smb1cli_req_mid(struct tevent_req *req)
1027 struct smbXcli_req_state *state =
1028 tevent_req_data(req,
1029 struct smbXcli_req_state);
1031 if (state->smb1.mid != 0) {
1032 return state->smb1.mid;
1035 return SVAL(state->smb1.hdr, HDR_MID);
1038 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1040 struct smbXcli_req_state *state =
1041 tevent_req_data(req,
1042 struct smbXcli_req_state);
1044 state->smb1.mid = mid;
1047 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1049 struct smbXcli_req_state *state =
1050 tevent_req_data(req,
1051 struct smbXcli_req_state);
1053 return state->smb1.seqnum;
1056 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1058 struct smbXcli_req_state *state =
1059 tevent_req_data(req,
1060 struct smbXcli_req_state);
1062 state->smb1.seqnum = seqnum;
1065 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1067 size_t result = 0;
1068 int i;
1069 for (i=0; i<count; i++) {
1070 result += iov[i].iov_len;
1072 return result;
1075 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1076 const struct iovec *iov,
1077 int count)
1079 size_t len = smbXcli_iov_len(iov, count);
1080 size_t copied;
1081 uint8_t *buf;
1082 int i;
1084 buf = talloc_array(mem_ctx, uint8_t, len);
1085 if (buf == NULL) {
1086 return NULL;
1088 copied = 0;
1089 for (i=0; i<count; i++) {
1090 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1091 copied += iov[i].iov_len;
1093 return buf;
1096 static void smb1cli_req_flags(enum protocol_types protocol,
1097 uint32_t smb1_capabilities,
1098 uint8_t smb_command,
1099 uint8_t additional_flags,
1100 uint8_t clear_flags,
1101 uint8_t *_flags,
1102 uint16_t additional_flags2,
1103 uint16_t clear_flags2,
1104 uint16_t *_flags2)
1106 uint8_t flags = 0;
1107 uint16_t flags2 = 0;
1109 if (protocol >= PROTOCOL_LANMAN1) {
1110 flags |= FLAG_CASELESS_PATHNAMES;
1111 flags |= FLAG_CANONICAL_PATHNAMES;
1114 if (protocol >= PROTOCOL_LANMAN2) {
1115 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1116 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1119 if (protocol >= PROTOCOL_NT1) {
1120 flags2 |= FLAGS2_IS_LONG_NAME;
1122 if (smb1_capabilities & CAP_UNICODE) {
1123 flags2 |= FLAGS2_UNICODE_STRINGS;
1125 if (smb1_capabilities & CAP_STATUS32) {
1126 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1128 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1129 flags2 |= FLAGS2_EXTENDED_SECURITY;
1133 flags |= additional_flags;
1134 flags &= ~clear_flags;
1135 flags2 |= additional_flags2;
1136 flags2 &= ~clear_flags2;
1138 *_flags = flags;
1139 *_flags2 = flags2;
1142 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1144 static bool smb1cli_req_cancel(struct tevent_req *req)
1146 struct smbXcli_req_state *state =
1147 tevent_req_data(req,
1148 struct smbXcli_req_state);
1149 uint8_t flags;
1150 uint16_t flags2;
1151 uint32_t pid;
1152 uint16_t mid;
1153 struct tevent_req *subreq;
1154 NTSTATUS status;
1156 flags = CVAL(state->smb1.hdr, HDR_FLG);
1157 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1158 pid = SVAL(state->smb1.hdr, HDR_PID);
1159 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1160 mid = SVAL(state->smb1.hdr, HDR_MID);
1162 subreq = smb1cli_req_create(state, state->ev,
1163 state->conn,
1164 SMBntcancel,
1165 flags, 0,
1166 flags2, 0,
1167 0, /* timeout */
1168 pid,
1169 state->tcon,
1170 state->session,
1171 0, NULL, /* vwv */
1172 0, NULL); /* bytes */
1173 if (subreq == NULL) {
1174 return false;
1176 smb1cli_req_set_mid(subreq, mid);
1178 status = smb1cli_req_chain_submit(&subreq, 1);
1179 if (!NT_STATUS_IS_OK(status)) {
1180 TALLOC_FREE(subreq);
1181 return false;
1183 smb1cli_req_set_mid(subreq, 0);
1185 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1187 return true;
1190 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1192 /* we do not care about the result */
1193 TALLOC_FREE(subreq);
1196 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1197 struct tevent_context *ev,
1198 struct smbXcli_conn *conn,
1199 uint8_t smb_command,
1200 uint8_t additional_flags,
1201 uint8_t clear_flags,
1202 uint16_t additional_flags2,
1203 uint16_t clear_flags2,
1204 uint32_t timeout_msec,
1205 uint32_t pid,
1206 struct smbXcli_tcon *tcon,
1207 struct smbXcli_session *session,
1208 uint8_t wct, uint16_t *vwv,
1209 int iov_count,
1210 struct iovec *bytes_iov)
1212 struct tevent_req *req;
1213 struct smbXcli_req_state *state;
1214 uint8_t flags = 0;
1215 uint16_t flags2 = 0;
1216 uint16_t uid = 0;
1217 uint16_t tid = 0;
1219 if (iov_count > MAX_SMB_IOV) {
1221 * Should not happen :-)
1223 return NULL;
1226 req = tevent_req_create(mem_ctx, &state,
1227 struct smbXcli_req_state);
1228 if (req == NULL) {
1229 return NULL;
1231 state->ev = ev;
1232 state->conn = conn;
1233 state->session = session;
1234 state->tcon = tcon;
1236 if (session) {
1237 uid = session->smb1.session_id;
1240 if (tcon) {
1241 tid = tcon->smb1.tcon_id;
1244 state->smb1.recv_cmd = 0xFF;
1245 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1246 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1247 if (state->smb1.recv_iov == NULL) {
1248 TALLOC_FREE(req);
1249 return NULL;
1252 smb1cli_req_flags(conn->protocol,
1253 conn->smb1.capabilities,
1254 smb_command,
1255 additional_flags,
1256 clear_flags,
1257 &flags,
1258 additional_flags2,
1259 clear_flags2,
1260 &flags2);
1262 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1263 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1264 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1265 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1266 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1267 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1268 SSVAL(state->smb1.hdr, HDR_TID, tid);
1269 SSVAL(state->smb1.hdr, HDR_PID, pid);
1270 SSVAL(state->smb1.hdr, HDR_UID, uid);
1271 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1272 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1274 state->smb1.vwv = vwv;
1276 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1278 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1279 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1280 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1281 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1282 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1283 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1284 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1285 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1287 if (iov_count != 0) {
1288 memcpy(&state->smb1.iov[4], bytes_iov,
1289 iov_count * sizeof(*bytes_iov));
1291 state->smb1.iov_count = iov_count + 4;
1293 if (timeout_msec > 0) {
1294 struct timeval endtime;
1296 endtime = timeval_current_ofs_msec(timeout_msec);
1297 if (!tevent_req_set_endtime(req, ev, endtime)) {
1298 return req;
1302 switch (smb_command) {
1303 case SMBtranss:
1304 case SMBtranss2:
1305 case SMBnttranss:
1306 state->one_way = true;
1307 break;
1308 case SMBntcancel:
1309 state->one_way = true;
1310 state->smb1.one_way_seqnum = true;
1311 break;
1312 case SMBlockingX:
1313 if ((wct == 8) &&
1314 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1315 state->one_way = true;
1317 break;
1320 return req;
1323 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1324 struct iovec *iov, int iov_count,
1325 uint32_t *seqnum,
1326 bool one_way_seqnum)
1328 TALLOC_CTX *frame = NULL;
1329 uint8_t *buf;
1332 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1333 * iovec directly. MD5Update would do that just fine.
1336 if (iov_count < 4) {
1337 return NT_STATUS_INVALID_PARAMETER_MIX;
1339 if (iov[0].iov_len != NBT_HDR_SIZE) {
1340 return NT_STATUS_INVALID_PARAMETER_MIX;
1342 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1343 return NT_STATUS_INVALID_PARAMETER_MIX;
1345 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1346 return NT_STATUS_INVALID_PARAMETER_MIX;
1348 if (iov[3].iov_len != sizeof(uint16_t)) {
1349 return NT_STATUS_INVALID_PARAMETER_MIX;
1352 frame = talloc_stackframe();
1354 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1355 if (buf == NULL) {
1356 return NT_STATUS_NO_MEMORY;
1359 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1360 one_way_seqnum);
1361 smb_signing_sign_pdu(conn->smb1.signing,
1362 buf, talloc_get_size(buf),
1363 *seqnum);
1364 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1366 TALLOC_FREE(frame);
1367 return NT_STATUS_OK;
1370 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1371 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1372 TALLOC_CTX *tmp_mem,
1373 uint8_t *inbuf);
1375 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1376 struct smbXcli_req_state *state,
1377 struct iovec *iov, int iov_count)
1379 struct tevent_req *subreq;
1380 NTSTATUS status;
1381 uint8_t cmd;
1382 uint16_t mid;
1384 if (!smbXcli_conn_is_connected(state->conn)) {
1385 return NT_STATUS_CONNECTION_DISCONNECTED;
1388 if (state->conn->protocol > PROTOCOL_NT1) {
1389 return NT_STATUS_REVISION_MISMATCH;
1392 if (iov_count < 4) {
1393 return NT_STATUS_INVALID_PARAMETER_MIX;
1395 if (iov[0].iov_len != NBT_HDR_SIZE) {
1396 return NT_STATUS_INVALID_PARAMETER_MIX;
1398 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1399 return NT_STATUS_INVALID_PARAMETER_MIX;
1401 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1402 return NT_STATUS_INVALID_PARAMETER_MIX;
1404 if (iov[3].iov_len != sizeof(uint16_t)) {
1405 return NT_STATUS_INVALID_PARAMETER_MIX;
1408 cmd = CVAL(iov[1].iov_base, HDR_COM);
1409 if (cmd == SMBreadBraw) {
1410 if (smbXcli_conn_has_async_calls(state->conn)) {
1411 return NT_STATUS_INVALID_PARAMETER_MIX;
1413 state->conn->smb1.read_braw_req = req;
1416 if (state->smb1.mid != 0) {
1417 mid = state->smb1.mid;
1418 } else {
1419 mid = smb1cli_alloc_mid(state->conn);
1421 SSVAL(iov[1].iov_base, HDR_MID, mid);
1423 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1425 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1426 &state->smb1.seqnum,
1427 state->smb1.one_way_seqnum);
1429 if (!NT_STATUS_IS_OK(status)) {
1430 return status;
1434 * If we supported multiple encrytion contexts
1435 * here we'd look up based on tid.
1437 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1438 char *buf, *enc_buf;
1440 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1441 if (buf == NULL) {
1442 return NT_STATUS_NO_MEMORY;
1444 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1445 (char *)buf, &enc_buf);
1446 TALLOC_FREE(buf);
1447 if (!NT_STATUS_IS_OK(status)) {
1448 DEBUG(0, ("Error in encrypting client message: %s\n",
1449 nt_errstr(status)));
1450 return status;
1452 buf = (char *)talloc_memdup(state, enc_buf,
1453 smb_len_nbt(enc_buf)+4);
1454 SAFE_FREE(enc_buf);
1455 if (buf == NULL) {
1456 return NT_STATUS_NO_MEMORY;
1458 iov[0].iov_base = (void *)buf;
1459 iov[0].iov_len = talloc_get_size(buf);
1460 iov_count = 1;
1463 if (state->conn->dispatch_incoming == NULL) {
1464 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1467 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1469 subreq = writev_send(state, state->ev, state->conn->outgoing,
1470 state->conn->write_fd, false, iov, iov_count);
1471 if (subreq == NULL) {
1472 return NT_STATUS_NO_MEMORY;
1474 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1475 return NT_STATUS_OK;
1478 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1479 struct tevent_context *ev,
1480 struct smbXcli_conn *conn,
1481 uint8_t smb_command,
1482 uint8_t additional_flags,
1483 uint8_t clear_flags,
1484 uint16_t additional_flags2,
1485 uint16_t clear_flags2,
1486 uint32_t timeout_msec,
1487 uint32_t pid,
1488 struct smbXcli_tcon *tcon,
1489 struct smbXcli_session *session,
1490 uint8_t wct, uint16_t *vwv,
1491 uint32_t num_bytes,
1492 const uint8_t *bytes)
1494 struct tevent_req *req;
1495 struct iovec iov;
1496 NTSTATUS status;
1498 iov.iov_base = discard_const_p(void, bytes);
1499 iov.iov_len = num_bytes;
1501 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1502 additional_flags, clear_flags,
1503 additional_flags2, clear_flags2,
1504 timeout_msec,
1505 pid, tcon, session,
1506 wct, vwv, 1, &iov);
1507 if (req == NULL) {
1508 return NULL;
1510 if (!tevent_req_is_in_progress(req)) {
1511 return tevent_req_post(req, ev);
1513 status = smb1cli_req_chain_submit(&req, 1);
1514 if (tevent_req_nterror(req, status)) {
1515 return tevent_req_post(req, ev);
1517 return req;
1520 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1522 struct tevent_req *req =
1523 tevent_req_callback_data(subreq,
1524 struct tevent_req);
1525 struct smbXcli_req_state *state =
1526 tevent_req_data(req,
1527 struct smbXcli_req_state);
1528 ssize_t nwritten;
1529 int err;
1531 nwritten = writev_recv(subreq, &err);
1532 TALLOC_FREE(subreq);
1533 if (nwritten == -1) {
1534 NTSTATUS status = map_nt_error_from_unix_common(err);
1535 smbXcli_conn_disconnect(state->conn, status);
1536 return;
1539 if (state->one_way) {
1540 state->inbuf = NULL;
1541 tevent_req_done(req);
1542 return;
1545 if (!smbXcli_req_set_pending(req)) {
1546 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1547 return;
1551 static void smbXcli_conn_received(struct tevent_req *subreq)
1553 struct smbXcli_conn *conn =
1554 tevent_req_callback_data(subreq,
1555 struct smbXcli_conn);
1556 TALLOC_CTX *frame = talloc_stackframe();
1557 NTSTATUS status;
1558 uint8_t *inbuf;
1559 ssize_t received;
1560 int err;
1562 if (subreq != conn->read_smb_req) {
1563 DEBUG(1, ("Internal error: cli_smb_received called with "
1564 "unexpected subreq\n"));
1565 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1566 TALLOC_FREE(frame);
1567 return;
1569 conn->read_smb_req = NULL;
1571 received = read_smb_recv(subreq, frame, &inbuf, &err);
1572 TALLOC_FREE(subreq);
1573 if (received == -1) {
1574 status = map_nt_error_from_unix_common(err);
1575 smbXcli_conn_disconnect(conn, status);
1576 TALLOC_FREE(frame);
1577 return;
1580 status = conn->dispatch_incoming(conn, frame, inbuf);
1581 TALLOC_FREE(frame);
1582 if (NT_STATUS_IS_OK(status)) {
1584 * We should not do any more processing
1585 * as the dispatch function called
1586 * tevent_req_done().
1588 return;
1591 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1593 * We got an error, so notify all pending requests
1595 smbXcli_conn_disconnect(conn, status);
1596 return;
1600 * We got NT_STATUS_RETRY, so we may ask for a
1601 * next incoming pdu.
1603 if (!smbXcli_conn_receive_next(conn)) {
1604 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1608 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1609 struct iovec **piov, int *pnum_iov)
1611 struct iovec *iov;
1612 int num_iov;
1613 size_t buflen;
1614 size_t taken;
1615 size_t remaining;
1616 uint8_t *hdr;
1617 uint8_t cmd;
1618 uint32_t wct_ofs;
1619 NTSTATUS status;
1620 size_t min_size = MIN_SMB_SIZE;
1622 buflen = smb_len_tcp(buf);
1623 taken = 0;
1625 hdr = buf + NBT_HDR_SIZE;
1627 status = smb1cli_pull_raw_error(hdr);
1628 if (NT_STATUS_IS_ERR(status)) {
1630 * This is an ugly hack to support OS/2
1631 * which skips the byte_count in the DATA block
1632 * on some error responses.
1634 * See bug #9096
1636 min_size -= sizeof(uint16_t);
1639 if (buflen < min_size) {
1640 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1644 * This returns iovec elements in the following order:
1646 * - SMB header
1648 * - Parameter Block
1649 * - Data Block
1651 * - Parameter Block
1652 * - Data Block
1654 * - Parameter Block
1655 * - Data Block
1657 num_iov = 1;
1659 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1660 if (iov == NULL) {
1661 return NT_STATUS_NO_MEMORY;
1663 iov[0].iov_base = hdr;
1664 iov[0].iov_len = HDR_WCT;
1665 taken += HDR_WCT;
1667 cmd = CVAL(hdr, HDR_COM);
1668 wct_ofs = HDR_WCT;
1670 while (true) {
1671 size_t len = buflen - taken;
1672 struct iovec *cur;
1673 struct iovec *iov_tmp;
1674 uint8_t wct;
1675 uint32_t bcc_ofs;
1676 uint16_t bcc;
1677 size_t needed;
1680 * we need at least WCT
1682 needed = sizeof(uint8_t);
1683 if (len < needed) {
1684 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1685 __location__, (int)len, (int)needed));
1686 goto inval;
1690 * Now we check if the specified words are there
1692 wct = CVAL(hdr, wct_ofs);
1693 needed += wct * sizeof(uint16_t);
1694 if (len < needed) {
1695 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1696 __location__, (int)len, (int)needed));
1697 goto inval;
1700 if ((num_iov == 1) &&
1701 (len == needed) &&
1702 NT_STATUS_IS_ERR(status))
1705 * This is an ugly hack to support OS/2
1706 * which skips the byte_count in the DATA block
1707 * on some error responses.
1709 * See bug #9096
1711 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1712 num_iov + 2);
1713 if (iov_tmp == NULL) {
1714 TALLOC_FREE(iov);
1715 return NT_STATUS_NO_MEMORY;
1717 iov = iov_tmp;
1718 cur = &iov[num_iov];
1719 num_iov += 2;
1721 cur[0].iov_len = 0;
1722 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1723 cur[1].iov_len = 0;
1724 cur[1].iov_base = cur[0].iov_base;
1726 taken += needed;
1727 break;
1731 * we need at least BCC
1733 needed += sizeof(uint16_t);
1734 if (len < needed) {
1735 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1736 __location__, (int)len, (int)needed));
1737 goto inval;
1741 * Now we check if the specified bytes are there
1743 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1744 bcc = SVAL(hdr, bcc_ofs);
1745 needed += bcc * sizeof(uint8_t);
1746 if (len < needed) {
1747 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1748 __location__, (int)len, (int)needed));
1749 goto inval;
1753 * we allocate 2 iovec structures for words and bytes
1755 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1756 num_iov + 2);
1757 if (iov_tmp == NULL) {
1758 TALLOC_FREE(iov);
1759 return NT_STATUS_NO_MEMORY;
1761 iov = iov_tmp;
1762 cur = &iov[num_iov];
1763 num_iov += 2;
1765 cur[0].iov_len = wct * sizeof(uint16_t);
1766 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1767 cur[1].iov_len = bcc * sizeof(uint8_t);
1768 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1770 taken += needed;
1772 if (!smb1cli_is_andx_req(cmd)) {
1774 * If the current command does not have AndX chanining
1775 * we are done.
1777 break;
1780 if (wct == 0 && bcc == 0) {
1782 * An empty response also ends the chain,
1783 * most likely with an error.
1785 break;
1788 if (wct < 2) {
1789 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1790 __location__, (int)wct, (int)cmd));
1791 goto inval;
1793 cmd = CVAL(cur[0].iov_base, 0);
1794 if (cmd == 0xFF) {
1796 * If it is the end of the chain we are also done.
1798 break;
1800 wct_ofs = SVAL(cur[0].iov_base, 2);
1802 if (wct_ofs < taken) {
1803 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1805 if (wct_ofs > buflen) {
1806 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1810 * we consumed everything up to the start of the next
1811 * parameter block.
1813 taken = wct_ofs;
1816 remaining = buflen - taken;
1818 if (remaining > 0 && num_iov >= 3) {
1820 * The last DATA block gets the remaining
1821 * bytes, this is needed to support
1822 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1824 iov[num_iov-1].iov_len += remaining;
1827 *piov = iov;
1828 *pnum_iov = num_iov;
1829 return NT_STATUS_OK;
1831 inval:
1832 TALLOC_FREE(iov);
1833 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1836 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1837 TALLOC_CTX *tmp_mem,
1838 uint8_t *inbuf)
1840 struct tevent_req *req;
1841 struct smbXcli_req_state *state;
1842 NTSTATUS status;
1843 size_t num_pending;
1844 size_t i;
1845 uint8_t cmd;
1846 uint16_t mid;
1847 bool oplock_break;
1848 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1849 size_t len = smb_len_tcp(inbuf);
1850 struct iovec *iov = NULL;
1851 int num_iov = 0;
1852 struct tevent_req **chain = NULL;
1853 size_t num_chained = 0;
1854 size_t num_responses = 0;
1856 if (conn->smb1.read_braw_req != NULL) {
1857 req = conn->smb1.read_braw_req;
1858 conn->smb1.read_braw_req = NULL;
1859 state = tevent_req_data(req, struct smbXcli_req_state);
1861 smbXcli_req_unset_pending(req);
1863 if (state->smb1.recv_iov == NULL) {
1865 * For requests with more than
1866 * one response, we have to readd the
1867 * recv_iov array.
1869 state->smb1.recv_iov = talloc_zero_array(state,
1870 struct iovec,
1872 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1873 return NT_STATUS_OK;
1877 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1878 state->smb1.recv_iov[0].iov_len = len;
1879 ZERO_STRUCT(state->smb1.recv_iov[1]);
1880 ZERO_STRUCT(state->smb1.recv_iov[2]);
1882 state->smb1.recv_cmd = SMBreadBraw;
1883 state->smb1.recv_status = NT_STATUS_OK;
1884 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1886 tevent_req_done(req);
1887 return NT_STATUS_OK;
1890 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1891 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1892 DEBUG(10, ("Got non-SMB PDU\n"));
1893 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1897 * If we supported multiple encrytion contexts
1898 * here we'd look up based on tid.
1900 if (common_encryption_on(conn->smb1.trans_enc)
1901 && (CVAL(inbuf, 0) == 0)) {
1902 uint16_t enc_ctx_num;
1904 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1905 if (!NT_STATUS_IS_OK(status)) {
1906 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1907 nt_errstr(status)));
1908 return status;
1911 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1912 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1913 enc_ctx_num,
1914 conn->smb1.trans_enc->enc_ctx_num));
1915 return NT_STATUS_INVALID_HANDLE;
1918 status = common_decrypt_buffer(conn->smb1.trans_enc,
1919 (char *)inbuf);
1920 if (!NT_STATUS_IS_OK(status)) {
1921 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1922 nt_errstr(status)));
1923 return status;
1925 inhdr = inbuf + NBT_HDR_SIZE;
1926 len = smb_len_nbt(inbuf);
1929 mid = SVAL(inhdr, HDR_MID);
1930 num_pending = talloc_array_length(conn->pending);
1932 for (i=0; i<num_pending; i++) {
1933 if (mid == smb1cli_req_mid(conn->pending[i])) {
1934 break;
1937 if (i == num_pending) {
1938 /* Dump unexpected reply */
1939 return NT_STATUS_RETRY;
1942 oplock_break = false;
1944 if (mid == 0xffff) {
1946 * Paranoia checks that this is really an oplock break request.
1948 oplock_break = (len == 51); /* hdr + 8 words */
1949 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1950 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1951 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1952 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1954 if (!oplock_break) {
1955 /* Dump unexpected reply */
1956 return NT_STATUS_RETRY;
1960 req = conn->pending[i];
1961 state = tevent_req_data(req, struct smbXcli_req_state);
1963 if (!oplock_break /* oplock breaks are not signed */
1964 && !smb_signing_check_pdu(conn->smb1.signing,
1965 inhdr, len, state->smb1.seqnum+1)) {
1966 DEBUG(10, ("cli_check_sign_mac failed\n"));
1967 return NT_STATUS_ACCESS_DENIED;
1970 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1971 &iov, &num_iov);
1972 if (!NT_STATUS_IS_OK(status)) {
1973 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1974 nt_errstr(status)));
1975 return status;
1978 cmd = CVAL(inhdr, HDR_COM);
1979 status = smb1cli_pull_raw_error(inhdr);
1981 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
1982 (state->session != NULL) && state->session->disconnect_expired)
1985 * this should be a short term hack
1986 * until the upper layers have implemented
1987 * re-authentication.
1989 return status;
1992 if (state->smb1.chained_requests == NULL) {
1993 if (num_iov != 3) {
1994 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1997 smbXcli_req_unset_pending(req);
1999 if (state->smb1.recv_iov == NULL) {
2001 * For requests with more than
2002 * one response, we have to readd the
2003 * recv_iov array.
2005 state->smb1.recv_iov = talloc_zero_array(state,
2006 struct iovec,
2008 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2009 return NT_STATUS_OK;
2013 state->smb1.recv_cmd = cmd;
2014 state->smb1.recv_status = status;
2015 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2017 state->smb1.recv_iov[0] = iov[0];
2018 state->smb1.recv_iov[1] = iov[1];
2019 state->smb1.recv_iov[2] = iov[2];
2021 if (talloc_array_length(conn->pending) == 0) {
2022 tevent_req_done(req);
2023 return NT_STATUS_OK;
2026 tevent_req_defer_callback(req, state->ev);
2027 tevent_req_done(req);
2028 return NT_STATUS_RETRY;
2031 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2032 num_chained = talloc_array_length(chain);
2033 num_responses = (num_iov - 1)/2;
2035 if (num_responses > num_chained) {
2036 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2039 for (i=0; i<num_chained; i++) {
2040 size_t iov_idx = 1 + (i*2);
2041 struct iovec *cur = &iov[iov_idx];
2042 uint8_t *inbuf_ref;
2044 req = chain[i];
2045 state = tevent_req_data(req, struct smbXcli_req_state);
2047 smbXcli_req_unset_pending(req);
2050 * as we finish multiple requests here
2051 * we need to defer the callbacks as
2052 * they could destroy our current stack state.
2054 tevent_req_defer_callback(req, state->ev);
2056 if (i >= num_responses) {
2057 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2058 continue;
2061 if (state->smb1.recv_iov == NULL) {
2063 * For requests with more than
2064 * one response, we have to readd the
2065 * recv_iov array.
2067 state->smb1.recv_iov = talloc_zero_array(state,
2068 struct iovec,
2070 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2071 continue;
2075 state->smb1.recv_cmd = cmd;
2077 if (i == (num_responses - 1)) {
2079 * The last request in the chain gets the status
2081 state->smb1.recv_status = status;
2082 } else {
2083 cmd = CVAL(cur[0].iov_base, 0);
2084 state->smb1.recv_status = NT_STATUS_OK;
2087 state->inbuf = inbuf;
2090 * Note: here we use talloc_reference() in a way
2091 * that does not expose it to the caller.
2093 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2094 if (tevent_req_nomem(inbuf_ref, req)) {
2095 continue;
2098 /* copy the related buffers */
2099 state->smb1.recv_iov[0] = iov[0];
2100 state->smb1.recv_iov[1] = cur[0];
2101 state->smb1.recv_iov[2] = cur[1];
2103 tevent_req_done(req);
2106 return NT_STATUS_RETRY;
2109 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2110 TALLOC_CTX *mem_ctx,
2111 struct iovec **piov,
2112 uint8_t **phdr,
2113 uint8_t *pwct,
2114 uint16_t **pvwv,
2115 uint32_t *pvwv_offset,
2116 uint32_t *pnum_bytes,
2117 uint8_t **pbytes,
2118 uint32_t *pbytes_offset,
2119 uint8_t **pinbuf,
2120 const struct smb1cli_req_expected_response *expected,
2121 size_t num_expected)
2123 struct smbXcli_req_state *state =
2124 tevent_req_data(req,
2125 struct smbXcli_req_state);
2126 NTSTATUS status = NT_STATUS_OK;
2127 struct iovec *recv_iov = NULL;
2128 uint8_t *hdr = NULL;
2129 uint8_t wct = 0;
2130 uint32_t vwv_offset = 0;
2131 uint16_t *vwv = NULL;
2132 uint32_t num_bytes = 0;
2133 uint32_t bytes_offset = 0;
2134 uint8_t *bytes = NULL;
2135 size_t i;
2136 bool found_status = false;
2137 bool found_size = false;
2139 if (piov != NULL) {
2140 *piov = NULL;
2142 if (phdr != NULL) {
2143 *phdr = 0;
2145 if (pwct != NULL) {
2146 *pwct = 0;
2148 if (pvwv != NULL) {
2149 *pvwv = NULL;
2151 if (pvwv_offset != NULL) {
2152 *pvwv_offset = 0;
2154 if (pnum_bytes != NULL) {
2155 *pnum_bytes = 0;
2157 if (pbytes != NULL) {
2158 *pbytes = NULL;
2160 if (pbytes_offset != NULL) {
2161 *pbytes_offset = 0;
2163 if (pinbuf != NULL) {
2164 *pinbuf = NULL;
2167 if (state->inbuf != NULL) {
2168 recv_iov = state->smb1.recv_iov;
2169 state->smb1.recv_iov = NULL;
2170 if (state->smb1.recv_cmd != SMBreadBraw) {
2171 hdr = (uint8_t *)recv_iov[0].iov_base;
2172 wct = recv_iov[1].iov_len/2;
2173 vwv = (uint16_t *)recv_iov[1].iov_base;
2174 vwv_offset = PTR_DIFF(vwv, hdr);
2175 num_bytes = recv_iov[2].iov_len;
2176 bytes = (uint8_t *)recv_iov[2].iov_base;
2177 bytes_offset = PTR_DIFF(bytes, hdr);
2181 if (tevent_req_is_nterror(req, &status)) {
2182 for (i=0; i < num_expected; i++) {
2183 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2184 found_status = true;
2185 break;
2189 if (found_status) {
2190 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2193 return status;
2196 if (num_expected == 0) {
2197 found_status = true;
2198 found_size = true;
2201 status = state->smb1.recv_status;
2203 for (i=0; i < num_expected; i++) {
2204 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2205 continue;
2208 found_status = true;
2209 if (expected[i].wct == 0) {
2210 found_size = true;
2211 break;
2214 if (expected[i].wct == wct) {
2215 found_size = true;
2216 break;
2220 if (!found_status) {
2221 return status;
2224 if (!found_size) {
2225 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2228 if (piov != NULL) {
2229 *piov = talloc_move(mem_ctx, &recv_iov);
2232 if (phdr != NULL) {
2233 *phdr = hdr;
2235 if (pwct != NULL) {
2236 *pwct = wct;
2238 if (pvwv != NULL) {
2239 *pvwv = vwv;
2241 if (pvwv_offset != NULL) {
2242 *pvwv_offset = vwv_offset;
2244 if (pnum_bytes != NULL) {
2245 *pnum_bytes = num_bytes;
2247 if (pbytes != NULL) {
2248 *pbytes = bytes;
2250 if (pbytes_offset != NULL) {
2251 *pbytes_offset = bytes_offset;
2253 if (pinbuf != NULL) {
2254 *pinbuf = state->inbuf;
2257 return status;
2260 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2262 size_t wct_ofs;
2263 int i;
2265 wct_ofs = HDR_WCT;
2267 for (i=0; i<num_reqs; i++) {
2268 struct smbXcli_req_state *state;
2269 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2270 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2271 state->smb1.iov_count-2);
2272 wct_ofs = (wct_ofs + 3) & ~3;
2274 return wct_ofs;
2277 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2279 struct smbXcli_req_state *first_state =
2280 tevent_req_data(reqs[0],
2281 struct smbXcli_req_state);
2282 struct smbXcli_req_state *state;
2283 size_t wct_offset;
2284 size_t chain_padding = 0;
2285 int i, iovlen;
2286 struct iovec *iov = NULL;
2287 struct iovec *this_iov;
2288 NTSTATUS status;
2289 size_t nbt_len;
2291 if (num_reqs == 1) {
2292 return smb1cli_req_writev_submit(reqs[0], first_state,
2293 first_state->smb1.iov,
2294 first_state->smb1.iov_count);
2297 iovlen = 0;
2298 for (i=0; i<num_reqs; i++) {
2299 if (!tevent_req_is_in_progress(reqs[i])) {
2300 return NT_STATUS_INTERNAL_ERROR;
2303 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2305 if (state->smb1.iov_count < 4) {
2306 return NT_STATUS_INVALID_PARAMETER_MIX;
2309 if (i == 0) {
2311 * The NBT and SMB header
2313 iovlen += 2;
2314 } else {
2316 * Chain padding
2318 iovlen += 1;
2322 * words and bytes
2324 iovlen += state->smb1.iov_count - 2;
2327 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2328 if (iov == NULL) {
2329 return NT_STATUS_NO_MEMORY;
2332 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2333 first_state, reqs, sizeof(*reqs) * num_reqs);
2334 if (first_state->smb1.chained_requests == NULL) {
2335 TALLOC_FREE(iov);
2336 return NT_STATUS_NO_MEMORY;
2339 wct_offset = HDR_WCT;
2340 this_iov = iov;
2342 for (i=0; i<num_reqs; i++) {
2343 size_t next_padding = 0;
2344 uint16_t *vwv;
2346 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2348 if (i < num_reqs-1) {
2349 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2350 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2351 TALLOC_FREE(iov);
2352 TALLOC_FREE(first_state->smb1.chained_requests);
2353 return NT_STATUS_INVALID_PARAMETER_MIX;
2357 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2358 state->smb1.iov_count-2) + 1;
2359 if ((wct_offset % 4) != 0) {
2360 next_padding = 4 - (wct_offset % 4);
2362 wct_offset += next_padding;
2363 vwv = state->smb1.vwv;
2365 if (i < num_reqs-1) {
2366 struct smbXcli_req_state *next_state =
2367 tevent_req_data(reqs[i+1],
2368 struct smbXcli_req_state);
2369 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2370 SCVAL(vwv+0, 1, 0);
2371 SSVAL(vwv+1, 0, wct_offset);
2372 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2373 /* properly end the chain */
2374 SCVAL(vwv+0, 0, 0xff);
2375 SCVAL(vwv+0, 1, 0xff);
2376 SSVAL(vwv+1, 0, 0);
2379 if (i == 0) {
2381 * The NBT and SMB header
2383 this_iov[0] = state->smb1.iov[0];
2384 this_iov[1] = state->smb1.iov[1];
2385 this_iov += 2;
2386 } else {
2388 * This one is a bit subtle. We have to add
2389 * chain_padding bytes between the requests, and we
2390 * have to also include the wct field of the
2391 * subsequent requests. We use the subsequent header
2392 * for the padding, it contains the wct field in its
2393 * last byte.
2395 this_iov[0].iov_len = chain_padding+1;
2396 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2397 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2398 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2399 this_iov += 1;
2403 * copy the words and bytes
2405 memcpy(this_iov, state->smb1.iov+2,
2406 sizeof(struct iovec) * (state->smb1.iov_count-2));
2407 this_iov += state->smb1.iov_count - 2;
2408 chain_padding = next_padding;
2411 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2412 if (nbt_len > first_state->conn->smb1.max_xmit) {
2413 TALLOC_FREE(iov);
2414 TALLOC_FREE(first_state->smb1.chained_requests);
2415 return NT_STATUS_INVALID_PARAMETER_MIX;
2418 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2419 if (!NT_STATUS_IS_OK(status)) {
2420 TALLOC_FREE(iov);
2421 TALLOC_FREE(first_state->smb1.chained_requests);
2422 return status;
2425 return NT_STATUS_OK;
2428 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2430 return ((tevent_queue_length(conn->outgoing) != 0)
2431 || (talloc_array_length(conn->pending) != 0));
2434 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2436 return conn->smb2.server.capabilities;
2439 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2441 return conn->smb2.server.security_mode;
2444 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2446 return conn->smb2.server.max_trans_size;
2449 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2451 return conn->smb2.server.max_read_size;
2454 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2456 return conn->smb2.server.max_write_size;
2459 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2460 uint16_t max_credits)
2462 conn->smb2.max_credits = max_credits;
2465 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2467 static bool smb2cli_req_cancel(struct tevent_req *req)
2469 struct smbXcli_req_state *state =
2470 tevent_req_data(req,
2471 struct smbXcli_req_state);
2472 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2473 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2474 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2475 struct smbXcli_tcon *tcon = state->tcon;
2476 struct smbXcli_session *session = state->session;
2477 uint8_t *fixed = state->smb2.pad;
2478 uint16_t fixed_len = 4;
2479 struct tevent_req *subreq;
2480 struct smbXcli_req_state *substate;
2481 NTSTATUS status;
2483 SSVAL(fixed, 0, 0x04);
2484 SSVAL(fixed, 2, 0);
2486 subreq = smb2cli_req_create(state, state->ev,
2487 state->conn,
2488 SMB2_OP_CANCEL,
2489 flags, 0,
2490 0, /* timeout */
2491 tcon, session,
2492 fixed, fixed_len,
2493 NULL, 0);
2494 if (subreq == NULL) {
2495 return false;
2497 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2500 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2501 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2503 flags &= SMB2_HDR_FLAG_ASYNC;
2505 if (flags & SMB2_HDR_FLAG_ASYNC) {
2506 mid = 0;
2509 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2510 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2511 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2513 status = smb2cli_req_compound_submit(&subreq, 1);
2514 if (!NT_STATUS_IS_OK(status)) {
2515 TALLOC_FREE(subreq);
2516 return false;
2519 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2521 return true;
2524 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2526 /* we do not care about the result */
2527 TALLOC_FREE(subreq);
2530 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2531 struct tevent_context *ev,
2532 struct smbXcli_conn *conn,
2533 uint16_t cmd,
2534 uint32_t additional_flags,
2535 uint32_t clear_flags,
2536 uint32_t timeout_msec,
2537 struct smbXcli_tcon *tcon,
2538 struct smbXcli_session *session,
2539 const uint8_t *fixed,
2540 uint16_t fixed_len,
2541 const uint8_t *dyn,
2542 uint32_t dyn_len)
2544 struct tevent_req *req;
2545 struct smbXcli_req_state *state;
2546 uint32_t flags = 0;
2547 uint32_t tid = 0;
2548 uint64_t uid = 0;
2549 bool use_channel_sequence = false;
2550 uint16_t channel_sequence = 0;
2552 req = tevent_req_create(mem_ctx, &state,
2553 struct smbXcli_req_state);
2554 if (req == NULL) {
2555 return NULL;
2558 state->ev = ev;
2559 state->conn = conn;
2560 state->session = session;
2561 state->tcon = tcon;
2563 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2564 use_channel_sequence = true;
2565 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2566 use_channel_sequence = true;
2569 if (session) {
2570 uid = session->smb2->session_id;
2572 if (use_channel_sequence) {
2573 channel_sequence = session->smb2->channel_sequence;
2576 state->smb2.should_sign = session->smb2->should_sign;
2577 state->smb2.should_encrypt = session->smb2->should_encrypt;
2579 if (cmd == SMB2_OP_SESSSETUP &&
2580 session->smb2->signing_key.length != 0) {
2581 state->smb2.should_sign = true;
2584 if (cmd == SMB2_OP_SESSSETUP &&
2585 session->smb2_channel.signing_key.length == 0) {
2586 state->smb2.should_encrypt = false;
2590 if (tcon) {
2591 tid = tcon->smb2.tcon_id;
2593 if (tcon->smb2.should_encrypt) {
2594 state->smb2.should_encrypt = true;
2598 if (state->smb2.should_encrypt) {
2599 state->smb2.should_sign = false;
2602 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2603 if (state->smb2.recv_iov == NULL) {
2604 TALLOC_FREE(req);
2605 return NULL;
2608 flags |= additional_flags;
2609 flags &= ~clear_flags;
2611 state->smb2.fixed = fixed;
2612 state->smb2.fixed_len = fixed_len;
2613 state->smb2.dyn = dyn;
2614 state->smb2.dyn_len = dyn_len;
2616 if (state->smb2.should_encrypt) {
2617 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2618 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2621 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2622 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2623 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2624 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2625 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2626 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2627 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2628 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2630 switch (cmd) {
2631 case SMB2_OP_CANCEL:
2632 state->one_way = true;
2633 break;
2634 case SMB2_OP_BREAK:
2636 * If this is a dummy request, it will have
2637 * UINT64_MAX as message id.
2638 * If we send on break acknowledgement,
2639 * this gets overwritten later.
2641 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2642 break;
2645 if (timeout_msec > 0) {
2646 struct timeval endtime;
2648 endtime = timeval_current_ofs_msec(timeout_msec);
2649 if (!tevent_req_set_endtime(req, ev, endtime)) {
2650 return req;
2654 return req;
2657 void smb2cli_req_set_notify_async(struct tevent_req *req)
2659 struct smbXcli_req_state *state =
2660 tevent_req_data(req,
2661 struct smbXcli_req_state);
2663 state->smb2.notify_async = true;
2666 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2667 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2668 TALLOC_CTX *tmp_mem,
2669 uint8_t *inbuf);
2671 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2672 int num_reqs)
2674 struct smbXcli_req_state *state;
2675 struct tevent_req *subreq;
2676 struct iovec *iov;
2677 int i, num_iov, nbt_len;
2678 int tf_iov = -1;
2679 const DATA_BLOB *encryption_key = NULL;
2680 uint64_t encryption_session_id = 0;
2683 * 1 for the nbt length, optional TRANSFORM
2684 * per request: HDR, fixed, dyn, padding
2685 * -1 because the last one does not need padding
2688 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2689 if (iov == NULL) {
2690 return NT_STATUS_NO_MEMORY;
2693 num_iov = 1;
2694 nbt_len = 0;
2697 * the session of the first request that requires encryption
2698 * specifies the encryption key.
2700 for (i=0; i<num_reqs; i++) {
2701 if (!tevent_req_is_in_progress(reqs[i])) {
2702 return NT_STATUS_INTERNAL_ERROR;
2705 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2707 if (!smbXcli_conn_is_connected(state->conn)) {
2708 return NT_STATUS_CONNECTION_DISCONNECTED;
2711 if ((state->conn->protocol != PROTOCOL_NONE) &&
2712 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2713 return NT_STATUS_REVISION_MISMATCH;
2716 if (state->session == NULL) {
2717 continue;
2720 if (!state->smb2.should_encrypt) {
2721 continue;
2724 encryption_key = &state->session->smb2->encryption_key;
2725 if (encryption_key->length == 0) {
2726 return NT_STATUS_INVALID_PARAMETER_MIX;
2729 encryption_session_id = state->session->smb2->session_id;
2731 tf_iov = num_iov;
2732 iov[num_iov].iov_base = state->smb2.transform;
2733 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2734 num_iov += 1;
2736 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2737 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2738 state->session->smb2->nonce_low);
2739 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2740 state->session->smb2->nonce_high);
2741 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2742 encryption_session_id);
2744 state->session->smb2->nonce_low += 1;
2745 if (state->session->smb2->nonce_low == 0) {
2746 state->session->smb2->nonce_high += 1;
2747 state->session->smb2->nonce_low += 1;
2750 nbt_len += SMB2_TF_HDR_SIZE;
2751 break;
2754 for (i=0; i<num_reqs; i++) {
2755 int hdr_iov;
2756 size_t reqlen;
2757 bool ret;
2758 uint16_t opcode;
2759 uint64_t avail;
2760 uint16_t charge;
2761 uint16_t credits;
2762 uint64_t mid;
2763 const DATA_BLOB *signing_key = NULL;
2765 if (!tevent_req_is_in_progress(reqs[i])) {
2766 return NT_STATUS_INTERNAL_ERROR;
2769 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2771 if (!smbXcli_conn_is_connected(state->conn)) {
2772 return NT_STATUS_CONNECTION_DISCONNECTED;
2775 if ((state->conn->protocol != PROTOCOL_NONE) &&
2776 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2777 return NT_STATUS_REVISION_MISMATCH;
2780 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2781 if (opcode == SMB2_OP_CANCEL) {
2782 goto skip_credits;
2785 avail = UINT64_MAX - state->conn->smb2.mid;
2786 if (avail < 1) {
2787 return NT_STATUS_CONNECTION_ABORTED;
2790 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2791 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2792 } else {
2793 charge = 1;
2796 charge = MAX(state->smb2.credit_charge, charge);
2798 avail = MIN(avail, state->conn->smb2.cur_credits);
2799 if (avail < charge) {
2800 return NT_STATUS_INTERNAL_ERROR;
2803 credits = 0;
2804 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2805 credits = state->conn->smb2.max_credits -
2806 state->conn->smb2.cur_credits;
2808 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2809 credits += 1;
2812 mid = state->conn->smb2.mid;
2813 state->conn->smb2.mid += charge;
2814 state->conn->smb2.cur_credits -= charge;
2816 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2817 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2819 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2820 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2822 skip_credits:
2823 if (state->session && encryption_key == NULL) {
2825 * We prefer the channel signing key if it is
2826 * already there.
2828 if (state->smb2.should_sign) {
2829 signing_key = &state->session->smb2_channel.signing_key;
2833 * If it is a channel binding, we already have the main
2834 * signing key and try that one.
2836 if (signing_key && signing_key->length == 0) {
2837 signing_key = &state->session->smb2->signing_key;
2841 * If we do not have any session key yet, we skip the
2842 * signing of SMB2_OP_SESSSETUP requests.
2844 if (signing_key && signing_key->length == 0) {
2845 signing_key = NULL;
2849 hdr_iov = num_iov;
2850 iov[num_iov].iov_base = state->smb2.hdr;
2851 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2852 num_iov += 1;
2854 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2855 iov[num_iov].iov_len = state->smb2.fixed_len;
2856 num_iov += 1;
2858 if (state->smb2.dyn != NULL) {
2859 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2860 iov[num_iov].iov_len = state->smb2.dyn_len;
2861 num_iov += 1;
2864 reqlen = sizeof(state->smb2.hdr);
2865 reqlen += state->smb2.fixed_len;
2866 reqlen += state->smb2.dyn_len;
2868 if (i < num_reqs-1) {
2869 if ((reqlen % 8) > 0) {
2870 uint8_t pad = 8 - (reqlen % 8);
2871 iov[num_iov].iov_base = state->smb2.pad;
2872 iov[num_iov].iov_len = pad;
2873 num_iov += 1;
2874 reqlen += pad;
2876 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2879 state->smb2.encryption_session_id = encryption_session_id;
2881 if (signing_key != NULL) {
2882 NTSTATUS status;
2884 status = smb2_signing_sign_pdu(*signing_key,
2885 state->session->conn->protocol,
2886 &iov[hdr_iov], num_iov - hdr_iov);
2887 if (!NT_STATUS_IS_OK(status)) {
2888 return status;
2892 nbt_len += reqlen;
2894 ret = smbXcli_req_set_pending(reqs[i]);
2895 if (!ret) {
2896 return NT_STATUS_NO_MEMORY;
2900 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2901 _smb_setlen_tcp(state->length_hdr, nbt_len);
2902 iov[0].iov_base = state->length_hdr;
2903 iov[0].iov_len = sizeof(state->length_hdr);
2905 if (encryption_key != NULL) {
2906 NTSTATUS status;
2907 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
2908 uint8_t *buf;
2909 int vi;
2911 buf = talloc_array(iov, uint8_t, buflen);
2912 if (buf == NULL) {
2913 return NT_STATUS_NO_MEMORY;
2917 * We copy the buffers before encrypting them,
2918 * this is at least currently needed for the
2919 * to keep state->smb2.hdr.
2921 * Also the callers may expect there buffers
2922 * to be const.
2924 for (vi = tf_iov + 1; vi < num_iov; vi++) {
2925 struct iovec *v = &iov[vi];
2926 const uint8_t *o = (const uint8_t *)v->iov_base;
2928 memcpy(buf, o, v->iov_len);
2929 v->iov_base = (void *)buf;
2930 buf += v->iov_len;
2933 status = smb2_signing_encrypt_pdu(*encryption_key,
2934 state->conn->protocol,
2935 &iov[tf_iov], num_iov - tf_iov);
2936 if (!NT_STATUS_IS_OK(status)) {
2937 return status;
2941 if (state->conn->dispatch_incoming == NULL) {
2942 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2945 subreq = writev_send(state, state->ev, state->conn->outgoing,
2946 state->conn->write_fd, false, iov, num_iov);
2947 if (subreq == NULL) {
2948 return NT_STATUS_NO_MEMORY;
2950 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2951 return NT_STATUS_OK;
2954 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2956 struct smbXcli_req_state *state =
2957 tevent_req_data(req,
2958 struct smbXcli_req_state);
2960 state->smb2.credit_charge = charge;
2963 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2964 struct tevent_context *ev,
2965 struct smbXcli_conn *conn,
2966 uint16_t cmd,
2967 uint32_t additional_flags,
2968 uint32_t clear_flags,
2969 uint32_t timeout_msec,
2970 struct smbXcli_tcon *tcon,
2971 struct smbXcli_session *session,
2972 const uint8_t *fixed,
2973 uint16_t fixed_len,
2974 const uint8_t *dyn,
2975 uint32_t dyn_len)
2977 struct tevent_req *req;
2978 NTSTATUS status;
2980 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2981 additional_flags, clear_flags,
2982 timeout_msec,
2983 tcon, session,
2984 fixed, fixed_len, dyn, dyn_len);
2985 if (req == NULL) {
2986 return NULL;
2988 if (!tevent_req_is_in_progress(req)) {
2989 return tevent_req_post(req, ev);
2991 status = smb2cli_req_compound_submit(&req, 1);
2992 if (tevent_req_nterror(req, status)) {
2993 return tevent_req_post(req, ev);
2995 return req;
2998 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3000 struct tevent_req *req =
3001 tevent_req_callback_data(subreq,
3002 struct tevent_req);
3003 struct smbXcli_req_state *state =
3004 tevent_req_data(req,
3005 struct smbXcli_req_state);
3006 ssize_t nwritten;
3007 int err;
3009 nwritten = writev_recv(subreq, &err);
3010 TALLOC_FREE(subreq);
3011 if (nwritten == -1) {
3012 /* here, we need to notify all pending requests */
3013 NTSTATUS status = map_nt_error_from_unix_common(err);
3014 smbXcli_conn_disconnect(state->conn, status);
3015 return;
3019 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3020 uint8_t *buf,
3021 size_t buflen,
3022 TALLOC_CTX *mem_ctx,
3023 struct iovec **piov, int *pnum_iov)
3025 struct iovec *iov;
3026 int num_iov = 0;
3027 size_t taken = 0;
3028 uint8_t *first_hdr = buf;
3029 size_t verified_buflen = 0;
3030 uint8_t *tf = NULL;
3031 size_t tf_len = 0;
3033 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3034 if (iov == NULL) {
3035 return NT_STATUS_NO_MEMORY;
3038 while (taken < buflen) {
3039 size_t len = buflen - taken;
3040 uint8_t *hdr = first_hdr + taken;
3041 struct iovec *cur;
3042 size_t full_size;
3043 size_t next_command_ofs;
3044 uint16_t body_size;
3045 struct iovec *iov_tmp;
3047 if (verified_buflen > taken) {
3048 len = verified_buflen - taken;
3049 } else {
3050 tf = NULL;
3051 tf_len = 0;
3054 if (len < 4) {
3055 DEBUG(10, ("%d bytes left, expected at least %d\n",
3056 (int)len, 4));
3057 goto inval;
3059 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3060 struct smbXcli_session *s;
3061 uint64_t uid;
3062 struct iovec tf_iov[2];
3063 size_t enc_len;
3064 NTSTATUS status;
3066 if (len < SMB2_TF_HDR_SIZE) {
3067 DEBUG(10, ("%d bytes left, expected at least %d\n",
3068 (int)len, SMB2_TF_HDR_SIZE));
3069 goto inval;
3071 tf = hdr;
3072 tf_len = SMB2_TF_HDR_SIZE;
3073 taken += tf_len;
3075 hdr = first_hdr + taken;
3076 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3077 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3079 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3080 DEBUG(10, ("%d bytes left, expected at least %d\n",
3081 (int)len,
3082 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3083 goto inval;
3086 s = conn->sessions;
3087 for (; s; s = s->next) {
3088 if (s->smb2->session_id != uid) {
3089 continue;
3091 break;
3094 if (s == NULL) {
3095 DEBUG(10, ("unknown session_id %llu\n",
3096 (unsigned long long)uid));
3097 goto inval;
3100 tf_iov[0].iov_base = (void *)tf;
3101 tf_iov[0].iov_len = tf_len;
3102 tf_iov[1].iov_base = (void *)hdr;
3103 tf_iov[1].iov_len = enc_len;
3105 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3106 conn->protocol,
3107 tf_iov, 2);
3108 if (!NT_STATUS_IS_OK(status)) {
3109 TALLOC_FREE(iov);
3110 return status;
3113 verified_buflen = taken + enc_len;
3114 len = enc_len;
3118 * We need the header plus the body length field
3121 if (len < SMB2_HDR_BODY + 2) {
3122 DEBUG(10, ("%d bytes left, expected at least %d\n",
3123 (int)len, SMB2_HDR_BODY));
3124 goto inval;
3126 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3127 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3128 IVAL(hdr, 0)));
3129 goto inval;
3131 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3132 DEBUG(10, ("Got HDR len %d, expected %d\n",
3133 SVAL(hdr, 4), SMB2_HDR_BODY));
3134 goto inval;
3137 full_size = len;
3138 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3139 body_size = SVAL(hdr, SMB2_HDR_BODY);
3141 if (next_command_ofs != 0) {
3142 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3143 goto inval;
3145 if (next_command_ofs > full_size) {
3146 goto inval;
3148 full_size = next_command_ofs;
3150 if (body_size < 2) {
3151 goto inval;
3153 body_size &= 0xfffe;
3155 if (body_size > (full_size - SMB2_HDR_BODY)) {
3156 goto inval;
3159 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3160 num_iov + 4);
3161 if (iov_tmp == NULL) {
3162 TALLOC_FREE(iov);
3163 return NT_STATUS_NO_MEMORY;
3165 iov = iov_tmp;
3166 cur = &iov[num_iov];
3167 num_iov += 4;
3169 cur[0].iov_base = tf;
3170 cur[0].iov_len = tf_len;
3171 cur[1].iov_base = hdr;
3172 cur[1].iov_len = SMB2_HDR_BODY;
3173 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3174 cur[2].iov_len = body_size;
3175 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3176 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3178 taken += full_size;
3181 *piov = iov;
3182 *pnum_iov = num_iov;
3183 return NT_STATUS_OK;
3185 inval:
3186 TALLOC_FREE(iov);
3187 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3190 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3191 uint64_t mid)
3193 size_t num_pending = talloc_array_length(conn->pending);
3194 size_t i;
3196 for (i=0; i<num_pending; i++) {
3197 struct tevent_req *req = conn->pending[i];
3198 struct smbXcli_req_state *state =
3199 tevent_req_data(req,
3200 struct smbXcli_req_state);
3202 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3203 return req;
3206 return NULL;
3209 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3210 TALLOC_CTX *tmp_mem,
3211 uint8_t *inbuf)
3213 struct tevent_req *req;
3214 struct smbXcli_req_state *state = NULL;
3215 struct iovec *iov;
3216 int i, num_iov;
3217 NTSTATUS status;
3218 bool defer = true;
3219 struct smbXcli_session *last_session = NULL;
3220 size_t inbuf_len = smb_len_tcp(inbuf);
3222 status = smb2cli_inbuf_parse_compound(conn,
3223 inbuf + NBT_HDR_SIZE,
3224 inbuf_len,
3225 tmp_mem,
3226 &iov, &num_iov);
3227 if (!NT_STATUS_IS_OK(status)) {
3228 return status;
3231 for (i=0; i<num_iov; i+=4) {
3232 uint8_t *inbuf_ref = NULL;
3233 struct iovec *cur = &iov[i];
3234 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3235 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3236 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3237 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3238 uint16_t req_opcode;
3239 uint32_t req_flags;
3240 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3241 uint32_t new_credits;
3242 struct smbXcli_session *session = NULL;
3243 const DATA_BLOB *signing_key = NULL;
3244 bool was_encrypted = false;
3246 new_credits = conn->smb2.cur_credits;
3247 new_credits += credits;
3248 if (new_credits > UINT16_MAX) {
3249 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3251 conn->smb2.cur_credits += credits;
3253 req = smb2cli_conn_find_pending(conn, mid);
3254 if (req == NULL) {
3255 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3257 state = tevent_req_data(req, struct smbXcli_req_state);
3259 state->smb2.got_async = false;
3261 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3262 if (opcode != req_opcode) {
3263 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3265 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3267 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3268 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3271 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3272 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3273 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3274 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3277 * async interim responses are not signed,
3278 * even if the SMB2_HDR_FLAG_SIGNED flag
3279 * is set.
3281 req_flags |= SMB2_HDR_FLAG_ASYNC;
3282 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3283 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3285 if (state->smb2.notify_async) {
3286 state->smb2.got_async = true;
3287 tevent_req_defer_callback(req, state->ev);
3288 tevent_req_notify_callback(req);
3290 continue;
3293 session = state->session;
3294 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3295 session = last_session;
3297 last_session = session;
3299 if (state->smb2.should_sign) {
3300 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3301 return NT_STATUS_ACCESS_DENIED;
3305 if (flags & SMB2_HDR_FLAG_SIGNED) {
3306 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3308 if (session == NULL) {
3309 struct smbXcli_session *s;
3311 s = state->conn->sessions;
3312 for (; s; s = s->next) {
3313 if (s->smb2->session_id != uid) {
3314 continue;
3317 session = s;
3318 break;
3322 if (session == NULL) {
3323 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3326 last_session = session;
3327 signing_key = &session->smb2_channel.signing_key;
3330 if (opcode == SMB2_OP_SESSSETUP) {
3332 * We prefer the channel signing key, if it is
3333 * already there.
3335 * If we do not have a channel signing key yet,
3336 * we try the main signing key, if it is not
3337 * the final response.
3339 if (signing_key && signing_key->length == 0 &&
3340 !NT_STATUS_IS_OK(status)) {
3341 signing_key = &session->smb2->signing_key;
3344 if (signing_key && signing_key->length == 0) {
3346 * If we do not have a session key to
3347 * verify the signature, we defer the
3348 * signing check to the caller.
3350 * The caller gets NT_STATUS_OK, it
3351 * has to call
3352 * smb2cli_session_set_session_key()
3353 * or
3354 * smb2cli_session_set_channel_key()
3355 * which will check the signature
3356 * with the channel signing key.
3358 signing_key = NULL;
3362 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3363 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3364 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3367 * If the response was encrypted in a SMB2_TRANSFORM
3368 * pdu, which belongs to the correct session,
3369 * we do not need to do signing checks
3371 * It could be the session the response belongs to
3372 * or the session that was used to encrypt the
3373 * SMB2_TRANSFORM request.
3375 if ((session && session->smb2->session_id == uid) ||
3376 (state->smb2.encryption_session_id == uid)) {
3377 signing_key = NULL;
3378 was_encrypted = true;
3382 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3384 * if the server returns NT_STATUS_USER_SESSION_DELETED
3385 * the response is not signed and we should
3386 * propagate the NT_STATUS_USER_SESSION_DELETED
3387 * status to the caller.
3389 state->smb2.signing_skipped = true;
3390 signing_key = NULL;
3393 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3395 * if the server returns
3396 * NT_STATUS_INVALID_PARAMETER
3397 * the response might not be encrypted.
3399 if (state->smb2.should_encrypt && !was_encrypted) {
3400 state->smb2.signing_skipped = true;
3401 signing_key = NULL;
3405 if (state->smb2.should_encrypt && !was_encrypted) {
3406 if (!state->smb2.signing_skipped) {
3407 return NT_STATUS_ACCESS_DENIED;
3411 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3412 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3413 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3415 * if the server returns
3416 * NT_STATUS_NETWORK_NAME_DELETED
3417 * NT_STATUS_FILE_CLOSED
3418 * NT_STATUS_INVALID_PARAMETER
3419 * the response might not be signed
3420 * as this happens before the signing checks.
3422 * If server echos the signature (or all zeros)
3423 * we should report the status from the server
3424 * to the caller.
3426 if (signing_key) {
3427 int cmp;
3429 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3430 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3431 16);
3432 if (cmp == 0) {
3433 state->smb2.signing_skipped = true;
3434 signing_key = NULL;
3437 if (signing_key) {
3438 int cmp;
3439 static const uint8_t zeros[16];
3441 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3442 zeros,
3443 16);
3444 if (cmp == 0) {
3445 state->smb2.signing_skipped = true;
3446 signing_key = NULL;
3451 if (signing_key) {
3452 status = smb2_signing_check_pdu(*signing_key,
3453 state->conn->protocol,
3454 &cur[1], 3);
3455 if (!NT_STATUS_IS_OK(status)) {
3457 * If the signing check fails, we disconnect
3458 * the connection.
3460 return status;
3464 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3465 (session != NULL) && session->disconnect_expired)
3468 * this should be a short term hack
3469 * until the upper layers have implemented
3470 * re-authentication.
3472 return status;
3475 smbXcli_req_unset_pending(req);
3478 * There might be more than one response
3479 * we need to defer the notifications
3481 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3482 defer = false;
3485 if (defer) {
3486 tevent_req_defer_callback(req, state->ev);
3490 * Note: here we use talloc_reference() in a way
3491 * that does not expose it to the caller.
3493 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3494 if (tevent_req_nomem(inbuf_ref, req)) {
3495 continue;
3498 /* copy the related buffers */
3499 state->smb2.recv_iov[0] = cur[1];
3500 state->smb2.recv_iov[1] = cur[2];
3501 state->smb2.recv_iov[2] = cur[3];
3503 tevent_req_done(req);
3506 if (defer) {
3507 return NT_STATUS_RETRY;
3510 return NT_STATUS_OK;
3513 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3514 struct iovec **piov,
3515 const struct smb2cli_req_expected_response *expected,
3516 size_t num_expected)
3518 struct smbXcli_req_state *state =
3519 tevent_req_data(req,
3520 struct smbXcli_req_state);
3521 NTSTATUS status;
3522 size_t body_size;
3523 bool found_status = false;
3524 bool found_size = false;
3525 size_t i;
3527 if (piov != NULL) {
3528 *piov = NULL;
3531 if (state->smb2.got_async) {
3532 return STATUS_PENDING;
3535 if (tevent_req_is_nterror(req, &status)) {
3536 for (i=0; i < num_expected; i++) {
3537 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3538 found_status = true;
3539 break;
3543 if (found_status) {
3544 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3547 return status;
3550 if (num_expected == 0) {
3551 found_status = true;
3552 found_size = true;
3555 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3556 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3558 for (i=0; i < num_expected; i++) {
3559 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3560 continue;
3563 found_status = true;
3564 if (expected[i].body_size == 0) {
3565 found_size = true;
3566 break;
3569 if (expected[i].body_size == body_size) {
3570 found_size = true;
3571 break;
3575 if (!found_status) {
3576 return status;
3579 if (state->smb2.signing_skipped) {
3580 if (num_expected > 0) {
3581 return NT_STATUS_ACCESS_DENIED;
3583 if (!NT_STATUS_IS_ERR(status)) {
3584 return NT_STATUS_ACCESS_DENIED;
3588 if (!found_size) {
3589 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3592 if (piov != NULL) {
3593 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3596 return status;
3599 static const struct {
3600 enum protocol_types proto;
3601 const char *smb1_name;
3602 } smb1cli_prots[] = {
3603 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3604 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3605 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3606 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3607 {PROTOCOL_LANMAN2, "LM1.2X002"},
3608 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3609 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3610 {PROTOCOL_LANMAN2, "Samba"},
3611 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3612 {PROTOCOL_NT1, "NT LM 0.12"},
3613 {PROTOCOL_SMB2_02, "SMB 2.002"},
3614 {PROTOCOL_SMB2_10, "SMB 2.???"},
3617 static const struct {
3618 enum protocol_types proto;
3619 uint16_t smb2_dialect;
3620 } smb2cli_prots[] = {
3621 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3622 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3623 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3624 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3625 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3628 struct smbXcli_negprot_state {
3629 struct smbXcli_conn *conn;
3630 struct tevent_context *ev;
3631 uint32_t timeout_msec;
3632 enum protocol_types min_protocol;
3633 enum protocol_types max_protocol;
3635 struct {
3636 uint8_t fixed[36];
3637 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3638 } smb2;
3641 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3642 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3643 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3644 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3645 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3646 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3647 TALLOC_CTX *frame,
3648 uint8_t *inbuf);
3650 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3651 struct tevent_context *ev,
3652 struct smbXcli_conn *conn,
3653 uint32_t timeout_msec,
3654 enum protocol_types min_protocol,
3655 enum protocol_types max_protocol)
3657 struct tevent_req *req, *subreq;
3658 struct smbXcli_negprot_state *state;
3660 req = tevent_req_create(mem_ctx, &state,
3661 struct smbXcli_negprot_state);
3662 if (req == NULL) {
3663 return NULL;
3665 state->conn = conn;
3666 state->ev = ev;
3667 state->timeout_msec = timeout_msec;
3668 state->min_protocol = min_protocol;
3669 state->max_protocol = max_protocol;
3671 if (min_protocol == PROTOCOL_NONE) {
3672 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3673 return tevent_req_post(req, ev);
3676 if (max_protocol == PROTOCOL_NONE) {
3677 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3678 return tevent_req_post(req, ev);
3681 if (min_protocol > max_protocol) {
3682 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3683 return tevent_req_post(req, ev);
3686 if ((min_protocol < PROTOCOL_SMB2_02) &&
3687 (max_protocol < PROTOCOL_SMB2_02)) {
3689 * SMB1 only...
3691 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3693 subreq = smbXcli_negprot_smb1_subreq(state);
3694 if (tevent_req_nomem(subreq, req)) {
3695 return tevent_req_post(req, ev);
3697 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3698 return req;
3701 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3702 (max_protocol >= PROTOCOL_SMB2_02)) {
3704 * SMB2 only...
3706 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3708 subreq = smbXcli_negprot_smb2_subreq(state);
3709 if (tevent_req_nomem(subreq, req)) {
3710 return tevent_req_post(req, ev);
3712 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3713 return req;
3717 * We send an SMB1 negprot with the SMB2 dialects
3718 * and expect a SMB1 or a SMB2 response.
3720 * smbXcli_negprot_dispatch_incoming() will fix the
3721 * callback to match protocol of the response.
3723 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3725 subreq = smbXcli_negprot_smb1_subreq(state);
3726 if (tevent_req_nomem(subreq, req)) {
3727 return tevent_req_post(req, ev);
3729 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3730 return req;
3733 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3735 struct tevent_req *req =
3736 tevent_req_callback_data(subreq,
3737 struct tevent_req);
3738 NTSTATUS status;
3741 * we just want the low level error
3743 status = tevent_req_simple_recv_ntstatus(subreq);
3744 TALLOC_FREE(subreq);
3745 if (tevent_req_nterror(req, status)) {
3746 return;
3749 /* this should never happen */
3750 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3753 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3755 size_t i;
3756 DATA_BLOB bytes = data_blob_null;
3757 uint8_t flags;
3758 uint16_t flags2;
3760 /* setup the protocol strings */
3761 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3762 uint8_t c = 2;
3763 bool ok;
3765 if (smb1cli_prots[i].proto < state->min_protocol) {
3766 continue;
3769 if (smb1cli_prots[i].proto > state->max_protocol) {
3770 continue;
3773 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3774 if (!ok) {
3775 return NULL;
3779 * We now it is already ascii and
3780 * we want NULL termination.
3782 ok = data_blob_append(state, &bytes,
3783 smb1cli_prots[i].smb1_name,
3784 strlen(smb1cli_prots[i].smb1_name)+1);
3785 if (!ok) {
3786 return NULL;
3790 smb1cli_req_flags(state->max_protocol,
3791 state->conn->smb1.client.capabilities,
3792 SMBnegprot,
3793 0, 0, &flags,
3794 0, 0, &flags2);
3796 return smb1cli_req_send(state, state->ev, state->conn,
3797 SMBnegprot,
3798 flags, ~flags,
3799 flags2, ~flags2,
3800 state->timeout_msec,
3801 0xFFFE, 0, NULL, /* pid, tid, session */
3802 0, NULL, /* wct, vwv */
3803 bytes.length, bytes.data);
3806 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3808 struct tevent_req *req =
3809 tevent_req_callback_data(subreq,
3810 struct tevent_req);
3811 struct smbXcli_negprot_state *state =
3812 tevent_req_data(req,
3813 struct smbXcli_negprot_state);
3814 struct smbXcli_conn *conn = state->conn;
3815 struct iovec *recv_iov = NULL;
3816 uint8_t *inhdr;
3817 uint8_t wct;
3818 uint16_t *vwv;
3819 uint32_t num_bytes;
3820 uint8_t *bytes;
3821 NTSTATUS status;
3822 uint16_t protnum;
3823 size_t i;
3824 size_t num_prots = 0;
3825 uint8_t flags;
3826 uint32_t client_capabilities = conn->smb1.client.capabilities;
3827 uint32_t both_capabilities;
3828 uint32_t server_capabilities = 0;
3829 uint32_t capabilities;
3830 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3831 uint32_t server_max_xmit = 0;
3832 uint32_t max_xmit;
3833 uint32_t server_max_mux = 0;
3834 uint16_t server_security_mode = 0;
3835 uint32_t server_session_key = 0;
3836 bool server_readbraw = false;
3837 bool server_writebraw = false;
3838 bool server_lockread = false;
3839 bool server_writeunlock = false;
3840 struct GUID server_guid = GUID_zero();
3841 DATA_BLOB server_gss_blob = data_blob_null;
3842 uint8_t server_challenge[8];
3843 char *server_workgroup = NULL;
3844 char *server_name = NULL;
3845 int server_time_zone = 0;
3846 NTTIME server_system_time = 0;
3847 static const struct smb1cli_req_expected_response expected[] = {
3849 .status = NT_STATUS_OK,
3850 .wct = 0x11, /* NT1 */
3853 .status = NT_STATUS_OK,
3854 .wct = 0x0D, /* LM */
3857 .status = NT_STATUS_OK,
3858 .wct = 0x01, /* CORE */
3862 ZERO_STRUCT(server_challenge);
3864 status = smb1cli_req_recv(subreq, state,
3865 &recv_iov,
3866 &inhdr,
3867 &wct,
3868 &vwv,
3869 NULL, /* pvwv_offset */
3870 &num_bytes,
3871 &bytes,
3872 NULL, /* pbytes_offset */
3873 NULL, /* pinbuf */
3874 expected, ARRAY_SIZE(expected));
3875 TALLOC_FREE(subreq);
3876 if (tevent_req_nterror(req, status)) {
3877 return;
3880 flags = CVAL(inhdr, HDR_FLG);
3882 protnum = SVAL(vwv, 0);
3884 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3885 if (smb1cli_prots[i].proto < state->min_protocol) {
3886 continue;
3889 if (smb1cli_prots[i].proto > state->max_protocol) {
3890 continue;
3893 if (protnum != num_prots) {
3894 num_prots++;
3895 continue;
3898 conn->protocol = smb1cli_prots[i].proto;
3899 break;
3902 if (conn->protocol == PROTOCOL_NONE) {
3903 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3904 return;
3907 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3908 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3909 "and the selected protocol level doesn't support it.\n"));
3910 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3911 return;
3914 if (flags & FLAG_SUPPORT_LOCKREAD) {
3915 server_lockread = true;
3916 server_writeunlock = true;
3919 if (conn->protocol >= PROTOCOL_NT1) {
3920 const char *client_signing = NULL;
3921 bool server_mandatory = false;
3922 bool server_allowed = false;
3923 const char *server_signing = NULL;
3924 bool ok;
3925 uint8_t key_len;
3927 if (wct != 0x11) {
3928 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3929 return;
3932 /* NT protocol */
3933 server_security_mode = CVAL(vwv + 1, 0);
3934 server_max_mux = SVAL(vwv + 1, 1);
3935 server_max_xmit = IVAL(vwv + 3, 1);
3936 server_session_key = IVAL(vwv + 7, 1);
3937 server_time_zone = SVALS(vwv + 15, 1);
3938 server_time_zone *= 60;
3939 /* this time arrives in real GMT */
3940 server_system_time = BVAL(vwv + 11, 1);
3941 server_capabilities = IVAL(vwv + 9, 1);
3943 key_len = CVAL(vwv + 16, 1);
3945 if (server_capabilities & CAP_RAW_MODE) {
3946 server_readbraw = true;
3947 server_writebraw = true;
3949 if (server_capabilities & CAP_LOCK_AND_READ) {
3950 server_lockread = true;
3953 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3954 DATA_BLOB blob1, blob2;
3956 if (num_bytes < 16) {
3957 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3958 return;
3961 blob1 = data_blob_const(bytes, 16);
3962 status = GUID_from_data_blob(&blob1, &server_guid);
3963 if (tevent_req_nterror(req, status)) {
3964 return;
3967 blob1 = data_blob_const(bytes+16, num_bytes-16);
3968 blob2 = data_blob_dup_talloc(state, blob1);
3969 if (blob1.length > 0 &&
3970 tevent_req_nomem(blob2.data, req)) {
3971 return;
3973 server_gss_blob = blob2;
3974 } else {
3975 DATA_BLOB blob1, blob2;
3977 if (num_bytes < key_len) {
3978 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3979 return;
3982 if (key_len != 0 && key_len != 8) {
3983 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3984 return;
3987 if (key_len == 8) {
3988 memcpy(server_challenge, bytes, 8);
3991 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3992 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3993 if (blob1.length > 0) {
3994 size_t len;
3996 len = utf16_len_n(blob1.data,
3997 blob1.length);
3998 blob1.length = len;
4000 ok = convert_string_talloc(state,
4001 CH_UTF16LE,
4002 CH_UNIX,
4003 blob1.data,
4004 blob1.length,
4005 &server_workgroup,
4006 &len);
4007 if (!ok) {
4008 status = map_nt_error_from_unix_common(errno);
4009 tevent_req_nterror(req, status);
4010 return;
4014 blob2.data += blob1.length;
4015 blob2.length -= blob1.length;
4016 if (blob2.length > 0) {
4017 size_t len;
4019 len = utf16_len_n(blob1.data,
4020 blob1.length);
4021 blob1.length = len;
4023 ok = convert_string_talloc(state,
4024 CH_UTF16LE,
4025 CH_UNIX,
4026 blob2.data,
4027 blob2.length,
4028 &server_name,
4029 &len);
4030 if (!ok) {
4031 status = map_nt_error_from_unix_common(errno);
4032 tevent_req_nterror(req, status);
4033 return;
4038 client_signing = "disabled";
4039 if (conn->allow_signing) {
4040 client_signing = "allowed";
4042 if (conn->mandatory_signing) {
4043 client_signing = "required";
4046 server_signing = "not supported";
4047 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4048 server_signing = "supported";
4049 server_allowed = true;
4050 } else if (conn->mandatory_signing) {
4052 * We have mandatory signing as client
4053 * lets assume the server will look at our
4054 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4055 * flag in the session setup
4057 server_signing = "not announced";
4058 server_allowed = true;
4060 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4061 server_signing = "required";
4062 server_mandatory = true;
4065 ok = smb_signing_set_negotiated(conn->smb1.signing,
4066 server_allowed,
4067 server_mandatory);
4068 if (!ok) {
4069 DEBUG(1,("cli_negprot: SMB signing is required, "
4070 "but client[%s] and server[%s] mismatch\n",
4071 client_signing, server_signing));
4072 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4073 return;
4076 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4077 DATA_BLOB blob1;
4078 uint8_t key_len;
4079 time_t t;
4081 if (wct != 0x0D) {
4082 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4083 return;
4086 server_security_mode = SVAL(vwv + 1, 0);
4087 server_max_xmit = SVAL(vwv + 2, 0);
4088 server_max_mux = SVAL(vwv + 3, 0);
4089 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4090 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4091 server_session_key = IVAL(vwv + 6, 0);
4092 server_time_zone = SVALS(vwv + 10, 0);
4093 server_time_zone *= 60;
4094 /* this time is converted to GMT by make_unix_date */
4095 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4096 unix_to_nt_time(&server_system_time, t);
4097 key_len = SVAL(vwv + 11, 0);
4099 if (num_bytes < key_len) {
4100 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4101 return;
4104 if (key_len != 0 && key_len != 8) {
4105 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4106 return;
4109 if (key_len == 8) {
4110 memcpy(server_challenge, bytes, 8);
4113 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4114 if (blob1.length > 0) {
4115 size_t len;
4116 bool ok;
4118 len = utf16_len_n(blob1.data,
4119 blob1.length);
4120 blob1.length = len;
4122 ok = convert_string_talloc(state,
4123 CH_DOS,
4124 CH_UNIX,
4125 blob1.data,
4126 blob1.length,
4127 &server_workgroup,
4128 &len);
4129 if (!ok) {
4130 status = map_nt_error_from_unix_common(errno);
4131 tevent_req_nterror(req, status);
4132 return;
4136 } else {
4137 /* the old core protocol */
4138 server_time_zone = get_time_zone(time(NULL));
4139 server_max_xmit = 1024;
4140 server_max_mux = 1;
4143 if (server_max_xmit < 1024) {
4144 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4145 return;
4148 if (server_max_mux < 1) {
4149 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4150 return;
4154 * Now calculate the negotiated capabilities
4155 * based on the mask for:
4156 * - client only flags
4157 * - flags used in both directions
4158 * - server only flags
4160 both_capabilities = client_capabilities & server_capabilities;
4161 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4162 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4163 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4165 max_xmit = MIN(client_max_xmit, server_max_xmit);
4167 conn->smb1.server.capabilities = server_capabilities;
4168 conn->smb1.capabilities = capabilities;
4170 conn->smb1.server.max_xmit = server_max_xmit;
4171 conn->smb1.max_xmit = max_xmit;
4173 conn->smb1.server.max_mux = server_max_mux;
4175 conn->smb1.server.security_mode = server_security_mode;
4177 conn->smb1.server.readbraw = server_readbraw;
4178 conn->smb1.server.writebraw = server_writebraw;
4179 conn->smb1.server.lockread = server_lockread;
4180 conn->smb1.server.writeunlock = server_writeunlock;
4182 conn->smb1.server.session_key = server_session_key;
4184 talloc_steal(conn, server_gss_blob.data);
4185 conn->smb1.server.gss_blob = server_gss_blob;
4186 conn->smb1.server.guid = server_guid;
4187 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4188 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4189 conn->smb1.server.name = talloc_move(conn, &server_name);
4191 conn->smb1.server.time_zone = server_time_zone;
4192 conn->smb1.server.system_time = server_system_time;
4194 tevent_req_done(req);
4197 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4199 size_t i;
4200 uint8_t *buf;
4201 uint16_t dialect_count = 0;
4203 buf = state->smb2.dyn;
4204 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4205 if (smb2cli_prots[i].proto < state->min_protocol) {
4206 continue;
4209 if (smb2cli_prots[i].proto > state->max_protocol) {
4210 continue;
4213 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4214 dialect_count++;
4217 buf = state->smb2.fixed;
4218 SSVAL(buf, 0, 36);
4219 SSVAL(buf, 2, dialect_count);
4220 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4221 SSVAL(buf, 6, 0); /* Reserved */
4222 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4223 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4224 } else {
4225 SIVAL(buf, 8, 0); /* Capabilities */
4227 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4228 NTSTATUS status;
4229 DATA_BLOB blob;
4231 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4232 state, &blob);
4233 if (!NT_STATUS_IS_OK(status)) {
4234 return NULL;
4236 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4237 } else {
4238 memset(buf+12, 0, 16); /* ClientGuid */
4240 SBVAL(buf, 28, 0); /* ClientStartTime */
4242 return smb2cli_req_send(state, state->ev,
4243 state->conn, SMB2_OP_NEGPROT,
4244 0, 0, /* flags */
4245 state->timeout_msec,
4246 NULL, NULL, /* tcon, session */
4247 state->smb2.fixed, sizeof(state->smb2.fixed),
4248 state->smb2.dyn, dialect_count*2);
4251 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4253 struct tevent_req *req =
4254 tevent_req_callback_data(subreq,
4255 struct tevent_req);
4256 struct smbXcli_negprot_state *state =
4257 tevent_req_data(req,
4258 struct smbXcli_negprot_state);
4259 struct smbXcli_conn *conn = state->conn;
4260 size_t security_offset, security_length;
4261 DATA_BLOB blob;
4262 NTSTATUS status;
4263 struct iovec *iov;
4264 uint8_t *body;
4265 size_t i;
4266 uint16_t dialect_revision;
4267 static const struct smb2cli_req_expected_response expected[] = {
4269 .status = NT_STATUS_OK,
4270 .body_size = 0x41
4274 status = smb2cli_req_recv(subreq, state, &iov,
4275 expected, ARRAY_SIZE(expected));
4276 TALLOC_FREE(subreq);
4277 if (tevent_req_nterror(req, status)) {
4278 return;
4281 body = (uint8_t *)iov[1].iov_base;
4283 dialect_revision = SVAL(body, 4);
4285 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4286 if (smb2cli_prots[i].proto < state->min_protocol) {
4287 continue;
4290 if (smb2cli_prots[i].proto > state->max_protocol) {
4291 continue;
4294 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4295 continue;
4298 conn->protocol = smb2cli_prots[i].proto;
4299 break;
4302 if (conn->protocol == PROTOCOL_NONE) {
4303 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4304 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4305 return;
4308 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4309 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4310 return;
4313 /* make sure we do not loop forever */
4314 state->min_protocol = PROTOCOL_SMB2_02;
4317 * send a SMB2 negprot, in order to negotiate
4318 * the SMB2 dialect.
4320 subreq = smbXcli_negprot_smb2_subreq(state);
4321 if (tevent_req_nomem(subreq, req)) {
4322 return;
4324 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4325 return;
4328 conn->smb2.server.security_mode = SVAL(body, 2);
4330 blob = data_blob_const(body + 8, 16);
4331 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4332 if (tevent_req_nterror(req, status)) {
4333 return;
4336 conn->smb2.server.capabilities = IVAL(body, 24);
4337 conn->smb2.server.max_trans_size= IVAL(body, 28);
4338 conn->smb2.server.max_read_size = IVAL(body, 32);
4339 conn->smb2.server.max_write_size= IVAL(body, 36);
4340 conn->smb2.server.system_time = BVAL(body, 40);
4341 conn->smb2.server.start_time = BVAL(body, 48);
4343 security_offset = SVAL(body, 56);
4344 security_length = SVAL(body, 58);
4346 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4347 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4348 return;
4351 if (security_length > iov[2].iov_len) {
4352 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4353 return;
4356 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4357 iov[2].iov_base,
4358 security_length);
4359 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4360 return;
4363 tevent_req_done(req);
4366 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4367 TALLOC_CTX *tmp_mem,
4368 uint8_t *inbuf)
4370 size_t num_pending = talloc_array_length(conn->pending);
4371 struct tevent_req *subreq;
4372 struct smbXcli_req_state *substate;
4373 struct tevent_req *req;
4374 uint32_t protocol_magic;
4375 size_t inbuf_len = smb_len_nbt(inbuf);
4377 if (num_pending != 1) {
4378 return NT_STATUS_INTERNAL_ERROR;
4381 if (inbuf_len < 4) {
4382 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4385 subreq = conn->pending[0];
4386 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4387 req = tevent_req_callback_data(subreq, struct tevent_req);
4389 protocol_magic = IVAL(inbuf, 4);
4391 switch (protocol_magic) {
4392 case SMB_MAGIC:
4393 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4394 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4395 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4397 case SMB2_MAGIC:
4398 if (substate->smb2.recv_iov == NULL) {
4400 * For the SMB1 negprot we have move it.
4402 substate->smb2.recv_iov = substate->smb1.recv_iov;
4403 substate->smb1.recv_iov = NULL;
4407 * we got an SMB2 answer, which consumed sequence number 0
4408 * so we need to use 1 as the next one.
4410 * we also need to set the current credits to 0
4411 * as we consumed the initial one. The SMB2 answer
4412 * hopefully grant us a new credit.
4414 conn->smb2.mid = 1;
4415 conn->smb2.cur_credits = 0;
4416 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4417 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4418 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4421 DEBUG(10, ("Got non-SMB PDU\n"));
4422 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4425 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4427 return tevent_req_simple_recv_ntstatus(req);
4430 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4431 uint32_t timeout_msec,
4432 enum protocol_types min_protocol,
4433 enum protocol_types max_protocol)
4435 TALLOC_CTX *frame = talloc_stackframe();
4436 struct tevent_context *ev;
4437 struct tevent_req *req;
4438 NTSTATUS status = NT_STATUS_NO_MEMORY;
4439 bool ok;
4441 if (smbXcli_conn_has_async_calls(conn)) {
4443 * Can't use sync call while an async call is in flight
4445 status = NT_STATUS_INVALID_PARAMETER_MIX;
4446 goto fail;
4448 ev = samba_tevent_context_init(frame);
4449 if (ev == NULL) {
4450 goto fail;
4452 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4453 min_protocol, max_protocol);
4454 if (req == NULL) {
4455 goto fail;
4457 ok = tevent_req_poll(req, ev);
4458 if (!ok) {
4459 status = map_nt_error_from_unix_common(errno);
4460 goto fail;
4462 status = smbXcli_negprot_recv(req);
4463 fail:
4464 TALLOC_FREE(frame);
4465 return status;
4468 static int smbXcli_session_destructor(struct smbXcli_session *session)
4470 if (session->conn == NULL) {
4471 return 0;
4474 DLIST_REMOVE(session->conn->sessions, session);
4475 return 0;
4478 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4479 struct smbXcli_conn *conn)
4481 struct smbXcli_session *session;
4483 session = talloc_zero(mem_ctx, struct smbXcli_session);
4484 if (session == NULL) {
4485 return NULL;
4487 session->smb2 = talloc_zero(session, struct smb2cli_session);
4488 if (session->smb2 == NULL) {
4489 talloc_free(session);
4490 return NULL;
4492 talloc_set_destructor(session, smbXcli_session_destructor);
4494 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4495 session->conn = conn;
4497 return session;
4500 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
4501 struct smbXcli_session *src)
4503 struct smbXcli_session *session;
4505 session = talloc_zero(mem_ctx, struct smbXcli_session);
4506 if (session == NULL) {
4507 return NULL;
4509 session->smb2 = talloc_zero(session, struct smb2cli_session);
4510 if (session->smb2 == NULL) {
4511 talloc_free(session);
4512 return NULL;
4515 session->conn = src->conn;
4516 *session->smb2 = *src->smb2;
4517 session->smb2_channel = src->smb2_channel;
4518 session->disconnect_expired = src->disconnect_expired;
4520 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
4521 talloc_set_destructor(session, smbXcli_session_destructor);
4523 return session;
4527 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4528 TALLOC_CTX *mem_ctx,
4529 DATA_BLOB *key)
4531 const DATA_BLOB *application_key;
4533 *key = data_blob_null;
4535 if (session->conn == NULL) {
4536 return NT_STATUS_NO_USER_SESSION_KEY;
4539 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4540 application_key = &session->smb2->application_key;
4541 } else {
4542 application_key = &session->smb1.application_key;
4545 if (application_key->length == 0) {
4546 return NT_STATUS_NO_USER_SESSION_KEY;
4549 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4550 if (key->data == NULL) {
4551 return NT_STATUS_NO_MEMORY;
4554 return NT_STATUS_OK;
4557 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
4559 session->disconnect_expired = true;
4562 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4564 return session->smb1.session_id;
4567 void smb1cli_session_set_id(struct smbXcli_session *session,
4568 uint16_t session_id)
4570 session->smb1.session_id = session_id;
4573 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4574 const DATA_BLOB _session_key)
4576 struct smbXcli_conn *conn = session->conn;
4577 uint8_t session_key[16];
4579 if (conn == NULL) {
4580 return NT_STATUS_INVALID_PARAMETER_MIX;
4583 if (session->smb1.application_key.length != 0) {
4585 * TODO: do not allow this...
4587 * return NT_STATUS_INVALID_PARAMETER_MIX;
4589 data_blob_clear_free(&session->smb1.application_key);
4590 session->smb1.protected_key = false;
4593 if (_session_key.length == 0) {
4594 return NT_STATUS_OK;
4597 ZERO_STRUCT(session_key);
4598 memcpy(session_key, _session_key.data,
4599 MIN(_session_key.length, sizeof(session_key)));
4601 session->smb1.application_key = data_blob_talloc(session,
4602 session_key,
4603 sizeof(session_key));
4604 ZERO_STRUCT(session_key);
4605 if (session->smb1.application_key.data == NULL) {
4606 return NT_STATUS_NO_MEMORY;
4609 session->smb1.protected_key = false;
4611 return NT_STATUS_OK;
4614 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4616 if (session->smb1.protected_key) {
4617 /* already protected */
4618 return NT_STATUS_OK;
4621 if (session->smb1.application_key.length != 16) {
4622 return NT_STATUS_INVALID_PARAMETER_MIX;
4625 smb_key_derivation(session->smb1.application_key.data,
4626 session->smb1.application_key.length,
4627 session->smb1.application_key.data);
4629 session->smb1.protected_key = true;
4631 return NT_STATUS_OK;
4634 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4636 struct smbXcli_conn *conn = session->conn;
4637 uint8_t security_mode = 0;
4639 if (conn == NULL) {
4640 return security_mode;
4643 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4644 if (conn->mandatory_signing) {
4645 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4648 return security_mode;
4651 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4653 return session->smb2->session_id;
4656 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4658 return session->smb2->session_flags;
4661 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4662 uint64_t session_id,
4663 uint16_t session_flags)
4665 session->smb2->session_id = session_id;
4666 session->smb2->session_flags = session_flags;
4669 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4671 session->smb2->channel_sequence += 1;
4674 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4675 const DATA_BLOB _session_key,
4676 const struct iovec *recv_iov)
4678 struct smbXcli_conn *conn = session->conn;
4679 uint16_t no_sign_flags;
4680 uint8_t session_key[16];
4681 NTSTATUS status;
4683 if (conn == NULL) {
4684 return NT_STATUS_INVALID_PARAMETER_MIX;
4687 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4689 if (session->smb2->session_flags & no_sign_flags) {
4690 session->smb2->should_sign = false;
4691 return NT_STATUS_OK;
4694 if (session->smb2->signing_key.length != 0) {
4695 return NT_STATUS_INVALID_PARAMETER_MIX;
4698 ZERO_STRUCT(session_key);
4699 memcpy(session_key, _session_key.data,
4700 MIN(_session_key.length, sizeof(session_key)));
4702 session->smb2->signing_key = data_blob_talloc(session,
4703 session_key,
4704 sizeof(session_key));
4705 if (session->smb2->signing_key.data == NULL) {
4706 ZERO_STRUCT(session_key);
4707 return NT_STATUS_NO_MEMORY;
4710 if (conn->protocol >= PROTOCOL_SMB2_24) {
4711 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4712 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4714 smb2_key_derivation(session_key, sizeof(session_key),
4715 label.data, label.length,
4716 context.data, context.length,
4717 session->smb2->signing_key.data);
4720 session->smb2->encryption_key = data_blob_dup_talloc(session,
4721 session->smb2->signing_key);
4722 if (session->smb2->encryption_key.data == NULL) {
4723 ZERO_STRUCT(session_key);
4724 return NT_STATUS_NO_MEMORY;
4727 if (conn->protocol >= PROTOCOL_SMB2_24) {
4728 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4729 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4731 smb2_key_derivation(session_key, sizeof(session_key),
4732 label.data, label.length,
4733 context.data, context.length,
4734 session->smb2->encryption_key.data);
4737 session->smb2->decryption_key = data_blob_dup_talloc(session,
4738 session->smb2->signing_key);
4739 if (session->smb2->decryption_key.data == NULL) {
4740 ZERO_STRUCT(session_key);
4741 return NT_STATUS_NO_MEMORY;
4744 if (conn->protocol >= PROTOCOL_SMB2_24) {
4745 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4746 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4748 smb2_key_derivation(session_key, sizeof(session_key),
4749 label.data, label.length,
4750 context.data, context.length,
4751 session->smb2->decryption_key.data);
4754 session->smb2->application_key = data_blob_dup_talloc(session,
4755 session->smb2->signing_key);
4756 if (session->smb2->application_key.data == NULL) {
4757 ZERO_STRUCT(session_key);
4758 return NT_STATUS_NO_MEMORY;
4761 if (conn->protocol >= PROTOCOL_SMB2_24) {
4762 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4763 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4765 smb2_key_derivation(session_key, sizeof(session_key),
4766 label.data, label.length,
4767 context.data, context.length,
4768 session->smb2->application_key.data);
4770 ZERO_STRUCT(session_key);
4772 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4773 session->smb2->signing_key);
4774 if (session->smb2_channel.signing_key.data == NULL) {
4775 return NT_STATUS_NO_MEMORY;
4778 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4779 session->conn->protocol,
4780 recv_iov, 3);
4781 if (!NT_STATUS_IS_OK(status)) {
4782 return status;
4785 session->smb2->should_sign = false;
4786 session->smb2->should_encrypt = false;
4788 if (conn->desire_signing) {
4789 session->smb2->should_sign = true;
4792 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4793 session->smb2->should_sign = true;
4796 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4797 session->smb2->should_encrypt = true;
4800 if (conn->protocol < PROTOCOL_SMB2_24) {
4801 session->smb2->should_encrypt = false;
4804 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4805 session->smb2->should_encrypt = false;
4808 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4809 sizeof(session->smb2->nonce_high));
4810 session->smb2->nonce_low = 1;
4812 return NT_STATUS_OK;
4815 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4816 struct smbXcli_session *session1,
4817 struct smbXcli_conn *conn,
4818 struct smbXcli_session **_session2)
4820 struct smbXcli_session *session2;
4822 if (session1->smb2->signing_key.length == 0) {
4823 return NT_STATUS_INVALID_PARAMETER_MIX;
4826 if (conn == NULL) {
4827 return NT_STATUS_INVALID_PARAMETER_MIX;
4830 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4831 if (session2 == NULL) {
4832 return NT_STATUS_NO_MEMORY;
4834 session2->smb2 = talloc_reference(session2, session1->smb2);
4835 if (session2->smb2 == NULL) {
4836 talloc_free(session2);
4837 return NT_STATUS_NO_MEMORY;
4840 talloc_set_destructor(session2, smbXcli_session_destructor);
4841 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4842 session2->conn = conn;
4844 *_session2 = session2;
4845 return NT_STATUS_OK;
4848 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4849 const DATA_BLOB _channel_key,
4850 const struct iovec *recv_iov)
4852 struct smbXcli_conn *conn = session->conn;
4853 uint8_t channel_key[16];
4854 NTSTATUS status;
4856 if (conn == NULL) {
4857 return NT_STATUS_INVALID_PARAMETER_MIX;
4860 if (session->smb2_channel.signing_key.length != 0) {
4861 return NT_STATUS_INVALID_PARAMETER_MIX;
4864 ZERO_STRUCT(channel_key);
4865 memcpy(channel_key, _channel_key.data,
4866 MIN(_channel_key.length, sizeof(channel_key)));
4868 session->smb2_channel.signing_key = data_blob_talloc(session,
4869 channel_key,
4870 sizeof(channel_key));
4871 if (session->smb2_channel.signing_key.data == NULL) {
4872 ZERO_STRUCT(channel_key);
4873 return NT_STATUS_NO_MEMORY;
4876 if (conn->protocol >= PROTOCOL_SMB2_24) {
4877 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4878 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4880 smb2_key_derivation(channel_key, sizeof(channel_key),
4881 label.data, label.length,
4882 context.data, context.length,
4883 session->smb2_channel.signing_key.data);
4885 ZERO_STRUCT(channel_key);
4887 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4888 session->conn->protocol,
4889 recv_iov, 3);
4890 if (!NT_STATUS_IS_OK(status)) {
4891 return status;
4894 return NT_STATUS_OK;
4897 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4899 struct smbXcli_tcon *tcon;
4901 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4902 if (tcon == NULL) {
4903 return NULL;
4906 return tcon;
4909 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4911 return tcon->smb1.tcon_id;
4914 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4916 tcon->smb1.tcon_id = tcon_id;
4919 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
4920 uint16_t tcon_id,
4921 uint16_t optional_support,
4922 uint32_t maximal_access,
4923 uint32_t guest_maximal_access,
4924 const char *service,
4925 const char *fs_type)
4927 tcon->smb1.tcon_id = tcon_id;
4928 tcon->smb1.optional_support = optional_support;
4929 tcon->smb1.maximal_access = maximal_access;
4930 tcon->smb1.guest_maximal_access = guest_maximal_access;
4932 TALLOC_FREE(tcon->smb1.service);
4933 tcon->smb1.service = talloc_strdup(tcon, service);
4934 if (service != NULL && tcon->smb1.service == NULL) {
4935 return false;
4938 TALLOC_FREE(tcon->smb1.fs_type);
4939 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
4940 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
4941 return false;
4944 return true;
4947 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
4949 return tcon->smb2.tcon_id;
4952 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
4954 return tcon->smb2.capabilities;
4957 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
4958 struct smbXcli_session *session,
4959 uint32_t tcon_id,
4960 uint8_t type,
4961 uint32_t flags,
4962 uint32_t capabilities,
4963 uint32_t maximal_access)
4965 tcon->smb2.tcon_id = tcon_id;
4966 tcon->smb2.type = type;
4967 tcon->smb2.flags = flags;
4968 tcon->smb2.capabilities = capabilities;
4969 tcon->smb2.maximal_access = maximal_access;
4971 tcon->smb2.should_encrypt = false;
4973 if (session == NULL) {
4974 return;
4977 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
4979 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
4980 tcon->smb2.should_encrypt = true;