libcli/smb: encrypt SMB2 traffic if nedded/desired.
[Samba/gebeck_regimport.git] / libcli / smb / smbXcli_base.c
blobb85c5ab9a85c634f6187063d13e463f0cae7ce4b
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 transform[SMB2_TF_HDR_SIZE];
197 uint8_t hdr[SMB2_HDR_BODY];
198 uint8_t pad[7]; /* padding space for compounding */
201 * always an array of 3 talloc elements
202 * (without a SMB2_TRANSFORM header!)
204 * HDR, BODY, DYN
206 struct iovec *recv_iov;
208 uint16_t credit_charge;
210 bool should_sign;
211 bool should_encrypt;
213 bool signing_skipped;
214 bool notify_async;
215 bool got_async;
216 } smb2;
219 static int smbXcli_conn_destructor(struct smbXcli_conn *conn)
222 * NT_STATUS_OK, means we do not notify the callers
224 smbXcli_conn_disconnect(conn, NT_STATUS_OK);
226 while (conn->sessions) {
227 conn->sessions->conn = NULL;
228 DLIST_REMOVE(conn->sessions, conn->sessions);
231 if (conn->smb1.trans_enc) {
232 TALLOC_FREE(conn->smb1.trans_enc);
235 return 0;
238 struct smbXcli_conn *smbXcli_conn_create(TALLOC_CTX *mem_ctx,
239 int fd,
240 const char *remote_name,
241 enum smb_signing_setting signing_state,
242 uint32_t smb1_capabilities,
243 struct GUID *client_guid,
244 uint32_t smb2_capabilities)
246 struct smbXcli_conn *conn = NULL;
247 void *ss = NULL;
248 struct sockaddr *sa = NULL;
249 socklen_t sa_length;
250 int ret;
252 conn = talloc_zero(mem_ctx, struct smbXcli_conn);
253 if (!conn) {
254 return NULL;
257 conn->read_fd = fd;
258 conn->write_fd = dup(fd);
259 if (conn->write_fd == -1) {
260 goto error;
263 conn->remote_name = talloc_strdup(conn, remote_name);
264 if (conn->remote_name == NULL) {
265 goto error;
269 ss = (void *)&conn->local_ss;
270 sa = (struct sockaddr *)ss;
271 sa_length = sizeof(conn->local_ss);
272 ret = getsockname(fd, sa, &sa_length);
273 if (ret == -1) {
274 goto error;
276 ss = (void *)&conn->remote_ss;
277 sa = (struct sockaddr *)ss;
278 sa_length = sizeof(conn->remote_ss);
279 ret = getpeername(fd, sa, &sa_length);
280 if (ret == -1) {
281 goto error;
284 conn->outgoing = tevent_queue_create(conn, "smbXcli_outgoing");
285 if (conn->outgoing == NULL) {
286 goto error;
288 conn->pending = NULL;
290 conn->protocol = PROTOCOL_NONE;
292 switch (signing_state) {
293 case SMB_SIGNING_OFF:
294 /* never */
295 conn->allow_signing = false;
296 conn->desire_signing = false;
297 conn->mandatory_signing = false;
298 break;
299 case SMB_SIGNING_DEFAULT:
300 case SMB_SIGNING_IF_REQUIRED:
301 /* if the server requires it */
302 conn->allow_signing = true;
303 conn->desire_signing = false;
304 conn->mandatory_signing = false;
305 break;
306 case SMB_SIGNING_REQUIRED:
307 /* always */
308 conn->allow_signing = true;
309 conn->desire_signing = true;
310 conn->mandatory_signing = true;
311 break;
314 conn->smb1.client.capabilities = smb1_capabilities;
315 conn->smb1.client.max_xmit = UINT16_MAX;
317 conn->smb1.capabilities = conn->smb1.client.capabilities;
318 conn->smb1.max_xmit = 1024;
320 conn->smb1.mid = 1;
322 /* initialise signing */
323 conn->smb1.signing = smb_signing_init(conn,
324 conn->allow_signing,
325 conn->desire_signing,
326 conn->mandatory_signing);
327 if (!conn->smb1.signing) {
328 goto error;
331 conn->smb2.client.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
332 if (conn->mandatory_signing) {
333 conn->smb2.client.security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
335 if (client_guid) {
336 conn->smb2.client.guid = *client_guid;
338 conn->smb2.client.capabilities = smb2_capabilities;
340 conn->smb2.cur_credits = 1;
341 conn->smb2.max_credits = 0;
343 talloc_set_destructor(conn, smbXcli_conn_destructor);
344 return conn;
346 error:
347 if (conn->write_fd != -1) {
348 close(conn->write_fd);
350 TALLOC_FREE(conn);
351 return NULL;
354 bool smbXcli_conn_is_connected(struct smbXcli_conn *conn)
356 if (conn == NULL) {
357 return false;
360 if (conn->read_fd == -1) {
361 return false;
364 return true;
367 enum protocol_types smbXcli_conn_protocol(struct smbXcli_conn *conn)
369 return conn->protocol;
372 bool smbXcli_conn_use_unicode(struct smbXcli_conn *conn)
374 if (conn->protocol >= PROTOCOL_SMB2_02) {
375 return true;
378 if (conn->smb1.capabilities & CAP_UNICODE) {
379 return true;
382 return false;
385 void smbXcli_conn_set_sockopt(struct smbXcli_conn *conn, const char *options)
387 set_socket_options(conn->read_fd, options);
390 const struct sockaddr_storage *smbXcli_conn_local_sockaddr(struct smbXcli_conn *conn)
392 return &conn->local_ss;
395 const struct sockaddr_storage *smbXcli_conn_remote_sockaddr(struct smbXcli_conn *conn)
397 return &conn->remote_ss;
400 const char *smbXcli_conn_remote_name(struct smbXcli_conn *conn)
402 return conn->remote_name;
405 uint16_t smbXcli_conn_max_requests(struct smbXcli_conn *conn)
407 if (conn->protocol >= PROTOCOL_SMB2_02) {
409 * TODO...
411 return 1;
414 return conn->smb1.server.max_mux;
417 NTTIME smbXcli_conn_server_system_time(struct smbXcli_conn *conn)
419 if (conn->protocol >= PROTOCOL_SMB2_02) {
420 return conn->smb2.server.system_time;
423 return conn->smb1.server.system_time;
426 const DATA_BLOB *smbXcli_conn_server_gss_blob(struct smbXcli_conn *conn)
428 if (conn->protocol >= PROTOCOL_SMB2_02) {
429 return &conn->smb2.server.gss_blob;
432 return &conn->smb1.server.gss_blob;
435 const struct GUID *smbXcli_conn_server_guid(struct smbXcli_conn *conn)
437 if (conn->protocol >= PROTOCOL_SMB2_02) {
438 return &conn->smb2.server.guid;
441 return &conn->smb1.server.guid;
444 struct smbXcli_conn_samba_suicide_state {
445 struct smbXcli_conn *conn;
446 struct iovec iov;
447 uint8_t buf[9];
450 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq);
452 struct tevent_req *smbXcli_conn_samba_suicide_send(TALLOC_CTX *mem_ctx,
453 struct tevent_context *ev,
454 struct smbXcli_conn *conn,
455 uint8_t exitcode)
457 struct tevent_req *req, *subreq;
458 struct smbXcli_conn_samba_suicide_state *state;
460 req = tevent_req_create(mem_ctx, &state,
461 struct smbXcli_conn_samba_suicide_state);
462 if (req == NULL) {
463 return NULL;
465 state->conn = conn;
466 SIVAL(state->buf, 4, 0x74697865);
467 SCVAL(state->buf, 8, exitcode);
468 _smb_setlen_nbt(state->buf, sizeof(state->buf)-4);
470 state->iov.iov_base = state->buf;
471 state->iov.iov_len = sizeof(state->buf);
473 subreq = writev_send(state, ev, conn->outgoing, conn->write_fd,
474 false, &state->iov, 1);
475 if (tevent_req_nomem(subreq, req)) {
476 return tevent_req_post(req, ev);
478 tevent_req_set_callback(subreq, smbXcli_conn_samba_suicide_done, req);
479 return req;
482 static void smbXcli_conn_samba_suicide_done(struct tevent_req *subreq)
484 struct tevent_req *req = tevent_req_callback_data(
485 subreq, struct tevent_req);
486 struct smbXcli_conn_samba_suicide_state *state = tevent_req_data(
487 req, struct smbXcli_conn_samba_suicide_state);
488 ssize_t nwritten;
489 int err;
491 nwritten = writev_recv(subreq, &err);
492 TALLOC_FREE(subreq);
493 if (nwritten == -1) {
494 NTSTATUS status = map_nt_error_from_unix_common(err);
495 smbXcli_conn_disconnect(state->conn, status);
496 return;
498 tevent_req_done(req);
501 NTSTATUS smbXcli_conn_samba_suicide_recv(struct tevent_req *req)
503 return tevent_req_simple_recv_ntstatus(req);
506 NTSTATUS smbXcli_conn_samba_suicide(struct smbXcli_conn *conn,
507 uint8_t exitcode)
509 TALLOC_CTX *frame = talloc_stackframe();
510 struct tevent_context *ev;
511 struct tevent_req *req;
512 NTSTATUS status = NT_STATUS_NO_MEMORY;
513 bool ok;
515 if (smbXcli_conn_has_async_calls(conn)) {
517 * Can't use sync call while an async call is in flight
519 status = NT_STATUS_INVALID_PARAMETER_MIX;
520 goto fail;
522 ev = tevent_context_init(frame);
523 if (ev == NULL) {
524 goto fail;
526 req = smbXcli_conn_samba_suicide_send(frame, ev, conn, exitcode);
527 if (req == NULL) {
528 goto fail;
530 ok = tevent_req_poll(req, ev);
531 if (!ok) {
532 status = map_nt_error_from_unix_common(errno);
533 goto fail;
535 status = smbXcli_conn_samba_suicide_recv(req);
536 fail:
537 TALLOC_FREE(frame);
538 return status;
541 uint32_t smb1cli_conn_capabilities(struct smbXcli_conn *conn)
543 return conn->smb1.capabilities;
546 uint32_t smb1cli_conn_max_xmit(struct smbXcli_conn *conn)
548 return conn->smb1.max_xmit;
551 uint32_t smb1cli_conn_server_session_key(struct smbXcli_conn *conn)
553 return conn->smb1.server.session_key;
556 const uint8_t *smb1cli_conn_server_challenge(struct smbXcli_conn *conn)
558 return conn->smb1.server.challenge;
561 uint16_t smb1cli_conn_server_security_mode(struct smbXcli_conn *conn)
563 return conn->smb1.server.security_mode;
566 bool smb1cli_conn_server_readbraw(struct smbXcli_conn *conn)
568 return conn->smb1.server.readbraw;
571 bool smb1cli_conn_server_writebraw(struct smbXcli_conn *conn)
573 return conn->smb1.server.writebraw;
576 bool smb1cli_conn_server_lockread(struct smbXcli_conn *conn)
578 return conn->smb1.server.lockread;
581 bool smb1cli_conn_server_writeunlock(struct smbXcli_conn *conn)
583 return conn->smb1.server.writeunlock;
586 int smb1cli_conn_server_time_zone(struct smbXcli_conn *conn)
588 return conn->smb1.server.time_zone;
591 bool smb1cli_conn_activate_signing(struct smbXcli_conn *conn,
592 const DATA_BLOB user_session_key,
593 const DATA_BLOB response)
595 return smb_signing_activate(conn->smb1.signing,
596 user_session_key,
597 response);
600 bool smb1cli_conn_check_signing(struct smbXcli_conn *conn,
601 const uint8_t *buf, uint32_t seqnum)
603 return smb_signing_check_pdu(conn->smb1.signing, buf, seqnum);
606 bool smb1cli_conn_signing_is_active(struct smbXcli_conn *conn)
608 return smb_signing_is_active(conn->smb1.signing);
611 void smb1cli_conn_set_encryption(struct smbXcli_conn *conn,
612 struct smb_trans_enc_state *es)
614 /* Replace the old state, if any. */
615 if (conn->smb1.trans_enc) {
616 TALLOC_FREE(conn->smb1.trans_enc);
618 conn->smb1.trans_enc = es;
621 bool smb1cli_conn_encryption_on(struct smbXcli_conn *conn)
623 return common_encryption_on(conn->smb1.trans_enc);
627 static NTSTATUS smb1cli_pull_raw_error(const uint8_t *hdr)
629 uint32_t flags2 = SVAL(hdr, HDR_FLG2);
630 NTSTATUS status = NT_STATUS(IVAL(hdr, HDR_RCLS));
632 if (NT_STATUS_IS_OK(status)) {
633 return NT_STATUS_OK;
636 if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
637 return status;
640 return NT_STATUS_DOS(CVAL(hdr, HDR_RCLS), SVAL(hdr, HDR_ERR));
644 * Is the SMB command able to hold an AND_X successor
645 * @param[in] cmd The SMB command in question
646 * @retval Can we add a chained request after "cmd"?
648 bool smb1cli_is_andx_req(uint8_t cmd)
650 switch (cmd) {
651 case SMBtconX:
652 case SMBlockingX:
653 case SMBopenX:
654 case SMBreadX:
655 case SMBwriteX:
656 case SMBsesssetupX:
657 case SMBulogoffX:
658 case SMBntcreateX:
659 return true;
660 break;
661 default:
662 break;
665 return false;
668 static uint16_t smb1cli_alloc_mid(struct smbXcli_conn *conn)
670 size_t num_pending = talloc_array_length(conn->pending);
671 uint16_t result;
673 while (true) {
674 size_t i;
676 result = conn->smb1.mid++;
677 if ((result == 0) || (result == 0xffff)) {
678 continue;
681 for (i=0; i<num_pending; i++) {
682 if (result == smb1cli_req_mid(conn->pending[i])) {
683 break;
687 if (i == num_pending) {
688 return result;
693 void smbXcli_req_unset_pending(struct tevent_req *req)
695 struct smbXcli_req_state *state =
696 tevent_req_data(req,
697 struct smbXcli_req_state);
698 struct smbXcli_conn *conn = state->conn;
699 size_t num_pending = talloc_array_length(conn->pending);
700 size_t i;
702 if (state->smb1.mid != 0) {
704 * This is a [nt]trans[2] request which waits
705 * for more than one reply.
707 return;
710 talloc_set_destructor(req, NULL);
712 if (num_pending == 1) {
714 * The pending read_smb tevent_req is a child of
715 * conn->pending. So if nothing is pending anymore, we need to
716 * delete the socket read fde.
718 TALLOC_FREE(conn->pending);
719 conn->read_smb_req = NULL;
720 return;
723 for (i=0; i<num_pending; i++) {
724 if (req == conn->pending[i]) {
725 break;
728 if (i == num_pending) {
730 * Something's seriously broken. Just returning here is the
731 * right thing nevertheless, the point of this routine is to
732 * remove ourselves from conn->pending.
734 return;
738 * Remove ourselves from the conn->pending array
740 for (; i < (num_pending - 1); i++) {
741 conn->pending[i] = conn->pending[i+1];
745 * No NULL check here, we're shrinking by sizeof(void *), and
746 * talloc_realloc just adjusts the size for this.
748 conn->pending = talloc_realloc(NULL, conn->pending, struct tevent_req *,
749 num_pending - 1);
750 return;
753 static int smbXcli_req_destructor(struct tevent_req *req)
755 struct smbXcli_req_state *state =
756 tevent_req_data(req,
757 struct smbXcli_req_state);
760 * Make sure we really remove it from
761 * the pending array on destruction.
763 state->smb1.mid = 0;
764 smbXcli_req_unset_pending(req);
765 return 0;
768 static bool smb1cli_req_cancel(struct tevent_req *req);
769 static bool smb2cli_req_cancel(struct tevent_req *req);
771 static bool smbXcli_req_cancel(struct tevent_req *req)
773 struct smbXcli_req_state *state =
774 tevent_req_data(req,
775 struct smbXcli_req_state);
777 if (!smbXcli_conn_is_connected(state->conn)) {
778 return false;
781 if (state->conn->protocol == PROTOCOL_NONE) {
782 return false;
785 if (state->conn->protocol >= PROTOCOL_SMB2_02) {
786 return smb2cli_req_cancel(req);
789 return smb1cli_req_cancel(req);
792 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn);
794 bool smbXcli_req_set_pending(struct tevent_req *req)
796 struct smbXcli_req_state *state =
797 tevent_req_data(req,
798 struct smbXcli_req_state);
799 struct smbXcli_conn *conn;
800 struct tevent_req **pending;
801 size_t num_pending;
803 conn = state->conn;
805 if (!smbXcli_conn_is_connected(conn)) {
806 return false;
809 num_pending = talloc_array_length(conn->pending);
811 pending = talloc_realloc(conn, conn->pending, struct tevent_req *,
812 num_pending+1);
813 if (pending == NULL) {
814 return false;
816 pending[num_pending] = req;
817 conn->pending = pending;
818 talloc_set_destructor(req, smbXcli_req_destructor);
819 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
821 if (!smbXcli_conn_receive_next(conn)) {
823 * the caller should notify the current request
825 * And all other pending requests get notified
826 * by smbXcli_conn_disconnect().
828 smbXcli_req_unset_pending(req);
829 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
830 return false;
833 return true;
836 static void smbXcli_conn_received(struct tevent_req *subreq);
838 static bool smbXcli_conn_receive_next(struct smbXcli_conn *conn)
840 size_t num_pending = talloc_array_length(conn->pending);
841 struct tevent_req *req;
842 struct smbXcli_req_state *state;
844 if (conn->read_smb_req != NULL) {
845 return true;
848 if (num_pending == 0) {
849 if (conn->smb2.mid < UINT64_MAX) {
850 /* no more pending requests, so we are done for now */
851 return true;
855 * If there are no more SMB2 requests possible,
856 * because we are out of message ids,
857 * we need to disconnect.
859 smbXcli_conn_disconnect(conn, NT_STATUS_CONNECTION_ABORTED);
860 return true;
863 req = conn->pending[0];
864 state = tevent_req_data(req, struct smbXcli_req_state);
867 * We're the first ones, add the read_smb request that waits for the
868 * answer from the server
870 conn->read_smb_req = read_smb_send(conn->pending,
871 state->ev,
872 conn->read_fd);
873 if (conn->read_smb_req == NULL) {
874 return false;
876 tevent_req_set_callback(conn->read_smb_req, smbXcli_conn_received, conn);
877 return true;
880 void smbXcli_conn_disconnect(struct smbXcli_conn *conn, NTSTATUS status)
882 tevent_queue_stop(conn->outgoing);
884 if (conn->read_fd != -1) {
885 close(conn->read_fd);
887 if (conn->write_fd != -1) {
888 close(conn->write_fd);
890 conn->read_fd = -1;
891 conn->write_fd = -1;
894 * Cancel all pending requests. We do not do a for-loop walking
895 * conn->pending because that array changes in
896 * smbXcli_req_unset_pending.
898 while (talloc_array_length(conn->pending) > 0) {
899 struct tevent_req *req;
900 struct smbXcli_req_state *state;
901 struct tevent_req **chain;
902 size_t num_chained;
903 size_t i;
905 req = conn->pending[0];
906 state = tevent_req_data(req, struct smbXcli_req_state);
908 if (state->smb1.chained_requests == NULL) {
910 * We're dead. No point waiting for trans2
911 * replies.
913 state->smb1.mid = 0;
915 smbXcli_req_unset_pending(req);
917 if (NT_STATUS_IS_OK(status)) {
918 /* do not notify the callers */
919 continue;
923 * we need to defer the callback, because we may notify
924 * more then one caller.
926 tevent_req_defer_callback(req, state->ev);
927 tevent_req_nterror(req, status);
928 continue;
931 chain = talloc_move(conn, &state->smb1.chained_requests);
932 num_chained = talloc_array_length(chain);
934 for (i=0; i<num_chained; i++) {
935 req = chain[i];
936 state = tevent_req_data(req, struct smbXcli_req_state);
939 * We're dead. No point waiting for trans2
940 * replies.
942 state->smb1.mid = 0;
944 smbXcli_req_unset_pending(req);
946 if (NT_STATUS_IS_OK(status)) {
947 /* do not notify the callers */
948 continue;
952 * we need to defer the callback, because we may notify
953 * more than one caller.
955 tevent_req_defer_callback(req, state->ev);
956 tevent_req_nterror(req, status);
958 TALLOC_FREE(chain);
963 * Fetch a smb request's mid. Only valid after the request has been sent by
964 * smb1cli_req_send().
966 uint16_t smb1cli_req_mid(struct tevent_req *req)
968 struct smbXcli_req_state *state =
969 tevent_req_data(req,
970 struct smbXcli_req_state);
972 if (state->smb1.mid != 0) {
973 return state->smb1.mid;
976 return SVAL(state->smb1.hdr, HDR_MID);
979 void smb1cli_req_set_mid(struct tevent_req *req, uint16_t mid)
981 struct smbXcli_req_state *state =
982 tevent_req_data(req,
983 struct smbXcli_req_state);
985 state->smb1.mid = mid;
988 uint32_t smb1cli_req_seqnum(struct tevent_req *req)
990 struct smbXcli_req_state *state =
991 tevent_req_data(req,
992 struct smbXcli_req_state);
994 return state->smb1.seqnum;
997 void smb1cli_req_set_seqnum(struct tevent_req *req, uint32_t seqnum)
999 struct smbXcli_req_state *state =
1000 tevent_req_data(req,
1001 struct smbXcli_req_state);
1003 state->smb1.seqnum = seqnum;
1006 static size_t smbXcli_iov_len(const struct iovec *iov, int count)
1008 size_t result = 0;
1009 int i;
1010 for (i=0; i<count; i++) {
1011 result += iov[i].iov_len;
1013 return result;
1016 static uint8_t *smbXcli_iov_concat(TALLOC_CTX *mem_ctx,
1017 const struct iovec *iov,
1018 int count)
1020 size_t len = smbXcli_iov_len(iov, count);
1021 size_t copied;
1022 uint8_t *buf;
1023 int i;
1025 buf = talloc_array(mem_ctx, uint8_t, len);
1026 if (buf == NULL) {
1027 return NULL;
1029 copied = 0;
1030 for (i=0; i<count; i++) {
1031 memcpy(buf+copied, iov[i].iov_base, iov[i].iov_len);
1032 copied += iov[i].iov_len;
1034 return buf;
1037 static void smb1cli_req_flags(enum protocol_types protocol,
1038 uint32_t smb1_capabilities,
1039 uint8_t smb_command,
1040 uint8_t additional_flags,
1041 uint8_t clear_flags,
1042 uint8_t *_flags,
1043 uint16_t additional_flags2,
1044 uint16_t clear_flags2,
1045 uint16_t *_flags2)
1047 uint8_t flags = 0;
1048 uint16_t flags2 = 0;
1050 if (protocol >= PROTOCOL_LANMAN1) {
1051 flags |= FLAG_CASELESS_PATHNAMES;
1052 flags |= FLAG_CANONICAL_PATHNAMES;
1055 if (protocol >= PROTOCOL_LANMAN2) {
1056 flags2 |= FLAGS2_LONG_PATH_COMPONENTS;
1057 flags2 |= FLAGS2_EXTENDED_ATTRIBUTES;
1060 if (protocol >= PROTOCOL_NT1) {
1061 flags2 |= FLAGS2_IS_LONG_NAME;
1063 if (smb1_capabilities & CAP_UNICODE) {
1064 flags2 |= FLAGS2_UNICODE_STRINGS;
1066 if (smb1_capabilities & CAP_STATUS32) {
1067 flags2 |= FLAGS2_32_BIT_ERROR_CODES;
1069 if (smb1_capabilities & CAP_EXTENDED_SECURITY) {
1070 flags2 |= FLAGS2_EXTENDED_SECURITY;
1074 flags |= additional_flags;
1075 flags &= ~clear_flags;
1076 flags2 |= additional_flags2;
1077 flags2 &= ~clear_flags2;
1079 *_flags = flags;
1080 *_flags2 = flags2;
1083 static void smb1cli_req_cancel_done(struct tevent_req *subreq);
1085 static bool smb1cli_req_cancel(struct tevent_req *req)
1087 struct smbXcli_req_state *state =
1088 tevent_req_data(req,
1089 struct smbXcli_req_state);
1090 uint8_t flags;
1091 uint16_t flags2;
1092 uint32_t pid;
1093 uint16_t tid;
1094 uint16_t uid;
1095 uint16_t mid;
1096 struct tevent_req *subreq;
1097 NTSTATUS status;
1099 flags = CVAL(state->smb1.hdr, HDR_FLG);
1100 flags2 = SVAL(state->smb1.hdr, HDR_FLG2);
1101 pid = SVAL(state->smb1.hdr, HDR_PID);
1102 pid |= SVAL(state->smb1.hdr, HDR_PIDHIGH)<<16;
1103 tid = SVAL(state->smb1.hdr, HDR_TID);
1104 uid = SVAL(state->smb1.hdr, HDR_UID);
1105 mid = SVAL(state->smb1.hdr, HDR_MID);
1107 subreq = smb1cli_req_create(state, state->ev,
1108 state->conn,
1109 SMBntcancel,
1110 flags, 0,
1111 flags2, 0,
1112 0, /* timeout */
1113 pid, tid, uid,
1114 0, NULL, /* vwv */
1115 0, NULL); /* bytes */
1116 if (subreq == NULL) {
1117 return false;
1119 smb1cli_req_set_mid(subreq, mid);
1121 status = smb1cli_req_chain_submit(&subreq, 1);
1122 if (!NT_STATUS_IS_OK(status)) {
1123 TALLOC_FREE(subreq);
1124 return false;
1126 smb1cli_req_set_mid(subreq, 0);
1128 tevent_req_set_callback(subreq, smb1cli_req_cancel_done, NULL);
1130 return true;
1133 static void smb1cli_req_cancel_done(struct tevent_req *subreq)
1135 /* we do not care about the result */
1136 TALLOC_FREE(subreq);
1139 struct tevent_req *smb1cli_req_create(TALLOC_CTX *mem_ctx,
1140 struct tevent_context *ev,
1141 struct smbXcli_conn *conn,
1142 uint8_t smb_command,
1143 uint8_t additional_flags,
1144 uint8_t clear_flags,
1145 uint16_t additional_flags2,
1146 uint16_t clear_flags2,
1147 uint32_t timeout_msec,
1148 uint32_t pid,
1149 uint16_t tid,
1150 uint16_t uid,
1151 uint8_t wct, uint16_t *vwv,
1152 int iov_count,
1153 struct iovec *bytes_iov)
1155 struct tevent_req *req;
1156 struct smbXcli_req_state *state;
1157 uint8_t flags = 0;
1158 uint16_t flags2 = 0;
1160 if (iov_count > MAX_SMB_IOV) {
1162 * Should not happen :-)
1164 return NULL;
1167 req = tevent_req_create(mem_ctx, &state,
1168 struct smbXcli_req_state);
1169 if (req == NULL) {
1170 return NULL;
1172 state->ev = ev;
1173 state->conn = conn;
1175 state->smb1.recv_cmd = 0xFF;
1176 state->smb1.recv_status = NT_STATUS_INTERNAL_ERROR;
1177 state->smb1.recv_iov = talloc_zero_array(state, struct iovec, 3);
1178 if (state->smb1.recv_iov == NULL) {
1179 TALLOC_FREE(req);
1180 return NULL;
1183 smb1cli_req_flags(conn->protocol,
1184 conn->smb1.capabilities,
1185 smb_command,
1186 additional_flags,
1187 clear_flags,
1188 &flags,
1189 additional_flags2,
1190 clear_flags2,
1191 &flags2);
1193 SIVAL(state->smb1.hdr, 0, SMB_MAGIC);
1194 SCVAL(state->smb1.hdr, HDR_COM, smb_command);
1195 SIVAL(state->smb1.hdr, HDR_RCLS, NT_STATUS_V(NT_STATUS_OK));
1196 SCVAL(state->smb1.hdr, HDR_FLG, flags);
1197 SSVAL(state->smb1.hdr, HDR_FLG2, flags2);
1198 SSVAL(state->smb1.hdr, HDR_PIDHIGH, pid >> 16);
1199 SSVAL(state->smb1.hdr, HDR_TID, tid);
1200 SSVAL(state->smb1.hdr, HDR_PID, pid);
1201 SSVAL(state->smb1.hdr, HDR_UID, uid);
1202 SSVAL(state->smb1.hdr, HDR_MID, 0); /* this comes later */
1203 SCVAL(state->smb1.hdr, HDR_WCT, wct);
1205 state->smb1.vwv = vwv;
1207 SSVAL(state->smb1.bytecount_buf, 0, smbXcli_iov_len(bytes_iov, iov_count));
1209 state->smb1.iov[0].iov_base = (void *)state->length_hdr;
1210 state->smb1.iov[0].iov_len = sizeof(state->length_hdr);
1211 state->smb1.iov[1].iov_base = (void *)state->smb1.hdr;
1212 state->smb1.iov[1].iov_len = sizeof(state->smb1.hdr);
1213 state->smb1.iov[2].iov_base = (void *)state->smb1.vwv;
1214 state->smb1.iov[2].iov_len = wct * sizeof(uint16_t);
1215 state->smb1.iov[3].iov_base = (void *)state->smb1.bytecount_buf;
1216 state->smb1.iov[3].iov_len = sizeof(uint16_t);
1218 if (iov_count != 0) {
1219 memcpy(&state->smb1.iov[4], bytes_iov,
1220 iov_count * sizeof(*bytes_iov));
1222 state->smb1.iov_count = iov_count + 4;
1224 if (timeout_msec > 0) {
1225 struct timeval endtime;
1227 endtime = timeval_current_ofs_msec(timeout_msec);
1228 if (!tevent_req_set_endtime(req, ev, endtime)) {
1229 return req;
1233 switch (smb_command) {
1234 case SMBtranss:
1235 case SMBtranss2:
1236 case SMBnttranss:
1237 state->one_way = true;
1238 break;
1239 case SMBntcancel:
1240 state->one_way = true;
1241 state->smb1.one_way_seqnum = true;
1242 break;
1243 case SMBlockingX:
1244 if ((wct == 8) &&
1245 (CVAL(vwv+3, 0) == LOCKING_ANDX_OPLOCK_RELEASE)) {
1246 state->one_way = true;
1248 break;
1251 return req;
1254 static NTSTATUS smb1cli_conn_signv(struct smbXcli_conn *conn,
1255 struct iovec *iov, int iov_count,
1256 uint32_t *seqnum,
1257 bool one_way_seqnum)
1259 TALLOC_CTX *frame = NULL;
1260 uint8_t *buf;
1263 * Obvious optimization: Make cli_calculate_sign_mac work with struct
1264 * iovec directly. MD5Update would do that just fine.
1267 if (iov_count < 4) {
1268 return NT_STATUS_INVALID_PARAMETER_MIX;
1270 if (iov[0].iov_len != NBT_HDR_SIZE) {
1271 return NT_STATUS_INVALID_PARAMETER_MIX;
1273 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1274 return NT_STATUS_INVALID_PARAMETER_MIX;
1276 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1277 return NT_STATUS_INVALID_PARAMETER_MIX;
1279 if (iov[3].iov_len != sizeof(uint16_t)) {
1280 return NT_STATUS_INVALID_PARAMETER_MIX;
1283 frame = talloc_stackframe();
1285 buf = smbXcli_iov_concat(frame, iov, iov_count);
1286 if (buf == NULL) {
1287 return NT_STATUS_NO_MEMORY;
1290 *seqnum = smb_signing_next_seqnum(conn->smb1.signing,
1291 one_way_seqnum);
1292 smb_signing_sign_pdu(conn->smb1.signing, buf, *seqnum);
1293 memcpy(iov[1].iov_base, buf+4, iov[1].iov_len);
1295 TALLOC_FREE(frame);
1296 return NT_STATUS_OK;
1299 static void smb1cli_req_writev_done(struct tevent_req *subreq);
1300 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1301 TALLOC_CTX *tmp_mem,
1302 uint8_t *inbuf);
1304 static NTSTATUS smb1cli_req_writev_submit(struct tevent_req *req,
1305 struct smbXcli_req_state *state,
1306 struct iovec *iov, int iov_count)
1308 struct tevent_req *subreq;
1309 NTSTATUS status;
1310 uint8_t cmd;
1311 uint16_t mid;
1313 if (!smbXcli_conn_is_connected(state->conn)) {
1314 return NT_STATUS_CONNECTION_DISCONNECTED;
1317 if (state->conn->protocol > PROTOCOL_NT1) {
1318 return NT_STATUS_REVISION_MISMATCH;
1321 if (iov_count < 4) {
1322 return NT_STATUS_INVALID_PARAMETER_MIX;
1324 if (iov[0].iov_len != NBT_HDR_SIZE) {
1325 return NT_STATUS_INVALID_PARAMETER_MIX;
1327 if (iov[1].iov_len != (MIN_SMB_SIZE-sizeof(uint16_t))) {
1328 return NT_STATUS_INVALID_PARAMETER_MIX;
1330 if (iov[2].iov_len > (0xFF * sizeof(uint16_t))) {
1331 return NT_STATUS_INVALID_PARAMETER_MIX;
1333 if (iov[3].iov_len != sizeof(uint16_t)) {
1334 return NT_STATUS_INVALID_PARAMETER_MIX;
1337 cmd = CVAL(iov[1].iov_base, HDR_COM);
1338 if (cmd == SMBreadBraw) {
1339 if (smbXcli_conn_has_async_calls(state->conn)) {
1340 return NT_STATUS_INVALID_PARAMETER_MIX;
1342 state->conn->smb1.read_braw_req = req;
1345 if (state->smb1.mid != 0) {
1346 mid = state->smb1.mid;
1347 } else {
1348 mid = smb1cli_alloc_mid(state->conn);
1350 SSVAL(iov[1].iov_base, HDR_MID, mid);
1352 _smb_setlen_nbt(iov[0].iov_base, smbXcli_iov_len(&iov[1], iov_count-1));
1354 status = smb1cli_conn_signv(state->conn, iov, iov_count,
1355 &state->smb1.seqnum,
1356 state->smb1.one_way_seqnum);
1358 if (!NT_STATUS_IS_OK(status)) {
1359 return status;
1363 * If we supported multiple encrytion contexts
1364 * here we'd look up based on tid.
1366 if (common_encryption_on(state->conn->smb1.trans_enc)) {
1367 char *buf, *enc_buf;
1369 buf = (char *)smbXcli_iov_concat(talloc_tos(), iov, iov_count);
1370 if (buf == NULL) {
1371 return NT_STATUS_NO_MEMORY;
1373 status = common_encrypt_buffer(state->conn->smb1.trans_enc,
1374 (char *)buf, &enc_buf);
1375 TALLOC_FREE(buf);
1376 if (!NT_STATUS_IS_OK(status)) {
1377 DEBUG(0, ("Error in encrypting client message: %s\n",
1378 nt_errstr(status)));
1379 return status;
1381 buf = (char *)talloc_memdup(state, enc_buf,
1382 smb_len_nbt(enc_buf)+4);
1383 SAFE_FREE(enc_buf);
1384 if (buf == NULL) {
1385 return NT_STATUS_NO_MEMORY;
1387 iov[0].iov_base = (void *)buf;
1388 iov[0].iov_len = talloc_get_size(buf);
1389 iov_count = 1;
1392 if (state->conn->dispatch_incoming == NULL) {
1393 state->conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
1396 tevent_req_set_cancel_fn(req, smbXcli_req_cancel);
1398 subreq = writev_send(state, state->ev, state->conn->outgoing,
1399 state->conn->write_fd, false, iov, iov_count);
1400 if (subreq == NULL) {
1401 return NT_STATUS_NO_MEMORY;
1403 tevent_req_set_callback(subreq, smb1cli_req_writev_done, req);
1404 return NT_STATUS_OK;
1407 struct tevent_req *smb1cli_req_send(TALLOC_CTX *mem_ctx,
1408 struct tevent_context *ev,
1409 struct smbXcli_conn *conn,
1410 uint8_t smb_command,
1411 uint8_t additional_flags,
1412 uint8_t clear_flags,
1413 uint16_t additional_flags2,
1414 uint16_t clear_flags2,
1415 uint32_t timeout_msec,
1416 uint32_t pid,
1417 uint16_t tid,
1418 uint16_t uid,
1419 uint8_t wct, uint16_t *vwv,
1420 uint32_t num_bytes,
1421 const uint8_t *bytes)
1423 struct tevent_req *req;
1424 struct iovec iov;
1425 NTSTATUS status;
1427 iov.iov_base = discard_const_p(void, bytes);
1428 iov.iov_len = num_bytes;
1430 req = smb1cli_req_create(mem_ctx, ev, conn, smb_command,
1431 additional_flags, clear_flags,
1432 additional_flags2, clear_flags2,
1433 timeout_msec,
1434 pid, tid, uid,
1435 wct, vwv, 1, &iov);
1436 if (req == NULL) {
1437 return NULL;
1439 if (!tevent_req_is_in_progress(req)) {
1440 return tevent_req_post(req, ev);
1442 status = smb1cli_req_chain_submit(&req, 1);
1443 if (tevent_req_nterror(req, status)) {
1444 return tevent_req_post(req, ev);
1446 return req;
1449 static void smb1cli_req_writev_done(struct tevent_req *subreq)
1451 struct tevent_req *req =
1452 tevent_req_callback_data(subreq,
1453 struct tevent_req);
1454 struct smbXcli_req_state *state =
1455 tevent_req_data(req,
1456 struct smbXcli_req_state);
1457 ssize_t nwritten;
1458 int err;
1460 nwritten = writev_recv(subreq, &err);
1461 TALLOC_FREE(subreq);
1462 if (nwritten == -1) {
1463 NTSTATUS status = map_nt_error_from_unix_common(err);
1464 smbXcli_conn_disconnect(state->conn, status);
1465 return;
1468 if (state->one_way) {
1469 state->inbuf = NULL;
1470 tevent_req_done(req);
1471 return;
1474 if (!smbXcli_req_set_pending(req)) {
1475 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1476 return;
1480 static void smbXcli_conn_received(struct tevent_req *subreq)
1482 struct smbXcli_conn *conn =
1483 tevent_req_callback_data(subreq,
1484 struct smbXcli_conn);
1485 TALLOC_CTX *frame = talloc_stackframe();
1486 NTSTATUS status;
1487 uint8_t *inbuf;
1488 ssize_t received;
1489 int err;
1491 if (subreq != conn->read_smb_req) {
1492 DEBUG(1, ("Internal error: cli_smb_received called with "
1493 "unexpected subreq\n"));
1494 status = NT_STATUS_INTERNAL_ERROR;
1495 smbXcli_conn_disconnect(conn, status);
1496 TALLOC_FREE(frame);
1497 return;
1499 conn->read_smb_req = NULL;
1501 received = read_smb_recv(subreq, frame, &inbuf, &err);
1502 TALLOC_FREE(subreq);
1503 if (received == -1) {
1504 status = map_nt_error_from_unix_common(err);
1505 smbXcli_conn_disconnect(conn, status);
1506 TALLOC_FREE(frame);
1507 return;
1510 status = conn->dispatch_incoming(conn, frame, inbuf);
1511 TALLOC_FREE(frame);
1512 if (NT_STATUS_IS_OK(status)) {
1514 * We should not do any more processing
1515 * as the dispatch function called
1516 * tevent_req_done().
1518 return;
1519 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1521 * We got an error, so notify all pending requests
1523 smbXcli_conn_disconnect(conn, status);
1524 return;
1528 * We got NT_STATUS_RETRY, so we may ask for a
1529 * next incoming pdu.
1531 if (!smbXcli_conn_receive_next(conn)) {
1532 smbXcli_conn_disconnect(conn, NT_STATUS_NO_MEMORY);
1536 static NTSTATUS smb1cli_inbuf_parse_chain(uint8_t *buf, TALLOC_CTX *mem_ctx,
1537 struct iovec **piov, int *pnum_iov)
1539 struct iovec *iov;
1540 int num_iov;
1541 size_t buflen;
1542 size_t taken;
1543 size_t remaining;
1544 uint8_t *hdr;
1545 uint8_t cmd;
1546 uint32_t wct_ofs;
1548 buflen = smb_len_nbt(buf);
1549 taken = 0;
1551 hdr = buf + NBT_HDR_SIZE;
1553 if (buflen < MIN_SMB_SIZE) {
1554 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1558 * This returns iovec elements in the following order:
1560 * - SMB header
1562 * - Parameter Block
1563 * - Data Block
1565 * - Parameter Block
1566 * - Data Block
1568 * - Parameter Block
1569 * - Data Block
1571 num_iov = 1;
1573 iov = talloc_array(mem_ctx, struct iovec, num_iov);
1574 if (iov == NULL) {
1575 return NT_STATUS_NO_MEMORY;
1577 iov[0].iov_base = hdr;
1578 iov[0].iov_len = HDR_WCT;
1579 taken += HDR_WCT;
1581 cmd = CVAL(hdr, HDR_COM);
1582 wct_ofs = HDR_WCT;
1584 while (true) {
1585 size_t len = buflen - taken;
1586 struct iovec *cur;
1587 struct iovec *iov_tmp;
1588 uint8_t wct;
1589 uint32_t bcc_ofs;
1590 uint16_t bcc;
1591 size_t needed;
1594 * we need at least WCT and BCC
1596 needed = sizeof(uint8_t) + sizeof(uint16_t);
1597 if (len < needed) {
1598 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1599 __location__, (int)len, (int)needed));
1600 goto inval;
1604 * Now we check if the specified words are there
1606 wct = CVAL(hdr, wct_ofs);
1607 needed += wct * sizeof(uint16_t);
1608 if (len < needed) {
1609 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1610 __location__, (int)len, (int)needed));
1611 goto inval;
1615 * Now we check if the specified bytes are there
1617 bcc_ofs = wct_ofs + sizeof(uint8_t) + wct * sizeof(uint16_t);
1618 bcc = SVAL(hdr, bcc_ofs);
1619 needed += bcc * sizeof(uint8_t);
1620 if (len < needed) {
1621 DEBUG(10, ("%s: %d bytes left, expected at least %d\n",
1622 __location__, (int)len, (int)needed));
1623 goto inval;
1627 * we allocate 2 iovec structures for words and bytes
1629 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
1630 num_iov + 2);
1631 if (iov_tmp == NULL) {
1632 TALLOC_FREE(iov);
1633 return NT_STATUS_NO_MEMORY;
1635 iov = iov_tmp;
1636 cur = &iov[num_iov];
1637 num_iov += 2;
1639 cur[0].iov_len = wct * sizeof(uint16_t);
1640 cur[0].iov_base = hdr + (wct_ofs + sizeof(uint8_t));
1641 cur[1].iov_len = bcc * sizeof(uint8_t);
1642 cur[1].iov_base = hdr + (bcc_ofs + sizeof(uint16_t));
1644 taken += needed;
1646 if (!smb1cli_is_andx_req(cmd)) {
1648 * If the current command does not have AndX chanining
1649 * we are done.
1651 break;
1654 if (wct == 0 && bcc == 0) {
1656 * An empty response also ends the chain,
1657 * most likely with an error.
1659 break;
1662 if (wct < 2) {
1663 DEBUG(10, ("%s: wct[%d] < 2 for cmd[0x%02X]\n",
1664 __location__, (int)wct, (int)cmd));
1665 goto inval;
1667 cmd = CVAL(cur[0].iov_base, 0);
1668 if (cmd == 0xFF) {
1670 * If it is the end of the chain we are also done.
1672 break;
1674 wct_ofs = SVAL(cur[0].iov_base, 2);
1676 if (wct_ofs < taken) {
1677 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1679 if (wct_ofs > buflen) {
1680 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1684 * we consumed everything up to the start of the next
1685 * parameter block.
1687 taken = wct_ofs;
1690 remaining = buflen - taken;
1692 if (remaining > 0 && num_iov >= 3) {
1694 * The last DATA block gets the remaining
1695 * bytes, this is needed to support
1696 * CAP_LARGE_WRITEX and CAP_LARGE_READX.
1698 iov[num_iov-1].iov_len += remaining;
1701 *piov = iov;
1702 *pnum_iov = num_iov;
1703 return NT_STATUS_OK;
1705 inval:
1706 TALLOC_FREE(iov);
1707 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1710 static NTSTATUS smb1cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
1711 TALLOC_CTX *tmp_mem,
1712 uint8_t *inbuf)
1714 struct tevent_req *req;
1715 struct smbXcli_req_state *state;
1716 NTSTATUS status;
1717 size_t num_pending;
1718 size_t i;
1719 uint8_t cmd;
1720 uint16_t mid;
1721 bool oplock_break;
1722 const uint8_t *inhdr = inbuf + NBT_HDR_SIZE;
1723 struct iovec *iov = NULL;
1724 int num_iov = 0;
1725 struct tevent_req **chain = NULL;
1726 size_t num_chained = 0;
1727 size_t num_responses = 0;
1729 if (conn->smb1.read_braw_req != NULL) {
1730 req = conn->smb1.read_braw_req;
1731 conn->smb1.read_braw_req = NULL;
1732 state = tevent_req_data(req, struct smbXcli_req_state);
1734 smbXcli_req_unset_pending(req);
1736 if (state->smb1.recv_iov == NULL) {
1738 * For requests with more than
1739 * one response, we have to readd the
1740 * recv_iov array.
1742 state->smb1.recv_iov = talloc_zero_array(state,
1743 struct iovec,
1745 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1746 return NT_STATUS_OK;
1750 state->smb1.recv_iov[0].iov_base = (void *)(inbuf + NBT_HDR_SIZE);
1751 state->smb1.recv_iov[0].iov_len = smb_len_nbt(inbuf);
1752 ZERO_STRUCT(state->smb1.recv_iov[1]);
1753 ZERO_STRUCT(state->smb1.recv_iov[2]);
1755 state->smb1.recv_cmd = SMBreadBraw;
1756 state->smb1.recv_status = NT_STATUS_OK;
1757 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1759 tevent_req_done(req);
1760 return NT_STATUS_OK;
1763 if ((IVAL(inhdr, 0) != SMB_MAGIC) /* 0xFF"SMB" */
1764 && (SVAL(inhdr, 0) != 0x45ff)) /* 0xFF"E" */ {
1765 DEBUG(10, ("Got non-SMB PDU\n"));
1766 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1770 * If we supported multiple encrytion contexts
1771 * here we'd look up based on tid.
1773 if (common_encryption_on(conn->smb1.trans_enc)
1774 && (CVAL(inbuf, 0) == 0)) {
1775 uint16_t enc_ctx_num;
1777 status = get_enc_ctx_num(inbuf, &enc_ctx_num);
1778 if (!NT_STATUS_IS_OK(status)) {
1779 DEBUG(10, ("get_enc_ctx_num returned %s\n",
1780 nt_errstr(status)));
1781 return status;
1784 if (enc_ctx_num != conn->smb1.trans_enc->enc_ctx_num) {
1785 DEBUG(10, ("wrong enc_ctx %d, expected %d\n",
1786 enc_ctx_num,
1787 conn->smb1.trans_enc->enc_ctx_num));
1788 return NT_STATUS_INVALID_HANDLE;
1791 status = common_decrypt_buffer(conn->smb1.trans_enc,
1792 (char *)inbuf);
1793 if (!NT_STATUS_IS_OK(status)) {
1794 DEBUG(10, ("common_decrypt_buffer returned %s\n",
1795 nt_errstr(status)));
1796 return status;
1800 mid = SVAL(inhdr, HDR_MID);
1801 num_pending = talloc_array_length(conn->pending);
1803 for (i=0; i<num_pending; i++) {
1804 if (mid == smb1cli_req_mid(conn->pending[i])) {
1805 break;
1808 if (i == num_pending) {
1809 /* Dump unexpected reply */
1810 return NT_STATUS_RETRY;
1813 oplock_break = false;
1815 if (mid == 0xffff) {
1817 * Paranoia checks that this is really an oplock break request.
1819 oplock_break = (smb_len_nbt(inbuf) == 51); /* hdr + 8 words */
1820 oplock_break &= ((CVAL(inhdr, HDR_FLG) & FLAG_REPLY) == 0);
1821 oplock_break &= (CVAL(inhdr, HDR_COM) == SMBlockingX);
1822 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(6)) == 0);
1823 oplock_break &= (SVAL(inhdr, HDR_VWV+VWV(7)) == 0);
1825 if (!oplock_break) {
1826 /* Dump unexpected reply */
1827 return NT_STATUS_RETRY;
1831 req = conn->pending[i];
1832 state = tevent_req_data(req, struct smbXcli_req_state);
1834 if (!oplock_break /* oplock breaks are not signed */
1835 && !smb_signing_check_pdu(conn->smb1.signing,
1836 inbuf, state->smb1.seqnum+1)) {
1837 DEBUG(10, ("cli_check_sign_mac failed\n"));
1838 return NT_STATUS_ACCESS_DENIED;
1841 status = smb1cli_inbuf_parse_chain(inbuf, tmp_mem,
1842 &iov, &num_iov);
1843 if (!NT_STATUS_IS_OK(status)) {
1844 DEBUG(10,("smb1cli_inbuf_parse_chain - %s\n",
1845 nt_errstr(status)));
1846 return status;
1849 cmd = CVAL(inhdr, HDR_COM);
1850 status = smb1cli_pull_raw_error(inhdr);
1852 if (state->smb1.chained_requests == NULL) {
1853 if (num_iov != 3) {
1854 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1857 smbXcli_req_unset_pending(req);
1859 if (state->smb1.recv_iov == NULL) {
1861 * For requests with more than
1862 * one response, we have to readd the
1863 * recv_iov array.
1865 state->smb1.recv_iov = talloc_zero_array(state,
1866 struct iovec,
1868 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1869 return NT_STATUS_OK;
1873 state->smb1.recv_cmd = cmd;
1874 state->smb1.recv_status = status;
1875 state->inbuf = talloc_move(state->smb1.recv_iov, &inbuf);
1877 state->smb1.recv_iov[0] = iov[0];
1878 state->smb1.recv_iov[1] = iov[1];
1879 state->smb1.recv_iov[2] = iov[2];
1881 if (talloc_array_length(conn->pending) == 0) {
1882 tevent_req_done(req);
1883 return NT_STATUS_OK;
1886 tevent_req_defer_callback(req, state->ev);
1887 tevent_req_done(req);
1888 return NT_STATUS_RETRY;
1891 chain = talloc_move(tmp_mem, &state->smb1.chained_requests);
1892 num_chained = talloc_array_length(chain);
1893 num_responses = (num_iov - 1)/2;
1895 if (num_responses > num_chained) {
1896 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1899 for (i=0; i<num_chained; i++) {
1900 size_t iov_idx = 1 + (i*2);
1901 struct iovec *cur = &iov[iov_idx];
1902 uint8_t *inbuf_ref;
1904 req = chain[i];
1905 state = tevent_req_data(req, struct smbXcli_req_state);
1907 smbXcli_req_unset_pending(req);
1910 * as we finish multiple requests here
1911 * we need to defer the callbacks as
1912 * they could destroy our current stack state.
1914 tevent_req_defer_callback(req, state->ev);
1916 if (i >= num_responses) {
1917 tevent_req_nterror(req, NT_STATUS_REQUEST_ABORTED);
1918 continue;
1921 if (state->smb1.recv_iov == NULL) {
1923 * For requests with more than
1924 * one response, we have to readd the
1925 * recv_iov array.
1927 state->smb1.recv_iov = talloc_zero_array(state,
1928 struct iovec,
1930 if (tevent_req_nomem(state->smb1.recv_iov, req)) {
1931 continue;
1935 state->smb1.recv_cmd = cmd;
1937 if (i == (num_responses - 1)) {
1939 * The last request in the chain gets the status
1941 state->smb1.recv_status = status;
1942 } else {
1943 cmd = CVAL(cur[0].iov_base, 0);
1944 state->smb1.recv_status = NT_STATUS_OK;
1947 state->inbuf = inbuf;
1950 * Note: here we use talloc_reference() in a way
1951 * that does not expose it to the caller.
1953 inbuf_ref = talloc_reference(state->smb1.recv_iov, inbuf);
1954 if (tevent_req_nomem(inbuf_ref, req)) {
1955 continue;
1958 /* copy the related buffers */
1959 state->smb1.recv_iov[0] = iov[0];
1960 state->smb1.recv_iov[1] = cur[0];
1961 state->smb1.recv_iov[2] = cur[1];
1963 tevent_req_done(req);
1966 return NT_STATUS_RETRY;
1969 NTSTATUS smb1cli_req_recv(struct tevent_req *req,
1970 TALLOC_CTX *mem_ctx,
1971 struct iovec **piov,
1972 uint8_t **phdr,
1973 uint8_t *pwct,
1974 uint16_t **pvwv,
1975 uint32_t *pvwv_offset,
1976 uint32_t *pnum_bytes,
1977 uint8_t **pbytes,
1978 uint32_t *pbytes_offset,
1979 uint8_t **pinbuf,
1980 const struct smb1cli_req_expected_response *expected,
1981 size_t num_expected)
1983 struct smbXcli_req_state *state =
1984 tevent_req_data(req,
1985 struct smbXcli_req_state);
1986 NTSTATUS status = NT_STATUS_OK;
1987 struct iovec *recv_iov = NULL;
1988 uint8_t *hdr = NULL;
1989 uint8_t wct = 0;
1990 uint32_t vwv_offset = 0;
1991 uint16_t *vwv = NULL;
1992 uint32_t num_bytes = 0;
1993 uint32_t bytes_offset = 0;
1994 uint8_t *bytes = NULL;
1995 size_t i;
1996 bool found_status = false;
1997 bool found_size = false;
1999 if (piov != NULL) {
2000 *piov = NULL;
2002 if (phdr != NULL) {
2003 *phdr = 0;
2005 if (pwct != NULL) {
2006 *pwct = 0;
2008 if (pvwv != NULL) {
2009 *pvwv = NULL;
2011 if (pvwv_offset != NULL) {
2012 *pvwv_offset = 0;
2014 if (pnum_bytes != NULL) {
2015 *pnum_bytes = 0;
2017 if (pbytes != NULL) {
2018 *pbytes = NULL;
2020 if (pbytes_offset != NULL) {
2021 *pbytes_offset = 0;
2023 if (pinbuf != NULL) {
2024 *pinbuf = NULL;
2027 if (state->inbuf != NULL) {
2028 recv_iov = state->smb1.recv_iov;
2029 state->smb1.recv_iov = NULL;
2030 if (state->smb1.recv_cmd != SMBreadBraw) {
2031 hdr = (uint8_t *)recv_iov[0].iov_base;
2032 wct = recv_iov[1].iov_len/2;
2033 vwv = (uint16_t *)recv_iov[1].iov_base;
2034 vwv_offset = PTR_DIFF(vwv, hdr);
2035 num_bytes = recv_iov[2].iov_len;
2036 bytes = (uint8_t *)recv_iov[2].iov_base;
2037 bytes_offset = PTR_DIFF(bytes, hdr);
2041 if (tevent_req_is_nterror(req, &status)) {
2042 for (i=0; i < num_expected; i++) {
2043 if (NT_STATUS_EQUAL(status, expected[i].status)) {
2044 found_status = true;
2045 break;
2049 if (found_status) {
2050 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2053 return status;
2056 if (num_expected == 0) {
2057 found_status = true;
2058 found_size = true;
2061 status = state->smb1.recv_status;
2063 for (i=0; i < num_expected; i++) {
2064 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
2065 continue;
2068 found_status = true;
2069 if (expected[i].wct == 0) {
2070 found_size = true;
2071 break;
2074 if (expected[i].wct == wct) {
2075 found_size = true;
2076 break;
2080 if (!found_status) {
2081 return status;
2084 if (!found_size) {
2085 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2088 if (piov != NULL) {
2089 *piov = talloc_move(mem_ctx, &recv_iov);
2092 if (phdr != NULL) {
2093 *phdr = hdr;
2095 if (pwct != NULL) {
2096 *pwct = wct;
2098 if (pvwv != NULL) {
2099 *pvwv = vwv;
2101 if (pvwv_offset != NULL) {
2102 *pvwv_offset = vwv_offset;
2104 if (pnum_bytes != NULL) {
2105 *pnum_bytes = num_bytes;
2107 if (pbytes != NULL) {
2108 *pbytes = bytes;
2110 if (pbytes_offset != NULL) {
2111 *pbytes_offset = bytes_offset;
2113 if (pinbuf != NULL) {
2114 *pinbuf = state->inbuf;
2117 return status;
2120 size_t smb1cli_req_wct_ofs(struct tevent_req **reqs, int num_reqs)
2122 size_t wct_ofs;
2123 int i;
2125 wct_ofs = HDR_WCT;
2127 for (i=0; i<num_reqs; i++) {
2128 struct smbXcli_req_state *state;
2129 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2130 wct_ofs += smbXcli_iov_len(state->smb1.iov+2,
2131 state->smb1.iov_count-2);
2132 wct_ofs = (wct_ofs + 3) & ~3;
2134 return wct_ofs;
2137 NTSTATUS smb1cli_req_chain_submit(struct tevent_req **reqs, int num_reqs)
2139 struct smbXcli_req_state *first_state =
2140 tevent_req_data(reqs[0],
2141 struct smbXcli_req_state);
2142 struct smbXcli_req_state *state;
2143 size_t wct_offset;
2144 size_t chain_padding = 0;
2145 int i, iovlen;
2146 struct iovec *iov = NULL;
2147 struct iovec *this_iov;
2148 NTSTATUS status;
2149 size_t nbt_len;
2151 if (num_reqs == 1) {
2152 return smb1cli_req_writev_submit(reqs[0], first_state,
2153 first_state->smb1.iov,
2154 first_state->smb1.iov_count);
2157 iovlen = 0;
2158 for (i=0; i<num_reqs; i++) {
2159 if (!tevent_req_is_in_progress(reqs[i])) {
2160 return NT_STATUS_INTERNAL_ERROR;
2163 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2165 if (state->smb1.iov_count < 4) {
2166 return NT_STATUS_INVALID_PARAMETER_MIX;
2169 if (i == 0) {
2171 * The NBT and SMB header
2173 iovlen += 2;
2174 } else {
2176 * Chain padding
2178 iovlen += 1;
2182 * words and bytes
2184 iovlen += state->smb1.iov_count - 2;
2187 iov = talloc_zero_array(first_state, struct iovec, iovlen);
2188 if (iov == NULL) {
2189 return NT_STATUS_NO_MEMORY;
2192 first_state->smb1.chained_requests = (struct tevent_req **)talloc_memdup(
2193 first_state, reqs, sizeof(*reqs) * num_reqs);
2194 if (first_state->smb1.chained_requests == NULL) {
2195 TALLOC_FREE(iov);
2196 return NT_STATUS_NO_MEMORY;
2199 wct_offset = HDR_WCT;
2200 this_iov = iov;
2202 for (i=0; i<num_reqs; i++) {
2203 size_t next_padding = 0;
2204 uint16_t *vwv;
2206 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2208 if (i < num_reqs-1) {
2209 if (!smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))
2210 || CVAL(state->smb1.hdr, HDR_WCT) < 2) {
2211 TALLOC_FREE(iov);
2212 TALLOC_FREE(first_state->smb1.chained_requests);
2213 return NT_STATUS_INVALID_PARAMETER_MIX;
2217 wct_offset += smbXcli_iov_len(state->smb1.iov+2,
2218 state->smb1.iov_count-2) + 1;
2219 if ((wct_offset % 4) != 0) {
2220 next_padding = 4 - (wct_offset % 4);
2222 wct_offset += next_padding;
2223 vwv = state->smb1.vwv;
2225 if (i < num_reqs-1) {
2226 struct smbXcli_req_state *next_state =
2227 tevent_req_data(reqs[i+1],
2228 struct smbXcli_req_state);
2229 SCVAL(vwv+0, 0, CVAL(next_state->smb1.hdr, HDR_COM));
2230 SCVAL(vwv+0, 1, 0);
2231 SSVAL(vwv+1, 0, wct_offset);
2232 } else if (smb1cli_is_andx_req(CVAL(state->smb1.hdr, HDR_COM))) {
2233 /* properly end the chain */
2234 SCVAL(vwv+0, 0, 0xff);
2235 SCVAL(vwv+0, 1, 0xff);
2236 SSVAL(vwv+1, 0, 0);
2239 if (i == 0) {
2241 * The NBT and SMB header
2243 this_iov[0] = state->smb1.iov[0];
2244 this_iov[1] = state->smb1.iov[1];
2245 this_iov += 2;
2246 } else {
2248 * This one is a bit subtle. We have to add
2249 * chain_padding bytes between the requests, and we
2250 * have to also include the wct field of the
2251 * subsequent requests. We use the subsequent header
2252 * for the padding, it contains the wct field in its
2253 * last byte.
2255 this_iov[0].iov_len = chain_padding+1;
2256 this_iov[0].iov_base = (void *)&state->smb1.hdr[
2257 sizeof(state->smb1.hdr) - this_iov[0].iov_len];
2258 memset(this_iov[0].iov_base, 0, this_iov[0].iov_len-1);
2259 this_iov += 1;
2263 * copy the words and bytes
2265 memcpy(this_iov, state->smb1.iov+2,
2266 sizeof(struct iovec) * (state->smb1.iov_count-2));
2267 this_iov += state->smb1.iov_count - 2;
2268 chain_padding = next_padding;
2271 nbt_len = smbXcli_iov_len(&iov[1], iovlen-1);
2272 if (nbt_len > first_state->conn->smb1.max_xmit) {
2273 TALLOC_FREE(iov);
2274 TALLOC_FREE(first_state->smb1.chained_requests);
2275 return NT_STATUS_INVALID_PARAMETER_MIX;
2278 status = smb1cli_req_writev_submit(reqs[0], first_state, iov, iovlen);
2279 if (!NT_STATUS_IS_OK(status)) {
2280 TALLOC_FREE(iov);
2281 TALLOC_FREE(first_state->smb1.chained_requests);
2282 return status;
2285 return NT_STATUS_OK;
2288 bool smbXcli_conn_has_async_calls(struct smbXcli_conn *conn)
2290 return ((tevent_queue_length(conn->outgoing) != 0)
2291 || (talloc_array_length(conn->pending) != 0));
2294 uint32_t smb2cli_conn_server_capabilities(struct smbXcli_conn *conn)
2296 return conn->smb2.server.capabilities;
2299 uint16_t smb2cli_conn_server_security_mode(struct smbXcli_conn *conn)
2301 return conn->smb2.server.security_mode;
2304 uint32_t smb2cli_conn_max_trans_size(struct smbXcli_conn *conn)
2306 return conn->smb2.server.max_trans_size;
2309 uint32_t smb2cli_conn_max_read_size(struct smbXcli_conn *conn)
2311 return conn->smb2.server.max_read_size;
2314 uint32_t smb2cli_conn_max_write_size(struct smbXcli_conn *conn)
2316 return conn->smb2.server.max_write_size;
2319 void smb2cli_conn_set_max_credits(struct smbXcli_conn *conn,
2320 uint16_t max_credits)
2322 conn->smb2.max_credits = max_credits;
2325 static void smb2cli_req_cancel_done(struct tevent_req *subreq);
2327 static bool smb2cli_req_cancel(struct tevent_req *req)
2329 struct smbXcli_req_state *state =
2330 tevent_req_data(req,
2331 struct smbXcli_req_state);
2332 uint32_t flags = IVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
2333 uint32_t pid = IVAL(state->smb2.hdr, SMB2_HDR_PID);
2334 uint32_t tid = IVAL(state->smb2.hdr, SMB2_HDR_TID);
2335 uint64_t mid = BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID);
2336 uint64_t aid = BVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID);
2337 struct smbXcli_session *session = state->session;
2338 uint8_t *fixed = state->smb2.pad;
2339 uint16_t fixed_len = 4;
2340 struct tevent_req *subreq;
2341 struct smbXcli_req_state *substate;
2342 NTSTATUS status;
2344 SSVAL(fixed, 0, 0x04);
2345 SSVAL(fixed, 2, 0);
2347 subreq = smb2cli_req_create(state, state->ev,
2348 state->conn,
2349 SMB2_OP_CANCEL,
2350 flags, 0,
2351 0, /* timeout */
2352 pid, tid, session,
2353 fixed, fixed_len,
2354 NULL, 0);
2355 if (subreq == NULL) {
2356 return false;
2358 substate = tevent_req_data(subreq, struct smbXcli_req_state);
2360 if (flags & SMB2_HDR_FLAG_ASYNC) {
2361 mid = 0;
2364 SIVAL(substate->smb2.hdr, SMB2_HDR_FLAGS, flags);
2365 SIVAL(substate->smb2.hdr, SMB2_HDR_PID, pid);
2366 SIVAL(substate->smb2.hdr, SMB2_HDR_TID, tid);
2367 SBVAL(substate->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2368 SBVAL(substate->smb2.hdr, SMB2_HDR_ASYNC_ID, aid);
2370 status = smb2cli_req_compound_submit(&subreq, 1);
2371 if (!NT_STATUS_IS_OK(status)) {
2372 TALLOC_FREE(subreq);
2373 return false;
2376 tevent_req_set_callback(subreq, smb2cli_req_cancel_done, NULL);
2378 return true;
2381 static void smb2cli_req_cancel_done(struct tevent_req *subreq)
2383 /* we do not care about the result */
2384 TALLOC_FREE(subreq);
2387 struct tevent_req *smb2cli_req_create(TALLOC_CTX *mem_ctx,
2388 struct tevent_context *ev,
2389 struct smbXcli_conn *conn,
2390 uint16_t cmd,
2391 uint32_t additional_flags,
2392 uint32_t clear_flags,
2393 uint32_t timeout_msec,
2394 uint32_t pid,
2395 uint32_t tid,
2396 struct smbXcli_session *session,
2397 const uint8_t *fixed,
2398 uint16_t fixed_len,
2399 const uint8_t *dyn,
2400 uint32_t dyn_len)
2402 struct tevent_req *req;
2403 struct smbXcli_req_state *state;
2404 uint32_t flags = 0;
2405 uint64_t uid = 0;
2407 req = tevent_req_create(mem_ctx, &state,
2408 struct smbXcli_req_state);
2409 if (req == NULL) {
2410 return NULL;
2413 state->ev = ev;
2414 state->conn = conn;
2415 state->session = session;
2417 if (session) {
2418 uid = session->smb2.session_id;
2420 state->smb2.should_sign = session->smb2.should_sign;
2421 state->smb2.should_encrypt = session->smb2.should_encrypt;
2423 /* TODO: turn on encryption based on the tree connect. */
2425 if (cmd == SMB2_OP_SESSSETUP &&
2426 session->smb2.signing_key.length != 0) {
2427 state->smb2.should_sign = true;
2430 if (cmd == SMB2_OP_SESSSETUP) {
2431 state->smb2.should_encrypt = false;
2434 if (state->smb2.should_encrypt) {
2435 state->smb2.should_sign = false;
2439 state->smb2.recv_iov = talloc_zero_array(state, struct iovec, 3);
2440 if (state->smb2.recv_iov == NULL) {
2441 TALLOC_FREE(req);
2442 return NULL;
2445 flags |= additional_flags;
2446 flags &= ~clear_flags;
2448 state->smb2.fixed = fixed;
2449 state->smb2.fixed_len = fixed_len;
2450 state->smb2.dyn = dyn;
2451 state->smb2.dyn_len = dyn_len;
2453 if (state->smb2.should_encrypt) {
2454 SIVAL(state->smb2.transform, SMB2_TF_PROTOCOL_ID, SMB2_TF_MAGIC);
2455 SBVAL(state->smb2.transform, SMB2_TF_SESSION_ID, uid);
2458 SIVAL(state->smb2.hdr, SMB2_HDR_PROTOCOL_ID, SMB2_MAGIC);
2459 SSVAL(state->smb2.hdr, SMB2_HDR_LENGTH, SMB2_HDR_BODY);
2460 SSVAL(state->smb2.hdr, SMB2_HDR_OPCODE, cmd);
2461 SIVAL(state->smb2.hdr, SMB2_HDR_FLAGS, flags);
2462 SIVAL(state->smb2.hdr, SMB2_HDR_PID, pid);
2463 SIVAL(state->smb2.hdr, SMB2_HDR_TID, tid);
2464 SBVAL(state->smb2.hdr, SMB2_HDR_SESSION_ID, uid);
2466 switch (cmd) {
2467 case SMB2_OP_CANCEL:
2468 state->one_way = true;
2469 break;
2470 case SMB2_OP_BREAK:
2472 * If this is a dummy request, it will have
2473 * UINT64_MAX as message id.
2474 * If we send on break acknowledgement,
2475 * this gets overwritten later.
2477 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, UINT64_MAX);
2478 break;
2481 if (timeout_msec > 0) {
2482 struct timeval endtime;
2484 endtime = timeval_current_ofs_msec(timeout_msec);
2485 if (!tevent_req_set_endtime(req, ev, endtime)) {
2486 return req;
2490 return req;
2493 void smb2cli_req_set_notify_async(struct tevent_req *req)
2495 struct smbXcli_req_state *state =
2496 tevent_req_data(req,
2497 struct smbXcli_req_state);
2499 state->smb2.notify_async = true;
2502 static void smb2cli_req_writev_done(struct tevent_req *subreq);
2503 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2504 TALLOC_CTX *tmp_mem,
2505 uint8_t *inbuf);
2507 NTSTATUS smb2cli_req_compound_submit(struct tevent_req **reqs,
2508 int num_reqs)
2510 struct smbXcli_req_state *state;
2511 struct tevent_req *subreq;
2512 struct iovec *iov;
2513 int i, num_iov, nbt_len;
2516 * 1 for the nbt length
2517 * per request: TRANSFORM, HDR, fixed, dyn, padding
2518 * -1 because the last one does not need padding
2521 iov = talloc_array(reqs[0], struct iovec, 1 + 5*num_reqs - 1);
2522 if (iov == NULL) {
2523 return NT_STATUS_NO_MEMORY;
2526 num_iov = 1;
2527 nbt_len = 0;
2529 for (i=0; i<num_reqs; i++) {
2530 int tf_iov;
2531 int hdr_iov;
2532 size_t reqlen;
2533 bool ret;
2534 uint16_t opcode;
2535 uint64_t avail;
2536 uint16_t charge;
2537 uint16_t credits;
2538 uint64_t mid;
2539 const DATA_BLOB *signing_key = NULL;
2540 const DATA_BLOB *encryption_key = NULL;
2542 if (!tevent_req_is_in_progress(reqs[i])) {
2543 return NT_STATUS_INTERNAL_ERROR;
2546 state = tevent_req_data(reqs[i], struct smbXcli_req_state);
2548 if (!smbXcli_conn_is_connected(state->conn)) {
2549 return NT_STATUS_CONNECTION_DISCONNECTED;
2552 if ((state->conn->protocol != PROTOCOL_NONE) &&
2553 (state->conn->protocol < PROTOCOL_SMB2_02)) {
2554 return NT_STATUS_REVISION_MISMATCH;
2557 opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
2558 if (opcode == SMB2_OP_CANCEL) {
2559 goto skip_credits;
2562 avail = UINT64_MAX - state->conn->smb2.mid;
2563 if (avail < 1) {
2564 return NT_STATUS_CONNECTION_ABORTED;
2567 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2568 charge = (MAX(state->smb2.dyn_len, 1) - 1)/ 65536 + 1;
2569 } else {
2570 charge = 1;
2573 charge = MAX(state->smb2.credit_charge, charge);
2575 avail = MIN(avail, state->conn->smb2.cur_credits);
2576 if (avail < charge) {
2577 return NT_STATUS_INTERNAL_ERROR;
2580 credits = 0;
2581 if (state->conn->smb2.max_credits > state->conn->smb2.cur_credits) {
2582 credits = state->conn->smb2.max_credits -
2583 state->conn->smb2.cur_credits;
2585 if (state->conn->smb2.max_credits >= state->conn->smb2.cur_credits) {
2586 credits += 1;
2589 mid = state->conn->smb2.mid;
2590 state->conn->smb2.mid += charge;
2591 state->conn->smb2.cur_credits -= charge;
2593 if (state->conn->smb2.server.capabilities & SMB2_CAP_LARGE_MTU) {
2594 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT_CHARGE, charge);
2596 SSVAL(state->smb2.hdr, SMB2_HDR_CREDIT, credits);
2597 SBVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID, mid);
2599 skip_credits:
2600 if (state->session) {
2602 * We prefer the channel signing key if it is
2603 * already there.
2605 if (state->smb2.should_sign) {
2606 signing_key = &state->session->smb2.channel_signing_key;
2610 * If it is a channel binding, we already have the main
2611 * signing key and try that one.
2613 if (signing_key && signing_key->length == 0) {
2614 signing_key = &state->session->smb2.signing_key;
2618 * If we do not have any session key yet, we skip the
2619 * signing of SMB2_OP_SESSSETUP requests.
2621 if (signing_key && signing_key->length == 0) {
2622 signing_key = NULL;
2625 if (state->smb2.should_encrypt) {
2626 encryption_key = &state->session->smb2.encryption_key;
2630 if (encryption_key) {
2631 tf_iov = num_iov;
2632 iov[num_iov].iov_base = state->smb2.transform;
2633 iov[num_iov].iov_len = sizeof(state->smb2.transform);
2634 num_iov += 1;
2637 hdr_iov = num_iov;
2638 iov[num_iov].iov_base = state->smb2.hdr;
2639 iov[num_iov].iov_len = sizeof(state->smb2.hdr);
2640 num_iov += 1;
2642 iov[num_iov].iov_base = discard_const(state->smb2.fixed);
2643 iov[num_iov].iov_len = state->smb2.fixed_len;
2644 num_iov += 1;
2646 if (state->smb2.dyn != NULL) {
2647 iov[num_iov].iov_base = discard_const(state->smb2.dyn);
2648 iov[num_iov].iov_len = state->smb2.dyn_len;
2649 num_iov += 1;
2652 reqlen = sizeof(state->smb2.hdr);
2653 reqlen += state->smb2.fixed_len;
2654 reqlen += state->smb2.dyn_len;
2656 if (i < num_reqs-1) {
2657 if ((reqlen % 8) > 0) {
2658 uint8_t pad = 8 - (reqlen % 8);
2659 iov[num_iov].iov_base = state->smb2.pad;
2660 iov[num_iov].iov_len = pad;
2661 num_iov += 1;
2662 reqlen += pad;
2664 SIVAL(state->smb2.hdr, SMB2_HDR_NEXT_COMMAND, reqlen);
2667 if (encryption_key) {
2668 NTSTATUS status;
2669 uint8_t *buf;
2670 int vi;
2672 SBVAL(state->smb2.transform, SMB2_TF_NONCE, mid);
2673 SBVAL(state->smb2.transform, SMB2_TF_NONCE+8,
2674 state->session->smb2.channel_nonce);
2675 SBVAL(state->smb2.transform, SMB2_TF_MSG_SIZE,
2676 reqlen);
2678 buf = talloc_array(iov, uint8_t, reqlen);
2679 if (buf == NULL) {
2680 return NT_STATUS_NO_MEMORY;
2683 reqlen += SMB2_TF_HDR_SIZE;
2686 * We copy the buffers before encrypting them,
2687 * this is at least currently needed for the
2688 * to keep state->smb2.hdr.
2690 * Also the callers may expect there buffers
2691 * to be const.
2693 for (vi = hdr_iov; vi < num_iov; vi++) {
2694 struct iovec *v = &iov[vi];
2695 const uint8_t *o = (const uint8_t *)v->iov_base;
2697 memcpy(buf, o, v->iov_len);
2698 v->iov_base = (void *)buf;
2699 buf += v->iov_len;
2702 status = smb2_signing_encrypt_pdu(*encryption_key,
2703 state->session->conn->protocol,
2704 &iov[tf_iov], num_iov - tf_iov);
2705 if (!NT_STATUS_IS_OK(status)) {
2706 return status;
2708 } else if (signing_key) {
2709 NTSTATUS status;
2711 status = smb2_signing_sign_pdu(*signing_key,
2712 state->session->conn->protocol,
2713 &iov[hdr_iov], num_iov - hdr_iov);
2714 if (!NT_STATUS_IS_OK(status)) {
2715 return status;
2719 nbt_len += reqlen;
2721 ret = smbXcli_req_set_pending(reqs[i]);
2722 if (!ret) {
2723 return NT_STATUS_NO_MEMORY;
2727 state = tevent_req_data(reqs[0], struct smbXcli_req_state);
2728 _smb_setlen_tcp(state->length_hdr, nbt_len);
2729 iov[0].iov_base = state->length_hdr;
2730 iov[0].iov_len = sizeof(state->length_hdr);
2732 if (state->conn->dispatch_incoming == NULL) {
2733 state->conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
2736 subreq = writev_send(state, state->ev, state->conn->outgoing,
2737 state->conn->write_fd, false, iov, num_iov);
2738 if (subreq == NULL) {
2739 return NT_STATUS_NO_MEMORY;
2741 tevent_req_set_callback(subreq, smb2cli_req_writev_done, reqs[0]);
2742 return NT_STATUS_OK;
2745 void smb2cli_req_set_credit_charge(struct tevent_req *req, uint16_t charge)
2747 struct smbXcli_req_state *state =
2748 tevent_req_data(req,
2749 struct smbXcli_req_state);
2751 state->smb2.credit_charge = charge;
2754 struct tevent_req *smb2cli_req_send(TALLOC_CTX *mem_ctx,
2755 struct tevent_context *ev,
2756 struct smbXcli_conn *conn,
2757 uint16_t cmd,
2758 uint32_t additional_flags,
2759 uint32_t clear_flags,
2760 uint32_t timeout_msec,
2761 uint32_t pid,
2762 uint32_t tid,
2763 struct smbXcli_session *session,
2764 const uint8_t *fixed,
2765 uint16_t fixed_len,
2766 const uint8_t *dyn,
2767 uint32_t dyn_len)
2769 struct tevent_req *req;
2770 NTSTATUS status;
2772 req = smb2cli_req_create(mem_ctx, ev, conn, cmd,
2773 additional_flags, clear_flags,
2774 timeout_msec,
2775 pid, tid, session,
2776 fixed, fixed_len, dyn, dyn_len);
2777 if (req == NULL) {
2778 return NULL;
2780 if (!tevent_req_is_in_progress(req)) {
2781 return tevent_req_post(req, ev);
2783 status = smb2cli_req_compound_submit(&req, 1);
2784 if (tevent_req_nterror(req, status)) {
2785 return tevent_req_post(req, ev);
2787 return req;
2790 static void smb2cli_req_writev_done(struct tevent_req *subreq)
2792 struct tevent_req *req =
2793 tevent_req_callback_data(subreq,
2794 struct tevent_req);
2795 struct smbXcli_req_state *state =
2796 tevent_req_data(req,
2797 struct smbXcli_req_state);
2798 ssize_t nwritten;
2799 int err;
2801 nwritten = writev_recv(subreq, &err);
2802 TALLOC_FREE(subreq);
2803 if (nwritten == -1) {
2804 /* here, we need to notify all pending requests */
2805 NTSTATUS status = map_nt_error_from_unix_common(err);
2806 smbXcli_conn_disconnect(state->conn, status);
2807 return;
2811 static NTSTATUS smb2cli_inbuf_parse_compound(struct smbXcli_conn *conn,
2812 uint8_t *buf,
2813 size_t buflen,
2814 TALLOC_CTX *mem_ctx,
2815 struct iovec **piov, int *pnum_iov)
2817 struct iovec *iov;
2818 int num_iov = 0;
2819 size_t taken = 0;
2820 uint8_t *first_hdr = buf;
2822 iov = talloc_array(mem_ctx, struct iovec, num_iov);
2823 if (iov == NULL) {
2824 return NT_STATUS_NO_MEMORY;
2827 while (taken < buflen) {
2828 uint8_t *tf = NULL;
2829 size_t tf_len = 0;
2830 size_t len = buflen - taken;
2831 uint8_t *hdr = first_hdr + taken;
2832 struct iovec *cur;
2833 size_t full_size;
2834 size_t next_command_ofs;
2835 uint16_t body_size;
2836 struct iovec *iov_tmp;
2838 if (len < 4) {
2839 DEBUG(10, ("%d bytes left, expected at least %d\n",
2840 (int)len, 4));
2841 goto inval;
2843 if (IVAL(hdr, 0) == SMB2_TF_MAGIC) {
2844 struct smbXcli_session *s;
2845 uint64_t uid;
2846 struct iovec tf_iov[2];
2847 NTSTATUS status;
2849 if (len < SMB2_TF_HDR_SIZE) {
2850 DEBUG(10, ("%d bytes left, expected at least %d\n",
2851 (int)len, SMB2_TF_HDR_SIZE));
2852 goto inval;
2854 tf = hdr;
2855 tf_len = SMB2_TF_HDR_SIZE;
2856 taken += tf_len;
2858 hdr = first_hdr + taken;
2859 len = IVAL(tf, SMB2_TF_MSG_SIZE);
2860 uid = BVAL(tf, SMB2_TF_SESSION_ID);
2862 s = conn->sessions;
2863 for (; s; s = s->next) {
2864 if (s->smb2.session_id != uid) {
2865 continue;
2867 break;
2870 if (s == NULL) {
2871 DEBUG(10, ("unknown session_id %llu\n",
2872 (unsigned long long)uid));
2873 goto inval;
2876 tf_iov[0].iov_base = (void *)tf;
2877 tf_iov[0].iov_len = tf_len;
2878 tf_iov[1].iov_base = (void *)hdr;
2879 tf_iov[1].iov_len = len;
2881 status = smb2_signing_decrypt_pdu(s->smb2.decryption_key,
2882 conn->protocol,
2883 tf_iov, 2);
2884 if (!NT_STATUS_IS_OK(status)) {
2885 TALLOC_FREE(iov);
2886 return status;
2891 * We need the header plus the body length field
2894 if (len < SMB2_HDR_BODY + 2) {
2895 DEBUG(10, ("%d bytes left, expected at least %d\n",
2896 (int)len, SMB2_HDR_BODY));
2897 goto inval;
2899 if (IVAL(hdr, 0) != SMB2_MAGIC) {
2900 DEBUG(10, ("Got non-SMB2 PDU: %x\n",
2901 IVAL(hdr, 0)));
2902 goto inval;
2904 if (SVAL(hdr, 4) != SMB2_HDR_BODY) {
2905 DEBUG(10, ("Got HDR len %d, expected %d\n",
2906 SVAL(hdr, 4), SMB2_HDR_BODY));
2907 goto inval;
2910 full_size = len;
2911 next_command_ofs = IVAL(hdr, SMB2_HDR_NEXT_COMMAND);
2912 body_size = SVAL(hdr, SMB2_HDR_BODY);
2914 if (next_command_ofs != 0) {
2915 if (next_command_ofs < (SMB2_HDR_BODY + 2)) {
2916 goto inval;
2918 if (next_command_ofs > full_size) {
2919 goto inval;
2921 if (tf && next_command_ofs < len) {
2922 goto inval;
2924 full_size = next_command_ofs;
2926 if (body_size < 2) {
2927 goto inval;
2929 body_size &= 0xfffe;
2931 if (body_size > (full_size - SMB2_HDR_BODY)) {
2932 goto inval;
2935 iov_tmp = talloc_realloc(mem_ctx, iov, struct iovec,
2936 num_iov + 4);
2937 if (iov_tmp == NULL) {
2938 TALLOC_FREE(iov);
2939 return NT_STATUS_NO_MEMORY;
2941 iov = iov_tmp;
2942 cur = &iov[num_iov];
2943 num_iov += 4;
2945 cur[0].iov_base = tf;
2946 cur[0].iov_len = tf_len;
2947 cur[1].iov_base = hdr;
2948 cur[1].iov_len = SMB2_HDR_BODY;
2949 cur[2].iov_base = hdr + SMB2_HDR_BODY;
2950 cur[2].iov_len = body_size;
2951 cur[3].iov_base = hdr + SMB2_HDR_BODY + body_size;
2952 cur[3].iov_len = full_size - (SMB2_HDR_BODY + body_size);
2954 taken += full_size;
2957 *piov = iov;
2958 *pnum_iov = num_iov;
2959 return NT_STATUS_OK;
2961 inval:
2962 TALLOC_FREE(iov);
2963 return NT_STATUS_INVALID_NETWORK_RESPONSE;
2966 static struct tevent_req *smb2cli_conn_find_pending(struct smbXcli_conn *conn,
2967 uint64_t mid)
2969 size_t num_pending = talloc_array_length(conn->pending);
2970 size_t i;
2972 for (i=0; i<num_pending; i++) {
2973 struct tevent_req *req = conn->pending[i];
2974 struct smbXcli_req_state *state =
2975 tevent_req_data(req,
2976 struct smbXcli_req_state);
2978 if (mid == BVAL(state->smb2.hdr, SMB2_HDR_MESSAGE_ID)) {
2979 return req;
2982 return NULL;
2985 static NTSTATUS smb2cli_conn_dispatch_incoming(struct smbXcli_conn *conn,
2986 TALLOC_CTX *tmp_mem,
2987 uint8_t *inbuf)
2989 struct tevent_req *req;
2990 struct smbXcli_req_state *state = NULL;
2991 struct iovec *iov;
2992 int i, num_iov;
2993 NTSTATUS status;
2994 bool defer = true;
2995 struct smbXcli_session *last_session = NULL;
2996 size_t inbuf_len = smb_len_tcp(inbuf);
2998 status = smb2cli_inbuf_parse_compound(conn,
2999 inbuf + NBT_HDR_SIZE,
3000 inbuf_len,
3001 tmp_mem,
3002 &iov, &num_iov);
3003 if (!NT_STATUS_IS_OK(status)) {
3004 return status;
3007 for (i=0; i<num_iov; i+=4) {
3008 uint8_t *inbuf_ref = NULL;
3009 struct iovec *cur = &iov[i];
3010 uint8_t *inhdr = (uint8_t *)cur[1].iov_base;
3011 uint16_t opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
3012 uint32_t flags = IVAL(inhdr, SMB2_HDR_FLAGS);
3013 uint64_t mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);
3014 uint16_t req_opcode;
3015 uint32_t req_flags;
3016 uint16_t credits = SVAL(inhdr, SMB2_HDR_CREDIT);
3017 uint32_t new_credits;
3018 struct smbXcli_session *session = NULL;
3019 const DATA_BLOB *signing_key = NULL;
3021 new_credits = conn->smb2.cur_credits;
3022 new_credits += credits;
3023 if (new_credits > UINT16_MAX) {
3024 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3026 conn->smb2.cur_credits += credits;
3028 req = smb2cli_conn_find_pending(conn, mid);
3029 if (req == NULL) {
3030 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3032 state = tevent_req_data(req, struct smbXcli_req_state);
3034 state->smb2.got_async = false;
3036 req_opcode = SVAL(state->smb2.hdr, SMB2_HDR_OPCODE);
3037 if (opcode != req_opcode) {
3038 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3040 req_flags = SVAL(state->smb2.hdr, SMB2_HDR_FLAGS);
3042 if (!(flags & SMB2_HDR_FLAG_REDIRECT)) {
3043 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3046 status = NT_STATUS(IVAL(inhdr, SMB2_HDR_STATUS));
3047 if ((flags & SMB2_HDR_FLAG_ASYNC) &&
3048 NT_STATUS_EQUAL(status, STATUS_PENDING)) {
3049 uint64_t async_id = BVAL(inhdr, SMB2_HDR_ASYNC_ID);
3052 * async interim responses are not signed,
3053 * even if the SMB2_HDR_FLAG_SIGNED flag
3054 * is set.
3056 req_flags |= SMB2_HDR_FLAG_ASYNC;
3057 SBVAL(state->smb2.hdr, SMB2_HDR_FLAGS, req_flags);
3058 SBVAL(state->smb2.hdr, SMB2_HDR_ASYNC_ID, async_id);
3060 if (state->smb2.notify_async) {
3061 state->smb2.got_async = true;
3062 tevent_req_defer_callback(req, state->ev);
3063 tevent_req_notify_callback(req);
3065 continue;
3068 session = state->session;
3069 if (req_flags & SMB2_HDR_FLAG_CHAINED) {
3070 session = last_session;
3072 last_session = session;
3074 if (state->smb2.should_sign) {
3075 if (!(flags & SMB2_HDR_FLAG_SIGNED)) {
3076 return NT_STATUS_ACCESS_DENIED;
3080 if (flags & SMB2_HDR_FLAG_SIGNED) {
3081 uint64_t uid = BVAL(inhdr, SMB2_HDR_SESSION_ID);
3083 if (session == NULL) {
3084 struct smbXcli_session *s;
3086 s = state->conn->sessions;
3087 for (; s; s = s->next) {
3088 if (s->smb2.session_id != uid) {
3089 continue;
3092 session = s;
3093 break;
3097 if (session == NULL) {
3098 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3101 last_session = session;
3102 signing_key = &session->smb2.channel_signing_key;
3105 if (opcode == SMB2_OP_SESSSETUP) {
3107 * We prefer the channel signing key, if it is
3108 * already there.
3110 * If we do not have a channel signing key yet,
3111 * we try the main signing key, if it is not
3112 * the final response.
3114 if (signing_key && signing_key->length == 0 &&
3115 !NT_STATUS_IS_OK(status)) {
3116 signing_key = &session->smb2.signing_key;
3119 if (signing_key && signing_key->length == 0) {
3121 * If we do not have a session key to
3122 * verify the signature, we defer the
3123 * signing check to the caller.
3125 * The caller gets NT_STATUS_OK, it
3126 * has to call
3127 * smb2cli_session_set_session_key()
3128 * or
3129 * smb2cli_session_set_channel_key()
3130 * which will check the signature
3131 * with the channel signing key.
3133 signing_key = NULL;
3137 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_SESSION_DELETED)) {
3139 * if the server returns NT_STATUS_USER_SESSION_DELETED
3140 * the response is not signed and we should
3141 * propagate the NT_STATUS_USER_SESSION_DELETED
3142 * status to the caller.
3144 signing_key = NULL;
3145 } else if (state->smb2.should_encrypt) {
3146 if (cur[0].iov_len != SMB2_TF_HDR_SIZE) {
3147 return NT_STATUS_ACCESS_DENIED;
3151 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_NAME_DELETED) ||
3152 NT_STATUS_EQUAL(status, NT_STATUS_FILE_CLOSED) ||
3153 NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
3155 * if the server returns
3156 * NT_STATUS_NETWORK_NAME_DELETED
3157 * NT_STATUS_FILE_CLOSED
3158 * NT_STATUS_INVALID_PARAMETER
3159 * the response might not be signed
3160 * as this happens before the signing checks.
3162 * If server echos the signature (or all zeros)
3163 * we should report the status from the server
3164 * to the caller.
3166 if (signing_key) {
3167 int cmp;
3169 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3170 state->smb2.hdr+SMB2_HDR_SIGNATURE,
3171 16);
3172 if (cmp == 0) {
3173 state->smb2.signing_skipped = true;
3174 signing_key = NULL;
3177 if (signing_key) {
3178 int cmp;
3179 static const uint8_t zeros[16];
3181 cmp = memcmp(inhdr+SMB2_HDR_SIGNATURE,
3182 zeros,
3183 16);
3184 if (cmp == 0) {
3185 state->smb2.signing_skipped = true;
3186 signing_key = NULL;
3191 if (signing_key) {
3192 status = smb2_signing_check_pdu(*signing_key,
3193 state->conn->protocol,
3194 &cur[1], 3);
3195 if (!NT_STATUS_IS_OK(status)) {
3197 * If the signing check fails, we disconnect
3198 * the connection.
3200 return status;
3204 smbXcli_req_unset_pending(req);
3207 * There might be more than one response
3208 * we need to defer the notifications
3210 if ((num_iov == 5) && (talloc_array_length(conn->pending) == 0)) {
3211 defer = false;
3214 if (defer) {
3215 tevent_req_defer_callback(req, state->ev);
3219 * Note: here we use talloc_reference() in a way
3220 * that does not expose it to the caller.
3222 inbuf_ref = talloc_reference(state->smb2.recv_iov, inbuf);
3223 if (tevent_req_nomem(inbuf_ref, req)) {
3224 continue;
3227 /* copy the related buffers */
3228 state->smb2.recv_iov[0] = cur[1];
3229 state->smb2.recv_iov[1] = cur[2];
3230 state->smb2.recv_iov[2] = cur[3];
3232 tevent_req_done(req);
3235 if (defer) {
3236 return NT_STATUS_RETRY;
3239 return NT_STATUS_OK;
3242 NTSTATUS smb2cli_req_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
3243 struct iovec **piov,
3244 const struct smb2cli_req_expected_response *expected,
3245 size_t num_expected)
3247 struct smbXcli_req_state *state =
3248 tevent_req_data(req,
3249 struct smbXcli_req_state);
3250 NTSTATUS status;
3251 size_t body_size;
3252 bool found_status = false;
3253 bool found_size = false;
3254 size_t i;
3256 if (piov != NULL) {
3257 *piov = NULL;
3260 if (state->smb2.got_async) {
3261 return STATUS_PENDING;
3264 if (tevent_req_is_nterror(req, &status)) {
3265 for (i=0; i < num_expected; i++) {
3266 if (NT_STATUS_EQUAL(status, expected[i].status)) {
3267 found_status = true;
3268 break;
3272 if (found_status) {
3273 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3276 return status;
3279 if (num_expected == 0) {
3280 found_status = true;
3281 found_size = true;
3284 status = NT_STATUS(IVAL(state->smb2.recv_iov[0].iov_base, SMB2_HDR_STATUS));
3285 body_size = SVAL(state->smb2.recv_iov[1].iov_base, 0);
3287 for (i=0; i < num_expected; i++) {
3288 if (!NT_STATUS_EQUAL(status, expected[i].status)) {
3289 continue;
3292 found_status = true;
3293 if (expected[i].body_size == 0) {
3294 found_size = true;
3295 break;
3298 if (expected[i].body_size == body_size) {
3299 found_size = true;
3300 break;
3304 if (!found_status) {
3305 return status;
3308 if (state->smb2.signing_skipped) {
3309 if (num_expected > 0) {
3310 return NT_STATUS_ACCESS_DENIED;
3312 if (!NT_STATUS_IS_ERR(status)) {
3313 return NT_STATUS_ACCESS_DENIED;
3317 if (!found_size) {
3318 return NT_STATUS_INVALID_NETWORK_RESPONSE;
3321 if (piov != NULL) {
3322 *piov = talloc_move(mem_ctx, &state->smb2.recv_iov);
3325 return status;
3328 static const struct {
3329 enum protocol_types proto;
3330 const char *smb1_name;
3331 } smb1cli_prots[] = {
3332 {PROTOCOL_CORE, "PC NETWORK PROGRAM 1.0"},
3333 {PROTOCOL_COREPLUS, "MICROSOFT NETWORKS 1.03"},
3334 {PROTOCOL_LANMAN1, "MICROSOFT NETWORKS 3.0"},
3335 {PROTOCOL_LANMAN1, "LANMAN1.0"},
3336 {PROTOCOL_LANMAN2, "LM1.2X002"},
3337 {PROTOCOL_LANMAN2, "DOS LANMAN2.1"},
3338 {PROTOCOL_LANMAN2, "LANMAN2.1"},
3339 {PROTOCOL_LANMAN2, "Samba"},
3340 {PROTOCOL_NT1, "NT LANMAN 1.0"},
3341 {PROTOCOL_NT1, "NT LM 0.12"},
3342 {PROTOCOL_SMB2_02, "SMB 2.002"},
3343 {PROTOCOL_SMB2_10, "SMB 2.???"},
3346 static const struct {
3347 enum protocol_types proto;
3348 uint16_t smb2_dialect;
3349 } smb2cli_prots[] = {
3350 {PROTOCOL_SMB2_02, SMB2_DIALECT_REVISION_202},
3351 {PROTOCOL_SMB2_10, SMB2_DIALECT_REVISION_210},
3352 {PROTOCOL_SMB2_22, SMB2_DIALECT_REVISION_222},
3353 {PROTOCOL_SMB2_24, SMB2_DIALECT_REVISION_224},
3354 {PROTOCOL_SMB3_00, SMB3_DIALECT_REVISION_300},
3357 struct smbXcli_negprot_state {
3358 struct smbXcli_conn *conn;
3359 struct tevent_context *ev;
3360 uint32_t timeout_msec;
3361 enum protocol_types min_protocol;
3362 enum protocol_types max_protocol;
3364 struct {
3365 uint8_t fixed[36];
3366 uint8_t dyn[ARRAY_SIZE(smb2cli_prots)*2];
3367 } smb2;
3370 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq);
3371 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state);
3372 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq);
3373 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state);
3374 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq);
3375 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
3376 TALLOC_CTX *frame,
3377 uint8_t *inbuf);
3379 struct tevent_req *smbXcli_negprot_send(TALLOC_CTX *mem_ctx,
3380 struct tevent_context *ev,
3381 struct smbXcli_conn *conn,
3382 uint32_t timeout_msec,
3383 enum protocol_types min_protocol,
3384 enum protocol_types max_protocol)
3386 struct tevent_req *req, *subreq;
3387 struct smbXcli_negprot_state *state;
3389 req = tevent_req_create(mem_ctx, &state,
3390 struct smbXcli_negprot_state);
3391 if (req == NULL) {
3392 return NULL;
3394 state->conn = conn;
3395 state->ev = ev;
3396 state->timeout_msec = timeout_msec;
3397 state->min_protocol = min_protocol;
3398 state->max_protocol = max_protocol;
3400 if (min_protocol == PROTOCOL_NONE) {
3401 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3402 return tevent_req_post(req, ev);
3405 if (max_protocol == PROTOCOL_NONE) {
3406 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3407 return tevent_req_post(req, ev);
3410 if (min_protocol > max_protocol) {
3411 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
3412 return tevent_req_post(req, ev);
3415 if ((min_protocol < PROTOCOL_SMB2_02) &&
3416 (max_protocol < PROTOCOL_SMB2_02)) {
3418 * SMB1 only...
3420 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
3422 subreq = smbXcli_negprot_smb1_subreq(state);
3423 if (tevent_req_nomem(subreq, req)) {
3424 return tevent_req_post(req, ev);
3426 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
3427 return req;
3430 if ((min_protocol >= PROTOCOL_SMB2_02) &&
3431 (max_protocol >= PROTOCOL_SMB2_02)) {
3433 * SMB2 only...
3435 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
3437 subreq = smbXcli_negprot_smb2_subreq(state);
3438 if (tevent_req_nomem(subreq, req)) {
3439 return tevent_req_post(req, ev);
3441 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
3442 return req;
3446 * We send an SMB1 negprot with the SMB2 dialects
3447 * and expect a SMB1 or a SMB2 response.
3449 * smbXcli_negprot_dispatch_incoming() will fix the
3450 * callback to match protocol of the response.
3452 conn->dispatch_incoming = smbXcli_negprot_dispatch_incoming;
3454 subreq = smbXcli_negprot_smb1_subreq(state);
3455 if (tevent_req_nomem(subreq, req)) {
3456 return tevent_req_post(req, ev);
3458 tevent_req_set_callback(subreq, smbXcli_negprot_invalid_done, req);
3459 return req;
3462 static void smbXcli_negprot_invalid_done(struct tevent_req *subreq)
3464 struct tevent_req *req =
3465 tevent_req_callback_data(subreq,
3466 struct tevent_req);
3467 NTSTATUS status;
3470 * we just want the low level error
3472 status = tevent_req_simple_recv_ntstatus(subreq);
3473 TALLOC_FREE(subreq);
3474 if (tevent_req_nterror(req, status)) {
3475 return;
3478 /* this should never happen */
3479 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
3482 static struct tevent_req *smbXcli_negprot_smb1_subreq(struct smbXcli_negprot_state *state)
3484 size_t i;
3485 DATA_BLOB bytes = data_blob_null;
3486 uint8_t flags;
3487 uint16_t flags2;
3489 /* setup the protocol strings */
3490 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3491 uint8_t c = 2;
3492 bool ok;
3494 if (smb1cli_prots[i].proto < state->min_protocol) {
3495 continue;
3498 if (smb1cli_prots[i].proto > state->max_protocol) {
3499 continue;
3502 ok = data_blob_append(state, &bytes, &c, sizeof(c));
3503 if (!ok) {
3504 return NULL;
3508 * We now it is already ascii and
3509 * we want NULL termination.
3511 ok = data_blob_append(state, &bytes,
3512 smb1cli_prots[i].smb1_name,
3513 strlen(smb1cli_prots[i].smb1_name)+1);
3514 if (!ok) {
3515 return NULL;
3519 smb1cli_req_flags(state->max_protocol,
3520 state->conn->smb1.client.capabilities,
3521 SMBnegprot,
3522 0, 0, &flags,
3523 0, 0, &flags2);
3525 return smb1cli_req_send(state, state->ev, state->conn,
3526 SMBnegprot,
3527 flags, ~flags,
3528 flags2, ~flags2,
3529 state->timeout_msec,
3530 0xFFFE, 0, 0, /* pid, tid, uid */
3531 0, NULL, /* wct, vwv */
3532 bytes.length, bytes.data);
3535 static void smbXcli_negprot_smb1_done(struct tevent_req *subreq)
3537 struct tevent_req *req =
3538 tevent_req_callback_data(subreq,
3539 struct tevent_req);
3540 struct smbXcli_negprot_state *state =
3541 tevent_req_data(req,
3542 struct smbXcli_negprot_state);
3543 struct smbXcli_conn *conn = state->conn;
3544 struct iovec *recv_iov = NULL;
3545 uint8_t *inhdr;
3546 uint8_t wct;
3547 uint16_t *vwv;
3548 uint32_t num_bytes;
3549 uint8_t *bytes;
3550 NTSTATUS status;
3551 uint16_t protnum;
3552 size_t i;
3553 size_t num_prots = 0;
3554 uint8_t flags;
3555 uint32_t client_capabilities = conn->smb1.client.capabilities;
3556 uint32_t both_capabilities;
3557 uint32_t server_capabilities = 0;
3558 uint32_t capabilities;
3559 uint32_t client_max_xmit = conn->smb1.client.max_xmit;
3560 uint32_t server_max_xmit = 0;
3561 uint32_t max_xmit;
3562 uint32_t server_max_mux = 0;
3563 uint16_t server_security_mode = 0;
3564 uint32_t server_session_key = 0;
3565 bool server_readbraw = false;
3566 bool server_writebraw = false;
3567 bool server_lockread = false;
3568 bool server_writeunlock = false;
3569 struct GUID server_guid = GUID_zero();
3570 DATA_BLOB server_gss_blob = data_blob_null;
3571 uint8_t server_challenge[8];
3572 char *server_workgroup = NULL;
3573 char *server_name = NULL;
3574 int server_time_zone = 0;
3575 NTTIME server_system_time = 0;
3576 static const struct smb1cli_req_expected_response expected[] = {
3578 .status = NT_STATUS_OK,
3579 .wct = 0x11, /* NT1 */
3582 .status = NT_STATUS_OK,
3583 .wct = 0x0D, /* LM */
3586 .status = NT_STATUS_OK,
3587 .wct = 0x01, /* CORE */
3591 ZERO_STRUCT(server_challenge);
3593 status = smb1cli_req_recv(subreq, state,
3594 &recv_iov,
3595 &inhdr,
3596 &wct,
3597 &vwv,
3598 NULL, /* pvwv_offset */
3599 &num_bytes,
3600 &bytes,
3601 NULL, /* pbytes_offset */
3602 NULL, /* pinbuf */
3603 expected, ARRAY_SIZE(expected));
3604 TALLOC_FREE(subreq);
3605 if (tevent_req_nterror(req, status)) {
3606 return;
3609 flags = CVAL(inhdr, HDR_FLG);
3611 protnum = SVAL(vwv, 0);
3613 for (i=0; i < ARRAY_SIZE(smb1cli_prots); i++) {
3614 if (smb1cli_prots[i].proto < state->min_protocol) {
3615 continue;
3618 if (smb1cli_prots[i].proto > state->max_protocol) {
3619 continue;
3622 if (protnum != num_prots) {
3623 num_prots++;
3624 continue;
3627 conn->protocol = smb1cli_prots[i].proto;
3628 break;
3631 if (conn->protocol == PROTOCOL_NONE) {
3632 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3633 return;
3636 if ((conn->protocol < PROTOCOL_NT1) && conn->mandatory_signing) {
3637 DEBUG(0,("smbXcli_negprot: SMB signing is mandatory "
3638 "and the selected protocol level doesn't support it.\n"));
3639 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3640 return;
3643 if (flags & FLAG_SUPPORT_LOCKREAD) {
3644 server_lockread = true;
3645 server_writeunlock = true;
3648 if (conn->protocol >= PROTOCOL_NT1) {
3649 const char *client_signing = NULL;
3650 bool server_mandatory = false;
3651 bool server_allowed = false;
3652 const char *server_signing = NULL;
3653 bool ok;
3654 uint8_t key_len;
3656 if (wct != 0x11) {
3657 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3658 return;
3661 /* NT protocol */
3662 server_security_mode = CVAL(vwv + 1, 0);
3663 server_max_mux = SVAL(vwv + 1, 1);
3664 server_max_xmit = IVAL(vwv + 3, 1);
3665 server_session_key = IVAL(vwv + 7, 1);
3666 server_time_zone = SVALS(vwv + 15, 1);
3667 server_time_zone *= 60;
3668 /* this time arrives in real GMT */
3669 server_system_time = BVAL(vwv + 11, 1);
3670 server_capabilities = IVAL(vwv + 9, 1);
3672 key_len = CVAL(vwv + 16, 1);
3674 if (server_capabilities & CAP_RAW_MODE) {
3675 server_readbraw = true;
3676 server_writebraw = true;
3678 if (server_capabilities & CAP_LOCK_AND_READ) {
3679 server_lockread = true;
3682 if (server_capabilities & CAP_EXTENDED_SECURITY) {
3683 DATA_BLOB blob1, blob2;
3685 if (num_bytes < 16) {
3686 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3687 return;
3690 blob1 = data_blob_const(bytes, 16);
3691 status = GUID_from_data_blob(&blob1, &server_guid);
3692 if (tevent_req_nterror(req, status)) {
3693 return;
3696 blob1 = data_blob_const(bytes+16, num_bytes-16);
3697 blob2 = data_blob_dup_talloc(state, blob1);
3698 if (blob1.length > 0 &&
3699 tevent_req_nomem(blob2.data, req)) {
3700 return;
3702 server_gss_blob = blob2;
3703 } else {
3704 DATA_BLOB blob1, blob2;
3706 if (num_bytes < key_len) {
3707 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3708 return;
3711 if (key_len != 0 && key_len != 8) {
3712 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3713 return;
3716 if (key_len == 8) {
3717 memcpy(server_challenge, bytes, 8);
3720 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3721 blob2 = data_blob_const(bytes+key_len, num_bytes-key_len);
3722 if (blob1.length > 0) {
3723 size_t len;
3725 len = utf16_len_n(blob1.data,
3726 blob1.length);
3727 blob1.length = len;
3729 ok = convert_string_talloc(state,
3730 CH_UTF16LE,
3731 CH_UNIX,
3732 blob1.data,
3733 blob1.length,
3734 &server_workgroup,
3735 &len);
3736 if (!ok) {
3737 status = map_nt_error_from_unix_common(errno);
3738 tevent_req_nterror(req, status);
3739 return;
3743 blob2.data += blob1.length;
3744 blob2.length -= blob1.length;
3745 if (blob2.length > 0) {
3746 size_t len;
3748 len = utf16_len_n(blob1.data,
3749 blob1.length);
3750 blob1.length = len;
3752 ok = convert_string_talloc(state,
3753 CH_UTF16LE,
3754 CH_UNIX,
3755 blob2.data,
3756 blob2.length,
3757 &server_name,
3758 &len);
3759 if (!ok) {
3760 status = map_nt_error_from_unix_common(errno);
3761 tevent_req_nterror(req, status);
3762 return;
3767 client_signing = "disabled";
3768 if (conn->allow_signing) {
3769 client_signing = "allowed";
3771 if (conn->mandatory_signing) {
3772 client_signing = "required";
3775 server_signing = "not supported";
3776 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_ENABLED) {
3777 server_signing = "supported";
3778 server_allowed = true;
3780 if (server_security_mode & NEGOTIATE_SECURITY_SIGNATURES_REQUIRED) {
3781 server_signing = "required";
3782 server_mandatory = true;
3785 ok = smb_signing_set_negotiated(conn->smb1.signing,
3786 server_allowed,
3787 server_mandatory);
3788 if (!ok) {
3789 DEBUG(1,("cli_negprot: SMB signing is required, "
3790 "but client[%s] and server[%s] mismatch\n",
3791 client_signing, server_signing));
3792 tevent_req_nterror(req, NT_STATUS_ACCESS_DENIED);
3793 return;
3796 } else if (conn->protocol >= PROTOCOL_LANMAN1) {
3797 DATA_BLOB blob1;
3798 uint8_t key_len;
3799 time_t t;
3801 if (wct != 0x0D) {
3802 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3803 return;
3806 server_security_mode = SVAL(vwv + 1, 0);
3807 server_max_xmit = SVAL(vwv + 2, 0);
3808 server_max_mux = SVAL(vwv + 3, 0);
3809 server_readbraw = ((SVAL(vwv + 5, 0) & 0x1) != 0);
3810 server_writebraw = ((SVAL(vwv + 5, 0) & 0x2) != 0);
3811 server_session_key = IVAL(vwv + 6, 0);
3812 server_time_zone = SVALS(vwv + 10, 0);
3813 server_time_zone *= 60;
3814 /* this time is converted to GMT by make_unix_date */
3815 t = pull_dos_date((const uint8_t *)(vwv + 8), server_time_zone);
3816 unix_to_nt_time(&server_system_time, t);
3817 key_len = SVAL(vwv + 11, 0);
3819 if (num_bytes < key_len) {
3820 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3821 return;
3824 if (key_len != 0 && key_len != 8) {
3825 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3826 return;
3829 if (key_len == 8) {
3830 memcpy(server_challenge, bytes, 8);
3833 blob1 = data_blob_const(bytes+key_len, num_bytes-key_len);
3834 if (blob1.length > 0) {
3835 size_t len;
3836 bool ok;
3838 len = utf16_len_n(blob1.data,
3839 blob1.length);
3840 blob1.length = len;
3842 ok = convert_string_talloc(state,
3843 CH_DOS,
3844 CH_UNIX,
3845 blob1.data,
3846 blob1.length,
3847 &server_workgroup,
3848 &len);
3849 if (!ok) {
3850 status = map_nt_error_from_unix_common(errno);
3851 tevent_req_nterror(req, status);
3852 return;
3856 } else {
3857 /* the old core protocol */
3858 server_time_zone = get_time_zone(time(NULL));
3859 server_max_xmit = 1024;
3860 server_max_mux = 1;
3863 if (server_max_xmit < 1024) {
3864 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3865 return;
3868 if (server_max_mux < 1) {
3869 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
3870 return;
3874 * Now calculate the negotiated capabilities
3875 * based on the mask for:
3876 * - client only flags
3877 * - flags used in both directions
3878 * - server only flags
3880 both_capabilities = client_capabilities & server_capabilities;
3881 capabilities = client_capabilities & SMB_CAP_CLIENT_MASK;
3882 capabilities |= both_capabilities & SMB_CAP_BOTH_MASK;
3883 capabilities |= server_capabilities & SMB_CAP_SERVER_MASK;
3885 max_xmit = MIN(client_max_xmit, server_max_xmit);
3887 conn->smb1.server.capabilities = server_capabilities;
3888 conn->smb1.capabilities = capabilities;
3890 conn->smb1.server.max_xmit = server_max_xmit;
3891 conn->smb1.max_xmit = max_xmit;
3893 conn->smb1.server.max_mux = server_max_mux;
3895 conn->smb1.server.security_mode = server_security_mode;
3897 conn->smb1.server.readbraw = server_readbraw;
3898 conn->smb1.server.writebraw = server_writebraw;
3899 conn->smb1.server.lockread = server_lockread;
3900 conn->smb1.server.writeunlock = server_writeunlock;
3902 conn->smb1.server.session_key = server_session_key;
3904 talloc_steal(conn, server_gss_blob.data);
3905 conn->smb1.server.gss_blob = server_gss_blob;
3906 conn->smb1.server.guid = server_guid;
3907 memcpy(conn->smb1.server.challenge, server_challenge, 8);
3908 conn->smb1.server.workgroup = talloc_move(conn, &server_workgroup);
3909 conn->smb1.server.name = talloc_move(conn, &server_name);
3911 conn->smb1.server.time_zone = server_time_zone;
3912 conn->smb1.server.system_time = server_system_time;
3914 tevent_req_done(req);
3917 static struct tevent_req *smbXcli_negprot_smb2_subreq(struct smbXcli_negprot_state *state)
3919 size_t i;
3920 uint8_t *buf;
3921 uint16_t dialect_count = 0;
3923 buf = state->smb2.dyn;
3924 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
3925 if (smb2cli_prots[i].proto < state->min_protocol) {
3926 continue;
3929 if (smb2cli_prots[i].proto > state->max_protocol) {
3930 continue;
3933 SSVAL(buf, dialect_count*2, smb2cli_prots[i].smb2_dialect);
3934 dialect_count++;
3937 buf = state->smb2.fixed;
3938 SSVAL(buf, 0, 36);
3939 SSVAL(buf, 2, dialect_count);
3940 SSVAL(buf, 4, state->conn->smb2.client.security_mode);
3941 SSVAL(buf, 6, 0); /* Reserved */
3942 if (state->max_protocol >= PROTOCOL_SMB2_22) {
3943 SIVAL(buf, 8, state->conn->smb2.client.capabilities);
3944 } else {
3945 SIVAL(buf, 8, 0); /* Capabilities */
3947 if (state->max_protocol >= PROTOCOL_SMB2_10) {
3948 NTSTATUS status;
3949 DATA_BLOB blob;
3951 status = GUID_to_ndr_blob(&state->conn->smb2.client.guid,
3952 state, &blob);
3953 if (!NT_STATUS_IS_OK(status)) {
3954 return NULL;
3956 memcpy(buf+12, blob.data, 16); /* ClientGuid */
3957 } else {
3958 memset(buf+12, 0, 16); /* ClientGuid */
3960 SBVAL(buf, 28, 0); /* ClientStartTime */
3962 return smb2cli_req_send(state, state->ev,
3963 state->conn, SMB2_OP_NEGPROT,
3964 0, 0, /* flags */
3965 state->timeout_msec,
3966 0xFEFF, 0, NULL, /* pid, tid, session */
3967 state->smb2.fixed, sizeof(state->smb2.fixed),
3968 state->smb2.dyn, dialect_count*2);
3971 static void smbXcli_negprot_smb2_done(struct tevent_req *subreq)
3973 struct tevent_req *req =
3974 tevent_req_callback_data(subreq,
3975 struct tevent_req);
3976 struct smbXcli_negprot_state *state =
3977 tevent_req_data(req,
3978 struct smbXcli_negprot_state);
3979 struct smbXcli_conn *conn = state->conn;
3980 size_t security_offset, security_length;
3981 DATA_BLOB blob;
3982 NTSTATUS status;
3983 struct iovec *iov;
3984 uint8_t *body;
3985 size_t i;
3986 uint16_t dialect_revision;
3987 static const struct smb2cli_req_expected_response expected[] = {
3989 .status = NT_STATUS_OK,
3990 .body_size = 0x41
3994 status = smb2cli_req_recv(subreq, state, &iov,
3995 expected, ARRAY_SIZE(expected));
3996 TALLOC_FREE(subreq);
3997 if (tevent_req_nterror(req, status)) {
3998 return;
4001 body = (uint8_t *)iov[1].iov_base;
4003 dialect_revision = SVAL(body, 4);
4005 for (i=0; i < ARRAY_SIZE(smb2cli_prots); i++) {
4006 if (smb2cli_prots[i].proto < state->min_protocol) {
4007 continue;
4010 if (smb2cli_prots[i].proto > state->max_protocol) {
4011 continue;
4014 if (smb2cli_prots[i].smb2_dialect != dialect_revision) {
4015 continue;
4018 conn->protocol = smb2cli_prots[i].proto;
4019 break;
4022 if (conn->protocol == PROTOCOL_NONE) {
4023 if (state->min_protocol >= PROTOCOL_SMB2_02) {
4024 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4025 return;
4028 if (dialect_revision != SMB2_DIALECT_REVISION_2FF) {
4029 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4030 return;
4033 /* make sure we do not loop forever */
4034 state->min_protocol = PROTOCOL_SMB2_02;
4037 * send a SMB2 negprot, in order to negotiate
4038 * the SMB2 dialect.
4040 subreq = smbXcli_negprot_smb2_subreq(state);
4041 if (tevent_req_nomem(subreq, req)) {
4042 return;
4044 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4045 return;
4048 conn->smb2.server.security_mode = SVAL(body, 2);
4050 blob = data_blob_const(body + 8, 16);
4051 status = GUID_from_data_blob(&blob, &conn->smb2.server.guid);
4052 if (tevent_req_nterror(req, status)) {
4053 return;
4056 conn->smb2.server.capabilities = IVAL(body, 24);
4057 conn->smb2.server.max_trans_size= IVAL(body, 28);
4058 conn->smb2.server.max_read_size = IVAL(body, 32);
4059 conn->smb2.server.max_write_size= IVAL(body, 36);
4060 conn->smb2.server.system_time = BVAL(body, 40);
4061 conn->smb2.server.start_time = BVAL(body, 48);
4063 security_offset = SVAL(body, 56);
4064 security_length = SVAL(body, 58);
4066 if (security_offset != SMB2_HDR_BODY + iov[1].iov_len) {
4067 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4068 return;
4071 if (security_length > iov[2].iov_len) {
4072 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4073 return;
4076 conn->smb2.server.gss_blob = data_blob_talloc(conn,
4077 iov[2].iov_base,
4078 security_length);
4079 if (tevent_req_nomem(conn->smb2.server.gss_blob.data, req)) {
4080 return;
4083 tevent_req_done(req);
4086 static NTSTATUS smbXcli_negprot_dispatch_incoming(struct smbXcli_conn *conn,
4087 TALLOC_CTX *tmp_mem,
4088 uint8_t *inbuf)
4090 size_t num_pending = talloc_array_length(conn->pending);
4091 struct tevent_req *subreq;
4092 struct smbXcli_req_state *substate;
4093 struct tevent_req *req;
4094 uint32_t protocol_magic;
4095 size_t inbuf_len = smb_len_nbt(inbuf);
4097 if (num_pending != 1) {
4098 return NT_STATUS_INTERNAL_ERROR;
4101 if (inbuf_len < 4) {
4102 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4105 subreq = conn->pending[0];
4106 substate = tevent_req_data(subreq, struct smbXcli_req_state);
4107 req = tevent_req_callback_data(subreq, struct tevent_req);
4109 protocol_magic = IVAL(inbuf, 4);
4111 switch (protocol_magic) {
4112 case SMB_MAGIC:
4113 tevent_req_set_callback(subreq, smbXcli_negprot_smb1_done, req);
4114 conn->dispatch_incoming = smb1cli_conn_dispatch_incoming;
4115 return smb1cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4117 case SMB2_MAGIC:
4118 if (substate->smb2.recv_iov == NULL) {
4120 * For the SMB1 negprot we have move it.
4122 substate->smb2.recv_iov = substate->smb1.recv_iov;
4123 substate->smb1.recv_iov = NULL;
4127 * we got an SMB2 answer, which consumed sequence number 0
4128 * so we need to use 1 as the next one
4130 conn->smb2.mid = 1;
4131 tevent_req_set_callback(subreq, smbXcli_negprot_smb2_done, req);
4132 conn->dispatch_incoming = smb2cli_conn_dispatch_incoming;
4133 return smb2cli_conn_dispatch_incoming(conn, tmp_mem, inbuf);
4136 DEBUG(10, ("Got non-SMB PDU\n"));
4137 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4140 NTSTATUS smbXcli_negprot_recv(struct tevent_req *req)
4142 return tevent_req_simple_recv_ntstatus(req);
4145 NTSTATUS smbXcli_negprot(struct smbXcli_conn *conn,
4146 uint32_t timeout_msec,
4147 enum protocol_types min_protocol,
4148 enum protocol_types max_protocol)
4150 TALLOC_CTX *frame = talloc_stackframe();
4151 struct tevent_context *ev;
4152 struct tevent_req *req;
4153 NTSTATUS status = NT_STATUS_NO_MEMORY;
4154 bool ok;
4156 if (smbXcli_conn_has_async_calls(conn)) {
4158 * Can't use sync call while an async call is in flight
4160 status = NT_STATUS_INVALID_PARAMETER_MIX;
4161 goto fail;
4163 ev = tevent_context_init(frame);
4164 if (ev == NULL) {
4165 goto fail;
4167 req = smbXcli_negprot_send(frame, ev, conn, timeout_msec,
4168 min_protocol, max_protocol);
4169 if (req == NULL) {
4170 goto fail;
4172 ok = tevent_req_poll(req, ev);
4173 if (!ok) {
4174 status = map_nt_error_from_unix_common(errno);
4175 goto fail;
4177 status = smbXcli_negprot_recv(req);
4178 fail:
4179 TALLOC_FREE(frame);
4180 return status;
4183 static int smbXcli_session_destructor(struct smbXcli_session *session)
4185 if (session->conn == NULL) {
4186 return 0;
4189 DLIST_REMOVE(session->conn->sessions, session);
4190 return 0;
4193 struct smbXcli_session *smbXcli_session_create(TALLOC_CTX *mem_ctx,
4194 struct smbXcli_conn *conn)
4196 struct smbXcli_session *session;
4198 session = talloc_zero(mem_ctx, struct smbXcli_session);
4199 if (session == NULL) {
4200 return NULL;
4202 talloc_set_destructor(session, smbXcli_session_destructor);
4204 DLIST_ADD_END(conn->sessions, session, struct smbXcli_session *);
4205 session->conn = conn;
4207 return session;
4210 uint8_t smb2cli_session_security_mode(struct smbXcli_session *session)
4212 struct smbXcli_conn *conn = session->conn;
4213 uint8_t security_mode = 0;
4215 if (conn == NULL) {
4216 return security_mode;
4219 security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
4220 if (conn->mandatory_signing) {
4221 security_mode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
4224 return security_mode;
4227 uint64_t smb2cli_session_current_id(struct smbXcli_session *session)
4229 return session->smb2.session_id;
4232 uint16_t smb2cli_session_get_flags(struct smbXcli_session *session)
4234 return session->smb2.session_flags;
4237 NTSTATUS smb2cli_session_application_key(struct smbXcli_session *session,
4238 TALLOC_CTX *mem_ctx,
4239 DATA_BLOB *key)
4241 *key = data_blob_null;
4243 if (session->smb2.application_key.length == 0) {
4244 return NT_STATUS_NO_USER_SESSION_KEY;
4247 *key = data_blob_dup_talloc(mem_ctx, session->smb2.application_key);
4248 if (key->data == NULL) {
4249 return NT_STATUS_NO_MEMORY;
4252 return NT_STATUS_OK;
4255 void smb2cli_session_set_id_and_flags(struct smbXcli_session *session,
4256 uint64_t session_id,
4257 uint16_t session_flags)
4259 session->smb2.session_id = session_id;
4260 session->smb2.session_flags = session_flags;
4263 NTSTATUS smb2cli_session_set_session_key(struct smbXcli_session *session,
4264 const DATA_BLOB _session_key,
4265 const struct iovec *recv_iov)
4267 struct smbXcli_conn *conn = session->conn;
4268 uint16_t no_sign_flags;
4269 uint8_t session_key[16];
4270 NTSTATUS status;
4272 if (conn == NULL) {
4273 return NT_STATUS_INVALID_PARAMETER_MIX;
4276 no_sign_flags = SMB2_SESSION_FLAG_IS_GUEST | SMB2_SESSION_FLAG_IS_NULL;
4278 if (session->smb2.session_flags & no_sign_flags) {
4279 session->smb2.should_sign = false;
4280 return NT_STATUS_OK;
4283 if (session->smb2.signing_key.length != 0) {
4284 return NT_STATUS_INVALID_PARAMETER_MIX;
4287 ZERO_STRUCT(session_key);
4288 memcpy(session_key, _session_key.data,
4289 MIN(_session_key.length, sizeof(session_key)));
4291 session->smb2.signing_key = data_blob_talloc(session,
4292 session_key,
4293 sizeof(session_key));
4294 if (session->smb2.signing_key.data == NULL) {
4295 ZERO_STRUCT(session_key);
4296 return NT_STATUS_NO_MEMORY;
4299 if (conn->protocol >= PROTOCOL_SMB2_24) {
4300 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4301 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4303 smb2_key_derivation(session_key, sizeof(session_key),
4304 label.data, label.length,
4305 context.data, context.length,
4306 session->smb2.signing_key.data);
4309 session->smb2.encryption_key = data_blob_dup_talloc(session,
4310 session->smb2.signing_key);
4311 if (session->smb2.encryption_key.data == NULL) {
4312 ZERO_STRUCT(session_key);
4313 return NT_STATUS_NO_MEMORY;
4316 if (conn->protocol >= PROTOCOL_SMB2_24) {
4317 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4318 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
4320 smb2_key_derivation(session_key, sizeof(session_key),
4321 label.data, label.length,
4322 context.data, context.length,
4323 session->smb2.encryption_key.data);
4326 session->smb2.decryption_key = data_blob_dup_talloc(session,
4327 session->smb2.signing_key);
4328 if (session->smb2.decryption_key.data == NULL) {
4329 ZERO_STRUCT(session_key);
4330 return NT_STATUS_NO_MEMORY;
4333 if (conn->protocol >= PROTOCOL_SMB2_24) {
4334 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
4335 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
4337 smb2_key_derivation(session_key, sizeof(session_key),
4338 label.data, label.length,
4339 context.data, context.length,
4340 session->smb2.decryption_key.data);
4343 session->smb2.application_key = data_blob_dup_talloc(session,
4344 session->smb2.signing_key);
4345 if (session->smb2.application_key.data == NULL) {
4346 ZERO_STRUCT(session_key);
4347 return NT_STATUS_NO_MEMORY;
4350 if (conn->protocol >= PROTOCOL_SMB2_24) {
4351 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
4352 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
4354 smb2_key_derivation(session_key, sizeof(session_key),
4355 label.data, label.length,
4356 context.data, context.length,
4357 session->smb2.application_key.data);
4359 ZERO_STRUCT(session_key);
4361 session->smb2.channel_signing_key = data_blob_dup_talloc(session,
4362 session->smb2.signing_key);
4363 if (session->smb2.channel_signing_key.data == NULL) {
4364 return NT_STATUS_NO_MEMORY;
4367 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4368 session->conn->protocol,
4369 recv_iov, 3);
4370 if (!NT_STATUS_IS_OK(status)) {
4371 return status;
4374 session->smb2.should_sign = false;
4375 session->smb2.should_encrypt = false;
4377 if (conn->desire_signing) {
4378 session->smb2.should_sign = true;
4381 if (conn->smb2.server.security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) {
4382 session->smb2.should_sign = true;
4385 generate_random_buffer((uint8_t *)&session->smb2.channel_nonce,
4386 sizeof(session->smb2.channel_nonce));
4387 session->smb2.channel_next = 1;
4389 return NT_STATUS_OK;
4392 NTSTATUS smb2cli_session_create_channel(TALLOC_CTX *mem_ctx,
4393 struct smbXcli_session *session1,
4394 struct smbXcli_conn *conn,
4395 struct smbXcli_session **_session2)
4397 struct smbXcli_session *session2;
4399 if (session1->smb2.signing_key.length == 0) {
4400 return NT_STATUS_INVALID_PARAMETER_MIX;
4403 if (session1->smb2.channel_next == 0) {
4404 return NT_STATUS_INVALID_PARAMETER_MIX;
4407 if (conn == NULL) {
4408 return NT_STATUS_INVALID_PARAMETER_MIX;
4411 session2 = talloc_zero(mem_ctx, struct smbXcli_session);
4412 if (session2 == NULL) {
4413 return NT_STATUS_NO_MEMORY;
4415 session2->smb2.session_id = session1->smb2.session_id;
4416 session2->smb2.session_flags = session1->smb2.session_flags;
4418 session2->smb2.signing_key = data_blob_dup_talloc(session2,
4419 session1->smb2.signing_key);
4420 if (session2->smb2.signing_key.data == NULL) {
4421 return NT_STATUS_NO_MEMORY;
4424 session2->smb2.application_key = data_blob_dup_talloc(session2,
4425 session1->smb2.application_key);
4426 if (session2->smb2.application_key.data == NULL) {
4427 return NT_STATUS_NO_MEMORY;
4430 session2->smb2.should_sign = session1->smb2.should_sign;
4431 session2->smb2.should_encrypt = session1->smb2.should_encrypt;
4433 session2->smb2.channel_nonce = session1->smb2.channel_nonce;
4434 session2->smb2.channel_nonce += session1->smb2.channel_next;
4435 session1->smb2.channel_next++;
4437 session2->smb2.encryption_key = data_blob_dup_talloc(session2,
4438 session1->smb2.encryption_key);
4439 if (session2->smb2.encryption_key.data == NULL) {
4440 return NT_STATUS_NO_MEMORY;
4443 session2->smb2.decryption_key = data_blob_dup_talloc(session2,
4444 session1->smb2.decryption_key);
4445 if (session2->smb2.decryption_key.data == NULL) {
4446 return NT_STATUS_NO_MEMORY;
4449 talloc_set_destructor(session2, smbXcli_session_destructor);
4450 DLIST_ADD_END(conn->sessions, session2, struct smbXcli_session *);
4451 session2->conn = conn;
4453 *_session2 = session2;
4454 return NT_STATUS_OK;
4457 NTSTATUS smb2cli_session_set_channel_key(struct smbXcli_session *session,
4458 const DATA_BLOB _channel_key,
4459 const struct iovec *recv_iov)
4461 struct smbXcli_conn *conn = session->conn;
4462 uint8_t channel_key[16];
4463 NTSTATUS status;
4465 if (conn == NULL) {
4466 return NT_STATUS_INVALID_PARAMETER_MIX;
4469 if (session->smb2.channel_signing_key.length != 0) {
4470 return NT_STATUS_INVALID_PARAMETER_MIX;
4473 ZERO_STRUCT(channel_key);
4474 memcpy(channel_key, _channel_key.data,
4475 MIN(_channel_key.length, sizeof(channel_key)));
4477 session->smb2.channel_signing_key = data_blob_talloc(session,
4478 channel_key,
4479 sizeof(channel_key));
4480 if (session->smb2.channel_signing_key.data == NULL) {
4481 ZERO_STRUCT(channel_key);
4482 return NT_STATUS_NO_MEMORY;
4485 if (conn->protocol >= PROTOCOL_SMB2_24) {
4486 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
4487 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
4489 smb2_key_derivation(channel_key, sizeof(channel_key),
4490 label.data, label.length,
4491 context.data, context.length,
4492 session->smb2.channel_signing_key.data);
4494 ZERO_STRUCT(channel_key);
4496 status = smb2_signing_check_pdu(session->smb2.channel_signing_key,
4497 session->conn->protocol,
4498 recv_iov, 3);
4499 if (!NT_STATUS_IS_OK(status)) {
4500 return status;
4503 return NT_STATUS_OK;