libcli/smb: prepare support for SMB2_SIGNING_CAPABILITIES negotiation
[Samba.git] / libcli / smb / smbXcli_base.c
blobc2d70ee016d1d28020b47f5be9b5e9c8952ff2e8
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 "libcli/smb/smb2_signing.h"
38 #include "lib/crypto/gnutls_helpers.h"
39 #include <gnutls/gnutls.h>
40 #include <gnutls/crypto.h>
42 struct smbXcli_conn;
43 struct smbXcli_req;
44 struct smbXcli_session;
45 struct smbXcli_tcon;
47 struct smbXcli_conn {
48 int sock_fd;
49 struct sockaddr_storage local_ss;
50 struct sockaddr_storage remote_ss;
51 const char *remote_name;
53 struct tevent_queue *outgoing;
54 struct tevent_req **pending;
55 struct tevent_req *read_smb_req;
56 struct tevent_req *suicide_req;
58 enum protocol_types min_protocol;
59 enum protocol_types max_protocol;
60 enum protocol_types protocol;
61 bool allow_signing;
62 bool desire_signing;
63 bool mandatory_signing;
66 * The incoming dispatch function should return:
67 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
68 * - NT_STATUS_OK, if no more processing is desired, e.g.
69 * the dispatch function called
70 * tevent_req_done().
71 * - All other return values disconnect the connection.
73 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
74 TALLOC_CTX *tmp_mem,
75 uint8_t *inbuf);
77 struct {
78 struct {
79 uint32_t capabilities;
80 uint32_t max_xmit;
81 } client;
83 struct {
84 uint32_t capabilities;
85 uint32_t max_xmit;
86 uint16_t max_mux;
87 uint16_t security_mode;
88 bool readbraw;
89 bool writebraw;
90 bool lockread;
91 bool writeunlock;
92 uint32_t session_key;
93 struct GUID guid;
94 DATA_BLOB gss_blob;
95 uint8_t challenge[8];
96 const char *workgroup;
97 const char *name;
98 int time_zone;
99 NTTIME system_time;
100 } server;
102 uint32_t capabilities;
103 uint32_t max_xmit;
105 uint16_t mid;
107 struct smb_signing_state *signing;
108 struct smb_trans_enc_state *trans_enc;
110 struct tevent_req *read_braw_req;
111 } smb1;
113 struct {
114 struct {
115 uint32_t capabilities;
116 uint16_t security_mode;
117 struct GUID guid;
118 struct smb311_capabilities smb3_capabilities;
119 } client;
121 struct {
122 uint32_t capabilities;
123 uint16_t security_mode;
124 struct GUID guid;
125 uint32_t max_trans_size;
126 uint32_t max_read_size;
127 uint32_t max_write_size;
128 NTTIME system_time;
129 NTTIME start_time;
130 DATA_BLOB gss_blob;
131 uint16_t sign_algo;
132 uint16_t cipher;
133 } server;
135 uint64_t mid;
136 uint16_t cur_credits;
137 uint16_t max_credits;
139 uint32_t cc_chunk_len;
140 uint32_t cc_max_chunks;
142 uint8_t io_priority;
144 bool force_channel_sequence;
146 uint8_t preauth_sha512[64];
147 } smb2;
149 struct smbXcli_session *sessions;
152 struct smb2cli_session {
153 uint64_t session_id;
154 uint16_t session_flags;
155 struct smb2_signing_key *application_key;
156 struct smb2_signing_key *signing_key;
157 bool should_sign;
158 bool should_encrypt;
159 struct smb2_signing_key *encryption_key;
160 struct smb2_signing_key *decryption_key;
161 uint64_t nonce_high_random;
162 uint64_t nonce_high_max;
163 uint64_t nonce_high;
164 uint64_t nonce_low;
165 uint16_t channel_sequence;
166 bool replay_active;
167 bool require_signed_response;
170 struct smbXcli_session {
171 struct smbXcli_session *prev, *next;
172 struct smbXcli_conn *conn;
174 struct {
175 uint16_t session_id;
176 uint16_t action;
177 DATA_BLOB application_key;
178 bool protected_key;
179 } smb1;
181 struct smb2cli_session *smb2;
183 struct {
184 struct smb2_signing_key *signing_key;
185 uint8_t preauth_sha512[64];
186 } smb2_channel;
189 * this should be a short term hack
190 * until the upper layers have implemented
191 * re-authentication.
193 bool disconnect_expired;
196 struct smbXcli_tcon {
197 bool is_smb1;
198 uint32_t fs_attributes;
200 struct {
201 uint16_t tcon_id;
202 uint16_t optional_support;
203 uint32_t maximal_access;
204 uint32_t guest_maximal_access;
205 char *service;
206 char *fs_type;
207 } smb1;
209 struct {
210 uint32_t tcon_id;
211 uint8_t type;
212 uint32_t flags;
213 uint32_t capabilities;
214 uint32_t maximal_access;
215 bool should_sign;
216 bool should_encrypt;
217 } smb2;
220 struct smbXcli_req_state {
221 struct tevent_context *ev;
222 struct smbXcli_conn *conn;
223 struct smbXcli_session *session; /* maybe NULL */
224 struct smbXcli_tcon *tcon; /* maybe NULL */
226 uint8_t length_hdr[4];
228 bool one_way;
230 uint8_t *inbuf;
232 struct tevent_req *write_req;
234 struct timeval endtime;
236 struct {
237 /* Space for the header including the wct */
238 uint8_t hdr[HDR_VWV];
241 * For normal requests, smb1cli_req_send chooses a mid.
242 * SecondaryV trans requests need to use the mid of the primary
243 * request, so we need a place to store it.
244 * Assume it is set if != 0.
246 uint16_t mid;
248 uint16_t *vwv;
249 uint8_t bytecount_buf[2];
251 #define MAX_SMB_IOV 10
252 /* length_hdr, hdr, words, byte_count, buffers */
253 struct iovec iov[1 + 3 + MAX_SMB_IOV];
254 int iov_count;
256 bool one_way_seqnum;
257 uint32_t seqnum;
258 struct tevent_req **chained_requests;
260 uint8_t recv_cmd;
261 NTSTATUS recv_status;
262 /* always an array of 3 talloc elements */
263 struct iovec *recv_iov;
264 } smb1;
266 struct {
267 const uint8_t *fixed;
268 uint16_t fixed_len;
269 const uint8_t *dyn;
270 uint32_t dyn_len;
272 uint8_t transform[SMB2_TF_HDR_SIZE];
273 uint8_t hdr[SMB2_HDR_BODY];
274 uint8_t pad[7]; /* padding space for compounding */
277 * always an array of 3 talloc elements
278 * (without a SMB2_TRANSFORM header!)
280 * HDR, BODY, DYN
282 struct iovec *recv_iov;
285 * the expected max for the response dyn_len
287 uint32_t max_dyn_len;
289 uint16_t credit_charge;
291 bool should_sign;
292 bool should_encrypt;
293 uint64_t encryption_session_id;
295 bool signing_skipped;
296 bool require_signed_response;
297 bool notify_async;
298 bool got_async;
299 uint16_t cancel_flags;
300 uint64_t cancel_mid;
301 uint64_t cancel_aid;
302 } smb2;
305 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
308 * NT_STATUS_OK, means we do not notify the callers
310 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
312 while (conn->sessions) {
313 conn->sessions->conn = NULL;
314 DLIST_REMOVE(conn->sessions, conn->sessions);
317 if (conn->smb1.trans_enc) {
318 TALLOC_FREE(conn->smb1.trans_enc);
321 return 0;
324 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
325 int fd,
326 const char *remote_name,
327 enum smb_signing_setting signing_state,
328 uint32_t smb1_capabilities,
329 struct GUID *client_guid,
330 uint32_t smb2_capabilities,
331 const struct smb311_capabilities *smb3_capabilities)
333 struct smbXcli_conn *conn = NULL;
334 void *ss = NULL;
335 struct sockaddr *sa = NULL;
336 socklen_t sa_length;
337 int ret;
339 if (smb3_capabilities != NULL) {
340 const struct smb3_signing_capabilities *sign_algos =
341 &smb3_capabilities->signing;
342 const struct smb3_encryption_capabilities *ciphers =
343 &smb3_capabilities->encryption;
345 SMB_ASSERT(sign_algos->num_algos <= SMB3_SIGNING_CAPABILITIES_MAX_ALGOS);
346 SMB_ASSERT(ciphers->num_algos <= SMB3_ENCRYTION_CAPABILITIES_MAX_ALGOS);
349 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
350 if (!conn) {
351 return NULL;
354 ret = set_blocking(fd, false);
355 if (ret < 0) {
356 goto error;
358 conn->sock_fd = fd;
360 conn->remote_name = talloc_strdup(conn, remote_name);
361 if (conn->remote_name == NULL) {
362 goto error;
365 ss = (void *)&conn->local_ss;
366 sa = (struct sockaddr *)ss;
367 sa_length = sizeof(conn->local_ss);
368 ret = getsockname(fd, sa, &sa_length);
369 if (ret == -1) {
370 goto error;
372 ss = (void *)&conn->remote_ss;
373 sa = (struct sockaddr *)ss;
374 sa_length = sizeof(conn->remote_ss);
375 ret = getpeername(fd, sa, &sa_length);
376 if (ret == -1) {
377 goto error;
380 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
381 if (conn->outgoing == NULL) {
382 goto error;
384 conn->pending = NULL;
386 conn->min_protocol = PROTOCOL_NONE;
387 conn->max_protocol = PROTOCOL_NONE;
388 conn->protocol = PROTOCOL_NONE;
390 switch (signing_state) {
391 case SMB_SIGNING_OFF:
392 /* never */
393 conn->allow_signing = false;
394 conn->desire_signing = false;
395 conn->mandatory_signing = false;
396 break;
397 case SMB_SIGNING_DEFAULT:
398 case SMB_SIGNING_IF_REQUIRED:
399 /* if the server requires it */
400 conn->allow_signing = true;
401 conn->desire_signing = false;
402 conn->mandatory_signing = false;
403 break;
404 case SMB_SIGNING_DESIRED:
405 /* if the server desires it */
406 conn->allow_signing = true;
407 conn->desire_signing = true;
408 conn->mandatory_signing = false;
409 break;
410 case SMB_SIGNING_IPC_DEFAULT:
411 case SMB_SIGNING_REQUIRED:
412 /* always */
413 conn->allow_signing = true;
414 conn->desire_signing = true;
415 conn->mandatory_signing = true;
416 break;
419 conn->smb1.client.capabilities = smb1_capabilities;
420 conn->smb1.client.max_xmit = UINT16_MAX;
422 conn->smb1.capabilities = conn->smb1.client.capabilities;
423 conn->smb1.max_xmit = 1024;
425 conn->smb1.mid = 1;
427 /* initialise signing */
428 conn->smb1.signing = smb_signing_init(conn,
429 conn->allow_signing,
430 conn->desire_signing,
431 conn->mandatory_signing);
432 if (!conn->smb1.signing) {
433 goto error;
436 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
437 if (conn->mandatory_signing) {
438 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
440 if (client_guid) {
441 conn->smb2.client.guid = *client_guid;
443 conn->smb2.client.capabilities = smb2_capabilities;
444 if (smb3_capabilities != NULL) {
445 conn->smb2.client.smb3_capabilities = *smb3_capabilities;
448 conn->smb2.cur_credits = 1;
449 conn->smb2.max_credits = 0;
450 conn->smb2.io_priority = 1;
453 * Samba and Windows servers accept a maximum of 16 MiB with a maximum
454 * chunk length of 1 MiB.
456 conn->smb2.cc_chunk_len = 1024 * 1024;
457 conn->smb2.cc_max_chunks = 16;
459 talloc_set_destructor(conn, smbXcli_conn_destructor);
460 return conn;
462 error:
463 TALLOC_FREE(conn);
464 return NULL;
467 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
469 if (conn == NULL) {
470 return false;
473 if (conn->sock_fd == -1) {
474 return false;
477 return true;
480 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
482 return conn->protocol;
485 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
487 if (conn->protocol >= PROTOCOL_SMB2_02) {
488 return true;
491 if (conn->smb1.capabilities & CAP_UNICODE) {
492 return true;
495 return false;
498 bool smbXcli_conn_signing_mandatory(struct smbXcli_conn *conn)
500 return conn->mandatory_signing;
504 * [MS-SMB] 2.2.2.3.5 - SMB1 support for passing through
505 * query/set commands to the file system
507 bool smbXcli_conn_support_passthrough(struct smbXcli_conn *conn)
509 if (conn->protocol >= PROTOCOL_SMB2_02) {
510 return true;
513 if (conn->smb1.capabilities & CAP_W2K_SMBS) {
514 return true;
517 return false;
520 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
522 set_socket_options(conn->sock_fd, options);
525 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
527 return &conn->local_ss;
530 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
532 return &conn->remote_ss;
535 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
537 return conn->remote_name;
540 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
542 if (conn->protocol >= PROTOCOL_SMB2_02) {
544 * TODO...
546 return 1;
549 return conn->smb1.server.max_mux;
552 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
554 if (conn->protocol >= PROTOCOL_SMB2_02) {
555 return conn->smb2.server.system_time;
558 return conn->smb1.server.system_time;
561 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
563 if (conn->protocol >= PROTOCOL_SMB2_02) {
564 return &conn->smb2.server.gss_blob;
567 return &conn->smb1.server.gss_blob;
570 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
572 if (conn->protocol >= PROTOCOL_SMB2_02) {
573 return &conn->smb2.server.guid;
576 return &conn->smb1.server.guid;
579 bool smbXcli_conn_get_force_channel_sequence(struct smbXcli_conn *conn)
581 return conn->smb2.force_channel_sequence;
584 void smbXcli_conn_set_force_channel_sequence(struct smbXcli_conn *conn,
585 bool v)
587 conn->smb2.force_channel_sequence = v;
590 struct smbXcli_conn_samba_suicide_state {
591 struct smbXcli_conn *conn;
592 struct iovec iov;
593 uint8_t buf[9];
594 struct tevent_req *write_req;
597 static void smbXcli_conn_samba_suicide_cleanup(struct tevent_req *req,
598 enum tevent_req_state req_state);
599 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
601 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
602 struct tevent_context *ev,
603 struct smbXcli_conn *conn,
604 uint8_t exitcode)
606 struct tevent_req *req, *subreq;
607 struct smbXcli_conn_samba_suicide_state *state;
609 req = tevent_req_create(mem_ctx, &state,
610 struct smbXcli_conn_samba_suicide_state);
611 if (req == NULL) {
612 return NULL;
614 state->conn = conn;
615 SIVAL(state->buf, 4, SMB_SUICIDE_PACKET);
616 SCVAL(state->buf, 8, exitcode);
617 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
619 if (conn->suicide_req != NULL) {
620 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
621 return tevent_req_post(req, ev);
624 state->iov.iov_base = state->buf;
625 state->iov.iov_len = sizeof(state->buf);
627 subreq = writev_send(state, ev, conn->outgoing, conn->sock_fd,
628 false, &state->iov, 1);
629 if (tevent_req_nomem(subreq, req)) {
630 return tevent_req_post(req, ev);
632 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
633 state->write_req = subreq;
635 tevent_req_set_cleanup_fn(req, smbXcli_conn_samba_suicide_cleanup);
638 * We need to use tevent_req_defer_callback()
639 * in order to allow smbXcli_conn_disconnect()
640 * to do a safe cleanup.
642 tevent_req_defer_callback(req, ev);
643 conn->suicide_req = req;
645 return req;
648 static void smbXcli_conn_samba_suicide_cleanup(struct tevent_req *req,
649 enum tevent_req_state req_state)
651 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
652 req, struct smbXcli_conn_samba_suicide_state);
654 TALLOC_FREE(state->write_req);
656 if (state->conn == NULL) {
657 return;
660 if (state->conn->suicide_req == req) {
661 state->conn->suicide_req = NULL;
663 state->conn = NULL;
666 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
668 struct tevent_req *req = tevent_req_callback_data(
669 subreq, struct tevent_req);
670 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
671 req, struct smbXcli_conn_samba_suicide_state);
672 ssize_t nwritten;
673 int err;
675 state->write_req = NULL;
677 nwritten = writev_recv(subreq, &err);
678 TALLOC_FREE(subreq);
679 if (nwritten == -1) {
680 /* here, we need to notify all pending requests */
681 NTSTATUS status = map_nt_error_from_unix_common(err);
682 smbXcli_conn_disconnect(state->conn, status);
683 return;
685 tevent_req_done(req);
688 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
690 return tevent_req_simple_recv_ntstatus(req);
693 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
694 uint8_t exitcode)
696 TALLOC_CTX *frame = talloc_stackframe();
697 struct tevent_context *ev;
698 struct tevent_req *req;
699 NTSTATUS status = NT_STATUS_NO_MEMORY;
700 bool ok;
702 if (smbXcli_conn_has_async_calls(conn)) {
704 * Can't use sync call while an async call is in flight
706 status = NT_STATUS_INVALID_PARAMETER_MIX;
707 goto fail;
709 ev = samba_tevent_context_init(frame);
710 if (ev == NULL) {
711 goto fail;
713 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
714 if (req == NULL) {
715 goto fail;
717 ok = tevent_req_poll_ntstatus(req, ev, &status);
718 if (!ok) {
719 goto fail;
721 status = smbXcli_conn_samba_suicide_recv(req);
722 fail:
723 TALLOC_FREE(frame);
724 return status;
727 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
729 return conn->smb1.capabilities;
732 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
734 return conn->smb1.max_xmit;
737 bool smb1cli_conn_req_possible(struct smbXcli_conn *conn)
739 size_t pending = talloc_array_length(conn->pending);
740 uint16_t possible = conn->smb1.server.max_mux;
742 if (pending >= possible) {
743 return false;
746 return true;
749 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
751 return conn->smb1.server.session_key;
754 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
756 return conn->smb1.server.challenge;
759 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
761 return conn->smb1.server.security_mode;
764 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
766 return conn->smb1.server.readbraw;
769 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
771 return conn->smb1.server.writebraw;
774 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
776 return conn->smb1.server.lockread;
779 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
781 return conn->smb1.server.writeunlock;
784 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
786 return conn->smb1.server.time_zone;
789 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
790 const DATA_BLOB user_session_key,
791 const DATA_BLOB response)
793 return smb_signing_activate(conn->smb1.signing,
794 user_session_key,
795 response);
798 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
799 const uint8_t *buf, uint32_t seqnum)
801 const uint8_t *hdr = buf + NBT_HDR_SIZE;
802 size_t len = smb_len_nbt(buf);
804 return smb_signing_check_pdu(conn->smb1.signing, hdr, len, seqnum);
807 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
809 return smb_signing_is_active(conn->smb1.signing);
812 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
813 struct smb_trans_enc_state *es)
815 /* Replace the old state, if any. */
816 if (conn->smb1.trans_enc) {
817 TALLOC_FREE(conn->smb1.trans_enc);
819 conn->smb1.trans_enc = es;
822 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
824 return common_encryption_on(conn->smb1.trans_enc);
828 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
830 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
831 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
833 if (NT_STATUS_IS_OK(status)) {
834 return NT_STATUS_OK;
837 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
838 return status;
841 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
845 * Is the SMB command able to hold an AND_X successor
846 * @param[in] cmd The SMB command in question
847 * @retval Can we add a chained request after "cmd"?
849 bool smb1cli_is_andx_req(uint8_t cmd)
851 switch (cmd) {
852 case SMBtconX:
853 case SMBlockingX:
854 case SMBopenX:
855 case SMBreadX:
856 case SMBwriteX:
857 case SMBsesssetupX:
858 case SMBulogoffX:
859 case SMBntcreateX:
860 return true;
861 break;
862 default:
863 break;
866 return false;
869 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
871 size_t num_pending = talloc_array_length(conn->pending);
872 uint16_t result;
874 if (conn->protocol == PROTOCOL_NONE) {
876 * This is what windows sends on the SMB1 Negprot request
877 * and some vendors reuse the SMB1 MID as SMB2 sequence number.
879 return 0;
882 while (true) {
883 size_t i;
885 result = conn->smb1.mid++;
886 if ((result == 0) || (result == 0xffff)) {
887 continue;
890 for (i=0; i<num_pending; i++) {
891 if (result == smb1cli_req_mid(conn->pending[i])) {
892 break;
896 if (i == num_pending) {
897 return result;
902 static NTSTATUS smbXcli_req_cancel_write_req(struct tevent_req *req)
904 struct smbXcli_req_state *state =
905 tevent_req_data(req,
906 struct smbXcli_req_state);
907 struct smbXcli_conn *conn = state->conn;
908 size_t num_pending = talloc_array_length(conn->pending);
909 ssize_t ret;
910 int err;
911 bool ok;
913 if (state->write_req == NULL) {
914 return NT_STATUS_OK;
918 * Check if it's possible to cancel the request.
919 * If the result is true it's not too late.
920 * See writev_cancel().
922 ok = tevent_req_cancel(state->write_req);
923 if (ok) {
924 TALLOC_FREE(state->write_req);
926 if (conn->protocol >= PROTOCOL_SMB2_02) {
928 * SMB2 has a sane signing state.
930 return NT_STATUS_OK;
933 if (num_pending > 1) {
935 * We have more pending requests following us. This
936 * means the signing state will be broken for them.
938 * As a solution we could add the requests directly to
939 * our outgoing queue and do the signing in the trigger
940 * function and then use writev_send() without passing a
941 * queue. That way we'll only sign packets we're most
942 * likely send to the wire.
944 return NT_STATUS_REQUEST_OUT_OF_SEQUENCE;
948 * If we're the only request that's
949 * pending, we're able to recover the signing
950 * state.
952 smb_signing_cancel_reply(conn->smb1.signing,
953 state->smb1.one_way_seqnum);
954 return NT_STATUS_OK;
957 ret = writev_recv(state->write_req, &err);
958 TALLOC_FREE(state->write_req);
959 if (ret == -1) {
960 return map_nt_error_from_unix_common(err);
963 return NT_STATUS_OK;
966 void smbXcli_req_unset_pending(struct tevent_req *req)
968 struct smbXcli_req_state *state =
969 tevent_req_data(req,
970 struct smbXcli_req_state);
971 struct smbXcli_conn *conn = state->conn;
972 size_t num_pending = talloc_array_length(conn->pending);
973 size_t i;
974 NTSTATUS cancel_status;
976 cancel_status = smbXcli_req_cancel_write_req(req);
978 if (state->smb1.mid != 0) {
980 * This is a [nt]trans[2] request which waits
981 * for more than one reply.
983 if (!NT_STATUS_IS_OK(cancel_status)) {
985 * If the write_req cancel didn't work
986 * we can't use the connection anymore.
988 smbXcli_conn_disconnect(conn, cancel_status);
989 return;
991 return;
994 tevent_req_set_cleanup_fn(req, NULL);
996 if (num_pending == 1) {
998 * The pending read_smb tevent_req is a child of
999 * conn->pending. So if nothing is pending anymore, we need to
1000 * delete the socket read fde.
1002 /* TODO: smbXcli_conn_cancel_read_req */
1003 TALLOC_FREE(conn->pending);
1004 conn->read_smb_req = NULL;
1006 if (!NT_STATUS_IS_OK(cancel_status)) {
1008 * If the write_req cancel didn't work
1009 * we can't use the connection anymore.
1011 smbXcli_conn_disconnect(conn, cancel_status);
1012 return;
1014 return;
1017 for (i=0; i<num_pending; i++) {
1018 if (req == conn->pending[i]) {
1019 break;
1022 if (i == num_pending) {
1024 * Something's seriously broken. Just returning here is the
1025 * right thing nevertheless, the point of this routine is to
1026 * remove ourselves from conn->pending.
1029 if (!NT_STATUS_IS_OK(cancel_status)) {
1031 * If the write_req cancel didn't work
1032 * we can't use the connection anymore.
1034 smbXcli_conn_disconnect(conn, cancel_status);
1035 return;
1037 return;
1041 * Remove ourselves from the conn->pending array
1043 for (; i < (num_pending - 1); i++) {
1044 conn->pending[i] = conn->pending[i+1];
1048 * No NULL check here, we're shrinking by sizeof(void *), and
1049 * talloc_realloc just adjusts the size for this.
1051 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
1052 num_pending - 1);
1054 if (!NT_STATUS_IS_OK(cancel_status)) {
1056 * If the write_req cancel didn't work
1057 * we can't use the connection anymore.
1059 smbXcli_conn_disconnect(conn, cancel_status);
1060 return;
1062 return;
1065 static void smbXcli_req_cleanup(struct tevent_req *req,
1066 enum tevent_req_state req_state)
1068 struct smbXcli_req_state *state =
1069 tevent_req_data(req,
1070 struct smbXcli_req_state);
1071 struct smbXcli_conn *conn = state->conn;
1072 NTSTATUS cancel_status;
1074 switch (req_state) {
1075 case TEVENT_REQ_RECEIVED:
1077 * Make sure we really remove it from
1078 * the pending array on destruction.
1080 * smbXcli_req_unset_pending() calls
1081 * smbXcli_req_cancel_write_req() internal
1083 state->smb1.mid = 0;
1084 smbXcli_req_unset_pending(req);
1085 return;
1086 default:
1087 cancel_status = smbXcli_req_cancel_write_req(req);
1088 if (!NT_STATUS_IS_OK(cancel_status)) {
1090 * If the write_req cancel didn't work
1091 * we can't use the connection anymore.
1093 smbXcli_conn_disconnect(conn, cancel_status);
1094 return;
1096 return;
1100 static bool smb1cli_req_cancel(struct tevent_req *req);
1101 static bool smb2cli_req_cancel(struct tevent_req *req);
1103 static bool smbXcli_req_cancel(struct tevent_req *req)
1105 struct smbXcli_req_state *state =
1106 tevent_req_data(req,
1107 struct smbXcli_req_state);
1109 if (!smbXcli_conn_is_connected(state->conn)) {
1110 return false;
1113 if (state->conn->protocol == PROTOCOL_NONE) {
1114 return false;
1117 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
1118 return smb2cli_req_cancel(req);
1121 return smb1cli_req_cancel(req);
1124 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
1126 bool smbXcli_req_set_pending(struct tevent_req *req)
1128 struct smbXcli_req_state *state =
1129 tevent_req_data(req,
1130 struct smbXcli_req_state);
1131 struct smbXcli_conn *conn;
1132 struct tevent_req **pending;
1133 size_t num_pending;
1135 conn = state->conn;
1137 if (!smbXcli_conn_is_connected(conn)) {
1138 return false;
1141 num_pending = talloc_array_length(conn->pending);
1143 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
1144 num_pending+1);
1145 if (pending == NULL) {
1146 return false;
1148 pending[num_pending] = req;
1149 conn->pending = pending;
1150 tevent_req_set_cleanup_fn(req, smbXcli_req_cleanup);
1151 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1153 if (!smbXcli_conn_receive_next(conn)) {
1155 * the caller should notify the current request
1157 * And all other pending requests get notified
1158 * by smbXcli_conn_disconnect().
1160 smbXcli_req_unset_pending(req);
1161 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1162 return false;
1165 return true;
1168 static void smbXcli_conn_received(struct tevent_req *subreq);
1170 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
1172 size_t num_pending = talloc_array_length(conn->pending);
1173 struct tevent_req *req;
1174 struct smbXcli_req_state *state;
1176 if (conn->read_smb_req != NULL) {
1177 return true;
1180 if (num_pending == 0) {
1181 if (conn->smb2.mid < UINT64_MAX) {
1182 /* no more pending requests, so we are done for now */
1183 return true;
1187 * If there are no more SMB2 requests possible,
1188 * because we are out of message ids,
1189 * we need to disconnect.
1191 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
1192 return true;
1195 req = conn->pending[0];
1196 state = tevent_req_data(req, struct smbXcli_req_state);
1199 * We're the first ones, add the read_smb request that waits for the
1200 * answer from the server
1202 conn->read_smb_req = read_smb_send(conn->pending,
1203 state->ev,
1204 conn->sock_fd);
1205 if (conn->read_smb_req == NULL) {
1206 return false;
1208 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
1209 return true;
1212 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
1214 struct smbXcli_session *session;
1215 int sock_fd = conn->sock_fd;
1217 tevent_queue_stop(conn->outgoing);
1219 conn->sock_fd = -1;
1221 session = conn->sessions;
1222 if (talloc_array_length(conn->pending) == 0) {
1224 * if we do not have pending requests
1225 * there is no need to update the channel_sequence
1227 session = NULL;
1229 for (; session; session = session->next) {
1230 smb2cli_session_increment_channel_sequence(session);
1233 if (conn->suicide_req != NULL) {
1235 * smbXcli_conn_samba_suicide_send()
1236 * used tevent_req_defer_callback() already.
1238 if (!NT_STATUS_IS_OK(status)) {
1239 tevent_req_nterror(conn->suicide_req, status);
1241 conn->suicide_req = NULL;
1245 * Cancel all pending requests. We do not do a for-loop walking
1246 * conn->pending because that array changes in
1247 * smbXcli_req_unset_pending.
1249 while (conn->pending != NULL &&
1250 talloc_array_length(conn->pending) > 0) {
1251 struct tevent_req *req;
1252 struct smbXcli_req_state *state;
1253 struct tevent_req **chain;
1254 size_t num_chained;
1255 size_t i;
1257 req = conn->pending[0];
1258 state = tevent_req_data(req, struct smbXcli_req_state);
1260 if (state->smb1.chained_requests == NULL) {
1261 bool in_progress;
1264 * We're dead. No point waiting for trans2
1265 * replies.
1267 state->smb1.mid = 0;
1269 smbXcli_req_unset_pending(req);
1271 if (NT_STATUS_IS_OK(status)) {
1272 /* do not notify the callers */
1273 continue;
1276 in_progress = tevent_req_is_in_progress(req);
1277 if (!in_progress) {
1279 * already finished
1281 continue;
1285 * we need to defer the callback, because we may notify
1286 * more then one caller.
1288 tevent_req_defer_callback(req, state->ev);
1289 tevent_req_nterror(req, status);
1290 continue;
1293 chain = talloc_move(conn, &state->smb1.chained_requests);
1294 num_chained = talloc_array_length(chain);
1296 for (i=0; i<num_chained; i++) {
1297 bool in_progress;
1299 req = chain[i];
1300 state = tevent_req_data(req, struct smbXcli_req_state);
1303 * We're dead. No point waiting for trans2
1304 * replies.
1306 state->smb1.mid = 0;
1308 smbXcli_req_unset_pending(req);
1310 if (NT_STATUS_IS_OK(status)) {
1311 /* do not notify the callers */
1312 continue;
1315 in_progress = tevent_req_is_in_progress(req);
1316 if (!in_progress) {
1318 * already finished
1320 continue;
1324 * we need to defer the callback, because we may notify
1325 * more than one caller.
1327 tevent_req_defer_callback(req, state->ev);
1328 tevent_req_nterror(req, status);
1330 TALLOC_FREE(chain);
1333 if (sock_fd != -1) {
1334 close(sock_fd);
1339 * Fetch a smb request's mid. Only valid after the request has been sent by
1340 * smb1cli_req_send().
1342 uint16_t smb1cli_req_mid(struct tevent_req *req)
1344 struct smbXcli_req_state *state =
1345 tevent_req_data(req,
1346 struct smbXcli_req_state);
1348 if (state->smb1.mid != 0) {
1349 return state->smb1.mid;
1352 return SVAL(state->smb1.hdr, HDR_MID);
1355 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
1357 struct smbXcli_req_state *state =
1358 tevent_req_data(req,
1359 struct smbXcli_req_state);
1361 state->smb1.mid = mid;
1364 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
1366 struct smbXcli_req_state *state =
1367 tevent_req_data(req,
1368 struct smbXcli_req_state);
1370 return state->smb1.seqnum;
1373 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
1375 struct smbXcli_req_state *state =
1376 tevent_req_data(req,
1377 struct smbXcli_req_state);
1379 state->smb1.seqnum = seqnum;
1382 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1384 ssize_t ret = iov_buflen(iov, count);
1386 /* Ignore the overflow case for now ... */
1387 return ret;
1390 static void smb1cli_req_flags(enum protocol_types protocol,
1391 uint32_t smb1_capabilities,
1392 uint8_t smb_command,
1393 uint8_t additional_flags,
1394 uint8_t clear_flags,
1395 uint8_t *_flags,
1396 uint16_t additional_flags2,
1397 uint16_t clear_flags2,
1398 uint16_t *_flags2)
1400 uint8_t flags = 0;
1401 uint16_t flags2 = 0;
1403 if (protocol >= PROTOCOL_LANMAN1) {
1404 flags |= FLAG_CASELESS_PATHNAMES;
1405 flags |= FLAG_CANONICAL_PATHNAMES;
1408 if (protocol >= PROTOCOL_LANMAN2) {
1409 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1410 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1413 if (protocol >= PROTOCOL_NT1) {
1414 flags2 |= FLAGS2_IS_LONG_NAME;
1416 if (smb1_capabilities & CAP_UNICODE) {
1417 flags2 |= FLAGS2_UNICODE_STRINGS;
1419 if (smb1_capabilities & CAP_STATUS32) {
1420 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1422 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1423 flags2 |= FLAGS2_EXTENDED_SECURITY;
1427 flags |= additional_flags;
1428 flags &= ~clear_flags;
1429 flags2 |= additional_flags2;
1430 flags2 &= ~clear_flags2;
1432 *_flags = flags;
1433 *_flags2 = flags2;
1436 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1438 static bool smb1cli_req_cancel(struct tevent_req *req)
1440 struct smbXcli_req_state *state =
1441 tevent_req_data(req,
1442 struct smbXcli_req_state);
1443 uint8_t flags;
1444 uint16_t flags2;
1445 uint32_t pid;
1446 uint16_t mid;
1447 struct tevent_req *subreq;
1448 NTSTATUS status;
1450 flags = CVAL(state->smb1.hdr, HDR_FLG);
1451 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1452 pid = SVAL(state->smb1.hdr, HDR_PID);
1453 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1454 mid = SVAL(state->smb1.hdr, HDR_MID);
1456 subreq = smb1cli_req_create(state, state->ev,
1457 state->conn,
1458 SMBntcancel,
1459 flags, 0,
1460 flags2, 0,
1461 0, /* timeout */
1462 pid,
1463 state->tcon,
1464 state->session,
1465 0, NULL, /* vwv */
1466 0, NULL); /* bytes */
1467 if (subreq == NULL) {
1468 return false;
1470 smb1cli_req_set_mid(subreq, mid);
1472 status = smb1cli_req_chain_submit(&subreq, 1);
1473 if (!NT_STATUS_IS_OK(status)) {
1474 TALLOC_FREE(subreq);
1475 return false;
1477 smb1cli_req_set_mid(subreq, 0);
1479 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1481 return true;
1484 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1486 /* we do not care about the result */
1487 TALLOC_FREE(subreq);
1490 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1491 struct tevent_context *ev,
1492 struct smbXcli_conn *conn,
1493 uint8_t smb_command,
1494 uint8_t additional_flags,
1495 uint8_t clear_flags,
1496 uint16_t additional_flags2,
1497 uint16_t clear_flags2,
1498 uint32_t timeout_msec,
1499 uint32_t pid,
1500 struct smbXcli_tcon *tcon,
1501 struct smbXcli_session *session,
1502 uint8_t wct, uint16_t *vwv,
1503 int iov_count,
1504 struct iovec *bytes_iov)
1506 struct tevent_req *req;
1507 struct smbXcli_req_state *state;
1508 uint8_t flags = 0;
1509 uint16_t flags2 = 0;
1510 uint16_t uid = 0;
1511 uint16_t tid = 0;
1512 ssize_t num_bytes;
1514 if (iov_count > MAX_SMB_IOV) {
1516 * Should not happen :-)
1518 return NULL;
1521 req = tevent_req_create(mem_ctx, &state,
1522 struct smbXcli_req_state);
1523 if (req == NULL) {
1524 return NULL;
1526 state->ev = ev;
1527 state->conn = conn;
1528 state->session = session;
1529 state->tcon = tcon;
1531 if (session) {
1532 uid = session->smb1.session_id;
1535 if (tcon) {
1536 tid = tcon->smb1.tcon_id;
1538 if (tcon->fs_attributes & FILE_CASE_SENSITIVE_SEARCH) {
1539 clear_flags |= FLAG_CASELESS_PATHNAMES;
1540 } else {
1541 /* Default setting, case insensitive. */
1542 additional_flags |= FLAG_CASELESS_PATHNAMES;
1545 if (smbXcli_conn_dfs_supported(conn) &&
1546 smbXcli_tcon_is_dfs_share(tcon))
1548 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
1552 state->smb1.recv_cmd = 0xFF;
1553 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1554 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1555 if (state->smb1.recv_iov == NULL) {
1556 TALLOC_FREE(req);
1557 return NULL;
1560 smb1cli_req_flags(conn->protocol,
1561 conn->smb1.capabilities,
1562 smb_command,
1563 additional_flags,
1564 clear_flags,
1565 &flags,
1566 additional_flags2,
1567 clear_flags2,
1568 &flags2);
1570 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1571 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1572 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1573 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1574 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1575 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1576 SSVAL(state->smb1.hdr, HDR_TID, tid);
1577 SSVAL(state->smb1.hdr, HDR_PID, pid);
1578 SSVAL(state->smb1.hdr, HDR_UID, uid);
1579 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1580 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1582 state->smb1.vwv = vwv;
1584 num_bytes = iov_buflen(bytes_iov, iov_count);
1585 if (num_bytes == -1) {
1587 * I'd love to add a check for num_bytes<=UINT16_MAX here, but
1588 * the smbclient->samba connections can lie and transfer more.
1590 TALLOC_FREE(req);
1591 return NULL;
1594 SSVAL(state->smb1.bytecount_buf, 0, num_bytes);
1596 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1597 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1598 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1599 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1600 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1601 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1602 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1603 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1605 if (iov_count != 0) {
1606 memcpy(&state->smb1.iov[4], bytes_iov,
1607 iov_count * sizeof(*bytes_iov));
1609 state->smb1.iov_count = iov_count + 4;
1611 if (timeout_msec > 0) {
1612 state->endtime = timeval_current_ofs_msec(timeout_msec);
1613 if (!tevent_req_set_endtime(req, ev, state->endtime)) {
1614 return req;
1618 switch (smb_command) {
1619 case SMBtranss:
1620 case SMBtranss2:
1621 case SMBnttranss:
1622 state->one_way = true;
1623 break;
1624 case SMBntcancel:
1625 state->one_way = true;
1626 state->smb1.one_way_seqnum = true;
1627 break;
1628 case SMBlockingX:
1629 if ((wct == 8) &&
1630 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1631 state->one_way = true;
1633 break;
1636 return req;
1639 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1640 struct iovec *iov, int iov_count,
1641 uint32_t *seqnum,
1642 bool one_way_seqnum)
1644 TALLOC_CTX *frame = NULL;
1645 NTSTATUS status;
1646 uint8_t *buf;
1649 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1650 * iovec directly. MD5Update would do that just fine.
1653 if (iov_count < 4) {
1654 return NT_STATUS_INVALID_PARAMETER_MIX;
1656 if (iov[0].iov_len != NBT_HDR_SIZE) {
1657 return NT_STATUS_INVALID_PARAMETER_MIX;
1659 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1660 return NT_STATUS_INVALID_PARAMETER_MIX;
1662 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1663 return NT_STATUS_INVALID_PARAMETER_MIX;
1665 if (iov[3].iov_len != sizeof(uint16_t)) {
1666 return NT_STATUS_INVALID_PARAMETER_MIX;
1669 frame = talloc_stackframe();
1671 buf = iov_concat(frame, &iov[1], iov_count - 1);
1672 if (buf == NULL) {
1673 return NT_STATUS_NO_MEMORY;
1676 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1677 one_way_seqnum);
1678 status = smb_signing_sign_pdu(conn->smb1.signing,
1679 buf,
1680 talloc_get_size(buf),
1681 *seqnum);
1682 if (!NT_STATUS_IS_OK(status)) {
1683 return status;
1685 memcpy(iov[1].iov_base, buf, iov[1].iov_len);
1687 TALLOC_FREE(frame);
1688 return NT_STATUS_OK;
1691 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1692 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1693 TALLOC_CTX *tmp_mem,
1694 uint8_t *inbuf);
1696 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1697 struct smbXcli_req_state *state,
1698 struct iovec *iov, int iov_count)
1700 struct tevent_req *subreq;
1701 NTSTATUS status;
1702 uint8_t cmd;
1703 uint16_t mid;
1704 ssize_t nbtlen;
1706 if (!smbXcli_conn_is_connected(state->conn)) {
1707 return NT_STATUS_CONNECTION_DISCONNECTED;
1710 if (state->conn->protocol > PROTOCOL_NT1) {
1711 DBG_ERR("called for dialect[%s] server[%s]\n",
1712 smb_protocol_types_string(state->conn->protocol),
1713 smbXcli_conn_remote_name(state->conn));
1714 return NT_STATUS_REVISION_MISMATCH;
1717 if (iov_count < 4) {
1718 return NT_STATUS_INVALID_PARAMETER_MIX;
1720 if (iov[0].iov_len != NBT_HDR_SIZE) {
1721 return NT_STATUS_INVALID_PARAMETER_MIX;
1723 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1724 return NT_STATUS_INVALID_PARAMETER_MIX;
1726 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1727 return NT_STATUS_INVALID_PARAMETER_MIX;
1729 if (iov[3].iov_len != sizeof(uint16_t)) {
1730 return NT_STATUS_INVALID_PARAMETER_MIX;
1733 cmd = CVAL(iov[1].iov_base, HDR_COM);
1734 if (cmd == SMBreadBraw) {
1735 if (smbXcli_conn_has_async_calls(state->conn)) {
1736 return NT_STATUS_INVALID_PARAMETER_MIX;
1738 state->conn->smb1.read_braw_req = req;
1741 if (state->smb1.mid != 0) {
1742 mid = state->smb1.mid;
1743 } else {
1744 mid = smb1cli_alloc_mid(state->conn);
1746 SSVAL(iov[1].iov_base, HDR_MID, mid);
1748 nbtlen = iov_buflen(&iov[1], iov_count-1);
1749 if ((nbtlen == -1) || (nbtlen > 0x1FFFF)) {
1750 return NT_STATUS_INVALID_PARAMETER_MIX;
1753 _smb_setlen_nbt(iov[0].iov_base, nbtlen);
1755 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1756 &state->smb1.seqnum,
1757 state->smb1.one_way_seqnum);
1759 if (!NT_STATUS_IS_OK(status)) {
1760 return status;
1764 * If we supported multiple encrytion contexts
1765 * here we'd look up based on tid.
1767 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1768 char *buf, *enc_buf;
1770 buf = (char *)iov_concat(talloc_tos(), iov, iov_count);
1771 if (buf == NULL) {
1772 return NT_STATUS_NO_MEMORY;
1774 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1775 (char *)buf, &enc_buf);
1776 TALLOC_FREE(buf);
1777 if (!NT_STATUS_IS_OK(status)) {
1778 DEBUG(0, ("Error in encrypting client message: %s\n",
1779 nt_errstr(status)));
1780 return status;
1782 buf = (char *)talloc_memdup(state, enc_buf,
1783 smb_len_nbt(enc_buf)+4);
1784 SAFE_FREE(enc_buf);
1785 if (buf == NULL) {
1786 return NT_STATUS_NO_MEMORY;
1788 iov[0].iov_base = (void *)buf;
1789 iov[0].iov_len = talloc_get_size(buf);
1790 iov_count = 1;
1793 if (state->conn->dispatch_incoming == NULL) {
1794 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1797 if (!smbXcli_req_set_pending(req)) {
1798 return NT_STATUS_NO_MEMORY;
1801 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1803 subreq = writev_send(state, state->ev, state->conn->outgoing,
1804 state->conn->sock_fd, false, iov, iov_count);
1805 if (subreq == NULL) {
1806 return NT_STATUS_NO_MEMORY;
1808 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1809 state->write_req = subreq;
1811 return NT_STATUS_OK;
1814 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1815 struct tevent_context *ev,
1816 struct smbXcli_conn *conn,
1817 uint8_t smb_command,
1818 uint8_t additional_flags,
1819 uint8_t clear_flags,
1820 uint16_t additional_flags2,
1821 uint16_t clear_flags2,
1822 uint32_t timeout_msec,
1823 uint32_t pid,
1824 struct smbXcli_tcon *tcon,
1825 struct smbXcli_session *session,
1826 uint8_t wct, uint16_t *vwv,
1827 uint32_t num_bytes,
1828 const uint8_t *bytes)
1830 struct tevent_req *req;
1831 struct iovec iov;
1832 NTSTATUS status;
1834 iov.iov_base = discard_const_p(void, bytes);
1835 iov.iov_len = num_bytes;
1837 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1838 additional_flags, clear_flags,
1839 additional_flags2, clear_flags2,
1840 timeout_msec,
1841 pid, tcon, session,
1842 wct, vwv, 1, &iov);
1843 if (req == NULL) {
1844 return NULL;
1846 if (!tevent_req_is_in_progress(req)) {
1847 return tevent_req_post(req, ev);
1849 status = smb1cli_req_chain_submit(&req, 1);
1850 if (tevent_req_nterror(req, status)) {
1851 return tevent_req_post(req, ev);
1853 return req;
1856 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1858 struct tevent_req *req =
1859 tevent_req_callback_data(subreq,
1860 struct tevent_req);
1861 struct smbXcli_req_state *state =
1862 tevent_req_data(req,
1863 struct smbXcli_req_state);
1864 ssize_t nwritten;
1865 int err;
1867 state->write_req = NULL;
1869 nwritten = writev_recv(subreq, &err);
1870 TALLOC_FREE(subreq);
1871 if (nwritten == -1) {
1872 /* here, we need to notify all pending requests */
1873 NTSTATUS status = map_nt_error_from_unix_common(err);
1874 smbXcli_conn_disconnect(state->conn, status);
1875 return;
1878 if (state->one_way) {
1879 state->inbuf = NULL;
1880 tevent_req_done(req);
1881 return;
1885 static void smbXcli_conn_received(struct tevent_req *subreq)
1887 struct smbXcli_conn *conn =
1888 tevent_req_callback_data(subreq,
1889 struct smbXcli_conn);
1890 TALLOC_CTX *frame = talloc_stackframe();
1891 NTSTATUS status;
1892 uint8_t *inbuf;
1893 ssize_t received;
1894 int err;
1896 if (subreq != conn->read_smb_req) {
1897 DEBUG(1, ("Internal error: cli_smb_received called with "
1898 "unexpected subreq\n"));
1899 smbXcli_conn_disconnect(conn, NT_STATUS_INTERNAL_ERROR);
1900 TALLOC_FREE(frame);
1901 return;
1903 conn->read_smb_req = NULL;
1905 received = read_smb_recv(subreq, frame, &inbuf, &err);
1906 TALLOC_FREE(subreq);
1907 if (received == -1) {
1908 status = map_nt_error_from_unix_common(err);
1909 smbXcli_conn_disconnect(conn, status);
1910 TALLOC_FREE(frame);
1911 return;
1914 status = conn->dispatch_incoming(conn, frame, inbuf);
1915 TALLOC_FREE(frame);
1916 if (NT_STATUS_IS_OK(status)) {
1918 * We should not do any more processing
1919 * as the dispatch function called
1920 * tevent_req_done().
1922 return;
1925 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1927 * We got an error, so notify all pending requests
1929 smbXcli_conn_disconnect(conn, status);
1930 return;
1934 * We got NT_STATUS_RETRY, so we may ask for a
1935 * next incoming pdu.
1937 if (!smbXcli_conn_receive_next(conn)) {
1938 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1942 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1943 struct iovec **piov, int *pnum_iov)
1945 struct iovec *iov;
1946 size_t num_iov;
1947 size_t buflen;
1948 size_t taken;
1949 size_t remaining;
1950 uint8_t *hdr;
1951 uint8_t cmd;
1952 uint32_t wct_ofs;
1953 NTSTATUS status;
1954 size_t min_size = MIN_SMB_SIZE;
1956 buflen = smb_len_tcp(buf);
1957 taken = 0;
1959 hdr = buf + NBT_HDR_SIZE;
1961 status = smb1cli_pull_raw_error(hdr);
1962 if (NT_STATUS_IS_ERR(status)) {
1964 * This is an ugly hack to support OS/2
1965 * which skips the byte_count in the DATA block
1966 * on some error responses.
1968 * See bug #9096
1970 min_size -= sizeof(uint16_t);
1973 if (buflen < min_size) {
1974 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1978 * This returns iovec elements in the following order:
1980 * - SMB header
1982 * - Parameter Block
1983 * - Data Block
1985 * - Parameter Block
1986 * - Data Block
1988 * - Parameter Block
1989 * - Data Block
1991 num_iov = 1;
1993 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1994 if (iov == NULL) {
1995 return NT_STATUS_NO_MEMORY;
1997 iov[0].iov_base = hdr;
1998 iov[0].iov_len = HDR_WCT;
1999 taken += HDR_WCT;
2001 cmd = CVAL(hdr, HDR_COM);
2002 wct_ofs = HDR_WCT;
2004 while (true) {
2005 size_t len = buflen - taken;
2006 struct iovec *cur;
2007 struct iovec *iov_tmp;
2008 uint8_t wct;
2009 uint32_t bcc_ofs;
2010 uint16_t bcc;
2011 size_t needed;
2014 * we need at least WCT
2016 needed = sizeof(uint8_t);
2017 if (len < needed) {
2018 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2019 __location__, (int)len, (int)needed));
2020 goto inval;
2024 * Now we check if the specified words are there
2026 wct = CVAL(hdr, wct_ofs);
2027 needed += wct * sizeof(uint16_t);
2028 if (len < needed) {
2029 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2030 __location__, (int)len, (int)needed));
2031 goto inval;
2034 if ((num_iov == 1) &&
2035 (len == needed) &&
2036 NT_STATUS_IS_ERR(status))
2039 * This is an ugly hack to support OS/2
2040 * which skips the byte_count in the DATA block
2041 * on some error responses.
2043 * See bug #9096
2045 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2046 num_iov + 2);
2047 if (iov_tmp == NULL) {
2048 TALLOC_FREE(iov);
2049 return NT_STATUS_NO_MEMORY;
2051 iov = iov_tmp;
2052 cur = &iov[num_iov];
2053 num_iov += 2;
2055 cur[0].iov_len = 0;
2056 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
2057 cur[1].iov_len = 0;
2058 cur[1].iov_base = cur[0].iov_base;
2060 taken += needed;
2061 break;
2065 * we need at least BCC
2067 needed += sizeof(uint16_t);
2068 if (len < needed) {
2069 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2070 __location__, (int)len, (int)needed));
2071 goto inval;
2075 * Now we check if the specified bytes are there
2077 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
2078 bcc = SVAL(hdr, bcc_ofs);
2079 needed += bcc * sizeof(uint8_t);
2080 if (len < needed) {
2081 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
2082 __location__, (int)len, (int)needed));
2083 goto inval;
2087 * we allocate 2 iovec structures for words and bytes
2089 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2090 num_iov + 2);
2091 if (iov_tmp == NULL) {
2092 TALLOC_FREE(iov);
2093 return NT_STATUS_NO_MEMORY;
2095 iov = iov_tmp;
2096 cur = &iov[num_iov];
2097 num_iov += 2;
2099 cur[0].iov_len = wct * sizeof(uint16_t);
2100 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
2101 cur[1].iov_len = bcc * sizeof(uint8_t);
2102 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
2104 taken += needed;
2106 if (!smb1cli_is_andx_req(cmd)) {
2108 * If the current command does not have AndX chanining
2109 * we are done.
2111 break;
2114 if (wct == 0 && bcc == 0) {
2116 * An empty response also ends the chain,
2117 * most likely with an error.
2119 break;
2122 if (wct < 2) {
2123 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
2124 __location__, (int)wct, (int)cmd));
2125 goto inval;
2127 cmd = CVAL(cur[0].iov_base, 0);
2128 if (cmd == 0xFF) {
2130 * If it is the end of the chain we are also done.
2132 break;
2134 wct_ofs = SVAL(cur[0].iov_base, 2);
2136 if (wct_ofs < taken) {
2137 goto inval;
2139 if (wct_ofs > buflen) {
2140 goto inval;
2144 * we consumed everything up to the start of the next
2145 * parameter block.
2147 taken = wct_ofs;
2150 remaining = buflen - taken;
2152 if (remaining > 0 && num_iov >= 3) {
2154 * The last DATA block gets the remaining
2155 * bytes, this is needed to support
2156 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
2158 iov[num_iov-1].iov_len += remaining;
2161 *piov = iov;
2162 *pnum_iov = num_iov;
2163 return NT_STATUS_OK;
2165 inval:
2166 TALLOC_FREE(iov);
2167 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2170 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2171 TALLOC_CTX *tmp_mem,
2172 uint8_t *inbuf)
2174 struct tevent_req *req;
2175 struct smbXcli_req_state *state;
2176 NTSTATUS status;
2177 size_t num_pending;
2178 size_t i;
2179 uint8_t cmd;
2180 uint16_t mid;
2181 bool oplock_break;
2182 uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
2183 size_t len = smb_len_tcp(inbuf);
2184 struct iovec *iov = NULL;
2185 int num_iov = 0;
2186 struct tevent_req **chain = NULL;
2187 size_t num_chained = 0;
2188 size_t num_responses = 0;
2190 if (conn->smb1.read_braw_req != NULL) {
2191 req = conn->smb1.read_braw_req;
2192 conn->smb1.read_braw_req = NULL;
2193 state = tevent_req_data(req, struct smbXcli_req_state);
2195 smbXcli_req_unset_pending(req);
2197 if (state->smb1.recv_iov == NULL) {
2199 * For requests with more than
2200 * one response, we have to readd the
2201 * recv_iov array.
2203 state->smb1.recv_iov = talloc_zero_array(state,
2204 struct iovec,
2206 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2207 return NT_STATUS_OK;
2211 state->smb1.recv_iov[0].iov_base = (void *)(inhdr);
2212 state->smb1.recv_iov[0].iov_len = len;
2213 ZERO_STRUCT(state->smb1.recv_iov[1]);
2214 ZERO_STRUCT(state->smb1.recv_iov[2]);
2216 state->smb1.recv_cmd = SMBreadBraw;
2217 state->smb1.recv_status = NT_STATUS_OK;
2218 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2220 tevent_req_done(req);
2221 return NT_STATUS_OK;
2224 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
2225 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
2226 DEBUG(10, ("Got non-SMB PDU\n"));
2227 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2231 * If we supported multiple encrytion contexts
2232 * here we'd look up based on tid.
2234 if (common_encryption_on(conn->smb1.trans_enc)
2235 && (CVAL(inbuf, 0) == 0)) {
2236 uint16_t enc_ctx_num;
2238 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
2239 if (!NT_STATUS_IS_OK(status)) {
2240 DEBUG(10, ("get_enc_ctx_num returned %s\n",
2241 nt_errstr(status)));
2242 return status;
2245 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
2246 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
2247 enc_ctx_num,
2248 conn->smb1.trans_enc->enc_ctx_num));
2249 return NT_STATUS_INVALID_HANDLE;
2252 status = common_decrypt_buffer(conn->smb1.trans_enc,
2253 (char *)inbuf);
2254 if (!NT_STATUS_IS_OK(status)) {
2255 DEBUG(10, ("common_decrypt_buffer returned %s\n",
2256 nt_errstr(status)));
2257 return status;
2259 inhdr = inbuf + NBT_HDR_SIZE;
2260 len = smb_len_nbt(inbuf);
2263 mid = SVAL(inhdr, HDR_MID);
2264 num_pending = talloc_array_length(conn->pending);
2266 for (i=0; i<num_pending; i++) {
2267 if (mid == smb1cli_req_mid(conn->pending[i])) {
2268 break;
2271 if (i == num_pending) {
2272 /* Dump unexpected reply */
2273 return NT_STATUS_RETRY;
2276 oplock_break = false;
2278 if (mid == 0xffff) {
2280 * Paranoia checks that this is really an oplock break request.
2282 oplock_break = (len == 51); /* hdr + 8 words */
2283 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
2284 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
2285 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
2286 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
2288 if (!oplock_break) {
2289 /* Dump unexpected reply */
2290 return NT_STATUS_RETRY;
2294 req = conn->pending[i];
2295 state = tevent_req_data(req, struct smbXcli_req_state);
2297 if (!oplock_break /* oplock breaks are not signed */
2298 && !smb_signing_check_pdu(conn->smb1.signing,
2299 inhdr, len, state->smb1.seqnum+1)) {
2300 DEBUG(10, ("cli_check_sign_mac failed\n"));
2301 return NT_STATUS_ACCESS_DENIED;
2304 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
2305 &iov, &num_iov);
2306 if (!NT_STATUS_IS_OK(status)) {
2307 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
2308 nt_errstr(status)));
2309 return status;
2312 cmd = CVAL(inhdr, HDR_COM);
2313 status = smb1cli_pull_raw_error(inhdr);
2315 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
2316 (state->session != NULL) && state->session->disconnect_expired)
2319 * this should be a short term hack
2320 * until the upper layers have implemented
2321 * re-authentication.
2323 return status;
2326 if (state->smb1.chained_requests == NULL) {
2327 if (num_iov != 3) {
2328 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2331 smbXcli_req_unset_pending(req);
2333 if (state->smb1.recv_iov == NULL) {
2335 * For requests with more than
2336 * one response, we have to readd the
2337 * recv_iov array.
2339 state->smb1.recv_iov = talloc_zero_array(state,
2340 struct iovec,
2342 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2343 return NT_STATUS_OK;
2347 state->smb1.recv_cmd = cmd;
2348 state->smb1.recv_status = status;
2349 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
2351 state->smb1.recv_iov[0] = iov[0];
2352 state->smb1.recv_iov[1] = iov[1];
2353 state->smb1.recv_iov[2] = iov[2];
2355 if (talloc_array_length(conn->pending) == 0) {
2356 tevent_req_done(req);
2357 return NT_STATUS_OK;
2360 tevent_req_defer_callback(req, state->ev);
2361 tevent_req_done(req);
2362 return NT_STATUS_RETRY;
2365 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
2366 num_chained = talloc_array_length(chain);
2367 num_responses = (num_iov - 1)/2;
2369 if (num_responses > num_chained) {
2370 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2373 for (i=0; i<num_chained; i++) {
2374 size_t iov_idx = 1 + (i*2);
2375 struct iovec *cur = &iov[iov_idx];
2376 uint8_t *inbuf_ref;
2378 req = chain[i];
2379 state = tevent_req_data(req, struct smbXcli_req_state);
2381 smbXcli_req_unset_pending(req);
2384 * as we finish multiple requests here
2385 * we need to defer the callbacks as
2386 * they could destroy our current stack state.
2388 tevent_req_defer_callback(req, state->ev);
2390 if (i >= num_responses) {
2391 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
2392 continue;
2395 if (state->smb1.recv_iov == NULL) {
2397 * For requests with more than
2398 * one response, we have to readd the
2399 * recv_iov array.
2401 state->smb1.recv_iov = talloc_zero_array(state,
2402 struct iovec,
2404 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
2405 continue;
2409 state->smb1.recv_cmd = cmd;
2411 if (i == (num_responses - 1)) {
2413 * The last request in the chain gets the status
2415 state->smb1.recv_status = status;
2416 } else {
2417 cmd = CVAL(cur[0].iov_base, 0);
2418 state->smb1.recv_status = NT_STATUS_OK;
2421 state->inbuf = inbuf;
2424 * Note: here we use talloc_reference() in a way
2425 * that does not expose it to the caller.
2427 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
2428 if (tevent_req_nomem(inbuf_ref, req)) {
2429 continue;
2432 /* copy the related buffers */
2433 state->smb1.recv_iov[0] = iov[0];
2434 state->smb1.recv_iov[1] = cur[0];
2435 state->smb1.recv_iov[2] = cur[1];
2437 tevent_req_done(req);
2440 return NT_STATUS_RETRY;
2443 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
2444 TALLOC_CTX *mem_ctx,
2445 struct iovec **piov,
2446 uint8_t **phdr,
2447 uint8_t *pwct,
2448 uint16_t **pvwv,
2449 uint32_t *pvwv_offset,
2450 uint32_t *pnum_bytes,
2451 uint8_t **pbytes,
2452 uint32_t *pbytes_offset,
2453 uint8_t **pinbuf,
2454 const struct smb1cli_req_expected_response *expected,
2455 size_t num_expected)
2457 struct smbXcli_req_state *state =
2458 tevent_req_data(req,
2459 struct smbXcli_req_state);
2460 NTSTATUS status = NT_STATUS_OK;
2461 struct iovec *recv_iov = NULL;
2462 uint8_t *hdr = NULL;
2463 uint8_t wct = 0;
2464 uint32_t vwv_offset = 0;
2465 uint16_t *vwv = NULL;
2466 uint32_t num_bytes = 0;
2467 uint32_t bytes_offset = 0;
2468 uint8_t *bytes = NULL;
2469 size_t i;
2470 bool found_status = false;
2471 bool found_size = false;
2473 if (piov != NULL) {
2474 *piov = NULL;
2476 if (phdr != NULL) {
2477 *phdr = 0;
2479 if (pwct != NULL) {
2480 *pwct = 0;
2482 if (pvwv != NULL) {
2483 *pvwv = NULL;
2485 if (pvwv_offset != NULL) {
2486 *pvwv_offset = 0;
2488 if (pnum_bytes != NULL) {
2489 *pnum_bytes = 0;
2491 if (pbytes != NULL) {
2492 *pbytes = NULL;
2494 if (pbytes_offset != NULL) {
2495 *pbytes_offset = 0;
2497 if (pinbuf != NULL) {
2498 *pinbuf = NULL;
2501 if (state->inbuf != NULL) {
2502 recv_iov = state->smb1.recv_iov;
2503 state->smb1.recv_iov = NULL;
2504 if (state->smb1.recv_cmd != SMBreadBraw) {
2505 hdr = (uint8_t *)recv_iov[0].iov_base;
2506 wct = recv_iov[1].iov_len/2;
2507 vwv = (uint16_t *)recv_iov[1].iov_base;
2508 vwv_offset = PTR_DIFF(vwv, hdr);
2509 num_bytes = recv_iov[2].iov_len;
2510 bytes = (uint8_t *)recv_iov[2].iov_base;
2511 bytes_offset = PTR_DIFF(bytes, hdr);
2515 if (tevent_req_is_nterror(req, &status)) {
2516 for (i=0; i < num_expected; i++) {
2517 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2518 found_status = true;
2519 break;
2523 if (found_status) {
2524 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2527 return status;
2530 if (num_expected == 0) {
2531 found_status = true;
2532 found_size = true;
2535 status = state->smb1.recv_status;
2537 for (i=0; i < num_expected; i++) {
2538 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2539 continue;
2542 found_status = true;
2543 if (expected[i].wct == 0) {
2544 found_size = true;
2545 break;
2548 if (expected[i].wct == wct) {
2549 found_size = true;
2550 break;
2554 if (!found_status) {
2555 return status;
2558 if (!found_size) {
2559 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2562 if (piov != NULL) {
2563 *piov = talloc_move(mem_ctx, &recv_iov);
2566 if (phdr != NULL) {
2567 *phdr = hdr;
2569 if (pwct != NULL) {
2570 *pwct = wct;
2572 if (pvwv != NULL) {
2573 *pvwv = vwv;
2575 if (pvwv_offset != NULL) {
2576 *pvwv_offset = vwv_offset;
2578 if (pnum_bytes != NULL) {
2579 *pnum_bytes = num_bytes;
2581 if (pbytes != NULL) {
2582 *pbytes = bytes;
2584 if (pbytes_offset != NULL) {
2585 *pbytes_offset = bytes_offset;
2587 if (pinbuf != NULL) {
2588 *pinbuf = state->inbuf;
2591 return status;
2594 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2596 size_t wct_ofs;
2597 int i;
2599 wct_ofs = HDR_WCT;
2601 for (i=0; i<num_reqs; i++) {
2602 struct smbXcli_req_state *state;
2603 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2604 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2605 state->smb1.iov_count-2);
2606 wct_ofs = (wct_ofs + 3) & ~3;
2608 return wct_ofs;
2611 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2613 struct smbXcli_req_state *first_state =
2614 tevent_req_data(reqs[0],
2615 struct smbXcli_req_state);
2616 struct smbXcli_req_state *state;
2617 size_t wct_offset;
2618 size_t chain_padding = 0;
2619 int i, iovlen;
2620 struct iovec *iov = NULL;
2621 struct iovec *this_iov;
2622 NTSTATUS status;
2623 ssize_t nbt_len;
2625 if (num_reqs == 1) {
2626 return smb1cli_req_writev_submit(reqs[0], first_state,
2627 first_state->smb1.iov,
2628 first_state->smb1.iov_count);
2631 iovlen = 0;
2632 for (i=0; i<num_reqs; i++) {
2633 if (!tevent_req_is_in_progress(reqs[i])) {
2634 return NT_STATUS_INTERNAL_ERROR;
2637 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2639 if (state->smb1.iov_count < 4) {
2640 return NT_STATUS_INVALID_PARAMETER_MIX;
2643 if (i == 0) {
2645 * The NBT and SMB header
2647 iovlen += 2;
2648 } else {
2650 * Chain padding
2652 iovlen += 1;
2656 * words and bytes
2658 iovlen += state->smb1.iov_count - 2;
2661 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2662 if (iov == NULL) {
2663 return NT_STATUS_NO_MEMORY;
2666 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2667 first_state, reqs, sizeof(*reqs) * num_reqs);
2668 if (first_state->smb1.chained_requests == NULL) {
2669 TALLOC_FREE(iov);
2670 return NT_STATUS_NO_MEMORY;
2673 wct_offset = HDR_WCT;
2674 this_iov = iov;
2676 for (i=0; i<num_reqs; i++) {
2677 size_t next_padding = 0;
2678 uint16_t *vwv;
2680 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2682 if (i < num_reqs-1) {
2683 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2684 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2685 TALLOC_FREE(iov);
2686 TALLOC_FREE(first_state->smb1.chained_requests);
2687 return NT_STATUS_INVALID_PARAMETER_MIX;
2691 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2692 state->smb1.iov_count-2) + 1;
2693 if ((wct_offset % 4) != 0) {
2694 next_padding = 4 - (wct_offset % 4);
2696 wct_offset += next_padding;
2697 vwv = state->smb1.vwv;
2699 if (i < num_reqs-1) {
2700 struct smbXcli_req_state *next_state =
2701 tevent_req_data(reqs[i+1],
2702 struct smbXcli_req_state);
2703 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2704 SCVAL(vwv+0, 1, 0);
2705 SSVAL(vwv+1, 0, wct_offset);
2706 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2707 /* properly end the chain */
2708 SCVAL(vwv+0, 0, 0xff);
2709 SCVAL(vwv+0, 1, 0xff);
2710 SSVAL(vwv+1, 0, 0);
2713 if (i == 0) {
2715 * The NBT and SMB header
2717 this_iov[0] = state->smb1.iov[0];
2718 this_iov[1] = state->smb1.iov[1];
2719 this_iov += 2;
2720 } else {
2722 * This one is a bit subtle. We have to add
2723 * chain_padding bytes between the requests, and we
2724 * have to also include the wct field of the
2725 * subsequent requests. We use the subsequent header
2726 * for the padding, it contains the wct field in its
2727 * last byte.
2729 this_iov[0].iov_len = chain_padding+1;
2730 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2731 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2732 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2733 this_iov += 1;
2737 * copy the words and bytes
2739 memcpy(this_iov, state->smb1.iov+2,
2740 sizeof(struct iovec) * (state->smb1.iov_count-2));
2741 this_iov += state->smb1.iov_count - 2;
2742 chain_padding = next_padding;
2745 nbt_len = iov_buflen(&iov[1], iovlen-1);
2746 if ((nbt_len == -1) || (nbt_len > first_state->conn->smb1.max_xmit)) {
2747 TALLOC_FREE(iov);
2748 TALLOC_FREE(first_state->smb1.chained_requests);
2749 return NT_STATUS_INVALID_PARAMETER_MIX;
2752 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2753 if (!NT_STATUS_IS_OK(status)) {
2754 TALLOC_FREE(iov);
2755 TALLOC_FREE(first_state->smb1.chained_requests);
2756 return status;
2759 return NT_STATUS_OK;
2762 struct tevent_queue *smbXcli_conn_send_queue(struct smbXcli_conn *conn)
2764 return conn->outgoing;
2767 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2769 return ((tevent_queue_length(conn->outgoing) != 0)
2770 || (talloc_array_length(conn->pending) != 0));
2773 bool smbXcli_conn_dfs_supported(struct smbXcli_conn *conn)
2775 if (conn->protocol >= PROTOCOL_SMB2_02) {
2776 return (smb2cli_conn_server_capabilities(conn) & SMB2_CAP_DFS);
2779 return (smb1cli_conn_capabilities(conn) & CAP_DFS);
2782 bool smb2cli_conn_req_possible(struct smbXcli_conn *conn, uint32_t *max_dyn_len)
2784 uint16_t credits = 1;
2786 if (conn->smb2.cur_credits == 0) {
2787 if (max_dyn_len != NULL) {
2788 *max_dyn_len = 0;
2790 return false;
2793 if (conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2794 credits = conn->smb2.cur_credits;
2797 if (max_dyn_len != NULL) {
2798 *max_dyn_len = credits * 65536;
2801 return true;
2804 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2806 return conn->smb2.server.capabilities;
2809 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2811 return conn->smb2.server.security_mode;
2814 uint16_t smb2cli_conn_server_signing_algo(struct smbXcli_conn *conn)
2816 return conn->smb2.server.sign_algo;
2819 uint16_t smb2cli_conn_server_encryption_algo(struct smbXcli_conn *conn)
2821 return conn->smb2.server.cipher;
2824 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2826 return conn->smb2.server.max_trans_size;
2829 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2831 return conn->smb2.server.max_read_size;
2834 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2836 return conn->smb2.server.max_write_size;
2839 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2840 uint16_t max_credits)
2842 conn->smb2.max_credits = max_credits;
2845 uint16_t smb2cli_conn_get_cur_credits(struct smbXcli_conn *conn)
2847 return conn->smb2.cur_credits;
2850 uint8_t smb2cli_conn_get_io_priority(struct smbXcli_conn *conn)
2852 if (conn->protocol < PROTOCOL_SMB3_11) {
2853 return 0;
2856 return conn->smb2.io_priority;
2859 void smb2cli_conn_set_io_priority(struct smbXcli_conn *conn,
2860 uint8_t io_priority)
2862 conn->smb2.io_priority = io_priority;
2865 uint32_t smb2cli_conn_cc_chunk_len(struct smbXcli_conn *conn)
2867 return conn->smb2.cc_chunk_len;
2870 void smb2cli_conn_set_cc_chunk_len(struct smbXcli_conn *conn,
2871 uint32_t chunk_len)
2873 conn->smb2.cc_chunk_len = chunk_len;
2876 uint32_t smb2cli_conn_cc_max_chunks(struct smbXcli_conn *conn)
2878 return conn->smb2.cc_max_chunks;
2881 void smb2cli_conn_set_cc_max_chunks(struct smbXcli_conn *conn,
2882 uint32_t max_chunks)
2884 conn->smb2.cc_max_chunks = max_chunks;
2887 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2889 static bool smb2cli_req_cancel(struct tevent_req *req)
2891 struct smbXcli_req_state *state =
2892 tevent_req_data(req,
2893 struct smbXcli_req_state);
2894 struct smbXcli_tcon *tcon = state->tcon;
2895 struct smbXcli_session *session = state->session;
2896 uint8_t *fixed = state->smb2.pad;
2897 uint16_t fixed_len = 4;
2898 struct tevent_req *subreq;
2899 struct smbXcli_req_state *substate;
2900 NTSTATUS status;
2902 if (state->smb2.cancel_mid == UINT64_MAX) {
2904 * We already send a cancel,
2905 * make sure we don't do it
2906 * twice, otherwise we may
2907 * expose the same NONCE for
2908 * AES-128-GMAC signing
2910 return true;
2913 SSVAL(fixed, 0, 0x04);
2914 SSVAL(fixed, 2, 0);
2916 subreq = smb2cli_req_create(state, state->ev,
2917 state->conn,
2918 SMB2_OP_CANCEL,
2919 0, 0, /* flags */
2920 0, /* timeout */
2921 tcon, session,
2922 fixed, fixed_len,
2923 NULL, 0, 0);
2924 if (subreq == NULL) {
2925 return false;
2927 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2929 substate->smb2.cancel_mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2931 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, state->smb2.cancel_flags);
2932 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, state->smb2.cancel_mid);
2933 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, state->smb2.cancel_aid);
2936 * remember that we don't send a cancel again
2938 state->smb2.cancel_mid = UINT64_MAX;
2940 status = smb2cli_req_compound_submit(&subreq, 1);
2941 if (!NT_STATUS_IS_OK(status)) {
2942 TALLOC_FREE(subreq);
2943 return false;
2946 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2948 return true;
2951 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2953 /* we do not care about the result */
2954 TALLOC_FREE(subreq);
2957 struct timeval smbXcli_req_endtime(struct tevent_req *req)
2959 struct smbXcli_req_state *state = tevent_req_data(
2960 req, struct smbXcli_req_state);
2962 return state->endtime;
2965 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2966 struct tevent_context *ev,
2967 struct smbXcli_conn *conn,
2968 uint16_t cmd,
2969 uint32_t additional_flags,
2970 uint32_t clear_flags,
2971 uint32_t timeout_msec,
2972 struct smbXcli_tcon *tcon,
2973 struct smbXcli_session *session,
2974 const uint8_t *fixed,
2975 uint16_t fixed_len,
2976 const uint8_t *dyn,
2977 uint32_t dyn_len,
2978 uint32_t max_dyn_len)
2980 struct tevent_req *req;
2981 struct smbXcli_req_state *state;
2982 uint32_t flags = 0;
2983 uint32_t tid = 0;
2984 uint64_t uid = 0;
2985 bool use_channel_sequence = conn->smb2.force_channel_sequence;
2986 uint16_t channel_sequence = 0;
2987 bool use_replay_flag = false;
2989 req = tevent_req_create(mem_ctx, &state,
2990 struct smbXcli_req_state);
2991 if (req == NULL) {
2992 return NULL;
2995 state->ev = ev;
2996 state->conn = conn;
2997 state->session = session;
2998 state->tcon = tcon;
3000 if (conn->smb2.server.capabilities & SMB2_CAP_PERSISTENT_HANDLES) {
3001 use_channel_sequence = true;
3002 } else if (conn->smb2.server.capabilities & SMB2_CAP_MULTI_CHANNEL) {
3003 use_channel_sequence = true;
3006 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_00) {
3007 use_replay_flag = true;
3010 if (smbXcli_conn_protocol(conn) >= PROTOCOL_SMB3_11) {
3011 flags |= SMB2_PRIORITY_VALUE_TO_MASK(conn->smb2.io_priority);
3014 if (session) {
3015 uid = session->smb2->session_id;
3017 if (use_channel_sequence) {
3018 channel_sequence = session->smb2->channel_sequence;
3021 if (use_replay_flag && session->smb2->replay_active) {
3022 additional_flags |= SMB2_HDR_FLAG_REPLAY_OPERATION;
3025 state->smb2.should_sign = session->smb2->should_sign;
3026 state->smb2.should_encrypt = session->smb2->should_encrypt;
3027 state->smb2.require_signed_response =
3028 session->smb2->require_signed_response;
3030 if (cmd == SMB2_OP_SESSSETUP &&
3031 !smb2_signing_key_valid(session->smb2_channel.signing_key) &&
3032 smb2_signing_key_valid(session->smb2->signing_key))
3035 * a session bind needs to be signed
3037 state->smb2.should_sign = true;
3040 if (cmd == SMB2_OP_SESSSETUP &&
3041 !smb2_signing_key_valid(session->smb2_channel.signing_key)) {
3042 state->smb2.should_encrypt = false;
3045 if (additional_flags & SMB2_HDR_FLAG_SIGNED) {
3046 if (!smb2_signing_key_valid(session->smb2_channel.signing_key)) {
3047 tevent_req_nterror(req, NT_STATUS_NO_USER_SESSION_KEY);
3048 return req;
3051 additional_flags &= ~SMB2_HDR_FLAG_SIGNED;
3052 state->smb2.should_sign = true;
3056 if (tcon) {
3057 tid = tcon->smb2.tcon_id;
3059 if (tcon->smb2.should_sign) {
3060 state->smb2.should_sign = true;
3062 if (tcon->smb2.should_encrypt) {
3063 state->smb2.should_encrypt = true;
3067 if (state->smb2.should_encrypt) {
3068 state->smb2.should_sign = false;
3071 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
3072 if (state->smb2.recv_iov == NULL) {
3073 TALLOC_FREE(req);
3074 return NULL;
3077 flags |= additional_flags;
3078 flags &= ~clear_flags;
3080 state->smb2.fixed = fixed;
3081 state->smb2.fixed_len = fixed_len;
3082 state->smb2.dyn = dyn;
3083 state->smb2.dyn_len = dyn_len;
3084 state->smb2.max_dyn_len = max_dyn_len;
3086 if (state->smb2.should_encrypt) {
3087 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
3088 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
3091 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
3092 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
3093 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
3094 SSVAL(state->smb2.hdr, SMB2_HDR_CHANNEL_SEQUENCE, channel_sequence);
3095 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
3096 SIVAL(state->smb2.hdr, SMB2_HDR_PID, 0); /* reserved */
3097 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
3098 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
3100 switch (cmd) {
3101 case SMB2_OP_CANCEL:
3102 state->one_way = true;
3103 break;
3104 case SMB2_OP_BREAK:
3106 * If this is a dummy request, it will have
3107 * UINT64_MAX as message id.
3108 * If we send on break acknowledgement,
3109 * this gets overwritten later.
3111 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
3112 break;
3115 if (timeout_msec > 0) {
3116 state->endtime = timeval_current_ofs_msec(timeout_msec);
3117 if (!tevent_req_set_endtime(req, ev, state->endtime)) {
3118 return req;
3122 return req;
3125 void smb2cli_req_set_notify_async(struct tevent_req *req)
3127 struct smbXcli_req_state *state =
3128 tevent_req_data(req,
3129 struct smbXcli_req_state);
3131 state->smb2.notify_async = true;
3134 static void smb2cli_req_writev_done(struct tevent_req *subreq);
3135 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3136 TALLOC_CTX *tmp_mem,
3137 uint8_t *inbuf);
3139 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
3140 int num_reqs)
3142 struct smbXcli_req_state *state;
3143 struct tevent_req *subreq;
3144 struct iovec *iov;
3145 int i, num_iov, nbt_len;
3146 int tf_iov = -1;
3147 struct smb2_signing_key *encryption_key = NULL;
3148 uint64_t encryption_session_id = 0;
3149 uint64_t nonce_high = UINT64_MAX;
3150 uint64_t nonce_low = UINT64_MAX;
3153 * 1 for the nbt length, optional TRANSFORM
3154 * per request: HDR, fixed, dyn, padding
3155 * -1 because the last one does not need padding
3158 iov = talloc_array(reqs[0], struct iovec, 1 + 1 + 4*num_reqs - 1);
3159 if (iov == NULL) {
3160 return NT_STATUS_NO_MEMORY;
3163 num_iov = 1;
3164 nbt_len = 0;
3167 * the session of the first request that requires encryption
3168 * specifies the encryption key.
3170 for (i=0; i<num_reqs; i++) {
3171 if (!tevent_req_is_in_progress(reqs[i])) {
3172 return NT_STATUS_INTERNAL_ERROR;
3175 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
3177 if (!smbXcli_conn_is_connected(state->conn)) {
3178 return NT_STATUS_CONNECTION_DISCONNECTED;
3181 if ((state->conn->protocol != PROTOCOL_NONE) &&
3182 (state->conn->protocol < PROTOCOL_SMB2_02)) {
3183 return NT_STATUS_REVISION_MISMATCH;
3186 if (state->session == NULL) {
3187 continue;
3190 if (!state->smb2.should_encrypt) {
3191 continue;
3194 encryption_key = state->session->smb2->encryption_key;
3195 if (!smb2_signing_key_valid(encryption_key)) {
3196 return NT_STATUS_INVALID_PARAMETER_MIX;
3199 encryption_session_id = state->session->smb2->session_id;
3201 state->session->smb2->nonce_low += 1;
3202 if (state->session->smb2->nonce_low == 0) {
3203 state->session->smb2->nonce_high += 1;
3204 state->session->smb2->nonce_low += 1;
3208 * CCM and GCM algorithms must never have their
3209 * nonce wrap, or the security of the whole
3210 * communication and the keys is destroyed.
3211 * We must drop the connection once we have
3212 * transfered too much data.
3214 * NOTE: We assume nonces greater than 8 bytes.
3216 if (state->session->smb2->nonce_high >=
3217 state->session->smb2->nonce_high_max)
3219 return NT_STATUS_ENCRYPTION_FAILED;
3222 nonce_high = state->session->smb2->nonce_high_random;
3223 nonce_high += state->session->smb2->nonce_high;
3224 nonce_low = state->session->smb2->nonce_low;
3226 tf_iov = num_iov;
3227 iov[num_iov].iov_base = state->smb2.transform;
3228 iov[num_iov].iov_len = sizeof(state->smb2.transform);
3229 num_iov += 1;
3231 SBVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
3232 SBVAL(state->smb2.transform, SMB2_TF_NONCE,
3233 nonce_low);
3234 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
3235 nonce_high);
3236 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID,
3237 encryption_session_id);
3239 nbt_len += SMB2_TF_HDR_SIZE;
3240 break;
3243 for (i=0; i<num_reqs; i++) {
3244 int hdr_iov;
3245 size_t reqlen;
3246 bool ret;
3247 uint16_t opcode;
3248 uint64_t avail;
3249 uint16_t charge;
3250 uint16_t credits;
3251 uint64_t mid;
3252 struct smb2_signing_key *signing_key = NULL;
3254 if (!tevent_req_is_in_progress(reqs[i])) {
3255 return NT_STATUS_INTERNAL_ERROR;
3258 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
3260 if (!smbXcli_conn_is_connected(state->conn)) {
3261 return NT_STATUS_CONNECTION_DISCONNECTED;
3264 if ((state->conn->protocol != PROTOCOL_NONE) &&
3265 (state->conn->protocol < PROTOCOL_SMB2_02)) {
3266 return NT_STATUS_REVISION_MISMATCH;
3269 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3270 if (opcode == SMB2_OP_CANCEL) {
3271 goto skip_credits;
3274 avail = UINT64_MAX - state->conn->smb2.mid;
3275 if (avail < 1) {
3276 return NT_STATUS_CONNECTION_ABORTED;
3279 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
3280 uint32_t max_dyn_len = 1;
3282 max_dyn_len = MAX(max_dyn_len, state->smb2.dyn_len);
3283 max_dyn_len = MAX(max_dyn_len, state->smb2.max_dyn_len);
3285 charge = (max_dyn_len - 1)/ 65536 + 1;
3286 } else {
3287 charge = 1;
3290 charge = MAX(state->smb2.credit_charge, charge);
3292 avail = MIN(avail, state->conn->smb2.cur_credits);
3293 if (avail < charge) {
3294 DBG_ERR("Insufficient credits. "
3295 "%"PRIu64" available, %"PRIu16" needed\n",
3296 avail, charge);
3297 return NT_STATUS_INTERNAL_ERROR;
3300 credits = 0;
3301 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
3302 credits = state->conn->smb2.max_credits -
3303 state->conn->smb2.cur_credits;
3305 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
3306 credits += 1;
3309 mid = state->conn->smb2.mid;
3310 state->conn->smb2.mid += charge;
3311 state->conn->smb2.cur_credits -= charge;
3313 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
3314 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
3316 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
3317 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
3319 state->smb2.cancel_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3320 state->smb2.cancel_flags &= ~SMB2_HDR_FLAG_CHAINED;
3321 state->smb2.cancel_mid = mid;
3322 state->smb2.cancel_aid = 0;
3324 skip_credits:
3325 if (state->session && encryption_key == NULL) {
3327 * We prefer the channel signing key if it is
3328 * already there.
3330 if (state->smb2.should_sign) {
3331 signing_key = state->session->smb2_channel.signing_key;
3335 * If it is a channel binding, we already have the main
3336 * signing key and try that one.
3338 if (signing_key != NULL &&
3339 !smb2_signing_key_valid(signing_key)) {
3340 signing_key = state->session->smb2->signing_key;
3344 * If we do not have any session key yet, we skip the
3345 * signing of SMB2_OP_SESSSETUP requests.
3347 if (signing_key != NULL &&
3348 !smb2_signing_key_valid(signing_key)) {
3349 signing_key = NULL;
3353 hdr_iov = num_iov;
3354 iov[num_iov].iov_base = state->smb2.hdr;
3355 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
3356 num_iov += 1;
3358 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
3359 iov[num_iov].iov_len = state->smb2.fixed_len;
3360 num_iov += 1;
3362 if (state->smb2.dyn != NULL) {
3363 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
3364 iov[num_iov].iov_len = state->smb2.dyn_len;
3365 num_iov += 1;
3368 reqlen = sizeof(state->smb2.hdr);
3369 reqlen += state->smb2.fixed_len;
3370 reqlen += state->smb2.dyn_len;
3372 if (i < num_reqs-1) {
3373 if ((reqlen % 8) > 0) {
3374 uint8_t pad = 8 - (reqlen % 8);
3375 iov[num_iov].iov_base = state->smb2.pad;
3376 iov[num_iov].iov_len = pad;
3377 num_iov += 1;
3378 reqlen += pad;
3380 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
3383 state->smb2.encryption_session_id = encryption_session_id;
3385 if (signing_key != NULL) {
3386 NTSTATUS status;
3388 status = smb2_signing_sign_pdu(signing_key,
3389 &iov[hdr_iov], num_iov - hdr_iov);
3390 if (!NT_STATUS_IS_OK(status)) {
3391 return status;
3395 nbt_len += reqlen;
3397 ret = smbXcli_req_set_pending(reqs[i]);
3398 if (!ret) {
3399 return NT_STATUS_NO_MEMORY;
3403 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
3404 _smb_setlen_tcp(state->length_hdr, nbt_len);
3405 iov[0].iov_base = state->length_hdr;
3406 iov[0].iov_len = sizeof(state->length_hdr);
3408 if (encryption_key != NULL) {
3409 NTSTATUS status;
3410 size_t buflen = nbt_len - SMB2_TF_HDR_SIZE;
3411 uint8_t *buf;
3412 int vi;
3414 buf = talloc_array(iov, uint8_t, buflen);
3415 if (buf == NULL) {
3416 return NT_STATUS_NO_MEMORY;
3420 * We copy the buffers before encrypting them,
3421 * this is at least currently needed for the
3422 * to keep state->smb2.hdr.
3424 * Also the callers may expect there buffers
3425 * to be const.
3427 for (vi = tf_iov + 1; vi < num_iov; vi++) {
3428 struct iovec *v = &iov[vi];
3429 const uint8_t *o = (const uint8_t *)v->iov_base;
3431 memcpy(buf, o, v->iov_len);
3432 v->iov_base = (void *)buf;
3433 buf += v->iov_len;
3436 status = smb2_signing_encrypt_pdu(encryption_key,
3437 &iov[tf_iov], num_iov - tf_iov);
3438 if (!NT_STATUS_IS_OK(status)) {
3439 return status;
3443 if (state->conn->dispatch_incoming == NULL) {
3444 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3447 subreq = writev_send(state, state->ev, state->conn->outgoing,
3448 state->conn->sock_fd, false, iov, num_iov);
3449 if (subreq == NULL) {
3450 return NT_STATUS_NO_MEMORY;
3452 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
3453 state->write_req = subreq;
3455 return NT_STATUS_OK;
3458 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
3460 struct smbXcli_req_state *state =
3461 tevent_req_data(req,
3462 struct smbXcli_req_state);
3464 state->smb2.credit_charge = charge;
3467 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
3468 struct tevent_context *ev,
3469 struct smbXcli_conn *conn,
3470 uint16_t cmd,
3471 uint32_t additional_flags,
3472 uint32_t clear_flags,
3473 uint32_t timeout_msec,
3474 struct smbXcli_tcon *tcon,
3475 struct smbXcli_session *session,
3476 const uint8_t *fixed,
3477 uint16_t fixed_len,
3478 const uint8_t *dyn,
3479 uint32_t dyn_len,
3480 uint32_t max_dyn_len)
3482 struct tevent_req *req;
3483 NTSTATUS status;
3485 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
3486 additional_flags, clear_flags,
3487 timeout_msec,
3488 tcon, session,
3489 fixed, fixed_len,
3490 dyn, dyn_len,
3491 max_dyn_len);
3492 if (req == NULL) {
3493 return NULL;
3495 if (!tevent_req_is_in_progress(req)) {
3496 return tevent_req_post(req, ev);
3498 status = smb2cli_req_compound_submit(&req, 1);
3499 if (tevent_req_nterror(req, status)) {
3500 return tevent_req_post(req, ev);
3502 return req;
3505 static void smb2cli_req_writev_done(struct tevent_req *subreq)
3507 struct tevent_req *req =
3508 tevent_req_callback_data(subreq,
3509 struct tevent_req);
3510 struct smbXcli_req_state *state =
3511 tevent_req_data(req,
3512 struct smbXcli_req_state);
3513 ssize_t nwritten;
3514 int err;
3516 state->write_req = NULL;
3518 nwritten = writev_recv(subreq, &err);
3519 TALLOC_FREE(subreq);
3520 if (nwritten == -1) {
3521 /* here, we need to notify all pending requests */
3522 NTSTATUS status = map_nt_error_from_unix_common(err);
3523 smbXcli_conn_disconnect(state->conn, status);
3524 return;
3528 static struct smbXcli_session* smbXcli_session_by_uid(struct smbXcli_conn *conn,
3529 uint64_t uid)
3531 struct smbXcli_session *s = conn->sessions;
3533 for (; s; s = s->next) {
3534 if (s->smb2->session_id != uid) {
3535 continue;
3537 break;
3540 return s;
3543 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
3544 uint8_t *buf,
3545 size_t buflen,
3546 TALLOC_CTX *mem_ctx,
3547 struct iovec **piov,
3548 size_t *pnum_iov)
3550 struct iovec *iov;
3551 int num_iov = 0;
3552 size_t taken = 0;
3553 uint8_t *first_hdr = buf;
3554 size_t verified_buflen = 0;
3555 uint8_t *tf = NULL;
3556 size_t tf_len = 0;
3558 iov = talloc_array(mem_ctx, struct iovec, num_iov);
3559 if (iov == NULL) {
3560 return NT_STATUS_NO_MEMORY;
3563 while (taken < buflen) {
3564 size_t len = buflen - taken;
3565 uint8_t *hdr = first_hdr + taken;
3566 struct iovec *cur;
3567 size_t full_size;
3568 size_t next_command_ofs;
3569 uint16_t body_size;
3570 struct iovec *iov_tmp;
3572 if (verified_buflen > taken) {
3573 len = verified_buflen - taken;
3574 } else {
3575 tf = NULL;
3576 tf_len = 0;
3579 if (len < 4) {
3580 DEBUG(10, ("%d bytes left, expected at least %d\n",
3581 (int)len, 4));
3582 goto inval;
3584 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
3585 struct smbXcli_session *s;
3586 uint64_t uid;
3587 struct iovec tf_iov[2];
3588 size_t enc_len;
3589 NTSTATUS status;
3591 if (len < SMB2_TF_HDR_SIZE) {
3592 DEBUG(10, ("%d bytes left, expected at least %d\n",
3593 (int)len, SMB2_TF_HDR_SIZE));
3594 goto inval;
3596 tf = hdr;
3597 tf_len = SMB2_TF_HDR_SIZE;
3598 taken += tf_len;
3600 hdr = first_hdr + taken;
3601 enc_len = IVAL(tf, SMB2_TF_MSG_SIZE);
3602 uid = BVAL(tf, SMB2_TF_SESSION_ID);
3604 if (len < SMB2_TF_HDR_SIZE + enc_len) {
3605 DEBUG(10, ("%d bytes left, expected at least %d\n",
3606 (int)len,
3607 (int)(SMB2_TF_HDR_SIZE + enc_len)));
3608 goto inval;
3611 s = smbXcli_session_by_uid(conn, uid);
3612 if (s == NULL) {
3613 DEBUG(10, ("unknown session_id %llu\n",
3614 (unsigned long long)uid));
3615 goto inval;
3618 tf_iov[0].iov_base = (void *)tf;
3619 tf_iov[0].iov_len = tf_len;
3620 tf_iov[1].iov_base = (void *)hdr;
3621 tf_iov[1].iov_len = enc_len;
3623 status = smb2_signing_decrypt_pdu(s->smb2->decryption_key,
3624 tf_iov, 2);
3625 if (!NT_STATUS_IS_OK(status)) {
3626 TALLOC_FREE(iov);
3627 return status;
3630 verified_buflen = taken + enc_len;
3631 len = enc_len;
3635 * We need the header plus the body length field
3638 if (len < SMB2_HDR_BODY + 2) {
3639 DEBUG(10, ("%d bytes left, expected at least %d\n",
3640 (int)len, SMB2_HDR_BODY));
3641 goto inval;
3643 if (IVAL(hdr, 0) != SMB2_MAGIC) {
3644 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
3645 IVAL(hdr, 0)));
3646 goto inval;
3648 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
3649 DEBUG(10, ("Got HDR len %d, expected %d\n",
3650 SVAL(hdr, 4), SMB2_HDR_BODY));
3651 goto inval;
3654 full_size = len;
3655 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
3656 body_size = SVAL(hdr, SMB2_HDR_BODY);
3658 if (next_command_ofs != 0) {
3659 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
3660 goto inval;
3662 if (next_command_ofs > full_size) {
3663 goto inval;
3665 full_size = next_command_ofs;
3667 if (body_size < 2) {
3668 goto inval;
3670 body_size &= 0xfffe;
3672 if (body_size > (full_size - SMB2_HDR_BODY)) {
3673 goto inval;
3676 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
3677 num_iov + 4);
3678 if (iov_tmp == NULL) {
3679 TALLOC_FREE(iov);
3680 return NT_STATUS_NO_MEMORY;
3682 iov = iov_tmp;
3683 cur = &iov[num_iov];
3684 num_iov += 4;
3686 cur[0].iov_base = tf;
3687 cur[0].iov_len = tf_len;
3688 cur[1].iov_base = hdr;
3689 cur[1].iov_len = SMB2_HDR_BODY;
3690 cur[2].iov_base = hdr + SMB2_HDR_BODY;
3691 cur[2].iov_len = body_size;
3692 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
3693 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
3695 taken += full_size;
3698 *piov = iov;
3699 *pnum_iov = num_iov;
3700 return NT_STATUS_OK;
3702 inval:
3703 TALLOC_FREE(iov);
3704 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3707 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
3708 uint64_t mid)
3710 size_t num_pending = talloc_array_length(conn->pending);
3711 size_t i;
3713 for (i=0; i<num_pending; i++) {
3714 struct tevent_req *req = conn->pending[i];
3715 struct smbXcli_req_state *state =
3716 tevent_req_data(req,
3717 struct smbXcli_req_state);
3719 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
3720 return req;
3723 return NULL;
3726 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
3727 TALLOC_CTX *tmp_mem,
3728 uint8_t *inbuf)
3730 struct tevent_req *req;
3731 struct smbXcli_req_state *state = NULL;
3732 struct iovec *iov = NULL;
3733 size_t i, num_iov = 0;
3734 NTSTATUS status;
3735 bool defer = true;
3736 struct smbXcli_session *last_session = NULL;
3737 size_t inbuf_len = smb_len_tcp(inbuf);
3739 status = smb2cli_inbuf_parse_compound(conn,
3740 inbuf + NBT_HDR_SIZE,
3741 inbuf_len,
3742 tmp_mem,
3743 &iov, &num_iov);
3744 if (!NT_STATUS_IS_OK(status)) {
3745 return status;
3748 for (i=0; i<num_iov; i+=4) {
3749 uint8_t *inbuf_ref = NULL;
3750 struct iovec *cur = &iov[i];
3751 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3752 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3753 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3754 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3755 uint16_t req_opcode;
3756 uint32_t req_flags;
3757 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3758 uint32_t new_credits;
3759 struct smbXcli_session *session = NULL;
3760 struct smb2_signing_key *signing_key = NULL;
3761 bool was_encrypted = false;
3763 new_credits = conn->smb2.cur_credits;
3764 new_credits += credits;
3765 if (new_credits > UINT16_MAX) {
3766 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3768 conn->smb2.cur_credits += credits;
3770 req = smb2cli_conn_find_pending(conn, mid);
3771 if (req == NULL) {
3772 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3774 state = tevent_req_data(req, struct smbXcli_req_state);
3776 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3777 if (opcode != req_opcode) {
3778 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3780 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3782 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3783 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3786 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3787 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3788 NT_STATUS_EQUAL(status, NT_STATUS_PENDING)) {
3789 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3791 if (state->smb2.got_async) {
3792 /* We only expect one STATUS_PENDING response */
3793 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3795 state->smb2.got_async = true;
3798 * async interim responses are not signed,
3799 * even if the SMB2_HDR_FLAG_SIGNED flag
3800 * is set.
3802 state->smb2.cancel_flags |= SMB2_HDR_FLAG_ASYNC;
3803 state->smb2.cancel_aid = async_id;
3805 if (state->smb2.notify_async) {
3806 tevent_req_defer_callback(req, state->ev);
3807 tevent_req_notify_callback(req);
3809 continue;
3812 session = state->session;
3813 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3814 session = last_session;
3816 last_session = session;
3818 if (flags & SMB2_HDR_FLAG_SIGNED) {
3819 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3821 if (session == NULL) {
3822 session = smbXcli_session_by_uid(state->conn,
3823 uid);
3826 if (session == NULL) {
3827 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3830 last_session = session;
3831 signing_key = session->smb2_channel.signing_key;
3834 if (opcode == SMB2_OP_SESSSETUP) {
3836 * We prefer the channel signing key, if it is
3837 * already there.
3839 * If we do not have a channel signing key yet,
3840 * we try the main signing key, if it is not
3841 * the final response.
3843 if (signing_key != NULL &&
3844 !smb2_signing_key_valid(signing_key) &&
3845 !NT_STATUS_IS_OK(status)) {
3846 signing_key = session->smb2->signing_key;
3849 if (signing_key != NULL &&
3850 !smb2_signing_key_valid(signing_key)) {
3852 * If we do not have a session key to
3853 * verify the signature, we defer the
3854 * signing check to the caller.
3856 * The caller gets NT_STATUS_OK, it
3857 * has to call
3858 * smb2cli_session_set_session_key()
3859 * or
3860 * smb2cli_session_set_channel_key()
3861 * which will check the signature
3862 * with the channel signing key.
3864 signing_key = NULL;
3867 if (!NT_STATUS_IS_OK(status)) {
3869 * Only check the signature of the last response
3870 * of a successfull session auth. This matches
3871 * Windows behaviour for NTLM auth and reauth.
3873 state->smb2.require_signed_response = false;
3877 if (state->smb2.should_sign ||
3878 state->smb2.require_signed_response)
3880 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3881 return NT_STATUS_ACCESS_DENIED;
3885 if (!smb2_signing_key_valid(signing_key) &&
3886 state->smb2.require_signed_response) {
3887 signing_key = session->smb2_channel.signing_key;
3890 if (cur[0].iov_len == SMB2_TF_HDR_SIZE) {
3891 const uint8_t *tf = (const uint8_t *)cur[0].iov_base;
3892 uint64_t uid = BVAL(tf, SMB2_TF_SESSION_ID);
3895 * If the response was encrypted in a SMB2_TRANSFORM
3896 * pdu, which belongs to the correct session,
3897 * we do not need to do signing checks
3899 * It could be the session the response belongs to
3900 * or the session that was used to encrypt the
3901 * SMB2_TRANSFORM request.
3903 if ((session && session->smb2->session_id == uid) ||
3904 (state->smb2.encryption_session_id == uid)) {
3905 signing_key = NULL;
3906 was_encrypted = true;
3910 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3912 * if the server returns NT_STATUS_USER_SESSION_DELETED
3913 * the response is not signed and we should
3914 * propagate the NT_STATUS_USER_SESSION_DELETED
3915 * status to the caller.
3917 state->smb2.signing_skipped = true;
3918 signing_key = NULL;
3920 if (NT_STATUS_EQUAL(status, NT_STATUS_REQUEST_OUT_OF_SEQUENCE)) {
3922 * if the server returns
3923 * NT_STATUS_REQUEST_OUT_OF_SEQUENCE for a session setup
3924 * request, the response is not signed and we should
3925 * propagate the NT_STATUS_REQUEST_OUT_OF_SEQUENCE
3926 * status to the caller
3928 if (opcode == SMB2_OP_SESSSETUP) {
3929 state->smb2.signing_skipped = true;
3930 signing_key = NULL;
3933 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
3935 * if the server returns NT_STATUS_NOT_SUPPORTED
3936 * for a session setup request, the response is not
3937 * signed and we should propagate the NT_STATUS_NOT_SUPPORTED
3938 * status to the caller.
3940 if (opcode == SMB2_OP_SESSSETUP) {
3941 state->smb2.signing_skipped = true;
3942 signing_key = NULL;
3945 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
3947 * if the server returns
3948 * NT_STATUS_ACCESS_DENIED for a session setup
3949 * request, the response is not signed and we should
3950 * propagate the NT_STATUS_ACCESS_DENIED
3951 * status to the caller without disconnecting
3952 * the connection because we where not able to
3953 * verify the response signature.
3955 if (opcode == SMB2_OP_SESSSETUP) {
3956 state->smb2.signing_skipped = true;
3957 signing_key = NULL;
3961 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3963 * if the server returns
3964 * NT_STATUS_INVALID_PARAMETER
3965 * the response might not be encrypted.
3967 if (state->smb2.should_encrypt && !was_encrypted) {
3968 state->smb2.signing_skipped = true;
3969 signing_key = NULL;
3973 if (state->smb2.should_encrypt && !was_encrypted) {
3974 if (!state->smb2.signing_skipped) {
3975 return NT_STATUS_ACCESS_DENIED;
3979 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3980 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3981 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3983 * if the server returns
3984 * NT_STATUS_NETWORK_NAME_DELETED
3985 * NT_STATUS_FILE_CLOSED
3986 * NT_STATUS_INVALID_PARAMETER
3987 * the response might not be signed
3988 * as this happens before the signing checks.
3990 * If server echos the signature (or all zeros)
3991 * we should report the status from the server
3992 * to the caller.
3994 if (signing_key) {
3995 int cmp;
3997 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3998 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3999 16);
4000 if (cmp == 0) {
4001 state->smb2.signing_skipped = true;
4002 signing_key = NULL;
4005 if (signing_key) {
4006 bool zero;
4007 zero = all_zero(inhdr+SMB2_HDR_SIGNATURE, 16);
4008 if (zero) {
4009 state->smb2.signing_skipped = true;
4010 signing_key = NULL;
4015 if (signing_key) {
4016 NTSTATUS signing_status;
4018 signing_status = smb2_signing_check_pdu(signing_key,
4019 &cur[1], 3);
4020 if (!NT_STATUS_IS_OK(signing_status)) {
4022 * If the signing check fails, we disconnect
4023 * the connection.
4025 return signing_status;
4029 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED) &&
4030 (session != NULL) && session->disconnect_expired)
4033 * this should be a short term hack
4034 * until the upper layers have implemented
4035 * re-authentication.
4037 return status;
4040 smbXcli_req_unset_pending(req);
4043 * There might be more than one response
4044 * we need to defer the notifications
4046 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
4047 defer = false;
4050 if (defer) {
4051 tevent_req_defer_callback(req, state->ev);
4055 * Note: here we use talloc_reference() in a way
4056 * that does not expose it to the caller.
4058 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
4059 if (tevent_req_nomem(inbuf_ref, req)) {
4060 continue;
4063 /* copy the related buffers */
4064 state->smb2.recv_iov[0] = cur[1];
4065 state->smb2.recv_iov[1] = cur[2];
4066 state->smb2.recv_iov[2] = cur[3];
4068 tevent_req_done(req);
4071 if (defer) {
4072 return NT_STATUS_RETRY;
4075 return NT_STATUS_OK;
4078 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4079 struct iovec **piov,
4080 const struct smb2cli_req_expected_response *expected,
4081 size_t num_expected)
4083 struct smbXcli_req_state *state =
4084 tevent_req_data(req,
4085 struct smbXcli_req_state);
4086 NTSTATUS status;
4087 size_t body_size;
4088 bool found_status = false;
4089 bool found_size = false;
4090 size_t i;
4092 if (piov != NULL) {
4093 *piov = NULL;
4096 if (tevent_req_is_in_progress(req) && state->smb2.got_async) {
4097 return NT_STATUS_PENDING;
4100 if (tevent_req_is_nterror(req, &status)) {
4101 for (i=0; i < num_expected; i++) {
4102 if (NT_STATUS_EQUAL(status, expected[i].status)) {
4103 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
4107 return status;
4110 if (num_expected == 0) {
4111 found_status = true;
4112 found_size = true;
4115 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
4116 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
4118 for (i=0; i < num_expected; i++) {
4119 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
4120 continue;
4123 found_status = true;
4124 if (expected[i].body_size == 0) {
4125 found_size = true;
4126 break;
4129 if (expected[i].body_size == body_size) {
4130 found_size = true;
4131 break;
4135 if (!found_status) {
4136 return status;
4139 if (state->smb2.signing_skipped) {
4140 if (num_expected > 0) {
4141 return NT_STATUS_ACCESS_DENIED;
4143 if (!NT_STATUS_IS_ERR(status)) {
4144 return NT_STATUS_ACCESS_DENIED;
4148 if (!found_size) {
4149 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4152 if (piov != NULL) {
4153 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
4156 return status;
4159 NTSTATUS smb2cli_req_get_sent_iov(struct tevent_req *req,
4160 struct iovec *sent_iov)
4162 struct smbXcli_req_state *state =
4163 tevent_req_data(req,
4164 struct smbXcli_req_state);
4166 if (tevent_req_is_in_progress(req)) {
4167 return NT_STATUS_PENDING;
4170 sent_iov[0].iov_base = state->smb2.hdr;
4171 sent_iov[0].iov_len = sizeof(state->smb2.hdr);
4173 sent_iov[1].iov_base = discard_const(state->smb2.fixed);
4174 sent_iov[1].iov_len = state->smb2.fixed_len;
4176 if (state->smb2.dyn != NULL) {
4177 sent_iov[2].iov_base = discard_const(state->smb2.dyn);
4178 sent_iov[2].iov_len = state->smb2.dyn_len;
4179 } else {
4180 sent_iov[2].iov_base = NULL;
4181 sent_iov[2].iov_len = 0;
4184 return NT_STATUS_OK;
4187 static const struct {
4188 enum protocol_types proto;
4189 const char *smb1_name;
4190 } smb1cli_prots[] = {
4191 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
4192 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
4193 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
4194 {PROTOCOL_LANMAN1, "LANMAN1.0"},
4195 {PROTOCOL_LANMAN2, "LM1.2X002"},
4196 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
4197 {PROTOCOL_LANMAN2, "LANMAN2.1"},
4198 {PROTOCOL_LANMAN2, "Samba"},
4199 {PROTOCOL_NT1, "NT LANMAN 1.0"},
4200 {PROTOCOL_NT1, "NT LM 0.12"},
4201 {PROTOCOL_SMB2_02, "SMB 2.002"},
4202 {PROTOCOL_SMB2_10, "SMB 2.???"},
4205 static const struct {
4206 enum protocol_types proto;
4207 uint16_t smb2_dialect;
4208 } smb2cli_prots[] = {
4209 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
4210 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
4211 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
4212 {PROTOCOL_SMB3_02, SMB3_DIALECT_REVISION_302},
4213 {PROTOCOL_SMB3_11, SMB3_DIALECT_REVISION_311},
4216 struct smbXcli_negprot_state {
4217 struct smbXcli_conn *conn;
4218 struct tevent_context *ev;
4219 uint32_t timeout_msec;
4221 struct {
4222 uint8_t fixed[36];
4223 } smb2;
4226 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
4227 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
4228 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
4229 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
4230 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
4231 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4232 TALLOC_CTX *frame,
4233 uint8_t *inbuf);
4235 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
4236 struct tevent_context *ev,
4237 struct smbXcli_conn *conn,
4238 uint32_t timeout_msec,
4239 enum protocol_types min_protocol,
4240 enum protocol_types max_protocol,
4241 uint16_t max_credits)
4243 struct tevent_req *req, *subreq;
4244 struct smbXcli_negprot_state *state;
4246 req = tevent_req_create(mem_ctx, &state,
4247 struct smbXcli_negprot_state);
4248 if (req == NULL) {
4249 return NULL;
4251 state->conn = conn;
4252 state->ev = ev;
4253 state->timeout_msec = timeout_msec;
4255 if (min_protocol == PROTOCOL_NONE) {
4256 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
4257 return tevent_req_post(req, ev);
4260 if (max_protocol == PROTOCOL_NONE) {
4261 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
4262 return tevent_req_post(req, ev);
4265 if (min_protocol > max_protocol) {
4266 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
4267 return tevent_req_post(req, ev);
4270 conn->min_protocol = min_protocol;
4271 conn->max_protocol = max_protocol;
4272 conn->protocol = PROTOCOL_NONE;
4274 if (max_protocol >= PROTOCOL_SMB2_02) {
4275 conn->smb2.max_credits = max_credits;
4278 if ((min_protocol < PROTOCOL_SMB2_02) &&
4279 (max_protocol < PROTOCOL_SMB2_02)) {
4281 * SMB1 only...
4283 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4285 subreq = smbXcli_negprot_smb1_subreq(state);
4286 if (tevent_req_nomem(subreq, req)) {
4287 return tevent_req_post(req, ev);
4289 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4290 return req;
4293 if ((min_protocol >= PROTOCOL_SMB2_02) &&
4294 (max_protocol >= PROTOCOL_SMB2_02)) {
4296 * SMB2 only...
4298 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4300 subreq = smbXcli_negprot_smb2_subreq(state);
4301 if (tevent_req_nomem(subreq, req)) {
4302 return tevent_req_post(req, ev);
4304 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4305 return req;
4309 * We send an SMB1 negprot with the SMB2 dialects
4310 * and expect a SMB1 or a SMB2 response.
4312 * smbXcli_negprot_dispatch_incoming() will fix the
4313 * callback to match protocol of the response.
4315 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
4317 subreq = smbXcli_negprot_smb1_subreq(state);
4318 if (tevent_req_nomem(subreq, req)) {
4319 return tevent_req_post(req, ev);
4321 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
4322 return req;
4325 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
4327 struct tevent_req *req =
4328 tevent_req_callback_data(subreq,
4329 struct tevent_req);
4330 NTSTATUS status;
4333 * we just want the low level error
4335 status = tevent_req_simple_recv_ntstatus(subreq);
4336 TALLOC_FREE(subreq);
4337 if (tevent_req_nterror(req, status)) {
4338 return;
4341 /* this should never happen */
4342 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4345 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
4347 size_t i;
4348 DATA_BLOB bytes = data_blob_null;
4349 uint8_t flags;
4350 uint16_t flags2;
4352 /* setup the protocol strings */
4353 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4354 uint8_t c = 2;
4355 bool ok;
4357 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4358 continue;
4361 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4362 continue;
4365 ok = data_blob_append(state, &bytes, &c, sizeof(c));
4366 if (!ok) {
4367 return NULL;
4371 * We now it is already ascii and
4372 * we want NULL termination.
4374 ok = data_blob_append(state, &bytes,
4375 smb1cli_prots[i].smb1_name,
4376 strlen(smb1cli_prots[i].smb1_name)+1);
4377 if (!ok) {
4378 return NULL;
4382 smb1cli_req_flags(state->conn->max_protocol,
4383 state->conn->smb1.client.capabilities,
4384 SMBnegprot,
4385 0, 0, &flags,
4386 0, 0, &flags2);
4388 return smb1cli_req_send(state, state->ev, state->conn,
4389 SMBnegprot,
4390 flags, ~flags,
4391 flags2, ~flags2,
4392 state->timeout_msec,
4393 0xFFFE, 0, NULL, /* pid, tid, session */
4394 0, NULL, /* wct, vwv */
4395 bytes.length, bytes.data);
4398 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
4400 struct tevent_req *req =
4401 tevent_req_callback_data(subreq,
4402 struct tevent_req);
4403 struct smbXcli_negprot_state *state =
4404 tevent_req_data(req,
4405 struct smbXcli_negprot_state);
4406 struct smbXcli_conn *conn = state->conn;
4407 struct iovec *recv_iov = NULL;
4408 uint8_t *inhdr = NULL;
4409 uint8_t wct;
4410 uint16_t *vwv;
4411 uint32_t num_bytes;
4412 uint8_t *bytes;
4413 NTSTATUS status;
4414 uint16_t protnum;
4415 size_t i;
4416 size_t num_prots = 0;
4417 uint8_t flags;
4418 uint32_t client_capabilities = conn->smb1.client.capabilities;
4419 uint32_t both_capabilities;
4420 uint32_t server_capabilities = 0;
4421 uint32_t capabilities;
4422 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
4423 uint32_t server_max_xmit = 0;
4424 uint32_t max_xmit;
4425 uint32_t server_max_mux = 0;
4426 uint16_t server_security_mode = 0;
4427 uint32_t server_session_key = 0;
4428 bool server_readbraw = false;
4429 bool server_writebraw = false;
4430 bool server_lockread = false;
4431 bool server_writeunlock = false;
4432 struct GUID server_guid = GUID_zero();
4433 DATA_BLOB server_gss_blob = data_blob_null;
4434 uint8_t server_challenge[8];
4435 char *server_workgroup = NULL;
4436 char *server_name = NULL;
4437 int server_time_zone = 0;
4438 NTTIME server_system_time = 0;
4439 static const struct smb1cli_req_expected_response expected[] = {
4441 .status = NT_STATUS_OK,
4442 .wct = 0x11, /* NT1 */
4445 .status = NT_STATUS_OK,
4446 .wct = 0x0D, /* LM */
4449 .status = NT_STATUS_OK,
4450 .wct = 0x01, /* CORE */
4454 ZERO_STRUCT(server_challenge);
4456 status = smb1cli_req_recv(subreq, state,
4457 &recv_iov,
4458 &inhdr,
4459 &wct,
4460 &vwv,
4461 NULL, /* pvwv_offset */
4462 &num_bytes,
4463 &bytes,
4464 NULL, /* pbytes_offset */
4465 NULL, /* pinbuf */
4466 expected, ARRAY_SIZE(expected));
4467 TALLOC_FREE(subreq);
4468 if (inhdr == NULL || tevent_req_nterror(req, status)) {
4469 return;
4472 flags = CVAL(inhdr, HDR_FLG);
4474 protnum = SVAL(vwv, 0);
4476 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
4477 if (smb1cli_prots[i].proto < state->conn->min_protocol) {
4478 continue;
4481 if (smb1cli_prots[i].proto > state->conn->max_protocol) {
4482 continue;
4485 if (protnum != num_prots) {
4486 num_prots++;
4487 continue;
4490 conn->protocol = smb1cli_prots[i].proto;
4491 break;
4494 if (conn->protocol == PROTOCOL_NONE) {
4495 DBG_ERR("No compatible protocol selected by server.\n");
4496 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4497 return;
4500 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
4501 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
4502 "and the selected protocol level doesn't support it.\n"));
4503 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4504 return;
4507 if (flags & FLAG_SUPPORT_LOCKREAD) {
4508 server_lockread = true;
4509 server_writeunlock = true;
4512 if (conn->protocol >= PROTOCOL_NT1) {
4513 const char *client_signing = NULL;
4514 bool server_mandatory = false;
4515 bool server_allowed = false;
4516 const char *server_signing = NULL;
4517 bool ok;
4518 uint8_t key_len;
4520 if (wct != 0x11) {
4521 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4522 return;
4525 /* NT protocol */
4526 server_security_mode = CVAL(vwv + 1, 0);
4527 server_max_mux = SVAL(vwv + 1, 1);
4528 server_max_xmit = IVAL(vwv + 3, 1);
4529 server_session_key = IVAL(vwv + 7, 1);
4530 server_time_zone = SVALS(vwv + 15, 1);
4531 server_time_zone *= 60;
4532 /* this time arrives in real GMT */
4533 server_system_time = BVAL(vwv + 11, 1);
4534 server_capabilities = IVAL(vwv + 9, 1);
4536 key_len = CVAL(vwv + 16, 1);
4538 if (server_capabilities & CAP_RAW_MODE) {
4539 server_readbraw = true;
4540 server_writebraw = true;
4542 if (server_capabilities & CAP_LOCK_AND_READ) {
4543 server_lockread = true;
4546 if (server_capabilities & CAP_EXTENDED_SECURITY) {
4547 DATA_BLOB blob1, blob2;
4549 if (num_bytes < 16) {
4550 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4551 return;
4554 blob1 = data_blob_const(bytes, 16);
4555 status = GUID_from_data_blob(&blob1, &server_guid);
4556 if (tevent_req_nterror(req, status)) {
4557 return;
4560 blob1 = data_blob_const(bytes+16, num_bytes-16);
4561 blob2 = data_blob_dup_talloc(state, blob1);
4562 if (blob1.length > 0 &&
4563 tevent_req_nomem(blob2.data, req)) {
4564 return;
4566 server_gss_blob = blob2;
4567 } else {
4568 DATA_BLOB blob1, blob2;
4570 if (num_bytes < key_len) {
4571 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4572 return;
4575 if (key_len != 0 && key_len != 8) {
4576 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4577 return;
4580 if (key_len == 8) {
4581 memcpy(server_challenge, bytes, 8);
4584 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4585 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
4586 if (blob1.length > 0) {
4587 size_t len;
4589 len = utf16_len_n(blob1.data,
4590 blob1.length);
4591 blob1.length = len;
4593 ok = convert_string_talloc(state,
4594 CH_UTF16LE,
4595 CH_UNIX,
4596 blob1.data,
4597 blob1.length,
4598 &server_workgroup,
4599 &len);
4600 if (!ok) {
4601 status = map_nt_error_from_unix_common(errno);
4602 tevent_req_nterror(req, status);
4603 return;
4607 blob2.data += blob1.length;
4608 blob2.length -= blob1.length;
4609 if (blob2.length > 0) {
4610 size_t len;
4612 len = utf16_len_n(blob1.data,
4613 blob1.length);
4614 blob1.length = len;
4616 ok = convert_string_talloc(state,
4617 CH_UTF16LE,
4618 CH_UNIX,
4619 blob2.data,
4620 blob2.length,
4621 &server_name,
4622 &len);
4623 if (!ok) {
4624 status = map_nt_error_from_unix_common(errno);
4625 tevent_req_nterror(req, status);
4626 return;
4631 client_signing = "disabled";
4632 if (conn->allow_signing) {
4633 client_signing = "allowed";
4635 if (conn->mandatory_signing) {
4636 client_signing = "required";
4639 server_signing = "not supported";
4640 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
4641 server_signing = "supported";
4642 server_allowed = true;
4643 } else if (conn->mandatory_signing) {
4645 * We have mandatory signing as client
4646 * lets assume the server will look at our
4647 * FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED
4648 * flag in the session setup
4650 server_signing = "not announced";
4651 server_allowed = true;
4653 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
4654 server_signing = "required";
4655 server_mandatory = true;
4658 ok = smb_signing_set_negotiated(conn->smb1.signing,
4659 server_allowed,
4660 server_mandatory);
4661 if (!ok) {
4662 DEBUG(1,("cli_negprot: SMB signing is required, "
4663 "but client[%s] and server[%s] mismatch\n",
4664 client_signing, server_signing));
4665 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
4666 return;
4669 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
4670 DATA_BLOB blob1;
4671 uint8_t key_len;
4672 time_t t;
4674 if (wct != 0x0D) {
4675 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4676 return;
4679 server_security_mode = SVAL(vwv + 1, 0);
4680 server_max_xmit = SVAL(vwv + 2, 0);
4681 server_max_mux = SVAL(vwv + 3, 0);
4682 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
4683 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
4684 server_session_key = IVAL(vwv + 6, 0);
4685 server_time_zone = SVALS(vwv + 10, 0);
4686 server_time_zone *= 60;
4687 /* this time is converted to GMT by make_unix_date */
4688 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
4689 unix_to_nt_time(&server_system_time, t);
4690 key_len = SVAL(vwv + 11, 0);
4692 if (num_bytes < key_len) {
4693 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4694 return;
4697 if (key_len != 0 && key_len != 8) {
4698 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4699 return;
4702 if (key_len == 8) {
4703 memcpy(server_challenge, bytes, 8);
4706 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
4707 if (blob1.length > 0) {
4708 size_t len;
4709 bool ok;
4711 len = utf16_len_n(blob1.data,
4712 blob1.length);
4713 blob1.length = len;
4715 ok = convert_string_talloc(state,
4716 CH_DOS,
4717 CH_UNIX,
4718 blob1.data,
4719 blob1.length,
4720 &server_workgroup,
4721 &len);
4722 if (!ok) {
4723 status = map_nt_error_from_unix_common(errno);
4724 tevent_req_nterror(req, status);
4725 return;
4729 } else {
4730 /* the old core protocol */
4731 server_time_zone = get_time_zone(time(NULL));
4732 server_max_xmit = 1024;
4733 server_max_mux = 1;
4736 if (server_max_xmit < 1024) {
4737 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4738 return;
4741 if (server_max_mux < 1) {
4742 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4743 return;
4747 * Now calculate the negotiated capabilities
4748 * based on the mask for:
4749 * - client only flags
4750 * - flags used in both directions
4751 * - server only flags
4753 both_capabilities = client_capabilities & server_capabilities;
4754 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
4755 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
4756 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
4758 max_xmit = MIN(client_max_xmit, server_max_xmit);
4760 conn->smb1.server.capabilities = server_capabilities;
4761 conn->smb1.capabilities = capabilities;
4763 conn->smb1.server.max_xmit = server_max_xmit;
4764 conn->smb1.max_xmit = max_xmit;
4766 conn->smb1.server.max_mux = server_max_mux;
4768 conn->smb1.server.security_mode = server_security_mode;
4770 conn->smb1.server.readbraw = server_readbraw;
4771 conn->smb1.server.writebraw = server_writebraw;
4772 conn->smb1.server.lockread = server_lockread;
4773 conn->smb1.server.writeunlock = server_writeunlock;
4775 conn->smb1.server.session_key = server_session_key;
4777 talloc_steal(conn, server_gss_blob.data);
4778 conn->smb1.server.gss_blob = server_gss_blob;
4779 conn->smb1.server.guid = server_guid;
4780 memcpy(conn->smb1.server.challenge, server_challenge, 8);
4781 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
4782 conn->smb1.server.name = talloc_move(conn, &server_name);
4784 conn->smb1.server.time_zone = server_time_zone;
4785 conn->smb1.server.system_time = server_system_time;
4787 tevent_req_done(req);
4790 static size_t smbXcli_padding_helper(uint32_t offset, size_t n)
4792 if ((offset & (n-1)) == 0) return 0;
4793 return n - (offset & (n-1));
4796 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
4798 size_t i;
4799 uint8_t *buf;
4800 uint16_t dialect_count = 0;
4801 DATA_BLOB dyn = data_blob_null;
4803 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4804 bool ok;
4805 uint8_t val[2];
4807 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
4808 continue;
4811 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
4812 continue;
4815 SSVAL(val, 0, smb2cli_prots[i].smb2_dialect);
4817 ok = data_blob_append(state, &dyn, val, sizeof(val));
4818 if (!ok) {
4819 return NULL;
4822 dialect_count++;
4825 buf = state->smb2.fixed;
4826 SSVAL(buf, 0, 36);
4827 SSVAL(buf, 2, dialect_count);
4828 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
4829 SSVAL(buf, 6, 0); /* Reserved */
4830 if (state->conn->max_protocol >= PROTOCOL_SMB3_00) {
4831 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
4832 } else {
4833 SIVAL(buf, 8, 0); /* Capabilities */
4835 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
4836 NTSTATUS status;
4837 struct GUID_ndr_buf guid_buf = { .buf = {0}, };
4839 status = GUID_to_ndr_buf(&state->conn->smb2.client.guid,
4840 &guid_buf);
4841 if (!NT_STATUS_IS_OK(status)) {
4842 return NULL;
4844 memcpy(buf+12, guid_buf.buf, 16); /* ClientGuid */
4845 } else {
4846 memset(buf+12, 0, 16); /* ClientGuid */
4849 if (state->conn->max_protocol >= PROTOCOL_SMB3_11) {
4850 const struct smb3_signing_capabilities *client_sign_algos =
4851 &state->conn->smb2.client.smb3_capabilities.signing;
4852 const struct smb3_encryption_capabilities *client_ciphers =
4853 &state->conn->smb2.client.smb3_capabilities.encryption;
4854 NTSTATUS status;
4855 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4856 uint8_t *netname_utf16 = NULL;
4857 size_t netname_utf16_len = 0;
4858 uint32_t offset;
4859 DATA_BLOB b;
4860 uint8_t p[38];
4861 const uint8_t zeros[8] = {0, };
4862 size_t pad;
4863 bool ok;
4865 SSVAL(p, 0, 1); /* HashAlgorithmCount */
4866 SSVAL(p, 2, 32); /* SaltLength */
4867 SSVAL(p, 4, SMB2_PREAUTH_INTEGRITY_SHA512);
4868 generate_random_buffer(p + 6, 32);
4870 status = smb2_negotiate_context_add(
4871 state, &c, SMB2_PREAUTH_INTEGRITY_CAPABILITIES, p, 38);
4872 if (!NT_STATUS_IS_OK(status)) {
4873 return NULL;
4876 if (client_ciphers->num_algos > 0) {
4877 size_t ofs = 0;
4878 SSVAL(p, ofs, client_ciphers->num_algos);
4879 ofs += 2;
4881 for (i = 0; i < client_ciphers->num_algos; i++) {
4882 size_t next_ofs = ofs + 2;
4883 SMB_ASSERT(next_ofs < ARRAY_SIZE(p));
4884 SSVAL(p, ofs, client_ciphers->algos[i]);
4885 ofs = next_ofs;
4888 status = smb2_negotiate_context_add(
4889 state, &c, SMB2_ENCRYPTION_CAPABILITIES, p, ofs);
4890 if (!NT_STATUS_IS_OK(status)) {
4891 return NULL;
4895 if (client_sign_algos->num_algos > 0) {
4896 size_t ofs = 0;
4897 SSVAL(p, ofs, client_sign_algos->num_algos);
4898 ofs += 2;
4900 for (i = 0; i < client_sign_algos->num_algos; i++) {
4901 size_t next_ofs = ofs + 2;
4902 SMB_ASSERT(next_ofs < ARRAY_SIZE(p));
4903 SSVAL(p, ofs, client_sign_algos->algos[i]);
4904 ofs = next_ofs;
4907 status = smb2_negotiate_context_add(
4908 state, &c, SMB2_SIGNING_CAPABILITIES, p, ofs);
4909 if (!NT_STATUS_IS_OK(status)) {
4910 return NULL;
4914 ok = convert_string_talloc(state, CH_UNIX, CH_UTF16,
4915 state->conn->remote_name,
4916 strlen(state->conn->remote_name),
4917 &netname_utf16, &netname_utf16_len);
4918 if (!ok) {
4919 return NULL;
4922 status = smb2_negotiate_context_add(state, &c,
4923 SMB2_NETNAME_NEGOTIATE_CONTEXT_ID,
4924 netname_utf16, netname_utf16_len);
4925 if (!NT_STATUS_IS_OK(status)) {
4926 return NULL;
4929 status = smb2_negotiate_context_push(state, &b, c);
4930 if (!NT_STATUS_IS_OK(status)) {
4931 return NULL;
4934 offset = SMB2_HDR_BODY + sizeof(state->smb2.fixed) + dyn.length;
4935 pad = smbXcli_padding_helper(offset, 8);
4937 ok = data_blob_append(state, &dyn, zeros, pad);
4938 if (!ok) {
4939 return NULL;
4941 offset += pad;
4943 ok = data_blob_append(state, &dyn, b.data, b.length);
4944 if (!ok) {
4945 return NULL;
4948 SIVAL(buf, 28, offset); /* NegotiateContextOffset */
4949 SSVAL(buf, 32, c.num_contexts); /* NegotiateContextCount */
4950 SSVAL(buf, 34, 0); /* Reserved */
4951 } else {
4952 SBVAL(buf, 28, 0); /* Reserved/ClientStartTime */
4955 return smb2cli_req_send(state, state->ev,
4956 state->conn, SMB2_OP_NEGPROT,
4957 0, 0, /* flags */
4958 state->timeout_msec,
4959 NULL, NULL, /* tcon, session */
4960 state->smb2.fixed, sizeof(state->smb2.fixed),
4961 dyn.data, dyn.length,
4962 UINT16_MAX); /* max_dyn_len */
4965 static NTSTATUS smbXcli_negprot_smb3_check_capabilities(struct tevent_req *req);
4967 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
4969 struct tevent_req *req =
4970 tevent_req_callback_data(subreq,
4971 struct tevent_req);
4972 struct smbXcli_negprot_state *state =
4973 tevent_req_data(req,
4974 struct smbXcli_negprot_state);
4975 struct smbXcli_conn *conn = state->conn;
4976 size_t security_offset, security_length;
4977 DATA_BLOB blob;
4978 NTSTATUS status;
4979 struct iovec *iov = NULL;
4980 uint8_t *body;
4981 size_t i;
4982 uint16_t dialect_revision;
4983 struct smb2_negotiate_contexts c = { .num_contexts = 0, };
4984 uint32_t negotiate_context_offset = 0;
4985 uint16_t negotiate_context_count = 0;
4986 DATA_BLOB negotiate_context_blob = data_blob_null;
4987 size_t avail;
4988 size_t ctx_ofs;
4989 size_t needed;
4990 struct smb2_negotiate_context *preauth = NULL;
4991 uint16_t hash_count;
4992 uint16_t salt_length;
4993 uint16_t hash_selected;
4994 gnutls_hash_hd_t hash_hnd = NULL;
4995 struct smb2_negotiate_context *sign_algo = NULL;
4996 struct smb2_negotiate_context *cipher = NULL;
4997 struct iovec sent_iov[3] = {{0}, {0}, {0}};
4998 static const struct smb2cli_req_expected_response expected[] = {
5000 .status = NT_STATUS_OK,
5001 .body_size = 0x41
5004 int rc;
5006 status = smb2cli_req_recv(subreq, state, &iov,
5007 expected, ARRAY_SIZE(expected));
5008 if (tevent_req_nterror(req, status) || iov == NULL) {
5009 return;
5012 body = (uint8_t *)iov[1].iov_base;
5014 dialect_revision = SVAL(body, 4);
5016 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
5017 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
5018 continue;
5021 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
5022 continue;
5025 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
5026 continue;
5029 conn->protocol = smb2cli_prots[i].proto;
5030 break;
5033 if (conn->protocol == PROTOCOL_NONE) {
5034 TALLOC_FREE(subreq);
5036 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
5037 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5038 return;
5041 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
5042 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5043 return;
5046 /* make sure we do not loop forever */
5047 state->conn->min_protocol = PROTOCOL_SMB2_02;
5050 * send a SMB2 negprot, in order to negotiate
5051 * the SMB2 dialect.
5053 subreq = smbXcli_negprot_smb2_subreq(state);
5054 if (tevent_req_nomem(subreq, req)) {
5055 return;
5057 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
5058 return;
5061 conn->smb2.server.security_mode = SVAL(body, 2);
5062 if (conn->protocol >= PROTOCOL_SMB3_11) {
5063 negotiate_context_count = SVAL(body, 6);
5066 blob = data_blob_const(body + 8, 16);
5067 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
5068 if (tevent_req_nterror(req, status)) {
5069 return;
5072 conn->smb2.server.capabilities = IVAL(body, 24);
5073 conn->smb2.server.max_trans_size= IVAL(body, 28);
5074 conn->smb2.server.max_read_size = IVAL(body, 32);
5075 conn->smb2.server.max_write_size= IVAL(body, 36);
5076 conn->smb2.server.system_time = BVAL(body, 40);
5077 conn->smb2.server.start_time = BVAL(body, 48);
5079 security_offset = SVAL(body, 56);
5080 security_length = SVAL(body, 58);
5082 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
5083 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5084 return;
5087 if (security_length > iov[2].iov_len) {
5088 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5089 return;
5092 conn->smb2.server.gss_blob = data_blob_talloc(conn,
5093 iov[2].iov_base,
5094 security_length);
5095 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
5096 return;
5099 if (conn->protocol >= PROTOCOL_SMB3_00) {
5100 conn->smb2.server.sign_algo = SMB2_SIGNING_AES128_CMAC;
5101 } else {
5102 conn->smb2.server.sign_algo = SMB2_SIGNING_HMAC_SHA256;
5105 if (conn->protocol < PROTOCOL_SMB3_11) {
5106 TALLOC_FREE(subreq);
5108 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
5109 conn->smb2.server.cipher = SMB2_ENCRYPTION_AES128_CCM;
5112 status = smbXcli_negprot_smb3_check_capabilities(req);
5113 if (tevent_req_nterror(req, status)) {
5114 return;
5117 tevent_req_done(req);
5118 return;
5122 * Here we are now at SMB3_11, so encryption should be
5123 * negotiated via context, not capabilities.
5126 if (conn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION) {
5128 * Server set SMB2_CAP_ENCRYPTION capability,
5129 * but *SHOULD* not, not *MUST* not. Just mask it off.
5130 * NetApp seems to do this:
5131 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13009
5133 conn->smb2.server.capabilities &= ~SMB2_CAP_ENCRYPTION;
5136 negotiate_context_offset = IVAL(body, 60);
5137 if (negotiate_context_offset < security_offset) {
5138 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5139 return;
5142 ctx_ofs = negotiate_context_offset - security_offset;
5143 if (ctx_ofs > iov[2].iov_len) {
5144 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5145 return;
5147 avail = iov[2].iov_len - security_length;
5148 needed = iov[2].iov_len - ctx_ofs;
5149 if (needed > avail) {
5150 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5151 return;
5154 negotiate_context_blob.data = (uint8_t *)iov[2].iov_base;
5155 negotiate_context_blob.length = iov[2].iov_len;
5157 negotiate_context_blob.data += ctx_ofs;
5158 negotiate_context_blob.length -= ctx_ofs;
5160 status = smb2_negotiate_context_parse(state,
5161 negotiate_context_blob,
5162 negotiate_context_count,
5163 &c);
5164 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
5165 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
5167 if (tevent_req_nterror(req, status)) {
5168 return;
5171 preauth = smb2_negotiate_context_find(&c,
5172 SMB2_PREAUTH_INTEGRITY_CAPABILITIES);
5173 if (preauth == NULL) {
5174 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5175 return;
5178 if (preauth->data.length < 6) {
5179 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5180 return;
5183 hash_count = SVAL(preauth->data.data, 0);
5184 salt_length = SVAL(preauth->data.data, 2);
5185 hash_selected = SVAL(preauth->data.data, 4);
5187 if (hash_count != 1) {
5188 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5189 return;
5192 if (preauth->data.length != (6 + salt_length)) {
5193 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5194 return;
5197 if (hash_selected != SMB2_PREAUTH_INTEGRITY_SHA512) {
5198 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5199 return;
5202 sign_algo = smb2_negotiate_context_find(&c, SMB2_SIGNING_CAPABILITIES);
5203 if (sign_algo != NULL) {
5204 const struct smb3_signing_capabilities *client_sign_algos =
5205 &state->conn->smb2.client.smb3_capabilities.signing;
5206 bool found_selected = false;
5207 uint16_t sign_algo_count;
5208 uint16_t sign_algo_selected;
5210 if (client_sign_algos->num_algos == 0) {
5212 * We didn't ask for SMB2_ENCRYPTION_CAPABILITIES
5214 tevent_req_nterror(req,
5215 NT_STATUS_INVALID_NETWORK_RESPONSE);
5216 return;
5219 if (sign_algo->data.length < 2) {
5220 tevent_req_nterror(req,
5221 NT_STATUS_INVALID_NETWORK_RESPONSE);
5222 return;
5225 sign_algo_count = SVAL(sign_algo->data.data, 0);
5226 if (sign_algo_count != 1) {
5227 tevent_req_nterror(req,
5228 NT_STATUS_INVALID_NETWORK_RESPONSE);
5229 return;
5232 if (sign_algo->data.length < (2 + 2 * sign_algo_count)) {
5233 tevent_req_nterror(req,
5234 NT_STATUS_INVALID_NETWORK_RESPONSE);
5235 return;
5237 sign_algo_selected = SVAL(sign_algo->data.data, 2);
5239 for (i = 0; i < client_sign_algos->num_algos; i++) {
5240 if (client_sign_algos->algos[i] == sign_algo_selected) {
5242 * We found a match
5244 found_selected = true;
5245 break;
5249 if (!found_selected) {
5251 * The server send a sign_algo we didn't offer.
5253 tevent_req_nterror(req,
5254 NT_STATUS_INVALID_NETWORK_RESPONSE);
5255 return;
5258 conn->smb2.server.sign_algo = sign_algo_selected;
5261 cipher = smb2_negotiate_context_find(&c, SMB2_ENCRYPTION_CAPABILITIES);
5262 if (cipher != NULL) {
5263 const struct smb3_encryption_capabilities *client_ciphers =
5264 &state->conn->smb2.client.smb3_capabilities.encryption;
5265 bool found_selected = false;
5266 uint16_t cipher_count;
5267 uint16_t cipher_selected;
5269 if (client_ciphers->num_algos == 0) {
5271 * We didn't ask for SMB2_ENCRYPTION_CAPABILITIES
5273 tevent_req_nterror(req,
5274 NT_STATUS_INVALID_NETWORK_RESPONSE);
5275 return;
5278 if (cipher->data.length < 2) {
5279 tevent_req_nterror(req,
5280 NT_STATUS_INVALID_NETWORK_RESPONSE);
5281 return;
5284 cipher_count = SVAL(cipher->data.data, 0);
5285 if (cipher_count != 1) {
5286 tevent_req_nterror(req,
5287 NT_STATUS_INVALID_NETWORK_RESPONSE);
5288 return;
5291 if (cipher->data.length < (2 + 2 * cipher_count)) {
5292 tevent_req_nterror(req,
5293 NT_STATUS_INVALID_NETWORK_RESPONSE);
5294 return;
5296 cipher_selected = SVAL(cipher->data.data, 2);
5298 for (i = 0; i < client_ciphers->num_algos; i++) {
5299 if (cipher_selected == SMB2_ENCRYPTION_NONE) {
5301 * encryption not supported
5303 found_selected = true;
5304 break;
5306 if (client_ciphers->algos[i] == cipher_selected) {
5308 * We found a match
5310 found_selected = true;
5311 break;
5315 if (!found_selected) {
5317 * The server send a cipher we didn't offer.
5319 tevent_req_nterror(req,
5320 NT_STATUS_INVALID_NETWORK_RESPONSE);
5321 return;
5324 conn->smb2.server.cipher = cipher_selected;
5327 /* First we hash the request */
5328 smb2cli_req_get_sent_iov(subreq, sent_iov);
5330 rc = gnutls_hash_init(&hash_hnd, GNUTLS_DIG_SHA512);
5331 if (rc < 0) {
5332 tevent_req_nterror(req,
5333 gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED));
5334 return;
5337 rc = gnutls_hash(hash_hnd,
5338 conn->smb2.preauth_sha512,
5339 sizeof(conn->smb2.preauth_sha512));
5340 if (rc < 0) {
5341 gnutls_hash_deinit(hash_hnd, NULL);
5342 tevent_req_nterror(req,
5343 gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED));
5344 return;
5346 for (i = 0; i < 3; i++) {
5347 rc = gnutls_hash(hash_hnd,
5348 sent_iov[i].iov_base,
5349 sent_iov[i].iov_len);
5350 if (rc < 0) {
5351 gnutls_hash_deinit(hash_hnd, NULL);
5352 tevent_req_nterror(req,
5353 gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED));
5354 return;
5358 if (rc < 0) {
5359 gnutls_hash_deinit(hash_hnd, NULL);
5360 tevent_req_nterror(req,
5361 gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED));
5362 return;
5365 /* This resets the hash state */
5366 gnutls_hash_output(hash_hnd, conn->smb2.preauth_sha512);
5367 TALLOC_FREE(subreq);
5369 /* And now we hash the response */
5370 rc = gnutls_hash(hash_hnd,
5371 conn->smb2.preauth_sha512,
5372 sizeof(conn->smb2.preauth_sha512));
5373 if (rc < 0) {
5374 gnutls_hash_deinit(hash_hnd, NULL);
5375 tevent_req_nterror(req,
5376 gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED));
5377 return;
5379 for (i = 0; i < 3; i++) {
5380 rc = gnutls_hash(hash_hnd,
5381 iov[i].iov_base,
5382 iov[i].iov_len);
5383 if (rc < 0) {
5384 gnutls_hash_deinit(hash_hnd, NULL);
5385 tevent_req_nterror(req,
5386 gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED));
5387 return;
5390 gnutls_hash_deinit(hash_hnd, conn->smb2.preauth_sha512);
5391 if (rc < 0) {
5392 tevent_req_nterror(req,
5393 NT_STATUS_UNSUCCESSFUL);
5394 return;
5397 status = smbXcli_negprot_smb3_check_capabilities(req);
5398 if (tevent_req_nterror(req, status)) {
5399 return;
5402 tevent_req_done(req);
5405 static NTSTATUS smbXcli_negprot_smb3_check_capabilities(struct tevent_req *req)
5407 struct smbXcli_negprot_state *state =
5408 tevent_req_data(req,
5409 struct smbXcli_negprot_state);
5410 struct smbXcli_conn *conn = state->conn;
5412 return smb311_capabilities_check(&conn->smb2.client.smb3_capabilities,
5413 "smbXcli_negprot",
5414 DBGLVL_ERR,
5415 NT_STATUS_ACCESS_DENIED,
5416 "client",
5417 conn->protocol,
5418 conn->smb2.server.cipher);
5421 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
5422 TALLOC_CTX *tmp_mem,
5423 uint8_t *inbuf)
5425 size_t num_pending = talloc_array_length(conn->pending);
5426 struct tevent_req *subreq;
5427 struct smbXcli_req_state *substate;
5428 struct tevent_req *req;
5429 uint32_t protocol_magic;
5430 size_t inbuf_len = smb_len_nbt(inbuf);
5432 if (num_pending != 1) {
5433 return NT_STATUS_INTERNAL_ERROR;
5436 if (inbuf_len < 4) {
5437 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5440 subreq = conn->pending[0];
5441 substate = tevent_req_data(subreq, struct smbXcli_req_state);
5442 req = tevent_req_callback_data(subreq, struct tevent_req);
5444 protocol_magic = IVAL(inbuf, 4);
5446 switch (protocol_magic) {
5447 case SMB_MAGIC:
5448 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
5449 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
5450 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
5452 case SMB2_MAGIC:
5453 if (substate->smb2.recv_iov == NULL) {
5455 * For the SMB1 negprot we have move it.
5457 substate->smb2.recv_iov = substate->smb1.recv_iov;
5458 substate->smb1.recv_iov = NULL;
5462 * we got an SMB2 answer, which consumed sequence number 0
5463 * so we need to use 1 as the next one.
5465 * we also need to set the current credits to 0
5466 * as we consumed the initial one. The SMB2 answer
5467 * hopefully grant us a new credit.
5469 conn->smb2.mid = 1;
5470 conn->smb2.cur_credits = 0;
5471 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
5472 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
5473 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
5476 DEBUG(10, ("Got non-SMB PDU\n"));
5477 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5480 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
5482 return tevent_req_simple_recv_ntstatus(req);
5485 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
5486 uint32_t timeout_msec,
5487 enum protocol_types min_protocol,
5488 enum protocol_types max_protocol)
5490 TALLOC_CTX *frame = talloc_stackframe();
5491 struct tevent_context *ev;
5492 struct tevent_req *req;
5493 NTSTATUS status = NT_STATUS_NO_MEMORY;
5494 bool ok;
5496 if (smbXcli_conn_has_async_calls(conn)) {
5498 * Can't use sync call while an async call is in flight
5500 status = NT_STATUS_INVALID_PARAMETER_MIX;
5501 goto fail;
5503 ev = samba_tevent_context_init(frame);
5504 if (ev == NULL) {
5505 goto fail;
5507 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
5508 min_protocol, max_protocol,
5509 WINDOWS_CLIENT_PURE_SMB2_NEGPROT_INITIAL_CREDIT_ASK);
5510 if (req == NULL) {
5511 goto fail;
5513 ok = tevent_req_poll_ntstatus(req, ev, &status);
5514 if (!ok) {
5515 goto fail;
5517 status = smbXcli_negprot_recv(req);
5518 fail:
5519 TALLOC_FREE(frame);
5520 return status;
5523 struct smb2cli_validate_negotiate_info_state {
5524 struct smbXcli_conn *conn;
5525 DATA_BLOB in_input_buffer;
5526 DATA_BLOB in_output_buffer;
5527 DATA_BLOB out_input_buffer;
5528 DATA_BLOB out_output_buffer;
5529 uint16_t dialect;
5532 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq);
5534 struct tevent_req *smb2cli_validate_negotiate_info_send(TALLOC_CTX *mem_ctx,
5535 struct tevent_context *ev,
5536 struct smbXcli_conn *conn,
5537 uint32_t timeout_msec,
5538 struct smbXcli_session *session,
5539 struct smbXcli_tcon *tcon)
5541 struct tevent_req *req;
5542 struct smb2cli_validate_negotiate_info_state *state;
5543 uint8_t *buf;
5544 uint16_t dialect_count = 0;
5545 struct tevent_req *subreq;
5546 bool _save_should_sign;
5547 size_t i;
5549 req = tevent_req_create(mem_ctx, &state,
5550 struct smb2cli_validate_negotiate_info_state);
5551 if (req == NULL) {
5552 return NULL;
5554 state->conn = conn;
5556 state->in_input_buffer = data_blob_talloc_zero(state,
5557 4 + 16 + 1 + 1 + 2);
5558 if (tevent_req_nomem(state->in_input_buffer.data, req)) {
5559 return tevent_req_post(req, ev);
5561 buf = state->in_input_buffer.data;
5563 if (state->conn->max_protocol >= PROTOCOL_SMB3_00) {
5564 SIVAL(buf, 0, conn->smb2.client.capabilities);
5565 } else {
5566 SIVAL(buf, 0, 0); /* Capabilities */
5568 if (state->conn->max_protocol >= PROTOCOL_SMB2_10) {
5569 NTSTATUS status;
5570 struct GUID_ndr_buf guid_buf = { .buf = {0}, };
5572 status = GUID_to_ndr_buf(&conn->smb2.client.guid,
5573 &guid_buf);
5574 if (!NT_STATUS_IS_OK(status)) {
5575 return NULL;
5577 memcpy(buf+4, guid_buf.buf, 16); /* ClientGuid */
5578 } else {
5579 memset(buf+4, 0, 16); /* ClientGuid */
5581 if (state->conn->min_protocol >= PROTOCOL_SMB2_02) {
5582 SCVAL(buf, 20, conn->smb2.client.security_mode);
5583 } else {
5584 SCVAL(buf, 20, 0);
5586 SCVAL(buf, 21, 0); /* reserved */
5588 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
5589 bool ok;
5590 size_t ofs;
5592 if (smb2cli_prots[i].proto < state->conn->min_protocol) {
5593 continue;
5596 if (smb2cli_prots[i].proto > state->conn->max_protocol) {
5597 continue;
5600 if (smb2cli_prots[i].proto == state->conn->protocol) {
5601 state->dialect = smb2cli_prots[i].smb2_dialect;
5604 ofs = state->in_input_buffer.length;
5605 ok = data_blob_realloc(state, &state->in_input_buffer,
5606 ofs + 2);
5607 if (!ok) {
5608 tevent_req_oom(req);
5609 return tevent_req_post(req, ev);
5612 buf = state->in_input_buffer.data;
5613 SSVAL(buf, ofs, smb2cli_prots[i].smb2_dialect);
5615 dialect_count++;
5617 buf = state->in_input_buffer.data;
5618 SSVAL(buf, 22, dialect_count);
5620 _save_should_sign = smb2cli_tcon_is_signing_on(tcon);
5621 smb2cli_tcon_should_sign(tcon, true);
5622 subreq = smb2cli_ioctl_send(state, ev, conn,
5623 timeout_msec, session, tcon,
5624 UINT64_MAX, /* in_fid_persistent */
5625 UINT64_MAX, /* in_fid_volatile */
5626 FSCTL_VALIDATE_NEGOTIATE_INFO,
5627 0, /* in_max_input_length */
5628 &state->in_input_buffer,
5629 24, /* in_max_output_length */
5630 &state->in_output_buffer,
5631 SMB2_IOCTL_FLAG_IS_FSCTL);
5632 smb2cli_tcon_should_sign(tcon, _save_should_sign);
5633 if (tevent_req_nomem(subreq, req)) {
5634 return tevent_req_post(req, ev);
5636 tevent_req_set_callback(subreq,
5637 smb2cli_validate_negotiate_info_done,
5638 req);
5640 return req;
5643 static void smb2cli_validate_negotiate_info_done(struct tevent_req *subreq)
5645 struct tevent_req *req =
5646 tevent_req_callback_data(subreq,
5647 struct tevent_req);
5648 struct smb2cli_validate_negotiate_info_state *state =
5649 tevent_req_data(req,
5650 struct smb2cli_validate_negotiate_info_state);
5651 NTSTATUS status;
5652 const uint8_t *buf;
5653 uint32_t capabilities;
5654 DATA_BLOB guid_blob;
5655 struct GUID server_guid;
5656 uint16_t security_mode;
5657 uint16_t dialect;
5659 status = smb2cli_ioctl_recv(subreq, state,
5660 &state->out_input_buffer,
5661 &state->out_output_buffer);
5662 TALLOC_FREE(subreq);
5665 * This response must be signed correctly for
5666 * these "normal" error codes to be processed.
5667 * If the packet wasn't signed correctly we will get
5668 * NT_STATUS_ACCESS_DENIED or NT_STATUS_HMAC_NOT_SUPPORTED,
5669 * or NT_STATUS_INVALID_NETWORK_RESPONSE
5670 * from smb2_signing_check_pdu().
5672 * We must never ignore the above errors here.
5675 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED)) {
5677 * The response was signed, but not supported
5679 * Older Windows and Samba releases return
5680 * NT_STATUS_FILE_CLOSED.
5682 tevent_req_done(req);
5683 return;
5685 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_DEVICE_REQUEST)) {
5687 * The response was signed, but not supported
5689 * This is returned by the NTVFS based Samba 4.x file server
5690 * for file shares.
5692 tevent_req_done(req);
5693 return;
5695 if (NT_STATUS_EQUAL(status, NT_STATUS_FS_DRIVER_REQUIRED)) {
5697 * The response was signed, but not supported
5699 * This is returned by the NTVFS based Samba 4.x file server
5700 * for ipc shares.
5702 tevent_req_done(req);
5703 return;
5705 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
5707 * The response was signed, but not supported
5709 * This might be returned by older Windows versions or by
5710 * NetApp SMB server implementations.
5712 * See
5714 * https://blogs.msdn.microsoft.com/openspecification/2012/06/28/smb3-secure-dialect-negotiation/
5717 tevent_req_done(req);
5718 return;
5720 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
5722 * The response was signed, but not supported
5724 * This might be returned by NetApp Ontap 7.3.7 SMB server
5725 * implementations.
5727 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=14607
5730 tevent_req_done(req);
5731 return;
5733 if (tevent_req_nterror(req, status)) {
5734 return;
5737 if (state->out_output_buffer.length != 24) {
5738 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5739 return;
5742 buf = state->out_output_buffer.data;
5744 capabilities = IVAL(buf, 0);
5745 guid_blob = data_blob_const(buf + 4, 16);
5746 status = GUID_from_data_blob(&guid_blob, &server_guid);
5747 if (tevent_req_nterror(req, status)) {
5748 return;
5750 security_mode = CVAL(buf, 20);
5751 dialect = SVAL(buf, 22);
5753 if (capabilities != state->conn->smb2.server.capabilities) {
5754 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5755 return;
5758 if (!GUID_equal(&server_guid, &state->conn->smb2.server.guid)) {
5759 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5760 return;
5763 if (security_mode != state->conn->smb2.server.security_mode) {
5764 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5765 return;
5768 if (dialect != state->dialect) {
5769 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
5770 return;
5773 tevent_req_done(req);
5776 NTSTATUS smb2cli_validate_negotiate_info_recv(struct tevent_req *req)
5778 return tevent_req_simple_recv_ntstatus(req);
5781 static int smbXcli_session_destructor(struct smbXcli_session *session)
5783 if (session->conn == NULL) {
5784 return 0;
5787 DLIST_REMOVE(session->conn->sessions, session);
5788 return 0;
5791 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
5792 struct smbXcli_conn *conn)
5794 struct smbXcli_session *session;
5795 NTSTATUS status;
5797 session = talloc_zero(mem_ctx, struct smbXcli_session);
5798 if (session == NULL) {
5799 return NULL;
5801 session->smb2 = talloc_zero(session, struct smb2cli_session);
5802 if (session->smb2 == NULL) {
5803 talloc_free(session);
5804 return NULL;
5806 talloc_set_destructor(session, smbXcli_session_destructor);
5808 status = smb2_signing_key_sign_create(session->smb2,
5809 conn->smb2.server.sign_algo,
5810 NULL, /* no master key */
5811 NULL, /* derivations */
5812 &session->smb2->signing_key);
5813 if (!NT_STATUS_IS_OK(status)) {
5814 talloc_free(session);
5815 return NULL;
5818 DLIST_ADD_END(conn->sessions, session);
5819 session->conn = conn;
5821 status = smb2_signing_key_sign_create(session,
5822 conn->smb2.server.sign_algo,
5823 NULL, /* no master key */
5824 NULL, /* derivations */
5825 &session->smb2_channel.signing_key);
5826 if (!NT_STATUS_IS_OK(status)) {
5827 talloc_free(session);
5828 return NULL;
5831 memcpy(session->smb2_channel.preauth_sha512,
5832 conn->smb2.preauth_sha512,
5833 sizeof(session->smb2_channel.preauth_sha512));
5835 return session;
5838 struct smbXcli_session *smbXcli_session_shallow_copy(TALLOC_CTX *mem_ctx,
5839 struct smbXcli_session *src)
5841 struct smbXcli_session *session;
5842 struct timespec ts;
5843 NTTIME nt;
5845 session = talloc_zero(mem_ctx, struct smbXcli_session);
5846 if (session == NULL) {
5847 return NULL;
5849 session->smb2 = talloc_zero(session, struct smb2cli_session);
5850 if (session->smb2 == NULL) {
5851 talloc_free(session);
5852 return NULL;
5856 * Note we keep a pointer to the session keys of the
5857 * main session and rely on the caller to free the
5858 * shallow copy first!
5860 session->conn = src->conn;
5861 *session->smb2 = *src->smb2;
5862 session->smb2_channel = src->smb2_channel;
5863 session->disconnect_expired = src->disconnect_expired;
5866 * This is only supposed to be called in test code
5867 * but we should not reuse nonces!
5869 * Add the current timestamp as NTTIME to nonce_high
5870 * and set nonce_low to a value we can recognize in captures.
5872 clock_gettime_mono(&ts);
5873 nt = unix_timespec_to_nt_time(ts);
5874 nt &= session->smb2->nonce_high_max;
5875 if (nt == session->smb2->nonce_high_max || nt < UINT8_MAX) {
5876 talloc_free(session);
5877 return NULL;
5879 session->smb2->nonce_high += nt;
5880 session->smb2->nonce_low = UINT32_MAX;
5882 DLIST_ADD_END(src->conn->sessions, session);
5883 talloc_set_destructor(session, smbXcli_session_destructor);
5885 return session;
5888 bool smbXcli_session_is_guest(struct smbXcli_session *session)
5890 if (session == NULL) {
5891 return false;
5894 if (session->conn == NULL) {
5895 return false;
5898 if (session->conn->mandatory_signing) {
5899 return false;
5902 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5903 if (session->smb2->session_flags & SMB2_SESSION_FLAG_IS_GUEST) {
5904 return true;
5906 return false;
5909 if (session->smb1.action & SMB_SETUP_GUEST) {
5910 return true;
5913 return false;
5916 bool smbXcli_session_is_authenticated(struct smbXcli_session *session)
5918 const DATA_BLOB *application_key;
5920 if (session == NULL) {
5921 return false;
5924 if (session->conn == NULL) {
5925 return false;
5929 * If we have an application key we had a session key negotiated
5930 * at auth time.
5932 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
5933 if (!smb2_signing_key_valid(session->smb2->application_key)) {
5934 return false;
5936 application_key = &session->smb2->application_key->blob;
5937 } else {
5938 application_key = &session->smb1.application_key;
5941 if (application_key->length == 0) {
5942 return false;
5945 return true;
5948 NTSTATUS smb2cli_session_signing_key(struct smbXcli_session *session,
5949 TALLOC_CTX *mem_ctx,
5950 DATA_BLOB *key)
5952 const struct smb2_signing_key *sig = NULL;
5954 if (session->conn == NULL) {
5955 return NT_STATUS_NO_USER_SESSION_KEY;
5959 * Use channel signing key if there is one, otherwise fallback
5960 * to session.
5963 if (smb2_signing_key_valid(session->smb2_channel.signing_key)) {
5964 sig = session->smb2_channel.signing_key;
5965 } else if (smb2_signing_key_valid(session->smb2->signing_key)) {
5966 sig = session->smb2->signing_key;
5967 } else {
5968 return NT_STATUS_NO_USER_SESSION_KEY;
5971 *key = data_blob_dup_talloc(mem_ctx, sig->blob);
5972 if (key->data == NULL) {
5973 return NT_STATUS_NO_MEMORY;
5976 return NT_STATUS_OK;
5979 NTSTATUS smb2cli_session_encryption_key(struct smbXcli_session *session,
5980 TALLOC_CTX *mem_ctx,
5981 DATA_BLOB *key)
5983 if (session->conn == NULL) {
5984 return NT_STATUS_NO_USER_SESSION_KEY;
5987 if (session->conn->protocol < PROTOCOL_SMB3_00) {
5988 return NT_STATUS_NO_USER_SESSION_KEY;
5991 if (!smb2_signing_key_valid(session->smb2->encryption_key)) {
5992 return NT_STATUS_NO_USER_SESSION_KEY;
5995 *key = data_blob_dup_talloc(mem_ctx, session->smb2->encryption_key->blob);
5996 if (key->data == NULL) {
5997 return NT_STATUS_NO_MEMORY;
6000 return NT_STATUS_OK;
6003 NTSTATUS smb2cli_session_decryption_key(struct smbXcli_session *session,
6004 TALLOC_CTX *mem_ctx,
6005 DATA_BLOB *key)
6007 if (session->conn == NULL) {
6008 return NT_STATUS_NO_USER_SESSION_KEY;
6011 if (session->conn->protocol < PROTOCOL_SMB3_00) {
6012 return NT_STATUS_NO_USER_SESSION_KEY;
6015 if (!smb2_signing_key_valid(session->smb2->decryption_key)) {
6016 return NT_STATUS_NO_USER_SESSION_KEY;
6019 *key = data_blob_dup_talloc(mem_ctx, session->smb2->decryption_key->blob);
6020 if (key->data == NULL) {
6021 return NT_STATUS_NO_MEMORY;
6024 return NT_STATUS_OK;
6027 NTSTATUS smbXcli_session_application_key(struct smbXcli_session *session,
6028 TALLOC_CTX *mem_ctx,
6029 DATA_BLOB *key)
6031 const DATA_BLOB *application_key;
6033 *key = data_blob_null;
6035 if (session->conn == NULL) {
6036 return NT_STATUS_NO_USER_SESSION_KEY;
6039 if (session->conn->protocol >= PROTOCOL_SMB2_02) {
6040 if (!smb2_signing_key_valid(session->smb2->application_key)) {
6041 return NT_STATUS_NO_USER_SESSION_KEY;
6043 application_key = &session->smb2->application_key->blob;
6044 } else {
6045 application_key = &session->smb1.application_key;
6048 if (application_key->length == 0) {
6049 return NT_STATUS_NO_USER_SESSION_KEY;
6052 *key = data_blob_dup_talloc(mem_ctx, *application_key);
6053 if (key->data == NULL) {
6054 return NT_STATUS_NO_MEMORY;
6057 return NT_STATUS_OK;
6060 void smbXcli_session_set_disconnect_expired(struct smbXcli_session *session)
6062 session->disconnect_expired = true;
6065 uint16_t smb1cli_session_current_id(struct smbXcli_session *session)
6067 return session->smb1.session_id;
6070 void smb1cli_session_set_id(struct smbXcli_session *session,
6071 uint16_t session_id)
6073 session->smb1.session_id = session_id;
6076 void smb1cli_session_set_action(struct smbXcli_session *session,
6077 uint16_t action)
6079 session->smb1.action = action;
6082 NTSTATUS smb1cli_session_set_session_key(struct smbXcli_session *session,
6083 const DATA_BLOB _session_key)
6085 struct smbXcli_conn *conn = session->conn;
6086 uint8_t session_key[16];
6088 if (conn == NULL) {
6089 return NT_STATUS_INVALID_PARAMETER_MIX;
6092 if (session->smb1.application_key.length != 0) {
6094 * TODO: do not allow this...
6096 * return NT_STATUS_INVALID_PARAMETER_MIX;
6098 data_blob_clear_free(&session->smb1.application_key);
6099 session->smb1.protected_key = false;
6102 if (_session_key.length == 0) {
6103 return NT_STATUS_OK;
6106 ZERO_STRUCT(session_key);
6107 memcpy(session_key, _session_key.data,
6108 MIN(_session_key.length, sizeof(session_key)));
6110 session->smb1.application_key = data_blob_talloc(session,
6111 session_key,
6112 sizeof(session_key));
6113 ZERO_STRUCT(session_key);
6114 if (session->smb1.application_key.data == NULL) {
6115 return NT_STATUS_NO_MEMORY;
6118 session->smb1.protected_key = false;
6120 return NT_STATUS_OK;
6123 NTSTATUS smb1cli_session_protect_session_key(struct smbXcli_session *session)
6125 NTSTATUS status;
6127 if (session->smb1.protected_key) {
6128 /* already protected */
6129 return NT_STATUS_OK;
6132 if (session->smb1.application_key.length != 16) {
6133 return NT_STATUS_INVALID_PARAMETER_MIX;
6136 status = smb_key_derivation(session->smb1.application_key.data,
6137 session->smb1.application_key.length,
6138 session->smb1.application_key.data);
6139 if (!NT_STATUS_IS_OK(status)) {
6140 return status;
6143 session->smb1.protected_key = true;
6145 return NT_STATUS_OK;
6148 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
6150 struct smbXcli_conn *conn = session->conn;
6151 uint8_t security_mode = 0;
6153 if (conn == NULL) {
6154 return security_mode;
6157 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
6158 if (conn->mandatory_signing) {
6159 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
6161 if (session->smb2->should_sign) {
6162 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
6165 return security_mode;
6168 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
6170 return session->smb2->session_id;
6173 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
6175 return session->smb2->session_flags;
6178 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
6179 uint64_t session_id,
6180 uint16_t session_flags)
6182 session->smb2->session_id = session_id;
6183 session->smb2->session_flags = session_flags;
6186 void smb2cli_session_increment_channel_sequence(struct smbXcli_session *session)
6188 session->smb2->channel_sequence += 1;
6191 uint16_t smb2cli_session_reset_channel_sequence(struct smbXcli_session *session,
6192 uint16_t channel_sequence)
6194 uint16_t prev_cs;
6196 prev_cs = session->smb2->channel_sequence;
6197 session->smb2->channel_sequence = channel_sequence;
6199 return prev_cs;
6202 uint16_t smb2cli_session_current_channel_sequence(struct smbXcli_session *session)
6204 return session->smb2->channel_sequence;
6207 void smb2cli_session_start_replay(struct smbXcli_session *session)
6209 session->smb2->replay_active = true;
6212 void smb2cli_session_stop_replay(struct smbXcli_session *session)
6214 session->smb2->replay_active = false;
6217 void smb2cli_session_require_signed_response(struct smbXcli_session *session,
6218 bool require_signed_response)
6220 session->smb2->require_signed_response = require_signed_response;
6223 NTSTATUS smb2cli_session_update_preauth(struct smbXcli_session *session,
6224 const struct iovec *iov)
6226 gnutls_hash_hd_t hash_hnd = NULL;
6227 size_t i;
6228 int rc;
6230 if (session->conn == NULL) {
6231 return NT_STATUS_INTERNAL_ERROR;
6234 if (session->conn->protocol < PROTOCOL_SMB3_11) {
6235 return NT_STATUS_OK;
6238 if (smb2_signing_key_valid(session->smb2_channel.signing_key)) {
6239 return NT_STATUS_OK;
6242 rc = gnutls_hash_init(&hash_hnd,
6243 GNUTLS_DIG_SHA512);
6244 if (rc < 0) {
6245 return gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
6248 rc = gnutls_hash(hash_hnd,
6249 session->smb2_channel.preauth_sha512,
6250 sizeof(session->smb2_channel.preauth_sha512));
6251 if (rc < 0) {
6252 gnutls_hash_deinit(hash_hnd, NULL);
6253 return gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
6255 for (i = 0; i < 3; i++) {
6256 rc = gnutls_hash(hash_hnd,
6257 iov[i].iov_base,
6258 iov[i].iov_len);
6259 if (rc < 0) {
6260 gnutls_hash_deinit(hash_hnd, NULL);
6261 return gnutls_error_to_ntstatus(rc, NT_STATUS_HASH_NOT_SUPPORTED);
6264 gnutls_hash_deinit(hash_hnd, session->smb2_channel.preauth_sha512);
6266 return NT_STATUS_OK;
6269 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
6270 const DATA_BLOB _session_key,
6271 const struct iovec *recv_iov)
6273 struct smbXcli_conn *conn = session->conn;
6274 uint16_t no_sign_flags = 0;
6275 bool check_signature = true;
6276 uint32_t hdr_flags;
6277 NTSTATUS status;
6278 struct smb2_signing_derivations derivations = {
6279 .signing = NULL,
6281 DATA_BLOB preauth_hash = data_blob_null;
6282 size_t nonce_size = 0;
6284 if (conn == NULL) {
6285 return NT_STATUS_INVALID_PARAMETER_MIX;
6288 if (recv_iov[0].iov_len != SMB2_HDR_BODY) {
6289 return NT_STATUS_INVALID_PARAMETER_MIX;
6292 if (!conn->mandatory_signing) {
6294 * only allow guest sessions without
6295 * mandatory signing.
6297 * If we try an authentication with username != ""
6298 * and the server let us in without verifying the
6299 * password we don't have a negotiated session key
6300 * for signing.
6302 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST;
6305 if (session->smb2->session_flags & no_sign_flags) {
6306 session->smb2->should_sign = false;
6307 return NT_STATUS_OK;
6310 if (smb2_signing_key_valid(session->smb2->signing_key)) {
6311 return NT_STATUS_INVALID_PARAMETER_MIX;
6314 if (conn->protocol >= PROTOCOL_SMB3_11) {
6315 preauth_hash = data_blob_const(session->smb2_channel.preauth_sha512,
6316 sizeof(session->smb2_channel.preauth_sha512));
6319 smb2_signing_derivations_fill_const_stack(&derivations,
6320 conn->protocol,
6321 preauth_hash);
6323 status = smb2_signing_key_sign_create(session->smb2,
6324 conn->smb2.server.sign_algo,
6325 &_session_key,
6326 derivations.signing,
6327 &session->smb2->signing_key);
6328 if (!NT_STATUS_IS_OK(status)) {
6329 return status;
6332 status = smb2_signing_key_cipher_create(session->smb2,
6333 conn->smb2.server.cipher,
6334 &_session_key,
6335 derivations.cipher_c2s,
6336 &session->smb2->encryption_key);
6337 if (!NT_STATUS_IS_OK(status)) {
6338 return status;
6341 status = smb2_signing_key_cipher_create(session->smb2,
6342 conn->smb2.server.cipher,
6343 &_session_key,
6344 derivations.cipher_s2c,
6345 &session->smb2->decryption_key);
6346 if (!NT_STATUS_IS_OK(status)) {
6347 return status;
6350 status = smb2_signing_key_sign_create(session->smb2,
6351 conn->smb2.server.sign_algo,
6352 &_session_key,
6353 derivations.application,
6354 &session->smb2->application_key);
6355 if (!NT_STATUS_IS_OK(status)) {
6356 return status;
6359 status = smb2_signing_key_copy(session,
6360 session->smb2->signing_key,
6361 &session->smb2_channel.signing_key);
6362 if (!NT_STATUS_IS_OK(status)) {
6363 return status;
6366 check_signature = conn->mandatory_signing;
6368 hdr_flags = IVAL(recv_iov[0].iov_base, SMB2_HDR_FLAGS);
6369 if (hdr_flags & SMB2_HDR_FLAG_SIGNED) {
6371 * Sadly some vendors don't sign the
6372 * final SMB2 session setup response
6374 * At least Windows and Samba are always doing this
6375 * if there's a session key available.
6377 * We only check the signature if it's mandatory
6378 * or SMB2_HDR_FLAG_SIGNED is provided.
6380 check_signature = true;
6383 if (conn->protocol >= PROTOCOL_SMB3_11) {
6384 check_signature = true;
6387 if (check_signature) {
6388 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
6389 recv_iov, 3);
6390 if (!NT_STATUS_IS_OK(status)) {
6391 return status;
6395 session->smb2->should_sign = false;
6396 session->smb2->should_encrypt = false;
6398 if (conn->desire_signing) {
6399 session->smb2->should_sign = true;
6402 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
6403 session->smb2->should_sign = true;
6406 if (session->smb2->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) {
6407 session->smb2->should_encrypt = true;
6410 if (conn->protocol < PROTOCOL_SMB3_00) {
6411 session->smb2->should_encrypt = false;
6414 if (conn->smb2.server.cipher == 0) {
6415 session->smb2->should_encrypt = false;
6419 * CCM and GCM algorithms must never have their
6420 * nonce wrap, or the security of the whole
6421 * communication and the keys is destroyed.
6422 * We must drop the connection once we have
6423 * transfered too much data.
6425 * NOTE: We assume nonces greater than 8 bytes.
6427 generate_nonce_buffer((uint8_t *)&session->smb2->nonce_high_random,
6428 sizeof(session->smb2->nonce_high_random));
6429 switch (conn->smb2.server.cipher) {
6430 case SMB2_ENCRYPTION_AES128_CCM:
6431 nonce_size = SMB2_AES_128_CCM_NONCE_SIZE;
6432 break;
6433 case SMB2_ENCRYPTION_AES128_GCM:
6434 nonce_size = gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_128_GCM);
6435 break;
6436 case SMB2_ENCRYPTION_AES256_CCM:
6437 nonce_size = SMB2_AES_128_CCM_NONCE_SIZE;
6438 break;
6439 case SMB2_ENCRYPTION_AES256_GCM:
6440 nonce_size = gnutls_cipher_get_iv_size(GNUTLS_CIPHER_AES_256_GCM);
6441 break;
6442 default:
6443 nonce_size = 0;
6444 break;
6446 session->smb2->nonce_high_max = SMB2_NONCE_HIGH_MAX(nonce_size);
6447 session->smb2->nonce_high = 0;
6448 session->smb2->nonce_low = 0;
6450 return NT_STATUS_OK;
6453 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
6454 struct smbXcli_session *session1,
6455 struct smbXcli_conn *conn,
6456 struct smbXcli_session **_session2)
6458 struct smbXcli_session *session2;
6459 NTSTATUS status;
6461 if (!smb2_signing_key_valid(session1->smb2->signing_key)) {
6462 return NT_STATUS_INVALID_PARAMETER_MIX;
6465 if (conn == NULL) {
6466 return NT_STATUS_INVALID_PARAMETER_MIX;
6469 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
6470 if (session2 == NULL) {
6471 return NT_STATUS_NO_MEMORY;
6473 session2->smb2 = talloc_reference(session2, session1->smb2);
6474 if (session2->smb2 == NULL) {
6475 talloc_free(session2);
6476 return NT_STATUS_NO_MEMORY;
6479 talloc_set_destructor(session2, smbXcli_session_destructor);
6480 DLIST_ADD_END(conn->sessions, session2);
6481 session2->conn = conn;
6483 status = smb2_signing_key_sign_create(session2,
6484 conn->smb2.server.sign_algo,
6485 NULL, /* no master key */
6486 NULL, /* derivations */
6487 &session2->smb2_channel.signing_key);
6488 if (!NT_STATUS_IS_OK(status)) {
6489 talloc_free(session2);
6490 return NT_STATUS_NO_MEMORY;
6493 memcpy(session2->smb2_channel.preauth_sha512,
6494 conn->smb2.preauth_sha512,
6495 sizeof(session2->smb2_channel.preauth_sha512));
6497 *_session2 = session2;
6498 return NT_STATUS_OK;
6501 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
6502 const DATA_BLOB _channel_key,
6503 const struct iovec *recv_iov)
6505 struct smbXcli_conn *conn = session->conn;
6506 uint8_t channel_key[16];
6507 NTSTATUS status;
6508 struct _derivation {
6509 DATA_BLOB label;
6510 DATA_BLOB context;
6512 struct {
6513 struct _derivation signing;
6514 } derivation = {
6515 .signing.label.length = 0,
6518 if (conn == NULL) {
6519 return NT_STATUS_INVALID_PARAMETER_MIX;
6522 if (smb2_signing_key_valid(session->smb2_channel.signing_key)) {
6523 return NT_STATUS_INVALID_PARAMETER_MIX;
6526 if (conn->protocol >= PROTOCOL_SMB3_11) {
6527 struct _derivation *d;
6528 DATA_BLOB p;
6530 p = data_blob_const(session->smb2_channel.preauth_sha512,
6531 sizeof(session->smb2_channel.preauth_sha512));
6533 d = &derivation.signing;
6534 d->label = data_blob_string_const_null("SMBSigningKey");
6535 d->context = p;
6536 } else if (conn->protocol >= PROTOCOL_SMB3_00) {
6537 struct _derivation *d;
6539 d = &derivation.signing;
6540 d->label = data_blob_string_const_null("SMB2AESCMAC");
6541 d->context = data_blob_string_const_null("SmbSign");
6544 ZERO_STRUCT(channel_key);
6545 memcpy(channel_key, _channel_key.data,
6546 MIN(_channel_key.length, sizeof(channel_key)));
6548 session->smb2_channel.signing_key->blob =
6549 data_blob_talloc(session->smb2_channel.signing_key,
6550 channel_key,
6551 sizeof(channel_key));
6552 if (!smb2_signing_key_valid(session->smb2_channel.signing_key)) {
6553 ZERO_STRUCT(channel_key);
6554 return NT_STATUS_NO_MEMORY;
6557 if (conn->protocol >= PROTOCOL_SMB3_00) {
6558 struct _derivation *d = &derivation.signing;
6560 status = smb2_key_derivation(channel_key, sizeof(channel_key),
6561 d->label.data, d->label.length,
6562 d->context.data, d->context.length,
6563 session->smb2_channel.signing_key->blob.data,
6564 session->smb2_channel.signing_key->blob.length);
6565 if (!NT_STATUS_IS_OK(status)) {
6566 return status;
6569 ZERO_STRUCT(channel_key);
6571 status = smb2_signing_check_pdu(session->smb2_channel.signing_key,
6572 recv_iov, 3);
6573 if (!NT_STATUS_IS_OK(status)) {
6574 return status;
6577 return NT_STATUS_OK;
6580 NTSTATUS smb2cli_session_encryption_on(struct smbXcli_session *session)
6582 if (!session->smb2->should_sign) {
6584 * We need required signing on the session
6585 * in order to prevent man in the middle attacks.
6587 return NT_STATUS_INVALID_PARAMETER_MIX;
6590 if (session->smb2->should_encrypt) {
6591 return NT_STATUS_OK;
6594 if (session->conn->protocol < PROTOCOL_SMB3_00) {
6595 return NT_STATUS_NOT_SUPPORTED;
6598 if (session->conn->smb2.server.cipher == 0) {
6599 return NT_STATUS_NOT_SUPPORTED;
6602 if (!smb2_signing_key_valid(session->smb2->signing_key)) {
6603 return NT_STATUS_NOT_SUPPORTED;
6605 session->smb2->should_encrypt = true;
6606 return NT_STATUS_OK;
6609 uint16_t smb2cli_session_get_encryption_cipher(struct smbXcli_session *session)
6611 if (session->conn->protocol < PROTOCOL_SMB3_00) {
6612 return 0;
6615 if (!session->smb2->should_encrypt) {
6616 return 0;
6619 return session->conn->smb2.server.cipher;
6622 struct smbXcli_tcon *smbXcli_tcon_create(TALLOC_CTX *mem_ctx)
6624 struct smbXcli_tcon *tcon;
6626 tcon = talloc_zero(mem_ctx, struct smbXcli_tcon);
6627 if (tcon == NULL) {
6628 return NULL;
6631 return tcon;
6635 * Return a deep structure copy of a struct smbXcli_tcon *
6638 struct smbXcli_tcon *smbXcli_tcon_copy(TALLOC_CTX *mem_ctx,
6639 const struct smbXcli_tcon *tcon_in)
6641 struct smbXcli_tcon *tcon;
6643 tcon = talloc_memdup(mem_ctx, tcon_in, sizeof(struct smbXcli_tcon));
6644 if (tcon == NULL) {
6645 return NULL;
6648 /* Deal with the SMB1 strings. */
6649 if (tcon_in->smb1.service != NULL) {
6650 tcon->smb1.service = talloc_strdup(tcon, tcon_in->smb1.service);
6651 if (tcon->smb1.service == NULL) {
6652 TALLOC_FREE(tcon);
6653 return NULL;
6656 if (tcon->smb1.fs_type != NULL) {
6657 tcon->smb1.fs_type = talloc_strdup(tcon, tcon_in->smb1.fs_type);
6658 if (tcon->smb1.fs_type == NULL) {
6659 TALLOC_FREE(tcon);
6660 return NULL;
6663 return tcon;
6666 void smbXcli_tcon_set_fs_attributes(struct smbXcli_tcon *tcon,
6667 uint32_t fs_attributes)
6669 tcon->fs_attributes = fs_attributes;
6672 uint32_t smbXcli_tcon_get_fs_attributes(struct smbXcli_tcon *tcon)
6674 return tcon->fs_attributes;
6677 bool smbXcli_tcon_is_dfs_share(struct smbXcli_tcon *tcon)
6679 if (tcon == NULL) {
6680 return false;
6683 if (tcon->is_smb1) {
6684 if (tcon->smb1.optional_support & SMB_SHARE_IN_DFS) {
6685 return true;
6688 return false;
6691 if (tcon->smb2.capabilities & SMB2_SHARE_CAP_DFS) {
6692 return true;
6695 return false;
6698 uint16_t smb1cli_tcon_current_id(struct smbXcli_tcon *tcon)
6700 return tcon->smb1.tcon_id;
6703 void smb1cli_tcon_set_id(struct smbXcli_tcon *tcon, uint16_t tcon_id)
6705 tcon->is_smb1 = true;
6706 tcon->smb1.tcon_id = tcon_id;
6709 bool smb1cli_tcon_set_values(struct smbXcli_tcon *tcon,
6710 uint16_t tcon_id,
6711 uint16_t optional_support,
6712 uint32_t maximal_access,
6713 uint32_t guest_maximal_access,
6714 const char *service,
6715 const char *fs_type)
6717 tcon->is_smb1 = true;
6718 tcon->fs_attributes = 0;
6719 tcon->smb1.tcon_id = tcon_id;
6720 tcon->smb1.optional_support = optional_support;
6721 tcon->smb1.maximal_access = maximal_access;
6722 tcon->smb1.guest_maximal_access = guest_maximal_access;
6724 TALLOC_FREE(tcon->smb1.service);
6725 tcon->smb1.service = talloc_strdup(tcon, service);
6726 if (service != NULL && tcon->smb1.service == NULL) {
6727 return false;
6730 TALLOC_FREE(tcon->smb1.fs_type);
6731 tcon->smb1.fs_type = talloc_strdup(tcon, fs_type);
6732 if (fs_type != NULL && tcon->smb1.fs_type == NULL) {
6733 return false;
6736 return true;
6739 uint32_t smb2cli_tcon_current_id(struct smbXcli_tcon *tcon)
6741 return tcon->smb2.tcon_id;
6744 void smb2cli_tcon_set_id(struct smbXcli_tcon *tcon, uint32_t tcon_id)
6746 tcon->smb2.tcon_id = tcon_id;
6749 uint32_t smb2cli_tcon_capabilities(struct smbXcli_tcon *tcon)
6751 return tcon->smb2.capabilities;
6754 uint32_t smb2cli_tcon_flags(struct smbXcli_tcon *tcon)
6756 return tcon->smb2.flags;
6759 void smb2cli_tcon_set_values(struct smbXcli_tcon *tcon,
6760 struct smbXcli_session *session,
6761 uint32_t tcon_id,
6762 uint8_t type,
6763 uint32_t flags,
6764 uint32_t capabilities,
6765 uint32_t maximal_access)
6767 tcon->is_smb1 = false;
6768 tcon->fs_attributes = 0;
6769 tcon->smb2.tcon_id = tcon_id;
6770 tcon->smb2.type = type;
6771 tcon->smb2.flags = flags;
6772 tcon->smb2.capabilities = capabilities;
6773 tcon->smb2.maximal_access = maximal_access;
6775 tcon->smb2.should_sign = false;
6776 tcon->smb2.should_encrypt = false;
6778 if (session == NULL) {
6779 return;
6782 tcon->smb2.should_sign = session->smb2->should_sign;
6783 tcon->smb2.should_encrypt = session->smb2->should_encrypt;
6785 if (flags & SMB2_SHAREFLAG_ENCRYPT_DATA) {
6786 tcon->smb2.should_encrypt = true;
6790 void smb2cli_tcon_should_sign(struct smbXcli_tcon *tcon,
6791 bool should_sign)
6793 tcon->smb2.should_sign = should_sign;
6796 bool smb2cli_tcon_is_signing_on(struct smbXcli_tcon *tcon)
6798 if (tcon->smb2.should_encrypt) {
6799 return true;
6802 return tcon->smb2.should_sign;
6805 void smb2cli_tcon_should_encrypt(struct smbXcli_tcon *tcon,
6806 bool should_encrypt)
6808 tcon->smb2.should_encrypt = should_encrypt;
6811 bool smb2cli_tcon_is_encryption_on(struct smbXcli_tcon *tcon)
6813 return tcon->smb2.should_encrypt;
6816 void smb2cli_conn_set_mid(struct smbXcli_conn *conn, uint64_t mid)
6818 conn->smb2.mid = mid;
6821 uint64_t smb2cli_conn_get_mid(struct smbXcli_conn *conn)
6823 return conn->smb2.mid;