libcli/smb: maintain smb2.should_sign on smbXcli_req_state
[Samba/gebeck_regimport.git] / libcli / smb / smbXcli_base.c
blob8a41824ef96d233aa91099304102e7ebb6830a24
1 /*
2 Unix SMB/CIFS implementation.
3 Infrastructure for async SMB client requests
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Stefan Metzmacher 2011
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/network.h"
23 #include "../lib/async_req/async_sock.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "../lib/util/tevent_unix.h"
26 #include "lib/util/util_net.h"
27 #include "lib/util/dlinklist.h"
28 #include "../libcli/smb/smb_common.h"
29 #include "../libcli/smb/smb_seal.h"
30 #include "../libcli/smb/smb_signing.h"
31 #include "../libcli/smb/read_smb.h"
32 #include "smbXcli_base.h"
33 #include "librpc/ndr/libndr.h"
35 struct smbXcli_conn;
36 struct smbXcli_req;
37 struct smbXcli_session;
39 struct smbXcli_conn {
40 int read_fd;
41 int write_fd;
42 struct sockaddr_storage local_ss;
43 struct sockaddr_storage remote_ss;
44 const char *remote_name;
46 struct tevent_queue *outgoing;
47 struct tevent_req **pending;
48 struct tevent_req *read_smb_req;
50 enum protocol_types protocol;
51 bool allow_signing;
52 bool desire_signing;
53 bool mandatory_signing;
56 * The incoming dispatch function should return:
57 * - NT_STATUS_RETRY, if more incoming PDUs are expected.
58 * - NT_STATUS_OK, if no more processing is desired, e.g.
59 * the dispatch function called
60 * tevent_req_done().
61 * - All other return values disconnect the connection.
63 NTSTATUS (*dispatch_incoming)(struct smbXcli_conn *conn,
64 TALLOC_CTX *tmp_mem,
65 uint8_t *inbuf);
67 struct {
68 struct {
69 uint32_t capabilities;
70 uint32_t max_xmit;
71 } client;
73 struct {
74 uint32_t capabilities;
75 uint32_t max_xmit;
76 uint16_t max_mux;
77 uint16_t security_mode;
78 bool readbraw;
79 bool writebraw;
80 bool lockread;
81 bool writeunlock;
82 uint32_t session_key;
83 struct GUID guid;
84 DATA_BLOB gss_blob;
85 uint8_t challenge[8];
86 const char *workgroup;
87 const char *name;
88 int time_zone;
89 NTTIME system_time;
90 } server;
92 uint32_t capabilities;
93 uint32_t max_xmit;
95 uint16_t mid;
97 struct smb_signing_state *signing;
98 struct smb_trans_enc_state *trans_enc;
100 struct tevent_req *read_braw_req;
101 } smb1;
103 struct {
104 struct {
105 uint32_t capabilities;
106 uint16_t security_mode;
107 struct GUID guid;
108 } client;
110 struct {
111 uint32_t capabilities;
112 uint16_t security_mode;
113 struct GUID guid;
114 uint32_t max_trans_size;
115 uint32_t max_read_size;
116 uint32_t max_write_size;
117 NTTIME system_time;
118 NTTIME start_time;
119 DATA_BLOB gss_blob;
120 } server;
122 uint64_t mid;
123 uint16_t cur_credits;
124 uint16_t max_credits;
125 } smb2;
127 struct smbXcli_session *sessions;
130 struct smbXcli_session {
131 struct smbXcli_session *prev, *next;
132 struct smbXcli_conn *conn;
134 struct {
135 uint64_t session_id;
136 uint16_t session_flags;
137 DATA_BLOB application_key;
138 DATA_BLOB signing_key;
139 bool should_sign;
140 bool should_encrypt;
141 DATA_BLOB encryption_key;
142 DATA_BLOB decryption_key;
143 uint64_t channel_nonce;
144 uint64_t channel_next;
145 DATA_BLOB channel_signing_key;
146 } smb2;
149 struct smbXcli_req_state {
150 struct tevent_context *ev;
151 struct smbXcli_conn *conn;
152 struct smbXcli_session *session; /* maybe NULL */
154 uint8_t length_hdr[4];
156 bool one_way;
158 uint8_t *inbuf;
160 struct {
161 /* Space for the header including the wct */
162 uint8_t hdr[HDR_VWV];
165 * For normal requests, smb1cli_req_send chooses a mid.
166 * SecondaryV trans requests need to use the mid of the primary
167 * request, so we need a place to store it.
168 * Assume it is set if != 0.
170 uint16_t mid;
172 uint16_t *vwv;
173 uint8_t bytecount_buf[2];
175 #define MAX_SMB_IOV 10
176 /* length_hdr, hdr, words, byte_count, buffers */
177 struct iovec iov[1 + 3 + MAX_SMB_IOV];
178 int iov_count;
180 bool one_way_seqnum;
181 uint32_t seqnum;
182 struct tevent_req **chained_requests;
184 uint8_t recv_cmd;
185 NTSTATUS recv_status;
186 /* always an array of 3 talloc elements */
187 struct iovec *recv_iov;
188 } smb1;
190 struct {
191 const uint8_t *fixed;
192 uint16_t fixed_len;
193 const uint8_t *dyn;
194 uint32_t dyn_len;
196 uint8_t hdr[SMB2_HDR_BODY];
197 uint8_t pad[7]; /* padding space for compounding */
200 * always an array of 3 talloc elements
201 * (without a SMB2_TRANSFORM header!)
203 * HDR, BODY, DYN
205 struct iovec *recv_iov;
207 uint16_t credit_charge;
209 bool should_sign;
211 bool signing_skipped;
212 bool notify_async;
213 bool got_async;
214 } smb2;
217 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
220 * NT_STATUS_OK, means we do not notify the callers
222 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
224 while (conn->sessions) {
225 conn->sessions->conn = NULL;
226 DLIST_REMOVE(conn->sessions, conn->sessions);
229 if (conn->smb1.trans_enc) {
230 TALLOC_FREE(conn->smb1.trans_enc);
233 return 0;
236 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
237 int fd,
238 const char *remote_name,
239 enum smb_signing_setting signing_state,
240 uint32_t smb1_capabilities,
241 struct GUID *client_guid,
242 uint32_t smb2_capabilities)
244 struct smbXcli_conn *conn = NULL;
245 void *ss = NULL;
246 struct sockaddr *sa = NULL;
247 socklen_t sa_length;
248 int ret;
250 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
251 if (!conn) {
252 return NULL;
255 conn->read_fd = fd;
256 conn->write_fd = dup(fd);
257 if (conn->write_fd == -1) {
258 goto error;
261 conn->remote_name = talloc_strdup(conn, remote_name);
262 if (conn->remote_name == NULL) {
263 goto error;
267 ss = (void *)&conn->local_ss;
268 sa = (struct sockaddr *)ss;
269 sa_length = sizeof(conn->local_ss);
270 ret = getsockname(fd, sa, &sa_length);
271 if (ret == -1) {
272 goto error;
274 ss = (void *)&conn->remote_ss;
275 sa = (struct sockaddr *)ss;
276 sa_length = sizeof(conn->remote_ss);
277 ret = getpeername(fd, sa, &sa_length);
278 if (ret == -1) {
279 goto error;
282 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
283 if (conn->outgoing == NULL) {
284 goto error;
286 conn->pending = NULL;
288 conn->protocol = PROTOCOL_NONE;
290 switch (signing_state) {
291 case SMB_SIGNING_OFF:
292 /* never */
293 conn->allow_signing = false;
294 conn->desire_signing = false;
295 conn->mandatory_signing = false;
296 break;
297 case SMB_SIGNING_DEFAULT:
298 case SMB_SIGNING_IF_REQUIRED:
299 /* if the server requires it */
300 conn->allow_signing = true;
301 conn->desire_signing = false;
302 conn->mandatory_signing = false;
303 break;
304 case SMB_SIGNING_REQUIRED:
305 /* always */
306 conn->allow_signing = true;
307 conn->desire_signing = true;
308 conn->mandatory_signing = true;
309 break;
312 conn->smb1.client.capabilities = smb1_capabilities;
313 conn->smb1.client.max_xmit = UINT16_MAX;
315 conn->smb1.capabilities = conn->smb1.client.capabilities;
316 conn->smb1.max_xmit = 1024;
318 conn->smb1.mid = 1;
320 /* initialise signing */
321 conn->smb1.signing = smb_signing_init(conn,
322 conn->allow_signing,
323 conn->desire_signing,
324 conn->mandatory_signing);
325 if (!conn->smb1.signing) {
326 goto error;
329 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
330 if (conn->mandatory_signing) {
331 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
333 if (client_guid) {
334 conn->smb2.client.guid = *client_guid;
336 conn->smb2.client.capabilities = smb2_capabilities;
338 conn->smb2.cur_credits = 1;
339 conn->smb2.max_credits = 0;
341 talloc_set_destructor(conn, smbXcli_conn_destructor);
342 return conn;
344 error:
345 if (conn->write_fd != -1) {
346 close(conn->write_fd);
348 TALLOC_FREE(conn);
349 return NULL;
352 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
354 if (conn == NULL) {
355 return false;
358 if (conn->read_fd == -1) {
359 return false;
362 return true;
365 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
367 return conn->protocol;
370 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
372 if (conn->protocol >= PROTOCOL_SMB2_02) {
373 return true;
376 if (conn->smb1.capabilities & CAP_UNICODE) {
377 return true;
380 return false;
383 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
385 set_socket_options(conn->read_fd, options);
388 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
390 return &conn->local_ss;
393 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
395 return &conn->remote_ss;
398 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
400 return conn->remote_name;
403 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
405 if (conn->protocol >= PROTOCOL_SMB2_02) {
407 * TODO...
409 return 1;
412 return conn->smb1.server.max_mux;
415 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
417 if (conn->protocol >= PROTOCOL_SMB2_02) {
418 return conn->smb2.server.system_time;
421 return conn->smb1.server.system_time;
424 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
426 if (conn->protocol >= PROTOCOL_SMB2_02) {
427 return &conn->smb2.server.gss_blob;
430 return &conn->smb1.server.gss_blob;
433 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
435 if (conn->protocol >= PROTOCOL_SMB2_02) {
436 return &conn->smb2.server.guid;
439 return &conn->smb1.server.guid;
442 struct smbXcli_conn_samba_suicide_state {
443 struct smbXcli_conn *conn;
444 struct iovec iov;
445 uint8_t buf[9];
448 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
450 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
451 struct tevent_context *ev,
452 struct smbXcli_conn *conn,
453 uint8_t exitcode)
455 struct tevent_req *req, *subreq;
456 struct smbXcli_conn_samba_suicide_state *state;
458 req = tevent_req_create(mem_ctx, &state,
459 struct smbXcli_conn_samba_suicide_state);
460 if (req == NULL) {
461 return NULL;
463 state->conn = conn;
464 SIVAL(state->buf, 4, 0x74697865);
465 SCVAL(state->buf, 8, exitcode);
466 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
468 state->iov.iov_base = state->buf;
469 state->iov.iov_len = sizeof(state->buf);
471 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
472 false, &state->iov, 1);
473 if (tevent_req_nomem(subreq, req)) {
474 return tevent_req_post(req, ev);
476 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
477 return req;
480 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
482 struct tevent_req *req = tevent_req_callback_data(
483 subreq, struct tevent_req);
484 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
485 req, struct smbXcli_conn_samba_suicide_state);
486 ssize_t nwritten;
487 int err;
489 nwritten = writev_recv(subreq, &err);
490 TALLOC_FREE(subreq);
491 if (nwritten == -1) {
492 NTSTATUS status = map_nt_error_from_unix_common(err);
493 smbXcli_conn_disconnect(state->conn, status);
494 return;
496 tevent_req_done(req);
499 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
501 return tevent_req_simple_recv_ntstatus(req);
504 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
505 uint8_t exitcode)
507 TALLOC_CTX *frame = talloc_stackframe();
508 struct tevent_context *ev;
509 struct tevent_req *req;
510 NTSTATUS status = NT_STATUS_NO_MEMORY;
511 bool ok;
513 if (smbXcli_conn_has_async_calls(conn)) {
515 * Can't use sync call while an async call is in flight
517 status = NT_STATUS_INVALID_PARAMETER_MIX;
518 goto fail;
520 ev = tevent_context_init(frame);
521 if (ev == NULL) {
522 goto fail;
524 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
525 if (req == NULL) {
526 goto fail;
528 ok = tevent_req_poll(req, ev);
529 if (!ok) {
530 status = map_nt_error_from_unix_common(errno);
531 goto fail;
533 status = smbXcli_conn_samba_suicide_recv(req);
534 fail:
535 TALLOC_FREE(frame);
536 return status;
539 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
541 return conn->smb1.capabilities;
544 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
546 return conn->smb1.max_xmit;
549 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
551 return conn->smb1.server.session_key;
554 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
556 return conn->smb1.server.challenge;
559 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
561 return conn->smb1.server.security_mode;
564 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
566 return conn->smb1.server.readbraw;
569 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
571 return conn->smb1.server.writebraw;
574 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
576 return conn->smb1.server.lockread;
579 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
581 return conn->smb1.server.writeunlock;
584 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
586 return conn->smb1.server.time_zone;
589 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
590 const DATA_BLOB user_session_key,
591 const DATA_BLOB response)
593 return smb_signing_activate(conn->smb1.signing,
594 user_session_key,
595 response);
598 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
599 const uint8_t *buf, uint32_t seqnum)
601 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
604 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
606 return smb_signing_is_active(conn->smb1.signing);
609 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
610 struct smb_trans_enc_state *es)
612 /* Replace the old state, if any. */
613 if (conn->smb1.trans_enc) {
614 TALLOC_FREE(conn->smb1.trans_enc);
616 conn->smb1.trans_enc = es;
619 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
621 return common_encryption_on(conn->smb1.trans_enc);
625 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
627 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
628 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
630 if (NT_STATUS_IS_OK(status)) {
631 return NT_STATUS_OK;
634 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
635 return status;
638 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
642 * Is the SMB command able to hold an AND_X successor
643 * @param[in] cmd The SMB command in question
644 * @retval Can we add a chained request after "cmd"?
646 bool smb1cli_is_andx_req(uint8_t cmd)
648 switch (cmd) {
649 case SMBtconX:
650 case SMBlockingX:
651 case SMBopenX:
652 case SMBreadX:
653 case SMBwriteX:
654 case SMBsesssetupX:
655 case SMBulogoffX:
656 case SMBntcreateX:
657 return true;
658 break;
659 default:
660 break;
663 return false;
666 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
668 size_t num_pending = talloc_array_length(conn->pending);
669 uint16_t result;
671 while (true) {
672 size_t i;
674 result = conn->smb1.mid++;
675 if ((result == 0) || (result == 0xffff)) {
676 continue;
679 for (i=0; i<num_pending; i++) {
680 if (result == smb1cli_req_mid(conn->pending[i])) {
681 break;
685 if (i == num_pending) {
686 return result;
691 void smbXcli_req_unset_pending(struct tevent_req *req)
693 struct smbXcli_req_state *state =
694 tevent_req_data(req,
695 struct smbXcli_req_state);
696 struct smbXcli_conn *conn = state->conn;
697 size_t num_pending = talloc_array_length(conn->pending);
698 size_t i;
700 if (state->smb1.mid != 0) {
702 * This is a [nt]trans[2] request which waits
703 * for more than one reply.
705 return;
708 talloc_set_destructor(req, NULL);
710 if (num_pending == 1) {
712 * The pending read_smb tevent_req is a child of
713 * conn->pending. So if nothing is pending anymore, we need to
714 * delete the socket read fde.
716 TALLOC_FREE(conn->pending);
717 conn->read_smb_req = NULL;
718 return;
721 for (i=0; i<num_pending; i++) {
722 if (req == conn->pending[i]) {
723 break;
726 if (i == num_pending) {
728 * Something's seriously broken. Just returning here is the
729 * right thing nevertheless, the point of this routine is to
730 * remove ourselves from conn->pending.
732 return;
736 * Remove ourselves from the conn->pending array
738 for (; i < (num_pending - 1); i++) {
739 conn->pending[i] = conn->pending[i+1];
743 * No NULL check here, we're shrinking by sizeof(void *), and
744 * talloc_realloc just adjusts the size for this.
746 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
747 num_pending - 1);
748 return;
751 static int smbXcli_req_destructor(struct tevent_req *req)
753 struct smbXcli_req_state *state =
754 tevent_req_data(req,
755 struct smbXcli_req_state);
758 * Make sure we really remove it from
759 * the pending array on destruction.
761 state->smb1.mid = 0;
762 smbXcli_req_unset_pending(req);
763 return 0;
766 static bool smb1cli_req_cancel(struct tevent_req *req);
767 static bool smb2cli_req_cancel(struct tevent_req *req);
769 static bool smbXcli_req_cancel(struct tevent_req *req)
771 struct smbXcli_req_state *state =
772 tevent_req_data(req,
773 struct smbXcli_req_state);
775 if (!smbXcli_conn_is_connected(state->conn)) {
776 return false;
779 if (state->conn->protocol == PROTOCOL_NONE) {
780 return false;
783 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
784 return smb2cli_req_cancel(req);
787 return smb1cli_req_cancel(req);
790 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
792 bool smbXcli_req_set_pending(struct tevent_req *req)
794 struct smbXcli_req_state *state =
795 tevent_req_data(req,
796 struct smbXcli_req_state);
797 struct smbXcli_conn *conn;
798 struct tevent_req **pending;
799 size_t num_pending;
801 conn = state->conn;
803 if (!smbXcli_conn_is_connected(conn)) {
804 return false;
807 num_pending = talloc_array_length(conn->pending);
809 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
810 num_pending+1);
811 if (pending == NULL) {
812 return false;
814 pending[num_pending] = req;
815 conn->pending = pending;
816 talloc_set_destructor(req, smbXcli_req_destructor);
817 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
819 if (!smbXcli_conn_receive_next(conn)) {
821 * the caller should notify the current request
823 * And all other pending requests get notified
824 * by smbXcli_conn_disconnect().
826 smbXcli_req_unset_pending(req);
827 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
828 return false;
831 return true;
834 static void smbXcli_conn_received(struct tevent_req *subreq);
836 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
838 size_t num_pending = talloc_array_length(conn->pending);
839 struct tevent_req *req;
840 struct smbXcli_req_state *state;
842 if (conn->read_smb_req != NULL) {
843 return true;
846 if (num_pending == 0) {
847 if (conn->smb2.mid < UINT64_MAX) {
848 /* no more pending requests, so we are done for now */
849 return true;
853 * If there are no more SMB2 requests possible,
854 * because we are out of message ids,
855 * we need to disconnect.
857 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
858 return true;
861 req = conn->pending[0];
862 state = tevent_req_data(req, struct smbXcli_req_state);
865 * We're the first ones, add the read_smb request that waits for the
866 * answer from the server
868 conn->read_smb_req = read_smb_send(conn->pending,
869 state->ev,
870 conn->read_fd);
871 if (conn->read_smb_req == NULL) {
872 return false;
874 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
875 return true;
878 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
880 tevent_queue_stop(conn->outgoing);
882 if (conn->read_fd != -1) {
883 close(conn->read_fd);
885 if (conn->write_fd != -1) {
886 close(conn->write_fd);
888 conn->read_fd = -1;
889 conn->write_fd = -1;
892 * Cancel all pending requests. We do not do a for-loop walking
893 * conn->pending because that array changes in
894 * smbXcli_req_unset_pending.
896 while (talloc_array_length(conn->pending) > 0) {
897 struct tevent_req *req;
898 struct smbXcli_req_state *state;
899 struct tevent_req **chain;
900 size_t num_chained;
901 size_t i;
903 req = conn->pending[0];
904 state = tevent_req_data(req, struct smbXcli_req_state);
906 if (state->smb1.chained_requests == NULL) {
908 * We're dead. No point waiting for trans2
909 * replies.
911 state->smb1.mid = 0;
913 smbXcli_req_unset_pending(req);
915 if (NT_STATUS_IS_OK(status)) {
916 /* do not notify the callers */
917 continue;
921 * we need to defer the callback, because we may notify
922 * more then one caller.
924 tevent_req_defer_callback(req, state->ev);
925 tevent_req_nterror(req, status);
926 continue;
929 chain = talloc_move(conn, &state->smb1.chained_requests);
930 num_chained = talloc_array_length(chain);
932 for (i=0; i<num_chained; i++) {
933 req = chain[i];
934 state = tevent_req_data(req, struct smbXcli_req_state);
937 * We're dead. No point waiting for trans2
938 * replies.
940 state->smb1.mid = 0;
942 smbXcli_req_unset_pending(req);
944 if (NT_STATUS_IS_OK(status)) {
945 /* do not notify the callers */
946 continue;
950 * we need to defer the callback, because we may notify
951 * more than one caller.
953 tevent_req_defer_callback(req, state->ev);
954 tevent_req_nterror(req, status);
956 TALLOC_FREE(chain);
961 * Fetch a smb request's mid. Only valid after the request has been sent by
962 * smb1cli_req_send().
964 uint16_t smb1cli_req_mid(struct tevent_req *req)
966 struct smbXcli_req_state *state =
967 tevent_req_data(req,
968 struct smbXcli_req_state);
970 if (state->smb1.mid != 0) {
971 return state->smb1.mid;
974 return SVAL(state->smb1.hdr, HDR_MID);
977 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
979 struct smbXcli_req_state *state =
980 tevent_req_data(req,
981 struct smbXcli_req_state);
983 state->smb1.mid = mid;
986 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
988 struct smbXcli_req_state *state =
989 tevent_req_data(req,
990 struct smbXcli_req_state);
992 return state->smb1.seqnum;
995 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
997 struct smbXcli_req_state *state =
998 tevent_req_data(req,
999 struct smbXcli_req_state);
1001 state->smb1.seqnum = seqnum;
1004 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1006 size_t result = 0;
1007 int i;
1008 for (i=0; i<count; i++) {
1009 result += iov[i].iov_len;
1011 return result;
1014 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1015 const struct iovec *iov,
1016 int count)
1018 size_t len = smbXcli_iov_len(iov, count);
1019 size_t copied;
1020 uint8_t *buf;
1021 int i;
1023 buf = talloc_array(mem_ctx, uint8_t, len);
1024 if (buf == NULL) {
1025 return NULL;
1027 copied = 0;
1028 for (i=0; i<count; i++) {
1029 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1030 copied += iov[i].iov_len;
1032 return buf;
1035 static void smb1cli_req_flags(enum protocol_types protocol,
1036 uint32_t smb1_capabilities,
1037 uint8_t smb_command,
1038 uint8_t additional_flags,
1039 uint8_t clear_flags,
1040 uint8_t *_flags,
1041 uint16_t additional_flags2,
1042 uint16_t clear_flags2,
1043 uint16_t *_flags2)
1045 uint8_t flags = 0;
1046 uint16_t flags2 = 0;
1048 if (protocol >= PROTOCOL_LANMAN1) {
1049 flags |= FLAG_CASELESS_PATHNAMES;
1050 flags |= FLAG_CANONICAL_PATHNAMES;
1053 if (protocol >= PROTOCOL_LANMAN2) {
1054 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1055 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1058 if (protocol >= PROTOCOL_NT1) {
1059 flags2 |= FLAGS2_IS_LONG_NAME;
1061 if (smb1_capabilities & CAP_UNICODE) {
1062 flags2 |= FLAGS2_UNICODE_STRINGS;
1064 if (smb1_capabilities & CAP_STATUS32) {
1065 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1067 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1068 flags2 |= FLAGS2_EXTENDED_SECURITY;
1072 flags |= additional_flags;
1073 flags &= ~clear_flags;
1074 flags2 |= additional_flags2;
1075 flags2 &= ~clear_flags2;
1077 *_flags = flags;
1078 *_flags2 = flags2;
1081 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1083 static bool smb1cli_req_cancel(struct tevent_req *req)
1085 struct smbXcli_req_state *state =
1086 tevent_req_data(req,
1087 struct smbXcli_req_state);
1088 uint8_t flags;
1089 uint16_t flags2;
1090 uint32_t pid;
1091 uint16_t tid;
1092 uint16_t uid;
1093 uint16_t mid;
1094 struct tevent_req *subreq;
1095 NTSTATUS status;
1097 flags = CVAL(state->smb1.hdr, HDR_FLG);
1098 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1099 pid = SVAL(state->smb1.hdr, HDR_PID);
1100 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1101 tid = SVAL(state->smb1.hdr, HDR_TID);
1102 uid = SVAL(state->smb1.hdr, HDR_UID);
1103 mid = SVAL(state->smb1.hdr, HDR_MID);
1105 subreq = smb1cli_req_create(state, state->ev,
1106 state->conn,
1107 SMBntcancel,
1108 flags, 0,
1109 flags2, 0,
1110 0, /* timeout */
1111 pid, tid, uid,
1112 0, NULL, /* vwv */
1113 0, NULL); /* bytes */
1114 if (subreq == NULL) {
1115 return false;
1117 smb1cli_req_set_mid(subreq, mid);
1119 status = smb1cli_req_chain_submit(&subreq, 1);
1120 if (!NT_STATUS_IS_OK(status)) {
1121 TALLOC_FREE(subreq);
1122 return false;
1124 smb1cli_req_set_mid(subreq, 0);
1126 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1128 return true;
1131 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1133 /* we do not care about the result */
1134 TALLOC_FREE(subreq);
1137 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1138 struct tevent_context *ev,
1139 struct smbXcli_conn *conn,
1140 uint8_t smb_command,
1141 uint8_t additional_flags,
1142 uint8_t clear_flags,
1143 uint16_t additional_flags2,
1144 uint16_t clear_flags2,
1145 uint32_t timeout_msec,
1146 uint32_t pid,
1147 uint16_t tid,
1148 uint16_t uid,
1149 uint8_t wct, uint16_t *vwv,
1150 int iov_count,
1151 struct iovec *bytes_iov)
1153 struct tevent_req *req;
1154 struct smbXcli_req_state *state;
1155 uint8_t flags = 0;
1156 uint16_t flags2 = 0;
1158 if (iov_count > MAX_SMB_IOV) {
1160 * Should not happen :-)
1162 return NULL;
1165 req = tevent_req_create(mem_ctx, &state,
1166 struct smbXcli_req_state);
1167 if (req == NULL) {
1168 return NULL;
1170 state->ev = ev;
1171 state->conn = conn;
1173 state->smb1.recv_cmd = 0xFF;
1174 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1175 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1176 if (state->smb1.recv_iov == NULL) {
1177 TALLOC_FREE(req);
1178 return NULL;
1181 smb1cli_req_flags(conn->protocol,
1182 conn->smb1.capabilities,
1183 smb_command,
1184 additional_flags,
1185 clear_flags,
1186 &flags,
1187 additional_flags2,
1188 clear_flags2,
1189 &flags2);
1191 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1192 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1193 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1194 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1195 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1196 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1197 SSVAL(state->smb1.hdr, HDR_TID, tid);
1198 SSVAL(state->smb1.hdr, HDR_PID, pid);
1199 SSVAL(state->smb1.hdr, HDR_UID, uid);
1200 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1201 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1203 state->smb1.vwv = vwv;
1205 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1207 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1208 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1209 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1210 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1211 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1212 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1213 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1214 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1216 if (iov_count != 0) {
1217 memcpy(&state->smb1.iov[4], bytes_iov,
1218 iov_count * sizeof(*bytes_iov));
1220 state->smb1.iov_count = iov_count + 4;
1222 if (timeout_msec > 0) {
1223 struct timeval endtime;
1225 endtime = timeval_current_ofs_msec(timeout_msec);
1226 if (!tevent_req_set_endtime(req, ev, endtime)) {
1227 return req;
1231 switch (smb_command) {
1232 case SMBtranss:
1233 case SMBtranss2:
1234 case SMBnttranss:
1235 state->one_way = true;
1236 break;
1237 case SMBntcancel:
1238 state->one_way = true;
1239 state->smb1.one_way_seqnum = true;
1240 break;
1241 case SMBlockingX:
1242 if ((wct == 8) &&
1243 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1244 state->one_way = true;
1246 break;
1249 return req;
1252 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1253 struct iovec *iov, int iov_count,
1254 uint32_t *seqnum,
1255 bool one_way_seqnum)
1257 TALLOC_CTX *frame = NULL;
1258 uint8_t *buf;
1261 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1262 * iovec directly. MD5Update would do that just fine.
1265 if (iov_count < 4) {
1266 return NT_STATUS_INVALID_PARAMETER_MIX;
1268 if (iov[0].iov_len != NBT_HDR_SIZE) {
1269 return NT_STATUS_INVALID_PARAMETER_MIX;
1271 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1272 return NT_STATUS_INVALID_PARAMETER_MIX;
1274 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1275 return NT_STATUS_INVALID_PARAMETER_MIX;
1277 if (iov[3].iov_len != sizeof(uint16_t)) {
1278 return NT_STATUS_INVALID_PARAMETER_MIX;
1281 frame = talloc_stackframe();
1283 buf = smbXcli_iov_concat(frame, iov, iov_count);
1284 if (buf == NULL) {
1285 return NT_STATUS_NO_MEMORY;
1288 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1289 one_way_seqnum);
1290 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1291 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1293 TALLOC_FREE(frame);
1294 return NT_STATUS_OK;
1297 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1298 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1299 TALLOC_CTX *tmp_mem,
1300 uint8_t *inbuf);
1302 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1303 struct smbXcli_req_state *state,
1304 struct iovec *iov, int iov_count)
1306 struct tevent_req *subreq;
1307 NTSTATUS status;
1308 uint8_t cmd;
1309 uint16_t mid;
1311 if (!smbXcli_conn_is_connected(state->conn)) {
1312 return NT_STATUS_CONNECTION_DISCONNECTED;
1315 if (state->conn->protocol > PROTOCOL_NT1) {
1316 return NT_STATUS_REVISION_MISMATCH;
1319 if (iov_count < 4) {
1320 return NT_STATUS_INVALID_PARAMETER_MIX;
1322 if (iov[0].iov_len != NBT_HDR_SIZE) {
1323 return NT_STATUS_INVALID_PARAMETER_MIX;
1325 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1326 return NT_STATUS_INVALID_PARAMETER_MIX;
1328 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1329 return NT_STATUS_INVALID_PARAMETER_MIX;
1331 if (iov[3].iov_len != sizeof(uint16_t)) {
1332 return NT_STATUS_INVALID_PARAMETER_MIX;
1335 cmd = CVAL(iov[1].iov_base, HDR_COM);
1336 if (cmd == SMBreadBraw) {
1337 if (smbXcli_conn_has_async_calls(state->conn)) {
1338 return NT_STATUS_INVALID_PARAMETER_MIX;
1340 state->conn->smb1.read_braw_req = req;
1343 if (state->smb1.mid != 0) {
1344 mid = state->smb1.mid;
1345 } else {
1346 mid = smb1cli_alloc_mid(state->conn);
1348 SSVAL(iov[1].iov_base, HDR_MID, mid);
1350 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1352 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1353 &state->smb1.seqnum,
1354 state->smb1.one_way_seqnum);
1356 if (!NT_STATUS_IS_OK(status)) {
1357 return status;
1361 * If we supported multiple encrytion contexts
1362 * here we'd look up based on tid.
1364 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1365 char *buf, *enc_buf;
1367 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1368 if (buf == NULL) {
1369 return NT_STATUS_NO_MEMORY;
1371 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1372 (char *)buf, &enc_buf);
1373 TALLOC_FREE(buf);
1374 if (!NT_STATUS_IS_OK(status)) {
1375 DEBUG(0, ("Error in encrypting client message: %s\n",
1376 nt_errstr(status)));
1377 return status;
1379 buf = (char *)talloc_memdup(state, enc_buf,
1380 smb_len_nbt(enc_buf)+4);
1381 SAFE_FREE(enc_buf);
1382 if (buf == NULL) {
1383 return NT_STATUS_NO_MEMORY;
1385 iov[0].iov_base = (void *)buf;
1386 iov[0].iov_len = talloc_get_size(buf);
1387 iov_count = 1;
1390 if (state->conn->dispatch_incoming == NULL) {
1391 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1394 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1396 subreq = writev_send(state, state->ev, state->conn->outgoing,
1397 state->conn->write_fd, false, iov, iov_count);
1398 if (subreq == NULL) {
1399 return NT_STATUS_NO_MEMORY;
1401 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1402 return NT_STATUS_OK;
1405 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1406 struct tevent_context *ev,
1407 struct smbXcli_conn *conn,
1408 uint8_t smb_command,
1409 uint8_t additional_flags,
1410 uint8_t clear_flags,
1411 uint16_t additional_flags2,
1412 uint16_t clear_flags2,
1413 uint32_t timeout_msec,
1414 uint32_t pid,
1415 uint16_t tid,
1416 uint16_t uid,
1417 uint8_t wct, uint16_t *vwv,
1418 uint32_t num_bytes,
1419 const uint8_t *bytes)
1421 struct tevent_req *req;
1422 struct iovec iov;
1423 NTSTATUS status;
1425 iov.iov_base = discard_const_p(void, bytes);
1426 iov.iov_len = num_bytes;
1428 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1429 additional_flags, clear_flags,
1430 additional_flags2, clear_flags2,
1431 timeout_msec,
1432 pid, tid, uid,
1433 wct, vwv, 1, &iov);
1434 if (req == NULL) {
1435 return NULL;
1437 if (!tevent_req_is_in_progress(req)) {
1438 return tevent_req_post(req, ev);
1440 status = smb1cli_req_chain_submit(&req, 1);
1441 if (tevent_req_nterror(req, status)) {
1442 return tevent_req_post(req, ev);
1444 return req;
1447 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1449 struct tevent_req *req =
1450 tevent_req_callback_data(subreq,
1451 struct tevent_req);
1452 struct smbXcli_req_state *state =
1453 tevent_req_data(req,
1454 struct smbXcli_req_state);
1455 ssize_t nwritten;
1456 int err;
1458 nwritten = writev_recv(subreq, &err);
1459 TALLOC_FREE(subreq);
1460 if (nwritten == -1) {
1461 NTSTATUS status = map_nt_error_from_unix_common(err);
1462 smbXcli_conn_disconnect(state->conn, status);
1463 return;
1466 if (state->one_way) {
1467 state->inbuf = NULL;
1468 tevent_req_done(req);
1469 return;
1472 if (!smbXcli_req_set_pending(req)) {
1473 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1474 return;
1478 static void smbXcli_conn_received(struct tevent_req *subreq)
1480 struct smbXcli_conn *conn =
1481 tevent_req_callback_data(subreq,
1482 struct smbXcli_conn);
1483 TALLOC_CTX *frame = talloc_stackframe();
1484 NTSTATUS status;
1485 uint8_t *inbuf;
1486 ssize_t received;
1487 int err;
1489 if (subreq != conn->read_smb_req) {
1490 DEBUG(1, ("Internal error: cli_smb_received called with "
1491 "unexpected subreq\n"));
1492 status = NT_STATUS_INTERNAL_ERROR;
1493 smbXcli_conn_disconnect(conn, status);
1494 TALLOC_FREE(frame);
1495 return;
1497 conn->read_smb_req = NULL;
1499 received = read_smb_recv(subreq, frame, &inbuf, &err);
1500 TALLOC_FREE(subreq);
1501 if (received == -1) {
1502 status = map_nt_error_from_unix_common(err);
1503 smbXcli_conn_disconnect(conn, status);
1504 TALLOC_FREE(frame);
1505 return;
1508 status = conn->dispatch_incoming(conn, frame, inbuf);
1509 TALLOC_FREE(frame);
1510 if (NT_STATUS_IS_OK(status)) {
1512 * We should not do any more processing
1513 * as the dispatch function called
1514 * tevent_req_done().
1516 return;
1517 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1519 * We got an error, so notify all pending requests
1521 smbXcli_conn_disconnect(conn, status);
1522 return;
1526 * We got NT_STATUS_RETRY, so we may ask for a
1527 * next incoming pdu.
1529 if (!smbXcli_conn_receive_next(conn)) {
1530 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1534 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1535 struct iovec **piov, int *pnum_iov)
1537 struct iovec *iov;
1538 int num_iov;
1539 size_t buflen;
1540 size_t taken;
1541 size_t remaining;
1542 uint8_t *hdr;
1543 uint8_t cmd;
1544 uint32_t wct_ofs;
1546 buflen = smb_len_nbt(buf);
1547 taken = 0;
1549 hdr = buf + NBT_HDR_SIZE;
1551 if (buflen < MIN_SMB_SIZE) {
1552 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1556 * This returns iovec elements in the following order:
1558 * - SMB header
1560 * - Parameter Block
1561 * - Data Block
1563 * - Parameter Block
1564 * - Data Block
1566 * - Parameter Block
1567 * - Data Block
1569 num_iov = 1;
1571 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1572 if (iov == NULL) {
1573 return NT_STATUS_NO_MEMORY;
1575 iov[0].iov_base = hdr;
1576 iov[0].iov_len = HDR_WCT;
1577 taken += HDR_WCT;
1579 cmd = CVAL(hdr, HDR_COM);
1580 wct_ofs = HDR_WCT;
1582 while (true) {
1583 size_t len = buflen - taken;
1584 struct iovec *cur;
1585 struct iovec *iov_tmp;
1586 uint8_t wct;
1587 uint32_t bcc_ofs;
1588 uint16_t bcc;
1589 size_t needed;
1592 * we need at least WCT and BCC
1594 needed = sizeof(uint8_t) + sizeof(uint16_t);
1595 if (len < needed) {
1596 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1597 __location__, (int)len, (int)needed));
1598 goto inval;
1602 * Now we check if the specified words are there
1604 wct = CVAL(hdr, wct_ofs);
1605 needed += wct * sizeof(uint16_t);
1606 if (len < needed) {
1607 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1608 __location__, (int)len, (int)needed));
1609 goto inval;
1613 * Now we check if the specified bytes are there
1615 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1616 bcc = SVAL(hdr, bcc_ofs);
1617 needed += bcc * sizeof(uint8_t);
1618 if (len < needed) {
1619 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1620 __location__, (int)len, (int)needed));
1621 goto inval;
1625 * we allocate 2 iovec structures for words and bytes
1627 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1628 num_iov + 2);
1629 if (iov_tmp == NULL) {
1630 TALLOC_FREE(iov);
1631 return NT_STATUS_NO_MEMORY;
1633 iov = iov_tmp;
1634 cur = &iov[num_iov];
1635 num_iov += 2;
1637 cur[0].iov_len = wct * sizeof(uint16_t);
1638 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1639 cur[1].iov_len = bcc * sizeof(uint8_t);
1640 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1642 taken += needed;
1644 if (!smb1cli_is_andx_req(cmd)) {
1646 * If the current command does not have AndX chanining
1647 * we are done.
1649 break;
1652 if (wct == 0 && bcc == 0) {
1654 * An empty response also ends the chain,
1655 * most likely with an error.
1657 break;
1660 if (wct < 2) {
1661 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1662 __location__, (int)wct, (int)cmd));
1663 goto inval;
1665 cmd = CVAL(cur[0].iov_base, 0);
1666 if (cmd == 0xFF) {
1668 * If it is the end of the chain we are also done.
1670 break;
1672 wct_ofs = SVAL(cur[0].iov_base, 2);
1674 if (wct_ofs < taken) {
1675 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1677 if (wct_ofs > buflen) {
1678 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1682 * we consumed everything up to the start of the next
1683 * parameter block.
1685 taken = wct_ofs;
1688 remaining = buflen - taken;
1690 if (remaining > 0 && num_iov >= 3) {
1692 * The last DATA block gets the remaining
1693 * bytes, this is needed to support
1694 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1696 iov[num_iov-1].iov_len += remaining;
1699 *piov = iov;
1700 *pnum_iov = num_iov;
1701 return NT_STATUS_OK;
1703 inval:
1704 TALLOC_FREE(iov);
1705 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1708 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1709 TALLOC_CTX *tmp_mem,
1710 uint8_t *inbuf)
1712 struct tevent_req *req;
1713 struct smbXcli_req_state *state;
1714 NTSTATUS status;
1715 size_t num_pending;
1716 size_t i;
1717 uint8_t cmd;
1718 uint16_t mid;
1719 bool oplock_break;
1720 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1721 struct iovec *iov = NULL;
1722 int num_iov = 0;
1723 struct tevent_req **chain = NULL;
1724 size_t num_chained = 0;
1725 size_t num_responses = 0;
1727 if (conn->smb1.read_braw_req != NULL) {
1728 req = conn->smb1.read_braw_req;
1729 conn->smb1.read_braw_req = NULL;
1730 state = tevent_req_data(req, struct smbXcli_req_state);
1732 smbXcli_req_unset_pending(req);
1734 if (state->smb1.recv_iov == NULL) {
1736 * For requests with more than
1737 * one response, we have to readd the
1738 * recv_iov array.
1740 state->smb1.recv_iov = talloc_zero_array(state,
1741 struct iovec,
1743 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1744 return NT_STATUS_OK;
1748 state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
1749 state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
1750 ZERO_STRUCT(state->smb1.recv_iov[1]);
1751 ZERO_STRUCT(state->smb1.recv_iov[2]);
1753 state->smb1.recv_cmd = SMBreadBraw;
1754 state->smb1.recv_status = NT_STATUS_OK;
1755 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1757 tevent_req_done(req);
1758 return NT_STATUS_OK;
1761 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1762 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1763 DEBUG(10, ("Got non-SMB PDU\n"));
1764 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1768 * If we supported multiple encrytion contexts
1769 * here we'd look up based on tid.
1771 if (common_encryption_on(conn->smb1.trans_enc)
1772 && (CVAL(inbuf, 0) == 0)) {
1773 uint16_t enc_ctx_num;
1775 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1776 if (!NT_STATUS_IS_OK(status)) {
1777 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1778 nt_errstr(status)));
1779 return status;
1782 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1783 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1784 enc_ctx_num,
1785 conn->smb1.trans_enc->enc_ctx_num));
1786 return NT_STATUS_INVALID_HANDLE;
1789 status = common_decrypt_buffer(conn->smb1.trans_enc,
1790 (char *)inbuf);
1791 if (!NT_STATUS_IS_OK(status)) {
1792 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1793 nt_errstr(status)));
1794 return status;
1798 mid = SVAL(inhdr, HDR_MID);
1799 num_pending = talloc_array_length(conn->pending);
1801 for (i=0; i<num_pending; i++) {
1802 if (mid == smb1cli_req_mid(conn->pending[i])) {
1803 break;
1806 if (i == num_pending) {
1807 /* Dump unexpected reply */
1808 return NT_STATUS_RETRY;
1811 oplock_break = false;
1813 if (mid == 0xffff) {
1815 * Paranoia checks that this is really an oplock break request.
1817 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1818 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1819 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1820 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1821 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1823 if (!oplock_break) {
1824 /* Dump unexpected reply */
1825 return NT_STATUS_RETRY;
1829 req = conn->pending[i];
1830 state = tevent_req_data(req, struct smbXcli_req_state);
1832 if (!oplock_break /* oplock breaks are not signed */
1833 && !smb_signing_check_pdu(conn->smb1.signing,
1834 inbuf, state->smb1.seqnum+1)) {
1835 DEBUG(10, ("cli_check_sign_mac failed\n"));
1836 return NT_STATUS_ACCESS_DENIED;
1839 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1840 &iov, &num_iov);
1841 if (!NT_STATUS_IS_OK(status)) {
1842 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1843 nt_errstr(status)));
1844 return status;
1847 cmd = CVAL(inhdr, HDR_COM);
1848 status = smb1cli_pull_raw_error(inhdr);
1850 if (state->smb1.chained_requests == NULL) {
1851 if (num_iov != 3) {
1852 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1855 smbXcli_req_unset_pending(req);
1857 if (state->smb1.recv_iov == NULL) {
1859 * For requests with more than
1860 * one response, we have to readd the
1861 * recv_iov array.
1863 state->smb1.recv_iov = talloc_zero_array(state,
1864 struct iovec,
1866 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1867 return NT_STATUS_OK;
1871 state->smb1.recv_cmd = cmd;
1872 state->smb1.recv_status = status;
1873 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1875 state->smb1.recv_iov[0] = iov[0];
1876 state->smb1.recv_iov[1] = iov[1];
1877 state->smb1.recv_iov[2] = iov[2];
1879 if (talloc_array_length(conn->pending) == 0) {
1880 tevent_req_done(req);
1881 return NT_STATUS_OK;
1884 tevent_req_defer_callback(req, state->ev);
1885 tevent_req_done(req);
1886 return NT_STATUS_RETRY;
1889 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1890 num_chained = talloc_array_length(chain);
1891 num_responses = (num_iov - 1)/2;
1893 if (num_responses > num_chained) {
1894 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1897 for (i=0; i<num_chained; i++) {
1898 size_t iov_idx = 1 + (i*2);
1899 struct iovec *cur = &iov[iov_idx];
1900 uint8_t *inbuf_ref;
1902 req = chain[i];
1903 state = tevent_req_data(req, struct smbXcli_req_state);
1905 smbXcli_req_unset_pending(req);
1908 * as we finish multiple requests here
1909 * we need to defer the callbacks as
1910 * they could destroy our current stack state.
1912 tevent_req_defer_callback(req, state->ev);
1914 if (i >= num_responses) {
1915 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1916 continue;
1919 if (state->smb1.recv_iov == NULL) {
1921 * For requests with more than
1922 * one response, we have to readd the
1923 * recv_iov array.
1925 state->smb1.recv_iov = talloc_zero_array(state,
1926 struct iovec,
1928 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1929 continue;
1933 state->smb1.recv_cmd = cmd;
1935 if (i == (num_responses - 1)) {
1937 * The last request in the chain gets the status
1939 state->smb1.recv_status = status;
1940 } else {
1941 cmd = CVAL(cur[0].iov_base, 0);
1942 state->smb1.recv_status = NT_STATUS_OK;
1945 state->inbuf = inbuf;
1948 * Note: here we use talloc_reference() in a way
1949 * that does not expose it to the caller.
1951 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1952 if (tevent_req_nomem(inbuf_ref, req)) {
1953 continue;
1956 /* copy the related buffers */
1957 state->smb1.recv_iov[0] = iov[0];
1958 state->smb1.recv_iov[1] = cur[0];
1959 state->smb1.recv_iov[2] = cur[1];
1961 tevent_req_done(req);
1964 return NT_STATUS_RETRY;
1967 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
1968 TALLOC_CTX *mem_ctx,
1969 struct iovec **piov,
1970 uint8_t **phdr,
1971 uint8_t *pwct,
1972 uint16_t **pvwv,
1973 uint32_t *pvwv_offset,
1974 uint32_t *pnum_bytes,
1975 uint8_t **pbytes,
1976 uint32_t *pbytes_offset,
1977 uint8_t **pinbuf,
1978 const struct smb1cli_req_expected_response *expected,
1979 size_t num_expected)
1981 struct smbXcli_req_state *state =
1982 tevent_req_data(req,
1983 struct smbXcli_req_state);
1984 NTSTATUS status = NT_STATUS_OK;
1985 struct iovec *recv_iov = NULL;
1986 uint8_t *hdr = NULL;
1987 uint8_t wct = 0;
1988 uint32_t vwv_offset = 0;
1989 uint16_t *vwv = NULL;
1990 uint32_t num_bytes = 0;
1991 uint32_t bytes_offset = 0;
1992 uint8_t *bytes = NULL;
1993 size_t i;
1994 bool found_status = false;
1995 bool found_size = false;
1997 if (piov != NULL) {
1998 *piov = NULL;
2000 if (phdr != NULL) {
2001 *phdr = 0;
2003 if (pwct != NULL) {
2004 *pwct = 0;
2006 if (pvwv != NULL) {
2007 *pvwv = NULL;
2009 if (pvwv_offset != NULL) {
2010 *pvwv_offset = 0;
2012 if (pnum_bytes != NULL) {
2013 *pnum_bytes = 0;
2015 if (pbytes != NULL) {
2016 *pbytes = NULL;
2018 if (pbytes_offset != NULL) {
2019 *pbytes_offset = 0;
2021 if (pinbuf != NULL) {
2022 *pinbuf = NULL;
2025 if (state->inbuf != NULL) {
2026 recv_iov = state->smb1.recv_iov;
2027 state->smb1.recv_iov = NULL;
2028 if (state->smb1.recv_cmd != SMBreadBraw) {
2029 hdr = (uint8_t *)recv_iov[0].iov_base;
2030 wct = recv_iov[1].iov_len/2;
2031 vwv = (uint16_t *)recv_iov[1].iov_base;
2032 vwv_offset = PTR_DIFF(vwv, hdr);
2033 num_bytes = recv_iov[2].iov_len;
2034 bytes = (uint8_t *)recv_iov[2].iov_base;
2035 bytes_offset = PTR_DIFF(bytes, hdr);
2039 if (tevent_req_is_nterror(req, &status)) {
2040 for (i=0; i < num_expected; i++) {
2041 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2042 found_status = true;
2043 break;
2047 if (found_status) {
2048 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2051 return status;
2054 if (num_expected == 0) {
2055 found_status = true;
2056 found_size = true;
2059 status = state->smb1.recv_status;
2061 for (i=0; i < num_expected; i++) {
2062 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2063 continue;
2066 found_status = true;
2067 if (expected[i].wct == 0) {
2068 found_size = true;
2069 break;
2072 if (expected[i].wct == wct) {
2073 found_size = true;
2074 break;
2078 if (!found_status) {
2079 return status;
2082 if (!found_size) {
2083 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2086 if (piov != NULL) {
2087 *piov = talloc_move(mem_ctx, &recv_iov);
2090 if (phdr != NULL) {
2091 *phdr = hdr;
2093 if (pwct != NULL) {
2094 *pwct = wct;
2096 if (pvwv != NULL) {
2097 *pvwv = vwv;
2099 if (pvwv_offset != NULL) {
2100 *pvwv_offset = vwv_offset;
2102 if (pnum_bytes != NULL) {
2103 *pnum_bytes = num_bytes;
2105 if (pbytes != NULL) {
2106 *pbytes = bytes;
2108 if (pbytes_offset != NULL) {
2109 *pbytes_offset = bytes_offset;
2111 if (pinbuf != NULL) {
2112 *pinbuf = state->inbuf;
2115 return status;
2118 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2120 size_t wct_ofs;
2121 int i;
2123 wct_ofs = HDR_WCT;
2125 for (i=0; i<num_reqs; i++) {
2126 struct smbXcli_req_state *state;
2127 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2128 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2129 state->smb1.iov_count-2);
2130 wct_ofs = (wct_ofs + 3) & ~3;
2132 return wct_ofs;
2135 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2137 struct smbXcli_req_state *first_state =
2138 tevent_req_data(reqs[0],
2139 struct smbXcli_req_state);
2140 struct smbXcli_req_state *state;
2141 size_t wct_offset;
2142 size_t chain_padding = 0;
2143 int i, iovlen;
2144 struct iovec *iov = NULL;
2145 struct iovec *this_iov;
2146 NTSTATUS status;
2147 size_t nbt_len;
2149 if (num_reqs == 1) {
2150 return smb1cli_req_writev_submit(reqs[0], first_state,
2151 first_state->smb1.iov,
2152 first_state->smb1.iov_count);
2155 iovlen = 0;
2156 for (i=0; i<num_reqs; i++) {
2157 if (!tevent_req_is_in_progress(reqs[i])) {
2158 return NT_STATUS_INTERNAL_ERROR;
2161 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2163 if (state->smb1.iov_count < 4) {
2164 return NT_STATUS_INVALID_PARAMETER_MIX;
2167 if (i == 0) {
2169 * The NBT and SMB header
2171 iovlen += 2;
2172 } else {
2174 * Chain padding
2176 iovlen += 1;
2180 * words and bytes
2182 iovlen += state->smb1.iov_count - 2;
2185 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2186 if (iov == NULL) {
2187 return NT_STATUS_NO_MEMORY;
2190 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2191 first_state, reqs, sizeof(*reqs) * num_reqs);
2192 if (first_state->smb1.chained_requests == NULL) {
2193 TALLOC_FREE(iov);
2194 return NT_STATUS_NO_MEMORY;
2197 wct_offset = HDR_WCT;
2198 this_iov = iov;
2200 for (i=0; i<num_reqs; i++) {
2201 size_t next_padding = 0;
2202 uint16_t *vwv;
2204 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2206 if (i < num_reqs-1) {
2207 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2208 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2209 TALLOC_FREE(iov);
2210 TALLOC_FREE(first_state->smb1.chained_requests);
2211 return NT_STATUS_INVALID_PARAMETER_MIX;
2215 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2216 state->smb1.iov_count-2) + 1;
2217 if ((wct_offset % 4) != 0) {
2218 next_padding = 4 - (wct_offset % 4);
2220 wct_offset += next_padding;
2221 vwv = state->smb1.vwv;
2223 if (i < num_reqs-1) {
2224 struct smbXcli_req_state *next_state =
2225 tevent_req_data(reqs[i+1],
2226 struct smbXcli_req_state);
2227 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2228 SCVAL(vwv+0, 1, 0);
2229 SSVAL(vwv+1, 0, wct_offset);
2230 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2231 /* properly end the chain */
2232 SCVAL(vwv+0, 0, 0xff);
2233 SCVAL(vwv+0, 1, 0xff);
2234 SSVAL(vwv+1, 0, 0);
2237 if (i == 0) {
2239 * The NBT and SMB header
2241 this_iov[0] = state->smb1.iov[0];
2242 this_iov[1] = state->smb1.iov[1];
2243 this_iov += 2;
2244 } else {
2246 * This one is a bit subtle. We have to add
2247 * chain_padding bytes between the requests, and we
2248 * have to also include the wct field of the
2249 * subsequent requests. We use the subsequent header
2250 * for the padding, it contains the wct field in its
2251 * last byte.
2253 this_iov[0].iov_len = chain_padding+1;
2254 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2255 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2256 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2257 this_iov += 1;
2261 * copy the words and bytes
2263 memcpy(this_iov, state->smb1.iov+2,
2264 sizeof(struct iovec) * (state->smb1.iov_count-2));
2265 this_iov += state->smb1.iov_count - 2;
2266 chain_padding = next_padding;
2269 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2270 if (nbt_len > first_state->conn->smb1.max_xmit) {
2271 TALLOC_FREE(iov);
2272 TALLOC_FREE(first_state->smb1.chained_requests);
2273 return NT_STATUS_INVALID_PARAMETER_MIX;
2276 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2277 if (!NT_STATUS_IS_OK(status)) {
2278 TALLOC_FREE(iov);
2279 TALLOC_FREE(first_state->smb1.chained_requests);
2280 return status;
2283 return NT_STATUS_OK;
2286 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2288 return ((tevent_queue_length(conn->outgoing) != 0)
2289 || (talloc_array_length(conn->pending) != 0));
2292 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2294 return conn->smb2.server.capabilities;
2297 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2299 return conn->smb2.server.security_mode;
2302 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2304 return conn->smb2.server.max_trans_size;
2307 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2309 return conn->smb2.server.max_read_size;
2312 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2314 return conn->smb2.server.max_write_size;
2317 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2318 uint16_t max_credits)
2320 conn->smb2.max_credits = max_credits;
2323 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2325 static bool smb2cli_req_cancel(struct tevent_req *req)
2327 struct smbXcli_req_state *state =
2328 tevent_req_data(req,
2329 struct smbXcli_req_state);
2330 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2331 uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
2332 uint32_t tid = IVAL(state->smb2.hdr, SMB2_HDR_TID);
2333 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2334 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2335 struct smbXcli_session *session = state->session;
2336 uint8_t *fixed = state->smb2.pad;
2337 uint16_t fixed_len = 4;
2338 struct tevent_req *subreq;
2339 struct smbXcli_req_state *substate;
2340 NTSTATUS status;
2342 SSVAL(fixed, 0, 0x04);
2343 SSVAL(fixed, 2, 0);
2345 subreq = smb2cli_req_create(state, state->ev,
2346 state->conn,
2347 SMB2_OP_CANCEL,
2348 flags, 0,
2349 0, /* timeout */
2350 pid, tid, session,
2351 fixed, fixed_len,
2352 NULL, 0);
2353 if (subreq == NULL) {
2354 return false;
2356 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2358 if (flags & SMB2_HDR_FLAG_ASYNC) {
2359 mid = 0;
2362 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2363 SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
2364 SIVAL(substate->smb2.hdr, SMB2_HDR_TID, tid);
2365 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2366 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2368 status = smb2cli_req_compound_submit(&subreq, 1);
2369 if (!NT_STATUS_IS_OK(status)) {
2370 TALLOC_FREE(subreq);
2371 return false;
2374 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2376 return true;
2379 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2381 /* we do not care about the result */
2382 TALLOC_FREE(subreq);
2385 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2386 struct tevent_context *ev,
2387 struct smbXcli_conn *conn,
2388 uint16_t cmd,
2389 uint32_t additional_flags,
2390 uint32_t clear_flags,
2391 uint32_t timeout_msec,
2392 uint32_t pid,
2393 uint32_t tid,
2394 struct smbXcli_session *session,
2395 const uint8_t *fixed,
2396 uint16_t fixed_len,
2397 const uint8_t *dyn,
2398 uint32_t dyn_len)
2400 struct tevent_req *req;
2401 struct smbXcli_req_state *state;
2402 uint32_t flags = 0;
2403 uint64_t uid = 0;
2405 req = tevent_req_create(mem_ctx, &state,
2406 struct smbXcli_req_state);
2407 if (req == NULL) {
2408 return NULL;
2411 state->ev = ev;
2412 state->conn = conn;
2413 state->session = session;
2415 if (session) {
2416 uid = session->smb2.session_id;
2418 state->smb2.should_sign = session->smb2.should_sign;
2420 if (cmd == SMB2_OP_SESSSETUP &&
2421 session->smb2.signing_key.length != 0) {
2422 state->smb2.should_sign = true;
2426 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2427 if (state->smb2.recv_iov == NULL) {
2428 TALLOC_FREE(req);
2429 return NULL;
2432 flags |= additional_flags;
2433 flags &= ~clear_flags;
2435 state->smb2.fixed = fixed;
2436 state->smb2.fixed_len = fixed_len;
2437 state->smb2.dyn = dyn;
2438 state->smb2.dyn_len = dyn_len;
2440 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2441 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2442 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2443 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2444 SIVAL(state->smb2.hdr, SMB2_HDR_PID, pid);
2445 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2446 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2448 switch (cmd) {
2449 case SMB2_OP_CANCEL:
2450 state->one_way = true;
2451 break;
2452 case SMB2_OP_BREAK:
2454 * If this is a dummy request, it will have
2455 * UINT64_MAX as message id.
2456 * If we send on break acknowledgement,
2457 * this gets overwritten later.
2459 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2460 break;
2463 if (timeout_msec > 0) {
2464 struct timeval endtime;
2466 endtime = timeval_current_ofs_msec(timeout_msec);
2467 if (!tevent_req_set_endtime(req, ev, endtime)) {
2468 return req;
2472 return req;
2475 void smb2cli_req_set_notify_async(struct tevent_req *req)
2477 struct smbXcli_req_state *state =
2478 tevent_req_data(req,
2479 struct smbXcli_req_state);
2481 state->smb2.notify_async = true;
2484 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2485 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2486 TALLOC_CTX *tmp_mem,
2487 uint8_t *inbuf);
2489 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2490 int num_reqs)
2492 struct smbXcli_req_state *state;
2493 struct tevent_req *subreq;
2494 struct iovec *iov;
2495 int i, num_iov, nbt_len;
2498 * 1 for the nbt length
2499 * per request: HDR, fixed, dyn, padding
2500 * -1 because the last one does not need padding
2503 iov = talloc_array(reqs[0], struct iovec, 1 + 4*num_reqs - 1);
2504 if (iov == NULL) {
2505 return NT_STATUS_NO_MEMORY;
2508 num_iov = 1;
2509 nbt_len = 0;
2511 for (i=0; i<num_reqs; i++) {
2512 int hdr_iov;
2513 size_t reqlen;
2514 bool ret;
2515 uint16_t opcode;
2516 uint64_t avail;
2517 uint16_t charge;
2518 uint16_t credits;
2519 uint64_t mid;
2520 const DATA_BLOB *signing_key = NULL;
2522 if (!tevent_req_is_in_progress(reqs[i])) {
2523 return NT_STATUS_INTERNAL_ERROR;
2526 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2528 if (!smbXcli_conn_is_connected(state->conn)) {
2529 return NT_STATUS_CONNECTION_DISCONNECTED;
2532 if ((state->conn->protocol != PROTOCOL_NONE) &&
2533 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2534 return NT_STATUS_REVISION_MISMATCH;
2537 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2538 if (opcode == SMB2_OP_CANCEL) {
2539 goto skip_credits;
2542 avail = UINT64_MAX - state->conn->smb2.mid;
2543 if (avail < 1) {
2544 return NT_STATUS_CONNECTION_ABORTED;
2547 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2548 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2549 } else {
2550 charge = 1;
2553 charge = MAX(state->smb2.credit_charge, charge);
2555 avail = MIN(avail, state->conn->smb2.cur_credits);
2556 if (avail < charge) {
2557 return NT_STATUS_INTERNAL_ERROR;
2560 credits = 0;
2561 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2562 credits = state->conn->smb2.max_credits -
2563 state->conn->smb2.cur_credits;
2565 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2566 credits += 1;
2569 mid = state->conn->smb2.mid;
2570 state->conn->smb2.mid += charge;
2571 state->conn->smb2.cur_credits -= charge;
2573 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2574 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2576 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2577 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2579 skip_credits:
2580 if (state->session) {
2582 * We prefer the channel signing key if it is
2583 * already there.
2585 if (state->smb2.should_sign) {
2586 signing_key = &state->session->smb2.channel_signing_key;
2590 * If it is a channel binding, we already have the main
2591 * signing key and try that one.
2593 if (signing_key && signing_key->length == 0) {
2594 signing_key = &state->session->smb2.signing_key;
2598 * If we do not have any session key yet, we skip the
2599 * signing of SMB2_OP_SESSSETUP requests.
2601 if (signing_key && signing_key->length == 0) {
2602 signing_key = NULL;
2606 hdr_iov = num_iov;
2607 iov[num_iov].iov_base = state->smb2.hdr;
2608 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2609 num_iov += 1;
2611 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2612 iov[num_iov].iov_len = state->smb2.fixed_len;
2613 num_iov += 1;
2615 if (state->smb2.dyn != NULL) {
2616 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2617 iov[num_iov].iov_len = state->smb2.dyn_len;
2618 num_iov += 1;
2621 reqlen = sizeof(state->smb2.hdr);
2622 reqlen += state->smb2.fixed_len;
2623 reqlen += state->smb2.dyn_len;
2625 if (i < num_reqs-1) {
2626 if ((reqlen % 8) > 0) {
2627 uint8_t pad = 8 - (reqlen % 8);
2628 iov[num_iov].iov_base = state->smb2.pad;
2629 iov[num_iov].iov_len = pad;
2630 num_iov += 1;
2631 reqlen += pad;
2633 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2635 nbt_len += reqlen;
2637 if (signing_key) {
2638 NTSTATUS status;
2640 status = smb2_signing_sign_pdu(*signing_key,
2641 state->session->conn->protocol,
2642 &iov[hdr_iov], num_iov - hdr_iov);
2643 if (!NT_STATUS_IS_OK(status)) {
2644 return status;
2648 ret = smbXcli_req_set_pending(reqs[i]);
2649 if (!ret) {
2650 return NT_STATUS_NO_MEMORY;
2654 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2655 _smb_setlen_tcp(state->length_hdr, nbt_len);
2656 iov[0].iov_base = state->length_hdr;
2657 iov[0].iov_len = sizeof(state->length_hdr);
2659 if (state->conn->dispatch_incoming == NULL) {
2660 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2663 subreq = writev_send(state, state->ev, state->conn->outgoing,
2664 state->conn->write_fd, false, iov, num_iov);
2665 if (subreq == NULL) {
2666 return NT_STATUS_NO_MEMORY;
2668 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2669 return NT_STATUS_OK;
2672 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2674 struct smbXcli_req_state *state =
2675 tevent_req_data(req,
2676 struct smbXcli_req_state);
2678 state->smb2.credit_charge = charge;
2681 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2682 struct tevent_context *ev,
2683 struct smbXcli_conn *conn,
2684 uint16_t cmd,
2685 uint32_t additional_flags,
2686 uint32_t clear_flags,
2687 uint32_t timeout_msec,
2688 uint32_t pid,
2689 uint32_t tid,
2690 struct smbXcli_session *session,
2691 const uint8_t *fixed,
2692 uint16_t fixed_len,
2693 const uint8_t *dyn,
2694 uint32_t dyn_len)
2696 struct tevent_req *req;
2697 NTSTATUS status;
2699 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2700 additional_flags, clear_flags,
2701 timeout_msec,
2702 pid, tid, session,
2703 fixed, fixed_len, dyn, dyn_len);
2704 if (req == NULL) {
2705 return NULL;
2707 if (!tevent_req_is_in_progress(req)) {
2708 return tevent_req_post(req, ev);
2710 status = smb2cli_req_compound_submit(&req, 1);
2711 if (tevent_req_nterror(req, status)) {
2712 return tevent_req_post(req, ev);
2714 return req;
2717 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2719 struct tevent_req *req =
2720 tevent_req_callback_data(subreq,
2721 struct tevent_req);
2722 struct smbXcli_req_state *state =
2723 tevent_req_data(req,
2724 struct smbXcli_req_state);
2725 ssize_t nwritten;
2726 int err;
2728 nwritten = writev_recv(subreq, &err);
2729 TALLOC_FREE(subreq);
2730 if (nwritten == -1) {
2731 /* here, we need to notify all pending requests */
2732 NTSTATUS status = map_nt_error_from_unix_common(err);
2733 smbXcli_conn_disconnect(state->conn, status);
2734 return;
2738 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
2739 uint8_t *buf,
2740 size_t buflen,
2741 TALLOC_CTX *mem_ctx,
2742 struct iovec **piov, int *pnum_iov)
2744 struct iovec *iov;
2745 int num_iov = 0;
2746 size_t taken = 0;
2747 uint8_t *first_hdr = buf;
2749 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2750 if (iov == NULL) {
2751 return NT_STATUS_NO_MEMORY;
2754 while (taken < buflen) {
2755 uint8_t *tf = NULL;
2756 size_t tf_len = 0;
2757 size_t len = buflen - taken;
2758 uint8_t *hdr = first_hdr + taken;
2759 struct iovec *cur;
2760 size_t full_size;
2761 size_t next_command_ofs;
2762 uint16_t body_size;
2763 struct iovec *iov_tmp;
2765 if (len < 4) {
2766 DEBUG(10, ("%d bytes left, expected at least %d\n",
2767 (int)len, 4));
2768 goto inval;
2770 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
2771 struct smbXcli_session *s;
2772 uint64_t uid;
2773 struct iovec tf_iov[2];
2774 NTSTATUS status;
2776 if (len < SMB2_TF_HDR_SIZE) {
2777 DEBUG(10, ("%d bytes left, expected at least %d\n",
2778 (int)len, SMB2_TF_HDR_SIZE));
2779 goto inval;
2781 tf = hdr;
2782 tf_len = SMB2_TF_HDR_SIZE;
2783 taken += tf_len;
2785 hdr = first_hdr + taken;
2786 len = IVAL(tf, SMB2_TF_MSG_SIZE);
2787 uid = BVAL(tf, SMB2_TF_SESSION_ID);
2789 s = conn->sessions;
2790 for (; s; s = s->next) {
2791 if (s->smb2.session_id != uid) {
2792 continue;
2794 break;
2797 if (s == NULL) {
2798 DEBUG(10, ("unknown session_id %llu\n",
2799 (unsigned long long)uid));
2800 goto inval;
2803 tf_iov[0].iov_base = (void *)tf;
2804 tf_iov[0].iov_len = tf_len;
2805 tf_iov[1].iov_base = (void *)hdr;
2806 tf_iov[1].iov_len = len;
2808 status = smb2_signing_decrypt_pdu(s->smb2.decryption_key,
2809 conn->protocol,
2810 tf_iov, 2);
2811 if (!NT_STATUS_IS_OK(status)) {
2812 TALLOC_FREE(iov);
2813 return status;
2818 * We need the header plus the body length field
2821 if (len < SMB2_HDR_BODY + 2) {
2822 DEBUG(10, ("%d bytes left, expected at least %d\n",
2823 (int)len, SMB2_HDR_BODY));
2824 goto inval;
2826 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2827 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2828 IVAL(hdr, 0)));
2829 goto inval;
2831 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2832 DEBUG(10, ("Got HDR len %d, expected %d\n",
2833 SVAL(hdr, 4), SMB2_HDR_BODY));
2834 goto inval;
2837 full_size = len;
2838 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2839 body_size = SVAL(hdr, SMB2_HDR_BODY);
2841 if (next_command_ofs != 0) {
2842 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2843 goto inval;
2845 if (next_command_ofs > full_size) {
2846 goto inval;
2848 if (tf && next_command_ofs < len) {
2849 goto inval;
2851 full_size = next_command_ofs;
2853 if (body_size < 2) {
2854 goto inval;
2856 body_size &= 0xfffe;
2858 if (body_size > (full_size - SMB2_HDR_BODY)) {
2859 goto inval;
2862 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2863 num_iov + 4);
2864 if (iov_tmp == NULL) {
2865 TALLOC_FREE(iov);
2866 return NT_STATUS_NO_MEMORY;
2868 iov = iov_tmp;
2869 cur = &iov[num_iov];
2870 num_iov += 4;
2872 cur[0].iov_base = tf;
2873 cur[0].iov_len = tf_len;
2874 cur[1].iov_base = hdr;
2875 cur[1].iov_len = SMB2_HDR_BODY;
2876 cur[2].iov_base = hdr + SMB2_HDR_BODY;
2877 cur[2].iov_len = body_size;
2878 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
2879 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
2881 taken += full_size;
2884 *piov = iov;
2885 *pnum_iov = num_iov;
2886 return NT_STATUS_OK;
2888 inval:
2889 TALLOC_FREE(iov);
2890 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2893 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
2894 uint64_t mid)
2896 size_t num_pending = talloc_array_length(conn->pending);
2897 size_t i;
2899 for (i=0; i<num_pending; i++) {
2900 struct tevent_req *req = conn->pending[i];
2901 struct smbXcli_req_state *state =
2902 tevent_req_data(req,
2903 struct smbXcli_req_state);
2905 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
2906 return req;
2909 return NULL;
2912 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2913 TALLOC_CTX *tmp_mem,
2914 uint8_t *inbuf)
2916 struct tevent_req *req;
2917 struct smbXcli_req_state *state = NULL;
2918 struct iovec *iov;
2919 int i, num_iov;
2920 NTSTATUS status;
2921 bool defer = true;
2922 struct smbXcli_session *last_session = NULL;
2923 size_t inbuf_len = smb_len_tcp(inbuf);
2925 status = smb2cli_inbuf_parse_compound(conn,
2926 inbuf + NBT_HDR_SIZE,
2927 inbuf_len,
2928 tmp_mem,
2929 &iov, &num_iov);
2930 if (!NT_STATUS_IS_OK(status)) {
2931 return status;
2934 for (i=0; i<num_iov; i+=4) {
2935 uint8_t *inbuf_ref = NULL;
2936 struct iovec *cur = &iov[i];
2937 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
2938 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
2939 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
2940 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
2941 uint16_t req_opcode;
2942 uint32_t req_flags;
2943 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
2944 uint32_t new_credits;
2945 struct smbXcli_session *session = NULL;
2946 const DATA_BLOB *signing_key = NULL;
2948 new_credits = conn->smb2.cur_credits;
2949 new_credits += credits;
2950 if (new_credits > UINT16_MAX) {
2951 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2953 conn->smb2.cur_credits += credits;
2955 req = smb2cli_conn_find_pending(conn, mid);
2956 if (req == NULL) {
2957 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2959 state = tevent_req_data(req, struct smbXcli_req_state);
2961 state->smb2.got_async = false;
2963 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2964 if (opcode != req_opcode) {
2965 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2967 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2969 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
2970 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2973 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
2974 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
2975 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
2976 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
2979 * async interim responses are not signed,
2980 * even if the SMB2_HDR_FLAG_SIGNED flag
2981 * is set.
2983 req_flags |= SMB2_HDR_FLAG_ASYNC;
2984 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
2985 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
2987 if (state->smb2.notify_async) {
2988 state->smb2.got_async = true;
2989 tevent_req_defer_callback(req, state->ev);
2990 tevent_req_notify_callback(req);
2992 continue;
2995 session = state->session;
2996 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
2997 session = last_session;
2999 last_session = session;
3001 if (state->smb2.should_sign) {
3002 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3003 return NT_STATUS_ACCESS_DENIED;
3007 if (flags & SMB2_HDR_FLAG_SIGNED) {
3008 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3010 if (session == NULL) {
3011 struct smbXcli_session *s;
3013 s = state->conn->sessions;
3014 for (; s; s = s->next) {
3015 if (s->smb2.session_id != uid) {
3016 continue;
3019 session = s;
3020 break;
3024 if (session == NULL) {
3025 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3028 last_session = session;
3029 signing_key = &session->smb2.channel_signing_key;
3032 if (opcode == SMB2_OP_SESSSETUP) {
3034 * We prefer the channel signing key, if it is
3035 * already there.
3037 * If we do not have a channel signing key yet,
3038 * we try the main signing key, if it is not
3039 * the final response.
3041 if (signing_key && signing_key->length == 0 &&
3042 !NT_STATUS_IS_OK(status)) {
3043 signing_key = &session->smb2.signing_key;
3046 if (signing_key && signing_key->length == 0) {
3048 * If we do not have a session key to
3049 * verify the signature, we defer the
3050 * signing check to the caller.
3052 * The caller gets NT_STATUS_OK, it
3053 * has to call
3054 * smb2cli_session_set_session_key()
3055 * or
3056 * smb2cli_session_set_channel_key()
3057 * which will check the signature
3058 * with the channel signing key.
3060 signing_key = NULL;
3064 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3066 * if the server returns NT_STATUS_USER_SESSION_DELETED
3067 * the response is not signed and we should
3068 * propagate the NT_STATUS_USER_SESSION_DELETED
3069 * status to the caller.
3071 signing_key = NULL;
3074 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3075 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3076 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3078 * if the server returns
3079 * NT_STATUS_NETWORK_NAME_DELETED
3080 * NT_STATUS_FILE_CLOSED
3081 * NT_STATUS_INVALID_PARAMETER
3082 * the response might not be signed
3083 * as this happens before the signing checks.
3085 * If server echos the signature (or all zeros)
3086 * we should report the status from the server
3087 * to the caller.
3089 if (signing_key) {
3090 int cmp;
3092 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3093 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3094 16);
3095 if (cmp == 0) {
3096 state->smb2.signing_skipped = true;
3097 signing_key = NULL;
3100 if (signing_key) {
3101 int cmp;
3102 static const uint8_t zeros[16];
3104 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3105 zeros,
3106 16);
3107 if (cmp == 0) {
3108 state->smb2.signing_skipped = true;
3109 signing_key = NULL;
3114 if (signing_key) {
3115 status = smb2_signing_check_pdu(*signing_key,
3116 state->conn->protocol,
3117 &cur[1], 3);
3118 if (!NT_STATUS_IS_OK(status)) {
3120 * If the signing check fails, we disconnect
3121 * the connection.
3123 return status;
3127 smbXcli_req_unset_pending(req);
3130 * There might be more than one response
3131 * we need to defer the notifications
3133 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3134 defer = false;
3137 if (defer) {
3138 tevent_req_defer_callback(req, state->ev);
3142 * Note: here we use talloc_reference() in a way
3143 * that does not expose it to the caller.
3145 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3146 if (tevent_req_nomem(inbuf_ref, req)) {
3147 continue;
3150 /* copy the related buffers */
3151 state->smb2.recv_iov[0] = cur[1];
3152 state->smb2.recv_iov[1] = cur[2];
3153 state->smb2.recv_iov[2] = cur[3];
3155 tevent_req_done(req);
3158 if (defer) {
3159 return NT_STATUS_RETRY;
3162 return NT_STATUS_OK;
3165 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3166 struct iovec **piov,
3167 const struct smb2cli_req_expected_response *expected,
3168 size_t num_expected)
3170 struct smbXcli_req_state *state =
3171 tevent_req_data(req,
3172 struct smbXcli_req_state);
3173 NTSTATUS status;
3174 size_t body_size;
3175 bool found_status = false;
3176 bool found_size = false;
3177 size_t i;
3179 if (piov != NULL) {
3180 *piov = NULL;
3183 if (state->smb2.got_async) {
3184 return STATUS_PENDING;
3187 if (tevent_req_is_nterror(req, &status)) {
3188 for (i=0; i < num_expected; i++) {
3189 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3190 found_status = true;
3191 break;
3195 if (found_status) {
3196 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3199 return status;
3202 if (num_expected == 0) {
3203 found_status = true;
3204 found_size = true;
3207 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3208 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3210 for (i=0; i < num_expected; i++) {
3211 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3212 continue;
3215 found_status = true;
3216 if (expected[i].body_size == 0) {
3217 found_size = true;
3218 break;
3221 if (expected[i].body_size == body_size) {
3222 found_size = true;
3223 break;
3227 if (!found_status) {
3228 return status;
3231 if (state->smb2.signing_skipped) {
3232 if (num_expected > 0) {
3233 return NT_STATUS_ACCESS_DENIED;
3235 if (!NT_STATUS_IS_ERR(status)) {
3236 return NT_STATUS_ACCESS_DENIED;
3240 if (!found_size) {
3241 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3244 if (piov != NULL) {
3245 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3248 return status;
3251 static const struct {
3252 enum protocol_types proto;
3253 const char *smb1_name;
3254 } smb1cli_prots[] = {
3255 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3256 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3257 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3258 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3259 {PROTOCOL_LANMAN2, "LM1.2X002"},
3260 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3261 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3262 {PROTOCOL_LANMAN2, "Samba"},
3263 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3264 {PROTOCOL_NT1, "NT LM 0.12"},
3265 {PROTOCOL_SMB2_02, "SMB 2.002"},
3266 {PROTOCOL_SMB2_10, "SMB 2.???"},
3269 static const struct {
3270 enum protocol_types proto;
3271 uint16_t smb2_dialect;
3272 } smb2cli_prots[] = {
3273 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3274 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3275 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3276 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3277 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3280 struct smbXcli_negprot_state {
3281 struct smbXcli_conn *conn;
3282 struct tevent_context *ev;
3283 uint32_t timeout_msec;
3284 enum protocol_types min_protocol;
3285 enum protocol_types max_protocol;
3287 struct {
3288 uint8_t fixed[36];
3289 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3290 } smb2;
3293 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3294 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3295 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3296 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3297 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3298 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3299 TALLOC_CTX *frame,
3300 uint8_t *inbuf);
3302 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3303 struct tevent_context *ev,
3304 struct smbXcli_conn *conn,
3305 uint32_t timeout_msec,
3306 enum protocol_types min_protocol,
3307 enum protocol_types max_protocol)
3309 struct tevent_req *req, *subreq;
3310 struct smbXcli_negprot_state *state;
3312 req = tevent_req_create(mem_ctx, &state,
3313 struct smbXcli_negprot_state);
3314 if (req == NULL) {
3315 return NULL;
3317 state->conn = conn;
3318 state->ev = ev;
3319 state->timeout_msec = timeout_msec;
3320 state->min_protocol = min_protocol;
3321 state->max_protocol = max_protocol;
3323 if (min_protocol == PROTOCOL_NONE) {
3324 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3325 return tevent_req_post(req, ev);
3328 if (max_protocol == PROTOCOL_NONE) {
3329 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3330 return tevent_req_post(req, ev);
3333 if (min_protocol > max_protocol) {
3334 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3335 return tevent_req_post(req, ev);
3338 if ((min_protocol < PROTOCOL_SMB2_02) &&
3339 (max_protocol < PROTOCOL_SMB2_02)) {
3341 * SMB1 only...
3343 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3345 subreq = smbXcli_negprot_smb1_subreq(state);
3346 if (tevent_req_nomem(subreq, req)) {
3347 return tevent_req_post(req, ev);
3349 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3350 return req;
3353 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3354 (max_protocol >= PROTOCOL_SMB2_02)) {
3356 * SMB2 only...
3358 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3360 subreq = smbXcli_negprot_smb2_subreq(state);
3361 if (tevent_req_nomem(subreq, req)) {
3362 return tevent_req_post(req, ev);
3364 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3365 return req;
3369 * We send an SMB1 negprot with the SMB2 dialects
3370 * and expect a SMB1 or a SMB2 response.
3372 * smbXcli_negprot_dispatch_incoming() will fix the
3373 * callback to match protocol of the response.
3375 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3377 subreq = smbXcli_negprot_smb1_subreq(state);
3378 if (tevent_req_nomem(subreq, req)) {
3379 return tevent_req_post(req, ev);
3381 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3382 return req;
3385 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3387 struct tevent_req *req =
3388 tevent_req_callback_data(subreq,
3389 struct tevent_req);
3390 NTSTATUS status;
3393 * we just want the low level error
3395 status = tevent_req_simple_recv_ntstatus(subreq);
3396 TALLOC_FREE(subreq);
3397 if (tevent_req_nterror(req, status)) {
3398 return;
3401 /* this should never happen */
3402 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3405 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3407 size_t i;
3408 DATA_BLOB bytes = data_blob_null;
3409 uint8_t flags;
3410 uint16_t flags2;
3412 /* setup the protocol strings */
3413 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3414 uint8_t c = 2;
3415 bool ok;
3417 if (smb1cli_prots[i].proto < state->min_protocol) {
3418 continue;
3421 if (smb1cli_prots[i].proto > state->max_protocol) {
3422 continue;
3425 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3426 if (!ok) {
3427 return NULL;
3431 * We now it is already ascii and
3432 * we want NULL termination.
3434 ok = data_blob_append(state, &bytes,
3435 smb1cli_prots[i].smb1_name,
3436 strlen(smb1cli_prots[i].smb1_name)+1);
3437 if (!ok) {
3438 return NULL;
3442 smb1cli_req_flags(state->max_protocol,
3443 state->conn->smb1.client.capabilities,
3444 SMBnegprot,
3445 0, 0, &flags,
3446 0, 0, &flags2);
3448 return smb1cli_req_send(state, state->ev, state->conn,
3449 SMBnegprot,
3450 flags, ~flags,
3451 flags2, ~flags2,
3452 state->timeout_msec,
3453 0xFFFE, 0, 0, /* pid, tid, uid */
3454 0, NULL, /* wct, vwv */
3455 bytes.length, bytes.data);
3458 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3460 struct tevent_req *req =
3461 tevent_req_callback_data(subreq,
3462 struct tevent_req);
3463 struct smbXcli_negprot_state *state =
3464 tevent_req_data(req,
3465 struct smbXcli_negprot_state);
3466 struct smbXcli_conn *conn = state->conn;
3467 struct iovec *recv_iov = NULL;
3468 uint8_t *inhdr;
3469 uint8_t wct;
3470 uint16_t *vwv;
3471 uint32_t num_bytes;
3472 uint8_t *bytes;
3473 NTSTATUS status;
3474 uint16_t protnum;
3475 size_t i;
3476 size_t num_prots = 0;
3477 uint8_t flags;
3478 uint32_t client_capabilities = conn->smb1.client.capabilities;
3479 uint32_t both_capabilities;
3480 uint32_t server_capabilities = 0;
3481 uint32_t capabilities;
3482 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3483 uint32_t server_max_xmit = 0;
3484 uint32_t max_xmit;
3485 uint32_t server_max_mux = 0;
3486 uint16_t server_security_mode = 0;
3487 uint32_t server_session_key = 0;
3488 bool server_readbraw = false;
3489 bool server_writebraw = false;
3490 bool server_lockread = false;
3491 bool server_writeunlock = false;
3492 struct GUID server_guid = GUID_zero();
3493 DATA_BLOB server_gss_blob = data_blob_null;
3494 uint8_t server_challenge[8];
3495 char *server_workgroup = NULL;
3496 char *server_name = NULL;
3497 int server_time_zone = 0;
3498 NTTIME server_system_time = 0;
3499 static const struct smb1cli_req_expected_response expected[] = {
3501 .status = NT_STATUS_OK,
3502 .wct = 0x11, /* NT1 */
3505 .status = NT_STATUS_OK,
3506 .wct = 0x0D, /* LM */
3509 .status = NT_STATUS_OK,
3510 .wct = 0x01, /* CORE */
3514 ZERO_STRUCT(server_challenge);
3516 status = smb1cli_req_recv(subreq, state,
3517 &recv_iov,
3518 &inhdr,
3519 &wct,
3520 &vwv,
3521 NULL, /* pvwv_offset */
3522 &num_bytes,
3523 &bytes,
3524 NULL, /* pbytes_offset */
3525 NULL, /* pinbuf */
3526 expected, ARRAY_SIZE(expected));
3527 TALLOC_FREE(subreq);
3528 if (tevent_req_nterror(req, status)) {
3529 return;
3532 flags = CVAL(inhdr, HDR_FLG);
3534 protnum = SVAL(vwv, 0);
3536 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3537 if (smb1cli_prots[i].proto < state->min_protocol) {
3538 continue;
3541 if (smb1cli_prots[i].proto > state->max_protocol) {
3542 continue;
3545 if (protnum != num_prots) {
3546 num_prots++;
3547 continue;
3550 conn->protocol = smb1cli_prots[i].proto;
3551 break;
3554 if (conn->protocol == PROTOCOL_NONE) {
3555 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3556 return;
3559 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3560 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3561 "and the selected protocol level doesn't support it.\n"));
3562 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3563 return;
3566 if (flags & FLAG_SUPPORT_LOCKREAD) {
3567 server_lockread = true;
3568 server_writeunlock = true;
3571 if (conn->protocol >= PROTOCOL_NT1) {
3572 const char *client_signing = NULL;
3573 bool server_mandatory = false;
3574 bool server_allowed = false;
3575 const char *server_signing = NULL;
3576 bool ok;
3577 uint8_t key_len;
3579 if (wct != 0x11) {
3580 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3581 return;
3584 /* NT protocol */
3585 server_security_mode = CVAL(vwv + 1, 0);
3586 server_max_mux = SVAL(vwv + 1, 1);
3587 server_max_xmit = IVAL(vwv + 3, 1);
3588 server_session_key = IVAL(vwv + 7, 1);
3589 server_time_zone = SVALS(vwv + 15, 1);
3590 server_time_zone *= 60;
3591 /* this time arrives in real GMT */
3592 server_system_time = BVAL(vwv + 11, 1);
3593 server_capabilities = IVAL(vwv + 9, 1);
3595 key_len = CVAL(vwv + 16, 1);
3597 if (server_capabilities & CAP_RAW_MODE) {
3598 server_readbraw = true;
3599 server_writebraw = true;
3601 if (server_capabilities & CAP_LOCK_AND_READ) {
3602 server_lockread = true;
3605 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3606 DATA_BLOB blob1, blob2;
3608 if (num_bytes < 16) {
3609 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3610 return;
3613 blob1 = data_blob_const(bytes, 16);
3614 status = GUID_from_data_blob(&blob1, &server_guid);
3615 if (tevent_req_nterror(req, status)) {
3616 return;
3619 blob1 = data_blob_const(bytes+16, num_bytes-16);
3620 blob2 = data_blob_dup_talloc(state, blob1);
3621 if (blob1.length > 0 &&
3622 tevent_req_nomem(blob2.data, req)) {
3623 return;
3625 server_gss_blob = blob2;
3626 } else {
3627 DATA_BLOB blob1, blob2;
3629 if (num_bytes < key_len) {
3630 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3631 return;
3634 if (key_len != 0 && key_len != 8) {
3635 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3636 return;
3639 if (key_len == 8) {
3640 memcpy(server_challenge, bytes, 8);
3643 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3644 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3645 if (blob1.length > 0) {
3646 size_t len;
3648 len = utf16_len_n(blob1.data,
3649 blob1.length);
3650 blob1.length = len;
3652 ok = convert_string_talloc(state,
3653 CH_UTF16LE,
3654 CH_UNIX,
3655 blob1.data,
3656 blob1.length,
3657 &server_workgroup,
3658 &len);
3659 if (!ok) {
3660 status = map_nt_error_from_unix_common(errno);
3661 tevent_req_nterror(req, status);
3662 return;
3666 blob2.data += blob1.length;
3667 blob2.length -= blob1.length;
3668 if (blob2.length > 0) {
3669 size_t len;
3671 len = utf16_len_n(blob1.data,
3672 blob1.length);
3673 blob1.length = len;
3675 ok = convert_string_talloc(state,
3676 CH_UTF16LE,
3677 CH_UNIX,
3678 blob2.data,
3679 blob2.length,
3680 &server_name,
3681 &len);
3682 if (!ok) {
3683 status = map_nt_error_from_unix_common(errno);
3684 tevent_req_nterror(req, status);
3685 return;
3690 client_signing = "disabled";
3691 if (conn->allow_signing) {
3692 client_signing = "allowed";
3694 if (conn->mandatory_signing) {
3695 client_signing = "required";
3698 server_signing = "not supported";
3699 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3700 server_signing = "supported";
3701 server_allowed = true;
3703 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3704 server_signing = "required";
3705 server_mandatory = true;
3708 ok = smb_signing_set_negotiated(conn->smb1.signing,
3709 server_allowed,
3710 server_mandatory);
3711 if (!ok) {
3712 DEBUG(1,("cli_negprot: SMB signing is required, "
3713 "but client[%s] and server[%s] mismatch\n",
3714 client_signing, server_signing));
3715 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3716 return;
3719 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3720 DATA_BLOB blob1;
3721 uint8_t key_len;
3722 time_t t;
3724 if (wct != 0x0D) {
3725 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3726 return;
3729 server_security_mode = SVAL(vwv + 1, 0);
3730 server_max_xmit = SVAL(vwv + 2, 0);
3731 server_max_mux = SVAL(vwv + 3, 0);
3732 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3733 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3734 server_session_key = IVAL(vwv + 6, 0);
3735 server_time_zone = SVALS(vwv + 10, 0);
3736 server_time_zone *= 60;
3737 /* this time is converted to GMT by make_unix_date */
3738 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3739 unix_to_nt_time(&server_system_time, t);
3740 key_len = SVAL(vwv + 11, 0);
3742 if (num_bytes < key_len) {
3743 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3744 return;
3747 if (key_len != 0 && key_len != 8) {
3748 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3749 return;
3752 if (key_len == 8) {
3753 memcpy(server_challenge, bytes, 8);
3756 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3757 if (blob1.length > 0) {
3758 size_t len;
3759 bool ok;
3761 len = utf16_len_n(blob1.data,
3762 blob1.length);
3763 blob1.length = len;
3765 ok = convert_string_talloc(state,
3766 CH_DOS,
3767 CH_UNIX,
3768 blob1.data,
3769 blob1.length,
3770 &server_workgroup,
3771 &len);
3772 if (!ok) {
3773 status = map_nt_error_from_unix_common(errno);
3774 tevent_req_nterror(req, status);
3775 return;
3779 } else {
3780 /* the old core protocol */
3781 server_time_zone = get_time_zone(time(NULL));
3782 server_max_xmit = 1024;
3783 server_max_mux = 1;
3786 if (server_max_xmit < 1024) {
3787 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3788 return;
3791 if (server_max_mux < 1) {
3792 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3793 return;
3797 * Now calculate the negotiated capabilities
3798 * based on the mask for:
3799 * - client only flags
3800 * - flags used in both directions
3801 * - server only flags
3803 both_capabilities = client_capabilities & server_capabilities;
3804 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3805 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3806 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3808 max_xmit = MIN(client_max_xmit, server_max_xmit);
3810 conn->smb1.server.capabilities = server_capabilities;
3811 conn->smb1.capabilities = capabilities;
3813 conn->smb1.server.max_xmit = server_max_xmit;
3814 conn->smb1.max_xmit = max_xmit;
3816 conn->smb1.server.max_mux = server_max_mux;
3818 conn->smb1.server.security_mode = server_security_mode;
3820 conn->smb1.server.readbraw = server_readbraw;
3821 conn->smb1.server.writebraw = server_writebraw;
3822 conn->smb1.server.lockread = server_lockread;
3823 conn->smb1.server.writeunlock = server_writeunlock;
3825 conn->smb1.server.session_key = server_session_key;
3827 talloc_steal(conn, server_gss_blob.data);
3828 conn->smb1.server.gss_blob = server_gss_blob;
3829 conn->smb1.server.guid = server_guid;
3830 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3831 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3832 conn->smb1.server.name = talloc_move(conn, &server_name);
3834 conn->smb1.server.time_zone = server_time_zone;
3835 conn->smb1.server.system_time = server_system_time;
3837 tevent_req_done(req);
3840 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
3842 size_t i;
3843 uint8_t *buf;
3844 uint16_t dialect_count = 0;
3846 buf = state->smb2.dyn;
3847 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3848 if (smb2cli_prots[i].proto < state->min_protocol) {
3849 continue;
3852 if (smb2cli_prots[i].proto > state->max_protocol) {
3853 continue;
3856 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
3857 dialect_count++;
3860 buf = state->smb2.fixed;
3861 SSVAL(buf, 0, 36);
3862 SSVAL(buf, 2, dialect_count);
3863 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
3864 SSVAL(buf, 6, 0); /* Reserved */
3865 if (state->max_protocol >= PROTOCOL_SMB2_22) {
3866 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
3867 } else {
3868 SIVAL(buf, 8, 0); /* Capabilities */
3870 if (state->max_protocol >= PROTOCOL_SMB2_10) {
3871 NTSTATUS status;
3872 DATA_BLOB blob;
3874 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
3875 state, &blob);
3876 if (!NT_STATUS_IS_OK(status)) {
3877 return NULL;
3879 memcpy(buf+12, blob.data, 16); /* ClientGuid */
3880 } else {
3881 memset(buf+12, 0, 16); /* ClientGuid */
3883 SBVAL(buf, 28, 0); /* ClientStartTime */
3885 return smb2cli_req_send(state, state->ev,
3886 state->conn, SMB2_OP_NEGPROT,
3887 0, 0, /* flags */
3888 state->timeout_msec,
3889 0xFEFF, 0, NULL, /* pid, tid, session */
3890 state->smb2.fixed, sizeof(state->smb2.fixed),
3891 state->smb2.dyn, dialect_count*2);
3894 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
3896 struct tevent_req *req =
3897 tevent_req_callback_data(subreq,
3898 struct tevent_req);
3899 struct smbXcli_negprot_state *state =
3900 tevent_req_data(req,
3901 struct smbXcli_negprot_state);
3902 struct smbXcli_conn *conn = state->conn;
3903 size_t security_offset, security_length;
3904 DATA_BLOB blob;
3905 NTSTATUS status;
3906 struct iovec *iov;
3907 uint8_t *body;
3908 size_t i;
3909 uint16_t dialect_revision;
3910 static const struct smb2cli_req_expected_response expected[] = {
3912 .status = NT_STATUS_OK,
3913 .body_size = 0x41
3917 status = smb2cli_req_recv(subreq, state, &iov,
3918 expected, ARRAY_SIZE(expected));
3919 TALLOC_FREE(subreq);
3920 if (tevent_req_nterror(req, status)) {
3921 return;
3924 body = (uint8_t *)iov[1].iov_base;
3926 dialect_revision = SVAL(body, 4);
3928 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3929 if (smb2cli_prots[i].proto < state->min_protocol) {
3930 continue;
3933 if (smb2cli_prots[i].proto > state->max_protocol) {
3934 continue;
3937 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
3938 continue;
3941 conn->protocol = smb2cli_prots[i].proto;
3942 break;
3945 if (conn->protocol == PROTOCOL_NONE) {
3946 if (state->min_protocol >= PROTOCOL_SMB2_02) {
3947 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3948 return;
3951 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
3952 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3953 return;
3956 /* make sure we do not loop forever */
3957 state->min_protocol = PROTOCOL_SMB2_02;
3960 * send a SMB2 negprot, in order to negotiate
3961 * the SMB2 dialect.
3963 subreq = smbXcli_negprot_smb2_subreq(state);
3964 if (tevent_req_nomem(subreq, req)) {
3965 return;
3967 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3968 return;
3971 conn->smb2.server.security_mode = SVAL(body, 2);
3973 blob = data_blob_const(body + 8, 16);
3974 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
3975 if (tevent_req_nterror(req, status)) {
3976 return;
3979 conn->smb2.server.capabilities = IVAL(body, 24);
3980 conn->smb2.server.max_trans_size= IVAL(body, 28);
3981 conn->smb2.server.max_read_size = IVAL(body, 32);
3982 conn->smb2.server.max_write_size= IVAL(body, 36);
3983 conn->smb2.server.system_time = BVAL(body, 40);
3984 conn->smb2.server.start_time = BVAL(body, 48);
3986 security_offset = SVAL(body, 56);
3987 security_length = SVAL(body, 58);
3989 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
3990 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3991 return;
3994 if (security_length > iov[2].iov_len) {
3995 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3996 return;
3999 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4000 iov[2].iov_base,
4001 security_length);
4002 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4003 return;
4006 tevent_req_done(req);
4009 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4010 TALLOC_CTX *tmp_mem,
4011 uint8_t *inbuf)
4013 size_t num_pending = talloc_array_length(conn->pending);
4014 struct tevent_req *subreq;
4015 struct smbXcli_req_state *substate;
4016 struct tevent_req *req;
4017 uint32_t protocol_magic;
4018 size_t inbuf_len = smb_len_nbt(inbuf);
4020 if (num_pending != 1) {
4021 return NT_STATUS_INTERNAL_ERROR;
4024 if (inbuf_len < 4) {
4025 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4028 subreq = conn->pending[0];
4029 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4030 req = tevent_req_callback_data(subreq, struct tevent_req);
4032 protocol_magic = IVAL(inbuf, 4);
4034 switch (protocol_magic) {
4035 case SMB_MAGIC:
4036 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4037 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4038 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4040 case SMB2_MAGIC:
4041 if (substate->smb2.recv_iov == NULL) {
4043 * For the SMB1 negprot we have move it.
4045 substate->smb2.recv_iov = substate->smb1.recv_iov;
4046 substate->smb1.recv_iov = NULL;
4050 * we got an SMB2 answer, which consumed sequence number 0
4051 * so we need to use 1 as the next one
4053 conn->smb2.mid = 1;
4054 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4055 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4056 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4059 DEBUG(10, ("Got non-SMB PDU\n"));
4060 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4063 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4065 return tevent_req_simple_recv_ntstatus(req);
4068 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4069 uint32_t timeout_msec,
4070 enum protocol_types min_protocol,
4071 enum protocol_types max_protocol)
4073 TALLOC_CTX *frame = talloc_stackframe();
4074 struct tevent_context *ev;
4075 struct tevent_req *req;
4076 NTSTATUS status = NT_STATUS_NO_MEMORY;
4077 bool ok;
4079 if (smbXcli_conn_has_async_calls(conn)) {
4081 * Can't use sync call while an async call is in flight
4083 status = NT_STATUS_INVALID_PARAMETER_MIX;
4084 goto fail;
4086 ev = tevent_context_init(frame);
4087 if (ev == NULL) {
4088 goto fail;
4090 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4091 min_protocol, max_protocol);
4092 if (req == NULL) {
4093 goto fail;
4095 ok = tevent_req_poll(req, ev);
4096 if (!ok) {
4097 status = map_nt_error_from_unix_common(errno);
4098 goto fail;
4100 status = smbXcli_negprot_recv(req);
4101 fail:
4102 TALLOC_FREE(frame);
4103 return status;
4106 static int smbXcli_session_destructor(struct smbXcli_session *session)
4108 if (session->conn == NULL) {
4109 return 0;
4112 DLIST_REMOVE(session->conn->sessions, session);
4113 return 0;
4116 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4117 struct smbXcli_conn *conn)
4119 struct smbXcli_session *session;
4121 session = talloc_zero(mem_ctx, struct smbXcli_session);
4122 if (session == NULL) {
4123 return NULL;
4125 talloc_set_destructor(session, smbXcli_session_destructor);
4127 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4128 session->conn = conn;
4130 return session;
4133 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4135 struct smbXcli_conn *conn = session->conn;
4136 uint8_t security_mode = 0;
4138 if (conn == NULL) {
4139 return security_mode;
4142 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4143 if (conn->mandatory_signing) {
4144 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4147 return security_mode;
4150 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4152 return session->smb2.session_id;
4155 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4157 return session->smb2.session_flags;
4160 NTSTATUS smb2cli_session_application_key(struct smbXcli_session *session,
4161 TALLOC_CTX *mem_ctx,
4162 DATA_BLOB *key)
4164 *key = data_blob_null;
4166 if (session->smb2.application_key.length == 0) {
4167 return NT_STATUS_NO_USER_SESSION_KEY;
4170 *key = data_blob_dup_talloc(mem_ctx, session->smb2.application_key);
4171 if (key->data == NULL) {
4172 return NT_STATUS_NO_MEMORY;
4175 return NT_STATUS_OK;
4178 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4179 uint64_t session_id,
4180 uint16_t session_flags)
4182 session->smb2.session_id = session_id;
4183 session->smb2.session_flags = session_flags;
4186 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4187 const DATA_BLOB _session_key,
4188 const struct iovec *recv_iov)
4190 struct smbXcli_conn *conn = session->conn;
4191 uint16_t no_sign_flags;
4192 uint8_t session_key[16];
4193 NTSTATUS status;
4195 if (conn == NULL) {
4196 return NT_STATUS_INVALID_PARAMETER_MIX;
4199 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4201 if (session->smb2.session_flags & no_sign_flags) {
4202 session->smb2.should_sign = false;
4203 return NT_STATUS_OK;
4206 if (session->smb2.signing_key.length != 0) {
4207 return NT_STATUS_INVALID_PARAMETER_MIX;
4210 ZERO_STRUCT(session_key);
4211 memcpy(session_key, _session_key.data,
4212 MIN(_session_key.length, sizeof(session_key)));
4214 session->smb2.signing_key = data_blob_talloc(session,
4215 session_key,
4216 sizeof(session_key));
4217 if (session->smb2.signing_key.data == NULL) {
4218 ZERO_STRUCT(session_key);
4219 return NT_STATUS_NO_MEMORY;
4222 if (conn->protocol >= PROTOCOL_SMB2_24) {
4223 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4224 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4226 smb2_key_derivation(session_key, sizeof(session_key),
4227 label.data, label.length,
4228 context.data, context.length,
4229 session->smb2.signing_key.data);
4232 session->smb2.encryption_key = data_blob_dup_talloc(session,
4233 session->smb2.signing_key);
4234 if (session->smb2.encryption_key.data == NULL) {
4235 ZERO_STRUCT(session_key);
4236 return NT_STATUS_NO_MEMORY;
4239 if (conn->protocol >= PROTOCOL_SMB2_24) {
4240 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4241 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4243 smb2_key_derivation(session_key, sizeof(session_key),
4244 label.data, label.length,
4245 context.data, context.length,
4246 session->smb2.encryption_key.data);
4249 session->smb2.decryption_key = data_blob_dup_talloc(session,
4250 session->smb2.signing_key);
4251 if (session->smb2.decryption_key.data == NULL) {
4252 ZERO_STRUCT(session_key);
4253 return NT_STATUS_NO_MEMORY;
4256 if (conn->protocol >= PROTOCOL_SMB2_24) {
4257 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4258 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4260 smb2_key_derivation(session_key, sizeof(session_key),
4261 label.data, label.length,
4262 context.data, context.length,
4263 session->smb2.decryption_key.data);
4266 session->smb2.application_key = data_blob_dup_talloc(session,
4267 session->smb2.signing_key);
4268 if (session->smb2.application_key.data == NULL) {
4269 ZERO_STRUCT(session_key);
4270 return NT_STATUS_NO_MEMORY;
4273 if (conn->protocol >= PROTOCOL_SMB2_24) {
4274 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4275 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4277 smb2_key_derivation(session_key, sizeof(session_key),
4278 label.data, label.length,
4279 context.data, context.length,
4280 session->smb2.application_key.data);
4282 ZERO_STRUCT(session_key);
4284 session->smb2.channel_signing_key = data_blob_dup_talloc(session,
4285 session->smb2.signing_key);
4286 if (session->smb2.channel_signing_key.data == NULL) {
4287 return NT_STATUS_NO_MEMORY;
4290 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4291 session->conn->protocol,
4292 recv_iov, 3);
4293 if (!NT_STATUS_IS_OK(status)) {
4294 return status;
4297 session->smb2.should_sign = false;
4298 session->smb2.should_encrypt = false;
4300 if (conn->desire_signing) {
4301 session->smb2.should_sign = true;
4304 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4305 session->smb2.should_sign = true;
4308 generate_random_buffer((uint8_t *)&session->smb2.channel_nonce,
4309 sizeof(session->smb2.channel_nonce));
4310 session->smb2.channel_next = 1;
4312 return NT_STATUS_OK;
4315 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4316 struct smbXcli_session *session1,
4317 struct smbXcli_conn *conn,
4318 struct smbXcli_session **_session2)
4320 struct smbXcli_session *session2;
4322 if (session1->smb2.signing_key.length == 0) {
4323 return NT_STATUS_INVALID_PARAMETER_MIX;
4326 if (session1->smb2.channel_next == 0) {
4327 return NT_STATUS_INVALID_PARAMETER_MIX;
4330 if (conn == NULL) {
4331 return NT_STATUS_INVALID_PARAMETER_MIX;
4334 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4335 if (session2 == NULL) {
4336 return NT_STATUS_NO_MEMORY;
4338 session2->smb2.session_id = session1->smb2.session_id;
4339 session2->smb2.session_flags = session1->smb2.session_flags;
4341 session2->smb2.signing_key = data_blob_dup_talloc(session2,
4342 session1->smb2.signing_key);
4343 if (session2->smb2.signing_key.data == NULL) {
4344 return NT_STATUS_NO_MEMORY;
4347 session2->smb2.application_key = data_blob_dup_talloc(session2,
4348 session1->smb2.application_key);
4349 if (session2->smb2.application_key.data == NULL) {
4350 return NT_STATUS_NO_MEMORY;
4353 session2->smb2.should_sign = session1->smb2.should_sign;
4354 session2->smb2.should_encrypt = session1->smb2.should_encrypt;
4356 session2->smb2.channel_nonce = session1->smb2.channel_nonce;
4357 session2->smb2.channel_nonce += session1->smb2.channel_next;
4358 session1->smb2.channel_next++;
4360 session2->smb2.encryption_key = data_blob_dup_talloc(session2,
4361 session1->smb2.encryption_key);
4362 if (session2->smb2.encryption_key.data == NULL) {
4363 return NT_STATUS_NO_MEMORY;
4366 session2->smb2.decryption_key = data_blob_dup_talloc(session2,
4367 session1->smb2.decryption_key);
4368 if (session2->smb2.decryption_key.data == NULL) {
4369 return NT_STATUS_NO_MEMORY;
4372 talloc_set_destructor(session2, smbXcli_session_destructor);
4373 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4374 session2->conn = conn;
4376 *_session2 = session2;
4377 return NT_STATUS_OK;
4380 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4381 const DATA_BLOB _channel_key,
4382 const struct iovec *recv_iov)
4384 struct smbXcli_conn *conn = session->conn;
4385 uint8_t channel_key[16];
4386 NTSTATUS status;
4388 if (conn == NULL) {
4389 return NT_STATUS_INVALID_PARAMETER_MIX;
4392 if (session->smb2.channel_signing_key.length != 0) {
4393 return NT_STATUS_INVALID_PARAMETER_MIX;
4396 ZERO_STRUCT(channel_key);
4397 memcpy(channel_key, _channel_key.data,
4398 MIN(_channel_key.length, sizeof(channel_key)));
4400 session->smb2.channel_signing_key = data_blob_talloc(session,
4401 channel_key,
4402 sizeof(channel_key));
4403 if (session->smb2.channel_signing_key.data == NULL) {
4404 ZERO_STRUCT(channel_key);
4405 return NT_STATUS_NO_MEMORY;
4408 if (conn->protocol >= PROTOCOL_SMB2_24) {
4409 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4410 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4412 smb2_key_derivation(channel_key, sizeof(channel_key),
4413 label.data, label.length,
4414 context.data, context.length,
4415 session->smb2.channel_signing_key.data);
4417 ZERO_STRUCT(channel_key);
4419 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4420 session->conn->protocol,
4421 recv_iov, 3);
4422 if (!NT_STATUS_IS_OK(status)) {
4423 return status;
4426 return NT_STATUS_OK;