libcli/smb: use SMB1 MID=0 for the initial Negprot
[Samba.git] / libcli / smb / smbXcli_base.c
blob1e91975b128da2992e79828fda2ec39dad2451a9
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;
250 * the expected max for the response dyn_len
252 uint32_t max_dyn_len;
254 uint16_t credit_charge;
256 bool should_sign;
257 bool should_encrypt;
258 uint64_t encryption_session_id;
260 bool signing_skipped;
261 bool notify_async;
262 bool got_async;
263 } smb2;
266 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
269 * NT_STATUS_OK, means we do not notify the callers
271 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
273 while (conn->sessions) {
274 conn->sessions->conn = NULL;
275 DLIST_REMOVE(conn->sessions, conn->sessions);
278 if (conn->smb1.trans_enc) {
279 TALLOC_FREE(conn->smb1.trans_enc);
282 return 0;
285 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
286 int fd,
287 const char *remote_name,
288 enum smb_signing_setting signing_state,
289 uint32_t smb1_capabilities,
290 struct GUID *client_guid,
291 uint32_t smb2_capabilities)
293 struct smbXcli_conn *conn = NULL;
294 void *ss = NULL;
295 struct sockaddr *sa = NULL;
296 socklen_t sa_length;
297 int ret;
299 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
300 if (!conn) {
301 return NULL;
304 conn->read_fd = fd;
305 conn->write_fd = dup(fd);
306 if (conn->write_fd == -1) {
307 goto error;
310 conn->remote_name = talloc_strdup(conn, remote_name);
311 if (conn->remote_name == NULL) {
312 goto error;
316 ss = (void *)&conn->local_ss;
317 sa = (struct sockaddr *)ss;
318 sa_length = sizeof(conn->local_ss);
319 ret = getsockname(fd, sa, &sa_length);
320 if (ret == -1) {
321 goto error;
323 ss = (void *)&conn->remote_ss;
324 sa = (struct sockaddr *)ss;
325 sa_length = sizeof(conn->remote_ss);
326 ret = getpeername(fd, sa, &sa_length);
327 if (ret == -1) {
328 goto error;
331 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
332 if (conn->outgoing == NULL) {
333 goto error;
335 conn->pending = NULL;
337 conn->protocol = PROTOCOL_NONE;
339 switch (signing_state) {
340 case SMB_SIGNING_OFF:
341 /* never */
342 conn->allow_signing = false;
343 conn->desire_signing = false;
344 conn->mandatory_signing = false;
345 break;
346 case SMB_SIGNING_DEFAULT:
347 case SMB_SIGNING_IF_REQUIRED:
348 /* if the server requires it */
349 conn->allow_signing = true;
350 conn->desire_signing = false;
351 conn->mandatory_signing = false;
352 break;
353 case SMB_SIGNING_REQUIRED:
354 /* always */
355 conn->allow_signing = true;
356 conn->desire_signing = true;
357 conn->mandatory_signing = true;
358 break;
361 conn->smb1.client.capabilities = smb1_capabilities;
362 conn->smb1.client.max_xmit = UINT16_MAX;
364 conn->smb1.capabilities = conn->smb1.client.capabilities;
365 conn->smb1.max_xmit = 1024;
367 conn->smb1.mid = 1;
369 /* initialise signing */
370 conn->smb1.signing = smb_signing_init(conn,
371 conn->allow_signing,
372 conn->desire_signing,
373 conn->mandatory_signing);
374 if (!conn->smb1.signing) {
375 goto error;
378 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
379 if (conn->mandatory_signing) {
380 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
382 if (client_guid) {
383 conn->smb2.client.guid = *client_guid;
385 conn->smb2.client.capabilities = smb2_capabilities;
387 conn->smb2.cur_credits = 1;
388 conn->smb2.max_credits = 0;
390 talloc_set_destructor(conn, smbXcli_conn_destructor);
391 return conn;
393 error:
394 if (conn->write_fd != -1) {
395 close(conn->write_fd);
397 TALLOC_FREE(conn);
398 return NULL;
401 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
403 if (conn == NULL) {
404 return false;
407 if (conn->read_fd == -1) {
408 return false;
411 return true;
414 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
416 return conn->protocol;
419 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
421 if (conn->protocol >= PROTOCOL_SMB2_02) {
422 return true;
425 if (conn->smb1.capabilities & CAP_UNICODE) {
426 return true;
429 return false;
432 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
434 set_socket_options(conn->read_fd, options);
437 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
439 return &conn->local_ss;
442 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
444 return &conn->remote_ss;
447 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
449 return conn->remote_name;
452 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
454 if (conn->protocol >= PROTOCOL_SMB2_02) {
456 * TODO...
458 return 1;
461 return conn->smb1.server.max_mux;
464 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
466 if (conn->protocol >= PROTOCOL_SMB2_02) {
467 return conn->smb2.server.system_time;
470 return conn->smb1.server.system_time;
473 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
475 if (conn->protocol >= PROTOCOL_SMB2_02) {
476 return &conn->smb2.server.gss_blob;
479 return &conn->smb1.server.gss_blob;
482 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
484 if (conn->protocol >= PROTOCOL_SMB2_02) {
485 return &conn->smb2.server.guid;
488 return &conn->smb1.server.guid;
491 struct smbXcli_conn_samba_suicide_state {
492 struct smbXcli_conn *conn;
493 struct iovec iov;
494 uint8_t buf[9];
497 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
499 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
500 struct tevent_context *ev,
501 struct smbXcli_conn *conn,
502 uint8_t exitcode)
504 struct tevent_req *req, *subreq;
505 struct smbXcli_conn_samba_suicide_state *state;
507 req = tevent_req_create(mem_ctx, &state,
508 struct smbXcli_conn_samba_suicide_state);
509 if (req == NULL) {
510 return NULL;
512 state->conn = conn;
513 SIVAL(state->buf, 4, 0x74697865);
514 SCVAL(state->buf, 8, exitcode);
515 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
517 state->iov.iov_base = state->buf;
518 state->iov.iov_len = sizeof(state->buf);
520 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
521 false, &state->iov, 1);
522 if (tevent_req_nomem(subreq, req)) {
523 return tevent_req_post(req, ev);
525 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
526 return req;
529 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
531 struct tevent_req *req = tevent_req_callback_data(
532 subreq, struct tevent_req);
533 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
534 req, struct smbXcli_conn_samba_suicide_state);
535 ssize_t nwritten;
536 int err;
538 nwritten = writev_recv(subreq, &err);
539 TALLOC_FREE(subreq);
540 if (nwritten == -1) {
541 NTSTATUS status = map_nt_error_from_unix_common(err);
542 smbXcli_conn_disconnect(state->conn, status);
543 return;
545 tevent_req_done(req);
548 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
550 return tevent_req_simple_recv_ntstatus(req);
553 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
554 uint8_t exitcode)
556 TALLOC_CTX *frame = talloc_stackframe();
557 struct tevent_context *ev;
558 struct tevent_req *req;
559 NTSTATUS status = NT_STATUS_NO_MEMORY;
560 bool ok;
562 if (smbXcli_conn_has_async_calls(conn)) {
564 * Can't use sync call while an async call is in flight
566 status = NT_STATUS_INVALID_PARAMETER_MIX;
567 goto fail;
569 ev = samba_tevent_context_init(frame);
570 if (ev == NULL) {
571 goto fail;
573 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
574 if (req == NULL) {
575 goto fail;
577 ok = tevent_req_poll(req, ev);
578 if (!ok) {
579 status = map_nt_error_from_unix_common(errno);
580 goto fail;
582 status = smbXcli_conn_samba_suicide_recv(req);
583 fail:
584 TALLOC_FREE(frame);
585 return status;
588 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
590 return conn->smb1.capabilities;
593 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
595 return conn->smb1.max_xmit;
598 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
600 size_t pending;
601 uint16_t possible = conn->smb1.server.max_mux;
603 pending = tevent_queue_length(conn->outgoing);
604 if (pending >= possible) {
605 return false;
607 pending += talloc_array_length(conn->pending);
608 if (pending >= possible) {
609 return false;
612 return true;
615 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
617 return conn->smb1.server.session_key;
620 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
622 return conn->smb1.server.challenge;
625 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
627 return conn->smb1.server.security_mode;
630 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
632 return conn->smb1.server.readbraw;
635 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
637 return conn->smb1.server.writebraw;
640 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
642 return conn->smb1.server.lockread;
645 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
647 return conn->smb1.server.writeunlock;
650 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
652 return conn->smb1.server.time_zone;
655 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
656 const DATA_BLOB user_session_key,
657 const DATA_BLOB response)
659 return smb_signing_activate(conn->smb1.signing,
660 user_session_key,
661 response);
664 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
665 const uint8_t *buf, uint32_t seqnum)
667 const uint8_t *hdr = buf + NBT_HDR_SIZE;
668 size_t len = smb_len_nbt(buf);
670 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
673 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
675 return smb_signing_is_active(conn->smb1.signing);
678 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
679 struct smb_trans_enc_state *es)
681 /* Replace the old state, if any. */
682 if (conn->smb1.trans_enc) {
683 TALLOC_FREE(conn->smb1.trans_enc);
685 conn->smb1.trans_enc = es;
688 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
690 return common_encryption_on(conn->smb1.trans_enc);
694 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
696 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
697 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
699 if (NT_STATUS_IS_OK(status)) {
700 return NT_STATUS_OK;
703 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
704 return status;
707 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
711 * Is the SMB command able to hold an AND_X successor
712 * @param[in] cmd The SMB command in question
713 * @retval Can we add a chained request after "cmd"?
715 bool smb1cli_is_andx_req(uint8_t cmd)
717 switch (cmd) {
718 case SMBtconX:
719 case SMBlockingX:
720 case SMBopenX:
721 case SMBreadX:
722 case SMBwriteX:
723 case SMBsesssetupX:
724 case SMBulogoffX:
725 case SMBntcreateX:
726 return true;
727 break;
728 default:
729 break;
732 return false;
735 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
737 size_t num_pending = talloc_array_length(conn->pending);
738 uint16_t result;
740 if (conn->protocol == PROTOCOL_NONE) {
742 * This is what windows sends on the SMB1 Negprot request
743 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
745 return 0;
748 while (true) {
749 size_t i;
751 result = conn->smb1.mid++;
752 if ((result == 0) || (result == 0xffff)) {
753 continue;
756 for (i=0; i<num_pending; i++) {
757 if (result == smb1cli_req_mid(conn->pending[i])) {
758 break;
762 if (i == num_pending) {
763 return result;
768 void smbXcli_req_unset_pending(struct tevent_req *req)
770 struct smbXcli_req_state *state =
771 tevent_req_data(req,
772 struct smbXcli_req_state);
773 struct smbXcli_conn *conn = state->conn;
774 size_t num_pending = talloc_array_length(conn->pending);
775 size_t i;
777 if (state->smb1.mid != 0) {
779 * This is a [nt]trans[2] request which waits
780 * for more than one reply.
782 return;
785 talloc_set_destructor(req, NULL);
787 if (num_pending == 1) {
789 * The pending read_smb tevent_req is a child of
790 * conn->pending. So if nothing is pending anymore, we need to
791 * delete the socket read fde.
793 TALLOC_FREE(conn->pending);
794 conn->read_smb_req = NULL;
795 return;
798 for (i=0; i<num_pending; i++) {
799 if (req == conn->pending[i]) {
800 break;
803 if (i == num_pending) {
805 * Something's seriously broken. Just returning here is the
806 * right thing nevertheless, the point of this routine is to
807 * remove ourselves from conn->pending.
809 return;
813 * Remove ourselves from the conn->pending array
815 for (; i < (num_pending - 1); i++) {
816 conn->pending[i] = conn->pending[i+1];
820 * No NULL check here, we're shrinking by sizeof(void *), and
821 * talloc_realloc just adjusts the size for this.
823 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
824 num_pending - 1);
825 return;
828 static int smbXcli_req_destructor(struct tevent_req *req)
830 struct smbXcli_req_state *state =
831 tevent_req_data(req,
832 struct smbXcli_req_state);
835 * Make sure we really remove it from
836 * the pending array on destruction.
838 state->smb1.mid = 0;
839 smbXcli_req_unset_pending(req);
840 return 0;
843 static bool smb1cli_req_cancel(struct tevent_req *req);
844 static bool smb2cli_req_cancel(struct tevent_req *req);
846 static bool smbXcli_req_cancel(struct tevent_req *req)
848 struct smbXcli_req_state *state =
849 tevent_req_data(req,
850 struct smbXcli_req_state);
852 if (!smbXcli_conn_is_connected(state->conn)) {
853 return false;
856 if (state->conn->protocol == PROTOCOL_NONE) {
857 return false;
860 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
861 return smb2cli_req_cancel(req);
864 return smb1cli_req_cancel(req);
867 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
869 bool smbXcli_req_set_pending(struct tevent_req *req)
871 struct smbXcli_req_state *state =
872 tevent_req_data(req,
873 struct smbXcli_req_state);
874 struct smbXcli_conn *conn;
875 struct tevent_req **pending;
876 size_t num_pending;
878 conn = state->conn;
880 if (!smbXcli_conn_is_connected(conn)) {
881 return false;
884 num_pending = talloc_array_length(conn->pending);
886 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
887 num_pending+1);
888 if (pending == NULL) {
889 return false;
891 pending[num_pending] = req;
892 conn->pending = pending;
893 talloc_set_destructor(req, smbXcli_req_destructor);
894 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
896 if (!smbXcli_conn_receive_next(conn)) {
898 * the caller should notify the current request
900 * And all other pending requests get notified
901 * by smbXcli_conn_disconnect().
903 smbXcli_req_unset_pending(req);
904 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
905 return false;
908 return true;
911 static void smbXcli_conn_received(struct tevent_req *subreq);
913 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
915 size_t num_pending = talloc_array_length(conn->pending);
916 struct tevent_req *req;
917 struct smbXcli_req_state *state;
919 if (conn->read_smb_req != NULL) {
920 return true;
923 if (num_pending == 0) {
924 if (conn->smb2.mid < UINT64_MAX) {
925 /* no more pending requests, so we are done for now */
926 return true;
930 * If there are no more SMB2 requests possible,
931 * because we are out of message ids,
932 * we need to disconnect.
934 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
935 return true;
938 req = conn->pending[0];
939 state = tevent_req_data(req, struct smbXcli_req_state);
942 * We're the first ones, add the read_smb request that waits for the
943 * answer from the server
945 conn->read_smb_req = read_smb_send(conn->pending,
946 state->ev,
947 conn->read_fd);
948 if (conn->read_smb_req == NULL) {
949 return false;
951 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
952 return true;
955 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
957 struct smbXcli_session *session;
959 tevent_queue_stop(conn->outgoing);
961 if (conn->read_fd != -1) {
962 close(conn->read_fd);
964 if (conn->write_fd != -1) {
965 close(conn->write_fd);
967 conn->read_fd = -1;
968 conn->write_fd = -1;
970 session = conn->sessions;
971 if (talloc_array_length(conn->pending) == 0) {
973 * if we do not have pending requests
974 * there is no need to update the channel_sequence
976 session = NULL;
978 for (; session; session = session->next) {
979 smb2cli_session_increment_channel_sequence(session);
983 * Cancel all pending requests. We do not do a for-loop walking
984 * conn->pending because that array changes in
985 * smbXcli_req_unset_pending.
987 while (talloc_array_length(conn->pending) > 0) {
988 struct tevent_req *req;
989 struct smbXcli_req_state *state;
990 struct tevent_req **chain;
991 size_t num_chained;
992 size_t i;
994 req = conn->pending[0];
995 state = tevent_req_data(req, struct smbXcli_req_state);
997 if (state->smb1.chained_requests == NULL) {
999 * We're dead. No point waiting for trans2
1000 * replies.
1002 state->smb1.mid = 0;
1004 smbXcli_req_unset_pending(req);
1006 if (NT_STATUS_IS_OK(status)) {
1007 /* do not notify the callers */
1008 continue;
1012 * we need to defer the callback, because we may notify
1013 * more then one caller.
1015 tevent_req_defer_callback(req, state->ev);
1016 tevent_req_nterror(req, status);
1017 continue;
1020 chain = talloc_move(conn, &state->smb1.chained_requests);
1021 num_chained = talloc_array_length(chain);
1023 for (i=0; i<num_chained; i++) {
1024 req = chain[i];
1025 state = tevent_req_data(req, struct smbXcli_req_state);
1028 * We're dead. No point waiting for trans2
1029 * replies.
1031 state->smb1.mid = 0;
1033 smbXcli_req_unset_pending(req);
1035 if (NT_STATUS_IS_OK(status)) {
1036 /* do not notify the callers */
1037 continue;
1041 * we need to defer the callback, because we may notify
1042 * more than one caller.
1044 tevent_req_defer_callback(req, state->ev);
1045 tevent_req_nterror(req, status);
1047 TALLOC_FREE(chain);
1052 * Fetch a smb request's mid. Only valid after the request has been sent by
1053 * smb1cli_req_send().
1055 uint16_t smb1cli_req_mid(struct tevent_req *req)
1057 struct smbXcli_req_state *state =
1058 tevent_req_data(req,
1059 struct smbXcli_req_state);
1061 if (state->smb1.mid != 0) {
1062 return state->smb1.mid;
1065 return SVAL(state->smb1.hdr, HDR_MID);
1068 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1070 struct smbXcli_req_state *state =
1071 tevent_req_data(req,
1072 struct smbXcli_req_state);
1074 state->smb1.mid = mid;
1077 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1079 struct smbXcli_req_state *state =
1080 tevent_req_data(req,
1081 struct smbXcli_req_state);
1083 return state->smb1.seqnum;
1086 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1088 struct smbXcli_req_state *state =
1089 tevent_req_data(req,
1090 struct smbXcli_req_state);
1092 state->smb1.seqnum = seqnum;
1095 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1097 size_t result = 0;
1098 int i;
1099 for (i=0; i<count; i++) {
1100 result += iov[i].iov_len;
1102 return result;
1105 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1106 const struct iovec *iov,
1107 int count)
1109 size_t len = smbXcli_iov_len(iov, count);
1110 size_t copied;
1111 uint8_t *buf;
1112 int i;
1114 buf = talloc_array(mem_ctx, uint8_t, len);
1115 if (buf == NULL) {
1116 return NULL;
1118 copied = 0;
1119 for (i=0; i<count; i++) {
1120 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1121 copied += iov[i].iov_len;
1123 return buf;
1126 static void smb1cli_req_flags(enum protocol_types protocol,
1127 uint32_t smb1_capabilities,
1128 uint8_t smb_command,
1129 uint8_t additional_flags,
1130 uint8_t clear_flags,
1131 uint8_t *_flags,
1132 uint16_t additional_flags2,
1133 uint16_t clear_flags2,
1134 uint16_t *_flags2)
1136 uint8_t flags = 0;
1137 uint16_t flags2 = 0;
1139 if (protocol >= PROTOCOL_LANMAN1) {
1140 flags |= FLAG_CASELESS_PATHNAMES;
1141 flags |= FLAG_CANONICAL_PATHNAMES;
1144 if (protocol >= PROTOCOL_LANMAN2) {
1145 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1146 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1149 if (protocol >= PROTOCOL_NT1) {
1150 flags2 |= FLAGS2_IS_LONG_NAME;
1152 if (smb1_capabilities & CAP_UNICODE) {
1153 flags2 |= FLAGS2_UNICODE_STRINGS;
1155 if (smb1_capabilities & CAP_STATUS32) {
1156 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1158 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1159 flags2 |= FLAGS2_EXTENDED_SECURITY;
1163 flags |= additional_flags;
1164 flags &= ~clear_flags;
1165 flags2 |= additional_flags2;
1166 flags2 &= ~clear_flags2;
1168 *_flags = flags;
1169 *_flags2 = flags2;
1172 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1174 static bool smb1cli_req_cancel(struct tevent_req *req)
1176 struct smbXcli_req_state *state =
1177 tevent_req_data(req,
1178 struct smbXcli_req_state);
1179 uint8_t flags;
1180 uint16_t flags2;
1181 uint32_t pid;
1182 uint16_t mid;
1183 struct tevent_req *subreq;
1184 NTSTATUS status;
1186 flags = CVAL(state->smb1.hdr, HDR_FLG);
1187 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1188 pid = SVAL(state->smb1.hdr, HDR_PID);
1189 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1190 mid = SVAL(state->smb1.hdr, HDR_MID);
1192 subreq = smb1cli_req_create(state, state->ev,
1193 state->conn,
1194 SMBntcancel,
1195 flags, 0,
1196 flags2, 0,
1197 0, /* timeout */
1198 pid,
1199 state->tcon,
1200 state->session,
1201 0, NULL, /* vwv */
1202 0, NULL); /* bytes */
1203 if (subreq == NULL) {
1204 return false;
1206 smb1cli_req_set_mid(subreq, mid);
1208 status = smb1cli_req_chain_submit(&subreq, 1);
1209 if (!NT_STATUS_IS_OK(status)) {
1210 TALLOC_FREE(subreq);
1211 return false;
1213 smb1cli_req_set_mid(subreq, 0);
1215 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1217 return true;
1220 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1222 /* we do not care about the result */
1223 TALLOC_FREE(subreq);
1226 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1227 struct tevent_context *ev,
1228 struct smbXcli_conn *conn,
1229 uint8_t smb_command,
1230 uint8_t additional_flags,
1231 uint8_t clear_flags,
1232 uint16_t additional_flags2,
1233 uint16_t clear_flags2,
1234 uint32_t timeout_msec,
1235 uint32_t pid,
1236 struct smbXcli_tcon *tcon,
1237 struct smbXcli_session *session,
1238 uint8_t wct, uint16_t *vwv,
1239 int iov_count,
1240 struct iovec *bytes_iov)
1242 struct tevent_req *req;
1243 struct smbXcli_req_state *state;
1244 uint8_t flags = 0;
1245 uint16_t flags2 = 0;
1246 uint16_t uid = 0;
1247 uint16_t tid = 0;
1249 if (iov_count > MAX_SMB_IOV) {
1251 * Should not happen :-)
1253 return NULL;
1256 req = tevent_req_create(mem_ctx, &state,
1257 struct smbXcli_req_state);
1258 if (req == NULL) {
1259 return NULL;
1261 state->ev = ev;
1262 state->conn = conn;
1263 state->session = session;
1264 state->tcon = tcon;
1266 if (session) {
1267 uid = session->smb1.session_id;
1270 if (tcon) {
1271 tid = tcon->smb1.tcon_id;
1274 state->smb1.recv_cmd = 0xFF;
1275 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1276 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1277 if (state->smb1.recv_iov == NULL) {
1278 TALLOC_FREE(req);
1279 return NULL;
1282 smb1cli_req_flags(conn->protocol,
1283 conn->smb1.capabilities,
1284 smb_command,
1285 additional_flags,
1286 clear_flags,
1287 &flags,
1288 additional_flags2,
1289 clear_flags2,
1290 &flags2);
1292 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1293 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1294 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1295 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1296 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1297 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1298 SSVAL(state->smb1.hdr, HDR_TID, tid);
1299 SSVAL(state->smb1.hdr, HDR_PID, pid);
1300 SSVAL(state->smb1.hdr, HDR_UID, uid);
1301 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1302 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1304 state->smb1.vwv = vwv;
1306 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1308 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1309 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1310 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1311 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1312 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1313 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1314 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1315 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1317 if (iov_count != 0) {
1318 memcpy(&state->smb1.iov[4], bytes_iov,
1319 iov_count * sizeof(*bytes_iov));
1321 state->smb1.iov_count = iov_count + 4;
1323 if (timeout_msec > 0) {
1324 struct timeval endtime;
1326 endtime = timeval_current_ofs_msec(timeout_msec);
1327 if (!tevent_req_set_endtime(req, ev, endtime)) {
1328 return req;
1332 switch (smb_command) {
1333 case SMBtranss:
1334 case SMBtranss2:
1335 case SMBnttranss:
1336 state->one_way = true;
1337 break;
1338 case SMBntcancel:
1339 state->one_way = true;
1340 state->smb1.one_way_seqnum = true;
1341 break;
1342 case SMBlockingX:
1343 if ((wct == 8) &&
1344 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1345 state->one_way = true;
1347 break;
1350 return req;
1353 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1354 struct iovec *iov, int iov_count,
1355 uint32_t *seqnum,
1356 bool one_way_seqnum)
1358 TALLOC_CTX *frame = NULL;
1359 uint8_t *buf;
1362 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1363 * iovec directly. MD5Update would do that just fine.
1366 if (iov_count < 4) {
1367 return NT_STATUS_INVALID_PARAMETER_MIX;
1369 if (iov[0].iov_len != NBT_HDR_SIZE) {
1370 return NT_STATUS_INVALID_PARAMETER_MIX;
1372 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1373 return NT_STATUS_INVALID_PARAMETER_MIX;
1375 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1376 return NT_STATUS_INVALID_PARAMETER_MIX;
1378 if (iov[3].iov_len != sizeof(uint16_t)) {
1379 return NT_STATUS_INVALID_PARAMETER_MIX;
1382 frame = talloc_stackframe();
1384 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1385 if (buf == NULL) {
1386 return NT_STATUS_NO_MEMORY;
1389 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1390 one_way_seqnum);
1391 smb_signing_sign_pdu(conn->smb1.signing,
1392 buf, talloc_get_size(buf),
1393 *seqnum);
1394 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1396 TALLOC_FREE(frame);
1397 return NT_STATUS_OK;
1400 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1401 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1402 TALLOC_CTX *tmp_mem,
1403 uint8_t *inbuf);
1405 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1406 struct smbXcli_req_state *state,
1407 struct iovec *iov, int iov_count)
1409 struct tevent_req *subreq;
1410 NTSTATUS status;
1411 uint8_t cmd;
1412 uint16_t mid;
1414 if (!smbXcli_conn_is_connected(state->conn)) {
1415 return NT_STATUS_CONNECTION_DISCONNECTED;
1418 if (state->conn->protocol > PROTOCOL_NT1) {
1419 return NT_STATUS_REVISION_MISMATCH;
1422 if (iov_count < 4) {
1423 return NT_STATUS_INVALID_PARAMETER_MIX;
1425 if (iov[0].iov_len != NBT_HDR_SIZE) {
1426 return NT_STATUS_INVALID_PARAMETER_MIX;
1428 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1429 return NT_STATUS_INVALID_PARAMETER_MIX;
1431 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1432 return NT_STATUS_INVALID_PARAMETER_MIX;
1434 if (iov[3].iov_len != sizeof(uint16_t)) {
1435 return NT_STATUS_INVALID_PARAMETER_MIX;
1438 cmd = CVAL(iov[1].iov_base, HDR_COM);
1439 if (cmd == SMBreadBraw) {
1440 if (smbXcli_conn_has_async_calls(state->conn)) {
1441 return NT_STATUS_INVALID_PARAMETER_MIX;
1443 state->conn->smb1.read_braw_req = req;
1446 if (state->smb1.mid != 0) {
1447 mid = state->smb1.mid;
1448 } else {
1449 mid = smb1cli_alloc_mid(state->conn);
1451 SSVAL(iov[1].iov_base, HDR_MID, mid);
1453 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1455 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1456 &state->smb1.seqnum,
1457 state->smb1.one_way_seqnum);
1459 if (!NT_STATUS_IS_OK(status)) {
1460 return status;
1464 * If we supported multiple encrytion contexts
1465 * here we'd look up based on tid.
1467 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1468 char *buf, *enc_buf;
1470 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1471 if (buf == NULL) {
1472 return NT_STATUS_NO_MEMORY;
1474 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1475 (char *)buf, &enc_buf);
1476 TALLOC_FREE(buf);
1477 if (!NT_STATUS_IS_OK(status)) {
1478 DEBUG(0, ("Error in encrypting client message: %s\n",
1479 nt_errstr(status)));
1480 return status;
1482 buf = (char *)talloc_memdup(state, enc_buf,
1483 smb_len_nbt(enc_buf)+4);
1484 SAFE_FREE(enc_buf);
1485 if (buf == NULL) {
1486 return NT_STATUS_NO_MEMORY;
1488 iov[0].iov_base = (void *)buf;
1489 iov[0].iov_len = talloc_get_size(buf);
1490 iov_count = 1;
1493 if (state->conn->dispatch_incoming == NULL) {
1494 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1497 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1499 subreq = writev_send(state, state->ev, state->conn->outgoing,
1500 state->conn->write_fd, false, iov, iov_count);
1501 if (subreq == NULL) {
1502 return NT_STATUS_NO_MEMORY;
1504 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1505 return NT_STATUS_OK;
1508 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1509 struct tevent_context *ev,
1510 struct smbXcli_conn *conn,
1511 uint8_t smb_command,
1512 uint8_t additional_flags,
1513 uint8_t clear_flags,
1514 uint16_t additional_flags2,
1515 uint16_t clear_flags2,
1516 uint32_t timeout_msec,
1517 uint32_t pid,
1518 struct smbXcli_tcon *tcon,
1519 struct smbXcli_session *session,
1520 uint8_t wct, uint16_t *vwv,
1521 uint32_t num_bytes,
1522 const uint8_t *bytes)
1524 struct tevent_req *req;
1525 struct iovec iov;
1526 NTSTATUS status;
1528 iov.iov_base = discard_const_p(void, bytes);
1529 iov.iov_len = num_bytes;
1531 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1532 additional_flags, clear_flags,
1533 additional_flags2, clear_flags2,
1534 timeout_msec,
1535 pid, tcon, session,
1536 wct, vwv, 1, &iov);
1537 if (req == NULL) {
1538 return NULL;
1540 if (!tevent_req_is_in_progress(req)) {
1541 return tevent_req_post(req, ev);
1543 status = smb1cli_req_chain_submit(&req, 1);
1544 if (tevent_req_nterror(req, status)) {
1545 return tevent_req_post(req, ev);
1547 return req;
1550 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1552 struct tevent_req *req =
1553 tevent_req_callback_data(subreq,
1554 struct tevent_req);
1555 struct smbXcli_req_state *state =
1556 tevent_req_data(req,
1557 struct smbXcli_req_state);
1558 ssize_t nwritten;
1559 int err;
1561 nwritten = writev_recv(subreq, &err);
1562 TALLOC_FREE(subreq);
1563 if (nwritten == -1) {
1564 NTSTATUS status = map_nt_error_from_unix_common(err);
1565 smbXcli_conn_disconnect(state->conn, status);
1566 return;
1569 if (state->one_way) {
1570 state->inbuf = NULL;
1571 tevent_req_done(req);
1572 return;
1575 if (!smbXcli_req_set_pending(req)) {
1576 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1577 return;
1581 static void smbXcli_conn_received(struct tevent_req *subreq)
1583 struct smbXcli_conn *conn =
1584 tevent_req_callback_data(subreq,
1585 struct smbXcli_conn);
1586 TALLOC_CTX *frame = talloc_stackframe();
1587 NTSTATUS status;
1588 uint8_t *inbuf;
1589 ssize_t received;
1590 int err;
1592 if (subreq != conn->read_smb_req) {
1593 DEBUG(1, ("Internal error: cli_smb_received called with "
1594 "unexpected subreq\n"));
1595 status = NT_STATUS_INTERNAL_ERROR;
1596 smbXcli_conn_disconnect(conn, status);
1597 TALLOC_FREE(frame);
1598 return;
1600 conn->read_smb_req = NULL;
1602 received = read_smb_recv(subreq, frame, &inbuf, &err);
1603 TALLOC_FREE(subreq);
1604 if (received == -1) {
1605 status = map_nt_error_from_unix_common(err);
1606 smbXcli_conn_disconnect(conn, status);
1607 TALLOC_FREE(frame);
1608 return;
1611 status = conn->dispatch_incoming(conn, frame, inbuf);
1612 TALLOC_FREE(frame);
1613 if (NT_STATUS_IS_OK(status)) {
1615 * We should not do any more processing
1616 * as the dispatch function called
1617 * tevent_req_done().
1619 return;
1620 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1622 * We got an error, so notify all pending requests
1624 smbXcli_conn_disconnect(conn, status);
1625 return;
1629 * We got NT_STATUS_RETRY, so we may ask for a
1630 * next incoming pdu.
1632 if (!smbXcli_conn_receive_next(conn)) {
1633 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1637 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1638 struct iovec **piov, int *pnum_iov)
1640 struct iovec *iov;
1641 int num_iov;
1642 size_t buflen;
1643 size_t taken;
1644 size_t remaining;
1645 uint8_t *hdr;
1646 uint8_t cmd;
1647 uint32_t wct_ofs;
1648 NTSTATUS status;
1649 size_t min_size = MIN_SMB_SIZE;
1651 buflen = smb_len_tcp(buf);
1652 taken = 0;
1654 hdr = buf + NBT_HDR_SIZE;
1656 status = smb1cli_pull_raw_error(hdr);
1657 if (NT_STATUS_IS_ERR(status)) {
1659 * This is an ugly hack to support OS/2
1660 * which skips the byte_count in the DATA block
1661 * on some error responses.
1663 * See bug #9096
1665 min_size -= sizeof(uint16_t);
1668 if (buflen < min_size) {
1669 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1673 * This returns iovec elements in the following order:
1675 * - SMB header
1677 * - Parameter Block
1678 * - Data Block
1680 * - Parameter Block
1681 * - Data Block
1683 * - Parameter Block
1684 * - Data Block
1686 num_iov = 1;
1688 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1689 if (iov == NULL) {
1690 return NT_STATUS_NO_MEMORY;
1692 iov[0].iov_base = hdr;
1693 iov[0].iov_len = HDR_WCT;
1694 taken += HDR_WCT;
1696 cmd = CVAL(hdr, HDR_COM);
1697 wct_ofs = HDR_WCT;
1699 while (true) {
1700 size_t len = buflen - taken;
1701 struct iovec *cur;
1702 struct iovec *iov_tmp;
1703 uint8_t wct;
1704 uint32_t bcc_ofs;
1705 uint16_t bcc;
1706 size_t needed;
1709 * we need at least WCT
1711 needed = sizeof(uint8_t);
1712 if (len < needed) {
1713 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1714 __location__, (int)len, (int)needed));
1715 goto inval;
1719 * Now we check if the specified words are there
1721 wct = CVAL(hdr, wct_ofs);
1722 needed += wct * sizeof(uint16_t);
1723 if (len < needed) {
1724 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1725 __location__, (int)len, (int)needed));
1726 goto inval;
1729 if ((num_iov == 1) &&
1730 (len == needed) &&
1731 NT_STATUS_IS_ERR(status))
1734 * This is an ugly hack to support OS/2
1735 * which skips the byte_count in the DATA block
1736 * on some error responses.
1738 * See bug #9096
1740 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1741 num_iov + 2);
1742 if (iov_tmp == NULL) {
1743 TALLOC_FREE(iov);
1744 return NT_STATUS_NO_MEMORY;
1746 iov = iov_tmp;
1747 cur = &iov[num_iov];
1748 num_iov += 2;
1750 cur[0].iov_len = 0;
1751 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1752 cur[1].iov_len = 0;
1753 cur[1].iov_base = cur[0].iov_base;
1755 taken += needed;
1756 break;
1760 * we need at least BCC
1762 needed += sizeof(uint16_t);
1763 if (len < needed) {
1764 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1765 __location__, (int)len, (int)needed));
1766 goto inval;
1770 * Now we check if the specified bytes are there
1772 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1773 bcc = SVAL(hdr, bcc_ofs);
1774 needed += bcc * sizeof(uint8_t);
1775 if (len < needed) {
1776 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1777 __location__, (int)len, (int)needed));
1778 goto inval;
1782 * we allocate 2 iovec structures for words and bytes
1784 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1785 num_iov + 2);
1786 if (iov_tmp == NULL) {
1787 TALLOC_FREE(iov);
1788 return NT_STATUS_NO_MEMORY;
1790 iov = iov_tmp;
1791 cur = &iov[num_iov];
1792 num_iov += 2;
1794 cur[0].iov_len = wct * sizeof(uint16_t);
1795 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1796 cur[1].iov_len = bcc * sizeof(uint8_t);
1797 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1799 taken += needed;
1801 if (!smb1cli_is_andx_req(cmd)) {
1803 * If the current command does not have AndX chanining
1804 * we are done.
1806 break;
1809 if (wct == 0 && bcc == 0) {
1811 * An empty response also ends the chain,
1812 * most likely with an error.
1814 break;
1817 if (wct < 2) {
1818 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1819 __location__, (int)wct, (int)cmd));
1820 goto inval;
1822 cmd = CVAL(cur[0].iov_base, 0);
1823 if (cmd == 0xFF) {
1825 * If it is the end of the chain we are also done.
1827 break;
1829 wct_ofs = SVAL(cur[0].iov_base, 2);
1831 if (wct_ofs < taken) {
1832 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1834 if (wct_ofs > buflen) {
1835 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1839 * we consumed everything up to the start of the next
1840 * parameter block.
1842 taken = wct_ofs;
1845 remaining = buflen - taken;
1847 if (remaining > 0 && num_iov >= 3) {
1849 * The last DATA block gets the remaining
1850 * bytes, this is needed to support
1851 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1853 iov[num_iov-1].iov_len += remaining;
1856 *piov = iov;
1857 *pnum_iov = num_iov;
1858 return NT_STATUS_OK;
1860 inval:
1861 TALLOC_FREE(iov);
1862 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1865 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1866 TALLOC_CTX *tmp_mem,
1867 uint8_t *inbuf)
1869 struct tevent_req *req;
1870 struct smbXcli_req_state *state;
1871 NTSTATUS status;
1872 size_t num_pending;
1873 size_t i;
1874 uint8_t cmd;
1875 uint16_t mid;
1876 bool oplock_break;
1877 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1878 size_t len = smb_len_tcp(inbuf);
1879 struct iovec *iov = NULL;
1880 int num_iov = 0;
1881 struct tevent_req **chain = NULL;
1882 size_t num_chained = 0;
1883 size_t num_responses = 0;
1885 if (conn->smb1.read_braw_req != NULL) {
1886 req = conn->smb1.read_braw_req;
1887 conn->smb1.read_braw_req = NULL;
1888 state = tevent_req_data(req, struct smbXcli_req_state);
1890 smbXcli_req_unset_pending(req);
1892 if (state->smb1.recv_iov == NULL) {
1894 * For requests with more than
1895 * one response, we have to readd the
1896 * recv_iov array.
1898 state->smb1.recv_iov = talloc_zero_array(state,
1899 struct iovec,
1901 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1902 return NT_STATUS_OK;
1906 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1907 state->smb1.recv_iov[0].iov_len = len;
1908 ZERO_STRUCT(state->smb1.recv_iov[1]);
1909 ZERO_STRUCT(state->smb1.recv_iov[2]);
1911 state->smb1.recv_cmd = SMBreadBraw;
1912 state->smb1.recv_status = NT_STATUS_OK;
1913 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1915 tevent_req_done(req);
1916 return NT_STATUS_OK;
1919 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1920 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1921 DEBUG(10, ("Got non-SMB PDU\n"));
1922 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1926 * If we supported multiple encrytion contexts
1927 * here we'd look up based on tid.
1929 if (common_encryption_on(conn->smb1.trans_enc)
1930 && (CVAL(inbuf, 0) == 0)) {
1931 uint16_t enc_ctx_num;
1933 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1934 if (!NT_STATUS_IS_OK(status)) {
1935 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1936 nt_errstr(status)));
1937 return status;
1940 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1941 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1942 enc_ctx_num,
1943 conn->smb1.trans_enc->enc_ctx_num));
1944 return NT_STATUS_INVALID_HANDLE;
1947 status = common_decrypt_buffer(conn->smb1.trans_enc,
1948 (char *)inbuf);
1949 if (!NT_STATUS_IS_OK(status)) {
1950 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1951 nt_errstr(status)));
1952 return status;
1954 inhdr = inbuf + NBT_HDR_SIZE;
1955 len = smb_len_nbt(inbuf);
1958 mid = SVAL(inhdr, HDR_MID);
1959 num_pending = talloc_array_length(conn->pending);
1961 for (i=0; i<num_pending; i++) {
1962 if (mid == smb1cli_req_mid(conn->pending[i])) {
1963 break;
1966 if (i == num_pending) {
1967 /* Dump unexpected reply */
1968 return NT_STATUS_RETRY;
1971 oplock_break = false;
1973 if (mid == 0xffff) {
1975 * Paranoia checks that this is really an oplock break request.
1977 oplock_break = (len == 51); /* hdr + 8 words */
1978 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1979 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1980 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1981 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1983 if (!oplock_break) {
1984 /* Dump unexpected reply */
1985 return NT_STATUS_RETRY;
1989 req = conn->pending[i];
1990 state = tevent_req_data(req, struct smbXcli_req_state);
1992 if (!oplock_break /* oplock breaks are not signed */
1993 && !smb_signing_check_pdu(conn->smb1.signing,
1994 inhdr, len, state->smb1.seqnum+1)) {
1995 DEBUG(10, ("cli_check_sign_mac failed\n"));
1996 return NT_STATUS_ACCESS_DENIED;
1999 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2000 &iov, &num_iov);
2001 if (!NT_STATUS_IS_OK(status)) {
2002 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2003 nt_errstr(status)));
2004 return status;
2007 cmd = CVAL(inhdr, HDR_COM);
2008 status = smb1cli_pull_raw_error(inhdr);
2010 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2011 (state->session != NULL) && state->session->disconnect_expired)
2014 * this should be a short term hack
2015 * until the upper layers have implemented
2016 * re-authentication.
2018 return status;
2021 if (state->smb1.chained_requests == NULL) {
2022 if (num_iov != 3) {
2023 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2026 smbXcli_req_unset_pending(req);
2028 if (state->smb1.recv_iov == NULL) {
2030 * For requests with more than
2031 * one response, we have to readd the
2032 * recv_iov array.
2034 state->smb1.recv_iov = talloc_zero_array(state,
2035 struct iovec,
2037 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2038 return NT_STATUS_OK;
2042 state->smb1.recv_cmd = cmd;
2043 state->smb1.recv_status = status;
2044 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2046 state->smb1.recv_iov[0] = iov[0];
2047 state->smb1.recv_iov[1] = iov[1];
2048 state->smb1.recv_iov[2] = iov[2];
2050 if (talloc_array_length(conn->pending) == 0) {
2051 tevent_req_done(req);
2052 return NT_STATUS_OK;
2055 tevent_req_defer_callback(req, state->ev);
2056 tevent_req_done(req);
2057 return NT_STATUS_RETRY;
2060 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2061 num_chained = talloc_array_length(chain);
2062 num_responses = (num_iov - 1)/2;
2064 if (num_responses > num_chained) {
2065 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2068 for (i=0; i<num_chained; i++) {
2069 size_t iov_idx = 1 + (i*2);
2070 struct iovec *cur = &iov[iov_idx];
2071 uint8_t *inbuf_ref;
2073 req = chain[i];
2074 state = tevent_req_data(req, struct smbXcli_req_state);
2076 smbXcli_req_unset_pending(req);
2079 * as we finish multiple requests here
2080 * we need to defer the callbacks as
2081 * they could destroy our current stack state.
2083 tevent_req_defer_callback(req, state->ev);
2085 if (i >= num_responses) {
2086 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2087 continue;
2090 if (state->smb1.recv_iov == NULL) {
2092 * For requests with more than
2093 * one response, we have to readd the
2094 * recv_iov array.
2096 state->smb1.recv_iov = talloc_zero_array(state,
2097 struct iovec,
2099 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2100 continue;
2104 state->smb1.recv_cmd = cmd;
2106 if (i == (num_responses - 1)) {
2108 * The last request in the chain gets the status
2110 state->smb1.recv_status = status;
2111 } else {
2112 cmd = CVAL(cur[0].iov_base, 0);
2113 state->smb1.recv_status = NT_STATUS_OK;
2116 state->inbuf = inbuf;
2119 * Note: here we use talloc_reference() in a way
2120 * that does not expose it to the caller.
2122 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2123 if (tevent_req_nomem(inbuf_ref, req)) {
2124 continue;
2127 /* copy the related buffers */
2128 state->smb1.recv_iov[0] = iov[0];
2129 state->smb1.recv_iov[1] = cur[0];
2130 state->smb1.recv_iov[2] = cur[1];
2132 tevent_req_done(req);
2135 return NT_STATUS_RETRY;
2138 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2139 TALLOC_CTX *mem_ctx,
2140 struct iovec **piov,
2141 uint8_t **phdr,
2142 uint8_t *pwct,
2143 uint16_t **pvwv,
2144 uint32_t *pvwv_offset,
2145 uint32_t *pnum_bytes,
2146 uint8_t **pbytes,
2147 uint32_t *pbytes_offset,
2148 uint8_t **pinbuf,
2149 const struct smb1cli_req_expected_response *expected,
2150 size_t num_expected)
2152 struct smbXcli_req_state *state =
2153 tevent_req_data(req,
2154 struct smbXcli_req_state);
2155 NTSTATUS status = NT_STATUS_OK;
2156 struct iovec *recv_iov = NULL;
2157 uint8_t *hdr = NULL;
2158 uint8_t wct = 0;
2159 uint32_t vwv_offset = 0;
2160 uint16_t *vwv = NULL;
2161 uint32_t num_bytes = 0;
2162 uint32_t bytes_offset = 0;
2163 uint8_t *bytes = NULL;
2164 size_t i;
2165 bool found_status = false;
2166 bool found_size = false;
2168 if (piov != NULL) {
2169 *piov = NULL;
2171 if (phdr != NULL) {
2172 *phdr = 0;
2174 if (pwct != NULL) {
2175 *pwct = 0;
2177 if (pvwv != NULL) {
2178 *pvwv = NULL;
2180 if (pvwv_offset != NULL) {
2181 *pvwv_offset = 0;
2183 if (pnum_bytes != NULL) {
2184 *pnum_bytes = 0;
2186 if (pbytes != NULL) {
2187 *pbytes = NULL;
2189 if (pbytes_offset != NULL) {
2190 *pbytes_offset = 0;
2192 if (pinbuf != NULL) {
2193 *pinbuf = NULL;
2196 if (state->inbuf != NULL) {
2197 recv_iov = state->smb1.recv_iov;
2198 state->smb1.recv_iov = NULL;
2199 if (state->smb1.recv_cmd != SMBreadBraw) {
2200 hdr = (uint8_t *)recv_iov[0].iov_base;
2201 wct = recv_iov[1].iov_len/2;
2202 vwv = (uint16_t *)recv_iov[1].iov_base;
2203 vwv_offset = PTR_DIFF(vwv, hdr);
2204 num_bytes = recv_iov[2].iov_len;
2205 bytes = (uint8_t *)recv_iov[2].iov_base;
2206 bytes_offset = PTR_DIFF(bytes, hdr);
2210 if (tevent_req_is_nterror(req, &status)) {
2211 for (i=0; i < num_expected; i++) {
2212 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2213 found_status = true;
2214 break;
2218 if (found_status) {
2219 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2222 return status;
2225 if (num_expected == 0) {
2226 found_status = true;
2227 found_size = true;
2230 status = state->smb1.recv_status;
2232 for (i=0; i < num_expected; i++) {
2233 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2234 continue;
2237 found_status = true;
2238 if (expected[i].wct == 0) {
2239 found_size = true;
2240 break;
2243 if (expected[i].wct == wct) {
2244 found_size = true;
2245 break;
2249 if (!found_status) {
2250 return status;
2253 if (!found_size) {
2254 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2257 if (piov != NULL) {
2258 *piov = talloc_move(mem_ctx, &recv_iov);
2261 if (phdr != NULL) {
2262 *phdr = hdr;
2264 if (pwct != NULL) {
2265 *pwct = wct;
2267 if (pvwv != NULL) {
2268 *pvwv = vwv;
2270 if (pvwv_offset != NULL) {
2271 *pvwv_offset = vwv_offset;
2273 if (pnum_bytes != NULL) {
2274 *pnum_bytes = num_bytes;
2276 if (pbytes != NULL) {
2277 *pbytes = bytes;
2279 if (pbytes_offset != NULL) {
2280 *pbytes_offset = bytes_offset;
2282 if (pinbuf != NULL) {
2283 *pinbuf = state->inbuf;
2286 return status;
2289 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2291 size_t wct_ofs;
2292 int i;
2294 wct_ofs = HDR_WCT;
2296 for (i=0; i<num_reqs; i++) {
2297 struct smbXcli_req_state *state;
2298 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2299 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2300 state->smb1.iov_count-2);
2301 wct_ofs = (wct_ofs + 3) & ~3;
2303 return wct_ofs;
2306 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2308 struct smbXcli_req_state *first_state =
2309 tevent_req_data(reqs[0],
2310 struct smbXcli_req_state);
2311 struct smbXcli_req_state *state;
2312 size_t wct_offset;
2313 size_t chain_padding = 0;
2314 int i, iovlen;
2315 struct iovec *iov = NULL;
2316 struct iovec *this_iov;
2317 NTSTATUS status;
2318 size_t nbt_len;
2320 if (num_reqs == 1) {
2321 return smb1cli_req_writev_submit(reqs[0], first_state,
2322 first_state->smb1.iov,
2323 first_state->smb1.iov_count);
2326 iovlen = 0;
2327 for (i=0; i<num_reqs; i++) {
2328 if (!tevent_req_is_in_progress(reqs[i])) {
2329 return NT_STATUS_INTERNAL_ERROR;
2332 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2334 if (state->smb1.iov_count < 4) {
2335 return NT_STATUS_INVALID_PARAMETER_MIX;
2338 if (i == 0) {
2340 * The NBT and SMB header
2342 iovlen += 2;
2343 } else {
2345 * Chain padding
2347 iovlen += 1;
2351 * words and bytes
2353 iovlen += state->smb1.iov_count - 2;
2356 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2357 if (iov == NULL) {
2358 return NT_STATUS_NO_MEMORY;
2361 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2362 first_state, reqs, sizeof(*reqs) * num_reqs);
2363 if (first_state->smb1.chained_requests == NULL) {
2364 TALLOC_FREE(iov);
2365 return NT_STATUS_NO_MEMORY;
2368 wct_offset = HDR_WCT;
2369 this_iov = iov;
2371 for (i=0; i<num_reqs; i++) {
2372 size_t next_padding = 0;
2373 uint16_t *vwv;
2375 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2377 if (i < num_reqs-1) {
2378 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2379 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2380 TALLOC_FREE(iov);
2381 TALLOC_FREE(first_state->smb1.chained_requests);
2382 return NT_STATUS_INVALID_PARAMETER_MIX;
2386 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2387 state->smb1.iov_count-2) + 1;
2388 if ((wct_offset % 4) != 0) {
2389 next_padding = 4 - (wct_offset % 4);
2391 wct_offset += next_padding;
2392 vwv = state->smb1.vwv;
2394 if (i < num_reqs-1) {
2395 struct smbXcli_req_state *next_state =
2396 tevent_req_data(reqs[i+1],
2397 struct smbXcli_req_state);
2398 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2399 SCVAL(vwv+0, 1, 0);
2400 SSVAL(vwv+1, 0, wct_offset);
2401 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2402 /* properly end the chain */
2403 SCVAL(vwv+0, 0, 0xff);
2404 SCVAL(vwv+0, 1, 0xff);
2405 SSVAL(vwv+1, 0, 0);
2408 if (i == 0) {
2410 * The NBT and SMB header
2412 this_iov[0] = state->smb1.iov[0];
2413 this_iov[1] = state->smb1.iov[1];
2414 this_iov += 2;
2415 } else {
2417 * This one is a bit subtle. We have to add
2418 * chain_padding bytes between the requests, and we
2419 * have to also include the wct field of the
2420 * subsequent requests. We use the subsequent header
2421 * for the padding, it contains the wct field in its
2422 * last byte.
2424 this_iov[0].iov_len = chain_padding+1;
2425 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2426 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2427 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2428 this_iov += 1;
2432 * copy the words and bytes
2434 memcpy(this_iov, state->smb1.iov+2,
2435 sizeof(struct iovec) * (state->smb1.iov_count-2));
2436 this_iov += state->smb1.iov_count - 2;
2437 chain_padding = next_padding;
2440 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2441 if (nbt_len > first_state->conn->smb1.max_xmit) {
2442 TALLOC_FREE(iov);
2443 TALLOC_FREE(first_state->smb1.chained_requests);
2444 return NT_STATUS_INVALID_PARAMETER_MIX;
2447 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2448 if (!NT_STATUS_IS_OK(status)) {
2449 TALLOC_FREE(iov);
2450 TALLOC_FREE(first_state->smb1.chained_requests);
2451 return status;
2454 return NT_STATUS_OK;
2457 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2459 return ((tevent_queue_length(conn->outgoing) != 0)
2460 || (talloc_array_length(conn->pending) != 0));
2463 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2465 uint16_t credits = 1;
2467 if (conn->smb2.cur_credits == 0) {
2468 if (max_dyn_len != NULL) {
2469 *max_dyn_len = 0;
2471 return false;
2474 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2475 credits = conn->smb2.cur_credits;
2478 if (max_dyn_len != NULL) {
2479 *max_dyn_len = credits * 65536;
2482 return true;
2485 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2487 return conn->smb2.server.capabilities;
2490 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2492 return conn->smb2.server.security_mode;
2495 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2497 return conn->smb2.server.max_trans_size;
2500 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2502 return conn->smb2.server.max_read_size;
2505 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2507 return conn->smb2.server.max_write_size;
2510 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2511 uint16_t max_credits)
2513 conn->smb2.max_credits = max_credits;
2516 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2518 static bool smb2cli_req_cancel(struct tevent_req *req)
2520 struct smbXcli_req_state *state =
2521 tevent_req_data(req,
2522 struct smbXcli_req_state);
2523 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2524 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2525 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2526 struct smbXcli_tcon *tcon = state->tcon;
2527 struct smbXcli_session *session = state->session;
2528 uint8_t *fixed = state->smb2.pad;
2529 uint16_t fixed_len = 4;
2530 struct tevent_req *subreq;
2531 struct smbXcli_req_state *substate;
2532 NTSTATUS status;
2534 SSVAL(fixed, 0, 0x04);
2535 SSVAL(fixed, 2, 0);
2537 subreq = smb2cli_req_create(state, state->ev,
2538 state->conn,
2539 SMB2_OP_CANCEL,
2540 flags, 0,
2541 0, /* timeout */
2542 tcon, session,
2543 fixed, fixed_len,
2544 NULL, 0, 0);
2545 if (subreq == NULL) {
2546 return false;
2548 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2551 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2552 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2554 flags &= SMB2_HDR_FLAG_ASYNC;
2556 if (flags & SMB2_HDR_FLAG_ASYNC) {
2557 mid = 0;
2560 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2561 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2562 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2564 status = smb2cli_req_compound_submit(&subreq, 1);
2565 if (!NT_STATUS_IS_OK(status)) {
2566 TALLOC_FREE(subreq);
2567 return false;
2570 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2572 return true;
2575 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2577 /* we do not care about the result */
2578 TALLOC_FREE(subreq);
2581 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2582 struct tevent_context *ev,
2583 struct smbXcli_conn *conn,
2584 uint16_t cmd,
2585 uint32_t additional_flags,
2586 uint32_t clear_flags,
2587 uint32_t timeout_msec,
2588 struct smbXcli_tcon *tcon,
2589 struct smbXcli_session *session,
2590 const uint8_t *fixed,
2591 uint16_t fixed_len,
2592 const uint8_t *dyn,
2593 uint32_t dyn_len,
2594 uint32_t max_dyn_len)
2596 struct tevent_req *req;
2597 struct smbXcli_req_state *state;
2598 uint32_t flags = 0;
2599 uint32_t tid = 0;
2600 uint64_t uid = 0;
2601 bool use_channel_sequence = false;
2602 uint16_t channel_sequence = 0;
2604 req = tevent_req_create(mem_ctx, &state,
2605 struct smbXcli_req_state);
2606 if (req == NULL) {
2607 return NULL;
2610 state->ev = ev;
2611 state->conn = conn;
2612 state->session = session;
2613 state->tcon = tcon;
2615 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2616 use_channel_sequence = true;
2617 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2618 use_channel_sequence = true;
2621 if (session) {
2622 uid = session->smb2->session_id;
2624 if (use_channel_sequence) {
2625 channel_sequence = session->smb2->channel_sequence;
2628 state->smb2.should_sign = session->smb2->should_sign;
2629 state->smb2.should_encrypt = session->smb2->should_encrypt;
2631 if (cmd == SMB2_OP_SESSSETUP &&
2632 session->smb2->signing_key.length != 0) {
2633 state->smb2.should_sign = true;
2636 if (cmd == SMB2_OP_SESSSETUP &&
2637 session->smb2_channel.signing_key.length == 0) {
2638 state->smb2.should_encrypt = false;
2642 if (tcon) {
2643 tid = tcon->smb2.tcon_id;
2645 if (tcon->smb2.should_encrypt) {
2646 state->smb2.should_encrypt = true;
2650 if (state->smb2.should_encrypt) {
2651 state->smb2.should_sign = false;
2654 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2655 if (state->smb2.recv_iov == NULL) {
2656 TALLOC_FREE(req);
2657 return NULL;
2660 flags |= additional_flags;
2661 flags &= ~clear_flags;
2663 state->smb2.fixed = fixed;
2664 state->smb2.fixed_len = fixed_len;
2665 state->smb2.dyn = dyn;
2666 state->smb2.dyn_len = dyn_len;
2667 state->smb2.max_dyn_len = max_dyn_len;
2669 if (state->smb2.should_encrypt) {
2670 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2671 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2674 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2675 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2676 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2677 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2678 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2679 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2680 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2681 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2683 switch (cmd) {
2684 case SMB2_OP_CANCEL:
2685 state->one_way = true;
2686 break;
2687 case SMB2_OP_BREAK:
2689 * If this is a dummy request, it will have
2690 * UINT64_MAX as message id.
2691 * If we send on break acknowledgement,
2692 * this gets overwritten later.
2694 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2695 break;
2698 if (timeout_msec > 0) {
2699 struct timeval endtime;
2701 endtime = timeval_current_ofs_msec(timeout_msec);
2702 if (!tevent_req_set_endtime(req, ev, endtime)) {
2703 return req;
2707 return req;
2710 void smb2cli_req_set_notify_async(struct tevent_req *req)
2712 struct smbXcli_req_state *state =
2713 tevent_req_data(req,
2714 struct smbXcli_req_state);
2716 state->smb2.notify_async = true;
2719 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2720 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2721 TALLOC_CTX *tmp_mem,
2722 uint8_t *inbuf);
2724 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2725 int num_reqs)
2727 struct smbXcli_req_state *state;
2728 struct tevent_req *subreq;
2729 struct iovec *iov;
2730 int i, num_iov, nbt_len;
2731 int tf_iov = -1;
2732 const DATA_BLOB *encryption_key = NULL;
2733 uint64_t encryption_session_id = 0;
2736 * 1 for the nbt length, optional TRANSFORM
2737 * per request: HDR, fixed, dyn, padding
2738 * -1 because the last one does not need padding
2741 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2742 if (iov == NULL) {
2743 return NT_STATUS_NO_MEMORY;
2746 num_iov = 1;
2747 nbt_len = 0;
2750 * the session of the first request that requires encryption
2751 * specifies the encryption key.
2753 for (i=0; i<num_reqs; i++) {
2754 if (!tevent_req_is_in_progress(reqs[i])) {
2755 return NT_STATUS_INTERNAL_ERROR;
2758 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2760 if (!smbXcli_conn_is_connected(state->conn)) {
2761 return NT_STATUS_CONNECTION_DISCONNECTED;
2764 if ((state->conn->protocol != PROTOCOL_NONE) &&
2765 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2766 return NT_STATUS_REVISION_MISMATCH;
2769 if (state->session == NULL) {
2770 continue;
2773 if (!state->smb2.should_encrypt) {
2774 continue;
2777 encryption_key = &state->session->smb2->encryption_key;
2778 if (encryption_key->length == 0) {
2779 return NT_STATUS_INVALID_PARAMETER_MIX;
2782 encryption_session_id = state->session->smb2->session_id;
2784 tf_iov = num_iov;
2785 iov[num_iov].iov_base = state->smb2.transform;
2786 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2787 num_iov += 1;
2789 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2790 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2791 state->session->smb2->nonce_low);
2792 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2793 state->session->smb2->nonce_high);
2794 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2795 encryption_session_id);
2797 state->session->smb2->nonce_low += 1;
2798 if (state->session->smb2->nonce_low == 0) {
2799 state->session->smb2->nonce_high += 1;
2800 state->session->smb2->nonce_low += 1;
2803 nbt_len += SMB2_TF_HDR_SIZE;
2804 break;
2807 for (i=0; i<num_reqs; i++) {
2808 int hdr_iov;
2809 size_t reqlen;
2810 bool ret;
2811 uint16_t opcode;
2812 uint64_t avail;
2813 uint16_t charge;
2814 uint16_t credits;
2815 uint64_t mid;
2816 const DATA_BLOB *signing_key = NULL;
2818 if (!tevent_req_is_in_progress(reqs[i])) {
2819 return NT_STATUS_INTERNAL_ERROR;
2822 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2824 if (!smbXcli_conn_is_connected(state->conn)) {
2825 return NT_STATUS_CONNECTION_DISCONNECTED;
2828 if ((state->conn->protocol != PROTOCOL_NONE) &&
2829 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2830 return NT_STATUS_REVISION_MISMATCH;
2833 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2834 if (opcode == SMB2_OP_CANCEL) {
2835 goto skip_credits;
2838 avail = UINT64_MAX - state->conn->smb2.mid;
2839 if (avail < 1) {
2840 return NT_STATUS_CONNECTION_ABORTED;
2843 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2844 uint32_t max_dyn_len = 1;
2846 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
2847 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
2849 charge = (max_dyn_len - 1)/ 65536 + 1;
2850 } else {
2851 charge = 1;
2854 charge = MAX(state->smb2.credit_charge, charge);
2856 avail = MIN(avail, state->conn->smb2.cur_credits);
2857 if (avail < charge) {
2858 return NT_STATUS_INTERNAL_ERROR;
2861 credits = 0;
2862 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2863 credits = state->conn->smb2.max_credits -
2864 state->conn->smb2.cur_credits;
2866 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2867 credits += 1;
2870 mid = state->conn->smb2.mid;
2871 state->conn->smb2.mid += charge;
2872 state->conn->smb2.cur_credits -= charge;
2874 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2875 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2877 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2878 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2880 skip_credits:
2881 if (state->session && encryption_key == NULL) {
2883 * We prefer the channel signing key if it is
2884 * already there.
2886 if (state->smb2.should_sign) {
2887 signing_key = &state->session->smb2_channel.signing_key;
2891 * If it is a channel binding, we already have the main
2892 * signing key and try that one.
2894 if (signing_key && signing_key->length == 0) {
2895 signing_key = &state->session->smb2->signing_key;
2899 * If we do not have any session key yet, we skip the
2900 * signing of SMB2_OP_SESSSETUP requests.
2902 if (signing_key && signing_key->length == 0) {
2903 signing_key = NULL;
2907 hdr_iov = num_iov;
2908 iov[num_iov].iov_base = state->smb2.hdr;
2909 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2910 num_iov += 1;
2912 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2913 iov[num_iov].iov_len = state->smb2.fixed_len;
2914 num_iov += 1;
2916 if (state->smb2.dyn != NULL) {
2917 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2918 iov[num_iov].iov_len = state->smb2.dyn_len;
2919 num_iov += 1;
2922 reqlen = sizeof(state->smb2.hdr);
2923 reqlen += state->smb2.fixed_len;
2924 reqlen += state->smb2.dyn_len;
2926 if (i < num_reqs-1) {
2927 if ((reqlen % 8) > 0) {
2928 uint8_t pad = 8 - (reqlen % 8);
2929 iov[num_iov].iov_base = state->smb2.pad;
2930 iov[num_iov].iov_len = pad;
2931 num_iov += 1;
2932 reqlen += pad;
2934 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2937 state->smb2.encryption_session_id = encryption_session_id;
2939 if (signing_key != NULL) {
2940 NTSTATUS status;
2942 status = smb2_signing_sign_pdu(*signing_key,
2943 state->session->conn->protocol,
2944 &iov[hdr_iov], num_iov - hdr_iov);
2945 if (!NT_STATUS_IS_OK(status)) {
2946 return status;
2950 nbt_len += reqlen;
2952 ret = smbXcli_req_set_pending(reqs[i]);
2953 if (!ret) {
2954 return NT_STATUS_NO_MEMORY;
2958 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2959 _smb_setlen_tcp(state->length_hdr, nbt_len);
2960 iov[0].iov_base = state->length_hdr;
2961 iov[0].iov_len = sizeof(state->length_hdr);
2963 if (encryption_key != NULL) {
2964 NTSTATUS status;
2965 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
2966 uint8_t *buf;
2967 int vi;
2969 buf = talloc_array(iov, uint8_t, buflen);
2970 if (buf == NULL) {
2971 return NT_STATUS_NO_MEMORY;
2975 * We copy the buffers before encrypting them,
2976 * this is at least currently needed for the
2977 * to keep state->smb2.hdr.
2979 * Also the callers may expect there buffers
2980 * to be const.
2982 for (vi = tf_iov + 1; vi < num_iov; vi++) {
2983 struct iovec *v = &iov[vi];
2984 const uint8_t *o = (const uint8_t *)v->iov_base;
2986 memcpy(buf, o, v->iov_len);
2987 v->iov_base = (void *)buf;
2988 buf += v->iov_len;
2991 status = smb2_signing_encrypt_pdu(*encryption_key,
2992 state->conn->protocol,
2993 &iov[tf_iov], num_iov - tf_iov);
2994 if (!NT_STATUS_IS_OK(status)) {
2995 return status;
2999 if (state->conn->dispatch_incoming == NULL) {
3000 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3003 subreq = writev_send(state, state->ev, state->conn->outgoing,
3004 state->conn->write_fd, false, iov, num_iov);
3005 if (subreq == NULL) {
3006 return NT_STATUS_NO_MEMORY;
3008 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3009 return NT_STATUS_OK;
3012 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3014 struct smbXcli_req_state *state =
3015 tevent_req_data(req,
3016 struct smbXcli_req_state);
3018 state->smb2.credit_charge = charge;
3021 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3022 struct tevent_context *ev,
3023 struct smbXcli_conn *conn,
3024 uint16_t cmd,
3025 uint32_t additional_flags,
3026 uint32_t clear_flags,
3027 uint32_t timeout_msec,
3028 struct smbXcli_tcon *tcon,
3029 struct smbXcli_session *session,
3030 const uint8_t *fixed,
3031 uint16_t fixed_len,
3032 const uint8_t *dyn,
3033 uint32_t dyn_len,
3034 uint32_t max_dyn_len)
3036 struct tevent_req *req;
3037 NTSTATUS status;
3039 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3040 additional_flags, clear_flags,
3041 timeout_msec,
3042 tcon, session,
3043 fixed, fixed_len,
3044 dyn, dyn_len,
3045 max_dyn_len);
3046 if (req == NULL) {
3047 return NULL;
3049 if (!tevent_req_is_in_progress(req)) {
3050 return tevent_req_post(req, ev);
3052 status = smb2cli_req_compound_submit(&req, 1);
3053 if (tevent_req_nterror(req, status)) {
3054 return tevent_req_post(req, ev);
3056 return req;
3059 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3061 struct tevent_req *req =
3062 tevent_req_callback_data(subreq,
3063 struct tevent_req);
3064 struct smbXcli_req_state *state =
3065 tevent_req_data(req,
3066 struct smbXcli_req_state);
3067 ssize_t nwritten;
3068 int err;
3070 nwritten = writev_recv(subreq, &err);
3071 TALLOC_FREE(subreq);
3072 if (nwritten == -1) {
3073 /* here, we need to notify all pending requests */
3074 NTSTATUS status = map_nt_error_from_unix_common(err);
3075 smbXcli_conn_disconnect(state->conn, status);
3076 return;
3080 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3081 uint8_t *buf,
3082 size_t buflen,
3083 TALLOC_CTX *mem_ctx,
3084 struct iovec **piov, int *pnum_iov)
3086 struct iovec *iov;
3087 int num_iov = 0;
3088 size_t taken = 0;
3089 uint8_t *first_hdr = buf;
3090 size_t verified_buflen = 0;
3091 uint8_t *tf = NULL;
3092 size_t tf_len = 0;
3094 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3095 if (iov == NULL) {
3096 return NT_STATUS_NO_MEMORY;
3099 while (taken < buflen) {
3100 size_t len = buflen - taken;
3101 uint8_t *hdr = first_hdr + taken;
3102 struct iovec *cur;
3103 size_t full_size;
3104 size_t next_command_ofs;
3105 uint16_t body_size;
3106 struct iovec *iov_tmp;
3108 if (verified_buflen > taken) {
3109 len = verified_buflen - taken;
3110 } else {
3111 tf = NULL;
3112 tf_len = 0;
3115 if (len < 4) {
3116 DEBUG(10, ("%d bytes left, expected at least %d\n",
3117 (int)len, 4));
3118 goto inval;
3120 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3121 struct smbXcli_session *s;
3122 uint64_t uid;
3123 struct iovec tf_iov[2];
3124 size_t enc_len;
3125 NTSTATUS status;
3127 if (len < SMB2_TF_HDR_SIZE) {
3128 DEBUG(10, ("%d bytes left, expected at least %d\n",
3129 (int)len, SMB2_TF_HDR_SIZE));
3130 goto inval;
3132 tf = hdr;
3133 tf_len = SMB2_TF_HDR_SIZE;
3134 taken += tf_len;
3136 hdr = first_hdr + taken;
3137 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3138 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3140 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3141 DEBUG(10, ("%d bytes left, expected at least %d\n",
3142 (int)len,
3143 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3144 goto inval;
3147 s = conn->sessions;
3148 for (; s; s = s->next) {
3149 if (s->smb2->session_id != uid) {
3150 continue;
3152 break;
3155 if (s == NULL) {
3156 DEBUG(10, ("unknown session_id %llu\n",
3157 (unsigned long long)uid));
3158 goto inval;
3161 tf_iov[0].iov_base = (void *)tf;
3162 tf_iov[0].iov_len = tf_len;
3163 tf_iov[1].iov_base = (void *)hdr;
3164 tf_iov[1].iov_len = enc_len;
3166 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3167 conn->protocol,
3168 tf_iov, 2);
3169 if (!NT_STATUS_IS_OK(status)) {
3170 TALLOC_FREE(iov);
3171 return status;
3174 verified_buflen = taken + enc_len;
3175 len = enc_len;
3179 * We need the header plus the body length field
3182 if (len < SMB2_HDR_BODY + 2) {
3183 DEBUG(10, ("%d bytes left, expected at least %d\n",
3184 (int)len, SMB2_HDR_BODY));
3185 goto inval;
3187 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3188 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3189 IVAL(hdr, 0)));
3190 goto inval;
3192 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3193 DEBUG(10, ("Got HDR len %d, expected %d\n",
3194 SVAL(hdr, 4), SMB2_HDR_BODY));
3195 goto inval;
3198 full_size = len;
3199 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3200 body_size = SVAL(hdr, SMB2_HDR_BODY);
3202 if (next_command_ofs != 0) {
3203 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3204 goto inval;
3206 if (next_command_ofs > full_size) {
3207 goto inval;
3209 full_size = next_command_ofs;
3211 if (body_size < 2) {
3212 goto inval;
3214 body_size &= 0xfffe;
3216 if (body_size > (full_size - SMB2_HDR_BODY)) {
3217 goto inval;
3220 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3221 num_iov + 4);
3222 if (iov_tmp == NULL) {
3223 TALLOC_FREE(iov);
3224 return NT_STATUS_NO_MEMORY;
3226 iov = iov_tmp;
3227 cur = &iov[num_iov];
3228 num_iov += 4;
3230 cur[0].iov_base = tf;
3231 cur[0].iov_len = tf_len;
3232 cur[1].iov_base = hdr;
3233 cur[1].iov_len = SMB2_HDR_BODY;
3234 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3235 cur[2].iov_len = body_size;
3236 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3237 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3239 taken += full_size;
3242 *piov = iov;
3243 *pnum_iov = num_iov;
3244 return NT_STATUS_OK;
3246 inval:
3247 TALLOC_FREE(iov);
3248 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3251 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3252 uint64_t mid)
3254 size_t num_pending = talloc_array_length(conn->pending);
3255 size_t i;
3257 for (i=0; i<num_pending; i++) {
3258 struct tevent_req *req = conn->pending[i];
3259 struct smbXcli_req_state *state =
3260 tevent_req_data(req,
3261 struct smbXcli_req_state);
3263 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3264 return req;
3267 return NULL;
3270 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3271 TALLOC_CTX *tmp_mem,
3272 uint8_t *inbuf)
3274 struct tevent_req *req;
3275 struct smbXcli_req_state *state = NULL;
3276 struct iovec *iov;
3277 int i, num_iov;
3278 NTSTATUS status;
3279 bool defer = true;
3280 struct smbXcli_session *last_session = NULL;
3281 size_t inbuf_len = smb_len_tcp(inbuf);
3283 status = smb2cli_inbuf_parse_compound(conn,
3284 inbuf + NBT_HDR_SIZE,
3285 inbuf_len,
3286 tmp_mem,
3287 &iov, &num_iov);
3288 if (!NT_STATUS_IS_OK(status)) {
3289 return status;
3292 for (i=0; i<num_iov; i+=4) {
3293 uint8_t *inbuf_ref = NULL;
3294 struct iovec *cur = &iov[i];
3295 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3296 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3297 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3298 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3299 uint16_t req_opcode;
3300 uint32_t req_flags;
3301 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3302 uint32_t new_credits;
3303 struct smbXcli_session *session = NULL;
3304 const DATA_BLOB *signing_key = NULL;
3305 bool was_encrypted = false;
3307 new_credits = conn->smb2.cur_credits;
3308 new_credits += credits;
3309 if (new_credits > UINT16_MAX) {
3310 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3312 conn->smb2.cur_credits += credits;
3314 req = smb2cli_conn_find_pending(conn, mid);
3315 if (req == NULL) {
3316 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3318 state = tevent_req_data(req, struct smbXcli_req_state);
3320 state->smb2.got_async = false;
3322 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3323 if (opcode != req_opcode) {
3324 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3326 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3328 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3329 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3332 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3333 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3334 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3335 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3338 * async interim responses are not signed,
3339 * even if the SMB2_HDR_FLAG_SIGNED flag
3340 * is set.
3342 req_flags |= SMB2_HDR_FLAG_ASYNC;
3343 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3344 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3346 if (state->smb2.notify_async) {
3347 state->smb2.got_async = true;
3348 tevent_req_defer_callback(req, state->ev);
3349 tevent_req_notify_callback(req);
3351 continue;
3354 session = state->session;
3355 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3356 session = last_session;
3358 last_session = session;
3360 if (state->smb2.should_sign) {
3361 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3362 return NT_STATUS_ACCESS_DENIED;
3366 if (flags & SMB2_HDR_FLAG_SIGNED) {
3367 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3369 if (session == NULL) {
3370 struct smbXcli_session *s;
3372 s = state->conn->sessions;
3373 for (; s; s = s->next) {
3374 if (s->smb2->session_id != uid) {
3375 continue;
3378 session = s;
3379 break;
3383 if (session == NULL) {
3384 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3387 last_session = session;
3388 signing_key = &session->smb2_channel.signing_key;
3391 if (opcode == SMB2_OP_SESSSETUP) {
3393 * We prefer the channel signing key, if it is
3394 * already there.
3396 * If we do not have a channel signing key yet,
3397 * we try the main signing key, if it is not
3398 * the final response.
3400 if (signing_key && signing_key->length == 0 &&
3401 !NT_STATUS_IS_OK(status)) {
3402 signing_key = &session->smb2->signing_key;
3405 if (signing_key && signing_key->length == 0) {
3407 * If we do not have a session key to
3408 * verify the signature, we defer the
3409 * signing check to the caller.
3411 * The caller gets NT_STATUS_OK, it
3412 * has to call
3413 * smb2cli_session_set_session_key()
3414 * or
3415 * smb2cli_session_set_channel_key()
3416 * which will check the signature
3417 * with the channel signing key.
3419 signing_key = NULL;
3423 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3424 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3425 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3428 * If the response was encrypted in a SMB2_TRANSFORM
3429 * pdu, which belongs to the correct session,
3430 * we do not need to do signing checks
3432 * It could be the session the response belongs to
3433 * or the session that was used to encrypt the
3434 * SMB2_TRANSFORM request.
3436 if ((session && session->smb2->session_id == uid) ||
3437 (state->smb2.encryption_session_id == uid)) {
3438 signing_key = NULL;
3439 was_encrypted = true;
3443 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3445 * if the server returns NT_STATUS_USER_SESSION_DELETED
3446 * the response is not signed and we should
3447 * propagate the NT_STATUS_USER_SESSION_DELETED
3448 * status to the caller.
3450 state->smb2.signing_skipped = true;
3451 signing_key = NULL;
3454 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3456 * if the server returns
3457 * NT_STATUS_INVALID_PARAMETER
3458 * the response might not be encrypted.
3460 if (state->smb2.should_encrypt && !was_encrypted) {
3461 state->smb2.signing_skipped = true;
3462 signing_key = NULL;
3466 if (state->smb2.should_encrypt && !was_encrypted) {
3467 if (!state->smb2.signing_skipped) {
3468 return NT_STATUS_ACCESS_DENIED;
3472 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3473 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3474 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3476 * if the server returns
3477 * NT_STATUS_NETWORK_NAME_DELETED
3478 * NT_STATUS_FILE_CLOSED
3479 * NT_STATUS_INVALID_PARAMETER
3480 * the response might not be signed
3481 * as this happens before the signing checks.
3483 * If server echos the signature (or all zeros)
3484 * we should report the status from the server
3485 * to the caller.
3487 if (signing_key) {
3488 int cmp;
3490 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3491 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3492 16);
3493 if (cmp == 0) {
3494 state->smb2.signing_skipped = true;
3495 signing_key = NULL;
3498 if (signing_key) {
3499 int cmp;
3500 static const uint8_t zeros[16];
3502 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3503 zeros,
3504 16);
3505 if (cmp == 0) {
3506 state->smb2.signing_skipped = true;
3507 signing_key = NULL;
3512 if (signing_key) {
3513 status = smb2_signing_check_pdu(*signing_key,
3514 state->conn->protocol,
3515 &cur[1], 3);
3516 if (!NT_STATUS_IS_OK(status)) {
3518 * If the signing check fails, we disconnect
3519 * the connection.
3521 return status;
3525 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3526 (session != NULL) && session->disconnect_expired)
3529 * this should be a short term hack
3530 * until the upper layers have implemented
3531 * re-authentication.
3533 return status;
3536 smbXcli_req_unset_pending(req);
3539 * There might be more than one response
3540 * we need to defer the notifications
3542 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3543 defer = false;
3546 if (defer) {
3547 tevent_req_defer_callback(req, state->ev);
3551 * Note: here we use talloc_reference() in a way
3552 * that does not expose it to the caller.
3554 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3555 if (tevent_req_nomem(inbuf_ref, req)) {
3556 continue;
3559 /* copy the related buffers */
3560 state->smb2.recv_iov[0] = cur[1];
3561 state->smb2.recv_iov[1] = cur[2];
3562 state->smb2.recv_iov[2] = cur[3];
3564 tevent_req_done(req);
3567 if (defer) {
3568 return NT_STATUS_RETRY;
3571 return NT_STATUS_OK;
3574 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3575 struct iovec **piov,
3576 const struct smb2cli_req_expected_response *expected,
3577 size_t num_expected)
3579 struct smbXcli_req_state *state =
3580 tevent_req_data(req,
3581 struct smbXcli_req_state);
3582 NTSTATUS status;
3583 size_t body_size;
3584 bool found_status = false;
3585 bool found_size = false;
3586 size_t i;
3588 if (piov != NULL) {
3589 *piov = NULL;
3592 if (state->smb2.got_async) {
3593 return STATUS_PENDING;
3596 if (tevent_req_is_nterror(req, &status)) {
3597 for (i=0; i < num_expected; i++) {
3598 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3599 found_status = true;
3600 break;
3604 if (found_status) {
3605 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3608 return status;
3611 if (num_expected == 0) {
3612 found_status = true;
3613 found_size = true;
3616 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3617 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3619 for (i=0; i < num_expected; i++) {
3620 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3621 continue;
3624 found_status = true;
3625 if (expected[i].body_size == 0) {
3626 found_size = true;
3627 break;
3630 if (expected[i].body_size == body_size) {
3631 found_size = true;
3632 break;
3636 if (!found_status) {
3637 return status;
3640 if (state->smb2.signing_skipped) {
3641 if (num_expected > 0) {
3642 return NT_STATUS_ACCESS_DENIED;
3644 if (!NT_STATUS_IS_ERR(status)) {
3645 return NT_STATUS_ACCESS_DENIED;
3649 if (!found_size) {
3650 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3653 if (piov != NULL) {
3654 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3657 return status;
3660 static const struct {
3661 enum protocol_types proto;
3662 const char *smb1_name;
3663 } smb1cli_prots[] = {
3664 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3665 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3666 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3667 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3668 {PROTOCOL_LANMAN2, "LM1.2X002"},
3669 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3670 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3671 {PROTOCOL_LANMAN2, "Samba"},
3672 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3673 {PROTOCOL_NT1, "NT LM 0.12"},
3674 {PROTOCOL_SMB2_02, "SMB 2.002"},
3675 {PROTOCOL_SMB2_10, "SMB 2.???"},
3678 static const struct {
3679 enum protocol_types proto;
3680 uint16_t smb2_dialect;
3681 } smb2cli_prots[] = {
3682 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3683 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3684 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3685 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3686 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3689 struct smbXcli_negprot_state {
3690 struct smbXcli_conn *conn;
3691 struct tevent_context *ev;
3692 uint32_t timeout_msec;
3693 enum protocol_types min_protocol;
3694 enum protocol_types max_protocol;
3696 struct {
3697 uint8_t fixed[36];
3698 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3699 } smb2;
3702 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3703 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3704 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3705 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3706 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3707 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3708 TALLOC_CTX *frame,
3709 uint8_t *inbuf);
3711 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3712 struct tevent_context *ev,
3713 struct smbXcli_conn *conn,
3714 uint32_t timeout_msec,
3715 enum protocol_types min_protocol,
3716 enum protocol_types max_protocol)
3718 struct tevent_req *req, *subreq;
3719 struct smbXcli_negprot_state *state;
3721 req = tevent_req_create(mem_ctx, &state,
3722 struct smbXcli_negprot_state);
3723 if (req == NULL) {
3724 return NULL;
3726 state->conn = conn;
3727 state->ev = ev;
3728 state->timeout_msec = timeout_msec;
3729 state->min_protocol = min_protocol;
3730 state->max_protocol = max_protocol;
3732 if (min_protocol == PROTOCOL_NONE) {
3733 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3734 return tevent_req_post(req, ev);
3737 if (max_protocol == PROTOCOL_NONE) {
3738 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3739 return tevent_req_post(req, ev);
3742 if (min_protocol > max_protocol) {
3743 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3744 return tevent_req_post(req, ev);
3747 if ((min_protocol < PROTOCOL_SMB2_02) &&
3748 (max_protocol < PROTOCOL_SMB2_02)) {
3750 * SMB1 only...
3752 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3754 subreq = smbXcli_negprot_smb1_subreq(state);
3755 if (tevent_req_nomem(subreq, req)) {
3756 return tevent_req_post(req, ev);
3758 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3759 return req;
3762 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3763 (max_protocol >= PROTOCOL_SMB2_02)) {
3765 * SMB2 only...
3767 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3769 subreq = smbXcli_negprot_smb2_subreq(state);
3770 if (tevent_req_nomem(subreq, req)) {
3771 return tevent_req_post(req, ev);
3773 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3774 return req;
3778 * We send an SMB1 negprot with the SMB2 dialects
3779 * and expect a SMB1 or a SMB2 response.
3781 * smbXcli_negprot_dispatch_incoming() will fix the
3782 * callback to match protocol of the response.
3784 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3786 subreq = smbXcli_negprot_smb1_subreq(state);
3787 if (tevent_req_nomem(subreq, req)) {
3788 return tevent_req_post(req, ev);
3790 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3791 return req;
3794 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3796 struct tevent_req *req =
3797 tevent_req_callback_data(subreq,
3798 struct tevent_req);
3799 NTSTATUS status;
3802 * we just want the low level error
3804 status = tevent_req_simple_recv_ntstatus(subreq);
3805 TALLOC_FREE(subreq);
3806 if (tevent_req_nterror(req, status)) {
3807 return;
3810 /* this should never happen */
3811 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3814 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3816 size_t i;
3817 DATA_BLOB bytes = data_blob_null;
3818 uint8_t flags;
3819 uint16_t flags2;
3821 /* setup the protocol strings */
3822 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3823 uint8_t c = 2;
3824 bool ok;
3826 if (smb1cli_prots[i].proto < state->min_protocol) {
3827 continue;
3830 if (smb1cli_prots[i].proto > state->max_protocol) {
3831 continue;
3834 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3835 if (!ok) {
3836 return NULL;
3840 * We now it is already ascii and
3841 * we want NULL termination.
3843 ok = data_blob_append(state, &bytes,
3844 smb1cli_prots[i].smb1_name,
3845 strlen(smb1cli_prots[i].smb1_name)+1);
3846 if (!ok) {
3847 return NULL;
3851 smb1cli_req_flags(state->max_protocol,
3852 state->conn->smb1.client.capabilities,
3853 SMBnegprot,
3854 0, 0, &flags,
3855 0, 0, &flags2);
3857 return smb1cli_req_send(state, state->ev, state->conn,
3858 SMBnegprot,
3859 flags, ~flags,
3860 flags2, ~flags2,
3861 state->timeout_msec,
3862 0xFFFE, 0, NULL, /* pid, tid, session */
3863 0, NULL, /* wct, vwv */
3864 bytes.length, bytes.data);
3867 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3869 struct tevent_req *req =
3870 tevent_req_callback_data(subreq,
3871 struct tevent_req);
3872 struct smbXcli_negprot_state *state =
3873 tevent_req_data(req,
3874 struct smbXcli_negprot_state);
3875 struct smbXcli_conn *conn = state->conn;
3876 struct iovec *recv_iov = NULL;
3877 uint8_t *inhdr;
3878 uint8_t wct;
3879 uint16_t *vwv;
3880 uint32_t num_bytes;
3881 uint8_t *bytes;
3882 NTSTATUS status;
3883 uint16_t protnum;
3884 size_t i;
3885 size_t num_prots = 0;
3886 uint8_t flags;
3887 uint32_t client_capabilities = conn->smb1.client.capabilities;
3888 uint32_t both_capabilities;
3889 uint32_t server_capabilities = 0;
3890 uint32_t capabilities;
3891 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3892 uint32_t server_max_xmit = 0;
3893 uint32_t max_xmit;
3894 uint32_t server_max_mux = 0;
3895 uint16_t server_security_mode = 0;
3896 uint32_t server_session_key = 0;
3897 bool server_readbraw = false;
3898 bool server_writebraw = false;
3899 bool server_lockread = false;
3900 bool server_writeunlock = false;
3901 struct GUID server_guid = GUID_zero();
3902 DATA_BLOB server_gss_blob = data_blob_null;
3903 uint8_t server_challenge[8];
3904 char *server_workgroup = NULL;
3905 char *server_name = NULL;
3906 int server_time_zone = 0;
3907 NTTIME server_system_time = 0;
3908 static const struct smb1cli_req_expected_response expected[] = {
3910 .status = NT_STATUS_OK,
3911 .wct = 0x11, /* NT1 */
3914 .status = NT_STATUS_OK,
3915 .wct = 0x0D, /* LM */
3918 .status = NT_STATUS_OK,
3919 .wct = 0x01, /* CORE */
3923 ZERO_STRUCT(server_challenge);
3925 status = smb1cli_req_recv(subreq, state,
3926 &recv_iov,
3927 &inhdr,
3928 &wct,
3929 &vwv,
3930 NULL, /* pvwv_offset */
3931 &num_bytes,
3932 &bytes,
3933 NULL, /* pbytes_offset */
3934 NULL, /* pinbuf */
3935 expected, ARRAY_SIZE(expected));
3936 TALLOC_FREE(subreq);
3937 if (tevent_req_nterror(req, status)) {
3938 return;
3941 flags = CVAL(inhdr, HDR_FLG);
3943 protnum = SVAL(vwv, 0);
3945 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3946 if (smb1cli_prots[i].proto < state->min_protocol) {
3947 continue;
3950 if (smb1cli_prots[i].proto > state->max_protocol) {
3951 continue;
3954 if (protnum != num_prots) {
3955 num_prots++;
3956 continue;
3959 conn->protocol = smb1cli_prots[i].proto;
3960 break;
3963 if (conn->protocol == PROTOCOL_NONE) {
3964 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3965 return;
3968 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3969 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3970 "and the selected protocol level doesn't support it.\n"));
3971 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3972 return;
3975 if (flags & FLAG_SUPPORT_LOCKREAD) {
3976 server_lockread = true;
3977 server_writeunlock = true;
3980 if (conn->protocol >= PROTOCOL_NT1) {
3981 const char *client_signing = NULL;
3982 bool server_mandatory = false;
3983 bool server_allowed = false;
3984 const char *server_signing = NULL;
3985 bool ok;
3986 uint8_t key_len;
3988 if (wct != 0x11) {
3989 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3990 return;
3993 /* NT protocol */
3994 server_security_mode = CVAL(vwv + 1, 0);
3995 server_max_mux = SVAL(vwv + 1, 1);
3996 server_max_xmit = IVAL(vwv + 3, 1);
3997 server_session_key = IVAL(vwv + 7, 1);
3998 server_time_zone = SVALS(vwv + 15, 1);
3999 server_time_zone *= 60;
4000 /* this time arrives in real GMT */
4001 server_system_time = BVAL(vwv + 11, 1);
4002 server_capabilities = IVAL(vwv + 9, 1);
4004 key_len = CVAL(vwv + 16, 1);
4006 if (server_capabilities & CAP_RAW_MODE) {
4007 server_readbraw = true;
4008 server_writebraw = true;
4010 if (server_capabilities & CAP_LOCK_AND_READ) {
4011 server_lockread = true;
4014 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4015 DATA_BLOB blob1, blob2;
4017 if (num_bytes < 16) {
4018 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4019 return;
4022 blob1 = data_blob_const(bytes, 16);
4023 status = GUID_from_data_blob(&blob1, &server_guid);
4024 if (tevent_req_nterror(req, status)) {
4025 return;
4028 blob1 = data_blob_const(bytes+16, num_bytes-16);
4029 blob2 = data_blob_dup_talloc(state, blob1);
4030 if (blob1.length > 0 &&
4031 tevent_req_nomem(blob2.data, req)) {
4032 return;
4034 server_gss_blob = blob2;
4035 } else {
4036 DATA_BLOB blob1, blob2;
4038 if (num_bytes < key_len) {
4039 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4040 return;
4043 if (key_len != 0 && key_len != 8) {
4044 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4045 return;
4048 if (key_len == 8) {
4049 memcpy(server_challenge, bytes, 8);
4052 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4053 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4054 if (blob1.length > 0) {
4055 size_t len;
4057 len = utf16_len_n(blob1.data,
4058 blob1.length);
4059 blob1.length = len;
4061 ok = convert_string_talloc(state,
4062 CH_UTF16LE,
4063 CH_UNIX,
4064 blob1.data,
4065 blob1.length,
4066 &server_workgroup,
4067 &len);
4068 if (!ok) {
4069 status = map_nt_error_from_unix_common(errno);
4070 tevent_req_nterror(req, status);
4071 return;
4075 blob2.data += blob1.length;
4076 blob2.length -= blob1.length;
4077 if (blob2.length > 0) {
4078 size_t len;
4080 len = utf16_len_n(blob1.data,
4081 blob1.length);
4082 blob1.length = len;
4084 ok = convert_string_talloc(state,
4085 CH_UTF16LE,
4086 CH_UNIX,
4087 blob2.data,
4088 blob2.length,
4089 &server_name,
4090 &len);
4091 if (!ok) {
4092 status = map_nt_error_from_unix_common(errno);
4093 tevent_req_nterror(req, status);
4094 return;
4099 client_signing = "disabled";
4100 if (conn->allow_signing) {
4101 client_signing = "allowed";
4103 if (conn->mandatory_signing) {
4104 client_signing = "required";
4107 server_signing = "not supported";
4108 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4109 server_signing = "supported";
4110 server_allowed = true;
4111 } else if (conn->mandatory_signing) {
4113 * We have mandatory signing as client
4114 * lets assume the server will look at our
4115 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4116 * flag in the session setup
4118 server_signing = "not announced";
4119 server_allowed = true;
4121 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4122 server_signing = "required";
4123 server_mandatory = true;
4126 ok = smb_signing_set_negotiated(conn->smb1.signing,
4127 server_allowed,
4128 server_mandatory);
4129 if (!ok) {
4130 DEBUG(1,("cli_negprot: SMB signing is required, "
4131 "but client[%s] and server[%s] mismatch\n",
4132 client_signing, server_signing));
4133 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4134 return;
4137 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4138 DATA_BLOB blob1;
4139 uint8_t key_len;
4140 time_t t;
4142 if (wct != 0x0D) {
4143 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4144 return;
4147 server_security_mode = SVAL(vwv + 1, 0);
4148 server_max_xmit = SVAL(vwv + 2, 0);
4149 server_max_mux = SVAL(vwv + 3, 0);
4150 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4151 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4152 server_session_key = IVAL(vwv + 6, 0);
4153 server_time_zone = SVALS(vwv + 10, 0);
4154 server_time_zone *= 60;
4155 /* this time is converted to GMT by make_unix_date */
4156 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4157 unix_to_nt_time(&server_system_time, t);
4158 key_len = SVAL(vwv + 11, 0);
4160 if (num_bytes < key_len) {
4161 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4162 return;
4165 if (key_len != 0 && key_len != 8) {
4166 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4167 return;
4170 if (key_len == 8) {
4171 memcpy(server_challenge, bytes, 8);
4174 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4175 if (blob1.length > 0) {
4176 size_t len;
4177 bool ok;
4179 len = utf16_len_n(blob1.data,
4180 blob1.length);
4181 blob1.length = len;
4183 ok = convert_string_talloc(state,
4184 CH_DOS,
4185 CH_UNIX,
4186 blob1.data,
4187 blob1.length,
4188 &server_workgroup,
4189 &len);
4190 if (!ok) {
4191 status = map_nt_error_from_unix_common(errno);
4192 tevent_req_nterror(req, status);
4193 return;
4197 } else {
4198 /* the old core protocol */
4199 server_time_zone = get_time_zone(time(NULL));
4200 server_max_xmit = 1024;
4201 server_max_mux = 1;
4204 if (server_max_xmit < 1024) {
4205 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4206 return;
4209 if (server_max_mux < 1) {
4210 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4211 return;
4215 * Now calculate the negotiated capabilities
4216 * based on the mask for:
4217 * - client only flags
4218 * - flags used in both directions
4219 * - server only flags
4221 both_capabilities = client_capabilities & server_capabilities;
4222 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4223 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4224 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4226 max_xmit = MIN(client_max_xmit, server_max_xmit);
4228 conn->smb1.server.capabilities = server_capabilities;
4229 conn->smb1.capabilities = capabilities;
4231 conn->smb1.server.max_xmit = server_max_xmit;
4232 conn->smb1.max_xmit = max_xmit;
4234 conn->smb1.server.max_mux = server_max_mux;
4236 conn->smb1.server.security_mode = server_security_mode;
4238 conn->smb1.server.readbraw = server_readbraw;
4239 conn->smb1.server.writebraw = server_writebraw;
4240 conn->smb1.server.lockread = server_lockread;
4241 conn->smb1.server.writeunlock = server_writeunlock;
4243 conn->smb1.server.session_key = server_session_key;
4245 talloc_steal(conn, server_gss_blob.data);
4246 conn->smb1.server.gss_blob = server_gss_blob;
4247 conn->smb1.server.guid = server_guid;
4248 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4249 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4250 conn->smb1.server.name = talloc_move(conn, &server_name);
4252 conn->smb1.server.time_zone = server_time_zone;
4253 conn->smb1.server.system_time = server_system_time;
4255 tevent_req_done(req);
4258 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4260 size_t i;
4261 uint8_t *buf;
4262 uint16_t dialect_count = 0;
4264 buf = state->smb2.dyn;
4265 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4266 if (smb2cli_prots[i].proto < state->min_protocol) {
4267 continue;
4270 if (smb2cli_prots[i].proto > state->max_protocol) {
4271 continue;
4274 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
4275 dialect_count++;
4278 buf = state->smb2.fixed;
4279 SSVAL(buf, 0, 36);
4280 SSVAL(buf, 2, dialect_count);
4281 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4282 SSVAL(buf, 6, 0); /* Reserved */
4283 if (state->max_protocol >= PROTOCOL_SMB2_22) {
4284 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4285 } else {
4286 SIVAL(buf, 8, 0); /* Capabilities */
4288 if (state->max_protocol >= PROTOCOL_SMB2_10) {
4289 NTSTATUS status;
4290 DATA_BLOB blob;
4292 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4293 state, &blob);
4294 if (!NT_STATUS_IS_OK(status)) {
4295 return NULL;
4297 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4298 } else {
4299 memset(buf+12, 0, 16); /* ClientGuid */
4301 SBVAL(buf, 28, 0); /* ClientStartTime */
4303 return smb2cli_req_send(state, state->ev,
4304 state->conn, SMB2_OP_NEGPROT,
4305 0, 0, /* flags */
4306 state->timeout_msec,
4307 NULL, NULL, /* tcon, session */
4308 state->smb2.fixed, sizeof(state->smb2.fixed),
4309 state->smb2.dyn, dialect_count*2,
4310 UINT16_MAX); /* max_dyn_len */
4313 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4315 struct tevent_req *req =
4316 tevent_req_callback_data(subreq,
4317 struct tevent_req);
4318 struct smbXcli_negprot_state *state =
4319 tevent_req_data(req,
4320 struct smbXcli_negprot_state);
4321 struct smbXcli_conn *conn = state->conn;
4322 size_t security_offset, security_length;
4323 DATA_BLOB blob;
4324 NTSTATUS status;
4325 struct iovec *iov;
4326 uint8_t *body;
4327 size_t i;
4328 uint16_t dialect_revision;
4329 static const struct smb2cli_req_expected_response expected[] = {
4331 .status = NT_STATUS_OK,
4332 .body_size = 0x41
4336 status = smb2cli_req_recv(subreq, state, &iov,
4337 expected, ARRAY_SIZE(expected));
4338 TALLOC_FREE(subreq);
4339 if (tevent_req_nterror(req, status)) {
4340 return;
4343 body = (uint8_t *)iov[1].iov_base;
4345 dialect_revision = SVAL(body, 4);
4347 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4348 if (smb2cli_prots[i].proto < state->min_protocol) {
4349 continue;
4352 if (smb2cli_prots[i].proto > state->max_protocol) {
4353 continue;
4356 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4357 continue;
4360 conn->protocol = smb2cli_prots[i].proto;
4361 break;
4364 if (conn->protocol == PROTOCOL_NONE) {
4365 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4366 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4367 return;
4370 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4371 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4372 return;
4375 /* make sure we do not loop forever */
4376 state->min_protocol = PROTOCOL_SMB2_02;
4379 * send a SMB2 negprot, in order to negotiate
4380 * the SMB2 dialect.
4382 subreq = smbXcli_negprot_smb2_subreq(state);
4383 if (tevent_req_nomem(subreq, req)) {
4384 return;
4386 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4387 return;
4390 conn->smb2.server.security_mode = SVAL(body, 2);
4392 blob = data_blob_const(body + 8, 16);
4393 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4394 if (tevent_req_nterror(req, status)) {
4395 return;
4398 conn->smb2.server.capabilities = IVAL(body, 24);
4399 conn->smb2.server.max_trans_size= IVAL(body, 28);
4400 conn->smb2.server.max_read_size = IVAL(body, 32);
4401 conn->smb2.server.max_write_size= IVAL(body, 36);
4402 conn->smb2.server.system_time = BVAL(body, 40);
4403 conn->smb2.server.start_time = BVAL(body, 48);
4405 security_offset = SVAL(body, 56);
4406 security_length = SVAL(body, 58);
4408 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4409 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4410 return;
4413 if (security_length > iov[2].iov_len) {
4414 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4415 return;
4418 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4419 iov[2].iov_base,
4420 security_length);
4421 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4422 return;
4425 tevent_req_done(req);
4428 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4429 TALLOC_CTX *tmp_mem,
4430 uint8_t *inbuf)
4432 size_t num_pending = talloc_array_length(conn->pending);
4433 struct tevent_req *subreq;
4434 struct smbXcli_req_state *substate;
4435 struct tevent_req *req;
4436 uint32_t protocol_magic;
4437 size_t inbuf_len = smb_len_nbt(inbuf);
4439 if (num_pending != 1) {
4440 return NT_STATUS_INTERNAL_ERROR;
4443 if (inbuf_len < 4) {
4444 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4447 subreq = conn->pending[0];
4448 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4449 req = tevent_req_callback_data(subreq, struct tevent_req);
4451 protocol_magic = IVAL(inbuf, 4);
4453 switch (protocol_magic) {
4454 case SMB_MAGIC:
4455 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4456 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4457 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4459 case SMB2_MAGIC:
4460 if (substate->smb2.recv_iov == NULL) {
4462 * For the SMB1 negprot we have move it.
4464 substate->smb2.recv_iov = substate->smb1.recv_iov;
4465 substate->smb1.recv_iov = NULL;
4469 * we got an SMB2 answer, which consumed sequence number 0
4470 * so we need to use 1 as the next one.
4472 * we also need to set the current credits to 0
4473 * as we consumed the initial one. The SMB2 answer
4474 * hopefully grant us a new credit.
4476 conn->smb2.mid = 1;
4477 conn->smb2.cur_credits = 0;
4478 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4479 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4480 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4483 DEBUG(10, ("Got non-SMB PDU\n"));
4484 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4487 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4489 return tevent_req_simple_recv_ntstatus(req);
4492 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4493 uint32_t timeout_msec,
4494 enum protocol_types min_protocol,
4495 enum protocol_types max_protocol)
4497 TALLOC_CTX *frame = talloc_stackframe();
4498 struct tevent_context *ev;
4499 struct tevent_req *req;
4500 NTSTATUS status = NT_STATUS_NO_MEMORY;
4501 bool ok;
4503 if (smbXcli_conn_has_async_calls(conn)) {
4505 * Can't use sync call while an async call is in flight
4507 status = NT_STATUS_INVALID_PARAMETER_MIX;
4508 goto fail;
4510 ev = samba_tevent_context_init(frame);
4511 if (ev == NULL) {
4512 goto fail;
4514 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4515 min_protocol, max_protocol);
4516 if (req == NULL) {
4517 goto fail;
4519 ok = tevent_req_poll(req, ev);
4520 if (!ok) {
4521 status = map_nt_error_from_unix_common(errno);
4522 goto fail;
4524 status = smbXcli_negprot_recv(req);
4525 fail:
4526 TALLOC_FREE(frame);
4527 return status;
4530 static int smbXcli_session_destructor(struct smbXcli_session *session)
4532 if (session->conn == NULL) {
4533 return 0;
4536 DLIST_REMOVE(session->conn->sessions, session);
4537 return 0;
4540 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4541 struct smbXcli_conn *conn)
4543 struct smbXcli_session *session;
4545 session = talloc_zero(mem_ctx, struct smbXcli_session);
4546 if (session == NULL) {
4547 return NULL;
4549 session->smb2 = talloc_zero(session, struct smb2cli_session);
4550 if (session->smb2 == NULL) {
4551 talloc_free(session);
4552 return NULL;
4554 talloc_set_destructor(session, smbXcli_session_destructor);
4556 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4557 session->conn = conn;
4559 return session;
4562 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
4563 struct smbXcli_session *src)
4565 struct smbXcli_session *session;
4567 session = talloc_zero(mem_ctx, struct smbXcli_session);
4568 if (session == NULL) {
4569 return NULL;
4571 session->smb2 = talloc_zero(session, struct smb2cli_session);
4572 if (session->smb2 == NULL) {
4573 talloc_free(session);
4574 return NULL;
4577 session->conn = src->conn;
4578 *session->smb2 = *src->smb2;
4579 session->smb2_channel = src->smb2_channel;
4580 session->disconnect_expired = src->disconnect_expired;
4582 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
4583 talloc_set_destructor(session, smbXcli_session_destructor);
4585 return session;
4589 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
4590 TALLOC_CTX *mem_ctx,
4591 DATA_BLOB *key)
4593 const DATA_BLOB *application_key;
4595 *key = data_blob_null;
4597 if (session->conn == NULL) {
4598 return NT_STATUS_NO_USER_SESSION_KEY;
4601 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
4602 application_key = &session->smb2->application_key;
4603 } else {
4604 application_key = &session->smb1.application_key;
4607 if (application_key->length == 0) {
4608 return NT_STATUS_NO_USER_SESSION_KEY;
4611 *key = data_blob_dup_talloc(mem_ctx, *application_key);
4612 if (key->data == NULL) {
4613 return NT_STATUS_NO_MEMORY;
4616 return NT_STATUS_OK;
4619 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
4621 session->disconnect_expired = true;
4624 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
4626 return session->smb1.session_id;
4629 void smb1cli_session_set_id(struct smbXcli_session *session,
4630 uint16_t session_id)
4632 session->smb1.session_id = session_id;
4635 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
4636 const DATA_BLOB _session_key)
4638 struct smbXcli_conn *conn = session->conn;
4639 uint8_t session_key[16];
4641 if (conn == NULL) {
4642 return NT_STATUS_INVALID_PARAMETER_MIX;
4645 if (session->smb1.application_key.length != 0) {
4647 * TODO: do not allow this...
4649 * return NT_STATUS_INVALID_PARAMETER_MIX;
4651 data_blob_clear_free(&session->smb1.application_key);
4652 session->smb1.protected_key = false;
4655 if (_session_key.length == 0) {
4656 return NT_STATUS_OK;
4659 ZERO_STRUCT(session_key);
4660 memcpy(session_key, _session_key.data,
4661 MIN(_session_key.length, sizeof(session_key)));
4663 session->smb1.application_key = data_blob_talloc(session,
4664 session_key,
4665 sizeof(session_key));
4666 ZERO_STRUCT(session_key);
4667 if (session->smb1.application_key.data == NULL) {
4668 return NT_STATUS_NO_MEMORY;
4671 session->smb1.protected_key = false;
4673 return NT_STATUS_OK;
4676 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
4678 if (session->smb1.protected_key) {
4679 /* already protected */
4680 return NT_STATUS_OK;
4683 if (session->smb1.application_key.length != 16) {
4684 return NT_STATUS_INVALID_PARAMETER_MIX;
4687 smb_key_derivation(session->smb1.application_key.data,
4688 session->smb1.application_key.length,
4689 session->smb1.application_key.data);
4691 session->smb1.protected_key = true;
4693 return NT_STATUS_OK;
4696 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4698 struct smbXcli_conn *conn = session->conn;
4699 uint8_t security_mode = 0;
4701 if (conn == NULL) {
4702 return security_mode;
4705 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4706 if (conn->mandatory_signing) {
4707 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4710 return security_mode;
4713 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4715 return session->smb2->session_id;
4718 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4720 return session->smb2->session_flags;
4723 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4724 uint64_t session_id,
4725 uint16_t session_flags)
4727 session->smb2->session_id = session_id;
4728 session->smb2->session_flags = session_flags;
4731 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
4733 session->smb2->channel_sequence += 1;
4736 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4737 const DATA_BLOB _session_key,
4738 const struct iovec *recv_iov)
4740 struct smbXcli_conn *conn = session->conn;
4741 uint16_t no_sign_flags;
4742 uint8_t session_key[16];
4743 NTSTATUS status;
4745 if (conn == NULL) {
4746 return NT_STATUS_INVALID_PARAMETER_MIX;
4749 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4751 if (session->smb2->session_flags & no_sign_flags) {
4752 session->smb2->should_sign = false;
4753 return NT_STATUS_OK;
4756 if (session->smb2->signing_key.length != 0) {
4757 return NT_STATUS_INVALID_PARAMETER_MIX;
4760 ZERO_STRUCT(session_key);
4761 memcpy(session_key, _session_key.data,
4762 MIN(_session_key.length, sizeof(session_key)));
4764 session->smb2->signing_key = data_blob_talloc(session,
4765 session_key,
4766 sizeof(session_key));
4767 if (session->smb2->signing_key.data == NULL) {
4768 ZERO_STRUCT(session_key);
4769 return NT_STATUS_NO_MEMORY;
4772 if (conn->protocol >= PROTOCOL_SMB2_24) {
4773 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4774 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4776 smb2_key_derivation(session_key, sizeof(session_key),
4777 label.data, label.length,
4778 context.data, context.length,
4779 session->smb2->signing_key.data);
4782 session->smb2->encryption_key = data_blob_dup_talloc(session,
4783 session->smb2->signing_key);
4784 if (session->smb2->encryption_key.data == NULL) {
4785 ZERO_STRUCT(session_key);
4786 return NT_STATUS_NO_MEMORY;
4789 if (conn->protocol >= PROTOCOL_SMB2_24) {
4790 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4791 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4793 smb2_key_derivation(session_key, sizeof(session_key),
4794 label.data, label.length,
4795 context.data, context.length,
4796 session->smb2->encryption_key.data);
4799 session->smb2->decryption_key = data_blob_dup_talloc(session,
4800 session->smb2->signing_key);
4801 if (session->smb2->decryption_key.data == NULL) {
4802 ZERO_STRUCT(session_key);
4803 return NT_STATUS_NO_MEMORY;
4806 if (conn->protocol >= PROTOCOL_SMB2_24) {
4807 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4808 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4810 smb2_key_derivation(session_key, sizeof(session_key),
4811 label.data, label.length,
4812 context.data, context.length,
4813 session->smb2->decryption_key.data);
4816 session->smb2->application_key = data_blob_dup_talloc(session,
4817 session->smb2->signing_key);
4818 if (session->smb2->application_key.data == NULL) {
4819 ZERO_STRUCT(session_key);
4820 return NT_STATUS_NO_MEMORY;
4823 if (conn->protocol >= PROTOCOL_SMB2_24) {
4824 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4825 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4827 smb2_key_derivation(session_key, sizeof(session_key),
4828 label.data, label.length,
4829 context.data, context.length,
4830 session->smb2->application_key.data);
4832 ZERO_STRUCT(session_key);
4834 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
4835 session->smb2->signing_key);
4836 if (session->smb2_channel.signing_key.data == NULL) {
4837 return NT_STATUS_NO_MEMORY;
4840 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4841 session->conn->protocol,
4842 recv_iov, 3);
4843 if (!NT_STATUS_IS_OK(status)) {
4844 return status;
4847 session->smb2->should_sign = false;
4848 session->smb2->should_encrypt = false;
4850 if (conn->desire_signing) {
4851 session->smb2->should_sign = true;
4854 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4855 session->smb2->should_sign = true;
4858 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
4859 session->smb2->should_encrypt = true;
4862 if (conn->protocol < PROTOCOL_SMB2_24) {
4863 session->smb2->should_encrypt = false;
4866 if (!(conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4867 session->smb2->should_encrypt = false;
4870 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
4871 sizeof(session->smb2->nonce_high));
4872 session->smb2->nonce_low = 1;
4874 return NT_STATUS_OK;
4877 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4878 struct smbXcli_session *session1,
4879 struct smbXcli_conn *conn,
4880 struct smbXcli_session **_session2)
4882 struct smbXcli_session *session2;
4884 if (session1->smb2->signing_key.length == 0) {
4885 return NT_STATUS_INVALID_PARAMETER_MIX;
4888 if (conn == NULL) {
4889 return NT_STATUS_INVALID_PARAMETER_MIX;
4892 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4893 if (session2 == NULL) {
4894 return NT_STATUS_NO_MEMORY;
4896 session2->smb2 = talloc_reference(session2, session1->smb2);
4897 if (session2->smb2 == NULL) {
4898 talloc_free(session2);
4899 return NT_STATUS_NO_MEMORY;
4902 talloc_set_destructor(session2, smbXcli_session_destructor);
4903 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4904 session2->conn = conn;
4906 *_session2 = session2;
4907 return NT_STATUS_OK;
4910 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4911 const DATA_BLOB _channel_key,
4912 const struct iovec *recv_iov)
4914 struct smbXcli_conn *conn = session->conn;
4915 uint8_t channel_key[16];
4916 NTSTATUS status;
4918 if (conn == NULL) {
4919 return NT_STATUS_INVALID_PARAMETER_MIX;
4922 if (session->smb2_channel.signing_key.length != 0) {
4923 return NT_STATUS_INVALID_PARAMETER_MIX;
4926 ZERO_STRUCT(channel_key);
4927 memcpy(channel_key, _channel_key.data,
4928 MIN(_channel_key.length, sizeof(channel_key)));
4930 session->smb2_channel.signing_key = data_blob_talloc(session,
4931 channel_key,
4932 sizeof(channel_key));
4933 if (session->smb2_channel.signing_key.data == NULL) {
4934 ZERO_STRUCT(channel_key);
4935 return NT_STATUS_NO_MEMORY;
4938 if (conn->protocol >= PROTOCOL_SMB2_24) {
4939 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4940 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4942 smb2_key_derivation(channel_key, sizeof(channel_key),
4943 label.data, label.length,
4944 context.data, context.length,
4945 session->smb2_channel.signing_key.data);
4947 ZERO_STRUCT(channel_key);
4949 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
4950 session->conn->protocol,
4951 recv_iov, 3);
4952 if (!NT_STATUS_IS_OK(status)) {
4953 return status;
4956 return NT_STATUS_OK;
4959 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
4961 if (session->smb2->should_encrypt) {
4962 return NT_STATUS_OK;
4965 if (session->conn->protocol < PROTOCOL_SMB2_24) {
4966 return NT_STATUS_NOT_SUPPORTED;
4969 if (!(session->conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
4970 return NT_STATUS_NOT_SUPPORTED;
4973 if (session->smb2->signing_key.data == NULL) {
4974 return NT_STATUS_NOT_SUPPORTED;
4976 session->smb2->should_encrypt = true;
4977 return NT_STATUS_OK;
4980 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
4982 struct smbXcli_tcon *tcon;
4984 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
4985 if (tcon == NULL) {
4986 return NULL;
4989 return tcon;
4992 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
4994 return tcon->smb1.tcon_id;
4997 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
4999 tcon->smb1.tcon_id = tcon_id;
5002 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
5003 uint16_t tcon_id,
5004 uint16_t optional_support,
5005 uint32_t maximal_access,
5006 uint32_t guest_maximal_access,
5007 const char *service,
5008 const char *fs_type)
5010 tcon->smb1.tcon_id = tcon_id;
5011 tcon->smb1.optional_support = optional_support;
5012 tcon->smb1.maximal_access = maximal_access;
5013 tcon->smb1.guest_maximal_access = guest_maximal_access;
5015 TALLOC_FREE(tcon->smb1.service);
5016 tcon->smb1.service = talloc_strdup(tcon, service);
5017 if (service != NULL && tcon->smb1.service == NULL) {
5018 return false;
5021 TALLOC_FREE(tcon->smb1.fs_type);
5022 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5023 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5024 return false;
5027 return true;
5030 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5032 return tcon->smb2.tcon_id;
5035 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5037 return tcon->smb2.capabilities;
5040 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5041 struct smbXcli_session *session,
5042 uint32_t tcon_id,
5043 uint8_t type,
5044 uint32_t flags,
5045 uint32_t capabilities,
5046 uint32_t maximal_access)
5048 tcon->smb2.tcon_id = tcon_id;
5049 tcon->smb2.type = type;
5050 tcon->smb2.flags = flags;
5051 tcon->smb2.capabilities = capabilities;
5052 tcon->smb2.maximal_access = maximal_access;
5054 tcon->smb2.should_encrypt = false;
5056 if (session == NULL) {
5057 return;
5060 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5062 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5063 tcon->smb2.should_encrypt = true;