YouCompleteMe: Add missing path.
[Samba.git] / libcli / smb / smbXcli_base.c
blob9f73566c7e46d425ac09a5279a195cdba0c201b5
1 /*
2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Stefan Metzmacher 2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/network.h"
23 #include "../lib/async_req/async_sock.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
26 #include "lib/util/util_net.h"
27 #include "lib/util/dlinklist.h"
28 #include "lib/util/iov_buf.h"
29 #include "../libcli/smb/smb_common.h"
30 #include "../libcli/smb/smb_seal.h"
31 #include "../libcli/smb/smb_signing.h"
32 #include "../libcli/smb/read_smb.h"
33 #include "smbXcli_base.h"
34 #include "librpc/ndr/libndr.h"
35 #include "libcli/smb/smb2_negotiate_context.h"
36 #include "lib/crypto/sha512.h"
38 struct smbXcli_conn;
39 struct smbXcli_req;
40 struct smbXcli_session;
41 struct smbXcli_tcon;
43 struct smbXcli_conn {
44 int read_fd;
45 int write_fd;
46 struct sockaddr_storage local_ss;
47 struct sockaddr_storage remote_ss;
48 const char *remote_name;
50 struct tevent_queue *outgoing;
51 struct tevent_req **pending;
52 struct tevent_req *read_smb_req;
54 enum protocol_types min_protocol;
55 enum protocol_types max_protocol;
56 enum protocol_types protocol;
57 bool allow_signing;
58 bool desire_signing;
59 bool mandatory_signing;
62 * The incoming dispatch function should return:
63 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
64 * - NT_STATUS_OK, if no more processing is desired, e.g.
65 * the dispatch function called
66 * tevent_req_done().
67 * - All other return values disconnect the connection.
69 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
70 TALLOC_CTX *tmp_mem,
71 uint8_t *inbuf);
73 struct {
74 struct {
75 uint32_t capabilities;
76 uint32_t max_xmit;
77 } client;
79 struct {
80 uint32_t capabilities;
81 uint32_t max_xmit;
82 uint16_t max_mux;
83 uint16_t security_mode;
84 bool readbraw;
85 bool writebraw;
86 bool lockread;
87 bool writeunlock;
88 uint32_t session_key;
89 struct GUID guid;
90 DATA_BLOB gss_blob;
91 uint8_t challenge[8];
92 const char *workgroup;
93 const char *name;
94 int time_zone;
95 NTTIME system_time;
96 } server;
98 uint32_t capabilities;
99 uint32_t max_xmit;
101 uint16_t mid;
103 struct smb_signing_state *signing;
104 struct smb_trans_enc_state *trans_enc;
106 struct tevent_req *read_braw_req;
107 } smb1;
109 struct {
110 struct {
111 uint32_t capabilities;
112 uint16_t security_mode;
113 struct GUID guid;
114 } client;
116 struct {
117 uint32_t capabilities;
118 uint16_t security_mode;
119 struct GUID guid;
120 uint32_t max_trans_size;
121 uint32_t max_read_size;
122 uint32_t max_write_size;
123 NTTIME system_time;
124 NTTIME start_time;
125 DATA_BLOB gss_blob;
126 uint16_t cipher;
127 } server;
129 uint64_t mid;
130 uint16_t cur_credits;
131 uint16_t max_credits;
133 uint8_t preauth_sha512[64];
134 } smb2;
136 struct smbXcli_session *sessions;
139 struct smb2cli_session {
140 uint64_t session_id;
141 uint16_t session_flags;
142 DATA_BLOB application_key;
143 DATA_BLOB signing_key;
144 bool should_sign;
145 bool should_encrypt;
146 DATA_BLOB encryption_key;
147 DATA_BLOB decryption_key;
148 uint64_t nonce_high;
149 uint64_t nonce_low;
150 uint16_t channel_sequence;
151 bool replay_active;
154 struct smbXcli_session {
155 struct smbXcli_session *prev, *next;
156 struct smbXcli_conn *conn;
158 struct {
159 uint16_t session_id;
160 DATA_BLOB application_key;
161 bool protected_key;
162 } smb1;
164 struct smb2cli_session *smb2;
166 struct {
167 DATA_BLOB signing_key;
168 uint8_t preauth_sha512[64];
169 } smb2_channel;
172 * this should be a short term hack
173 * until the upper layers have implemented
174 * re-authentication.
176 bool disconnect_expired;
179 struct smbXcli_tcon {
180 bool is_smb1;
181 uint32_t fs_attributes;
183 struct {
184 uint16_t tcon_id;
185 uint16_t optional_support;
186 uint32_t maximal_access;
187 uint32_t guest_maximal_access;
188 char *service;
189 char *fs_type;
190 } smb1;
192 struct {
193 uint32_t tcon_id;
194 uint8_t type;
195 uint32_t flags;
196 uint32_t capabilities;
197 uint32_t maximal_access;
198 bool should_sign;
199 bool should_encrypt;
200 } smb2;
203 struct smbXcli_req_state {
204 struct tevent_context *ev;
205 struct smbXcli_conn *conn;
206 struct smbXcli_session *session; /* maybe NULL */
207 struct smbXcli_tcon *tcon; /* maybe NULL */
209 uint8_t length_hdr[4];
211 bool one_way;
213 uint8_t *inbuf;
215 struct {
216 /* Space for the header including the wct */
217 uint8_t hdr[HDR_VWV];
220 * For normal requests, smb1cli_req_send chooses a mid.
221 * SecondaryV trans requests need to use the mid of the primary
222 * request, so we need a place to store it.
223 * Assume it is set if != 0.
225 uint16_t mid;
227 uint16_t *vwv;
228 uint8_t bytecount_buf[2];
230 #define MAX_SMB_IOV 10
231 /* length_hdr, hdr, words, byte_count, buffers */
232 struct iovec iov[1 + 3 + MAX_SMB_IOV];
233 int iov_count;
235 bool one_way_seqnum;
236 uint32_t seqnum;
237 struct tevent_req **chained_requests;
239 uint8_t recv_cmd;
240 NTSTATUS recv_status;
241 /* always an array of 3 talloc elements */
242 struct iovec *recv_iov;
243 } smb1;
245 struct {
246 const uint8_t *fixed;
247 uint16_t fixed_len;
248 const uint8_t *dyn;
249 uint32_t dyn_len;
251 uint8_t transform[SMB2_TF_HDR_SIZE];
252 uint8_t hdr[SMB2_HDR_BODY];
253 uint8_t pad[7]; /* padding space for compounding */
256 * always an array of 3 talloc elements
257 * (without a SMB2_TRANSFORM header!)
259 * HDR, BODY, DYN
261 struct iovec *recv_iov;
264 * the expected max for the response dyn_len
266 uint32_t max_dyn_len;
268 uint16_t credit_charge;
270 bool should_sign;
271 bool should_encrypt;
272 uint64_t encryption_session_id;
274 bool signing_skipped;
275 bool notify_async;
276 bool got_async;
277 } smb2;
280 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
283 * NT_STATUS_OK, means we do not notify the callers
285 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
287 while (conn->sessions) {
288 conn->sessions->conn = NULL;
289 DLIST_REMOVE(conn->sessions, conn->sessions);
292 if (conn->smb1.trans_enc) {
293 TALLOC_FREE(conn->smb1.trans_enc);
296 return 0;
299 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
300 int fd,
301 const char *remote_name,
302 enum smb_signing_setting signing_state,
303 uint32_t smb1_capabilities,
304 struct GUID *client_guid,
305 uint32_t smb2_capabilities)
307 struct smbXcli_conn *conn = NULL;
308 void *ss = NULL;
309 struct sockaddr *sa = NULL;
310 socklen_t sa_length;
311 int ret;
313 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
314 if (!conn) {
315 return NULL;
318 conn->read_fd = fd;
319 conn->write_fd = dup(fd);
320 if (conn->write_fd == -1) {
321 goto error;
324 conn->remote_name = talloc_strdup(conn, remote_name);
325 if (conn->remote_name == NULL) {
326 goto error;
330 ss = (void *)&conn->local_ss;
331 sa = (struct sockaddr *)ss;
332 sa_length = sizeof(conn->local_ss);
333 ret = getsockname(fd, sa, &sa_length);
334 if (ret == -1) {
335 goto error;
337 ss = (void *)&conn->remote_ss;
338 sa = (struct sockaddr *)ss;
339 sa_length = sizeof(conn->remote_ss);
340 ret = getpeername(fd, sa, &sa_length);
341 if (ret == -1) {
342 goto error;
345 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
346 if (conn->outgoing == NULL) {
347 goto error;
349 conn->pending = NULL;
351 conn->min_protocol = PROTOCOL_NONE;
352 conn->max_protocol = PROTOCOL_NONE;
353 conn->protocol = PROTOCOL_NONE;
355 switch (signing_state) {
356 case SMB_SIGNING_OFF:
357 /* never */
358 conn->allow_signing = false;
359 conn->desire_signing = false;
360 conn->mandatory_signing = false;
361 break;
362 case SMB_SIGNING_DEFAULT:
363 case SMB_SIGNING_IF_REQUIRED:
364 /* if the server requires it */
365 conn->allow_signing = true;
366 conn->desire_signing = false;
367 conn->mandatory_signing = false;
368 break;
369 case SMB_SIGNING_REQUIRED:
370 /* always */
371 conn->allow_signing = true;
372 conn->desire_signing = true;
373 conn->mandatory_signing = true;
374 break;
377 conn->smb1.client.capabilities = smb1_capabilities;
378 conn->smb1.client.max_xmit = UINT16_MAX;
380 conn->smb1.capabilities = conn->smb1.client.capabilities;
381 conn->smb1.max_xmit = 1024;
383 conn->smb1.mid = 1;
385 /* initialise signing */
386 conn->smb1.signing = smb_signing_init(conn,
387 conn->allow_signing,
388 conn->desire_signing,
389 conn->mandatory_signing);
390 if (!conn->smb1.signing) {
391 goto error;
394 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
395 if (conn->mandatory_signing) {
396 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
398 if (client_guid) {
399 conn->smb2.client.guid = *client_guid;
401 conn->smb2.client.capabilities = smb2_capabilities;
403 conn->smb2.cur_credits = 1;
404 conn->smb2.max_credits = 0;
406 talloc_set_destructor(conn, smbXcli_conn_destructor);
407 return conn;
409 error:
410 if (conn->write_fd != -1) {
411 close(conn->write_fd);
413 TALLOC_FREE(conn);
414 return NULL;
417 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
419 if (conn == NULL) {
420 return false;
423 if (conn->read_fd == -1) {
424 return false;
427 return true;
430 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
432 return conn->protocol;
435 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
437 if (conn->protocol >= PROTOCOL_SMB2_02) {
438 return true;
441 if (conn->smb1.capabilities & CAP_UNICODE) {
442 return true;
445 return false;
448 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
450 set_socket_options(conn->read_fd, options);
453 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
455 return &conn->local_ss;
458 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
460 return &conn->remote_ss;
463 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
465 return conn->remote_name;
468 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
470 if (conn->protocol >= PROTOCOL_SMB2_02) {
472 * TODO...
474 return 1;
477 return conn->smb1.server.max_mux;
480 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
482 if (conn->protocol >= PROTOCOL_SMB2_02) {
483 return conn->smb2.server.system_time;
486 return conn->smb1.server.system_time;
489 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
491 if (conn->protocol >= PROTOCOL_SMB2_02) {
492 return &conn->smb2.server.gss_blob;
495 return &conn->smb1.server.gss_blob;
498 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
500 if (conn->protocol >= PROTOCOL_SMB2_02) {
501 return &conn->smb2.server.guid;
504 return &conn->smb1.server.guid;
507 struct smbXcli_conn_samba_suicide_state {
508 struct smbXcli_conn *conn;
509 struct iovec iov;
510 uint8_t buf[9];
513 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
515 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
516 struct tevent_context *ev,
517 struct smbXcli_conn *conn,
518 uint8_t exitcode)
520 struct tevent_req *req, *subreq;
521 struct smbXcli_conn_samba_suicide_state *state;
523 req = tevent_req_create(mem_ctx, &state,
524 struct smbXcli_conn_samba_suicide_state);
525 if (req == NULL) {
526 return NULL;
528 state->conn = conn;
529 SIVAL(state->buf, 4, 0x74697865);
530 SCVAL(state->buf, 8, exitcode);
531 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
533 state->iov.iov_base = state->buf;
534 state->iov.iov_len = sizeof(state->buf);
536 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
537 false, &state->iov, 1);
538 if (tevent_req_nomem(subreq, req)) {
539 return tevent_req_post(req, ev);
541 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
542 return req;
545 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
547 struct tevent_req *req = tevent_req_callback_data(
548 subreq, struct tevent_req);
549 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
550 req, struct smbXcli_conn_samba_suicide_state);
551 ssize_t nwritten;
552 int err;
554 nwritten = writev_recv(subreq, &err);
555 TALLOC_FREE(subreq);
556 if (nwritten == -1) {
557 NTSTATUS status = map_nt_error_from_unix_common(err);
558 smbXcli_conn_disconnect(state->conn, status);
559 return;
561 tevent_req_done(req);
564 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
566 return tevent_req_simple_recv_ntstatus(req);
569 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
570 uint8_t exitcode)
572 TALLOC_CTX *frame = talloc_stackframe();
573 struct tevent_context *ev;
574 struct tevent_req *req;
575 NTSTATUS status = NT_STATUS_NO_MEMORY;
576 bool ok;
578 if (smbXcli_conn_has_async_calls(conn)) {
580 * Can't use sync call while an async call is in flight
582 status = NT_STATUS_INVALID_PARAMETER_MIX;
583 goto fail;
585 ev = samba_tevent_context_init(frame);
586 if (ev == NULL) {
587 goto fail;
589 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
590 if (req == NULL) {
591 goto fail;
593 ok = tevent_req_poll_ntstatus(req, ev, &status);
594 if (!ok) {
595 goto fail;
597 status = smbXcli_conn_samba_suicide_recv(req);
598 fail:
599 TALLOC_FREE(frame);
600 return status;
603 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
605 return conn->smb1.capabilities;
608 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
610 return conn->smb1.max_xmit;
613 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
615 size_t pending;
616 uint16_t possible = conn->smb1.server.max_mux;
618 pending = tevent_queue_length(conn->outgoing);
619 if (pending >= possible) {
620 return false;
622 pending += talloc_array_length(conn->pending);
623 if (pending >= possible) {
624 return false;
627 return true;
630 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
632 return conn->smb1.server.session_key;
635 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
637 return conn->smb1.server.challenge;
640 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
642 return conn->smb1.server.security_mode;
645 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
647 return conn->smb1.server.readbraw;
650 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
652 return conn->smb1.server.writebraw;
655 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
657 return conn->smb1.server.lockread;
660 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
662 return conn->smb1.server.writeunlock;
665 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
667 return conn->smb1.server.time_zone;
670 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
671 const DATA_BLOB user_session_key,
672 const DATA_BLOB response)
674 return smb_signing_activate(conn->smb1.signing,
675 user_session_key,
676 response);
679 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
680 const uint8_t *buf, uint32_t seqnum)
682 const uint8_t *hdr = buf + NBT_HDR_SIZE;
683 size_t len = smb_len_nbt(buf);
685 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
688 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
690 return smb_signing_is_active(conn->smb1.signing);
693 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
694 struct smb_trans_enc_state *es)
696 /* Replace the old state, if any. */
697 if (conn->smb1.trans_enc) {
698 TALLOC_FREE(conn->smb1.trans_enc);
700 conn->smb1.trans_enc = es;
703 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
705 return common_encryption_on(conn->smb1.trans_enc);
709 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
711 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
712 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
714 if (NT_STATUS_IS_OK(status)) {
715 return NT_STATUS_OK;
718 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
719 return status;
722 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
726 * Is the SMB command able to hold an AND_X successor
727 * @param[in] cmd The SMB command in question
728 * @retval Can we add a chained request after "cmd"?
730 bool smb1cli_is_andx_req(uint8_t cmd)
732 switch (cmd) {
733 case SMBtconX:
734 case SMBlockingX:
735 case SMBopenX:
736 case SMBreadX:
737 case SMBwriteX:
738 case SMBsesssetupX:
739 case SMBulogoffX:
740 case SMBntcreateX:
741 return true;
742 break;
743 default:
744 break;
747 return false;
750 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
752 size_t num_pending = talloc_array_length(conn->pending);
753 uint16_t result;
755 if (conn->protocol == PROTOCOL_NONE) {
757 * This is what windows sends on the SMB1 Negprot request
758 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
760 return 0;
763 while (true) {
764 size_t i;
766 result = conn->smb1.mid++;
767 if ((result == 0) || (result == 0xffff)) {
768 continue;
771 for (i=0; i<num_pending; i++) {
772 if (result == smb1cli_req_mid(conn->pending[i])) {
773 break;
777 if (i == num_pending) {
778 return result;
783 void smbXcli_req_unset_pending(struct tevent_req *req)
785 struct smbXcli_req_state *state =
786 tevent_req_data(req,
787 struct smbXcli_req_state);
788 struct smbXcli_conn *conn = state->conn;
789 size_t num_pending = talloc_array_length(conn->pending);
790 size_t i;
792 if (state->smb1.mid != 0) {
794 * This is a [nt]trans[2] request which waits
795 * for more than one reply.
797 return;
800 tevent_req_set_cleanup_fn(req, NULL);
802 if (num_pending == 1) {
804 * The pending read_smb tevent_req is a child of
805 * conn->pending. So if nothing is pending anymore, we need to
806 * delete the socket read fde.
808 TALLOC_FREE(conn->pending);
809 conn->read_smb_req = NULL;
810 return;
813 for (i=0; i<num_pending; i++) {
814 if (req == conn->pending[i]) {
815 break;
818 if (i == num_pending) {
820 * Something's seriously broken. Just returning here is the
821 * right thing nevertheless, the point of this routine is to
822 * remove ourselves from conn->pending.
824 return;
828 * Remove ourselves from the conn->pending array
830 for (; i < (num_pending - 1); i++) {
831 conn->pending[i] = conn->pending[i+1];
835 * No NULL check here, we're shrinking by sizeof(void *), and
836 * talloc_realloc just adjusts the size for this.
838 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
839 num_pending - 1);
840 return;
843 static void smbXcli_req_cleanup(struct tevent_req *req,
844 enum tevent_req_state req_state)
846 struct smbXcli_req_state *state =
847 tevent_req_data(req,
848 struct smbXcli_req_state);
850 switch (req_state) {
851 case TEVENT_REQ_RECEIVED:
853 * Make sure we really remove it from
854 * the pending array on destruction.
856 state->smb1.mid = 0;
857 smbXcli_req_unset_pending(req);
858 return;
859 default:
860 return;
864 static bool smb1cli_req_cancel(struct tevent_req *req);
865 static bool smb2cli_req_cancel(struct tevent_req *req);
867 static bool smbXcli_req_cancel(struct tevent_req *req)
869 struct smbXcli_req_state *state =
870 tevent_req_data(req,
871 struct smbXcli_req_state);
873 if (!smbXcli_conn_is_connected(state->conn)) {
874 return false;
877 if (state->conn->protocol == PROTOCOL_NONE) {
878 return false;
881 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
882 return smb2cli_req_cancel(req);
885 return smb1cli_req_cancel(req);
888 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
890 bool smbXcli_req_set_pending(struct tevent_req *req)
892 struct smbXcli_req_state *state =
893 tevent_req_data(req,
894 struct smbXcli_req_state);
895 struct smbXcli_conn *conn;
896 struct tevent_req **pending;
897 size_t num_pending;
899 conn = state->conn;
901 if (!smbXcli_conn_is_connected(conn)) {
902 return false;
905 num_pending = talloc_array_length(conn->pending);
907 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
908 num_pending+1);
909 if (pending == NULL) {
910 return false;
912 pending[num_pending] = req;
913 conn->pending = pending;
914 tevent_req_set_cleanup_fn(req, smbXcli_req_cleanup);
915 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
917 if (!smbXcli_conn_receive_next(conn)) {
919 * the caller should notify the current request
921 * And all other pending requests get notified
922 * by smbXcli_conn_disconnect().
924 smbXcli_req_unset_pending(req);
925 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
926 return false;
929 return true;
932 static void smbXcli_conn_received(struct tevent_req *subreq);
934 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
936 size_t num_pending = talloc_array_length(conn->pending);
937 struct tevent_req *req;
938 struct smbXcli_req_state *state;
940 if (conn->read_smb_req != NULL) {
941 return true;
944 if (num_pending == 0) {
945 if (conn->smb2.mid < UINT64_MAX) {
946 /* no more pending requests, so we are done for now */
947 return true;
951 * If there are no more SMB2 requests possible,
952 * because we are out of message ids,
953 * we need to disconnect.
955 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
956 return true;
959 req = conn->pending[0];
960 state = tevent_req_data(req, struct smbXcli_req_state);
963 * We're the first ones, add the read_smb request that waits for the
964 * answer from the server
966 conn->read_smb_req = read_smb_send(conn->pending,
967 state->ev,
968 conn->read_fd);
969 if (conn->read_smb_req == NULL) {
970 return false;
972 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
973 return true;
976 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
978 struct smbXcli_session *session;
980 tevent_queue_stop(conn->outgoing);
982 if (conn->read_fd != -1) {
983 close(conn->read_fd);
985 if (conn->write_fd != -1) {
986 close(conn->write_fd);
988 conn->read_fd = -1;
989 conn->write_fd = -1;
991 session = conn->sessions;
992 if (talloc_array_length(conn->pending) == 0) {
994 * if we do not have pending requests
995 * there is no need to update the channel_sequence
997 session = NULL;
999 for (; session; session = session->next) {
1000 smb2cli_session_increment_channel_sequence(session);
1004 * Cancel all pending requests. We do not do a for-loop walking
1005 * conn->pending because that array changes in
1006 * smbXcli_req_unset_pending.
1008 while (talloc_array_length(conn->pending) > 0) {
1009 struct tevent_req *req;
1010 struct smbXcli_req_state *state;
1011 struct tevent_req **chain;
1012 size_t num_chained;
1013 size_t i;
1015 req = conn->pending[0];
1016 state = tevent_req_data(req, struct smbXcli_req_state);
1018 if (state->smb1.chained_requests == NULL) {
1020 * We're dead. No point waiting for trans2
1021 * replies.
1023 state->smb1.mid = 0;
1025 smbXcli_req_unset_pending(req);
1027 if (NT_STATUS_IS_OK(status)) {
1028 /* do not notify the callers */
1029 continue;
1033 * we need to defer the callback, because we may notify
1034 * more then one caller.
1036 tevent_req_defer_callback(req, state->ev);
1037 tevent_req_nterror(req, status);
1038 continue;
1041 chain = talloc_move(conn, &state->smb1.chained_requests);
1042 num_chained = talloc_array_length(chain);
1044 for (i=0; i<num_chained; i++) {
1045 req = chain[i];
1046 state = tevent_req_data(req, struct smbXcli_req_state);
1049 * We're dead. No point waiting for trans2
1050 * replies.
1052 state->smb1.mid = 0;
1054 smbXcli_req_unset_pending(req);
1056 if (NT_STATUS_IS_OK(status)) {
1057 /* do not notify the callers */
1058 continue;
1062 * we need to defer the callback, because we may notify
1063 * more than one caller.
1065 tevent_req_defer_callback(req, state->ev);
1066 tevent_req_nterror(req, status);
1068 TALLOC_FREE(chain);
1073 * Fetch a smb request's mid. Only valid after the request has been sent by
1074 * smb1cli_req_send().
1076 uint16_t smb1cli_req_mid(struct tevent_req *req)
1078 struct smbXcli_req_state *state =
1079 tevent_req_data(req,
1080 struct smbXcli_req_state);
1082 if (state->smb1.mid != 0) {
1083 return state->smb1.mid;
1086 return SVAL(state->smb1.hdr, HDR_MID);
1089 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1091 struct smbXcli_req_state *state =
1092 tevent_req_data(req,
1093 struct smbXcli_req_state);
1095 state->smb1.mid = mid;
1098 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1100 struct smbXcli_req_state *state =
1101 tevent_req_data(req,
1102 struct smbXcli_req_state);
1104 return state->smb1.seqnum;
1107 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1109 struct smbXcli_req_state *state =
1110 tevent_req_data(req,
1111 struct smbXcli_req_state);
1113 state->smb1.seqnum = seqnum;
1116 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1118 ssize_t ret = iov_buflen(iov, count);
1120 /* Ignore the overflow case for now ... */
1121 return ret;
1124 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1125 const struct iovec *iov,
1126 int count)
1128 ssize_t buflen;
1129 uint8_t *buf;
1131 buflen = iov_buflen(iov, count);
1132 if (buflen == -1) {
1133 return NULL;
1136 buf = talloc_array(mem_ctx, uint8_t, buflen);
1137 if (buf == NULL) {
1138 return NULL;
1141 iov_buf(iov, count, buf, buflen);
1143 return buf;
1146 static void smb1cli_req_flags(enum protocol_types protocol,
1147 uint32_t smb1_capabilities,
1148 uint8_t smb_command,
1149 uint8_t additional_flags,
1150 uint8_t clear_flags,
1151 uint8_t *_flags,
1152 uint16_t additional_flags2,
1153 uint16_t clear_flags2,
1154 uint16_t *_flags2)
1156 uint8_t flags = 0;
1157 uint16_t flags2 = 0;
1159 if (protocol >= PROTOCOL_LANMAN1) {
1160 flags |= FLAG_CASELESS_PATHNAMES;
1161 flags |= FLAG_CANONICAL_PATHNAMES;
1164 if (protocol >= PROTOCOL_LANMAN2) {
1165 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1166 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1169 if (protocol >= PROTOCOL_NT1) {
1170 flags2 |= FLAGS2_IS_LONG_NAME;
1172 if (smb1_capabilities & CAP_UNICODE) {
1173 flags2 |= FLAGS2_UNICODE_STRINGS;
1175 if (smb1_capabilities & CAP_STATUS32) {
1176 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1178 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1179 flags2 |= FLAGS2_EXTENDED_SECURITY;
1183 flags |= additional_flags;
1184 flags &= ~clear_flags;
1185 flags2 |= additional_flags2;
1186 flags2 &= ~clear_flags2;
1188 *_flags = flags;
1189 *_flags2 = flags2;
1192 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1194 static bool smb1cli_req_cancel(struct tevent_req *req)
1196 struct smbXcli_req_state *state =
1197 tevent_req_data(req,
1198 struct smbXcli_req_state);
1199 uint8_t flags;
1200 uint16_t flags2;
1201 uint32_t pid;
1202 uint16_t mid;
1203 struct tevent_req *subreq;
1204 NTSTATUS status;
1206 flags = CVAL(state->smb1.hdr, HDR_FLG);
1207 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1208 pid = SVAL(state->smb1.hdr, HDR_PID);
1209 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1210 mid = SVAL(state->smb1.hdr, HDR_MID);
1212 subreq = smb1cli_req_create(state, state->ev,
1213 state->conn,
1214 SMBntcancel,
1215 flags, 0,
1216 flags2, 0,
1217 0, /* timeout */
1218 pid,
1219 state->tcon,
1220 state->session,
1221 0, NULL, /* vwv */
1222 0, NULL); /* bytes */
1223 if (subreq == NULL) {
1224 return false;
1226 smb1cli_req_set_mid(subreq, mid);
1228 status = smb1cli_req_chain_submit(&subreq, 1);
1229 if (!NT_STATUS_IS_OK(status)) {
1230 TALLOC_FREE(subreq);
1231 return false;
1233 smb1cli_req_set_mid(subreq, 0);
1235 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1237 return true;
1240 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1242 /* we do not care about the result */
1243 TALLOC_FREE(subreq);
1246 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1247 struct tevent_context *ev,
1248 struct smbXcli_conn *conn,
1249 uint8_t smb_command,
1250 uint8_t additional_flags,
1251 uint8_t clear_flags,
1252 uint16_t additional_flags2,
1253 uint16_t clear_flags2,
1254 uint32_t timeout_msec,
1255 uint32_t pid,
1256 struct smbXcli_tcon *tcon,
1257 struct smbXcli_session *session,
1258 uint8_t wct, uint16_t *vwv,
1259 int iov_count,
1260 struct iovec *bytes_iov)
1262 struct tevent_req *req;
1263 struct smbXcli_req_state *state;
1264 uint8_t flags = 0;
1265 uint16_t flags2 = 0;
1266 uint16_t uid = 0;
1267 uint16_t tid = 0;
1268 ssize_t num_bytes;
1270 if (iov_count > MAX_SMB_IOV) {
1272 * Should not happen :-)
1274 return NULL;
1277 req = tevent_req_create(mem_ctx, &state,
1278 struct smbXcli_req_state);
1279 if (req == NULL) {
1280 return NULL;
1282 state->ev = ev;
1283 state->conn = conn;
1284 state->session = session;
1285 state->tcon = tcon;
1287 if (session) {
1288 uid = session->smb1.session_id;
1291 if (tcon) {
1292 tid = tcon->smb1.tcon_id;
1294 if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
1295 clear_flags |= FLAG_CASELESS_PATHNAMES;
1296 } else {
1297 /* Default setting, case insensitive. */
1298 additional_flags |= FLAG_CASELESS_PATHNAMES;
1301 if (smbXcli_conn_dfs_supported(conn) &&
1302 smbXcli_tcon_is_dfs_share(tcon))
1304 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
1308 state->smb1.recv_cmd = 0xFF;
1309 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1310 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1311 if (state->smb1.recv_iov == NULL) {
1312 TALLOC_FREE(req);
1313 return NULL;
1316 smb1cli_req_flags(conn->protocol,
1317 conn->smb1.capabilities,
1318 smb_command,
1319 additional_flags,
1320 clear_flags,
1321 &flags,
1322 additional_flags2,
1323 clear_flags2,
1324 &flags2);
1326 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1327 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1328 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1329 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1330 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1331 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1332 SSVAL(state->smb1.hdr, HDR_TID, tid);
1333 SSVAL(state->smb1.hdr, HDR_PID, pid);
1334 SSVAL(state->smb1.hdr, HDR_UID, uid);
1335 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1336 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1338 state->smb1.vwv = vwv;
1340 num_bytes = iov_buflen(bytes_iov, iov_count);
1341 if (num_bytes == -1) {
1343 * I'd love to add a check for num_bytes<=UINT16_MAX here, but
1344 * the smbclient->samba connections can lie and transfer more.
1346 TALLOC_FREE(req);
1347 return NULL;
1350 SSVAL(state->smb1.bytecount_buf, 0, num_bytes);
1352 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1353 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1354 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1355 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1356 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1357 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1358 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1359 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1361 if (iov_count != 0) {
1362 memcpy(&state->smb1.iov[4], bytes_iov,
1363 iov_count * sizeof(*bytes_iov));
1365 state->smb1.iov_count = iov_count + 4;
1367 if (timeout_msec > 0) {
1368 struct timeval endtime;
1370 endtime = timeval_current_ofs_msec(timeout_msec);
1371 if (!tevent_req_set_endtime(req, ev, endtime)) {
1372 return req;
1376 switch (smb_command) {
1377 case SMBtranss:
1378 case SMBtranss2:
1379 case SMBnttranss:
1380 state->one_way = true;
1381 break;
1382 case SMBntcancel:
1383 state->one_way = true;
1384 state->smb1.one_way_seqnum = true;
1385 break;
1386 case SMBlockingX:
1387 if ((wct == 8) &&
1388 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1389 state->one_way = true;
1391 break;
1394 return req;
1397 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1398 struct iovec *iov, int iov_count,
1399 uint32_t *seqnum,
1400 bool one_way_seqnum)
1402 TALLOC_CTX *frame = NULL;
1403 uint8_t *buf;
1406 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1407 * iovec directly. MD5Update would do that just fine.
1410 if (iov_count < 4) {
1411 return NT_STATUS_INVALID_PARAMETER_MIX;
1413 if (iov[0].iov_len != NBT_HDR_SIZE) {
1414 return NT_STATUS_INVALID_PARAMETER_MIX;
1416 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1417 return NT_STATUS_INVALID_PARAMETER_MIX;
1419 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1420 return NT_STATUS_INVALID_PARAMETER_MIX;
1422 if (iov[3].iov_len != sizeof(uint16_t)) {
1423 return NT_STATUS_INVALID_PARAMETER_MIX;
1426 frame = talloc_stackframe();
1428 buf = smbXcli_iov_concat(frame, &iov[1], iov_count - 1);
1429 if (buf == NULL) {
1430 return NT_STATUS_NO_MEMORY;
1433 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1434 one_way_seqnum);
1435 smb_signing_sign_pdu(conn->smb1.signing,
1436 buf, talloc_get_size(buf),
1437 *seqnum);
1438 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1440 TALLOC_FREE(frame);
1441 return NT_STATUS_OK;
1444 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1445 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1446 TALLOC_CTX *tmp_mem,
1447 uint8_t *inbuf);
1449 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1450 struct smbXcli_req_state *state,
1451 struct iovec *iov, int iov_count)
1453 struct tevent_req *subreq;
1454 NTSTATUS status;
1455 uint8_t cmd;
1456 uint16_t mid;
1457 ssize_t nbtlen;
1459 if (!smbXcli_conn_is_connected(state->conn)) {
1460 return NT_STATUS_CONNECTION_DISCONNECTED;
1463 if (state->conn->protocol > PROTOCOL_NT1) {
1464 return NT_STATUS_REVISION_MISMATCH;
1467 if (iov_count < 4) {
1468 return NT_STATUS_INVALID_PARAMETER_MIX;
1470 if (iov[0].iov_len != NBT_HDR_SIZE) {
1471 return NT_STATUS_INVALID_PARAMETER_MIX;
1473 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1474 return NT_STATUS_INVALID_PARAMETER_MIX;
1476 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1477 return NT_STATUS_INVALID_PARAMETER_MIX;
1479 if (iov[3].iov_len != sizeof(uint16_t)) {
1480 return NT_STATUS_INVALID_PARAMETER_MIX;
1483 cmd = CVAL(iov[1].iov_base, HDR_COM);
1484 if (cmd == SMBreadBraw) {
1485 if (smbXcli_conn_has_async_calls(state->conn)) {
1486 return NT_STATUS_INVALID_PARAMETER_MIX;
1488 state->conn->smb1.read_braw_req = req;
1491 if (state->smb1.mid != 0) {
1492 mid = state->smb1.mid;
1493 } else {
1494 mid = smb1cli_alloc_mid(state->conn);
1496 SSVAL(iov[1].iov_base, HDR_MID, mid);
1498 nbtlen = iov_buflen(&iov[1], iov_count-1);
1499 if ((nbtlen == -1) || (nbtlen > 0x1FFFF)) {
1500 return NT_STATUS_INVALID_PARAMETER_MIX;
1503 _smb_setlen_nbt(iov[0].iov_base, nbtlen);
1505 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1506 &state->smb1.seqnum,
1507 state->smb1.one_way_seqnum);
1509 if (!NT_STATUS_IS_OK(status)) {
1510 return status;
1514 * If we supported multiple encrytion contexts
1515 * here we'd look up based on tid.
1517 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1518 char *buf, *enc_buf;
1520 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1521 if (buf == NULL) {
1522 return NT_STATUS_NO_MEMORY;
1524 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1525 (char *)buf, &enc_buf);
1526 TALLOC_FREE(buf);
1527 if (!NT_STATUS_IS_OK(status)) {
1528 DEBUG(0, ("Error in encrypting client message: %s\n",
1529 nt_errstr(status)));
1530 return status;
1532 buf = (char *)talloc_memdup(state, enc_buf,
1533 smb_len_nbt(enc_buf)+4);
1534 SAFE_FREE(enc_buf);
1535 if (buf == NULL) {
1536 return NT_STATUS_NO_MEMORY;
1538 iov[0].iov_base = (void *)buf;
1539 iov[0].iov_len = talloc_get_size(buf);
1540 iov_count = 1;
1543 if (state->conn->dispatch_incoming == NULL) {
1544 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1547 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1549 subreq = writev_send(state, state->ev, state->conn->outgoing,
1550 state->conn->write_fd, false, iov, iov_count);
1551 if (subreq == NULL) {
1552 return NT_STATUS_NO_MEMORY;
1554 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1555 return NT_STATUS_OK;
1558 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1559 struct tevent_context *ev,
1560 struct smbXcli_conn *conn,
1561 uint8_t smb_command,
1562 uint8_t additional_flags,
1563 uint8_t clear_flags,
1564 uint16_t additional_flags2,
1565 uint16_t clear_flags2,
1566 uint32_t timeout_msec,
1567 uint32_t pid,
1568 struct smbXcli_tcon *tcon,
1569 struct smbXcli_session *session,
1570 uint8_t wct, uint16_t *vwv,
1571 uint32_t num_bytes,
1572 const uint8_t *bytes)
1574 struct tevent_req *req;
1575 struct iovec iov;
1576 NTSTATUS status;
1578 iov.iov_base = discard_const_p(void, bytes);
1579 iov.iov_len = num_bytes;
1581 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1582 additional_flags, clear_flags,
1583 additional_flags2, clear_flags2,
1584 timeout_msec,
1585 pid, tcon, session,
1586 wct, vwv, 1, &iov);
1587 if (req == NULL) {
1588 return NULL;
1590 if (!tevent_req_is_in_progress(req)) {
1591 return tevent_req_post(req, ev);
1593 status = smb1cli_req_chain_submit(&req, 1);
1594 if (tevent_req_nterror(req, status)) {
1595 return tevent_req_post(req, ev);
1597 return req;
1600 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1602 struct tevent_req *req =
1603 tevent_req_callback_data(subreq,
1604 struct tevent_req);
1605 struct smbXcli_req_state *state =
1606 tevent_req_data(req,
1607 struct smbXcli_req_state);
1608 ssize_t nwritten;
1609 int err;
1611 nwritten = writev_recv(subreq, &err);
1612 TALLOC_FREE(subreq);
1613 if (nwritten == -1) {
1614 NTSTATUS status = map_nt_error_from_unix_common(err);
1615 smbXcli_conn_disconnect(state->conn, status);
1616 tevent_req_nterror(req, status);
1617 return;
1620 if (state->one_way) {
1621 state->inbuf = NULL;
1622 tevent_req_done(req);
1623 return;
1626 if (!smbXcli_req_set_pending(req)) {
1627 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1628 return;
1632 static void smbXcli_conn_received(struct tevent_req *subreq)
1634 struct smbXcli_conn *conn =
1635 tevent_req_callback_data(subreq,
1636 struct smbXcli_conn);
1637 TALLOC_CTX *frame = talloc_stackframe();
1638 NTSTATUS status;
1639 uint8_t *inbuf;
1640 ssize_t received;
1641 int err;
1643 if (subreq != conn->read_smb_req) {
1644 DEBUG(1, ("Internal error: cli_smb_received called with "
1645 "unexpected subreq\n"));
1646 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1647 TALLOC_FREE(frame);
1648 return;
1650 conn->read_smb_req = NULL;
1652 received = read_smb_recv(subreq, frame, &inbuf, &err);
1653 TALLOC_FREE(subreq);
1654 if (received == -1) {
1655 status = map_nt_error_from_unix_common(err);
1656 smbXcli_conn_disconnect(conn, status);
1657 TALLOC_FREE(frame);
1658 return;
1661 status = conn->dispatch_incoming(conn, frame, inbuf);
1662 TALLOC_FREE(frame);
1663 if (NT_STATUS_IS_OK(status)) {
1665 * We should not do any more processing
1666 * as the dispatch function called
1667 * tevent_req_done().
1669 return;
1672 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1674 * We got an error, so notify all pending requests
1676 smbXcli_conn_disconnect(conn, status);
1677 return;
1681 * We got NT_STATUS_RETRY, so we may ask for a
1682 * next incoming pdu.
1684 if (!smbXcli_conn_receive_next(conn)) {
1685 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1689 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1690 struct iovec **piov, int *pnum_iov)
1692 struct iovec *iov;
1693 int num_iov;
1694 size_t buflen;
1695 size_t taken;
1696 size_t remaining;
1697 uint8_t *hdr;
1698 uint8_t cmd;
1699 uint32_t wct_ofs;
1700 NTSTATUS status;
1701 size_t min_size = MIN_SMB_SIZE;
1703 buflen = smb_len_tcp(buf);
1704 taken = 0;
1706 hdr = buf + NBT_HDR_SIZE;
1708 status = smb1cli_pull_raw_error(hdr);
1709 if (NT_STATUS_IS_ERR(status)) {
1711 * This is an ugly hack to support OS/2
1712 * which skips the byte_count in the DATA block
1713 * on some error responses.
1715 * See bug #9096
1717 min_size -= sizeof(uint16_t);
1720 if (buflen < min_size) {
1721 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1725 * This returns iovec elements in the following order:
1727 * - SMB header
1729 * - Parameter Block
1730 * - Data Block
1732 * - Parameter Block
1733 * - Data Block
1735 * - Parameter Block
1736 * - Data Block
1738 num_iov = 1;
1740 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1741 if (iov == NULL) {
1742 return NT_STATUS_NO_MEMORY;
1744 iov[0].iov_base = hdr;
1745 iov[0].iov_len = HDR_WCT;
1746 taken += HDR_WCT;
1748 cmd = CVAL(hdr, HDR_COM);
1749 wct_ofs = HDR_WCT;
1751 while (true) {
1752 size_t len = buflen - taken;
1753 struct iovec *cur;
1754 struct iovec *iov_tmp;
1755 uint8_t wct;
1756 uint32_t bcc_ofs;
1757 uint16_t bcc;
1758 size_t needed;
1761 * we need at least WCT
1763 needed = sizeof(uint8_t);
1764 if (len < needed) {
1765 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1766 __location__, (int)len, (int)needed));
1767 goto inval;
1771 * Now we check if the specified words are there
1773 wct = CVAL(hdr, wct_ofs);
1774 needed += wct * sizeof(uint16_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;
1781 if ((num_iov == 1) &&
1782 (len == needed) &&
1783 NT_STATUS_IS_ERR(status))
1786 * This is an ugly hack to support OS/2
1787 * which skips the byte_count in the DATA block
1788 * on some error responses.
1790 * See bug #9096
1792 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1793 num_iov + 2);
1794 if (iov_tmp == NULL) {
1795 TALLOC_FREE(iov);
1796 return NT_STATUS_NO_MEMORY;
1798 iov = iov_tmp;
1799 cur = &iov[num_iov];
1800 num_iov += 2;
1802 cur[0].iov_len = 0;
1803 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1804 cur[1].iov_len = 0;
1805 cur[1].iov_base = cur[0].iov_base;
1807 taken += needed;
1808 break;
1812 * we need at least BCC
1814 needed += sizeof(uint16_t);
1815 if (len < needed) {
1816 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1817 __location__, (int)len, (int)needed));
1818 goto inval;
1822 * Now we check if the specified bytes are there
1824 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1825 bcc = SVAL(hdr, bcc_ofs);
1826 needed += bcc * sizeof(uint8_t);
1827 if (len < needed) {
1828 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1829 __location__, (int)len, (int)needed));
1830 goto inval;
1834 * we allocate 2 iovec structures for words and bytes
1836 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1837 num_iov + 2);
1838 if (iov_tmp == NULL) {
1839 TALLOC_FREE(iov);
1840 return NT_STATUS_NO_MEMORY;
1842 iov = iov_tmp;
1843 cur = &iov[num_iov];
1844 num_iov += 2;
1846 cur[0].iov_len = wct * sizeof(uint16_t);
1847 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1848 cur[1].iov_len = bcc * sizeof(uint8_t);
1849 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1851 taken += needed;
1853 if (!smb1cli_is_andx_req(cmd)) {
1855 * If the current command does not have AndX chanining
1856 * we are done.
1858 break;
1861 if (wct == 0 && bcc == 0) {
1863 * An empty response also ends the chain,
1864 * most likely with an error.
1866 break;
1869 if (wct < 2) {
1870 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1871 __location__, (int)wct, (int)cmd));
1872 goto inval;
1874 cmd = CVAL(cur[0].iov_base, 0);
1875 if (cmd == 0xFF) {
1877 * If it is the end of the chain we are also done.
1879 break;
1881 wct_ofs = SVAL(cur[0].iov_base, 2);
1883 if (wct_ofs < taken) {
1884 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1886 if (wct_ofs > buflen) {
1887 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1891 * we consumed everything up to the start of the next
1892 * parameter block.
1894 taken = wct_ofs;
1897 remaining = buflen - taken;
1899 if (remaining > 0 && num_iov >= 3) {
1901 * The last DATA block gets the remaining
1902 * bytes, this is needed to support
1903 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1905 iov[num_iov-1].iov_len += remaining;
1908 *piov = iov;
1909 *pnum_iov = num_iov;
1910 return NT_STATUS_OK;
1912 inval:
1913 TALLOC_FREE(iov);
1914 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1917 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1918 TALLOC_CTX *tmp_mem,
1919 uint8_t *inbuf)
1921 struct tevent_req *req;
1922 struct smbXcli_req_state *state;
1923 NTSTATUS status;
1924 size_t num_pending;
1925 size_t i;
1926 uint8_t cmd;
1927 uint16_t mid;
1928 bool oplock_break;
1929 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1930 size_t len = smb_len_tcp(inbuf);
1931 struct iovec *iov = NULL;
1932 int num_iov = 0;
1933 struct tevent_req **chain = NULL;
1934 size_t num_chained = 0;
1935 size_t num_responses = 0;
1937 if (conn->smb1.read_braw_req != NULL) {
1938 req = conn->smb1.read_braw_req;
1939 conn->smb1.read_braw_req = NULL;
1940 state = tevent_req_data(req, struct smbXcli_req_state);
1942 smbXcli_req_unset_pending(req);
1944 if (state->smb1.recv_iov == NULL) {
1946 * For requests with more than
1947 * one response, we have to readd the
1948 * recv_iov array.
1950 state->smb1.recv_iov = talloc_zero_array(state,
1951 struct iovec,
1953 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1954 return NT_STATUS_OK;
1958 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
1959 state->smb1.recv_iov[0].iov_len = len;
1960 ZERO_STRUCT(state->smb1.recv_iov[1]);
1961 ZERO_STRUCT(state->smb1.recv_iov[2]);
1963 state->smb1.recv_cmd = SMBreadBraw;
1964 state->smb1.recv_status = NT_STATUS_OK;
1965 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1967 tevent_req_done(req);
1968 return NT_STATUS_OK;
1971 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1972 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1973 DEBUG(10, ("Got non-SMB PDU\n"));
1974 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1978 * If we supported multiple encrytion contexts
1979 * here we'd look up based on tid.
1981 if (common_encryption_on(conn->smb1.trans_enc)
1982 && (CVAL(inbuf, 0) == 0)) {
1983 uint16_t enc_ctx_num;
1985 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1986 if (!NT_STATUS_IS_OK(status)) {
1987 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1988 nt_errstr(status)));
1989 return status;
1992 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1993 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1994 enc_ctx_num,
1995 conn->smb1.trans_enc->enc_ctx_num));
1996 return NT_STATUS_INVALID_HANDLE;
1999 status = common_decrypt_buffer(conn->smb1.trans_enc,
2000 (char *)inbuf);
2001 if (!NT_STATUS_IS_OK(status)) {
2002 DEBUG(10, ("common_decrypt_buffer returned %s\n",
2003 nt_errstr(status)));
2004 return status;
2006 inhdr = inbuf + NBT_HDR_SIZE;
2007 len = smb_len_nbt(inbuf);
2010 mid = SVAL(inhdr, HDR_MID);
2011 num_pending = talloc_array_length(conn->pending);
2013 for (i=0; i<num_pending; i++) {
2014 if (mid == smb1cli_req_mid(conn->pending[i])) {
2015 break;
2018 if (i == num_pending) {
2019 /* Dump unexpected reply */
2020 return NT_STATUS_RETRY;
2023 oplock_break = false;
2025 if (mid == 0xffff) {
2027 * Paranoia checks that this is really an oplock break request.
2029 oplock_break = (len == 51); /* hdr + 8 words */
2030 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
2031 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
2032 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
2033 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
2035 if (!oplock_break) {
2036 /* Dump unexpected reply */
2037 return NT_STATUS_RETRY;
2041 req = conn->pending[i];
2042 state = tevent_req_data(req, struct smbXcli_req_state);
2044 if (!oplock_break /* oplock breaks are not signed */
2045 && !smb_signing_check_pdu(conn->smb1.signing,
2046 inhdr, len, state->smb1.seqnum+1)) {
2047 DEBUG(10, ("cli_check_sign_mac failed\n"));
2048 return NT_STATUS_ACCESS_DENIED;
2051 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2052 &iov, &num_iov);
2053 if (!NT_STATUS_IS_OK(status)) {
2054 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2055 nt_errstr(status)));
2056 return status;
2059 cmd = CVAL(inhdr, HDR_COM);
2060 status = smb1cli_pull_raw_error(inhdr);
2062 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2063 (state->session != NULL) && state->session->disconnect_expired)
2066 * this should be a short term hack
2067 * until the upper layers have implemented
2068 * re-authentication.
2070 return status;
2073 if (state->smb1.chained_requests == NULL) {
2074 if (num_iov != 3) {
2075 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2078 smbXcli_req_unset_pending(req);
2080 if (state->smb1.recv_iov == NULL) {
2082 * For requests with more than
2083 * one response, we have to readd the
2084 * recv_iov array.
2086 state->smb1.recv_iov = talloc_zero_array(state,
2087 struct iovec,
2089 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2090 return NT_STATUS_OK;
2094 state->smb1.recv_cmd = cmd;
2095 state->smb1.recv_status = status;
2096 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2098 state->smb1.recv_iov[0] = iov[0];
2099 state->smb1.recv_iov[1] = iov[1];
2100 state->smb1.recv_iov[2] = iov[2];
2102 if (talloc_array_length(conn->pending) == 0) {
2103 tevent_req_done(req);
2104 return NT_STATUS_OK;
2107 tevent_req_defer_callback(req, state->ev);
2108 tevent_req_done(req);
2109 return NT_STATUS_RETRY;
2112 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2113 num_chained = talloc_array_length(chain);
2114 num_responses = (num_iov - 1)/2;
2116 if (num_responses > num_chained) {
2117 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2120 for (i=0; i<num_chained; i++) {
2121 size_t iov_idx = 1 + (i*2);
2122 struct iovec *cur = &iov[iov_idx];
2123 uint8_t *inbuf_ref;
2125 req = chain[i];
2126 state = tevent_req_data(req, struct smbXcli_req_state);
2128 smbXcli_req_unset_pending(req);
2131 * as we finish multiple requests here
2132 * we need to defer the callbacks as
2133 * they could destroy our current stack state.
2135 tevent_req_defer_callback(req, state->ev);
2137 if (i >= num_responses) {
2138 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2139 continue;
2142 if (state->smb1.recv_iov == NULL) {
2144 * For requests with more than
2145 * one response, we have to readd the
2146 * recv_iov array.
2148 state->smb1.recv_iov = talloc_zero_array(state,
2149 struct iovec,
2151 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2152 continue;
2156 state->smb1.recv_cmd = cmd;
2158 if (i == (num_responses - 1)) {
2160 * The last request in the chain gets the status
2162 state->smb1.recv_status = status;
2163 } else {
2164 cmd = CVAL(cur[0].iov_base, 0);
2165 state->smb1.recv_status = NT_STATUS_OK;
2168 state->inbuf = inbuf;
2171 * Note: here we use talloc_reference() in a way
2172 * that does not expose it to the caller.
2174 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2175 if (tevent_req_nomem(inbuf_ref, req)) {
2176 continue;
2179 /* copy the related buffers */
2180 state->smb1.recv_iov[0] = iov[0];
2181 state->smb1.recv_iov[1] = cur[0];
2182 state->smb1.recv_iov[2] = cur[1];
2184 tevent_req_done(req);
2187 return NT_STATUS_RETRY;
2190 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2191 TALLOC_CTX *mem_ctx,
2192 struct iovec **piov,
2193 uint8_t **phdr,
2194 uint8_t *pwct,
2195 uint16_t **pvwv,
2196 uint32_t *pvwv_offset,
2197 uint32_t *pnum_bytes,
2198 uint8_t **pbytes,
2199 uint32_t *pbytes_offset,
2200 uint8_t **pinbuf,
2201 const struct smb1cli_req_expected_response *expected,
2202 size_t num_expected)
2204 struct smbXcli_req_state *state =
2205 tevent_req_data(req,
2206 struct smbXcli_req_state);
2207 NTSTATUS status = NT_STATUS_OK;
2208 struct iovec *recv_iov = NULL;
2209 uint8_t *hdr = NULL;
2210 uint8_t wct = 0;
2211 uint32_t vwv_offset = 0;
2212 uint16_t *vwv = NULL;
2213 uint32_t num_bytes = 0;
2214 uint32_t bytes_offset = 0;
2215 uint8_t *bytes = NULL;
2216 size_t i;
2217 bool found_status = false;
2218 bool found_size = false;
2220 if (piov != NULL) {
2221 *piov = NULL;
2223 if (phdr != NULL) {
2224 *phdr = 0;
2226 if (pwct != NULL) {
2227 *pwct = 0;
2229 if (pvwv != NULL) {
2230 *pvwv = NULL;
2232 if (pvwv_offset != NULL) {
2233 *pvwv_offset = 0;
2235 if (pnum_bytes != NULL) {
2236 *pnum_bytes = 0;
2238 if (pbytes != NULL) {
2239 *pbytes = NULL;
2241 if (pbytes_offset != NULL) {
2242 *pbytes_offset = 0;
2244 if (pinbuf != NULL) {
2245 *pinbuf = NULL;
2248 if (state->inbuf != NULL) {
2249 recv_iov = state->smb1.recv_iov;
2250 state->smb1.recv_iov = NULL;
2251 if (state->smb1.recv_cmd != SMBreadBraw) {
2252 hdr = (uint8_t *)recv_iov[0].iov_base;
2253 wct = recv_iov[1].iov_len/2;
2254 vwv = (uint16_t *)recv_iov[1].iov_base;
2255 vwv_offset = PTR_DIFF(vwv, hdr);
2256 num_bytes = recv_iov[2].iov_len;
2257 bytes = (uint8_t *)recv_iov[2].iov_base;
2258 bytes_offset = PTR_DIFF(bytes, hdr);
2262 if (tevent_req_is_nterror(req, &status)) {
2263 for (i=0; i < num_expected; i++) {
2264 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2265 found_status = true;
2266 break;
2270 if (found_status) {
2271 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2274 return status;
2277 if (num_expected == 0) {
2278 found_status = true;
2279 found_size = true;
2282 status = state->smb1.recv_status;
2284 for (i=0; i < num_expected; i++) {
2285 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2286 continue;
2289 found_status = true;
2290 if (expected[i].wct == 0) {
2291 found_size = true;
2292 break;
2295 if (expected[i].wct == wct) {
2296 found_size = true;
2297 break;
2301 if (!found_status) {
2302 return status;
2305 if (!found_size) {
2306 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2309 if (piov != NULL) {
2310 *piov = talloc_move(mem_ctx, &recv_iov);
2313 if (phdr != NULL) {
2314 *phdr = hdr;
2316 if (pwct != NULL) {
2317 *pwct = wct;
2319 if (pvwv != NULL) {
2320 *pvwv = vwv;
2322 if (pvwv_offset != NULL) {
2323 *pvwv_offset = vwv_offset;
2325 if (pnum_bytes != NULL) {
2326 *pnum_bytes = num_bytes;
2328 if (pbytes != NULL) {
2329 *pbytes = bytes;
2331 if (pbytes_offset != NULL) {
2332 *pbytes_offset = bytes_offset;
2334 if (pinbuf != NULL) {
2335 *pinbuf = state->inbuf;
2338 return status;
2341 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2343 size_t wct_ofs;
2344 int i;
2346 wct_ofs = HDR_WCT;
2348 for (i=0; i<num_reqs; i++) {
2349 struct smbXcli_req_state *state;
2350 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2351 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2352 state->smb1.iov_count-2);
2353 wct_ofs = (wct_ofs + 3) & ~3;
2355 return wct_ofs;
2358 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2360 struct smbXcli_req_state *first_state =
2361 tevent_req_data(reqs[0],
2362 struct smbXcli_req_state);
2363 struct smbXcli_req_state *state;
2364 size_t wct_offset;
2365 size_t chain_padding = 0;
2366 int i, iovlen;
2367 struct iovec *iov = NULL;
2368 struct iovec *this_iov;
2369 NTSTATUS status;
2370 ssize_t nbt_len;
2372 if (num_reqs == 1) {
2373 return smb1cli_req_writev_submit(reqs[0], first_state,
2374 first_state->smb1.iov,
2375 first_state->smb1.iov_count);
2378 iovlen = 0;
2379 for (i=0; i<num_reqs; i++) {
2380 if (!tevent_req_is_in_progress(reqs[i])) {
2381 return NT_STATUS_INTERNAL_ERROR;
2384 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2386 if (state->smb1.iov_count < 4) {
2387 return NT_STATUS_INVALID_PARAMETER_MIX;
2390 if (i == 0) {
2392 * The NBT and SMB header
2394 iovlen += 2;
2395 } else {
2397 * Chain padding
2399 iovlen += 1;
2403 * words and bytes
2405 iovlen += state->smb1.iov_count - 2;
2408 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2409 if (iov == NULL) {
2410 return NT_STATUS_NO_MEMORY;
2413 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2414 first_state, reqs, sizeof(*reqs) * num_reqs);
2415 if (first_state->smb1.chained_requests == NULL) {
2416 TALLOC_FREE(iov);
2417 return NT_STATUS_NO_MEMORY;
2420 wct_offset = HDR_WCT;
2421 this_iov = iov;
2423 for (i=0; i<num_reqs; i++) {
2424 size_t next_padding = 0;
2425 uint16_t *vwv;
2427 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2429 if (i < num_reqs-1) {
2430 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2431 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2432 TALLOC_FREE(iov);
2433 TALLOC_FREE(first_state->smb1.chained_requests);
2434 return NT_STATUS_INVALID_PARAMETER_MIX;
2438 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2439 state->smb1.iov_count-2) + 1;
2440 if ((wct_offset % 4) != 0) {
2441 next_padding = 4 - (wct_offset % 4);
2443 wct_offset += next_padding;
2444 vwv = state->smb1.vwv;
2446 if (i < num_reqs-1) {
2447 struct smbXcli_req_state *next_state =
2448 tevent_req_data(reqs[i+1],
2449 struct smbXcli_req_state);
2450 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2451 SCVAL(vwv+0, 1, 0);
2452 SSVAL(vwv+1, 0, wct_offset);
2453 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2454 /* properly end the chain */
2455 SCVAL(vwv+0, 0, 0xff);
2456 SCVAL(vwv+0, 1, 0xff);
2457 SSVAL(vwv+1, 0, 0);
2460 if (i == 0) {
2462 * The NBT and SMB header
2464 this_iov[0] = state->smb1.iov[0];
2465 this_iov[1] = state->smb1.iov[1];
2466 this_iov += 2;
2467 } else {
2469 * This one is a bit subtle. We have to add
2470 * chain_padding bytes between the requests, and we
2471 * have to also include the wct field of the
2472 * subsequent requests. We use the subsequent header
2473 * for the padding, it contains the wct field in its
2474 * last byte.
2476 this_iov[0].iov_len = chain_padding+1;
2477 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2478 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2479 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2480 this_iov += 1;
2484 * copy the words and bytes
2486 memcpy(this_iov, state->smb1.iov+2,
2487 sizeof(struct iovec) * (state->smb1.iov_count-2));
2488 this_iov += state->smb1.iov_count - 2;
2489 chain_padding = next_padding;
2492 nbt_len = iov_buflen(&iov[1], iovlen-1);
2493 if ((nbt_len == -1) || (nbt_len > first_state->conn->smb1.max_xmit)) {
2494 TALLOC_FREE(iov);
2495 TALLOC_FREE(first_state->smb1.chained_requests);
2496 return NT_STATUS_INVALID_PARAMETER_MIX;
2499 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2500 if (!NT_STATUS_IS_OK(status)) {
2501 TALLOC_FREE(iov);
2502 TALLOC_FREE(first_state->smb1.chained_requests);
2503 return status;
2506 return NT_STATUS_OK;
2509 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2511 return ((tevent_queue_length(conn->outgoing) != 0)
2512 || (talloc_array_length(conn->pending) != 0));
2515 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2517 if (conn->protocol >= PROTOCOL_SMB2_02) {
2518 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2521 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2524 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2526 uint16_t credits = 1;
2528 if (conn->smb2.cur_credits == 0) {
2529 if (max_dyn_len != NULL) {
2530 *max_dyn_len = 0;
2532 return false;
2535 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2536 credits = conn->smb2.cur_credits;
2539 if (max_dyn_len != NULL) {
2540 *max_dyn_len = credits * 65536;
2543 return true;
2546 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2548 return conn->smb2.server.capabilities;
2551 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2553 return conn->smb2.server.security_mode;
2556 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2558 return conn->smb2.server.max_trans_size;
2561 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2563 return conn->smb2.server.max_read_size;
2566 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2568 return conn->smb2.server.max_write_size;
2571 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2572 uint16_t max_credits)
2574 conn->smb2.max_credits = max_credits;
2577 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2579 static bool smb2cli_req_cancel(struct tevent_req *req)
2581 struct smbXcli_req_state *state =
2582 tevent_req_data(req,
2583 struct smbXcli_req_state);
2584 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2585 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2586 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2587 struct smbXcli_tcon *tcon = state->tcon;
2588 struct smbXcli_session *session = state->session;
2589 uint8_t *fixed = state->smb2.pad;
2590 uint16_t fixed_len = 4;
2591 struct tevent_req *subreq;
2592 struct smbXcli_req_state *substate;
2593 NTSTATUS status;
2595 SSVAL(fixed, 0, 0x04);
2596 SSVAL(fixed, 2, 0);
2598 subreq = smb2cli_req_create(state, state->ev,
2599 state->conn,
2600 SMB2_OP_CANCEL,
2601 flags, 0,
2602 0, /* timeout */
2603 tcon, session,
2604 fixed, fixed_len,
2605 NULL, 0, 0);
2606 if (subreq == NULL) {
2607 return false;
2609 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2612 * clear everything but the SMB2_HDR_FLAG_ASYNC flag
2613 * e.g. if SMB2_HDR_FLAG_CHAINED is set we get INVALID_PARAMETER back
2615 flags &= SMB2_HDR_FLAG_ASYNC;
2617 if (flags & SMB2_HDR_FLAG_ASYNC) {
2618 mid = 0;
2621 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2622 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2623 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2625 status = smb2cli_req_compound_submit(&subreq, 1);
2626 if (!NT_STATUS_IS_OK(status)) {
2627 TALLOC_FREE(subreq);
2628 return false;
2631 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2633 return true;
2636 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2638 /* we do not care about the result */
2639 TALLOC_FREE(subreq);
2642 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2643 struct tevent_context *ev,
2644 struct smbXcli_conn *conn,
2645 uint16_t cmd,
2646 uint32_t additional_flags,
2647 uint32_t clear_flags,
2648 uint32_t timeout_msec,
2649 struct smbXcli_tcon *tcon,
2650 struct smbXcli_session *session,
2651 const uint8_t *fixed,
2652 uint16_t fixed_len,
2653 const uint8_t *dyn,
2654 uint32_t dyn_len,
2655 uint32_t max_dyn_len)
2657 struct tevent_req *req;
2658 struct smbXcli_req_state *state;
2659 uint32_t flags = 0;
2660 uint32_t tid = 0;
2661 uint64_t uid = 0;
2662 bool use_channel_sequence = false;
2663 uint16_t channel_sequence = 0;
2664 bool use_replay_flag = false;
2666 req = tevent_req_create(mem_ctx, &state,
2667 struct smbXcli_req_state);
2668 if (req == NULL) {
2669 return NULL;
2672 state->ev = ev;
2673 state->conn = conn;
2674 state->session = session;
2675 state->tcon = tcon;
2677 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
2678 use_channel_sequence = true;
2679 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
2680 use_channel_sequence = true;
2683 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_00) {
2684 use_replay_flag = true;
2687 if (session) {
2688 uid = session->smb2->session_id;
2690 if (use_channel_sequence) {
2691 channel_sequence = session->smb2->channel_sequence;
2694 if (use_replay_flag && session->smb2->replay_active) {
2695 additional_flags |= SMB2_HDR_FLAG_REPLAY_OPERATION;
2698 state->smb2.should_sign = session->smb2->should_sign;
2699 state->smb2.should_encrypt = session->smb2->should_encrypt;
2701 if (cmd == SMB2_OP_SESSSETUP &&
2702 session->smb2_channel.signing_key.length == 0 &&
2703 session->smb2->signing_key.length != 0)
2706 * a session bind needs to be signed
2708 state->smb2.should_sign = true;
2711 if (cmd == SMB2_OP_SESSSETUP &&
2712 session->smb2_channel.signing_key.length == 0) {
2713 state->smb2.should_encrypt = false;
2716 if (additional_flags & SMB2_HDR_FLAG_SIGNED) {
2717 if (session->smb2_channel.signing_key.length == 0) {
2718 tevent_req_nterror(req, NT_STATUS_NO_USER_SESSION_KEY);
2719 return req;
2722 additional_flags &= ~SMB2_HDR_FLAG_SIGNED;
2723 state->smb2.should_sign = true;
2727 if (tcon) {
2728 tid = tcon->smb2.tcon_id;
2730 if (tcon->smb2.should_sign) {
2731 state->smb2.should_sign = true;
2733 if (tcon->smb2.should_encrypt) {
2734 state->smb2.should_encrypt = true;
2738 if (state->smb2.should_encrypt) {
2739 state->smb2.should_sign = false;
2742 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2743 if (state->smb2.recv_iov == NULL) {
2744 TALLOC_FREE(req);
2745 return NULL;
2748 flags |= additional_flags;
2749 flags &= ~clear_flags;
2751 state->smb2.fixed = fixed;
2752 state->smb2.fixed_len = fixed_len;
2753 state->smb2.dyn = dyn;
2754 state->smb2.dyn_len = dyn_len;
2755 state->smb2.max_dyn_len = max_dyn_len;
2757 if (state->smb2.should_encrypt) {
2758 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2759 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2762 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2763 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2764 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2765 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
2766 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2767 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
2768 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2769 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2771 switch (cmd) {
2772 case SMB2_OP_CANCEL:
2773 state->one_way = true;
2774 break;
2775 case SMB2_OP_BREAK:
2777 * If this is a dummy request, it will have
2778 * UINT64_MAX as message id.
2779 * If we send on break acknowledgement,
2780 * this gets overwritten later.
2782 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2783 break;
2786 if (timeout_msec > 0) {
2787 struct timeval endtime;
2789 endtime = timeval_current_ofs_msec(timeout_msec);
2790 if (!tevent_req_set_endtime(req, ev, endtime)) {
2791 return req;
2795 return req;
2798 void smb2cli_req_set_notify_async(struct tevent_req *req)
2800 struct smbXcli_req_state *state =
2801 tevent_req_data(req,
2802 struct smbXcli_req_state);
2804 state->smb2.notify_async = true;
2807 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2808 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2809 TALLOC_CTX *tmp_mem,
2810 uint8_t *inbuf);
2812 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2813 int num_reqs)
2815 struct smbXcli_req_state *state;
2816 struct tevent_req *subreq;
2817 struct iovec *iov;
2818 int i, num_iov, nbt_len;
2819 int tf_iov = -1;
2820 const DATA_BLOB *encryption_key = NULL;
2821 uint64_t encryption_session_id = 0;
2824 * 1 for the nbt length, optional TRANSFORM
2825 * per request: HDR, fixed, dyn, padding
2826 * -1 because the last one does not need padding
2829 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
2830 if (iov == NULL) {
2831 return NT_STATUS_NO_MEMORY;
2834 num_iov = 1;
2835 nbt_len = 0;
2838 * the session of the first request that requires encryption
2839 * specifies the encryption key.
2841 for (i=0; i<num_reqs; i++) {
2842 if (!tevent_req_is_in_progress(reqs[i])) {
2843 return NT_STATUS_INTERNAL_ERROR;
2846 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2848 if (!smbXcli_conn_is_connected(state->conn)) {
2849 return NT_STATUS_CONNECTION_DISCONNECTED;
2852 if ((state->conn->protocol != PROTOCOL_NONE) &&
2853 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2854 return NT_STATUS_REVISION_MISMATCH;
2857 if (state->session == NULL) {
2858 continue;
2861 if (!state->smb2.should_encrypt) {
2862 continue;
2865 encryption_key = &state->session->smb2->encryption_key;
2866 if (encryption_key->length == 0) {
2867 return NT_STATUS_INVALID_PARAMETER_MIX;
2870 encryption_session_id = state->session->smb2->session_id;
2872 tf_iov = num_iov;
2873 iov[num_iov].iov_base = state->smb2.transform;
2874 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2875 num_iov += 1;
2877 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2878 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
2879 state->session->smb2->nonce_low);
2880 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2881 state->session->smb2->nonce_high);
2882 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
2883 encryption_session_id);
2885 state->session->smb2->nonce_low += 1;
2886 if (state->session->smb2->nonce_low == 0) {
2887 state->session->smb2->nonce_high += 1;
2888 state->session->smb2->nonce_low += 1;
2891 nbt_len += SMB2_TF_HDR_SIZE;
2892 break;
2895 for (i=0; i<num_reqs; i++) {
2896 int hdr_iov;
2897 size_t reqlen;
2898 bool ret;
2899 uint16_t opcode;
2900 uint64_t avail;
2901 uint16_t charge;
2902 uint16_t credits;
2903 uint64_t mid;
2904 const DATA_BLOB *signing_key = NULL;
2906 if (!tevent_req_is_in_progress(reqs[i])) {
2907 return NT_STATUS_INTERNAL_ERROR;
2910 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2912 if (!smbXcli_conn_is_connected(state->conn)) {
2913 return NT_STATUS_CONNECTION_DISCONNECTED;
2916 if ((state->conn->protocol != PROTOCOL_NONE) &&
2917 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2918 return NT_STATUS_REVISION_MISMATCH;
2921 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2922 if (opcode == SMB2_OP_CANCEL) {
2923 goto skip_credits;
2926 avail = UINT64_MAX - state->conn->smb2.mid;
2927 if (avail < 1) {
2928 return NT_STATUS_CONNECTION_ABORTED;
2931 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2932 uint32_t max_dyn_len = 1;
2934 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
2935 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
2937 charge = (max_dyn_len - 1)/ 65536 + 1;
2938 } else {
2939 charge = 1;
2942 charge = MAX(state->smb2.credit_charge, charge);
2944 avail = MIN(avail, state->conn->smb2.cur_credits);
2945 if (avail < charge) {
2946 return NT_STATUS_INTERNAL_ERROR;
2949 credits = 0;
2950 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2951 credits = state->conn->smb2.max_credits -
2952 state->conn->smb2.cur_credits;
2954 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2955 credits += 1;
2958 mid = state->conn->smb2.mid;
2959 state->conn->smb2.mid += charge;
2960 state->conn->smb2.cur_credits -= charge;
2962 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2963 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2965 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2966 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2968 skip_credits:
2969 if (state->session && encryption_key == NULL) {
2971 * We prefer the channel signing key if it is
2972 * already there.
2974 if (state->smb2.should_sign) {
2975 signing_key = &state->session->smb2_channel.signing_key;
2979 * If it is a channel binding, we already have the main
2980 * signing key and try that one.
2982 if (signing_key && signing_key->length == 0) {
2983 signing_key = &state->session->smb2->signing_key;
2987 * If we do not have any session key yet, we skip the
2988 * signing of SMB2_OP_SESSSETUP requests.
2990 if (signing_key && signing_key->length == 0) {
2991 signing_key = NULL;
2995 hdr_iov = num_iov;
2996 iov[num_iov].iov_base = state->smb2.hdr;
2997 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2998 num_iov += 1;
3000 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
3001 iov[num_iov].iov_len = state->smb2.fixed_len;
3002 num_iov += 1;
3004 if (state->smb2.dyn != NULL) {
3005 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
3006 iov[num_iov].iov_len = state->smb2.dyn_len;
3007 num_iov += 1;
3010 reqlen = sizeof(state->smb2.hdr);
3011 reqlen += state->smb2.fixed_len;
3012 reqlen += state->smb2.dyn_len;
3014 if (i < num_reqs-1) {
3015 if ((reqlen % 8) > 0) {
3016 uint8_t pad = 8 - (reqlen % 8);
3017 iov[num_iov].iov_base = state->smb2.pad;
3018 iov[num_iov].iov_len = pad;
3019 num_iov += 1;
3020 reqlen += pad;
3022 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
3025 state->smb2.encryption_session_id = encryption_session_id;
3027 if (signing_key != NULL) {
3028 NTSTATUS status;
3030 status = smb2_signing_sign_pdu(*signing_key,
3031 state->session->conn->protocol,
3032 &iov[hdr_iov], num_iov - hdr_iov);
3033 if (!NT_STATUS_IS_OK(status)) {
3034 return status;
3038 nbt_len += reqlen;
3040 ret = smbXcli_req_set_pending(reqs[i]);
3041 if (!ret) {
3042 return NT_STATUS_NO_MEMORY;
3046 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
3047 _smb_setlen_tcp(state->length_hdr, nbt_len);
3048 iov[0].iov_base = state->length_hdr;
3049 iov[0].iov_len = sizeof(state->length_hdr);
3051 if (encryption_key != NULL) {
3052 NTSTATUS status;
3053 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
3054 uint8_t *buf;
3055 int vi;
3057 buf = talloc_array(iov, uint8_t, buflen);
3058 if (buf == NULL) {
3059 return NT_STATUS_NO_MEMORY;
3063 * We copy the buffers before encrypting them,
3064 * this is at least currently needed for the
3065 * to keep state->smb2.hdr.
3067 * Also the callers may expect there buffers
3068 * to be const.
3070 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3071 struct iovec *v = &iov[vi];
3072 const uint8_t *o = (const uint8_t *)v->iov_base;
3074 memcpy(buf, o, v->iov_len);
3075 v->iov_base = (void *)buf;
3076 buf += v->iov_len;
3079 status = smb2_signing_encrypt_pdu(*encryption_key,
3080 state->conn->smb2.server.cipher,
3081 &iov[tf_iov], num_iov - tf_iov);
3082 if (!NT_STATUS_IS_OK(status)) {
3083 return status;
3087 if (state->conn->dispatch_incoming == NULL) {
3088 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3091 subreq = writev_send(state, state->ev, state->conn->outgoing,
3092 state->conn->write_fd, false, iov, num_iov);
3093 if (subreq == NULL) {
3094 return NT_STATUS_NO_MEMORY;
3096 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3097 return NT_STATUS_OK;
3100 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3102 struct smbXcli_req_state *state =
3103 tevent_req_data(req,
3104 struct smbXcli_req_state);
3106 state->smb2.credit_charge = charge;
3109 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3110 struct tevent_context *ev,
3111 struct smbXcli_conn *conn,
3112 uint16_t cmd,
3113 uint32_t additional_flags,
3114 uint32_t clear_flags,
3115 uint32_t timeout_msec,
3116 struct smbXcli_tcon *tcon,
3117 struct smbXcli_session *session,
3118 const uint8_t *fixed,
3119 uint16_t fixed_len,
3120 const uint8_t *dyn,
3121 uint32_t dyn_len,
3122 uint32_t max_dyn_len)
3124 struct tevent_req *req;
3125 NTSTATUS status;
3127 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3128 additional_flags, clear_flags,
3129 timeout_msec,
3130 tcon, session,
3131 fixed, fixed_len,
3132 dyn, dyn_len,
3133 max_dyn_len);
3134 if (req == NULL) {
3135 return NULL;
3137 if (!tevent_req_is_in_progress(req)) {
3138 return tevent_req_post(req, ev);
3140 status = smb2cli_req_compound_submit(&req, 1);
3141 if (tevent_req_nterror(req, status)) {
3142 return tevent_req_post(req, ev);
3144 return req;
3147 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3149 struct tevent_req *req =
3150 tevent_req_callback_data(subreq,
3151 struct tevent_req);
3152 struct smbXcli_req_state *state =
3153 tevent_req_data(req,
3154 struct smbXcli_req_state);
3155 ssize_t nwritten;
3156 int err;
3158 nwritten = writev_recv(subreq, &err);
3159 TALLOC_FREE(subreq);
3160 if (nwritten == -1) {
3161 /* here, we need to notify all pending requests */
3162 NTSTATUS status = map_nt_error_from_unix_common(err);
3163 smbXcli_conn_disconnect(state->conn, status);
3164 return;
3168 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3169 uint8_t *buf,
3170 size_t buflen,
3171 TALLOC_CTX *mem_ctx,
3172 struct iovec **piov, int *pnum_iov)
3174 struct iovec *iov;
3175 int num_iov = 0;
3176 size_t taken = 0;
3177 uint8_t *first_hdr = buf;
3178 size_t verified_buflen = 0;
3179 uint8_t *tf = NULL;
3180 size_t tf_len = 0;
3182 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3183 if (iov == NULL) {
3184 return NT_STATUS_NO_MEMORY;
3187 while (taken < buflen) {
3188 size_t len = buflen - taken;
3189 uint8_t *hdr = first_hdr + taken;
3190 struct iovec *cur;
3191 size_t full_size;
3192 size_t next_command_ofs;
3193 uint16_t body_size;
3194 struct iovec *iov_tmp;
3196 if (verified_buflen > taken) {
3197 len = verified_buflen - taken;
3198 } else {
3199 tf = NULL;
3200 tf_len = 0;
3203 if (len < 4) {
3204 DEBUG(10, ("%d bytes left, expected at least %d\n",
3205 (int)len, 4));
3206 goto inval;
3208 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3209 struct smbXcli_session *s;
3210 uint64_t uid;
3211 struct iovec tf_iov[2];
3212 size_t enc_len;
3213 NTSTATUS status;
3215 if (len < SMB2_TF_HDR_SIZE) {
3216 DEBUG(10, ("%d bytes left, expected at least %d\n",
3217 (int)len, SMB2_TF_HDR_SIZE));
3218 goto inval;
3220 tf = hdr;
3221 tf_len = SMB2_TF_HDR_SIZE;
3222 taken += tf_len;
3224 hdr = first_hdr + taken;
3225 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3226 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3228 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3229 DEBUG(10, ("%d bytes left, expected at least %d\n",
3230 (int)len,
3231 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3232 goto inval;
3235 s = conn->sessions;
3236 for (; s; s = s->next) {
3237 if (s->smb2->session_id != uid) {
3238 continue;
3240 break;
3243 if (s == NULL) {
3244 DEBUG(10, ("unknown session_id %llu\n",
3245 (unsigned long long)uid));
3246 goto inval;
3249 tf_iov[0].iov_base = (void *)tf;
3250 tf_iov[0].iov_len = tf_len;
3251 tf_iov[1].iov_base = (void *)hdr;
3252 tf_iov[1].iov_len = enc_len;
3254 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3255 conn->smb2.server.cipher,
3256 tf_iov, 2);
3257 if (!NT_STATUS_IS_OK(status)) {
3258 TALLOC_FREE(iov);
3259 return status;
3262 verified_buflen = taken + enc_len;
3263 len = enc_len;
3267 * We need the header plus the body length field
3270 if (len < SMB2_HDR_BODY + 2) {
3271 DEBUG(10, ("%d bytes left, expected at least %d\n",
3272 (int)len, SMB2_HDR_BODY));
3273 goto inval;
3275 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3276 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3277 IVAL(hdr, 0)));
3278 goto inval;
3280 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3281 DEBUG(10, ("Got HDR len %d, expected %d\n",
3282 SVAL(hdr, 4), SMB2_HDR_BODY));
3283 goto inval;
3286 full_size = len;
3287 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3288 body_size = SVAL(hdr, SMB2_HDR_BODY);
3290 if (next_command_ofs != 0) {
3291 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3292 goto inval;
3294 if (next_command_ofs > full_size) {
3295 goto inval;
3297 full_size = next_command_ofs;
3299 if (body_size < 2) {
3300 goto inval;
3302 body_size &= 0xfffe;
3304 if (body_size > (full_size - SMB2_HDR_BODY)) {
3305 goto inval;
3308 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3309 num_iov + 4);
3310 if (iov_tmp == NULL) {
3311 TALLOC_FREE(iov);
3312 return NT_STATUS_NO_MEMORY;
3314 iov = iov_tmp;
3315 cur = &iov[num_iov];
3316 num_iov += 4;
3318 cur[0].iov_base = tf;
3319 cur[0].iov_len = tf_len;
3320 cur[1].iov_base = hdr;
3321 cur[1].iov_len = SMB2_HDR_BODY;
3322 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3323 cur[2].iov_len = body_size;
3324 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3325 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3327 taken += full_size;
3330 *piov = iov;
3331 *pnum_iov = num_iov;
3332 return NT_STATUS_OK;
3334 inval:
3335 TALLOC_FREE(iov);
3336 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3339 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3340 uint64_t mid)
3342 size_t num_pending = talloc_array_length(conn->pending);
3343 size_t i;
3345 for (i=0; i<num_pending; i++) {
3346 struct tevent_req *req = conn->pending[i];
3347 struct smbXcli_req_state *state =
3348 tevent_req_data(req,
3349 struct smbXcli_req_state);
3351 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3352 return req;
3355 return NULL;
3358 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3359 TALLOC_CTX *tmp_mem,
3360 uint8_t *inbuf)
3362 struct tevent_req *req;
3363 struct smbXcli_req_state *state = NULL;
3364 struct iovec *iov;
3365 int i, num_iov;
3366 NTSTATUS status;
3367 bool defer = true;
3368 struct smbXcli_session *last_session = NULL;
3369 size_t inbuf_len = smb_len_tcp(inbuf);
3371 status = smb2cli_inbuf_parse_compound(conn,
3372 inbuf + NBT_HDR_SIZE,
3373 inbuf_len,
3374 tmp_mem,
3375 &iov, &num_iov);
3376 if (!NT_STATUS_IS_OK(status)) {
3377 return status;
3380 for (i=0; i<num_iov; i+=4) {
3381 uint8_t *inbuf_ref = NULL;
3382 struct iovec *cur = &iov[i];
3383 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3384 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3385 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3386 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3387 uint16_t req_opcode;
3388 uint32_t req_flags;
3389 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3390 uint32_t new_credits;
3391 struct smbXcli_session *session = NULL;
3392 const DATA_BLOB *signing_key = NULL;
3393 bool was_encrypted = false;
3395 new_credits = conn->smb2.cur_credits;
3396 new_credits += credits;
3397 if (new_credits > UINT16_MAX) {
3398 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3400 conn->smb2.cur_credits += credits;
3402 req = smb2cli_conn_find_pending(conn, mid);
3403 if (req == NULL) {
3404 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3406 state = tevent_req_data(req, struct smbXcli_req_state);
3408 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3409 if (opcode != req_opcode) {
3410 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3412 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3414 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3415 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3418 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3419 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3420 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3421 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3423 if (state->smb2.got_async) {
3424 /* We only expect one STATUS_PENDING response */
3425 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3427 state->smb2.got_async = true;
3430 * async interim responses are not signed,
3431 * even if the SMB2_HDR_FLAG_SIGNED flag
3432 * is set.
3434 req_flags |= SMB2_HDR_FLAG_ASYNC;
3435 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3436 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3438 if (state->smb2.notify_async) {
3439 tevent_req_defer_callback(req, state->ev);
3440 tevent_req_notify_callback(req);
3442 continue;
3445 session = state->session;
3446 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3447 session = last_session;
3449 last_session = session;
3451 if (state->smb2.should_sign) {
3452 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3453 return NT_STATUS_ACCESS_DENIED;
3457 if (flags & SMB2_HDR_FLAG_SIGNED) {
3458 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3460 if (session == NULL) {
3461 struct smbXcli_session *s;
3463 s = state->conn->sessions;
3464 for (; s; s = s->next) {
3465 if (s->smb2->session_id != uid) {
3466 continue;
3469 session = s;
3470 break;
3474 if (session == NULL) {
3475 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3478 last_session = session;
3479 signing_key = &session->smb2_channel.signing_key;
3482 if (opcode == SMB2_OP_SESSSETUP) {
3484 * We prefer the channel signing key, if it is
3485 * already there.
3487 * If we do not have a channel signing key yet,
3488 * we try the main signing key, if it is not
3489 * the final response.
3491 if (signing_key && signing_key->length == 0 &&
3492 !NT_STATUS_IS_OK(status)) {
3493 signing_key = &session->smb2->signing_key;
3496 if (signing_key && signing_key->length == 0) {
3498 * If we do not have a session key to
3499 * verify the signature, we defer the
3500 * signing check to the caller.
3502 * The caller gets NT_STATUS_OK, it
3503 * has to call
3504 * smb2cli_session_set_session_key()
3505 * or
3506 * smb2cli_session_set_channel_key()
3507 * which will check the signature
3508 * with the channel signing key.
3510 signing_key = NULL;
3514 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3515 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3516 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3519 * If the response was encrypted in a SMB2_TRANSFORM
3520 * pdu, which belongs to the correct session,
3521 * we do not need to do signing checks
3523 * It could be the session the response belongs to
3524 * or the session that was used to encrypt the
3525 * SMB2_TRANSFORM request.
3527 if ((session && session->smb2->session_id == uid) ||
3528 (state->smb2.encryption_session_id == uid)) {
3529 signing_key = NULL;
3530 was_encrypted = true;
3534 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3536 * if the server returns NT_STATUS_USER_SESSION_DELETED
3537 * the response is not signed and we should
3538 * propagate the NT_STATUS_USER_SESSION_DELETED
3539 * status to the caller.
3541 state->smb2.signing_skipped = true;
3542 signing_key = NULL;
3545 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3547 * if the server returns
3548 * NT_STATUS_INVALID_PARAMETER
3549 * the response might not be encrypted.
3551 if (state->smb2.should_encrypt && !was_encrypted) {
3552 state->smb2.signing_skipped = true;
3553 signing_key = NULL;
3557 if (state->smb2.should_encrypt && !was_encrypted) {
3558 if (!state->smb2.signing_skipped) {
3559 return NT_STATUS_ACCESS_DENIED;
3563 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3564 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3565 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3567 * if the server returns
3568 * NT_STATUS_NETWORK_NAME_DELETED
3569 * NT_STATUS_FILE_CLOSED
3570 * NT_STATUS_INVALID_PARAMETER
3571 * the response might not be signed
3572 * as this happens before the signing checks.
3574 * If server echos the signature (or all zeros)
3575 * we should report the status from the server
3576 * to the caller.
3578 if (signing_key) {
3579 int cmp;
3581 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3582 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3583 16);
3584 if (cmp == 0) {
3585 state->smb2.signing_skipped = true;
3586 signing_key = NULL;
3589 if (signing_key) {
3590 int cmp;
3591 static const uint8_t zeros[16];
3593 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3594 zeros,
3595 16);
3596 if (cmp == 0) {
3597 state->smb2.signing_skipped = true;
3598 signing_key = NULL;
3603 if (signing_key) {
3604 status = smb2_signing_check_pdu(*signing_key,
3605 state->conn->protocol,
3606 &cur[1], 3);
3607 if (!NT_STATUS_IS_OK(status)) {
3609 * If the signing check fails, we disconnect
3610 * the connection.
3612 return status;
3616 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
3617 (session != NULL) && session->disconnect_expired)
3620 * this should be a short term hack
3621 * until the upper layers have implemented
3622 * re-authentication.
3624 return status;
3627 smbXcli_req_unset_pending(req);
3630 * There might be more than one response
3631 * we need to defer the notifications
3633 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3634 defer = false;
3637 if (defer) {
3638 tevent_req_defer_callback(req, state->ev);
3642 * Note: here we use talloc_reference() in a way
3643 * that does not expose it to the caller.
3645 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3646 if (tevent_req_nomem(inbuf_ref, req)) {
3647 continue;
3650 /* copy the related buffers */
3651 state->smb2.recv_iov[0] = cur[1];
3652 state->smb2.recv_iov[1] = cur[2];
3653 state->smb2.recv_iov[2] = cur[3];
3655 tevent_req_done(req);
3658 if (defer) {
3659 return NT_STATUS_RETRY;
3662 return NT_STATUS_OK;
3665 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3666 struct iovec **piov,
3667 const struct smb2cli_req_expected_response *expected,
3668 size_t num_expected)
3670 struct smbXcli_req_state *state =
3671 tevent_req_data(req,
3672 struct smbXcli_req_state);
3673 NTSTATUS status;
3674 size_t body_size;
3675 bool found_status = false;
3676 bool found_size = false;
3677 size_t i;
3679 if (piov != NULL) {
3680 *piov = NULL;
3683 if (tevent_req_is_in_progress(req) && state->smb2.got_async) {
3684 return STATUS_PENDING;
3687 if (tevent_req_is_nterror(req, &status)) {
3688 for (i=0; i < num_expected; i++) {
3689 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3690 found_status = true;
3691 break;
3695 if (found_status) {
3696 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3699 return status;
3702 if (num_expected == 0) {
3703 found_status = true;
3704 found_size = true;
3707 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3708 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3710 for (i=0; i < num_expected; i++) {
3711 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3712 continue;
3715 found_status = true;
3716 if (expected[i].body_size == 0) {
3717 found_size = true;
3718 break;
3721 if (expected[i].body_size == body_size) {
3722 found_size = true;
3723 break;
3727 if (!found_status) {
3728 return status;
3731 if (state->smb2.signing_skipped) {
3732 if (num_expected > 0) {
3733 return NT_STATUS_ACCESS_DENIED;
3735 if (!NT_STATUS_IS_ERR(status)) {
3736 return NT_STATUS_ACCESS_DENIED;
3740 if (!found_size) {
3741 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3744 if (piov != NULL) {
3745 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3748 return status;
3751 NTSTATUS smb2cli_req_get_sent_iov(struct tevent_req *req,
3752 struct iovec *sent_iov)
3754 struct smbXcli_req_state *state =
3755 tevent_req_data(req,
3756 struct smbXcli_req_state);
3758 if (tevent_req_is_in_progress(req)) {
3759 return STATUS_PENDING;
3762 sent_iov[0].iov_base = state->smb2.hdr;
3763 sent_iov[0].iov_len = sizeof(state->smb2.hdr);
3765 sent_iov[1].iov_base = discard_const(state->smb2.fixed);
3766 sent_iov[1].iov_len = state->smb2.fixed_len;
3768 if (state->smb2.dyn != NULL) {
3769 sent_iov[2].iov_base = discard_const(state->smb2.dyn);
3770 sent_iov[2].iov_len = state->smb2.dyn_len;
3771 } else {
3772 sent_iov[2].iov_base = NULL;
3773 sent_iov[2].iov_len = 0;
3776 return NT_STATUS_OK;
3779 static const struct {
3780 enum protocol_types proto;
3781 const char *smb1_name;
3782 } smb1cli_prots[] = {
3783 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3784 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3785 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3786 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3787 {PROTOCOL_LANMAN2, "LM1.2X002"},
3788 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3789 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3790 {PROTOCOL_LANMAN2, "Samba"},
3791 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3792 {PROTOCOL_NT1, "NT LM 0.12"},
3793 {PROTOCOL_SMB2_02, "SMB 2.002"},
3794 {PROTOCOL_SMB2_10, "SMB 2.???"},
3797 static const struct {
3798 enum protocol_types proto;
3799 uint16_t smb2_dialect;
3800 } smb2cli_prots[] = {
3801 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3802 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3803 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3804 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3805 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3806 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
3807 {PROTOCOL_SMB3_10, SMB3_DIALECT_REVISION_310},
3810 struct smbXcli_negprot_state {
3811 struct smbXcli_conn *conn;
3812 struct tevent_context *ev;
3813 uint32_t timeout_msec;
3815 struct {
3816 uint8_t fixed[36];
3817 } smb2;
3820 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3821 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3822 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3823 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3824 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3825 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3826 TALLOC_CTX *frame,
3827 uint8_t *inbuf);
3829 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3830 struct tevent_context *ev,
3831 struct smbXcli_conn *conn,
3832 uint32_t timeout_msec,
3833 enum protocol_types min_protocol,
3834 enum protocol_types max_protocol)
3836 struct tevent_req *req, *subreq;
3837 struct smbXcli_negprot_state *state;
3839 req = tevent_req_create(mem_ctx, &state,
3840 struct smbXcli_negprot_state);
3841 if (req == NULL) {
3842 return NULL;
3844 state->conn = conn;
3845 state->ev = ev;
3846 state->timeout_msec = timeout_msec;
3848 if (min_protocol == PROTOCOL_NONE) {
3849 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3850 return tevent_req_post(req, ev);
3853 if (max_protocol == PROTOCOL_NONE) {
3854 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3855 return tevent_req_post(req, ev);
3858 if (min_protocol > max_protocol) {
3859 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3860 return tevent_req_post(req, ev);
3863 conn->min_protocol = min_protocol;
3864 conn->max_protocol = max_protocol;
3865 conn->protocol = PROTOCOL_NONE;
3867 if ((min_protocol < PROTOCOL_SMB2_02) &&
3868 (max_protocol < PROTOCOL_SMB2_02)) {
3870 * SMB1 only...
3872 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3874 subreq = smbXcli_negprot_smb1_subreq(state);
3875 if (tevent_req_nomem(subreq, req)) {
3876 return tevent_req_post(req, ev);
3878 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3879 return req;
3882 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3883 (max_protocol >= PROTOCOL_SMB2_02)) {
3885 * SMB2 only...
3887 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3890 * As we're starting with an SMB2 negprot, emulate Windows
3891 * and ask for 31 credits in the initial SMB2 negprot.
3892 * If we don't and leave requested credits at
3893 * zero, MacOSX servers return zero credits on
3894 * the negprot reply and we fail to connect.
3896 smb2cli_conn_set_max_credits(conn,
3897 WINDOWS_CLIENT_PURE_SMB2_NEGPROT_INITIAL_CREDIT_ASK);
3899 subreq = smbXcli_negprot_smb2_subreq(state);
3900 if (tevent_req_nomem(subreq, req)) {
3901 return tevent_req_post(req, ev);
3903 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3904 return req;
3908 * We send an SMB1 negprot with the SMB2 dialects
3909 * and expect a SMB1 or a SMB2 response.
3911 * smbXcli_negprot_dispatch_incoming() will fix the
3912 * callback to match protocol of the response.
3914 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3916 subreq = smbXcli_negprot_smb1_subreq(state);
3917 if (tevent_req_nomem(subreq, req)) {
3918 return tevent_req_post(req, ev);
3920 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3921 return req;
3924 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3926 struct tevent_req *req =
3927 tevent_req_callback_data(subreq,
3928 struct tevent_req);
3929 NTSTATUS status;
3932 * we just want the low level error
3934 status = tevent_req_simple_recv_ntstatus(subreq);
3935 TALLOC_FREE(subreq);
3936 if (tevent_req_nterror(req, status)) {
3937 return;
3940 /* this should never happen */
3941 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3944 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3946 size_t i;
3947 DATA_BLOB bytes = data_blob_null;
3948 uint8_t flags;
3949 uint16_t flags2;
3951 /* setup the protocol strings */
3952 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3953 uint8_t c = 2;
3954 bool ok;
3956 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
3957 continue;
3960 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
3961 continue;
3964 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3965 if (!ok) {
3966 return NULL;
3970 * We now it is already ascii and
3971 * we want NULL termination.
3973 ok = data_blob_append(state, &bytes,
3974 smb1cli_prots[i].smb1_name,
3975 strlen(smb1cli_prots[i].smb1_name)+1);
3976 if (!ok) {
3977 return NULL;
3981 smb1cli_req_flags(state->conn->max_protocol,
3982 state->conn->smb1.client.capabilities,
3983 SMBnegprot,
3984 0, 0, &flags,
3985 0, 0, &flags2);
3987 return smb1cli_req_send(state, state->ev, state->conn,
3988 SMBnegprot,
3989 flags, ~flags,
3990 flags2, ~flags2,
3991 state->timeout_msec,
3992 0xFFFE, 0, NULL, /* pid, tid, session */
3993 0, NULL, /* wct, vwv */
3994 bytes.length, bytes.data);
3997 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3999 struct tevent_req *req =
4000 tevent_req_callback_data(subreq,
4001 struct tevent_req);
4002 struct smbXcli_negprot_state *state =
4003 tevent_req_data(req,
4004 struct smbXcli_negprot_state);
4005 struct smbXcli_conn *conn = state->conn;
4006 struct iovec *recv_iov = NULL;
4007 uint8_t *inhdr;
4008 uint8_t wct;
4009 uint16_t *vwv;
4010 uint32_t num_bytes;
4011 uint8_t *bytes;
4012 NTSTATUS status;
4013 uint16_t protnum;
4014 size_t i;
4015 size_t num_prots = 0;
4016 uint8_t flags;
4017 uint32_t client_capabilities = conn->smb1.client.capabilities;
4018 uint32_t both_capabilities;
4019 uint32_t server_capabilities = 0;
4020 uint32_t capabilities;
4021 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
4022 uint32_t server_max_xmit = 0;
4023 uint32_t max_xmit;
4024 uint32_t server_max_mux = 0;
4025 uint16_t server_security_mode = 0;
4026 uint32_t server_session_key = 0;
4027 bool server_readbraw = false;
4028 bool server_writebraw = false;
4029 bool server_lockread = false;
4030 bool server_writeunlock = false;
4031 struct GUID server_guid = GUID_zero();
4032 DATA_BLOB server_gss_blob = data_blob_null;
4033 uint8_t server_challenge[8];
4034 char *server_workgroup = NULL;
4035 char *server_name = NULL;
4036 int server_time_zone = 0;
4037 NTTIME server_system_time = 0;
4038 static const struct smb1cli_req_expected_response expected[] = {
4040 .status = NT_STATUS_OK,
4041 .wct = 0x11, /* NT1 */
4044 .status = NT_STATUS_OK,
4045 .wct = 0x0D, /* LM */
4048 .status = NT_STATUS_OK,
4049 .wct = 0x01, /* CORE */
4053 ZERO_STRUCT(server_challenge);
4055 status = smb1cli_req_recv(subreq, state,
4056 &recv_iov,
4057 &inhdr,
4058 &wct,
4059 &vwv,
4060 NULL, /* pvwv_offset */
4061 &num_bytes,
4062 &bytes,
4063 NULL, /* pbytes_offset */
4064 NULL, /* pinbuf */
4065 expected, ARRAY_SIZE(expected));
4066 TALLOC_FREE(subreq);
4067 if (tevent_req_nterror(req, status)) {
4068 return;
4071 flags = CVAL(inhdr, HDR_FLG);
4073 protnum = SVAL(vwv, 0);
4075 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4076 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4077 continue;
4080 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4081 continue;
4084 if (protnum != num_prots) {
4085 num_prots++;
4086 continue;
4089 conn->protocol = smb1cli_prots[i].proto;
4090 break;
4093 if (conn->protocol == PROTOCOL_NONE) {
4094 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4095 return;
4098 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
4099 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4100 "and the selected protocol level doesn't support it.\n"));
4101 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4102 return;
4105 if (flags & FLAG_SUPPORT_LOCKREAD) {
4106 server_lockread = true;
4107 server_writeunlock = true;
4110 if (conn->protocol >= PROTOCOL_NT1) {
4111 const char *client_signing = NULL;
4112 bool server_mandatory = false;
4113 bool server_allowed = false;
4114 const char *server_signing = NULL;
4115 bool ok;
4116 uint8_t key_len;
4118 if (wct != 0x11) {
4119 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4120 return;
4123 /* NT protocol */
4124 server_security_mode = CVAL(vwv + 1, 0);
4125 server_max_mux = SVAL(vwv + 1, 1);
4126 server_max_xmit = IVAL(vwv + 3, 1);
4127 server_session_key = IVAL(vwv + 7, 1);
4128 server_time_zone = SVALS(vwv + 15, 1);
4129 server_time_zone *= 60;
4130 /* this time arrives in real GMT */
4131 server_system_time = BVAL(vwv + 11, 1);
4132 server_capabilities = IVAL(vwv + 9, 1);
4134 key_len = CVAL(vwv + 16, 1);
4136 if (server_capabilities & CAP_RAW_MODE) {
4137 server_readbraw = true;
4138 server_writebraw = true;
4140 if (server_capabilities & CAP_LOCK_AND_READ) {
4141 server_lockread = true;
4144 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4145 DATA_BLOB blob1, blob2;
4147 if (num_bytes < 16) {
4148 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4149 return;
4152 blob1 = data_blob_const(bytes, 16);
4153 status = GUID_from_data_blob(&blob1, &server_guid);
4154 if (tevent_req_nterror(req, status)) {
4155 return;
4158 blob1 = data_blob_const(bytes+16, num_bytes-16);
4159 blob2 = data_blob_dup_talloc(state, blob1);
4160 if (blob1.length > 0 &&
4161 tevent_req_nomem(blob2.data, req)) {
4162 return;
4164 server_gss_blob = blob2;
4165 } else {
4166 DATA_BLOB blob1, blob2;
4168 if (num_bytes < key_len) {
4169 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4170 return;
4173 if (key_len != 0 && key_len != 8) {
4174 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4175 return;
4178 if (key_len == 8) {
4179 memcpy(server_challenge, bytes, 8);
4182 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4183 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4184 if (blob1.length > 0) {
4185 size_t len;
4187 len = utf16_len_n(blob1.data,
4188 blob1.length);
4189 blob1.length = len;
4191 ok = convert_string_talloc(state,
4192 CH_UTF16LE,
4193 CH_UNIX,
4194 blob1.data,
4195 blob1.length,
4196 &server_workgroup,
4197 &len);
4198 if (!ok) {
4199 status = map_nt_error_from_unix_common(errno);
4200 tevent_req_nterror(req, status);
4201 return;
4205 blob2.data += blob1.length;
4206 blob2.length -= blob1.length;
4207 if (blob2.length > 0) {
4208 size_t len;
4210 len = utf16_len_n(blob1.data,
4211 blob1.length);
4212 blob1.length = len;
4214 ok = convert_string_talloc(state,
4215 CH_UTF16LE,
4216 CH_UNIX,
4217 blob2.data,
4218 blob2.length,
4219 &server_name,
4220 &len);
4221 if (!ok) {
4222 status = map_nt_error_from_unix_common(errno);
4223 tevent_req_nterror(req, status);
4224 return;
4229 client_signing = "disabled";
4230 if (conn->allow_signing) {
4231 client_signing = "allowed";
4233 if (conn->mandatory_signing) {
4234 client_signing = "required";
4237 server_signing = "not supported";
4238 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4239 server_signing = "supported";
4240 server_allowed = true;
4241 } else if (conn->mandatory_signing) {
4243 * We have mandatory signing as client
4244 * lets assume the server will look at our
4245 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4246 * flag in the session setup
4248 server_signing = "not announced";
4249 server_allowed = true;
4251 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4252 server_signing = "required";
4253 server_mandatory = true;
4256 ok = smb_signing_set_negotiated(conn->smb1.signing,
4257 server_allowed,
4258 server_mandatory);
4259 if (!ok) {
4260 DEBUG(1,("cli_negprot: SMB signing is required, "
4261 "but client[%s] and server[%s] mismatch\n",
4262 client_signing, server_signing));
4263 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4264 return;
4267 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4268 DATA_BLOB blob1;
4269 uint8_t key_len;
4270 time_t t;
4272 if (wct != 0x0D) {
4273 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4274 return;
4277 server_security_mode = SVAL(vwv + 1, 0);
4278 server_max_xmit = SVAL(vwv + 2, 0);
4279 server_max_mux = SVAL(vwv + 3, 0);
4280 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4281 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4282 server_session_key = IVAL(vwv + 6, 0);
4283 server_time_zone = SVALS(vwv + 10, 0);
4284 server_time_zone *= 60;
4285 /* this time is converted to GMT by make_unix_date */
4286 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4287 unix_to_nt_time(&server_system_time, t);
4288 key_len = SVAL(vwv + 11, 0);
4290 if (num_bytes < key_len) {
4291 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4292 return;
4295 if (key_len != 0 && key_len != 8) {
4296 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4297 return;
4300 if (key_len == 8) {
4301 memcpy(server_challenge, bytes, 8);
4304 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4305 if (blob1.length > 0) {
4306 size_t len;
4307 bool ok;
4309 len = utf16_len_n(blob1.data,
4310 blob1.length);
4311 blob1.length = len;
4313 ok = convert_string_talloc(state,
4314 CH_DOS,
4315 CH_UNIX,
4316 blob1.data,
4317 blob1.length,
4318 &server_workgroup,
4319 &len);
4320 if (!ok) {
4321 status = map_nt_error_from_unix_common(errno);
4322 tevent_req_nterror(req, status);
4323 return;
4327 } else {
4328 /* the old core protocol */
4329 server_time_zone = get_time_zone(time(NULL));
4330 server_max_xmit = 1024;
4331 server_max_mux = 1;
4334 if (server_max_xmit < 1024) {
4335 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4336 return;
4339 if (server_max_mux < 1) {
4340 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4341 return;
4345 * Now calculate the negotiated capabilities
4346 * based on the mask for:
4347 * - client only flags
4348 * - flags used in both directions
4349 * - server only flags
4351 both_capabilities = client_capabilities & server_capabilities;
4352 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4353 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4354 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4356 max_xmit = MIN(client_max_xmit, server_max_xmit);
4358 conn->smb1.server.capabilities = server_capabilities;
4359 conn->smb1.capabilities = capabilities;
4361 conn->smb1.server.max_xmit = server_max_xmit;
4362 conn->smb1.max_xmit = max_xmit;
4364 conn->smb1.server.max_mux = server_max_mux;
4366 conn->smb1.server.security_mode = server_security_mode;
4368 conn->smb1.server.readbraw = server_readbraw;
4369 conn->smb1.server.writebraw = server_writebraw;
4370 conn->smb1.server.lockread = server_lockread;
4371 conn->smb1.server.writeunlock = server_writeunlock;
4373 conn->smb1.server.session_key = server_session_key;
4375 talloc_steal(conn, server_gss_blob.data);
4376 conn->smb1.server.gss_blob = server_gss_blob;
4377 conn->smb1.server.guid = server_guid;
4378 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4379 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4380 conn->smb1.server.name = talloc_move(conn, &server_name);
4382 conn->smb1.server.time_zone = server_time_zone;
4383 conn->smb1.server.system_time = server_system_time;
4385 tevent_req_done(req);
4388 static size_t smbXcli_padding_helper(uint32_t offset, size_t n)
4390 if ((offset & (n-1)) == 0) return 0;
4391 return n - (offset & (n-1));
4394 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4396 size_t i;
4397 uint8_t *buf;
4398 uint16_t dialect_count = 0;
4399 DATA_BLOB dyn = data_blob_null;
4401 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4402 bool ok;
4403 uint8_t val[2];
4405 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4406 continue;
4409 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4410 continue;
4413 SSVAL(val, 0, smb2cli_prots[i].smb2_dialect);
4415 ok = data_blob_append(state, &dyn, val, sizeof(val));
4416 if (!ok) {
4417 return NULL;
4420 dialect_count++;
4423 buf = state->smb2.fixed;
4424 SSVAL(buf, 0, 36);
4425 SSVAL(buf, 2, dialect_count);
4426 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4427 SSVAL(buf, 6, 0); /* Reserved */
4428 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
4429 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4430 } else {
4431 SIVAL(buf, 8, 0); /* Capabilities */
4433 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4434 NTSTATUS status;
4435 DATA_BLOB blob;
4437 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
4438 state, &blob);
4439 if (!NT_STATUS_IS_OK(status)) {
4440 return NULL;
4442 memcpy(buf+12, blob.data, 16); /* ClientGuid */
4443 } else {
4444 memset(buf+12, 0, 16); /* ClientGuid */
4447 if (state->conn->max_protocol >= PROTOCOL_SMB3_10) {
4448 NTSTATUS status;
4449 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4450 uint32_t offset;
4451 DATA_BLOB b;
4452 uint8_t p[38];
4453 const uint8_t zeros[8] = {0, };
4454 size_t pad;
4455 bool ok;
4457 SSVAL(p, 0, 1); /* HashAlgorithmCount */
4458 SSVAL(p, 2, 32); /* SaltLength */
4459 SSVAL(p, 4, SMB2_PREAUTH_INTEGRITY_SHA512);
4460 generate_random_buffer(p + 6, 32);
4462 b = data_blob_const(p, 38);
4463 status = smb2_negotiate_context_add(state, &c,
4464 SMB2_PREAUTH_INTEGRITY_CAPABILITIES, b);
4465 if (!NT_STATUS_IS_OK(status)) {
4466 return NULL;
4469 SSVAL(p, 0, 2); /* ChiperCount */
4470 SSVAL(p, 2, SMB2_ENCRYPTION_AES128_GCM);
4471 SSVAL(p, 4, SMB2_ENCRYPTION_AES128_CCM);
4473 b = data_blob_const(p, 6);
4474 status = smb2_negotiate_context_add(state, &c,
4475 SMB2_ENCRYPTION_CAPABILITIES, b);
4476 if (!NT_STATUS_IS_OK(status)) {
4477 return NULL;
4480 status = smb2_negotiate_context_push(state, &b, c);
4481 if (!NT_STATUS_IS_OK(status)) {
4482 return NULL;
4485 offset = SMB2_HDR_BODY + sizeof(state->smb2.fixed) + dyn.length;
4486 pad = smbXcli_padding_helper(offset, 8);
4488 ok = data_blob_append(state, &dyn, zeros, pad);
4489 if (!ok) {
4490 return NULL;
4492 offset += pad;
4494 ok = data_blob_append(state, &dyn, b.data, b.length);
4495 if (!ok) {
4496 return NULL;
4499 SIVAL(buf, 28, offset); /* NegotiateContextOffset */
4500 SSVAL(buf, 32, c.num_contexts); /* NegotiateContextCount */
4501 SSVAL(buf, 34, 0); /* Reserved */
4502 } else {
4503 SBVAL(buf, 28, 0); /* Reserved/ClientStartTime */
4506 return smb2cli_req_send(state, state->ev,
4507 state->conn, SMB2_OP_NEGPROT,
4508 0, 0, /* flags */
4509 state->timeout_msec,
4510 NULL, NULL, /* tcon, session */
4511 state->smb2.fixed, sizeof(state->smb2.fixed),
4512 dyn.data, dyn.length,
4513 UINT16_MAX); /* max_dyn_len */
4516 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4518 struct tevent_req *req =
4519 tevent_req_callback_data(subreq,
4520 struct tevent_req);
4521 struct smbXcli_negprot_state *state =
4522 tevent_req_data(req,
4523 struct smbXcli_negprot_state);
4524 struct smbXcli_conn *conn = state->conn;
4525 size_t security_offset, security_length;
4526 DATA_BLOB blob;
4527 NTSTATUS status;
4528 struct iovec *iov;
4529 uint8_t *body;
4530 size_t i;
4531 uint16_t dialect_revision;
4532 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4533 uint32_t negotiate_context_offset = 0;
4534 uint16_t negotiate_context_count = 0;
4535 DATA_BLOB negotiate_context_blob = data_blob_null;
4536 size_t avail;
4537 size_t ctx_ofs;
4538 size_t needed;
4539 struct smb2_negotiate_context *preauth = NULL;
4540 uint16_t hash_count;
4541 uint16_t salt_length;
4542 uint16_t hash_selected;
4543 struct hc_sha512state sctx;
4544 struct smb2_negotiate_context *cipher = NULL;
4545 struct iovec sent_iov[3];
4546 static const struct smb2cli_req_expected_response expected[] = {
4548 .status = NT_STATUS_OK,
4549 .body_size = 0x41
4553 status = smb2cli_req_recv(subreq, state, &iov,
4554 expected, ARRAY_SIZE(expected));
4555 if (tevent_req_nterror(req, status)) {
4556 return;
4559 body = (uint8_t *)iov[1].iov_base;
4561 dialect_revision = SVAL(body, 4);
4563 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4564 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4565 continue;
4568 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4569 continue;
4572 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4573 continue;
4576 conn->protocol = smb2cli_prots[i].proto;
4577 break;
4580 if (conn->protocol == PROTOCOL_NONE) {
4581 TALLOC_FREE(subreq);
4583 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
4584 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4585 return;
4588 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4589 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4590 return;
4593 /* make sure we do not loop forever */
4594 state->conn->min_protocol = PROTOCOL_SMB2_02;
4597 * send a SMB2 negprot, in order to negotiate
4598 * the SMB2 dialect.
4600 subreq = smbXcli_negprot_smb2_subreq(state);
4601 if (tevent_req_nomem(subreq, req)) {
4602 return;
4604 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4605 return;
4608 conn->smb2.server.security_mode = SVAL(body, 2);
4609 if (conn->protocol >= PROTOCOL_SMB3_10) {
4610 negotiate_context_count = SVAL(body, 6);
4613 blob = data_blob_const(body + 8, 16);
4614 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4615 if (tevent_req_nterror(req, status)) {
4616 return;
4619 conn->smb2.server.capabilities = IVAL(body, 24);
4620 conn->smb2.server.max_trans_size= IVAL(body, 28);
4621 conn->smb2.server.max_read_size = IVAL(body, 32);
4622 conn->smb2.server.max_write_size= IVAL(body, 36);
4623 conn->smb2.server.system_time = BVAL(body, 40);
4624 conn->smb2.server.start_time = BVAL(body, 48);
4626 security_offset = SVAL(body, 56);
4627 security_length = SVAL(body, 58);
4629 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4630 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4631 return;
4634 if (security_length > iov[2].iov_len) {
4635 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4636 return;
4639 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4640 iov[2].iov_base,
4641 security_length);
4642 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4643 return;
4646 if (conn->protocol < PROTOCOL_SMB3_10) {
4647 TALLOC_FREE(subreq);
4649 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4650 conn->smb2.server.cipher = SMB2_ENCRYPTION_AES128_CCM;
4652 tevent_req_done(req);
4653 return;
4656 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
4657 tevent_req_nterror(req,
4658 NT_STATUS_INVALID_NETWORK_RESPONSE);
4659 return;
4662 negotiate_context_offset = IVAL(body, 60);
4663 if (negotiate_context_offset < security_offset) {
4664 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4665 return;
4668 ctx_ofs = negotiate_context_offset - security_offset;
4669 if (ctx_ofs > iov[2].iov_len) {
4670 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4671 return;
4673 avail = iov[2].iov_len - security_length;
4674 needed = iov[2].iov_len - ctx_ofs;
4675 if (needed > avail) {
4676 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4677 return;
4680 negotiate_context_blob.data = (uint8_t *)iov[2].iov_base;
4681 negotiate_context_blob.length = iov[2].iov_len;
4683 negotiate_context_blob.data += ctx_ofs;
4684 negotiate_context_blob.length -= ctx_ofs;
4686 status = smb2_negotiate_context_parse(state, negotiate_context_blob, &c);
4687 if (tevent_req_nterror(req, status)) {
4688 return;
4691 if (negotiate_context_count != c.num_contexts) {
4692 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4693 return;
4696 preauth = smb2_negotiate_context_find(&c,
4697 SMB2_PREAUTH_INTEGRITY_CAPABILITIES);
4698 if (preauth == NULL) {
4699 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4700 return;
4703 if (preauth->data.length < 6) {
4704 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4705 return;
4708 hash_count = SVAL(preauth->data.data, 0);
4709 salt_length = SVAL(preauth->data.data, 2);
4710 hash_selected = SVAL(preauth->data.data, 4);
4712 if (hash_count != 1) {
4713 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4714 return;
4717 if (preauth->data.length != (6 + salt_length)) {
4718 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4719 return;
4722 if (hash_selected != SMB2_PREAUTH_INTEGRITY_SHA512) {
4723 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4724 return;
4727 cipher = smb2_negotiate_context_find(&c, SMB2_ENCRYPTION_CAPABILITIES);
4728 if (cipher != NULL) {
4729 uint16_t cipher_count;
4731 if (cipher->data.length < 2) {
4732 tevent_req_nterror(req,
4733 NT_STATUS_INVALID_NETWORK_RESPONSE);
4734 return;
4737 cipher_count = SVAL(cipher->data.data, 0);
4739 if (cipher_count > 1) {
4740 tevent_req_nterror(req,
4741 NT_STATUS_INVALID_NETWORK_RESPONSE);
4742 return;
4745 if (cipher->data.length != (2 + 2 * cipher_count)) {
4746 tevent_req_nterror(req,
4747 NT_STATUS_INVALID_NETWORK_RESPONSE);
4748 return;
4751 if (cipher_count == 1) {
4752 uint16_t cipher_selected;
4754 cipher_selected = SVAL(cipher->data.data, 2);
4756 switch (cipher_selected) {
4757 case SMB2_ENCRYPTION_AES128_GCM:
4758 case SMB2_ENCRYPTION_AES128_CCM:
4759 conn->smb2.server.cipher = cipher_selected;
4760 break;
4765 /* First we hash the request */
4766 smb2cli_req_get_sent_iov(subreq, sent_iov);
4767 samba_SHA512_Init(&sctx);
4768 samba_SHA512_Update(&sctx, conn->smb2.preauth_sha512,
4769 sizeof(conn->smb2.preauth_sha512));
4770 for (i = 0; i < 3; i++) {
4771 samba_SHA512_Update(&sctx, sent_iov[i].iov_base, sent_iov[i].iov_len);
4773 samba_SHA512_Final(conn->smb2.preauth_sha512, &sctx);
4774 TALLOC_FREE(subreq);
4776 /* And now we hash the response */
4777 samba_SHA512_Init(&sctx);
4778 samba_SHA512_Update(&sctx, conn->smb2.preauth_sha512,
4779 sizeof(conn->smb2.preauth_sha512));
4780 for (i = 0; i < 3; i++) {
4781 samba_SHA512_Update(&sctx, iov[i].iov_base, iov[i].iov_len);
4783 samba_SHA512_Final(conn->smb2.preauth_sha512, &sctx);
4785 tevent_req_done(req);
4788 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4789 TALLOC_CTX *tmp_mem,
4790 uint8_t *inbuf)
4792 size_t num_pending = talloc_array_length(conn->pending);
4793 struct tevent_req *subreq;
4794 struct smbXcli_req_state *substate;
4795 struct tevent_req *req;
4796 uint32_t protocol_magic;
4797 size_t inbuf_len = smb_len_nbt(inbuf);
4799 if (num_pending != 1) {
4800 return NT_STATUS_INTERNAL_ERROR;
4803 if (inbuf_len < 4) {
4804 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4807 subreq = conn->pending[0];
4808 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4809 req = tevent_req_callback_data(subreq, struct tevent_req);
4811 protocol_magic = IVAL(inbuf, 4);
4813 switch (protocol_magic) {
4814 case SMB_MAGIC:
4815 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4816 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4817 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4819 case SMB2_MAGIC:
4820 if (substate->smb2.recv_iov == NULL) {
4822 * For the SMB1 negprot we have move it.
4824 substate->smb2.recv_iov = substate->smb1.recv_iov;
4825 substate->smb1.recv_iov = NULL;
4829 * we got an SMB2 answer, which consumed sequence number 0
4830 * so we need to use 1 as the next one.
4832 * we also need to set the current credits to 0
4833 * as we consumed the initial one. The SMB2 answer
4834 * hopefully grant us a new credit.
4836 conn->smb2.mid = 1;
4837 conn->smb2.cur_credits = 0;
4838 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4839 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4840 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4843 DEBUG(10, ("Got non-SMB PDU\n"));
4844 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4847 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4849 return tevent_req_simple_recv_ntstatus(req);
4852 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4853 uint32_t timeout_msec,
4854 enum protocol_types min_protocol,
4855 enum protocol_types max_protocol)
4857 TALLOC_CTX *frame = talloc_stackframe();
4858 struct tevent_context *ev;
4859 struct tevent_req *req;
4860 NTSTATUS status = NT_STATUS_NO_MEMORY;
4861 bool ok;
4863 if (smbXcli_conn_has_async_calls(conn)) {
4865 * Can't use sync call while an async call is in flight
4867 status = NT_STATUS_INVALID_PARAMETER_MIX;
4868 goto fail;
4870 ev = samba_tevent_context_init(frame);
4871 if (ev == NULL) {
4872 goto fail;
4874 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4875 min_protocol, max_protocol);
4876 if (req == NULL) {
4877 goto fail;
4879 ok = tevent_req_poll_ntstatus(req, ev, &status);
4880 if (!ok) {
4881 goto fail;
4883 status = smbXcli_negprot_recv(req);
4884 fail:
4885 TALLOC_FREE(frame);
4886 return status;
4889 struct smb2cli_validate_negotiate_info_state {
4890 struct smbXcli_conn *conn;
4891 DATA_BLOB in_input_buffer;
4892 DATA_BLOB in_output_buffer;
4893 DATA_BLOB out_input_buffer;
4894 DATA_BLOB out_output_buffer;
4895 uint16_t dialect;
4898 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq);
4900 struct tevent_req *smb2cli_validate_negotiate_info_send(TALLOC_CTX *mem_ctx,
4901 struct tevent_context *ev,
4902 struct smbXcli_conn *conn,
4903 uint32_t timeout_msec,
4904 struct smbXcli_session *session,
4905 struct smbXcli_tcon *tcon)
4907 struct tevent_req *req;
4908 struct smb2cli_validate_negotiate_info_state *state;
4909 uint8_t *buf;
4910 uint16_t dialect_count = 0;
4911 struct tevent_req *subreq;
4912 bool _save_should_sign;
4913 size_t i;
4915 req = tevent_req_create(mem_ctx, &state,
4916 struct smb2cli_validate_negotiate_info_state);
4917 if (req == NULL) {
4918 return NULL;
4920 state->conn = conn;
4922 state->in_input_buffer = data_blob_talloc_zero(state,
4923 4 + 16 + 1 + 1 + 2);
4924 if (tevent_req_nomem(state->in_input_buffer.data, req)) {
4925 return tevent_req_post(req, ev);
4927 buf = state->in_input_buffer.data;
4929 if (state->conn->max_protocol >= PROTOCOL_SMB2_22) {
4930 SIVAL(buf, 0, conn->smb2.client.capabilities);
4931 } else {
4932 SIVAL(buf, 0, 0); /* Capabilities */
4934 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4935 NTSTATUS status;
4936 DATA_BLOB blob;
4938 status = GUID_to_ndr_blob(&conn->smb2.client.guid,
4939 state, &blob);
4940 if (!NT_STATUS_IS_OK(status)) {
4941 return NULL;
4943 memcpy(buf+4, blob.data, 16); /* ClientGuid */
4944 } else {
4945 memset(buf+4, 0, 16); /* ClientGuid */
4947 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
4948 SCVAL(buf, 20, conn->smb2.client.security_mode);
4949 } else {
4950 SCVAL(buf, 20, 0);
4952 SCVAL(buf, 21, 0); /* reserved */
4954 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4955 bool ok;
4956 size_t ofs;
4958 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4959 continue;
4962 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4963 continue;
4966 if (smb2cli_prots[i].proto == state->conn->protocol) {
4967 state->dialect = smb2cli_prots[i].smb2_dialect;
4970 ofs = state->in_input_buffer.length;
4971 ok = data_blob_realloc(state, &state->in_input_buffer,
4972 ofs + 2);
4973 if (!ok) {
4974 tevent_req_oom(req);
4975 return tevent_req_post(req, ev);
4978 buf = state->in_input_buffer.data;
4979 SSVAL(buf, ofs, smb2cli_prots[i].smb2_dialect);
4981 dialect_count++;
4983 buf = state->in_input_buffer.data;
4984 SSVAL(buf, 22, dialect_count);
4986 _save_should_sign = smb2cli_tcon_is_signing_on(tcon);
4987 smb2cli_tcon_should_sign(tcon, true);
4988 subreq = smb2cli_ioctl_send(state, ev, conn,
4989 timeout_msec, session, tcon,
4990 UINT64_MAX, /* in_fid_persistent */
4991 UINT64_MAX, /* in_fid_volatile */
4992 FSCTL_VALIDATE_NEGOTIATE_INFO,
4993 0, /* in_max_input_length */
4994 &state->in_input_buffer,
4995 24, /* in_max_output_length */
4996 &state->in_output_buffer,
4997 SMB2_IOCTL_FLAG_IS_FSCTL);
4998 smb2cli_tcon_should_sign(tcon, _save_should_sign);
4999 if (tevent_req_nomem(subreq, req)) {
5000 return tevent_req_post(req, ev);
5002 tevent_req_set_callback(subreq,
5003 smb2cli_validate_negotiate_info_done,
5004 req);
5006 return req;
5009 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq)
5011 struct tevent_req *req =
5012 tevent_req_callback_data(subreq,
5013 struct tevent_req);
5014 struct smb2cli_validate_negotiate_info_state *state =
5015 tevent_req_data(req,
5016 struct smb2cli_validate_negotiate_info_state);
5017 NTSTATUS status;
5018 const uint8_t *buf;
5019 uint32_t capabilities;
5020 DATA_BLOB guid_blob;
5021 struct GUID server_guid;
5022 uint16_t security_mode;
5023 uint16_t dialect;
5025 status = smb2cli_ioctl_recv(subreq, state,
5026 &state->out_input_buffer,
5027 &state->out_output_buffer);
5028 TALLOC_FREE(subreq);
5029 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
5031 * The response was signed, but not supported
5033 * Older Windows and Samba releases return
5034 * NT_STATUS_FILE_CLOSED.
5036 tevent_req_done(req);
5037 return;
5039 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_DEVICE_REQUEST)) {
5041 * The response was signed, but not supported
5043 * This is returned by the NTVFS based Samba 4.x file server
5044 * for file shares.
5046 tevent_req_done(req);
5047 return;
5049 if (NT_STATUS_EQUAL(status, NT_STATUS_FS_DRIVER_REQUIRED)) {
5051 * The response was signed, but not supported
5053 * This is returned by the NTVFS based Samba 4.x file server
5054 * for ipc shares.
5056 tevent_req_done(req);
5057 return;
5059 if (tevent_req_nterror(req, status)) {
5060 return;
5063 if (state->out_output_buffer.length != 24) {
5064 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5065 return;
5068 buf = state->out_output_buffer.data;
5070 capabilities = IVAL(buf, 0);
5071 guid_blob = data_blob_const(buf + 4, 16);
5072 status = GUID_from_data_blob(&guid_blob, &server_guid);
5073 if (tevent_req_nterror(req, status)) {
5074 return;
5076 security_mode = CVAL(buf, 20);
5077 dialect = SVAL(buf, 22);
5079 if (capabilities != state->conn->smb2.server.capabilities) {
5080 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5081 return;
5084 if (!GUID_equal(&server_guid, &state->conn->smb2.server.guid)) {
5085 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5086 return;
5089 if (security_mode != state->conn->smb2.server.security_mode) {
5090 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5091 return;
5094 if (dialect != state->dialect) {
5095 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5096 return;
5099 tevent_req_done(req);
5102 NTSTATUS smb2cli_validate_negotiate_info_recv(struct tevent_req *req)
5104 return tevent_req_simple_recv_ntstatus(req);
5107 static int smbXcli_session_destructor(struct smbXcli_session *session)
5109 if (session->conn == NULL) {
5110 return 0;
5113 DLIST_REMOVE(session->conn->sessions, session);
5114 return 0;
5117 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
5118 struct smbXcli_conn *conn)
5120 struct smbXcli_session *session;
5122 session = talloc_zero(mem_ctx, struct smbXcli_session);
5123 if (session == NULL) {
5124 return NULL;
5126 session->smb2 = talloc_zero(session, struct smb2cli_session);
5127 if (session->smb2 == NULL) {
5128 talloc_free(session);
5129 return NULL;
5131 talloc_set_destructor(session, smbXcli_session_destructor);
5133 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
5134 session->conn = conn;
5136 memcpy(session->smb2_channel.preauth_sha512,
5137 conn->smb2.preauth_sha512,
5138 sizeof(session->smb2_channel.preauth_sha512));
5140 return session;
5143 struct smbXcli_session *smbXcli_session_copy(TALLOC_CTX *mem_ctx,
5144 struct smbXcli_session *src)
5146 struct smbXcli_session *session;
5148 session = talloc_zero(mem_ctx, struct smbXcli_session);
5149 if (session == NULL) {
5150 return NULL;
5152 session->smb2 = talloc_zero(session, struct smb2cli_session);
5153 if (session->smb2 == NULL) {
5154 talloc_free(session);
5155 return NULL;
5158 session->conn = src->conn;
5159 *session->smb2 = *src->smb2;
5160 session->smb2_channel = src->smb2_channel;
5161 session->disconnect_expired = src->disconnect_expired;
5163 DLIST_ADD_END(src->conn->sessions, session, struct smbXcli_session *);
5164 talloc_set_destructor(session, smbXcli_session_destructor);
5166 return session;
5169 bool smbXcli_session_is_authenticated(struct smbXcli_session *session)
5171 const DATA_BLOB *application_key;
5173 if (session->conn == NULL) {
5174 return false;
5178 * If we have an application key we had a session key negotiated
5179 * at auth time.
5181 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5182 application_key = &session->smb2->application_key;
5183 } else {
5184 application_key = &session->smb1.application_key;
5187 if (application_key->length == 0) {
5188 return false;
5191 return true;
5194 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
5195 TALLOC_CTX *mem_ctx,
5196 DATA_BLOB *key)
5198 const DATA_BLOB *application_key;
5200 *key = data_blob_null;
5202 if (session->conn == NULL) {
5203 return NT_STATUS_NO_USER_SESSION_KEY;
5206 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5207 application_key = &session->smb2->application_key;
5208 } else {
5209 application_key = &session->smb1.application_key;
5212 if (application_key->length == 0) {
5213 return NT_STATUS_NO_USER_SESSION_KEY;
5216 *key = data_blob_dup_talloc(mem_ctx, *application_key);
5217 if (key->data == NULL) {
5218 return NT_STATUS_NO_MEMORY;
5221 return NT_STATUS_OK;
5224 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
5226 session->disconnect_expired = true;
5229 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
5231 return session->smb1.session_id;
5234 void smb1cli_session_set_id(struct smbXcli_session *session,
5235 uint16_t session_id)
5237 session->smb1.session_id = session_id;
5240 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
5241 const DATA_BLOB _session_key)
5243 struct smbXcli_conn *conn = session->conn;
5244 uint8_t session_key[16];
5246 if (conn == NULL) {
5247 return NT_STATUS_INVALID_PARAMETER_MIX;
5250 if (session->smb1.application_key.length != 0) {
5252 * TODO: do not allow this...
5254 * return NT_STATUS_INVALID_PARAMETER_MIX;
5256 data_blob_clear_free(&session->smb1.application_key);
5257 session->smb1.protected_key = false;
5260 if (_session_key.length == 0) {
5261 return NT_STATUS_OK;
5264 ZERO_STRUCT(session_key);
5265 memcpy(session_key, _session_key.data,
5266 MIN(_session_key.length, sizeof(session_key)));
5268 session->smb1.application_key = data_blob_talloc(session,
5269 session_key,
5270 sizeof(session_key));
5271 ZERO_STRUCT(session_key);
5272 if (session->smb1.application_key.data == NULL) {
5273 return NT_STATUS_NO_MEMORY;
5276 session->smb1.protected_key = false;
5278 return NT_STATUS_OK;
5281 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
5283 if (session->smb1.protected_key) {
5284 /* already protected */
5285 return NT_STATUS_OK;
5288 if (session->smb1.application_key.length != 16) {
5289 return NT_STATUS_INVALID_PARAMETER_MIX;
5292 smb_key_derivation(session->smb1.application_key.data,
5293 session->smb1.application_key.length,
5294 session->smb1.application_key.data);
5296 session->smb1.protected_key = true;
5298 return NT_STATUS_OK;
5301 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
5303 struct smbXcli_conn *conn = session->conn;
5304 uint8_t security_mode = 0;
5306 if (conn == NULL) {
5307 return security_mode;
5310 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
5311 if (conn->mandatory_signing) {
5312 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
5315 return security_mode;
5318 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
5320 return session->smb2->session_id;
5323 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
5325 return session->smb2->session_flags;
5328 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
5329 uint64_t session_id,
5330 uint16_t session_flags)
5332 session->smb2->session_id = session_id;
5333 session->smb2->session_flags = session_flags;
5336 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
5338 session->smb2->channel_sequence += 1;
5341 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session *session,
5342 uint16_t channel_sequence)
5344 uint16_t prev_cs;
5346 prev_cs = session->smb2->channel_sequence;
5347 session->smb2->channel_sequence = channel_sequence;
5349 return prev_cs;
5352 void smb2cli_session_start_replay(struct smbXcli_session *session)
5354 session->smb2->replay_active = true;
5357 void smb2cli_session_stop_replay(struct smbXcli_session *session)
5359 session->smb2->replay_active = false;
5362 NTSTATUS smb2cli_session_update_preauth(struct smbXcli_session *session,
5363 const struct iovec *iov)
5365 struct hc_sha512state sctx;
5366 size_t i;
5368 if (session->conn == NULL) {
5369 return NT_STATUS_INTERNAL_ERROR;
5372 if (session->conn->protocol < PROTOCOL_SMB3_10) {
5373 return NT_STATUS_OK;
5376 if (session->smb2_channel.signing_key.length != 0) {
5377 return NT_STATUS_OK;
5380 samba_SHA512_Init(&sctx);
5381 samba_SHA512_Update(&sctx, session->smb2_channel.preauth_sha512,
5382 sizeof(session->smb2_channel.preauth_sha512));
5383 for (i = 0; i < 3; i++) {
5384 samba_SHA512_Update(&sctx, iov[i].iov_base, iov[i].iov_len);
5386 samba_SHA512_Final(session->smb2_channel.preauth_sha512, &sctx);
5388 return NT_STATUS_OK;
5391 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
5392 const DATA_BLOB _session_key,
5393 const struct iovec *recv_iov)
5395 struct smbXcli_conn *conn = session->conn;
5396 uint16_t no_sign_flags;
5397 uint8_t session_key[16];
5398 bool check_signature = true;
5399 uint32_t hdr_flags;
5400 NTSTATUS status;
5401 struct _derivation {
5402 DATA_BLOB label;
5403 DATA_BLOB context;
5405 struct {
5406 struct _derivation signing;
5407 struct _derivation encryption;
5408 struct _derivation decryption;
5409 struct _derivation application;
5410 } derivation = { };
5412 if (conn == NULL) {
5413 return NT_STATUS_INVALID_PARAMETER_MIX;
5416 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
5417 return NT_STATUS_INVALID_PARAMETER_MIX;
5420 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
5422 if (session->smb2->session_flags & no_sign_flags) {
5423 session->smb2->should_sign = false;
5424 return NT_STATUS_OK;
5427 if (session->smb2->signing_key.length != 0) {
5428 return NT_STATUS_INVALID_PARAMETER_MIX;
5431 if (conn->protocol >= PROTOCOL_SMB3_10) {
5432 struct _derivation *d;
5433 DATA_BLOB p;
5435 p = data_blob_const(session->smb2_channel.preauth_sha512,
5436 sizeof(session->smb2_channel.preauth_sha512));
5438 d = &derivation.signing;
5439 d->label = data_blob_string_const_null("SMBSigningKey");
5440 d->context = p;
5442 d = &derivation.encryption;
5443 d->label = data_blob_string_const_null("SMBC2SCipherKey");
5444 d->context = p;
5446 d = &derivation.decryption;
5447 d->label = data_blob_string_const_null("SMBS2CCipherKey");
5448 d->context = p;
5450 d = &derivation.application;
5451 d->label = data_blob_string_const_null("SMBAppKey");
5452 d->context = p;
5454 } else if (conn->protocol >= PROTOCOL_SMB2_24) {
5455 struct _derivation *d;
5457 d = &derivation.signing;
5458 d->label = data_blob_string_const_null("SMB2AESCMAC");
5459 d->context = data_blob_string_const_null("SmbSign");
5461 d = &derivation.encryption;
5462 d->label = data_blob_string_const_null("SMB2AESCCM");
5463 d->context = data_blob_string_const_null("ServerIn ");
5465 d = &derivation.decryption;
5466 d->label = data_blob_string_const_null("SMB2AESCCM");
5467 d->context = data_blob_string_const_null("ServerOut");
5469 d = &derivation.application;
5470 d->label = data_blob_string_const_null("SMB2APP");
5471 d->context = data_blob_string_const_null("SmbRpc");
5474 ZERO_STRUCT(session_key);
5475 memcpy(session_key, _session_key.data,
5476 MIN(_session_key.length, sizeof(session_key)));
5478 session->smb2->signing_key = data_blob_talloc(session,
5479 session_key,
5480 sizeof(session_key));
5481 if (session->smb2->signing_key.data == NULL) {
5482 ZERO_STRUCT(session_key);
5483 return NT_STATUS_NO_MEMORY;
5486 if (conn->protocol >= PROTOCOL_SMB2_24) {
5487 struct _derivation *d = &derivation.signing;
5489 smb2_key_derivation(session_key, sizeof(session_key),
5490 d->label.data, d->label.length,
5491 d->context.data, d->context.length,
5492 session->smb2->signing_key.data);
5495 session->smb2->encryption_key = data_blob_dup_talloc(session,
5496 session->smb2->signing_key);
5497 if (session->smb2->encryption_key.data == NULL) {
5498 ZERO_STRUCT(session_key);
5499 return NT_STATUS_NO_MEMORY;
5502 if (conn->protocol >= PROTOCOL_SMB2_24) {
5503 struct _derivation *d = &derivation.encryption;
5505 smb2_key_derivation(session_key, sizeof(session_key),
5506 d->label.data, d->label.length,
5507 d->context.data, d->context.length,
5508 session->smb2->encryption_key.data);
5511 session->smb2->decryption_key = data_blob_dup_talloc(session,
5512 session->smb2->signing_key);
5513 if (session->smb2->decryption_key.data == NULL) {
5514 ZERO_STRUCT(session_key);
5515 return NT_STATUS_NO_MEMORY;
5518 if (conn->protocol >= PROTOCOL_SMB2_24) {
5519 struct _derivation *d = &derivation.decryption;
5521 smb2_key_derivation(session_key, sizeof(session_key),
5522 d->label.data, d->label.length,
5523 d->context.data, d->context.length,
5524 session->smb2->decryption_key.data);
5527 session->smb2->application_key = data_blob_dup_talloc(session,
5528 session->smb2->signing_key);
5529 if (session->smb2->application_key.data == NULL) {
5530 ZERO_STRUCT(session_key);
5531 return NT_STATUS_NO_MEMORY;
5534 if (conn->protocol >= PROTOCOL_SMB2_24) {
5535 struct _derivation *d = &derivation.application;
5537 smb2_key_derivation(session_key, sizeof(session_key),
5538 d->label.data, d->label.length,
5539 d->context.data, d->context.length,
5540 session->smb2->application_key.data);
5542 ZERO_STRUCT(session_key);
5544 session->smb2_channel.signing_key = data_blob_dup_talloc(session,
5545 session->smb2->signing_key);
5546 if (session->smb2_channel.signing_key.data == NULL) {
5547 return NT_STATUS_NO_MEMORY;
5550 check_signature = conn->mandatory_signing;
5552 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
5553 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
5555 * Sadly some vendors don't sign the
5556 * final SMB2 session setup response
5558 * At least Windows and Samba are always doing this
5559 * if there's a session key available.
5561 * We only check the signature if it's mandatory
5562 * or SMB2_HDR_FLAG_SIGNED is provided.
5564 check_signature = true;
5567 if (conn->protocol >= PROTOCOL_SMB3_10) {
5568 check_signature = true;
5571 if (check_signature) {
5572 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5573 session->conn->protocol,
5574 recv_iov, 3);
5575 if (!NT_STATUS_IS_OK(status)) {
5576 return status;
5580 session->smb2->should_sign = false;
5581 session->smb2->should_encrypt = false;
5583 if (conn->desire_signing) {
5584 session->smb2->should_sign = true;
5587 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
5588 session->smb2->should_sign = true;
5591 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
5592 session->smb2->should_encrypt = true;
5595 if (conn->protocol < PROTOCOL_SMB2_24) {
5596 session->smb2->should_encrypt = false;
5599 if (conn->smb2.server.cipher == 0) {
5600 session->smb2->should_encrypt = false;
5603 generate_random_buffer((uint8_t *)&session->smb2->nonce_high,
5604 sizeof(session->smb2->nonce_high));
5605 session->smb2->nonce_low = 1;
5607 return NT_STATUS_OK;
5610 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
5611 struct smbXcli_session *session1,
5612 struct smbXcli_conn *conn,
5613 struct smbXcli_session **_session2)
5615 struct smbXcli_session *session2;
5617 if (session1->smb2->signing_key.length == 0) {
5618 return NT_STATUS_INVALID_PARAMETER_MIX;
5621 if (conn == NULL) {
5622 return NT_STATUS_INVALID_PARAMETER_MIX;
5625 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
5626 if (session2 == NULL) {
5627 return NT_STATUS_NO_MEMORY;
5629 session2->smb2 = talloc_reference(session2, session1->smb2);
5630 if (session2->smb2 == NULL) {
5631 talloc_free(session2);
5632 return NT_STATUS_NO_MEMORY;
5635 talloc_set_destructor(session2, smbXcli_session_destructor);
5636 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
5637 session2->conn = conn;
5639 memcpy(session2->smb2_channel.preauth_sha512,
5640 conn->smb2.preauth_sha512,
5641 sizeof(session2->smb2_channel.preauth_sha512));
5643 *_session2 = session2;
5644 return NT_STATUS_OK;
5647 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
5648 const DATA_BLOB _channel_key,
5649 const struct iovec *recv_iov)
5651 struct smbXcli_conn *conn = session->conn;
5652 uint8_t channel_key[16];
5653 NTSTATUS status;
5654 struct _derivation {
5655 DATA_BLOB label;
5656 DATA_BLOB context;
5658 struct {
5659 struct _derivation signing;
5660 } derivation = { };
5662 if (conn == NULL) {
5663 return NT_STATUS_INVALID_PARAMETER_MIX;
5666 if (session->smb2_channel.signing_key.length != 0) {
5667 return NT_STATUS_INVALID_PARAMETER_MIX;
5670 if (conn->protocol >= PROTOCOL_SMB3_10) {
5671 struct _derivation *d;
5672 DATA_BLOB p;
5674 p = data_blob_const(session->smb2_channel.preauth_sha512,
5675 sizeof(session->smb2_channel.preauth_sha512));
5677 d = &derivation.signing;
5678 d->label = data_blob_string_const_null("SMBSigningKey");
5679 d->context = p;
5680 } else if (conn->protocol >= PROTOCOL_SMB2_24) {
5681 struct _derivation *d;
5683 d = &derivation.signing;
5684 d->label = data_blob_string_const_null("SMB2AESCMAC");
5685 d->context = data_blob_string_const_null("SmbSign");
5688 ZERO_STRUCT(channel_key);
5689 memcpy(channel_key, _channel_key.data,
5690 MIN(_channel_key.length, sizeof(channel_key)));
5692 session->smb2_channel.signing_key = data_blob_talloc(session,
5693 channel_key,
5694 sizeof(channel_key));
5695 if (session->smb2_channel.signing_key.data == NULL) {
5696 ZERO_STRUCT(channel_key);
5697 return NT_STATUS_NO_MEMORY;
5700 if (conn->protocol >= PROTOCOL_SMB2_24) {
5701 struct _derivation *d = &derivation.signing;
5703 smb2_key_derivation(channel_key, sizeof(channel_key),
5704 d->label.data, d->label.length,
5705 d->context.data, d->context.length,
5706 session->smb2_channel.signing_key.data);
5708 ZERO_STRUCT(channel_key);
5710 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
5711 session->conn->protocol,
5712 recv_iov, 3);
5713 if (!NT_STATUS_IS_OK(status)) {
5714 return status;
5717 return NT_STATUS_OK;
5720 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
5722 if (session->smb2->should_encrypt) {
5723 return NT_STATUS_OK;
5726 if (session->conn->protocol < PROTOCOL_SMB2_24) {
5727 return NT_STATUS_NOT_SUPPORTED;
5730 if (session->conn->smb2.server.cipher == 0) {
5731 return NT_STATUS_NOT_SUPPORTED;
5734 if (session->smb2->signing_key.data == NULL) {
5735 return NT_STATUS_NOT_SUPPORTED;
5737 session->smb2->should_encrypt = true;
5738 return NT_STATUS_OK;
5741 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
5743 struct smbXcli_tcon *tcon;
5745 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
5746 if (tcon == NULL) {
5747 return NULL;
5750 return tcon;
5753 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
5754 uint32_t fs_attributes)
5756 tcon->fs_attributes = fs_attributes;
5759 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
5761 return tcon->fs_attributes;
5764 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
5766 if (tcon == NULL) {
5767 return false;
5770 if (tcon->is_smb1) {
5771 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
5772 return true;
5775 return false;
5778 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
5779 return true;
5782 return false;
5785 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
5787 return tcon->smb1.tcon_id;
5790 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
5792 tcon->is_smb1 = true;
5793 tcon->smb1.tcon_id = tcon_id;
5796 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
5797 uint16_t tcon_id,
5798 uint16_t optional_support,
5799 uint32_t maximal_access,
5800 uint32_t guest_maximal_access,
5801 const char *service,
5802 const char *fs_type)
5804 tcon->is_smb1 = true;
5805 tcon->fs_attributes = 0;
5806 tcon->smb1.tcon_id = tcon_id;
5807 tcon->smb1.optional_support = optional_support;
5808 tcon->smb1.maximal_access = maximal_access;
5809 tcon->smb1.guest_maximal_access = guest_maximal_access;
5811 TALLOC_FREE(tcon->smb1.service);
5812 tcon->smb1.service = talloc_strdup(tcon, service);
5813 if (service != NULL && tcon->smb1.service == NULL) {
5814 return false;
5817 TALLOC_FREE(tcon->smb1.fs_type);
5818 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
5819 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
5820 return false;
5823 return true;
5826 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
5828 return tcon->smb2.tcon_id;
5831 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
5833 return tcon->smb2.capabilities;
5836 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
5837 struct smbXcli_session *session,
5838 uint32_t tcon_id,
5839 uint8_t type,
5840 uint32_t flags,
5841 uint32_t capabilities,
5842 uint32_t maximal_access)
5844 tcon->is_smb1 = false;
5845 tcon->fs_attributes = 0;
5846 tcon->smb2.tcon_id = tcon_id;
5847 tcon->smb2.type = type;
5848 tcon->smb2.flags = flags;
5849 tcon->smb2.capabilities = capabilities;
5850 tcon->smb2.maximal_access = maximal_access;
5852 tcon->smb2.should_sign = false;
5853 tcon->smb2.should_encrypt = false;
5855 if (session == NULL) {
5856 return;
5859 tcon->smb2.should_sign = session->smb2->should_sign;
5860 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
5862 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
5863 tcon->smb2.should_encrypt = true;
5867 void smb2cli_tcon_should_sign(struct smbXcli_tcon *tcon,
5868 bool should_sign)
5870 tcon->smb2.should_sign = should_sign;
5873 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon *tcon)
5875 if (tcon->smb2.should_encrypt) {
5876 return true;
5879 return tcon->smb2.should_sign;
5882 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon *tcon,
5883 bool should_encrypt)
5885 tcon->smb2.should_encrypt = should_encrypt;
5888 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
5890 return tcon->smb2.should_encrypt;